diff --git a/README.md b/README.md index a9072f8..f7bd271 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,8 @@ To response another status than 202: ```sh srv42 -s 204 ``` + +To have the full details of the request: +```sh +srv42 -v +``` diff --git a/main.go b/main.go index 1a9bac6..697dcb4 100644 --- a/main.go +++ b/main.go @@ -4,16 +4,18 @@ import ( "flag" "fmt" "net/http" + "net/http/httputil" "time" ) const ( - FormatPattern = "HTTP - %s - - [%s] \"%s %s %s\" %d %d \"%s\" %d\n" + FormatLog = "HTTP - %s - - [%s] \"%s %s %s\" %d %d \"%s\" %d\n" ) var ( - port = flag.Int("p", 1337, "port to use") - status = flag.Int("s", 202, "status code to return") + port = flag.Int("p", 1337, "port to use") + status = flag.Int("s", 202, "status code to return") + verbose = flag.Bool("v", false, "should srv42 print the full path and body") ) func Handler(w http.ResponseWriter, r *http.Request) { @@ -21,7 +23,7 @@ func Handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(*status) - fmt.Printf(FormatPattern, + fmt.Printf(FormatLog, r.RemoteAddr, t.Format("02/Jan/2006:15:04:05 -0700"), r.Method, @@ -32,6 +34,14 @@ func Handler(w http.ResponseWriter, r *http.Request) { r.UserAgent(), time.Since(t), ) + + if *verbose { + requestDump, err := httputil.DumpRequest(r, true) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%s\n\n", string(requestDump)) + } } func Serve() {