forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/net/http2/frame.go

2959 lines
49 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2014 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package http2
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"strings"
"sync"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/http2/hpack"
)
const frameHeaderLen = 9
var padZeros = make([]byte, 255) // zeros for padding
// A FrameType is a registered frame type as defined in
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7540.html#rfc.section.11.2
2024-02-18 10:42:21 +00:00
type FrameType uint8
const (
FrameData FrameType = 0x0
FrameHeaders FrameType = 0x1
FramePriority FrameType = 0x2
FrameRSTStream FrameType = 0x3
FrameSettings FrameType = 0x4
FramePushPromise FrameType = 0x5
FramePing FrameType = 0x6
FrameGoAway FrameType = 0x7
2024-02-18 10:42:21 +00:00
FrameWindowUpdate FrameType = 0x8
2024-02-18 10:42:21 +00:00
FrameContinuation FrameType = 0x9
)
var frameName = map[FrameType]string{
FrameData: "DATA",
FrameHeaders: "HEADERS",
FramePriority: "PRIORITY",
FrameRSTStream: "RST_STREAM",
FrameSettings: "SETTINGS",
FramePushPromise: "PUSH_PROMISE",
FramePing: "PING",
FrameGoAway: "GOAWAY",
2024-02-18 10:42:21 +00:00
FrameWindowUpdate: "WINDOW_UPDATE",
2024-02-18 10:42:21 +00:00
FrameContinuation: "CONTINUATION",
}
func (t FrameType) String() string {
2024-02-18 10:42:21 +00:00
if s, ok := frameName[t]; ok {
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t))
2024-02-18 10:42:21 +00:00
}
// Flags is a bitmask of HTTP/2 flags.
2024-02-18 10:42:21 +00:00
// The meaning of flags varies depending on the frame type.
2024-02-18 10:42:21 +00:00
type Flags uint8
// Has reports whether f contains all (0 or more) flags in v.
2024-02-18 10:42:21 +00:00
func (f Flags) Has(v Flags) bool {
2024-02-18 10:42:21 +00:00
return (f & v) == v
2024-02-18 10:42:21 +00:00
}
// Frame-specific FrameHeader flag bits.
2024-02-18 10:42:21 +00:00
const (
2024-02-18 10:42:21 +00:00
// Data Frame
2024-02-18 10:42:21 +00:00
FlagDataEndStream Flags = 0x1
FlagDataPadded Flags = 0x8
2024-02-18 10:42:21 +00:00
// Headers Frame
FlagHeadersEndStream Flags = 0x1
2024-02-18 10:42:21 +00:00
FlagHeadersEndHeaders Flags = 0x4
FlagHeadersPadded Flags = 0x8
FlagHeadersPriority Flags = 0x20
2024-02-18 10:42:21 +00:00
// Settings Frame
2024-02-18 10:42:21 +00:00
FlagSettingsAck Flags = 0x1
// Ping Frame
2024-02-18 10:42:21 +00:00
FlagPingAck Flags = 0x1
// Continuation Frame
2024-02-18 10:42:21 +00:00
FlagContinuationEndHeaders Flags = 0x4
FlagPushPromiseEndHeaders Flags = 0x4
FlagPushPromisePadded Flags = 0x8
2024-02-18 10:42:21 +00:00
)
var flagName = map[FrameType]map[Flags]string{
2024-02-18 10:42:21 +00:00
FrameData: {
2024-02-18 10:42:21 +00:00
FlagDataEndStream: "END_STREAM",
FlagDataPadded: "PADDED",
2024-02-18 10:42:21 +00:00
},
2024-02-18 10:42:21 +00:00
FrameHeaders: {
FlagHeadersEndStream: "END_STREAM",
2024-02-18 10:42:21 +00:00
FlagHeadersEndHeaders: "END_HEADERS",
FlagHeadersPadded: "PADDED",
FlagHeadersPriority: "PRIORITY",
2024-02-18 10:42:21 +00:00
},
2024-02-18 10:42:21 +00:00
FrameSettings: {
2024-02-18 10:42:21 +00:00
FlagSettingsAck: "ACK",
},
2024-02-18 10:42:21 +00:00
FramePing: {
2024-02-18 10:42:21 +00:00
FlagPingAck: "ACK",
},
2024-02-18 10:42:21 +00:00
FrameContinuation: {
2024-02-18 10:42:21 +00:00
FlagContinuationEndHeaders: "END_HEADERS",
},
2024-02-18 10:42:21 +00:00
FramePushPromise: {
2024-02-18 10:42:21 +00:00
FlagPushPromiseEndHeaders: "END_HEADERS",
FlagPushPromisePadded: "PADDED",
2024-02-18 10:42:21 +00:00
},
}
// a frameParser parses a frame given its FrameHeader and payload
2024-02-18 10:42:21 +00:00
// bytes. The length of payload will always equal fh.Length (which
2024-02-18 10:42:21 +00:00
// might be 0).
2024-02-18 10:42:21 +00:00
type frameParser func(fc *frameCache, fh FrameHeader, countError func(string), payload []byte) (Frame, error)
var frameParsers = map[FrameType]frameParser{
FrameData: parseDataFrame,
FrameHeaders: parseHeadersFrame,
FramePriority: parsePriorityFrame,
FrameRSTStream: parseRSTStreamFrame,
FrameSettings: parseSettingsFrame,
FramePushPromise: parsePushPromise,
FramePing: parsePingFrame,
FrameGoAway: parseGoAwayFrame,
2024-02-18 10:42:21 +00:00
FrameWindowUpdate: parseWindowUpdateFrame,
2024-02-18 10:42:21 +00:00
FrameContinuation: parseContinuationFrame,
}
func typeFrameParser(t FrameType) frameParser {
2024-02-18 10:42:21 +00:00
if f := frameParsers[t]; f != nil {
2024-02-18 10:42:21 +00:00
return f
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return parseUnknownFrame
2024-02-18 10:42:21 +00:00
}
// A FrameHeader is the 9 byte header of all HTTP/2 frames.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#FrameHeader
2024-02-18 10:42:21 +00:00
type FrameHeader struct {
valid bool // caller can access []byte fields in the Frame
// Type is the 1 byte frame type. There are ten standard frame
2024-02-18 10:42:21 +00:00
// types, but extension frame types may be written by WriteRawFrame
2024-02-18 10:42:21 +00:00
// and will be returned by ReadFrame (as UnknownFrame).
2024-02-18 10:42:21 +00:00
Type FrameType
// Flags are the 1 byte of 8 potential bit flags per frame.
2024-02-18 10:42:21 +00:00
// They are specific to the frame type.
2024-02-18 10:42:21 +00:00
Flags Flags
// Length is the length of the frame, not including the 9 byte header.
2024-02-18 10:42:21 +00:00
// The maximum size is one byte less than 16MB (uint24), but only
2024-02-18 10:42:21 +00:00
// frames up to 16KB are allowed without peer agreement.
2024-02-18 10:42:21 +00:00
Length uint32
// StreamID is which stream this frame is for. Certain frames
2024-02-18 10:42:21 +00:00
// are not stream-specific, in which case this field is 0.
2024-02-18 10:42:21 +00:00
StreamID uint32
}
// Header returns h. It exists so FrameHeaders can be embedded in other
2024-02-18 10:42:21 +00:00
// specific frame types and implement the Frame interface.
2024-02-18 10:42:21 +00:00
func (h FrameHeader) Header() FrameHeader { return h }
func (h FrameHeader) String() string {
2024-02-18 10:42:21 +00:00
var buf bytes.Buffer
2024-02-18 10:42:21 +00:00
buf.WriteString("[FrameHeader ")
2024-02-18 10:42:21 +00:00
h.writeDebug(&buf)
2024-02-18 10:42:21 +00:00
buf.WriteByte(']')
2024-02-18 10:42:21 +00:00
return buf.String()
2024-02-18 10:42:21 +00:00
}
func (h FrameHeader) writeDebug(buf *bytes.Buffer) {
2024-02-18 10:42:21 +00:00
buf.WriteString(h.Type.String())
2024-02-18 10:42:21 +00:00
if h.Flags != 0 {
2024-02-18 10:42:21 +00:00
buf.WriteString(" flags=")
2024-02-18 10:42:21 +00:00
set := 0
2024-02-18 10:42:21 +00:00
for i := uint8(0); i < 8; i++ {
2024-02-18 10:42:21 +00:00
if h.Flags&(1<<i) == 0 {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
set++
2024-02-18 10:42:21 +00:00
if set > 1 {
2024-02-18 10:42:21 +00:00
buf.WriteByte('|')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
name := flagName[h.Type][Flags(1<<i)]
2024-02-18 10:42:21 +00:00
if name != "" {
2024-02-18 10:42:21 +00:00
buf.WriteString(name)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
fmt.Fprintf(buf, "0x%x", 1<<i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if h.StreamID != 0 {
2024-02-18 10:42:21 +00:00
fmt.Fprintf(buf, " stream=%d", h.StreamID)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fmt.Fprintf(buf, " len=%d", h.Length)
2024-02-18 10:42:21 +00:00
}
func (h *FrameHeader) checkValid() {
2024-02-18 10:42:21 +00:00
if !h.valid {
2024-02-18 10:42:21 +00:00
panic("Frame accessor called on non-owned Frame")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (h *FrameHeader) invalidate() { h.valid = false }
// frame header bytes.
2024-02-18 10:42:21 +00:00
// Used only by ReadFrameHeader.
2024-02-18 10:42:21 +00:00
var fhBytes = sync.Pool{
2024-02-18 10:42:21 +00:00
New: func() interface{} {
2024-02-18 10:42:21 +00:00
buf := make([]byte, frameHeaderLen)
2024-02-18 10:42:21 +00:00
return &buf
2024-02-18 10:42:21 +00:00
},
}
// ReadFrameHeader reads 9 bytes from r and returns a FrameHeader.
2024-02-18 10:42:21 +00:00
// Most users should use Framer.ReadFrame instead.
2024-02-18 10:42:21 +00:00
func ReadFrameHeader(r io.Reader) (FrameHeader, error) {
2024-02-18 10:42:21 +00:00
bufp := fhBytes.Get().(*[]byte)
2024-02-18 10:42:21 +00:00
defer fhBytes.Put(bufp)
2024-02-18 10:42:21 +00:00
return readFrameHeader(*bufp, r)
2024-02-18 10:42:21 +00:00
}
func readFrameHeader(buf []byte, r io.Reader) (FrameHeader, error) {
2024-02-18 10:42:21 +00:00
_, err := io.ReadFull(r, buf[:frameHeaderLen])
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return FrameHeader{}, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return FrameHeader{
Length: (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])),
Type: FrameType(buf[3]),
Flags: Flags(buf[4]),
2024-02-18 10:42:21 +00:00
StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1),
valid: true,
2024-02-18 10:42:21 +00:00
}, nil
2024-02-18 10:42:21 +00:00
}
// A Frame is the base interface implemented by all frame types.
2024-02-18 10:42:21 +00:00
// Callers will generally type-assert the specific frame type:
2024-02-18 10:42:21 +00:00
// *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Frames are only valid until the next call to Framer.ReadFrame.
2024-02-18 10:42:21 +00:00
type Frame interface {
Header() FrameHeader
// invalidate is called by Framer.ReadFrame to make this
2024-02-18 10:42:21 +00:00
// frame's buffers as being invalid, since the subsequent
2024-02-18 10:42:21 +00:00
// frame will reuse them.
2024-02-18 10:42:21 +00:00
invalidate()
}
// A Framer reads and writes Frames.
2024-02-18 10:42:21 +00:00
type Framer struct {
r io.Reader
2024-02-18 10:42:21 +00:00
lastFrame Frame
2024-02-18 10:42:21 +00:00
errDetail error
// countError is a non-nil func that's called on a frame parse
2024-02-18 10:42:21 +00:00
// error with some unique error path token. It's initialized
2024-02-18 10:42:21 +00:00
// from Transport.CountError or Server.CountError.
2024-02-18 10:42:21 +00:00
countError func(errToken string)
// lastHeaderStream is non-zero if the last frame was an
2024-02-18 10:42:21 +00:00
// unfinished HEADERS/CONTINUATION.
2024-02-18 10:42:21 +00:00
lastHeaderStream uint32
maxReadSize uint32
headerBuf [frameHeaderLen]byte
2024-02-18 10:42:21 +00:00
// TODO: let getReadBuf be configurable, and use a less memory-pinning
2024-02-18 10:42:21 +00:00
// allocator in server.go to minimize memory pinned for many idle conns.
2024-02-18 10:42:21 +00:00
// Will probably also need to make frame invalidation have a hook too.
2024-02-18 10:42:21 +00:00
getReadBuf func(size uint32) []byte
readBuf []byte // cache for default getReadBuf
2024-02-18 10:42:21 +00:00
maxWriteSize uint32 // zero means unlimited; TODO: implement
w io.Writer
2024-02-18 10:42:21 +00:00
wbuf []byte
// AllowIllegalWrites permits the Framer's Write methods to
2024-02-18 10:42:21 +00:00
// write frames that do not conform to the HTTP/2 spec. This
2024-02-18 10:42:21 +00:00
// permits using the Framer to test other HTTP/2
2024-02-18 10:42:21 +00:00
// implementations' conformance to the spec.
2024-02-18 10:42:21 +00:00
// If false, the Write methods will prefer to return an error
2024-02-18 10:42:21 +00:00
// rather than comply.
2024-02-18 10:42:21 +00:00
AllowIllegalWrites bool
// AllowIllegalReads permits the Framer's ReadFrame method
2024-02-18 10:42:21 +00:00
// to return non-compliant frames or frame orders.
2024-02-18 10:42:21 +00:00
// This is for testing and permits using the Framer to test
2024-02-18 10:42:21 +00:00
// other HTTP/2 implementations' conformance to the spec.
2024-02-18 10:42:21 +00:00
// It is not compatible with ReadMetaHeaders.
2024-02-18 10:42:21 +00:00
AllowIllegalReads bool
// ReadMetaHeaders if non-nil causes ReadFrame to merge
2024-02-18 10:42:21 +00:00
// HEADERS and CONTINUATION frames together and return
2024-02-18 10:42:21 +00:00
// MetaHeadersFrame instead.
2024-02-18 10:42:21 +00:00
ReadMetaHeaders *hpack.Decoder
// MaxHeaderListSize is the http2 MAX_HEADER_LIST_SIZE.
2024-02-18 10:42:21 +00:00
// It's used only if ReadMetaHeaders is set; 0 means a sane default
2024-02-18 10:42:21 +00:00
// (currently 16MB)
2024-02-18 10:42:21 +00:00
// If the limit is hit, MetaHeadersFrame.Truncated is set true.
2024-02-18 10:42:21 +00:00
MaxHeaderListSize uint32
// TODO: track which type of frame & with which flags was sent
2024-02-18 10:42:21 +00:00
// last. Then return an error (unless AllowIllegalWrites) if
2024-02-18 10:42:21 +00:00
// we're in the middle of a header block and a
2024-02-18 10:42:21 +00:00
// non-Continuation or Continuation on a different stream is
2024-02-18 10:42:21 +00:00
// attempted to be written.
logReads, logWrites bool
debugFramer *Framer // only use for logging written writes
debugFramerBuf *bytes.Buffer
debugReadLoggerf func(string, ...interface{})
2024-02-18 10:42:21 +00:00
debugWriteLoggerf func(string, ...interface{})
frameCache *frameCache // nil if frames aren't reused (default)
2024-02-18 10:42:21 +00:00
}
func (fr *Framer) maxHeaderListSize() uint32 {
2024-02-18 10:42:21 +00:00
if fr.MaxHeaderListSize == 0 {
2024-02-18 10:42:21 +00:00
return 16 << 20 // sane default, per docs
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fr.MaxHeaderListSize
2024-02-18 10:42:21 +00:00
}
func (f *Framer) startWrite(ftype FrameType, flags Flags, streamID uint32) {
2024-02-18 10:42:21 +00:00
// Write the FrameHeader.
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf[:0],
2024-02-18 10:42:21 +00:00
0, // 3 bytes of length, filled in in endWrite
2024-02-18 10:42:21 +00:00
0,
2024-02-18 10:42:21 +00:00
0,
2024-02-18 10:42:21 +00:00
byte(ftype),
2024-02-18 10:42:21 +00:00
byte(flags),
2024-02-18 10:42:21 +00:00
byte(streamID>>24),
2024-02-18 10:42:21 +00:00
byte(streamID>>16),
2024-02-18 10:42:21 +00:00
byte(streamID>>8),
2024-02-18 10:42:21 +00:00
byte(streamID))
2024-02-18 10:42:21 +00:00
}
func (f *Framer) endWrite() error {
2024-02-18 10:42:21 +00:00
// Now that we know the final size, fill in the FrameHeader in
2024-02-18 10:42:21 +00:00
// the space previously reserved for it. Abuse append.
2024-02-18 10:42:21 +00:00
length := len(f.wbuf) - frameHeaderLen
2024-02-18 10:42:21 +00:00
if length >= (1 << 24) {
2024-02-18 10:42:21 +00:00
return ErrFrameTooLarge
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = append(f.wbuf[:0],
2024-02-18 10:42:21 +00:00
byte(length>>16),
2024-02-18 10:42:21 +00:00
byte(length>>8),
2024-02-18 10:42:21 +00:00
byte(length))
2024-02-18 10:42:21 +00:00
if f.logWrites {
2024-02-18 10:42:21 +00:00
f.logWrite()
2024-02-18 10:42:21 +00:00
}
n, err := f.w.Write(f.wbuf)
2024-02-18 10:42:21 +00:00
if err == nil && n != len(f.wbuf) {
2024-02-18 10:42:21 +00:00
err = io.ErrShortWrite
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func (f *Framer) logWrite() {
2024-02-18 10:42:21 +00:00
if f.debugFramer == nil {
2024-02-18 10:42:21 +00:00
f.debugFramerBuf = new(bytes.Buffer)
2024-02-18 10:42:21 +00:00
f.debugFramer = NewFramer(nil, f.debugFramerBuf)
2024-02-18 10:42:21 +00:00
f.debugFramer.logReads = false // we log it ourselves, saying "wrote" below
2024-02-18 10:42:21 +00:00
// Let us read anything, even if we accidentally wrote it
2024-02-18 10:42:21 +00:00
// in the wrong order:
2024-02-18 10:42:21 +00:00
f.debugFramer.AllowIllegalReads = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.debugFramerBuf.Write(f.wbuf)
2024-02-18 10:42:21 +00:00
fr, err := f.debugFramer.ReadFrame()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
f.debugWriteLoggerf("http2: Framer %p: failed to decode just-written frame", f)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.debugWriteLoggerf("http2: Framer %p: wrote %v", f, summarizeFrame(fr))
2024-02-18 10:42:21 +00:00
}
func (f *Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) }
func (f *Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) }
2024-02-18 10:42:21 +00:00
func (f *Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) }
2024-02-18 10:42:21 +00:00
func (f *Framer) writeUint32(v uint32) {
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
2024-02-18 10:42:21 +00:00
}
const (
minMaxFrameSize = 1 << 14
maxFrameSize = 1<<24 - 1
2024-02-18 10:42:21 +00:00
)
// SetReuseFrames allows the Framer to reuse Frames.
2024-02-18 10:42:21 +00:00
// If called on a Framer, Frames returned by calls to ReadFrame are only
2024-02-18 10:42:21 +00:00
// valid until the next call to ReadFrame.
2024-02-18 10:42:21 +00:00
func (fr *Framer) SetReuseFrames() {
2024-02-18 10:42:21 +00:00
if fr.frameCache != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fr.frameCache = &frameCache{}
2024-02-18 10:42:21 +00:00
}
type frameCache struct {
dataFrame DataFrame
}
func (fc *frameCache) getDataFrame() *DataFrame {
2024-02-18 10:42:21 +00:00
if fc == nil {
2024-02-18 10:42:21 +00:00
return &DataFrame{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &fc.dataFrame
2024-02-18 10:42:21 +00:00
}
// NewFramer returns a Framer that writes frames to w and reads them from r.
2024-02-18 10:42:21 +00:00
func NewFramer(w io.Writer, r io.Reader) *Framer {
2024-02-18 10:42:21 +00:00
fr := &Framer{
w: w,
r: r,
countError: func(string) {},
logReads: logFrameReads,
logWrites: logFrameWrites,
debugReadLoggerf: log.Printf,
2024-02-18 10:42:21 +00:00
debugWriteLoggerf: log.Printf,
}
2024-02-18 10:42:21 +00:00
fr.getReadBuf = func(size uint32) []byte {
2024-02-18 10:42:21 +00:00
if cap(fr.readBuf) >= int(size) {
2024-02-18 10:42:21 +00:00
return fr.readBuf[:size]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fr.readBuf = make([]byte, size)
2024-02-18 10:42:21 +00:00
return fr.readBuf
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fr.SetMaxReadFrameSize(maxFrameSize)
2024-02-18 10:42:21 +00:00
return fr
2024-02-18 10:42:21 +00:00
}
// SetMaxReadFrameSize sets the maximum size of a frame
2024-02-18 10:42:21 +00:00
// that will be read by a subsequent call to ReadFrame.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to advertise this
2024-02-18 10:42:21 +00:00
// limit with a SETTINGS frame.
2024-02-18 10:42:21 +00:00
func (fr *Framer) SetMaxReadFrameSize(v uint32) {
2024-02-18 10:42:21 +00:00
if v > maxFrameSize {
2024-02-18 10:42:21 +00:00
v = maxFrameSize
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fr.maxReadSize = v
2024-02-18 10:42:21 +00:00
}
// ErrorDetail returns a more detailed error of the last error
2024-02-18 10:42:21 +00:00
// returned by Framer.ReadFrame. For instance, if ReadFrame
2024-02-18 10:42:21 +00:00
// returns a StreamError with code PROTOCOL_ERROR, ErrorDetail
2024-02-18 10:42:21 +00:00
// will say exactly what was invalid. ErrorDetail is not guaranteed
2024-02-18 10:42:21 +00:00
// to return a non-nil value and like the rest of the http2 package,
2024-02-18 10:42:21 +00:00
// its return value is not protected by an API compatibility promise.
2024-02-18 10:42:21 +00:00
// ErrorDetail is reset after the next call to ReadFrame.
2024-02-18 10:42:21 +00:00
func (fr *Framer) ErrorDetail() error {
2024-02-18 10:42:21 +00:00
return fr.errDetail
2024-02-18 10:42:21 +00:00
}
// ErrFrameTooLarge is returned from Framer.ReadFrame when the peer
2024-02-18 10:42:21 +00:00
// sends a frame that is larger than declared with SetMaxReadFrameSize.
2024-02-18 10:42:21 +00:00
var ErrFrameTooLarge = errors.New("http2: frame too large")
// terminalReadFrameError reports whether err is an unrecoverable
2024-02-18 10:42:21 +00:00
// error from ReadFrame and no other frames should be read.
2024-02-18 10:42:21 +00:00
func terminalReadFrameError(err error) bool {
2024-02-18 10:42:21 +00:00
if _, ok := err.(StreamError); ok {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err != nil
2024-02-18 10:42:21 +00:00
}
// ReadFrame reads a single frame. The returned Frame is only valid
2024-02-18 10:42:21 +00:00
// until the next call to ReadFrame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If the frame is larger than previously set with SetMaxReadFrameSize, the
2024-02-18 10:42:21 +00:00
// returned error is ErrFrameTooLarge. Other errors may be of type
2024-02-18 10:42:21 +00:00
// ConnectionError, StreamError, or anything else from the underlying
2024-02-18 10:42:21 +00:00
// reader.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// If ReadFrame returns an error and a non-nil Frame, the Frame's StreamID
2024-05-14 13:07:09 +00:00
// indicates the stream responsible for the error.
2024-02-18 10:42:21 +00:00
func (fr *Framer) ReadFrame() (Frame, error) {
2024-02-18 10:42:21 +00:00
fr.errDetail = nil
2024-02-18 10:42:21 +00:00
if fr.lastFrame != nil {
2024-02-18 10:42:21 +00:00
fr.lastFrame.invalidate()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fh, err := readFrameHeader(fr.headerBuf[:], fr.r)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.Length > fr.maxReadSize {
2024-02-18 10:42:21 +00:00
return nil, ErrFrameTooLarge
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
payload := fr.getReadBuf(fh.Length)
2024-02-18 10:42:21 +00:00
if _, err := io.ReadFull(fr.r, payload); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f, err := typeFrameParser(fh.Type)(fr.frameCache, fh, fr.countError, payload)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
if ce, ok := err.(connError); ok {
2024-02-18 10:42:21 +00:00
return nil, fr.connError(ce.Code, ce.Reason)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err := fr.checkFrameOrder(f); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fr.logReads {
2024-02-18 10:42:21 +00:00
fr.debugReadLoggerf("http2: Framer %p: read %v", fr, summarizeFrame(f))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.Type == FrameHeaders && fr.ReadMetaHeaders != nil {
2024-02-18 10:42:21 +00:00
return fr.readMetaFrame(f.(*HeadersFrame))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return f, nil
2024-02-18 10:42:21 +00:00
}
// connError returns ConnectionError(code) but first
2024-02-18 10:42:21 +00:00
// stashes away a public reason to the caller can optionally relay it
2024-02-18 10:42:21 +00:00
// to the peer before hanging up on them. This might help others debug
2024-02-18 10:42:21 +00:00
// their implementations.
2024-02-18 10:42:21 +00:00
func (fr *Framer) connError(code ErrCode, reason string) error {
2024-02-18 10:42:21 +00:00
fr.errDetail = errors.New(reason)
2024-02-18 10:42:21 +00:00
return ConnectionError(code)
2024-02-18 10:42:21 +00:00
}
// checkFrameOrder reports an error if f is an invalid frame to return
2024-02-18 10:42:21 +00:00
// next from ReadFrame. Mostly it checks whether HEADERS and
2024-02-18 10:42:21 +00:00
// CONTINUATION frames are contiguous.
2024-02-18 10:42:21 +00:00
func (fr *Framer) checkFrameOrder(f Frame) error {
2024-02-18 10:42:21 +00:00
last := fr.lastFrame
2024-02-18 10:42:21 +00:00
fr.lastFrame = f
2024-02-18 10:42:21 +00:00
if fr.AllowIllegalReads {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
fh := f.Header()
2024-02-18 10:42:21 +00:00
if fr.lastHeaderStream != 0 {
2024-02-18 10:42:21 +00:00
if fh.Type != FrameContinuation {
2024-02-18 10:42:21 +00:00
return fr.connError(ErrCodeProtocol,
2024-02-18 10:42:21 +00:00
fmt.Sprintf("got %s for stream %d; expected CONTINUATION following %s for stream %d",
2024-02-18 10:42:21 +00:00
fh.Type, fh.StreamID,
2024-02-18 10:42:21 +00:00
last.Header().Type, fr.lastHeaderStream))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.StreamID != fr.lastHeaderStream {
2024-02-18 10:42:21 +00:00
return fr.connError(ErrCodeProtocol,
2024-02-18 10:42:21 +00:00
fmt.Sprintf("got CONTINUATION for stream %d; expected stream %d",
2024-02-18 10:42:21 +00:00
fh.StreamID, fr.lastHeaderStream))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if fh.Type == FrameContinuation {
2024-02-18 10:42:21 +00:00
return fr.connError(ErrCodeProtocol, fmt.Sprintf("unexpected CONTINUATION for stream %d", fh.StreamID))
2024-02-18 10:42:21 +00:00
}
switch fh.Type {
2024-02-18 10:42:21 +00:00
case FrameHeaders, FrameContinuation:
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagHeadersEndHeaders) {
2024-02-18 10:42:21 +00:00
fr.lastHeaderStream = 0
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
fr.lastHeaderStream = fh.StreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// A DataFrame conveys arbitrary, variable-length sequences of octets
2024-02-18 10:42:21 +00:00
// associated with a stream.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.1
2024-02-18 10:42:21 +00:00
type DataFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
data []byte
}
func (f *DataFrame) StreamEnded() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagDataEndStream)
2024-02-18 10:42:21 +00:00
}
// Data returns the frame's data octets, not including any padding
2024-02-18 10:42:21 +00:00
// size byte or padding suffix bytes.
2024-02-18 10:42:21 +00:00
// The caller must not retain the returned memory past the next
2024-02-18 10:42:21 +00:00
// call to ReadFrame.
2024-02-18 10:42:21 +00:00
func (f *DataFrame) Data() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.data
2024-02-18 10:42:21 +00:00
}
func parseDataFrame(fc *frameCache, fh FrameHeader, countError func(string), payload []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
// DATA frames MUST be associated with a stream. If a
2024-02-18 10:42:21 +00:00
// DATA frame is received whose stream identifier
2024-02-18 10:42:21 +00:00
// field is 0x0, the recipient MUST respond with a
2024-02-18 10:42:21 +00:00
// connection error (Section 5.4.1) of type
2024-02-18 10:42:21 +00:00
// PROTOCOL_ERROR.
2024-02-18 10:42:21 +00:00
countError("frame_data_stream_0")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeProtocol, "DATA frame with stream ID 0"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f := fc.getDataFrame()
2024-02-18 10:42:21 +00:00
f.FrameHeader = fh
var padSize byte
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagDataPadded) {
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
payload, padSize, err = readByte(payload)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_data_pad_byte_short")
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if int(padSize) > len(payload) {
2024-02-18 10:42:21 +00:00
// If the length of the padding is greater than the
2024-02-18 10:42:21 +00:00
// length of the frame payload, the recipient MUST
2024-02-18 10:42:21 +00:00
// treat this as a connection error.
2024-02-18 10:42:21 +00:00
// Filed: https://github.com/http2/http2-spec/issues/610
2024-02-18 10:42:21 +00:00
countError("frame_data_pad_too_big")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeProtocol, "pad size larger than data payload"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.data = payload[:len(payload)-int(padSize)]
2024-02-18 10:42:21 +00:00
return f, nil
2024-02-18 10:42:21 +00:00
}
var (
errStreamID = errors.New("invalid stream ID")
2024-02-18 10:42:21 +00:00
errDepStreamID = errors.New("invalid dependent stream ID")
errPadLength = errors.New("pad length too large")
errPadBytes = errors.New("padding bytes must all be zeros unless AllowIllegalWrites is enabled")
2024-02-18 10:42:21 +00:00
)
func validStreamIDOrZero(streamID uint32) bool {
2024-02-18 10:42:21 +00:00
return streamID&(1<<31) == 0
2024-02-18 10:42:21 +00:00
}
func validStreamID(streamID uint32) bool {
2024-02-18 10:42:21 +00:00
return streamID != 0 && streamID&(1<<31) == 0
2024-02-18 10:42:21 +00:00
}
// WriteData writes a DATA frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility not to violate the maximum frame size
2024-02-18 10:42:21 +00:00
// and to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteData(streamID uint32, endStream bool, data []byte) error {
2024-02-18 10:42:21 +00:00
return f.WriteDataPadded(streamID, endStream, data, nil)
2024-02-18 10:42:21 +00:00
}
// WriteDataPadded writes a DATA frame with optional padding.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If pad is nil, the padding bit is not sent.
2024-02-18 10:42:21 +00:00
// The length of pad must not exceed 255 bytes.
2024-02-18 10:42:21 +00:00
// The bytes of pad must all be zero, unless f.AllowIllegalWrites is set.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility not to violate the maximum frame size
2024-02-18 10:42:21 +00:00
// and to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteDataPadded(streamID uint32, endStream bool, data, pad []byte) error {
2024-02-18 10:42:21 +00:00
if err := f.startWriteDataPadded(streamID, endStream, data, pad); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// startWriteDataPadded is WriteDataPadded, but only writes the frame to the Framer's internal buffer.
2024-02-18 10:42:21 +00:00
// The caller should call endWrite to flush the frame to the underlying writer.
2024-02-18 10:42:21 +00:00
func (f *Framer) startWriteDataPadded(streamID uint32, endStream bool, data, pad []byte) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(streamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(pad) > 0 {
2024-02-18 10:42:21 +00:00
if len(pad) > 255 {
2024-02-18 10:42:21 +00:00
return errPadLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
for _, b := range pad {
2024-02-18 10:42:21 +00:00
if b != 0 {
2024-02-18 10:42:21 +00:00
// "Padding octets MUST be set to zero when sending."
2024-02-18 10:42:21 +00:00
return errPadBytes
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var flags Flags
2024-02-18 10:42:21 +00:00
if endStream {
2024-02-18 10:42:21 +00:00
flags |= FlagDataEndStream
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if pad != nil {
2024-02-18 10:42:21 +00:00
flags |= FlagDataPadded
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FrameData, flags, streamID)
2024-02-18 10:42:21 +00:00
if pad != nil {
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, byte(len(pad)))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, data...)
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, pad...)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// A SettingsFrame conveys configuration parameters that affect how
2024-02-18 10:42:21 +00:00
// endpoints communicate, such as preferences and constraints on peer
2024-02-18 10:42:21 +00:00
// behavior.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#SETTINGS
2024-02-18 10:42:21 +00:00
type SettingsFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
p []byte
}
func parseSettingsFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagSettingsAck) && fh.Length > 0 {
2024-02-18 10:42:21 +00:00
// When this (ACK 0x1) bit is set, the payload of the
2024-02-18 10:42:21 +00:00
// SETTINGS frame MUST be empty. Receipt of a
2024-02-18 10:42:21 +00:00
// SETTINGS frame with the ACK flag set and a length
2024-02-18 10:42:21 +00:00
// field value other than 0 MUST be treated as a
2024-02-18 10:42:21 +00:00
// connection error (Section 5.4.1) of type
2024-02-18 10:42:21 +00:00
// FRAME_SIZE_ERROR.
2024-02-18 10:42:21 +00:00
countError("frame_settings_ack_with_length")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.StreamID != 0 {
2024-02-18 10:42:21 +00:00
// SETTINGS frames always apply to a connection,
2024-02-18 10:42:21 +00:00
// never a single stream. The stream identifier for a
2024-02-18 10:42:21 +00:00
// SETTINGS frame MUST be zero (0x0). If an endpoint
2024-02-18 10:42:21 +00:00
// receives a SETTINGS frame whose stream identifier
2024-02-18 10:42:21 +00:00
// field is anything other than 0x0, the endpoint MUST
2024-02-18 10:42:21 +00:00
// respond with a connection error (Section 5.4.1) of
2024-02-18 10:42:21 +00:00
// type PROTOCOL_ERROR.
2024-02-18 10:42:21 +00:00
countError("frame_settings_has_stream")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(p)%6 != 0 {
2024-02-18 10:42:21 +00:00
countError("frame_settings_mod_6")
2024-02-18 10:42:21 +00:00
// Expecting even number of 6 byte settings.
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f := &SettingsFrame{FrameHeader: fh, p: p}
2024-02-18 10:42:21 +00:00
if v, ok := f.Value(SettingInitialWindowSize); ok && v > (1<<31)-1 {
2024-02-18 10:42:21 +00:00
countError("frame_settings_window_size_too_big")
2024-02-18 10:42:21 +00:00
// Values above the maximum flow control window size of 2^31 - 1 MUST
2024-02-18 10:42:21 +00:00
// be treated as a connection error (Section 5.4.1) of type
2024-02-18 10:42:21 +00:00
// FLOW_CONTROL_ERROR.
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFlowControl)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return f, nil
2024-02-18 10:42:21 +00:00
}
func (f *SettingsFrame) IsAck() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagSettingsAck)
2024-02-18 10:42:21 +00:00
}
func (f *SettingsFrame) Value(id SettingID) (v uint32, ok bool) {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
for i := 0; i < f.NumSettings(); i++ {
2024-02-18 10:42:21 +00:00
if s := f.Setting(i); s.ID == id {
2024-02-18 10:42:21 +00:00
return s.Val, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, false
2024-02-18 10:42:21 +00:00
}
// Setting returns the setting from the frame at the given 0-based index.
2024-02-18 10:42:21 +00:00
// The index must be >= 0 and less than f.NumSettings().
2024-02-18 10:42:21 +00:00
func (f *SettingsFrame) Setting(i int) Setting {
2024-02-18 10:42:21 +00:00
buf := f.p
2024-02-18 10:42:21 +00:00
return Setting{
ID: SettingID(binary.BigEndian.Uint16(buf[i*6 : i*6+2])),
2024-02-18 10:42:21 +00:00
Val: binary.BigEndian.Uint32(buf[i*6+2 : i*6+6]),
}
2024-02-18 10:42:21 +00:00
}
func (f *SettingsFrame) NumSettings() int { return len(f.p) / 6 }
// HasDuplicates reports whether f contains any duplicate setting IDs.
2024-02-18 10:42:21 +00:00
func (f *SettingsFrame) HasDuplicates() bool {
2024-02-18 10:42:21 +00:00
num := f.NumSettings()
2024-02-18 10:42:21 +00:00
if num == 0 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// If it's small enough (the common case), just do the n^2
2024-02-18 10:42:21 +00:00
// thing and avoid a map allocation.
2024-02-18 10:42:21 +00:00
if num < 10 {
2024-02-18 10:42:21 +00:00
for i := 0; i < num; i++ {
2024-02-18 10:42:21 +00:00
idi := f.Setting(i).ID
2024-02-18 10:42:21 +00:00
for j := i + 1; j < num; j++ {
2024-02-18 10:42:21 +00:00
idj := f.Setting(j).ID
2024-02-18 10:42:21 +00:00
if idi == idj {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
seen := map[SettingID]bool{}
2024-02-18 10:42:21 +00:00
for i := 0; i < num; i++ {
2024-02-18 10:42:21 +00:00
id := f.Setting(i).ID
2024-02-18 10:42:21 +00:00
if seen[id] {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
seen[id] = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// ForeachSetting runs fn for each setting.
2024-02-18 10:42:21 +00:00
// It stops and returns the first error.
2024-02-18 10:42:21 +00:00
func (f *SettingsFrame) ForeachSetting(fn func(Setting) error) error {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
for i := 0; i < f.NumSettings(); i++ {
2024-02-18 10:42:21 +00:00
if err := fn(f.Setting(i)); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// WriteSettings writes a SETTINGS frame with zero or more settings
2024-02-18 10:42:21 +00:00
// specified and the ACK bit not set.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteSettings(settings ...Setting) error {
2024-02-18 10:42:21 +00:00
f.startWrite(FrameSettings, 0, 0)
2024-02-18 10:42:21 +00:00
for _, s := range settings {
2024-02-18 10:42:21 +00:00
f.writeUint16(uint16(s.ID))
2024-02-18 10:42:21 +00:00
f.writeUint32(s.Val)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// WriteSettingsAck writes an empty SETTINGS frame with the ACK bit set.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteSettingsAck() error {
2024-02-18 10:42:21 +00:00
f.startWrite(FrameSettings, FlagSettingsAck, 0)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A PingFrame is a mechanism for measuring a minimal round trip time
2024-02-18 10:42:21 +00:00
// from the sender, as well as determining whether an idle connection
2024-02-18 10:42:21 +00:00
// is still functional.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.7
2024-02-18 10:42:21 +00:00
type PingFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
Data [8]byte
}
func (f *PingFrame) IsAck() bool { return f.Flags.Has(FlagPingAck) }
func parsePingFrame(_ *frameCache, fh FrameHeader, countError func(string), payload []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if len(payload) != 8 {
2024-02-18 10:42:21 +00:00
countError("frame_ping_length")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.StreamID != 0 {
2024-02-18 10:42:21 +00:00
countError("frame_ping_has_stream")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f := &PingFrame{FrameHeader: fh}
2024-02-18 10:42:21 +00:00
copy(f.Data[:], payload)
2024-02-18 10:42:21 +00:00
return f, nil
2024-02-18 10:42:21 +00:00
}
func (f *Framer) WritePing(ack bool, data [8]byte) error {
2024-02-18 10:42:21 +00:00
var flags Flags
2024-02-18 10:42:21 +00:00
if ack {
2024-02-18 10:42:21 +00:00
flags = FlagPingAck
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FramePing, flags, 0)
2024-02-18 10:42:21 +00:00
f.writeBytes(data[:])
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A GoAwayFrame informs the remote peer to stop creating streams on this connection.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.8
2024-02-18 10:42:21 +00:00
type GoAwayFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
LastStreamID uint32
ErrCode ErrCode
debugData []byte
2024-02-18 10:42:21 +00:00
}
// DebugData returns any debug data in the GOAWAY frame. Its contents
2024-02-18 10:42:21 +00:00
// are not defined.
2024-02-18 10:42:21 +00:00
// The caller must not retain the returned memory past the next
2024-02-18 10:42:21 +00:00
// call to ReadFrame.
2024-02-18 10:42:21 +00:00
func (f *GoAwayFrame) DebugData() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.debugData
2024-02-18 10:42:21 +00:00
}
func parseGoAwayFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fh.StreamID != 0 {
2024-02-18 10:42:21 +00:00
countError("frame_goaway_has_stream")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(p) < 8 {
2024-02-18 10:42:21 +00:00
countError("frame_goaway_short")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &GoAwayFrame{
FrameHeader: fh,
2024-02-18 10:42:21 +00:00
LastStreamID: binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1),
ErrCode: ErrCode(binary.BigEndian.Uint32(p[4:8])),
debugData: p[8:],
2024-02-18 10:42:21 +00:00
}, nil
2024-02-18 10:42:21 +00:00
}
func (f *Framer) WriteGoAway(maxStreamID uint32, code ErrCode, debugData []byte) error {
2024-02-18 10:42:21 +00:00
f.startWrite(FrameGoAway, 0, 0)
2024-02-18 10:42:21 +00:00
f.writeUint32(maxStreamID & (1<<31 - 1))
2024-02-18 10:42:21 +00:00
f.writeUint32(uint32(code))
2024-02-18 10:42:21 +00:00
f.writeBytes(debugData)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// An UnknownFrame is the frame type returned when the frame type is unknown
2024-02-18 10:42:21 +00:00
// or no specific frame type parser exists.
2024-02-18 10:42:21 +00:00
type UnknownFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
p []byte
}
// Payload returns the frame's payload (after the header). It is not
2024-02-18 10:42:21 +00:00
// valid to call this method after a subsequent call to
2024-02-18 10:42:21 +00:00
// Framer.ReadFrame, nor is it valid to retain the returned slice.
2024-02-18 10:42:21 +00:00
// The memory is owned by the Framer and is invalidated when the next
2024-02-18 10:42:21 +00:00
// frame is read.
2024-02-18 10:42:21 +00:00
func (f *UnknownFrame) Payload() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.p
2024-02-18 10:42:21 +00:00
}
func parseUnknownFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
return &UnknownFrame{fh, p}, nil
2024-02-18 10:42:21 +00:00
}
// A WindowUpdateFrame is used to implement flow control.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.9
2024-02-18 10:42:21 +00:00
type WindowUpdateFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
Increment uint32 // never read with high bit set
2024-02-18 10:42:21 +00:00
}
func parseWindowUpdateFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if len(p) != 4 {
2024-02-18 10:42:21 +00:00
countError("frame_windowupdate_bad_len")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff // mask off high reserved bit
2024-02-18 10:42:21 +00:00
if inc == 0 {
2024-02-18 10:42:21 +00:00
// A receiver MUST treat the receipt of a
2024-02-18 10:42:21 +00:00
// WINDOW_UPDATE frame with an flow control window
2024-02-18 10:42:21 +00:00
// increment of 0 as a stream error (Section 5.4.2) of
2024-02-18 10:42:21 +00:00
// type PROTOCOL_ERROR; errors on the connection flow
2024-02-18 10:42:21 +00:00
// control window MUST be treated as a connection
2024-02-18 10:42:21 +00:00
// error (Section 5.4.1).
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
countError("frame_windowupdate_zero_inc_conn")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
countError("frame_windowupdate_zero_inc_stream")
2024-02-18 10:42:21 +00:00
return nil, streamError(fh.StreamID, ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &WindowUpdateFrame{
2024-02-18 10:42:21 +00:00
FrameHeader: fh,
Increment: inc,
2024-02-18 10:42:21 +00:00
}, nil
2024-02-18 10:42:21 +00:00
}
// WriteWindowUpdate writes a WINDOW_UPDATE frame.
2024-02-18 10:42:21 +00:00
// The increment value must be between 1 and 2,147,483,647, inclusive.
2024-02-18 10:42:21 +00:00
// If the Stream ID is zero, the window update applies to the
2024-02-18 10:42:21 +00:00
// connection as a whole.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteWindowUpdate(streamID, incr uint32) error {
2024-02-18 10:42:21 +00:00
// "The legal range for the increment to the flow control window is 1 to 2^31-1 (2,147,483,647) octets."
2024-02-18 10:42:21 +00:00
if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errors.New("illegal window increment value")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FrameWindowUpdate, 0, streamID)
2024-02-18 10:42:21 +00:00
f.writeUint32(incr)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A HeadersFrame is used to open a stream and additionally carries a
2024-02-18 10:42:21 +00:00
// header block fragment.
2024-02-18 10:42:21 +00:00
type HeadersFrame struct {
FrameHeader
// Priority is set if FlagHeadersPriority is set in the FrameHeader.
2024-02-18 10:42:21 +00:00
Priority PriorityParam
headerFragBuf []byte // not owned
2024-02-18 10:42:21 +00:00
}
func (f *HeadersFrame) HeaderBlockFragment() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.headerFragBuf
2024-02-18 10:42:21 +00:00
}
func (f *HeadersFrame) HeadersEnded() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagHeadersEndHeaders)
2024-02-18 10:42:21 +00:00
}
func (f *HeadersFrame) StreamEnded() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagHeadersEndStream)
2024-02-18 10:42:21 +00:00
}
func (f *HeadersFrame) HasPriority() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagHeadersPriority)
2024-02-18 10:42:21 +00:00
}
func parseHeadersFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (_ Frame, err error) {
2024-02-18 10:42:21 +00:00
hf := &HeadersFrame{
2024-02-18 10:42:21 +00:00
FrameHeader: fh,
}
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
// HEADERS frames MUST be associated with a stream. If a HEADERS frame
2024-02-18 10:42:21 +00:00
// is received whose stream identifier field is 0x0, the recipient MUST
2024-02-18 10:42:21 +00:00
// respond with a connection error (Section 5.4.1) of type
2024-02-18 10:42:21 +00:00
// PROTOCOL_ERROR.
2024-02-18 10:42:21 +00:00
countError("frame_headers_zero_stream")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeProtocol, "HEADERS frame with stream ID 0"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var padLength uint8
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagHeadersPadded) {
2024-02-18 10:42:21 +00:00
if p, padLength, err = readByte(p); err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_headers_pad_short")
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagHeadersPriority) {
2024-02-18 10:42:21 +00:00
var v uint32
2024-02-18 10:42:21 +00:00
p, v, err = readUint32(p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_headers_prio_short")
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf.Priority.StreamDep = v & 0x7fffffff
2024-02-18 10:42:21 +00:00
hf.Priority.Exclusive = (v != hf.Priority.StreamDep) // high bit was set
2024-02-18 10:42:21 +00:00
p, hf.Priority.Weight, err = readByte(p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_headers_prio_weight_short")
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(p)-int(padLength) < 0 {
2024-02-18 10:42:21 +00:00
countError("frame_headers_pad_too_big")
2024-02-18 10:42:21 +00:00
return nil, streamError(fh.StreamID, ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf.headerFragBuf = p[:len(p)-int(padLength)]
2024-02-18 10:42:21 +00:00
return hf, nil
2024-02-18 10:42:21 +00:00
}
// HeadersFrameParam are the parameters for writing a HEADERS frame.
2024-02-18 10:42:21 +00:00
type HeadersFrameParam struct {
2024-02-18 10:42:21 +00:00
// StreamID is the required Stream ID to initiate.
2024-02-18 10:42:21 +00:00
StreamID uint32
2024-02-18 10:42:21 +00:00
// BlockFragment is part (or all) of a Header Block.
2024-02-18 10:42:21 +00:00
BlockFragment []byte
// EndStream indicates that the header block is the last that
2024-02-18 10:42:21 +00:00
// the endpoint will send for the identified stream. Setting
2024-02-18 10:42:21 +00:00
// this flag causes the stream to enter one of "half closed"
2024-02-18 10:42:21 +00:00
// states.
2024-02-18 10:42:21 +00:00
EndStream bool
// EndHeaders indicates that this frame contains an entire
2024-02-18 10:42:21 +00:00
// header block and is not followed by any
2024-02-18 10:42:21 +00:00
// CONTINUATION frames.
2024-02-18 10:42:21 +00:00
EndHeaders bool
// PadLength is the optional number of bytes of zeros to add
2024-02-18 10:42:21 +00:00
// to this frame.
2024-02-18 10:42:21 +00:00
PadLength uint8
// Priority, if non-zero, includes stream priority information
2024-02-18 10:42:21 +00:00
// in the HEADER frame.
2024-02-18 10:42:21 +00:00
Priority PriorityParam
}
// WriteHeaders writes a single HEADERS frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This is a low-level header writing method. Encoding headers and
2024-02-18 10:42:21 +00:00
// splitting them into any necessary CONTINUATION frames is handled
2024-02-18 10:42:21 +00:00
// elsewhere.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteHeaders(p HeadersFrameParam) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(p.StreamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var flags Flags
2024-02-18 10:42:21 +00:00
if p.PadLength != 0 {
2024-02-18 10:42:21 +00:00
flags |= FlagHeadersPadded
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.EndStream {
2024-02-18 10:42:21 +00:00
flags |= FlagHeadersEndStream
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.EndHeaders {
2024-02-18 10:42:21 +00:00
flags |= FlagHeadersEndHeaders
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !p.Priority.IsZero() {
2024-02-18 10:42:21 +00:00
flags |= FlagHeadersPriority
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FrameHeaders, flags, p.StreamID)
2024-02-18 10:42:21 +00:00
if p.PadLength != 0 {
2024-02-18 10:42:21 +00:00
f.writeByte(p.PadLength)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !p.Priority.IsZero() {
2024-02-18 10:42:21 +00:00
v := p.Priority.StreamDep
2024-02-18 10:42:21 +00:00
if !validStreamIDOrZero(v) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errDepStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.Priority.Exclusive {
2024-02-18 10:42:21 +00:00
v |= 1 << 31
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.writeUint32(v)
2024-02-18 10:42:21 +00:00
f.writeByte(p.Priority.Weight)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, p.BlockFragment...)
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, padZeros[:p.PadLength]...)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A PriorityFrame specifies the sender-advised priority of a stream.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.3
2024-02-18 10:42:21 +00:00
type PriorityFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
PriorityParam
}
// PriorityParam are the stream prioritzation parameters.
2024-02-18 10:42:21 +00:00
type PriorityParam struct {
2024-02-18 10:42:21 +00:00
// StreamDep is a 31-bit stream identifier for the
2024-02-18 10:42:21 +00:00
// stream that this stream depends on. Zero means no
2024-02-18 10:42:21 +00:00
// dependency.
2024-02-18 10:42:21 +00:00
StreamDep uint32
// Exclusive is whether the dependency is exclusive.
2024-02-18 10:42:21 +00:00
Exclusive bool
// Weight is the stream's zero-indexed weight. It should be
2024-02-18 10:42:21 +00:00
// set together with StreamDep, or neither should be set. Per
2024-02-18 10:42:21 +00:00
// the spec, "Add one to the value to obtain a weight between
2024-02-18 10:42:21 +00:00
// 1 and 256."
2024-02-18 10:42:21 +00:00
Weight uint8
}
func (p PriorityParam) IsZero() bool {
2024-02-18 10:42:21 +00:00
return p == PriorityParam{}
2024-02-18 10:42:21 +00:00
}
func parsePriorityFrame(_ *frameCache, fh FrameHeader, countError func(string), payload []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
countError("frame_priority_zero_stream")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeProtocol, "PRIORITY frame with stream ID 0"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(payload) != 5 {
2024-02-18 10:42:21 +00:00
countError("frame_priority_bad_length")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeFrameSize, fmt.Sprintf("PRIORITY frame payload size was %d; want 5", len(payload))}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v := binary.BigEndian.Uint32(payload[:4])
2024-02-18 10:42:21 +00:00
streamID := v & 0x7fffffff // mask off high bit
2024-02-18 10:42:21 +00:00
return &PriorityFrame{
2024-02-18 10:42:21 +00:00
FrameHeader: fh,
2024-02-18 10:42:21 +00:00
PriorityParam: PriorityParam{
Weight: payload[4],
2024-02-18 10:42:21 +00:00
StreamDep: streamID,
2024-02-18 10:42:21 +00:00
Exclusive: streamID != v, // was high bit set?
2024-02-18 10:42:21 +00:00
},
}, nil
2024-02-18 10:42:21 +00:00
}
// WritePriority writes a PRIORITY frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WritePriority(streamID uint32, p PriorityParam) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(streamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !validStreamIDOrZero(p.StreamDep) {
2024-02-18 10:42:21 +00:00
return errDepStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FramePriority, 0, streamID)
2024-02-18 10:42:21 +00:00
v := p.StreamDep
2024-02-18 10:42:21 +00:00
if p.Exclusive {
2024-02-18 10:42:21 +00:00
v |= 1 << 31
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.writeUint32(v)
2024-02-18 10:42:21 +00:00
f.writeByte(p.Weight)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A RSTStreamFrame allows for abnormal termination of a stream.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.4
2024-02-18 10:42:21 +00:00
type RSTStreamFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
ErrCode ErrCode
}
func parseRSTStreamFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if len(p) != 4 {
2024-02-18 10:42:21 +00:00
countError("frame_rststream_bad_len")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeFrameSize)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
countError("frame_rststream_zero_stream")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &RSTStreamFrame{fh, ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil
2024-02-18 10:42:21 +00:00
}
// WriteRSTStream writes a RST_STREAM frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteRSTStream(streamID uint32, code ErrCode) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(streamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FrameRSTStream, 0, streamID)
2024-02-18 10:42:21 +00:00
f.writeUint32(uint32(code))
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A ContinuationFrame is used to continue a sequence of header block fragments.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.10
2024-02-18 10:42:21 +00:00
type ContinuationFrame struct {
FrameHeader
2024-02-18 10:42:21 +00:00
headerFragBuf []byte
}
func parseContinuationFrame(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fh.StreamID == 0 {
2024-02-18 10:42:21 +00:00
countError("frame_continuation_zero_stream")
2024-02-18 10:42:21 +00:00
return nil, connError{ErrCodeProtocol, "CONTINUATION frame with stream ID 0"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &ContinuationFrame{fh, p}, nil
2024-02-18 10:42:21 +00:00
}
func (f *ContinuationFrame) HeaderBlockFragment() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.headerFragBuf
2024-02-18 10:42:21 +00:00
}
func (f *ContinuationFrame) HeadersEnded() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagContinuationEndHeaders)
2024-02-18 10:42:21 +00:00
}
// WriteContinuation writes a CONTINUATION frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(streamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var flags Flags
2024-02-18 10:42:21 +00:00
if endHeaders {
2024-02-18 10:42:21 +00:00
flags |= FlagContinuationEndHeaders
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FrameContinuation, flags, streamID)
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, headerBlockFragment...)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// A PushPromiseFrame is used to initiate a server stream.
2024-02-18 10:42:21 +00:00
// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.6
2024-02-18 10:42:21 +00:00
type PushPromiseFrame struct {
FrameHeader
PromiseID uint32
2024-02-18 10:42:21 +00:00
headerFragBuf []byte // not owned
2024-02-18 10:42:21 +00:00
}
func (f *PushPromiseFrame) HeaderBlockFragment() []byte {
2024-02-18 10:42:21 +00:00
f.checkValid()
2024-02-18 10:42:21 +00:00
return f.headerFragBuf
2024-02-18 10:42:21 +00:00
}
func (f *PushPromiseFrame) HeadersEnded() bool {
2024-02-18 10:42:21 +00:00
return f.FrameHeader.Flags.Has(FlagPushPromiseEndHeaders)
2024-02-18 10:42:21 +00:00
}
func parsePushPromise(_ *frameCache, fh FrameHeader, countError func(string), p []byte) (_ Frame, err error) {
2024-02-18 10:42:21 +00:00
pp := &PushPromiseFrame{
2024-02-18 10:42:21 +00:00
FrameHeader: fh,
}
2024-02-18 10:42:21 +00:00
if pp.StreamID == 0 {
2024-02-18 10:42:21 +00:00
// PUSH_PROMISE frames MUST be associated with an existing,
2024-02-18 10:42:21 +00:00
// peer-initiated stream. The stream identifier of a
2024-02-18 10:42:21 +00:00
// PUSH_PROMISE frame indicates the stream it is associated
2024-02-18 10:42:21 +00:00
// with. If the stream identifier field specifies the value
2024-02-18 10:42:21 +00:00
// 0x0, a recipient MUST respond with a connection error
2024-02-18 10:42:21 +00:00
// (Section 5.4.1) of type PROTOCOL_ERROR.
2024-02-18 10:42:21 +00:00
countError("frame_pushpromise_zero_stream")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// The PUSH_PROMISE frame includes optional padding.
2024-02-18 10:42:21 +00:00
// Padding fields and flags are identical to those defined for DATA frames
2024-02-18 10:42:21 +00:00
var padLength uint8
2024-02-18 10:42:21 +00:00
if fh.Flags.Has(FlagPushPromisePadded) {
2024-02-18 10:42:21 +00:00
if p, padLength, err = readByte(p); err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_pushpromise_pad_short")
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
p, pp.PromiseID, err = readUint32(p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
countError("frame_pushpromise_promiseid_short")
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pp.PromiseID = pp.PromiseID & (1<<31 - 1)
if int(padLength) > len(p) {
2024-02-18 10:42:21 +00:00
// like the DATA frame, error out if padding is longer than the body.
2024-02-18 10:42:21 +00:00
countError("frame_pushpromise_pad_too_big")
2024-02-18 10:42:21 +00:00
return nil, ConnectionError(ErrCodeProtocol)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pp.headerFragBuf = p[:len(p)-int(padLength)]
2024-02-18 10:42:21 +00:00
return pp, nil
2024-02-18 10:42:21 +00:00
}
// PushPromiseParam are the parameters for writing a PUSH_PROMISE frame.
2024-02-18 10:42:21 +00:00
type PushPromiseParam struct {
2024-02-18 10:42:21 +00:00
// StreamID is the required Stream ID to initiate.
2024-02-18 10:42:21 +00:00
StreamID uint32
// PromiseID is the required Stream ID which this
2024-02-18 10:42:21 +00:00
// Push Promises
2024-02-18 10:42:21 +00:00
PromiseID uint32
// BlockFragment is part (or all) of a Header Block.
2024-02-18 10:42:21 +00:00
BlockFragment []byte
// EndHeaders indicates that this frame contains an entire
2024-02-18 10:42:21 +00:00
// header block and is not followed by any
2024-02-18 10:42:21 +00:00
// CONTINUATION frames.
2024-02-18 10:42:21 +00:00
EndHeaders bool
// PadLength is the optional number of bytes of zeros to add
2024-02-18 10:42:21 +00:00
// to this frame.
2024-02-18 10:42:21 +00:00
PadLength uint8
}
// WritePushPromise writes a single PushPromise Frame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// As with Header Frames, This is the low level call for writing
2024-02-18 10:42:21 +00:00
// individual frames. Continuation frames are handled elsewhere.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It will perform exactly one Write to the underlying Writer.
2024-02-18 10:42:21 +00:00
// It is the caller's responsibility to not call other Write methods concurrently.
2024-02-18 10:42:21 +00:00
func (f *Framer) WritePushPromise(p PushPromiseParam) error {
2024-02-18 10:42:21 +00:00
if !validStreamID(p.StreamID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var flags Flags
2024-02-18 10:42:21 +00:00
if p.PadLength != 0 {
2024-02-18 10:42:21 +00:00
flags |= FlagPushPromisePadded
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.EndHeaders {
2024-02-18 10:42:21 +00:00
flags |= FlagPushPromiseEndHeaders
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.startWrite(FramePushPromise, flags, p.StreamID)
2024-02-18 10:42:21 +00:00
if p.PadLength != 0 {
2024-02-18 10:42:21 +00:00
f.writeByte(p.PadLength)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !validStreamID(p.PromiseID) && !f.AllowIllegalWrites {
2024-02-18 10:42:21 +00:00
return errStreamID
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
f.writeUint32(p.PromiseID)
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, p.BlockFragment...)
2024-02-18 10:42:21 +00:00
f.wbuf = append(f.wbuf, padZeros[:p.PadLength]...)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
// WriteRawFrame writes a raw frame. This can be used to write
2024-02-18 10:42:21 +00:00
// extension frames unknown to this package.
2024-02-18 10:42:21 +00:00
func (f *Framer) WriteRawFrame(t FrameType, flags Flags, streamID uint32, payload []byte) error {
2024-02-18 10:42:21 +00:00
f.startWrite(t, flags, streamID)
2024-02-18 10:42:21 +00:00
f.writeBytes(payload)
2024-02-18 10:42:21 +00:00
return f.endWrite()
2024-02-18 10:42:21 +00:00
}
func readByte(p []byte) (remain []byte, b byte, err error) {
2024-02-18 10:42:21 +00:00
if len(p) == 0 {
2024-02-18 10:42:21 +00:00
return nil, 0, io.ErrUnexpectedEOF
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p[1:], p[0], nil
2024-02-18 10:42:21 +00:00
}
func readUint32(p []byte) (remain []byte, v uint32, err error) {
2024-02-18 10:42:21 +00:00
if len(p) < 4 {
2024-02-18 10:42:21 +00:00
return nil, 0, io.ErrUnexpectedEOF
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p[4:], binary.BigEndian.Uint32(p[:4]), nil
2024-02-18 10:42:21 +00:00
}
type streamEnder interface {
StreamEnded() bool
}
type headersEnder interface {
HeadersEnded() bool
}
type headersOrContinuation interface {
headersEnder
2024-02-18 10:42:21 +00:00
HeaderBlockFragment() []byte
}
// A MetaHeadersFrame is the representation of one HEADERS frame and
2024-02-18 10:42:21 +00:00
// zero or more contiguous CONTINUATION frames and the decoding of
2024-02-18 10:42:21 +00:00
// their HPACK-encoded contents.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This type of frame does not appear on the wire and is only returned
2024-02-18 10:42:21 +00:00
// by the Framer when Framer.ReadMetaHeaders is set.
2024-02-18 10:42:21 +00:00
type MetaHeadersFrame struct {
*HeadersFrame
// Fields are the fields contained in the HEADERS and
2024-02-18 10:42:21 +00:00
// CONTINUATION frames. The underlying slice is owned by the
2024-02-18 10:42:21 +00:00
// Framer and must not be retained after the next call to
2024-02-18 10:42:21 +00:00
// ReadFrame.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Fields are guaranteed to be in the correct http2 order and
2024-02-18 10:42:21 +00:00
// not have unknown pseudo header fields or invalid header
2024-02-18 10:42:21 +00:00
// field names or values. Required pseudo header fields may be
2024-02-18 10:42:21 +00:00
// missing, however. Use the MetaHeadersFrame.Pseudo accessor
2024-02-18 10:42:21 +00:00
// method access pseudo headers.
2024-02-18 10:42:21 +00:00
Fields []hpack.HeaderField
// Truncated is whether the max header list size limit was hit
2024-02-18 10:42:21 +00:00
// and Fields is incomplete. The hpack decoder state is still
2024-02-18 10:42:21 +00:00
// valid, however.
2024-02-18 10:42:21 +00:00
Truncated bool
}
// PseudoValue returns the given pseudo header field's value.
2024-02-18 10:42:21 +00:00
// The provided pseudo field should not contain the leading colon.
2024-02-18 10:42:21 +00:00
func (mh *MetaHeadersFrame) PseudoValue(pseudo string) string {
2024-02-18 10:42:21 +00:00
for _, hf := range mh.Fields {
2024-02-18 10:42:21 +00:00
if !hf.IsPseudo() {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if hf.Name[1:] == pseudo {
2024-02-18 10:42:21 +00:00
return hf.Value
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// RegularFields returns the regular (non-pseudo) header fields of mh.
2024-02-18 10:42:21 +00:00
// The caller does not own the returned slice.
2024-02-18 10:42:21 +00:00
func (mh *MetaHeadersFrame) RegularFields() []hpack.HeaderField {
2024-02-18 10:42:21 +00:00
for i, hf := range mh.Fields {
2024-02-18 10:42:21 +00:00
if !hf.IsPseudo() {
2024-02-18 10:42:21 +00:00
return mh.Fields[i:]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// PseudoFields returns the pseudo header fields of mh.
2024-02-18 10:42:21 +00:00
// The caller does not own the returned slice.
2024-02-18 10:42:21 +00:00
func (mh *MetaHeadersFrame) PseudoFields() []hpack.HeaderField {
2024-02-18 10:42:21 +00:00
for i, hf := range mh.Fields {
2024-02-18 10:42:21 +00:00
if !hf.IsPseudo() {
2024-02-18 10:42:21 +00:00
return mh.Fields[:i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return mh.Fields
2024-02-18 10:42:21 +00:00
}
func (mh *MetaHeadersFrame) checkPseudos() error {
2024-02-18 10:42:21 +00:00
var isRequest, isResponse bool
2024-02-18 10:42:21 +00:00
pf := mh.PseudoFields()
2024-02-18 10:42:21 +00:00
for i, hf := range pf {
2024-02-18 10:42:21 +00:00
switch hf.Name {
2024-02-18 10:42:21 +00:00
case ":method", ":path", ":scheme", ":authority":
2024-02-18 10:42:21 +00:00
isRequest = true
2024-02-18 10:42:21 +00:00
case ":status":
2024-02-18 10:42:21 +00:00
isResponse = true
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return pseudoHeaderError(hf.Name)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Check for duplicates.
2024-02-18 10:42:21 +00:00
// This would be a bad algorithm, but N is 4.
2024-02-18 10:42:21 +00:00
// And this doesn't allocate.
2024-02-18 10:42:21 +00:00
for _, hf2 := range pf[:i] {
2024-02-18 10:42:21 +00:00
if hf.Name == hf2.Name {
2024-02-18 10:42:21 +00:00
return duplicatePseudoHeaderError(hf.Name)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if isRequest && isResponse {
2024-02-18 10:42:21 +00:00
return errMixPseudoHeaderTypes
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (fr *Framer) maxHeaderStringLen() int {
2024-05-14 13:07:09 +00:00
v := int(fr.maxHeaderListSize())
2024-05-14 13:07:09 +00:00
if v < 0 {
2024-05-14 13:07:09 +00:00
// If maxHeaderListSize overflows an int, use no limit (0).
2024-05-14 13:07:09 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
2024-05-14 13:07:09 +00:00
return v
2024-02-18 10:42:21 +00:00
}
// readMetaFrame returns 0 or more CONTINUATION frames from fr and
2024-02-18 10:42:21 +00:00
// merge them into the provided hf and returns a MetaHeadersFrame
2024-02-18 10:42:21 +00:00
// with the decoded hpack values.
2024-05-14 13:07:09 +00:00
func (fr *Framer) readMetaFrame(hf *HeadersFrame) (Frame, error) {
2024-02-18 10:42:21 +00:00
if fr.AllowIllegalReads {
2024-02-18 10:42:21 +00:00
return nil, errors.New("illegal use of AllowIllegalReads with ReadMetaHeaders")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mh := &MetaHeadersFrame{
2024-02-18 10:42:21 +00:00
HeadersFrame: hf,
}
2024-02-18 10:42:21 +00:00
var remainSize = fr.maxHeaderListSize()
2024-02-18 10:42:21 +00:00
var sawRegular bool
var invalid error // pseudo header field errors
2024-02-18 10:42:21 +00:00
hdec := fr.ReadMetaHeaders
2024-02-18 10:42:21 +00:00
hdec.SetEmitEnabled(true)
2024-02-18 10:42:21 +00:00
hdec.SetMaxStringLength(fr.maxHeaderStringLen())
2024-02-18 10:42:21 +00:00
hdec.SetEmitFunc(func(hf hpack.HeaderField) {
2024-02-18 10:42:21 +00:00
if VerboseLogs && fr.logReads {
2024-02-18 10:42:21 +00:00
fr.debugReadLoggerf("http2: decoded hpack field %+v", hf)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !httpguts.ValidHeaderFieldValue(hf.Value) {
2024-02-18 10:42:21 +00:00
// Don't include the value in the error, because it may be sensitive.
2024-02-18 10:42:21 +00:00
invalid = headerFieldValueError(hf.Name)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
isPseudo := strings.HasPrefix(hf.Name, ":")
2024-02-18 10:42:21 +00:00
if isPseudo {
2024-02-18 10:42:21 +00:00
if sawRegular {
2024-02-18 10:42:21 +00:00
invalid = errPseudoAfterRegular
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
sawRegular = true
2024-02-18 10:42:21 +00:00
if !validWireHeaderFieldName(hf.Name) {
2024-02-18 10:42:21 +00:00
invalid = headerFieldNameError(hf.Name)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if invalid != nil {
2024-02-18 10:42:21 +00:00
hdec.SetEmitEnabled(false)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
size := hf.Size()
2024-02-18 10:42:21 +00:00
if size > remainSize {
2024-02-18 10:42:21 +00:00
hdec.SetEmitEnabled(false)
2024-02-18 10:42:21 +00:00
mh.Truncated = true
2024-05-14 13:07:09 +00:00
remainSize = 0
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
remainSize -= size
mh.Fields = append(mh.Fields, hf)
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
// Lose reference to MetaHeadersFrame:
2024-02-18 10:42:21 +00:00
defer hdec.SetEmitFunc(func(hf hpack.HeaderField) {})
var hc headersOrContinuation = hf
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
frag := hc.HeaderBlockFragment()
2024-05-14 13:07:09 +00:00
// Avoid parsing large amounts of headers that we will then discard.
2024-05-14 13:07:09 +00:00
// If the sender exceeds the max header list size by too much,
2024-05-14 13:07:09 +00:00
// skip parsing the fragment and close the connection.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// "Too much" is either any CONTINUATION frame after we've already
2024-05-14 13:07:09 +00:00
// exceeded the max header list size (in which case remainSize is 0),
2024-05-14 13:07:09 +00:00
// or a frame whose encoded size is more than twice the remaining
2024-05-14 13:07:09 +00:00
// header list bytes we're willing to accept.
2024-05-14 13:07:09 +00:00
if int64(len(frag)) > int64(2*remainSize) {
2024-05-14 13:07:09 +00:00
if VerboseLogs {
2024-05-14 13:07:09 +00:00
log.Printf("http2: header list too large")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
// It would be nice to send a RST_STREAM before sending the GOAWAY,
2024-05-14 13:07:09 +00:00
// but the structure of the server's frame writer makes this difficult.
2024-05-14 13:07:09 +00:00
return mh, ConnectionError(ErrCodeProtocol)
2024-05-14 13:07:09 +00:00
}
// Also close the connection after any CONTINUATION frame following an
2024-05-14 13:07:09 +00:00
// invalid header, since we stop tracking the size of the headers after
2024-05-14 13:07:09 +00:00
// an invalid one.
2024-05-14 13:07:09 +00:00
if invalid != nil {
2024-05-14 13:07:09 +00:00
if VerboseLogs {
2024-05-14 13:07:09 +00:00
log.Printf("http2: invalid header: %v", invalid)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
// It would be nice to send a RST_STREAM before sending the GOAWAY,
2024-05-14 13:07:09 +00:00
// but the structure of the server's frame writer makes this difficult.
2024-05-14 13:07:09 +00:00
return mh, ConnectionError(ErrCodeProtocol)
2024-05-14 13:07:09 +00:00
}
2024-02-18 10:42:21 +00:00
if _, err := hdec.Write(frag); err != nil {
2024-05-14 13:07:09 +00:00
return mh, ConnectionError(ErrCodeCompression)
2024-02-18 10:42:21 +00:00
}
if hc.HeadersEnded() {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if f, err := fr.ReadFrame(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
hc = f.(*ContinuationFrame) // guaranteed by checkFrameOrder
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
mh.HeadersFrame.headerFragBuf = nil
2024-02-18 10:42:21 +00:00
mh.HeadersFrame.invalidate()
if err := hdec.Close(); err != nil {
2024-05-14 13:07:09 +00:00
return mh, ConnectionError(ErrCodeCompression)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if invalid != nil {
2024-02-18 10:42:21 +00:00
fr.errDetail = invalid
2024-02-18 10:42:21 +00:00
if VerboseLogs {
2024-02-18 10:42:21 +00:00
log.Printf("http2: invalid header: %v", invalid)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, StreamError{mh.StreamID, ErrCodeProtocol, invalid}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err := mh.checkPseudos(); err != nil {
2024-02-18 10:42:21 +00:00
fr.errDetail = err
2024-02-18 10:42:21 +00:00
if VerboseLogs {
2024-02-18 10:42:21 +00:00
log.Printf("http2: invalid pseudo headers: %v", err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, StreamError{mh.StreamID, ErrCodeProtocol, err}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return mh, nil
2024-02-18 10:42:21 +00:00
}
func summarizeFrame(f Frame) string {
2024-02-18 10:42:21 +00:00
var buf bytes.Buffer
2024-02-18 10:42:21 +00:00
f.Header().writeDebug(&buf)
2024-02-18 10:42:21 +00:00
switch f := f.(type) {
2024-02-18 10:42:21 +00:00
case *SettingsFrame:
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
f.ForeachSetting(func(s Setting) error {
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
if n == 1 {
2024-02-18 10:42:21 +00:00
buf.WriteString(", settings:")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " %v=%v,", s.ID, s.Val)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
buf.Truncate(buf.Len() - 1) // remove trailing comma
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case *DataFrame:
2024-02-18 10:42:21 +00:00
data := f.Data()
2024-02-18 10:42:21 +00:00
const max = 256
2024-02-18 10:42:21 +00:00
if len(data) > max {
2024-02-18 10:42:21 +00:00
data = data[:max]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " data=%q", data)
2024-02-18 10:42:21 +00:00
if len(f.Data()) > max {
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " (%d bytes omitted)", len(f.Data())-max)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case *WindowUpdateFrame:
2024-02-18 10:42:21 +00:00
if f.StreamID == 0 {
2024-02-18 10:42:21 +00:00
buf.WriteString(" (conn)")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " incr=%v", f.Increment)
2024-02-18 10:42:21 +00:00
case *PingFrame:
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " ping=%q", f.Data[:])
2024-02-18 10:42:21 +00:00
case *GoAwayFrame:
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " LastStreamID=%v ErrCode=%v Debug=%q",
2024-02-18 10:42:21 +00:00
f.LastStreamID, f.ErrCode, f.debugData)
2024-02-18 10:42:21 +00:00
case *RSTStreamFrame:
2024-02-18 10:42:21 +00:00
fmt.Fprintf(&buf, " ErrCode=%v", f.ErrCode)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return buf.String()
2024-02-18 10:42:21 +00:00
}