1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:44:44 -07:00

net/http: ensure triv.go compiles and runs

R=golang-dev, bradfitz, dsymonds, dave, r
CC=golang-dev
https://golang.org/cl/5795069
This commit is contained in:
Robert Hencke 2012-03-14 20:25:57 -07:00 committed by Brad Fitzpatrick
parent 181dc14cd6
commit 1c224ab9dd

View File

@ -15,6 +15,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"strconv" "strconv"
) )
@ -95,35 +96,18 @@ func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// exec a program, redirecting output // exec a program, redirecting output
func DateServer(rw http.ResponseWriter, req *http.Request) { func DateServer(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "text/plain; charset=utf-8") rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
r, w, err := os.Pipe()
if err != nil {
fmt.Fprintf(rw, "pipe: %s\n", err)
return
}
p, err := os.StartProcess("/bin/date", []string{"date"}, &os.ProcAttr{Files: []*os.File{nil, w, w}}) date, err := exec.Command("/bin/date").Output()
defer r.Close()
w.Close()
if err != nil { if err != nil {
fmt.Fprintf(rw, "fork/exec: %s\n", err) http.Error(rw, err.Error(), 500)
return
}
io.Copy(rw, r)
wait, err := p.Wait(0)
if err != nil {
fmt.Fprintf(rw, "wait: %s\n", err)
return
}
if !wait.Exited() || wait.ExitStatus() != 0 {
fmt.Fprintf(rw, "date: %v\n", wait)
return return
} }
rw.Write(date)
} }
func Logger(w http.ResponseWriter, req *http.Request) { func Logger(w http.ResponseWriter, req *http.Request) {
log.Print(req.URL.Raw) log.Print(req.URL)
w.WriteHeader(404) http.Error(w, "oops", 404)
w.Write([]byte("oops"))
} }
var webroot = flag.String("root", "/home/rsc", "web root directory") var webroot = flag.String("root", "/home/rsc", "web root directory")