github.com/useflyent/fhttp@v0.0.0-20211004035111-333f430cfbbf/http2/server.go (about) 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. 4 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 15 // returns. 16 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. 25 26 package http2 27 28 import ( 29 "bufio" 30 "bytes" 31 "context" 32 "crypto/tls" 33 "errors" 34 "fmt" 35 "io" 36 "log" 37 "math" 38 "net" 39 "net/textproto" 40 "net/url" 41 "os" 42 "reflect" 43 "runtime" 44 "strconv" 45 "strings" 46 "sync" 47 "time" 48 49 http "github.com/useflyent/fhttp" 50 "github.com/useflyent/fhttp/http2/hpack" 51 52 "golang.org/x/net/http/httpguts" 53 ) 54 55 const ( 56 prefaceTimeout = 10 * time.Second 57 firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway 58 handlerChunkWriteSize = 4 << 10 59 defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to? 60 maxQueuedControlFrames = 10000 61 ) 62 63 var ( 64 errClientDisconnected = errors.New("client disconnected") 65 errClosedBody = errors.New("body closed by handler") 66 errHandlerComplete = errors.New("http2: request body closed due to handler exiting") 67 errStreamClosed = errors.New("http2: stream closed") 68 ) 69 70 var responseWriterStatePool = sync.Pool{ 71 New: func() interface{} { 72 rws := &responseWriterState{} 73 rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize) 74 return rws 75 }, 76 } 77 78 // Test hooks. 79 var ( 80 testHookOnConn func() 81 testHookGetServerConn func(*serverConn) 82 testHookOnPanicMu *sync.Mutex // nil except in tests 83 testHookOnPanic func(sc *serverConn, panicVal interface{}) (rePanic bool) 84 ) 85 86 // Server is an HTTP/2 server. 87 type Server struct { 88 // MaxHandlers limits the number of http.Handler ServeHTTP goroutines 89 // which may run at a time over all connections. 90 // Negative or zero no limit. 91 // TODO: implement 92 MaxHandlers int 93 94 // MaxConcurrentStreams optionally specifies the number of 95 // concurrent streams that each client may have open at a 96 // time. This is unrelated to the number of http.Handler goroutines 97 // which may be active globally, which is MaxHandlers. 98 // If zero, MaxConcurrentStreams defaults to at least 100, per 99 // the HTTP/2 spec's recommendations. 100 MaxConcurrentStreams uint32 101 102 // MaxReadFrameSize optionally specifies the largest frame 103 // this server is willing to read. A valid value is between 104 // 16k and 16M, inclusive. If zero or otherwise invalid, a 105 // default value is used. 106 MaxReadFrameSize uint32 107 108 // PermitProhibitedCipherSuites, if true, permits the use of 109 // cipher suites prohibited by the HTTP/2 spec. 110 PermitProhibitedCipherSuites bool 111 112 // IdleTimeout specifies how long until idle clients should be 113 // closed with a GOAWAY frame. PING frames are not considered 114 // activity for the purposes of IdleTimeout. 115 IdleTimeout time.Duration 116 117 // MaxUploadBufferPerConnection is the size of the initial flow 118 // control window for each connections. The HTTP/2 spec does not 119 // allow this to be smaller than 65535 or larger than 2^32-1. 120 // If the value is outside this range, a default value will be 121 // used instead. 122 MaxUploadBufferPerConnection int32 123 124 // MaxUploadBufferPerStream is the size of the initial flow control 125 // window for each stream. The HTTP/2 spec does not allow this to 126 // be larger than 2^32-1. If the value is zero or larger than the 127 // maximum, a default value will be used instead. 128 MaxUploadBufferPerStream int32 129 130 // NewWriteScheduler constructs a write scheduler for a connection. 131 // If nil, a default scheduler is chosen. 132 NewWriteScheduler func() WriteScheduler 133 134 // Internal state. This is a pointer (rather than embedded directly) 135 // so that we don't embed a Mutex in this struct, which will make the 136 // struct non-copyable, which might break some callers. 137 state *serverInternalState 138 } 139 140 func (s *Server) initialConnRecvWindowSize() int32 { 141 if s.MaxUploadBufferPerConnection > initialWindowSize { 142 return s.MaxUploadBufferPerConnection 143 } 144 return 1 << 20 145 } 146 147 func (s *Server) initialStreamRecvWindowSize() int32 { 148 if s.MaxUploadBufferPerStream > 0 { 149 return s.MaxUploadBufferPerStream 150 } 151 return 1 << 20 152 } 153 154 func (s *Server) maxReadFrameSize() uint32 { 155 if v := s.MaxReadFrameSize; v >= minMaxFrameSize && v <= maxFrameSize { 156 return v 157 } 158 return defaultMaxReadFrameSize 159 } 160 161 func (s *Server) maxConcurrentStreams() uint32 { 162 if v := s.MaxConcurrentStreams; v > 0 { 163 return v 164 } 165 return defaultMaxStreams 166 } 167 168 // maxQueuedControlFrames is the maximum number of control frames like 169 // SETTINGS, PING and RST_STREAM that will be queued for writing before 170 // the connection is closed to prevent memory exhaustion attacks. 171 func (s *Server) maxQueuedControlFrames() int { 172 // TODO: if anybody asks, add a Server field, and remember to define the 173 // behavior of negative values. 174 return maxQueuedControlFrames 175 } 176 177 type serverInternalState struct { 178 mu sync.Mutex 179 activeConns map[*serverConn]struct{} 180 } 181 182 func (s *serverInternalState) registerConn(sc *serverConn) { 183 if s == nil { 184 return // if the Server was used without calling ConfigureServer 185 } 186 s.mu.Lock() 187 s.activeConns[sc] = struct{}{} 188 s.mu.Unlock() 189 } 190 191 func (s *serverInternalState) unregisterConn(sc *serverConn) { 192 if s == nil { 193 return // if the Server was used without calling ConfigureServer 194 } 195 s.mu.Lock() 196 delete(s.activeConns, sc) 197 s.mu.Unlock() 198 } 199 200 func (s *serverInternalState) startGracefulShutdown() { 201 if s == nil { 202 return // if the Server was used without calling ConfigureServer 203 } 204 s.mu.Lock() 205 for sc := range s.activeConns { 206 sc.startGracefulShutdown() 207 } 208 s.mu.Unlock() 209 } 210 211 // ConfigureServer adds HTTP/2 support to a net/http Server. 212 // 213 // The configuration conf may be nil. 214 // 215 // ConfigureServer must be called before s begins serving. 216 func ConfigureServer(s *http.Server, conf *Server) error { 217 if s == nil { 218 panic("nil *http.Server") 219 } 220 if conf == nil { 221 conf = new(Server) 222 } 223 conf.state = &serverInternalState{activeConns: make(map[*serverConn]struct{})} 224 if h1, h2 := s, conf; h2.IdleTimeout == 0 { 225 if h1.IdleTimeout != 0 { 226 h2.IdleTimeout = h1.IdleTimeout 227 } else { 228 h2.IdleTimeout = h1.ReadTimeout 229 } 230 } 231 s.RegisterOnShutdown(conf.state.startGracefulShutdown) 232 233 if s.TLSConfig == nil { 234 s.TLSConfig = new(tls.Config) 235 } else if s.TLSConfig.CipherSuites != nil { 236 // If they already provided a CipherSuite list, return 237 // an error if it has a bad order or is missing 238 // ECDHE_RSA_WITH_AES_128_GCM_SHA256 or ECDHE_ECDSA_WITH_AES_128_GCM_SHA256. 239 haveRequired := false 240 sawBad := false 241 for i, cs := range s.TLSConfig.CipherSuites { 242 switch cs { 243 case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 244 // Alternative MTI cipher to not discourage ECDSA-only servers. 245 // See http://golang.org/cl/30721 for further information. 246 tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: 247 haveRequired = true 248 } 249 if isBadCipher(cs) { 250 sawBad = true 251 } else if sawBad { 252 return fmt.Errorf("http2: TLSConfig.CipherSuites index %d contains an HTTP/2-approved cipher suite (%#04x), but it comes after unapproved cipher suites. With this configuration, clients that don't support previous, approved cipher suites may be given an unapproved one and reject the connection.", i, cs) 253 } 254 } 255 if !haveRequired { 256 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).") 257 } 258 } 259 260 // Note: not setting MinVersion to tls.VersionTLS12, 261 // as we don't want to interfere with HTTP/1.1 traffic 262 // on the user's server. We enforce TLS 1.2 later once 263 // we accept a connection. Ideally this should be done 264 // during next-proto selection, but using TLS <1.2 with 265 // HTTP/2 is still the client's bug. 266 267 s.TLSConfig.PreferServerCipherSuites = true 268 269 haveNPN := false 270 for _, p := range s.TLSConfig.NextProtos { 271 if p == NextProtoTLS { 272 haveNPN = true 273 break 274 } 275 } 276 if !haveNPN { 277 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, NextProtoTLS) 278 } 279 280 if s.TLSNextProto == nil { 281 s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){} 282 } 283 protoHandler := func(hs *http.Server, c *tls.Conn, h http.Handler) { 284 if testHookOnConn != nil { 285 testHookOnConn() 286 } 287 // The TLSNextProto interface predates contexts, so 288 // the net/http package passes down its per-connection 289 // base context via an exported but unadvertised 290 // method on the Handler. This is for internal 291 // net/http<=>http2 use only. 292 var ctx context.Context 293 type baseContexter interface { 294 BaseContext() context.Context 295 } 296 if bc, ok := h.(baseContexter); ok { 297 ctx = bc.BaseContext() 298 } 299 conf.ServeConn(c, &ServeConnOpts{ 300 Context: ctx, 301 Handler: h, 302 BaseConfig: hs, 303 }) 304 } 305 s.TLSNextProto[NextProtoTLS] = protoHandler 306 return nil 307 } 308 309 // ServeConnOpts are options for the Server.ServeConn method. 310 type ServeConnOpts struct { 311 // Context is the base context to use. 312 // If nil, context.Background is used. 313 Context context.Context 314 315 // BaseConfig optionally sets the base configuration 316 // for values. If nil, defaults are used. 317 BaseConfig *http.Server 318 319 // Handler specifies which handler to use for processing 320 // requests. If nil, BaseConfig.Handler is used. If BaseConfig 321 // or BaseConfig.Handler is nil, http.DefaultServeMux is used. 322 Handler http.Handler 323 } 324 325 func (o *ServeConnOpts) context() context.Context { 326 if o != nil && o.Context != nil { 327 return o.Context 328 } 329 return context.Background() 330 } 331 332 func (o *ServeConnOpts) baseConfig() *http.Server { 333 if o != nil && o.BaseConfig != nil { 334 return o.BaseConfig 335 } 336 return new(http.Server) 337 } 338 339 func (o *ServeConnOpts) handler() http.Handler { 340 if o != nil { 341 if o.Handler != nil { 342 return o.Handler 343 } 344 if o.BaseConfig != nil && o.BaseConfig.Handler != nil { 345 return o.BaseConfig.Handler 346 } 347 } 348 return http.DefaultServeMux 349 } 350 351 // ServeConn serves HTTP/2 requests on the provided connection and 352 // blocks until the connection is no longer readable. 353 // 354 // ServeConn starts speaking HTTP/2 assuming that c has not had any 355 // reads or writes. It writes its initial settings frame and expects 356 // to be able to read the preface and settings frame from the 357 // client. If c has a ConnectionState method like a *tls.Conn, the 358 // ConnectionState is used to verify the TLS ciphersuite and to set 359 // the Request.TLS field in Handlers. 360 // 361 // ServeConn does not support h2c by itself. Any h2c support must be 362 // implemented in terms of providing a suitably-behaving net.Conn. 363 // 364 // The opts parameter is optional. If nil, default values are used. 365 func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) { 366 baseCtx, cancel := serverConnBaseContext(c, opts) 367 defer cancel() 368 369 sc := &serverConn{ 370 srv: s, 371 hs: opts.baseConfig(), 372 conn: c, 373 baseCtx: baseCtx, 374 remoteAddrStr: c.RemoteAddr().String(), 375 bw: newBufferedWriter(c), 376 handler: opts.handler(), 377 streams: make(map[uint32]*stream), 378 readFrameCh: make(chan readFrameResult), 379 wantWriteFrameCh: make(chan FrameWriteRequest, 8), 380 serveMsgCh: make(chan interface{}, 8), 381 wroteFrameCh: make(chan frameWriteResult, 1), // buffered; one send in writeFrameAsync 382 bodyReadCh: make(chan bodyReadMsg), // buffering doesn't matter either way 383 doneServing: make(chan struct{}), 384 clientMaxStreams: math.MaxUint32, // Section 6.5.2: "Initially, there is no limit to this value" 385 advMaxStreams: s.maxConcurrentStreams(), 386 initialStreamSendWindowSize: initialWindowSize, 387 maxFrameSize: initialMaxFrameSize, 388 headerTableSize: initialHeaderTableSize, 389 serveG: newGoroutineLock(), 390 pushEnabled: true, 391 } 392 393 s.state.registerConn(sc) 394 defer s.state.unregisterConn(sc) 395 396 // The net/http package sets the write deadline from the 397 // http.Server.WriteTimeout during the TLS handshake, but then 398 // passes the connection off to us with the deadline already set. 399 // Write deadlines are set per stream in serverConn.newStream. 400 // Disarm the net.Conn write deadline here. 401 if sc.hs.WriteTimeout != 0 { 402 sc.conn.SetWriteDeadline(time.Time{}) 403 } 404 405 if s.NewWriteScheduler != nil { 406 sc.writeSched = s.NewWriteScheduler() 407 } else { 408 sc.writeSched = NewRandomWriteScheduler() 409 } 410 411 // These start at the RFC-specified defaults. If there is a higher 412 // configured value for inflow, that will be updated when we send a 413 // WINDOW_UPDATE shortly after sending SETTINGS. 414 sc.flow.add(initialWindowSize) 415 sc.inflow.add(initialWindowSize) 416 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf) 417 418 fr := NewFramer(sc.bw, c) 419 fr.ReadMetaHeaders = hpack.NewDecoder(initialHeaderTableSize, nil) 420 fr.MaxHeaderListSize = sc.maxHeaderListSize() 421 fr.SetMaxReadFrameSize(s.maxReadFrameSize()) 422 sc.framer = fr 423 424 if tc, ok := c.(connectionStater); ok { 425 sc.tlsState = new(tls.ConnectionState) 426 *sc.tlsState = tc.ConnectionState() 427 // 9.2 Use of TLS Features 428 // An implementation of HTTP/2 over TLS MUST use TLS 429 // 1.2 or higher with the restrictions on feature set 430 // and cipher suite described in this section. Due to 431 // implementation limitations, it might not be 432 // possible to fail TLS negotiation. An endpoint MUST 433 // immediately terminate an HTTP/2 connection that 434 // does not meet the TLS requirements described in 435 // this section with a connection error (Section 436 // 5.4.1) of type INADEQUATE_SECURITY. 437 if sc.tlsState.Version < tls.VersionTLS12 { 438 sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low") 439 return 440 } 441 442 if sc.tlsState.ServerName == "" { 443 // Client must use SNI, but we don't enforce that anymore, 444 // since it was causing problems when connecting to bare IP 445 // addresses during development. 446 // 447 // TODO: optionally enforce? Or enforce at the time we receive 448 // a new request, and verify the ServerName matches the :authority? 449 // But that precludes proxy situations, perhaps. 450 // 451 // So for now, do nothing here again. 452 } 453 454 if !s.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) { 455 // "Endpoints MAY choose to generate a connection error 456 // (Section 5.4.1) of type INADEQUATE_SECURITY if one of 457 // the prohibited cipher suites are negotiated." 458 // 459 // We choose that. In my opinion, the spec is weak 460 // here. It also says both parties must support at least 461 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no 462 // excuses here. If we really must, we could allow an 463 // "AllowInsecureWeakCiphers" option on the server later. 464 // Let's see how it plays out first. 465 sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite)) 466 return 467 } 468 } 469 470 if hook := testHookGetServerConn; hook != nil { 471 hook(sc) 472 } 473 sc.serve() 474 } 475 476 func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) { 477 ctx, cancel = context.WithCancel(opts.context()) 478 ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr()) 479 if hs := opts.baseConfig(); hs != nil { 480 ctx = context.WithValue(ctx, http.ServerContextKey, hs) 481 } 482 return 483 } 484 485 func (sc *serverConn) rejectConn(err ErrCode, debug string) { 486 sc.vlogf("http2: server rejecting conn: %v, %s", err, debug) 487 // ignoring errors. hanging up anyway. 488 sc.framer.WriteGoAway(0, err, []byte(debug)) 489 sc.bw.Flush() 490 sc.conn.Close() 491 } 492 493 type serverConn struct { 494 // Immutable: 495 srv *Server 496 hs *http.Server 497 conn net.Conn 498 bw *bufferedWriter // writing to conn 499 handler http.Handler 500 baseCtx context.Context 501 framer *Framer 502 doneServing chan struct{} // closed when serverConn.serve ends 503 readFrameCh chan readFrameResult // written by serverConn.readFrames 504 wantWriteFrameCh chan FrameWriteRequest // from handlers -> serve 505 wroteFrameCh chan frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes 506 bodyReadCh chan bodyReadMsg // from handlers -> serve 507 serveMsgCh chan interface{} // misc messages & code to send to / run on the serve loop 508 flow flow // conn-wide (not stream-specific) outbound flow control 509 inflow flow // conn-wide inbound flow control 510 tlsState *tls.ConnectionState // shared by all handlers, like net/http 511 remoteAddrStr string 512 writeSched WriteScheduler 513 514 // Everything following is owned by the serve loop; use serveG.check(): 515 serveG goroutineLock // used to verify funcs are on serve() 516 pushEnabled bool 517 sawFirstSettings bool // got the initial SETTINGS frame after the preface 518 needToSendSettingsAck bool 519 unackedSettings int // how many SETTINGS have we sent without ACKs? 520 queuedControlFrames int // control frames in the writeSched queue 521 clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit) 522 advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client 523 curClientStreams uint32 // number of open streams initiated by the client 524 curPushedStreams uint32 // number of open streams initiated by server push 525 maxClientStreamID uint32 // max ever seen from client (odd), or 0 if there have been no client requests 526 maxPushPromiseID uint32 // ID of the last push promise (even), or 0 if there have been no pushes 527 streams map[uint32]*stream 528 initialStreamSendWindowSize int32 529 maxFrameSize int32 530 headerTableSize uint32 531 peerMaxHeaderListSize uint32 // zero means unknown (default) 532 canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case 533 writingFrame bool // started writing a frame (on serve goroutine or separate) 534 writingFrameAsync bool // started a frame on its own goroutine but haven't heard back on wroteFrameCh 535 needsFrameFlush bool // last frame write wasn't a flush 536 inGoAway bool // we've started to or sent GOAWAY 537 inFrameScheduleLoop bool // whether we're in the scheduleFrameWrite loop 538 needToSendGoAway bool // we need to schedule a GOAWAY frame write 539 goAwayCode ErrCode 540 shutdownTimer *time.Timer // nil until used 541 idleTimer *time.Timer // nil if unused 542 543 // Owned by the writeFrameAsync goroutine: 544 headerWriteBuf bytes.Buffer 545 hpackEncoder *hpack.Encoder 546 547 // Used by startGracefulShutdown. 548 shutdownOnce sync.Once 549 } 550 551 func (sc *serverConn) maxHeaderListSize() uint32 { 552 n := sc.hs.MaxHeaderBytes 553 if n <= 0 { 554 n = http.DefaultMaxHeaderBytes 555 } 556 // http2's count is in a slightly different unit and includes 32 bytes per pair. 557 // So, take the net/http.Server value and pad it up a bit, assuming 10 headers. 558 const perFieldOverhead = 32 // per http2 spec 559 const typicalHeaders = 10 // conservative 560 return uint32(n + typicalHeaders*perFieldOverhead) 561 } 562 563 func (sc *serverConn) curOpenStreams() uint32 { 564 sc.serveG.check() 565 return sc.curClientStreams + sc.curPushedStreams 566 } 567 568 // stream represents a stream. This is the minimal metadata needed by 569 // the serve goroutine. Most of the actual stream state is owned by 570 // the http.Handler's goroutine in the responseWriter. Because the 571 // responseWriter's responseWriterState is recycled at the end of a 572 // handler, this struct intentionally has no pointer to the 573 // *responseWriter{,State} itself, as the Handler ending nils out the 574 // responseWriter's state field. 575 type stream struct { 576 // immutable: 577 sc *serverConn 578 id uint32 579 body *pipe // non-nil if expecting DATA frames 580 cw closeWaiter // closed wait stream transitions to closed state 581 ctx context.Context 582 cancelCtx func() 583 584 // owned by serverConn's serve loop: 585 bodyBytes int64 // body bytes seen so far 586 declBodyBytes int64 // or -1 if undeclared 587 flow flow // limits writing from Handler to client 588 inflow flow // what the client is allowed to POST/etc to us 589 state streamState 590 resetQueued bool // RST_STREAM queued for write; set by sc.resetStream 591 gotTrailerHeader bool // HEADER frame for trailers was seen 592 wroteHeaders bool // whether we wrote headers (not status 100) 593 writeDeadline *time.Timer // nil if unused 594 595 trailer http.Header // accumulated trailers 596 reqTrailer http.Header // handler's Request.Trailer 597 } 598 599 func (sc *serverConn) Framer() *Framer { return sc.framer } 600 func (sc *serverConn) CloseConn() error { return sc.conn.Close() } 601 func (sc *serverConn) Flush() error { return sc.bw.Flush() } 602 func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) { 603 return sc.hpackEncoder, &sc.headerWriteBuf 604 } 605 606 func (sc *serverConn) state(streamID uint32) (streamState, *stream) { 607 sc.serveG.check() 608 // http://tools.ietf.org/html/rfc7540#section-5.1 609 if st, ok := sc.streams[streamID]; ok { 610 return st.state, st 611 } 612 // "The first use of a new stream identifier implicitly closes all 613 // streams in the "idle" state that might have been initiated by 614 // that peer with a lower-valued stream identifier. For example, if 615 // a client sends a HEADERS frame on stream 7 without ever sending a 616 // frame on stream 5, then stream 5 transitions to the "closed" 617 // state when the first frame for stream 7 is sent or received." 618 if streamID%2 == 1 { 619 if streamID <= sc.maxClientStreamID { 620 return stateClosed, nil 621 } 622 } else { 623 if streamID <= sc.maxPushPromiseID { 624 return stateClosed, nil 625 } 626 } 627 return stateIdle, nil 628 } 629 630 // setConnState calls the net/http ConnState hook for this connection, if configured. 631 // Note that the net/http package does StateNew and StateClosed for us. 632 // There is currently no plan for StateHijacked or hijacking HTTP/2 connections. 633 func (sc *serverConn) setConnState(state http.ConnState) { 634 if sc.hs.ConnState != nil { 635 sc.hs.ConnState(sc.conn, state) 636 } 637 } 638 639 func (sc *serverConn) vlogf(format string, args ...interface{}) { 640 if VerboseLogs { 641 sc.logf(format, args...) 642 } 643 } 644 645 func (sc *serverConn) logf(format string, args ...interface{}) { 646 if lg := sc.hs.ErrorLog; lg != nil { 647 lg.Printf(format, args...) 648 } else { 649 log.Printf(format, args...) 650 } 651 } 652 653 // errno returns v's underlying uintptr, else 0. 654 // 655 // TODO: remove this helper function once http2 can use build 656 // tags. See comment in isClosedConnError. 657 func errno(v error) uintptr { 658 if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr { 659 return uintptr(rv.Uint()) 660 } 661 return 0 662 } 663 664 // isClosedConnError reports whether err is an error from use of a closed 665 // network connection. 666 func isClosedConnError(err error) bool { 667 if err == nil { 668 return false 669 } 670 671 // TODO: remove this string search and be more like the Windows 672 // case below. That might involve modifying the standard library 673 // to return better error types. 674 str := err.Error() 675 if strings.Contains(str, "use of closed network connection") { 676 return true 677 } 678 679 // TODO(bradfitz): x/tools/cmd/bundle doesn't really support 680 // build tags, so I can't make an http2_windows.go file with 681 // Windows-specific stuff. Fix that and move this, once we 682 // have a way to bundle this into std's net/http somehow. 683 if runtime.GOOS == "windows" { 684 if oe, ok := err.(*net.OpError); ok && oe.Op == "read" { 685 if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" { 686 const WSAECONNABORTED = 10053 687 const WSAECONNRESET = 10054 688 if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED { 689 return true 690 } 691 } 692 } 693 } 694 return false 695 } 696 697 func (sc *serverConn) condlogf(err error, format string, args ...interface{}) { 698 if err == nil { 699 return 700 } 701 if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) || err == errPrefaceTimeout { 702 // Boring, expected errors. 703 sc.vlogf(format, args...) 704 } else { 705 sc.logf(format, args...) 706 } 707 } 708 709 func (sc *serverConn) canonicalHeader(v string) string { 710 sc.serveG.check() 711 buildCommonHeaderMapsOnce() 712 cv, ok := commonCanonHeader[v] 713 if ok { 714 return cv 715 } 716 cv, ok = sc.canonHeader[v] 717 if ok { 718 return cv 719 } 720 if sc.canonHeader == nil { 721 sc.canonHeader = make(map[string]string) 722 } 723 cv = http.CanonicalHeaderKey(v) 724 sc.canonHeader[v] = cv 725 return cv 726 } 727 728 type readFrameResult struct { 729 f Frame // valid until readMore is called 730 err error 731 732 // readMore should be called once the consumer no longer needs or 733 // retains f. After readMore, f is invalid and more frames can be 734 // read. 735 readMore func() 736 } 737 738 // readFrames is the loop that reads incoming frames. 739 // It takes care to only read one frame at a time, blocking until the 740 // consumer is done with the frame. 741 // It's run on its own goroutine. 742 func (sc *serverConn) readFrames() { 743 gate := make(gate) 744 gateDone := gate.Done 745 for { 746 f, err := sc.framer.ReadFrame() 747 select { 748 case sc.readFrameCh <- readFrameResult{f, err, gateDone}: 749 case <-sc.doneServing: 750 return 751 } 752 select { 753 case <-gate: 754 case <-sc.doneServing: 755 return 756 } 757 if terminalReadFrameError(err) { 758 return 759 } 760 } 761 } 762 763 // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine. 764 type frameWriteResult struct { 765 _ incomparable 766 wr FrameWriteRequest // what was written (or attempted) 767 err error // result of the writeFrame call 768 } 769 770 // writeFrameAsync runs in its own goroutine and writes a single frame 771 // and then reports when it's done. 772 // At most one goroutine can be running writeFrameAsync at a time per 773 // serverConn. 774 func (sc *serverConn) writeFrameAsync(wr FrameWriteRequest) { 775 err := wr.write.writeFrame(sc) 776 sc.wroteFrameCh <- frameWriteResult{wr: wr, err: err} 777 } 778 779 func (sc *serverConn) closeAllStreamsOnConnClose() { 780 sc.serveG.check() 781 for _, st := range sc.streams { 782 sc.closeStream(st, errClientDisconnected) 783 } 784 } 785 786 func (sc *serverConn) stopShutdownTimer() { 787 sc.serveG.check() 788 if t := sc.shutdownTimer; t != nil { 789 t.Stop() 790 } 791 } 792 793 func (sc *serverConn) notePanic() { 794 // Note: this is for serverConn.serve panicking, not http.Handler code. 795 if testHookOnPanicMu != nil { 796 testHookOnPanicMu.Lock() 797 defer testHookOnPanicMu.Unlock() 798 } 799 if testHookOnPanic != nil { 800 if e := recover(); e != nil { 801 if testHookOnPanic(sc, e) { 802 panic(e) 803 } 804 } 805 } 806 } 807 808 func (sc *serverConn) serve() { 809 sc.serveG.check() 810 defer sc.notePanic() 811 defer sc.conn.Close() 812 defer sc.closeAllStreamsOnConnClose() 813 defer sc.stopShutdownTimer() 814 defer close(sc.doneServing) // unblocks handlers trying to send 815 816 if VerboseLogs { 817 sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs) 818 } 819 820 sc.writeFrame(FrameWriteRequest{ 821 write: writeSettings{ 822 {SettingMaxFrameSize, sc.srv.maxReadFrameSize()}, 823 {SettingMaxConcurrentStreams, sc.advMaxStreams}, 824 {SettingMaxHeaderListSize, sc.maxHeaderListSize()}, 825 {SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())}, 826 }, 827 }) 828 sc.unackedSettings++ 829 830 // Each connection starts with intialWindowSize inflow tokens. 831 // If a higher value is configured, we add more tokens. 832 if diff := sc.srv.initialConnRecvWindowSize() - initialWindowSize; diff > 0 { 833 sc.sendWindowUpdate(nil, int(diff)) 834 } 835 836 if err := sc.readPreface(); err != nil { 837 sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err) 838 return 839 } 840 // Now that we've got the preface, get us out of the 841 // "StateNew" state. We can't go directly to idle, though. 842 // Active means we read some data and anticipate a request. We'll 843 // do another Active when we get a HEADERS frame. 844 sc.setConnState(http.StateActive) 845 sc.setConnState(http.StateIdle) 846 847 if sc.srv.IdleTimeout != 0 { 848 sc.idleTimer = time.AfterFunc(sc.srv.IdleTimeout, sc.onIdleTimer) 849 defer sc.idleTimer.Stop() 850 } 851 852 go sc.readFrames() // closed by defer sc.conn.Close above 853 854 settingsTimer := time.AfterFunc(firstSettingsTimeout, sc.onSettingsTimer) 855 defer settingsTimer.Stop() 856 857 loopNum := 0 858 for { 859 loopNum++ 860 select { 861 case wr := <-sc.wantWriteFrameCh: 862 if se, ok := wr.write.(StreamError); ok { 863 sc.resetStream(se) 864 break 865 } 866 sc.writeFrame(wr) 867 case res := <-sc.wroteFrameCh: 868 sc.wroteFrame(res) 869 case res := <-sc.readFrameCh: 870 if !sc.processFrameFromReader(res) { 871 return 872 } 873 res.readMore() 874 if settingsTimer != nil { 875 settingsTimer.Stop() 876 settingsTimer = nil 877 } 878 case m := <-sc.bodyReadCh: 879 sc.noteBodyRead(m.st, m.n) 880 case msg := <-sc.serveMsgCh: 881 switch v := msg.(type) { 882 case func(int): 883 v(loopNum) // for testing 884 case *serverMessage: 885 switch v { 886 case settingsTimerMsg: 887 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr()) 888 return 889 case idleTimerMsg: 890 sc.vlogf("connection is idle") 891 sc.goAway(ErrCodeNo) 892 case shutdownTimerMsg: 893 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr()) 894 return 895 case gracefulShutdownMsg: 896 sc.startGracefulShutdownInternal() 897 default: 898 panic("unknown timer") 899 } 900 case *startPushRequest: 901 sc.startPush(v) 902 default: 903 panic(fmt.Sprintf("unexpected type %T", v)) 904 } 905 } 906 907 // If the peer is causing us to generate a lot of control frames, 908 // but not reading them from us, assume they are trying to make us 909 // run out of memory. 910 if sc.queuedControlFrames > sc.srv.maxQueuedControlFrames() { 911 sc.vlogf("http2: too many control frames in send queue, closing connection") 912 return 913 } 914 915 // Start the shutdown timer after sending a GOAWAY. When sending GOAWAY 916 // with no error code (graceful shutdown), don't start the timer until 917 // all open streams have been completed. 918 sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame 919 gracefulShutdownComplete := sc.goAwayCode == ErrCodeNo && sc.curOpenStreams() == 0 920 if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != ErrCodeNo || gracefulShutdownComplete) { 921 sc.shutDownIn(goAwayTimeout) 922 } 923 } 924 } 925 926 func (sc *serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) { 927 select { 928 case <-sc.doneServing: 929 case <-sharedCh: 930 close(privateCh) 931 } 932 } 933 934 type serverMessage int 935 936 // Message values sent to serveMsgCh. 937 var ( 938 settingsTimerMsg = new(serverMessage) 939 idleTimerMsg = new(serverMessage) 940 shutdownTimerMsg = new(serverMessage) 941 gracefulShutdownMsg = new(serverMessage) 942 ) 943 944 func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) } 945 func (sc *serverConn) onIdleTimer() { sc.sendServeMsg(idleTimerMsg) } 946 func (sc *serverConn) onShutdownTimer() { sc.sendServeMsg(shutdownTimerMsg) } 947 948 func (sc *serverConn) sendServeMsg(msg interface{}) { 949 sc.serveG.checkNotOn() // NOT 950 select { 951 case sc.serveMsgCh <- msg: 952 case <-sc.doneServing: 953 } 954 } 955 956 var errPrefaceTimeout = errors.New("timeout waiting for client preface") 957 958 // readPreface reads the ClientPreface greeting from the peer or 959 // returns errPrefaceTimeout on timeout, or an error if the greeting 960 // is invalid. 961 func (sc *serverConn) readPreface() error { 962 errc := make(chan error, 1) 963 go func() { 964 // Read the client preface 965 buf := make([]byte, len(ClientPreface)) 966 if _, err := io.ReadFull(sc.conn, buf); err != nil { 967 errc <- err 968 } else if !bytes.Equal(buf, clientPreface) { 969 errc <- fmt.Errorf("bogus greeting %q", buf) 970 } else { 971 errc <- nil 972 } 973 }() 974 timer := time.NewTimer(prefaceTimeout) // TODO: configurable on *Server? 975 defer timer.Stop() 976 select { 977 case <-timer.C: 978 return errPrefaceTimeout 979 case err := <-errc: 980 if err == nil { 981 if VerboseLogs { 982 sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr()) 983 } 984 } 985 return err 986 } 987 } 988 989 var errChanPool = sync.Pool{ 990 New: func() interface{} { return make(chan error, 1) }, 991 } 992 993 var writeDataPool = sync.Pool{ 994 New: func() interface{} { return new(writeData) }, 995 } 996 997 // writeDataFromHandler writes DATA response frames from a handler on 998 // the given stream. 999 func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error { 1000 ch := errChanPool.Get().(chan error) 1001 writeArg := writeDataPool.Get().(*writeData) 1002 *writeArg = writeData{stream.id, data, endStream} 1003 err := sc.writeFrameFromHandler(FrameWriteRequest{ 1004 write: writeArg, 1005 stream: stream, 1006 done: ch, 1007 }) 1008 if err != nil { 1009 return err 1010 } 1011 var frameWriteDone bool // the frame write is done (successfully or not) 1012 select { 1013 case err = <-ch: 1014 frameWriteDone = true 1015 case <-sc.doneServing: 1016 return errClientDisconnected 1017 case <-stream.cw: 1018 // If both ch and stream.cw were ready (as might 1019 // happen on the final Write after an http.Handler 1020 // ends), prefer the write result. Otherwise this 1021 // might just be us successfully closing the stream. 1022 // The writeFrameAsync and serve goroutines guarantee 1023 // that the ch send will happen before the stream.cw 1024 // close. 1025 select { 1026 case err = <-ch: 1027 frameWriteDone = true 1028 default: 1029 return errStreamClosed 1030 } 1031 } 1032 errChanPool.Put(ch) 1033 if frameWriteDone { 1034 writeDataPool.Put(writeArg) 1035 } 1036 return err 1037 } 1038 1039 // writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts 1040 // if the connection has gone away. 1041 // 1042 // This must not be run from the serve goroutine itself, else it might 1043 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 1044 // buffered and is read by serve itself). If you're on the serve 1045 // goroutine, call writeFrame instead. 1046 func (sc *serverConn) writeFrameFromHandler(wr FrameWriteRequest) error { 1047 sc.serveG.checkNotOn() // NOT 1048 select { 1049 case sc.wantWriteFrameCh <- wr: 1050 return nil 1051 case <-sc.doneServing: 1052 // Serve loop is gone. 1053 // Client has closed their connection to the server. 1054 return errClientDisconnected 1055 } 1056 } 1057 1058 // writeFrame schedules a frame to write and sends it if there's nothing 1059 // already being written. 1060 // 1061 // There is no pushback here (the serve goroutine never blocks). It's 1062 // the http.Handlers that block, waiting for their previous frames to 1063 // make it onto the wire 1064 // 1065 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 1066 func (sc *serverConn) writeFrame(wr FrameWriteRequest) { 1067 sc.serveG.check() 1068 1069 // If true, wr will not be written and wr.done will not be signaled. 1070 var ignoreWrite bool 1071 1072 // We are not allowed to write frames on closed streams. RFC 7540 Section 1073 // 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on 1074 // a closed stream." Our server never sends PRIORITY, so that exception 1075 // does not apply. 1076 // 1077 // The serverConn might close an open stream while the stream's handler 1078 // is still running. For example, the server might close a stream when it 1079 // receives bad data from the client. If this happens, the handler might 1080 // attempt to write a frame after the stream has been closed (since the 1081 // handler hasn't yet been notified of the close). In this case, we simply 1082 // ignore the frame. The handler will notice that the stream is closed when 1083 // it waits for the frame to be written. 1084 // 1085 // As an exception to this rule, we allow sending RST_STREAM after close. 1086 // This allows us to immediately reject new streams without tracking any 1087 // state for those streams (except for the queued RST_STREAM frame). This 1088 // may result in duplicate RST_STREAMs in some cases, but the client should 1089 // ignore those. 1090 if wr.StreamID() != 0 { 1091 _, isReset := wr.write.(StreamError) 1092 if state, _ := sc.state(wr.StreamID()); state == stateClosed && !isReset { 1093 ignoreWrite = true 1094 } 1095 } 1096 1097 // Don't send a 100-continue response if we've already sent headers. 1098 // See golang.org/issue/14030. 1099 switch wr.write.(type) { 1100 case *writeResHeaders: 1101 wr.stream.wroteHeaders = true 1102 case write100ContinueHeadersFrame: 1103 if wr.stream.wroteHeaders { 1104 // We do not need to notify wr.done because this frame is 1105 // never written with wr.done != nil. 1106 if wr.done != nil { 1107 panic("wr.done != nil for write100ContinueHeadersFrame") 1108 } 1109 ignoreWrite = true 1110 } 1111 } 1112 1113 if !ignoreWrite { 1114 if wr.isControl() { 1115 sc.queuedControlFrames++ 1116 // For extra safety, detect wraparounds, which should not happen, 1117 // and pull the plug. 1118 if sc.queuedControlFrames < 0 { 1119 sc.conn.Close() 1120 } 1121 } 1122 sc.writeSched.Push(wr) 1123 } 1124 sc.scheduleFrameWrite() 1125 } 1126 1127 // startFrameWrite starts a goroutine to write wr (in a separate 1128 // goroutine since that might block on the network), and updates the 1129 // serve goroutine's state about the world, updated from info in wr. 1130 func (sc *serverConn) startFrameWrite(wr FrameWriteRequest) { 1131 sc.serveG.check() 1132 if sc.writingFrame { 1133 panic("internal error: can only be writing one frame at a time") 1134 } 1135 1136 st := wr.stream 1137 if st != nil { 1138 switch st.state { 1139 case stateHalfClosedLocal: 1140 switch wr.write.(type) { 1141 case StreamError, handlerPanicRST, writeWindowUpdate: 1142 // RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE 1143 // in this state. (We never send PRIORITY from the server, so that is not checked.) 1144 default: 1145 panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr)) 1146 } 1147 case stateClosed: 1148 panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr)) 1149 } 1150 } 1151 if wpp, ok := wr.write.(*writePushPromise); ok { 1152 var err error 1153 wpp.promisedID, err = wpp.allocatePromisedID() 1154 if err != nil { 1155 sc.writingFrameAsync = false 1156 wr.replyToWriter(err) 1157 return 1158 } 1159 } 1160 1161 sc.writingFrame = true 1162 sc.needsFrameFlush = true 1163 if wr.write.staysWithinBuffer(sc.bw.Available()) { 1164 sc.writingFrameAsync = false 1165 err := wr.write.writeFrame(sc) 1166 sc.wroteFrame(frameWriteResult{wr: wr, err: err}) 1167 } else { 1168 sc.writingFrameAsync = true 1169 go sc.writeFrameAsync(wr) 1170 } 1171 } 1172 1173 // errHandlerPanicked is the error given to any callers blocked in a read from 1174 // Request.Body when the main goroutine panics. Since most handlers read in the 1175 // main ServeHTTP goroutine, this will show up rarely. 1176 var errHandlerPanicked = errors.New("http2: handler panicked") 1177 1178 // wroteFrame is called on the serve goroutine with the result of 1179 // whatever happened on writeFrameAsync. 1180 func (sc *serverConn) wroteFrame(res frameWriteResult) { 1181 sc.serveG.check() 1182 if !sc.writingFrame { 1183 panic("internal error: expected to be already writing a frame") 1184 } 1185 sc.writingFrame = false 1186 sc.writingFrameAsync = false 1187 1188 wr := res.wr 1189 1190 if writeEndsStream(wr.write) { 1191 st := wr.stream 1192 if st == nil { 1193 panic("internal error: expecting non-nil stream") 1194 } 1195 switch st.state { 1196 case stateOpen: 1197 // Here we would go to stateHalfClosedLocal in 1198 // theory, but since our handler is done and 1199 // the net/http package provides no mechanism 1200 // for closing a ResponseWriter while still 1201 // reading data (see possible TODO at top of 1202 // this file), we go into closed state here 1203 // anyway, after telling the peer we're 1204 // hanging up on them. We'll transition to 1205 // stateClosed after the RST_STREAM frame is 1206 // written. 1207 st.state = stateHalfClosedLocal 1208 // Section 8.1: a server MAY request that the client abort 1209 // transmission of a request without error by sending a 1210 // RST_STREAM with an error code of NO_ERROR after sending 1211 // a complete response. 1212 sc.resetStream(streamError(st.id, ErrCodeNo)) 1213 case stateHalfClosedRemote: 1214 sc.closeStream(st, errHandlerComplete) 1215 } 1216 } else { 1217 switch v := wr.write.(type) { 1218 case StreamError: 1219 // st may be unknown if the RST_STREAM was generated to reject bad input. 1220 if st, ok := sc.streams[v.StreamID]; ok { 1221 sc.closeStream(st, v) 1222 } 1223 case handlerPanicRST: 1224 sc.closeStream(wr.stream, errHandlerPanicked) 1225 } 1226 } 1227 1228 // Reply (if requested) to unblock the ServeHTTP goroutine. 1229 wr.replyToWriter(res.err) 1230 1231 sc.scheduleFrameWrite() 1232 } 1233 1234 // scheduleFrameWrite tickles the frame writing scheduler. 1235 // 1236 // If a frame is already being written, nothing happens. This will be called again 1237 // when the frame is done being written. 1238 // 1239 // If a frame isn't being written and we need to send one, the best frame 1240 // to send is selected by writeSched. 1241 // 1242 // If a frame isn't being written and there's nothing else to send, we 1243 // flush the write buffer. 1244 func (sc *serverConn) scheduleFrameWrite() { 1245 sc.serveG.check() 1246 if sc.writingFrame || sc.inFrameScheduleLoop { 1247 return 1248 } 1249 sc.inFrameScheduleLoop = true 1250 for !sc.writingFrameAsync { 1251 if sc.needToSendGoAway { 1252 sc.needToSendGoAway = false 1253 sc.startFrameWrite(FrameWriteRequest{ 1254 write: &writeGoAway{ 1255 maxStreamID: sc.maxClientStreamID, 1256 code: sc.goAwayCode, 1257 }, 1258 }) 1259 continue 1260 } 1261 if sc.needToSendSettingsAck { 1262 sc.needToSendSettingsAck = false 1263 sc.startFrameWrite(FrameWriteRequest{write: writeSettingsAck{}}) 1264 continue 1265 } 1266 if !sc.inGoAway || sc.goAwayCode == ErrCodeNo { 1267 if wr, ok := sc.writeSched.Pop(); ok { 1268 if wr.isControl() { 1269 sc.queuedControlFrames-- 1270 } 1271 sc.startFrameWrite(wr) 1272 continue 1273 } 1274 } 1275 if sc.needsFrameFlush { 1276 sc.startFrameWrite(FrameWriteRequest{write: flushFrameWriter{}}) 1277 sc.needsFrameFlush = false // after startFrameWrite, since it sets this true 1278 continue 1279 } 1280 break 1281 } 1282 sc.inFrameScheduleLoop = false 1283 } 1284 1285 // startGracefulShutdown gracefully shuts down a connection. This 1286 // sends GOAWAY with ErrCodeNo to tell the client we're gracefully 1287 // shutting down. The connection isn't closed until all current 1288 // streams are done. 1289 // 1290 // startGracefulShutdown returns immediately; it does not wait until 1291 // the connection has shut down. 1292 func (sc *serverConn) startGracefulShutdown() { 1293 sc.serveG.checkNotOn() // NOT 1294 sc.shutdownOnce.Do(func() { sc.sendServeMsg(gracefulShutdownMsg) }) 1295 } 1296 1297 // After sending GOAWAY, the connection will close after goAwayTimeout. 1298 // If we close the connection immediately after sending GOAWAY, there may 1299 // be unsent data in our kernel receive buffer, which will cause the kernel 1300 // to send a TCP RST on close() instead of a FIN. This RST will abort the 1301 // connection immediately, whether or not the client had received the GOAWAY. 1302 // 1303 // Ideally we should delay for at least 1 RTT + epsilon so the client has 1304 // a chance to read the GOAWAY and stop sending messages. Measuring RTT 1305 // is hard, so we approximate with 1 second. See golang.org/issue/18701. 1306 // 1307 // This is a var so it can be shorter in tests, where all requests uses the 1308 // loopback interface making the expected RTT very small. 1309 // 1310 // TODO: configurable? 1311 var goAwayTimeout = 1 * time.Second 1312 1313 func (sc *serverConn) startGracefulShutdownInternal() { 1314 sc.goAway(ErrCodeNo) 1315 } 1316 1317 func (sc *serverConn) goAway(code ErrCode) { 1318 sc.serveG.check() 1319 if sc.inGoAway { 1320 return 1321 } 1322 sc.inGoAway = true 1323 sc.needToSendGoAway = true 1324 sc.goAwayCode = code 1325 sc.scheduleFrameWrite() 1326 } 1327 1328 func (sc *serverConn) shutDownIn(d time.Duration) { 1329 sc.serveG.check() 1330 sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer) 1331 } 1332 1333 func (sc *serverConn) resetStream(se StreamError) { 1334 sc.serveG.check() 1335 sc.writeFrame(FrameWriteRequest{write: se}) 1336 if st, ok := sc.streams[se.StreamID]; ok { 1337 st.resetQueued = true 1338 } 1339 } 1340 1341 // processFrameFromReader processes the serve loop's read from readFrameCh from the 1342 // frame-reading goroutine. 1343 // processFrameFromReader returns whether the connection should be kept open. 1344 func (sc *serverConn) processFrameFromReader(res readFrameResult) bool { 1345 sc.serveG.check() 1346 err := res.err 1347 if err != nil { 1348 if err == ErrFrameTooLarge { 1349 sc.goAway(ErrCodeFrameSize) 1350 return true // goAway will close the loop 1351 } 1352 clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) 1353 if clientGone { 1354 // TODO: could we also get into this state if 1355 // the peer does a half close 1356 // (e.g. CloseWrite) because they're done 1357 // sending frames but they're still wanting 1358 // our open replies? Investigate. 1359 // TODO: add CloseWrite to crypto/tls.Conn first 1360 // so we have a way to test this? I suppose 1361 // just for testing we could have a non-TLS mode. 1362 return false 1363 } 1364 } else { 1365 f := res.f 1366 if VerboseLogs { 1367 sc.vlogf("http2: server read frame %v", summarizeFrame(f)) 1368 } 1369 err = sc.processFrame(f) 1370 if err == nil { 1371 return true 1372 } 1373 } 1374 1375 switch ev := err.(type) { 1376 case StreamError: 1377 sc.resetStream(ev) 1378 return true 1379 case goAwayFlowError: 1380 sc.goAway(ErrCodeFlowControl) 1381 return true 1382 case ConnectionError: 1383 sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev) 1384 sc.goAway(ErrCode(ev)) 1385 return true // goAway will handle shutdown 1386 default: 1387 if res.err != nil { 1388 sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 1389 } else { 1390 sc.logf("http2: server closing client connection: %v", err) 1391 } 1392 return false 1393 } 1394 } 1395 1396 func (sc *serverConn) processFrame(f Frame) error { 1397 sc.serveG.check() 1398 1399 // First frame received must be SETTINGS. 1400 if !sc.sawFirstSettings { 1401 if _, ok := f.(*SettingsFrame); !ok { 1402 return ConnectionError(ErrCodeProtocol) 1403 } 1404 sc.sawFirstSettings = true 1405 } 1406 1407 switch f := f.(type) { 1408 case *SettingsFrame: 1409 return sc.processSettings(f) 1410 case *MetaHeadersFrame: 1411 return sc.processHeaders(f) 1412 case *WindowUpdateFrame: 1413 return sc.processWindowUpdate(f) 1414 case *PingFrame: 1415 return sc.processPing(f) 1416 case *DataFrame: 1417 return sc.processData(f) 1418 case *RSTStreamFrame: 1419 return sc.processResetStream(f) 1420 case *PriorityFrame: 1421 return sc.processPriority(f) 1422 case *GoAwayFrame: 1423 return sc.processGoAway(f) 1424 case *MetaPushPromiseFrame: 1425 // A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE 1426 // frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 1427 return ConnectionError(ErrCodeProtocol) 1428 default: 1429 sc.vlogf("http2: server ignoring frame: %v", f.Header()) 1430 return nil 1431 } 1432 } 1433 1434 func (sc *serverConn) processPing(f *PingFrame) error { 1435 sc.serveG.check() 1436 if f.IsAck() { 1437 // 6.7 PING: " An endpoint MUST NOT respond to PING frames 1438 // containing this flag." 1439 return nil 1440 } 1441 if f.StreamID != 0 { 1442 // "PING frames are not associated with any individual 1443 // stream. If a PING frame is received with a stream 1444 // identifier field value other than 0x0, the recipient MUST 1445 // respond with a connection error (Section 5.4.1) of type 1446 // PROTOCOL_ERROR." 1447 return ConnectionError(ErrCodeProtocol) 1448 } 1449 if sc.inGoAway && sc.goAwayCode != ErrCodeNo { 1450 return nil 1451 } 1452 sc.writeFrame(FrameWriteRequest{write: writePingAck{f}}) 1453 return nil 1454 } 1455 1456 func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error { 1457 sc.serveG.check() 1458 switch { 1459 case f.StreamID != 0: // stream-level flow control 1460 state, st := sc.state(f.StreamID) 1461 if state == stateIdle { 1462 // Section 5.1: "Receiving any frame other than HEADERS 1463 // or PRIORITY on a stream in this state MUST be 1464 // treated as a connection error (Section 5.4.1) of 1465 // type PROTOCOL_ERROR." 1466 return ConnectionError(ErrCodeProtocol) 1467 } 1468 if st == nil { 1469 // "WINDOW_UPDATE can be sent by a peer that has sent a 1470 // frame bearing the END_STREAM flag. This means that a 1471 // receiver could receive a WINDOW_UPDATE frame on a "half 1472 // closed (remote)" or "closed" stream. A receiver MUST 1473 // NOT treat this as an error, see Section 5.1." 1474 return nil 1475 } 1476 if !st.flow.add(int32(f.Increment)) { 1477 return streamError(f.StreamID, ErrCodeFlowControl) 1478 } 1479 default: // connection-level flow control 1480 if !sc.flow.add(int32(f.Increment)) { 1481 return goAwayFlowError{} 1482 } 1483 } 1484 sc.scheduleFrameWrite() 1485 return nil 1486 } 1487 1488 func (sc *serverConn) processResetStream(f *RSTStreamFrame) error { 1489 sc.serveG.check() 1490 1491 state, st := sc.state(f.StreamID) 1492 if state == stateIdle { 1493 // 6.4 "RST_STREAM frames MUST NOT be sent for a 1494 // stream in the "idle" state. If a RST_STREAM frame 1495 // identifying an idle stream is received, the 1496 // recipient MUST treat this as a connection error 1497 // (Section 5.4.1) of type PROTOCOL_ERROR. 1498 return ConnectionError(ErrCodeProtocol) 1499 } 1500 if st != nil { 1501 st.cancelCtx() 1502 sc.closeStream(st, streamError(f.StreamID, f.ErrCode)) 1503 } 1504 return nil 1505 } 1506 1507 func (sc *serverConn) closeStream(st *stream, err error) { 1508 sc.serveG.check() 1509 if st.state == stateIdle || st.state == stateClosed { 1510 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 1511 } 1512 st.state = stateClosed 1513 if st.writeDeadline != nil { 1514 st.writeDeadline.Stop() 1515 } 1516 if st.isPushed() { 1517 sc.curPushedStreams-- 1518 } else { 1519 sc.curClientStreams-- 1520 } 1521 delete(sc.streams, st.id) 1522 if len(sc.streams) == 0 { 1523 sc.setConnState(http.StateIdle) 1524 if sc.srv.IdleTimeout != 0 { 1525 sc.idleTimer.Reset(sc.srv.IdleTimeout) 1526 } 1527 if h1ServerKeepAlivesDisabled(sc.hs) { 1528 sc.startGracefulShutdownInternal() 1529 } 1530 } 1531 if p := st.body; p != nil { 1532 // Return any buffered unread bytes worth of conn-level flow control. 1533 // See golang.org/issue/16481 1534 sc.sendWindowUpdate(nil, p.Len()) 1535 1536 p.CloseWithError(err) 1537 } 1538 st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc 1539 sc.writeSched.CloseStream(st.id) 1540 } 1541 1542 func (sc *serverConn) processSettings(f *SettingsFrame) error { 1543 sc.serveG.check() 1544 if f.IsAck() { 1545 sc.unackedSettings-- 1546 if sc.unackedSettings < 0 { 1547 // Why is the peer ACKing settings we never sent? 1548 // The spec doesn't mention this case, but 1549 // hang up on them anyway. 1550 return ConnectionError(ErrCodeProtocol) 1551 } 1552 return nil 1553 } 1554 if f.NumSettings() > 100 || f.HasDuplicates() { 1555 // This isn't actually in the spec, but hang up on 1556 // suspiciously large settings frames or those with 1557 // duplicate entries. 1558 return ConnectionError(ErrCodeProtocol) 1559 } 1560 if err := f.ForeachSetting(sc.processSetting); err != nil { 1561 return err 1562 } 1563 // TODO: judging by RFC 7540, Section 6.5.3 each SETTINGS frame should be 1564 // acknowledged individually, even if multiple are received before the ACK. 1565 sc.needToSendSettingsAck = true 1566 sc.scheduleFrameWrite() 1567 return nil 1568 } 1569 1570 func (sc *serverConn) processSetting(s Setting) error { 1571 sc.serveG.check() 1572 if err := s.Valid(); err != nil { 1573 return err 1574 } 1575 if VerboseLogs { 1576 sc.vlogf("http2: server processing setting %v", s) 1577 } 1578 switch s.ID { 1579 case SettingHeaderTableSize: 1580 sc.headerTableSize = s.Val 1581 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 1582 case SettingEnablePush: 1583 sc.pushEnabled = s.Val != 0 1584 case SettingMaxConcurrentStreams: 1585 sc.clientMaxStreams = s.Val 1586 case SettingInitialWindowSize: 1587 return sc.processSettingInitialWindowSize(s.Val) 1588 case SettingMaxFrameSize: 1589 sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31 1590 case SettingMaxHeaderListSize: 1591 sc.peerMaxHeaderListSize = s.Val 1592 default: 1593 // Unknown setting: "An endpoint that receives a SETTINGS 1594 // frame with any unknown or unsupported identifier MUST 1595 // ignore that setting." 1596 if VerboseLogs { 1597 sc.vlogf("http2: server ignoring unknown setting %v", s) 1598 } 1599 } 1600 return nil 1601 } 1602 1603 func (sc *serverConn) processSettingInitialWindowSize(val uint32) error { 1604 sc.serveG.check() 1605 // Note: val already validated to be within range by 1606 // processSetting's Valid call. 1607 1608 // "A SETTINGS frame can alter the initial flow control window 1609 // size for all current streams. When the value of 1610 // SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST 1611 // adjust the size of all stream flow control windows that it 1612 // maintains by the difference between the new value and the 1613 // old value." 1614 old := sc.initialStreamSendWindowSize 1615 sc.initialStreamSendWindowSize = int32(val) 1616 growth := int32(val) - old // may be negative 1617 for _, st := range sc.streams { 1618 if !st.flow.add(growth) { 1619 // 6.9.2 Initial Flow Control Window Size 1620 // "An endpoint MUST treat a change to 1621 // SETTINGS_INITIAL_WINDOW_SIZE that causes any flow 1622 // control window to exceed the maximum size as a 1623 // connection error (Section 5.4.1) of type 1624 // FLOW_CONTROL_ERROR." 1625 return ConnectionError(ErrCodeFlowControl) 1626 } 1627 } 1628 return nil 1629 } 1630 1631 func (sc *serverConn) processData(f *DataFrame) error { 1632 sc.serveG.check() 1633 if sc.inGoAway && sc.goAwayCode != ErrCodeNo { 1634 return nil 1635 } 1636 data := f.Data() 1637 1638 // "If a DATA frame is received whose stream is not in "open" 1639 // or "half closed (local)" state, the recipient MUST respond 1640 // with a stream error (Section 5.4.2) of type STREAM_CLOSED." 1641 id := f.Header().StreamID 1642 state, st := sc.state(id) 1643 if id == 0 || state == stateIdle { 1644 // Section 5.1: "Receiving any frame other than HEADERS 1645 // or PRIORITY on a stream in this state MUST be 1646 // treated as a connection error (Section 5.4.1) of 1647 // type PROTOCOL_ERROR." 1648 return ConnectionError(ErrCodeProtocol) 1649 } 1650 if st == nil || state != stateOpen || st.gotTrailerHeader || st.resetQueued { 1651 // This includes sending a RST_STREAM if the stream is 1652 // in stateHalfClosedLocal (which currently means that 1653 // the http.Handler returned, so it's done reading & 1654 // done writing). Try to stop the client from sending 1655 // more DATA. 1656 1657 // But still enforce their connection-level flow control, 1658 // and return any flow control bytes since we're not going 1659 // to consume them. 1660 if sc.inflow.available() < int32(f.Length) { 1661 return streamError(id, ErrCodeFlowControl) 1662 } 1663 // Deduct the flow control from inflow, since we're 1664 // going to immediately add it back in 1665 // sendWindowUpdate, which also schedules sending the 1666 // frames. 1667 sc.inflow.take(int32(f.Length)) 1668 sc.sendWindowUpdate(nil, int(f.Length)) // conn-level 1669 1670 if st != nil && st.resetQueued { 1671 // Already have a stream error in flight. Don't send another. 1672 return nil 1673 } 1674 return streamError(id, ErrCodeStreamClosed) 1675 } 1676 if st.body == nil { 1677 panic("internal error: should have a body in this state") 1678 } 1679 1680 // Sender sending more than they'd declared? 1681 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 1682 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 1683 // RFC 7540, sec 8.1.2.6: A request or response is also malformed if the 1684 // value of a content-length header field does not equal the sum of the 1685 // DATA frame payload lengths that form the body. 1686 return streamError(id, ErrCodeProtocol) 1687 } 1688 if f.Length > 0 { 1689 // Check whether the client has flow control quota. 1690 if st.inflow.available() < int32(f.Length) { 1691 return streamError(id, ErrCodeFlowControl) 1692 } 1693 st.inflow.take(int32(f.Length)) 1694 1695 if len(data) > 0 { 1696 wrote, err := st.body.Write(data) 1697 if err != nil { 1698 sc.sendWindowUpdate(nil, int(f.Length)-wrote) 1699 return streamError(id, ErrCodeStreamClosed) 1700 } 1701 if wrote != len(data) { 1702 panic("internal error: bad Writer") 1703 } 1704 st.bodyBytes += int64(len(data)) 1705 } 1706 1707 // Return any padded flow control now, since we won't 1708 // refund it later on body reads. 1709 if pad := int32(f.Length) - int32(len(data)); pad > 0 { 1710 sc.sendWindowUpdate32(nil, pad) 1711 sc.sendWindowUpdate32(st, pad) 1712 } 1713 } 1714 if f.StreamEnded() { 1715 st.endStream() 1716 } 1717 return nil 1718 } 1719 1720 func (sc *serverConn) processGoAway(f *GoAwayFrame) error { 1721 sc.serveG.check() 1722 if f.ErrCode != ErrCodeNo { 1723 sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f) 1724 } else { 1725 sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f) 1726 } 1727 sc.startGracefulShutdownInternal() 1728 // http://tools.ietf.org/html/rfc7540#section-6.8 1729 // We should not create any new streams, which means we should disable push. 1730 sc.pushEnabled = false 1731 return nil 1732 } 1733 1734 // isPushed reports whether the stream is server-initiated. 1735 func (st *stream) isPushed() bool { 1736 return st.id%2 == 0 1737 } 1738 1739 // endStream closes a Request.Body's pipe. It is called when a DATA 1740 // frame says a request body is over (or after trailers). 1741 func (st *stream) endStream() { 1742 sc := st.sc 1743 sc.serveG.check() 1744 1745 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 1746 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 1747 st.declBodyBytes, st.bodyBytes)) 1748 } else { 1749 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest) 1750 st.body.CloseWithError(io.EOF) 1751 } 1752 st.state = stateHalfClosedRemote 1753 } 1754 1755 // copyTrailersToHandlerRequest is run in the Handler's goroutine in 1756 // its Request.Body.Read just before it gets io.EOF. 1757 func (st *stream) copyTrailersToHandlerRequest() { 1758 for k, vv := range st.trailer { 1759 if _, ok := st.reqTrailer[k]; ok { 1760 // Only copy it over it was pre-declared. 1761 st.reqTrailer[k] = vv 1762 } 1763 } 1764 } 1765 1766 // onWriteTimeout is run on its own goroutine (from time.AfterFunc) 1767 // when the stream's WriteTimeout has fired. 1768 func (st *stream) onWriteTimeout() { 1769 st.sc.writeFrameFromHandler(FrameWriteRequest{write: streamError(st.id, ErrCodeInternal)}) 1770 } 1771 1772 func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error { 1773 sc.serveG.check() 1774 id := f.StreamID 1775 if sc.inGoAway { 1776 // Ignore. 1777 return nil 1778 } 1779 // http://tools.ietf.org/html/rfc7540#section-5.1.1 1780 // Streams initiated by a client MUST use odd-numbered stream 1781 // identifiers. [...] An endpoint that receives an unexpected 1782 // stream identifier MUST respond with a connection error 1783 // (Section 5.4.1) of type PROTOCOL_ERROR. 1784 if id%2 != 1 { 1785 return ConnectionError(ErrCodeProtocol) 1786 } 1787 // A HEADERS frame can be used to create a new stream or 1788 // send a trailer for an open one. If we already have a stream 1789 // open, let it process its own HEADERS frame (trailers at this 1790 // point, if it's valid). 1791 if st := sc.streams[f.StreamID]; st != nil { 1792 if st.resetQueued { 1793 // We're sending RST_STREAM to close the stream, so don't bother 1794 // processing this frame. 1795 return nil 1796 } 1797 // RFC 7540, sec 5.1: If an endpoint receives additional frames, other than 1798 // WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in 1799 // this state, it MUST respond with a stream error (Section 5.4.2) of 1800 // type STREAM_CLOSED. 1801 if st.state == stateHalfClosedRemote { 1802 return streamError(id, ErrCodeStreamClosed) 1803 } 1804 return st.processTrailerHeaders(f) 1805 } 1806 1807 // [...] The identifier of a newly established stream MUST be 1808 // numerically greater than all streams that the initiating 1809 // endpoint has opened or reserved. [...] An endpoint that 1810 // receives an unexpected stream identifier MUST respond with 1811 // a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 1812 if id <= sc.maxClientStreamID { 1813 return ConnectionError(ErrCodeProtocol) 1814 } 1815 sc.maxClientStreamID = id 1816 1817 if sc.idleTimer != nil { 1818 sc.idleTimer.Stop() 1819 } 1820 1821 // http://tools.ietf.org/html/rfc7540#section-5.1.2 1822 // [...] Endpoints MUST NOT exceed the limit set by their peer. An 1823 // endpoint that receives a HEADERS frame that causes their 1824 // advertised concurrent stream limit to be exceeded MUST treat 1825 // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR 1826 // or REFUSED_STREAM. 1827 if sc.curClientStreams+1 > sc.advMaxStreams { 1828 if sc.unackedSettings == 0 { 1829 // They should know better. 1830 return streamError(id, ErrCodeProtocol) 1831 } 1832 // Assume it's a network race, where they just haven't 1833 // received our last SETTINGS update. But actually 1834 // this can't happen yet, because we don't yet provide 1835 // a way for users to adjust server parameters at 1836 // runtime. 1837 return streamError(id, ErrCodeRefusedStream) 1838 } 1839 1840 initialState := stateOpen 1841 if f.StreamEnded() { 1842 initialState = stateHalfClosedRemote 1843 } 1844 st := sc.newStream(id, 0, initialState) 1845 1846 if f.HasPriority() { 1847 if err := checkPriority(f.StreamID, f.Priority); err != nil { 1848 return err 1849 } 1850 sc.writeSched.AdjustStream(st.id, f.Priority) 1851 } 1852 1853 rw, req, err := sc.newWriterAndRequest(st, f) 1854 if err != nil { 1855 return err 1856 } 1857 st.reqTrailer = req.Trailer 1858 if st.reqTrailer != nil { 1859 st.trailer = make(http.Header) 1860 } 1861 st.body = req.Body.(*requestBody).pipe // may be nil 1862 st.declBodyBytes = req.ContentLength 1863 1864 handler := sc.handler.ServeHTTP 1865 if f.Truncated { 1866 // Their header list was too long. Send a 431 error. 1867 handler = handleHeaderListTooLong 1868 } else if err := checkValidHTTP2RequestHeaders(req.Header); err != nil { 1869 handler = new400Handler(err) 1870 } 1871 1872 // The net/http package sets the read deadline from the 1873 // http.Server.ReadTimeout during the TLS handshake, but then 1874 // passes the connection off to us with the deadline already 1875 // set. Disarm it here after the request headers are read, 1876 // similar to how the http1 server works. Here it's 1877 // technically more like the http1 Server's ReadHeaderTimeout 1878 // (in Go 1.8), though. That's a more sane option anyway. 1879 if sc.hs.ReadTimeout != 0 { 1880 sc.conn.SetReadDeadline(time.Time{}) 1881 } 1882 1883 go sc.runHandler(rw, req, handler) 1884 return nil 1885 } 1886 1887 func (st *stream) processTrailerHeaders(f *MetaHeadersFrame) error { 1888 sc := st.sc 1889 sc.serveG.check() 1890 if st.gotTrailerHeader { 1891 return ConnectionError(ErrCodeProtocol) 1892 } 1893 st.gotTrailerHeader = true 1894 if !f.StreamEnded() { 1895 return streamError(st.id, ErrCodeProtocol) 1896 } 1897 1898 if len(f.PseudoFields()) > 0 { 1899 return streamError(st.id, ErrCodeProtocol) 1900 } 1901 if st.trailer != nil { 1902 for _, hf := range f.RegularFields() { 1903 key := sc.canonicalHeader(hf.Name) 1904 if !httpguts.ValidTrailerHeader(key) { 1905 // TODO: send more details to the peer somehow. But http2 has 1906 // no way to send debug data at a stream level. Discuss with 1907 // HTTP folk. 1908 return streamError(st.id, ErrCodeProtocol) 1909 } 1910 st.trailer[key] = append(st.trailer[key], hf.Value) 1911 } 1912 } 1913 st.endStream() 1914 return nil 1915 } 1916 1917 func checkPriority(streamID uint32, p PriorityParam) error { 1918 if streamID == p.StreamDep { 1919 // Section 5.3.1: "A stream cannot depend on itself. An endpoint MUST treat 1920 // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR." 1921 // Section 5.3.3 says that a stream can depend on one of its dependencies, 1922 // so it's only self-dependencies that are forbidden. 1923 return streamError(streamID, ErrCodeProtocol) 1924 } 1925 return nil 1926 } 1927 1928 func (sc *serverConn) processPriority(f *PriorityFrame) error { 1929 if sc.inGoAway { 1930 return nil 1931 } 1932 if err := checkPriority(f.StreamID, f.PriorityParam); err != nil { 1933 return err 1934 } 1935 sc.writeSched.AdjustStream(f.StreamID, f.PriorityParam) 1936 return nil 1937 } 1938 1939 func (sc *serverConn) newStream(id, pusherID uint32, state streamState) *stream { 1940 sc.serveG.check() 1941 if id == 0 { 1942 panic("internal error: cannot create stream with id 0") 1943 } 1944 1945 ctx, cancelCtx := context.WithCancel(sc.baseCtx) 1946 st := &stream{ 1947 sc: sc, 1948 id: id, 1949 state: state, 1950 ctx: ctx, 1951 cancelCtx: cancelCtx, 1952 } 1953 st.cw.Init() 1954 st.flow.conn = &sc.flow // link to conn-level counter 1955 st.flow.add(sc.initialStreamSendWindowSize) 1956 st.inflow.conn = &sc.inflow // link to conn-level counter 1957 st.inflow.add(sc.srv.initialStreamRecvWindowSize()) 1958 if sc.hs.WriteTimeout != 0 { 1959 st.writeDeadline = time.AfterFunc(sc.hs.WriteTimeout, st.onWriteTimeout) 1960 } 1961 1962 sc.streams[id] = st 1963 sc.writeSched.OpenStream(st.id, OpenStreamOptions{PusherID: pusherID}) 1964 if st.isPushed() { 1965 sc.curPushedStreams++ 1966 } else { 1967 sc.curClientStreams++ 1968 } 1969 if sc.curOpenStreams() == 1 { 1970 sc.setConnState(http.StateActive) 1971 } 1972 1973 return st 1974 } 1975 1976 func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*responseWriter, *http.Request, error) { 1977 sc.serveG.check() 1978 1979 rp := requestParam{ 1980 method: f.PseudoValue("method"), 1981 scheme: f.PseudoValue("scheme"), 1982 authority: f.PseudoValue("authority"), 1983 path: f.PseudoValue("path"), 1984 } 1985 1986 isConnect := rp.method == "CONNECT" 1987 if isConnect { 1988 if rp.path != "" || rp.scheme != "" || rp.authority == "" { 1989 return nil, nil, streamError(f.StreamID, ErrCodeProtocol) 1990 } 1991 } else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") { 1992 // See 8.1.2.6 Malformed Requests and Responses: 1993 // 1994 // Malformed requests or responses that are detected 1995 // MUST be treated as a stream error (Section 5.4.2) 1996 // of type PROTOCOL_ERROR." 1997 // 1998 // 8.1.2.3 Request Pseudo-Header Fields 1999 // "All HTTP/2 requests MUST include exactly one valid 2000 // value for the :method, :scheme, and :path 2001 // pseudo-header fields" 2002 return nil, nil, streamError(f.StreamID, ErrCodeProtocol) 2003 } 2004 2005 bodyOpen := !f.StreamEnded() 2006 if rp.method == "HEAD" && bodyOpen { 2007 // HEAD requests can't have bodies 2008 return nil, nil, streamError(f.StreamID, ErrCodeProtocol) 2009 } 2010 2011 rp.header = make(http.Header) 2012 for _, hf := range f.RegularFields() { 2013 rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value) 2014 } 2015 if rp.authority == "" { 2016 rp.authority = rp.header.Get("Host") 2017 } 2018 2019 rw, req, err := sc.newWriterAndRequestNoBody(st, rp) 2020 if err != nil { 2021 return nil, nil, err 2022 } 2023 if bodyOpen { 2024 if vv, ok := rp.header["Content-Length"]; ok { 2025 if cl, err := strconv.ParseUint(vv[0], 10, 63); err == nil { 2026 req.ContentLength = int64(cl) 2027 } else { 2028 req.ContentLength = 0 2029 } 2030 } else { 2031 req.ContentLength = -1 2032 } 2033 req.Body.(*requestBody).pipe = &pipe{ 2034 b: &dataBuffer{expected: req.ContentLength}, 2035 } 2036 } 2037 return rw, req, nil 2038 } 2039 2040 type requestParam struct { 2041 method string 2042 scheme, authority, path string 2043 header http.Header 2044 } 2045 2046 func (sc *serverConn) newWriterAndRequestNoBody(st *stream, rp requestParam) (*responseWriter, *http.Request, error) { 2047 sc.serveG.check() 2048 2049 var tlsState *tls.ConnectionState // nil if not scheme https 2050 if rp.scheme == "https" { 2051 tlsState = sc.tlsState 2052 } 2053 2054 needsContinue := rp.header.Get("Expect") == "100-continue" 2055 if needsContinue { 2056 rp.header.Del("Expect") 2057 } 2058 // Merge Cookie headers into one "; "-delimited value. 2059 if cookies := rp.header["Cookie"]; len(cookies) > 1 { 2060 rp.header.Set("Cookie", strings.Join(cookies, "; ")) 2061 } 2062 2063 // Setup Trailers 2064 var trailer http.Header 2065 for _, v := range rp.header["Trailer"] { 2066 for _, key := range strings.Split(v, ",") { 2067 key = http.CanonicalHeaderKey(textproto.TrimString(key)) 2068 switch key { 2069 case "Transfer-Encoding", "Trailer", "Content-Length": 2070 // Bogus. (copy of http1 rules) 2071 // Ignore. 2072 default: 2073 if trailer == nil { 2074 trailer = make(http.Header) 2075 } 2076 trailer[key] = nil 2077 } 2078 } 2079 } 2080 delete(rp.header, "Trailer") 2081 2082 var url_ *url.URL 2083 var requestURI string 2084 if rp.method == "CONNECT" { 2085 url_ = &url.URL{Host: rp.authority} 2086 requestURI = rp.authority // mimic HTTP/1 server behavior 2087 } else { 2088 var err error 2089 url_, err = url.ParseRequestURI(rp.path) 2090 if err != nil { 2091 return nil, nil, streamError(st.id, ErrCodeProtocol) 2092 } 2093 requestURI = rp.path 2094 } 2095 2096 body := &requestBody{ 2097 conn: sc, 2098 stream: st, 2099 needsContinue: needsContinue, 2100 } 2101 req := &http.Request{ 2102 Method: rp.method, 2103 URL: url_, 2104 RemoteAddr: sc.remoteAddrStr, 2105 Header: rp.header, 2106 RequestURI: requestURI, 2107 Proto: "HTTP/2.0", 2108 ProtoMajor: 2, 2109 ProtoMinor: 0, 2110 TLS: tlsState, 2111 Host: rp.authority, 2112 Body: body, 2113 Trailer: trailer, 2114 } 2115 req = req.WithContext(st.ctx) 2116 2117 rws := responseWriterStatePool.Get().(*responseWriterState) 2118 bwSave := rws.bw 2119 *rws = responseWriterState{} // zero all the fields 2120 rws.conn = sc 2121 rws.bw = bwSave 2122 rws.bw.Reset(chunkWriter{rws}) 2123 rws.stream = st 2124 rws.req = req 2125 rws.body = body 2126 2127 rw := &responseWriter{rws: rws} 2128 return rw, req, nil 2129 } 2130 2131 // Run on its own goroutine. 2132 func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) { 2133 didPanic := true 2134 defer func() { 2135 rw.rws.stream.cancelCtx() 2136 if didPanic { 2137 e := recover() 2138 sc.writeFrameFromHandler(FrameWriteRequest{ 2139 write: handlerPanicRST{rw.rws.stream.id}, 2140 stream: rw.rws.stream, 2141 }) 2142 // Same as net/http: 2143 if e != nil && e != http.ErrAbortHandler { 2144 const size = 64 << 10 2145 buf := make([]byte, size) 2146 buf = buf[:runtime.Stack(buf, false)] 2147 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf) 2148 } 2149 return 2150 } 2151 rw.handlerDone() 2152 }() 2153 handler(rw, req) 2154 didPanic = false 2155 } 2156 2157 func handleHeaderListTooLong(w http.ResponseWriter, r *http.Request) { 2158 // 10.5.1 Limits on Header Block Size: 2159 // .. "A server that receives a larger header block than it is 2160 // willing to handle can send an HTTP 431 (Request Header Fields Too 2161 // Large) status code" 2162 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 2163 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 2164 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 2165 } 2166 2167 // called from handler goroutines. 2168 // h may be nil. 2169 func (sc *serverConn) writeHeaders(st *stream, headerData *writeResHeaders) error { 2170 sc.serveG.checkNotOn() // NOT on 2171 var errc chan error 2172 if headerData.h != nil { 2173 // If there's a header map (which we don't own), so we have to block on 2174 // waiting for this frame to be written, so an http.Flush mid-handler 2175 // writes out the correct value of keys, before a handler later potentially 2176 // mutates it. 2177 errc = errChanPool.Get().(chan error) 2178 } 2179 if err := sc.writeFrameFromHandler(FrameWriteRequest{ 2180 write: headerData, 2181 stream: st, 2182 done: errc, 2183 }); err != nil { 2184 return err 2185 } 2186 if errc != nil { 2187 select { 2188 case err := <-errc: 2189 errChanPool.Put(errc) 2190 return err 2191 case <-sc.doneServing: 2192 return errClientDisconnected 2193 case <-st.cw: 2194 return errStreamClosed 2195 } 2196 } 2197 return nil 2198 } 2199 2200 // called from handler goroutines. 2201 func (sc *serverConn) write100ContinueHeaders(st *stream) { 2202 sc.writeFrameFromHandler(FrameWriteRequest{ 2203 write: write100ContinueHeadersFrame{st.id}, 2204 stream: st, 2205 }) 2206 } 2207 2208 // A bodyReadMsg tells the server loop that the http.Handler read n 2209 // bytes of the DATA from the client on the given stream. 2210 type bodyReadMsg struct { 2211 st *stream 2212 n int 2213 } 2214 2215 // called from handler goroutines. 2216 // Notes that the handler for the given stream ID read n bytes of its body 2217 // and schedules flow control tokens to be sent. 2218 func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int, err error) { 2219 sc.serveG.checkNotOn() // NOT on 2220 if n > 0 { 2221 select { 2222 case sc.bodyReadCh <- bodyReadMsg{st, n}: 2223 case <-sc.doneServing: 2224 } 2225 } 2226 } 2227 2228 func (sc *serverConn) noteBodyRead(st *stream, n int) { 2229 sc.serveG.check() 2230 sc.sendWindowUpdate(nil, n) // conn-level 2231 if st.state != stateHalfClosedRemote && st.state != stateClosed { 2232 // Don't send this WINDOW_UPDATE if the stream is closed 2233 // remotely. 2234 sc.sendWindowUpdate(st, n) 2235 } 2236 } 2237 2238 // st may be nil for conn-level 2239 func (sc *serverConn) sendWindowUpdate(st *stream, n int) { 2240 sc.serveG.check() 2241 // "The legal range for the increment to the flow control 2242 // window is 1 to 2^31-1 (2,147,483,647) octets." 2243 // A Go Read call on 64-bit machines could in theory read 2244 // a larger Read than this. Very unlikely, but we handle it here 2245 // rather than elsewhere for now. 2246 const maxUint31 = 1<<31 - 1 2247 for n >= maxUint31 { 2248 sc.sendWindowUpdate32(st, maxUint31) 2249 n -= maxUint31 2250 } 2251 sc.sendWindowUpdate32(st, int32(n)) 2252 } 2253 2254 // st may be nil for conn-level 2255 func (sc *serverConn) sendWindowUpdate32(st *stream, n int32) { 2256 sc.serveG.check() 2257 if n == 0 { 2258 return 2259 } 2260 if n < 0 { 2261 panic("negative update") 2262 } 2263 var streamID uint32 2264 if st != nil { 2265 streamID = st.id 2266 } 2267 sc.writeFrame(FrameWriteRequest{ 2268 write: writeWindowUpdate{streamID: streamID, n: uint32(n)}, 2269 stream: st, 2270 }) 2271 var ok bool 2272 if st == nil { 2273 ok = sc.inflow.add(n) 2274 } else { 2275 ok = st.inflow.add(n) 2276 } 2277 if !ok { 2278 panic("internal error; sent too many window updates without decrements?") 2279 } 2280 } 2281 2282 // requestBody is the Handler's Request.Body type. 2283 // Read and Close may be called concurrently. 2284 type requestBody struct { 2285 _ incomparable 2286 stream *stream 2287 conn *serverConn 2288 closed bool // for use by Close only 2289 sawEOF bool // for use by Read only 2290 pipe *pipe // non-nil if we have a HTTP entity message body 2291 needsContinue bool // need to send a 100-continue 2292 } 2293 2294 func (b *requestBody) Close() error { 2295 if b.pipe != nil && !b.closed { 2296 b.pipe.BreakWithError(errClosedBody) 2297 } 2298 b.closed = true 2299 return nil 2300 } 2301 2302 func (b *requestBody) Read(p []byte) (n int, err error) { 2303 if b.needsContinue { 2304 b.needsContinue = false 2305 b.conn.write100ContinueHeaders(b.stream) 2306 } 2307 if b.pipe == nil || b.sawEOF { 2308 return 0, io.EOF 2309 } 2310 n, err = b.pipe.Read(p) 2311 if err == io.EOF { 2312 b.sawEOF = true 2313 } 2314 if b.conn == nil && inTests { 2315 return 2316 } 2317 b.conn.noteBodyReadFromHandler(b.stream, n, err) 2318 return 2319 } 2320 2321 // responseWriter is the http.ResponseWriter implementation. It's 2322 // intentionally small (1 pointer wide) to minimize garbage. The 2323 // responseWriterState pointer inside is zeroed at the end of a 2324 // request (in handlerDone) and calls on the responseWriter thereafter 2325 // simply crash (caller's mistake), but the much larger responseWriterState 2326 // and buffers are reused between multiple requests. 2327 type responseWriter struct { 2328 rws *responseWriterState 2329 } 2330 2331 // Optional http.ResponseWriter interfaces implemented. 2332 var ( 2333 _ http.CloseNotifier = (*responseWriter)(nil) 2334 _ http.Flusher = (*responseWriter)(nil) 2335 _ stringWriter = (*responseWriter)(nil) 2336 ) 2337 2338 type responseWriterState struct { 2339 // immutable within a request: 2340 stream *stream 2341 req *http.Request 2342 body *requestBody // to close at end of request, if DATA frames didn't 2343 conn *serverConn 2344 2345 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 2346 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 2347 2348 // mutated by http.Handler goroutine: 2349 handlerHeader http.Header // nil until called 2350 snapHeader http.Header // snapshot of handlerHeader at WriteHeader time 2351 trailers []string // set in writeChunk 2352 status int // status code passed to WriteHeader 2353 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 2354 sentHeader bool // have we sent the header frame? 2355 handlerDone bool // handler has finished 2356 dirty bool // a Write failed; don't reuse this responseWriterState 2357 2358 sentContentLen int64 // non-zero if handler set a Content-Length header 2359 wroteBytes int64 2360 2361 closeNotifierMu sync.Mutex // guards closeNotifierCh 2362 closeNotifierCh chan bool // nil until first used 2363 } 2364 2365 type chunkWriter struct{ rws *responseWriterState } 2366 2367 func (cw chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 2368 2369 func (rws *responseWriterState) hasTrailers() bool { return len(rws.trailers) > 0 } 2370 2371 func (rws *responseWriterState) hasNonemptyTrailers() bool { 2372 for _, trailer := range rws.trailers { 2373 if _, ok := rws.handlerHeader[trailer]; ok { 2374 return true 2375 } 2376 } 2377 return false 2378 } 2379 2380 // declareTrailer is called for each Trailer header when the 2381 // response header is written. It notes that a header will need to be 2382 // written in the trailers at the end of the response. 2383 func (rws *responseWriterState) declareTrailer(k string) { 2384 k = http.CanonicalHeaderKey(k) 2385 if !httpguts.ValidTrailerHeader(k) { 2386 // Forbidden by RFC 7230, section 4.1.2. 2387 rws.conn.logf("ignoring invalid trailer %q", k) 2388 return 2389 } 2390 if !strSliceContains(rws.trailers, k) { 2391 rws.trailers = append(rws.trailers, k) 2392 } 2393 } 2394 2395 // writeChunk writes chunks from the bufio.Writer. But because 2396 // bufio.Writer may bypass its chunking, sometimes p may be 2397 // arbitrarily large. 2398 // 2399 // writeChunk is also responsible (on the first chunk) for sending the 2400 // HEADER response. 2401 func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) { 2402 if !rws.wroteHeader { 2403 rws.writeHeader(200) 2404 } 2405 2406 isHeadResp := rws.req.Method == "HEAD" 2407 if !rws.sentHeader { 2408 rws.sentHeader = true 2409 var ctype, clen string 2410 if clen = rws.snapHeader.Get("Content-Length"); clen != "" { 2411 rws.snapHeader.Del("Content-Length") 2412 if cl, err := strconv.ParseUint(clen, 10, 63); err == nil { 2413 rws.sentContentLen = int64(cl) 2414 } else { 2415 clen = "" 2416 } 2417 } 2418 if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) { 2419 clen = strconv.Itoa(len(p)) 2420 } 2421 _, hasContentType := rws.snapHeader["Content-Type"] 2422 // If the Content-Encoding is non-blank, we shouldn't 2423 // sniff the body. See Issue golang.org/issue/31753. 2424 ce := rws.snapHeader.Get("Content-Encoding") 2425 hasCE := len(ce) > 0 2426 if !hasCE && !hasContentType && bodyAllowedForStatus(rws.status) && len(p) > 0 { 2427 ctype = http.DetectContentType(p) 2428 } 2429 var date string 2430 if _, ok := rws.snapHeader["Date"]; !ok { 2431 // TODO(bradfitz): be faster here, like net/http? measure. 2432 date = time.Now().UTC().Format(http.TimeFormat) 2433 } 2434 2435 for _, v := range rws.snapHeader["Trailer"] { 2436 foreachHeaderElement(v, rws.declareTrailer) 2437 } 2438 2439 // "Connection" headers aren't allowed in HTTP/2 (RFC 7540, 8.1.2.2), 2440 // but respect "Connection" == "close" to mean sending a GOAWAY and tearing 2441 // down the TCP connection when idle, like we do for HTTP/1. 2442 // TODO: remove more Connection-specific header fields here, in addition 2443 // to "Connection". 2444 if _, ok := rws.snapHeader["Connection"]; ok { 2445 v := rws.snapHeader.Get("Connection") 2446 delete(rws.snapHeader, "Connection") 2447 if v == "close" { 2448 rws.conn.startGracefulShutdown() 2449 } 2450 } 2451 2452 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp 2453 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{ 2454 streamID: rws.stream.id, 2455 httpResCode: rws.status, 2456 h: rws.snapHeader, 2457 endStream: endStream, 2458 contentType: ctype, 2459 contentLength: clen, 2460 date: date, 2461 }) 2462 if err != nil { 2463 rws.dirty = true 2464 return 0, err 2465 } 2466 if endStream { 2467 return 0, nil 2468 } 2469 } 2470 if isHeadResp { 2471 return len(p), nil 2472 } 2473 if len(p) == 0 && !rws.handlerDone { 2474 return 0, nil 2475 } 2476 2477 if rws.handlerDone { 2478 rws.promoteUndeclaredTrailers() 2479 } 2480 2481 // only send trailers if they have actually been defined by the 2482 // server handler. 2483 hasNonemptyTrailers := rws.hasNonemptyTrailers() 2484 endStream := rws.handlerDone && !hasNonemptyTrailers 2485 if len(p) > 0 || endStream { 2486 // only send a 0 byte DATA frame if we're ending the stream. 2487 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil { 2488 rws.dirty = true 2489 return 0, err 2490 } 2491 } 2492 2493 if rws.handlerDone && hasNonemptyTrailers { 2494 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{ 2495 streamID: rws.stream.id, 2496 h: rws.handlerHeader, 2497 trailers: rws.trailers, 2498 endStream: true, 2499 }) 2500 if err != nil { 2501 rws.dirty = true 2502 } 2503 return len(p), err 2504 } 2505 return len(p), nil 2506 } 2507 2508 // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys 2509 // that, if present, signals that the map entry is actually for 2510 // the response trailers, and not the response headers. The prefix 2511 // is stripped after the ServeHTTP call finishes and the values are 2512 // sent in the trailers. 2513 // 2514 // This mechanism is intended only for trailers that are not known 2515 // prior to the headers being written. If the set of trailers is fixed 2516 // or known before the header is written, the normal Go trailers mechanism 2517 // is preferred: 2518 // https://golang.org/pkg/net/http/#ResponseWriter 2519 // https://golang.org/pkg/net/http/#example_ResponseWriter_trailers 2520 const TrailerPrefix = "Trailer:" 2521 2522 // promoteUndeclaredTrailers permits http.Handlers to set trailers 2523 // after the header has already been flushed. Because the Go 2524 // ResponseWriter interface has no way to set Trailers (only the 2525 // Header), and because we didn't want to expand the ResponseWriter 2526 // interface, and because nobody used trailers, and because RFC 7230 2527 // says you SHOULD (but not must) predeclare any trailers in the 2528 // header, the official ResponseWriter rules said trailers in Go must 2529 // be predeclared, and then we reuse the same ResponseWriter.Header() 2530 // map to mean both Headers and Trailers. When it's time to write the 2531 // Trailers, we pick out the fields of Headers that were declared as 2532 // trailers. That worked for a while, until we found the first major 2533 // user of Trailers in the wild: gRPC (using them only over http2), 2534 // and gRPC libraries permit setting trailers mid-stream without 2535 // predeclaring them. So: change of plans. We still permit the old 2536 // way, but we also permit this hack: if a Header() key begins with 2537 // "Trailer:", the suffix of that key is a Trailer. Because ':' is an 2538 // invalid token byte anyway, there is no ambiguity. (And it's already 2539 // filtered out) It's mildly hacky, but not terrible. 2540 // 2541 // This method runs after the Handler is done and promotes any Header 2542 // fields to be trailers. 2543 func (rws *responseWriterState) promoteUndeclaredTrailers() { 2544 for k, vv := range rws.handlerHeader { 2545 if !strings.HasPrefix(k, TrailerPrefix) { 2546 continue 2547 } 2548 trailerKey := strings.TrimPrefix(k, TrailerPrefix) 2549 rws.declareTrailer(trailerKey) 2550 rws.handlerHeader[http.CanonicalHeaderKey(trailerKey)] = vv 2551 } 2552 2553 if len(rws.trailers) > 1 { 2554 sorter := sorterPool.Get().(*sorter) 2555 sorter.SortStrings(rws.trailers) 2556 sorterPool.Put(sorter) 2557 } 2558 } 2559 2560 func (w *responseWriter) Flush() { 2561 rws := w.rws 2562 if rws == nil { 2563 panic("Header called after Handler finished") 2564 } 2565 if rws.bw.Buffered() > 0 { 2566 if err := rws.bw.Flush(); err != nil { 2567 // Ignore the error. The frame writer already knows. 2568 return 2569 } 2570 } else { 2571 // The bufio.Writer won't call chunkWriter.Write 2572 // (writeChunk with zero bytes, so we have to do it 2573 // ourselves to force the HTTP response header and/or 2574 // final DATA frame (with END_STREAM) to be sent. 2575 rws.writeChunk(nil) 2576 } 2577 } 2578 2579 func (w *responseWriter) CloseNotify() <-chan bool { 2580 rws := w.rws 2581 if rws == nil { 2582 panic("CloseNotify called after Handler finished") 2583 } 2584 rws.closeNotifierMu.Lock() 2585 ch := rws.closeNotifierCh 2586 if ch == nil { 2587 ch = make(chan bool, 1) 2588 rws.closeNotifierCh = ch 2589 cw := rws.stream.cw 2590 go func() { 2591 cw.Wait() // wait for close 2592 ch <- true 2593 }() 2594 } 2595 rws.closeNotifierMu.Unlock() 2596 return ch 2597 } 2598 2599 func (w *responseWriter) Header() http.Header { 2600 rws := w.rws 2601 if rws == nil { 2602 panic("Header called after Handler finished") 2603 } 2604 if rws.handlerHeader == nil { 2605 rws.handlerHeader = make(http.Header) 2606 } 2607 return rws.handlerHeader 2608 } 2609 2610 // checkWriteHeaderCode is a copy of net/http's checkWriteHeaderCode. 2611 func checkWriteHeaderCode(code int) { 2612 // Issue 22880: require valid WriteHeader status codes. 2613 // For now we only enforce that it's three digits. 2614 // In the future we might block things over 599 (600 and above aren't defined 2615 // at http://httpwg.org/specs/rfc7231.html#status.codes) 2616 // and we might block under 200 (once we have more mature 1xx support). 2617 // But for now any three digits. 2618 // 2619 // We used to send "HTTP/1.1 000 0" on the wire in responses but there's 2620 // no equivalent bogus thing we can realistically send in HTTP/2, 2621 // so we'll consistently panic instead and help people find their bugs 2622 // early. (We can't return an error from WriteHeader even if we wanted to.) 2623 if code < 100 || code > 999 { 2624 panic(fmt.Sprintf("invalid WriteHeader code %v", code)) 2625 } 2626 } 2627 2628 func (w *responseWriter) WriteHeader(code int) { 2629 rws := w.rws 2630 if rws == nil { 2631 panic("WriteHeader called after Handler finished") 2632 } 2633 rws.writeHeader(code) 2634 } 2635 2636 func (rws *responseWriterState) writeHeader(code int) { 2637 if !rws.wroteHeader { 2638 checkWriteHeaderCode(code) 2639 rws.wroteHeader = true 2640 rws.status = code 2641 if len(rws.handlerHeader) > 0 { 2642 rws.snapHeader = cloneHeader(rws.handlerHeader) 2643 } 2644 } 2645 } 2646 2647 func cloneHeader(h http.Header) http.Header { 2648 h2 := make(http.Header, len(h)) 2649 for k, vv := range h { 2650 vv2 := make([]string, len(vv)) 2651 copy(vv2, vv) 2652 h2[k] = vv2 2653 } 2654 return h2 2655 } 2656 2657 // The Life Of A Write is like this: 2658 // 2659 // * Handler calls w.Write or w.WriteString -> 2660 // * -> rws.bw (*bufio.Writer) -> 2661 // * (Handler might call Flush) 2662 // * -> chunkWriter{rws} 2663 // * -> responseWriterState.writeChunk(p []byte) 2664 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 2665 func (w *responseWriter) Write(p []byte) (n int, err error) { 2666 return w.write(len(p), p, "") 2667 } 2668 2669 func (w *responseWriter) WriteString(s string) (n int, err error) { 2670 return w.write(len(s), nil, s) 2671 } 2672 2673 // either dataB or dataS is non-zero. 2674 func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 2675 rws := w.rws 2676 if rws == nil { 2677 panic("Write called after Handler finished") 2678 } 2679 if !rws.wroteHeader { 2680 w.WriteHeader(200) 2681 } 2682 if !bodyAllowedForStatus(rws.status) { 2683 return 0, http.ErrBodyNotAllowed 2684 } 2685 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set 2686 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen { 2687 // TODO: send a RST_STREAM 2688 return 0, errors.New("http2: handler wrote more than declared Content-Length") 2689 } 2690 2691 if dataB != nil { 2692 return rws.bw.Write(dataB) 2693 } else { 2694 return rws.bw.WriteString(dataS) 2695 } 2696 } 2697 2698 func (w *responseWriter) handlerDone() { 2699 rws := w.rws 2700 dirty := rws.dirty 2701 rws.handlerDone = true 2702 w.Flush() 2703 w.rws = nil 2704 if !dirty { 2705 // Only recycle the pool if all prior Write calls to 2706 // the serverConn goroutine completed successfully. If 2707 // they returned earlier due to resets from the peer 2708 // there might still be write goroutines outstanding 2709 // from the serverConn referencing the rws memory. See 2710 // issue 20704. 2711 responseWriterStatePool.Put(rws) 2712 } 2713 } 2714 2715 // Push errors. 2716 var ( 2717 ErrRecursivePush = errors.New("http2: recursive push not allowed") 2718 ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS") 2719 ) 2720 2721 var _ http.Pusher = (*responseWriter)(nil) 2722 2723 func (w *responseWriter) Push(target string, opts *http.PushOptions) error { 2724 st := w.rws.stream 2725 sc := st.sc 2726 sc.serveG.checkNotOn() 2727 2728 // No recursive pushes: "PUSH_PROMISE frames MUST only be sent on a peer-initiated stream." 2729 // http://tools.ietf.org/html/rfc7540#section-6.6 2730 if st.isPushed() { 2731 return ErrRecursivePush 2732 } 2733 2734 if opts == nil { 2735 opts = new(http.PushOptions) 2736 } 2737 2738 // Default options. 2739 if opts.Method == "" { 2740 opts.Method = "GET" 2741 } 2742 if opts.Header == nil { 2743 opts.Header = http.Header{} 2744 } 2745 wantScheme := "http" 2746 if w.rws.req.TLS != nil { 2747 wantScheme = "https" 2748 } 2749 2750 // Validate the request. 2751 u, err := url.Parse(target) 2752 if err != nil { 2753 return err 2754 } 2755 if u.Scheme == "" { 2756 if !strings.HasPrefix(target, "/") { 2757 return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target) 2758 } 2759 u.Scheme = wantScheme 2760 u.Host = w.rws.req.Host 2761 } else { 2762 if u.Scheme != wantScheme { 2763 return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme) 2764 } 2765 if u.Host == "" { 2766 return errors.New("URL must have a host") 2767 } 2768 } 2769 for k := range opts.Header { 2770 if strings.HasPrefix(k, ":") { 2771 return fmt.Errorf("promised request headers cannot include pseudo header %q", k) 2772 } 2773 } 2774 if err := checkValidPushPromiseRequestHeaders(opts.Header); err != nil { 2775 return err 2776 } 2777 2778 // The RFC effectively limits promised requests to GET and HEAD: 2779 // "Promised requests MUST be cacheable [GET, HEAD, or POST], and MUST be safe [GET or HEAD]" 2780 // http://tools.ietf.org/html/rfc7540#section-8.2 2781 if opts.Method != "GET" && opts.Method != "HEAD" { 2782 return fmt.Errorf("method %q must be GET or HEAD", opts.Method) 2783 } 2784 2785 msg := &startPushRequest{ 2786 parent: st, 2787 method: opts.Method, 2788 url: u, 2789 header: cloneHeader(opts.Header), 2790 done: errChanPool.Get().(chan error), 2791 } 2792 2793 select { 2794 case <-sc.doneServing: 2795 return errClientDisconnected 2796 case <-st.cw: 2797 return errStreamClosed 2798 case sc.serveMsgCh <- msg: 2799 } 2800 2801 select { 2802 case <-sc.doneServing: 2803 return errClientDisconnected 2804 case <-st.cw: 2805 return errStreamClosed 2806 case err := <-msg.done: 2807 errChanPool.Put(msg.done) 2808 return err 2809 } 2810 } 2811 2812 type startPushRequest struct { 2813 parent *stream 2814 method string 2815 url *url.URL 2816 header http.Header 2817 done chan error 2818 } 2819 2820 func (sc *serverConn) startPush(msg *startPushRequest) { 2821 sc.serveG.check() 2822 // http://tools.ietf.org/html/rfc7540#section-6.6. 2823 // PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that 2824 // is in either the "open" or "half-closed (remote)" state. 2825 if msg.parent.state != stateOpen && msg.parent.state != stateHalfClosedRemote { 2826 // responseWriter.Push checks that the stream is peer-initiated. 2827 msg.done <- errStreamClosed 2828 return 2829 } 2830 2831 // http://tools.ietf.org/html/rfc7540#section-6.6. 2832 if !sc.pushEnabled { 2833 msg.done <- http.ErrNotSupported 2834 return 2835 } 2836 2837 // PUSH_PROMISE frames must be sent in increasing order by stream ID, so 2838 // we allocate an ID for the promised stream lazily, when the PUSH_PROMISE 2839 // is written. Once the ID is allocated, we start the request handler. 2840 allocatePromisedID := func() (uint32, error) { 2841 sc.serveG.check() 2842 2843 // Check this again, just in case. Technically, we might have received 2844 // an updated SETTINGS by the time we got around to writing this frame. 2845 if !sc.pushEnabled { 2846 return 0, http.ErrNotSupported 2847 } 2848 // http://tools.ietf.org/html/rfc7540#section-6.5.2. 2849 if sc.curPushedStreams+1 > sc.clientMaxStreams { 2850 return 0, ErrPushLimitReached 2851 } 2852 2853 // http://tools.ietf.org/html/rfc7540#section-5.1.1. 2854 // Streams initiated by the server MUST use even-numbered identifiers. 2855 // A server that is unable to establish a new stream identifier can send a GOAWAY 2856 // frame so that the client is forced to open a new connection for new streams. 2857 if sc.maxPushPromiseID+2 >= 1<<31 { 2858 sc.startGracefulShutdownInternal() 2859 return 0, ErrPushLimitReached 2860 } 2861 sc.maxPushPromiseID += 2 2862 promisedID := sc.maxPushPromiseID 2863 2864 // http://tools.ietf.org/html/rfc7540#section-8.2. 2865 // Strictly speaking, the new stream should start in "reserved (local)", then 2866 // transition to "half closed (remote)" after sending the initial HEADERS, but 2867 // we start in "half closed (remote)" for simplicity. 2868 // See further comments at the definition of stateHalfClosedRemote. 2869 promised := sc.newStream(promisedID, msg.parent.id, stateHalfClosedRemote) 2870 rw, req, err := sc.newWriterAndRequestNoBody(promised, requestParam{ 2871 method: msg.method, 2872 scheme: msg.url.Scheme, 2873 authority: msg.url.Host, 2874 path: msg.url.RequestURI(), 2875 header: cloneHeader(msg.header), // clone since handler runs concurrently with writing the PUSH_PROMISE 2876 }) 2877 if err != nil { 2878 // Should not happen, since we've already validated msg.url. 2879 panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err)) 2880 } 2881 2882 go sc.runHandler(rw, req, sc.handler.ServeHTTP) 2883 return promisedID, nil 2884 } 2885 2886 sc.writeFrame(FrameWriteRequest{ 2887 write: &writePushPromise{ 2888 streamID: msg.parent.id, 2889 method: msg.method, 2890 url: msg.url, 2891 h: msg.header, 2892 allocatePromisedID: allocatePromisedID, 2893 }, 2894 stream: msg.parent, 2895 done: msg.done, 2896 }) 2897 } 2898 2899 // foreachHeaderElement splits v according to the "#rule" construction 2900 // in RFC 7230 section 7 and calls fn for each non-empty element. 2901 func foreachHeaderElement(v string, fn func(string)) { 2902 v = textproto.TrimString(v) 2903 if v == "" { 2904 return 2905 } 2906 if !strings.Contains(v, ",") { 2907 fn(v) 2908 return 2909 } 2910 for _, f := range strings.Split(v, ",") { 2911 if f = textproto.TrimString(f); f != "" { 2912 fn(f) 2913 } 2914 } 2915 } 2916 2917 // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2 2918 var connHeaders = []string{ 2919 "Connection", 2920 "Keep-Alive", 2921 "Proxy-Connection", 2922 "Transfer-Encoding", 2923 "Upgrade", 2924 } 2925 2926 // checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request, 2927 // per RFC 7540 Section 8.1.2.2. 2928 // The returned error is reported to users. 2929 func checkValidHTTP2RequestHeaders(h http.Header) error { 2930 for _, k := range connHeaders { 2931 if _, ok := h[k]; ok { 2932 return fmt.Errorf("request header %q is not valid in HTTP/2", k) 2933 } 2934 } 2935 te := h["Te"] 2936 if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) { 2937 return errors.New(`request header "TE" may only be "trailers" in HTTP/2`) 2938 } 2939 return nil 2940 } 2941 2942 var bodyRequestHeaders = []string{ 2943 "Content-Encoding", 2944 "Content-Length", 2945 "Expect", 2946 "Te", 2947 "Trailer", 2948 } 2949 2950 func checkValidPushPromiseRequestHeaders(h http.Header) error { 2951 // PUSH_PROMISE requests cannot have a body 2952 // http://tools.ietf.org/html/rfc7540#section-8.2 2953 for _, k := range bodyRequestHeaders { 2954 if _, ok := h[k]; ok { 2955 return fmt.Errorf("promised request cannot include body related header %q", k) 2956 } 2957 } 2958 // if _, ok := h["Host"]; ok { 2959 // return fmt.Errorf(`promised URL must be absolute so "Host" header disallowed`) 2960 // } 2961 return nil 2962 } 2963 2964 func new400Handler(err error) http.HandlerFunc { 2965 return func(w http.ResponseWriter, r *http.Request) { 2966 http.Error(w, err.Error(), http.StatusBadRequest) 2967 } 2968 } 2969 2970 // h1ServerKeepAlivesDisabled reports whether hs has its keep-alives 2971 // disabled. See comments on h1ServerShutdownChan above for why 2972 // the code is written this way. 2973 func h1ServerKeepAlivesDisabled(hs *http.Server) bool { 2974 var x interface{} = hs 2975 type I interface { 2976 doKeepAlives() bool 2977 } 2978 if hs, ok := x.(I); ok { 2979 return !hs.doKeepAlives() 2980 } 2981 return false 2982 }