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  	MaxResponseHeaderBytes() int64
    98  	DisableCompression() bool
    99  	DisableKeepAlives() bool
   100  	ExpectContinueTimeout() time.Duration
   101  	ResponseHeaderTimeout() time.Duration
   102  	IdleConnTimeout() time.Duration
   103  	HTTP2Config() Config
   104  }
   105  
   106  // ServerConfig is configuration from an http.Server.
   107  type ServerConfig interface {
   108  	MaxHeaderBytes() int
   109  	ConnState(net.Conn, ConnState)
   110  	DoKeepAlives() bool
   111  	WriteTimeout() time.Duration
   112  	SendPingTimeout() time.Duration
   113  	ErrorLog() *log.Logger
   114  	ReadTimeout() time.Duration
   115  	HTTP2Config() Config
   116  	DisableClientPriority() bool
   117  }
   118  
   119  type Handler interface {
   120  	ServeHTTP(*ResponseWriter, *ServerRequest)
   121  }
   122  
   123  type ResponseWriter = responseWriter
   124  
   125  type PushOptions struct {
   126  	Method string
   127  	Header Header
   128  }
   129  
   130  // A ServerRequest is a Request used by the HTTP/2 server.
   131  type ServerRequest struct {
   132  	Context       context.Context
   133  	Proto         string // e.g. "HTTP/1.0"
   134  	ProtoMajor    int    // e.g. 1
   135  	ProtoMinor    int    // e.g. 0
   136  	Method        string
   137  	URL           *url.URL
   138  	Header        Header
   139  	Trailer       Header
   140  	Body          io.ReadCloser
   141  	Host          string
   142  	ContentLength int64
   143  	RemoteAddr    string
   144  	RequestURI    string
   145  	TLS           *tls.ConnectionState
   146  	MultipartForm *multipart.Form
   147  }
   148  
   149  // ConnState is identical to net/http.ConnState.
   150  type ConnState int
   151  
   152  const (
   153  	ConnStateNew ConnState = iota
   154  	ConnStateActive
   155  	ConnStateIdle
   156  	ConnStateHijacked
   157  	ConnStateClosed
   158  )
   159  

View as plain text