Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Can I remove response headers coming from ReverseProxy?

I'm using httputil.ReverseProxy to proxy Amazon s3 files to my clients. I'd like to hide all headers coming from Amazon - is that possible without having to reimplement Reverse Proxy?

proxy := httputil.ReverseProxy{Director: func(r *http.Request) {
    r.Header = http.Header{} // Don't send client's request headers to Amazon.
    r.URL = proxyURL
    r.Host = proxyURL.Host
}}
proxy.ServeHTTP(w, r) // How do I remove w.Headers ?
like image 911
Vojtech Vitek Avatar asked Mar 12 '23 10:03

Vojtech Vitek


1 Answers

You can implement ReverseProxy.Transport

type MyTransport struct{
    header http.Header
}
func (t MyTransport) RoundTrip(r *Request) (*Response, error){
    resp, err := http.DefaultTransport.RoundTrip(r)
    resp.Header = t.header
    return resp, err
}
mytransport := MyTransport{
//construct Header
}
proxy := httputil.ReverseProxy{Director: func(r *http.Request) {
    r.Header = http.Header{} // Don't send client's request headers to Amazon.
    r.URL = proxyURL
    r.Host = proxyURL.Host
  },
  Transport: mytransport,
}
like image 185
Uvelichitel Avatar answered Apr 06 '23 08:04

Uvelichitel