I want to build a http reverse proxy which checks the HTTP body and send HTTP requests to it's upstream servers after that. How can you do that in go?
Initial attempt (follows) fails because ReverseProxy copies the incoming request, modifies it and sends but the body is already read.
func main() {
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("ioutil.ReadAll: %s", err), 500)
return
}
// expecting to see hoge=fuga
fmt.Fprintf(w, "this call was relayed by the reverse proxy, body: %s", string(b))
}))
defer backendServer.Close()
rpURL, err := url.Parse(backendServer.URL)
if err != nil {
log.Fatal(err)
}
proxy := func(u *url.URL) http.Handler {
p := httputil.NewSingleHostReverseProxy(u)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, fmt.Sprintf("ParseForm: %s", err), 500)
return
}
p.ServeHTTP(w, r)
})
}(rpURL)
frontendProxy := httptest.NewServer(proxy)
defer frontendProxy.Close()
resp, err := http.Post(frontendProxy.URL, "application/x-www-form-urlencoded", bytes.NewBufferString("hoge=fuga"))
if err != nil {
log.Fatalf("http.Post: %s", err)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("ioutil.ReadAll: %s", err)
}
fmt.Printf("%s", b)
}
// shows: "http: proxy error: http: ContentLength=9 with Body length 0"
Then my next attempt would be to read the whole body into bytes.Reader and use that to check the body content, and Seek to the beginning before sending to upstream servers. But then I have to re-implement ReverseProxy which I would like to avoid. Is there any other elegant way?