github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/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  
    51  func singleJoiningSlash(a, b string) string {
    52  	aslash := strings.HasSuffix(a, "/")
    53  	bslash := strings.HasPrefix(b, "/")
    54  	switch {
    55  	case aslash && bslash:
    56  		return a + b[1:]
    57  	case !aslash && !bslash:
    58  		return a + "/" + b
    59  	}
    60  	return a + b
    61  }
    62  
    63  // NewSingleHostReverseProxy returns a new ReverseProxy that routes
    64  // URLs to the scheme, host, and base path provided in target. If the
    65  // target's path is "/base" and the incoming request was for "/dir",
    66  // the target request will be for /base/dir.
    67  // NewSingleHostReverseProxy does not rewrite the Host header.
    68  // To rewrite Host headers, use ReverseProxy directly with a custom
    69  // Director policy.
    70  func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
    71  	targetQuery := target.RawQuery
    72  	director := func(req *http.Request) {
    73  		req.URL.Scheme = target.Scheme
    74  		req.URL.Host = target.Host
    75  		req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
    76  		if targetQuery == "" || req.URL.RawQuery == "" {
    77  			req.URL.RawQuery = targetQuery + req.URL.RawQuery
    78  		} else {
    79  			req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
    80  		}
    81  	}
    82  	return &ReverseProxy{Director: director}
    83  }
    84  
    85  func copyHeader(dst, src http.Header) {
    86  	for k, vv := range src {
    87  		for _, v := range vv {
    88  			dst.Add(k, v)
    89  		}
    90  	}
    91  }
    92  
    93  // Hop-by-hop headers. These are removed when sent to the backend.
    94  // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
    95  var hopHeaders = []string{
    96  	"Connection",
    97  	"Keep-Alive",
    98  	"Proxy-Authenticate",
    99  	"Proxy-Authorization",
   100  	"Te", // canonicalized version of "TE"
   101  	"Trailers",
   102  	"Transfer-Encoding",
   103  	"Upgrade",
   104  }
   105  
   106  type requestCanceler interface {
   107  	CancelRequest(*http.Request)
   108  }
   109  
   110  type runOnFirstRead struct {
   111  	io.Reader // optional; nil means empty body
   112  
   113  	fn func() // Run before first Read, then set to nil
   114  }
   115  
   116  func (c *runOnFirstRead) Read(bs []byte) (int, error) {
   117  	if c.fn != nil {
   118  		c.fn()
   119  		c.fn = nil
   120  	}
   121  	if c.Reader == nil {
   122  		return 0, io.EOF
   123  	}
   124  	return c.Reader.Read(bs)
   125  }
   126  
   127  func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
   128  	transport := p.Transport
   129  	if transport == nil {
   130  		transport = http.DefaultTransport
   131  	}
   132  
   133  	outreq := new(http.Request)
   134  	*outreq = *req // includes shallow copies of maps, but okay
   135  
   136  	if closeNotifier, ok := rw.(http.CloseNotifier); ok {
   137  		if requestCanceler, ok := transport.(requestCanceler); ok {
   138  			reqDone := make(chan struct{})
   139  			defer close(reqDone)
   140  
   141  			clientGone := closeNotifier.CloseNotify()
   142  
   143  			outreq.Body = struct {
   144  				io.Reader
   145  				io.Closer
   146  			}{
   147  				Reader: &runOnFirstRead{
   148  					Reader: outreq.Body,
   149  					fn: func() {
   150  						go func() {
   151  							select {
   152  							case <-clientGone:
   153  								requestCanceler.CancelRequest(outreq)
   154  							case <-reqDone:
   155  							}
   156  						}()
   157  					},
   158  				},
   159  				Closer: outreq.Body,
   160  			}
   161  		}
   162  	}
   163  
   164  	p.Director(outreq)
   165  	outreq.Proto = "HTTP/1.1"
   166  	outreq.ProtoMajor = 1
   167  	outreq.ProtoMinor = 1
   168  	outreq.Close = false
   169  
   170  	// Remove hop-by-hop headers to the backend.  Especially
   171  	// important is "Connection" because we want a persistent
   172  	// connection, regardless of what the client sent to us.  This
   173  	// is modifying the same underlying map from req (shallow
   174  	// copied above) so we only copy it if necessary.
   175  	copiedHeaders := false
   176  	for _, h := range hopHeaders {
   177  		if outreq.Header.Get(h) != "" {
   178  			if !copiedHeaders {
   179  				outreq.Header = make(http.Header)
   180  				copyHeader(outreq.Header, req.Header)
   181  				copiedHeaders = true
   182  			}
   183  			outreq.Header.Del(h)
   184  		}
   185  	}
   186  
   187  	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   188  		// If we aren't the first proxy retain prior
   189  		// X-Forwarded-For information as a comma+space
   190  		// separated list and fold multiple headers into one.
   191  		if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
   192  			clientIP = strings.Join(prior, ", ") + ", " + clientIP
   193  		}
   194  		outreq.Header.Set("X-Forwarded-For", clientIP)
   195  	}
   196  
   197  	res, err := transport.RoundTrip(outreq)
   198  	if err != nil {
   199  		p.logf("http: proxy error: %v", err)
   200  		rw.WriteHeader(http.StatusInternalServerError)
   201  		return
   202  	}
   203  
   204  	for _, h := range hopHeaders {
   205  		res.Header.Del(h)
   206  	}
   207  
   208  	copyHeader(rw.Header(), res.Header)
   209  
   210  	// The "Trailer" header isn't included in the Transport's response,
   211  	// at least for *http.Transport. Build it up from Trailer.
   212  	if len(res.Trailer) > 0 {
   213  		var trailerKeys []string
   214  		for k := range res.Trailer {
   215  			trailerKeys = append(trailerKeys, k)
   216  		}
   217  		rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
   218  	}
   219  
   220  	rw.WriteHeader(res.StatusCode)
   221  	if len(res.Trailer) > 0 {
   222  		// Force chunking if we saw a response trailer.
   223  		// This prevents net/http from calculating the length for short
   224  		// bodies and adding a Content-Length.
   225  		if fl, ok := rw.(http.Flusher); ok {
   226  			fl.Flush()
   227  		}
   228  	}
   229  	p.copyResponse(rw, res.Body)
   230  	res.Body.Close() // close now, instead of defer, to populate res.Trailer
   231  	copyHeader(rw.Header(), res.Trailer)
   232  }
   233  
   234  func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
   235  	if p.FlushInterval != 0 {
   236  		if wf, ok := dst.(writeFlusher); ok {
   237  			mlw := &maxLatencyWriter{
   238  				dst:     wf,
   239  				latency: p.FlushInterval,
   240  				done:    make(chan bool),
   241  			}
   242  			go mlw.flushLoop()
   243  			defer mlw.stop()
   244  			dst = mlw
   245  		}
   246  	}
   247  
   248  	io.Copy(dst, src)
   249  }
   250  
   251  func (p *ReverseProxy) logf(format string, args ...interface{}) {
   252  	if p.ErrorLog != nil {
   253  		p.ErrorLog.Printf(format, args...)
   254  	} else {
   255  		log.Printf(format, args...)
   256  	}
   257  }
   258  
   259  type writeFlusher interface {
   260  	io.Writer
   261  	http.Flusher
   262  }
   263  
   264  type maxLatencyWriter struct {
   265  	dst     writeFlusher
   266  	latency time.Duration
   267  
   268  	lk   sync.Mutex // protects Write + Flush
   269  	done chan bool
   270  }
   271  
   272  func (m *maxLatencyWriter) Write(p []byte) (int, error) {
   273  	m.lk.Lock()
   274  	defer m.lk.Unlock()
   275  	return m.dst.Write(p)
   276  }
   277  
   278  func (m *maxLatencyWriter) flushLoop() {
   279  	t := time.NewTicker(m.latency)
   280  	defer t.Stop()
   281  	for {
   282  		select {
   283  		case <-m.done:
   284  			if onExitFlushLoop != nil {
   285  				onExitFlushLoop()
   286  			}
   287  			return
   288  		case <-t.C:
   289  			m.lk.Lock()
   290  			m.dst.Flush()
   291  			m.lk.Unlock()
   292  		}
   293  	}
   294  }
   295  
   296  func (m *maxLatencyWriter) stop() { m.done <- true }