github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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  	"Keep-Alive",
   110  	"Proxy-Authenticate",
   111  	"Proxy-Authorization",
   112  	"Te", // canonicalized version of "TE"
   113  	"Trailers",
   114  	"Transfer-Encoding",
   115  	"Upgrade",
   116  }
   117  
   118  type requestCanceler interface {
   119  	CancelRequest(*http.Request)
   120  }
   121  
   122  type runOnFirstRead struct {
   123  	io.Reader // optional; nil means empty body
   124  
   125  	fn func() // Run before first Read, then set to nil
   126  }
   127  
   128  func (c *runOnFirstRead) Read(bs []byte) (int, error) {
   129  	if c.fn != nil {
   130  		c.fn()
   131  		c.fn = nil
   132  	}
   133  	if c.Reader == nil {
   134  		return 0, io.EOF
   135  	}
   136  	return c.Reader.Read(bs)
   137  }
   138  
   139  func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
   140  	transport := p.Transport
   141  	if transport == nil {
   142  		transport = http.DefaultTransport
   143  	}
   144  
   145  	outreq := new(http.Request)
   146  	*outreq = *req // includes shallow copies of maps, but okay
   147  
   148  	if closeNotifier, ok := rw.(http.CloseNotifier); ok {
   149  		if requestCanceler, ok := transport.(requestCanceler); ok {
   150  			reqDone := make(chan struct{})
   151  			defer close(reqDone)
   152  
   153  			clientGone := closeNotifier.CloseNotify()
   154  
   155  			outreq.Body = struct {
   156  				io.Reader
   157  				io.Closer
   158  			}{
   159  				Reader: &runOnFirstRead{
   160  					Reader: outreq.Body,
   161  					fn: func() {
   162  						go func() {
   163  							select {
   164  							case <-clientGone:
   165  								requestCanceler.CancelRequest(outreq)
   166  							case <-reqDone:
   167  							}
   168  						}()
   169  					},
   170  				},
   171  				Closer: outreq.Body,
   172  			}
   173  		}
   174  	}
   175  
   176  	p.Director(outreq)
   177  	outreq.Proto = "HTTP/1.1"
   178  	outreq.ProtoMajor = 1
   179  	outreq.ProtoMinor = 1
   180  	outreq.Close = false
   181  
   182  	// Remove hop-by-hop headers to the backend.  Especially
   183  	// important is "Connection" because we want a persistent
   184  	// connection, regardless of what the client sent to us.  This
   185  	// is modifying the same underlying map from req (shallow
   186  	// copied above) so we only copy it if necessary.
   187  	copiedHeaders := false
   188  	for _, h := range hopHeaders {
   189  		if outreq.Header.Get(h) != "" {
   190  			if !copiedHeaders {
   191  				outreq.Header = make(http.Header)
   192  				copyHeader(outreq.Header, req.Header)
   193  				copiedHeaders = true
   194  			}
   195  			outreq.Header.Del(h)
   196  		}
   197  	}
   198  
   199  	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   200  		// If we aren't the first proxy retain prior
   201  		// X-Forwarded-For information as a comma+space
   202  		// separated list and fold multiple headers into one.
   203  		if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
   204  			clientIP = strings.Join(prior, ", ") + ", " + clientIP
   205  		}
   206  		outreq.Header.Set("X-Forwarded-For", clientIP)
   207  	}
   208  
   209  	res, err := transport.RoundTrip(outreq)
   210  	if err != nil {
   211  		p.logf("http: proxy error: %v", err)
   212  		rw.WriteHeader(http.StatusInternalServerError)
   213  		return
   214  	}
   215  
   216  	for _, h := range hopHeaders {
   217  		res.Header.Del(h)
   218  	}
   219  
   220  	copyHeader(rw.Header(), res.Header)
   221  
   222  	// The "Trailer" header isn't included in the Transport's response,
   223  	// at least for *http.Transport. Build it up from Trailer.
   224  	if len(res.Trailer) > 0 {
   225  		var trailerKeys []string
   226  		for k := range res.Trailer {
   227  			trailerKeys = append(trailerKeys, k)
   228  		}
   229  		rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
   230  	}
   231  
   232  	rw.WriteHeader(res.StatusCode)
   233  	if len(res.Trailer) > 0 {
   234  		// Force chunking if we saw a response trailer.
   235  		// This prevents net/http from calculating the length for short
   236  		// bodies and adding a Content-Length.
   237  		if fl, ok := rw.(http.Flusher); ok {
   238  			fl.Flush()
   239  		}
   240  	}
   241  	p.copyResponse(rw, res.Body)
   242  	res.Body.Close() // close now, instead of defer, to populate res.Trailer
   243  	copyHeader(rw.Header(), res.Trailer)
   244  }
   245  
   246  func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
   247  	if p.FlushInterval != 0 {
   248  		if wf, ok := dst.(writeFlusher); ok {
   249  			mlw := &maxLatencyWriter{
   250  				dst:     wf,
   251  				latency: p.FlushInterval,
   252  				done:    make(chan bool),
   253  			}
   254  			go mlw.flushLoop()
   255  			defer mlw.stop()
   256  			dst = mlw
   257  		}
   258  	}
   259  
   260  	var buf []byte
   261  	if p.BufferPool != nil {
   262  		buf = p.BufferPool.Get()
   263  	}
   264  	io.CopyBuffer(dst, src, buf)
   265  	if p.BufferPool != nil {
   266  		p.BufferPool.Put(buf)
   267  	}
   268  }
   269  
   270  func (p *ReverseProxy) logf(format string, args ...interface{}) {
   271  	if p.ErrorLog != nil {
   272  		p.ErrorLog.Printf(format, args...)
   273  	} else {
   274  		log.Printf(format, args...)
   275  	}
   276  }
   277  
   278  type writeFlusher interface {
   279  	io.Writer
   280  	http.Flusher
   281  }
   282  
   283  type maxLatencyWriter struct {
   284  	dst     writeFlusher
   285  	latency time.Duration
   286  
   287  	lk   sync.Mutex // protects Write + Flush
   288  	done chan bool
   289  }
   290  
   291  func (m *maxLatencyWriter) Write(p []byte) (int, error) {
   292  	m.lk.Lock()
   293  	defer m.lk.Unlock()
   294  	return m.dst.Write(p)
   295  }
   296  
   297  func (m *maxLatencyWriter) flushLoop() {
   298  	t := time.NewTicker(m.latency)
   299  	defer t.Stop()
   300  	for {
   301  		select {
   302  		case <-m.done:
   303  			if onExitFlushLoop != nil {
   304  				onExitFlushLoop()
   305  			}
   306  			return
   307  		case <-t.C:
   308  			m.lk.Lock()
   309  			m.dst.Flush()
   310  			m.lk.Unlock()
   311  		}
   312  	}
   313  }
   314  
   315  func (m *maxLatencyWriter) stop() { m.done <- true }