Hello! Thanks for writing a good module.
I know no one uses HTTP GET with body in the wild, but indeed it is possible to send GET with the body data. And, from what I accidentally noticed, when your library is plugged to the request, this request errors:
2020/09/04 21:52:31 httputil: ReverseProxy read error during body copy: stream error: stream ID 1; PROTOCOL_ERROR
Here's what I do:
url, err := url.Parse("https://www.google.com")
proxy := logRequestHandler(httputil.NewSingleHostReverseProxy(url))
Try GET <host>/favicon.ico
with any body and it errors. logRequestHandler is:
func logRequestHandler(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(h, w, r)
log.Println("RESPONSE SIZE: " + strconv.FormatInt(m.Written, 10))
}
return http.HandlerFunc(fn)
}
The error doesn't happen if I remove "logRequestHandler()". The other part of the program if you're curious to test my setup:
func main() {
port := ":80"
http.HandleFunc("/", handler())
log.Println("Listeting on " + port)
log.Fatal(http.ListenAndServe(port, nil))
}
func handler() func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
url, err := url.Parse("https://www.google.com")
if err != nil {
panic(err)
}
// Remove logRequestHandler() and the error will be gone
proxy := logRequestHandler(httputil.NewSingleHostReverseProxy(url))
req.URL.Host = url.Host
req.Host = url.Host
req.URL.Scheme = url.Scheme
req.Header.Set("X-Forwarded-Host", url.Host)
proxy.ServeHTTP(res, req)
}
}
Hope this helps to make this library even better! :)