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  	"io"
    14  	"log"
    15  	"mime"
    16  	"net"
    17  	"net/http"
    18  	"net/http/httptrace"
    19  	"net/http/internal/ascii"
    20  	"net/textproto"
    21  	"net/url"
    22  	"strings"
    23  	"sync"
    24  	"sync/atomic"
    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  		// Wrap the body in a reader where Close does nothing. This is done
   440  		// because p.Transport.RoundTrip would close the reverse proxy's
   441  		// outbound request body if it fails to connect to upstream. If we do
   442  		// not wrap the body, when we close the reverse proxy's outbound
   443  		// request, it will also close the reverse proxy's inbound request body
   444  		// (i.e. the client's outbound request body). This is because
   445  		// http.(*Request).Clone creates a shallow copy of the body. This can
   446  		// cause an infinite hang in cases where the body is not yet received
   447  		// from the client (e.g. 100-continue requests): Close, which
   448  		// internally tries to consume the body content, would be called too
   449  		// early and would hang.
   450  		outreq.Body = &noopCloseReader{readCloser: outreq.Body}
   451  		// Reading from the request body after returning from a handler is not
   452  		// allowed, and the RoundTrip goroutine that reads the Body can outlive
   453  		// this handler. This can lead to a crash if the handler panics (see
   454  		// Issue 46866). Although calling Close doesn't guarantee there isn't
   455  		// any Read in flight after the handle returns, in practice it's safe to
   456  		// read after closing it.
   457  		defer outreq.Body.Close()
   458  	}
   459  	if outreq.Header == nil {
   460  		outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
   461  	}
   462  
   463  	if (p.Director != nil) == (p.Rewrite != nil) {
   464  		p.getErrorHandler()(rw, req, errors.New("ReverseProxy must have exactly one of Director or Rewrite set"))
   465  		return
   466  	}
   467  
   468  	if p.Director != nil {
   469  		p.Director(outreq)
   470  		if outreq.Form != nil {
   471  			outreq.URL.RawQuery = cleanQueryParams(outreq.URL.RawQuery)
   472  		}
   473  	}
   474  	outreq.Close = false
   475  
   476  	reqUpType := upgradeType(outreq.Header)
   477  	if !ascii.IsPrint(reqUpType) {
   478  		p.getErrorHandler()(rw, req, fmt.Errorf("client tried to switch to invalid protocol %q", reqUpType))
   479  		return
   480  	}
   481  	removeHopByHopHeaders(outreq.Header)
   482  
   483  	// Issue 21096: tell backend applications that care about trailer support
   484  	// that we support trailers. (We do, but we don't go out of our way to
   485  	// advertise that unless the incoming client request thought it was worth
   486  	// mentioning.) Note that we look at req.Header, not outreq.Header, since
   487  	// the latter has passed through removeHopByHopHeaders.
   488  	if httpguts.HeaderValuesContainsToken(req.Header["Te"], "trailers") {
   489  		outreq.Header.Set("Te", "trailers")
   490  	}
   491  
   492  	// After stripping all the hop-by-hop connection headers above, add back any
   493  	// necessary for protocol upgrades, such as for websockets.
   494  	if reqUpType != "" {
   495  		outreq.Header.Set("Connection", "Upgrade")
   496  		outreq.Header.Set("Upgrade", reqUpType)
   497  	}
   498  
   499  	if p.Rewrite != nil {
   500  		// Strip client-provided forwarding headers.
   501  		// The Rewrite func may use SetXForwarded to set new values
   502  		// for these or copy the previous values from the inbound request.
   503  		outreq.Header.Del("Forwarded")
   504  		outreq.Header.Del("X-Forwarded-For")
   505  		outreq.Header.Del("X-Forwarded-Host")
   506  		outreq.Header.Del("X-Forwarded-Proto")
   507  
   508  		// Remove unparsable query parameters from the outbound request.
   509  		outreq.URL.RawQuery = cleanQueryParams(outreq.URL.RawQuery)
   510  
   511  		pr := &ProxyRequest{
   512  			In:  req,
   513  			Out: outreq,
   514  		}
   515  		p.Rewrite(pr)
   516  		outreq = pr.Out
   517  	} else {
   518  		if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   519  			// If we aren't the first proxy retain prior
   520  			// X-Forwarded-For information as a comma+space
   521  			// separated list and fold multiple headers into one.
   522  			prior, ok := outreq.Header["X-Forwarded-For"]
   523  			omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
   524  			if len(prior) > 0 {
   525  				clientIP = strings.Join(prior, ", ") + ", " + clientIP
   526  			}
   527  			if !omit {
   528  				outreq.Header.Set("X-Forwarded-For", clientIP)
   529  			}
   530  		}
   531  	}
   532  
   533  	if _, ok := outreq.Header["User-Agent"]; !ok {
   534  		// If the outbound request doesn't have a User-Agent header set,
   535  		// don't send the default Go HTTP client User-Agent.
   536  		outreq.Header.Set("User-Agent", "")
   537  	}
   538  
   539  	var (
   540  		roundTripMutex sync.Mutex
   541  		roundTripDone  bool
   542  	)
   543  	trace := &httptrace.ClientTrace{
   544  		Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
   545  			roundTripMutex.Lock()
   546  			defer roundTripMutex.Unlock()
   547  			if roundTripDone {
   548  				// If RoundTrip has returned, don't try to further modify
   549  				// the ResponseWriter's header map.
   550  				return nil
   551  			}
   552  			h := rw.Header()
   553  			copyHeader(h, http.Header(header))
   554  			rw.WriteHeader(code)
   555  
   556  			// Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses
   557  			clear(h)
   558  			return nil
   559  		},
   560  	}
   561  	outreq = outreq.WithContext(httptrace.WithClientTrace(outreq.Context(), trace))
   562  
   563  	res, err := transport.RoundTrip(outreq)
   564  	roundTripMutex.Lock()
   565  	roundTripDone = true
   566  	roundTripMutex.Unlock()
   567  	if err != nil {
   568  		p.getErrorHandler()(rw, outreq, err)
   569  		return
   570  	}
   571  
   572  	// Deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
   573  	if res.StatusCode == http.StatusSwitchingProtocols {
   574  		if !p.modifyResponse(rw, res, outreq) {
   575  			return
   576  		}
   577  		p.handleUpgradeResponse(rw, outreq, res)
   578  		return
   579  	}
   580  
   581  	removeHopByHopHeaders(res.Header)
   582  
   583  	if !p.modifyResponse(rw, res, outreq) {
   584  		return
   585  	}
   586  
   587  	copyHeader(rw.Header(), res.Header)
   588  
   589  	// The "Trailer" header isn't included in the Transport's response,
   590  	// at least for *http.Transport. Build it up from Trailer.
   591  	announcedTrailers := len(res.Trailer)
   592  	if announcedTrailers > 0 {
   593  		trailerKeys := make([]string, 0, len(res.Trailer))
   594  		for k := range res.Trailer {
   595  			trailerKeys = append(trailerKeys, k)
   596  		}
   597  		rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
   598  	}
   599  
   600  	rw.WriteHeader(res.StatusCode)
   601  
   602  	err = p.copyResponse(rw, res.Body, p.flushInterval(res))
   603  	if err != nil {
   604  		defer res.Body.Close()
   605  		// Since we're streaming the response, if we run into an error all we can do
   606  		// is abort the request. Issue 23643: ReverseProxy should use ErrAbortHandler
   607  		// on read error while copying body.
   608  		if !shouldPanicOnCopyError(req) {
   609  			p.logf("suppressing panic for copyResponse error in test; copy error: %v", err)
   610  			return
   611  		}
   612  		panic(http.ErrAbortHandler)
   613  	}
   614  	res.Body.Close() // close now, instead of defer, to populate res.Trailer
   615  
   616  	if len(res.Trailer) > 0 {
   617  		// Force chunking if we saw a response trailer.
   618  		// This prevents net/http from calculating the length for short
   619  		// bodies and adding a Content-Length.
   620  		http.NewResponseController(rw).Flush()
   621  	}
   622  
   623  	if len(res.Trailer) == announcedTrailers {
   624  		copyHeader(rw.Header(), res.Trailer)
   625  		return
   626  	}
   627  
   628  	for k, vv := range res.Trailer {
   629  		k = http.TrailerPrefix + k
   630  		for _, v := range vv {
   631  			rw.Header().Add(k, v)
   632  		}
   633  	}
   634  }
   635  
   636  var inOurTests bool // whether we're in our own tests
   637  
   638  // shouldPanicOnCopyError reports whether the reverse proxy should
   639  // panic with http.ErrAbortHandler. This is the right thing to do by
   640  // default, but Go 1.10 and earlier did not, so existing unit tests
   641  // weren't expecting panics. Only panic in our own tests, or when
   642  // running under the HTTP server.
   643  func shouldPanicOnCopyError(req *http.Request) bool {
   644  	if inOurTests {
   645  		// Our tests know to handle this panic.
   646  		return true
   647  	}
   648  	if req.Context().Value(http.ServerContextKey) != nil {
   649  		// We seem to be running under an HTTP server, so
   650  		// it'll recover the panic.
   651  		return true
   652  	}
   653  	// Otherwise act like Go 1.10 and earlier to not break
   654  	// existing tests.
   655  	return false
   656  }
   657  
   658  // removeHopByHopHeaders removes hop-by-hop headers.
   659  func removeHopByHopHeaders(h http.Header) {
   660  	// RFC 7230, section 6.1: Remove headers listed in the "Connection" header.
   661  	for _, f := range h["Connection"] {
   662  		for sf := range strings.SplitSeq(f, ",") {
   663  			if sf = textproto.TrimString(sf); sf != "" {
   664  				h.Del(sf)
   665  			}
   666  		}
   667  	}
   668  	// RFC 2616, section 13.5.1: Remove a set of known hop-by-hop headers.
   669  	// This behavior is superseded by the RFC 7230 Connection header, but
   670  	// preserve it for backwards compatibility.
   671  	for _, f := range hopHeaders {
   672  		h.Del(f)
   673  	}
   674  }
   675  
   676  // flushInterval returns the p.FlushInterval value, conditionally
   677  // overriding its value for a specific request/response.
   678  func (p *ReverseProxy) flushInterval(res *http.Response) time.Duration {
   679  	resCT := res.Header.Get("Content-Type")
   680  
   681  	// For Server-Sent Events responses, flush immediately.
   682  	// The MIME type is defined in https://www.w3.org/TR/eventsource/#text-event-stream
   683  	if baseCT, _, _ := mime.ParseMediaType(resCT); baseCT == "text/event-stream" {
   684  		return -1 // negative means immediately
   685  	}
   686  
   687  	// We might have the case of streaming for which Content-Length might be unset.
   688  	if res.ContentLength == -1 {
   689  		return -1
   690  	}
   691  
   692  	return p.FlushInterval
   693  }
   694  
   695  func (p *ReverseProxy) copyResponse(dst http.ResponseWriter, src io.Reader, flushInterval time.Duration) error {
   696  	var w io.Writer = dst
   697  
   698  	if flushInterval != 0 {
   699  		mlw := &maxLatencyWriter{
   700  			dst:     dst,
   701  			flush:   http.NewResponseController(dst).Flush,
   702  			latency: flushInterval,
   703  		}
   704  		defer mlw.stop()
   705  
   706  		// set up initial timer so headers get flushed even if body writes are delayed
   707  		mlw.flushPending = true
   708  		mlw.t = time.AfterFunc(flushInterval, mlw.delayedFlush)
   709  
   710  		w = mlw
   711  	}
   712  
   713  	var buf []byte
   714  	if p.BufferPool != nil {
   715  		buf = p.BufferPool.Get()
   716  		defer p.BufferPool.Put(buf)
   717  	}
   718  	_, err := p.copyBuffer(w, src, buf)
   719  	return err
   720  }
   721  
   722  // copyBuffer returns any write errors or non-EOF read errors, and the amount
   723  // of bytes written.
   724  func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
   725  	if len(buf) == 0 {
   726  		buf = make([]byte, 32*1024)
   727  	}
   728  	var written int64
   729  	for {
   730  		nr, rerr := src.Read(buf)
   731  		if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
   732  			p.logf("httputil: ReverseProxy read error during body copy: %v", rerr)
   733  		}
   734  		if nr > 0 {
   735  			nw, werr := dst.Write(buf[:nr])
   736  			if nw > 0 {
   737  				written += int64(nw)
   738  			}
   739  			if werr != nil {
   740  				return written, werr
   741  			}
   742  			if nr != nw {
   743  				return written, io.ErrShortWrite
   744  			}
   745  		}
   746  		if rerr != nil {
   747  			if rerr == io.EOF {
   748  				rerr = nil
   749  			}
   750  			return written, rerr
   751  		}
   752  	}
   753  }
   754  
   755  func (p *ReverseProxy) logf(format string, args ...any) {
   756  	if p.ErrorLog != nil {
   757  		p.ErrorLog.Printf(format, args...)
   758  	} else {
   759  		log.Printf(format, args...)
   760  	}
   761  }
   762  
   763  type maxLatencyWriter struct {
   764  	dst     io.Writer
   765  	flush   func() error
   766  	latency time.Duration // non-zero; negative means to flush immediately
   767  
   768  	mu           sync.Mutex // protects t, flushPending, and dst.Flush
   769  	t            *time.Timer
   770  	flushPending bool
   771  }
   772  
   773  func (m *maxLatencyWriter) Write(p []byte) (n int, err error) {
   774  	m.mu.Lock()
   775  	defer m.mu.Unlock()
   776  	n, err = m.dst.Write(p)
   777  	if m.latency < 0 {
   778  		m.flush()
   779  		return
   780  	}
   781  	if m.flushPending {
   782  		return
   783  	}
   784  	if m.t == nil {
   785  		m.t = time.AfterFunc(m.latency, m.delayedFlush)
   786  	} else {
   787  		m.t.Reset(m.latency)
   788  	}
   789  	m.flushPending = true
   790  	return
   791  }
   792  
   793  func (m *maxLatencyWriter) delayedFlush() {
   794  	m.mu.Lock()
   795  	defer m.mu.Unlock()
   796  	if !m.flushPending { // if stop was called but AfterFunc already started this goroutine
   797  		return
   798  	}
   799  	m.flush()
   800  	m.flushPending = false
   801  }
   802  
   803  func (m *maxLatencyWriter) stop() {
   804  	m.mu.Lock()
   805  	defer m.mu.Unlock()
   806  	m.flushPending = false
   807  	if m.t != nil {
   808  		m.t.Stop()
   809  	}
   810  }
   811  
   812  func upgradeType(h http.Header) string {
   813  	if !httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") {
   814  		return ""
   815  	}
   816  	return h.Get("Upgrade")
   817  }
   818  
   819  func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
   820  	reqUpType := upgradeType(req.Header)
   821  	resUpType := upgradeType(res.Header)
   822  	if !ascii.IsPrint(resUpType) { // We know reqUpType is ASCII, it's checked by the caller.
   823  		p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch to invalid protocol %q", resUpType))
   824  		return
   825  	}
   826  	if !ascii.EqualFold(reqUpType, resUpType) {
   827  		p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch protocol %q when %q was requested", resUpType, reqUpType))
   828  		return
   829  	}
   830  
   831  	backConn, ok := res.Body.(io.ReadWriteCloser)
   832  	if !ok {
   833  		p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
   834  		return
   835  	}
   836  
   837  	rc := http.NewResponseController(rw)
   838  	conn, brw, hijackErr := rc.Hijack()
   839  	if errors.Is(hijackErr, http.ErrNotSupported) {
   840  		p.getErrorHandler()(rw, req, fmt.Errorf("can't switch protocols using non-Hijacker ResponseWriter type %T", rw))
   841  		return
   842  	}
   843  
   844  	backConnCloseCh := make(chan bool)
   845  	go func() {
   846  		// Ensure that the cancellation of a request closes the backend.
   847  		// See issue https://golang.org/issue/35559.
   848  		select {
   849  		case <-req.Context().Done():
   850  		case <-backConnCloseCh:
   851  		}
   852  		backConn.Close()
   853  	}()
   854  	defer close(backConnCloseCh)
   855  
   856  	if hijackErr != nil {
   857  		p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", hijackErr))
   858  		return
   859  	}
   860  	defer conn.Close()
   861  
   862  	copyHeader(rw.Header(), res.Header)
   863  
   864  	res.Header = rw.Header()
   865  	res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
   866  	if err := res.Write(brw); err != nil {
   867  		p.getErrorHandler()(rw, req, fmt.Errorf("response write: %v", err))
   868  		return
   869  	}
   870  	if err := brw.Flush(); err != nil {
   871  		p.getErrorHandler()(rw, req, fmt.Errorf("response flush: %v", err))
   872  		return
   873  	}
   874  	errc := make(chan error, 1)
   875  	spc := switchProtocolCopier{user: conn, backend: backConn}
   876  	go spc.copyToBackend(errc)
   877  	go spc.copyFromBackend(errc)
   878  
   879  	// Wait until both copy functions have sent on the error channel,
   880  	// or until one fails.
   881  	err := <-errc
   882  	if err == nil {
   883  		err = <-errc
   884  	}
   885  }
   886  
   887  var errCopyDone = errors.New("hijacked connection copy complete")
   888  
   889  // switchProtocolCopier exists so goroutines proxying data back and
   890  // forth have nice names in stacks.
   891  type switchProtocolCopier struct {
   892  	user, backend io.ReadWriter
   893  }
   894  
   895  func (c switchProtocolCopier) copyFromBackend(errc chan<- error) {
   896  	if _, err := io.Copy(c.user, c.backend); err != nil {
   897  		errc <- err
   898  		return
   899  	}
   900  
   901  	// backend conn has reached EOF so propogate close write to user conn
   902  	if wc, ok := c.user.(interface{ CloseWrite() error }); ok {
   903  		errc <- wc.CloseWrite()
   904  		return
   905  	}
   906  
   907  	errc <- errCopyDone
   908  }
   909  
   910  func (c switchProtocolCopier) copyToBackend(errc chan<- error) {
   911  	if _, err := io.Copy(c.backend, c.user); err != nil {
   912  		errc <- err
   913  		return
   914  	}
   915  
   916  	// user conn has reached EOF so propogate close write to backend conn
   917  	if wc, ok := c.backend.(interface{ CloseWrite() error }); ok {
   918  		errc <- wc.CloseWrite()
   919  		return
   920  	}
   921  
   922  	errc <- errCopyDone
   923  }
   924  
   925  func cleanQueryParams(s string) string {
   926  	reencode := func(s string) string {
   927  		v, _ := url.ParseQuery(s)
   928  		return v.Encode()
   929  	}
   930  	for i := 0; i < len(s); {
   931  		switch s[i] {
   932  		case ';':
   933  			return reencode(s)
   934  		case '%':
   935  			if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
   936  				return reencode(s)
   937  			}
   938  			i += 3
   939  		default:
   940  			i++
   941  		}
   942  	}
   943  	return s
   944  }
   945  
   946  func ishex(c byte) bool {
   947  	switch {
   948  	case '0' <= c && c <= '9':
   949  		return true
   950  	case 'a' <= c && c <= 'f':
   951  		return true
   952  	case 'A' <= c && c <= 'F':
   953  		return true
   954  	}
   955  	return false
   956  }
   957  
   958  type noopCloseReader struct {
   959  	readCloser io.ReadCloser
   960  	closed     atomic.Bool
   961  }
   962  
   963  func (ncr *noopCloseReader) Close() error {
   964  	ncr.closed.Store(true)
   965  	return nil
   966  }
   967  
   968  func (ncr *noopCloseReader) Read(p []byte) (int, error) {
   969  	if ncr.closed.Load() {
   970  		return 0, errors.New("ReverseProxy does an invalid Read on closed Body")
   971  	}
   972  	return ncr.readCloser.Read(p)
   973  }
   974  

View as plain text