github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/net/http/httputil/reverseproxy.go (about)

     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.
     4  
     5  // HTTP reverse proxy handler
     6  
     7  package httputil
     8  
     9  import (
    10  	"io"
    11  	"log"
    12  	"net"
    13  	"net/http"
    14  	"net/url"
    15  	"strings"
    16  	"sync"
    17  	"time"
    18  )
    19  
    20  // onExitFlushLoop is a callback set by tests to detect the state of the
    21  // flushLoop() goroutine.
    22  var onExitFlushLoop func()
    23  
    24  // ReverseProxy is an HTTP Handler that takes an incoming request and
    25  // sends it to another server, proxying the response back to the
    26  // client.
    27  type ReverseProxy struct {
    28  	// Director must be a function which modifies
    29  	// the request into a new request to be sent
    30  	// using Transport. Its response is then copied
    31  	// back to the original client unmodified.
    32  	Director func(*http.Request)
    33  
    34  	// The transport used to perform proxy requests.
    35  	// If nil, http.DefaultTransport is used.
    36  	Transport http.RoundTripper
    37  
    38  	// FlushInterval specifies the flush interval
    39  	// to flush to the client while copying the
    40  	// response body.
    41  	// If zero, no periodic flushing is done.
    42  	FlushInterval time.Duration
    43  
    44  	// ErrorLog specifies an optional logger for errors
    45  	// that occur when attempting to proxy the request.
    46  	// If nil, logging goes to os.Stderr via the log package's
    47  	// standard logger.
    48  	ErrorLog *log.Logger
    49  
    50  	// BufferPool optionally specifies a buffer pool to
    51  	// get byte slices for use by io.CopyBuffer when
    52  	// copying HTTP response bodies.
    53  	BufferPool BufferPool
    54  }
    55  
    56  // A BufferPool is an interface for getting and returning temporary
    57  // byte slices for use by io.CopyBuffer.
    58  type BufferPool interface {
    59  	Get() []byte
    60  	Put([]byte)
    61  }
    62  
    63  func singleJoiningSlash(a, b string) string {
    64  	aslash := strings.HasSuffix(a, "/")
    65  	bslash := strings.HasPrefix(b, "/")
    66  	switch {
    67  	case aslash && bslash:
    68  		return a + b[1:]
    69  	case !aslash && !bslash:
    70  		return a + "/" + b
    71  	}
    72  	return a + b
    73  }
    74  
    75  // NewSingleHostReverseProxy returns a new ReverseProxy that routes
    76  // URLs to the scheme, host, and base path provided in target. If the
    77  // target's path is "/base" and the incoming request was for "/dir",
    78  // the target request will be for /base/dir.
    79  // NewSingleHostReverseProxy does not rewrite the Host header.
    80  // To rewrite Host headers, use ReverseProxy directly with a custom
    81  // Director policy.
    82  func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
    83  	targetQuery := target.RawQuery
    84  	director := func(req *http.Request) {
    85  		req.URL.Scheme = target.Scheme
    86  		req.URL.Host = target.Host
    87  		req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
    88  		if targetQuery == "" || req.URL.RawQuery == "" {
    89  			req.URL.RawQuery = targetQuery + req.URL.RawQuery
    90  		} else {
    91  			req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
    92  		}
    93  	}
    94  	return &ReverseProxy{Director: director}
    95  }
    96  
    97  func copyHeader(dst, src http.Header) {
    98  	for k, vv := range src {
    99  		for _, v := range vv {
   100  			dst.Add(k, v)
   101  		}
   102  	}
   103  }
   104  
   105  // Hop-by-hop headers. These are removed when sent to the backend.
   106  // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
   107  var hopHeaders = []string{
   108  	"Connection",
   109  	"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
   110  	"Keep-Alive",
   111  	"Proxy-Authenticate",
   112  	"Proxy-Authorization",
   113  	"Te",      // canonicalized version of "TE"
   114  	"Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
   115  	"Transfer-Encoding",
   116  	"Upgrade",
   117  }
   118  
   119  type requestCanceler interface {
   120  	CancelRequest(*http.Request)
   121  }
   122  
   123  type runOnFirstRead struct {
   124  	io.Reader // optional; nil means empty body
   125  
   126  	fn func() // Run before first Read, then set to nil
   127  }
   128  
   129  func (c *runOnFirstRead) Read(bs []byte) (int, error) {
   130  	if c.fn != nil {
   131  		c.fn()
   132  		c.fn = nil
   133  	}
   134  	if c.Reader == nil {
   135  		return 0, io.EOF
   136  	}
   137  	return c.Reader.Read(bs)
   138  }
   139  
   140  func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
   141  	transport := p.Transport
   142  	if transport == nil {
   143  		transport = http.DefaultTransport
   144  	}
   145  
   146  	outreq := new(http.Request)
   147  	*outreq = *req // includes shallow copies of maps, but okay
   148  
   149  	if closeNotifier, ok := rw.(http.CloseNotifier); ok {
   150  		if requestCanceler, ok := transport.(requestCanceler); ok {
   151  			reqDone := make(chan struct{})
   152  			defer close(reqDone)
   153  
   154  			clientGone := closeNotifier.CloseNotify()
   155  
   156  			outreq.Body = struct {
   157  				io.Reader
   158  				io.Closer
   159  			}{
   160  				Reader: &runOnFirstRead{
   161  					Reader: outreq.Body,
   162  					fn: func() {
   163  						go func() {
   164  							select {
   165  							case <-clientGone:
   166  								requestCanceler.CancelRequest(outreq)
   167  							case <-reqDone:
   168  							}
   169  						}()
   170  					},
   171  				},
   172  				Closer: outreq.Body,
   173  			}
   174  		}
   175  	}
   176  
   177  	p.Director(outreq)
   178  	outreq.Proto = "HTTP/1.1"
   179  	outreq.ProtoMajor = 1
   180  	outreq.ProtoMinor = 1
   181  	outreq.Close = false
   182  
   183  	// Remove hop-by-hop headers to the backend. Especially
   184  	// important is "Connection" because we want a persistent
   185  	// connection, regardless of what the client sent to us. This
   186  	// is modifying the same underlying map from req (shallow
   187  	// copied above) so we only copy it if necessary.
   188  	copiedHeaders := false
   189  	for _, h := range hopHeaders {
   190  		if outreq.Header.Get(h) != "" {
   191  			if !copiedHeaders {
   192  				outreq.Header = make(http.Header)
   193  				copyHeader(outreq.Header, req.Header)
   194  				copiedHeaders = true
   195  			}
   196  			outreq.Header.Del(h)
   197  		}
   198  	}
   199  
   200  	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   201  		// If we aren't the first proxy retain prior
   202  		// X-Forwarded-For information as a comma+space
   203  		// separated list and fold multiple headers into one.
   204  		if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
   205  			clientIP = strings.Join(prior, ", ") + ", " + clientIP
   206  		}
   207  		outreq.Header.Set("X-Forwarded-For", clientIP)
   208  	}
   209  
   210  	res, err := transport.RoundTrip(outreq)
   211  	if err != nil {
   212  		p.logf("http: proxy error: %v", err)
   213  		rw.WriteHeader(http.StatusBadGateway)
   214  		return
   215  	}
   216  
   217  	for _, h := range hopHeaders {
   218  		res.Header.Del(h)
   219  	}
   220  
   221  	copyHeader(rw.Header(), res.Header)
   222  
   223  	// The "Trailer" header isn't included in the Transport's response,
   224  	// at least for *http.Transport. Build it up from Trailer.
   225  	if len(res.Trailer) > 0 {
   226  		var trailerKeys []string
   227  		for k := range res.Trailer {
   228  			trailerKeys = append(trailerKeys, k)
   229  		}
   230  		rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
   231  	}
   232  
   233  	rw.WriteHeader(res.StatusCode)
   234  	if len(res.Trailer) > 0 {
   235  		// Force chunking if we saw a response trailer.
   236  		// This prevents net/http from calculating the length for short
   237  		// bodies and adding a Content-Length.
   238  		if fl, ok := rw.(http.Flusher); ok {
   239  			fl.Flush()
   240  		}
   241  	}
   242  	p.copyResponse(rw, res.Body)
   243  	res.Body.Close() // close now, instead of defer, to populate res.Trailer
   244  	copyHeader(rw.Header(), res.Trailer)
   245  }
   246  
   247  func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
   248  	if p.FlushInterval != 0 {
   249  		if wf, ok := dst.(writeFlusher); ok {
   250  			mlw := &maxLatencyWriter{
   251  				dst:     wf,
   252  				latency: p.FlushInterval,
   253  				done:    make(chan bool),
   254  			}
   255  			go mlw.flushLoop()
   256  			defer mlw.stop()
   257  			dst = mlw
   258  		}
   259  	}
   260  
   261  	var buf []byte
   262  	if p.BufferPool != nil {
   263  		buf = p.BufferPool.Get()
   264  	}
   265  	io.CopyBuffer(dst, src, buf)
   266  	if p.BufferPool != nil {
   267  		p.BufferPool.Put(buf)
   268  	}
   269  }
   270  
   271  func (p *ReverseProxy) logf(format string, args ...interface{}) {
   272  	if p.ErrorLog != nil {
   273  		p.ErrorLog.Printf(format, args...)
   274  	} else {
   275  		log.Printf(format, args...)
   276  	}
   277  }
   278  
   279  type writeFlusher interface {
   280  	io.Writer
   281  	http.Flusher
   282  }
   283  
   284  type maxLatencyWriter struct {
   285  	dst     writeFlusher
   286  	latency time.Duration
   287  
   288  	mu   sync.Mutex // protects Write + Flush
   289  	done chan bool
   290  }
   291  
   292  func (m *maxLatencyWriter) Write(p []byte) (int, error) {
   293  	m.mu.Lock()
   294  	defer m.mu.Unlock()
   295  	return m.dst.Write(p)
   296  }
   297  
   298  func (m *maxLatencyWriter) flushLoop() {
   299  	t := time.NewTicker(m.latency)
   300  	defer t.Stop()
   301  	for {
   302  		select {
   303  		case <-m.done:
   304  			if onExitFlushLoop != nil {
   305  				onExitFlushLoop()
   306  			}
   307  			return
   308  		case <-t.C:
   309  			m.mu.Lock()
   310  			m.dst.Flush()
   311  			m.mu.Unlock()
   312  		}
   313  	}
   314  }
   315  
   316  func (m *maxLatencyWriter) stop() { m.done <- true }