diff --git a/hermit/.gitignore b/hermit/.gitignore index 11a955c52..3e362f29e 100644 --- a/hermit/.gitignore +++ b/hermit/.gitignore @@ -19,6 +19,7 @@ usr/tests/thr_hello usr/tests/pi usr/tests/RCCE_minimum usr/tests/signals +usr/tests/server usr/benchmarks/RCCE_pingping usr/benchmarks/RCCE_pingpong usr/benchmarks/stream diff --git a/hermit/usr/tests/Makefile b/hermit/usr/tests/Makefile index 64073afea..0c2d417eb 100644 --- a/hermit/usr/tests/Makefile +++ b/hermit/usr/tests/Makefile @@ -49,7 +49,7 @@ endif default: all -all: hello hello++ thr_hello jacobi hellof RCCE_minimum signals pi +all: hello hello++ thr_hello jacobi hellof RCCE_minimum signals pi server hello++: hello++.o @echo [LD] $@ @@ -79,6 +79,13 @@ pi: pi.o $Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@ $Qchmod a-x $@.sym +server: server.o + @echo [LD] $@ + $Q$(GO_FOR_TARGET) -pthread -o $@ $< $(LDFLAGS_FOR_TARGET) $(GOFLAGS_FOR_TARGET) -lnetgo + $Q$(OBJCOPY_FOR_TARGET) $(KEEP_DEBUG) $@ $@.sym + $Q$(OBJCOPY_FOR_TARGET) $(STRIP_DEBUG) $@ + $Qchmod a-x $@.sym + jacobi: jacobi.o @echo [LD] $@ $Q$(CC_FOR_TARGET) -o $@ $< $(LDFLAGS_FOR_TARGET) $(CFLAGS_FOR_TARGET) @@ -117,11 +124,11 @@ RCCE_minimum: RCCE_minimum.o clean: @echo Cleaning tests - $Q$(RM) hello hello++ hellof jacobi thr_hello RCCE_minimum signals pi *.sym *.o *~ + $Q$(RM) hello hello++ hellof jacobi thr_hello RCCE_minimum signals pi server *.sym *.o *~ veryclean: @echo Propper cleaning tests - $Q$(RM) hello hello++ hellof jacobi thr_hello RCCE_minimum pi *.sym *.o *~ + $Q$(RM) hello hello++ hellof jacobi thr_hello RCCE_minimum pi server *.sym *.o *~ depend: $Q$(CC_FOR_TARGET) -MM $(CFLAGS_FOR_TARGET) *.c > Makefile.dep diff --git a/hermit/usr/tests/server.go b/hermit/usr/tests/server.go new file mode 100644 index 000000000..45fd979f6 --- /dev/null +++ b/hermit/usr/tests/server.go @@ -0,0 +1,43 @@ +// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// The original code was published at http://www.gopl.io, see page 21. + +// This is an "echo" server that displays request parameters. + +package main + +import ( + "fmt" + "log" + "net/http" +) + +func main() { + fmt.Println("This is an \"echo\" server that displays request parameters.") + fmt.Println("Start the server and send a http request to it (e.g. curl http://localhost:8000/help).") + fmt.Println("The server uses port 8000. Please open the port by setting the") + fmt.Println("environment variable HERMIT_APP_PORT to 8000.") + + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8000", nil)) +} + +//!+handler +// handler echoes the HTTP request. +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%s %s %s\n", r.Method, r.URL, r.Proto) + for k, v := range r.Header { + fmt.Fprintf(w, "Header[%q] = %q\n", k, v) + } + fmt.Fprintf(w, "Host = %q\n", r.Host) + fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr) + if err := r.ParseForm(); err != nil { + log.Print(err) + } + for k, v := range r.Form { + fmt.Fprintf(w, "Form[%q] = %q\n", k, v) + } +} + +//!-handler