summaryrefslogtreecommitdiff
path: root/src/pkg/http/triv.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/http/triv.go')
-rw-r--r--src/pkg/http/triv.go77
1 files changed, 30 insertions, 47 deletions
diff --git a/src/pkg/http/triv.go b/src/pkg/http/triv.go
index 612b6161e..03cfafa7b 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,53 +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)
-}
-
-// simple file server
-var webroot = flag.String("root", "/home/rsc", "web root directory")
-var pathVar = expvar.NewMap("file-requests")
-
-func FileServer(c *http.Conn, req *http.Request) {
- c.SetHeader("content-type", "text/plain; charset=utf-8")
- pathVar.Add(req.URL.Path, 1)
- path := *webroot + req.URL.Path // TODO: insecure: use os.CleanName
- f, err := os.Open(path, os.O_RDONLY, 0)
- if err != nil {
- c.WriteHeader(http.StatusNotFound)
- fmt.Fprintf(c, "open %s: %v\n", path, err)
- return
- }
- n, _ := io.Copy(c, f)
- fmt.Fprintf(c, "[%d bytes]\n", n)
- f.Close()
+ 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, " ")
}
}
@@ -106,44 +87,46 @@ 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) {
- log.Stdout(req.URL.Raw)
- c.WriteHeader(404)
- c.Write([]byte("oops"))
+func Logger(w http.ResponseWriter, req *http.Request) {
+ log.Print(req.URL.Raw)
+ w.WriteHeader(404)
+ w.Write([]byte("oops"))
}
+var webroot = flag.String("root", "/home/rsc", "web root directory")
+
func main() {
flag.Parse()
@@ -153,7 +136,7 @@ func main() {
expvar.Publish("counter", ctr)
http.Handle("/", http.HandlerFunc(Logger))
- http.Handle("/go/", http.HandlerFunc(FileServer))
+ http.Handle("/go/", http.FileServer(*webroot, "/go/"))
http.Handle("/flags", http.HandlerFunc(FlagServer))
http.Handle("/args", http.HandlerFunc(ArgServer))
http.Handle("/go/hello", http.HandlerFunc(HelloServer))
@@ -161,6 +144,6 @@ func main() {
http.Handle("/date", http.HandlerFunc(DateServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
- log.Crash("ListenAndServe: ", err)
+ log.Panicln("ListenAndServe:", err)
}
}