Source file src/vendor/golang.org/x/net/internal/http3/server.go

     1  // Copyright 2025 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  package http3
     6  
     7  import (
     8  	"context"
     9  	"crypto/tls"
    10  	"errors"
    11  	"fmt"
    12  	"maps"
    13  	"net"
    14  	"net/http"
    15  	"net/textproto"
    16  	"os"
    17  	"slices"
    18  	"strconv"
    19  	"strings"
    20  	"sync"
    21  	"time"
    22  
    23  	"golang.org/x/net/http/httpguts"
    24  	"golang.org/x/net/internal/httpcommon"
    25  	"golang.org/x/net/quic"
    26  )
    27  
    28  // A server is an HTTP/3 server.
    29  // The zero value for server is a valid server.
    30  type server struct {
    31  	srv1 *http.Server
    32  	opts ServerOpts
    33  
    34  	initOnce sync.Once
    35  
    36  	// connClosed is used to signal that a connection has been unregistered
    37  	// from activeConns. That way, when shutting down gracefully, the server
    38  	// can avoid busy-waiting for activeConns to be empty.
    39  	connClosed  chan any
    40  	mu          sync.Mutex // Guards fields below.
    41  	activeConns map[*serverConn]struct{}
    42  }
    43  
    44  // netHTTPServer implements the net/http.http3Server interface,
    45  // allowing our HTTP/3 server to integrate with net/http.
    46  type netHTTPServer struct {
    47  	*server
    48  }
    49  
    50  // Implement net.Listener, so we can pass a netHTTPServer to net/http.Server.Serve.
    51  func (netHTTPServer) Accept() (net.Conn, error) { return nil, net.ErrClosed }
    52  func (netHTTPServer) Close() error              { return nil }
    53  func (netHTTPServer) Addr() net.Addr            { return nil }
    54  
    55  // ServeHTTP3 starts serving HTTP/3 on a UDP port.
    56  //
    57  // The ctx parameter is used as the base context for request handlers
    58  // for requests receieved via this port.
    59  func (s netHTTPServer) ServeHTTP3(ctx context.Context, conn net.PacketConn, tlsConfig *tls.Config, h http.Handler) error {
    60  	s.init()
    61  	e, err := quic.NewEndpoint(conn, newQUICConfig(s.opts.QUICConfig, tlsConfig))
    62  	if err != nil {
    63  		return err
    64  	}
    65  	return s.serve(ctx, e, h)
    66  }
    67  
    68  // Shutdown shuts down the server.
    69  func (s netHTTPServer) Shutdown(ctx context.Context) error {
    70  	s.shutdown(ctx)
    71  	return nil
    72  }
    73  
    74  type ServerOpts struct {
    75  	// QUICConfig is the QUIC configuration used by the server.
    76  	// QUICConfig may be nil and should not be modified after calling
    77  	// RegisterServer.
    78  	// If QUICConfig.TLSConfig is nil, the TLSConfig of the net/http Server
    79  	// given to RegisterServer will be used.
    80  	QUICConfig *quic.Config
    81  }
    82  
    83  // RegisterServer adds HTTP/3 support to a net/http Server.
    84  //
    85  // RegisterServer must be called before s begins serving, and only affects
    86  // s.ListenAndServeTLS.
    87  func RegisterServer(s *http.Server, opts ServerOpts) error {
    88  	if err := s.Serve(netHTTPServer{&server{
    89  		opts: opts,
    90  		srv1: s,
    91  	}}); err != nil {
    92  		return errors.New("http3: net/http does not support HTTP/3")
    93  	}
    94  	return nil
    95  }
    96  
    97  func (s *server) init() {
    98  	s.initOnce.Do(func() {
    99  		s.activeConns = make(map[*serverConn]struct{})
   100  		s.connClosed = make(chan any, 1)
   101  	})
   102  }
   103  
   104  // serve accepts incoming connections on the QUIC endpoint e,
   105  // and handles requests from those connections.
   106  func (s *server) serve(ctx context.Context, e *quic.Endpoint, h http.Handler) error {
   107  	s.init()
   108  	defer e.Close(canceledCtx)
   109  	for {
   110  		qconn, err := e.Accept(ctx)
   111  		if err != nil {
   112  			return err
   113  		}
   114  		go s.newServerConn(ctx, qconn, h)
   115  	}
   116  }
   117  
   118  // shutdown attempts a graceful shutdown for the server.
   119  func (s *server) shutdown(ctx context.Context) {
   120  	// Set a reasonable default in case ctx is nil.
   121  	if ctx == nil {
   122  		var cancel context.CancelFunc
   123  		ctx, cancel = context.WithTimeout(context.Background(), time.Second)
   124  		defer cancel()
   125  	}
   126  
   127  	// Send GOAWAY frames to all active connections to give a chance for them
   128  	// to gracefully terminate.
   129  	s.mu.Lock()
   130  	for sc := range s.activeConns {
   131  		// TODO: Modify x/net/quic stream API so that write errors from context
   132  		// deadline are sticky.
   133  		go sc.sendGoaway()
   134  	}
   135  	s.mu.Unlock()
   136  
   137  	// Complete shutdown as soon as there are no more active connections or ctx
   138  	// is done, whichever comes first.
   139  	defer func() {
   140  		s.mu.Lock()
   141  		defer s.mu.Unlock()
   142  		for sc := range s.activeConns {
   143  			sc.abort(&connectionError{
   144  				code:    errH3NoError,
   145  				message: "server is shutting down",
   146  			})
   147  		}
   148  	}()
   149  	noMoreConns := func() bool {
   150  		s.mu.Lock()
   151  		defer s.mu.Unlock()
   152  		return len(s.activeConns) == 0
   153  	}
   154  	for {
   155  		if noMoreConns() {
   156  			return
   157  		}
   158  		select {
   159  		case <-ctx.Done():
   160  			return
   161  		case <-s.connClosed:
   162  		}
   163  	}
   164  }
   165  
   166  func (s *server) registerConn(sc *serverConn) {
   167  	s.mu.Lock()
   168  	defer s.mu.Unlock()
   169  	s.activeConns[sc] = struct{}{}
   170  }
   171  
   172  func (s *server) unregisterConn(sc *serverConn) {
   173  	s.mu.Lock()
   174  	delete(s.activeConns, sc)
   175  	s.mu.Unlock()
   176  	select {
   177  	case s.connClosed <- struct{}{}:
   178  	default:
   179  		// Channel already full. No need to send more values since we are just
   180  		// using this channel as a simpler sync.Cond.
   181  	}
   182  }
   183  
   184  func (s *server) readHeaderTimeout() time.Duration {
   185  	if s.srv1 == nil || s.srv1.ReadHeaderTimeout == 0 {
   186  		return s.readTimeout()
   187  	}
   188  	return s.srv1.ReadHeaderTimeout
   189  }
   190  
   191  func (s *server) readTimeout() time.Duration {
   192  	if s.srv1 == nil {
   193  		return 0
   194  	}
   195  	return s.srv1.ReadTimeout
   196  }
   197  
   198  func (s *server) writeTimeout() time.Duration {
   199  	if s.srv1 == nil {
   200  		return 0
   201  	}
   202  	return s.srv1.WriteTimeout
   203  }
   204  
   205  // TODO: this is currently unused, enforce it.
   206  func (s *server) idleTimeout() time.Duration {
   207  	if s.srv1 == nil || s.srv1.IdleTimeout == 0 {
   208  		return s.readTimeout()
   209  	}
   210  	return s.srv1.IdleTimeout
   211  }
   212  
   213  type serverConn struct {
   214  	qconn   *quic.Conn
   215  	srv     *server
   216  	baseCtx context.Context
   217  	handler http.Handler
   218  
   219  	genericConn // for handleUnidirectionalStream
   220  	enc         qpackEncoder
   221  	dec         qpackDecoder
   222  
   223  	// For handling shutdown.
   224  	controlStream      *stream
   225  	mu                 sync.Mutex // Guards everything below.
   226  	maxRequestStreamID int64
   227  	goawaySent         bool
   228  }
   229  
   230  // newServerConn handles a new connection.
   231  // The baseCtx parameter is the base context for request handlers on this connection.
   232  func (s *server) newServerConn(baseCtx context.Context, qconn *quic.Conn, h http.Handler) {
   233  	sc := &serverConn{
   234  		qconn:   qconn,
   235  		srv:     s,
   236  		baseCtx: baseCtx,
   237  		handler: h,
   238  	}
   239  	s.registerConn(sc)
   240  	defer s.unregisterConn(sc)
   241  	sc.enc.init()
   242  
   243  	// Create control stream and send SETTINGS frame.
   244  	// TODO: Time out on creating stream.
   245  	var err error
   246  	sc.controlStream, err = newConnStream(context.Background(), sc.qconn, streamTypeControl)
   247  	if err != nil {
   248  		return
   249  	}
   250  	sc.controlStream.writeSettings()
   251  	sc.controlStream.Flush()
   252  
   253  	sc.acceptStreams(sc.qconn, sc)
   254  }
   255  
   256  func (sc *serverConn) handleControlStream(st *stream) error {
   257  	// "A SETTINGS frame MUST be sent as the first frame of each control stream [...]"
   258  	// https://www.rfc-editor.org/rfc/rfc9114.html#section-7.2.4-2
   259  	if err := st.readSettings(func(settingsType, settingsValue int64) error {
   260  		switch settingsType {
   261  		case settingsMaxFieldSectionSize:
   262  			_ = settingsValue // TODO
   263  		case settingsQPACKMaxTableCapacity:
   264  			_ = settingsValue // TODO
   265  		case settingsQPACKBlockedStreams:
   266  			_ = settingsValue // TODO
   267  		default:
   268  			// Unknown settings types are ignored.
   269  		}
   270  		return nil
   271  	}); err != nil {
   272  		return err
   273  	}
   274  
   275  	for {
   276  		ftype, err := st.readFrameHeader()
   277  		if err != nil {
   278  			return err
   279  		}
   280  		switch ftype {
   281  		case frameTypeCancelPush:
   282  			// "If a server receives a CANCEL_PUSH frame for a push ID
   283  			// that has not yet been mentioned by a PUSH_PROMISE frame,
   284  			// this MUST be treated as a connection error of type H3_ID_ERROR."
   285  			// https://www.rfc-editor.org/rfc/rfc9114.html#section-7.2.3-8
   286  			return &connectionError{
   287  				code:    errH3IDError,
   288  				message: "CANCEL_PUSH for unsent push ID",
   289  			}
   290  		case frameTypeGoaway:
   291  			return errH3NoError
   292  		default:
   293  			// Unknown frames are ignored.
   294  			if err := st.discardUnknownFrame(ftype); err != nil {
   295  				return err
   296  			}
   297  		}
   298  	}
   299  }
   300  
   301  func (sc *serverConn) handleEncoderStream(*stream) error {
   302  	// TODO
   303  	return nil
   304  }
   305  
   306  func (sc *serverConn) handleDecoderStream(*stream) error {
   307  	// TODO
   308  	return nil
   309  }
   310  
   311  func (sc *serverConn) handlePushStream(*stream) error {
   312  	// "[...] if a server receives a client-initiated push stream,
   313  	// this MUST be treated as a connection error of type H3_STREAM_CREATION_ERROR."
   314  	// https://www.rfc-editor.org/rfc/rfc9114.html#section-6.2.2-3
   315  	return &connectionError{
   316  		code:    errH3StreamCreationError,
   317  		message: "client created push stream",
   318  	}
   319  }
   320  
   321  // hasDisallowedConnectionHeader reports whether h contains connection headers
   322  // that are not allowed in HTTP/3:
   323  //
   324  // "An endpoint MUST NOT generate an HTTP/3 field section containing
   325  // connection-specific fields; any message containing connection-specific
   326  // fields MUST be treated as malformed."
   327  //
   328  // "The only exception to this is the TE header field, which MAY be present in
   329  // an HTTP/3 request header; when it is, it MUST NOT contain any value other
   330  // than "trailers"."
   331  func hasDisallowedConnectionHeader(h http.Header) bool {
   332  	neverAllowed := []string{
   333  		"Connection",
   334  		"Keep-Alive",
   335  		"Proxy-Connection",
   336  		"Transfer-Encoding",
   337  		"Upgrade",
   338  	}
   339  	for _, k := range neverAllowed {
   340  		if _, ok := h[k]; ok {
   341  			return true
   342  		}
   343  	}
   344  	if te, ok := h["Te"]; ok && (len(te) != 1 || te[0] != "trailers") {
   345  		return true
   346  	}
   347  	return false
   348  }
   349  
   350  type pseudoHeader struct {
   351  	method    string
   352  	scheme    string
   353  	path      string
   354  	authority string
   355  }
   356  
   357  func (sc *serverConn) parseHeader(st *stream) (http.Header, pseudoHeader, error) {
   358  	ftype, err := st.readFrameHeader()
   359  	if err != nil {
   360  		return nil, pseudoHeader{}, err
   361  	}
   362  	if ftype != frameTypeHeaders {
   363  		return nil, pseudoHeader{}, &streamError{errH3MessageError, "received other frames when expecting HEADERS"}
   364  	}
   365  	header := make(http.Header)
   366  	var pHeader pseudoHeader
   367  	var dec qpackDecoder
   368  	var hasMethod, hasScheme, hasPath, hasAuthority bool
   369  	if err := dec.decode(st, func(_ indexType, name, value string) error {
   370  		if !httpguts.ValidHeaderFieldValue(value) {
   371  			return &streamError{errH3MessageError, "invalid field value"}
   372  		}
   373  		if name == "" || (name[0] == ':' && value == "") {
   374  			// Reject 0-length pseudo-header values up front,
   375  			// to avoid any confusion down the line between
   376  			// "present but zero-length" and "absent".
   377  			return &streamError{errH3MessageError, "invalid field"}
   378  		}
   379  		switch name {
   380  		case ":method":
   381  			if hasMethod {
   382  				return &streamError{errH3MessageError, "duplicate :method"}
   383  			}
   384  			hasMethod = true
   385  			pHeader.method = value
   386  		case ":scheme":
   387  			if hasScheme {
   388  				return &streamError{errH3MessageError, "duplicate :scheme"}
   389  			}
   390  			hasScheme = true
   391  			pHeader.scheme = value
   392  		case ":path":
   393  			if hasPath {
   394  				return &streamError{errH3MessageError, "duplicate :path"}
   395  			}
   396  			hasPath = true
   397  			pHeader.path = value
   398  		case ":authority":
   399  			if hasAuthority {
   400  				return &streamError{errH3MessageError, "duplicate :authority"}
   401  			}
   402  			hasAuthority = true
   403  			pHeader.authority = value
   404  		default:
   405  			if !validWireHeaderFieldName(name) {
   406  				return &streamError{errH3MessageError, "invalid field name"}
   407  			}
   408  			header.Add(name, value)
   409  		}
   410  		return nil
   411  	}); err != nil {
   412  		return nil, pseudoHeader{}, err
   413  	}
   414  	if err := st.endFrame(); err != nil {
   415  		return nil, pseudoHeader{}, err
   416  	}
   417  	if hasDisallowedConnectionHeader(header) {
   418  		return nil, pseudoHeader{}, &streamError{errH3MessageError, "invalid connection-related header"}
   419  	}
   420  
   421  	// "All HTTP/3 requests MUST include exactly one value for the :method,
   422  	// :scheme, and :path pseudo-header fields, unless the request is a CONNECT
   423  	// request"
   424  	//
   425  	// "A CONNECT request MUST be constructed as follows:
   426  	// - The :method pseudo-header field is set to "CONNECT"
   427  	// - The :scheme and :path pseudo-header fields are omitted
   428  	// - The :authority pseudo-header field contains the host and port to connect to"
   429  	if !hasMethod {
   430  		return nil, pseudoHeader{}, &streamError{errH3MessageError, "missing :method"}
   431  	}
   432  	if pHeader.method != "CONNECT" && (!hasScheme || !hasPath) {
   433  		return nil, pseudoHeader{}, &streamError{errH3MessageError, "missing :scheme or :path for non-CONNECT requests"}
   434  	}
   435  	if pHeader.method == "CONNECT" && (hasScheme || hasPath || !hasAuthority) {
   436  		return nil, pseudoHeader{}, &streamError{
   437  			errH3MessageError, "CONNECT request must only have :method and :authority pseudo-headers",
   438  		}
   439  	}
   440  	return header, pHeader, nil
   441  }
   442  
   443  func (sc *serverConn) sendGoaway() {
   444  	sc.mu.Lock()
   445  	if sc.goawaySent || sc.controlStream == nil {
   446  		sc.mu.Unlock()
   447  		return
   448  	}
   449  	sc.goawaySent = true
   450  	sc.mu.Unlock()
   451  
   452  	// No lock in this section in case writing to stream blocks. This is safe
   453  	// since sc.maxRequestStreamID is only updated when sc.goawaySent is false.
   454  	sc.controlStream.writeVarint(int64(frameTypeGoaway))
   455  	sc.controlStream.writeVarint(int64(sizeVarint(uint64(sc.maxRequestStreamID))))
   456  	sc.controlStream.writeVarint(sc.maxRequestStreamID)
   457  	sc.controlStream.Flush()
   458  }
   459  
   460  // requestShouldGoAway returns true if st has a stream ID that is equal or
   461  // greater than the ID we have sent in a GOAWAY frame, if any.
   462  func (sc *serverConn) requestShouldGoaway(st *stream) bool {
   463  	sc.mu.Lock()
   464  	defer sc.mu.Unlock()
   465  	if sc.goawaySent {
   466  		return st.stream.ID() >= sc.maxRequestStreamID
   467  	} else {
   468  		sc.maxRequestStreamID = max(sc.maxRequestStreamID, st.stream.ID())
   469  		return false
   470  	}
   471  }
   472  
   473  func (sc *serverConn) handleRequestStream(st *stream) error {
   474  	if sc.requestShouldGoaway(st) {
   475  		return &streamError{
   476  			code:    errH3RequestRejected,
   477  			message: "GOAWAY request with equal or lower ID than the stream has been sent",
   478  		}
   479  	}
   480  
   481  	readStartTime := time.Now()
   482  	if t := sc.srv.readHeaderTimeout(); t > 0 {
   483  		st.readDeadline.set(readStartTime.Add(t))
   484  	}
   485  	header, pHeader, err := sc.parseHeader(st)
   486  	if err != nil {
   487  		if errors.Is(err, os.ErrDeadlineExceeded) {
   488  			return &streamError{
   489  				code:    errH3RequestRejected,
   490  				message: "exceeded deadline while parsing header",
   491  			}
   492  		}
   493  		return err
   494  	}
   495  
   496  	if t := sc.srv.readTimeout(); t > 0 {
   497  		st.readDeadline.set(readStartTime.Add(t))
   498  	} else {
   499  		st.readDeadline.set(time.Time{})
   500  	}
   501  	reqInfo := httpcommon.NewServerRequest(httpcommon.ServerRequestParam{
   502  		Method:    pHeader.method,
   503  		Scheme:    pHeader.scheme,
   504  		Authority: pHeader.authority,
   505  		Path:      pHeader.path,
   506  		Header:    header,
   507  	})
   508  	if reqInfo.InvalidReason != "" {
   509  		return &streamError{
   510  			code:    errH3MessageError,
   511  			message: reqInfo.InvalidReason,
   512  		}
   513  	}
   514  
   515  	contentLength := int64(-1)
   516  	if n, err := strconv.ParseUint(header.Get("Content-Length"), 10, 63); err == nil {
   517  		contentLength = int64(n)
   518  	}
   519  
   520  	req := (&http.Request{
   521  		Proto:         "HTTP/3.0",
   522  		Method:        pHeader.method,
   523  		Host:          reqInfo.Host,
   524  		URL:           reqInfo.URL,
   525  		RequestURI:    reqInfo.RequestURI,
   526  		Trailer:       reqInfo.Trailer,
   527  		ProtoMajor:    3,
   528  		RemoteAddr:    sc.qconn.RemoteAddr().String(),
   529  		Header:        header,
   530  		ContentLength: contentLength,
   531  	}).WithContext(sc.baseCtx)
   532  
   533  	rw := &responseWriter{
   534  		st:             st,
   535  		headers:        make(http.Header),
   536  		trailer:        make(http.Header),
   537  		bb:             make(bodyBuffer, 0, defaultBodyBufferCap),
   538  		cannotHaveBody: req.Method == "HEAD",
   539  		bw: &bodyWriter{
   540  			st:     st,
   541  			remain: -1,
   542  			flush:  false,
   543  			name:   "response",
   544  			enc:    &sc.enc,
   545  		},
   546  	}
   547  
   548  	if contentLength != 0 || len(reqInfo.Trailer) != 0 {
   549  		req.Body = &serverRequestReader{
   550  			rw: rw,
   551  			br: bodyReader{
   552  				st:            st,
   553  				remain:        contentLength,
   554  				trailer:       reqInfo.Trailer,
   555  				filterTrailer: true,
   556  			},
   557  			needsContinue: reqInfo.NeedsContinue,
   558  		}
   559  		defer req.Body.Close()
   560  	} else {
   561  		req.Body = http.NoBody
   562  	}
   563  
   564  	// TODO: handle panic coming from the HTTP handler.
   565  	if t := sc.srv.writeTimeout(); t > 0 {
   566  		st.writeDeadline.set(time.Now().Add(t))
   567  	}
   568  	sc.handler.ServeHTTP(rw, req)
   569  	return rw.close()
   570  }
   571  
   572  // abort closes the connection with an error.
   573  func (sc *serverConn) abort(err error) {
   574  	if e, ok := err.(*connectionError); ok {
   575  		sc.qconn.Abort(&quic.ApplicationError{
   576  			Code:   uint64(e.code),
   577  			Reason: e.message,
   578  		})
   579  	} else {
   580  		sc.qconn.Abort(err)
   581  	}
   582  }
   583  
   584  // responseCanHaveBody reports whether a given response status code permits a
   585  // body. See RFC 7230, section 3.3.
   586  func responseCanHaveBody(status int) bool {
   587  	switch {
   588  	case status >= 100 && status <= 199:
   589  		return false
   590  	case status == 204:
   591  		return false
   592  	case status == 304:
   593  		return false
   594  	}
   595  	return true
   596  }
   597  
   598  // trailerPrefix is a magic prefix for [responseWriter.Header] map keys that,
   599  // if present, signals that the map entry is actually for the response
   600  // trailers, and not the response headers. See [net/http.TrailerPrefix] for
   601  // details.
   602  const trailerPrefix = "Trailer:"
   603  
   604  type responseWriter struct {
   605  	st             *stream
   606  	bw             *bodyWriter
   607  	mu             sync.Mutex
   608  	headers        http.Header
   609  	snapHeaders    http.Header // Snapshot of headers at WriteHeader time
   610  	trailer        http.Header
   611  	bb             bodyBuffer
   612  	wroteHeader    bool  // Non-1xx header has been (logically) written.
   613  	statusCode     int   // Non-1xx status of the response that will be sent in HEADERS frame. Zero means none has been set.
   614  	sent100        bool  // Status 100 has been sent by the server.
   615  	cannotHaveBody bool  // Response should not have a body (e.g. response to a HEAD request).
   616  	bodyLenLeft    int64 // How much of the content body is left to be sent, set via "Content-Length" header. -1 if unknown.
   617  }
   618  
   619  func (rw *responseWriter) Header() http.Header {
   620  	return rw.headers
   621  }
   622  
   623  // prepareTrailerForWriteLocked populates any pre-declared trailer header with
   624  // its value, and passes it to bodyWriter so it can be written after body EOF.
   625  // Caller must hold rw.mu.
   626  func (rw *responseWriter) prepareTrailerForWriteLocked() {
   627  	for name := range rw.trailer {
   628  		if val, ok := rw.headers[name]; ok {
   629  			rw.trailer[name] = val
   630  		} else {
   631  			delete(rw.trailer, name)
   632  		}
   633  	}
   634  	for name, vals := range rw.headers {
   635  		if name, found := strings.CutPrefix(name, trailerPrefix); found {
   636  			name = textproto.CanonicalMIMEHeaderKey(textproto.TrimString(name))
   637  			rw.trailer[name] = vals
   638  		}
   639  	}
   640  	if len(rw.trailer) > 0 {
   641  		rw.bw.trailer = rw.trailer
   642  	}
   643  }
   644  
   645  // writeHeaderLockedOnce writes the final response header. If rw.wroteHeader is
   646  // true, calling this method is a no-op. Sending informational status headers
   647  // should be done using writeInfoHeaderLocked, rather than this method.
   648  // Caller must hold rw.mu.
   649  func (rw *responseWriter) writeHeaderLockedOnce() {
   650  	if rw.wroteHeader {
   651  		return
   652  	}
   653  	if !responseCanHaveBody(rw.statusCode) {
   654  		rw.cannotHaveBody = true
   655  	}
   656  	// If there is any Trailer declared, save them so we know which trailers
   657  	// have been pre-declared. Also, write back the extracted value, which is
   658  	// canonicalized, for consistency.
   659  	if _, ok := rw.snapHeaders["Trailer"]; ok {
   660  		extractTrailerFromHeader(rw.snapHeaders, rw.trailer)
   661  		rw.snapHeaders.Set("Trailer", strings.Join(slices.Sorted(maps.Keys(rw.trailer)), ", "))
   662  	}
   663  
   664  	rw.bb.inferHeader(rw.snapHeaders, rw.statusCode)
   665  	encHeaders := rw.bw.enc.encode(func(f func(itype indexType, name, value string)) {
   666  		f(mayIndex, ":status", strconv.Itoa(rw.statusCode))
   667  		for name, values := range rw.snapHeaders {
   668  			if !httpguts.ValidHeaderFieldName(name) {
   669  				continue
   670  			}
   671  			for _, val := range values {
   672  				if !httpguts.ValidHeaderFieldValue(val) {
   673  					continue
   674  				}
   675  				// Issue #71374: Consider supporting never-indexed fields.
   676  				f(mayIndex, name, val)
   677  			}
   678  		}
   679  	})
   680  
   681  	rw.st.writeVarint(int64(frameTypeHeaders))
   682  	rw.st.writeVarint(int64(len(encHeaders)))
   683  	rw.st.Write(encHeaders)
   684  	rw.wroteHeader = true
   685  }
   686  
   687  // writeHeaderLocked writes informational status headers (i.e. status 1XX).
   688  // If a non-informational status header has been written via
   689  // writeHeaderLockedOnce, this method is a no-op.
   690  // Caller must hold rw.mu.
   691  func (rw *responseWriter) writeHeaderLocked(statusCode int) {
   692  	if rw.wroteHeader {
   693  		return
   694  	}
   695  	if statusCode == 100 {
   696  		if rw.sent100 {
   697  			return
   698  		}
   699  		rw.sent100 = true
   700  	}
   701  	encHeaders := rw.bw.enc.encode(func(f func(itype indexType, name, value string)) {
   702  		f(mayIndex, ":status", strconv.Itoa(statusCode))
   703  		for name, values := range rw.headers {
   704  			if name == "Content-Length" || name == "Transfer-Encoding" {
   705  				continue
   706  			}
   707  			if !httpguts.ValidHeaderFieldName(name) {
   708  				continue
   709  			}
   710  			for _, val := range values {
   711  				if !httpguts.ValidHeaderFieldValue(val) {
   712  					continue
   713  				}
   714  				// Issue #71374: Consider supporting never-indexed fields.
   715  				f(mayIndex, name, val)
   716  			}
   717  		}
   718  	})
   719  	rw.st.writeVarint(int64(frameTypeHeaders))
   720  	rw.st.writeVarint(int64(len(encHeaders)))
   721  	rw.st.Write(encHeaders)
   722  }
   723  
   724  func isInfoStatus(status int) bool {
   725  	return status >= 100 && status < 200
   726  }
   727  
   728  // checkWriteHeaderCode is a copy of net/http's checkWriteHeaderCode.
   729  func checkWriteHeaderCode(code int) {
   730  	// Issue 22880: require valid WriteHeader status codes.
   731  	// For now we only enforce that it's three digits.
   732  	// In the future we might block things over 599 (600 and above aren't defined
   733  	// at http://httpwg.org/specs/rfc7231.html#status.codes).
   734  	// But for now any three digits.
   735  	//
   736  	// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
   737  	// no equivalent bogus thing we can realistically send in HTTP/3,
   738  	// so we'll consistently panic instead and help people find their bugs
   739  	// early. (We can't return an error from WriteHeader even if we wanted to.)
   740  	if code < 100 || code > 999 {
   741  		panic(fmt.Sprintf("invalid WriteHeader code %v", code))
   742  	}
   743  }
   744  
   745  func (rw *responseWriter) WriteHeader(statusCode int) {
   746  	// TODO: handle sending informational status headers (e.g. 103).
   747  	rw.mu.Lock()
   748  	defer rw.mu.Unlock()
   749  	if rw.statusCode != 0 {
   750  		return
   751  	}
   752  	checkWriteHeaderCode(statusCode)
   753  
   754  	// Informational headers can be sent multiple times, and should be flushed
   755  	// immediately.
   756  	if isInfoStatus(statusCode) {
   757  		rw.writeHeaderLocked(statusCode)
   758  		rw.st.Flush()
   759  		return
   760  	}
   761  
   762  	// Non-informational headers should only be set once, and should be
   763  	// buffered.
   764  	if n, err := strconv.ParseUint(rw.headers.Get("Content-Length"), 10, 63); err == nil {
   765  		rw.bodyLenLeft = int64(n)
   766  	} else {
   767  		rw.headers.Del("Content-Length")
   768  		rw.bodyLenLeft = -1 // Unknown.
   769  	}
   770  	rw.statusCode = statusCode
   771  	rw.snapHeaders = rw.headers.Clone()
   772  }
   773  
   774  // trimWriteLocked trims a byte slice, b, such that the length of b will not
   775  // exceed rw.bodyLenLeft. This method will update rw.bodyLenLeft when trimming
   776  // b, and will also return whether b was trimmed or not.
   777  // Caller must hold rw.mu.
   778  func (rw *responseWriter) trimWriteLocked(b []byte) ([]byte, bool) {
   779  	if rw.bodyLenLeft < 0 {
   780  		return b, false
   781  	}
   782  	n := min(int64(len(b)), rw.bodyLenLeft)
   783  	rw.bodyLenLeft -= n
   784  	return b[:n], n != int64(len(b))
   785  }
   786  
   787  func (rw *responseWriter) Write(b []byte) (n int, err error) {
   788  	// Calling Write implicitly calls WriteHeader(200) if WriteHeader has not
   789  	// been called before.
   790  	rw.WriteHeader(http.StatusOK)
   791  	rw.mu.Lock()
   792  	defer rw.mu.Unlock()
   793  
   794  	if rw.statusCode == http.StatusNotModified {
   795  		return 0, http.ErrBodyNotAllowed
   796  	}
   797  
   798  	b, trimmed := rw.trimWriteLocked(b)
   799  	if trimmed {
   800  		defer func() {
   801  			err = http.ErrContentLength
   802  		}()
   803  	}
   804  
   805  	// If b fits entirely in our body buffer, save it to the buffer and return
   806  	// early so we can coalesce small writes.
   807  	// As a special case, we always want to save b to the buffer even when b is
   808  	// big if we had yet to write our header, so we can infer headers like
   809  	// "Content-Type" with as much information as possible.
   810  	initialBLen := len(b)
   811  	initialBufLen := len(rw.bb)
   812  	if !rw.wroteHeader || len(b) <= cap(rw.bb)-len(rw.bb) {
   813  		b = rw.bb.write(b)
   814  		if len(b) == 0 {
   815  			return initialBLen, nil
   816  		}
   817  	}
   818  
   819  	// Reaching this point means that our buffer has been sufficiently filled.
   820  	// Therefore, we now want to:
   821  	// 1. Infer and write response headers based on our body buffer, if not
   822  	// done yet.
   823  	// 2. Write our body buffer and the rest of b (if any).
   824  	// 3. Reset the current body buffer so it can be used again.
   825  	rw.writeHeaderLockedOnce()
   826  	if rw.cannotHaveBody {
   827  		return initialBLen, nil
   828  	}
   829  	if n, err := rw.bw.write(rw.bb, b); err != nil {
   830  		return max(0, n-initialBufLen), err
   831  	}
   832  	rw.bb.discard()
   833  	return initialBLen, nil
   834  }
   835  
   836  func (rw *responseWriter) SetReadDeadline(deadline time.Time) error {
   837  	rw.st.readDeadline.set(deadline)
   838  	return nil
   839  }
   840  
   841  func (rw *responseWriter) SetWriteDeadline(deadline time.Time) error {
   842  	rw.st.writeDeadline.set(deadline)
   843  	return nil
   844  }
   845  
   846  func (rw *responseWriter) EnableFullDuplex() error {
   847  	return nil
   848  }
   849  
   850  func (rw *responseWriter) Flush() { rw.FlushError() }
   851  func (rw *responseWriter) FlushError() error {
   852  	// Calling Flush implicitly calls WriteHeader(200) if WriteHeader has not
   853  	// been called before.
   854  	rw.WriteHeader(http.StatusOK)
   855  	rw.mu.Lock()
   856  	defer rw.mu.Unlock()
   857  	rw.writeHeaderLockedOnce()
   858  	if !rw.cannotHaveBody {
   859  		_, err := rw.bw.Write(rw.bb)
   860  		rw.bb.discard()
   861  		if err != nil {
   862  			return err
   863  		}
   864  	}
   865  	return rw.st.Flush()
   866  }
   867  
   868  func (rw *responseWriter) close() error {
   869  	if errors.Is(rw.st.writeDeadline.err(), os.ErrDeadlineExceeded) {
   870  		return &streamError{
   871  			code:    errH3RequestCancelled,
   872  			message: "exceeded deadline while writing response",
   873  		}
   874  	}
   875  
   876  	retErr := rw.FlushError()
   877  	rw.mu.Lock()
   878  	defer rw.mu.Unlock()
   879  	rw.prepareTrailerForWriteLocked()
   880  	if err := rw.bw.Close(); retErr == nil {
   881  		retErr = err
   882  	}
   883  	if errors.Is(retErr, os.ErrDeadlineExceeded) {
   884  		return &streamError{
   885  			code:    errH3RequestCancelled,
   886  			message: retErr.Error(),
   887  		}
   888  	}
   889  	return retErr
   890  }
   891  
   892  // defaultBodyBufferCap is the default number of bytes of body that we are
   893  // willing to save in a buffer for the sake of inferring headers and coalescing
   894  // small writes. 512 was chosen to be consistent with how much
   895  // http.DetectContentType is willing to read.
   896  const defaultBodyBufferCap = 512
   897  
   898  // bodyBuffer is a buffer used to store body content of a response.
   899  type bodyBuffer []byte
   900  
   901  // write writes b to the buffer. It returns a new slice of b, which contains
   902  // any remaining data that could not be written to the buffer, if any.
   903  func (bb *bodyBuffer) write(b []byte) []byte {
   904  	n := min(len(b), cap(*bb)-len(*bb))
   905  	*bb = append(*bb, b[:n]...)
   906  	return b[n:]
   907  }
   908  
   909  // discard resets the buffer so it can be used again.
   910  func (bb *bodyBuffer) discard() {
   911  	*bb = (*bb)[:0]
   912  }
   913  
   914  // inferHeader populates h with the header values that we can infer from our
   915  // current buffer content, if not already explicitly set. This method should be
   916  // called only once with as much body content as possible in the buffer, before
   917  // a HEADERS frame is sent, and before discard has been called. Doing so
   918  // properly is the responsibility of the caller.
   919  func (bb *bodyBuffer) inferHeader(h http.Header, status int) {
   920  	if _, ok := h["Date"]; !ok {
   921  		h.Set("Date", time.Now().UTC().Format(http.TimeFormat))
   922  	}
   923  	// If the Content-Encoding is non-blank, we shouldn't
   924  	// sniff the body. See Issue golang.org/issue/31753.
   925  	hasCE := len(h.Get("Content-Encoding")) > 0
   926  	_, hasCT := h["Content-Type"]
   927  	if !hasCE && !hasCT && responseCanHaveBody(status) && len(*bb) > 0 {
   928  		h.Set("Content-Type", http.DetectContentType(*bb))
   929  	}
   930  	// We can technically infer Content-Length too here, as long as the entire
   931  	// response body fits within hi.buf and does not require flushing. However,
   932  	// we have chosen not to do so for now as Content-Length is not very
   933  	// important for HTTP/3, and such inconsistent behavior might be confusing.
   934  }
   935  
   936  // serverRequestReader wraps around bodyReader, allowing Read and Close calls
   937  // done from within a server handler to coordinate correctly with the
   938  // responseWriter; for example, sending status 100 on Read when appropriate.
   939  type serverRequestReader struct {
   940  	rw            *responseWriter
   941  	br            bodyReader
   942  	needsContinue bool
   943  }
   944  
   945  // maybeSendContinue attempts to send a 100 Continue status code. It
   946  // ensures that status 100 will only be sent once and when appropriate. If a
   947  // non-1xx header has been set before 100 was ever set, it also ensures that
   948  // all subsequent Read will fail.
   949  func (srr *serverRequestReader) maybeSendContinue() {
   950  	if !srr.needsContinue {
   951  		return
   952  	}
   953  	srr.rw.mu.Lock()
   954  	defer srr.rw.mu.Unlock()
   955  	if srr.rw.sent100 {
   956  		return
   957  	}
   958  	if srr.rw.statusCode != 0 {
   959  		srr.br.Close()
   960  		return
   961  	}
   962  	srr.rw.writeHeaderLocked(100)
   963  	srr.rw.st.Flush()
   964  }
   965  
   966  func (srr *serverRequestReader) Read(p []byte) (int, error) {
   967  	srr.maybeSendContinue()
   968  	return srr.br.Read(p)
   969  }
   970  
   971  func (srr *serverRequestReader) Close() error {
   972  	return srr.br.Close()
   973  }
   974  

View as plain text