POST request dump, working cookies

This commit is contained in:
Pablu23
2024-06-09 18:40:26 +02:00
parent 9a0c086741
commit 029abdc9bb

69
main.go
View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httputil"
) )
func main() { func main() {
@@ -20,74 +21,82 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
port := domains[r.Host] 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() subUrlPath := r.URL.RequestURI()
req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body) req, err := http.NewRequest(r.Method, fmt.Sprintf("http://localhost:%d%s", port, subUrlPath), r.Body)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// logRequest(*req)
for name, values := range r.Header {
for _, value := range values {
req.Header.Set(name, value)
}
}
for _, cookie := range r.Cookies() { 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) 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) res, err := client.Do(req)
if err != nil { if err != nil {
panic(err) 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() { cookies := res.Cookies()
fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value) for _, cookie := range cookies {
// fmt.Printf("Setting cookie, Name: %s, Value: %s\n", cookie.Name, cookie.Value)
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
} }
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 name, values := range res.Header {
for _, value := range values { for _, value := range values {
// fmt.Printf("Adding Header vor name: %s, value: %s\n", name, value)
w.Header().Set(name, value) w.Header().Set(name, value)
} }
} }
w.WriteHeader(res.StatusCode) w.WriteHeader(res.StatusCode)
// fmt.Println("Reading Body")
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer res.Body.Close() defer res.Body.Close()
// fmt.Printf("Body: %s\n", hex.EncodeToString(body))
_, err = w.Write(body) _, err = w.Write(body)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}
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("----------------------------------------")
}
}) })
fmt.Println("Starting server on :80") fmt.Println("Starting server on :80")
http.ListenAndServe(":80", nil) 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)
}