1
// Copyright 2014 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// TODO: turn off the serve goroutine when idle, so
6
// an idle conn only has the readFrames goroutine active. (which could
7
// also be optimized probably to pin less memory in crypto/tls). This
8
// would involve tracking when the serve goroutine is active (atomic
9
// int32 read/CAS probably?) and starting it up when frames arrive,
10
// and shutting it down when all handlers exit. the occasional PING
11
// packets could use time.AfterFunc to call sc.wakeStartServeLoop()
12
// (which is a no-op if already running) and then queue the PING write
13
// as normal. The serve loop would then exit in most cases (if no
14
// Handlers running) and not be woken up again until the PING packet
17
// TODO (maybe): add a mechanism for Handlers to going into
18
// half-closed-local mode (rw.(io.Closer) test?) but not exit their
19
// handler, and continue to be able to read from the
20
// Request.Body. This would be a somewhat semantic change from HTTP/1
21
// (or at least what we expose in net/http), so I'd probably want to
22
// add it there too. For now, this package says that returning from
23
// the Handler ServeHTTP function means you're both done reading and
24
// done writing, without a way to stop just one or the other.
50
"golang.org/x/net/http/httpguts"
51
"golang.org/x/net/http2/hpack"
55
prefaceTimeout = 10 * time.Second
56
firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway
57
handlerChunkWriteSize = 4 << 10
58
defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to?
59
maxQueuedControlFrames = 10000
63
errClientDisconnected = errors.New("client disconnected")
64
errClosedBody = errors.New("body closed by handler")
65
errHandlerComplete = errors.New("http2: request body closed due to handler exiting")
66
errStreamClosed = errors.New("http2: stream closed")
69
var responseWriterStatePool = sync.Pool{
70
New: func() interface{} {
71
rws := &responseWriterState{}
72
rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize)
80
testHookGetServerConn func(*serverConn)
81
testHookOnPanicMu *sync.Mutex // nil except in tests
82
testHookOnPanic func(sc *serverConn, panicVal interface{}) (rePanic bool)
85
// Server is an HTTP/2 server.
87
// MaxHandlers limits the number of http.Handler ServeHTTP goroutines
88
// which may run at a time over all connections.
89
// Negative or zero no limit.
93
// MaxConcurrentStreams optionally specifies the number of
94
// concurrent streams that each client may have open at a
95
// time. This is unrelated to the number of http.Handler goroutines
96
// which may be active globally, which is MaxHandlers.
97
// If zero, MaxConcurrentStreams defaults to at least 100, per
98
// the HTTP/2 spec's recommendations.
99
MaxConcurrentStreams uint32
101
// MaxDecoderHeaderTableSize optionally specifies the http2
102
// SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It
103
// informs the remote endpoint of the maximum size of the header compression
104
// table used to decode header blocks, in octets. If zero, the default value
106
MaxDecoderHeaderTableSize uint32
108
// MaxEncoderHeaderTableSize optionally specifies an upper limit for the
109
// header compression table used for encoding request headers. Received
110
// SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero,
111
// the default value of 4096 is used.
112
MaxEncoderHeaderTableSize uint32
114
// MaxReadFrameSize optionally specifies the largest frame
115
// this server is willing to read. A valid value is between
116
// 16k and 16M, inclusive. If zero or otherwise invalid, a
117
// default value is used.
118
MaxReadFrameSize uint32
120
// PermitProhibitedCipherSuites, if true, permits the use of
121
// cipher suites prohibited by the HTTP/2 spec.
122
PermitProhibitedCipherSuites bool
124
// IdleTimeout specifies how long until idle clients should be
125
// closed with a GOAWAY frame. PING frames are not considered
126
// activity for the purposes of IdleTimeout.
127
IdleTimeout time.Duration
129
// MaxUploadBufferPerConnection is the size of the initial flow
130
// control window for each connections. The HTTP/2 spec does not
131
// allow this to be smaller than 65535 or larger than 2^32-1.
132
// If the value is outside this range, a default value will be
134
MaxUploadBufferPerConnection int32
136
// MaxUploadBufferPerStream is the size of the initial flow control
137
// window for each stream. The HTTP/2 spec does not allow this to
138
// be larger than 2^32-1. If the value is zero or larger than the
139
// maximum, a default value will be used instead.
140
MaxUploadBufferPerStream int32
142
// NewWriteScheduler constructs a write scheduler for a connection.
143
// If nil, a default scheduler is chosen.
144
NewWriteScheduler func() WriteScheduler
146
// CountError, if non-nil, is called on HTTP/2 server errors.
147
// It's intended to increment a metric for monitoring, such
148
// as an expvar or Prometheus metric.
149
// The errType consists of only ASCII word characters.
150
CountError func(errType string)
152
// Internal state. This is a pointer (rather than embedded directly)
153
// so that we don't embed a Mutex in this struct, which will make the
154
// struct non-copyable, which might break some callers.
155
state *serverInternalState
158
func (s *Server) initialConnRecvWindowSize() int32 {
159
if s.MaxUploadBufferPerConnection >= initialWindowSize {
160
return s.MaxUploadBufferPerConnection
165
func (s *Server) initialStreamRecvWindowSize() int32 {
166
if s.MaxUploadBufferPerStream > 0 {
167
return s.MaxUploadBufferPerStream
172
func (s *Server) maxReadFrameSize() uint32 {
173
if v := s.MaxReadFrameSize; v >= minMaxFrameSize && v <= maxFrameSize {
176
return defaultMaxReadFrameSize
179
func (s *Server) maxConcurrentStreams() uint32 {
180
if v := s.MaxConcurrentStreams; v > 0 {
183
return defaultMaxStreams
186
func (s *Server) maxDecoderHeaderTableSize() uint32 {
187
if v := s.MaxDecoderHeaderTableSize; v > 0 {
190
return initialHeaderTableSize
193
func (s *Server) maxEncoderHeaderTableSize() uint32 {
194
if v := s.MaxEncoderHeaderTableSize; v > 0 {
197
return initialHeaderTableSize
200
// maxQueuedControlFrames is the maximum number of control frames like
201
// SETTINGS, PING and RST_STREAM that will be queued for writing before
202
// the connection is closed to prevent memory exhaustion attacks.
203
func (s *Server) maxQueuedControlFrames() int {
204
// TODO: if anybody asks, add a Server field, and remember to define the
205
// behavior of negative values.
206
return maxQueuedControlFrames
209
type serverInternalState struct {
211
activeConns map[*serverConn]struct{}
214
func (s *serverInternalState) registerConn(sc *serverConn) {
216
return // if the Server was used without calling ConfigureServer
219
s.activeConns[sc] = struct{}{}
223
func (s *serverInternalState) unregisterConn(sc *serverConn) {
225
return // if the Server was used without calling ConfigureServer
228
delete(s.activeConns, sc)
232
func (s *serverInternalState) startGracefulShutdown() {
234
return // if the Server was used without calling ConfigureServer
237
for sc := range s.activeConns {
238
sc.startGracefulShutdown()
243
// ConfigureServer adds HTTP/2 support to a net/http Server.
245
// The configuration conf may be nil.
247
// ConfigureServer must be called before s begins serving.
248
func ConfigureServer(s *http.Server, conf *Server) error {
250
panic("nil *http.Server")
255
conf.state = &serverInternalState{activeConns: make(map[*serverConn]struct{})}
256
if h1, h2 := s, conf; h2.IdleTimeout == 0 {
257
if h1.IdleTimeout != 0 {
258
h2.IdleTimeout = h1.IdleTimeout
260
h2.IdleTimeout = h1.ReadTimeout
263
s.RegisterOnShutdown(conf.state.startGracefulShutdown)
265
if s.TLSConfig == nil {
266
s.TLSConfig = new(tls.Config)
267
} else if s.TLSConfig.CipherSuites != nil && s.TLSConfig.MinVersion < tls.VersionTLS13 {
268
// If they already provided a TLS 1.0–1.2 CipherSuite list, return an
269
// error if it is missing ECDHE_RSA_WITH_AES_128_GCM_SHA256 or
270
// ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
271
haveRequired := false
272
for _, cs := range s.TLSConfig.CipherSuites {
274
case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
275
// Alternative MTI cipher to not discourage ECDSA-only servers.
276
// See http://golang.org/cl/30721 for further information.
277
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
282
return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher (need at least one of TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 or TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)")
286
// Note: not setting MinVersion to tls.VersionTLS12,
287
// as we don't want to interfere with HTTP/1.1 traffic
288
// on the user's server. We enforce TLS 1.2 later once
289
// we accept a connection. Ideally this should be done
290
// during next-proto selection, but using TLS <1.2 with
291
// HTTP/2 is still the client's bug.
293
s.TLSConfig.PreferServerCipherSuites = true
295
if !strSliceContains(s.TLSConfig.NextProtos, NextProtoTLS) {
296
s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, NextProtoTLS)
298
if !strSliceContains(s.TLSConfig.NextProtos, "http/1.1") {
299
s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "http/1.1")
302
if s.TLSNextProto == nil {
303
s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){}
305
protoHandler := func(hs *http.Server, c *tls.Conn, h http.Handler) {
306
if testHookOnConn != nil {
309
// The TLSNextProto interface predates contexts, so
310
// the net/http package passes down its per-connection
311
// base context via an exported but unadvertised
312
// method on the Handler. This is for internal
313
// net/http<=>http2 use only.
314
var ctx context.Context
315
type baseContexter interface {
316
BaseContext() context.Context
318
if bc, ok := h.(baseContexter); ok {
319
ctx = bc.BaseContext()
321
conf.ServeConn(c, &ServeConnOpts{
327
s.TLSNextProto[NextProtoTLS] = protoHandler
331
// ServeConnOpts are options for the Server.ServeConn method.
332
type ServeConnOpts struct {
333
// Context is the base context to use.
334
// If nil, context.Background is used.
335
Context context.Context
337
// BaseConfig optionally sets the base configuration
338
// for values. If nil, defaults are used.
339
BaseConfig *http.Server
341
// Handler specifies which handler to use for processing
342
// requests. If nil, BaseConfig.Handler is used. If BaseConfig
343
// or BaseConfig.Handler is nil, http.DefaultServeMux is used.
346
// UpgradeRequest is an initial request received on a connection
347
// undergoing an h2c upgrade. The request body must have been
348
// completely read from the connection before calling ServeConn,
349
// and the 101 Switching Protocols response written.
350
UpgradeRequest *http.Request
352
// Settings is the decoded contents of the HTTP2-Settings header
353
// in an h2c upgrade request.
356
// SawClientPreface is set if the HTTP/2 connection preface
357
// has already been read from the connection.
358
SawClientPreface bool
361
func (o *ServeConnOpts) context() context.Context {
362
if o != nil && o.Context != nil {
365
return context.Background()
368
func (o *ServeConnOpts) baseConfig() *http.Server {
369
if o != nil && o.BaseConfig != nil {
372
return new(http.Server)
375
func (o *ServeConnOpts) handler() http.Handler {
377
if o.Handler != nil {
380
if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
381
return o.BaseConfig.Handler
384
return http.DefaultServeMux
387
// ServeConn serves HTTP/2 requests on the provided connection and
388
// blocks until the connection is no longer readable.
390
// ServeConn starts speaking HTTP/2 assuming that c has not had any
391
// reads or writes. It writes its initial settings frame and expects
392
// to be able to read the preface and settings frame from the
393
// client. If c has a ConnectionState method like a *tls.Conn, the
394
// ConnectionState is used to verify the TLS ciphersuite and to set
395
// the Request.TLS field in Handlers.
397
// ServeConn does not support h2c by itself. Any h2c support must be
398
// implemented in terms of providing a suitably-behaving net.Conn.
400
// The opts parameter is optional. If nil, default values are used.
401
func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) {
402
baseCtx, cancel := serverConnBaseContext(c, opts)
407
hs: opts.baseConfig(),
410
remoteAddrStr: c.RemoteAddr().String(),
411
bw: newBufferedWriter(c),
412
handler: opts.handler(),
413
streams: make(map[uint32]*stream),
414
readFrameCh: make(chan readFrameResult),
415
wantWriteFrameCh: make(chan FrameWriteRequest, 8),
416
serveMsgCh: make(chan interface{}, 8),
417
wroteFrameCh: make(chan frameWriteResult, 1), // buffered; one send in writeFrameAsync
418
bodyReadCh: make(chan bodyReadMsg), // buffering doesn't matter either way
419
doneServing: make(chan struct{}),
420
clientMaxStreams: math.MaxUint32, // Section 6.5.2: "Initially, there is no limit to this value"
421
advMaxStreams: s.maxConcurrentStreams(),
422
initialStreamSendWindowSize: initialWindowSize,
423
maxFrameSize: initialMaxFrameSize,
424
serveG: newGoroutineLock(),
426
sawClientPreface: opts.SawClientPreface,
429
s.state.registerConn(sc)
430
defer s.state.unregisterConn(sc)
432
// The net/http package sets the write deadline from the
433
// http.Server.WriteTimeout during the TLS handshake, but then
434
// passes the connection off to us with the deadline already set.
435
// Write deadlines are set per stream in serverConn.newStream.
436
// Disarm the net.Conn write deadline here.
437
if sc.hs.WriteTimeout != 0 {
438
sc.conn.SetWriteDeadline(time.Time{})
441
if s.NewWriteScheduler != nil {
442
sc.writeSched = s.NewWriteScheduler()
444
sc.writeSched = NewPriorityWriteScheduler(nil)
447
// These start at the RFC-specified defaults. If there is a higher
448
// configured value for inflow, that will be updated when we send a
449
// WINDOW_UPDATE shortly after sending SETTINGS.
450
sc.flow.add(initialWindowSize)
451
sc.inflow.init(initialWindowSize)
452
sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
453
sc.hpackEncoder.SetMaxDynamicTableSizeLimit(s.maxEncoderHeaderTableSize())
455
fr := NewFramer(sc.bw, c)
456
if s.CountError != nil {
457
fr.countError = s.CountError
459
fr.ReadMetaHeaders = hpack.NewDecoder(s.maxDecoderHeaderTableSize(), nil)
460
fr.MaxHeaderListSize = sc.maxHeaderListSize()
461
fr.SetMaxReadFrameSize(s.maxReadFrameSize())
464
if tc, ok := c.(connectionStater); ok {
465
sc.tlsState = new(tls.ConnectionState)
466
*sc.tlsState = tc.ConnectionState()
467
// 9.2 Use of TLS Features
468
// An implementation of HTTP/2 over TLS MUST use TLS
469
// 1.2 or higher with the restrictions on feature set
470
// and cipher suite described in this section. Due to
471
// implementation limitations, it might not be
472
// possible to fail TLS negotiation. An endpoint MUST
473
// immediately terminate an HTTP/2 connection that
474
// does not meet the TLS requirements described in
475
// this section with a connection error (Section
476
// 5.4.1) of type INADEQUATE_SECURITY.
477
if sc.tlsState.Version < tls.VersionTLS12 {
478
sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low")
482
if sc.tlsState.ServerName == "" {
483
// Client must use SNI, but we don't enforce that anymore,
484
// since it was causing problems when connecting to bare IP
485
// addresses during development.
487
// TODO: optionally enforce? Or enforce at the time we receive
488
// a new request, and verify the ServerName matches the :authority?
489
// But that precludes proxy situations, perhaps.
491
// So for now, do nothing here again.
494
if !s.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) {
495
// "Endpoints MAY choose to generate a connection error
496
// (Section 5.4.1) of type INADEQUATE_SECURITY if one of
497
// the prohibited cipher suites are negotiated."
499
// We choose that. In my opinion, the spec is weak
500
// here. It also says both parties must support at least
501
// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no
502
// excuses here. If we really must, we could allow an
503
// "AllowInsecureWeakCiphers" option on the server later.
504
// Let's see how it plays out first.
505
sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
510
if opts.Settings != nil {
511
fr := &SettingsFrame{
512
FrameHeader: FrameHeader{valid: true},
515
if err := fr.ForeachSetting(sc.processSetting); err != nil {
516
sc.rejectConn(ErrCodeProtocol, "invalid settings")
522
if hook := testHookGetServerConn; hook != nil {
526
if opts.UpgradeRequest != nil {
527
sc.upgradeRequest(opts.UpgradeRequest)
528
opts.UpgradeRequest = nil
534
func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) {
535
ctx, cancel = context.WithCancel(opts.context())
536
ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
537
if hs := opts.baseConfig(); hs != nil {
538
ctx = context.WithValue(ctx, http.ServerContextKey, hs)
543
func (sc *serverConn) rejectConn(err ErrCode, debug string) {
544
sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
545
// ignoring errors. hanging up anyway.
546
sc.framer.WriteGoAway(0, err, []byte(debug))
551
type serverConn struct {
556
bw *bufferedWriter // writing to conn
558
baseCtx context.Context
560
doneServing chan struct{} // closed when serverConn.serve ends
561
readFrameCh chan readFrameResult // written by serverConn.readFrames
562
wantWriteFrameCh chan FrameWriteRequest // from handlers -> serve
563
wroteFrameCh chan frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes
564
bodyReadCh chan bodyReadMsg // from handlers -> serve
565
serveMsgCh chan interface{} // misc messages & code to send to / run on the serve loop
566
flow outflow // conn-wide (not stream-specific) outbound flow control
567
inflow inflow // conn-wide inbound flow control
568
tlsState *tls.ConnectionState // shared by all handlers, like net/http
570
writeSched WriteScheduler
572
// Everything following is owned by the serve loop; use serveG.check():
573
serveG goroutineLock // used to verify funcs are on serve()
575
sawClientPreface bool // preface has already been read, used in h2c upgrade
576
sawFirstSettings bool // got the initial SETTINGS frame after the preface
577
needToSendSettingsAck bool
578
unackedSettings int // how many SETTINGS have we sent without ACKs?
579
queuedControlFrames int // control frames in the writeSched queue
580
clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
581
advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
582
curClientStreams uint32 // number of open streams initiated by the client
583
curPushedStreams uint32 // number of open streams initiated by server push
584
maxClientStreamID uint32 // max ever seen from client (odd), or 0 if there have been no client requests
585
maxPushPromiseID uint32 // ID of the last push promise (even), or 0 if there have been no pushes
586
streams map[uint32]*stream
587
initialStreamSendWindowSize int32
589
peerMaxHeaderListSize uint32 // zero means unknown (default)
590
canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case
591
canonHeaderKeysSize int // canonHeader keys size in bytes
592
writingFrame bool // started writing a frame (on serve goroutine or separate)
593
writingFrameAsync bool // started a frame on its own goroutine but haven't heard back on wroteFrameCh
594
needsFrameFlush bool // last frame write wasn't a flush
595
inGoAway bool // we've started to or sent GOAWAY
596
inFrameScheduleLoop bool // whether we're in the scheduleFrameWrite loop
597
needToSendGoAway bool // we need to schedule a GOAWAY frame write
599
shutdownTimer *time.Timer // nil until used
600
idleTimer *time.Timer // nil if unused
602
// Owned by the writeFrameAsync goroutine:
603
headerWriteBuf bytes.Buffer
604
hpackEncoder *hpack.Encoder
606
// Used by startGracefulShutdown.
607
shutdownOnce sync.Once
610
func (sc *serverConn) maxHeaderListSize() uint32 {
611
n := sc.hs.MaxHeaderBytes
613
n = http.DefaultMaxHeaderBytes
615
// http2's count is in a slightly different unit and includes 32 bytes per pair.
616
// So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
617
const perFieldOverhead = 32 // per http2 spec
618
const typicalHeaders = 10 // conservative
619
return uint32(n + typicalHeaders*perFieldOverhead)
622
func (sc *serverConn) curOpenStreams() uint32 {
624
return sc.curClientStreams + sc.curPushedStreams
627
// stream represents a stream. This is the minimal metadata needed by
628
// the serve goroutine. Most of the actual stream state is owned by
629
// the http.Handler's goroutine in the responseWriter. Because the
630
// responseWriter's responseWriterState is recycled at the end of a
631
// handler, this struct intentionally has no pointer to the
632
// *responseWriter{,State} itself, as the Handler ending nils out the
633
// responseWriter's state field.
638
body *pipe // non-nil if expecting DATA frames
639
cw closeWaiter // closed wait stream transitions to closed state
643
// owned by serverConn's serve loop:
644
bodyBytes int64 // body bytes seen so far
645
declBodyBytes int64 // or -1 if undeclared
646
flow outflow // limits writing from Handler to client
647
inflow inflow // what the client is allowed to POST/etc to us
649
resetQueued bool // RST_STREAM queued for write; set by sc.resetStream
650
gotTrailerHeader bool // HEADER frame for trailers was seen
651
wroteHeaders bool // whether we wrote headers (not status 100)
652
readDeadline *time.Timer // nil if unused
653
writeDeadline *time.Timer // nil if unused
654
closeErr error // set before cw is closed
656
trailer http.Header // accumulated trailers
657
reqTrailer http.Header // handler's Request.Trailer
660
func (sc *serverConn) Framer() *Framer { return sc.framer }
661
func (sc *serverConn) CloseConn() error { return sc.conn.Close() }
662
func (sc *serverConn) Flush() error { return sc.bw.Flush() }
663
func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
664
return sc.hpackEncoder, &sc.headerWriteBuf
667
func (sc *serverConn) state(streamID uint32) (streamState, *stream) {
669
// http://tools.ietf.org/html/rfc7540#section-5.1
670
if st, ok := sc.streams[streamID]; ok {
673
// "The first use of a new stream identifier implicitly closes all
674
// streams in the "idle" state that might have been initiated by
675
// that peer with a lower-valued stream identifier. For example, if
676
// a client sends a HEADERS frame on stream 7 without ever sending a
677
// frame on stream 5, then stream 5 transitions to the "closed"
678
// state when the first frame for stream 7 is sent or received."
680
if streamID <= sc.maxClientStreamID {
681
return stateClosed, nil
684
if streamID <= sc.maxPushPromiseID {
685
return stateClosed, nil
688
return stateIdle, nil
691
// setConnState calls the net/http ConnState hook for this connection, if configured.
692
// Note that the net/http package does StateNew and StateClosed for us.
693
// There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
694
func (sc *serverConn) setConnState(state http.ConnState) {
695
if sc.hs.ConnState != nil {
696
sc.hs.ConnState(sc.conn, state)
700
func (sc *serverConn) vlogf(format string, args ...interface{}) {
702
sc.logf(format, args...)
706
func (sc *serverConn) logf(format string, args ...interface{}) {
707
if lg := sc.hs.ErrorLog; lg != nil {
708
lg.Printf(format, args...)
710
log.Printf(format, args...)
714
// errno returns v's underlying uintptr, else 0.
716
// TODO: remove this helper function once http2 can use build
717
// tags. See comment in isClosedConnError.
718
func errno(v error) uintptr {
719
if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
720
return uintptr(rv.Uint())
725
// isClosedConnError reports whether err is an error from use of a closed
726
// network connection.
727
func isClosedConnError(err error) bool {
732
// TODO: remove this string search and be more like the Windows
733
// case below. That might involve modifying the standard library
734
// to return better error types.
736
if strings.Contains(str, "use of closed network connection") {
740
// TODO(bradfitz): x/tools/cmd/bundle doesn't really support
741
// build tags, so I can't make an http2_windows.go file with
742
// Windows-specific stuff. Fix that and move this, once we
743
// have a way to bundle this into std's net/http somehow.
744
if runtime.GOOS == "windows" {
745
if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
746
if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
747
const WSAECONNABORTED = 10053
748
const WSAECONNRESET = 10054
749
if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
758
func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
762
if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) || err == errPrefaceTimeout {
763
// Boring, expected errors.
764
sc.vlogf(format, args...)
766
sc.logf(format, args...)
770
// maxCachedCanonicalHeadersKeysSize is an arbitrarily-chosen limit on the size
771
// of the entries in the canonHeader cache.
772
// This should be larger than the size of unique, uncommon header keys likely to
773
// be sent by the peer, while not so high as to permit unreasonable memory usage
774
// if the peer sends an unbounded number of unique header keys.
775
const maxCachedCanonicalHeadersKeysSize = 2048
777
func (sc *serverConn) canonicalHeader(v string) string {
779
buildCommonHeaderMapsOnce()
780
cv, ok := commonCanonHeader[v]
784
cv, ok = sc.canonHeader[v]
788
if sc.canonHeader == nil {
789
sc.canonHeader = make(map[string]string)
791
cv = http.CanonicalHeaderKey(v)
792
size := 100 + len(v)*2 // 100 bytes of map overhead + key + value
793
if sc.canonHeaderKeysSize+size <= maxCachedCanonicalHeadersKeysSize {
794
sc.canonHeader[v] = cv
795
sc.canonHeaderKeysSize += size
800
type readFrameResult struct {
801
f Frame // valid until readMore is called
804
// readMore should be called once the consumer no longer needs or
805
// retains f. After readMore, f is invalid and more frames can be
810
// readFrames is the loop that reads incoming frames.
811
// It takes care to only read one frame at a time, blocking until the
812
// consumer is done with the frame.
813
// It's run on its own goroutine.
814
func (sc *serverConn) readFrames() {
816
gateDone := gate.Done
818
f, err := sc.framer.ReadFrame()
820
case sc.readFrameCh <- readFrameResult{f, err, gateDone}:
821
case <-sc.doneServing:
826
case <-sc.doneServing:
829
if terminalReadFrameError(err) {
835
// frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
836
type frameWriteResult struct {
838
wr FrameWriteRequest // what was written (or attempted)
839
err error // result of the writeFrame call
842
// writeFrameAsync runs in its own goroutine and writes a single frame
843
// and then reports when it's done.
844
// At most one goroutine can be running writeFrameAsync at a time per
846
func (sc *serverConn) writeFrameAsync(wr FrameWriteRequest, wd *writeData) {
849
err = wr.write.writeFrame(sc)
851
err = sc.framer.endWrite()
853
sc.wroteFrameCh <- frameWriteResult{wr: wr, err: err}
856
func (sc *serverConn) closeAllStreamsOnConnClose() {
858
for _, st := range sc.streams {
859
sc.closeStream(st, errClientDisconnected)
863
func (sc *serverConn) stopShutdownTimer() {
865
if t := sc.shutdownTimer; t != nil {
870
func (sc *serverConn) notePanic() {
871
// Note: this is for serverConn.serve panicking, not http.Handler code.
872
if testHookOnPanicMu != nil {
873
testHookOnPanicMu.Lock()
874
defer testHookOnPanicMu.Unlock()
876
if testHookOnPanic != nil {
877
if e := recover(); e != nil {
878
if testHookOnPanic(sc, e) {
885
func (sc *serverConn) serve() {
888
defer sc.conn.Close()
889
defer sc.closeAllStreamsOnConnClose()
890
defer sc.stopShutdownTimer()
891
defer close(sc.doneServing) // unblocks handlers trying to send
894
sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
897
sc.writeFrame(FrameWriteRequest{
898
write: writeSettings{
899
{SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
900
{SettingMaxConcurrentStreams, sc.advMaxStreams},
901
{SettingMaxHeaderListSize, sc.maxHeaderListSize()},
902
{SettingHeaderTableSize, sc.srv.maxDecoderHeaderTableSize()},
903
{SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())},
908
// Each connection starts with initialWindowSize inflow tokens.
909
// If a higher value is configured, we add more tokens.
910
if diff := sc.srv.initialConnRecvWindowSize() - initialWindowSize; diff > 0 {
911
sc.sendWindowUpdate(nil, int(diff))
914
if err := sc.readPreface(); err != nil {
915
sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
918
// Now that we've got the preface, get us out of the
919
// "StateNew" state. We can't go directly to idle, though.
920
// Active means we read some data and anticipate a request. We'll
921
// do another Active when we get a HEADERS frame.
922
sc.setConnState(http.StateActive)
923
sc.setConnState(http.StateIdle)
925
if sc.srv.IdleTimeout != 0 {
926
sc.idleTimer = time.AfterFunc(sc.srv.IdleTimeout, sc.onIdleTimer)
927
defer sc.idleTimer.Stop()
930
go sc.readFrames() // closed by defer sc.conn.Close above
932
settingsTimer := time.AfterFunc(firstSettingsTimeout, sc.onSettingsTimer)
933
defer settingsTimer.Stop()
939
case wr := <-sc.wantWriteFrameCh:
940
if se, ok := wr.write.(StreamError); ok {
945
case res := <-sc.wroteFrameCh:
947
case res := <-sc.readFrameCh:
948
// Process any written frames before reading new frames from the client since a
949
// written frame could have triggered a new stream to be started.
950
if sc.writingFrameAsync {
952
case wroteRes := <-sc.wroteFrameCh:
953
sc.wroteFrame(wroteRes)
957
if !sc.processFrameFromReader(res) {
961
if settingsTimer != nil {
965
case m := <-sc.bodyReadCh:
966
sc.noteBodyRead(m.st, m.n)
967
case msg := <-sc.serveMsgCh:
968
switch v := msg.(type) {
970
v(loopNum) // for testing
973
case settingsTimerMsg:
974
sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
977
sc.vlogf("connection is idle")
979
case shutdownTimerMsg:
980
sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
982
case gracefulShutdownMsg:
983
sc.startGracefulShutdownInternal()
985
panic("unknown timer")
987
case *startPushRequest:
989
case func(*serverConn):
992
panic(fmt.Sprintf("unexpected type %T", v))
996
// If the peer is causing us to generate a lot of control frames,
997
// but not reading them from us, assume they are trying to make us
998
// run out of memory.
999
if sc.queuedControlFrames > sc.srv.maxQueuedControlFrames() {
1000
sc.vlogf("http2: too many control frames in send queue, closing connection")
1004
// Start the shutdown timer after sending a GOAWAY. When sending GOAWAY
1005
// with no error code (graceful shutdown), don't start the timer until
1006
// all open streams have been completed.
1007
sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame
1008
gracefulShutdownComplete := sc.goAwayCode == ErrCodeNo && sc.curOpenStreams() == 0
1009
if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != ErrCodeNo || gracefulShutdownComplete) {
1010
sc.shutDownIn(goAwayTimeout)
1015
func (sc *serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) {
1017
case <-sc.doneServing:
1023
type serverMessage int
1025
// Message values sent to serveMsgCh.
1027
settingsTimerMsg = new(serverMessage)
1028
idleTimerMsg = new(serverMessage)
1029
shutdownTimerMsg = new(serverMessage)
1030
gracefulShutdownMsg = new(serverMessage)
1033
func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) }
1034
func (sc *serverConn) onIdleTimer() { sc.sendServeMsg(idleTimerMsg) }
1035
func (sc *serverConn) onShutdownTimer() { sc.sendServeMsg(shutdownTimerMsg) }
1037
func (sc *serverConn) sendServeMsg(msg interface{}) {
1038
sc.serveG.checkNotOn() // NOT
1040
case sc.serveMsgCh <- msg:
1041
case <-sc.doneServing:
1045
var errPrefaceTimeout = errors.New("timeout waiting for client preface")
1047
// readPreface reads the ClientPreface greeting from the peer or
1048
// returns errPrefaceTimeout on timeout, or an error if the greeting
1050
func (sc *serverConn) readPreface() error {
1051
if sc.sawClientPreface {
1054
errc := make(chan error, 1)
1056
// Read the client preface
1057
buf := make([]byte, len(ClientPreface))
1058
if _, err := io.ReadFull(sc.conn, buf); err != nil {
1060
} else if !bytes.Equal(buf, clientPreface) {
1061
errc <- fmt.Errorf("bogus greeting %q", buf)
1066
timer := time.NewTimer(prefaceTimeout) // TODO: configurable on *Server?
1070
return errPrefaceTimeout
1074
sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
1081
var errChanPool = sync.Pool{
1082
New: func() interface{} { return make(chan error, 1) },
1085
var writeDataPool = sync.Pool{
1086
New: func() interface{} { return new(writeData) },
1089
// writeDataFromHandler writes DATA response frames from a handler on
1091
func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error {
1092
ch := errChanPool.Get().(chan error)
1093
writeArg := writeDataPool.Get().(*writeData)
1094
*writeArg = writeData{stream.id, data, endStream}
1095
err := sc.writeFrameFromHandler(FrameWriteRequest{
1103
var frameWriteDone bool // the frame write is done (successfully or not)
1106
frameWriteDone = true
1107
case <-sc.doneServing:
1108
return errClientDisconnected
1110
// If both ch and stream.cw were ready (as might
1111
// happen on the final Write after an http.Handler
1112
// ends), prefer the write result. Otherwise this
1113
// might just be us successfully closing the stream.
1114
// The writeFrameAsync and serve goroutines guarantee
1115
// that the ch send will happen before the stream.cw
1119
frameWriteDone = true
1121
return errStreamClosed
1126
writeDataPool.Put(writeArg)
1131
// writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts
1132
// if the connection has gone away.
1134
// This must not be run from the serve goroutine itself, else it might
1135
// deadlock writing to sc.wantWriteFrameCh (which is only mildly
1136
// buffered and is read by serve itself). If you're on the serve
1137
// goroutine, call writeFrame instead.
1138
func (sc *serverConn) writeFrameFromHandler(wr FrameWriteRequest) error {
1139
sc.serveG.checkNotOn() // NOT
1141
case sc.wantWriteFrameCh <- wr:
1143
case <-sc.doneServing:
1144
// Serve loop is gone.
1145
// Client has closed their connection to the server.
1146
return errClientDisconnected
1150
// writeFrame schedules a frame to write and sends it if there's nothing
1151
// already being written.
1153
// There is no pushback here (the serve goroutine never blocks). It's
1154
// the http.Handlers that block, waiting for their previous frames to
1155
// make it onto the wire
1157
// If you're not on the serve goroutine, use writeFrameFromHandler instead.
1158
func (sc *serverConn) writeFrame(wr FrameWriteRequest) {
1161
// If true, wr will not be written and wr.done will not be signaled.
1162
var ignoreWrite bool
1164
// We are not allowed to write frames on closed streams. RFC 7540 Section
1165
// 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on
1166
// a closed stream." Our server never sends PRIORITY, so that exception
1169
// The serverConn might close an open stream while the stream's handler
1170
// is still running. For example, the server might close a stream when it
1171
// receives bad data from the client. If this happens, the handler might
1172
// attempt to write a frame after the stream has been closed (since the
1173
// handler hasn't yet been notified of the close). In this case, we simply
1174
// ignore the frame. The handler will notice that the stream is closed when
1175
// it waits for the frame to be written.
1177
// As an exception to this rule, we allow sending RST_STREAM after close.
1178
// This allows us to immediately reject new streams without tracking any
1179
// state for those streams (except for the queued RST_STREAM frame). This
1180
// may result in duplicate RST_STREAMs in some cases, but the client should
1182
if wr.StreamID() != 0 {
1183
_, isReset := wr.write.(StreamError)
1184
if state, _ := sc.state(wr.StreamID()); state == stateClosed && !isReset {
1189
// Don't send a 100-continue response if we've already sent headers.
1190
// See golang.org/issue/14030.
1191
switch wr.write.(type) {
1192
case *writeResHeaders:
1193
wr.stream.wroteHeaders = true
1194
case write100ContinueHeadersFrame:
1195
if wr.stream.wroteHeaders {
1196
// We do not need to notify wr.done because this frame is
1197
// never written with wr.done != nil.
1199
panic("wr.done != nil for write100ContinueHeadersFrame")
1207
sc.queuedControlFrames++
1208
// For extra safety, detect wraparounds, which should not happen,
1209
// and pull the plug.
1210
if sc.queuedControlFrames < 0 {
1214
sc.writeSched.Push(wr)
1216
sc.scheduleFrameWrite()
1219
// startFrameWrite starts a goroutine to write wr (in a separate
1220
// goroutine since that might block on the network), and updates the
1221
// serve goroutine's state about the world, updated from info in wr.
1222
func (sc *serverConn) startFrameWrite(wr FrameWriteRequest) {
1224
if sc.writingFrame {
1225
panic("internal error: can only be writing one frame at a time")
1231
case stateHalfClosedLocal:
1232
switch wr.write.(type) {
1233
case StreamError, handlerPanicRST, writeWindowUpdate:
1234
// RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE
1235
// in this state. (We never send PRIORITY from the server, so that is not checked.)
1237
panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
1240
panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
1243
if wpp, ok := wr.write.(*writePushPromise); ok {
1245
wpp.promisedID, err = wpp.allocatePromisedID()
1247
sc.writingFrameAsync = false
1248
wr.replyToWriter(err)
1253
sc.writingFrame = true
1254
sc.needsFrameFlush = true
1255
if wr.write.staysWithinBuffer(sc.bw.Available()) {
1256
sc.writingFrameAsync = false
1257
err := wr.write.writeFrame(sc)
1258
sc.wroteFrame(frameWriteResult{wr: wr, err: err})
1259
} else if wd, ok := wr.write.(*writeData); ok {
1260
// Encode the frame in the serve goroutine, to ensure we don't have
1261
// any lingering asynchronous references to data passed to Write.
1262
// See https://go.dev/issue/58446.
1263
sc.framer.startWriteDataPadded(wd.streamID, wd.endStream, wd.p, nil)
1264
sc.writingFrameAsync = true
1265
go sc.writeFrameAsync(wr, wd)
1267
sc.writingFrameAsync = true
1268
go sc.writeFrameAsync(wr, nil)
1272
// errHandlerPanicked is the error given to any callers blocked in a read from
1273
// Request.Body when the main goroutine panics. Since most handlers read in the
1274
// main ServeHTTP goroutine, this will show up rarely.
1275
var errHandlerPanicked = errors.New("http2: handler panicked")
1277
// wroteFrame is called on the serve goroutine with the result of
1278
// whatever happened on writeFrameAsync.
1279
func (sc *serverConn) wroteFrame(res frameWriteResult) {
1281
if !sc.writingFrame {
1282
panic("internal error: expected to be already writing a frame")
1284
sc.writingFrame = false
1285
sc.writingFrameAsync = false
1289
if writeEndsStream(wr.write) {
1292
panic("internal error: expecting non-nil stream")
1296
// Here we would go to stateHalfClosedLocal in
1297
// theory, but since our handler is done and
1298
// the net/http package provides no mechanism
1299
// for closing a ResponseWriter while still
1300
// reading data (see possible TODO at top of
1301
// this file), we go into closed state here
1302
// anyway, after telling the peer we're
1303
// hanging up on them. We'll transition to
1304
// stateClosed after the RST_STREAM frame is
1306
st.state = stateHalfClosedLocal
1307
// Section 8.1: a server MAY request that the client abort
1308
// transmission of a request without error by sending a
1309
// RST_STREAM with an error code of NO_ERROR after sending
1310
// a complete response.
1311
sc.resetStream(streamError(st.id, ErrCodeNo))
1312
case stateHalfClosedRemote:
1313
sc.closeStream(st, errHandlerComplete)
1316
switch v := wr.write.(type) {
1318
// st may be unknown if the RST_STREAM was generated to reject bad input.
1319
if st, ok := sc.streams[v.StreamID]; ok {
1320
sc.closeStream(st, v)
1322
case handlerPanicRST:
1323
sc.closeStream(wr.stream, errHandlerPanicked)
1327
// Reply (if requested) to unblock the ServeHTTP goroutine.
1328
wr.replyToWriter(res.err)
1330
sc.scheduleFrameWrite()
1333
// scheduleFrameWrite tickles the frame writing scheduler.
1335
// If a frame is already being written, nothing happens. This will be called again
1336
// when the frame is done being written.
1338
// If a frame isn't being written and we need to send one, the best frame
1339
// to send is selected by writeSched.
1341
// If a frame isn't being written and there's nothing else to send, we
1342
// flush the write buffer.
1343
func (sc *serverConn) scheduleFrameWrite() {
1345
if sc.writingFrame || sc.inFrameScheduleLoop {
1348
sc.inFrameScheduleLoop = true
1349
for !sc.writingFrameAsync {
1350
if sc.needToSendGoAway {
1351
sc.needToSendGoAway = false
1352
sc.startFrameWrite(FrameWriteRequest{
1353
write: &writeGoAway{
1354
maxStreamID: sc.maxClientStreamID,
1355
code: sc.goAwayCode,
1360
if sc.needToSendSettingsAck {
1361
sc.needToSendSettingsAck = false
1362
sc.startFrameWrite(FrameWriteRequest{write: writeSettingsAck{}})
1365
if !sc.inGoAway || sc.goAwayCode == ErrCodeNo {
1366
if wr, ok := sc.writeSched.Pop(); ok {
1368
sc.queuedControlFrames--
1370
sc.startFrameWrite(wr)
1374
if sc.needsFrameFlush {
1375
sc.startFrameWrite(FrameWriteRequest{write: flushFrameWriter{}})
1376
sc.needsFrameFlush = false // after startFrameWrite, since it sets this true
1381
sc.inFrameScheduleLoop = false
1384
// startGracefulShutdown gracefully shuts down a connection. This
1385
// sends GOAWAY with ErrCodeNo to tell the client we're gracefully
1386
// shutting down. The connection isn't closed until all current
1389
// startGracefulShutdown returns immediately; it does not wait until
1390
// the connection has shut down.
1391
func (sc *serverConn) startGracefulShutdown() {
1392
sc.serveG.checkNotOn() // NOT
1393
sc.shutdownOnce.Do(func() { sc.sendServeMsg(gracefulShutdownMsg) })
1396
// After sending GOAWAY with an error code (non-graceful shutdown), the
1397
// connection will close after goAwayTimeout.
1399
// If we close the connection immediately after sending GOAWAY, there may
1400
// be unsent data in our kernel receive buffer, which will cause the kernel
1401
// to send a TCP RST on close() instead of a FIN. This RST will abort the
1402
// connection immediately, whether or not the client had received the GOAWAY.
1404
// Ideally we should delay for at least 1 RTT + epsilon so the client has
1405
// a chance to read the GOAWAY and stop sending messages. Measuring RTT
1406
// is hard, so we approximate with 1 second. See golang.org/issue/18701.
1408
// This is a var so it can be shorter in tests, where all requests uses the
1409
// loopback interface making the expected RTT very small.
1411
// TODO: configurable?
1412
var goAwayTimeout = 1 * time.Second
1414
func (sc *serverConn) startGracefulShutdownInternal() {
1415
sc.goAway(ErrCodeNo)
1418
func (sc *serverConn) goAway(code ErrCode) {
1421
if sc.goAwayCode == ErrCodeNo {
1422
sc.goAwayCode = code
1427
sc.needToSendGoAway = true
1428
sc.goAwayCode = code
1429
sc.scheduleFrameWrite()
1432
func (sc *serverConn) shutDownIn(d time.Duration) {
1434
sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer)
1437
func (sc *serverConn) resetStream(se StreamError) {
1439
sc.writeFrame(FrameWriteRequest{write: se})
1440
if st, ok := sc.streams[se.StreamID]; ok {
1441
st.resetQueued = true
1445
// processFrameFromReader processes the serve loop's read from readFrameCh from the
1446
// frame-reading goroutine.
1447
// processFrameFromReader returns whether the connection should be kept open.
1448
func (sc *serverConn) processFrameFromReader(res readFrameResult) bool {
1452
if err == ErrFrameTooLarge {
1453
sc.goAway(ErrCodeFrameSize)
1454
return true // goAway will close the loop
1456
clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err)
1458
// TODO: could we also get into this state if
1459
// the peer does a half close
1460
// (e.g. CloseWrite) because they're done
1461
// sending frames but they're still wanting
1462
// our open replies? Investigate.
1463
// TODO: add CloseWrite to crypto/tls.Conn first
1464
// so we have a way to test this? I suppose
1465
// just for testing we could have a non-TLS mode.
1471
sc.vlogf("http2: server read frame %v", summarizeFrame(f))
1473
err = sc.processFrame(f)
1479
switch ev := err.(type) {
1483
case goAwayFlowError:
1484
sc.goAway(ErrCodeFlowControl)
1486
case ConnectionError:
1487
sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
1488
sc.goAway(ErrCode(ev))
1489
return true // goAway will handle shutdown
1492
sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
1494
sc.logf("http2: server closing client connection: %v", err)
1500
func (sc *serverConn) processFrame(f Frame) error {
1503
// First frame received must be SETTINGS.
1504
if !sc.sawFirstSettings {
1505
if _, ok := f.(*SettingsFrame); !ok {
1506
return sc.countError("first_settings", ConnectionError(ErrCodeProtocol))
1508
sc.sawFirstSettings = true
1511
// Discard frames for streams initiated after the identified last
1512
// stream sent in a GOAWAY, or all frames after sending an error.
1513
// We still need to return connection-level flow control for DATA frames.
1514
// RFC 9113 Section 6.8.
1515
if sc.inGoAway && (sc.goAwayCode != ErrCodeNo || f.Header().StreamID > sc.maxClientStreamID) {
1517
if f, ok := f.(*DataFrame); ok {
1518
if !sc.inflow.take(f.Length) {
1519
return sc.countError("data_flow", streamError(f.Header().StreamID, ErrCodeFlowControl))
1521
sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
1526
switch f := f.(type) {
1527
case *SettingsFrame:
1528
return sc.processSettings(f)
1529
case *MetaHeadersFrame:
1530
return sc.processHeaders(f)
1531
case *WindowUpdateFrame:
1532
return sc.processWindowUpdate(f)
1534
return sc.processPing(f)
1536
return sc.processData(f)
1537
case *RSTStreamFrame:
1538
return sc.processResetStream(f)
1539
case *PriorityFrame:
1540
return sc.processPriority(f)
1542
return sc.processGoAway(f)
1543
case *PushPromiseFrame:
1544
// A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE
1545
// frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
1546
return sc.countError("push_promise", ConnectionError(ErrCodeProtocol))
1548
sc.vlogf("http2: server ignoring frame: %v", f.Header())
1553
func (sc *serverConn) processPing(f *PingFrame) error {
1556
// 6.7 PING: " An endpoint MUST NOT respond to PING frames
1557
// containing this flag."
1560
if f.StreamID != 0 {
1561
// "PING frames are not associated with any individual
1562
// stream. If a PING frame is received with a stream
1563
// identifier field value other than 0x0, the recipient MUST
1564
// respond with a connection error (Section 5.4.1) of type
1566
return sc.countError("ping_on_stream", ConnectionError(ErrCodeProtocol))
1568
sc.writeFrame(FrameWriteRequest{write: writePingAck{f}})
1572
func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error {
1575
case f.StreamID != 0: // stream-level flow control
1576
state, st := sc.state(f.StreamID)
1577
if state == stateIdle {
1578
// Section 5.1: "Receiving any frame other than HEADERS
1579
// or PRIORITY on a stream in this state MUST be
1580
// treated as a connection error (Section 5.4.1) of
1581
// type PROTOCOL_ERROR."
1582
return sc.countError("stream_idle", ConnectionError(ErrCodeProtocol))
1585
// "WINDOW_UPDATE can be sent by a peer that has sent a
1586
// frame bearing the END_STREAM flag. This means that a
1587
// receiver could receive a WINDOW_UPDATE frame on a "half
1588
// closed (remote)" or "closed" stream. A receiver MUST
1589
// NOT treat this as an error, see Section 5.1."
1592
if !st.flow.add(int32(f.Increment)) {
1593
return sc.countError("bad_flow", streamError(f.StreamID, ErrCodeFlowControl))
1595
default: // connection-level flow control
1596
if !sc.flow.add(int32(f.Increment)) {
1597
return goAwayFlowError{}
1600
sc.scheduleFrameWrite()
1604
func (sc *serverConn) processResetStream(f *RSTStreamFrame) error {
1607
state, st := sc.state(f.StreamID)
1608
if state == stateIdle {
1609
// 6.4 "RST_STREAM frames MUST NOT be sent for a
1610
// stream in the "idle" state. If a RST_STREAM frame
1611
// identifying an idle stream is received, the
1612
// recipient MUST treat this as a connection error
1613
// (Section 5.4.1) of type PROTOCOL_ERROR.
1614
return sc.countError("reset_idle_stream", ConnectionError(ErrCodeProtocol))
1618
sc.closeStream(st, streamError(f.StreamID, f.ErrCode))
1623
func (sc *serverConn) closeStream(st *stream, err error) {
1625
if st.state == stateIdle || st.state == stateClosed {
1626
panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
1628
st.state = stateClosed
1629
if st.readDeadline != nil {
1630
st.readDeadline.Stop()
1632
if st.writeDeadline != nil {
1633
st.writeDeadline.Stop()
1636
sc.curPushedStreams--
1638
sc.curClientStreams--
1640
delete(sc.streams, st.id)
1641
if len(sc.streams) == 0 {
1642
sc.setConnState(http.StateIdle)
1643
if sc.srv.IdleTimeout != 0 {
1644
sc.idleTimer.Reset(sc.srv.IdleTimeout)
1646
if h1ServerKeepAlivesDisabled(sc.hs) {
1647
sc.startGracefulShutdownInternal()
1650
if p := st.body; p != nil {
1651
// Return any buffered unread bytes worth of conn-level flow control.
1652
// See golang.org/issue/16481
1653
sc.sendWindowUpdate(nil, p.Len())
1655
p.CloseWithError(err)
1657
if e, ok := err.(StreamError); ok {
1661
err = errStreamClosed
1665
st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc
1666
sc.writeSched.CloseStream(st.id)
1669
func (sc *serverConn) processSettings(f *SettingsFrame) error {
1672
sc.unackedSettings--
1673
if sc.unackedSettings < 0 {
1674
// Why is the peer ACKing settings we never sent?
1675
// The spec doesn't mention this case, but
1676
// hang up on them anyway.
1677
return sc.countError("ack_mystery", ConnectionError(ErrCodeProtocol))
1681
if f.NumSettings() > 100 || f.HasDuplicates() {
1682
// This isn't actually in the spec, but hang up on
1683
// suspiciously large settings frames or those with
1684
// duplicate entries.
1685
return sc.countError("settings_big_or_dups", ConnectionError(ErrCodeProtocol))
1687
if err := f.ForeachSetting(sc.processSetting); err != nil {
1690
// TODO: judging by RFC 7540, Section 6.5.3 each SETTINGS frame should be
1691
// acknowledged individually, even if multiple are received before the ACK.
1692
sc.needToSendSettingsAck = true
1693
sc.scheduleFrameWrite()
1697
func (sc *serverConn) processSetting(s Setting) error {
1699
if err := s.Valid(); err != nil {
1703
sc.vlogf("http2: server processing setting %v", s)
1706
case SettingHeaderTableSize:
1707
sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
1708
case SettingEnablePush:
1709
sc.pushEnabled = s.Val != 0
1710
case SettingMaxConcurrentStreams:
1711
sc.clientMaxStreams = s.Val
1712
case SettingInitialWindowSize:
1713
return sc.processSettingInitialWindowSize(s.Val)
1714
case SettingMaxFrameSize:
1715
sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31
1716
case SettingMaxHeaderListSize:
1717
sc.peerMaxHeaderListSize = s.Val
1719
// Unknown setting: "An endpoint that receives a SETTINGS
1720
// frame with any unknown or unsupported identifier MUST
1721
// ignore that setting."
1723
sc.vlogf("http2: server ignoring unknown setting %v", s)
1729
func (sc *serverConn) processSettingInitialWindowSize(val uint32) error {
1731
// Note: val already validated to be within range by
1732
// processSetting's Valid call.
1734
// "A SETTINGS frame can alter the initial flow control window
1735
// size for all current streams. When the value of
1736
// SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST
1737
// adjust the size of all stream flow control windows that it
1738
// maintains by the difference between the new value and the
1740
old := sc.initialStreamSendWindowSize
1741
sc.initialStreamSendWindowSize = int32(val)
1742
growth := int32(val) - old // may be negative
1743
for _, st := range sc.streams {
1744
if !st.flow.add(growth) {
1745
// 6.9.2 Initial Flow Control Window Size
1746
// "An endpoint MUST treat a change to
1747
// SETTINGS_INITIAL_WINDOW_SIZE that causes any flow
1748
// control window to exceed the maximum size as a
1749
// connection error (Section 5.4.1) of type
1750
// FLOW_CONTROL_ERROR."
1751
return sc.countError("setting_win_size", ConnectionError(ErrCodeFlowControl))
1757
func (sc *serverConn) processData(f *DataFrame) error {
1759
id := f.Header().StreamID
1762
state, st := sc.state(id)
1763
if id == 0 || state == stateIdle {
1764
// Section 6.1: "DATA frames MUST be associated with a
1765
// stream. If a DATA frame is received whose stream
1766
// identifier field is 0x0, the recipient MUST respond
1767
// with a connection error (Section 5.4.1) of type
1770
// Section 5.1: "Receiving any frame other than HEADERS
1771
// or PRIORITY on a stream in this state MUST be
1772
// treated as a connection error (Section 5.4.1) of
1773
// type PROTOCOL_ERROR."
1774
return sc.countError("data_on_idle", ConnectionError(ErrCodeProtocol))
1777
// "If a DATA frame is received whose stream is not in "open"
1778
// or "half closed (local)" state, the recipient MUST respond
1779
// with a stream error (Section 5.4.2) of type STREAM_CLOSED."
1780
if st == nil || state != stateOpen || st.gotTrailerHeader || st.resetQueued {
1781
// This includes sending a RST_STREAM if the stream is
1782
// in stateHalfClosedLocal (which currently means that
1783
// the http.Handler returned, so it's done reading &
1784
// done writing). Try to stop the client from sending
1787
// But still enforce their connection-level flow control,
1788
// and return any flow control bytes since we're not going
1790
if !sc.inflow.take(f.Length) {
1791
return sc.countError("data_flow", streamError(id, ErrCodeFlowControl))
1793
sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
1795
if st != nil && st.resetQueued {
1796
// Already have a stream error in flight. Don't send another.
1799
return sc.countError("closed", streamError(id, ErrCodeStreamClosed))
1802
panic("internal error: should have a body in this state")
1805
// Sender sending more than they'd declared?
1806
if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
1807
if !sc.inflow.take(f.Length) {
1808
return sc.countError("data_flow", streamError(id, ErrCodeFlowControl))
1810
sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
1812
st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
1813
// RFC 7540, sec 8.1.2.6: A request or response is also malformed if the
1814
// value of a content-length header field does not equal the sum of the
1815
// DATA frame payload lengths that form the body.
1816
return sc.countError("send_too_much", streamError(id, ErrCodeProtocol))
1819
// Check whether the client has flow control quota.
1820
if !takeInflows(&sc.inflow, &st.inflow, f.Length) {
1821
return sc.countError("flow_on_data_length", streamError(id, ErrCodeFlowControl))
1825
wrote, err := st.body.Write(data)
1827
sc.sendWindowUpdate(nil, int(f.Length)-wrote)
1828
return sc.countError("body_write_err", streamError(id, ErrCodeStreamClosed))
1830
if wrote != len(data) {
1831
panic("internal error: bad Writer")
1833
st.bodyBytes += int64(len(data))
1836
// Return any padded flow control now, since we won't
1837
// refund it later on body reads.
1838
// Call sendWindowUpdate even if there is no padding,
1839
// to return buffered flow control credit if the sent
1840
// window has shrunk.
1841
pad := int32(f.Length) - int32(len(data))
1842
sc.sendWindowUpdate32(nil, pad)
1843
sc.sendWindowUpdate32(st, pad)
1845
if f.StreamEnded() {
1851
func (sc *serverConn) processGoAway(f *GoAwayFrame) error {
1853
if f.ErrCode != ErrCodeNo {
1854
sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f)
1856
sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f)
1858
sc.startGracefulShutdownInternal()
1859
// http://tools.ietf.org/html/rfc7540#section-6.8
1860
// We should not create any new streams, which means we should disable push.
1861
sc.pushEnabled = false
1865
// isPushed reports whether the stream is server-initiated.
1866
func (st *stream) isPushed() bool {
1870
// endStream closes a Request.Body's pipe. It is called when a DATA
1871
// frame says a request body is over (or after trailers).
1872
func (st *stream) endStream() {
1876
if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
1877
st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
1878
st.declBodyBytes, st.bodyBytes))
1880
st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
1881
st.body.CloseWithError(io.EOF)
1883
st.state = stateHalfClosedRemote
1886
// copyTrailersToHandlerRequest is run in the Handler's goroutine in
1887
// its Request.Body.Read just before it gets io.EOF.
1888
func (st *stream) copyTrailersToHandlerRequest() {
1889
for k, vv := range st.trailer {
1890
if _, ok := st.reqTrailer[k]; ok {
1891
// Only copy it over it was pre-declared.
1892
st.reqTrailer[k] = vv
1897
// onReadTimeout is run on its own goroutine (from time.AfterFunc)
1898
// when the stream's ReadTimeout has fired.
1899
func (st *stream) onReadTimeout() {
1900
// Wrap the ErrDeadlineExceeded to avoid callers depending on us
1901
// returning the bare error.
1902
st.body.CloseWithError(fmt.Errorf("%w", os.ErrDeadlineExceeded))
1905
// onWriteTimeout is run on its own goroutine (from time.AfterFunc)
1906
// when the stream's WriteTimeout has fired.
1907
func (st *stream) onWriteTimeout() {
1908
st.sc.writeFrameFromHandler(FrameWriteRequest{write: StreamError{
1910
Code: ErrCodeInternal,
1911
Cause: os.ErrDeadlineExceeded,
1915
func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error {
1918
// http://tools.ietf.org/html/rfc7540#section-5.1.1
1919
// Streams initiated by a client MUST use odd-numbered stream
1920
// identifiers. [...] An endpoint that receives an unexpected
1921
// stream identifier MUST respond with a connection error
1922
// (Section 5.4.1) of type PROTOCOL_ERROR.
1924
return sc.countError("headers_even", ConnectionError(ErrCodeProtocol))
1926
// A HEADERS frame can be used to create a new stream or
1927
// send a trailer for an open one. If we already have a stream
1928
// open, let it process its own HEADERS frame (trailers at this
1929
// point, if it's valid).
1930
if st := sc.streams[f.StreamID]; st != nil {
1932
// We're sending RST_STREAM to close the stream, so don't bother
1933
// processing this frame.
1936
// RFC 7540, sec 5.1: If an endpoint receives additional frames, other than
1937
// WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
1938
// this state, it MUST respond with a stream error (Section 5.4.2) of
1939
// type STREAM_CLOSED.
1940
if st.state == stateHalfClosedRemote {
1941
return sc.countError("headers_half_closed", streamError(id, ErrCodeStreamClosed))
1943
return st.processTrailerHeaders(f)
1946
// [...] The identifier of a newly established stream MUST be
1947
// numerically greater than all streams that the initiating
1948
// endpoint has opened or reserved. [...] An endpoint that
1949
// receives an unexpected stream identifier MUST respond with
1950
// a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
1951
if id <= sc.maxClientStreamID {
1952
return sc.countError("stream_went_down", ConnectionError(ErrCodeProtocol))
1954
sc.maxClientStreamID = id
1956
if sc.idleTimer != nil {
1960
// http://tools.ietf.org/html/rfc7540#section-5.1.2
1961
// [...] Endpoints MUST NOT exceed the limit set by their peer. An
1962
// endpoint that receives a HEADERS frame that causes their
1963
// advertised concurrent stream limit to be exceeded MUST treat
1964
// this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR
1965
// or REFUSED_STREAM.
1966
if sc.curClientStreams+1 > sc.advMaxStreams {
1967
if sc.unackedSettings == 0 {
1968
// They should know better.
1969
return sc.countError("over_max_streams", streamError(id, ErrCodeProtocol))
1971
// Assume it's a network race, where they just haven't
1972
// received our last SETTINGS update. But actually
1973
// this can't happen yet, because we don't yet provide
1974
// a way for users to adjust server parameters at
1976
return sc.countError("over_max_streams_race", streamError(id, ErrCodeRefusedStream))
1979
initialState := stateOpen
1980
if f.StreamEnded() {
1981
initialState = stateHalfClosedRemote
1983
st := sc.newStream(id, 0, initialState)
1985
if f.HasPriority() {
1986
if err := sc.checkPriority(f.StreamID, f.Priority); err != nil {
1989
sc.writeSched.AdjustStream(st.id, f.Priority)
1992
rw, req, err := sc.newWriterAndRequest(st, f)
1996
st.reqTrailer = req.Trailer
1997
if st.reqTrailer != nil {
1998
st.trailer = make(http.Header)
2000
st.body = req.Body.(*requestBody).pipe // may be nil
2001
st.declBodyBytes = req.ContentLength
2003
handler := sc.handler.ServeHTTP
2005
// Their header list was too long. Send a 431 error.
2006
handler = handleHeaderListTooLong
2007
} else if err := checkValidHTTP2RequestHeaders(req.Header); err != nil {
2008
handler = new400Handler(err)
2011
// The net/http package sets the read deadline from the
2012
// http.Server.ReadTimeout during the TLS handshake, but then
2013
// passes the connection off to us with the deadline already
2014
// set. Disarm it here after the request headers are read,
2015
// similar to how the http1 server works. Here it's
2016
// technically more like the http1 Server's ReadHeaderTimeout
2017
// (in Go 1.8), though. That's a more sane option anyway.
2018
if sc.hs.ReadTimeout != 0 {
2019
sc.conn.SetReadDeadline(time.Time{})
2021
st.readDeadline = time.AfterFunc(sc.hs.ReadTimeout, st.onReadTimeout)
2025
go sc.runHandler(rw, req, handler)
2029
func (sc *serverConn) upgradeRequest(req *http.Request) {
2032
sc.maxClientStreamID = id
2033
st := sc.newStream(id, 0, stateHalfClosedRemote)
2034
st.reqTrailer = req.Trailer
2035
if st.reqTrailer != nil {
2036
st.trailer = make(http.Header)
2038
rw := sc.newResponseWriter(st, req)
2040
// Disable any read deadline set by the net/http package
2041
// prior to the upgrade.
2042
if sc.hs.ReadTimeout != 0 {
2043
sc.conn.SetReadDeadline(time.Time{})
2046
go sc.runHandler(rw, req, sc.handler.ServeHTTP)
2049
func (st *stream) processTrailerHeaders(f *MetaHeadersFrame) error {
2052
if st.gotTrailerHeader {
2053
return sc.countError("dup_trailers", ConnectionError(ErrCodeProtocol))
2055
st.gotTrailerHeader = true
2056
if !f.StreamEnded() {
2057
return sc.countError("trailers_not_ended", streamError(st.id, ErrCodeProtocol))
2060
if len(f.PseudoFields()) > 0 {
2061
return sc.countError("trailers_pseudo", streamError(st.id, ErrCodeProtocol))
2063
if st.trailer != nil {
2064
for _, hf := range f.RegularFields() {
2065
key := sc.canonicalHeader(hf.Name)
2066
if !httpguts.ValidTrailerHeader(key) {
2067
// TODO: send more details to the peer somehow. But http2 has
2068
// no way to send debug data at a stream level. Discuss with
2070
return sc.countError("trailers_bogus", streamError(st.id, ErrCodeProtocol))
2072
st.trailer[key] = append(st.trailer[key], hf.Value)
2079
func (sc *serverConn) checkPriority(streamID uint32, p PriorityParam) error {
2080
if streamID == p.StreamDep {
2081
// Section 5.3.1: "A stream cannot depend on itself. An endpoint MUST treat
2082
// this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR."
2083
// Section 5.3.3 says that a stream can depend on one of its dependencies,
2084
// so it's only self-dependencies that are forbidden.
2085
return sc.countError("priority", streamError(streamID, ErrCodeProtocol))
2090
func (sc *serverConn) processPriority(f *PriorityFrame) error {
2091
if err := sc.checkPriority(f.StreamID, f.PriorityParam); err != nil {
2094
sc.writeSched.AdjustStream(f.StreamID, f.PriorityParam)
2098
func (sc *serverConn) newStream(id, pusherID uint32, state streamState) *stream {
2101
panic("internal error: cannot create stream with id 0")
2104
ctx, cancelCtx := context.WithCancel(sc.baseCtx)
2110
cancelCtx: cancelCtx,
2113
st.flow.conn = &sc.flow // link to conn-level counter
2114
st.flow.add(sc.initialStreamSendWindowSize)
2115
st.inflow.init(sc.srv.initialStreamRecvWindowSize())
2116
if sc.hs.WriteTimeout != 0 {
2117
st.writeDeadline = time.AfterFunc(sc.hs.WriteTimeout, st.onWriteTimeout)
2121
sc.writeSched.OpenStream(st.id, OpenStreamOptions{PusherID: pusherID})
2123
sc.curPushedStreams++
2125
sc.curClientStreams++
2127
if sc.curOpenStreams() == 1 {
2128
sc.setConnState(http.StateActive)
2134
func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*responseWriter, *http.Request, error) {
2138
method: f.PseudoValue("method"),
2139
scheme: f.PseudoValue("scheme"),
2140
authority: f.PseudoValue("authority"),
2141
path: f.PseudoValue("path"),
2144
isConnect := rp.method == "CONNECT"
2146
if rp.path != "" || rp.scheme != "" || rp.authority == "" {
2147
return nil, nil, sc.countError("bad_connect", streamError(f.StreamID, ErrCodeProtocol))
2149
} else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") {
2150
// See 8.1.2.6 Malformed Requests and Responses:
2152
// Malformed requests or responses that are detected
2153
// MUST be treated as a stream error (Section 5.4.2)
2154
// of type PROTOCOL_ERROR."
2156
// 8.1.2.3 Request Pseudo-Header Fields
2157
// "All HTTP/2 requests MUST include exactly one valid
2158
// value for the :method, :scheme, and :path
2159
// pseudo-header fields"
2160
return nil, nil, sc.countError("bad_path_method", streamError(f.StreamID, ErrCodeProtocol))
2163
rp.header = make(http.Header)
2164
for _, hf := range f.RegularFields() {
2165
rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value)
2167
if rp.authority == "" {
2168
rp.authority = rp.header.Get("Host")
2171
rw, req, err := sc.newWriterAndRequestNoBody(st, rp)
2173
return nil, nil, err
2175
bodyOpen := !f.StreamEnded()
2177
if vv, ok := rp.header["Content-Length"]; ok {
2178
if cl, err := strconv.ParseUint(vv[0], 10, 63); err == nil {
2179
req.ContentLength = int64(cl)
2181
req.ContentLength = 0
2184
req.ContentLength = -1
2186
req.Body.(*requestBody).pipe = &pipe{
2187
b: &dataBuffer{expected: req.ContentLength},
2193
type requestParam struct {
2195
scheme, authority, path string
2199
func (sc *serverConn) newWriterAndRequestNoBody(st *stream, rp requestParam) (*responseWriter, *http.Request, error) {
2202
var tlsState *tls.ConnectionState // nil if not scheme https
2203
if rp.scheme == "https" {
2204
tlsState = sc.tlsState
2207
needsContinue := httpguts.HeaderValuesContainsToken(rp.header["Expect"], "100-continue")
2209
rp.header.Del("Expect")
2211
// Merge Cookie headers into one "; "-delimited value.
2212
if cookies := rp.header["Cookie"]; len(cookies) > 1 {
2213
rp.header.Set("Cookie", strings.Join(cookies, "; "))
2217
var trailer http.Header
2218
for _, v := range rp.header["Trailer"] {
2219
for _, key := range strings.Split(v, ",") {
2220
key = http.CanonicalHeaderKey(textproto.TrimString(key))
2222
case "Transfer-Encoding", "Trailer", "Content-Length":
2223
// Bogus. (copy of http1 rules)
2227
trailer = make(http.Header)
2233
delete(rp.header, "Trailer")
2236
var requestURI string
2237
if rp.method == "CONNECT" {
2238
url_ = &url.URL{Host: rp.authority}
2239
requestURI = rp.authority // mimic HTTP/1 server behavior
2242
url_, err = url.ParseRequestURI(rp.path)
2244
return nil, nil, sc.countError("bad_path", streamError(st.id, ErrCodeProtocol))
2246
requestURI = rp.path
2249
body := &requestBody{
2252
needsContinue: needsContinue,
2254
req := &http.Request{
2257
RemoteAddr: sc.remoteAddrStr,
2259
RequestURI: requestURI,
2268
req = req.WithContext(st.ctx)
2270
rw := sc.newResponseWriter(st, req)
2274
func (sc *serverConn) newResponseWriter(st *stream, req *http.Request) *responseWriter {
2275
rws := responseWriterStatePool.Get().(*responseWriterState)
2277
*rws = responseWriterState{} // zero all the fields
2280
rws.bw.Reset(chunkWriter{rws})
2283
return &responseWriter{rws: rws}
2286
// Run on its own goroutine.
2287
func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) {
2290
rw.rws.stream.cancelCtx()
2291
if req.MultipartForm != nil {
2292
req.MultipartForm.RemoveAll()
2296
sc.writeFrameFromHandler(FrameWriteRequest{
2297
write: handlerPanicRST{rw.rws.stream.id},
2298
stream: rw.rws.stream,
2300
// Same as net/http:
2301
if e != nil && e != http.ErrAbortHandler {
2302
const size = 64 << 10
2303
buf := make([]byte, size)
2304
buf = buf[:runtime.Stack(buf, false)]
2305
sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
2315
func handleHeaderListTooLong(w http.ResponseWriter, r *http.Request) {
2316
// 10.5.1 Limits on Header Block Size:
2317
// .. "A server that receives a larger header block than it is
2318
// willing to handle can send an HTTP 431 (Request Header Fields Too
2319
// Large) status code"
2320
const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+
2321
w.WriteHeader(statusRequestHeaderFieldsTooLarge)
2322
io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
2325
// called from handler goroutines.
2327
func (sc *serverConn) writeHeaders(st *stream, headerData *writeResHeaders) error {
2328
sc.serveG.checkNotOn() // NOT on
2330
if headerData.h != nil {
2331
// If there's a header map (which we don't own), so we have to block on
2332
// waiting for this frame to be written, so an http.Flush mid-handler
2333
// writes out the correct value of keys, before a handler later potentially
2335
errc = errChanPool.Get().(chan error)
2337
if err := sc.writeFrameFromHandler(FrameWriteRequest{
2347
errChanPool.Put(errc)
2349
case <-sc.doneServing:
2350
return errClientDisconnected
2352
return errStreamClosed
2358
// called from handler goroutines.
2359
func (sc *serverConn) write100ContinueHeaders(st *stream) {
2360
sc.writeFrameFromHandler(FrameWriteRequest{
2361
write: write100ContinueHeadersFrame{st.id},
2366
// A bodyReadMsg tells the server loop that the http.Handler read n
2367
// bytes of the DATA from the client on the given stream.
2368
type bodyReadMsg struct {
2373
// called from handler goroutines.
2374
// Notes that the handler for the given stream ID read n bytes of its body
2375
// and schedules flow control tokens to be sent.
2376
func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int, err error) {
2377
sc.serveG.checkNotOn() // NOT on
2380
case sc.bodyReadCh <- bodyReadMsg{st, n}:
2381
case <-sc.doneServing:
2386
func (sc *serverConn) noteBodyRead(st *stream, n int) {
2388
sc.sendWindowUpdate(nil, n) // conn-level
2389
if st.state != stateHalfClosedRemote && st.state != stateClosed {
2390
// Don't send this WINDOW_UPDATE if the stream is closed
2392
sc.sendWindowUpdate(st, n)
2396
// st may be nil for conn-level
2397
func (sc *serverConn) sendWindowUpdate32(st *stream, n int32) {
2398
sc.sendWindowUpdate(st, int(n))
2401
// st may be nil for conn-level
2402
func (sc *serverConn) sendWindowUpdate(st *stream, n int) {
2407
send = sc.inflow.add(n)
2410
send = st.inflow.add(n)
2415
sc.writeFrame(FrameWriteRequest{
2416
write: writeWindowUpdate{streamID: streamID, n: uint32(send)},
2421
// requestBody is the Handler's Request.Body type.
2422
// Read and Close may be called concurrently.
2423
type requestBody struct {
2427
closeOnce sync.Once // for use by Close only
2428
sawEOF bool // for use by Read only
2429
pipe *pipe // non-nil if we have a HTTP entity message body
2430
needsContinue bool // need to send a 100-continue
2433
func (b *requestBody) Close() error {
2434
b.closeOnce.Do(func() {
2436
b.pipe.BreakWithError(errClosedBody)
2442
func (b *requestBody) Read(p []byte) (n int, err error) {
2443
if b.needsContinue {
2444
b.needsContinue = false
2445
b.conn.write100ContinueHeaders(b.stream)
2447
if b.pipe == nil || b.sawEOF {
2450
n, err = b.pipe.Read(p)
2454
if b.conn == nil && inTests {
2457
b.conn.noteBodyReadFromHandler(b.stream, n, err)
2461
// responseWriter is the http.ResponseWriter implementation. It's
2462
// intentionally small (1 pointer wide) to minimize garbage. The
2463
// responseWriterState pointer inside is zeroed at the end of a
2464
// request (in handlerDone) and calls on the responseWriter thereafter
2465
// simply crash (caller's mistake), but the much larger responseWriterState
2466
// and buffers are reused between multiple requests.
2467
type responseWriter struct {
2468
rws *responseWriterState
2471
// Optional http.ResponseWriter interfaces implemented.
2473
_ http.CloseNotifier = (*responseWriter)(nil)
2474
_ http.Flusher = (*responseWriter)(nil)
2475
_ stringWriter = (*responseWriter)(nil)
2478
type responseWriterState struct {
2479
// immutable within a request:
2484
// TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
2485
bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
2487
// mutated by http.Handler goroutine:
2488
handlerHeader http.Header // nil until called
2489
snapHeader http.Header // snapshot of handlerHeader at WriteHeader time
2490
trailers []string // set in writeChunk
2491
status int // status code passed to WriteHeader
2492
wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
2493
sentHeader bool // have we sent the header frame?
2494
handlerDone bool // handler has finished
2495
dirty bool // a Write failed; don't reuse this responseWriterState
2497
sentContentLen int64 // non-zero if handler set a Content-Length header
2500
closeNotifierMu sync.Mutex // guards closeNotifierCh
2501
closeNotifierCh chan bool // nil until first used
2504
type chunkWriter struct{ rws *responseWriterState }
2506
func (cw chunkWriter) Write(p []byte) (n int, err error) {
2507
n, err = cw.rws.writeChunk(p)
2508
if err == errStreamClosed {
2509
// If writing failed because the stream has been closed,
2510
// return the reason it was closed.
2511
err = cw.rws.stream.closeErr
2516
func (rws *responseWriterState) hasTrailers() bool { return len(rws.trailers) > 0 }
2518
func (rws *responseWriterState) hasNonemptyTrailers() bool {
2519
for _, trailer := range rws.trailers {
2520
if _, ok := rws.handlerHeader[trailer]; ok {
2527
// declareTrailer is called for each Trailer header when the
2528
// response header is written. It notes that a header will need to be
2529
// written in the trailers at the end of the response.
2530
func (rws *responseWriterState) declareTrailer(k string) {
2531
k = http.CanonicalHeaderKey(k)
2532
if !httpguts.ValidTrailerHeader(k) {
2533
// Forbidden by RFC 7230, section 4.1.2.
2534
rws.conn.logf("ignoring invalid trailer %q", k)
2537
if !strSliceContains(rws.trailers, k) {
2538
rws.trailers = append(rws.trailers, k)
2542
// writeChunk writes chunks from the bufio.Writer. But because
2543
// bufio.Writer may bypass its chunking, sometimes p may be
2544
// arbitrarily large.
2546
// writeChunk is also responsible (on the first chunk) for sending the
2548
func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
2549
if !rws.wroteHeader {
2550
rws.writeHeader(200)
2553
if rws.handlerDone {
2554
rws.promoteUndeclaredTrailers()
2557
isHeadResp := rws.req.Method == "HEAD"
2558
if !rws.sentHeader {
2559
rws.sentHeader = true
2560
var ctype, clen string
2561
if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
2562
rws.snapHeader.Del("Content-Length")
2563
if cl, err := strconv.ParseUint(clen, 10, 63); err == nil {
2564
rws.sentContentLen = int64(cl)
2569
if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
2570
clen = strconv.Itoa(len(p))
2572
_, hasContentType := rws.snapHeader["Content-Type"]
2573
// If the Content-Encoding is non-blank, we shouldn't
2574
// sniff the body. See Issue golang.org/issue/31753.
2575
ce := rws.snapHeader.Get("Content-Encoding")
2576
hasCE := len(ce) > 0
2577
if !hasCE && !hasContentType && bodyAllowedForStatus(rws.status) && len(p) > 0 {
2578
ctype = http.DetectContentType(p)
2581
if _, ok := rws.snapHeader["Date"]; !ok {
2582
// TODO(bradfitz): be faster here, like net/http? measure.
2583
date = time.Now().UTC().Format(http.TimeFormat)
2586
for _, v := range rws.snapHeader["Trailer"] {
2587
foreachHeaderElement(v, rws.declareTrailer)
2590
// "Connection" headers aren't allowed in HTTP/2 (RFC 7540, 8.1.2.2),
2591
// but respect "Connection" == "close" to mean sending a GOAWAY and tearing
2592
// down the TCP connection when idle, like we do for HTTP/1.
2593
// TODO: remove more Connection-specific header fields here, in addition
2595
if _, ok := rws.snapHeader["Connection"]; ok {
2596
v := rws.snapHeader.Get("Connection")
2597
delete(rws.snapHeader, "Connection")
2599
rws.conn.startGracefulShutdown()
2603
endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
2604
err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2605
streamID: rws.stream.id,
2606
httpResCode: rws.status,
2608
endStream: endStream,
2610
contentLength: clen,
2624
if len(p) == 0 && !rws.handlerDone {
2628
// only send trailers if they have actually been defined by the
2630
hasNonemptyTrailers := rws.hasNonemptyTrailers()
2631
endStream := rws.handlerDone && !hasNonemptyTrailers
2632
if len(p) > 0 || endStream {
2633
// only send a 0 byte DATA frame if we're ending the stream.
2634
if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
2640
if rws.handlerDone && hasNonemptyTrailers {
2641
err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2642
streamID: rws.stream.id,
2643
h: rws.handlerHeader,
2644
trailers: rws.trailers,
2655
// TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
2656
// that, if present, signals that the map entry is actually for
2657
// the response trailers, and not the response headers. The prefix
2658
// is stripped after the ServeHTTP call finishes and the values are
2659
// sent in the trailers.
2661
// This mechanism is intended only for trailers that are not known
2662
// prior to the headers being written. If the set of trailers is fixed
2663
// or known before the header is written, the normal Go trailers mechanism
2666
// https://golang.org/pkg/net/http/#ResponseWriter
2667
// https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
2668
const TrailerPrefix = "Trailer:"
2670
// promoteUndeclaredTrailers permits http.Handlers to set trailers
2671
// after the header has already been flushed. Because the Go
2672
// ResponseWriter interface has no way to set Trailers (only the
2673
// Header), and because we didn't want to expand the ResponseWriter
2674
// interface, and because nobody used trailers, and because RFC 7230
2675
// says you SHOULD (but not must) predeclare any trailers in the
2676
// header, the official ResponseWriter rules said trailers in Go must
2677
// be predeclared, and then we reuse the same ResponseWriter.Header()
2678
// map to mean both Headers and Trailers. When it's time to write the
2679
// Trailers, we pick out the fields of Headers that were declared as
2680
// trailers. That worked for a while, until we found the first major
2681
// user of Trailers in the wild: gRPC (using them only over http2),
2682
// and gRPC libraries permit setting trailers mid-stream without
2683
// predeclaring them. So: change of plans. We still permit the old
2684
// way, but we also permit this hack: if a Header() key begins with
2685
// "Trailer:", the suffix of that key is a Trailer. Because ':' is an
2686
// invalid token byte anyway, there is no ambiguity. (And it's already
2687
// filtered out) It's mildly hacky, but not terrible.
2689
// This method runs after the Handler is done and promotes any Header
2690
// fields to be trailers.
2691
func (rws *responseWriterState) promoteUndeclaredTrailers() {
2692
for k, vv := range rws.handlerHeader {
2693
if !strings.HasPrefix(k, TrailerPrefix) {
2696
trailerKey := strings.TrimPrefix(k, TrailerPrefix)
2697
rws.declareTrailer(trailerKey)
2698
rws.handlerHeader[http.CanonicalHeaderKey(trailerKey)] = vv
2701
if len(rws.trailers) > 1 {
2702
sorter := sorterPool.Get().(*sorter)
2703
sorter.SortStrings(rws.trailers)
2704
sorterPool.Put(sorter)
2708
func (w *responseWriter) SetReadDeadline(deadline time.Time) error {
2710
if !deadline.IsZero() && deadline.Before(time.Now()) {
2711
// If we're setting a deadline in the past, reset the stream immediately
2712
// so writes after SetWriteDeadline returns will fail.
2716
w.rws.conn.sendServeMsg(func(sc *serverConn) {
2717
if st.readDeadline != nil {
2718
if !st.readDeadline.Stop() {
2719
// Deadline already exceeded, or stream has been closed.
2723
if deadline.IsZero() {
2724
st.readDeadline = nil
2725
} else if st.readDeadline == nil {
2726
st.readDeadline = time.AfterFunc(deadline.Sub(time.Now()), st.onReadTimeout)
2728
st.readDeadline.Reset(deadline.Sub(time.Now()))
2734
func (w *responseWriter) SetWriteDeadline(deadline time.Time) error {
2736
if !deadline.IsZero() && deadline.Before(time.Now()) {
2737
// If we're setting a deadline in the past, reset the stream immediately
2738
// so writes after SetWriteDeadline returns will fail.
2742
w.rws.conn.sendServeMsg(func(sc *serverConn) {
2743
if st.writeDeadline != nil {
2744
if !st.writeDeadline.Stop() {
2745
// Deadline already exceeded, or stream has been closed.
2749
if deadline.IsZero() {
2750
st.writeDeadline = nil
2751
} else if st.writeDeadline == nil {
2752
st.writeDeadline = time.AfterFunc(deadline.Sub(time.Now()), st.onWriteTimeout)
2754
st.writeDeadline.Reset(deadline.Sub(time.Now()))
2760
func (w *responseWriter) Flush() {
2764
func (w *responseWriter) FlushError() error {
2767
panic("Header called after Handler finished")
2770
if rws.bw.Buffered() > 0 {
2771
err = rws.bw.Flush()
2773
// The bufio.Writer won't call chunkWriter.Write
2774
// (writeChunk with zero bytes, so we have to do it
2775
// ourselves to force the HTTP response header and/or
2776
// final DATA frame (with END_STREAM) to be sent.
2777
_, err = chunkWriter{rws}.Write(nil)
2780
case <-rws.stream.cw:
2781
err = rws.stream.closeErr
2789
func (w *responseWriter) CloseNotify() <-chan bool {
2792
panic("CloseNotify called after Handler finished")
2794
rws.closeNotifierMu.Lock()
2795
ch := rws.closeNotifierCh
2797
ch = make(chan bool, 1)
2798
rws.closeNotifierCh = ch
2801
cw.Wait() // wait for close
2805
rws.closeNotifierMu.Unlock()
2809
func (w *responseWriter) Header() http.Header {
2812
panic("Header called after Handler finished")
2814
if rws.handlerHeader == nil {
2815
rws.handlerHeader = make(http.Header)
2817
return rws.handlerHeader
2820
// checkWriteHeaderCode is a copy of net/http's checkWriteHeaderCode.
2821
func checkWriteHeaderCode(code int) {
2822
// Issue 22880: require valid WriteHeader status codes.
2823
// For now we only enforce that it's three digits.
2824
// In the future we might block things over 599 (600 and above aren't defined
2825
// at http://httpwg.org/specs/rfc7231.html#status.codes).
2826
// But for now any three digits.
2828
// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
2829
// no equivalent bogus thing we can realistically send in HTTP/2,
2830
// so we'll consistently panic instead and help people find their bugs
2831
// early. (We can't return an error from WriteHeader even if we wanted to.)
2832
if code < 100 || code > 999 {
2833
panic(fmt.Sprintf("invalid WriteHeader code %v", code))
2837
func (w *responseWriter) WriteHeader(code int) {
2840
panic("WriteHeader called after Handler finished")
2842
rws.writeHeader(code)
2845
func (rws *responseWriterState) writeHeader(code int) {
2846
if rws.wroteHeader {
2850
checkWriteHeaderCode(code)
2852
// Handle informational headers
2853
if code >= 100 && code <= 199 {
2854
// Per RFC 8297 we must not clear the current header map
2855
h := rws.handlerHeader
2857
_, cl := h["Content-Length"]
2858
_, te := h["Transfer-Encoding"]
2861
h.Del("Content-Length")
2862
h.Del("Transfer-Encoding")
2865
if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2866
streamID: rws.stream.id,
2869
endStream: rws.handlerDone && !rws.hasTrailers(),
2877
rws.wroteHeader = true
2879
if len(rws.handlerHeader) > 0 {
2880
rws.snapHeader = cloneHeader(rws.handlerHeader)
2884
func cloneHeader(h http.Header) http.Header {
2885
h2 := make(http.Header, len(h))
2886
for k, vv := range h {
2887
vv2 := make([]string, len(vv))
2894
// The Life Of A Write is like this:
2896
// * Handler calls w.Write or w.WriteString ->
2897
// * -> rws.bw (*bufio.Writer) ->
2898
// * (Handler might call Flush)
2899
// * -> chunkWriter{rws}
2900
// * -> responseWriterState.writeChunk(p []byte)
2901
// * -> responseWriterState.writeChunk (most of the magic; see comment there)
2902
func (w *responseWriter) Write(p []byte) (n int, err error) {
2903
return w.write(len(p), p, "")
2906
func (w *responseWriter) WriteString(s string) (n int, err error) {
2907
return w.write(len(s), nil, s)
2910
// either dataB or dataS is non-zero.
2911
func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
2914
panic("Write called after Handler finished")
2916
if !rws.wroteHeader {
2919
if !bodyAllowedForStatus(rws.status) {
2920
return 0, http.ErrBodyNotAllowed
2922
rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set
2923
if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
2924
// TODO: send a RST_STREAM
2925
return 0, errors.New("http2: handler wrote more than declared Content-Length")
2929
return rws.bw.Write(dataB)
2931
return rws.bw.WriteString(dataS)
2935
func (w *responseWriter) handlerDone() {
2938
rws.handlerDone = true
2942
// Only recycle the pool if all prior Write calls to
2943
// the serverConn goroutine completed successfully. If
2944
// they returned earlier due to resets from the peer
2945
// there might still be write goroutines outstanding
2946
// from the serverConn referencing the rws memory. See
2948
responseWriterStatePool.Put(rws)
2954
ErrRecursivePush = errors.New("http2: recursive push not allowed")
2955
ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS")
2958
var _ http.Pusher = (*responseWriter)(nil)
2960
func (w *responseWriter) Push(target string, opts *http.PushOptions) error {
2963
sc.serveG.checkNotOn()
2965
// No recursive pushes: "PUSH_PROMISE frames MUST only be sent on a peer-initiated stream."
2966
// http://tools.ietf.org/html/rfc7540#section-6.6
2968
return ErrRecursivePush
2972
opts = new(http.PushOptions)
2976
if opts.Method == "" {
2979
if opts.Header == nil {
2980
opts.Header = http.Header{}
2982
wantScheme := "http"
2983
if w.rws.req.TLS != nil {
2984
wantScheme = "https"
2987
// Validate the request.
2988
u, err := url.Parse(target)
2993
if !strings.HasPrefix(target, "/") {
2994
return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target)
2996
u.Scheme = wantScheme
2997
u.Host = w.rws.req.Host
2999
if u.Scheme != wantScheme {
3000
return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme)
3003
return errors.New("URL must have a host")
3006
for k := range opts.Header {
3007
if strings.HasPrefix(k, ":") {
3008
return fmt.Errorf("promised request headers cannot include pseudo header %q", k)
3010
// These headers are meaningful only if the request has a body,
3011
// but PUSH_PROMISE requests cannot have a body.
3012
// http://tools.ietf.org/html/rfc7540#section-8.2
3013
// Also disallow Host, since the promised URL must be absolute.
3014
if asciiEqualFold(k, "content-length") ||
3015
asciiEqualFold(k, "content-encoding") ||
3016
asciiEqualFold(k, "trailer") ||
3017
asciiEqualFold(k, "te") ||
3018
asciiEqualFold(k, "expect") ||
3019
asciiEqualFold(k, "host") {
3020
return fmt.Errorf("promised request headers cannot include %q", k)
3023
if err := checkValidHTTP2RequestHeaders(opts.Header); err != nil {
3027
// The RFC effectively limits promised requests to GET and HEAD:
3028
// "Promised requests MUST be cacheable [GET, HEAD, or POST], and MUST be safe [GET or HEAD]"
3029
// http://tools.ietf.org/html/rfc7540#section-8.2
3030
if opts.Method != "GET" && opts.Method != "HEAD" {
3031
return fmt.Errorf("method %q must be GET or HEAD", opts.Method)
3034
msg := &startPushRequest{
3036
method: opts.Method,
3038
header: cloneHeader(opts.Header),
3039
done: errChanPool.Get().(chan error),
3043
case <-sc.doneServing:
3044
return errClientDisconnected
3046
return errStreamClosed
3047
case sc.serveMsgCh <- msg:
3051
case <-sc.doneServing:
3052
return errClientDisconnected
3054
return errStreamClosed
3055
case err := <-msg.done:
3056
errChanPool.Put(msg.done)
3061
type startPushRequest struct {
3069
func (sc *serverConn) startPush(msg *startPushRequest) {
3072
// http://tools.ietf.org/html/rfc7540#section-6.6.
3073
// PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
3074
// is in either the "open" or "half-closed (remote)" state.
3075
if msg.parent.state != stateOpen && msg.parent.state != stateHalfClosedRemote {
3076
// responseWriter.Push checks that the stream is peer-initiated.
3077
msg.done <- errStreamClosed
3081
// http://tools.ietf.org/html/rfc7540#section-6.6.
3082
if !sc.pushEnabled {
3083
msg.done <- http.ErrNotSupported
3087
// PUSH_PROMISE frames must be sent in increasing order by stream ID, so
3088
// we allocate an ID for the promised stream lazily, when the PUSH_PROMISE
3089
// is written. Once the ID is allocated, we start the request handler.
3090
allocatePromisedID := func() (uint32, error) {
3093
// Check this again, just in case. Technically, we might have received
3094
// an updated SETTINGS by the time we got around to writing this frame.
3095
if !sc.pushEnabled {
3096
return 0, http.ErrNotSupported
3098
// http://tools.ietf.org/html/rfc7540#section-6.5.2.
3099
if sc.curPushedStreams+1 > sc.clientMaxStreams {
3100
return 0, ErrPushLimitReached
3103
// http://tools.ietf.org/html/rfc7540#section-5.1.1.
3104
// Streams initiated by the server MUST use even-numbered identifiers.
3105
// A server that is unable to establish a new stream identifier can send a GOAWAY
3106
// frame so that the client is forced to open a new connection for new streams.
3107
if sc.maxPushPromiseID+2 >= 1<<31 {
3108
sc.startGracefulShutdownInternal()
3109
return 0, ErrPushLimitReached
3111
sc.maxPushPromiseID += 2
3112
promisedID := sc.maxPushPromiseID
3114
// http://tools.ietf.org/html/rfc7540#section-8.2.
3115
// Strictly speaking, the new stream should start in "reserved (local)", then
3116
// transition to "half closed (remote)" after sending the initial HEADERS, but
3117
// we start in "half closed (remote)" for simplicity.
3118
// See further comments at the definition of stateHalfClosedRemote.
3119
promised := sc.newStream(promisedID, msg.parent.id, stateHalfClosedRemote)
3120
rw, req, err := sc.newWriterAndRequestNoBody(promised, requestParam{
3122
scheme: msg.url.Scheme,
3123
authority: msg.url.Host,
3124
path: msg.url.RequestURI(),
3125
header: cloneHeader(msg.header), // clone since handler runs concurrently with writing the PUSH_PROMISE
3128
// Should not happen, since we've already validated msg.url.
3129
panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err))
3132
go sc.runHandler(rw, req, sc.handler.ServeHTTP)
3133
return promisedID, nil
3136
sc.writeFrame(FrameWriteRequest{
3137
write: &writePushPromise{
3138
streamID: msg.parent.id,
3142
allocatePromisedID: allocatePromisedID,
3149
// foreachHeaderElement splits v according to the "#rule" construction
3150
// in RFC 7230 section 7 and calls fn for each non-empty element.
3151
func foreachHeaderElement(v string, fn func(string)) {
3152
v = textproto.TrimString(v)
3156
if !strings.Contains(v, ",") {
3160
for _, f := range strings.Split(v, ",") {
3161
if f = textproto.TrimString(f); f != "" {
3167
// From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2
3168
var connHeaders = []string{
3172
"Transfer-Encoding",
3176
// checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request,
3177
// per RFC 7540 Section 8.1.2.2.
3178
// The returned error is reported to users.
3179
func checkValidHTTP2RequestHeaders(h http.Header) error {
3180
for _, k := range connHeaders {
3181
if _, ok := h[k]; ok {
3182
return fmt.Errorf("request header %q is not valid in HTTP/2", k)
3186
if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) {
3187
return errors.New(`request header "TE" may only be "trailers" in HTTP/2`)
3192
func new400Handler(err error) http.HandlerFunc {
3193
return func(w http.ResponseWriter, r *http.Request) {
3194
http.Error(w, err.Error(), http.StatusBadRequest)
3198
// h1ServerKeepAlivesDisabled reports whether hs has its keep-alives
3199
// disabled. See comments on h1ServerShutdownChan above for why
3200
// the code is written this way.
3201
func h1ServerKeepAlivesDisabled(hs *http.Server) bool {
3202
var x interface{} = hs
3206
if hs, ok := x.(I); ok {
3207
return !hs.doKeepAlives()
3212
func (sc *serverConn) countError(name string, err error) error {
3213
if sc == nil || sc.srv == nil {
3216
f := sc.srv.CountError
3222
switch e := err.(type) {
3223
case ConnectionError:
3228
code = ErrCode(e.Code)
3232
codeStr := errCodeName[code]
3234
codeStr = strconv.Itoa(int(code))
3236
f(fmt.Sprintf("%s_%s_%s", typ, codeStr, name))