From ae5cb2c58195d34b1b189fe9de543fd9cc243227 Mon Sep 17 00:00:00 2001 From: Stephen Ma Date: Thu, 30 Sep 2010 13:22:28 +1000 Subject: [PATCH] http: fix http handler signature changes previously missed R=adg, rsc CC=golang-dev https://golang.org/cl/2303041 --- src/pkg/http/server.go | 2 +- src/pkg/http/triv.go | 50 +++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/pkg/http/server.go b/src/pkg/http/server.go index 4404dda978b..03653ef87df 100644 --- a/src/pkg/http/server.go +++ b/src/pkg/http/server.go @@ -426,7 +426,7 @@ func (w *response) Hijack() (rwc io.ReadWriteCloser, buf *bufio.ReadWriter, err // Handler object that calls f. type HandlerFunc func(ResponseWriter, *Request) -// ServeHTTP calls f(c, req). +// ServeHTTP calls f(w, req). func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) } diff --git a/src/pkg/http/triv.go b/src/pkg/http/triv.go index 957ca5f94e4..23823a792c0 100644 --- a/src/pkg/http/triv.go +++ b/src/pkg/http/triv.go @@ -20,9 +20,9 @@ import ( // hello world, the web server var helloRequests = expvar.NewInt("hello-requests") -func HelloServer(c *http.Conn, req *http.Request) { +func HelloServer(w http.ResponseWriter, req *http.Request) { helloRequests.Add(1) - io.WriteString(c, "hello, world!\n") + io.WriteString(w, "hello, world!\n") } // Simple counter server. POSTing to it will set the value. @@ -34,7 +34,7 @@ type Counter struct { // it directly. func (ctr *Counter) String() string { return fmt.Sprintf("%d", ctr.n) } -func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) { +func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) { switch req.Method { case "GET": ctr.n++ @@ -43,34 +43,34 @@ func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) { io.Copy(buf, req.Body) body := buf.String() if n, err := strconv.Atoi(body); err != nil { - fmt.Fprintf(c, "bad POST: %v\nbody: [%v]\n", err, body) + fmt.Fprintf(w, "bad POST: %v\nbody: [%v]\n", err, body) } else { ctr.n = n - fmt.Fprint(c, "counter reset\n") + fmt.Fprint(w, "counter reset\n") } } - fmt.Fprintf(c, "counter = %d\n", ctr.n) + fmt.Fprintf(w, "counter = %d\n", ctr.n) } // simple flag server var booleanflag = flag.Bool("boolean", true, "another flag for testing") -func FlagServer(c *http.Conn, req *http.Request) { - c.SetHeader("content-type", "text/plain; charset=utf-8") - fmt.Fprint(c, "Flags:\n") +func FlagServer(w http.ResponseWriter, req *http.Request) { + w.SetHeader("content-type", "text/plain; charset=utf-8") + fmt.Fprint(w, "Flags:\n") flag.VisitAll(func(f *flag.Flag) { if f.Value.String() != f.DefValue { - fmt.Fprintf(c, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue) + fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue) } else { - fmt.Fprintf(c, "%s = %s\n", f.Name, f.Value.String()) + fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String()) } }) } // simple argument server -func ArgServer(c *http.Conn, req *http.Request) { +func ArgServer(w http.ResponseWriter, req *http.Request) { for _, s := range os.Args { - fmt.Fprint(c, s, " ") + fmt.Fprint(w, s, " ") } } @@ -87,41 +87,41 @@ func ChanCreate() Chan { return c } -func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) { - io.WriteString(c, fmt.Sprintf("channel send #%d\n", <-ch)) +func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, 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") +func DateServer(rw http.ResponseWriter, req *http.Request) { + rw.SetHeader("content-type", "text/plain; charset=utf-8") r, w, err := os.Pipe() if err != nil { - fmt.Fprintf(c, "pipe: %s\n", err) + fmt.Fprintf(rw, "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) + fmt.Fprintf(rw, "fork/exec: %s\n", err) return } - io.Copy(c, r) + io.Copy(rw, r) wait, err := os.Wait(pid, 0) if err != nil { - fmt.Fprintf(c, "wait: %s\n", err) + fmt.Fprintf(rw, "wait: %s\n", err) return } if !wait.Exited() || wait.ExitStatus() != 0 { - fmt.Fprintf(c, "date: %v\n", wait) + fmt.Fprintf(rw, "date: %v\n", wait) return } } -func Logger(c *http.Conn, req *http.Request) { +func Logger(w http.ResponseWriter, req *http.Request) { log.Stdout(req.URL.Raw) - c.WriteHeader(404) - c.Write([]byte("oops")) + w.WriteHeader(404) + w.Write([]byte("oops")) }