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

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

View as plain text