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.
23 // DefaultTransport is the default implementation of Transport and is
24 // used by DefaultClient. It establishes a new network connection for
25 // each call to Do and uses HTTP proxies as directed by the
26 // $HTTP_PROXY and $NO_PROXY (or $http_proxy and $no_proxy)
27 // environment variables.
28 var DefaultTransport RoundTripper = &Transport{Proxy: ProxyFromEnvironment}
30 // DefaultMaxIdleConnsPerHost is the default value of Transport's
31 // MaxIdleConnsPerHost.
32 const DefaultMaxIdleConnsPerHost = 2
34 // Transport is an implementation of RoundTripper that supports http,
35 // https, and http proxies (for either http or https with CONNECT).
36 // Transport can also cache connections for future re-use.
37 type Transport struct {
39 idleConn map[string][]*persistConn
40 altProto map[string]RoundTripper // nil or map of URI scheme => RoundTripper
42 // TODO: tunable on global max cached connections
43 // TODO: tunable on timeout on cached connections
44 // TODO: optional pipelining
46 // Proxy specifies a function to return a proxy for a given
47 // Request. If the function returns a non-nil error, the
48 // request is aborted with the provided error.
49 // If Proxy is nil or returns a nil *URL, no proxy is used.
50 Proxy func(*Request) (*url.URL, os.Error)
52 // Dial specifies the dial function for creating TCP
54 // If Dial is nil, net.Dial is used.
55 Dial func(net, addr string) (c net.Conn, err os.Error)
57 // TLSClientConfig specifies the TLS configuration to use with
58 // tls.Client. If nil, the default configuration is used.
59 TLSClientConfig *tls.Config
61 DisableKeepAlives bool
62 DisableCompression bool
64 // MaxIdleConnsPerHost, if non-zero, controls the maximum idle
65 // (keep-alive) to keep to keep per-host. If zero,
66 // DefaultMaxIdleConnsPerHost is used.
67 MaxIdleConnsPerHost int
70 // ProxyFromEnvironment returns the URL of the proxy to use for a
71 // given request, as indicated by the environment variables
72 // $HTTP_PROXY and $NO_PROXY (or $http_proxy and $no_proxy).
73 // Either URL or an error is returned.
74 func ProxyFromEnvironment(req *Request) (*url.URL, os.Error) {
75 proxy := getenvEitherCase("HTTP_PROXY")
79 if !useProxy(canonicalAddr(req.URL)) {
82 proxyURL, err := url.ParseRequest(proxy)
84 return nil, os.NewError("invalid proxy address")
86 if proxyURL.Host == "" {
87 proxyURL, err = url.ParseRequest("http://" + proxy)
89 return nil, os.NewError("invalid proxy address")
95 // ProxyURL returns a proxy function (for use in a Transport)
96 // that always returns the same URL.
97 func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, os.Error) {
98 return func(*Request) (*url.URL, os.Error) {
103 // transportRequest is a wrapper around a *Request that adds
104 // optional extra headers to write.
105 type transportRequest struct {
106 *Request // original request, not to be mutated
107 extra Header // extra headers to write, or nil
110 func (tr *transportRequest) extraHeaders() Header {
112 tr.extra = make(Header)
117 // RoundTrip implements the RoundTripper interface.
118 func (t *Transport) RoundTrip(req *Request) (resp *Response, err os.Error) {
120 return nil, os.NewError("http: nil Request.URL")
122 if req.Header == nil {
123 return nil, os.NewError("http: nil Request.Header")
125 if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
128 if t.altProto != nil {
129 rt = t.altProto[req.URL.Scheme]
133 return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
135 return rt.RoundTrip(req)
137 treq := &transportRequest{Request: req}
138 cm, err := t.connectMethodForRequest(treq)
143 // Get the cached or newly-created connection to either the
144 // host (for http or https), the http proxy, or the http proxy
145 // pre-CONNECTed to https server. In any case, we'll be ready
146 // to send it requests.
147 pconn, err := t.getConn(cm)
152 return pconn.roundTrip(treq)
155 // RegisterProtocol registers a new protocol with scheme.
156 // The Transport will pass requests using the given scheme to rt.
157 // It is rt's responsibility to simulate HTTP request semantics.
159 // RegisterProtocol can be used by other packages to provide
160 // implementations of protocol schemes like "ftp" or "file".
161 func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper) {
162 if scheme == "http" || scheme == "https" {
163 panic("protocol " + scheme + " already registered")
167 if t.altProto == nil {
168 t.altProto = make(map[string]RoundTripper)
170 if _, exists := t.altProto[scheme]; exists {
171 panic("protocol " + scheme + " already registered")
173 t.altProto[scheme] = rt
176 // CloseIdleConnections closes any connections which were previously
177 // connected from previous requests but are now sitting idle in
178 // a "keep-alive" state. It does not interrupt any connections currently
180 func (t *Transport) CloseIdleConnections() {
183 if t.idleConn == nil {
186 for _, conns := range t.idleConn {
187 for _, pconn := range conns {
195 // Private implementation past this point.
198 func getenvEitherCase(k string) string {
199 if v := os.Getenv(strings.ToUpper(k)); v != "" {
202 return os.Getenv(strings.ToLower(k))
205 func (t *Transport) connectMethodForRequest(treq *transportRequest) (*connectMethod, os.Error) {
206 cm := &connectMethod{
207 targetScheme: treq.URL.Scheme,
208 targetAddr: canonicalAddr(treq.URL),
212 cm.proxyURL, err = t.Proxy(treq.Request)
220 // proxyAuth returns the Proxy-Authorization header to set
221 // on requests, if applicable.
222 func (cm *connectMethod) proxyAuth() string {
223 if cm.proxyURL == nil {
226 proxyInfo := cm.proxyURL.RawUserinfo
228 return "Basic " + base64.URLEncoding.EncodeToString([]byte(proxyInfo))
233 func (t *Transport) putIdleConn(pconn *persistConn) {
236 if t.DisableKeepAlives || t.MaxIdleConnsPerHost < 0 {
240 if pconn.isBroken() {
243 key := pconn.cacheKey
244 max := t.MaxIdleConnsPerHost
246 max = DefaultMaxIdleConnsPerHost
248 if len(t.idleConn[key]) >= max {
252 t.idleConn[key] = append(t.idleConn[key], pconn)
255 func (t *Transport) getIdleConn(cm *connectMethod) (pconn *persistConn) {
258 if t.idleConn == nil {
259 t.idleConn = make(map[string][]*persistConn)
263 pconns, ok := t.idleConn[key]
267 if len(pconns) == 1 {
269 delete(t.idleConn, key)
271 // 2 or more cached connections; pop last
273 pconn = pconns[len(pconns)-1]
274 t.idleConn[key] = pconns[0 : len(pconns)-1]
276 if !pconn.isBroken() {
283 func (t *Transport) dial(network, addr string) (c net.Conn, err os.Error) {
285 return t.Dial(network, addr)
287 return net.Dial(network, addr)
290 // getConn dials and creates a new persistConn to the target as
291 // specified in the connectMethod. This includes doing a proxy CONNECT
292 // and/or setting up TLS. If this doesn't return an error, the persistConn
293 // is ready to write requests to.
294 func (t *Transport) getConn(cm *connectMethod) (*persistConn, os.Error) {
295 if pc := t.getIdleConn(cm); pc != nil {
299 conn, err := t.dial("tcp", cm.addr())
301 if cm.proxyURL != nil {
302 err = fmt.Errorf("http: error connecting to proxy %s: %v", cm.proxyURL, err)
309 pconn := &persistConn{
311 cacheKey: cm.String(),
313 reqch: make(chan requestAndChan, 50),
317 case cm.proxyURL == nil:
319 case cm.targetScheme == "http":
322 pconn.mutateHeaderFunc = func(h Header) {
323 h.Set("Proxy-Authorization", pa)
326 case cm.targetScheme == "https":
327 connectReq := &Request{
329 URL: &url.URL{RawPath: cm.targetAddr},
331 Header: make(Header),
334 connectReq.Header.Set("Proxy-Authorization", pa)
336 connectReq.Write(conn)
339 // Okay to use and discard buffered reader here, because
340 // TLS server will not speak until spoken to.
341 br := bufio.NewReader(conn)
342 resp, err := ReadResponse(br, connectReq)
347 if resp.StatusCode != 200 {
348 f := strings.SplitN(resp.Status, " ", 2)
350 return nil, os.NewError(f[1])
354 if cm.targetScheme == "https" {
355 // Initiate TLS and check remote host name against certificate.
356 conn = tls.Client(conn, t.TLSClientConfig)
357 if err = conn.(*tls.Conn).Handshake(); err != nil {
360 if err = conn.(*tls.Conn).VerifyHostname(cm.tlsHost()); err != nil {
366 pconn.br = bufio.NewReader(pconn.conn)
367 pconn.cc = NewClientConn(conn, pconn.br)
372 // useProxy returns true if requests to addr should use a proxy,
373 // according to the NO_PROXY or no_proxy environment variable.
374 // addr is always a canonicalAddr with a host and port.
375 func useProxy(addr string) bool {
379 host, _, err := net.SplitHostPort(addr)
383 if host == "localhost" {
386 if ip := net.ParseIP(host); ip != nil {
392 no_proxy := getenvEitherCase("NO_PROXY")
397 addr = strings.ToLower(strings.TrimSpace(addr))
399 addr = addr[:strings.LastIndex(addr, ":")]
402 for _, p := range strings.Split(no_proxy, ",") {
403 p = strings.ToLower(strings.TrimSpace(p))
408 p = p[:strings.LastIndex(p, ":")]
410 if addr == p || (p[0] == '.' && (strings.HasSuffix(addr, p) || addr == p[1:])) {
417 // connectMethod is the map key (in its String form) for keeping persistent
418 // TCP connections alive for subsequent HTTP requests.
420 // A connect method may be of the following types:
422 // Cache key form Description
423 // ----------------- -------------------------
424 // ||http|foo.com http directly to server, no proxy
425 // ||https|foo.com https directly to server, no proxy
426 // http://proxy.com|https|foo.com http to proxy, then CONNECT to foo.com
427 // http://proxy.com|http http to proxy, http to anywhere after that
429 // Note: no support to https to the proxy yet.
431 type connectMethod struct {
432 proxyURL *url.URL // nil for no proxy, else full proxy URL
433 targetScheme string // "http" or "https"
434 targetAddr string // Not used if proxy + http targetScheme (4th example in table)
437 func (ck *connectMethod) String() string {
439 if ck.proxyURL != nil {
440 proxyStr = ck.proxyURL.String()
442 return strings.Join([]string{proxyStr, ck.targetScheme, ck.targetAddr}, "|")
445 // addr returns the first hop "host:port" to which we need to TCP connect.
446 func (cm *connectMethod) addr() string {
447 if cm.proxyURL != nil {
448 return canonicalAddr(cm.proxyURL)
453 // tlsHost returns the host name to match against the peer's
455 func (cm *connectMethod) tlsHost() string {
458 h = h[:strings.LastIndex(h, ":")]
463 // persistConn wraps a connection, usually a persistent one
464 // (but may be used for non-keep-alive requests as well)
465 type persistConn struct {
467 cacheKey string // its connectMethod.String()
471 reqch chan requestAndChan // written by roundTrip(); read by readLoop()
474 // mutateHeaderFunc is an optional func to modify extra
475 // headers on each outbound request before it's written. (the
476 // original Request given to RoundTrip is not modified)
477 mutateHeaderFunc func(Header)
479 lk sync.Mutex // guards numExpectedResponses and broken
480 numExpectedResponses int
481 broken bool // an error has happened on this connection; marked broken so it's not reused.
484 func (pc *persistConn) isBroken() bool {
490 func (pc *persistConn) expectingResponse() bool {
493 return pc.numExpectedResponses > 0
496 var remoteSideClosedFunc func(os.Error) bool // or nil to use default
498 func remoteSideClosed(err os.Error) bool {
499 if err == os.EOF || err == os.EINVAL {
502 if remoteSideClosedFunc != nil {
503 return remoteSideClosedFunc(err)
508 func (pc *persistConn) readLoop() {
511 pb, err := pc.br.Peek(1)
513 if remoteSideClosed(err) && !pc.expectingResponse() {
514 // Remote side closed on us. (We probably hit their
520 if !pc.expectingResponse() {
521 log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v",
528 resp, err := pc.cc.readUsing(rc.req, func(buf *bufio.Reader, forReq *Request) (*Response, os.Error) {
529 resp, err := ReadResponse(buf, forReq)
530 if err != nil || resp.ContentLength == 0 {
533 if rc.addedGzip && resp.Header.Get("Content-Encoding") == "gzip" {
534 resp.Header.Del("Content-Encoding")
535 resp.Header.Del("Content-Length")
536 resp.ContentLength = -1
537 gzReader, err := gzip.NewReader(resp.Body)
542 resp.Body = &readFirstCloseBoth{&discardOnCloseReadCloser{gzReader}, resp.Body}
544 resp.Body = &bodyEOFSignal{body: resp.Body}
548 if err == ErrPersistEOF {
549 // Succeeded, but we can't send any more
550 // persistent connections on this again. We
551 // hide this error to upstream callers.
554 } else if err != nil || rc.req.Close {
558 hasBody := resp != nil && resp.ContentLength != 0
559 var waitForBodyRead chan bool
562 waitForBodyRead = make(chan bool)
563 resp.Body.(*bodyEOFSignal).fn = func() {
565 waitForBodyRead <- true
568 // When there's no response body, we immediately
569 // reuse the TCP connection (putIdleConn), but
570 // we need to prevent ClientConn.Read from
571 // closing the Response.Body on the next
572 // loop, otherwise it might close the body
573 // before the client code has had a chance to
574 // read it (even though it'll just be 0, EOF).
583 rc.ch <- responseAndError{resp, err}
585 // Wait for the just-returned response body to be fully consumed
586 // before we race and peek on the underlying bufio reader.
587 if waitForBodyRead != nil {
593 type responseAndError struct {
598 type requestAndChan struct {
600 ch chan responseAndError
602 // did the Transport (as opposed to the client code) add an
603 // Accept-Encoding gzip header? only if it we set it do
604 // we transparently decode the gzip.
608 func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err os.Error) {
609 if pc.mutateHeaderFunc != nil {
610 pc.mutateHeaderFunc(req.extraHeaders())
613 // Ask for a compressed version if the caller didn't set their
614 // own value for Accept-Encoding. We only attempted to
615 // uncompress the gzip stream if we were the layer that
617 requestedGzip := false
618 if !pc.t.DisableCompression && req.Header.Get("Accept-Encoding") == "" {
619 // Request gzip only, not deflate. Deflate is ambiguous and
620 // not as universally supported anyway.
621 // See: http://www.gzip.org/zlib/zlib_faq.html#faq38
623 req.extraHeaders().Set("Accept-Encoding", "gzip")
627 pc.numExpectedResponses++
630 pc.cc.writeReq = func(r *Request, w io.Writer) os.Error {
631 return r.write(w, pc.isProxy, req.extra)
634 err = pc.cc.Write(req.Request)
640 ch := make(chan responseAndError, 1)
641 pc.reqch <- requestAndChan{req.Request, ch, requestedGzip}
644 pc.numExpectedResponses--
647 return re.res, re.err
650 func (pc *persistConn) close() {
656 pc.mutateHeaderFunc = nil
659 var portMap = map[string]string{
664 // canonicalAddr returns url.Host but always with a ":port" suffix
665 func canonicalAddr(url *url.URL) string {
668 return addr + ":" + portMap[url.Scheme]
673 func responseIsKeepAlive(res *Response) bool {
674 // TODO: implement. for now just always shutting down the connection.
678 // bodyEOFSignal wraps a ReadCloser but runs fn (if non-nil) at most
679 // once, right before the final Read() or Close() call returns, but after
680 // EOF has been seen.
681 type bodyEOFSignal struct {
687 func (es *bodyEOFSignal) Read(p []byte) (n int, err os.Error) {
688 n, err = es.body.Read(p)
689 if es.isClosed && n > 0 {
690 panic("http: unexpected bodyEOFSignal Read after Close; see issue 1725")
692 if err == os.EOF && es.fn != nil {
699 func (es *bodyEOFSignal) Close() (err os.Error) {
704 err = es.body.Close()
705 if err == nil && es.fn != nil {
712 type readFirstCloseBoth struct {
717 func (r *readFirstCloseBoth) Close() os.Error {
718 if err := r.ReadCloser.Close(); err != nil {
722 if err := r.Closer.Close(); err != nil {
728 // discardOnCloseReadCloser consumes all its input on Close.
729 type discardOnCloseReadCloser struct {
733 func (d *discardOnCloseReadCloser) Close() os.Error {
734 io.Copy(ioutil.Discard, d.ReadCloser) // ignore errors; likely invalid or already closed
735 return d.ReadCloser.Close()