...

Source file src/net/http/httputil/reverseproxy.go

Documentation: net/http/httputil

     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  	"context"
    11  	"errors"
    12  	"fmt"
    13  	"internal/godebug"
    14  	"io"
    15  	"log"
    16  	"mime"
    17  	"net"
    18  	"net/http"
    19  	"net/http/httptrace"
    20  	"net/http/internal/ascii"
    21  	"net/textproto"
    22  	"net/url"
    23  	"strings"
    24  	"sync"
    25  	"sync/atomic"
    26  	"time"
    27  
    28  	"golang.org/x/net/http/httpguts"
    29  )
    30  
    31  // A ProxyRequest contains a request to be rewritten by a [ReverseProxy].
    32  type ProxyRequest struct {
    33  	// In is the request received by the proxy.
    34  	// The Rewrite function must not modify In.
    35  	In *http.Request
    36  
    37  	// Out is the request which will be sent by the proxy.
    38  	// The Rewrite function may modify or replace this request.
    39  	// Hop-by-hop headers are removed from this request
    40  	// before Rewrite is called.
    41  	Out *http.Request
    42  }
    43  
    44  // SetURL routes the outbound request to the scheme, host, and base path
    45  // provided in target. If the target's path is "/base" and the incoming
    46  // request was for "/dir", the target request will be for "/base/dir".
    47  // To route requests without joining the incoming path,
    48  // set r.Out.URL directly.
    49  //
    50  // SetURL rewrites the outbound Host header to match the target's host.
    51  // To preserve the inbound request's Host header (the default behavior
    52  // of [NewSingleHostReverseProxy]):
    53  //
    54  //	rewriteFunc := func(r *httputil.ProxyRequest) {
    55  //		r.SetURL(url)
    56  //		r.Out.Host = r.In.Host
    57  //	}
    58  func (r *ProxyRequest) SetURL(target *url.URL) {
    59  	rewriteRequestURL(r.Out, target)
    60  	r.Out.Host = ""
    61  }
    62  
    63  // SetXForwarded sets the X-Forwarded-For, X-Forwarded-Host, and
    64  // X-Forwarded-Proto headers of the outbound request.
    65  //
    66  //   - The X-Forwarded-For header is set to the client IP address.
    67  //   - The X-Forwarded-Host header is set to the host name requested
    68  //     by the client.
    69  //   - The X-Forwarded-Proto header is set to "http" or "https", depending
    70  //     on whether the inbound request was made on a TLS-enabled connection.
    71  //
    72  // If the outbound request contains an existing X-Forwarded-For header,
    73  // SetXForwarded appends the client IP address to it. To append to the
    74  // inbound request's X-Forwarded-For header (the default behavior of
    75  // [ReverseProxy] when using a Director function), copy the header
    76  // from the inbound request before calling SetXForwarded:
    77  //
    78  //	rewriteFunc := func(r *httputil.ProxyRequest) {
    79  //		r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
    80  //		r.SetXForwarded()
    81  //	}
    82  func (r *ProxyRequest) SetXForwarded() {
    83  	clientIP, _, err := net.SplitHostPort(r.In.RemoteAddr)
    84  	if err == nil {
    85  		prior := r.Out.Header["X-Forwarded-For"]
    86  		if len(prior) > 0 {
    87  			clientIP = strings.Join(prior, ", ") + ", " + clientIP
    88  		}
    89  		r.Out.Header.Set("X-Forwarded-For", clientIP)
    90  	} else {
    91  		r.Out.Header.Del("X-Forwarded-For")
    92  	}
    93  	r.Out.Header.Set("X-Forwarded-Host", r.In.Host)
    94  	if r.In.TLS == nil {
    95  		r.Out.Header.Set("X-Forwarded-Proto", "http")
    96  	} else {
    97  		r.Out.Header.Set("X-Forwarded-Proto", "https")
    98  	}
    99  }
   100  
   101  // ReverseProxy is an HTTP Handler that takes an incoming request and
   102  // sends it to another server, proxying the response back to the
   103  // client.
   104  //
   105  // 1xx responses are forwarded to the client if the underlying
   106  // transport supports ClientTrace.Got1xxResponse.
   107  //
   108  // Hop-by-hop headers (see RFC 9110, section 7.6.1), including
   109  // Connection, Proxy-Connection, Keep-Alive, Proxy-Authenticate,
   110  // Proxy-Authorization, TE, Trailer, Transfer-Encoding, and Upgrade,
   111  // are removed from client requests and backend responses.
   112  // The Rewrite function may be used to add hop-by-hop headers to the request,
   113  // and the ModifyResponse function may be used to remove them from the response.
   114  type ReverseProxy struct {
   115  	// Rewrite must be a function which modifies
   116  	// the request into a new request to be sent
   117  	// using Transport. Its response is then copied
   118  	// back to the original client unmodified.
   119  	// Rewrite must not access the provided ProxyRequest
   120  	// or its contents after returning.
   121  	//
   122  	// The Forwarded, X-Forwarded, X-Forwarded-Host,
   123  	// and X-Forwarded-Proto headers are removed from the
   124  	// outbound request before Rewrite is called. See also
   125  	// the ProxyRequest.SetXForwarded method.
   126  	//
   127  	// Unparsable query parameters are removed from the
   128  	// outbound request before Rewrite is called.
   129  	// The Rewrite function may copy the inbound URL's
   130  	// RawQuery to the outbound URL to preserve the original
   131  	// parameter string. Note that this can lead to security
   132  	// issues if the proxy's interpretation of query parameters
   133  	// does not match that of the downstream server.
   134  	//
   135  	// At most one of Rewrite or Director may be set.
   136  	Rewrite func(*ProxyRequest)
   137  
   138  	// The transport used to perform proxy requests.
   139  	// If nil, http.DefaultTransport is used.
   140  	Transport http.RoundTripper
   141  
   142  	// FlushInterval specifies the flush interval
   143  	// to flush to the client while copying the
   144  	// response body.
   145  	// If zero, no periodic flushing is done.
   146  	// A negative value means to flush immediately
   147  	// after each write to the client.
   148  	// The FlushInterval is ignored when ReverseProxy
   149  	// recognizes a response as a streaming response, or
   150  	// if its ContentLength is -1; for such responses, writes
   151  	// are flushed to the client immediately.
   152  	FlushInterval time.Duration
   153  
   154  	// ErrorLog specifies an optional logger for errors
   155  	// that occur when attempting to proxy the request.
   156  	// If nil, logging is done via the log package's standard logger.
   157  	ErrorLog *log.Logger
   158  
   159  	// BufferPool optionally specifies a buffer pool to
   160  	// get byte slices for use by io.CopyBuffer when
   161  	// copying HTTP response bodies.
   162  	BufferPool BufferPool
   163  
   164  	// ModifyResponse is an optional function that modifies the
   165  	// Response from the backend. It is called if the backend
   166  	// returns a response at all, with any HTTP status code.
   167  	// If the backend is unreachable, the optional ErrorHandler is
   168  	// called without any call to ModifyResponse.
   169  	//
   170  	// Hop-by-hop headers are removed from the response before
   171  	// calling ModifyResponse. ModifyResponse may need to remove
   172  	// additional headers to fit its deployment model, such as Alt-Svc.
   173  	//
   174  	// If ModifyResponse returns an error, ErrorHandler is called
   175  	// with its error value. If ErrorHandler is nil, its default
   176  	// implementation is used.
   177  	ModifyResponse func(*http.Response) error
   178  
   179  	// ErrorHandler is an optional function that handles errors
   180  	// reaching the backend or errors from ModifyResponse.
   181  	//
   182  	// If nil, the default is to log the provided error and return
   183  	// a 502 Status Bad Gateway response.
   184  	ErrorHandler func(http.ResponseWriter, *http.Request, error)
   185  
   186  	// Director is deprecated. Use Rewrite instead.
   187  	//
   188  	// This function is insecure:
   189  	//
   190  	//   - Hop-by-hop headers are removed from the request after Director
   191  	//     returns, which can remove headers added by Director.
   192  	//     A client can designate headers as hop-by-hop by listing them
   193  	//     in the Connection header, so this permits a malicious client
   194  	//     to remove any headers that may be added by Director.
   195  	//
   196  	//   - X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto
   197  	//     headers in inbound requests are preserved by default,
   198  	//     which can permit IP spoofing if the Director function is
   199  	//     not careful to remove these headers.
   200  	//
   201  	// Rewrite addresses these issues.
   202  	//
   203  	// As an example of converting a Director function to Rewrite:
   204  	//
   205  	//	// ReverseProxy with a Director function.
   206  	//	proxy := &httputil.ReverseProxy{
   207  	//		Director: func(req *http.Request) {
   208  	//			req.URL.Scheme = "https"
   209  	//			req.URL.Host = proxyHost
   210  	//
   211  	//			// A malicious client can remove this header.
   212  	//			req.Header.Set("Some-Header", "some-header-value")
   213  	//
   214  	//			// X-Forwarded-* headers sent by the client are preserved,
   215  	//			// since Director did not remove them.
   216  	//		},
   217  	//	}
   218  	//
   219  	//	// ReverseProxy with a Rewrite function.
   220  	//	proxy := &httputil.ReverseProxy{
   221  	//		Rewrite: func(preq *httputil.ProxyRequest) {
   222  	//			// See also ProxyRequest.SetURL.
   223  	//			preq.Out.URL.Scheme = "https"
   224  	//			preq.Out.URL.Host = proxyHost
   225  	//
   226  	//			// This header cannot be affected by a malicious client.
   227  	//			preq.Out.Header.Set("Some-Header", "some-header-value")
   228  	//
   229  	//			// X-Forwarded- headers sent by the client have been
   230  	//			// removed from preq.Out.
   231  	//			// ProxyRequest.SetXForwarded optionally adds new ones.
   232  	//			preq.SetXForwarded()
   233  	//		},
   234  	//	}
   235  	//
   236  	// Director is a function which modifies
   237  	// the request into a new request to be sent
   238  	// using Transport. Its response is then copied
   239  	// back to the original client unmodified.
   240  	// Director must not access the provided Request
   241  	// after returning.
   242  	//
   243  	// By default, the X-Forwarded-For header is set to the
   244  	// value of the client IP address. If an X-Forwarded-For
   245  	// header already exists, the client IP is appended to the
   246  	// existing values. As a special case, if the header
   247  	// exists in the Request.Header map but has a nil value
   248  	// (such as when set by the Director func), the X-Forwarded-For
   249  	// header is not modified.
   250  	//
   251  	// To prevent IP spoofing, be sure to delete any pre-existing
   252  	// X-Forwarded-For header coming from the client or
   253  	// an untrusted proxy.
   254  	//
   255  	// Hop-by-hop headers are removed from the request after
   256  	// Director returns, which can remove headers added by
   257  	// Director. Use a Rewrite function instead to ensure
   258  	// modifications to the request are preserved.
   259  	//
   260  	// Unparsable query parameters are removed from the outbound
   261  	// request if Request.Form is set after Director returns.
   262  	//
   263  	// At most one of Rewrite or Director may be set.
   264  	//
   265  	// Deprecated: Use Rewrite instead.
   266  	Director func(*http.Request)
   267  }
   268  
   269  // A BufferPool is an interface for getting and returning temporary
   270  // byte slices for use by [io.CopyBuffer].
   271  type BufferPool interface {
   272  	Get() []byte
   273  	Put([]byte)
   274  }
   275  
   276  func singleJoiningSlash(a, b string) string {
   277  	aslash := strings.HasSuffix(a, "/")
   278  	bslash := strings.HasPrefix(b, "/")
   279  	switch {
   280  	case aslash && bslash:
   281  		return a + b[1:]
   282  	case !aslash && !bslash:
   283  		return a + "/" + b
   284  	}
   285  	return a + b
   286  }
   287  
   288  func joinURLPath(a, b *url.URL) (path, rawpath string) {
   289  	if a.RawPath == "" && b.RawPath == "" {
   290  		return singleJoiningSlash(a.Path, b.Path), ""
   291  	}
   292  	// Same as singleJoiningSlash, but uses EscapedPath to determine
   293  	// whether a slash should be added
   294  	apath := a.EscapedPath()
   295  	bpath := b.EscapedPath()
   296  
   297  	aslash := strings.HasSuffix(apath, "/")
   298  	bslash := strings.HasPrefix(bpath, "/")
   299  
   300  	switch {
   301  	case aslash && bslash:
   302  		return a.Path + b.Path[1:], apath + bpath[1:]
   303  	case !aslash && !bslash:
   304  		return a.Path + "/" + b.Path, apath + "/" + bpath
   305  	}
   306  	return a.Path + b.Path, apath + bpath
   307  }
   308  
   309  // NewSingleHostReverseProxy returns a new [ReverseProxy] that routes
   310  // URLs to the scheme, host, and base path provided in target. If the
   311  // target's path is "/base" and the incoming request was for "/dir",
   312  // the target request will be for /base/dir.
   313  //
   314  // NewSingleHostReverseProxy does not rewrite the Host header.
   315  //
   316  // For backwards compatibility reasons, NewSingleHostReverseProxy
   317  // returns a ReverseProxy using the deprecated Director function.
   318  // This proxy preserves X-Forwarded-* headers sent by the client.
   319  //
   320  // To customize the ReverseProxy behavior beyond what
   321  // NewSingleHostReverseProxy provides, use ReverseProxy directly
   322  // with a Rewrite function. The ProxyRequest SetURL method
   323  // may be used to route the outbound request. (Note that SetURL,
   324  // unlike NewSingleHostReverseProxy, rewrites the Host header
   325  // of the outbound request by default.)
   326  //
   327  //	proxy := &ReverseProxy{
   328  //		Rewrite: func(r *ProxyRequest) {
   329  //			r.SetURL(target)
   330  //			r.Out.Host = r.In.Host // if desired
   331  //		},
   332  //	}
   333  func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
   334  	director := func(req *http.Request) {
   335  		rewriteRequestURL(req, target)
   336  	}
   337  	return &ReverseProxy{Director: director}
   338  }
   339  
   340  func rewriteRequestURL(req *http.Request, target *url.URL) {
   341  	targetQuery := target.RawQuery
   342  	req.URL.Scheme = target.Scheme
   343  	req.URL.Host = target.Host
   344  	req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
   345  	if targetQuery == "" || req.URL.RawQuery == "" {
   346  		req.URL.RawQuery = targetQuery + req.URL.RawQuery
   347  	} else {
   348  		req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
   349  	}
   350  }
   351  
   352  func copyHeader(dst, src http.Header) {
   353  	for k, vv := range src {
   354  		for _, v := range vv {
   355  			dst.Add(k, v)
   356  		}
   357  	}
   358  }
   359  
   360  // Hop-by-hop headers. These are removed when sent to the backend.
   361  // As of RFC 7230, hop-by-hop headers are required to appear in the
   362  // Connection header field. These are the headers defined by the
   363  // obsoleted RFC 2616 (section 13.5.1) and are used for backward
   364  // compatibility.
   365  var hopHeaders = []string{
   366  	"Connection",
   367  	"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
   368  	"Keep-Alive",
   369  	"Proxy-Authenticate",
   370  	"Proxy-Authorization",
   371  	"Te",      // canonicalized version of "TE"
   372  	"Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
   373  	"Transfer-Encoding",
   374  	"Upgrade",
   375  }
   376  
   377  func (p *ReverseProxy) defaultErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
   378  	p.logf("http: proxy error: %v", err)
   379  	rw.WriteHeader(http.StatusBadGateway)
   380  }
   381  
   382  func (p *ReverseProxy) getErrorHandler() func(http.ResponseWriter, *http.Request, error) {
   383  	if p.ErrorHandler != nil {
   384  		return p.ErrorHandler
   385  	}
   386  	return p.defaultErrorHandler
   387  }
   388  
   389  // modifyResponse conditionally runs the optional ModifyResponse hook
   390  // and reports whether the request should proceed.
   391  func (p *ReverseProxy) modifyResponse(rw http.ResponseWriter, res *http.Response, req *http.Request) bool {
   392  	if p.ModifyResponse == nil {
   393  		return true
   394  	}
   395  	if err := p.ModifyResponse(res); err != nil {
   396  		res.Body.Close()
   397  		p.getErrorHandler()(rw, req, err)
   398  		return false
   399  	}
   400  	return true
   401  }
   402  
   403  func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
   404  	transport := p.Transport
   405  	if transport == nil {
   406  		transport = http.DefaultTransport
   407  	}
   408  
   409  	ctx := req.Context()
   410  	if ctx.Done() != nil {
   411  		// CloseNotifier predates context.Context, and has been
   412  		// entirely superseded by it. If the request contains
   413  		// a Context that carries a cancellation signal, don't
   414  		// bother spinning up a goroutine to watch the CloseNotify
   415  		// channel (if any).
   416  		//
   417  		// If the request Context has a nil Done channel (which
   418  		// means it is either context.Background, or a custom
   419  		// Context implementation with no cancellation signal),
   420  		// then consult the CloseNotifier if available.
   421  	} else if cn, ok := rw.(http.CloseNotifier); ok {
   422  		var cancel context.CancelFunc
   423  		ctx, cancel = context.WithCancel(ctx)
   424  		defer cancel()
   425  		notifyChan := cn.CloseNotify()
   426  		go func() {
   427  			select {
   428  			case <-notifyChan:
   429  				cancel()
   430  			case <-ctx.Done():
   431  			}
   432  		}()
   433  	}
   434  
   435  	outreq := req.Clone(ctx)
   436  	if req.ContentLength == 0 {
   437  		outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
   438  	}
   439  	if outreq.Body != nil {
   440  		// Wrap the body in a reader where Close does nothing. This is done
   441  		// because p.Transport.RoundTrip would close the reverse proxy's
   442  		// outbound request body if it fails to connect to upstream. If we do
   443  		// not wrap the body, when we close the reverse proxy's outbound
   444  		// request, it will also close the reverse proxy's inbound request body
   445  		// (i.e. the client's outbound request body). This is because
   446  		// http.(*Request).Clone creates a shallow copy of the body. This can
   447  		// cause an infinite hang in cases where the body is not yet received
   448  		// from the client (e.g. 100-continue requests): Close, which
   449  		// internally tries to consume the body content, would be called too
   450  		// early and would hang.
   451  		outreq.Body = &noopCloseReader{readCloser: outreq.Body}
   452  		// Reading from the request body after returning from a handler is not
   453  		// allowed, and the RoundTrip goroutine that reads the Body can outlive
   454  		// this handler. This can lead to a crash if the handler panics (see
   455  		// Issue 46866). Although calling Close doesn't guarantee there isn't
   456  		// any Read in flight after the handle returns, in practice it's safe to
   457  		// read after closing it.
   458  		defer outreq.Body.Close()
   459  	}
   460  	if outreq.Header == nil {
   461  		outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
   462  	}
   463  
   464  	if (p.Director != nil) == (p.Rewrite != nil) {
   465  		p.getErrorHandler()(rw, req, errors.New("ReverseProxy must have exactly one of Director or Rewrite set"))
   466  		return
   467  	}
   468  
   469  	if p.Director != nil {
   470  		p.Director(outreq)
   471  		if outreq.Form != nil {
   472  			outreq.URL.RawQuery = cleanQueryParams(outreq.URL.RawQuery)
   473  		}
   474  	}
   475  	outreq.Close = false
   476  
   477  	reqUpType := upgradeType(outreq.Header)
   478  	if !ascii.IsPrint(reqUpType) {
   479  		p.getErrorHandler()(rw, req, fmt.Errorf("client tried to switch to invalid protocol %q", reqUpType))
   480  		return
   481  	}
   482  	removeHopByHopHeaders(outreq.Header)
   483  
   484  	// Issue 21096: tell backend applications that care about trailer support
   485  	// that we support trailers. (We do, but we don't go out of our way to
   486  	// advertise that unless the incoming client request thought it was worth
   487  	// mentioning.) Note that we look at req.Header, not outreq.Header, since
   488  	// the latter has passed through removeHopByHopHeaders.
   489  	if httpguts.HeaderValuesContainsToken(req.Header["Te"], "trailers") {
   490  		outreq.Header.Set("Te", "trailers")
   491  	}
   492  
   493  	// After stripping all the hop-by-hop connection headers above, add back any
   494  	// necessary for protocol upgrades, such as for websockets.
   495  	if reqUpType != "" {
   496  		outreq.Header.Set("Connection", "Upgrade")
   497  		outreq.Header.Set("Upgrade", reqUpType)
   498  	}
   499  
   500  	if p.Rewrite != nil {
   501  		// Strip client-provided forwarding headers.
   502  		// The Rewrite func may use SetXForwarded to set new values
   503  		// for these or copy the previous values from the inbound request.
   504  		outreq.Header.Del("Forwarded")
   505  		outreq.Header.Del("X-Forwarded-For")
   506  		outreq.Header.Del("X-Forwarded-Host")
   507  		outreq.Header.Del("X-Forwarded-Proto")
   508  
   509  		// Remove unparsable query parameters from the outbound request.
   510  		outreq.URL.RawQuery = cleanQueryParams(outreq.URL.RawQuery)
   511  
   512  		pr := &ProxyRequest{
   513  			In:  req,
   514  			Out: outreq,
   515  		}
   516  		p.Rewrite(pr)
   517  		outreq = pr.Out
   518  	} else {
   519  		if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   520  			// If we aren't the first proxy retain prior
   521  			// X-Forwarded-For information as a comma+space
   522  			// separated list and fold multiple headers into one.
   523  			prior, ok := outreq.Header["X-Forwarded-For"]
   524  			omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
   525  			if len(prior) > 0 {
   526  				clientIP = strings.Join(prior, ", ") + ", " + clientIP
   527  			}
   528  			if !omit {
   529  				outreq.Header.Set("X-Forwarded-For", clientIP)
   530  			}
   531  		}
   532  	}
   533  
   534  	if _, ok := outreq.Header["User-Agent"]; !ok {
   535  		// If the outbound request doesn't have a User-Agent header set,
   536  		// don't send the default Go HTTP client User-Agent.
   537  		outreq.Header.Set("User-Agent", "")
   538  	}
   539  
   540  	var (
   541  		roundTripMutex sync.Mutex
   542  		roundTripDone  bool
   543  	)
   544  	trace := &httptrace.ClientTrace{
   545  		Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
   546  			roundTripMutex.Lock()
   547  			defer roundTripMutex.Unlock()
   548  			if roundTripDone {
   549  				// If RoundTrip has returned, don't try to further modify
   550  				// the ResponseWriter's header map.
   551  				return nil
   552  			}
   553  			h := rw.Header()
   554  			copyHeader(h, http.Header(header))
   555  			rw.WriteHeader(code)
   556  
   557  			// Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses
   558  			clear(h)
   559  			return nil
   560  		},
   561  	}
   562  	outreq = outreq.WithContext(httptrace.WithClientTrace(outreq.Context(), trace))
   563  
   564  	res, err := transport.RoundTrip(outreq)
   565  	roundTripMutex.Lock()
   566  	roundTripDone = true
   567  	roundTripMutex.Unlock()
   568  	if err != nil {
   569  		p.getErrorHandler()(rw, outreq, err)
   570  		return
   571  	}
   572  
   573  	// Deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
   574  	if res.StatusCode == http.StatusSwitchingProtocols {
   575  		if !p.modifyResponse(rw, res, outreq) {
   576  			return
   577  		}
   578  		p.handleUpgradeResponse(rw, outreq, res)
   579  		return
   580  	}
   581  
   582  	removeHopByHopHeaders(res.Header)
   583  
   584  	if !p.modifyResponse(rw, res, outreq) {
   585  		return
   586  	}
   587  
   588  	copyHeader(rw.Header(), res.Header)
   589  
   590  	// The "Trailer" header isn't included in the Transport's response,
   591  	// at least for *http.Transport. Build it up from Trailer.
   592  	announcedTrailers := len(res.Trailer)
   593  	if announcedTrailers > 0 {
   594  		trailerKeys := make([]string, 0, len(res.Trailer))
   595  		for k := range res.Trailer {
   596  			trailerKeys = append(trailerKeys, k)
   597  		}
   598  		rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
   599  	}
   600  
   601  	rw.WriteHeader(res.StatusCode)
   602  
   603  	err = p.copyResponse(rw, res.Body, p.flushInterval(res))
   604  	if err != nil {
   605  		defer res.Body.Close()
   606  		// Since we're streaming the response, if we run into an error all we can do
   607  		// is abort the request. Issue 23643: ReverseProxy should use ErrAbortHandler
   608  		// on read error while copying body.
   609  		if !shouldPanicOnCopyError(req) {
   610  			p.logf("suppressing panic for copyResponse error in test; copy error: %v", err)
   611  			return
   612  		}
   613  		panic(http.ErrAbortHandler)
   614  	}
   615  	res.Body.Close() // close now, instead of defer, to populate res.Trailer
   616  
   617  	if len(res.Trailer) > 0 {
   618  		// Force chunking if we saw a response trailer.
   619  		// This prevents net/http from calculating the length for short
   620  		// bodies and adding a Content-Length.
   621  		http.NewResponseController(rw).Flush()
   622  	}
   623  
   624  	if len(res.Trailer) == announcedTrailers {
   625  		copyHeader(rw.Header(), res.Trailer)
   626  		return
   627  	}
   628  
   629  	for k, vv := range res.Trailer {
   630  		k = http.TrailerPrefix + k
   631  		for _, v := range vv {
   632  			rw.Header().Add(k, v)
   633  		}
   634  	}
   635  }
   636  
   637  var inOurTests bool // whether we're in our own tests
   638  
   639  // shouldPanicOnCopyError reports whether the reverse proxy should
   640  // panic with http.ErrAbortHandler. This is the right thing to do by
   641  // default, but Go 1.10 and earlier did not, so existing unit tests
   642  // weren't expecting panics. Only panic in our own tests, or when
   643  // running under the HTTP server.
   644  func shouldPanicOnCopyError(req *http.Request) bool {
   645  	if inOurTests {
   646  		// Our tests know to handle this panic.
   647  		return true
   648  	}
   649  	if req.Context().Value(http.ServerContextKey) != nil {
   650  		// We seem to be running under an HTTP server, so
   651  		// it'll recover the panic.
   652  		return true
   653  	}
   654  	// Otherwise act like Go 1.10 and earlier to not break
   655  	// existing tests.
   656  	return false
   657  }
   658  
   659  // removeHopByHopHeaders removes hop-by-hop headers.
   660  func removeHopByHopHeaders(h http.Header) {
   661  	// RFC 7230, section 6.1: Remove headers listed in the "Connection" header.
   662  	for _, f := range h["Connection"] {
   663  		for sf := range strings.SplitSeq(f, ",") {
   664  			if sf = textproto.TrimString(sf); sf != "" {
   665  				h.Del(sf)
   666  			}
   667  		}
   668  	}
   669  	// RFC 2616, section 13.5.1: Remove a set of known hop-by-hop headers.
   670  	// This behavior is superseded by the RFC 7230 Connection header, but
   671  	// preserve it for backwards compatibility.
   672  	for _, f := range hopHeaders {
   673  		h.Del(f)
   674  	}
   675  }
   676  
   677  // flushInterval returns the p.FlushInterval value, conditionally
   678  // overriding its value for a specific request/response.
   679  func (p *ReverseProxy) flushInterval(res *http.Response) time.Duration {
   680  	resCT := res.Header.Get("Content-Type")
   681  
   682  	// For Server-Sent Events responses, flush immediately.
   683  	// The MIME type is defined in https://www.w3.org/TR/eventsource/#text-event-stream
   684  	if baseCT, _, _ := mime.ParseMediaType(resCT); baseCT == "text/event-stream" {
   685  		return -1 // negative means immediately
   686  	}
   687  
   688  	// We might have the case of streaming for which Content-Length might be unset.
   689  	if res.ContentLength == -1 {
   690  		return -1
   691  	}
   692  
   693  	return p.FlushInterval
   694  }
   695  
   696  func (p *ReverseProxy) copyResponse(dst http.ResponseWriter, src io.Reader, flushInterval time.Duration) error {
   697  	var w io.Writer = dst
   698  
   699  	if flushInterval != 0 {
   700  		mlw := &maxLatencyWriter{
   701  			dst:     dst,
   702  			flush:   http.NewResponseController(dst).Flush,
   703  			latency: flushInterval,
   704  		}
   705  		defer mlw.stop()
   706  
   707  		// set up initial timer so headers get flushed even if body writes are delayed
   708  		mlw.flushPending = true
   709  		mlw.t = time.AfterFunc(flushInterval, mlw.delayedFlush)
   710  
   711  		w = mlw
   712  	}
   713  
   714  	var buf []byte
   715  	if p.BufferPool != nil {
   716  		buf = p.BufferPool.Get()
   717  		defer p.BufferPool.Put(buf)
   718  	}
   719  	_, err := p.copyBuffer(w, src, buf)
   720  	return err
   721  }
   722  
   723  // copyBuffer returns any write errors or non-EOF read errors, and the amount
   724  // of bytes written.
   725  func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
   726  	if len(buf) == 0 {
   727  		buf = make([]byte, 32*1024)
   728  	}
   729  	var written int64
   730  	for {
   731  		nr, rerr := src.Read(buf)
   732  		if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
   733  			p.logf("httputil: ReverseProxy read error during body copy: %v", rerr)
   734  		}
   735  		if nr > 0 {
   736  			nw, werr := dst.Write(buf[:nr])
   737  			if nw > 0 {
   738  				written += int64(nw)
   739  			}
   740  			if werr != nil {
   741  				return written, werr
   742  			}
   743  			if nr != nw {
   744  				return written, io.ErrShortWrite
   745  			}
   746  		}
   747  		if rerr != nil {
   748  			if rerr == io.EOF {
   749  				rerr = nil
   750  			}
   751  			return written, rerr
   752  		}
   753  	}
   754  }
   755  
   756  func (p *ReverseProxy) logf(format string, args ...any) {
   757  	if p.ErrorLog != nil {
   758  		p.ErrorLog.Printf(format, args...)
   759  	} else {
   760  		log.Printf(format, args...)
   761  	}
   762  }
   763  
   764  type maxLatencyWriter struct {
   765  	dst     io.Writer
   766  	flush   func() error
   767  	latency time.Duration // non-zero; negative means to flush immediately
   768  
   769  	mu           sync.Mutex // protects t, flushPending, and dst.Flush
   770  	t            *time.Timer
   771  	flushPending bool
   772  }
   773  
   774  func (m *maxLatencyWriter) Write(p []byte) (n int, err error) {
   775  	m.mu.Lock()
   776  	defer m.mu.Unlock()
   777  	n, err = m.dst.Write(p)
   778  	if m.latency < 0 {
   779  		m.flush()
   780  		return
   781  	}
   782  	if m.flushPending {
   783  		return
   784  	}
   785  	if m.t == nil {
   786  		m.t = time.AfterFunc(m.latency, m.delayedFlush)
   787  	} else {
   788  		m.t.Reset(m.latency)
   789  	}
   790  	m.flushPending = true
   791  	return
   792  }
   793  
   794  func (m *maxLatencyWriter) delayedFlush() {
   795  	m.mu.Lock()
   796  	defer m.mu.Unlock()
   797  	if !m.flushPending { // if stop was called but AfterFunc already started this goroutine
   798  		return
   799  	}
   800  	m.flush()
   801  	m.flushPending = false
   802  }
   803  
   804  func (m *maxLatencyWriter) stop() {
   805  	m.mu.Lock()
   806  	defer m.mu.Unlock()
   807  	m.flushPending = false
   808  	if m.t != nil {
   809  		m.t.Stop()
   810  	}
   811  }
   812  
   813  func upgradeType(h http.Header) string {
   814  	if !httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") {
   815  		return ""
   816  	}
   817  	return h.Get("Upgrade")
   818  }
   819  
   820  func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
   821  	reqUpType := upgradeType(req.Header)
   822  	resUpType := upgradeType(res.Header)
   823  	if !ascii.IsPrint(resUpType) { // We know reqUpType is ASCII, it's checked by the caller.
   824  		p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch to invalid protocol %q", resUpType))
   825  		return
   826  	}
   827  	if !ascii.EqualFold(reqUpType, resUpType) {
   828  		p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch protocol %q when %q was requested", resUpType, reqUpType))
   829  		return
   830  	}
   831  
   832  	backConn, ok := res.Body.(io.ReadWriteCloser)
   833  	if !ok {
   834  		p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
   835  		return
   836  	}
   837  
   838  	rc := http.NewResponseController(rw)
   839  	conn, brw, hijackErr := rc.Hijack()
   840  	if errors.Is(hijackErr, http.ErrNotSupported) {
   841  		p.getErrorHandler()(rw, req, fmt.Errorf("can't switch protocols using non-Hijacker ResponseWriter type %T", rw))
   842  		return
   843  	}
   844  
   845  	backConnCloseCh := make(chan bool)
   846  	go func() {
   847  		// Ensure that the cancellation of a request closes the backend.
   848  		// See issue https://golang.org/issue/35559.
   849  		select {
   850  		case <-req.Context().Done():
   851  		case <-backConnCloseCh:
   852  		}
   853  		backConn.Close()
   854  	}()
   855  	defer close(backConnCloseCh)
   856  
   857  	if hijackErr != nil {
   858  		p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", hijackErr))
   859  		return
   860  	}
   861  	defer conn.Close()
   862  
   863  	copyHeader(rw.Header(), res.Header)
   864  
   865  	res.Header = rw.Header()
   866  	res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
   867  	if err := res.Write(brw); err != nil {
   868  		p.getErrorHandler()(rw, req, fmt.Errorf("response write: %v", err))
   869  		return
   870  	}
   871  	if err := brw.Flush(); err != nil {
   872  		p.getErrorHandler()(rw, req, fmt.Errorf("response flush: %v", err))
   873  		return
   874  	}
   875  	errc := make(chan error, 1)
   876  	spc := switchProtocolCopier{user: conn, backend: backConn}
   877  	go spc.copyToBackend(errc)
   878  	go spc.copyFromBackend(errc)
   879  
   880  	// Wait until both copy functions have sent on the error channel,
   881  	// or until one fails.
   882  	err := <-errc
   883  	if err == nil {
   884  		err = <-errc
   885  	}
   886  }
   887  
   888  var errCopyDone = errors.New("hijacked connection copy complete")
   889  
   890  // switchProtocolCopier exists so goroutines proxying data back and
   891  // forth have nice names in stacks.
   892  type switchProtocolCopier struct {
   893  	user, backend io.ReadWriter
   894  }
   895  
   896  func (c switchProtocolCopier) copyFromBackend(errc chan<- error) {
   897  	if _, err := io.Copy(c.user, c.backend); err != nil {
   898  		errc <- err
   899  		return
   900  	}
   901  
   902  	// backend conn has reached EOF so propogate close write to user conn
   903  	if wc, ok := c.user.(interface{ CloseWrite() error }); ok {
   904  		errc <- wc.CloseWrite()
   905  		return
   906  	}
   907  
   908  	errc <- errCopyDone
   909  }
   910  
   911  func (c switchProtocolCopier) copyToBackend(errc chan<- error) {
   912  	if _, err := io.Copy(c.backend, c.user); err != nil {
   913  		errc <- err
   914  		return
   915  	}
   916  
   917  	// user conn has reached EOF so propogate close write to backend conn
   918  	if wc, ok := c.backend.(interface{ CloseWrite() error }); ok {
   919  		errc <- wc.CloseWrite()
   920  		return
   921  	}
   922  
   923  	errc <- errCopyDone
   924  }
   925  
   926  var urlmaxqueryparams = godebug.New("urlmaxqueryparams")
   927  
   928  // Keep this in sync with net/url.
   929  const defaultMaxParams = 10000
   930  
   931  func cleanQueryParams(s string) string {
   932  	reencode := func(s string) string {
   933  		v, _ := url.ParseQuery(s)
   934  		return v.Encode()
   935  	}
   936  	if urlmaxqueryparams.Value() != "" {
   937  		// Always reencode when a non-default urlmaxqueryparams is set.
   938  		return reencode(s)
   939  	}
   940  	if numParams := strings.Count(s, "&") + 1; numParams > defaultMaxParams {
   941  		// Too many query parameters.
   942  		return reencode(s)
   943  	}
   944  	for i := 0; i < len(s); {
   945  		switch s[i] {
   946  		case ';':
   947  			return reencode(s)
   948  		case '%':
   949  			if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
   950  				return reencode(s)
   951  			}
   952  			i += 3
   953  		default:
   954  			i++
   955  		}
   956  	}
   957  	return s
   958  }
   959  
   960  func ishex(c byte) bool {
   961  	switch {
   962  	case '0' <= c && c <= '9':
   963  		return true
   964  	case 'a' <= c && c <= 'f':
   965  		return true
   966  	case 'A' <= c && c <= 'F':
   967  		return true
   968  	}
   969  	return false
   970  }
   971  
   972  type noopCloseReader struct {
   973  	readCloser io.ReadCloser
   974  	closed     atomic.Bool
   975  }
   976  
   977  func (ncr *noopCloseReader) Close() error {
   978  	ncr.closed.Store(true)
   979  	return nil
   980  }
   981  
   982  func (ncr *noopCloseReader) Read(p []byte) (int, error) {
   983  	if ncr.closed.Load() {
   984  		return 0, errors.New("ReverseProxy does an invalid Read on closed Body")
   985  	}
   986  	return ncr.readCloser.Read(p)
   987  }
   988  

View as plain text