Source file src/net/http/server.go

     1  // Copyright 2009 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 server. See RFC 7230 through 7235.
     6  
     7  package http
     8  
     9  import (
    10  	"bufio"
    11  	"bytes"
    12  	"context"
    13  	"crypto/tls"
    14  	"errors"
    15  	"fmt"
    16  	"internal/godebug"
    17  	"io"
    18  	"log"
    19  	"maps"
    20  	"math/rand/v2"
    21  	"net"
    22  	"net/http/internal"
    23  	"net/textproto"
    24  	"net/url"
    25  	urlpkg "net/url"
    26  	"path"
    27  	"runtime"
    28  	"slices"
    29  	"strconv"
    30  	"strings"
    31  	"sync"
    32  	"sync/atomic"
    33  	"time"
    34  	_ "unsafe" // for linkname
    35  
    36  	"golang.org/x/net/http/httpguts"
    37  )
    38  
    39  // Errors used by the HTTP server.
    40  var (
    41  	// ErrBodyNotAllowed is returned by ResponseWriter.Write calls
    42  	// when the HTTP method or response code does not permit a
    43  	// body.
    44  	ErrBodyNotAllowed = internal.ErrBodyNotAllowed
    45  
    46  	// ErrHijacked is returned by ResponseWriter.Write calls when
    47  	// the underlying connection has been hijacked using the
    48  	// Hijacker interface. A zero-byte write on a hijacked
    49  	// connection will return ErrHijacked without any other side
    50  	// effects.
    51  	ErrHijacked = errors.New("http: connection has been hijacked")
    52  
    53  	// ErrContentLength is returned by ResponseWriter.Write calls
    54  	// when a Handler set a Content-Length response header with a
    55  	// declared size and then attempted to write more bytes than
    56  	// declared.
    57  	ErrContentLength = errors.New("http: wrote more than the declared Content-Length")
    58  
    59  	// Deprecated: ErrWriteAfterFlush is no longer returned by
    60  	// anything in the net/http package. Callers should not
    61  	// compare errors against this variable.
    62  	ErrWriteAfterFlush = errors.New("unused")
    63  )
    64  
    65  // A Handler responds to an HTTP request.
    66  //
    67  // [Handler.ServeHTTP] should write reply headers and data to the [ResponseWriter]
    68  // and then return. Returning signals that the request is finished; it
    69  // is not valid to use the [ResponseWriter] or read from the
    70  // [Request.Body] after or concurrently with the completion of the
    71  // ServeHTTP call.
    72  //
    73  // Depending on the HTTP client software, HTTP protocol version, and
    74  // any intermediaries between the client and the Go server, it may not
    75  // be possible to read from the [Request.Body] after writing to the
    76  // [ResponseWriter]. Cautious handlers should read the [Request.Body]
    77  // first, and then reply.
    78  //
    79  // Except for reading the body, handlers should not modify the
    80  // provided Request.
    81  //
    82  // If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
    83  // that the effect of the panic was isolated to the active request.
    84  // It recovers the panic, logs a stack trace to the server error log,
    85  // and either closes the network connection or sends an HTTP/2
    86  // RST_STREAM, depending on the HTTP protocol. To abort a handler so
    87  // the client sees an interrupted response but the server doesn't log
    88  // an error, panic with the value [ErrAbortHandler].
    89  type Handler interface {
    90  	ServeHTTP(ResponseWriter, *Request)
    91  }
    92  
    93  // A ResponseWriter interface is used by an HTTP handler to
    94  // construct an HTTP response.
    95  //
    96  // A ResponseWriter may not be used after [Handler.ServeHTTP] has returned.
    97  type ResponseWriter interface {
    98  	// Header returns the header map that will be sent by
    99  	// [ResponseWriter.WriteHeader]. The [Header] map also is the mechanism with which
   100  	// [Handler] implementations can set HTTP trailers.
   101  	//
   102  	// Changing the header map after a call to [ResponseWriter.WriteHeader] (or
   103  	// [ResponseWriter.Write]) has no effect unless the HTTP status code was of the
   104  	// 1xx class or the modified headers are trailers.
   105  	//
   106  	// There are two ways to set Trailers. The preferred way is to
   107  	// predeclare in the headers which trailers you will later
   108  	// send by setting the "Trailer" header to the names of the
   109  	// trailer keys which will come later. In this case, those
   110  	// keys of the Header map are treated as if they were
   111  	// trailers. See the example. The second way, for trailer
   112  	// keys not known to the [Handler] until after the first [ResponseWriter.Write],
   113  	// is to prefix the [Header] map keys with the [TrailerPrefix]
   114  	// constant value.
   115  	//
   116  	// To suppress automatic response headers (such as "Date"), set
   117  	// their value to nil.
   118  	Header() Header
   119  
   120  	// Write writes the data to the connection as part of an HTTP reply.
   121  	//
   122  	// If [ResponseWriter.WriteHeader] has not yet been called, Write calls
   123  	// WriteHeader(http.StatusOK) before writing the data. If the Header
   124  	// does not contain a Content-Type line, Write adds a Content-Type set
   125  	// to the result of passing the initial 512 bytes of written data to
   126  	// [DetectContentType]. Additionally, if the total size of all written
   127  	// data is under a few KB and there are no Flush calls, the
   128  	// Content-Length header is added automatically.
   129  	//
   130  	// Depending on the HTTP protocol version and the client, calling
   131  	// Write or WriteHeader may prevent future reads on the
   132  	// Request.Body. For HTTP/1.x requests, handlers should read any
   133  	// needed request body data before writing the response. Once the
   134  	// headers have been flushed (due to either an explicit Flusher.Flush
   135  	// call or writing enough data to trigger a flush), the request body
   136  	// may be unavailable. For HTTP/2 requests, the Go HTTP server permits
   137  	// handlers to continue to read the request body while concurrently
   138  	// writing the response. However, such behavior may not be supported
   139  	// by all HTTP/2 clients. Handlers should read before writing if
   140  	// possible to maximize compatibility.
   141  	Write([]byte) (int, error)
   142  
   143  	// WriteHeader sends an HTTP response header with the provided
   144  	// status code.
   145  	//
   146  	// If WriteHeader is not called explicitly, the first call to Write
   147  	// will trigger an implicit WriteHeader(http.StatusOK).
   148  	// Thus explicit calls to WriteHeader are mainly used to
   149  	// send error codes or 1xx informational responses.
   150  	//
   151  	// The provided code must be a valid HTTP 1xx-5xx status code.
   152  	// Any number of 1xx headers may be written, followed by at most
   153  	// one 2xx-5xx header. 1xx headers are sent immediately, but 2xx-5xx
   154  	// headers may be buffered. Use the Flusher interface to send
   155  	// buffered data. The header map is cleared when 2xx-5xx headers are
   156  	// sent, but not with 1xx headers.
   157  	//
   158  	// The server will automatically send a 100 (Continue) header
   159  	// on the first read from the request body if the request has
   160  	// an "Expect: 100-continue" header.
   161  	WriteHeader(statusCode int)
   162  }
   163  
   164  // The Flusher interface is implemented by ResponseWriters that allow
   165  // an HTTP handler to flush buffered data to the client.
   166  //
   167  // The default HTTP/1.x and HTTP/2 [ResponseWriter] implementations
   168  // support [Flusher], but ResponseWriter wrappers may not. Handlers
   169  // should always test for this ability at runtime.
   170  //
   171  // Note that even for ResponseWriters that support Flush,
   172  // if the client is connected through an HTTP proxy,
   173  // the buffered data may not reach the client until the response
   174  // completes.
   175  type Flusher interface {
   176  	// Flush sends any buffered data to the client.
   177  	Flush()
   178  }
   179  
   180  // The Hijacker interface is implemented by ResponseWriters that allow
   181  // an HTTP handler to take over the connection.
   182  //
   183  // The default [ResponseWriter] for HTTP/1.x connections supports
   184  // Hijacker, but HTTP/2 connections intentionally do not.
   185  // ResponseWriter wrappers may also not support Hijacker. Handlers
   186  // should always test for this ability at runtime.
   187  type Hijacker interface {
   188  	// Hijack lets the caller take over the connection.
   189  	// After a call to Hijack the HTTP server library
   190  	// will not do anything else with the connection.
   191  	//
   192  	// It becomes the caller's responsibility to manage
   193  	// and close the connection.
   194  	//
   195  	// The returned net.Conn may have read or write deadlines
   196  	// already set, depending on the configuration of the
   197  	// Server. It is the caller's responsibility to set
   198  	// or clear those deadlines as needed.
   199  	//
   200  	// The returned bufio.Reader may contain unprocessed buffered
   201  	// data from the client.
   202  	//
   203  	// After a call to Hijack, the original Request.Body must not
   204  	// be used. The original Request's Context remains valid and
   205  	// is not canceled until the Request's ServeHTTP method
   206  	// returns.
   207  	Hijack() (net.Conn, *bufio.ReadWriter, error)
   208  }
   209  
   210  // The CloseNotifier interface is implemented by ResponseWriters which
   211  // allow detecting when the underlying connection has gone away.
   212  //
   213  // This mechanism can be used to cancel long operations on the server
   214  // if the client has disconnected before the response is ready.
   215  //
   216  // Deprecated: the CloseNotifier interface predates Go's context package.
   217  // New code should use [Request.Context] instead.
   218  type CloseNotifier interface {
   219  	// CloseNotify returns a channel that receives at most a
   220  	// single value (true) when the client connection has gone
   221  	// away.
   222  	//
   223  	// CloseNotify may wait to notify until Request.Body has been
   224  	// fully read.
   225  	//
   226  	// After the Handler has returned, there is no guarantee
   227  	// that the channel receives a value.
   228  	//
   229  	// If the protocol is HTTP/1.1 and CloseNotify is called while
   230  	// processing an idempotent request (such as GET) while
   231  	// HTTP/1.1 pipelining is in use, the arrival of a subsequent
   232  	// pipelined request may cause a value to be sent on the
   233  	// returned channel. In practice HTTP/1.1 pipelining is not
   234  	// enabled in browsers and not seen often in the wild. If this
   235  	// is a problem, use HTTP/2 or only use CloseNotify on methods
   236  	// such as POST.
   237  	CloseNotify() <-chan bool
   238  }
   239  
   240  var (
   241  	// ServerContextKey is a context key. It can be used in HTTP
   242  	// handlers with Context.Value to access the server that
   243  	// started the handler. The associated value will be of
   244  	// type *Server.
   245  	ServerContextKey = &contextKey{"http-server"}
   246  
   247  	// LocalAddrContextKey is a context key. It can be used in
   248  	// HTTP handlers with Context.Value to access the local
   249  	// address the connection arrived on.
   250  	// The associated value will be of type net.Addr.
   251  	LocalAddrContextKey = &contextKey{"local-addr"}
   252  )
   253  
   254  // A conn represents the server side of an HTTP connection.
   255  type conn struct {
   256  	// server is the server on which the connection arrived.
   257  	// Immutable; never nil.
   258  	server *Server
   259  
   260  	// cancelCtx cancels the connection-level context.
   261  	cancelCtx context.CancelFunc
   262  
   263  	// rwc is the underlying network connection.
   264  	// This is never wrapped by other types and is the value given out
   265  	// to [Hijacker] callers. It is usually of type *net.TCPConn or
   266  	// *tls.Conn.
   267  	rwc net.Conn
   268  
   269  	// remoteAddr is rwc.RemoteAddr().String(). It is not populated synchronously
   270  	// inside the Listener's Accept goroutine, as some implementations block.
   271  	// It is populated immediately inside the (*conn).serve goroutine.
   272  	// This is the value of a Handler's (*Request).RemoteAddr.
   273  	remoteAddr string
   274  
   275  	// tlsState is the TLS connection state when using TLS.
   276  	// nil means not TLS.
   277  	tlsState *tls.ConnectionState
   278  
   279  	// werr is set to the first write error to rwc.
   280  	// It is set via checkConnErrorWriter{w}, where bufw writes.
   281  	werr error
   282  
   283  	// r is bufr's read source. It's a wrapper around rwc that provides
   284  	// io.LimitedReader-style limiting (while reading request headers)
   285  	// and functionality to support CloseNotifier. See *connReader docs.
   286  	r *connReader
   287  
   288  	// bufr reads from r.
   289  	bufr *bufio.Reader
   290  
   291  	// bufw writes to checkConnErrorWriter{c}, which populates werr on error.
   292  	bufw *bufio.Writer
   293  
   294  	// lastMethod is the method of the most recent request
   295  	// on this connection, if any.
   296  	lastMethod string
   297  
   298  	curReq atomic.Pointer[response] // (which has a Request in it)
   299  
   300  	curState atomic.Uint64 // packed (unixtime<<8|uint8(ConnState))
   301  
   302  	// mu guards hijackedv
   303  	mu sync.Mutex
   304  
   305  	// hijackedv is whether this connection has been hijacked
   306  	// by a Handler with the Hijacker interface.
   307  	// It is guarded by mu.
   308  	hijackedv bool
   309  
   310  	// http2HandedOff is whether the connection has been handed off to
   311  	// the HTTP/2 server, which then owns closing the connection and
   312  	// running the final ConnState hook, possibly after (*conn).serve
   313  	// has returned. It is only accessed by the (*conn).serve goroutine.
   314  	http2HandedOff bool
   315  }
   316  
   317  func (c *conn) hijacked() bool {
   318  	c.mu.Lock()
   319  	defer c.mu.Unlock()
   320  	return c.hijackedv
   321  }
   322  
   323  // c.mu must be held.
   324  func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
   325  	if c.hijackedv {
   326  		return nil, nil, ErrHijacked
   327  	}
   328  	c.r.abortPendingRead()
   329  
   330  	c.hijackedv = true
   331  	rwc = c.rwc
   332  	rwc.SetDeadline(time.Time{})
   333  
   334  	if c.r.hasByte {
   335  		if _, err := c.bufr.Peek(c.bufr.Buffered() + 1); err != nil {
   336  			return nil, nil, fmt.Errorf("unexpected Peek failure reading buffered byte: %v", err)
   337  		}
   338  	}
   339  	c.bufw.Reset(rwc)
   340  	buf = bufio.NewReadWriter(c.bufr, c.bufw)
   341  
   342  	c.setState(rwc, StateHijacked, runHooks)
   343  	return
   344  }
   345  
   346  // This should be >= 512 bytes for DetectContentType,
   347  // but otherwise it's somewhat arbitrary.
   348  const bufferBeforeChunkingSize = 2048
   349  
   350  // chunkWriter writes to a response's conn buffer, and is the writer
   351  // wrapped by the response.w buffered writer.
   352  //
   353  // chunkWriter also is responsible for finalizing the Header, including
   354  // conditionally setting the Content-Type and setting a Content-Length
   355  // in cases where the handler's final output is smaller than the buffer
   356  // size. It also conditionally adds chunk headers, when in chunking mode.
   357  //
   358  // See the comment above (*response).Write for the entire write flow.
   359  type chunkWriter struct {
   360  	res *response
   361  
   362  	// header is either nil or a deep clone of res.handlerHeader
   363  	// at the time of res.writeHeader, if res.writeHeader is
   364  	// called and extra buffering is being done to calculate
   365  	// Content-Type and/or Content-Length.
   366  	header Header
   367  
   368  	// wroteHeader tells whether the header's been written to "the
   369  	// wire" (or rather: w.conn.buf). this is unlike
   370  	// (*response).wroteHeader, which tells only whether it was
   371  	// logically written.
   372  	wroteHeader bool
   373  
   374  	// set by the writeHeader method:
   375  	chunking bool // using chunked transfer encoding for reply body
   376  }
   377  
   378  var (
   379  	crlf       = []byte("\r\n")
   380  	colonSpace = []byte(": ")
   381  )
   382  
   383  func (cw *chunkWriter) Write(p []byte) (n int, err error) {
   384  	if !cw.wroteHeader {
   385  		cw.writeHeader(p)
   386  	}
   387  	if cw.res.req.Method == "HEAD" {
   388  		// Eat writes.
   389  		return len(p), nil
   390  	}
   391  	if cw.chunking {
   392  		_, err = fmt.Fprintf(cw.res.conn.bufw, "%x\r\n", len(p))
   393  		if err != nil {
   394  			cw.res.conn.rwc.Close()
   395  			return
   396  		}
   397  	}
   398  	n, err = cw.res.conn.bufw.Write(p)
   399  	if cw.chunking && err == nil {
   400  		_, err = cw.res.conn.bufw.Write(crlf)
   401  	}
   402  	if err != nil {
   403  		cw.res.conn.rwc.Close()
   404  	}
   405  	return
   406  }
   407  
   408  func (cw *chunkWriter) flush() error {
   409  	if !cw.wroteHeader {
   410  		cw.writeHeader(nil)
   411  	}
   412  	return cw.res.conn.bufw.Flush()
   413  }
   414  
   415  func (cw *chunkWriter) close() {
   416  	if !cw.wroteHeader {
   417  		cw.writeHeader(nil)
   418  	}
   419  	if cw.chunking {
   420  		bw := cw.res.conn.bufw // conn's bufio writer
   421  		// zero chunk to mark EOF
   422  		bw.WriteString("0\r\n")
   423  		if trailers := cw.res.finalTrailers(); trailers != nil {
   424  			trailers.Write(bw) // the writer handles noting errors
   425  		}
   426  		// final blank line after the trailers (whether
   427  		// present or not)
   428  		bw.WriteString("\r\n")
   429  	}
   430  }
   431  
   432  // A response represents the server side of an HTTP response.
   433  type response struct {
   434  	conn             *conn
   435  	req              *Request           // request for this response
   436  	reqBody          *body              // nil when NoBody
   437  	cancelCtx        context.CancelFunc // when ServeHTTP exits
   438  	wroteHeader      bool               // a non-1xx header has been (logically) written
   439  	wants10KeepAlive bool               // HTTP/1.0 w/ Connection "keep-alive"
   440  	wantsClose       bool               // HTTP request has Connection "close"
   441  	ecReader         *expectContinueReader
   442  
   443  	// canWriteContinue is an atomic boolean that says whether or
   444  	// not a 100 Continue header can be written to the
   445  	// connection.
   446  	// writeContinueMu must be held while writing the header.
   447  	// These two fields together synchronize the body reader (the
   448  	// expectContinueReader, which wants to write 100 Continue)
   449  	// against the main writer.
   450  	writeContinueMu  sync.Mutex
   451  	canWriteContinue atomic.Bool
   452  
   453  	w  *bufio.Writer // buffers output in chunks to chunkWriter
   454  	cw chunkWriter
   455  
   456  	// handlerHeader is the Header that Handlers get access to,
   457  	// which may be retained and mutated even after WriteHeader.
   458  	// handlerHeader is copied into cw.header at WriteHeader
   459  	// time, and privately mutated thereafter.
   460  	handlerHeader Header
   461  	calledHeader  bool // handler accessed handlerHeader via Header
   462  
   463  	written       int64 // number of bytes written in body
   464  	contentLength int64 // explicitly-declared Content-Length; or -1
   465  	status        int   // status code passed to WriteHeader
   466  
   467  	// close connection after this reply.  set on request and
   468  	// updated after response from handler if there's a
   469  	// "Connection: keep-alive" response header and a
   470  	// Content-Length.
   471  	closeAfterReply bool
   472  
   473  	// When fullDuplex is false (the default), we consume any remaining
   474  	// request body before starting to write a response.
   475  	fullDuplex bool
   476  
   477  	// requestBodyLimitHit is set by requestTooLarge when
   478  	// maxBytesReader hits its max size. It is checked in
   479  	// WriteHeader, to make sure we don't consume the
   480  	// remaining request body to try to advance to the next HTTP
   481  	// request. Instead, when this is set, we stop reading
   482  	// subsequent requests on this connection and stop reading
   483  	// input from it.
   484  	requestBodyLimitHit atomic.Bool
   485  
   486  	// trailers are the headers to be sent after the handler
   487  	// finishes writing the body. This field is initialized from
   488  	// the Trailer response header when the response header is
   489  	// written.
   490  	trailers []string
   491  
   492  	handlerDone atomic.Bool // set true when the handler exits
   493  
   494  	// Buffers for Date, Content-Length, and status code
   495  	dateBuf   [len(TimeFormat)]byte
   496  	clenBuf   [10]byte
   497  	statusBuf [3]byte
   498  
   499  	// lazyCloseNotifyMu protects closeNotifyCh and closeNotifyTriggered.
   500  	lazyCloseNotifyMu sync.Mutex
   501  	// closeNotifyCh is the channel returned by CloseNotify.
   502  	closeNotifyCh chan bool
   503  	// closeNotifyTriggered tracks prior closeNotify calls.
   504  	closeNotifyTriggered bool
   505  }
   506  
   507  func (c *response) SetReadDeadline(deadline time.Time) error {
   508  	return c.conn.rwc.SetReadDeadline(deadline)
   509  }
   510  
   511  func (c *response) SetWriteDeadline(deadline time.Time) error {
   512  	return c.conn.rwc.SetWriteDeadline(deadline)
   513  }
   514  
   515  func (c *response) EnableFullDuplex() error {
   516  	c.fullDuplex = true
   517  	return nil
   518  }
   519  
   520  // TrailerPrefix is a magic prefix for [ResponseWriter.Header] map keys
   521  // that, if present, signals that the map entry is actually for
   522  // the response trailers, and not the response headers. The prefix
   523  // is stripped after the ServeHTTP call finishes and the values are
   524  // sent in the trailers.
   525  //
   526  // This mechanism is intended only for trailers that are not known
   527  // prior to the headers being written. If the set of trailers is fixed
   528  // or known before the header is written, the normal Go trailers mechanism
   529  // is preferred:
   530  //
   531  //	https://pkg.go.dev/net/http#ResponseWriter
   532  //	https://pkg.go.dev/net/http#example-ResponseWriter-Trailers
   533  const TrailerPrefix = "Trailer:"
   534  
   535  // finalTrailers is called after the Handler exits and returns a non-nil
   536  // value if the Handler set any trailers.
   537  func (w *response) finalTrailers() Header {
   538  	var t Header
   539  	for k, vv := range w.handlerHeader {
   540  		if kk, found := strings.CutPrefix(k, TrailerPrefix); found {
   541  			if t == nil {
   542  				t = make(Header)
   543  			}
   544  			t[kk] = vv
   545  		}
   546  	}
   547  	for _, k := range w.trailers {
   548  		if t == nil {
   549  			t = make(Header)
   550  		}
   551  		for _, v := range w.handlerHeader[k] {
   552  			t.Add(k, v)
   553  		}
   554  	}
   555  	return t
   556  }
   557  
   558  // declareTrailer is called for each Trailer header when the
   559  // response header is written. It notes that a header will need to be
   560  // written in the trailers at the end of the response.
   561  func (w *response) declareTrailer(k string) {
   562  	k = CanonicalHeaderKey(k)
   563  	if !httpguts.ValidTrailerHeader(k) {
   564  		// Forbidden by RFC 7230, section 4.1.2
   565  		return
   566  	}
   567  	w.trailers = append(w.trailers, k)
   568  }
   569  
   570  // requestTooLarge is called by maxBytesReader when too much input has
   571  // been read from the client.
   572  func (w *response) requestTooLarge() {
   573  	w.requestBodyLimitHit.Store(true)
   574  }
   575  
   576  // disableWriteContinue stops Request.Body.Read from sending an automatic
   577  // 100 Continue. As the name implies, it is only useful when the request
   578  // expects a 100 Continue and the body is wrapped in an expectContinueReader;
   579  // otherwise, it is a no-op.
   580  // If a 100-Continue is being written, it waits for it to complete before
   581  // continuing. If skipDrain is true, it also prevents the server from draining
   582  // the request body and flags the connection to be closed after the reply, as
   583  // the client will never send the body.
   584  func (w *response) disableWriteContinue(skipDrain bool) {
   585  	if w.ecReader == nil {
   586  		return
   587  	}
   588  	w.writeContinueMu.Lock()
   589  	if w.canWriteContinue.Load() {
   590  		w.canWriteContinue.Store(false)
   591  		if skipDrain {
   592  			// Make sure that the connection will not be reused by sending
   593  			// "Connection: close" header in the response.
   594  			w.closeAfterReply = true
   595  			// Ensure that the body will not be drained in Close.
   596  			w.ecReader.closed.Store(true)
   597  		}
   598  	}
   599  	w.writeContinueMu.Unlock()
   600  }
   601  
   602  // writerOnly hides an io.Writer value's optional ReadFrom method
   603  // from io.Copy.
   604  type writerOnly struct {
   605  	io.Writer
   606  }
   607  
   608  // ReadFrom is here to optimize copying from an [*os.File] regular file
   609  // to a [*net.TCPConn] with sendfile, or from a supported src type such
   610  // as a *net.TCPConn on Linux with splice.
   611  func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
   612  	buf := getCopyBuf()
   613  	defer putCopyBuf(buf)
   614  
   615  	// Our underlying w.conn.rwc is usually a *TCPConn (with its
   616  	// own ReadFrom method). If not, just fall back to the normal
   617  	// copy method.
   618  	rf, ok := w.conn.rwc.(io.ReaderFrom)
   619  	if !ok {
   620  		return io.CopyBuffer(writerOnly{w}, src, buf)
   621  	}
   622  
   623  	// Copy the first sniffLen bytes before switching to ReadFrom.
   624  	// This ensures we don't start writing the response before the
   625  	// source is available (see golang.org/issue/5660) and provides
   626  	// enough bytes to perform Content-Type sniffing when required.
   627  	if !w.cw.wroteHeader {
   628  		n0, err := io.CopyBuffer(writerOnly{w}, io.LimitReader(src, internal.SniffLen), buf)
   629  		n += n0
   630  		if err != nil || n0 < internal.SniffLen {
   631  			return n, err
   632  		}
   633  	}
   634  
   635  	w.w.Flush()  // get rid of any previous writes
   636  	w.cw.flush() // make sure Header is written; flush data to rwc
   637  
   638  	// Now that cw has been flushed, its chunking field is guaranteed initialized.
   639  	if !w.cw.chunking && w.bodyAllowed() && w.req.Method != "HEAD" {
   640  		// When a content length is declared, but exceeded; any excess bytes
   641  		// from src should be ignored, and ErrContentLength should be returned.
   642  		// This mirrors the behavior of response.Write.
   643  		if w.contentLength != -1 {
   644  			defer func(originalReader io.Reader) {
   645  				if w.written != w.contentLength {
   646  					return
   647  				}
   648  				if n, _ := originalReader.Read([]byte{0}); err == nil && n != 0 {
   649  					err = ErrContentLength
   650  				}
   651  			}(src)
   652  			// src can be an io.LimitedReader already. To avoid unnecessary
   653  			// alloc and having to unnest readers repeatedly in net.sendFile,
   654  			// just adjust the existing LimitedReader N when this is the case.
   655  			if lr, ok := src.(*io.LimitedReader); ok {
   656  				if lenDiff := lr.N - (w.contentLength - w.written); lenDiff > 0 {
   657  					defer func() { lr.N += lenDiff }()
   658  					lr.N -= lenDiff
   659  				}
   660  			} else {
   661  				src = io.LimitReader(src, w.contentLength-w.written)
   662  			}
   663  		}
   664  		n0, err := rf.ReadFrom(src)
   665  		n += n0
   666  		w.written += n0
   667  		return n, err
   668  	}
   669  
   670  	n0, err := io.CopyBuffer(writerOnly{w}, src, buf)
   671  	n += n0
   672  	return n, err
   673  }
   674  
   675  // debugServerConnections controls whether all server connections are wrapped
   676  // with a verbose logging wrapper.
   677  const debugServerConnections = false
   678  
   679  // Create new connection from rwc.
   680  func (s *Server) newConn(rwc net.Conn) *conn {
   681  	c := &conn{
   682  		server: s,
   683  		rwc:    rwc,
   684  	}
   685  	if debugServerConnections {
   686  		c.rwc = newLoggingConn("server", c.rwc)
   687  	}
   688  	return c
   689  }
   690  
   691  type readResult struct {
   692  	_   incomparable
   693  	n   int
   694  	err error
   695  	b   byte // byte read, if n == 1
   696  }
   697  
   698  // connReader is the io.Reader wrapper used by *conn. It combines a
   699  // selectively-activated io.LimitedReader (to bound request header
   700  // read sizes) with support for selectively keeping an io.Reader.Read
   701  // call blocked in a background goroutine to wait for activity and
   702  // trigger a CloseNotifier channel.
   703  // After a Handler has hijacked the conn and exited, connReader behaves like a
   704  // proxy for the net.Conn and the aforementioned behavior is bypassed.
   705  type connReader struct {
   706  	rwc net.Conn // rwc is the underlying network connection.
   707  
   708  	mu      sync.Mutex // guards following
   709  	conn    *conn      // conn is nil after handler exit.
   710  	hasByte bool
   711  	byteBuf [1]byte
   712  	cond    *sync.Cond
   713  	inRead  bool
   714  	aborted bool  // set true before conn.rwc deadline is set to past
   715  	probing bool  // set true during conn.serve's idle probe read, when a timeout is expected
   716  	remain  int64 // bytes remaining
   717  }
   718  
   719  func (cr *connReader) lock() {
   720  	cr.mu.Lock()
   721  	if cr.cond == nil {
   722  		cr.cond = sync.NewCond(&cr.mu)
   723  	}
   724  }
   725  
   726  func (cr *connReader) unlock() { cr.mu.Unlock() }
   727  
   728  func (cr *connReader) releaseConn() {
   729  	cr.lock()
   730  	defer cr.unlock()
   731  	cr.conn = nil
   732  }
   733  
   734  func (cr *connReader) startBackgroundRead() {
   735  	cr.lock()
   736  	defer cr.unlock()
   737  	if cr.inRead {
   738  		panic("invalid concurrent Body.Read call")
   739  	}
   740  	if cr.hasByte {
   741  		return
   742  	}
   743  	cr.inRead = true
   744  	cr.rwc.SetReadDeadline(time.Time{})
   745  	go cr.backgroundRead()
   746  }
   747  
   748  func (cr *connReader) backgroundRead() {
   749  	n, err := cr.rwc.Read(cr.byteBuf[:])
   750  	cr.lock()
   751  	if n == 1 {
   752  		cr.hasByte = true
   753  		// We were past the end of the previous request's body already
   754  		// (since we wouldn't be in a background read otherwise), so
   755  		// this is a pipelined HTTP request. Prior to Go 1.11 we used to
   756  		// send on the CloseNotify channel and cancel the context here,
   757  		// but the behavior was documented as only "may", and we only
   758  		// did that because that's how CloseNotify accidentally behaved
   759  		// in very early Go releases prior to context support. Once we
   760  		// added context support, people used a Handler's
   761  		// Request.Context() and passed it along. Having that context
   762  		// cancel on pipelined HTTP requests caused problems.
   763  		// Fortunately, almost nothing uses HTTP/1.x pipelining.
   764  		// Unfortunately, apt-get does, or sometimes does.
   765  		// New Go 1.11 behavior: don't fire CloseNotify or cancel
   766  		// contexts on pipelined requests. Shouldn't affect people, but
   767  		// fixes cases like Issue 23921. This does mean that a client
   768  		// closing their TCP connection after sending a pipelined
   769  		// request won't cancel the context, but we'll catch that on any
   770  		// write failure (in checkConnErrorWriter.Write).
   771  		// If the server never writes, yes, there are still contrived
   772  		// server & client behaviors where this fails to ever cancel the
   773  		// context, but that's kinda why HTTP/1.x pipelining died
   774  		// anyway.
   775  	}
   776  	if ne, ok := err.(net.Error); ok && cr.aborted && ne.Timeout() {
   777  		// Ignore this error. It's the expected error from
   778  		// another goroutine calling abortPendingRead.
   779  	} else if err != nil {
   780  		cr.handleReadErrorLocked(err)
   781  	}
   782  	cr.aborted = false
   783  	cr.inRead = false
   784  	cr.unlock()
   785  	cr.cond.Broadcast()
   786  }
   787  
   788  // idleBufsReleaseDelay is how long a keep-alive connection waits for
   789  // its next request before it is considered idle and its bufio buffers
   790  // are released to their pools. It trades a little extra work on
   791  // connections that idle past it against pinning ~8 kB of buffers on
   792  // every waiting connection.
   793  const idleBufsReleaseDelay = 50 * time.Millisecond
   794  
   795  // waitReadable blocks until data arrives on the connection, stashing
   796  // the byte it reads for the next connReader.Read, and reports whether
   797  // data arrived. It is called between requests, after the connection's
   798  // bufio buffers have been released to their pools, so that an idle
   799  // connection pins no buffer memory while it waits, possibly for a long
   800  // time, for the next request. A false return means the read failed
   801  // (EOF, a timeout, or another error) and the error has been handled by
   802  // handleReadErrorLocked.
   803  func (cr *connReader) waitReadable() (readable bool) {
   804  	cr.lock()
   805  	if cr.inRead {
   806  		panic("invalid concurrent connReader.waitReadable call")
   807  	}
   808  	if cr.hasByte {
   809  		cr.unlock()
   810  		return true
   811  	}
   812  	cr.inRead = true
   813  	cr.unlock()
   814  	n, err := cr.rwc.Read(cr.byteBuf[:])
   815  	cr.lock()
   816  	cr.inRead = false
   817  	if n == 1 {
   818  		cr.hasByte = true
   819  	}
   820  	if err != nil {
   821  		cr.handleReadErrorLocked(err)
   822  	}
   823  	cr.unlock()
   824  	cr.cond.Broadcast()
   825  	return n == 1 && err == nil
   826  }
   827  
   828  func (cr *connReader) abortPendingRead() {
   829  	cr.lock()
   830  	defer cr.unlock()
   831  	if !cr.inRead {
   832  		return
   833  	}
   834  	cr.aborted = true
   835  	cr.rwc.SetReadDeadline(aLongTimeAgo)
   836  	for cr.inRead {
   837  		cr.cond.Wait()
   838  	}
   839  	cr.rwc.SetReadDeadline(time.Time{})
   840  }
   841  
   842  func (cr *connReader) setProbing(v bool) {
   843  	cr.lock()
   844  	cr.probing = v
   845  	cr.unlock()
   846  }
   847  
   848  func (cr *connReader) setReadLimit(remain int64) { cr.remain = remain }
   849  func (cr *connReader) setInfiniteReadLimit()     { cr.remain = maxInt64 }
   850  func (cr *connReader) hitReadLimit() bool        { return cr.remain <= 0 }
   851  
   852  // isNetTimeoutError reports whether err is a net.Error with Timeout()
   853  // == true, such as an error from an expired connection deadline.
   854  // It is used instead of checking errors.Is(err, os.ErrDeadlineExceeded)
   855  // because non-standard net.Conn implementations may return bespoke
   856  // timeout errors that don't wrap os.ErrDeadlineExceeded as net package
   857  // connections have since Go 1.15.
   858  func isNetTimeoutError(err error) bool {
   859  	ne, ok := errors.AsType[net.Error](err)
   860  	return ok && ne.Timeout()
   861  }
   862  
   863  // handleReadErrorLocked is called whenever a Read from the client returns a
   864  // non-nil error.
   865  //
   866  // The provided non-nil err is almost always io.EOF or a "use of
   867  // closed network connection". Except for an expected timeout during the
   868  // serve loop's idle probe read, any error means the connection is dead
   869  // and we should shut down its context. An error other than io.EOF or an
   870  // expired read deadline also means the connection is dead for writing,
   871  // so any response write still in flight is aborted.
   872  //
   873  // The caller must hold connReader.mu.
   874  func (cr *connReader) handleReadErrorLocked(err error) {
   875  	if cr.conn == nil {
   876  		return
   877  	}
   878  	// A timeout during conn.serve's idle probe read means only that the
   879  	// connection has gone idle; it is otherwise fine. In particular,
   880  	// don't cancel the connection-level context: it is the parent of
   881  	// every subsequent request's context on this connection, so
   882  	// canceling it would deliver already-canceled contexts to all
   883  	// future requests.
   884  	if cr.probing && isNetTimeoutError(err) {
   885  		return
   886  	}
   887  	// io.EOF means the client half closed and may still be waiting for a
   888  	// response, and an expired read deadline is the server's own doing.
   889  	// Any other error means the connection is gone in both directions, so
   890  	// unblock a response write in flight.
   891  	//
   892  	// This matters because on some systems the poller never reports the
   893  	// socket as writable again once a read has consumed its pending error,
   894  	// so a handler blocked writing a large response would otherwise block
   895  	// forever. See go.dev/issue/78438.
   896  	if err != io.EOF && !isNetTimeoutError(err) {
   897  		cr.conn.rwc.SetWriteDeadline(aLongTimeAgo)
   898  	}
   899  	cr.conn.cancelCtx()
   900  	if res := cr.conn.curReq.Load(); res != nil {
   901  		res.closeNotify()
   902  	}
   903  }
   904  
   905  func (cr *connReader) Read(p []byte) (n int, err error) {
   906  	cr.lock()
   907  	if cr.conn == nil {
   908  		cr.unlock()
   909  		return cr.rwc.Read(p)
   910  	}
   911  	if cr.inRead {
   912  		hijacked := cr.conn.hijacked()
   913  		cr.unlock()
   914  		if hijacked {
   915  			panic("invalid Body.Read call. After hijacked, the original Request must not be used")
   916  		}
   917  		panic("invalid concurrent Body.Read call")
   918  	}
   919  	if cr.hitReadLimit() {
   920  		cr.unlock()
   921  		return 0, io.EOF
   922  	}
   923  	if len(p) == 0 {
   924  		cr.unlock()
   925  		return 0, nil
   926  	}
   927  	if int64(len(p)) > cr.remain {
   928  		p = p[:cr.remain]
   929  	}
   930  	if cr.hasByte {
   931  		p[0] = cr.byteBuf[0]
   932  		cr.hasByte = false
   933  		cr.unlock()
   934  		return 1, nil
   935  	}
   936  	cr.inRead = true
   937  	cr.unlock()
   938  	n, err = cr.rwc.Read(p)
   939  
   940  	cr.lock()
   941  	cr.inRead = false
   942  	if err != nil {
   943  		cr.handleReadErrorLocked(err)
   944  	}
   945  	cr.remain -= int64(n)
   946  	cr.unlock()
   947  
   948  	cr.cond.Broadcast()
   949  	return n, err
   950  }
   951  
   952  var (
   953  	bufioReaderPool   sync.Pool
   954  	bufioWriter2kPool sync.Pool
   955  	bufioWriter4kPool sync.Pool
   956  )
   957  
   958  const copyBufPoolSize = 32 * 1024
   959  
   960  var copyBufPool = sync.Pool{New: func() any { return new([copyBufPoolSize]byte) }}
   961  
   962  func getCopyBuf() []byte {
   963  	return copyBufPool.Get().(*[copyBufPoolSize]byte)[:]
   964  }
   965  
   966  func putCopyBuf(b []byte) {
   967  	if len(b) != copyBufPoolSize {
   968  		panic("trying to put back buffer of the wrong size in the copyBufPool")
   969  	}
   970  	copyBufPool.Put((*[copyBufPoolSize]byte)(b))
   971  }
   972  
   973  func bufioWriterPool(size int) *sync.Pool {
   974  	switch size {
   975  	case 2 << 10:
   976  		return &bufioWriter2kPool
   977  	case 4 << 10:
   978  		return &bufioWriter4kPool
   979  	}
   980  	return nil
   981  }
   982  
   983  func newBufioReader(r io.Reader) *bufio.Reader {
   984  	if v := bufioReaderPool.Get(); v != nil {
   985  		br := v.(*bufio.Reader)
   986  		br.Reset(r)
   987  		return br
   988  	}
   989  	// Note: if this reader size is ever changed, update
   990  	// TestHandlerBodyClose's assumptions.
   991  	return bufio.NewReader(r)
   992  }
   993  
   994  func putBufioReader(br *bufio.Reader) {
   995  	br.Reset(nil)
   996  	bufioReaderPool.Put(br)
   997  }
   998  
   999  func newBufioWriterSize(w io.Writer, size int) *bufio.Writer {
  1000  	pool := bufioWriterPool(size)
  1001  	if pool != nil {
  1002  		if v := pool.Get(); v != nil {
  1003  			bw := v.(*bufio.Writer)
  1004  			bw.Reset(w)
  1005  			return bw
  1006  		}
  1007  	}
  1008  	return bufio.NewWriterSize(w, size)
  1009  }
  1010  
  1011  func putBufioWriter(bw *bufio.Writer) {
  1012  	bw.Reset(nil)
  1013  	if pool := bufioWriterPool(bw.Available()); pool != nil {
  1014  		pool.Put(bw)
  1015  	}
  1016  }
  1017  
  1018  // DefaultMaxHeaderBytes is the maximum permitted size of the headers
  1019  // in an HTTP request.
  1020  // This can be overridden by setting [Server.MaxHeaderBytes].
  1021  const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
  1022  
  1023  // DefaultMaxHeaderValueCount is the maximum permitted number of
  1024  // header values in an HTTP request.
  1025  // This can be overridden by setting [Server.MaxHeaderValueCount].
  1026  const DefaultMaxHeaderValueCount = 500
  1027  
  1028  func (s *Server) maxHeaderBytes() int {
  1029  	if s.MaxHeaderBytes > 0 {
  1030  		return s.MaxHeaderBytes
  1031  	}
  1032  	return DefaultMaxHeaderBytes
  1033  }
  1034  
  1035  func (s *Server) maxHeaderValueCount() int {
  1036  	if s.MaxHeaderValueCount > 0 {
  1037  		return s.MaxHeaderValueCount
  1038  	}
  1039  	return DefaultMaxHeaderValueCount
  1040  }
  1041  
  1042  func (s *Server) initialReadLimitSize() int64 {
  1043  	return int64(s.maxHeaderBytes()) + 4096 // bufio slop
  1044  }
  1045  
  1046  // tlsHandshakeTimeout returns the time limit permitted for the TLS
  1047  // handshake, or zero for unlimited.
  1048  //
  1049  // It returns the minimum of any positive ReadHeaderTimeout,
  1050  // ReadTimeout, or WriteTimeout.
  1051  func (s *Server) tlsHandshakeTimeout() time.Duration {
  1052  	var ret time.Duration
  1053  	for _, v := range [...]time.Duration{
  1054  		s.ReadHeaderTimeout,
  1055  		s.ReadTimeout,
  1056  		s.WriteTimeout,
  1057  	} {
  1058  		if v <= 0 {
  1059  			continue
  1060  		}
  1061  		if ret == 0 || v < ret {
  1062  			ret = v
  1063  		}
  1064  	}
  1065  	return ret
  1066  }
  1067  
  1068  // wrapper around io.ReadCloser which on first read, sends an
  1069  // HTTP/1.1 100 Continue header
  1070  type expectContinueReader struct {
  1071  	resp       *response
  1072  	readCloser io.ReadCloser
  1073  	closed     atomic.Bool
  1074  }
  1075  
  1076  func (ecr *expectContinueReader) Read(p []byte) (n int, err error) {
  1077  	if ecr.closed.Load() {
  1078  		return 0, ErrBodyReadAfterClose
  1079  	}
  1080  	w := ecr.resp
  1081  	if w.canWriteContinue.Load() {
  1082  		w.writeContinueMu.Lock()
  1083  		if w.canWriteContinue.Load() {
  1084  			w.conn.bufw.WriteString("HTTP/1.1 100 Continue\r\n\r\n")
  1085  			w.conn.bufw.Flush()
  1086  			w.canWriteContinue.Store(false)
  1087  		}
  1088  		w.writeContinueMu.Unlock()
  1089  	}
  1090  	return ecr.readCloser.Read(p)
  1091  }
  1092  
  1093  func (ecr *expectContinueReader) Close() error {
  1094  	if ecr.resp.canWriteContinue.Load() {
  1095  		ecr.resp.disableWriteContinue(true)
  1096  	}
  1097  	if ecr.closed.Swap(true) {
  1098  		return nil
  1099  	}
  1100  	return ecr.readCloser.Close()
  1101  }
  1102  
  1103  // TimeFormat is the time format to use when generating times in HTTP
  1104  // headers. It is like [time.RFC1123] but hard-codes GMT as the time
  1105  // zone. The time being formatted must be in UTC for Format to
  1106  // generate the correct format.
  1107  //
  1108  // For parsing this time format, see [ParseTime].
  1109  const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
  1110  
  1111  var errTooLarge = errors.New("http: request too large")
  1112  
  1113  // Read next request from connection.
  1114  func (c *conn) readRequest(ctx context.Context) (w *response, err error) {
  1115  	if c.hijacked() {
  1116  		return nil, ErrHijacked
  1117  	}
  1118  
  1119  	t0 := time.Now()
  1120  	var wholeReqDeadline time.Time // or zero if none
  1121  	if d := c.server.ReadTimeout; d > 0 {
  1122  		wholeReqDeadline = t0.Add(d)
  1123  	}
  1124  	if d := c.server.WriteTimeout; d > 0 {
  1125  		defer func() {
  1126  			c.rwc.SetWriteDeadline(time.Now().Add(d))
  1127  		}()
  1128  	}
  1129  
  1130  	c.r.setReadLimit(c.server.initialReadLimitSize())
  1131  	if c.lastMethod == "POST" {
  1132  		// RFC 7230 section 3 tolerance for old buggy clients.
  1133  		peek, _ := c.bufr.Peek(4) // ReadRequest will get err below
  1134  		c.bufr.Discard(numLeadingCRorLF(peek))
  1135  	}
  1136  	req, err := readRequestLimit(c.bufr, int64(c.server.maxHeaderValueCount()))
  1137  	if err != nil {
  1138  		if c.r.hitReadLimit() {
  1139  			return nil, errTooLarge
  1140  		}
  1141  		return nil, err
  1142  	}
  1143  
  1144  	if !http1ServerSupportsRequest(req) {
  1145  		return nil, statusError{StatusHTTPVersionNotSupported, "unsupported protocol version"}
  1146  	}
  1147  
  1148  	c.lastMethod = req.Method
  1149  	c.r.setInfiniteReadLimit()
  1150  
  1151  	hosts, haveHost := req.Header["Host"]
  1152  	isH2Upgrade := req.isH2Upgrade()
  1153  	if req.ProtoAtLeast(1, 1) && (!haveHost || len(hosts) == 0) && !isH2Upgrade && req.Method != "CONNECT" {
  1154  		return nil, badRequestError("missing required Host header")
  1155  	}
  1156  	if len(hosts) == 1 && !httpguts.ValidHostHeader(hosts[0]) {
  1157  		return nil, badRequestError("malformed Host header")
  1158  	}
  1159  	for k, vv := range req.Header {
  1160  		if !httpguts.ValidHeaderFieldName(k) {
  1161  			return nil, badRequestError("invalid header name")
  1162  		}
  1163  		for _, v := range vv {
  1164  			if !httpguts.ValidHeaderFieldValue(v) {
  1165  				return nil, badRequestError("invalid header value")
  1166  			}
  1167  		}
  1168  	}
  1169  	delete(req.Header, "Host")
  1170  
  1171  	ctx, cancelCtx := context.WithCancel(ctx)
  1172  	req.ctx = ctx
  1173  	req.RemoteAddr = c.remoteAddr
  1174  	req.TLS = c.tlsState
  1175  	var reqBody *body
  1176  	switch b := req.Body.(type) {
  1177  	case noBody:
  1178  	case *body:
  1179  		reqBody = b
  1180  		reqBody.doEarlyClose = true
  1181  	default:
  1182  		panic(fmt.Errorf("http: unexpected request body type %T", req.Body))
  1183  	}
  1184  
  1185  	c.rwc.SetReadDeadline(wholeReqDeadline)
  1186  
  1187  	w = &response{
  1188  		conn:          c,
  1189  		cancelCtx:     cancelCtx,
  1190  		req:           req,
  1191  		reqBody:       reqBody,
  1192  		handlerHeader: make(Header),
  1193  		contentLength: -1,
  1194  
  1195  		// We populate these ahead of time so we're not
  1196  		// reading from req.Header after their Handler starts
  1197  		// and maybe mutates it (Issue 14940)
  1198  		wants10KeepAlive: req.wantsHttp10KeepAlive(),
  1199  		wantsClose:       req.wantsClose(),
  1200  	}
  1201  	if isH2Upgrade {
  1202  		w.closeAfterReply = true
  1203  	}
  1204  	w.cw.res = w
  1205  	w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
  1206  	return w, nil
  1207  }
  1208  
  1209  // http1ServerSupportsRequest reports whether Go's HTTP/1.x server
  1210  // supports the given request.
  1211  func http1ServerSupportsRequest(req *Request) bool {
  1212  	if req.ProtoMajor == 1 {
  1213  		return true
  1214  	}
  1215  	// Accept "PRI * HTTP/2.0" upgrade requests, so Handlers can
  1216  	// wire up their own HTTP/2 upgrades.
  1217  	if req.ProtoMajor == 2 && req.ProtoMinor == 0 &&
  1218  		req.Method == "PRI" && req.RequestURI == "*" {
  1219  		return true
  1220  	}
  1221  	// Reject HTTP/0.x, and all other HTTP/2+ requests (which
  1222  	// aren't encoded in ASCII anyway).
  1223  	return false
  1224  }
  1225  
  1226  func (w *response) Header() Header {
  1227  	if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader {
  1228  		// Accessing the header between logically writing it
  1229  		// and physically writing it means we need to allocate
  1230  		// a clone to snapshot the logically written state.
  1231  		w.cw.header = w.handlerHeader.Clone()
  1232  	}
  1233  	w.calledHeader = true
  1234  	return w.handlerHeader
  1235  }
  1236  
  1237  // maxPostHandlerReadBytes is the max number of Request.Body bytes not
  1238  // consumed by a handler that the server will read from the client
  1239  // in order to keep a connection alive. If there are more bytes
  1240  // than this, the server, to be paranoid, instead sends a
  1241  // "Connection close" response.
  1242  //
  1243  // This number is approximately what a typical machine's TCP buffer
  1244  // size is anyway.  (if we have the bytes on the machine, we might as
  1245  // well read them)
  1246  const maxPostHandlerReadBytes = 256 << 10
  1247  
  1248  func checkWriteHeaderCode(code int) {
  1249  	// Issue 22880: require valid WriteHeader status codes.
  1250  	// For now we only enforce that it's three digits.
  1251  	// In the future we might block things over 599 (600 and above aren't defined
  1252  	// at https://httpwg.org/specs/rfc7231.html#status.codes).
  1253  	// But for now any three digits.
  1254  	//
  1255  	// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
  1256  	// no equivalent bogus thing we can realistically send in HTTP/2,
  1257  	// so we'll consistently panic instead and help people find their bugs
  1258  	// early. (We can't return an error from WriteHeader even if we wanted to.)
  1259  	if code < 100 || code > 999 {
  1260  		panic(fmt.Sprintf("invalid WriteHeader code %v", code))
  1261  	}
  1262  }
  1263  
  1264  // relevantCaller searches the call stack for the first function outside of net/http.
  1265  // The purpose of this function is to provide more helpful error messages.
  1266  func relevantCaller() runtime.Frame {
  1267  	pc := make([]uintptr, 16)
  1268  	n := runtime.Callers(1, pc)
  1269  	frames := runtime.CallersFrames(pc[:n])
  1270  	var frame runtime.Frame
  1271  	for {
  1272  		var more bool
  1273  		frame, more = frames.Next()
  1274  		if !strings.HasPrefix(frame.Function, "net/http.") {
  1275  			return frame
  1276  		}
  1277  		if !more {
  1278  			break
  1279  		}
  1280  	}
  1281  	return frame
  1282  }
  1283  
  1284  func (w *response) WriteHeader(code int) {
  1285  	if w.conn.hijacked() {
  1286  		caller := relevantCaller()
  1287  		w.conn.server.logf("http: response.WriteHeader on hijacked connection from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
  1288  		return
  1289  	}
  1290  	if w.wroteHeader {
  1291  		caller := relevantCaller()
  1292  		w.conn.server.logf("http: superfluous response.WriteHeader call from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
  1293  		return
  1294  	}
  1295  	checkWriteHeaderCode(code)
  1296  
  1297  	// Sending a 100 Continue or any non-1XX header disables the
  1298  	// automatically-sent 100 Continue from Request.Body.Read. If it is a final
  1299  	// response (200 or higher), we skip draining the request body, which the
  1300  	// client will never send.
  1301  	if code == 100 || code >= 200 {
  1302  		w.disableWriteContinue(code >= 200)
  1303  	}
  1304  
  1305  	// Handle informational headers.
  1306  	//
  1307  	// We shouldn't send any further headers after 101 Switching Protocols,
  1308  	// so it takes the non-informational path.
  1309  	if code >= 100 && code <= 199 && code != StatusSwitchingProtocols {
  1310  		w.writeContinueMu.Lock()
  1311  		defer w.writeContinueMu.Unlock()
  1312  		writeStatusLine(w.conn.bufw, w.req.ProtoAtLeast(1, 1), code, w.statusBuf[:])
  1313  
  1314  		// Per RFC 8297 we must not clear the current header map
  1315  		w.handlerHeader.WriteSubset(w.conn.bufw, excludedHeadersNoBody)
  1316  		w.conn.bufw.Write(crlf)
  1317  		w.conn.bufw.Flush()
  1318  
  1319  		return
  1320  	}
  1321  
  1322  	w.wroteHeader = true
  1323  	w.status = code
  1324  
  1325  	if w.calledHeader && w.cw.header == nil {
  1326  		w.cw.header = w.handlerHeader.Clone()
  1327  	}
  1328  
  1329  	if cl := w.handlerHeader.get("Content-Length"); cl != "" {
  1330  		v, err := strconv.ParseInt(cl, 10, 64)
  1331  		if err == nil && v >= 0 {
  1332  			w.contentLength = v
  1333  		} else {
  1334  			w.conn.server.logf("http: invalid Content-Length of %q", cl)
  1335  			w.handlerHeader.Del("Content-Length")
  1336  		}
  1337  	}
  1338  }
  1339  
  1340  // extraHeader is the set of headers sometimes added by chunkWriter.writeHeader.
  1341  // This type is used to avoid extra allocations from cloning and/or populating
  1342  // the response Header map and all its 1-element slices.
  1343  type extraHeader struct {
  1344  	contentType      string
  1345  	connection       string
  1346  	transferEncoding string
  1347  	date             []byte // written if not nil
  1348  	contentLength    []byte // written if not nil
  1349  }
  1350  
  1351  // Sorted the same as extraHeader.Write's loop.
  1352  var extraHeaderKeys = [][]byte{
  1353  	[]byte("Content-Type"),
  1354  	[]byte("Connection"),
  1355  	[]byte("Transfer-Encoding"),
  1356  }
  1357  
  1358  var (
  1359  	headerContentLength = []byte("Content-Length: ")
  1360  	headerDate          = []byte("Date: ")
  1361  )
  1362  
  1363  // Write writes the headers described in h to w.
  1364  //
  1365  // This method has a value receiver, despite the somewhat large size
  1366  // of h, because it prevents an allocation. The escape analysis isn't
  1367  // smart enough to realize this function doesn't mutate h.
  1368  func (h extraHeader) Write(w *bufio.Writer) {
  1369  	if h.date != nil {
  1370  		w.Write(headerDate)
  1371  		w.Write(h.date)
  1372  		w.Write(crlf)
  1373  	}
  1374  	if h.contentLength != nil {
  1375  		w.Write(headerContentLength)
  1376  		w.Write(h.contentLength)
  1377  		w.Write(crlf)
  1378  	}
  1379  	for i, v := range []string{h.contentType, h.connection, h.transferEncoding} {
  1380  		if v != "" {
  1381  			w.Write(extraHeaderKeys[i])
  1382  			w.Write(colonSpace)
  1383  			w.WriteString(v)
  1384  			w.Write(crlf)
  1385  		}
  1386  	}
  1387  }
  1388  
  1389  // writeHeader finalizes the header sent to the client and writes it
  1390  // to cw.res.conn.bufw.
  1391  //
  1392  // p is not written by writeHeader, but is the first chunk of the body
  1393  // that will be written. It is sniffed for a Content-Type if none is
  1394  // set explicitly. It's also used to set the Content-Length, if the
  1395  // total body size was small and the handler has already finished
  1396  // running.
  1397  func (cw *chunkWriter) writeHeader(p []byte) {
  1398  	if cw.wroteHeader {
  1399  		return
  1400  	}
  1401  	cw.wroteHeader = true
  1402  
  1403  	w := cw.res
  1404  	keepAlivesEnabled := w.conn.server.doKeepAlives()
  1405  
  1406  	// Consult w.conn.lastMethod instead of w.req.Method,
  1407  	// just in case a middleware layer modified w.req.
  1408  	isHEAD := w.conn.lastMethod == "HEAD"
  1409  	isCONNECT := w.conn.lastMethod == "CONNECT"
  1410  
  1411  	// header is written out to w.conn.buf below. Depending on the
  1412  	// state of the handler, we either own the map or not. If we
  1413  	// don't own it, the exclude map is created lazily for
  1414  	// WriteSubset to remove headers. The setHeader struct holds
  1415  	// headers we need to add.
  1416  	header := cw.header
  1417  	owned := header != nil
  1418  	if !owned {
  1419  		header = w.handlerHeader
  1420  	}
  1421  	var excludeHeader map[string]bool
  1422  	delHeader := func(key string) {
  1423  		if owned {
  1424  			header.Del(key)
  1425  			return
  1426  		}
  1427  		if _, ok := header[key]; !ok {
  1428  			return
  1429  		}
  1430  		if excludeHeader == nil {
  1431  			excludeHeader = make(map[string]bool)
  1432  		}
  1433  		excludeHeader[key] = true
  1434  	}
  1435  	var setHeader extraHeader
  1436  
  1437  	// Don't write out the fake "Trailer:foo" keys. See TrailerPrefix.
  1438  	trailers := false
  1439  	for k := range cw.header {
  1440  		if strings.HasPrefix(k, TrailerPrefix) {
  1441  			if excludeHeader == nil {
  1442  				excludeHeader = make(map[string]bool)
  1443  			}
  1444  			excludeHeader[k] = true
  1445  			trailers = true
  1446  		}
  1447  	}
  1448  	for _, v := range cw.header["Trailer"] {
  1449  		trailers = true
  1450  		foreachHeaderElement(v, cw.res.declareTrailer)
  1451  	}
  1452  
  1453  	te := header.get("Transfer-Encoding")
  1454  	hasTE := te != ""
  1455  
  1456  	// If the handler is done but never sent a Content-Length
  1457  	// response header and this is our first (and last) write, set
  1458  	// it, even to zero. This helps HTTP/1.0 clients keep their
  1459  	// "keep-alive" connections alive.
  1460  	// Exceptions: 304/204/1xx responses never get Content-Length, and if
  1461  	// it was a HEAD request, we don't know the difference between
  1462  	// 0 actual bytes and 0 bytes because the handler noticed it
  1463  	// was a HEAD request and chose not to write anything. So for
  1464  	// HEAD, the handler should either write the Content-Length or
  1465  	// write non-zero bytes. If it's actually 0 bytes and the
  1466  	// handler never looked at the Request.Method, we just don't
  1467  	// send a Content-Length header.
  1468  	// Further, we don't send an automatic Content-Length if they
  1469  	// set a Transfer-Encoding, because they're generally incompatible.
  1470  	if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && !header.has("Content-Length") && (!isHEAD || len(p) > 0) {
  1471  		w.contentLength = int64(len(p))
  1472  		setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
  1473  	}
  1474  
  1475  	// Check for an explicit (and valid) Content-Length header.
  1476  	hasCL := w.contentLength != -1
  1477  
  1478  	if w.wants10KeepAlive && !w.wantsClose && (isHEAD || hasCL || !bodyAllowedForStatus(w.status)) {
  1479  		_, connectionHeaderSet := header["Connection"]
  1480  		if !connectionHeaderSet {
  1481  			setHeader.connection = "keep-alive"
  1482  		}
  1483  	} else if !w.req.ProtoAtLeast(1, 1) || w.wantsClose {
  1484  		w.closeAfterReply = true
  1485  	}
  1486  
  1487  	if httpguts.HeaderValuesContainsToken(header["Connection"], "close") || !keepAlivesEnabled {
  1488  		w.closeAfterReply = true
  1489  	}
  1490  
  1491  	// If the client wanted a 100-continue but we never sent it to
  1492  	// them (or, more strictly: we never finished reading their
  1493  	// request body), don't reuse this connection.
  1494  	//
  1495  	// This behavior was first added on the theory that we don't know
  1496  	// if the next bytes on the wire are going to be the remainder of
  1497  	// the request body or the subsequent request (see issue 11549),
  1498  	// but that's not correct: If we keep using the connection,
  1499  	// the client is required to send the request body whether we
  1500  	// asked for it or not.
  1501  	//
  1502  	// We probably do want to skip reusing the connection in most cases,
  1503  	// however. If the client is offering a large request body that we
  1504  	// don't intend to use, then it's better to close the connection
  1505  	// than to read the body. For now, assume that if we're sending
  1506  	// headers, the handler is done reading the body and we should
  1507  	// drop the connection if we haven't seen EOF.
  1508  	if w.ecReader != nil && w.reqBody.bodyRemains() {
  1509  		w.closeAfterReply = true
  1510  	}
  1511  
  1512  	if isCONNECT {
  1513  		// Don't reuse a connection after a CONNECT, even if we reject it.
  1514  		w.closeAfterReply = true
  1515  	}
  1516  
  1517  	if w.requestBodyLimitHit.Load() {
  1518  		w.closeAfterReply = true
  1519  	}
  1520  
  1521  	// We do this by default because there are a number of clients that
  1522  	// send a full request before starting to read the response, and they
  1523  	// can deadlock if we start writing the response with unconsumed body
  1524  	// remaining. See Issue 15527 for some history.
  1525  	//
  1526  	// If full duplex mode has been enabled with ResponseController.EnableFullDuplex,
  1527  	// then leave the request body alone.
  1528  	//
  1529  	// We don't take this path when w.closeAfterReply is set.
  1530  	// We may not need to consume the request to get ready for the next one
  1531  	// (since we're closing the conn), but a client which sends a full request
  1532  	// before reading a response may deadlock in this case.
  1533  	// This behavior has been present since CL 5268043 (2011), however,
  1534  	// so it doesn't seem to be causing problems.
  1535  	if w.req.ContentLength != 0 && w.reqBody != nil && !w.closeAfterReply && !w.fullDuplex {
  1536  		var discard, tooBig bool
  1537  		w.reqBody.mu.Lock()
  1538  		switch {
  1539  		case w.reqBody.closed:
  1540  			if !w.reqBody.sawEOF {
  1541  				// Body was closed in handler with non-EOF error.
  1542  				w.closeAfterReply = true
  1543  			}
  1544  		case w.reqBody.unreadDataSizeLocked() >= maxPostHandlerReadBytes:
  1545  			tooBig = true
  1546  		default:
  1547  			discard = true
  1548  		}
  1549  		w.reqBody.mu.Unlock()
  1550  
  1551  		if discard {
  1552  			w.reqBody.Close()
  1553  			if !w.reqBody.consumedEntireBody() {
  1554  				w.closeAfterReply = true
  1555  			}
  1556  		}
  1557  		if tooBig {
  1558  			w.requestTooLarge()
  1559  			delHeader("Connection")
  1560  			setHeader.connection = "close"
  1561  		}
  1562  	}
  1563  
  1564  	code := w.status
  1565  	if bodyAllowedForStatus(code) {
  1566  		// If no content type, apply sniffing algorithm to body.
  1567  		_, haveType := header["Content-Type"]
  1568  
  1569  		// If the Content-Encoding was set and is non-blank,
  1570  		// we shouldn't sniff the body. See Issue 31753.
  1571  		ce := header.Get("Content-Encoding")
  1572  		hasCE := len(ce) > 0
  1573  		if !hasCE && !haveType && !hasTE && len(p) > 0 {
  1574  			setHeader.contentType = DetectContentType(p)
  1575  		}
  1576  	} else {
  1577  		for _, k := range suppressedHeaders(code) {
  1578  			delHeader(k)
  1579  		}
  1580  	}
  1581  
  1582  	if !header.has("Date") {
  1583  		setHeader.date = time.Now().UTC().AppendFormat(cw.res.dateBuf[:0], TimeFormat)
  1584  	}
  1585  
  1586  	if hasCL && hasTE && te != "identity" {
  1587  		// TODO: return an error if WriteHeader gets a return parameter
  1588  		// For now just ignore the Content-Length.
  1589  		w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d",
  1590  			te, w.contentLength)
  1591  		delHeader("Content-Length")
  1592  		hasCL = false
  1593  	}
  1594  
  1595  	isSuccessfulCONNECT := isCONNECT && code >= 200 && code < 300
  1596  	if isSuccessfulCONNECT {
  1597  		// Tunnel established, connection is no longer HTTP.
  1598  		delHeader("Transfer-Encoding")
  1599  		delHeader("Content-Length")
  1600  		setHeader.contentLength = nil
  1601  	} else if isHEAD || !bodyAllowedForStatus(code) || code == StatusNoContent {
  1602  		// Response has no body.
  1603  		delHeader("Transfer-Encoding")
  1604  	} else if hasCL {
  1605  		// Content-Length has been provided, so no chunking is to be done.
  1606  		delHeader("Transfer-Encoding")
  1607  	} else if w.req.ProtoAtLeast(1, 1) {
  1608  		// HTTP/1.1 or greater: Transfer-Encoding has been set to identity, and no
  1609  		// content-length has been provided. The connection must be closed after the
  1610  		// reply is written, and no chunking is to be done. This is the setup
  1611  		// recommended in the Server-Sent Events candidate recommendation 11,
  1612  		// section 8.
  1613  		if hasTE && te == "identity" {
  1614  			cw.chunking = false
  1615  			w.closeAfterReply = true
  1616  			delHeader("Transfer-Encoding")
  1617  		} else {
  1618  			// HTTP/1.1 or greater: use chunked transfer encoding
  1619  			// to avoid closing the connection at EOF.
  1620  			cw.chunking = true
  1621  			setHeader.transferEncoding = "chunked"
  1622  			if hasTE && te == "chunked" {
  1623  				// We will send the chunked Transfer-Encoding header later.
  1624  				delHeader("Transfer-Encoding")
  1625  			}
  1626  		}
  1627  	} else {
  1628  		// HTTP version < 1.1: cannot do chunked transfer
  1629  		// encoding and we don't know the Content-Length so
  1630  		// signal EOF by closing connection.
  1631  		w.closeAfterReply = true
  1632  		delHeader("Transfer-Encoding") // in case already set
  1633  	}
  1634  
  1635  	// Cannot use Content-Length with non-identity Transfer-Encoding.
  1636  	if cw.chunking {
  1637  		delHeader("Content-Length")
  1638  	}
  1639  	if !w.req.ProtoAtLeast(1, 0) {
  1640  		return
  1641  	}
  1642  
  1643  	if code == StatusSwitchingProtocols {
  1644  		// Don't continue using a connection after sending 101 Switching Protocols.
  1645  		w.closeAfterReply = true
  1646  	}
  1647  
  1648  	if w.requestBodyLimitHit.Load() {
  1649  		w.closeAfterReply = true
  1650  	}
  1651  
  1652  	// Only override the Connection header if it is not a successful
  1653  	// protocol switch response and if KeepAlives are not enabled.
  1654  	// See https://golang.org/issue/36381.
  1655  	delConnectionHeader := w.closeAfterReply &&
  1656  		(!keepAlivesEnabled || !httpguts.HeaderValuesContainsToken(cw.header["Connection"], "close")) &&
  1657  		!isProtocolSwitchResponse(w.status, header)
  1658  	if delConnectionHeader {
  1659  		delHeader("Connection")
  1660  		// Don't set Connection: close on a 2xx CONNECT response,
  1661  		// even though we will close the connection if the handler doesn't hijack it.
  1662  		// If the handler does hijack the connection, the Connection: close is confusing.
  1663  		if w.req.ProtoAtLeast(1, 1) && !isSuccessfulCONNECT {
  1664  			setHeader.connection = "close"
  1665  		} else {
  1666  			setHeader.connection = ""
  1667  		}
  1668  	}
  1669  
  1670  	writeStatusLine(w.conn.bufw, w.req.ProtoAtLeast(1, 1), code, w.statusBuf[:])
  1671  	cw.header.WriteSubset(w.conn.bufw, excludeHeader)
  1672  	setHeader.Write(w.conn.bufw)
  1673  	w.conn.bufw.Write(crlf)
  1674  }
  1675  
  1676  // foreachHeaderElement splits v according to the "#rule" construction
  1677  // in RFC 7230 section 7 and calls fn for each non-empty element.
  1678  func foreachHeaderElement(v string, fn func(string)) {
  1679  	v = textproto.TrimString(v)
  1680  	if v == "" {
  1681  		return
  1682  	}
  1683  	if !strings.Contains(v, ",") {
  1684  		fn(v)
  1685  		return
  1686  	}
  1687  	for f := range strings.SplitSeq(v, ",") {
  1688  		if f = textproto.TrimString(f); f != "" {
  1689  			fn(f)
  1690  		}
  1691  	}
  1692  }
  1693  
  1694  // writeStatusLine writes an HTTP/1.x Status-Line (RFC 7230 Section 3.1.2)
  1695  // to bw. is11 is whether the HTTP request is HTTP/1.1. false means HTTP/1.0.
  1696  // code is the response status code.
  1697  // scratch is an optional scratch buffer. If it has at least capacity 3, it's used.
  1698  func writeStatusLine(bw *bufio.Writer, is11 bool, code int, scratch []byte) {
  1699  	if is11 {
  1700  		bw.WriteString("HTTP/1.1 ")
  1701  	} else {
  1702  		bw.WriteString("HTTP/1.0 ")
  1703  	}
  1704  	if text := StatusText(code); text != "" {
  1705  		bw.Write(strconv.AppendInt(scratch[:0], int64(code), 10))
  1706  		bw.WriteByte(' ')
  1707  		bw.WriteString(text)
  1708  		bw.WriteString("\r\n")
  1709  	} else {
  1710  		// don't worry about performance
  1711  		fmt.Fprintf(bw, "%03d status code %d\r\n", code, code)
  1712  	}
  1713  }
  1714  
  1715  // bodyAllowed reports whether a Write is allowed for this response type.
  1716  // It's illegal to call this before the header has been flushed.
  1717  func (w *response) bodyAllowed() bool {
  1718  	if !w.wroteHeader {
  1719  		panic("net/http: bodyAllowed called before the header was written")
  1720  	}
  1721  	return bodyAllowedForStatus(w.status)
  1722  }
  1723  
  1724  // The Life Of A Write is like this:
  1725  //
  1726  // Handler starts. No header has been sent. The handler can either
  1727  // write a header, or just start writing. Writing before sending a header
  1728  // sends an implicitly empty 200 OK header.
  1729  //
  1730  // If the handler didn't declare a Content-Length up front, we either
  1731  // go into chunking mode or, if the handler finishes running before
  1732  // the chunking buffer size, we compute a Content-Length and send that
  1733  // in the header instead.
  1734  //
  1735  // Likewise, if the handler didn't set a Content-Type, we sniff that
  1736  // from the initial chunk of output.
  1737  //
  1738  // The Writers are wired together like:
  1739  //
  1740  //  1. *response (the ResponseWriter) ->
  1741  //  2. (*response).w, a [*bufio.Writer] of bufferBeforeChunkingSize bytes ->
  1742  //  3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type)
  1743  //     and which writes the chunk headers, if needed ->
  1744  //  4. conn.bufw, a *bufio.Writer of default (4kB) bytes, writing to ->
  1745  //  5. checkConnErrorWriter{c}, which notes any non-nil error on Write
  1746  //     and populates c.werr with it if so, but otherwise writes to ->
  1747  //  6. the rwc, the [net.Conn].
  1748  //
  1749  // TODO(bradfitz): short-circuit some of the buffering when the
  1750  // initial header contains both a Content-Type and Content-Length.
  1751  // Also short-circuit in (1) when the header's been sent and not in
  1752  // chunking mode, writing directly to (4) instead, if (2) has no
  1753  // buffered data. More generally, we could short-circuit from (1) to
  1754  // (3) even in chunking mode if the write size from (1) is over some
  1755  // threshold and nothing is in (2).  The answer might be mostly making
  1756  // bufferBeforeChunkingSize smaller and having bufio's fast-paths deal
  1757  // with this instead.
  1758  func (w *response) Write(data []byte) (n int, err error) {
  1759  	return w.write(len(data), data, "")
  1760  }
  1761  
  1762  func (w *response) WriteString(data string) (n int, err error) {
  1763  	return w.write(len(data), nil, data)
  1764  }
  1765  
  1766  // either dataB or dataS is non-zero.
  1767  func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) {
  1768  	if w.conn.hijacked() {
  1769  		if lenData > 0 {
  1770  			caller := relevantCaller()
  1771  			w.conn.server.logf("http: response.Write on hijacked connection from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
  1772  		}
  1773  		return 0, ErrHijacked
  1774  	}
  1775  
  1776  	if w.canWriteContinue.Load() {
  1777  		// Body reader wants to write 100 Continue but hasn't yet. Tell it not to.
  1778  		w.disableWriteContinue(true)
  1779  	}
  1780  
  1781  	if !w.wroteHeader {
  1782  		w.WriteHeader(StatusOK)
  1783  	}
  1784  	if lenData == 0 {
  1785  		return 0, nil
  1786  	}
  1787  	if !w.bodyAllowed() {
  1788  		return 0, ErrBodyNotAllowed
  1789  	}
  1790  
  1791  	w.written += int64(lenData) // ignoring errors, for errorKludge
  1792  	if w.contentLength != -1 && w.written > w.contentLength {
  1793  		return 0, ErrContentLength
  1794  	}
  1795  	if dataB != nil {
  1796  		return w.w.Write(dataB)
  1797  	} else {
  1798  		return w.w.WriteString(dataS)
  1799  	}
  1800  }
  1801  
  1802  func (w *response) finishRequest() {
  1803  	w.handlerDone.Store(true)
  1804  
  1805  	if !w.wroteHeader {
  1806  		w.WriteHeader(StatusOK)
  1807  	}
  1808  
  1809  	w.w.Flush()
  1810  	putBufioWriter(w.w)
  1811  	w.cw.close()
  1812  	w.conn.bufw.Flush()
  1813  
  1814  	w.conn.r.abortPendingRead()
  1815  	w.reqBody.registerOnHitEOF(nil) // prevent new background read from starting
  1816  
  1817  	if w.canWriteContinue.Load() {
  1818  		w.disableWriteContinue(true)
  1819  	}
  1820  
  1821  	// Close the body (regardless of w.closeAfterReply) so we can
  1822  	// re-use its bufio.Reader later safely.
  1823  	//
  1824  	// In full-duplex mode, this may also drain the remaining request body.
  1825  	w.reqBody.Close()
  1826  }
  1827  
  1828  // shouldReuseConnection reports whether the underlying TCP connection can be reused.
  1829  // It must only be called after the handler is done executing.
  1830  func (w *response) shouldReuseConnection() bool {
  1831  	if w.closeAfterReply || w.requestBodyLimitHit.Load() {
  1832  		// The request or something set while executing the
  1833  		// handler indicated we shouldn't reuse this
  1834  		// connection.
  1835  		return false
  1836  	}
  1837  
  1838  	if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written {
  1839  		// Did not write enough. Avoid getting out of sync.
  1840  		return false
  1841  	}
  1842  
  1843  	// There was some error writing to the underlying connection
  1844  	// during the request, so don't re-use this conn.
  1845  	if w.conn.werr != nil {
  1846  		return false
  1847  	}
  1848  
  1849  	// We haven't read the entire request body, so we can't reuse the connection.
  1850  	if !w.reqBody.consumedEntireBody() {
  1851  		return false
  1852  	}
  1853  
  1854  	return true
  1855  }
  1856  
  1857  func (w *response) Flush() {
  1858  	w.FlushError()
  1859  }
  1860  
  1861  func (w *response) FlushError() error {
  1862  	if !w.wroteHeader {
  1863  		w.WriteHeader(StatusOK)
  1864  	}
  1865  	err := w.w.Flush()
  1866  	e2 := w.cw.flush()
  1867  	if err == nil {
  1868  		err = e2
  1869  	}
  1870  	return err
  1871  }
  1872  
  1873  func (c *conn) finalFlush() {
  1874  	if c.bufr != nil {
  1875  		// Steal the bufio.Reader (~4KB worth of memory) and its associated
  1876  		// reader for a future connection.
  1877  		putBufioReader(c.bufr)
  1878  		c.bufr = nil
  1879  	}
  1880  
  1881  	if c.bufw != nil {
  1882  		c.bufw.Flush()
  1883  		// Steal the bufio.Writer (~4KB worth of memory) and its associated
  1884  		// writer for a future connection.
  1885  		putBufioWriter(c.bufw)
  1886  		c.bufw = nil
  1887  	}
  1888  }
  1889  
  1890  // Close the connection.
  1891  func (c *conn) close() {
  1892  	c.finalFlush()
  1893  	c.rwc.Close()
  1894  }
  1895  
  1896  // rstAvoidanceDelay is the amount of time we sleep after closing the
  1897  // write side of a TCP connection before closing the entire socket.
  1898  // By sleeping, we increase the chances that the client sees our FIN
  1899  // and processes its final data before they process the subsequent RST
  1900  // from closing a connection with known unread data.
  1901  // This RST seems to occur mostly on BSD systems. (And Windows?)
  1902  // This timeout is somewhat arbitrary (~latency around the planet),
  1903  // and may be modified by tests.
  1904  //
  1905  // TODO(bcmills): This should arguably be a server configuration parameter,
  1906  // not a hard-coded value.
  1907  var rstAvoidanceDelay = 500 * time.Millisecond
  1908  
  1909  type closeWriter interface {
  1910  	CloseWrite() error
  1911  }
  1912  
  1913  var _ closeWriter = (*net.TCPConn)(nil)
  1914  
  1915  // closeWriteAndWait flushes any outstanding data and sends a FIN packet (if
  1916  // client is connected via TCP), signaling that we're done. We then
  1917  // pause for a bit, hoping the client processes it before any
  1918  // subsequent RST.
  1919  //
  1920  // See https://golang.org/issue/3595
  1921  func (c *conn) closeWriteAndWait() {
  1922  	c.finalFlush()
  1923  	if tcp, ok := c.rwc.(closeWriter); ok {
  1924  		tcp.CloseWrite()
  1925  	}
  1926  
  1927  	// When we return from closeWriteAndWait, the caller will fully close the
  1928  	// connection. If client is still writing to the connection, this will cause
  1929  	// the write to fail with ECONNRESET or similar. Unfortunately, many TCP
  1930  	// implementations will also drop unread packets from the client's read buffer
  1931  	// when a write fails, causing our final response to be truncated away too.
  1932  	//
  1933  	// As a result, https://www.rfc-editor.org/rfc/rfc7230#section-6.6 recommends
  1934  	// that “[t]he server … continues to read from the connection until it
  1935  	// receives a corresponding close by the client, or until the server is
  1936  	// reasonably certain that its own TCP stack has received the client's
  1937  	// acknowledgement of the packet(s) containing the server's last response.”
  1938  	//
  1939  	// Unfortunately, we have no straightforward way to be “reasonably certain”
  1940  	// that we have received the client's ACK, and at any rate we don't want to
  1941  	// allow a misbehaving client to soak up server connections indefinitely by
  1942  	// withholding an ACK, nor do we want to go through the complexity or overhead
  1943  	// of using low-level APIs to figure out when a TCP round-trip has completed.
  1944  	//
  1945  	// Instead, we declare that we are “reasonably certain” that we received the
  1946  	// ACK if maxRSTAvoidanceDelay has elapsed.
  1947  	time.Sleep(rstAvoidanceDelay)
  1948  }
  1949  
  1950  // validNextProto reports whether the proto is a valid ALPN protocol name.
  1951  // Everything is valid except the empty string and built-in protocol types,
  1952  // so that those can't be overridden with alternate implementations.
  1953  func validNextProto(proto string) bool {
  1954  	switch proto {
  1955  	case "", "http/1.1", "http/1.0":
  1956  		return false
  1957  	}
  1958  	return true
  1959  }
  1960  
  1961  const (
  1962  	runHooks  = true
  1963  	skipHooks = false
  1964  )
  1965  
  1966  func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) {
  1967  	srv := c.server
  1968  	switch state {
  1969  	case StateNew:
  1970  		srv.trackConn(c, true)
  1971  	case StateHijacked, StateClosed:
  1972  		srv.trackConn(c, false)
  1973  	}
  1974  	if state > 0xff || state < 0 {
  1975  		panic("internal error")
  1976  	}
  1977  	packedState := uint64(time.Now().Unix()<<8) | uint64(state)
  1978  	c.curState.Store(packedState)
  1979  	if !runHook {
  1980  		return
  1981  	}
  1982  	if hook := srv.ConnState; hook != nil {
  1983  		hook(nc, state)
  1984  	}
  1985  }
  1986  
  1987  func (c *conn) getState() (state ConnState, unixSec int64) {
  1988  	packedState := c.curState.Load()
  1989  	return ConnState(packedState & 0xff), int64(packedState >> 8)
  1990  }
  1991  
  1992  // badRequestError is a literal string (used by in the server in HTML,
  1993  // unescaped) to tell the user why their request was bad. It should
  1994  // be plain text without user info or other embedded errors.
  1995  func badRequestError(e string) error { return statusError{StatusBadRequest, e} }
  1996  
  1997  // statusError is an error used to respond to a request with an HTTP status.
  1998  // The text should be plain text without user info or other embedded errors.
  1999  type statusError struct {
  2000  	code int
  2001  	text string
  2002  }
  2003  
  2004  func (e statusError) Error() string { return StatusText(e.code) + ": " + e.text }
  2005  
  2006  // ErrAbortHandler is a sentinel panic value to abort a handler.
  2007  // While any panic from ServeHTTP aborts the response to the client,
  2008  // panicking with ErrAbortHandler also suppresses logging of a stack
  2009  // trace to the server's error log.
  2010  var ErrAbortHandler = internal.ErrAbortHandler
  2011  
  2012  // isCommonNetReadError reports whether err is a common error
  2013  // encountered during reading a request off the network when the
  2014  // client has gone away or had its read fail somehow. This is used to
  2015  // determine which logs are interesting enough to log about.
  2016  func isCommonNetReadError(err error) bool {
  2017  	if err == io.EOF {
  2018  		return true
  2019  	}
  2020  	if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
  2021  		return true
  2022  	}
  2023  	if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
  2024  		return true
  2025  	}
  2026  	return false
  2027  }
  2028  
  2029  // Serve a new connection.
  2030  func (c *conn) serve(ctx context.Context) {
  2031  	if ra := c.rwc.RemoteAddr(); ra != nil {
  2032  		c.remoteAddr = ra.String()
  2033  	}
  2034  	ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
  2035  	var inFlightResponse *response
  2036  	defer func() {
  2037  		if inFlightResponse != nil {
  2038  			inFlightResponse.cancelCtx()
  2039  			inFlightResponse.disableWriteContinue(true)
  2040  		}
  2041  		if !c.hijacked() && !c.http2HandedOff {
  2042  			if inFlightResponse != nil {
  2043  				inFlightResponse.conn.r.abortPendingRead()
  2044  				inFlightResponse.reqBody.Close()
  2045  			}
  2046  			c.close()
  2047  			c.setState(c.rwc, StateClosed, runHooks)
  2048  		}
  2049  	}()
  2050  
  2051  	type connectionStater interface {
  2052  		ConnectionState() tls.ConnectionState
  2053  	}
  2054  	type handshakeContexter interface {
  2055  		HandshakeContext(ctx context.Context) error
  2056  	}
  2057  	if connStater, ok := c.rwc.(connectionStater); ok {
  2058  		tlsTO := c.server.tlsHandshakeTimeout()
  2059  		if tlsTO > 0 {
  2060  			dl := time.Now().Add(tlsTO)
  2061  			c.rwc.SetReadDeadline(dl)
  2062  			c.rwc.SetWriteDeadline(dl)
  2063  		}
  2064  		var err error
  2065  		if handshaker, ok := c.rwc.(handshakeContexter); ok {
  2066  			err = handshaker.HandshakeContext(ctx)
  2067  		}
  2068  		if err != nil {
  2069  			// If the handshake failed due to the client not speaking
  2070  			// TLS, assume they're speaking plaintext HTTP and write a
  2071  			// 400 response on the TLS conn's underlying net.Conn.
  2072  			var reason string
  2073  			if re, ok := err.(tls.RecordHeaderError); ok && re.Conn != nil && tlsRecordHeaderLooksLikeHTTP(re.RecordHeader) {
  2074  				io.WriteString(re.Conn, "HTTP/1.0 400 Bad Request\r\n\r\nClient sent an HTTP request to an HTTPS server.\n")
  2075  				re.Conn.Close()
  2076  				reason = "client sent an HTTP request to an HTTPS server"
  2077  			} else {
  2078  				reason = err.Error()
  2079  			}
  2080  			c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), reason)
  2081  			return
  2082  		}
  2083  		// Restore Conn-level deadlines.
  2084  		if tlsTO > 0 {
  2085  			c.rwc.SetReadDeadline(time.Time{})
  2086  			c.rwc.SetWriteDeadline(time.Time{})
  2087  		}
  2088  		c.tlsState = new(tls.ConnectionState)
  2089  		*c.tlsState = connStater.ConnectionState()
  2090  		proto := c.tlsState.NegotiatedProtocol
  2091  		if proto == "h2" && c.server.h2 != nil {
  2092  			// net/http/internal/http2 path.
  2093  			//
  2094  			// Mark freshly created HTTP/2 as active and prevent any server state hooks
  2095  			// from being run on these connections. This prevents closeIdleConns from
  2096  			// closing such connections. See issue https://golang.org/issue/39776.
  2097  			c.setState(c.rwc, StateActive, skipHooks)
  2098  			const sawClientPreface = false
  2099  			c.serveHTTP2(ctx, sawClientPreface)
  2100  			return
  2101  		}
  2102  		tlsConn, tlsConnOK := c.rwc.(*tls.Conn)
  2103  		if validNextProto(proto) && tlsConnOK {
  2104  			// Legacy TLSNextProto path.
  2105  			if fn := c.server.TLSNextProto[proto]; fn != nil {
  2106  				h := initALPNRequest{ctx, tlsConn, c.server}
  2107  				// Mark freshly created HTTP/2 as active (see above).
  2108  				c.setState(c.rwc, StateActive, skipHooks)
  2109  				fn(c.server, tlsConn, h)
  2110  			}
  2111  			return
  2112  		}
  2113  	}
  2114  
  2115  	// HTTP/1.x or unencrypted HTTP/2.
  2116  
  2117  	// Set Request.TLS if the conn is not a *tls.Conn, but implements ConnectionState.
  2118  	if c.tlsState == nil {
  2119  		if tc, ok := c.rwc.(connectionStater); ok {
  2120  			c.tlsState = new(tls.ConnectionState)
  2121  			*c.tlsState = tc.ConnectionState()
  2122  		}
  2123  	}
  2124  
  2125  	// HTTP/2 may outlive this goroutine, so it gets the uncancelable ctx.
  2126  	connCtx := ctx
  2127  
  2128  	ctx, cancelCtx := context.WithCancel(ctx)
  2129  	c.cancelCtx = cancelCtx
  2130  	defer cancelCtx()
  2131  
  2132  	c.r = &connReader{conn: c, rwc: c.rwc}
  2133  	c.bufr = newBufioReader(c.r)
  2134  
  2135  	if d := c.server.readHeaderTimeout(); d > 0 {
  2136  		c.rwc.SetReadDeadline(time.Now().Add(d))
  2137  	}
  2138  
  2139  	protos := c.server.protocols()
  2140  	if c.tlsState == nil && protos.UnencryptedHTTP2() {
  2141  		if c.maybeServeUnencryptedHTTP2(connCtx) {
  2142  			return
  2143  		}
  2144  	}
  2145  	if !protos.HTTP1() {
  2146  		return
  2147  	}
  2148  
  2149  	// HTTP/1.x from here on.
  2150  
  2151  	c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10)
  2152  
  2153  	for {
  2154  		w, err := c.readRequest(ctx)
  2155  		if c.r.remain != c.server.initialReadLimitSize() {
  2156  			// If we read any bytes off the wire, we're active.
  2157  			c.setState(c.rwc, StateActive, runHooks)
  2158  		}
  2159  		if c.server.shuttingDown() {
  2160  			return
  2161  		}
  2162  		if err != nil {
  2163  			const errorHeaders = "\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: close\r\n\r\n"
  2164  
  2165  			switch {
  2166  			case err == errTooLarge:
  2167  				// Their HTTP client may or may not be
  2168  				// able to read this if we're
  2169  				// responding to them and hanging up
  2170  				// while they're still writing their
  2171  				// request. Undefined behavior.
  2172  				const publicErr = "431 Request Header Fields Too Large"
  2173  				fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
  2174  				c.closeWriteAndWait()
  2175  				return
  2176  
  2177  			case isUnsupportedTEError(err):
  2178  				// Respond as per RFC 7230 Section 3.3.1 which says,
  2179  				//      A server that receives a request message with a
  2180  				//      transfer coding it does not understand SHOULD
  2181  				//      respond with 501 (Unimplemented).
  2182  				code := StatusNotImplemented
  2183  
  2184  				// We purposefully aren't echoing back the transfer-encoding's value,
  2185  				// so as to mitigate the risk of cross side scripting by an attacker.
  2186  				fmt.Fprintf(c.rwc, "HTTP/1.1 %d %s%sUnsupported transfer encoding", code, StatusText(code), errorHeaders)
  2187  				return
  2188  
  2189  			case isCommonNetReadError(err):
  2190  				return // don't reply
  2191  
  2192  			default:
  2193  				if v, ok := err.(statusError); ok {
  2194  					fmt.Fprintf(c.rwc, "HTTP/1.1 %d %s: %s%s%d %s: %s", v.code, StatusText(v.code), v.text, errorHeaders, v.code, StatusText(v.code), v.text)
  2195  					return
  2196  				}
  2197  				const publicErr = "400 Bad Request"
  2198  				fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
  2199  				return
  2200  			}
  2201  		}
  2202  
  2203  		// Expect 100 Continue support
  2204  		req := w.req
  2205  		if req.expectsContinue() {
  2206  			if req.ProtoAtLeast(1, 1) && req.ContentLength != 0 {
  2207  				// Wrap the Body reader with one that replies on the connection
  2208  				w.ecReader = &expectContinueReader{readCloser: req.Body, resp: w}
  2209  				w.canWriteContinue.Store(true)
  2210  				req.Body = w.ecReader
  2211  			}
  2212  		} else if req.Header.get("Expect") != "" {
  2213  			w.sendExpectationFailed()
  2214  			return
  2215  		}
  2216  
  2217  		c.curReq.Store(w)
  2218  
  2219  		// Start background read, which detects when a client has closed its connection
  2220  		// while a request handler is still running. When the request has a body, we
  2221  		// start the background read only after the entire body has been consumed.
  2222  		if w.reqBody.bodyRemains() {
  2223  			w.reqBody.registerOnHitEOF(w.conn.r.startBackgroundRead)
  2224  		} else {
  2225  			w.conn.r.startBackgroundRead()
  2226  		}
  2227  
  2228  		// HTTP cannot have multiple simultaneous active requests.[*]
  2229  		// Until the server replies to this request, it can't read another,
  2230  		// so we might as well run the handler in this goroutine.
  2231  		// [*] Not strictly true: HTTP pipelining. We could let them all process
  2232  		// in parallel even if their responses need to be serialized.
  2233  		// But we're not going to implement HTTP pipelining because it
  2234  		// was never deployed in the wild and the answer is HTTP/2.
  2235  		inFlightResponse = w
  2236  		if err := c.server.HandleRequest(w, w.req); err != nil {
  2237  			return
  2238  		}
  2239  		inFlightResponse = nil
  2240  		w.cancelCtx()
  2241  		if c.hijacked() {
  2242  			c.r.releaseConn()
  2243  			return
  2244  		}
  2245  		w.finishRequest()
  2246  		c.rwc.SetWriteDeadline(time.Time{})
  2247  		if !w.shouldReuseConnection() {
  2248  			// On some platforms, closing a socket with data in the read buffer
  2249  			// sends a RST. If we do this with data sent by us in flight, the client
  2250  			// might read the RST before reading what we sent. So if we might still
  2251  			// have bytes in our read buffer, CloseWrite the connection (to send a FIN)
  2252  			// and wait a short while before closing it entirely.
  2253  			if w.requestBodyLimitHit.Load() || !w.reqBody.consumedEntireBody() {
  2254  				c.closeWriteAndWait()
  2255  			}
  2256  			return
  2257  		}
  2258  		c.setState(c.rwc, StateIdle, runHooks)
  2259  		c.curReq.Store(nil)
  2260  
  2261  		if !w.conn.server.doKeepAlives() {
  2262  			// We're in shutdown mode. We might've replied
  2263  			// to the user without "Connection: close" and
  2264  			// they might think they can send another
  2265  			// request, but such is life with HTTP/1.1.
  2266  			return
  2267  		}
  2268  
  2269  		var idleDeadline time.Time
  2270  		if d := c.server.idleTimeout(); d > 0 {
  2271  			idleDeadline = time.Now().Add(d)
  2272  		}
  2273  
  2274  		// Wait for the connection to become readable again before trying to
  2275  		// read the next request. This prevents a ReadHeaderTimeout or
  2276  		// ReadTimeout from starting until the first bytes of the next request
  2277  		// have been received.
  2278  		//
  2279  		// The wait runs in two phases. First wait briefly with the
  2280  		// connection's bufio buffers still attached: on a busy
  2281  		// connection the next request typically arrives almost
  2282  		// immediately, and this keeps the buffer release below off the
  2283  		// hot path. If the connection then still has nothing buffered,
  2284  		// it has gone idle, possibly for a long time, so release its
  2285  		// bufio.Reader and Writer (~8 kB of per-connection memory
  2286  		// holding no data) to their pools for the rest of the wait.
  2287  		// The byte read by waitReadable is stashed in the connReader
  2288  		// and yielded by its next Read after fresh buffers are
  2289  		// acquired.
  2290  		shortDeadline := time.Now().Add(idleBufsReleaseDelay)
  2291  		if !idleDeadline.IsZero() && idleDeadline.Before(shortDeadline) {
  2292  			shortDeadline = idleDeadline
  2293  		}
  2294  		c.rwc.SetReadDeadline(shortDeadline)
  2295  		c.r.setProbing(true)
  2296  		_, peekErr := c.bufr.Peek(4)
  2297  		c.r.setProbing(false)
  2298  		if isNetTimeoutError(peekErr) && (idleDeadline.IsZero() || time.Now().Before(idleDeadline)) {
  2299  			c.rwc.SetReadDeadline(idleDeadline)
  2300  			if c.bufr.Buffered() == 0 && c.bufw.Buffered() == 0 {
  2301  				putBufioReader(c.bufr)
  2302  				c.bufr = nil
  2303  				putBufioWriter(c.bufw)
  2304  				c.bufw = nil
  2305  				if !c.r.waitReadable() {
  2306  					return
  2307  				}
  2308  				c.bufr = newBufioReader(c.r)
  2309  				c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10)
  2310  			}
  2311  			_, peekErr = c.bufr.Peek(4)
  2312  		}
  2313  		if peekErr != nil {
  2314  			return
  2315  		}
  2316  
  2317  		if d := c.server.readHeaderTimeout(); d > 0 {
  2318  			c.rwc.SetReadDeadline(time.Now().Add(d))
  2319  		} else {
  2320  			c.rwc.SetReadDeadline(time.Time{})
  2321  		}
  2322  	}
  2323  }
  2324  
  2325  // unencryptedHTTP2Request is an HTTP handler that initializes
  2326  // certain uninitialized fields in its *Request.
  2327  //
  2328  // It's the unencrypted version of initALPNRequest.
  2329  type unencryptedHTTP2Request struct {
  2330  	ctx context.Context
  2331  	c   net.Conn
  2332  	srv *Server
  2333  }
  2334  
  2335  func (h unencryptedHTTP2Request) BaseContext() context.Context { return h.ctx }
  2336  
  2337  func (h unencryptedHTTP2Request) ServeHTTP(rw ResponseWriter, req *Request) {
  2338  	if req.Body == nil {
  2339  		req.Body = NoBody
  2340  	}
  2341  	if req.RemoteAddr == "" {
  2342  		req.RemoteAddr = h.c.RemoteAddr().String()
  2343  	}
  2344  	h.srv.serveHTTP(rw, req, h.srv.Handler)
  2345  }
  2346  
  2347  // unencryptedNetConnInTLSConn is used to pass an unencrypted net.Conn to
  2348  // functions that only accept a *tls.Conn.
  2349  type unencryptedNetConnInTLSConn struct {
  2350  	net.Conn // panic on all net.Conn methods
  2351  	conn     net.Conn
  2352  }
  2353  
  2354  func (c unencryptedNetConnInTLSConn) UnencryptedNetConn() net.Conn {
  2355  	return c.conn
  2356  }
  2357  
  2358  func unencryptedTLSConn(c net.Conn) *tls.Conn {
  2359  	return tls.Client(unencryptedNetConnInTLSConn{conn: c}, nil)
  2360  }
  2361  
  2362  // TLSNextProto key to use for unencrypted HTTP/2 connections.
  2363  // Not actually a TLS-negotiated protocol.
  2364  const nextProtoUnencryptedHTTP2 = "unencrypted_http2"
  2365  
  2366  func (c *conn) maybeServeUnencryptedHTTP2(ctx context.Context) bool {
  2367  	var nextFunc func(*Server, *tls.Conn, Handler)
  2368  	if c.server.h2 == nil {
  2369  		var ok bool
  2370  		nextFunc, ok = c.server.TLSNextProto[nextProtoUnencryptedHTTP2]
  2371  		if !ok {
  2372  			return false
  2373  		}
  2374  	}
  2375  	hasPreface := func(c *conn, preface []byte) bool {
  2376  		c.r.setReadLimit(int64(len(preface)) - int64(c.bufr.Buffered()))
  2377  		got, err := c.bufr.Peek(len(preface))
  2378  		c.r.setInfiniteReadLimit()
  2379  		return err == nil && bytes.Equal(got, preface)
  2380  	}
  2381  	if !hasPreface(c, []byte("PRI * HTTP/2.0")) {
  2382  		return false
  2383  	}
  2384  	if !hasPreface(c, []byte("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")) {
  2385  		return false
  2386  	}
  2387  	c.setState(c.rwc, StateActive, skipHooks)
  2388  	if c.server.h2 != nil {
  2389  		const sawClientPreface = true
  2390  		c.serveHTTP2(ctx, sawClientPreface)
  2391  	} else {
  2392  		c.rwc.SetReadDeadline(time.Time{})
  2393  		c.rwc.SetWriteDeadline(time.Time{})
  2394  		h := unencryptedHTTP2Request{ctx, c.rwc, c.server}
  2395  		nextFunc(c.server, unencryptedTLSConn(c.rwc), h)
  2396  	}
  2397  	return true
  2398  }
  2399  
  2400  // serveHTTP2 hands the connection off to the HTTP/2 server, which owns
  2401  // the connection from here on: it closes the connection and runs the
  2402  // final ConnState hook when it's done, possibly after this function has
  2403  // returned, since an idle HTTP/2 connection doesn't hold onto a
  2404  // goroutine.
  2405  func (c *conn) serveHTTP2(ctx context.Context, sawClientPreface bool) {
  2406  	c.http2HandedOff = true
  2407  
  2408  	// HTTP/2 only uses c.rwc, so release the bufio.Reader if we have one.
  2409  	if c.bufr != nil {
  2410  		putBufioReader(c.bufr)
  2411  		c.bufr = nil
  2412  	}
  2413  
  2414  	c.server.serveHTTP2Conn(ctx, c.rwc, c.server.Handler, sawClientPreface, nil, nil, func() {
  2415  		c.close()
  2416  		c.setState(c.rwc, StateClosed, runHooks)
  2417  	})
  2418  }
  2419  
  2420  func (w *response) sendExpectationFailed() {
  2421  	// TODO(bradfitz): let ServeHTTP handlers handle
  2422  	// requests with non-standard expectation[s]? Seems
  2423  	// theoretical at best, and doesn't fit into the
  2424  	// current ServeHTTP model anyway. We'd need to
  2425  	// make the ResponseWriter an optional
  2426  	// "ExpectReplier" interface or something.
  2427  	//
  2428  	// For now we'll just obey RFC 7231 5.1.1 which says
  2429  	// "A server that receives an Expect field-value other
  2430  	// than 100-continue MAY respond with a 417 (Expectation
  2431  	// Failed) status code to indicate that the unexpected
  2432  	// expectation cannot be met."
  2433  	w.Header().Set("Connection", "close")
  2434  	w.WriteHeader(StatusExpectationFailed)
  2435  	w.finishRequest()
  2436  }
  2437  
  2438  // Hijack implements the [Hijacker.Hijack] method. Our response is both a [ResponseWriter]
  2439  // and a [Hijacker].
  2440  func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
  2441  	if w.handlerDone.Load() {
  2442  		panic("net/http: Hijack called after ServeHTTP finished")
  2443  	}
  2444  	w.disableWriteContinue(false)
  2445  	if w.wroteHeader {
  2446  		w.cw.flush()
  2447  	}
  2448  
  2449  	c := w.conn
  2450  	c.mu.Lock()
  2451  	defer c.mu.Unlock()
  2452  
  2453  	// Release the bufioWriter that writes to the chunk writer, it is not
  2454  	// used after a connection has been hijacked.
  2455  	rwc, buf, err = c.hijackLocked()
  2456  	if err == nil {
  2457  		putBufioWriter(w.w)
  2458  		w.w = nil
  2459  	}
  2460  	return rwc, buf, err
  2461  }
  2462  
  2463  func (w *response) CloseNotify() <-chan bool {
  2464  	w.lazyCloseNotifyMu.Lock()
  2465  	defer w.lazyCloseNotifyMu.Unlock()
  2466  	if w.handlerDone.Load() {
  2467  		panic("net/http: CloseNotify called after ServeHTTP finished")
  2468  	}
  2469  	if w.closeNotifyCh == nil {
  2470  		w.closeNotifyCh = make(chan bool, 1)
  2471  		if w.closeNotifyTriggered {
  2472  			w.closeNotifyCh <- true // action prior closeNotify call
  2473  		}
  2474  	}
  2475  	return w.closeNotifyCh
  2476  }
  2477  
  2478  func (w *response) closeNotify() {
  2479  	w.lazyCloseNotifyMu.Lock()
  2480  	defer w.lazyCloseNotifyMu.Unlock()
  2481  	if w.closeNotifyTriggered {
  2482  		return // already triggered
  2483  	}
  2484  	w.closeNotifyTriggered = true
  2485  	if w.closeNotifyCh != nil {
  2486  		w.closeNotifyCh <- true
  2487  	}
  2488  }
  2489  
  2490  // The HandlerFunc type is an adapter to allow the use of
  2491  // ordinary functions as HTTP handlers. If f is a function
  2492  // with the appropriate signature, HandlerFunc(f) is a
  2493  // [Handler] that calls f.
  2494  type HandlerFunc func(ResponseWriter, *Request)
  2495  
  2496  // ServeHTTP calls f(w, r).
  2497  func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  2498  	f(w, r)
  2499  }
  2500  
  2501  // Helper handlers
  2502  
  2503  // Error replies to the request with the specified error message and HTTP code.
  2504  // It does not otherwise end the request; the caller should ensure no further
  2505  // writes are done to w.
  2506  // The error message should be plain text.
  2507  //
  2508  // Error deletes the Content-Length header,
  2509  // sets Content-Type to “text/plain; charset=utf-8”,
  2510  // and sets X-Content-Type-Options to “nosniff”.
  2511  // This configures the header properly for the error message,
  2512  // in case the caller had set it up expecting a successful output.
  2513  func Error(w ResponseWriter, error string, code int) {
  2514  	h := w.Header()
  2515  
  2516  	// Delete the Content-Length header, which might be for some other content.
  2517  	// Assuming the error string fits in the writer's buffer, we'll figure
  2518  	// out the correct Content-Length for it later.
  2519  	//
  2520  	// We don't delete Content-Encoding, because some middleware sets
  2521  	// Content-Encoding: gzip and wraps the ResponseWriter to compress on-the-fly.
  2522  	// See https://go.dev/issue/66343.
  2523  	h.Del("Content-Length")
  2524  
  2525  	// There might be content type already set, but we reset it to
  2526  	// text/plain for the error message.
  2527  	h.Set("Content-Type", "text/plain; charset=utf-8")
  2528  	h.Set("X-Content-Type-Options", "nosniff")
  2529  	w.WriteHeader(code)
  2530  	fmt.Fprintln(w, error)
  2531  }
  2532  
  2533  // NotFound replies to the request with an HTTP 404 not found error.
  2534  func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) }
  2535  
  2536  // NotFoundHandler returns a simple request handler
  2537  // that replies to each request with a “404 page not found” reply.
  2538  func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
  2539  
  2540  // StripPrefix returns a handler that serves HTTP requests by removing the
  2541  // given prefix from the request URL's Path (and RawPath if set) and invoking
  2542  // the handler h. StripPrefix handles a request for a path that doesn't begin
  2543  // with prefix by replying with an HTTP 404 not found error. The prefix must
  2544  // match exactly: if the prefix in the request contains escaped characters
  2545  // the reply is also an HTTP 404 not found error.
  2546  func StripPrefix(prefix string, h Handler) Handler {
  2547  	if prefix == "" {
  2548  		return h
  2549  	}
  2550  	return HandlerFunc(func(w ResponseWriter, r *Request) {
  2551  		p := strings.TrimPrefix(r.URL.Path, prefix)
  2552  		rp := strings.TrimPrefix(r.URL.RawPath, prefix)
  2553  		if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) {
  2554  			r2 := new(Request)
  2555  			*r2 = *r
  2556  			r2.URL = new(url.URL)
  2557  			*r2.URL = *r.URL
  2558  			r2.URL.Path = p
  2559  			r2.URL.RawPath = rp
  2560  			h.ServeHTTP(w, r2)
  2561  		} else {
  2562  			NotFound(w, r)
  2563  		}
  2564  	})
  2565  }
  2566  
  2567  // Redirect replies to the request with a redirect to url,
  2568  // which may be a path relative to the request path.
  2569  // Any non-ASCII characters in url will be percent-encoded,
  2570  // but existing percent encodings will not be changed.
  2571  //
  2572  // The provided code should be in the 3xx range and is usually
  2573  // [StatusMovedPermanently], [StatusFound] or [StatusSeeOther].
  2574  //
  2575  // If the Content-Type header has not been set, [Redirect] sets it
  2576  // to "text/html; charset=utf-8" and writes a small HTML body.
  2577  // Setting the Content-Type header to any value, including nil,
  2578  // disables that behavior.
  2579  func Redirect(w ResponseWriter, r *Request, url string, code int) {
  2580  	if u, err := urlpkg.Parse(url); err == nil {
  2581  		// If url was relative, make its path absolute by
  2582  		// combining with request path.
  2583  		// The client would probably do this for us,
  2584  		// but doing it ourselves is more reliable.
  2585  		// See RFC 7231, section 7.1.2
  2586  		if u.Scheme == "" && u.Host == "" {
  2587  			oldpath := r.URL.EscapedPath()
  2588  			if oldpath == "" { // should not happen, but avoid a crash if it does
  2589  				oldpath = "/"
  2590  			}
  2591  
  2592  			// no leading http://server
  2593  			if url == "" || url[0] != '/' {
  2594  				// make relative path absolute
  2595  				olddir, _ := path.Split(oldpath)
  2596  				url = olddir + url
  2597  			}
  2598  
  2599  			var query string
  2600  			if i := strings.Index(url, "?"); i != -1 {
  2601  				url, query = url[:i], url[i:]
  2602  			}
  2603  
  2604  			// clean up but preserve trailing slash
  2605  			trailing := strings.HasSuffix(url, "/")
  2606  			url = path.Clean(url)
  2607  			if trailing && !strings.HasSuffix(url, "/") {
  2608  				url += "/"
  2609  			}
  2610  			url += query
  2611  		}
  2612  	}
  2613  
  2614  	h := w.Header()
  2615  
  2616  	// RFC 7231 notes that a short HTML body is usually included in
  2617  	// the response because older user agents may not understand 301/307.
  2618  	// Do it only if the request didn't already have a Content-Type header.
  2619  	_, hadCT := h["Content-Type"]
  2620  
  2621  	h.Set("Location", hexEscapeNonASCII(url))
  2622  	if !hadCT && (r.Method == "GET" || r.Method == "HEAD") {
  2623  		h.Set("Content-Type", "text/html; charset=utf-8")
  2624  	}
  2625  	w.WriteHeader(code)
  2626  
  2627  	// Shouldn't send the body for POST or HEAD; that leaves GET.
  2628  	if !hadCT && r.Method == "GET" {
  2629  		body := "<a href=\"" + htmlEscape(url) + "\">" + StatusText(code) + "</a>.\n"
  2630  		fmt.Fprintln(w, body)
  2631  	}
  2632  }
  2633  
  2634  var htmlReplacer = strings.NewReplacer(
  2635  	"&", "&amp;",
  2636  	"<", "&lt;",
  2637  	">", "&gt;",
  2638  	// "&#34;" is shorter than "&quot;".
  2639  	`"`, "&#34;",
  2640  	// "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
  2641  	"'", "&#39;",
  2642  )
  2643  
  2644  func htmlEscape(s string) string {
  2645  	return htmlReplacer.Replace(s)
  2646  }
  2647  
  2648  // Redirect to a fixed URL
  2649  type redirectHandler struct {
  2650  	url  string
  2651  	code int
  2652  }
  2653  
  2654  func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) {
  2655  	Redirect(w, r, rh.url, rh.code)
  2656  }
  2657  
  2658  // RedirectHandler returns a request handler that redirects
  2659  // each request it receives to the given url using the given
  2660  // status code.
  2661  //
  2662  // The provided code should be in the 3xx range and is usually
  2663  // [StatusMovedPermanently], [StatusFound] or [StatusSeeOther].
  2664  func RedirectHandler(url string, code int) Handler {
  2665  	return &redirectHandler{url, code}
  2666  }
  2667  
  2668  // ServeMux is an HTTP request multiplexer.
  2669  // It matches the URL of each incoming request against a list of registered
  2670  // patterns and calls the handler for the pattern that
  2671  // most closely matches the URL.
  2672  //
  2673  // # Patterns
  2674  //
  2675  // Patterns can match the method, host and path of a request.
  2676  // Some examples:
  2677  //
  2678  //   - "/index.html" matches the path "/index.html" for any host and method.
  2679  //   - "GET /static/" matches a GET request whose path begins with "/static/".
  2680  //   - "example.com/" matches any request to the host "example.com".
  2681  //   - "example.com/{$}" matches requests with host "example.com" and path "/".
  2682  //   - "/b/{bucket}/o/{objectname...}" matches paths whose first segment is "b"
  2683  //     and whose third segment is "o". The name "bucket" denotes the second
  2684  //     segment and "objectname" denotes the remainder of the path.
  2685  //
  2686  // In general, a pattern looks like
  2687  //
  2688  //	[METHOD ][HOST]/[PATH]
  2689  //
  2690  // All three parts are optional; "/" is a valid pattern.
  2691  // If METHOD is present, it must be followed by at least one space or tab.
  2692  //
  2693  // Literal (that is, non-wildcard) parts of a pattern match
  2694  // the corresponding parts of a request case-sensitively.
  2695  //
  2696  // A pattern with no method matches every method. A pattern
  2697  // with the method GET matches both GET and HEAD requests.
  2698  // Otherwise, the method must match exactly.
  2699  //
  2700  // A pattern with no host matches every host.
  2701  // A pattern with a host matches URLs on that host only.
  2702  //
  2703  // A path can include wildcard segments of the form {NAME} or {NAME...}.
  2704  // For example, "/b/{bucket}/o/{objectname...}".
  2705  // The wildcard name must be a valid Go identifier.
  2706  // Wildcards must be full path segments: they must be preceded by a slash and followed by
  2707  // either a slash or the end of the string.
  2708  // For example, "/b_{bucket}" is not a valid pattern.
  2709  //
  2710  // Normally a wildcard matches only a single path segment,
  2711  // ending at the next literal slash (not %2F) in the request URL.
  2712  // But if the "..." is present, then the wildcard matches the remainder of the URL path, including slashes.
  2713  // (Therefore it is invalid for a "..." wildcard to appear anywhere but at the end of a pattern.)
  2714  // The match for a wildcard can be obtained by calling [Request.PathValue] with the wildcard's name.
  2715  // A trailing slash in a path acts as an anonymous "..." wildcard.
  2716  //
  2717  // The special wildcard {$} matches only the end of the URL.
  2718  // For example, the pattern "/{$}" matches only the path "/",
  2719  // whereas the pattern "/" matches every path.
  2720  //
  2721  // For matching, both pattern paths and incoming request paths are unescaped segment by segment.
  2722  // So, for example, the path "/a%2Fb/100%25" is treated as having two segments, "a/b" and "100%".
  2723  // The pattern "/a%2fb/" matches it, but the pattern "/a/b/" does not.
  2724  //
  2725  // # Precedence
  2726  //
  2727  // If two or more patterns match a request, then the most specific pattern takes precedence.
  2728  // A pattern P1 is more specific than P2 if P1 matches a strict subset of P2’s requests;
  2729  // that is, if P2 matches all the requests of P1 and more.
  2730  // If neither is more specific, then the patterns conflict.
  2731  // There is one exception to this rule, for backwards compatibility:
  2732  // if two patterns would otherwise conflict and one has a host while the other does not,
  2733  // then the pattern with the host takes precedence.
  2734  // If a pattern passed to [ServeMux.Handle] or [ServeMux.HandleFunc] conflicts with
  2735  // another pattern that is already registered, those functions panic.
  2736  //
  2737  // As an example of the general rule, "/images/thumbnails/" is more specific than "/images/",
  2738  // so both can be registered.
  2739  // The former matches paths beginning with "/images/thumbnails/"
  2740  // and the latter will match any other path in the "/images/" subtree.
  2741  //
  2742  // As another example, consider the patterns "GET /" and "/index.html":
  2743  // both match a GET request for "/index.html", but the former pattern
  2744  // matches all other GET and HEAD requests, while the latter matches any
  2745  // request for "/index.html" that uses a different method.
  2746  // The patterns conflict.
  2747  //
  2748  // # Trailing-slash redirection
  2749  //
  2750  // Consider a [ServeMux] with a handler for a subtree, registered using a trailing slash or "..." wildcard.
  2751  // If the ServeMux receives a request for the subtree root without a trailing slash,
  2752  // it redirects the request by adding the trailing slash.
  2753  // This behavior can be overridden with a separate registration for the path without
  2754  // the trailing slash or "..." wildcard. For example, registering "/images/" causes ServeMux
  2755  // to redirect a request for "/images" to "/images/", unless "/images" has
  2756  // been registered separately.
  2757  //
  2758  // # Request sanitizing
  2759  //
  2760  // ServeMux also takes care of sanitizing the URL request path and the Host
  2761  // header, stripping the port number and redirecting any request containing . or
  2762  // .. segments or repeated slashes to an equivalent, cleaner URL.
  2763  // Escaped path elements such as "%2e" for "." and "%2f" for "/" are preserved
  2764  // and aren't considered separators for request routing.
  2765  //
  2766  // # Compatibility
  2767  //
  2768  // The pattern syntax and matching behavior of ServeMux changed significantly
  2769  // in Go 1.22. To restore the old behavior, set the GODEBUG environment variable
  2770  // to "httpmuxgo121=1". This setting is read once, at program startup; changes
  2771  // during execution will be ignored.
  2772  //
  2773  // The backwards-incompatible changes include:
  2774  //   - Wildcards are just ordinary literal path segments in 1.21.
  2775  //     For example, the pattern "/{x}" will match only that path in 1.21,
  2776  //     but will match any one-segment path in 1.22.
  2777  //   - In 1.21, no pattern was rejected, unless it was empty or conflicted with an existing pattern.
  2778  //     In 1.22, syntactically invalid patterns will cause [ServeMux.Handle] and [ServeMux.HandleFunc] to panic.
  2779  //     For example, in 1.21, the patterns "/{"  and "/a{x}" match themselves,
  2780  //     but in 1.22 they are invalid and will cause a panic when registered.
  2781  //   - In 1.22, each segment of a pattern is unescaped; this was not done in 1.21.
  2782  //     For example, in 1.22 the pattern "/%61" matches the path "/a" ("%61" being the URL escape sequence for "a"),
  2783  //     but in 1.21 it would match only the path "/%2561" (where "%25" is the escape for the percent sign).
  2784  //   - When matching patterns to paths, in 1.22 each segment of the path is unescaped; in 1.21, the entire path is unescaped.
  2785  //     This change mostly affects how paths with %2F escapes adjacent to slashes are treated.
  2786  //     See https://go.dev/issue/21955 for details.
  2787  type ServeMux struct {
  2788  	mu     sync.RWMutex
  2789  	tree   routingNode
  2790  	index  routingIndex
  2791  	mux121 serveMux121 // used only when GODEBUG=httpmuxgo121=1
  2792  }
  2793  
  2794  // NewServeMux allocates and returns a new [ServeMux].
  2795  func NewServeMux() *ServeMux {
  2796  	return &ServeMux{}
  2797  }
  2798  
  2799  // DefaultServeMux is the default [ServeMux] used by [Serve].
  2800  var DefaultServeMux = &defaultServeMux
  2801  
  2802  var defaultServeMux ServeMux
  2803  
  2804  // cleanPath returns the canonical path for p, eliminating . and .. elements.
  2805  func cleanPath(p string) string {
  2806  	if p == "" {
  2807  		return "/"
  2808  	}
  2809  	if p[0] != '/' {
  2810  		p = "/" + p
  2811  	}
  2812  	np := path.Clean(p)
  2813  	// path.Clean removes trailing slash except for root;
  2814  	// put the trailing slash back if necessary.
  2815  	if p[len(p)-1] == '/' && np != "/" {
  2816  		// Fast path for common case of p being the string we want:
  2817  		if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
  2818  			np = p
  2819  		} else {
  2820  			np += "/"
  2821  		}
  2822  	}
  2823  	return np
  2824  }
  2825  
  2826  // stripHostPort returns h without any trailing ":<port>".
  2827  func stripHostPort(h string) string {
  2828  	// If no port on host, return unchanged
  2829  	if !strings.Contains(h, ":") {
  2830  		return h
  2831  	}
  2832  	host, _, err := net.SplitHostPort(h)
  2833  	if err != nil {
  2834  		return h // on error, return unchanged
  2835  	}
  2836  	return host
  2837  }
  2838  
  2839  // Handler returns the handler to use for the given request,
  2840  // consulting r.Method, r.Host, and r.URL.Path. It always returns
  2841  // a non-nil handler. If the path is not in its canonical form, the
  2842  // handler will be an internally-generated handler that redirects
  2843  // to the canonical path. If the host contains a port, it is ignored
  2844  // when matching handlers.
  2845  //
  2846  // The path and host are used unchanged for CONNECT requests.
  2847  //
  2848  // Handler also returns the registered pattern that matches the
  2849  // request or, in the case of internally-generated redirects,
  2850  // the path that will match after following the redirect.
  2851  //
  2852  // If there is no registered handler that applies to the request,
  2853  // Handler returns a “page not found” or “method not supported”
  2854  // handler and an empty pattern.
  2855  //
  2856  // Handler does not modify its argument. In particular, it does not
  2857  // populate named path wildcards, so r.PathValue will always return
  2858  // the empty string.
  2859  func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
  2860  	if use121 {
  2861  		return mux.mux121.findHandler(r)
  2862  	}
  2863  	h, p, _, _ := mux.findHandler(r)
  2864  	return h, p
  2865  }
  2866  
  2867  // findHandler finds a handler for a request.
  2868  // If there is a matching handler, it returns it and the pattern that matched.
  2869  // Otherwise it returns a Redirect or NotFound handler with the path that would match
  2870  // after the redirect.
  2871  func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string, _ *pattern, matches []string) {
  2872  	var n *routingNode
  2873  	host := r.URL.Host
  2874  	escapedPath := r.URL.EscapedPath()
  2875  	path := escapedPath
  2876  	// CONNECT requests are not canonicalized.
  2877  	if r.Method == "CONNECT" {
  2878  		// If r.URL.Path is /tree and its handler is not registered,
  2879  		// the /tree -> /tree/ redirect applies to CONNECT requests
  2880  		// but the path canonicalization does not.
  2881  		_, _, u := mux.matchOrRedirect(host, r.Method, path, r.URL)
  2882  		if u != nil {
  2883  			return RedirectHandler(u.String(), StatusTemporaryRedirect), u.Path, nil, nil
  2884  		}
  2885  		// Redo the match, this time with r.Host instead of r.URL.Host.
  2886  		// Pass a nil URL to skip the trailing-slash redirect logic.
  2887  		n, matches, _ = mux.matchOrRedirect(r.Host, r.Method, path, nil)
  2888  	} else {
  2889  		// All other requests have any port stripped and path cleaned
  2890  		// before passing to mux.handler.
  2891  		host = stripHostPort(r.Host)
  2892  		path = cleanPath(path)
  2893  
  2894  		// If the given path is /tree and its handler is not registered,
  2895  		// redirect for /tree/.
  2896  		var u *url.URL
  2897  		n, matches, u = mux.matchOrRedirect(host, r.Method, path, r.URL)
  2898  		if u != nil {
  2899  			return RedirectHandler(u.String(), StatusTemporaryRedirect), n.pattern.String(), nil, nil
  2900  		}
  2901  		if path != escapedPath {
  2902  			// Redirect to cleaned path.
  2903  			patStr := ""
  2904  			if n != nil {
  2905  				patStr = n.pattern.String()
  2906  			}
  2907  			u := urlFromEscaped(path, r.URL.RawQuery)
  2908  			return RedirectHandler(u.String(), StatusTemporaryRedirect), patStr, nil, nil
  2909  		}
  2910  	}
  2911  	if n == nil {
  2912  		// We didn't find a match with the request method. To distinguish between
  2913  		// Not Found and Method Not Allowed, see if there is another pattern that
  2914  		// matches except for the method.
  2915  		allowedMethods := mux.matchingMethods(host, path)
  2916  		if len(allowedMethods) > 0 {
  2917  			return HandlerFunc(func(w ResponseWriter, r *Request) {
  2918  				w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
  2919  				Error(w, StatusText(StatusMethodNotAllowed), StatusMethodNotAllowed)
  2920  			}), "", nil, nil
  2921  		}
  2922  		return NotFoundHandler(), "", nil, nil
  2923  	}
  2924  	return n.handler, n.pattern.String(), n.pattern, matches
  2925  }
  2926  
  2927  // matchOrRedirect looks up a node in the tree that matches the host, method and path.
  2928  //
  2929  // If the url argument is non-nil, handler also deals with trailing-slash
  2930  // redirection: when a path doesn't match exactly, the match is tried again
  2931  // after appending "/" to the path. If that second match succeeds, the last
  2932  // return value is the URL to redirect to.
  2933  func (mux *ServeMux) matchOrRedirect(host, method, path string, u *url.URL) (_ *routingNode, matches []string, redirectTo *url.URL) {
  2934  	mux.mu.RLock()
  2935  	defer mux.mu.RUnlock()
  2936  
  2937  	n, matches := mux.tree.match(host, method, path)
  2938  	// We can terminate here if any of the following is true:
  2939  	// - We have an exact match already.
  2940  	// - We were asked not to try trailing slash redirection.
  2941  	// - The URL already has a trailing slash.
  2942  	// - The URL is an empty string.
  2943  	if !exactMatch(n, path) && u != nil && !strings.HasSuffix(path, "/") && path != "" {
  2944  		// If there is an exact match with a trailing slash, then redirect.
  2945  		path += "/"
  2946  		n2, _ := mux.tree.match(host, method, path)
  2947  		if exactMatch(n2, path) {
  2948  			// It is safe to return n2 here: it is used only in the second RedirectHandler case
  2949  			// of findHandler, and that method returns before it does the "n == nil" check where
  2950  			// the first return value matters. We return it here only to make the pattern available
  2951  			// to findHandler.
  2952  			return n2, nil, urlFromEscaped(path, u.RawQuery)
  2953  		}
  2954  	}
  2955  	return n, matches, nil
  2956  }
  2957  
  2958  // urlFromEscaped returns a url.URL constructed from an escaped path and a raw
  2959  // query.
  2960  //
  2961  // It ensures that the Path and RawPath fields are in sync by unescaping the
  2962  // escaped path. Populating only the Path field and leaving RawPath empty (or
  2963  // failing to keep them in sync) can cause url.URL.String to produce a URL with
  2964  // either unexpected escaping (e.g., double-escaping "%" into "%25" in an
  2965  // already escaped path) or a lack thereof (e.g., losing the escaping of "%2f"
  2966  // and turning it into a literal path separator "/").
  2967  func urlFromEscaped(escaped, rawQuery string) *url.URL {
  2968  	unescaped, err := url.PathUnescape(escaped)
  2969  	// Should be impossible, since ServeMux will reject unparsable URLs way
  2970  	// earlier.
  2971  	if err != nil {
  2972  		unescaped = escaped
  2973  	}
  2974  	return &url.URL{
  2975  		Path:     unescaped,
  2976  		RawPath:  escaped,
  2977  		RawQuery: rawQuery,
  2978  	}
  2979  }
  2980  
  2981  // exactMatch reports whether the node's pattern exactly matches the path.
  2982  // As a special case, if the node is nil, exactMatch return false.
  2983  //
  2984  // Before wildcards were introduced, it was clear that an exact match meant
  2985  // that the pattern and path were the same string. The only other possibility
  2986  // was that a trailing-slash pattern, like "/", matched a path longer than
  2987  // it, like "/a".
  2988  //
  2989  // With wildcards, we define an inexact match as any one where a multi wildcard
  2990  // matches a non-empty string. All other matches are exact.
  2991  // For example, these are all exact matches:
  2992  //
  2993  //	pattern   path
  2994  //	/a        /a
  2995  //	/{x}      /a
  2996  //	/a/{$}    /a/
  2997  //	/a/       /a/
  2998  //
  2999  // The last case has a multi wildcard (implicitly), but the match is exact because
  3000  // the wildcard matches the empty string.
  3001  //
  3002  // Examples of matches that are not exact:
  3003  //
  3004  //	pattern   path
  3005  //	/         /a
  3006  //	/a/{x...} /a/b
  3007  func exactMatch(n *routingNode, path string) bool {
  3008  	if n == nil {
  3009  		return false
  3010  	}
  3011  	// We can't directly implement the definition (empty match for multi
  3012  	// wildcard) because we don't record a match for anonymous multis.
  3013  
  3014  	// If there is no multi, the match is exact.
  3015  	if !n.pattern.lastSegment().multi {
  3016  		return true
  3017  	}
  3018  
  3019  	// If the path doesn't end in a trailing slash, then the multi match
  3020  	// is non-empty.
  3021  	if len(path) > 0 && path[len(path)-1] != '/' {
  3022  		return false
  3023  	}
  3024  	// Only patterns ending in {$} or a multi wildcard can
  3025  	// match a path with a trailing slash.
  3026  	// For the match to be exact, the number of pattern
  3027  	// segments should be the same as the number of slashes in the path.
  3028  	// E.g. "/a/b/{$}" and "/a/b/{...}" exactly match "/a/b/", but "/a/" does not.
  3029  	return len(n.pattern.segments) == strings.Count(path, "/")
  3030  }
  3031  
  3032  // matchingMethods return a sorted list of all methods that would match with the given host and path.
  3033  func (mux *ServeMux) matchingMethods(host, path string) []string {
  3034  	// Hold the read lock for the entire method so that the two matches are done
  3035  	// on the same set of registered patterns.
  3036  	mux.mu.RLock()
  3037  	defer mux.mu.RUnlock()
  3038  	ms := map[string]bool{}
  3039  	mux.tree.matchingMethods(host, path, ms)
  3040  	// matchOrRedirect will try appending a trailing slash if there is no match.
  3041  	if !strings.HasSuffix(path, "/") {
  3042  		mux.tree.matchingMethods(host, path+"/", ms)
  3043  	}
  3044  	return slices.Sorted(maps.Keys(ms))
  3045  }
  3046  
  3047  // ServeHTTP dispatches the request to the handler whose
  3048  // pattern most closely matches the request URL.
  3049  func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
  3050  	if r.RequestURI == "*" {
  3051  		if r.ProtoAtLeast(1, 1) {
  3052  			w.Header().Set("Connection", "close")
  3053  		}
  3054  		w.WriteHeader(StatusBadRequest)
  3055  		return
  3056  	}
  3057  	var h Handler
  3058  	if use121 {
  3059  		h, _ = mux.mux121.findHandler(r)
  3060  	} else {
  3061  		h, r.Pattern, r.pat, r.matches = mux.findHandler(r)
  3062  	}
  3063  	h.ServeHTTP(w, r)
  3064  }
  3065  
  3066  // The four functions below all call ServeMux.register so that callerLocation
  3067  // always refers to user code.
  3068  
  3069  // Handle registers the handler for the given pattern.
  3070  // If the given pattern conflicts with one that is already registered
  3071  // or if the pattern is invalid, Handle panics.
  3072  //
  3073  // See [ServeMux] for details on valid patterns and conflict rules.
  3074  func (mux *ServeMux) Handle(pattern string, handler Handler) {
  3075  	if use121 {
  3076  		mux.mux121.handle(pattern, handler)
  3077  	} else {
  3078  		mux.register(pattern, handler)
  3079  	}
  3080  }
  3081  
  3082  // HandleFunc registers the handler function for the given pattern.
  3083  // If the given pattern conflicts with one that is already registered
  3084  // or if the pattern is invalid, HandleFunc panics.
  3085  //
  3086  // See [ServeMux] for details on valid patterns and conflict rules.
  3087  func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  3088  	if use121 {
  3089  		mux.mux121.handleFunc(pattern, handler)
  3090  	} else {
  3091  		mux.register(pattern, HandlerFunc(handler))
  3092  	}
  3093  }
  3094  
  3095  // Handle registers the handler for the given pattern in [DefaultServeMux].
  3096  // The documentation for [ServeMux] explains how patterns are matched.
  3097  func Handle(pattern string, handler Handler) {
  3098  	if use121 {
  3099  		DefaultServeMux.mux121.handle(pattern, handler)
  3100  	} else {
  3101  		DefaultServeMux.register(pattern, handler)
  3102  	}
  3103  }
  3104  
  3105  // HandleFunc registers the handler function for the given pattern in [DefaultServeMux].
  3106  // The documentation for [ServeMux] explains how patterns are matched.
  3107  func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
  3108  	if use121 {
  3109  		DefaultServeMux.mux121.handleFunc(pattern, handler)
  3110  	} else {
  3111  		DefaultServeMux.register(pattern, HandlerFunc(handler))
  3112  	}
  3113  }
  3114  
  3115  func (mux *ServeMux) register(pattern string, handler Handler) {
  3116  	if err := mux.registerErr(pattern, handler); err != nil {
  3117  		panic(err)
  3118  	}
  3119  }
  3120  
  3121  func (mux *ServeMux) registerErr(patstr string, handler Handler) error {
  3122  	if patstr == "" {
  3123  		return errors.New("http: invalid pattern")
  3124  	}
  3125  	if handler == nil {
  3126  		return errors.New("http: nil handler")
  3127  	}
  3128  	if f, ok := handler.(HandlerFunc); ok && f == nil {
  3129  		return errors.New("http: nil handler")
  3130  	}
  3131  
  3132  	pat, err := parsePattern(patstr)
  3133  	if err != nil {
  3134  		return fmt.Errorf("parsing %q: %w", patstr, err)
  3135  	}
  3136  
  3137  	// Get the caller's location, for better conflict error messages.
  3138  	// Skip register and whatever calls it.
  3139  	_, file, line, ok := runtime.Caller(3)
  3140  	if !ok {
  3141  		pat.loc = "unknown location"
  3142  	} else {
  3143  		pat.loc = fmt.Sprintf("%s:%d", file, line)
  3144  	}
  3145  
  3146  	mux.mu.Lock()
  3147  	defer mux.mu.Unlock()
  3148  	// Check for conflict.
  3149  	if err := mux.index.possiblyConflictingPatterns(pat, func(pat2 *pattern) error {
  3150  		if pat.conflictsWith(pat2) {
  3151  			d := describeConflict(pat, pat2)
  3152  			return fmt.Errorf("pattern %q (registered at %s) conflicts with pattern %q (registered at %s):\n%s",
  3153  				pat, pat.loc, pat2, pat2.loc, d)
  3154  		}
  3155  		return nil
  3156  	}); err != nil {
  3157  		return err
  3158  	}
  3159  	mux.tree.addPattern(pat, handler)
  3160  	mux.index.addPattern(pat)
  3161  	return nil
  3162  }
  3163  
  3164  // Serve accepts incoming HTTP connections on the listener l,
  3165  // creating a new service goroutine for each. The service goroutines
  3166  // read requests and then call handler to reply to them.
  3167  //
  3168  // The handler is typically nil, in which case [DefaultServeMux] is used.
  3169  //
  3170  // HTTP/2 support is only enabled if the Listener returns [*tls.Conn]
  3171  // connections or connections which implement the same ConnectionState
  3172  // method as *tls.Conn, and the connection state indicates that the "h2"
  3173  // protocol was negotiated by ALPN.
  3174  //
  3175  // Serve always returns a non-nil error.
  3176  func Serve(l net.Listener, handler Handler) error {
  3177  	srv := &Server{Handler: handler}
  3178  	return srv.Serve(l)
  3179  }
  3180  
  3181  // ServeTLS accepts incoming HTTPS connections on the listener l,
  3182  // creating a new service goroutine for each. The service goroutines
  3183  // read requests and then call handler to reply to them.
  3184  //
  3185  // The handler is typically nil, in which case [DefaultServeMux] is used.
  3186  //
  3187  // Additionally, files containing a certificate and matching private key
  3188  // for the server must be provided. If the certificate is signed by a
  3189  // certificate authority, the certFile should be the concatenation
  3190  // of the server's certificate, any intermediates, and the CA's certificate.
  3191  //
  3192  // ServeTLS always returns a non-nil error.
  3193  func ServeTLS(l net.Listener, handler Handler, certFile, keyFile string) error {
  3194  	srv := &Server{Handler: handler}
  3195  	return srv.ServeTLS(l, certFile, keyFile)
  3196  }
  3197  
  3198  // A Server defines parameters for running an HTTP server.
  3199  // The zero value for Server is a valid configuration.
  3200  type Server struct {
  3201  	// Addr optionally specifies the TCP address for the server to listen on,
  3202  	// in the form "host:port". If empty, ":http" (port 80) is used.
  3203  	// The service names are defined in RFC 6335 and assigned by IANA.
  3204  	// See net.Dial for details of the address format.
  3205  	Addr string
  3206  
  3207  	Handler Handler // handler to invoke, http.DefaultServeMux if nil
  3208  
  3209  	// DisableGeneralOptionsHandler, if true, passes "OPTIONS *" requests to the Handler,
  3210  	// otherwise responds with 200 OK and Content-Length: 0.
  3211  	DisableGeneralOptionsHandler bool
  3212  
  3213  	// TLSConfig optionally provides a TLS configuration for use
  3214  	// by ServeTLS and ListenAndServeTLS. Note that this value is
  3215  	// cloned by ServeTLS and ListenAndServeTLS, so it's not
  3216  	// possible to modify the configuration with methods like
  3217  	// tls.Config.SetSessionTicketKeys. To use
  3218  	// SetSessionTicketKeys, use Server.Serve with a TLS Listener
  3219  	// instead.
  3220  	TLSConfig *tls.Config
  3221  
  3222  	// ReadTimeout is the maximum duration for reading the entire
  3223  	// request, including the body. A zero or negative value means
  3224  	// there will be no timeout.
  3225  	//
  3226  	// Because ReadTimeout does not let Handlers make per-request
  3227  	// decisions on each request body's acceptable deadline or
  3228  	// upload rate, most users will prefer to use
  3229  	// ReadHeaderTimeout. It is valid to use them both.
  3230  	ReadTimeout time.Duration
  3231  
  3232  	// ReadHeaderTimeout is the amount of time allowed to read
  3233  	// request headers. The connection's read deadline is reset
  3234  	// after reading the headers and the Handler can decide what
  3235  	// is considered too slow for the body. If zero, the value of
  3236  	// ReadTimeout is used. If negative, or if zero and ReadTimeout
  3237  	// is zero or negative, there is no timeout.
  3238  	ReadHeaderTimeout time.Duration
  3239  
  3240  	// WriteTimeout is the maximum duration before timing out
  3241  	// writes of the response. It is reset whenever a new
  3242  	// request's header is read. Like ReadTimeout, it does not
  3243  	// let Handlers make decisions on a per-request basis.
  3244  	// A zero or negative value means there will be no timeout.
  3245  	WriteTimeout time.Duration
  3246  
  3247  	// IdleTimeout is the maximum amount of time to wait for the
  3248  	// next request when keep-alives are enabled. If zero, the value
  3249  	// of ReadTimeout is used. If negative, or if zero and ReadTimeout
  3250  	// is zero or negative, there is no timeout.
  3251  	IdleTimeout time.Duration
  3252  
  3253  	// MaxHeaderBytes controls the maximum number of bytes the
  3254  	// server will read parsing the request header's keys and
  3255  	// values, including the request line. It does not limit the
  3256  	// size of the request body.
  3257  	// If zero, DefaultMaxHeaderBytes is used.
  3258  	MaxHeaderBytes int
  3259  
  3260  	// MaxHeaderValueCount controls the maximum number of header
  3261  	// values that the server is willing to parse from a request.
  3262  	// If zero, DefaultMaxHeaderValueCount is used.
  3263  	// Comma-separated values in a single header line are counted
  3264  	// once, while values sent as multiple header lines are
  3265  	// counted multiple times. An exception to this is the Trailer
  3266  	// header, whose comma-separated values are counted separately,
  3267  	// as each of them is expected to be received later as an
  3268  	// individual trailer header line.
  3269  	MaxHeaderValueCount int
  3270  
  3271  	// TLSNextProto optionally specifies a function to take over
  3272  	// ownership of the provided TLS connection when an ALPN
  3273  	// protocol upgrade has occurred. The map key is the protocol
  3274  	// name negotiated. The Handler argument should be used to
  3275  	// handle HTTP requests and will initialize the Request's TLS
  3276  	// and RemoteAddr if not already set. The connection is
  3277  	// automatically closed when the function returns.
  3278  	// If TLSNextProto is not nil, HTTP/2 support is not enabled
  3279  	// automatically.
  3280  	//
  3281  	// Historically, TLSNextProto was used to disable HTTP/2 support.
  3282  	// The Server.Protocols field now provides a simpler way to do this.
  3283  	TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
  3284  
  3285  	// ConnState specifies an optional callback function that is
  3286  	// called when a client connection changes state. See the
  3287  	// ConnState type and associated constants for details.
  3288  	ConnState func(net.Conn, ConnState)
  3289  
  3290  	// ErrorLog specifies an optional logger for errors accepting
  3291  	// connections, unexpected behavior from handlers, and
  3292  	// underlying FileSystem errors.
  3293  	// If nil, logging is done via the log package's standard logger.
  3294  	ErrorLog *log.Logger
  3295  
  3296  	// BaseContext optionally specifies a function that returns
  3297  	// the base context for incoming requests on this server.
  3298  	// The provided Listener is the specific Listener that's
  3299  	// about to start accepting requests.
  3300  	// If BaseContext is nil, the default is context.Background().
  3301  	// If non-nil, it must return a non-nil context.
  3302  	BaseContext func(net.Listener) context.Context
  3303  
  3304  	// ConnContext optionally specifies a function that modifies
  3305  	// the context used for a new connection c. The provided ctx
  3306  	// is derived from the base context and has a ServerContextKey
  3307  	// value.
  3308  	ConnContext func(ctx context.Context, c net.Conn) context.Context
  3309  
  3310  	// HTTP2 configures HTTP/2 connections.
  3311  	HTTP2 *HTTP2Config
  3312  
  3313  	// Protocols is the set of protocols accepted by the server.
  3314  	//
  3315  	// If Protocols includes UnencryptedHTTP2, the server will accept
  3316  	// unencrypted HTTP/2 connections. The server can serve both
  3317  	// HTTP/1 and unencrypted HTTP/2 on the same address and port.
  3318  	//
  3319  	// If Protocols is nil, the default is usually HTTP/1 and HTTP/2.
  3320  	// If TLSNextProto is non-nil and does not contain an "h2" entry,
  3321  	// the default is HTTP/1 only.
  3322  	Protocols *Protocols
  3323  
  3324  	// DisableClientPriority specifies whether client-specified priority, as
  3325  	// specified in RFC 9218, should be respected or not.
  3326  	//
  3327  	// This field only takes effect if using HTTP/2, and if no custom write
  3328  	// scheduler is defined for the HTTP/2 server. Otherwise, this field is a
  3329  	// no-op.
  3330  	//
  3331  	// If set to true, requests will be served in a round-robin manner, without
  3332  	// prioritization.
  3333  	DisableClientPriority bool
  3334  
  3335  	inShutdown atomic.Bool // true when server is in shutdown
  3336  
  3337  	disableKeepAlives atomic.Bool
  3338  	nextProtoOnce     sync.Once // guards setupHTTP2_* init
  3339  	nextProtoErr      error     // result of http2.ConfigureServer if used
  3340  
  3341  	mu            sync.Mutex
  3342  	listeners     map[*net.Listener]struct{}
  3343  	activeConn    map[*conn]struct{}
  3344  	onShutdown    []func()
  3345  	h2            *http2Server
  3346  	h2Config      http2ExternalServerConfig
  3347  	h2IdleTimeout time.Duration
  3348  	h3Server      http3Server
  3349  
  3350  	listenerGroup sync.WaitGroup
  3351  }
  3352  
  3353  // Close immediately closes all active net.Listeners and any
  3354  // connections in state [StateNew], [StateActive], or [StateIdle]. For a
  3355  // graceful shutdown, use [Server.Shutdown].
  3356  //
  3357  // Close does not attempt to close (and does not even know about)
  3358  // any hijacked connections, such as WebSockets.
  3359  //
  3360  // Close returns any error returned from closing the [Server]'s
  3361  // underlying Listener(s).
  3362  func (s *Server) Close() error {
  3363  	s.inShutdown.Store(true)
  3364  	s.mu.Lock()
  3365  	defer s.mu.Unlock()
  3366  	err := s.closeListenersLocked()
  3367  	if s.h3Server != nil {
  3368  		ctx, cancel := context.WithCancel(context.Background())
  3369  		cancel()
  3370  		go s.h3Server.Shutdown(ctx)
  3371  	}
  3372  
  3373  	// Unlock s.mu while waiting for listenerGroup.
  3374  	// The group Add and Done calls are made with s.mu held,
  3375  	// to avoid adding a new listener in the window between
  3376  	// us setting inShutdown above and waiting here.
  3377  	s.mu.Unlock()
  3378  	s.listenerGroup.Wait()
  3379  	s.mu.Lock()
  3380  
  3381  	for c := range s.activeConn {
  3382  		c.rwc.Close()
  3383  		delete(s.activeConn, c)
  3384  	}
  3385  	return err
  3386  }
  3387  
  3388  // shutdownPollIntervalMax is the max polling interval when checking
  3389  // quiescence during Server.Shutdown. Polling starts with a small
  3390  // interval and backs off to the max.
  3391  // Ideally we could find a solution that doesn't involve polling,
  3392  // but which also doesn't have a high runtime cost (and doesn't
  3393  // involve any contentious mutexes), but that is left as an
  3394  // exercise for the reader.
  3395  const shutdownPollIntervalMax = 500 * time.Millisecond
  3396  
  3397  // Shutdown gracefully shuts down the server without interrupting any
  3398  // active connections. Shutdown works by first closing all open
  3399  // listeners, then closing all idle connections, and then waiting
  3400  // indefinitely for connections to return to idle and then shut down.
  3401  // If the provided context expires before the shutdown is complete,
  3402  // Shutdown returns the context's error, otherwise it returns any
  3403  // error returned from closing the [Server]'s underlying Listener(s).
  3404  //
  3405  // When Shutdown is called, [Serve], [ServeTLS], [ListenAndServe], and
  3406  // [ListenAndServeTLS] immediately return [ErrServerClosed]. Make sure the
  3407  // program doesn't exit and waits instead for Shutdown to return.
  3408  //
  3409  // Shutdown does not attempt to close nor wait for hijacked
  3410  // connections such as WebSockets. The caller of Shutdown should
  3411  // separately notify such long-lived connections of shutdown and wait
  3412  // for them to close, if desired. See [Server.RegisterOnShutdown] for a way to
  3413  // register shutdown notification functions.
  3414  //
  3415  // Once Shutdown has been called on a server, it may not be reused;
  3416  // future calls to methods such as Serve will return ErrServerClosed.
  3417  func (s *Server) Shutdown(ctx context.Context) error {
  3418  	s.inShutdown.Store(true)
  3419  
  3420  	s.mu.Lock()
  3421  	lnerr := s.closeListenersLocked()
  3422  	for _, f := range s.onShutdown {
  3423  		go f()
  3424  	}
  3425  	if s.h3Server != nil {
  3426  		go s.h3Server.Shutdown(ctx)
  3427  	}
  3428  	s.mu.Unlock()
  3429  	s.listenerGroup.Wait()
  3430  
  3431  	pollIntervalBase := time.Millisecond
  3432  	nextPollInterval := func() time.Duration {
  3433  		// Add 10% jitter.
  3434  		interval := pollIntervalBase + time.Duration(rand.IntN(int(pollIntervalBase/10)))
  3435  		// Double and clamp for next time.
  3436  		pollIntervalBase *= 2
  3437  		if pollIntervalBase > shutdownPollIntervalMax {
  3438  			pollIntervalBase = shutdownPollIntervalMax
  3439  		}
  3440  		return interval
  3441  	}
  3442  
  3443  	timer := time.NewTimer(nextPollInterval())
  3444  	defer timer.Stop()
  3445  	for {
  3446  		if s.closeIdleConns() {
  3447  			return lnerr
  3448  		}
  3449  		select {
  3450  		case <-ctx.Done():
  3451  			return ctx.Err()
  3452  		case <-timer.C:
  3453  			timer.Reset(nextPollInterval())
  3454  		}
  3455  	}
  3456  }
  3457  
  3458  // RegisterOnShutdown registers a function to call on [Server.Shutdown].
  3459  // This can be used to gracefully shutdown connections that have
  3460  // undergone ALPN protocol upgrade or that have been hijacked.
  3461  // This function should start protocol-specific graceful shutdown,
  3462  // but should not wait for shutdown to complete.
  3463  func (s *Server) RegisterOnShutdown(f func()) {
  3464  	s.mu.Lock()
  3465  	s.onShutdown = append(s.onShutdown, f)
  3466  	s.mu.Unlock()
  3467  }
  3468  
  3469  // closeIdleConns closes all idle connections and reports whether the
  3470  // server is quiescent.
  3471  func (s *Server) closeIdleConns() bool {
  3472  	s.mu.Lock()
  3473  	defer s.mu.Unlock()
  3474  	quiescent := true
  3475  	for c := range s.activeConn {
  3476  		st, unixSec := c.getState()
  3477  		// Issue 22682: treat StateNew connections as if
  3478  		// they're idle if we haven't read the first request's
  3479  		// header in over 5 seconds.
  3480  		if st == StateNew && unixSec < time.Now().Unix()-5 {
  3481  			st = StateIdle
  3482  		}
  3483  		if st != StateIdle || unixSec == 0 {
  3484  			// Assume unixSec == 0 means it's a very new
  3485  			// connection, without state set yet.
  3486  			quiescent = false
  3487  			continue
  3488  		}
  3489  		c.rwc.Close()
  3490  		delete(s.activeConn, c)
  3491  	}
  3492  	return quiescent
  3493  }
  3494  
  3495  func (s *Server) closeListenersLocked() error {
  3496  	var err error
  3497  	for ln := range s.listeners {
  3498  		if cerr := (*ln).Close(); cerr != nil && err == nil {
  3499  			err = cerr
  3500  		}
  3501  	}
  3502  	return err
  3503  }
  3504  
  3505  // A ConnState represents the state of a client connection to a server.
  3506  // It's used by the optional [Server.ConnState] hook.
  3507  type ConnState int
  3508  
  3509  const (
  3510  	// StateNew represents a new connection that is expected to
  3511  	// send a request immediately. Connections begin at this
  3512  	// state and then transition to either StateActive or
  3513  	// StateClosed.
  3514  	StateNew ConnState = iota
  3515  
  3516  	// StateActive represents a connection that has read 1 or more
  3517  	// bytes of a request. The Server.ConnState hook for
  3518  	// StateActive fires before the request has entered a handler
  3519  	// and doesn't fire again until the request has been
  3520  	// handled. After the request is handled, the state
  3521  	// transitions to StateClosed, StateHijacked, or StateIdle.
  3522  	// For HTTP/2, StateActive fires on the transition from zero
  3523  	// to one active request, and only transitions away once all
  3524  	// active requests are complete. That means that ConnState
  3525  	// cannot be used to do per-request work; ConnState only notes
  3526  	// the overall state of the connection.
  3527  	StateActive
  3528  
  3529  	// StateIdle represents a connection that has finished
  3530  	// handling a request and is in the keep-alive state, waiting
  3531  	// for a new request. Connections transition from StateIdle
  3532  	// to either StateActive or StateClosed.
  3533  	StateIdle
  3534  
  3535  	// StateHijacked represents a hijacked connection.
  3536  	// This is a terminal state. It does not transition to StateClosed.
  3537  	StateHijacked
  3538  
  3539  	// StateClosed represents a closed connection.
  3540  	// This is a terminal state. Hijacked connections do not
  3541  	// transition to StateClosed.
  3542  	StateClosed
  3543  )
  3544  
  3545  var stateName = map[ConnState]string{
  3546  	StateNew:      "new",
  3547  	StateActive:   "active",
  3548  	StateIdle:     "idle",
  3549  	StateHijacked: "hijacked",
  3550  	StateClosed:   "closed",
  3551  }
  3552  
  3553  func (c ConnState) String() string {
  3554  	return stateName[c]
  3555  }
  3556  
  3557  // badServeHTTP should be an internal detail,
  3558  // but widely used packages access it using linkname.
  3559  // Notable members of the hall of shame include:
  3560  //   - github.com/erda-project/erda-infra
  3561  //
  3562  // Do not remove or change the type signature.
  3563  // See go.dev/issue/67401.
  3564  //
  3565  //go:linkname badServeHTTP net/http.serverHandler.ServeHTTP
  3566  func badServeHTTP(srv *Server, rw ResponseWriter, req *Request) {
  3567  	srv.serveHTTP(rw, req, srv.Handler)
  3568  }
  3569  
  3570  // AllowQuerySemicolons returns a handler that serves requests by converting any
  3571  // unescaped semicolons in the URL query to ampersands, and invoking the handler h.
  3572  //
  3573  // This restores the pre-Go 1.17 behavior of splitting query parameters on both
  3574  // semicolons and ampersands. (See golang.org/issue/25192). Note that this
  3575  // behavior doesn't match that of many proxies, and the mismatch can lead to
  3576  // security issues.
  3577  //
  3578  // AllowQuerySemicolons should be invoked before [Request.ParseForm] is called.
  3579  func AllowQuerySemicolons(h Handler) Handler {
  3580  	return HandlerFunc(func(w ResponseWriter, r *Request) {
  3581  		if strings.Contains(r.URL.RawQuery, ";") {
  3582  			r2 := new(Request)
  3583  			*r2 = *r
  3584  			r2.URL = new(url.URL)
  3585  			*r2.URL = *r.URL
  3586  			r2.URL.RawQuery = strings.ReplaceAll(r.URL.RawQuery, ";", "&")
  3587  			h.ServeHTTP(w, r2)
  3588  		} else {
  3589  			h.ServeHTTP(w, r)
  3590  		}
  3591  	})
  3592  }
  3593  
  3594  // ListenAndServe listens on the TCP network address s.Addr and then
  3595  // calls [Serve] to handle requests on incoming connections.
  3596  // Accepted connections are configured to enable TCP keep-alives.
  3597  //
  3598  // If s.Addr is blank, ":http" is used.
  3599  //
  3600  // ListenAndServe always returns a non-nil error. After [Server.Shutdown] or [Server.Close],
  3601  // the returned error is [ErrServerClosed].
  3602  func (s *Server) ListenAndServe() error {
  3603  	if s.shuttingDown() {
  3604  		return ErrServerClosed
  3605  	}
  3606  	addr := s.Addr
  3607  	if addr == "" {
  3608  		addr = ":http"
  3609  	}
  3610  	ln, err := net.Listen("tcp", addr)
  3611  	if err != nil {
  3612  		return err
  3613  	}
  3614  	return s.Serve(ln)
  3615  }
  3616  
  3617  var testHookServerServe func(*Server, net.Listener) // used if non-nil
  3618  
  3619  // shouldConfigureHTTP2ForServe reports whether Server.Serve should configure
  3620  // automatic HTTP/2. (which sets up the s.TLSNextProto map)
  3621  func (s *Server) shouldConfigureHTTP2ForServe() bool {
  3622  	if s.TLSConfig == nil {
  3623  		// Compatibility with Go 1.6:
  3624  		// If there's no TLSConfig, it's possible that the user just
  3625  		// didn't set it on the http.Server, but did pass it to
  3626  		// tls.NewListener and passed that listener to Serve.
  3627  		// So we should configure HTTP/2 (to set up s.TLSNextProto)
  3628  		// in case the listener returns an "h2" *tls.Conn.
  3629  		return true
  3630  	}
  3631  	if s.protocols().UnencryptedHTTP2() {
  3632  		return true
  3633  	}
  3634  	// The user specified a TLSConfig on their http.Server.
  3635  	// In this, case, only configure HTTP/2 if their tls.Config
  3636  	// explicitly mentions "h2". Otherwise http2.ConfigureServer
  3637  	// would modify the tls.Config to add it, but they probably already
  3638  	// passed this tls.Config to tls.NewListener. And if they did,
  3639  	// it's too late anyway to fix it. It would only be potentially racy.
  3640  	// See Issue 15908.
  3641  	return slices.Contains(s.TLSConfig.NextProtos, "h2")
  3642  }
  3643  
  3644  // ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe],
  3645  // and [ListenAndServeTLS] methods after a call to [Server.Shutdown] or [Server.Close].
  3646  var ErrServerClosed = errors.New("http: Server closed")
  3647  
  3648  // Serve accepts incoming connections on the Listener l, creating a
  3649  // new service goroutine for each. The service goroutines read requests and
  3650  // then call s.Handler to reply to them.
  3651  //
  3652  // HTTP/2 support is only enabled if the Listener returns [*tls.Conn]
  3653  // connections and they were configured with "h2" in the TLS
  3654  // Config.NextProtos.
  3655  //
  3656  // Serve always returns a non-nil error and closes l.
  3657  // After [Server.Shutdown] or [Server.Close], the returned error is [ErrServerClosed].
  3658  func (s *Server) Serve(l net.Listener) error {
  3659  	// This is the sneaky path we use to let x/net/http2 wrap an http.Server
  3660  	// and net/http/internal/http3 install an HTTP/3 implementation:
  3661  	// http2.ConfigureServer calls http.Server.Serve with a net.Listener that
  3662  	// implements a certain interface, which we recognize here as an attempt
  3663  	// to associate an http2.Server with us.
  3664  	//
  3665  	// (This is about as principled as the way we (ab)use Transport.RegisterProtocol,
  3666  	// which is to say not at all. It's worth it.)
  3667  	//
  3668  	// Server.Serve never returns a nil error under normal circumstances.
  3669  	// Returning nil on success informs our caller that we support this
  3670  	// sneaky registration mechanism.
  3671  	switch conf := l.(type) {
  3672  	case http2ExternalServerConfig:
  3673  		s.setHTTP2Config(conf)
  3674  		return nil
  3675  	case http3Server:
  3676  		s.setHTTP3Server(conf)
  3677  		return nil
  3678  	}
  3679  
  3680  	if fn := testHookServerServe; fn != nil {
  3681  		fn(s, l) // call hook with unwrapped listener
  3682  	}
  3683  
  3684  	origListener := l
  3685  	l = &onceCloseListener{Listener: l}
  3686  	defer l.Close()
  3687  
  3688  	if err := s.setupHTTP2_Serve(); err != nil {
  3689  		return err
  3690  	}
  3691  
  3692  	if !s.trackListener(&l, true) {
  3693  		return ErrServerClosed
  3694  	}
  3695  	defer s.trackListener(&l, false)
  3696  
  3697  	baseCtx := context.Background()
  3698  	if s.BaseContext != nil {
  3699  		baseCtx = s.BaseContext(origListener)
  3700  		if baseCtx == nil {
  3701  			panic("BaseContext returned a nil context")
  3702  		}
  3703  	}
  3704  
  3705  	var tempDelay time.Duration // how long to sleep on accept failure
  3706  
  3707  	ctx := context.WithValue(baseCtx, ServerContextKey, s)
  3708  	for {
  3709  		rw, err := l.Accept()
  3710  		if err != nil {
  3711  			if s.shuttingDown() {
  3712  				return ErrServerClosed
  3713  			}
  3714  			if ne, ok := err.(net.Error); ok && ne.Temporary() {
  3715  				if tempDelay == 0 {
  3716  					tempDelay = 5 * time.Millisecond
  3717  				} else {
  3718  					tempDelay *= 2
  3719  				}
  3720  				if max := 1 * time.Second; tempDelay > max {
  3721  					tempDelay = max
  3722  				}
  3723  				s.logf("http: Accept error: %v; retrying in %v", err, tempDelay)
  3724  				time.Sleep(tempDelay)
  3725  				continue
  3726  			}
  3727  			return err
  3728  		}
  3729  		connCtx := ctx
  3730  		if cc := s.ConnContext; cc != nil {
  3731  			connCtx = cc(connCtx, rw)
  3732  			if connCtx == nil {
  3733  				panic("ConnContext returned nil")
  3734  			}
  3735  		}
  3736  		tempDelay = 0
  3737  		c := s.newConn(rw)
  3738  		c.setState(c.rwc, StateNew, runHooks) // before Serve can return
  3739  		go c.serve(connCtx)
  3740  	}
  3741  }
  3742  
  3743  func (s *Server) setupTLSConfig(certFile, keyFile string, nextProtos []string) (*tls.Config, error) {
  3744  	config := cloneTLSConfig(s.TLSConfig)
  3745  	config.NextProtos = nextProtos
  3746  
  3747  	configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil || config.GetConfigForClient != nil
  3748  	if !configHasCert || certFile != "" || keyFile != "" {
  3749  		var err error
  3750  		config.Certificates = make([]tls.Certificate, 1)
  3751  		config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
  3752  		if err != nil {
  3753  			return nil, err
  3754  		}
  3755  	}
  3756  	return config, nil
  3757  }
  3758  
  3759  // ServeTLS accepts incoming connections on the Listener l, creating a
  3760  // new service goroutine for each. The service goroutines perform TLS
  3761  // setup and then read requests, calling s.Handler to reply to them.
  3762  //
  3763  // Files containing a certificate and matching private key for the
  3764  // server must be provided if neither the [Server]'s
  3765  // TLSConfig.Certificates, TLSConfig.GetCertificate nor
  3766  // config.GetConfigForClient are populated.
  3767  // If the certificate is signed by a certificate authority, the
  3768  // certFile should be the concatenation of the server's certificate,
  3769  // any intermediates, and the CA's certificate.
  3770  //
  3771  // ServeTLS always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the
  3772  // returned error is [ErrServerClosed].
  3773  func (s *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
  3774  	// Setup HTTP/2 before s.Serve, to initialize s.TLSConfig
  3775  	// before we clone it and create the TLS Listener.
  3776  	if err := s.setupHTTP2_ServeTLS(); err != nil {
  3777  		return err
  3778  	}
  3779  	if s.h3Server != nil {
  3780  		// Temporary, test-only way to serve HTTP/3 from a PacketConn:
  3781  		// Pass it to ServeTLS wrapped in a net.Listener.
  3782  		// The caller should pass a net.Listener that immediately returns an error
  3783  		// if passed to a Server that doesn't support this path.
  3784  		if x, ok := l.(interface {
  3785  			HTTP3PacketConn() net.PacketConn
  3786  		}); ok {
  3787  			return s.serveHTTP3(x.HTTP3PacketConn(), certFile, keyFile)
  3788  		}
  3789  	}
  3790  
  3791  	var nextProtos []string
  3792  	if s.TLSConfig != nil {
  3793  		nextProtos = s.TLSConfig.NextProtos
  3794  	}
  3795  	config, err := s.setupTLSConfig(certFile, keyFile, adjustNextProtos(nextProtos, s.protocols()))
  3796  	if err != nil {
  3797  		return err
  3798  	}
  3799  
  3800  	tlsListener := tls.NewListener(l, config)
  3801  	return s.Serve(tlsListener)
  3802  }
  3803  
  3804  // HandleRequest handles a request as if it was received by the server.
  3805  //
  3806  //   - If Server.DisableGeneralOptionsHandler is not true,
  3807  //     it responds to "OPTIONS *" requests with 200 OK and Content-Length: 0.
  3808  //   - If Server.Handler is nil, it uses [DefaultServeMux].
  3809  //   - Panics in the handler are recovered.
  3810  //     If the panic is not [ErrAbortHandler], the panic is logged.
  3811  //
  3812  // HandleRequest returns a non-nil error if the handler panics.
  3813  // It returns [ErrAbortHandler] if the handler panicked with that value.
  3814  func (s *Server) HandleRequest(w ResponseWriter, req *Request) error {
  3815  	return s.handleRequest(w, req, s.Handler)
  3816  }
  3817  
  3818  func (s *Server) handleRequest(w ResponseWriter, req *Request, h Handler) (err error) {
  3819  	didPanic := true
  3820  	defer func() {
  3821  		if !didPanic {
  3822  			return
  3823  		}
  3824  		e := recover()
  3825  		if e == ErrAbortHandler {
  3826  			err = ErrAbortHandler
  3827  			return
  3828  		}
  3829  		if e != nil {
  3830  			const size = 64 << 10
  3831  			buf := make([]byte, size)
  3832  			buf = buf[:runtime.Stack(buf, false)]
  3833  			s.logf("http: panic serving %v: %v\n%s", req.RemoteAddr, e, buf)
  3834  		}
  3835  		err = fmt.Errorf("http: panic serving %v: %v", req.RemoteAddr, e)
  3836  	}()
  3837  	s.serveHTTP(w, req, h)
  3838  	didPanic = false
  3839  	return nil
  3840  }
  3841  
  3842  func (s *Server) serveHTTP(rw ResponseWriter, req *Request, handler Handler) {
  3843  	if handler == nil {
  3844  		handler = DefaultServeMux
  3845  	}
  3846  	if !s.DisableGeneralOptionsHandler && req.RequestURI == "*" && req.Method == "OPTIONS" {
  3847  		handler = globalOptionsHandler{}
  3848  	}
  3849  
  3850  	defer func() {
  3851  		if req.MultipartForm != nil {
  3852  			req.MultipartForm.RemoveAll()
  3853  		}
  3854  	}()
  3855  	handler.ServeHTTP(rw, req)
  3856  }
  3857  
  3858  func (s *Server) protocols() Protocols {
  3859  	if s.Protocols != nil {
  3860  		// Historically, even when Protocols for a Server was set to be empty,
  3861  		// the Server can still run normally with just HTTP/1.
  3862  		// To keep backward-compatibility, the zero value of Protocols is
  3863  		// defined as having only HTTP/1 enabled.
  3864  		if s.Protocols.empty() {
  3865  			var p Protocols
  3866  			p.SetHTTP1(true)
  3867  			return p
  3868  		}
  3869  		return *s.Protocols // user-configured set
  3870  	}
  3871  
  3872  	// The historic way of disabling HTTP/2 is to set TLSNextProto to
  3873  	// a non-nil map with no "h2" entry.
  3874  	_, hasH2 := s.TLSNextProto["h2"]
  3875  	http2Disabled := s.TLSNextProto != nil && !hasH2
  3876  
  3877  	// If GODEBUG=http2server=0, then HTTP/2 is disabled unless
  3878  	// the user has manually added an "h2" entry to TLSNextProto
  3879  	// (probably by using x/net/http2 directly).
  3880  	if http2server.Value() == "0" && !hasH2 {
  3881  		http2Disabled = true
  3882  	}
  3883  
  3884  	var p Protocols
  3885  	p.SetHTTP1(true) // default always includes HTTP/1
  3886  	if !http2Disabled {
  3887  		p.SetHTTP2(true)
  3888  	}
  3889  	return p
  3890  }
  3891  
  3892  // adjustNextProtos adds or removes "http/1.1" and "h2" entries from
  3893  // a tls.Config.NextProtos list, according to the set of protocols in protos.
  3894  func adjustNextProtos(nextProtos []string, protos Protocols) []string {
  3895  	// Make a copy of NextProtos since it might be shared with some other tls.Config.
  3896  	// (tls.Config.Clone doesn't do a deep copy.)
  3897  	//
  3898  	// We could avoid an allocation in the common case by checking to see if the slice
  3899  	// is already in order, but this is just one small allocation per connection.
  3900  	nextProtos = slices.Clone(nextProtos)
  3901  	var have Protocols
  3902  	nextProtos = slices.DeleteFunc(nextProtos, func(s string) bool {
  3903  		switch s {
  3904  		case "http/1.1":
  3905  			if !protos.HTTP1() {
  3906  				return true
  3907  			}
  3908  			have.SetHTTP1(true)
  3909  		case "h2":
  3910  			if !protos.HTTP2() {
  3911  				return true
  3912  			}
  3913  			have.SetHTTP2(true)
  3914  		}
  3915  		return false
  3916  	})
  3917  	if protos.HTTP2() && !have.HTTP2() {
  3918  		nextProtos = append(nextProtos, "h2")
  3919  	}
  3920  	if protos.HTTP1() && !have.HTTP1() {
  3921  		nextProtos = append(nextProtos, "http/1.1")
  3922  	}
  3923  	return nextProtos
  3924  }
  3925  
  3926  // trackListener adds or removes a net.Listener to the set of tracked
  3927  // listeners.
  3928  //
  3929  // We store a pointer to interface in the map set, in case the
  3930  // net.Listener is not comparable. This is safe because we only call
  3931  // trackListener via Serve and can track+defer untrack the same
  3932  // pointer to local variable there. We never need to compare a
  3933  // Listener from another caller.
  3934  //
  3935  // It reports whether the server is still up (not Shutdown or Closed).
  3936  func (s *Server) trackListener(ln *net.Listener, add bool) bool {
  3937  	s.mu.Lock()
  3938  	defer s.mu.Unlock()
  3939  	if s.listeners == nil {
  3940  		s.listeners = make(map[*net.Listener]struct{})
  3941  	}
  3942  	if add {
  3943  		if s.shuttingDown() {
  3944  			return false
  3945  		}
  3946  		s.listeners[ln] = struct{}{}
  3947  		s.listenerGroup.Add(1)
  3948  	} else {
  3949  		delete(s.listeners, ln)
  3950  		s.listenerGroup.Done()
  3951  	}
  3952  	return true
  3953  }
  3954  
  3955  func (s *Server) trackConn(c *conn, add bool) {
  3956  	s.mu.Lock()
  3957  	defer s.mu.Unlock()
  3958  	if s.activeConn == nil {
  3959  		s.activeConn = make(map[*conn]struct{})
  3960  	}
  3961  	if add {
  3962  		s.activeConn[c] = struct{}{}
  3963  	} else {
  3964  		delete(s.activeConn, c)
  3965  	}
  3966  }
  3967  
  3968  func (s *Server) idleTimeout() time.Duration {
  3969  	if s.IdleTimeout != 0 {
  3970  		return s.IdleTimeout
  3971  	}
  3972  	return s.ReadTimeout
  3973  }
  3974  
  3975  func (s *Server) readHeaderTimeout() time.Duration {
  3976  	if s.ReadHeaderTimeout != 0 {
  3977  		return s.ReadHeaderTimeout
  3978  	}
  3979  	return s.ReadTimeout
  3980  }
  3981  
  3982  func (s *Server) doKeepAlives() bool {
  3983  	return !s.disableKeepAlives.Load() && !s.shuttingDown()
  3984  }
  3985  
  3986  func (s *Server) shuttingDown() bool {
  3987  	return s.inShutdown.Load()
  3988  }
  3989  
  3990  // SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled.
  3991  // By default, keep-alives are always enabled. Only very
  3992  // resource-constrained environments or servers in the process of
  3993  // shutting down should disable them.
  3994  func (s *Server) SetKeepAlivesEnabled(v bool) {
  3995  	if v {
  3996  		s.disableKeepAlives.Store(false)
  3997  		return
  3998  	}
  3999  	s.disableKeepAlives.Store(true)
  4000  
  4001  	// Close idle HTTP/1 conns:
  4002  	s.closeIdleConns()
  4003  
  4004  	// TODO: Issue 26303: close HTTP/2 conns as soon as they become idle.
  4005  }
  4006  
  4007  func (s *Server) logf(format string, args ...any) {
  4008  	if s.ErrorLog != nil {
  4009  		s.ErrorLog.Printf(format, args...)
  4010  	} else {
  4011  		log.Printf(format, args...)
  4012  	}
  4013  }
  4014  
  4015  // logf prints to the ErrorLog of the *Server associated with request r
  4016  // via ServerContextKey. If there's no associated server, or if ErrorLog
  4017  // is nil, logging is done via the log package's standard logger.
  4018  func logf(r *Request, format string, args ...any) {
  4019  	s, _ := r.Context().Value(ServerContextKey).(*Server)
  4020  	if s != nil && s.ErrorLog != nil {
  4021  		s.ErrorLog.Printf(format, args...)
  4022  	} else {
  4023  		log.Printf(format, args...)
  4024  	}
  4025  }
  4026  
  4027  // ListenAndServe listens on the TCP network address addr and then calls
  4028  // [Serve] with handler to handle requests on incoming connections.
  4029  // Accepted connections are configured to enable TCP keep-alives.
  4030  //
  4031  // The handler is typically nil, in which case [DefaultServeMux] is used.
  4032  //
  4033  // ListenAndServe always returns a non-nil error.
  4034  func ListenAndServe(addr string, handler Handler) error {
  4035  	server := &Server{Addr: addr, Handler: handler}
  4036  	return server.ListenAndServe()
  4037  }
  4038  
  4039  // ListenAndServeTLS acts identically to [ListenAndServe], except that it
  4040  // expects HTTPS connections. Additionally, files containing a certificate and
  4041  // matching private key for the server must be provided. If the certificate
  4042  // is signed by a certificate authority, the certFile should be the concatenation
  4043  // of the server's certificate, any intermediates, and the CA's certificate.
  4044  func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
  4045  	server := &Server{Addr: addr, Handler: handler}
  4046  	return server.ListenAndServeTLS(certFile, keyFile)
  4047  }
  4048  
  4049  // http3ServerHandler implements an interface in an external library that
  4050  // supports HTTP/3, allowing an external implementation of HTTP/3 to be used
  4051  // via net/http. See https://go.dev/issue/77440 for details.
  4052  //
  4053  // This is currently only used with net/http/internal/http3, to allow us to
  4054  // test our HTTP/3 implementation against tests in net/http. HTTP/3 is not yet
  4055  // accessible to end-users.
  4056  type http3ServerHandler struct {
  4057  	srv         *Server
  4058  	tlsConfig   *tls.Config
  4059  	baseCtx     context.Context
  4060  	errc        chan error
  4061  	shutdownCtx context.Context
  4062  }
  4063  
  4064  // ServeHTTP ensures that http3ServerHandler implements the Handler interface,
  4065  // and gives an HTTP/3 server implementation access to the net/http handler.
  4066  func (h *http3ServerHandler) ServeHTTP(w ResponseWriter, r *Request) {
  4067  	h.srv.serveHTTP(w, r, h.srv.Handler)
  4068  }
  4069  
  4070  // Addr gives an HTTP/3 server implementation the address that it should listen
  4071  // on.
  4072  func (h *http3ServerHandler) Addr() string {
  4073  	return h.srv.Addr
  4074  }
  4075  
  4076  // TLSConfig gives an HTTP/3 server implementation the *tls.Config that it
  4077  // should use.
  4078  func (h *http3ServerHandler) TLSConfig() *tls.Config {
  4079  	return h.tlsConfig
  4080  }
  4081  
  4082  // BaseContext gives an HTTP/3 server implementation the base context to use
  4083  // for server requests.
  4084  func (h *http3ServerHandler) BaseContext() context.Context {
  4085  	return h.baseCtx
  4086  }
  4087  
  4088  // ListenErrHook should be called by an HTTP/3 server implementation to
  4089  // propagate any error it encounters when trying to listen, if any, to
  4090  // net/http.
  4091  func (h *http3ServerHandler) ListenErrHook(err error) {
  4092  	h.errc <- err
  4093  }
  4094  
  4095  // ShutdownContext gives an HTTP/3 server implementation the context that is
  4096  // used when [Server.Shutdown] is called. This allows an HTTP/3 server
  4097  // implementation to know how long it can take to gracefully shutdown in the
  4098  // function it registers with [Server.RegisterOnShutdown]. Callers must not use
  4099  // this method for any other purpose.
  4100  func (h *http3ServerHandler) ShutdownContext() context.Context {
  4101  	return h.shutdownCtx
  4102  }
  4103  
  4104  // ListenAndServeTLS listens on the TCP network address s.Addr and
  4105  // then calls [ServeTLS] to handle requests on incoming TLS connections.
  4106  // Accepted connections are configured to enable TCP keep-alives.
  4107  //
  4108  // Filenames containing a certificate and matching private key for the
  4109  // server must be provided if neither the [Server]'s TLSConfig.Certificates
  4110  // nor TLSConfig.GetCertificate are populated. If the certificate is
  4111  // signed by a certificate authority, the certFile should be the
  4112  // concatenation of the server's certificate, any intermediates, and
  4113  // the CA's certificate.
  4114  //
  4115  // If s.Addr is blank, ":https" is used.
  4116  //
  4117  // ListenAndServeTLS always returns a non-nil error. After [Server.Shutdown] or
  4118  // [Server.Close], the returned error is [ErrServerClosed].
  4119  func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
  4120  	if s.shuttingDown() {
  4121  		return ErrServerClosed
  4122  	}
  4123  	addr := s.Addr
  4124  	if addr == "" {
  4125  		addr = ":https"
  4126  	}
  4127  
  4128  	p := s.protocols()
  4129  	if p.http3() {
  4130  		// TODO: Support HTTP/3 here.
  4131  		// For now, tests use Server.ServeTLS.
  4132  		return errors.New("http: Server.Protocols contains HTTP3, but Server does not support HTTP/3")
  4133  	}
  4134  	// Only start a TCP listener if HTTP/1 or HTTP/2 is used.
  4135  	if !p.HTTP1() && !p.HTTP2() && !p.UnencryptedHTTP2() {
  4136  		return errors.New("http: no protocols configured")
  4137  	}
  4138  
  4139  	ln, err := net.Listen("tcp", addr)
  4140  	if err != nil {
  4141  		return err
  4142  	}
  4143  	defer ln.Close()
  4144  	return s.ServeTLS(ln, certFile, keyFile)
  4145  }
  4146  
  4147  // setupHTTP2_ServeTLS conditionally configures HTTP/2 on
  4148  // s and reports whether there was an error setting it up. If it is
  4149  // not configured for policy reasons, nil is returned.
  4150  func (s *Server) setupHTTP2_ServeTLS() error {
  4151  	s.nextProtoOnce.Do(s.onceSetNextProtoDefaults)
  4152  	return s.nextProtoErr
  4153  }
  4154  
  4155  // setupHTTP2_Serve is called from (*Server).Serve and conditionally
  4156  // configures HTTP/2 on s using a more conservative policy than
  4157  // setupHTTP2_ServeTLS because Serve is called after tls.Listen,
  4158  // and may be called concurrently. See shouldConfigureHTTP2ForServe.
  4159  //
  4160  // The tests named TestTransportAutomaticHTTP2* and
  4161  // TestConcurrentServerServe in server_test.go demonstrate some
  4162  // of the supported use cases and motivations.
  4163  func (s *Server) setupHTTP2_Serve() error {
  4164  	s.nextProtoOnce.Do(s.onceSetNextProtoDefaults_Serve)
  4165  	return s.nextProtoErr
  4166  }
  4167  
  4168  func (s *Server) onceSetNextProtoDefaults_Serve() {
  4169  	if s.shouldConfigureHTTP2ForServe() {
  4170  		s.onceSetNextProtoDefaults()
  4171  	}
  4172  }
  4173  
  4174  var http2server = godebug.New("http2server")
  4175  
  4176  // onceSetNextProtoDefaults configures HTTP/2, if the user hasn't
  4177  // configured otherwise. (by setting s.TLSNextProto non-nil)
  4178  // It must only be called via s.nextProtoOnce (use s.setupHTTP2_*).
  4179  func (s *Server) onceSetNextProtoDefaults() {
  4180  	if omitHTTP2Server {
  4181  		return
  4182  	}
  4183  	p := s.protocols()
  4184  	if !p.HTTP2() && !p.UnencryptedHTTP2() {
  4185  		return
  4186  	}
  4187  	if http2server.Value() == "0" {
  4188  		http2server.IncNonDefault()
  4189  		return
  4190  	}
  4191  	if _, ok := s.TLSNextProto["h2"]; ok {
  4192  		// TLSNextProto already contains an HTTP/2 implementation.
  4193  		// The user probably called golang.org/x/net/http2.ConfigureServer
  4194  		// to add it.
  4195  		return
  4196  	}
  4197  	s.configureHTTP2()
  4198  }
  4199  
  4200  // TimeoutHandler returns a [Handler] that runs h with the given time limit.
  4201  //
  4202  // The new Handler calls h.ServeHTTP to handle each request, but if a
  4203  // call runs for longer than its time limit, the handler responds with
  4204  // a 503 Service Unavailable error and the given message in its body.
  4205  // (If msg is empty, a suitable default message will be sent.)
  4206  // After such a timeout, writes by h to its [ResponseWriter] will return
  4207  // [ErrHandlerTimeout].
  4208  //
  4209  // TimeoutHandler supports the [Pusher] interface but does not support
  4210  // the [Hijacker] or [Flusher] interfaces.
  4211  func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler {
  4212  	return &timeoutHandler{
  4213  		handler: h,
  4214  		body:    msg,
  4215  		dt:      dt,
  4216  	}
  4217  }
  4218  
  4219  // ErrHandlerTimeout is returned on [ResponseWriter] Write calls
  4220  // in handlers which have timed out.
  4221  var ErrHandlerTimeout = errors.New("http: Handler timeout")
  4222  
  4223  type timeoutHandler struct {
  4224  	handler Handler
  4225  	body    string
  4226  	dt      time.Duration
  4227  
  4228  	// When set, no context will be created and this context will
  4229  	// be used instead.
  4230  	testContext context.Context
  4231  }
  4232  
  4233  func (h *timeoutHandler) errorBody() string {
  4234  	if h.body != "" {
  4235  		return h.body
  4236  	}
  4237  	return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>"
  4238  }
  4239  
  4240  func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
  4241  	ctx := h.testContext
  4242  	if ctx == nil {
  4243  		var cancelCtx context.CancelFunc
  4244  		ctx, cancelCtx = context.WithTimeout(r.Context(), h.dt)
  4245  		defer cancelCtx()
  4246  	}
  4247  	r = r.WithContext(ctx)
  4248  	done := make(chan struct{})
  4249  	tw := &timeoutWriter{
  4250  		w:   w,
  4251  		h:   make(Header),
  4252  		req: r,
  4253  	}
  4254  	panicChan := make(chan any, 1)
  4255  	go func() {
  4256  		defer func() {
  4257  			if p := recover(); p != nil {
  4258  				panicChan <- p
  4259  			}
  4260  		}()
  4261  		h.handler.ServeHTTP(tw, r)
  4262  		close(done)
  4263  	}()
  4264  	select {
  4265  	case p := <-panicChan:
  4266  		panic(p)
  4267  	case <-done:
  4268  		tw.mu.Lock()
  4269  		defer tw.mu.Unlock()
  4270  		dst := w.Header()
  4271  		maps.Copy(dst, tw.h)
  4272  		if !tw.wroteHeader {
  4273  			tw.code = StatusOK
  4274  		}
  4275  		w.WriteHeader(tw.code)
  4276  		w.Write(tw.wbuf.Bytes())
  4277  	case <-ctx.Done():
  4278  		tw.mu.Lock()
  4279  		defer tw.mu.Unlock()
  4280  		switch err := ctx.Err(); err {
  4281  		case context.DeadlineExceeded:
  4282  			w.WriteHeader(StatusServiceUnavailable)
  4283  			io.WriteString(w, h.errorBody())
  4284  			tw.err = ErrHandlerTimeout
  4285  		default:
  4286  			w.WriteHeader(StatusServiceUnavailable)
  4287  			tw.err = err
  4288  		}
  4289  	}
  4290  }
  4291  
  4292  type timeoutWriter struct {
  4293  	w    ResponseWriter
  4294  	h    Header
  4295  	wbuf bytes.Buffer
  4296  	req  *Request
  4297  
  4298  	mu          sync.Mutex
  4299  	err         error
  4300  	wroteHeader bool
  4301  	code        int
  4302  }
  4303  
  4304  var _ Pusher = (*timeoutWriter)(nil)
  4305  
  4306  // Push implements the [Pusher] interface.
  4307  func (tw *timeoutWriter) Push(target string, opts *PushOptions) error {
  4308  	if pusher, ok := tw.w.(Pusher); ok {
  4309  		return pusher.Push(target, opts)
  4310  	}
  4311  	return ErrNotSupported
  4312  }
  4313  
  4314  func (tw *timeoutWriter) Header() Header { return tw.h }
  4315  
  4316  func (tw *timeoutWriter) Write(p []byte) (int, error) {
  4317  	tw.mu.Lock()
  4318  	defer tw.mu.Unlock()
  4319  	if tw.err != nil {
  4320  		return 0, tw.err
  4321  	}
  4322  	if !tw.wroteHeader {
  4323  		tw.writeHeaderLocked(StatusOK)
  4324  	}
  4325  	return tw.wbuf.Write(p)
  4326  }
  4327  
  4328  func (tw *timeoutWriter) writeHeaderLocked(code int) {
  4329  	checkWriteHeaderCode(code)
  4330  
  4331  	switch {
  4332  	case tw.err != nil:
  4333  		return
  4334  	case tw.wroteHeader:
  4335  		if tw.req != nil {
  4336  			caller := relevantCaller()
  4337  			logf(tw.req, "http: superfluous response.WriteHeader call from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
  4338  		}
  4339  	default:
  4340  		tw.wroteHeader = true
  4341  		tw.code = code
  4342  	}
  4343  }
  4344  
  4345  func (tw *timeoutWriter) WriteHeader(code int) {
  4346  	tw.mu.Lock()
  4347  	defer tw.mu.Unlock()
  4348  	tw.writeHeaderLocked(code)
  4349  }
  4350  
  4351  // onceCloseListener wraps a net.Listener, protecting it from
  4352  // multiple Close calls.
  4353  type onceCloseListener struct {
  4354  	net.Listener
  4355  	once     sync.Once
  4356  	closeErr error
  4357  }
  4358  
  4359  func (oc *onceCloseListener) Close() error {
  4360  	oc.once.Do(oc.close)
  4361  	return oc.closeErr
  4362  }
  4363  
  4364  func (oc *onceCloseListener) close() { oc.closeErr = oc.Listener.Close() }
  4365  
  4366  // globalOptionsHandler responds to "OPTIONS *" requests.
  4367  type globalOptionsHandler struct{}
  4368  
  4369  func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
  4370  	w.Header().Set("Content-Length", "0")
  4371  	if r.ContentLength != 0 {
  4372  		// Read up to 4KB of OPTIONS body (as mentioned in the
  4373  		// spec as being reserved for future use), but anything
  4374  		// over that is considered a waste of server resources
  4375  		// (or an attack) and we abort and close the connection,
  4376  		// courtesy of MaxBytesReader's EOF behavior.
  4377  		mb := MaxBytesReader(w, r.Body, 4<<10)
  4378  		io.Copy(io.Discard, mb)
  4379  	}
  4380  }
  4381  
  4382  // initALPNRequest is an HTTP handler that initializes certain
  4383  // uninitialized fields in its *Request. Such partially-initialized
  4384  // Requests come from ALPN protocol handlers.
  4385  type initALPNRequest struct {
  4386  	ctx context.Context
  4387  	c   *tls.Conn
  4388  	srv *Server
  4389  }
  4390  
  4391  // BaseContext is an exported but unadvertised [http.Handler] method
  4392  // recognized by x/net/http2 to pass down a context; the TLSNextProto
  4393  // API predates context support so we shoehorn through the only
  4394  // interface we have available.
  4395  func (h initALPNRequest) BaseContext() context.Context { return h.ctx }
  4396  
  4397  func (h initALPNRequest) ServeHTTP(rw ResponseWriter, req *Request) {
  4398  	if req.TLS == nil {
  4399  		req.TLS = &tls.ConnectionState{}
  4400  		*req.TLS = h.c.ConnectionState()
  4401  	}
  4402  	if req.Body == nil {
  4403  		req.Body = NoBody
  4404  	}
  4405  	if req.RemoteAddr == "" {
  4406  		req.RemoteAddr = h.c.RemoteAddr().String()
  4407  	}
  4408  	h.srv.serveHTTP(rw, req, h.srv.Handler)
  4409  }
  4410  
  4411  // loggingConn is used for debugging.
  4412  type loggingConn struct {
  4413  	name string
  4414  	net.Conn
  4415  }
  4416  
  4417  var (
  4418  	uniqNameMu   sync.Mutex
  4419  	uniqNameNext = make(map[string]int)
  4420  )
  4421  
  4422  func newLoggingConn(baseName string, c net.Conn) net.Conn {
  4423  	uniqNameMu.Lock()
  4424  	defer uniqNameMu.Unlock()
  4425  	uniqNameNext[baseName]++
  4426  	return &loggingConn{
  4427  		name: fmt.Sprintf("%s-%d", baseName, uniqNameNext[baseName]),
  4428  		Conn: c,
  4429  	}
  4430  }
  4431  
  4432  func (c *loggingConn) Write(p []byte) (n int, err error) {
  4433  	log.Printf("%s.Write(%d) = ....", c.name, len(p))
  4434  	n, err = c.Conn.Write(p)
  4435  	log.Printf("%s.Write(%d) = %d, %v", c.name, len(p), n, err)
  4436  	return
  4437  }
  4438  
  4439  func (c *loggingConn) Read(p []byte) (n int, err error) {
  4440  	log.Printf("%s.Read(%d) = ....", c.name, len(p))
  4441  	n, err = c.Conn.Read(p)
  4442  	log.Printf("%s.Read(%d) = %d, %v", c.name, len(p), n, err)
  4443  	return
  4444  }
  4445  
  4446  func (c *loggingConn) Close() (err error) {
  4447  	log.Printf("%s.Close() = ...", c.name)
  4448  	err = c.Conn.Close()
  4449  	log.Printf("%s.Close() = %v", c.name, err)
  4450  	return
  4451  }
  4452  
  4453  // checkConnErrorWriter writes to c.rwc and records any write errors to c.werr.
  4454  // It only contains one field (and a pointer field at that), so it
  4455  // fits in an interface value without an extra allocation.
  4456  type checkConnErrorWriter struct {
  4457  	c *conn
  4458  }
  4459  
  4460  func (w checkConnErrorWriter) Write(p []byte) (n int, err error) {
  4461  	n, err = w.c.rwc.Write(p)
  4462  	if err != nil && w.c.werr == nil {
  4463  		w.c.werr = err
  4464  		w.c.cancelCtx()
  4465  	}
  4466  	return
  4467  }
  4468  
  4469  func numLeadingCRorLF(v []byte) (n int) {
  4470  	for _, b := range v {
  4471  		if b == '\r' || b == '\n' {
  4472  			n++
  4473  			continue
  4474  		}
  4475  		break
  4476  	}
  4477  	return
  4478  }
  4479  
  4480  // tlsRecordHeaderLooksLikeHTTP reports whether a TLS record header
  4481  // looks like it might've been a misdirected plaintext HTTP request.
  4482  func tlsRecordHeaderLooksLikeHTTP(hdr [5]byte) bool {
  4483  	switch string(hdr[:]) {
  4484  	case "GET /", "HEAD ", "POST ", "PUT /", "OPTIO":
  4485  		return true
  4486  	}
  4487  	return false
  4488  }
  4489  
  4490  // MaxBytesHandler returns a [Handler] that runs h with its [ResponseWriter] and [Request.Body] wrapped by a MaxBytesReader.
  4491  func MaxBytesHandler(h Handler, n int64) Handler {
  4492  	return HandlerFunc(func(w ResponseWriter, r *Request) {
  4493  		r2 := *r
  4494  		r2.Body = MaxBytesReader(w, r.Body, n)
  4495  		h.ServeHTTP(w, &r2)
  4496  	})
  4497  }
  4498  

View as plain text