1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // HTTP client implementation. See RFC 2616.
7 // This is the low-level Transport implementation of RoundTripper.
8 // The high-level interface is in client.go.
29 // DefaultTransport is the default implementation of Transport and is
30 // used by DefaultClient. It establishes a new network connection for
31 // each call to Do and uses HTTP proxies as directed by the
32 // $HTTP_PROXY and $NO_PROXY (or $http_proxy and $no_proxy)
33 // environment variables.
34 var DefaultTransport RoundTripper = &Transport{Proxy: ProxyFromEnvironment}
36 // DefaultMaxIdleConnsPerHost is the default value of Transport's
37 // MaxIdleConnsPerHost.
38 const DefaultMaxIdleConnsPerHost = 2
40 // Transport is an implementation of RoundTripper that supports http,
41 // https, and http proxies (for either http or https with CONNECT).
42 // Transport can also cache connections for future re-use.
43 type Transport struct {
45 idleConn map[string][]*persistConn
46 altProto map[string]RoundTripper // nil or map of URI scheme => RoundTripper
48 // TODO: tunable on global max cached connections
49 // TODO: tunable on timeout on cached connections
50 // TODO: optional pipelining
52 // Proxy specifies a function to return a proxy for a given
53 // Request. If the function returns a non-nil error, the
54 // request is aborted with the provided error.
55 // If Proxy is nil or returns a nil *URL, no proxy is used.
56 Proxy func(*Request) (*url.URL, error)
58 // Dial specifies the dial function for creating TCP
60 // If Dial is nil, net.Dial is used.
61 Dial func(net, addr string) (c net.Conn, err error)
63 // TLSClientConfig specifies the TLS configuration to use with
64 // tls.Client. If nil, the default configuration is used.
65 TLSClientConfig *tls.Config
67 DisableKeepAlives bool
68 DisableCompression bool
70 // MaxIdleConnsPerHost, if non-zero, controls the maximum idle
71 // (keep-alive) to keep to keep per-host. If zero,
72 // DefaultMaxIdleConnsPerHost is used.
73 MaxIdleConnsPerHost int
76 // ProxyFromEnvironment returns the URL of the proxy to use for a
77 // given request, as indicated by the environment variables
78 // $HTTP_PROXY and $NO_PROXY (or $http_proxy and $no_proxy).
79 // Either URL or an error is returned.
80 func ProxyFromEnvironment(req *Request) (*url.URL, error) {
81 proxy := getenvEitherCase("HTTP_PROXY")
85 if !useProxy(canonicalAddr(req.URL)) {
88 proxyURL, err := url.ParseRequest(proxy)
90 return nil, errors.New("invalid proxy address")
92 if proxyURL.Host == "" {
93 proxyURL, err = url.ParseRequest("http://" + proxy)
95 return nil, errors.New("invalid proxy address")
101 // ProxyURL returns a proxy function (for use in a Transport)
102 // that always returns the same URL.
103 func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error) {
104 return func(*Request) (*url.URL, error) {
109 // transportRequest is a wrapper around a *Request that adds
110 // optional extra headers to write.
111 type transportRequest struct {
112 *Request // original request, not to be mutated
113 extra Header // extra headers to write, or nil
116 func (tr *transportRequest) extraHeaders() Header {
118 tr.extra = make(Header)
123 // RoundTrip implements the RoundTripper interface.
124 func (t *Transport) RoundTrip(req *Request) (resp *Response, err error) {
126 return nil, errors.New("http: nil Request.URL")
128 if req.Header == nil {
129 return nil, errors.New("http: nil Request.Header")
131 if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
134 if t.altProto != nil {
135 rt = t.altProto[req.URL.Scheme]
139 return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
141 return rt.RoundTrip(req)
143 treq := &transportRequest{Request: req}
144 cm, err := t.connectMethodForRequest(treq)
149 // Get the cached or newly-created connection to either the
150 // host (for http or https), the http proxy, or the http proxy
151 // pre-CONNECTed to https server. In any case, we'll be ready
152 // to send it requests.
153 pconn, err := t.getConn(cm)
158 return pconn.roundTrip(treq)
161 // RegisterProtocol registers a new protocol with scheme.
162 // The Transport will pass requests using the given scheme to rt.
163 // It is rt's responsibility to simulate HTTP request semantics.
165 // RegisterProtocol can be used by other packages to provide
166 // implementations of protocol schemes like "ftp" or "file".
167 func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper) {
168 if scheme == "http" || scheme == "https" {
169 panic("protocol " + scheme + " already registered")
173 if t.altProto == nil {
174 t.altProto = make(map[string]RoundTripper)
176 if _, exists := t.altProto[scheme]; exists {
177 panic("protocol " + scheme + " already registered")
179 t.altProto[scheme] = rt
182 // CloseIdleConnections closes any connections which were previously
183 // connected from previous requests but are now sitting idle in
184 // a "keep-alive" state. It does not interrupt any connections currently
186 func (t *Transport) CloseIdleConnections() {
189 if t.idleConn == nil {
192 for _, conns := range t.idleConn {
193 for _, pconn := range conns {
201 // Private implementation past this point.
204 func getenvEitherCase(k string) string {
205 if v := os.Getenv(strings.ToUpper(k)); v != "" {
208 return os.Getenv(strings.ToLower(k))
211 func (t *Transport) connectMethodForRequest(treq *transportRequest) (*connectMethod, error) {
212 cm := &connectMethod{
213 targetScheme: treq.URL.Scheme,
214 targetAddr: canonicalAddr(treq.URL),
218 cm.proxyURL, err = t.Proxy(treq.Request)
226 // proxyAuth returns the Proxy-Authorization header to set
227 // on requests, if applicable.
228 func (cm *connectMethod) proxyAuth() string {
229 if cm.proxyURL == nil {
232 if u := cm.proxyURL.User; u != nil {
233 return "Basic " + base64.URLEncoding.EncodeToString([]byte(u.String()))
238 func (t *Transport) putIdleConn(pconn *persistConn) {
241 if t.DisableKeepAlives || t.MaxIdleConnsPerHost < 0 {
245 if pconn.isBroken() {
248 key := pconn.cacheKey
249 max := t.MaxIdleConnsPerHost
251 max = DefaultMaxIdleConnsPerHost
253 if len(t.idleConn[key]) >= max {
257 t.idleConn[key] = append(t.idleConn[key], pconn)
260 func (t *Transport) getIdleConn(cm *connectMethod) (pconn *persistConn) {
263 if t.idleConn == nil {
264 t.idleConn = make(map[string][]*persistConn)
268 pconns, ok := t.idleConn[key]
272 if len(pconns) == 1 {
274 delete(t.idleConn, key)
276 // 2 or more cached connections; pop last
278 pconn = pconns[len(pconns)-1]
279 t.idleConn[key] = pconns[0 : len(pconns)-1]
281 if !pconn.isBroken() {
288 func (t *Transport) dial(network, addr string) (c net.Conn, err error) {
290 return t.Dial(network, addr)
292 return net.Dial(network, addr)
295 // getConn dials and creates a new persistConn to the target as
296 // specified in the connectMethod. This includes doing a proxy CONNECT
297 // and/or setting up TLS. If this doesn't return an error, the persistConn
298 // is ready to write requests to.
299 func (t *Transport) getConn(cm *connectMethod) (*persistConn, error) {
300 if pc := t.getIdleConn(cm); pc != nil {
304 conn, err := t.dial("tcp", cm.addr())
306 if cm.proxyURL != nil {
307 err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err)
314 pconn := &persistConn{
316 cacheKey: cm.String(),
318 reqch: make(chan requestAndChan, 50),
322 case cm.proxyURL == nil:
324 case cm.targetScheme == "http":
327 pconn.mutateHeaderFunc = func(h Header) {
328 h.Set("Proxy-Authorization", pa)
331 case cm.targetScheme == "https":
332 connectReq := &Request{
334 URL: &url.URL{Opaque: cm.targetAddr},
336 Header: make(Header),
339 connectReq.Header.Set("Proxy-Authorization", pa)
341 connectReq.Write(conn)
344 // Okay to use and discard buffered reader here, because
345 // TLS server will not speak until spoken to.
346 br := bufio.NewReader(conn)
347 resp, err := ReadResponse(br, connectReq)
352 if resp.StatusCode != 200 {
353 f := strings.SplitN(resp.Status, " ", 2)
355 return nil, errors.New(f[1])
359 if cm.targetScheme == "https" {
360 // Initiate TLS and check remote host name against certificate.
361 conn = tls.Client(conn, t.TLSClientConfig)
362 if err = conn.(*tls.Conn).Handshake(); err != nil {
365 if t.TLSClientConfig == nil || !t.TLSClientConfig.InsecureSkipVerify {
366 if err = conn.(*tls.Conn).VerifyHostname(cm.tlsHost()); err != nil {
373 pconn.br = bufio.NewReader(pconn.conn)
374 pconn.bw = bufio.NewWriter(pconn.conn)
379 // useProxy returns true if requests to addr should use a proxy,
380 // according to the NO_PROXY or no_proxy environment variable.
381 // addr is always a canonicalAddr with a host and port.
382 func useProxy(addr string) bool {
386 host, _, err := net.SplitHostPort(addr)
390 if host == "localhost" {
393 if ip := net.ParseIP(host); ip != nil {
399 no_proxy := getenvEitherCase("NO_PROXY")
404 addr = strings.ToLower(strings.TrimSpace(addr))
406 addr = addr[:strings.LastIndex(addr, ":")]
409 for _, p := range strings.Split(no_proxy, ",") {
410 p = strings.ToLower(strings.TrimSpace(p))
415 p = p[:strings.LastIndex(p, ":")]
417 if addr == p || (p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:])) {
424 // connectMethod is the map key (in its String form) for keeping persistent
425 // TCP connections alive for subsequent HTTP requests.
427 // A connect method may be of the following types:
429 // Cache key form Description
430 // ----------------- -------------------------
431 // ||http|foo.com http directly to server, no proxy
432 // ||https|foo.com https directly to server, no proxy
433 // http://proxy.com|https|foo.com http to proxy, then CONNECT to foo.com
434 // http://proxy.com|http http to proxy, http to anywhere after that
436 // Note: no support to https to the proxy yet.
438 type connectMethod struct {
439 proxyURL *url.URL // nil for no proxy, else full proxy URL
440 targetScheme string // "http" or "https"
441 targetAddr string // Not used if proxy + http targetScheme (4th example in table)
444 func (ck *connectMethod) String() string {
446 if ck.proxyURL != nil {
447 proxyStr = ck.proxyURL.String()
449 return strings.Join([]string{proxyStr, ck.targetScheme, ck.targetAddr}, "|")
452 // addr returns the first hop "host:port" to which we need to TCP connect.
453 func (cm *connectMethod) addr() string {
454 if cm.proxyURL != nil {
455 return canonicalAddr(cm.proxyURL)
460 // tlsHost returns the host name to match against the peer's
462 func (cm *connectMethod) tlsHost() string {
465 h = h[:strings.LastIndex(h, ":")]
470 // persistConn wraps a connection, usually a persistent one
471 // (but may be used for non-keep-alive requests as well)
472 type persistConn struct {
474 cacheKey string // its connectMethod.String()
476 br *bufio.Reader // from conn
477 bw *bufio.Writer // to conn
478 reqch chan requestAndChan // written by roundTrip(); read by readLoop()
481 // mutateHeaderFunc is an optional func to modify extra
482 // headers on each outbound request before it's written. (the
483 // original Request given to RoundTrip is not modified)
484 mutateHeaderFunc func(Header)
486 lk sync.Mutex // guards numExpectedResponses and broken
487 numExpectedResponses int
488 broken bool // an error has happened on this connection; marked broken so it's not reused.
491 func (pc *persistConn) isBroken() bool {
497 func (pc *persistConn) expectingResponse() bool {
500 return pc.numExpectedResponses > 0
503 var remoteSideClosedFunc func(error) bool // or nil to use default
505 func remoteSideClosed(err error) bool {
509 if remoteSideClosedFunc != nil {
510 return remoteSideClosedFunc(err)
515 func (pc *persistConn) readLoop() {
517 var lastbody io.ReadCloser // last response body, if any, read on this connection
520 pb, err := pc.br.Peek(1)
521 if !pc.expectingResponse() {
523 log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v",
532 // Advance past the previous response's body, if the
533 // caller hasn't done so.
535 lastbody.Close() // assumed idempotent
538 resp, err := ReadResponse(pc.br, rc.req)
541 hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0
542 if rc.addedGzip && hasBody && resp.Header.Get("Content-Encoding") == "gzip" {
543 resp.Header.Del("Content-Encoding")
544 resp.Header.Del("Content-Length")
545 resp.ContentLength = -1
546 gzReader, zerr := gzip.NewReader(resp.Body)
551 resp.Body = &readFirstCloseBoth{&discardOnCloseReadCloser{gzReader}, resp.Body}
554 resp.Body = &bodyEOFSignal{body: resp.Body}
557 if err != nil || resp.Close || rc.req.Close {
561 hasBody := resp != nil && resp.ContentLength != 0
562 var waitForBodyRead chan bool
566 waitForBodyRead = make(chan bool)
567 resp.Body.(*bodyEOFSignal).fn = func() {
569 waitForBodyRead <- true
572 // When there's no response body, we immediately
573 // reuse the TCP connection (putIdleConn), but
574 // we need to prevent ClientConn.Read from
575 // closing the Response.Body on the next
576 // loop, otherwise it might close the body
577 // before the client code has had a chance to
578 // read it (even though it'll just be 0, EOF).
585 rc.ch <- responseAndError{resp, err}
587 // Wait for the just-returned response body to be fully consumed
588 // before we race and peek on the underlying bufio reader.
589 if waitForBodyRead != nil {
595 type responseAndError struct {
600 type requestAndChan struct {
602 ch chan responseAndError
604 // did the Transport (as opposed to the client code) add an
605 // Accept-Encoding gzip header? only if it we set it do
606 // we transparently decode the gzip.
610 func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err error) {
611 if pc.mutateHeaderFunc != nil {
612 pc.mutateHeaderFunc(req.extraHeaders())
615 // Ask for a compressed version if the caller didn't set their
616 // own value for Accept-Encoding. We only attempted to
617 // uncompress the gzip stream if we were the layer that
619 requestedGzip := false
620 if !pc.t.DisableCompression && req.Header.Get("Accept-Encoding") == "" {
621 // Request gzip only, not deflate. Deflate is ambiguous and
622 // not as universally supported anyway.
623 // See: http://www.gzip.org/zlib/zlib_faq.html#faq38
625 req.extraHeaders().Set("Accept-Encoding", "gzip")
629 pc.numExpectedResponses++
632 err = req.Request.write(pc.bw, pc.isProxy, req.extra)
639 ch := make(chan responseAndError, 1)
640 pc.reqch <- requestAndChan{req.Request, ch, requestedGzip}
643 pc.numExpectedResponses--
646 return re.res, re.err
649 func (pc *persistConn) close() {
654 pc.mutateHeaderFunc = nil
657 var portMap = map[string]string{
662 // canonicalAddr returns url.Host but always with a ":port" suffix
663 func canonicalAddr(url *url.URL) string {
666 return addr + ":" + portMap[url.Scheme]
671 func responseIsKeepAlive(res *Response) bool {
672 // TODO: implement. for now just always shutting down the connection.
676 // bodyEOFSignal wraps a ReadCloser but runs fn (if non-nil) at most
677 // once, right before the final Read() or Close() call returns, but after
678 // EOF has been seen.
679 type bodyEOFSignal struct {
685 func (es *bodyEOFSignal) Read(p []byte) (n int, err error) {
686 n, err = es.body.Read(p)
687 if es.isClosed && n > 0 {
688 panic("http: unexpected bodyEOFSignal Read after Close; see issue 1725")
690 if err == io.EOF && es.fn != nil {
697 func (es *bodyEOFSignal) Close() (err error) {
702 err = es.body.Close()
703 if err == nil && es.fn != nil {
710 type readFirstCloseBoth struct {
715 func (r *readFirstCloseBoth) Close() error {
716 if err := r.ReadCloser.Close(); err != nil {
720 if err := r.Closer.Close(); err != nil {
726 // discardOnCloseReadCloser consumes all its input on Close.
727 type discardOnCloseReadCloser struct {
731 func (d *discardOnCloseReadCloser) Close() error {
732 io.Copy(ioutil.Discard, d.ReadCloser) // ignore errors; likely invalid or already closed
733 return d.ReadCloser.Close()