From 029abdc9bb270f4b3b991bb555d580c35e190dca Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Sun, 9 Jun 2024 18:40:26 +0200 Subject: [PATCH] POST request dump, working cookies --- main.go | 97 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/main.go b/main.go index 3a074d1..c9db934 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "net/http/httputil" ) func main() { @@ -20,74 +21,82 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { port := domains[r.Host] - fmt.Printf("Received request on Port: %d\n", port) - // logRequest(*r) + + rDump, err := httputil.DumpRequest(r, true) + if err != nil { + panic(err) + } subUrlPath := r.URL.RequestURI() req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body) if err != nil { panic(err) } - // logRequest(*req) + + for name, values := range r.Header { + for _, value := range values { + req.Header.Set(name, value) + } + } + for _, cookie := range r.Cookies() { - fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) + // fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) req.AddCookie(cookie) } - // fmt.Println("Sending request to nested server") + reqDump, err := httputil.DumpRequestOut(req, true) + if err != nil { + panic(err) + } + res, err := client.Do(req) if err != nil { panic(err) } - if loc, err := res.Location(); !errors.Is(err, http.ErrNoLocation) { - http.Redirect(w, r, loc.RequestURI(), http.StatusFound) - return - } - for _, cookie := range res.Cookies() { - fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) + cookies := res.Cookies() + for _, cookie := range cookies { + // fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) http.SetCookie(w, cookie) } - for name, values := range res.Header { - for _, value := range values { - // fmt.Printf("Adding Header vor name: %s, value: %s\n", name, value) - w.Header().Set(name, value) + resDump, err := httputil.DumpResponse(res, true) + if err != nil { + panic(err) + } + + if loc, err := res.Location(); !errors.Is(err, http.ErrNoLocation) { + http.Redirect(w, r, loc.RequestURI(), http.StatusFound) + } else { + for name, values := range res.Header { + for _, value := range values { + w.Header().Set(name, value) + } } - } - w.WriteHeader(res.StatusCode) + w.WriteHeader(res.StatusCode) - // fmt.Println("Reading Body") - body, err := io.ReadAll(res.Body) - if err != nil { - panic(err) - } - defer res.Body.Close() - // fmt.Printf("Body: %s\n", hex.EncodeToString(body)) + body, err := io.ReadAll(res.Body) + if err != nil { + panic(err) + } + defer res.Body.Close() + + _, err = w.Write(body) + if err != nil { + panic(err) + } - _, err = w.Write(body) - if err != nil { - panic(err) } - fmt.Println("----------------------------------------") + + if r.Method == "POST" { + fmt.Print(string(rDump) + "\n\n") + fmt.Print(string(reqDump) + "\n\n") + fmt.Print(string(resDump) + "\n") + fmt.Println("----------------------------------------") + } + }) fmt.Println("Starting server on :80") http.ListenAndServe(":80", nil) } - -func logRequest(r http.Request) { - fmt.Printf("URL: %s %s\n", r.Method, r.URL) - fmt.Println("HEADER: ") - for name, values := range r.Header { - for _, value := range values { - fmt.Printf("%s: %s\n", name, value) - // w.Header().Set(name, value) - } - } - rbody, err := io.ReadAll(r.Body) - if err != nil { - panic(err) - } - fmt.Printf("BODY: %s\n", rbody) -}