diff options
| author | Russ Cox <rsc@golang.org> | 2009-06-08 14:03:21 -0700 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2009-06-08 14:03:21 -0700 |
| commit | cd80000d8d0e3804761407e94e7919bf613566b7 (patch) | |
| tree | 49a9b6287ffa031432c5e020116888a0a5b76b78 /src/lib/http | |
| parent | 80ca2afd08b61180fe7a58e0a93b05be4ab69a4e (diff) | |
| download | go-cd80000d8d0e3804761407e94e7919bf613566b7.tar.xz | |
add exec example to http triv.go.
fix darwin interrupt bug (race with SIGCHLD).
R=gri
DELTA=46 (40 added, 0 deleted, 6 changed)
OCL=30052
CL=30057
Diffstat (limited to 'src/lib/http')
| -rw-r--r-- | src/lib/http/triv.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/lib/http/triv.go b/src/lib/http/triv.go index 8528984904..fc95017697 100644 --- a/src/lib/http/triv.go +++ b/src/lib/http/triv.go @@ -110,6 +110,33 @@ func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) { io.WriteString(c, fmt.Sprintf("channel send #%d\n", <-ch)); } +// exec a program, redirecting output +func DateServer(c *http.Conn, req *http.Request) { + c.SetHeader("content-type", "text/plain; charset=utf-8"); + r, w, err := os.Pipe(); + if err != nil { + fmt.Fprintf(c, "pipe: %s\n", err); + return; + } + pid, err := os.ForkExec("/bin/date", []string{"date"}, os.Environ(), "", []*os.File{nil, w, w}); + defer r.Close(); + w.Close(); + if err != nil { + fmt.Fprintf(c, "fork/exec: %s\n", err); + return; + } + io.Copy(r, c); + wait, err := os.Wait(pid, 0); + if err != nil { + fmt.Fprintf(c, "wait: %s\n", err); + return; + } + if !wait.Exited() || wait.ExitStatus() != 0 { + fmt.Fprintf(c, "date: %v\n", wait); + return; + } +} + func main() { flag.Parse(); @@ -123,6 +150,7 @@ func main() { http.Handle("/args", http.HandlerFunc(ArgServer)); http.Handle("/go/hello", http.HandlerFunc(HelloServer)); http.Handle("/chan", ChanCreate()); + http.Handle("/date", http.HandlerFunc(DateServer)); err := http.ListenAndServe(":12345", nil); if err != nil { log.Crash("ListenAndServe: ", err) |
