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

592 lines
11 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"
"fmt"
"log"
"net/http"
"net/url"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/http2/hpack"
)
// writeFramer is implemented by any type that is used to write frames.
2024-02-18 10:42:21 +00:00
type writeFramer interface {
writeFrame(writeContext) error
// staysWithinBuffer reports whether this writer promises that
2024-02-18 10:42:21 +00:00
// it will only write less than or equal to size bytes, and it
2024-02-18 10:42:21 +00:00
// won't Flush the write context.
2024-02-18 10:42:21 +00:00
staysWithinBuffer(size int) bool
}
// writeContext is the interface needed by the various frame writer
2024-02-18 10:42:21 +00:00
// types below. All the writeFrame methods below are scheduled via the
2024-02-18 10:42:21 +00:00
// frame writing scheduler (see writeScheduler in writesched.go).
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This interface is implemented by *serverConn.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// TODO: decide whether to a) use this in the client code (which didn't
2024-02-18 10:42:21 +00:00
// end up using this yet, because it has a simpler design, not
2024-02-18 10:42:21 +00:00
// currently implementing priorities), or b) delete this and
2024-02-18 10:42:21 +00:00
// make the server code a bit more concrete.
2024-02-18 10:42:21 +00:00
type writeContext interface {
Framer() *Framer
2024-02-18 10:42:21 +00:00
Flush() error
2024-02-18 10:42:21 +00:00
CloseConn() error
2024-02-18 10:42:21 +00:00
// HeaderEncoder returns an HPACK encoder that writes to the
2024-02-18 10:42:21 +00:00
// returned buffer.
2024-02-18 10:42:21 +00:00
HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
}
// writeEndsStream reports whether w writes a frame that will transition
2024-02-18 10:42:21 +00:00
// the stream to a half-closed local state. This returns false for RST_STREAM,
2024-02-18 10:42:21 +00:00
// which closes the entire stream (not just the local half).
2024-02-18 10:42:21 +00:00
func writeEndsStream(w writeFramer) bool {
2024-02-18 10:42:21 +00:00
switch v := w.(type) {
2024-02-18 10:42:21 +00:00
case *writeData:
2024-02-18 10:42:21 +00:00
return v.endStream
2024-02-18 10:42:21 +00:00
case *writeResHeaders:
2024-02-18 10:42:21 +00:00
return v.endStream
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
// This can only happen if the caller reuses w after it's
2024-02-18 10:42:21 +00:00
// been intentionally nil'ed out to prevent use. Keep this
2024-02-18 10:42:21 +00:00
// here to catch future refactoring breaking it.
2024-02-18 10:42:21 +00:00
panic("writeEndsStream called on nil writeFramer")
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
}
type flushFrameWriter struct{}
func (flushFrameWriter) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Flush()
2024-02-18 10:42:21 +00:00
}
func (flushFrameWriter) staysWithinBuffer(max int) bool { return false }
type writeSettings []Setting
func (s writeSettings) staysWithinBuffer(max int) bool {
2024-02-18 10:42:21 +00:00
const settingSize = 6 // uint16 + uint32
2024-02-18 10:42:21 +00:00
return frameHeaderLen+settingSize*len(s) <= max
}
func (s writeSettings) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteSettings([]Setting(s)...)
2024-02-18 10:42:21 +00:00
}
type writeGoAway struct {
maxStreamID uint32
code ErrCode
2024-02-18 10:42:21 +00:00
}
func (p *writeGoAway) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
2024-02-18 10:42:21 +00:00
ctx.Flush() // ignore error: we're hanging up on them anyway
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func (*writeGoAway) staysWithinBuffer(max int) bool { return false } // flushes
type writeData struct {
streamID uint32
p []byte
2024-02-18 10:42:21 +00:00
endStream bool
}
func (w *writeData) String() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
2024-02-18 10:42:21 +00:00
}
func (w *writeData) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
2024-02-18 10:42:21 +00:00
}
func (w *writeData) staysWithinBuffer(max int) bool {
2024-02-18 10:42:21 +00:00
return frameHeaderLen+len(w.p) <= max
2024-02-18 10:42:21 +00:00
}
// handlerPanicRST is the message sent from handler goroutines when
2024-02-18 10:42:21 +00:00
// the handler panics.
2024-02-18 10:42:21 +00:00
type handlerPanicRST struct {
StreamID uint32
}
func (hp handlerPanicRST) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteRSTStream(hp.StreamID, ErrCodeInternal)
2024-02-18 10:42:21 +00:00
}
func (hp handlerPanicRST) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
func (se StreamError) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
2024-02-18 10:42:21 +00:00
}
func (se StreamError) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
type writePingAck struct{ pf *PingFrame }
func (w writePingAck) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WritePing(true, w.pf.Data)
2024-02-18 10:42:21 +00:00
}
func (w writePingAck) staysWithinBuffer(max int) bool { return frameHeaderLen+len(w.pf.Data) <= max }
type writeSettingsAck struct{}
func (writeSettingsAck) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteSettingsAck()
2024-02-18 10:42:21 +00:00
}
func (writeSettingsAck) staysWithinBuffer(max int) bool { return frameHeaderLen <= max }
// splitHeaderBlock splits headerBlock into fragments so that each fragment fits
2024-02-18 10:42:21 +00:00
// in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true
2024-02-18 10:42:21 +00:00
// for the first/last fragment, respectively.
2024-02-18 10:42:21 +00:00
func splitHeaderBlock(ctx writeContext, headerBlock []byte, fn func(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error) error {
2024-02-18 10:42:21 +00:00
// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
2024-02-18 10:42:21 +00:00
// that all peers must support (16KB). Later we could care
2024-02-18 10:42:21 +00:00
// more and send larger frames if the peer advertised it, but
2024-02-18 10:42:21 +00:00
// there's little point. Most headers are small anyway (so we
2024-02-18 10:42:21 +00:00
// generally won't have CONTINUATION frames), and extra frames
2024-02-18 10:42:21 +00:00
// only waste 9 bytes anyway.
2024-02-18 10:42:21 +00:00
const maxFrameSize = 16384
first := true
2024-02-18 10:42:21 +00:00
for len(headerBlock) > 0 {
2024-02-18 10:42:21 +00:00
frag := headerBlock
2024-02-18 10:42:21 +00:00
if len(frag) > maxFrameSize {
2024-02-18 10:42:21 +00:00
frag = frag[:maxFrameSize]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
headerBlock = headerBlock[len(frag):]
2024-02-18 10:42:21 +00:00
if err := fn(ctx, frag, first, len(headerBlock) == 0); 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
first = false
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
}
// writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
2024-02-18 10:42:21 +00:00
// for HTTP response headers or trailers from a server handler.
2024-02-18 10:42:21 +00:00
type writeResHeaders struct {
streamID uint32
httpResCode int // 0 means no ":status" line
h http.Header // may be nil
trailers []string // if non-nil, which keys of h to write. nil means all.
endStream bool
date string
contentType string
2024-02-18 10:42:21 +00:00
contentLength string
}
func encKV(enc *hpack.Encoder, k, v string) {
2024-02-18 10:42:21 +00:00
if VerboseLogs {
2024-02-18 10:42:21 +00:00
log.Printf("http2: server encoding header %q = %q", k, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
enc.WriteField(hpack.HeaderField{Name: k, Value: v})
2024-02-18 10:42:21 +00:00
}
func (w *writeResHeaders) staysWithinBuffer(max int) bool {
2024-02-18 10:42:21 +00:00
// TODO: this is a common one. It'd be nice to return true
2024-02-18 10:42:21 +00:00
// here and get into the fast path if we could be clever and
2024-02-18 10:42:21 +00:00
// calculate the size fast enough, or at least a conservative
2024-02-18 10:42:21 +00:00
// upper bound that usually fires. (Maybe if w.h and
2024-02-18 10:42:21 +00:00
// w.trailers are nil, so we don't need to enumerate it.)
2024-02-18 10:42:21 +00:00
// Otherwise I'm afraid that just calculating the length to
2024-02-18 10:42:21 +00:00
// answer this question would be slower than the ~2µs benefit.
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
func (w *writeResHeaders) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
enc, buf := ctx.HeaderEncoder()
2024-02-18 10:42:21 +00:00
buf.Reset()
if w.httpResCode != 0 {
2024-02-18 10:42:21 +00:00
encKV(enc, ":status", httpCodeString(w.httpResCode))
2024-02-18 10:42:21 +00:00
}
encodeHeaders(enc, w.h, w.trailers)
if w.contentType != "" {
2024-02-18 10:42:21 +00:00
encKV(enc, "content-type", w.contentType)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if w.contentLength != "" {
2024-02-18 10:42:21 +00:00
encKV(enc, "content-length", w.contentLength)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if w.date != "" {
2024-02-18 10:42:21 +00:00
encKV(enc, "date", w.date)
2024-02-18 10:42:21 +00:00
}
headerBlock := buf.Bytes()
2024-02-18 10:42:21 +00:00
if len(headerBlock) == 0 && w.trailers == nil {
2024-02-18 10:42:21 +00:00
panic("unexpected empty hpack")
2024-02-18 10:42:21 +00:00
}
return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
2024-02-18 10:42:21 +00:00
}
func (w *writeResHeaders) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
2024-02-18 10:42:21 +00:00
if firstFrag {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteHeaders(HeadersFrameParam{
StreamID: w.streamID,
2024-02-18 10:42:21 +00:00
BlockFragment: frag,
EndStream: w.endStream,
EndHeaders: lastFrag,
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
return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames.
2024-02-18 10:42:21 +00:00
type writePushPromise struct {
streamID uint32 // pusher stream
method string // for :method
url *url.URL // for :scheme, :authority, :path
h http.Header
2024-02-18 10:42:21 +00:00
// Creates an ID for a pushed stream. This runs on serveG just before
2024-02-18 10:42:21 +00:00
// the frame is written. The returned ID is copied to promisedID.
2024-02-18 10:42:21 +00:00
allocatePromisedID func() (uint32, error)
promisedID uint32
2024-02-18 10:42:21 +00:00
}
func (w *writePushPromise) staysWithinBuffer(max int) bool {
2024-02-18 10:42:21 +00:00
// TODO: see writeResHeaders.staysWithinBuffer
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
func (w *writePushPromise) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
enc, buf := ctx.HeaderEncoder()
2024-02-18 10:42:21 +00:00
buf.Reset()
encKV(enc, ":method", w.method)
2024-02-18 10:42:21 +00:00
encKV(enc, ":scheme", w.url.Scheme)
2024-02-18 10:42:21 +00:00
encKV(enc, ":authority", w.url.Host)
2024-02-18 10:42:21 +00:00
encKV(enc, ":path", w.url.RequestURI())
2024-02-18 10:42:21 +00:00
encodeHeaders(enc, w.h, nil)
headerBlock := buf.Bytes()
2024-02-18 10:42:21 +00:00
if len(headerBlock) == 0 {
2024-02-18 10:42:21 +00:00
panic("unexpected empty hpack")
2024-02-18 10:42:21 +00:00
}
return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
2024-02-18 10:42:21 +00:00
}
func (w *writePushPromise) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
2024-02-18 10:42:21 +00:00
if firstFrag {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WritePushPromise(PushPromiseParam{
StreamID: w.streamID,
PromiseID: w.promisedID,
2024-02-18 10:42:21 +00:00
BlockFragment: frag,
EndHeaders: lastFrag,
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
return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
type write100ContinueHeadersFrame struct {
streamID uint32
}
func (w write100ContinueHeadersFrame) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
enc, buf := ctx.HeaderEncoder()
2024-02-18 10:42:21 +00:00
buf.Reset()
2024-02-18 10:42:21 +00:00
encKV(enc, ":status", "100")
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteHeaders(HeadersFrameParam{
StreamID: w.streamID,
2024-02-18 10:42:21 +00:00
BlockFragment: buf.Bytes(),
EndStream: false,
EndHeaders: true,
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (w write100ContinueHeadersFrame) staysWithinBuffer(max int) bool {
2024-02-18 10:42:21 +00:00
// Sloppy but conservative:
2024-02-18 10:42:21 +00:00
return 9+2*(len(":status")+len("100")) <= max
2024-02-18 10:42:21 +00:00
}
type writeWindowUpdate struct {
streamID uint32 // or 0 for conn-level
n uint32
2024-02-18 10:42:21 +00:00
}
func (wu writeWindowUpdate) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
func (wu writeWindowUpdate) writeFrame(ctx writeContext) error {
2024-02-18 10:42:21 +00:00
return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
2024-02-18 10:42:21 +00:00
}
// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
2024-02-18 10:42:21 +00:00
// is encoded only if k is in keys.
2024-02-18 10:42:21 +00:00
func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
2024-02-18 10:42:21 +00:00
if keys == nil {
2024-02-18 10:42:21 +00:00
sorter := sorterPool.Get().(*sorter)
2024-02-18 10:42:21 +00:00
// Using defer here, since the returned keys from the
2024-02-18 10:42:21 +00:00
// sorter.Keys method is only valid until the sorter
2024-02-18 10:42:21 +00:00
// is returned:
2024-02-18 10:42:21 +00:00
defer sorterPool.Put(sorter)
2024-02-18 10:42:21 +00:00
keys = sorter.Keys(h)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, k := range keys {
2024-02-18 10:42:21 +00:00
vv := h[k]
2024-02-18 10:42:21 +00:00
k, ascii := lowerHeader(k)
2024-02-18 10:42:21 +00:00
if !ascii {
2024-02-18 10:42:21 +00:00
// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
2024-02-18 10:42:21 +00:00
// field names have to be ASCII characters (just as in HTTP/1.x).
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
if !validWireHeaderFieldName(k) {
2024-02-18 10:42:21 +00:00
// Skip it as backup paranoia. Per
2024-02-18 10:42:21 +00:00
// golang.org/issue/14048, these should
2024-02-18 10:42:21 +00:00
// already be rejected at a higher level.
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
isTE := k == "transfer-encoding"
2024-02-18 10:42:21 +00:00
for _, v := range vv {
2024-02-18 10:42:21 +00:00
if !httpguts.ValidHeaderFieldValue(v) {
2024-02-18 10:42:21 +00:00
// TODO: return an error? golang.org/issue/14048
2024-02-18 10:42:21 +00:00
// For now just omit it.
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
// TODO: more of "8.1.2.2 Connection-Specific Header Fields"
2024-02-18 10:42:21 +00:00
if isTE && v != "trailers" {
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
encKV(enc, k, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}