diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-12-07 01:11:29 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-12-07 01:11:29 +0000 |
commit | 8388e3abaaaa0546c5f1c87a68e583958063cde9 (patch) | |
tree | 84f27a6ab44d932e4b0455f18390b070b4de626e /libgo/go/net/http/triv.go | |
parent | bc461ec935abe0c6f3e92be027143b4f25b5682f (diff) | |
download | gcc-8388e3abaaaa0546c5f1c87a68e583958063cde9.tar.gz |
libgo: Update to weekly 2011-11-09.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@182073 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/net/http/triv.go')
-rw-r--r-- | libgo/go/net/http/triv.go | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/libgo/go/net/http/triv.go b/libgo/go/net/http/triv.go new file mode 100644 index 00000000000..994fc0e32f6 --- /dev/null +++ b/libgo/go/net/http/triv.go @@ -0,0 +1,149 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "bytes" + "expvar" + "flag" + "fmt" + "io" + "log" + "net/http" + "os" + "strconv" +) + +// hello world, the web server +var helloRequests = expvar.NewInt("hello-requests") + +func HelloServer(w http.ResponseWriter, req *http.Request) { + helloRequests.Add(1) + io.WriteString(w, "hello, world!\n") +} + +// Simple counter server. POSTing to it will set the value. +type Counter struct { + n int +} + +// This makes Counter satisfy the expvar.Var interface, so we can export +// it directly. +func (ctr *Counter) String() string { return fmt.Sprintf("%d", ctr.n) } + +func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) { + switch req.Method { + case "GET": + ctr.n++ + case "POST": + buf := new(bytes.Buffer) + io.Copy(buf, req.Body) + body := buf.String() + if n, err := strconv.Atoi(body); err != nil { + fmt.Fprintf(w, "bad POST: %v\nbody: [%v]\n", err, body) + } else { + ctr.n = n + fmt.Fprint(w, "counter reset\n") + } + } + fmt.Fprintf(w, "counter = %d\n", ctr.n) +} + +// simple flag server +var booleanflag = flag.Bool("boolean", true, "another flag for testing") + +func FlagServer(w http.ResponseWriter, req *http.Request) { + w.Header().Set("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(w, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue) + } else { + fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String()) + } + }) +} + +// simple argument server +func ArgServer(w http.ResponseWriter, req *http.Request) { + for _, s := range os.Args { + fmt.Fprint(w, s, " ") + } +} + +// a channel (just for the fun of it) +type Chan chan int + +func ChanCreate() Chan { + c := make(Chan) + go func(c Chan) { + for x := 0; ; x++ { + c <- x + } + }(c) + return c +} + +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(rw http.ResponseWriter, req *http.Request) { + 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}}) + defer r.Close() + w.Close() + if err != nil { + fmt.Fprintf(rw, "fork/exec: %s\n", err) + return + } + defer p.Release() + 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 + } +} + +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() + + // The counter is published as a variable directly. + ctr := new(Counter) + http.Handle("/counter", ctr) + expvar.Publish("counter", ctr) + + http.Handle("/", http.HandlerFunc(Logger)) + http.Handle("/go/", http.StripPrefix("/go/", http.FileServer(http.Dir(*webroot)))) + http.Handle("/flags", http.HandlerFunc(FlagServer)) + 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.Panicln("ListenAndServe:", err) + } +} |