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