Source file src/net/http/internal/http2/api.go

     1  // Copyright 2026 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 http2
     6  
     7  import (
     8  	"context"
     9  	"crypto/tls"
    10  	"errors"
    11  	"io"
    12  	"log"
    13  	"mime/multipart"
    14  	"net"
    15  	"net/http/internal"
    16  	"net/textproto"
    17  	"net/url"
    18  	"time"
    19  )
    20  
    21  // Since net/http imports the http2 package, http2 cannot use any net/http types.
    22  // This file contains definitions which exist to to avoid introducing a dependency cycle.
    23  
    24  // Variables defined in net/http and initialized by an init func in that package.
    25  //
    26  // NoBody and LocalAddrContextKey have concrete types in net/http,
    27  // and therefore can't be moved into a common package without introducing
    28  // a dependency cycle.
    29  var (
    30  	NoBody              io.ReadCloser
    31  	LocalAddrContextKey any
    32  )
    33  
    34  var (
    35  	ErrAbortHandler    = internal.ErrAbortHandler
    36  	ErrBodyNotAllowed  = internal.ErrBodyNotAllowed
    37  	ErrNotSupported    = errors.ErrUnsupported
    38  	ErrSkipAltProtocol = internal.ErrSkipAltProtocol
    39  )
    40  
    41  // A ClientRequest is a Request used by the HTTP/2 client (Transport).
    42  type ClientRequest struct {
    43  	Context       context.Context
    44  	Method        string
    45  	URL           *url.URL
    46  	Header        Header
    47  	Trailer       Header
    48  	Body          io.ReadCloser
    49  	Host          string
    50  	GetBody       func() (io.ReadCloser, error)
    51  	ContentLength int64
    52  	Cancel        <-chan struct{}
    53  	Close         bool
    54  	ResTrailer    *Header
    55  
    56  	// Include the per-request stream in the ClientRequest to avoid an allocation.
    57  	stream clientStream
    58  }
    59  
    60  // Clone makes a shallow copy of ClientRequest.
    61  //
    62  // Clone is only used in shouldRetryRequest.
    63  // We can drop it if we ever get rid of or rework that function.
    64  func (req *ClientRequest) Clone() *ClientRequest {
    65  	return &ClientRequest{
    66  		Context:       req.Context,
    67  		Method:        req.Method,
    68  		URL:           req.URL,
    69  		Header:        req.Header,
    70  		Trailer:       req.Trailer,
    71  		Body:          req.Body,
    72  		Host:          req.Host,
    73  		GetBody:       req.GetBody,
    74  		ContentLength: req.ContentLength,
    75  		Cancel:        req.Cancel,
    76  		Close:         req.Close,
    77  		ResTrailer:    req.ResTrailer,
    78  	}
    79  }
    80  
    81  // A ClientResponse is a Request used by the HTTP/2 client (Transport).
    82  type ClientResponse struct {
    83  	Status        string // e.g. "200"
    84  	StatusCode    int    // e.g. 200
    85  	ContentLength int64
    86  	Uncompressed  bool
    87  	Header        Header
    88  	Trailer       Header
    89  	Body          io.ReadCloser
    90  	TLS           *tls.ConnectionState
    91  }
    92  
    93  type Header = textproto.MIMEHeader
    94  
    95  // TransportConfig is configuration from an http.Transport.
    96  type TransportConfig interface {
    97  	MaxHeaderListSize() int64
    98  	MaxResponseHeaderBytes() int64
    99  	DisableCompression() bool
   100  	DisableKeepAlives() bool
   101  	ExpectContinueTimeout() time.Duration
   102  	ResponseHeaderTimeout() time.Duration
   103  	IdleConnTimeout() time.Duration
   104  	HTTP2Config() Config
   105  }
   106  
   107  // ServerConfig is configuration from an http.Server.
   108  type ServerConfig interface {
   109  	MaxHeaderBytes() int
   110  	MaxHeaderValueCount() int
   111  	ConnState(net.Conn, ConnState)
   112  	DoKeepAlives() bool
   113  	WriteTimeout() time.Duration
   114  	SendPingTimeout() time.Duration
   115  	ErrorLog() *log.Logger
   116  	ReadTimeout() time.Duration
   117  	HTTP2Config() Config
   118  	DisableClientPriority() bool
   119  	IdleTimeout() time.Duration
   120  }
   121  
   122  type Handler interface {
   123  	ServeHTTP(*ResponseWriter, *ServerRequest)
   124  }
   125  
   126  type ResponseWriter = responseWriter
   127  
   128  type PushOptions struct {
   129  	Method string
   130  	Header Header
   131  }
   132  
   133  // A ServerRequest is a Request used by the HTTP/2 server.
   134  type ServerRequest struct {
   135  	Context       context.Context
   136  	Proto         string // e.g. "HTTP/1.0"
   137  	ProtoMajor    int    // e.g. 1
   138  	ProtoMinor    int    // e.g. 0
   139  	Method        string
   140  	URL           *url.URL
   141  	Header        Header
   142  	Trailer       Header
   143  	Body          io.ReadCloser
   144  	Host          string
   145  	ContentLength int64
   146  	RemoteAddr    string
   147  	RequestURI    string
   148  	TLS           *tls.ConnectionState
   149  	MultipartForm *multipart.Form
   150  }
   151  
   152  // ConnState is identical to net/http.ConnState.
   153  type ConnState int
   154  
   155  const (
   156  	ConnStateNew ConnState = iota
   157  	ConnStateActive
   158  	ConnStateIdle
   159  	ConnStateHijacked
   160  	ConnStateClosed
   161  )
   162  

View as plain text