github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/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: replace all <-sc.doneServing with reads from the stream's cw 6 // instead, and make sure that on close we close all open 7 // streams. then remove doneServing? 8 9 // TODO: finish GOAWAY support. Consider each incoming frame type and 10 // whether it should be ignored during a shutdown race. 11 12 // TODO: disconnect idle clients. GFE seems to do 4 minutes. make 13 // configurable? or maximum number of idle clients and remove the 14 // oldest? 15 16 // TODO: turn off the serve goroutine when idle, so 17 // an idle conn only has the readFrames goroutine active. (which could 18 // also be optimized probably to pin less memory in crypto/tls). This 19 // would involve tracking when the serve goroutine is active (atomic 20 // int32 read/CAS probably?) and starting it up when frames arrive, 21 // and shutting it down when all handlers exit. the occasional PING 22 // packets could use time.AfterFunc to call sc.wakeStartServeLoop() 23 // (which is a no-op if already running) and then queue the PING write 24 // as normal. The serve loop would then exit in most cases (if no 25 // Handlers running) and not be woken up again until the PING packet 26 // returns. 27 28 // TODO (maybe): add a mechanism for Handlers to going into 29 // half-closed-local mode (rw.(io.Closer) test?) but not exit their 30 // handler, and continue to be able to read from the 31 // Request.Body. This would be a somewhat semantic change from HTTP/1 32 // (or at least what we expose in net/http), so I'd probably want to 33 // add it there too. For now, this package says that returning from 34 // the Handler ServeHTTP function means you're both done reading and 35 // done writing, without a way to stop just one or the other. 36 37 package http2 38 39 import ( 40 "bufio" 41 "bytes" 42 "crypto/tls" 43 "errors" 44 "fmt" 45 "io" 46 "log" 47 "net" 48 "net/http" 49 "net/textproto" 50 "net/url" 51 "runtime" 52 "strconv" 53 "strings" 54 "sync" 55 "time" 56 57 "golang.org/x/net/http2/hpack" 58 ) 59 60 const ( 61 prefaceTimeout = 10 * time.Second 62 firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway 63 handlerChunkWriteSize = 4 << 10 64 defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to? 65 ) 66 67 var ( 68 errClientDisconnected = errors.New("client disconnected") 69 errClosedBody = errors.New("body closed by handler") 70 errHandlerComplete = errors.New("http2: request body closed due to handler exiting") 71 errStreamClosed = errors.New("http2: stream closed") 72 ) 73 74 var responseWriterStatePool = sync.Pool{ 75 New: func() interface{} { 76 rws := &responseWriterState{} 77 rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize) 78 return rws 79 }, 80 } 81 82 // Test hooks. 83 var ( 84 testHookOnConn func() 85 testHookGetServerConn func(*serverConn) 86 testHookOnPanicMu *sync.Mutex // nil except in tests 87 testHookOnPanic func(sc *serverConn, panicVal interface{}) (rePanic bool) 88 ) 89 90 // Server is an HTTP/2 server. 91 type Server struct { 92 // MaxHandlers limits the number of http.Handler ServeHTTP goroutines 93 // which may run at a time over all connections. 94 // Negative or zero no limit. 95 // TODO: implement 96 MaxHandlers int 97 98 // MaxConcurrentStreams optionally specifies the number of 99 // concurrent streams that each client may have open at a 100 // time. This is unrelated to the number of http.Handler goroutines 101 // which may be active globally, which is MaxHandlers. 102 // If zero, MaxConcurrentStreams defaults to at least 100, per 103 // the HTTP/2 spec's recommendations. 104 MaxConcurrentStreams uint32 105 106 // MaxReadFrameSize optionally specifies the largest frame 107 // this server is willing to read. A valid value is between 108 // 16k and 16M, inclusive. If zero or otherwise invalid, a 109 // default value is used. 110 MaxReadFrameSize uint32 111 112 // PermitProhibitedCipherSuites, if true, permits the use of 113 // cipher suites prohibited by the HTTP/2 spec. 114 PermitProhibitedCipherSuites bool 115 } 116 117 func (s *Server) maxReadFrameSize() uint32 { 118 if v := s.MaxReadFrameSize; v >= minMaxFrameSize && v <= maxFrameSize { 119 return v 120 } 121 return defaultMaxReadFrameSize 122 } 123 124 func (s *Server) maxConcurrentStreams() uint32 { 125 if v := s.MaxConcurrentStreams; v > 0 { 126 return v 127 } 128 return defaultMaxStreams 129 } 130 131 // ConfigureServer adds HTTP/2 support to a net/http Server. 132 // 133 // The configuration conf may be nil. 134 // 135 // ConfigureServer must be called before s begins serving. 136 func ConfigureServer(s *http.Server, conf *Server) error { 137 if conf == nil { 138 conf = new(Server) 139 } 140 141 if s.TLSConfig == nil { 142 s.TLSConfig = new(tls.Config) 143 } else if s.TLSConfig.CipherSuites != nil { 144 // If they already provided a CipherSuite list, return 145 // an error if it has a bad order or is missing 146 // ECDHE_RSA_WITH_AES_128_GCM_SHA256. 147 const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 148 haveRequired := false 149 sawBad := false 150 for i, cs := range s.TLSConfig.CipherSuites { 151 if cs == requiredCipher { 152 haveRequired = true 153 } 154 if isBadCipher(cs) { 155 sawBad = true 156 } else if sawBad { 157 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) 158 } 159 } 160 if !haveRequired { 161 return fmt.Errorf("http2: TLSConfig.CipherSuites is missing HTTP/2-required TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256") 162 } 163 } 164 165 // Note: not setting MinVersion to tls.VersionTLS12, 166 // as we don't want to interfere with HTTP/1.1 traffic 167 // on the user's server. We enforce TLS 1.2 later once 168 // we accept a connection. Ideally this should be done 169 // during next-proto selection, but using TLS <1.2 with 170 // HTTP/2 is still the client's bug. 171 172 s.TLSConfig.PreferServerCipherSuites = true 173 174 haveNPN := false 175 for _, p := range s.TLSConfig.NextProtos { 176 if p == NextProtoTLS { 177 haveNPN = true 178 break 179 } 180 } 181 if !haveNPN { 182 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, NextProtoTLS) 183 } 184 // h2-14 is temporary (as of 2015-03-05) while we wait for all browsers 185 // to switch to "h2". 186 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2-14") 187 188 if s.TLSNextProto == nil { 189 s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){} 190 } 191 protoHandler := func(hs *http.Server, c *tls.Conn, h http.Handler) { 192 if testHookOnConn != nil { 193 testHookOnConn() 194 } 195 conf.handleConn(hs, c, h) 196 } 197 s.TLSNextProto[NextProtoTLS] = protoHandler 198 s.TLSNextProto["h2-14"] = protoHandler // temporary; see above. 199 return nil 200 } 201 202 func (srv *Server) handleConn(hs *http.Server, c net.Conn, h http.Handler) { 203 sc := &serverConn{ 204 srv: srv, 205 hs: hs, 206 conn: c, 207 remoteAddrStr: c.RemoteAddr().String(), 208 bw: newBufferedWriter(c), 209 handler: h, 210 streams: make(map[uint32]*stream), 211 readFrameCh: make(chan readFrameResult), 212 wantWriteFrameCh: make(chan frameWriteMsg, 8), 213 wroteFrameCh: make(chan frameWriteResult, 1), // buffered; one send in writeFrameAsync 214 bodyReadCh: make(chan bodyReadMsg), // buffering doesn't matter either way 215 doneServing: make(chan struct{}), 216 advMaxStreams: srv.maxConcurrentStreams(), 217 writeSched: writeScheduler{ 218 maxFrameSize: initialMaxFrameSize, 219 }, 220 initialWindowSize: initialWindowSize, 221 headerTableSize: initialHeaderTableSize, 222 serveG: newGoroutineLock(), 223 pushEnabled: true, 224 } 225 sc.flow.add(initialWindowSize) 226 sc.inflow.add(initialWindowSize) 227 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf) 228 sc.hpackDecoder = hpack.NewDecoder(initialHeaderTableSize, nil) 229 sc.hpackDecoder.SetMaxStringLength(sc.maxHeaderStringLen()) 230 231 fr := NewFramer(sc.bw, c) 232 fr.SetMaxReadFrameSize(srv.maxReadFrameSize()) 233 sc.framer = fr 234 235 if tc, ok := c.(*tls.Conn); ok { 236 sc.tlsState = new(tls.ConnectionState) 237 *sc.tlsState = tc.ConnectionState() 238 // 9.2 Use of TLS Features 239 // An implementation of HTTP/2 over TLS MUST use TLS 240 // 1.2 or higher with the restrictions on feature set 241 // and cipher suite described in this section. Due to 242 // implementation limitations, it might not be 243 // possible to fail TLS negotiation. An endpoint MUST 244 // immediately terminate an HTTP/2 connection that 245 // does not meet the TLS requirements described in 246 // this section with a connection error (Section 247 // 5.4.1) of type INADEQUATE_SECURITY. 248 if sc.tlsState.Version < tls.VersionTLS12 { 249 sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low") 250 return 251 } 252 253 if sc.tlsState.ServerName == "" { 254 // Client must use SNI, but we don't enforce that anymore, 255 // since it was causing problems when connecting to bare IP 256 // addresses during development. 257 // 258 // TODO: optionally enforce? Or enforce at the time we receive 259 // a new request, and verify the the ServerName matches the :authority? 260 // But that precludes proxy situations, perhaps. 261 // 262 // So for now, do nothing here again. 263 } 264 265 if !srv.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) { 266 // "Endpoints MAY choose to generate a connection error 267 // (Section 5.4.1) of type INADEQUATE_SECURITY if one of 268 // the prohibited cipher suites are negotiated." 269 // 270 // We choose that. In my opinion, the spec is weak 271 // here. It also says both parties must support at least 272 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no 273 // excuses here. If we really must, we could allow an 274 // "AllowInsecureWeakCiphers" option on the server later. 275 // Let's see how it plays out first. 276 sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite)) 277 return 278 } 279 } 280 281 if hook := testHookGetServerConn; hook != nil { 282 hook(sc) 283 } 284 sc.serve() 285 } 286 287 // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec. 288 func isBadCipher(cipher uint16) bool { 289 switch cipher { 290 case tls.TLS_RSA_WITH_RC4_128_SHA, 291 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, 292 tls.TLS_RSA_WITH_AES_128_CBC_SHA, 293 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 294 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 295 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 296 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 297 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, 298 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 299 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 300 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: 301 // Reject cipher suites from Appendix A. 302 // "This list includes those cipher suites that do not 303 // offer an ephemeral key exchange and those that are 304 // based on the TLS null, stream or block cipher type" 305 return true 306 default: 307 return false 308 } 309 } 310 311 func (sc *serverConn) rejectConn(err ErrCode, debug string) { 312 sc.vlogf("REJECTING conn: %v, %s", err, debug) 313 // ignoring errors. hanging up anyway. 314 sc.framer.WriteGoAway(0, err, []byte(debug)) 315 sc.bw.Flush() 316 sc.conn.Close() 317 } 318 319 type serverConn struct { 320 // Immutable: 321 srv *Server 322 hs *http.Server 323 conn net.Conn 324 bw *bufferedWriter // writing to conn 325 handler http.Handler 326 framer *Framer 327 hpackDecoder *hpack.Decoder 328 doneServing chan struct{} // closed when serverConn.serve ends 329 readFrameCh chan readFrameResult // written by serverConn.readFrames 330 wantWriteFrameCh chan frameWriteMsg // from handlers -> serve 331 wroteFrameCh chan frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes 332 bodyReadCh chan bodyReadMsg // from handlers -> serve 333 testHookCh chan func(int) // code to run on the serve loop 334 flow flow // conn-wide (not stream-specific) outbound flow control 335 inflow flow // conn-wide inbound flow control 336 tlsState *tls.ConnectionState // shared by all handlers, like net/http 337 remoteAddrStr string 338 339 // Everything following is owned by the serve loop; use serveG.check(): 340 serveG goroutineLock // used to verify funcs are on serve() 341 pushEnabled bool 342 sawFirstSettings bool // got the initial SETTINGS frame after the preface 343 needToSendSettingsAck bool 344 unackedSettings int // how many SETTINGS have we sent without ACKs? 345 clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit) 346 advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client 347 curOpenStreams uint32 // client's number of open streams 348 maxStreamID uint32 // max ever seen 349 streams map[uint32]*stream 350 initialWindowSize int32 351 headerTableSize uint32 352 peerMaxHeaderListSize uint32 // zero means unknown (default) 353 canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case 354 req requestParam // non-zero while reading request headers 355 writingFrame bool // started write goroutine but haven't heard back on wroteFrameCh 356 needsFrameFlush bool // last frame write wasn't a flush 357 writeSched writeScheduler 358 inGoAway bool // we've started to or sent GOAWAY 359 needToSendGoAway bool // we need to schedule a GOAWAY frame write 360 goAwayCode ErrCode 361 shutdownTimerCh <-chan time.Time // nil until used 362 shutdownTimer *time.Timer // nil until used 363 364 // Owned by the writeFrameAsync goroutine: 365 headerWriteBuf bytes.Buffer 366 hpackEncoder *hpack.Encoder 367 } 368 369 func (sc *serverConn) maxHeaderStringLen() int { 370 v := sc.maxHeaderListSize() 371 if uint32(int(v)) == v { 372 return int(v) 373 } 374 // They had a crazy big number for MaxHeaderBytes anyway, 375 // so give them unlimited header lengths: 376 return 0 377 } 378 379 func (sc *serverConn) maxHeaderListSize() uint32 { 380 n := sc.hs.MaxHeaderBytes 381 if n <= 0 { 382 n = http.DefaultMaxHeaderBytes 383 } 384 // http2's count is in a slightly different unit and includes 32 bytes per pair. 385 // So, take the net/http.Server value and pad it up a bit, assuming 10 headers. 386 const perFieldOverhead = 32 // per http2 spec 387 const typicalHeaders = 10 // conservative 388 return uint32(n + typicalHeaders*perFieldOverhead) 389 } 390 391 // requestParam is the state of the next request, initialized over 392 // potentially several frames HEADERS + zero or more CONTINUATION 393 // frames. 394 type requestParam struct { 395 // stream is non-nil if we're reading (HEADER or CONTINUATION) 396 // frames for a request (but not DATA). 397 stream *stream 398 header http.Header 399 method, path string 400 scheme, authority string 401 sawRegularHeader bool // saw a non-pseudo header already 402 invalidHeader bool // an invalid header was seen 403 headerListSize int64 // actually uint32, but easier math this way 404 } 405 406 // stream represents a stream. This is the minimal metadata needed by 407 // the serve goroutine. Most of the actual stream state is owned by 408 // the http.Handler's goroutine in the responseWriter. Because the 409 // responseWriter's responseWriterState is recycled at the end of a 410 // handler, this struct intentionally has no pointer to the 411 // *responseWriter{,State} itself, as the Handler ending nils out the 412 // responseWriter's state field. 413 type stream struct { 414 // immutable: 415 sc *serverConn 416 id uint32 417 body *pipe // non-nil if expecting DATA frames 418 cw closeWaiter // closed wait stream transitions to closed state 419 420 // owned by serverConn's serve loop: 421 bodyBytes int64 // body bytes seen so far 422 declBodyBytes int64 // or -1 if undeclared 423 flow flow // limits writing from Handler to client 424 inflow flow // what the client is allowed to POST/etc to us 425 parent *stream // or nil 426 numTrailerValues int64 427 weight uint8 428 state streamState 429 sentReset bool // only true once detached from streams map 430 gotReset bool // only true once detacted from streams map 431 gotTrailerHeader bool // HEADER frame for trailers was seen 432 433 trailer http.Header // accumulated trailers 434 reqTrailer http.Header // handler's Request.Trailer 435 } 436 437 func (sc *serverConn) Framer() *Framer { return sc.framer } 438 func (sc *serverConn) CloseConn() error { return sc.conn.Close() } 439 func (sc *serverConn) Flush() error { return sc.bw.Flush() } 440 func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) { 441 return sc.hpackEncoder, &sc.headerWriteBuf 442 } 443 444 func (sc *serverConn) state(streamID uint32) (streamState, *stream) { 445 sc.serveG.check() 446 // http://http2.github.io/http2-spec/#rfc.section.5.1 447 if st, ok := sc.streams[streamID]; ok { 448 return st.state, st 449 } 450 // "The first use of a new stream identifier implicitly closes all 451 // streams in the "idle" state that might have been initiated by 452 // that peer with a lower-valued stream identifier. For example, if 453 // a client sends a HEADERS frame on stream 7 without ever sending a 454 // frame on stream 5, then stream 5 transitions to the "closed" 455 // state when the first frame for stream 7 is sent or received." 456 if streamID <= sc.maxStreamID { 457 return stateClosed, nil 458 } 459 return stateIdle, nil 460 } 461 462 // setConnState calls the net/http ConnState hook for this connection, if configured. 463 // Note that the net/http package does StateNew and StateClosed for us. 464 // There is currently no plan for StateHijacked or hijacking HTTP/2 connections. 465 func (sc *serverConn) setConnState(state http.ConnState) { 466 if sc.hs.ConnState != nil { 467 sc.hs.ConnState(sc.conn, state) 468 } 469 } 470 471 func (sc *serverConn) vlogf(format string, args ...interface{}) { 472 if VerboseLogs { 473 sc.logf(format, args...) 474 } 475 } 476 477 func (sc *serverConn) logf(format string, args ...interface{}) { 478 if lg := sc.hs.ErrorLog; lg != nil { 479 lg.Printf(format, args...) 480 } else { 481 log.Printf(format, args...) 482 } 483 } 484 485 func (sc *serverConn) condlogf(err error, format string, args ...interface{}) { 486 if err == nil { 487 return 488 } 489 str := err.Error() 490 if err == io.EOF || strings.Contains(str, "use of closed network connection") { 491 // Boring, expected errors. 492 sc.vlogf(format, args...) 493 } else { 494 sc.logf(format, args...) 495 } 496 } 497 498 func (sc *serverConn) onNewHeaderField(f hpack.HeaderField) { 499 sc.serveG.check() 500 sc.vlogf("got header field %+v", f) 501 switch { 502 case !validHeader(f.Name): 503 sc.req.invalidHeader = true 504 case strings.HasPrefix(f.Name, ":"): 505 if sc.req.sawRegularHeader { 506 sc.logf("pseudo-header after regular header") 507 sc.req.invalidHeader = true 508 return 509 } 510 var dst *string 511 switch f.Name { 512 case ":method": 513 dst = &sc.req.method 514 case ":path": 515 dst = &sc.req.path 516 case ":scheme": 517 dst = &sc.req.scheme 518 case ":authority": 519 dst = &sc.req.authority 520 default: 521 // 8.1.2.1 Pseudo-Header Fields 522 // "Endpoints MUST treat a request or response 523 // that contains undefined or invalid 524 // pseudo-header fields as malformed (Section 525 // 8.1.2.6)." 526 sc.logf("invalid pseudo-header %q", f.Name) 527 sc.req.invalidHeader = true 528 return 529 } 530 if *dst != "" { 531 sc.logf("duplicate pseudo-header %q sent", f.Name) 532 sc.req.invalidHeader = true 533 return 534 } 535 *dst = f.Value 536 default: 537 sc.req.sawRegularHeader = true 538 sc.req.header.Add(sc.canonicalHeader(f.Name), f.Value) 539 const headerFieldOverhead = 32 // per spec 540 sc.req.headerListSize += int64(len(f.Name)) + int64(len(f.Value)) + headerFieldOverhead 541 if sc.req.headerListSize > int64(sc.maxHeaderListSize()) { 542 sc.hpackDecoder.SetEmitEnabled(false) 543 } 544 } 545 } 546 547 func (st *stream) onNewTrailerField(f hpack.HeaderField) { 548 sc := st.sc 549 sc.serveG.check() 550 sc.vlogf("got trailer field %+v", f) 551 switch { 552 case !validHeader(f.Name): 553 // TODO: change hpack signature so this can return 554 // errors? Or stash an error somewhere on st or sc 555 // for processHeaderBlockFragment etc to pick up and 556 // return after the hpack Write/Close. For now just 557 // ignore. 558 return 559 case strings.HasPrefix(f.Name, ":"): 560 // TODO: same TODO as above. 561 return 562 default: 563 key := sc.canonicalHeader(f.Name) 564 if st.trailer != nil { 565 vv := append(st.trailer[key], f.Value) 566 st.trailer[key] = vv 567 568 // arbitrary; TODO: read spec about header list size limits wrt trailers 569 const tooBig = 1000 570 if len(vv) >= tooBig { 571 sc.hpackDecoder.SetEmitEnabled(false) 572 } 573 574 } 575 } 576 } 577 578 func (sc *serverConn) canonicalHeader(v string) string { 579 sc.serveG.check() 580 cv, ok := commonCanonHeader[v] 581 if ok { 582 return cv 583 } 584 cv, ok = sc.canonHeader[v] 585 if ok { 586 return cv 587 } 588 if sc.canonHeader == nil { 589 sc.canonHeader = make(map[string]string) 590 } 591 cv = http.CanonicalHeaderKey(v) 592 sc.canonHeader[v] = cv 593 return cv 594 } 595 596 type readFrameResult struct { 597 f Frame // valid until readMore is called 598 err error 599 600 // readMore should be called once the consumer no longer needs or 601 // retains f. After readMore, f is invalid and more frames can be 602 // read. 603 readMore func() 604 } 605 606 // readFrames is the loop that reads incoming frames. 607 // It takes care to only read one frame at a time, blocking until the 608 // consumer is done with the frame. 609 // It's run on its own goroutine. 610 func (sc *serverConn) readFrames() { 611 gate := make(gate) 612 for { 613 f, err := sc.framer.ReadFrame() 614 select { 615 case sc.readFrameCh <- readFrameResult{f, err, gate.Done}: 616 case <-sc.doneServing: 617 return 618 } 619 select { 620 case <-gate: 621 case <-sc.doneServing: 622 return 623 } 624 if terminalReadFrameError(err) { 625 return 626 } 627 } 628 } 629 630 // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine. 631 type frameWriteResult struct { 632 wm frameWriteMsg // what was written (or attempted) 633 err error // result of the writeFrame call 634 } 635 636 // writeFrameAsync runs in its own goroutine and writes a single frame 637 // and then reports when it's done. 638 // At most one goroutine can be running writeFrameAsync at a time per 639 // serverConn. 640 func (sc *serverConn) writeFrameAsync(wm frameWriteMsg) { 641 err := wm.write.writeFrame(sc) 642 sc.wroteFrameCh <- frameWriteResult{wm, err} 643 } 644 645 func (sc *serverConn) closeAllStreamsOnConnClose() { 646 sc.serveG.check() 647 for _, st := range sc.streams { 648 sc.closeStream(st, errClientDisconnected) 649 } 650 } 651 652 func (sc *serverConn) stopShutdownTimer() { 653 sc.serveG.check() 654 if t := sc.shutdownTimer; t != nil { 655 t.Stop() 656 } 657 } 658 659 func (sc *serverConn) notePanic() { 660 // Note: this is for serverConn.serve panicking, not http.Handler code. 661 if testHookOnPanicMu != nil { 662 testHookOnPanicMu.Lock() 663 defer testHookOnPanicMu.Unlock() 664 } 665 if testHookOnPanic != nil { 666 if e := recover(); e != nil { 667 if testHookOnPanic(sc, e) { 668 panic(e) 669 } 670 } 671 } 672 } 673 674 func (sc *serverConn) serve() { 675 sc.serveG.check() 676 defer sc.notePanic() 677 defer sc.conn.Close() 678 defer sc.closeAllStreamsOnConnClose() 679 defer sc.stopShutdownTimer() 680 defer close(sc.doneServing) // unblocks handlers trying to send 681 682 sc.vlogf("HTTP/2 connection from %v on %p", sc.conn.RemoteAddr(), sc.hs) 683 684 sc.writeFrame(frameWriteMsg{ 685 write: writeSettings{ 686 {SettingMaxFrameSize, sc.srv.maxReadFrameSize()}, 687 {SettingMaxConcurrentStreams, sc.advMaxStreams}, 688 {SettingMaxHeaderListSize, sc.maxHeaderListSize()}, 689 690 // TODO: more actual settings, notably 691 // SettingInitialWindowSize, but then we also 692 // want to bump up the conn window size the 693 // same amount here right after the settings 694 }, 695 }) 696 sc.unackedSettings++ 697 698 if err := sc.readPreface(); err != nil { 699 sc.condlogf(err, "error reading preface from client %v: %v", sc.conn.RemoteAddr(), err) 700 return 701 } 702 // Now that we've got the preface, get us out of the 703 // "StateNew" state. We can't go directly to idle, though. 704 // Active means we read some data and anticipate a request. We'll 705 // do another Active when we get a HEADERS frame. 706 sc.setConnState(http.StateActive) 707 sc.setConnState(http.StateIdle) 708 709 go sc.readFrames() // closed by defer sc.conn.Close above 710 711 settingsTimer := time.NewTimer(firstSettingsTimeout) 712 loopNum := 0 713 for { 714 loopNum++ 715 select { 716 case wm := <-sc.wantWriteFrameCh: 717 sc.writeFrame(wm) 718 case res := <-sc.wroteFrameCh: 719 sc.wroteFrame(res) 720 case res := <-sc.readFrameCh: 721 if !sc.processFrameFromReader(res) { 722 return 723 } 724 res.readMore() 725 if settingsTimer.C != nil { 726 settingsTimer.Stop() 727 settingsTimer.C = nil 728 } 729 case m := <-sc.bodyReadCh: 730 sc.noteBodyRead(m.st, m.n) 731 case <-settingsTimer.C: 732 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr()) 733 return 734 case <-sc.shutdownTimerCh: 735 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr()) 736 return 737 case fn := <-sc.testHookCh: 738 fn(loopNum) 739 } 740 } 741 } 742 743 // readPreface reads the ClientPreface greeting from the peer 744 // or returns an error on timeout or an invalid greeting. 745 func (sc *serverConn) readPreface() error { 746 errc := make(chan error, 1) 747 go func() { 748 // Read the client preface 749 buf := make([]byte, len(ClientPreface)) 750 if _, err := io.ReadFull(sc.conn, buf); err != nil { 751 errc <- err 752 } else if !bytes.Equal(buf, clientPreface) { 753 errc <- fmt.Errorf("bogus greeting %q", buf) 754 } else { 755 errc <- nil 756 } 757 }() 758 timer := time.NewTimer(prefaceTimeout) // TODO: configurable on *Server? 759 defer timer.Stop() 760 select { 761 case <-timer.C: 762 return errors.New("timeout waiting for client preface") 763 case err := <-errc: 764 if err == nil { 765 sc.vlogf("client %v said hello", sc.conn.RemoteAddr()) 766 } 767 return err 768 } 769 } 770 771 var errChanPool = sync.Pool{ 772 New: func() interface{} { return make(chan error, 1) }, 773 } 774 775 var writeDataPool = sync.Pool{ 776 New: func() interface{} { return new(writeData) }, 777 } 778 779 // writeDataFromHandler writes DATA response frames from a handler on 780 // the given stream. 781 func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error { 782 ch := errChanPool.Get().(chan error) 783 writeArg := writeDataPool.Get().(*writeData) 784 *writeArg = writeData{stream.id, data, endStream} 785 err := sc.writeFrameFromHandler(frameWriteMsg{ 786 write: writeArg, 787 stream: stream, 788 done: ch, 789 }) 790 if err != nil { 791 return err 792 } 793 var frameWriteDone bool // the frame write is done (successfully or not) 794 select { 795 case err = <-ch: 796 frameWriteDone = true 797 case <-sc.doneServing: 798 return errClientDisconnected 799 case <-stream.cw: 800 // If both ch and stream.cw were ready (as might 801 // happen on the final Write after an http.Handler 802 // ends), prefer the write result. Otherwise this 803 // might just be us successfully closing the stream. 804 // The writeFrameAsync and serve goroutines guarantee 805 // that the ch send will happen before the stream.cw 806 // close. 807 select { 808 case err = <-ch: 809 frameWriteDone = true 810 default: 811 return errStreamClosed 812 } 813 } 814 errChanPool.Put(ch) 815 if frameWriteDone { 816 writeDataPool.Put(writeArg) 817 } 818 return err 819 } 820 821 // writeFrameFromHandler sends wm to sc.wantWriteFrameCh, but aborts 822 // if the connection has gone away. 823 // 824 // This must not be run from the serve goroutine itself, else it might 825 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 826 // buffered and is read by serve itself). If you're on the serve 827 // goroutine, call writeFrame instead. 828 func (sc *serverConn) writeFrameFromHandler(wm frameWriteMsg) error { 829 sc.serveG.checkNotOn() // NOT 830 select { 831 case sc.wantWriteFrameCh <- wm: 832 return nil 833 case <-sc.doneServing: 834 // Serve loop is gone. 835 // Client has closed their connection to the server. 836 return errClientDisconnected 837 } 838 } 839 840 // writeFrame schedules a frame to write and sends it if there's nothing 841 // already being written. 842 // 843 // There is no pushback here (the serve goroutine never blocks). It's 844 // the http.Handlers that block, waiting for their previous frames to 845 // make it onto the wire 846 // 847 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 848 func (sc *serverConn) writeFrame(wm frameWriteMsg) { 849 sc.serveG.check() 850 sc.writeSched.add(wm) 851 sc.scheduleFrameWrite() 852 } 853 854 // startFrameWrite starts a goroutine to write wm (in a separate 855 // goroutine since that might block on the network), and updates the 856 // serve goroutine's state about the world, updated from info in wm. 857 func (sc *serverConn) startFrameWrite(wm frameWriteMsg) { 858 sc.serveG.check() 859 if sc.writingFrame { 860 panic("internal error: can only be writing one frame at a time") 861 } 862 863 st := wm.stream 864 if st != nil { 865 switch st.state { 866 case stateHalfClosedLocal: 867 panic("internal error: attempt to send frame on half-closed-local stream") 868 case stateClosed: 869 if st.sentReset || st.gotReset { 870 // Skip this frame. 871 sc.scheduleFrameWrite() 872 return 873 } 874 panic(fmt.Sprintf("internal error: attempt to send a write %v on a closed stream", wm)) 875 } 876 } 877 878 sc.writingFrame = true 879 sc.needsFrameFlush = true 880 go sc.writeFrameAsync(wm) 881 } 882 883 // errHandlerPanicked is the error given to any callers blocked in a read from 884 // Request.Body when the main goroutine panics. Since most handlers read in the 885 // the main ServeHTTP goroutine, this will show up rarely. 886 var errHandlerPanicked = errors.New("http2: handler panicked") 887 888 // wroteFrame is called on the serve goroutine with the result of 889 // whatever happened on writeFrameAsync. 890 func (sc *serverConn) wroteFrame(res frameWriteResult) { 891 sc.serveG.check() 892 if !sc.writingFrame { 893 panic("internal error: expected to be already writing a frame") 894 } 895 sc.writingFrame = false 896 897 wm := res.wm 898 st := wm.stream 899 900 closeStream := endsStream(wm.write) 901 902 if _, ok := wm.write.(handlerPanicRST); ok { 903 sc.closeStream(st, errHandlerPanicked) 904 } 905 906 // Reply (if requested) to the blocked ServeHTTP goroutine. 907 if ch := wm.done; ch != nil { 908 select { 909 case ch <- res.err: 910 default: 911 panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.write)) 912 } 913 } 914 wm.write = nil // prevent use (assume it's tainted after wm.done send) 915 916 if closeStream { 917 if st == nil { 918 panic("internal error: expecting non-nil stream") 919 } 920 switch st.state { 921 case stateOpen: 922 // Here we would go to stateHalfClosedLocal in 923 // theory, but since our handler is done and 924 // the net/http package provides no mechanism 925 // for finishing writing to a ResponseWriter 926 // while still reading data (see possible TODO 927 // at top of this file), we go into closed 928 // state here anyway, after telling the peer 929 // we're hanging up on them. 930 st.state = stateHalfClosedLocal // won't last long, but necessary for closeStream via resetStream 931 errCancel := StreamError{st.id, ErrCodeCancel} 932 sc.resetStream(errCancel) 933 case stateHalfClosedRemote: 934 sc.closeStream(st, errHandlerComplete) 935 } 936 } 937 938 sc.scheduleFrameWrite() 939 } 940 941 // scheduleFrameWrite tickles the frame writing scheduler. 942 // 943 // If a frame is already being written, nothing happens. This will be called again 944 // when the frame is done being written. 945 // 946 // If a frame isn't being written we need to send one, the best frame 947 // to send is selected, preferring first things that aren't 948 // stream-specific (e.g. ACKing settings), and then finding the 949 // highest priority stream. 950 // 951 // If a frame isn't being written and there's nothing else to send, we 952 // flush the write buffer. 953 func (sc *serverConn) scheduleFrameWrite() { 954 sc.serveG.check() 955 if sc.writingFrame { 956 return 957 } 958 if sc.needToSendGoAway { 959 sc.needToSendGoAway = false 960 sc.startFrameWrite(frameWriteMsg{ 961 write: &writeGoAway{ 962 maxStreamID: sc.maxStreamID, 963 code: sc.goAwayCode, 964 }, 965 }) 966 return 967 } 968 if sc.needToSendSettingsAck { 969 sc.needToSendSettingsAck = false 970 sc.startFrameWrite(frameWriteMsg{write: writeSettingsAck{}}) 971 return 972 } 973 if !sc.inGoAway { 974 if wm, ok := sc.writeSched.take(); ok { 975 sc.startFrameWrite(wm) 976 return 977 } 978 } 979 if sc.needsFrameFlush { 980 sc.startFrameWrite(frameWriteMsg{write: flushFrameWriter{}}) 981 sc.needsFrameFlush = false // after startFrameWrite, since it sets this true 982 return 983 } 984 } 985 986 func (sc *serverConn) goAway(code ErrCode) { 987 sc.serveG.check() 988 if sc.inGoAway { 989 return 990 } 991 if code != ErrCodeNo { 992 sc.shutDownIn(250 * time.Millisecond) 993 } else { 994 // TODO: configurable 995 sc.shutDownIn(1 * time.Second) 996 } 997 sc.inGoAway = true 998 sc.needToSendGoAway = true 999 sc.goAwayCode = code 1000 sc.scheduleFrameWrite() 1001 } 1002 1003 func (sc *serverConn) shutDownIn(d time.Duration) { 1004 sc.serveG.check() 1005 sc.shutdownTimer = time.NewTimer(d) 1006 sc.shutdownTimerCh = sc.shutdownTimer.C 1007 } 1008 1009 func (sc *serverConn) resetStream(se StreamError) { 1010 sc.serveG.check() 1011 sc.writeFrame(frameWriteMsg{write: se}) 1012 if st, ok := sc.streams[se.StreamID]; ok { 1013 st.sentReset = true 1014 sc.closeStream(st, se) 1015 } 1016 } 1017 1018 // processFrameFromReader processes the serve loop's read from readFrameCh from the 1019 // frame-reading goroutine. 1020 // processFrameFromReader returns whether the connection should be kept open. 1021 func (sc *serverConn) processFrameFromReader(res readFrameResult) bool { 1022 sc.serveG.check() 1023 err := res.err 1024 if err != nil { 1025 if err == ErrFrameTooLarge { 1026 sc.goAway(ErrCodeFrameSize) 1027 return true // goAway will close the loop 1028 } 1029 clientGone := err == io.EOF || strings.Contains(err.Error(), "use of closed network connection") 1030 if clientGone { 1031 // TODO: could we also get into this state if 1032 // the peer does a half close 1033 // (e.g. CloseWrite) because they're done 1034 // sending frames but they're still wanting 1035 // our open replies? Investigate. 1036 // TODO: add CloseWrite to crypto/tls.Conn first 1037 // so we have a way to test this? I suppose 1038 // just for testing we could have a non-TLS mode. 1039 return false 1040 } 1041 } else { 1042 f := res.f 1043 sc.vlogf("got %v: %#v", f.Header(), f) 1044 err = sc.processFrame(f) 1045 if err == nil { 1046 return true 1047 } 1048 } 1049 1050 switch ev := err.(type) { 1051 case StreamError: 1052 sc.resetStream(ev) 1053 return true 1054 case goAwayFlowError: 1055 sc.goAway(ErrCodeFlowControl) 1056 return true 1057 case ConnectionError: 1058 sc.logf("%v: %v", sc.conn.RemoteAddr(), ev) 1059 sc.goAway(ErrCode(ev)) 1060 return true // goAway will handle shutdown 1061 default: 1062 if res.err != nil { 1063 sc.logf("disconnecting; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 1064 } else { 1065 sc.logf("disconnection due to other error: %v", err) 1066 } 1067 return false 1068 } 1069 } 1070 1071 func (sc *serverConn) processFrame(f Frame) error { 1072 sc.serveG.check() 1073 1074 // First frame received must be SETTINGS. 1075 if !sc.sawFirstSettings { 1076 if _, ok := f.(*SettingsFrame); !ok { 1077 return ConnectionError(ErrCodeProtocol) 1078 } 1079 sc.sawFirstSettings = true 1080 } 1081 1082 switch f := f.(type) { 1083 case *SettingsFrame: 1084 return sc.processSettings(f) 1085 case *HeadersFrame: 1086 return sc.processHeaders(f) 1087 case *ContinuationFrame: 1088 return sc.processContinuation(f) 1089 case *WindowUpdateFrame: 1090 return sc.processWindowUpdate(f) 1091 case *PingFrame: 1092 return sc.processPing(f) 1093 case *DataFrame: 1094 return sc.processData(f) 1095 case *RSTStreamFrame: 1096 return sc.processResetStream(f) 1097 case *PriorityFrame: 1098 return sc.processPriority(f) 1099 case *PushPromiseFrame: 1100 // A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE 1101 // frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 1102 return ConnectionError(ErrCodeProtocol) 1103 default: 1104 sc.vlogf("Ignoring frame: %v", f.Header()) 1105 return nil 1106 } 1107 } 1108 1109 func (sc *serverConn) processPing(f *PingFrame) error { 1110 sc.serveG.check() 1111 if f.IsAck() { 1112 // 6.7 PING: " An endpoint MUST NOT respond to PING frames 1113 // containing this flag." 1114 return nil 1115 } 1116 if f.StreamID != 0 { 1117 // "PING frames are not associated with any individual 1118 // stream. If a PING frame is received with a stream 1119 // identifier field value other than 0x0, the recipient MUST 1120 // respond with a connection error (Section 5.4.1) of type 1121 // PROTOCOL_ERROR." 1122 return ConnectionError(ErrCodeProtocol) 1123 } 1124 sc.writeFrame(frameWriteMsg{write: writePingAck{f}}) 1125 return nil 1126 } 1127 1128 func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error { 1129 sc.serveG.check() 1130 switch { 1131 case f.StreamID != 0: // stream-level flow control 1132 st := sc.streams[f.StreamID] 1133 if st == nil { 1134 // "WINDOW_UPDATE can be sent by a peer that has sent a 1135 // frame bearing the END_STREAM flag. This means that a 1136 // receiver could receive a WINDOW_UPDATE frame on a "half 1137 // closed (remote)" or "closed" stream. A receiver MUST 1138 // NOT treat this as an error, see Section 5.1." 1139 return nil 1140 } 1141 if !st.flow.add(int32(f.Increment)) { 1142 return StreamError{f.StreamID, ErrCodeFlowControl} 1143 } 1144 default: // connection-level flow control 1145 if !sc.flow.add(int32(f.Increment)) { 1146 return goAwayFlowError{} 1147 } 1148 } 1149 sc.scheduleFrameWrite() 1150 return nil 1151 } 1152 1153 func (sc *serverConn) processResetStream(f *RSTStreamFrame) error { 1154 sc.serveG.check() 1155 1156 state, st := sc.state(f.StreamID) 1157 if state == stateIdle { 1158 // 6.4 "RST_STREAM frames MUST NOT be sent for a 1159 // stream in the "idle" state. If a RST_STREAM frame 1160 // identifying an idle stream is received, the 1161 // recipient MUST treat this as a connection error 1162 // (Section 5.4.1) of type PROTOCOL_ERROR. 1163 return ConnectionError(ErrCodeProtocol) 1164 } 1165 if st != nil { 1166 st.gotReset = true 1167 sc.closeStream(st, StreamError{f.StreamID, f.ErrCode}) 1168 } 1169 return nil 1170 } 1171 1172 func (sc *serverConn) closeStream(st *stream, err error) { 1173 sc.serveG.check() 1174 if st.state == stateIdle || st.state == stateClosed { 1175 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 1176 } 1177 st.state = stateClosed 1178 sc.curOpenStreams-- 1179 if sc.curOpenStreams == 0 { 1180 sc.setConnState(http.StateIdle) 1181 } 1182 delete(sc.streams, st.id) 1183 if p := st.body; p != nil { 1184 p.CloseWithError(err) 1185 } 1186 st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc 1187 sc.writeSched.forgetStream(st.id) 1188 } 1189 1190 func (sc *serverConn) processSettings(f *SettingsFrame) error { 1191 sc.serveG.check() 1192 if f.IsAck() { 1193 sc.unackedSettings-- 1194 if sc.unackedSettings < 0 { 1195 // Why is the peer ACKing settings we never sent? 1196 // The spec doesn't mention this case, but 1197 // hang up on them anyway. 1198 return ConnectionError(ErrCodeProtocol) 1199 } 1200 return nil 1201 } 1202 if err := f.ForeachSetting(sc.processSetting); err != nil { 1203 return err 1204 } 1205 sc.needToSendSettingsAck = true 1206 sc.scheduleFrameWrite() 1207 return nil 1208 } 1209 1210 func (sc *serverConn) processSetting(s Setting) error { 1211 sc.serveG.check() 1212 if err := s.Valid(); err != nil { 1213 return err 1214 } 1215 sc.vlogf("processing setting %v", s) 1216 switch s.ID { 1217 case SettingHeaderTableSize: 1218 sc.headerTableSize = s.Val 1219 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 1220 case SettingEnablePush: 1221 sc.pushEnabled = s.Val != 0 1222 case SettingMaxConcurrentStreams: 1223 sc.clientMaxStreams = s.Val 1224 case SettingInitialWindowSize: 1225 return sc.processSettingInitialWindowSize(s.Val) 1226 case SettingMaxFrameSize: 1227 sc.writeSched.maxFrameSize = s.Val 1228 case SettingMaxHeaderListSize: 1229 sc.peerMaxHeaderListSize = s.Val 1230 default: 1231 // Unknown setting: "An endpoint that receives a SETTINGS 1232 // frame with any unknown or unsupported identifier MUST 1233 // ignore that setting." 1234 } 1235 return nil 1236 } 1237 1238 func (sc *serverConn) processSettingInitialWindowSize(val uint32) error { 1239 sc.serveG.check() 1240 // Note: val already validated to be within range by 1241 // processSetting's Valid call. 1242 1243 // "A SETTINGS frame can alter the initial flow control window 1244 // size for all current streams. When the value of 1245 // SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST 1246 // adjust the size of all stream flow control windows that it 1247 // maintains by the difference between the new value and the 1248 // old value." 1249 old := sc.initialWindowSize 1250 sc.initialWindowSize = int32(val) 1251 growth := sc.initialWindowSize - old // may be negative 1252 for _, st := range sc.streams { 1253 if !st.flow.add(growth) { 1254 // 6.9.2 Initial Flow Control Window Size 1255 // "An endpoint MUST treat a change to 1256 // SETTINGS_INITIAL_WINDOW_SIZE that causes any flow 1257 // control window to exceed the maximum size as a 1258 // connection error (Section 5.4.1) of type 1259 // FLOW_CONTROL_ERROR." 1260 return ConnectionError(ErrCodeFlowControl) 1261 } 1262 } 1263 return nil 1264 } 1265 1266 func (sc *serverConn) processData(f *DataFrame) error { 1267 sc.serveG.check() 1268 // "If a DATA frame is received whose stream is not in "open" 1269 // or "half closed (local)" state, the recipient MUST respond 1270 // with a stream error (Section 5.4.2) of type STREAM_CLOSED." 1271 id := f.Header().StreamID 1272 st, ok := sc.streams[id] 1273 if !ok || st.state != stateOpen || st.gotTrailerHeader { 1274 // This includes sending a RST_STREAM if the stream is 1275 // in stateHalfClosedLocal (which currently means that 1276 // the http.Handler returned, so it's done reading & 1277 // done writing). Try to stop the client from sending 1278 // more DATA. 1279 return StreamError{id, ErrCodeStreamClosed} 1280 } 1281 if st.body == nil { 1282 panic("internal error: should have a body in this state") 1283 } 1284 data := f.Data() 1285 1286 // Sender sending more than they'd declared? 1287 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 1288 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 1289 return StreamError{id, ErrCodeStreamClosed} 1290 } 1291 if len(data) > 0 { 1292 // Check whether the client has flow control quota. 1293 if int(st.inflow.available()) < len(data) { 1294 return StreamError{id, ErrCodeFlowControl} 1295 } 1296 st.inflow.take(int32(len(data))) 1297 wrote, err := st.body.Write(data) 1298 if err != nil { 1299 return StreamError{id, ErrCodeStreamClosed} 1300 } 1301 if wrote != len(data) { 1302 panic("internal error: bad Writer") 1303 } 1304 st.bodyBytes += int64(len(data)) 1305 } 1306 if f.StreamEnded() { 1307 st.endStream() 1308 } 1309 return nil 1310 } 1311 1312 // endStream closes a Request.Body's pipe. It is called when a DATA 1313 // frame says a request body is over (or after trailers). 1314 func (st *stream) endStream() { 1315 sc := st.sc 1316 sc.serveG.check() 1317 1318 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 1319 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 1320 st.declBodyBytes, st.bodyBytes)) 1321 } else { 1322 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest) 1323 st.body.CloseWithError(io.EOF) 1324 } 1325 st.state = stateHalfClosedRemote 1326 } 1327 1328 // copyTrailersToHandlerRequest is run in the Handler's goroutine in 1329 // its Request.Body.Read just before it gets io.EOF. 1330 func (st *stream) copyTrailersToHandlerRequest() { 1331 for k, vv := range st.trailer { 1332 if _, ok := st.reqTrailer[k]; ok { 1333 // Only copy it over it was pre-declared. 1334 st.reqTrailer[k] = vv 1335 } 1336 } 1337 } 1338 1339 func (sc *serverConn) processHeaders(f *HeadersFrame) error { 1340 sc.serveG.check() 1341 id := f.Header().StreamID 1342 if sc.inGoAway { 1343 // Ignore. 1344 return nil 1345 } 1346 // http://http2.github.io/http2-spec/#rfc.section.5.1.1 1347 // Streams initiated by a client MUST use odd-numbered stream 1348 // identifiers. [...] An endpoint that receives an unexpected 1349 // stream identifier MUST respond with a connection error 1350 // (Section 5.4.1) of type PROTOCOL_ERROR. 1351 if id%2 != 1 { 1352 return ConnectionError(ErrCodeProtocol) 1353 } 1354 // A HEADERS frame can be used to create a new stream or 1355 // send a trailer for an open one. If we already have a stream 1356 // open, let it process its own HEADERS frame (trailers at this 1357 // point, if it's valid). 1358 st := sc.streams[f.Header().StreamID] 1359 if st != nil { 1360 return st.processTrailerHeaders(f) 1361 } 1362 1363 // [...] The identifier of a newly established stream MUST be 1364 // numerically greater than all streams that the initiating 1365 // endpoint has opened or reserved. [...] An endpoint that 1366 // receives an unexpected stream identifier MUST respond with 1367 // a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 1368 if id <= sc.maxStreamID || sc.req.stream != nil { 1369 return ConnectionError(ErrCodeProtocol) 1370 } 1371 1372 if id > sc.maxStreamID { 1373 sc.maxStreamID = id 1374 } 1375 st = &stream{ 1376 sc: sc, 1377 id: id, 1378 state: stateOpen, 1379 } 1380 if f.StreamEnded() { 1381 st.state = stateHalfClosedRemote 1382 } 1383 st.cw.Init() 1384 1385 st.flow.conn = &sc.flow // link to conn-level counter 1386 st.flow.add(sc.initialWindowSize) 1387 st.inflow.conn = &sc.inflow // link to conn-level counter 1388 st.inflow.add(initialWindowSize) // TODO: update this when we send a higher initial window size in the initial settings 1389 1390 sc.streams[id] = st 1391 if f.HasPriority() { 1392 adjustStreamPriority(sc.streams, st.id, f.Priority) 1393 } 1394 sc.curOpenStreams++ 1395 if sc.curOpenStreams == 1 { 1396 sc.setConnState(http.StateActive) 1397 } 1398 sc.req = requestParam{ 1399 stream: st, 1400 header: make(http.Header), 1401 } 1402 sc.hpackDecoder.SetEmitFunc(sc.onNewHeaderField) 1403 sc.hpackDecoder.SetEmitEnabled(true) 1404 return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded()) 1405 } 1406 1407 func (st *stream) processTrailerHeaders(f *HeadersFrame) error { 1408 sc := st.sc 1409 sc.serveG.check() 1410 if st.gotTrailerHeader { 1411 return ConnectionError(ErrCodeProtocol) 1412 } 1413 st.gotTrailerHeader = true 1414 return st.processTrailerHeaderBlockFragment(f.HeaderBlockFragment(), f.HeadersEnded()) 1415 } 1416 1417 func (sc *serverConn) processContinuation(f *ContinuationFrame) error { 1418 sc.serveG.check() 1419 st := sc.streams[f.Header().StreamID] 1420 if st.gotTrailerHeader { 1421 return st.processTrailerHeaderBlockFragment(f.HeaderBlockFragment(), f.HeadersEnded()) 1422 } 1423 return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded()) 1424 } 1425 1426 func (sc *serverConn) processHeaderBlockFragment(st *stream, frag []byte, end bool) error { 1427 sc.serveG.check() 1428 if _, err := sc.hpackDecoder.Write(frag); err != nil { 1429 return ConnectionError(ErrCodeCompression) 1430 } 1431 if !end { 1432 return nil 1433 } 1434 if err := sc.hpackDecoder.Close(); err != nil { 1435 return ConnectionError(ErrCodeCompression) 1436 } 1437 defer sc.resetPendingRequest() 1438 if sc.curOpenStreams > sc.advMaxStreams { 1439 // "Endpoints MUST NOT exceed the limit set by their 1440 // peer. An endpoint that receives a HEADERS frame 1441 // that causes their advertised concurrent stream 1442 // limit to be exceeded MUST treat this as a stream 1443 // error (Section 5.4.2) of type PROTOCOL_ERROR or 1444 // REFUSED_STREAM." 1445 if sc.unackedSettings == 0 { 1446 // They should know better. 1447 return StreamError{st.id, ErrCodeProtocol} 1448 } 1449 // Assume it's a network race, where they just haven't 1450 // received our last SETTINGS update. But actually 1451 // this can't happen yet, because we don't yet provide 1452 // a way for users to adjust server parameters at 1453 // runtime. 1454 return StreamError{st.id, ErrCodeRefusedStream} 1455 } 1456 1457 rw, req, err := sc.newWriterAndRequest() 1458 if err != nil { 1459 return err 1460 } 1461 st.reqTrailer = req.Trailer 1462 if st.reqTrailer != nil { 1463 st.trailer = make(http.Header) 1464 } 1465 st.body = req.Body.(*requestBody).pipe // may be nil 1466 st.declBodyBytes = req.ContentLength 1467 1468 handler := sc.handler.ServeHTTP 1469 if !sc.hpackDecoder.EmitEnabled() { 1470 // Their header list was too long. Send a 431 error. 1471 handler = handleHeaderListTooLong 1472 } 1473 1474 go sc.runHandler(rw, req, handler) 1475 return nil 1476 } 1477 1478 func (st *stream) processTrailerHeaderBlockFragment(frag []byte, end bool) error { 1479 sc := st.sc 1480 sc.serveG.check() 1481 sc.hpackDecoder.SetEmitFunc(st.onNewTrailerField) 1482 if _, err := sc.hpackDecoder.Write(frag); err != nil { 1483 return ConnectionError(ErrCodeCompression) 1484 } 1485 if !end { 1486 return nil 1487 } 1488 err := sc.hpackDecoder.Close() 1489 st.endStream() 1490 if err != nil { 1491 return ConnectionError(ErrCodeCompression) 1492 } 1493 return nil 1494 } 1495 1496 func (sc *serverConn) processPriority(f *PriorityFrame) error { 1497 adjustStreamPriority(sc.streams, f.StreamID, f.PriorityParam) 1498 return nil 1499 } 1500 1501 func adjustStreamPriority(streams map[uint32]*stream, streamID uint32, priority PriorityParam) { 1502 st, ok := streams[streamID] 1503 if !ok { 1504 // TODO: not quite correct (this streamID might 1505 // already exist in the dep tree, but be closed), but 1506 // close enough for now. 1507 return 1508 } 1509 st.weight = priority.Weight 1510 parent := streams[priority.StreamDep] // might be nil 1511 if parent == st { 1512 // if client tries to set this stream to be the parent of itself 1513 // ignore and keep going 1514 return 1515 } 1516 1517 // section 5.3.3: If a stream is made dependent on one of its 1518 // own dependencies, the formerly dependent stream is first 1519 // moved to be dependent on the reprioritized stream's previous 1520 // parent. The moved dependency retains its weight. 1521 for piter := parent; piter != nil; piter = piter.parent { 1522 if piter == st { 1523 parent.parent = st.parent 1524 break 1525 } 1526 } 1527 st.parent = parent 1528 if priority.Exclusive && (st.parent != nil || priority.StreamDep == 0) { 1529 for _, openStream := range streams { 1530 if openStream != st && openStream.parent == st.parent { 1531 openStream.parent = st 1532 } 1533 } 1534 } 1535 } 1536 1537 // resetPendingRequest zeros out all state related to a HEADERS frame 1538 // and its zero or more CONTINUATION frames sent to start a new 1539 // request. 1540 func (sc *serverConn) resetPendingRequest() { 1541 sc.serveG.check() 1542 sc.req = requestParam{} 1543 } 1544 1545 func (sc *serverConn) newWriterAndRequest() (*responseWriter, *http.Request, error) { 1546 sc.serveG.check() 1547 rp := &sc.req 1548 1549 if rp.invalidHeader { 1550 return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol} 1551 } 1552 1553 isConnect := rp.method == "CONNECT" 1554 if isConnect { 1555 if rp.path != "" || rp.scheme != "" || rp.authority == "" { 1556 return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol} 1557 } 1558 } else if rp.method == "" || rp.path == "" || 1559 (rp.scheme != "https" && rp.scheme != "http") { 1560 // See 8.1.2.6 Malformed Requests and Responses: 1561 // 1562 // Malformed requests or responses that are detected 1563 // MUST be treated as a stream error (Section 5.4.2) 1564 // of type PROTOCOL_ERROR." 1565 // 1566 // 8.1.2.3 Request Pseudo-Header Fields 1567 // "All HTTP/2 requests MUST include exactly one valid 1568 // value for the :method, :scheme, and :path 1569 // pseudo-header fields" 1570 return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol} 1571 } 1572 1573 bodyOpen := rp.stream.state == stateOpen 1574 if rp.method == "HEAD" && bodyOpen { 1575 // HEAD requests can't have bodies 1576 return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol} 1577 } 1578 var tlsState *tls.ConnectionState // nil if not scheme https 1579 1580 if rp.scheme == "https" { 1581 tlsState = sc.tlsState 1582 } 1583 authority := rp.authority 1584 if authority == "" { 1585 authority = rp.header.Get("Host") 1586 } 1587 needsContinue := rp.header.Get("Expect") == "100-continue" 1588 if needsContinue { 1589 rp.header.Del("Expect") 1590 } 1591 // Merge Cookie headers into one "; "-delimited value. 1592 if cookies := rp.header["Cookie"]; len(cookies) > 1 { 1593 rp.header.Set("Cookie", strings.Join(cookies, "; ")) 1594 } 1595 1596 // Setup Trailers 1597 var trailer http.Header 1598 for _, v := range rp.header["Trailer"] { 1599 for _, key := range strings.Split(v, ",") { 1600 key = http.CanonicalHeaderKey(strings.TrimSpace(key)) 1601 switch key { 1602 case "Transfer-Encoding", "Trailer", "Content-Length": 1603 // Bogus. (copy of http1 rules) 1604 // Ignore. 1605 default: 1606 if trailer == nil { 1607 trailer = make(http.Header) 1608 } 1609 trailer[key] = nil 1610 } 1611 } 1612 } 1613 delete(rp.header, "Trailer") 1614 1615 body := &requestBody{ 1616 conn: sc, 1617 stream: rp.stream, 1618 needsContinue: needsContinue, 1619 } 1620 var url_ *url.URL 1621 var requestURI string 1622 if isConnect { 1623 url_ = &url.URL{Host: rp.authority} 1624 requestURI = rp.authority // mimic HTTP/1 server behavior 1625 } else { 1626 var err error 1627 // TODO: handle asterisk '*' requests + test 1628 url_, err = url.ParseRequestURI(rp.path) 1629 if err != nil { 1630 return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol} 1631 } 1632 requestURI = rp.path 1633 } 1634 req := &http.Request{ 1635 Method: rp.method, 1636 URL: url_, 1637 RemoteAddr: sc.remoteAddrStr, 1638 Header: rp.header, 1639 RequestURI: requestURI, 1640 Proto: "HTTP/2.0", 1641 ProtoMajor: 2, 1642 ProtoMinor: 0, 1643 TLS: tlsState, 1644 Host: authority, 1645 Body: body, 1646 Trailer: trailer, 1647 } 1648 if bodyOpen { 1649 body.pipe = &pipe{ 1650 b: &fixedBuffer{buf: make([]byte, initialWindowSize)}, // TODO: garbage 1651 } 1652 1653 if vv, ok := rp.header["Content-Length"]; ok { 1654 req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64) 1655 } else { 1656 req.ContentLength = -1 1657 } 1658 } 1659 1660 rws := responseWriterStatePool.Get().(*responseWriterState) 1661 bwSave := rws.bw 1662 *rws = responseWriterState{} // zero all the fields 1663 rws.conn = sc 1664 rws.bw = bwSave 1665 rws.bw.Reset(chunkWriter{rws}) 1666 rws.stream = rp.stream 1667 rws.req = req 1668 rws.body = body 1669 1670 rw := &responseWriter{rws: rws} 1671 return rw, req, nil 1672 } 1673 1674 // Run on its own goroutine. 1675 func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) { 1676 didPanic := true 1677 defer func() { 1678 if didPanic { 1679 e := recover() 1680 // Same as net/http: 1681 const size = 64 << 10 1682 buf := make([]byte, size) 1683 buf = buf[:runtime.Stack(buf, false)] 1684 sc.writeFrameFromHandler(frameWriteMsg{ 1685 write: handlerPanicRST{rw.rws.stream.id}, 1686 stream: rw.rws.stream, 1687 }) 1688 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf) 1689 return 1690 } 1691 rw.handlerDone() 1692 }() 1693 handler(rw, req) 1694 didPanic = false 1695 } 1696 1697 func handleHeaderListTooLong(w http.ResponseWriter, r *http.Request) { 1698 // 10.5.1 Limits on Header Block Size: 1699 // .. "A server that receives a larger header block than it is 1700 // willing to handle can send an HTTP 431 (Request Header Fields Too 1701 // Large) status code" 1702 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 1703 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 1704 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 1705 } 1706 1707 // called from handler goroutines. 1708 // h may be nil. 1709 func (sc *serverConn) writeHeaders(st *stream, headerData *writeResHeaders) error { 1710 sc.serveG.checkNotOn() // NOT on 1711 var errc chan error 1712 if headerData.h != nil { 1713 // If there's a header map (which we don't own), so we have to block on 1714 // waiting for this frame to be written, so an http.Flush mid-handler 1715 // writes out the correct value of keys, before a handler later potentially 1716 // mutates it. 1717 errc = errChanPool.Get().(chan error) 1718 } 1719 if err := sc.writeFrameFromHandler(frameWriteMsg{ 1720 write: headerData, 1721 stream: st, 1722 done: errc, 1723 }); err != nil { 1724 return err 1725 } 1726 if errc != nil { 1727 select { 1728 case err := <-errc: 1729 errChanPool.Put(errc) 1730 return err 1731 case <-sc.doneServing: 1732 return errClientDisconnected 1733 case <-st.cw: 1734 return errStreamClosed 1735 } 1736 } 1737 return nil 1738 } 1739 1740 // called from handler goroutines. 1741 func (sc *serverConn) write100ContinueHeaders(st *stream) { 1742 sc.writeFrameFromHandler(frameWriteMsg{ 1743 write: write100ContinueHeadersFrame{st.id}, 1744 stream: st, 1745 }) 1746 } 1747 1748 // A bodyReadMsg tells the server loop that the http.Handler read n 1749 // bytes of the DATA from the client on the given stream. 1750 type bodyReadMsg struct { 1751 st *stream 1752 n int 1753 } 1754 1755 // called from handler goroutines. 1756 // Notes that the handler for the given stream ID read n bytes of its body 1757 // and schedules flow control tokens to be sent. 1758 func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int) { 1759 sc.serveG.checkNotOn() // NOT on 1760 select { 1761 case sc.bodyReadCh <- bodyReadMsg{st, n}: 1762 case <-sc.doneServing: 1763 } 1764 } 1765 1766 func (sc *serverConn) noteBodyRead(st *stream, n int) { 1767 sc.serveG.check() 1768 sc.sendWindowUpdate(nil, n) // conn-level 1769 if st.state != stateHalfClosedRemote && st.state != stateClosed { 1770 // Don't send this WINDOW_UPDATE if the stream is closed 1771 // remotely. 1772 sc.sendWindowUpdate(st, n) 1773 } 1774 } 1775 1776 // st may be nil for conn-level 1777 func (sc *serverConn) sendWindowUpdate(st *stream, n int) { 1778 sc.serveG.check() 1779 // "The legal range for the increment to the flow control 1780 // window is 1 to 2^31-1 (2,147,483,647) octets." 1781 // A Go Read call on 64-bit machines could in theory read 1782 // a larger Read than this. Very unlikely, but we handle it here 1783 // rather than elsewhere for now. 1784 const maxUint31 = 1<<31 - 1 1785 for n >= maxUint31 { 1786 sc.sendWindowUpdate32(st, maxUint31) 1787 n -= maxUint31 1788 } 1789 sc.sendWindowUpdate32(st, int32(n)) 1790 } 1791 1792 // st may be nil for conn-level 1793 func (sc *serverConn) sendWindowUpdate32(st *stream, n int32) { 1794 sc.serveG.check() 1795 if n == 0 { 1796 return 1797 } 1798 if n < 0 { 1799 panic("negative update") 1800 } 1801 var streamID uint32 1802 if st != nil { 1803 streamID = st.id 1804 } 1805 sc.writeFrame(frameWriteMsg{ 1806 write: writeWindowUpdate{streamID: streamID, n: uint32(n)}, 1807 stream: st, 1808 }) 1809 var ok bool 1810 if st == nil { 1811 ok = sc.inflow.add(n) 1812 } else { 1813 ok = st.inflow.add(n) 1814 } 1815 if !ok { 1816 panic("internal error; sent too many window updates without decrements?") 1817 } 1818 } 1819 1820 type requestBody struct { 1821 stream *stream 1822 conn *serverConn 1823 closed bool 1824 pipe *pipe // non-nil if we have a HTTP entity message body 1825 needsContinue bool // need to send a 100-continue 1826 } 1827 1828 func (b *requestBody) Close() error { 1829 if b.pipe != nil { 1830 b.pipe.CloseWithError(errClosedBody) 1831 } 1832 b.closed = true 1833 return nil 1834 } 1835 1836 func (b *requestBody) Read(p []byte) (n int, err error) { 1837 if b.needsContinue { 1838 b.needsContinue = false 1839 b.conn.write100ContinueHeaders(b.stream) 1840 } 1841 if b.pipe == nil { 1842 return 0, io.EOF 1843 } 1844 n, err = b.pipe.Read(p) 1845 if n > 0 { 1846 b.conn.noteBodyReadFromHandler(b.stream, n) 1847 } 1848 return 1849 } 1850 1851 // responseWriter is the http.ResponseWriter implementation. It's 1852 // intentionally small (1 pointer wide) to minimize garbage. The 1853 // responseWriterState pointer inside is zeroed at the end of a 1854 // request (in handlerDone) and calls on the responseWriter thereafter 1855 // simply crash (caller's mistake), but the much larger responseWriterState 1856 // and buffers are reused between multiple requests. 1857 type responseWriter struct { 1858 rws *responseWriterState 1859 } 1860 1861 // Optional http.ResponseWriter interfaces implemented. 1862 var ( 1863 _ http.CloseNotifier = (*responseWriter)(nil) 1864 _ http.Flusher = (*responseWriter)(nil) 1865 _ stringWriter = (*responseWriter)(nil) 1866 ) 1867 1868 type responseWriterState struct { 1869 // immutable within a request: 1870 stream *stream 1871 req *http.Request 1872 body *requestBody // to close at end of request, if DATA frames didn't 1873 conn *serverConn 1874 1875 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 1876 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 1877 1878 // mutated by http.Handler goroutine: 1879 handlerHeader http.Header // nil until called 1880 snapHeader http.Header // snapshot of handlerHeader at WriteHeader time 1881 trailers []string // set in writeChunk 1882 status int // status code passed to WriteHeader 1883 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 1884 sentHeader bool // have we sent the header frame? 1885 handlerDone bool // handler has finished 1886 1887 sentContentLen int64 // non-zero if handler set a Content-Length header 1888 wroteBytes int64 1889 1890 closeNotifierMu sync.Mutex // guards closeNotifierCh 1891 closeNotifierCh chan bool // nil until first used 1892 } 1893 1894 type chunkWriter struct{ rws *responseWriterState } 1895 1896 func (cw chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 1897 1898 func (rws *responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 } 1899 1900 // declareTrailer is called for each Trailer header when the 1901 // response header is written. It notes that a header will need to be 1902 // written in the trailers at the end of the response. 1903 func (rws *responseWriterState) declareTrailer(k string) { 1904 k = http.CanonicalHeaderKey(k) 1905 switch k { 1906 case "Transfer-Encoding", "Content-Length", "Trailer": 1907 // Forbidden by RFC 2616 14.40. 1908 return 1909 } 1910 rws.trailers = append(rws.trailers, k) 1911 } 1912 1913 // writeChunk writes chunks from the bufio.Writer. But because 1914 // bufio.Writer may bypass its chunking, sometimes p may be 1915 // arbitrarily large. 1916 // 1917 // writeChunk is also responsible (on the first chunk) for sending the 1918 // HEADER response. 1919 func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) { 1920 if !rws.wroteHeader { 1921 rws.writeHeader(200) 1922 } 1923 1924 isHeadResp := rws.req.Method == "HEAD" 1925 if !rws.sentHeader { 1926 rws.sentHeader = true 1927 var ctype, clen string 1928 if clen = rws.snapHeader.Get("Content-Length"); clen != "" { 1929 rws.snapHeader.Del("Content-Length") 1930 clen64, err := strconv.ParseInt(clen, 10, 64) 1931 if err == nil && clen64 >= 0 { 1932 rws.sentContentLen = clen64 1933 } else { 1934 clen = "" 1935 } 1936 } 1937 if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) { 1938 clen = strconv.Itoa(len(p)) 1939 } 1940 _, hasContentType := rws.snapHeader["Content-Type"] 1941 if !hasContentType && bodyAllowedForStatus(rws.status) { 1942 ctype = http.DetectContentType(p) 1943 } 1944 var date string 1945 if _, ok := rws.snapHeader["Date"]; !ok { 1946 // TODO(bradfitz): be faster here, like net/http? measure. 1947 date = time.Now().UTC().Format(http.TimeFormat) 1948 } 1949 1950 for _, v := range rws.snapHeader["Trailer"] { 1951 foreachHeaderElement(v, rws.declareTrailer) 1952 } 1953 1954 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp 1955 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{ 1956 streamID: rws.stream.id, 1957 httpResCode: rws.status, 1958 h: rws.snapHeader, 1959 endStream: endStream, 1960 contentType: ctype, 1961 contentLength: clen, 1962 date: date, 1963 }) 1964 if err != nil { 1965 return 0, err 1966 } 1967 if endStream { 1968 return 0, nil 1969 } 1970 } 1971 if isHeadResp { 1972 return len(p), nil 1973 } 1974 if len(p) == 0 && !rws.handlerDone { 1975 return 0, nil 1976 } 1977 1978 endStream := rws.handlerDone && !rws.hasTrailers() 1979 if len(p) > 0 || endStream { 1980 // only send a 0 byte DATA frame if we're ending the stream. 1981 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil { 1982 return 0, err 1983 } 1984 } 1985 1986 if rws.handlerDone && rws.hasTrailers() { 1987 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{ 1988 streamID: rws.stream.id, 1989 h: rws.handlerHeader, 1990 trailers: rws.trailers, 1991 endStream: true, 1992 }) 1993 return len(p), err 1994 } 1995 return len(p), nil 1996 } 1997 1998 func (w *responseWriter) Flush() { 1999 rws := w.rws 2000 if rws == nil { 2001 panic("Header called after Handler finished") 2002 } 2003 if rws.bw.Buffered() > 0 { 2004 if err := rws.bw.Flush(); err != nil { 2005 // Ignore the error. The frame writer already knows. 2006 return 2007 } 2008 } else { 2009 // The bufio.Writer won't call chunkWriter.Write 2010 // (writeChunk with zero bytes, so we have to do it 2011 // ourselves to force the HTTP response header and/or 2012 // final DATA frame (with END_STREAM) to be sent. 2013 rws.writeChunk(nil) 2014 } 2015 } 2016 2017 func (w *responseWriter) CloseNotify() <-chan bool { 2018 rws := w.rws 2019 if rws == nil { 2020 panic("CloseNotify called after Handler finished") 2021 } 2022 rws.closeNotifierMu.Lock() 2023 ch := rws.closeNotifierCh 2024 if ch == nil { 2025 ch = make(chan bool, 1) 2026 rws.closeNotifierCh = ch 2027 go func() { 2028 rws.stream.cw.Wait() // wait for close 2029 ch <- true 2030 }() 2031 } 2032 rws.closeNotifierMu.Unlock() 2033 return ch 2034 } 2035 2036 func (w *responseWriter) Header() http.Header { 2037 rws := w.rws 2038 if rws == nil { 2039 panic("Header called after Handler finished") 2040 } 2041 if rws.handlerHeader == nil { 2042 rws.handlerHeader = make(http.Header) 2043 } 2044 return rws.handlerHeader 2045 } 2046 2047 func (w *responseWriter) WriteHeader(code int) { 2048 rws := w.rws 2049 if rws == nil { 2050 panic("WriteHeader called after Handler finished") 2051 } 2052 rws.writeHeader(code) 2053 } 2054 2055 func (rws *responseWriterState) writeHeader(code int) { 2056 if !rws.wroteHeader { 2057 rws.wroteHeader = true 2058 rws.status = code 2059 if len(rws.handlerHeader) > 0 { 2060 rws.snapHeader = cloneHeader(rws.handlerHeader) 2061 } 2062 } 2063 } 2064 2065 func cloneHeader(h http.Header) http.Header { 2066 h2 := make(http.Header, len(h)) 2067 for k, vv := range h { 2068 vv2 := make([]string, len(vv)) 2069 copy(vv2, vv) 2070 h2[k] = vv2 2071 } 2072 return h2 2073 } 2074 2075 // The Life Of A Write is like this: 2076 // 2077 // * Handler calls w.Write or w.WriteString -> 2078 // * -> rws.bw (*bufio.Writer) -> 2079 // * (Handler migth call Flush) 2080 // * -> chunkWriter{rws} 2081 // * -> responseWriterState.writeChunk(p []byte) 2082 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 2083 func (w *responseWriter) Write(p []byte) (n int, err error) { 2084 return w.write(len(p), p, "") 2085 } 2086 2087 func (w *responseWriter) WriteString(s string) (n int, err error) { 2088 return w.write(len(s), nil, s) 2089 } 2090 2091 // either dataB or dataS is non-zero. 2092 func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 2093 rws := w.rws 2094 if rws == nil { 2095 panic("Write called after Handler finished") 2096 } 2097 if !rws.wroteHeader { 2098 w.WriteHeader(200) 2099 } 2100 if !bodyAllowedForStatus(rws.status) { 2101 return 0, http.ErrBodyNotAllowed 2102 } 2103 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set 2104 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen { 2105 // TODO: send a RST_STREAM 2106 return 0, errors.New("http2: handler wrote more than declared Content-Length") 2107 } 2108 2109 if dataB != nil { 2110 return rws.bw.Write(dataB) 2111 } else { 2112 return rws.bw.WriteString(dataS) 2113 } 2114 } 2115 2116 func (w *responseWriter) handlerDone() { 2117 rws := w.rws 2118 rws.handlerDone = true 2119 w.Flush() 2120 w.rws = nil 2121 responseWriterStatePool.Put(rws) 2122 } 2123 2124 // foreachHeaderElement splits v according to the "#rule" construction 2125 // in RFC 2616 section 2.1 and calls fn for each non-empty element. 2126 func foreachHeaderElement(v string, fn func(string)) { 2127 v = textproto.TrimString(v) 2128 if v == "" { 2129 return 2130 } 2131 if !strings.Contains(v, ",") { 2132 fn(v) 2133 return 2134 } 2135 for _, f := range strings.Split(v, ",") { 2136 if f = textproto.TrimString(f); f != "" { 2137 fn(f) 2138 } 2139 } 2140 }