From d716da7a2fc8c3eda6c83022977cedeb0d430f72 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Tue, 23 Nov 2021 21:47:32 +0300 Subject: [PATCH] Chapter 1 - server3.go --- ch1/server3.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch1/server3.go diff --git a/ch1/server3.go b/ch1/server3.go new file mode 100644 index 0000000..065ad76 --- /dev/null +++ b/ch1/server3.go @@ -0,0 +1,29 @@ +// Обработчик HTTP-запросов. +package main + +import ( + "fmt" + "log" + "net/http" +) + +func main() { + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe("localhost:8888", nil)) +} + +// Обработчик HTTP-запросов. +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) + } +} \ No newline at end of file