h2

package
v0.0.0-...-e070eb6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const ConnectOverrideHeader = "x-host"

Variables

This section is empty.

Functions

func CopyRequestHeaders

func CopyRequestHeaders(dst, src http.Header)

used in createUpstreamRequetst to copy the headers to the new req.

func CopyResponseHeaders

func CopyResponseHeaders(dst, src http.Header)

Also used in httpproxy_capture, for forward http proxy

func CreateUpstreamRequest

func CreateUpstreamRequest(rw http.ResponseWriter, r *http.Request) *http.Request

CreateUpstremRequest shallow-copies r into a new request that can be sent upstream.

Derived from reverseproxy.go in the standard Go httputil package.

Use with a roundtripper - HTTP1.1 or H2C or HTTPS, followed by SendBackResponse

func SendBackResponse

func SendBackResponse(w http.ResponseWriter, r *http.Request,
	res *http.Response, err error)

Used by both ForwardHTTP and ForwardMesh, after RoundTrip is done. Will copy response headers and body

Types

type H2

type H2 struct {
	//MaxHandlers:                  0,
	//MaxConcurrentStreams:         0,
	//MaxDecoderHeaderTableSize:    0,
	//MaxEncoderHeaderTableSize:    0,
	//MaxReadFrameSize:             0,
	//PermitProhibitedCipherSuites: false,
	//IdleTimeout:                  0,
	//MaxUploadBufferPerConnection: 0,
	//MaxUploadBufferPerStream:     0,
	//NewWriteScheduler:            nil,
	//CountError:                   nil,
	// Addr - included
	http.Server

	NetListener net.Listener

	// The key is a route as defined by go ServerMux.
	// The value can be:
	// - a URL - in which case it's a reverse proxy
	// - a string that is a resource name - in which case it's a Handler
	// Other values like TCP proxy can be defined later.
	Routes map[string]string

	// The actual mux that is configured. Will be mapped to a H2C/H1 server by
	// default, assuming ambient or secure network.
	Mux *http.ServeMux `json:-`

	SSHStreamHandler func(net.Conn) error

	// Client side
	DialMeta    func(context.Context, string, string) (io.ReadWriteCloser, error)
	TokenSource TokenSource
	// ResourceStore is used to resolve resources, is a registry of types and
	// objects. We're looking for handlers.
	ResourceStore ResourceStore `json:-`
}

H2 is the HTTP/2 transport. It handles incoming http requests as mux and may be used as a http server as well.

As a transport it can accept and dial connections, with proxy support.

curl localhost:9080/debug/vars --http2-prior-knowledge

func (*H2) DialContext

func (st *H2) DialContext(ctx context.Context, net, addr string) (net.Conn, error)

func (*H2) InitMux

func (st *H2) InitMux(mux *http.ServeMux)

InitMux add the H2 functions on a mux.

func (*H2) Provision

func (r *H2) Provision(ctx context.Context) error

func (*H2) Start

func (r *H2) Start() error

func (*H2) WithResourceStore

func (r *H2) WithResourceStore(rs ResourceStore)

type H2C

type H2C struct {
	http.Transport
}

Can't do h2c using the std client - need custom code.

func (*H2C) Provision

func (h *H2C) Provision(ctx context.Context) error

type ResourceStore

type ResourceStore interface {
	Resource(ctx context.Context, name string) (any, error)
}

type Stats

type Stats struct {
	Open time.Time

	// last receive from local (and send to remote)
	LastWrite time.Time

	// last receive from remote (and send to local)
	LastRead time.Time

	// Sent from client to server ( client is initiator of the proxy )
	SentBytes   int
	SentPackets int

	// Received from server to client
	RcvdBytes   int
	RcvdPackets int
}

Stats holds telemetry for a stream or peer.

type StreamHttpClient

type StreamHttpClient struct {
	StreamState

	Request  *http.Request
	Response *http.Response

	RequestInPipe io.WriteCloser
}

func NewStreamH2

func NewStreamH2(ctx context.Context, hc *http.Client, addr string, tcpaddr string, mds TokenSource) (*StreamHttpClient, error)

NewStreamH2 creates a H2 stream using POST.

Will use the token provider if not nil.

func (*StreamHttpClient) Close

func (s *StreamHttpClient) Close() error

func (*StreamHttpClient) CloseWrite

func (s *StreamHttpClient) CloseWrite() error

func (*StreamHttpClient) Header

func (s *StreamHttpClient) Header() http.Header

func (*StreamHttpClient) LocalAddr

func (s *StreamHttpClient) LocalAddr() net.Addr

func (*StreamHttpClient) Read

func (s *StreamHttpClient) Read(b []byte) (n int, err error)

func (*StreamHttpClient) RemoteAddr

func (s *StreamHttpClient) RemoteAddr() net.Addr

func (*StreamHttpClient) RequestHeader

func (s *StreamHttpClient) RequestHeader() http.Header

func (*StreamHttpClient) SetDeadline

func (s *StreamHttpClient) SetDeadline(t time.Time) error

func (*StreamHttpClient) SetReadDeadline

func (s *StreamHttpClient) SetReadDeadline(t time.Time) error

func (*StreamHttpClient) SetWriteDeadline

func (s *StreamHttpClient) SetWriteDeadline(t time.Time) error

func (*StreamHttpClient) State

func (s *StreamHttpClient) State() *StreamState

func (*StreamHttpClient) Write

func (s *StreamHttpClient) Write(b []byte) (n int, err error)

type StreamHttpServer

type StreamHttpServer struct {
	StreamState
	Request        *http.Request
	TLS            *tls.ConnectionState
	ResponseWriter http.ResponseWriter

	// If set, the function will be called when Close() is called.
	ReadCloser func()
}

StreamHttpServer implements net.Conn on top of a H2 stream.

func NewStreamServerRequest

func NewStreamServerRequest(r *http.Request, w http.ResponseWriter) *StreamHttpServer

Create a new stream from a HTTP request/response.

For accepted requests, http2/server.go newWriterAndRequests populates the request based on the headers. Server validates method, path and scheme=http|https. Req.Body is a pipe - similar with what we use for egress. Request context is based on stream context, which is a 'with cancel' based on the serverConn baseCtx.

func (*StreamHttpServer) Close

func (s *StreamHttpServer) Close() error

func (*StreamHttpServer) CloseWrite

func (s *StreamHttpServer) CloseWrite() error

func (*StreamHttpServer) Context

func (s *StreamHttpServer) Context() context.Context

func (*StreamHttpServer) Header

func (s *StreamHttpServer) Header() http.Header

func (*StreamHttpServer) LocalAddr

func (s *StreamHttpServer) LocalAddr() net.Addr

func (*StreamHttpServer) Read

func (s *StreamHttpServer) Read(b []byte) (n int, err error)

func (*StreamHttpServer) RemoteAddr

func (s *StreamHttpServer) RemoteAddr() net.Addr

func (*StreamHttpServer) RequestHeader

func (s *StreamHttpServer) RequestHeader() http.Header

func (*StreamHttpServer) SetDeadline

func (s *StreamHttpServer) SetDeadline(t time.Time) error

func (*StreamHttpServer) SetReadDeadline

func (s *StreamHttpServer) SetReadDeadline(t time.Time) error

func (*StreamHttpServer) SetWriteDeadline

func (s *StreamHttpServer) SetWriteDeadline(t time.Time) error

func (*StreamHttpServer) State

func (s *StreamHttpServer) State() *StreamState

func (*StreamHttpServer) TLSConnectionState

func (s *StreamHttpServer) TLSConnectionState() *tls.ConnectionState

TLSConnectionState implements the tls.Conn interface. By default uses the request TLS state, but can be replaced with a synthetic one (for example with ztunnel or other split TLS).

func (*StreamHttpServer) Write

func (s *StreamHttpServer) Write(b []byte) (n int, err error)

type StreamState

type StreamState struct {

	// It is the key in the Active table.
	// Streams may also have local ids associated with the transport.
	StreamId string

	// WritErr indicates that Write failed - timeout or a RST closing the stream.
	WriteErr error `json:"-"`
	// ReadErr, if not nil, indicates that Read() failed - connection was closed with RST
	// or timedout instead of FIN
	ReadErr error `json:"-"`

	Stats

	// Original or infered destination.
	Dest string
}

StreamState provides metadata about a stream.

It includes errors, stats, other metadata. The Stream interface wraps a net.Conn with context and state.

type TokenSource

type TokenSource interface {
	// GetToken for a given audience.
	GetToken(context.Context, string) (string, error)
}

TokenSource is a common interface for anything returning Bearer or other kind of tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL