I'm writing a reverse proxy server in Go using the core net/http/httputil/ReverseProxy library. I already use a custom Director and I need to define my own RoundTrip and Dial for the Transport.
I'm able to use custom Dial like this:
transport := &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
lgr.Println("running custom magic below")
// custom magic
return net.Dial(network, addr)
},
}
res := &httputil.ReverseProxy{Director: director, Transport: transport}
return res
So I created a custom transport that embeds http.Transport like this:
type customTransport struct {
http.Transport
}
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := http.DefaultTransport.RoundTrip(req)
lgr.Println("checking the response")
// check the response
return res, err
}
And I tried to use it instead of the default Transport in this way:
transport := &customTransport{http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
lgr.Println("running custom magic below")
// custom magic
return net.Dial(network, addr)
},
}}
res := &httputil.ReverseProxy{Director: director, Transport: transport}
The problem is that while my custom RoundTrip works fine, the Dial stopped working now and no custom magic is done anymore.
When I log transport.Dial right after it is assigned, I see its address in memory. When I log my RevereProxy.Transport, which the function above returns in res, I even see the same address there:
fmt.Printf("%#v", transport.Dial)
2016/02/18 12:11:48.401245 main.go:76: (func(string, string) (net.Conn, error))(0x4039a0)
fmt.Printf("%#v", proxy.Transport)
2016/02/18 12:11:48.401403 main.go:100: &main.customTransport{Transport:http.Transport{idleMu:sync.Mutex{state:0, sema:0x0}, wantIdle:false, idleConn:map[http.connectMethodKey][]*http.persistConn(nil), idleConnCh:map[http.connectMethodKey]chan *http.persistConn(nil), reqMu:sync.Mutex{state:0, sema:0x0}, reqCanceler:map[*http.Request]func()(nil), altMu:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0}, writerSem:0x0, readerSem:0x0, readerCount:0, readerWait:0}, altProto:map[string]http.RoundTripper(nil), Proxy:(func(*http.Request) (*url.URL, error))(nil), Dial:(func(string, string) (net.Conn, error))(0x4039a0), DialTLS:(func(string, string) (net.Conn, error))(nil), TLSClientConfig:(*tls.Config)(nil), TLSHandshakeTimeout:0, DisableKeepAlives:false, DisableCompression:false, MaxIdleConnsPerHost:0, ResponseHeaderTimeout:0, ExpectContinueTimeout:0, TLSNextProto:map[string]func(string, *tls.Conn) http.RoundTripper(nil), nextProtoOnce:sync.Once{m:sync.Mutex{state:0, sema:0x0}, done:0x0}, h2transport:(*http.http2Transport)(nil)}}
Anyway, it seems that the core net.Dial method is called instead of my custom Dial and I really don't have a clue why. :-( Any ideas would be highly appreciated! Thanks!
You're defining a custom Transport, but calling RoundTrip
on the http.DefaultTransport
. You need to call RoundTrip
on the embedded Transport:
type customTransport struct {
*http.Transport
}
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := t.Transport.RoundTrip(req)
lgr.Println("checking the response")
// check the response
return res, err
}