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

View as plain text