1
0
mirror of https://github.com/golang/go synced 2024-11-22 02:44:39 -07:00

http: let Transport use a custom net.Dial function

Permits the use of SOCKS proxy dialer with
the transport.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4536091
This commit is contained in:
Brad Fitzpatrick 2011-05-29 09:32:36 -07:00
parent 62943df829
commit da32ed7bf1

View File

@ -42,12 +42,17 @@ type Transport struct {
// TODO: tunable on timeout on cached connections // TODO: tunable on timeout on cached connections
// TODO: optional pipelining // TODO: optional pipelining
// Proxy optionally specifies a function to return a proxy for // Proxy specifies a function to return a proxy for a given
// a given Request. If the function returns a non-nil error, // Request. If the function returns a non-nil error, the
// the request is aborted with the provided error. If Proxy is // request is aborted with the provided error.
// nil or returns a nil *URL, no proxy is used. // If Proxy is nil or returns a nil *URL, no proxy is used.
Proxy func(*Request) (*URL, os.Error) Proxy func(*Request) (*URL, os.Error)
// Dial specifies the dial function for creating TCP
// connections.
// If Dial is nil, net.Dial is used.
Dial func(net, addr string) (c net.Conn, err os.Error)
DisableKeepAlives bool DisableKeepAlives bool
DisableCompression bool DisableCompression bool
@ -255,6 +260,13 @@ func (t *Transport) getIdleConn(cm *connectMethod) (pconn *persistConn) {
return return
} }
func (t *Transport) dial(network, addr string) (c net.Conn, err os.Error) {
if t.Dial != nil {
return t.Dial(network, addr)
}
return net.Dial(network, addr)
}
// getConn dials and creates a new persistConn to the target as // getConn dials and creates a new persistConn to the target as
// specified in the connectMethod. This includes doing a proxy CONNECT // specified in the connectMethod. This includes doing a proxy CONNECT
// and/or setting up TLS. If this doesn't return an error, the persistConn // and/or setting up TLS. If this doesn't return an error, the persistConn
@ -264,7 +276,7 @@ func (t *Transport) getConn(cm *connectMethod) (*persistConn, os.Error) {
return pc, nil return pc, nil
} }
conn, err := net.Dial("tcp", cm.addr()) conn, err := t.dial("tcp", cm.addr())
if err != nil { if err != nil {
if cm.proxyURL != nil { if cm.proxyURL != nil {
err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err) err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err)