Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultRequestToHTTP(ctx context.Context, br *Request) (*http.Request, error)
- func MapToHTTP1Response(br *Response) (*http.Response, error)
- func RequestToHTTP1(ctx context.Context, br *Request, usingProxy, serverSide bool) (*http.Request, error)
- type InformationalResponse
- type InvalidMessageError
- type Message
- type Request
- type RequestControlData
- type RequestDecoder
- type RequestEncoder
- type RequestFromHTTP
- type RequestToHTTP
- type Response
- type ResponseDecoder
- type ResponseEncoder
- type ResponseFromHTTP
- type ResponseToHTTP
Constants ¶
const DefaultEncodingBufferLen = 4096
DefaultEncodingBufferLen is the default buffer length
Variables ¶
var (
ErrTooMuchData = errors.New("too much data")
)
Functions ¶
func DefaultRequestToHTTP ¶
DefaultRequestToHTTP is the default request mapping function used by the request decoder. It interprets BHTTP requests as unproxied HTTP/1.1 server side requests.
func MapToHTTP1Response ¶
MapToHTTP1Response maps a bhttp Response to a net/http Response
Types ¶
type InformationalResponse ¶
type InformationalResponse struct {
StatusCode int
// Header contains the headers as FieldLines for the BHTTP encoded message. This
// usually differs from what a net/http response contains, as Go moves some headers
// to/from fields. For example, the Host and Content-Length headers.
Header http.Header
}
InformationalResponse contains control data and header of an informational response response.
type InvalidMessageError ¶
type InvalidMessageError struct {
Err error
}
func (InvalidMessageError) Error ¶
func (e InvalidMessageError) Error() string
func (InvalidMessageError) Unwrap ¶
func (e InvalidMessageError) Unwrap() error
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
func (*Message) IsIndeterminateLength ¶
func (*Message) IsKnownLength ¶
func (*Message) IsResponse ¶
type Request ¶
type Request struct {
// KnownLength indicates whether the request should/is encoded as a known or indeterminate
// length message.
KnownLength bool
// ContentLength is the Body length of a known length request. Ignored for indeterminate length messages.
ContentLength int64
// ControlData contains the RequestControlData. This data is passed as-is to the encoded/decoded messages,
// mapping functions should take extra care to validate this data.
ControlData RequestControlData
// Header contains the headers that will be encoded to the BHTTP message.
//
// This might differ from what the original net/http request contains as net/http manages a few headers
// behind the scenes.
//
// For example, while net/http takes the Content-Length header from the .ContentLength field on the original
// request, the Content-Length header should be set in this map (if it's used).
Header http.Header
// Body usually reads the body of the original net/http request.
Body io.Reader
// Trailer works similar to the http.Request.Trailer field. It is up the MapFunc to:
// - Initialize the keys of the Trailer map.
// - Ensure that an appropriate Trailer header is added to the Header field.
// - The values of the Trailer map are set when Body returns io.EOF.
Trailer http.Header
}
Request is the BHTTP representation of a net/http request that the encoders and decoders work with.
func DefaultRequestFromHTTP ¶
DefaultRequestFromHTTP is the default request mapping function used by the request encoder. It interprets net/http requests as unproxied HTTP/1.1 client side requests.
func RequestFromHTTP1 ¶
RequestFromHTTP1 interprets the provided net/http request as a HTTP/1.1 request and maps it to a bhttp Request.
Just like net/http this method will automatically handle a few headers when appropriate: - Host: always set based on .Host or .URL.Host. Matches :authority request control data. - Content-Length: for known length requests.
Note: Unless the caller explicitly sets .TransferEncoding, we don't add it.
type RequestControlData ¶
type RequestControlData struct {
// Method contains the :method pseudo-header according to RFC9113 Section 8.1.2.3.
Method []byte
// Scheme contains the :scheme pseudo-header according to RFC9113 Section 8.1.2.3.
// Note: BHTTP RFC makes this required, so we can't omit it like in HTTP 2.
Scheme []byte
// Authority contains the :authority pseudo-header according to RFC9113 Section 8.1.2.3.
// Note: BHTTP RFC requires us to provide an empty encoding when omitted.
Authority []byte
// Path contains the :scheme pseudo-header according to RFC9113 Section 8.1.2.3.
// Note: BHTTP RFC makes this required, so we can't omit it like in HTTP 2.
Path []byte
}
RequestControlData contains the control data for a request.
RFC9292 (BHTTP): The values of these fields follow the rules in HTTP/2 (Section 8.3.1 of HTTP/2) that apply to the ":method", ":scheme", ":authority", and ":path" pseudo-header fields, respectively. However, where the ":authority" pseudo-header field might be omitted in HTTP/2, a zero-length value is encoded instead.
Note: BHTTP RFC only specifies to encode a zero-length value when the authority field is omitted. However, in HTTP/2 :scheme and :path can also be omitted. We'll encode zero length values for these cases as well.
RFC9113 8.1.2.3: https://www.rfc-editor.org/rfc/rfc9113#name-request-pseudo-header-field
type RequestDecoder ¶
type RequestDecoder struct {
// MaxHeaderBytes is the maximum number of header bytes that can be read. Will default to 16KB.
MaxHeaderBytes int64
// MapFunc determines how a BHTTP request is interpreted. If this field is nil, the decoder will default
// to interpreting the BHTTP request as a an unproxied HTTP/1.1 server side request.
MapFunc RequestToHTTP
}
RequestDecoder decodes a BHTTP message to a net/http request.
An empty decoder is safe to use and is the recommended way to construct a new request decoder. An empty request decoder will: - Interpret the incoming BHTTP messages as unproxied HTTP/1.1 server side requests. - Allow for header sections of up to 16KB.
func (*RequestDecoder) DecodeRequest ¶
DecodeRequest decodes a request from the provided reader. The context of the request will be set to ctx.
type RequestEncoder ¶
type RequestEncoder struct {
// MapFunc maps a net/http Request to a BHTTP response. If this field is nil, [DefaultRequestFromHTTP] will be used
// which interprets net/http requests as unproxied HTTP/1.1 client side requests.
MapFunc RequestFromHTTP
// PadToMultipleOf pads the message with zeroes until it reaches a multiple of this number. 0 will add no padding.
PadToMultipleOf uint64
// MaxEncodedChunkLen is the maximum length of indeterminate length content chunks (including their length prefix). MaxEncodedChunkLen
// should be at least 2 bytes so that it will always fit a quicencoded integer with some data. If this field is 0, it
// will default to 4096.
MaxEncodedChunkLen int
// contains filtered or unexported fields
}
RequestEncoder encodes net/http requests to bhttp messages.
An empty encoder is safe to use and is the recommended way to construct a new request encoder. An empty request encoder will:
- Interpret the net/http requests as unproxied HTTP/1.1 client side requests.
- Encode an indeterminate-length message where net/http would use chunked transfer encoding. BHTTP Message chunks will at most be 4096 bytes in length.
- Not including padding in the message.
If you need different encoding logic, use NewKnownLengthRequestEncoder, NewIndeterminateLengthRequestEncoder or create a custom RequestEncoder by setting the fields below.
func NewIndeterminateLengthRequestEncoder ¶
func NewIndeterminateLengthRequestEncoder() *RequestEncoder
NewIndeterminateLengthRequestEncoder returns an encoder that will encode all requests as indeterminate-length BHTTP messages, regardless of what the net/http request looks like.
Even requests where the Content-Length is known will be encoded as an indeterminate-length BHTTP message.
func NewKnownLengthRequestEncoder ¶
func NewKnownLengthRequestEncoder() *RequestEncoder
NewKnownLengthRequestEncoder returns an encoder that will encode all requests as known-length BHTTP messages, regardless of what the the net/http request looks like. Even requests that would normally use Transfer-Encoding: chunked will be encoded as known-length BHTTP messages.
Note: this encoder might read the full body of the request into memory to determine its exact length.
func (*RequestEncoder) EncodeRequest ¶
func (e *RequestEncoder) EncodeRequest(hr *http.Request) (*Message, error)
EncodeRequests encodes the provided net/http request as an bhttp message. The exact interpretation of the request depends on the MapFunc of this RequestEncoder, see that type for more details.
If this request has a body, the encoder will encode the body until EOF is encountered. It is the responsibility of the caller to close the body.
type RequestFromHTTP ¶
RequestFromHTTP maps a net/http request to a Request.
type RequestToHTTP ¶
RequestToHTTP maps a Request to a net/http request.
type Response ¶
type Response struct {
// KnownLength indicates whether the response should/is encoded as a known or indeterminate
// length message.
KnownLength bool
// ContentLength is the Body length of a known length request. Ignored for indeterminate length messages.
ContentLength int64
// Informational contains an array of InformationalResponses (1xx status codes and attendant headers)
// They do not map to go Response objects, so would need to be manually encoded.
Informational []InformationalResponse
// FinalStatusCode contains the 2xx-5xx status code of the final response.
FinalStatusCode int
// FinalHeader contains the headers for the final response.
//
// This might differ from what the original net/http response contains as net/http manages a few headers
// behind the scenes.
//
// For example, while net/http takes the Content-Length header from the .ContentLength field on the original response, the
// Content-Length header should be set in this map (if it's used).
FinalHeader http.Header
// Body usually reads the body of the original net/http response.
Body io.Reader
// Trailer works similar to the http.Response.Trailer field. It is up the MapFunc to:
// - Initialize the keys of the Trailer map.
// - Ensure that an appropriate Trailer header is added to the Header field.
// - The values of the Trailer map are set when Body returns io.EOF.
Trailer http.Header
}
Response is the bhttp representation of a net/http response that the encoders/decoders work with.
A BHTTP encoded response represents a "resolved response", so it actually represents 0 or more informational 1xx responses followed by a final 2xx-5xx response.
func DefaultResponseFromHTTP ¶
DefaultResponseFromHTTP interpret the net/http response as an unproxied HTTP/1.1 client side responses.
func MapFromHTTP1Response ¶
MapFromHTTP1Response interprets the provided net/http response as a HTTP/1.1 response and maps it to a bhttp Response.
It will never set informational responses on the BHTTP response, as this information is not available in a net/http response.
Note: DetermineResponseContentLength might consume the first byte of the reader on http.Response to check if the body actually contains data. If you don't want hr to be modified, be sure to pass in a clone.
type ResponseDecoder ¶
type ResponseDecoder struct {
// MaxHeaderBytes is the maximum number of header bytes that can be read. Will default to 16KB.
MaxHeaderBytes int64
// MapFunc maps a [*Response] to a net/http Response. If this field is nil, [*Response] will be mapped to a
// server-side, unproxied response.
MapFunc ResponseToHTTP
}
func (*ResponseDecoder) DecodeResponse ¶
DecodeResponse decodes a response from the provided reader. The context of the response will be set to ctx.
type ResponseEncoder ¶
type ResponseEncoder struct {
// MapFunc maps a net/http response . If this field is nil, DefaultResponseMapFunc will be used.
MapFunc ResponseFromHTTP
// PadToMultipleOf pads the message with zeroes until it reaches a multiple of this number. 0 will add no padding.
PadToMultipleOf uint64
// MaxEncodedChunkLen is the maximum length of indeterminate length content chunks (including their length prefix). MaxEncodedChunkLen
// should be at least 2 bytes so that it will always fit a quicencoded integer with some data. If this field is 0, it
// will default to 4096.
MaxEncodedChunkLen int
// contains filtered or unexported fields
}
ResponseEncoder encodes net/http responses to bhttp messages.
An empty encoder is safe to use and is the recommended way to construct a new response encoder. An empty request response will:
- Interpret the net/http responses as unproxied HTTP/1.1 client side responses.
- Encode an indeterminate-length message where net/http would use chunked transfer encoding. BHTTP Message chunks will at most be 4096 bytes in length.
- Not including padding in the message.
If you need different encoding logic, use NewKnownLengthResponseEncoder, NewIndeterminateLengthResponseEncoder or create a custom ResponseEncoder by setting the fields below.
func NewIndeterminateLengthResponseEncoder ¶
func NewIndeterminateLengthResponseEncoder() *ResponseEncoder
NewIndeterminateLengthResponseEncoder returns an encoder that will encode all requests as indeterminate-length BHTTP messages, regardless of what the net/http request looks like.
Even requests where the Content-Length is known will be encoded as an indeterminate-length BHTTP message.
func NewKnownLengthResponseEncoder ¶
func NewKnownLengthResponseEncoder() *ResponseEncoder
NewKnownLengthResponseEncoder returns an encoder that will encode all responses as known-length BHTTP messages, regardless of what the the net/http response looks like. Even responses that would normally use Transfer-Encoding: chunked will be encoded as known-length BHTTP messages.
Note: this encoder might read the full body of the response into memory to determine its exact length.
func (*ResponseEncoder) EncodeResponse ¶
func (e *ResponseEncoder) EncodeResponse(hr *http.Response) (*Message, error)
EncodeResponse encodes the provided net/http response as an bhttp message. The exact interpretation of the request depends on the MapFunc of this [ResponseEncoding], see that type for more details.
If this response has a body, the encoder will encode the body until EOF is encountered. It is the responsibility of the caller to close the body.
type ResponseFromHTTP ¶
ResponseFromHTTP maps a net/http response to a Response.