github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/http/h2_bundle.go (about) 1 // Code generated by golang.org/x/tools/cmd/bundle. 2 //go:generate bundle -o h2_bundle.go -prefix http2 golang.org/x/net/http2 3 4 // Package http2 implements the HTTP/2 protocol. 5 // 6 // This package is low-level and intended to be used directly by very 7 // few people. Most users will use it indirectly through the automatic 8 // use by the net/http package (from Go 1.6 and later). 9 // For use in earlier Go versions see ConfigureServer. (Transport support 10 // requires Go 1.6 or later) 11 // 12 // See https://http2.github.io/ for more information on HTTP/2. 13 // 14 // See https://http2.golang.org/ for a test server running this code. 15 // 16 17 package http 18 19 import ( 20 "bufio" 21 "bytes" 22 "compress/gzip" 23 "context" 24 "crypto/tls" 25 "encoding/binary" 26 "errors" 27 "fmt" 28 "io" 29 "io/ioutil" 30 "log" 31 "net" 32 "net/http/httptrace" 33 "net/textproto" 34 "net/url" 35 "os" 36 "reflect" 37 "runtime" 38 "sort" 39 "strconv" 40 "strings" 41 "sync" 42 "time" 43 44 "golang.org/x/net/http2/hpack" 45 "golang.org/x/net/lex/httplex" 46 ) 47 48 // ClientConnPool manages a pool of HTTP/2 client connections. 49 type http2ClientConnPool interface { 50 GetClientConn(req *Request, addr string) (*http2ClientConn, error) 51 MarkDead(*http2ClientConn) 52 } 53 54 // clientConnPoolIdleCloser is the interface implemented by ClientConnPool 55 // implementations which can close their idle connections. 56 type http2clientConnPoolIdleCloser interface { 57 http2ClientConnPool 58 closeIdleConnections() 59 } 60 61 var ( 62 _ http2clientConnPoolIdleCloser = (*http2clientConnPool)(nil) 63 _ http2clientConnPoolIdleCloser = http2noDialClientConnPool{} 64 ) 65 66 // TODO: use singleflight for dialing and addConnCalls? 67 type http2clientConnPool struct { 68 t *http2Transport 69 70 mu sync.Mutex // TODO: maybe switch to RWMutex 71 // TODO: add support for sharing conns based on cert names 72 // (e.g. share conn for googleapis.com and appspot.com) 73 conns map[string][]*http2ClientConn // key is host:port 74 dialing map[string]*http2dialCall // currently in-flight dials 75 keys map[*http2ClientConn][]string 76 addConnCalls map[string]*http2addConnCall // in-flight addConnIfNeede calls 77 } 78 79 func (p *http2clientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) { 80 return p.getClientConn(req, addr, http2dialOnMiss) 81 } 82 83 const ( 84 http2dialOnMiss = true 85 http2noDialOnMiss = false 86 ) 87 88 func (p *http2clientConnPool) getClientConn(_ *Request, addr string, dialOnMiss bool) (*http2ClientConn, error) { 89 p.mu.Lock() 90 for _, cc := range p.conns[addr] { 91 if cc.CanTakeNewRequest() { 92 p.mu.Unlock() 93 return cc, nil 94 } 95 } 96 if !dialOnMiss { 97 p.mu.Unlock() 98 return nil, http2ErrNoCachedConn 99 } 100 call := p.getStartDialLocked(addr) 101 p.mu.Unlock() 102 <-call.done 103 return call.res, call.err 104 } 105 106 // dialCall is an in-flight Transport dial call to a host. 107 type http2dialCall struct { 108 p *http2clientConnPool 109 done chan struct{} // closed when done 110 res *http2ClientConn // valid after done is closed 111 err error // valid after done is closed 112 } 113 114 // requires p.mu is held. 115 func (p *http2clientConnPool) getStartDialLocked(addr string) *http2dialCall { 116 if call, ok := p.dialing[addr]; ok { 117 118 return call 119 } 120 call := &http2dialCall{p: p, done: make(chan struct{})} 121 if p.dialing == nil { 122 p.dialing = make(map[string]*http2dialCall) 123 } 124 p.dialing[addr] = call 125 go call.dial(addr) 126 return call 127 } 128 129 // run in its own goroutine. 130 func (c *http2dialCall) dial(addr string) { 131 c.res, c.err = c.p.t.dialClientConn(addr) 132 close(c.done) 133 134 c.p.mu.Lock() 135 delete(c.p.dialing, addr) 136 if c.err == nil { 137 c.p.addConnLocked(addr, c.res) 138 } 139 c.p.mu.Unlock() 140 } 141 142 // addConnIfNeeded makes a NewClientConn out of c if a connection for key doesn't 143 // already exist. It coalesces concurrent calls with the same key. 144 // This is used by the http1 Transport code when it creates a new connection. Because 145 // the http1 Transport doesn't de-dup TCP dials to outbound hosts (because it doesn't know 146 // the protocol), it can get into a situation where it has multiple TLS connections. 147 // This code decides which ones live or die. 148 // The return value used is whether c was used. 149 // c is never closed. 150 func (p *http2clientConnPool) addConnIfNeeded(key string, t *http2Transport, c *tls.Conn) (used bool, err error) { 151 p.mu.Lock() 152 for _, cc := range p.conns[key] { 153 if cc.CanTakeNewRequest() { 154 p.mu.Unlock() 155 return false, nil 156 } 157 } 158 call, dup := p.addConnCalls[key] 159 if !dup { 160 if p.addConnCalls == nil { 161 p.addConnCalls = make(map[string]*http2addConnCall) 162 } 163 call = &http2addConnCall{ 164 p: p, 165 done: make(chan struct{}), 166 } 167 p.addConnCalls[key] = call 168 go call.run(t, key, c) 169 } 170 p.mu.Unlock() 171 172 <-call.done 173 if call.err != nil { 174 return false, call.err 175 } 176 return !dup, nil 177 } 178 179 type http2addConnCall struct { 180 p *http2clientConnPool 181 done chan struct{} // closed when done 182 err error 183 } 184 185 func (c *http2addConnCall) run(t *http2Transport, key string, tc *tls.Conn) { 186 cc, err := t.NewClientConn(tc) 187 188 p := c.p 189 p.mu.Lock() 190 if err != nil { 191 c.err = err 192 } else { 193 p.addConnLocked(key, cc) 194 } 195 delete(p.addConnCalls, key) 196 p.mu.Unlock() 197 close(c.done) 198 } 199 200 func (p *http2clientConnPool) addConn(key string, cc *http2ClientConn) { 201 p.mu.Lock() 202 p.addConnLocked(key, cc) 203 p.mu.Unlock() 204 } 205 206 // p.mu must be held 207 func (p *http2clientConnPool) addConnLocked(key string, cc *http2ClientConn) { 208 for _, v := range p.conns[key] { 209 if v == cc { 210 return 211 } 212 } 213 if p.conns == nil { 214 p.conns = make(map[string][]*http2ClientConn) 215 } 216 if p.keys == nil { 217 p.keys = make(map[*http2ClientConn][]string) 218 } 219 p.conns[key] = append(p.conns[key], cc) 220 p.keys[cc] = append(p.keys[cc], key) 221 } 222 223 func (p *http2clientConnPool) MarkDead(cc *http2ClientConn) { 224 p.mu.Lock() 225 defer p.mu.Unlock() 226 for _, key := range p.keys[cc] { 227 vv, ok := p.conns[key] 228 if !ok { 229 continue 230 } 231 newList := http2filterOutClientConn(vv, cc) 232 if len(newList) > 0 { 233 p.conns[key] = newList 234 } else { 235 delete(p.conns, key) 236 } 237 } 238 delete(p.keys, cc) 239 } 240 241 func (p *http2clientConnPool) closeIdleConnections() { 242 p.mu.Lock() 243 defer p.mu.Unlock() 244 245 for _, vv := range p.conns { 246 for _, cc := range vv { 247 cc.closeIfIdle() 248 } 249 } 250 } 251 252 func http2filterOutClientConn(in []*http2ClientConn, exclude *http2ClientConn) []*http2ClientConn { 253 out := in[:0] 254 for _, v := range in { 255 if v != exclude { 256 out = append(out, v) 257 } 258 } 259 260 if len(in) != len(out) { 261 in[len(in)-1] = nil 262 } 263 return out 264 } 265 266 // noDialClientConnPool is an implementation of http2.ClientConnPool 267 // which never dials. We let the HTTP/1.1 client dial and use its TLS 268 // connection instead. 269 type http2noDialClientConnPool struct{ *http2clientConnPool } 270 271 func (p http2noDialClientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) { 272 return p.getClientConn(req, addr, http2noDialOnMiss) 273 } 274 275 func http2configureTransport(t1 *Transport) (*http2Transport, error) { 276 connPool := new(http2clientConnPool) 277 t2 := &http2Transport{ 278 ConnPool: http2noDialClientConnPool{connPool}, 279 t1: t1, 280 } 281 connPool.t = t2 282 if err := http2registerHTTPSProtocol(t1, http2noDialH2RoundTripper{t2}); err != nil { 283 return nil, err 284 } 285 if t1.TLSClientConfig == nil { 286 t1.TLSClientConfig = new(tls.Config) 287 } 288 if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "h2") { 289 t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...) 290 } 291 if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") { 292 t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1") 293 } 294 upgradeFn := func(authority string, c *tls.Conn) RoundTripper { 295 addr := http2authorityAddr("https", authority) 296 if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil { 297 go c.Close() 298 return http2erringRoundTripper{err} 299 } else if !used { 300 301 go c.Close() 302 } 303 return t2 304 } 305 if m := t1.TLSNextProto; len(m) == 0 { 306 t1.TLSNextProto = map[string]func(string, *tls.Conn) RoundTripper{ 307 "h2": upgradeFn, 308 } 309 } else { 310 m["h2"] = upgradeFn 311 } 312 return t2, nil 313 } 314 315 // registerHTTPSProtocol calls Transport.RegisterProtocol but 316 // convering panics into errors. 317 func http2registerHTTPSProtocol(t *Transport, rt RoundTripper) (err error) { 318 defer func() { 319 if e := recover(); e != nil { 320 err = fmt.Errorf("%v", e) 321 } 322 }() 323 t.RegisterProtocol("https", rt) 324 return nil 325 } 326 327 // noDialH2RoundTripper is a RoundTripper which only tries to complete the request 328 // if there's already has a cached connection to the host. 329 type http2noDialH2RoundTripper struct{ t *http2Transport } 330 331 func (rt http2noDialH2RoundTripper) RoundTrip(req *Request) (*Response, error) { 332 res, err := rt.t.RoundTrip(req) 333 if err == http2ErrNoCachedConn { 334 return nil, ErrSkipAltProtocol 335 } 336 return res, err 337 } 338 339 // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec. 340 type http2ErrCode uint32 341 342 const ( 343 http2ErrCodeNo http2ErrCode = 0x0 344 http2ErrCodeProtocol http2ErrCode = 0x1 345 http2ErrCodeInternal http2ErrCode = 0x2 346 http2ErrCodeFlowControl http2ErrCode = 0x3 347 http2ErrCodeSettingsTimeout http2ErrCode = 0x4 348 http2ErrCodeStreamClosed http2ErrCode = 0x5 349 http2ErrCodeFrameSize http2ErrCode = 0x6 350 http2ErrCodeRefusedStream http2ErrCode = 0x7 351 http2ErrCodeCancel http2ErrCode = 0x8 352 http2ErrCodeCompression http2ErrCode = 0x9 353 http2ErrCodeConnect http2ErrCode = 0xa 354 http2ErrCodeEnhanceYourCalm http2ErrCode = 0xb 355 http2ErrCodeInadequateSecurity http2ErrCode = 0xc 356 http2ErrCodeHTTP11Required http2ErrCode = 0xd 357 ) 358 359 var http2errCodeName = map[http2ErrCode]string{ 360 http2ErrCodeNo: "NO_ERROR", 361 http2ErrCodeProtocol: "PROTOCOL_ERROR", 362 http2ErrCodeInternal: "INTERNAL_ERROR", 363 http2ErrCodeFlowControl: "FLOW_CONTROL_ERROR", 364 http2ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT", 365 http2ErrCodeStreamClosed: "STREAM_CLOSED", 366 http2ErrCodeFrameSize: "FRAME_SIZE_ERROR", 367 http2ErrCodeRefusedStream: "REFUSED_STREAM", 368 http2ErrCodeCancel: "CANCEL", 369 http2ErrCodeCompression: "COMPRESSION_ERROR", 370 http2ErrCodeConnect: "CONNECT_ERROR", 371 http2ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM", 372 http2ErrCodeInadequateSecurity: "INADEQUATE_SECURITY", 373 http2ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED", 374 } 375 376 func (e http2ErrCode) String() string { 377 if s, ok := http2errCodeName[e]; ok { 378 return s 379 } 380 return fmt.Sprintf("unknown error code 0x%x", uint32(e)) 381 } 382 383 // ConnectionError is an error that results in the termination of the 384 // entire connection. 385 type http2ConnectionError http2ErrCode 386 387 func (e http2ConnectionError) Error() string { 388 return fmt.Sprintf("connection error: %s", http2ErrCode(e)) 389 } 390 391 // StreamError is an error that only affects one stream within an 392 // HTTP/2 connection. 393 type http2StreamError struct { 394 StreamID uint32 395 Code http2ErrCode 396 } 397 398 func (e http2StreamError) Error() string { 399 return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code) 400 } 401 402 // 6.9.1 The Flow Control Window 403 // "If a sender receives a WINDOW_UPDATE that causes a flow control 404 // window to exceed this maximum it MUST terminate either the stream 405 // or the connection, as appropriate. For streams, [...]; for the 406 // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code." 407 type http2goAwayFlowError struct{} 408 409 func (http2goAwayFlowError) Error() string { return "connection exceeded flow control window size" } 410 411 // Errors of this type are only returned by the frame parser functions 412 // and converted into ConnectionError(ErrCodeProtocol). 413 type http2connError struct { 414 Code http2ErrCode 415 Reason string 416 } 417 418 func (e http2connError) Error() string { 419 return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason) 420 } 421 422 type http2pseudoHeaderError string 423 424 func (e http2pseudoHeaderError) Error() string { 425 return fmt.Sprintf("invalid pseudo-header %q", string(e)) 426 } 427 428 type http2duplicatePseudoHeaderError string 429 430 func (e http2duplicatePseudoHeaderError) Error() string { 431 return fmt.Sprintf("duplicate pseudo-header %q", string(e)) 432 } 433 434 type http2headerFieldNameError string 435 436 func (e http2headerFieldNameError) Error() string { 437 return fmt.Sprintf("invalid header field name %q", string(e)) 438 } 439 440 type http2headerFieldValueError string 441 442 func (e http2headerFieldValueError) Error() string { 443 return fmt.Sprintf("invalid header field value %q", string(e)) 444 } 445 446 var ( 447 http2errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers") 448 http2errPseudoAfterRegular = errors.New("pseudo header field after regular") 449 ) 450 451 // fixedBuffer is an io.ReadWriter backed by a fixed size buffer. 452 // It never allocates, but moves old data as new data is written. 453 type http2fixedBuffer struct { 454 buf []byte 455 r, w int 456 } 457 458 var ( 459 http2errReadEmpty = errors.New("read from empty fixedBuffer") 460 http2errWriteFull = errors.New("write on full fixedBuffer") 461 ) 462 463 // Read copies bytes from the buffer into p. 464 // It is an error to read when no data is available. 465 func (b *http2fixedBuffer) Read(p []byte) (n int, err error) { 466 if b.r == b.w { 467 return 0, http2errReadEmpty 468 } 469 n = copy(p, b.buf[b.r:b.w]) 470 b.r += n 471 if b.r == b.w { 472 b.r = 0 473 b.w = 0 474 } 475 return n, nil 476 } 477 478 // Len returns the number of bytes of the unread portion of the buffer. 479 func (b *http2fixedBuffer) Len() int { 480 return b.w - b.r 481 } 482 483 // Write copies bytes from p into the buffer. 484 // It is an error to write more data than the buffer can hold. 485 func (b *http2fixedBuffer) Write(p []byte) (n int, err error) { 486 487 if b.r > 0 && len(p) > len(b.buf)-b.w { 488 copy(b.buf, b.buf[b.r:b.w]) 489 b.w -= b.r 490 b.r = 0 491 } 492 493 n = copy(b.buf[b.w:], p) 494 b.w += n 495 if n < len(p) { 496 err = http2errWriteFull 497 } 498 return n, err 499 } 500 501 // flow is the flow control window's size. 502 type http2flow struct { 503 // n is the number of DATA bytes we're allowed to send. 504 // A flow is kept both on a conn and a per-stream. 505 n int32 506 507 // conn points to the shared connection-level flow that is 508 // shared by all streams on that conn. It is nil for the flow 509 // that's on the conn directly. 510 conn *http2flow 511 } 512 513 func (f *http2flow) setConnFlow(cf *http2flow) { f.conn = cf } 514 515 func (f *http2flow) available() int32 { 516 n := f.n 517 if f.conn != nil && f.conn.n < n { 518 n = f.conn.n 519 } 520 return n 521 } 522 523 func (f *http2flow) take(n int32) { 524 if n > f.available() { 525 panic("internal error: took too much") 526 } 527 f.n -= n 528 if f.conn != nil { 529 f.conn.n -= n 530 } 531 } 532 533 // add adds n bytes (positive or negative) to the flow control window. 534 // It returns false if the sum would exceed 2^31-1. 535 func (f *http2flow) add(n int32) bool { 536 remain := (1<<31 - 1) - f.n 537 if n > remain { 538 return false 539 } 540 f.n += n 541 return true 542 } 543 544 const http2frameHeaderLen = 9 545 546 var http2padZeros = make([]byte, 255) // zeros for padding 547 548 // A FrameType is a registered frame type as defined in 549 // http://http2.github.io/http2-spec/#rfc.section.11.2 550 type http2FrameType uint8 551 552 const ( 553 http2FrameData http2FrameType = 0x0 554 http2FrameHeaders http2FrameType = 0x1 555 http2FramePriority http2FrameType = 0x2 556 http2FrameRSTStream http2FrameType = 0x3 557 http2FrameSettings http2FrameType = 0x4 558 http2FramePushPromise http2FrameType = 0x5 559 http2FramePing http2FrameType = 0x6 560 http2FrameGoAway http2FrameType = 0x7 561 http2FrameWindowUpdate http2FrameType = 0x8 562 http2FrameContinuation http2FrameType = 0x9 563 ) 564 565 var http2frameName = map[http2FrameType]string{ 566 http2FrameData: "DATA", 567 http2FrameHeaders: "HEADERS", 568 http2FramePriority: "PRIORITY", 569 http2FrameRSTStream: "RST_STREAM", 570 http2FrameSettings: "SETTINGS", 571 http2FramePushPromise: "PUSH_PROMISE", 572 http2FramePing: "PING", 573 http2FrameGoAway: "GOAWAY", 574 http2FrameWindowUpdate: "WINDOW_UPDATE", 575 http2FrameContinuation: "CONTINUATION", 576 } 577 578 func (t http2FrameType) String() string { 579 if s, ok := http2frameName[t]; ok { 580 return s 581 } 582 return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t)) 583 } 584 585 // Flags is a bitmask of HTTP/2 flags. 586 // The meaning of flags varies depending on the frame type. 587 type http2Flags uint8 588 589 // Has reports whether f contains all (0 or more) flags in v. 590 func (f http2Flags) Has(v http2Flags) bool { 591 return (f & v) == v 592 } 593 594 // Frame-specific FrameHeader flag bits. 595 const ( 596 // Data Frame 597 http2FlagDataEndStream http2Flags = 0x1 598 http2FlagDataPadded http2Flags = 0x8 599 600 // Headers Frame 601 http2FlagHeadersEndStream http2Flags = 0x1 602 http2FlagHeadersEndHeaders http2Flags = 0x4 603 http2FlagHeadersPadded http2Flags = 0x8 604 http2FlagHeadersPriority http2Flags = 0x20 605 606 // Settings Frame 607 http2FlagSettingsAck http2Flags = 0x1 608 609 // Ping Frame 610 http2FlagPingAck http2Flags = 0x1 611 612 // Continuation Frame 613 http2FlagContinuationEndHeaders http2Flags = 0x4 614 615 http2FlagPushPromiseEndHeaders http2Flags = 0x4 616 http2FlagPushPromisePadded http2Flags = 0x8 617 ) 618 619 var http2flagName = map[http2FrameType]map[http2Flags]string{ 620 http2FrameData: { 621 http2FlagDataEndStream: "END_STREAM", 622 http2FlagDataPadded: "PADDED", 623 }, 624 http2FrameHeaders: { 625 http2FlagHeadersEndStream: "END_STREAM", 626 http2FlagHeadersEndHeaders: "END_HEADERS", 627 http2FlagHeadersPadded: "PADDED", 628 http2FlagHeadersPriority: "PRIORITY", 629 }, 630 http2FrameSettings: { 631 http2FlagSettingsAck: "ACK", 632 }, 633 http2FramePing: { 634 http2FlagPingAck: "ACK", 635 }, 636 http2FrameContinuation: { 637 http2FlagContinuationEndHeaders: "END_HEADERS", 638 }, 639 http2FramePushPromise: { 640 http2FlagPushPromiseEndHeaders: "END_HEADERS", 641 http2FlagPushPromisePadded: "PADDED", 642 }, 643 } 644 645 // a frameParser parses a frame given its FrameHeader and payload 646 // bytes. The length of payload will always equal fh.Length (which 647 // might be 0). 648 type http2frameParser func(fh http2FrameHeader, payload []byte) (http2Frame, error) 649 650 var http2frameParsers = map[http2FrameType]http2frameParser{ 651 http2FrameData: http2parseDataFrame, 652 http2FrameHeaders: http2parseHeadersFrame, 653 http2FramePriority: http2parsePriorityFrame, 654 http2FrameRSTStream: http2parseRSTStreamFrame, 655 http2FrameSettings: http2parseSettingsFrame, 656 http2FramePushPromise: http2parsePushPromise, 657 http2FramePing: http2parsePingFrame, 658 http2FrameGoAway: http2parseGoAwayFrame, 659 http2FrameWindowUpdate: http2parseWindowUpdateFrame, 660 http2FrameContinuation: http2parseContinuationFrame, 661 } 662 663 func http2typeFrameParser(t http2FrameType) http2frameParser { 664 if f := http2frameParsers[t]; f != nil { 665 return f 666 } 667 return http2parseUnknownFrame 668 } 669 670 // A FrameHeader is the 9 byte header of all HTTP/2 frames. 671 // 672 // See http://http2.github.io/http2-spec/#FrameHeader 673 type http2FrameHeader struct { 674 valid bool // caller can access []byte fields in the Frame 675 676 // Type is the 1 byte frame type. There are ten standard frame 677 // types, but extension frame types may be written by WriteRawFrame 678 // and will be returned by ReadFrame (as UnknownFrame). 679 Type http2FrameType 680 681 // Flags are the 1 byte of 8 potential bit flags per frame. 682 // They are specific to the frame type. 683 Flags http2Flags 684 685 // Length is the length of the frame, not including the 9 byte header. 686 // The maximum size is one byte less than 16MB (uint24), but only 687 // frames up to 16KB are allowed without peer agreement. 688 Length uint32 689 690 // StreamID is which stream this frame is for. Certain frames 691 // are not stream-specific, in which case this field is 0. 692 StreamID uint32 693 } 694 695 // Header returns h. It exists so FrameHeaders can be embedded in other 696 // specific frame types and implement the Frame interface. 697 func (h http2FrameHeader) Header() http2FrameHeader { return h } 698 699 func (h http2FrameHeader) String() string { 700 var buf bytes.Buffer 701 buf.WriteString("[FrameHeader ") 702 h.writeDebug(&buf) 703 buf.WriteByte(']') 704 return buf.String() 705 } 706 707 func (h http2FrameHeader) writeDebug(buf *bytes.Buffer) { 708 buf.WriteString(h.Type.String()) 709 if h.Flags != 0 { 710 buf.WriteString(" flags=") 711 set := 0 712 for i := uint8(0); i < 8; i++ { 713 if h.Flags&(1<<i) == 0 { 714 continue 715 } 716 set++ 717 if set > 1 { 718 buf.WriteByte('|') 719 } 720 name := http2flagName[h.Type][http2Flags(1<<i)] 721 if name != "" { 722 buf.WriteString(name) 723 } else { 724 fmt.Fprintf(buf, "0x%x", 1<<i) 725 } 726 } 727 } 728 if h.StreamID != 0 { 729 fmt.Fprintf(buf, " stream=%d", h.StreamID) 730 } 731 fmt.Fprintf(buf, " len=%d", h.Length) 732 } 733 734 func (h *http2FrameHeader) checkValid() { 735 if !h.valid { 736 panic("Frame accessor called on non-owned Frame") 737 } 738 } 739 740 func (h *http2FrameHeader) invalidate() { h.valid = false } 741 742 // frame header bytes. 743 // Used only by ReadFrameHeader. 744 var http2fhBytes = sync.Pool{ 745 New: func() interface{} { 746 buf := make([]byte, http2frameHeaderLen) 747 return &buf 748 }, 749 } 750 751 // ReadFrameHeader reads 9 bytes from r and returns a FrameHeader. 752 // Most users should use Framer.ReadFrame instead. 753 func http2ReadFrameHeader(r io.Reader) (http2FrameHeader, error) { 754 bufp := http2fhBytes.Get().(*[]byte) 755 defer http2fhBytes.Put(bufp) 756 return http2readFrameHeader(*bufp, r) 757 } 758 759 func http2readFrameHeader(buf []byte, r io.Reader) (http2FrameHeader, error) { 760 _, err := io.ReadFull(r, buf[:http2frameHeaderLen]) 761 if err != nil { 762 return http2FrameHeader{}, err 763 } 764 return http2FrameHeader{ 765 Length: (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])), 766 Type: http2FrameType(buf[3]), 767 Flags: http2Flags(buf[4]), 768 StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1), 769 valid: true, 770 }, nil 771 } 772 773 // A Frame is the base interface implemented by all frame types. 774 // Callers will generally type-assert the specific frame type: 775 // *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc. 776 // 777 // Frames are only valid until the next call to Framer.ReadFrame. 778 type http2Frame interface { 779 Header() http2FrameHeader 780 781 // invalidate is called by Framer.ReadFrame to make this 782 // frame's buffers as being invalid, since the subsequent 783 // frame will reuse them. 784 invalidate() 785 } 786 787 // A Framer reads and writes Frames. 788 type http2Framer struct { 789 r io.Reader 790 lastFrame http2Frame 791 errDetail error 792 793 // lastHeaderStream is non-zero if the last frame was an 794 // unfinished HEADERS/CONTINUATION. 795 lastHeaderStream uint32 796 797 maxReadSize uint32 798 headerBuf [http2frameHeaderLen]byte 799 800 // TODO: let getReadBuf be configurable, and use a less memory-pinning 801 // allocator in server.go to minimize memory pinned for many idle conns. 802 // Will probably also need to make frame invalidation have a hook too. 803 getReadBuf func(size uint32) []byte 804 readBuf []byte // cache for default getReadBuf 805 806 maxWriteSize uint32 // zero means unlimited; TODO: implement 807 808 w io.Writer 809 wbuf []byte 810 811 // AllowIllegalWrites permits the Framer's Write methods to 812 // write frames that do not conform to the HTTP/2 spec. This 813 // permits using the Framer to test other HTTP/2 814 // implementations' conformance to the spec. 815 // If false, the Write methods will prefer to return an error 816 // rather than comply. 817 AllowIllegalWrites bool 818 819 // AllowIllegalReads permits the Framer's ReadFrame method 820 // to return non-compliant frames or frame orders. 821 // This is for testing and permits using the Framer to test 822 // other HTTP/2 implementations' conformance to the spec. 823 // It is not compatible with ReadMetaHeaders. 824 AllowIllegalReads bool 825 826 // ReadMetaHeaders if non-nil causes ReadFrame to merge 827 // HEADERS and CONTINUATION frames together and return 828 // MetaHeadersFrame instead. 829 ReadMetaHeaders *hpack.Decoder 830 831 // MaxHeaderListSize is the http2 MAX_HEADER_LIST_SIZE. 832 // It's used only if ReadMetaHeaders is set; 0 means a sane default 833 // (currently 16MB) 834 // If the limit is hit, MetaHeadersFrame.Truncated is set true. 835 MaxHeaderListSize uint32 836 837 logReads bool 838 839 debugFramer *http2Framer // only use for logging written writes 840 debugFramerBuf *bytes.Buffer 841 } 842 843 func (fr *http2Framer) maxHeaderListSize() uint32 { 844 if fr.MaxHeaderListSize == 0 { 845 return 16 << 20 846 } 847 return fr.MaxHeaderListSize 848 } 849 850 func (f *http2Framer) startWrite(ftype http2FrameType, flags http2Flags, streamID uint32) { 851 852 f.wbuf = append(f.wbuf[:0], 853 0, 854 0, 855 0, 856 byte(ftype), 857 byte(flags), 858 byte(streamID>>24), 859 byte(streamID>>16), 860 byte(streamID>>8), 861 byte(streamID)) 862 } 863 864 func (f *http2Framer) endWrite() error { 865 866 length := len(f.wbuf) - http2frameHeaderLen 867 if length >= (1 << 24) { 868 return http2ErrFrameTooLarge 869 } 870 _ = append(f.wbuf[:0], 871 byte(length>>16), 872 byte(length>>8), 873 byte(length)) 874 if http2logFrameWrites { 875 f.logWrite() 876 } 877 878 n, err := f.w.Write(f.wbuf) 879 if err == nil && n != len(f.wbuf) { 880 err = io.ErrShortWrite 881 } 882 return err 883 } 884 885 func (f *http2Framer) logWrite() { 886 if f.debugFramer == nil { 887 f.debugFramerBuf = new(bytes.Buffer) 888 f.debugFramer = http2NewFramer(nil, f.debugFramerBuf) 889 f.debugFramer.logReads = false 890 891 f.debugFramer.AllowIllegalReads = true 892 } 893 f.debugFramerBuf.Write(f.wbuf) 894 fr, err := f.debugFramer.ReadFrame() 895 if err != nil { 896 log.Printf("http2: Framer %p: failed to decode just-written frame", f) 897 return 898 } 899 log.Printf("http2: Framer %p: wrote %v", f, http2summarizeFrame(fr)) 900 } 901 902 func (f *http2Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) } 903 904 func (f *http2Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) } 905 906 func (f *http2Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) } 907 908 func (f *http2Framer) writeUint32(v uint32) { 909 f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 910 } 911 912 const ( 913 http2minMaxFrameSize = 1 << 14 914 http2maxFrameSize = 1<<24 - 1 915 ) 916 917 // NewFramer returns a Framer that writes frames to w and reads them from r. 918 func http2NewFramer(w io.Writer, r io.Reader) *http2Framer { 919 fr := &http2Framer{ 920 w: w, 921 r: r, 922 logReads: http2logFrameReads, 923 } 924 fr.getReadBuf = func(size uint32) []byte { 925 if cap(fr.readBuf) >= int(size) { 926 return fr.readBuf[:size] 927 } 928 fr.readBuf = make([]byte, size) 929 return fr.readBuf 930 } 931 fr.SetMaxReadFrameSize(http2maxFrameSize) 932 return fr 933 } 934 935 // SetMaxReadFrameSize sets the maximum size of a frame 936 // that will be read by a subsequent call to ReadFrame. 937 // It is the caller's responsibility to advertise this 938 // limit with a SETTINGS frame. 939 func (fr *http2Framer) SetMaxReadFrameSize(v uint32) { 940 if v > http2maxFrameSize { 941 v = http2maxFrameSize 942 } 943 fr.maxReadSize = v 944 } 945 946 // ErrorDetail returns a more detailed error of the last error 947 // returned by Framer.ReadFrame. For instance, if ReadFrame 948 // returns a StreamError with code PROTOCOL_ERROR, ErrorDetail 949 // will say exactly what was invalid. ErrorDetail is not guaranteed 950 // to return a non-nil value and like the rest of the http2 package, 951 // its return value is not protected by an API compatibility promise. 952 // ErrorDetail is reset after the next call to ReadFrame. 953 func (fr *http2Framer) ErrorDetail() error { 954 return fr.errDetail 955 } 956 957 // ErrFrameTooLarge is returned from Framer.ReadFrame when the peer 958 // sends a frame that is larger than declared with SetMaxReadFrameSize. 959 var http2ErrFrameTooLarge = errors.New("http2: frame too large") 960 961 // terminalReadFrameError reports whether err is an unrecoverable 962 // error from ReadFrame and no other frames should be read. 963 func http2terminalReadFrameError(err error) bool { 964 if _, ok := err.(http2StreamError); ok { 965 return false 966 } 967 return err != nil 968 } 969 970 // ReadFrame reads a single frame. The returned Frame is only valid 971 // until the next call to ReadFrame. 972 // 973 // If the frame is larger than previously set with SetMaxReadFrameSize, the 974 // returned error is ErrFrameTooLarge. Other errors may be of type 975 // ConnectionError, StreamError, or anything else from from the underlying 976 // reader. 977 func (fr *http2Framer) ReadFrame() (http2Frame, error) { 978 fr.errDetail = nil 979 if fr.lastFrame != nil { 980 fr.lastFrame.invalidate() 981 } 982 fh, err := http2readFrameHeader(fr.headerBuf[:], fr.r) 983 if err != nil { 984 return nil, err 985 } 986 if fh.Length > fr.maxReadSize { 987 return nil, http2ErrFrameTooLarge 988 } 989 payload := fr.getReadBuf(fh.Length) 990 if _, err := io.ReadFull(fr.r, payload); err != nil { 991 return nil, err 992 } 993 f, err := http2typeFrameParser(fh.Type)(fh, payload) 994 if err != nil { 995 if ce, ok := err.(http2connError); ok { 996 return nil, fr.connError(ce.Code, ce.Reason) 997 } 998 return nil, err 999 } 1000 if err := fr.checkFrameOrder(f); err != nil { 1001 return nil, err 1002 } 1003 if fr.logReads { 1004 log.Printf("http2: Framer %p: read %v", fr, http2summarizeFrame(f)) 1005 } 1006 if fh.Type == http2FrameHeaders && fr.ReadMetaHeaders != nil { 1007 return fr.readMetaFrame(f.(*http2HeadersFrame)) 1008 } 1009 return f, nil 1010 } 1011 1012 // connError returns ConnectionError(code) but first 1013 // stashes away a public reason to the caller can optionally relay it 1014 // to the peer before hanging up on them. This might help others debug 1015 // their implementations. 1016 func (fr *http2Framer) connError(code http2ErrCode, reason string) error { 1017 fr.errDetail = errors.New(reason) 1018 return http2ConnectionError(code) 1019 } 1020 1021 // checkFrameOrder reports an error if f is an invalid frame to return 1022 // next from ReadFrame. Mostly it checks whether HEADERS and 1023 // CONTINUATION frames are contiguous. 1024 func (fr *http2Framer) checkFrameOrder(f http2Frame) error { 1025 last := fr.lastFrame 1026 fr.lastFrame = f 1027 if fr.AllowIllegalReads { 1028 return nil 1029 } 1030 1031 fh := f.Header() 1032 if fr.lastHeaderStream != 0 { 1033 if fh.Type != http2FrameContinuation { 1034 return fr.connError(http2ErrCodeProtocol, 1035 fmt.Sprintf("got %s for stream %d; expected CONTINUATION following %s for stream %d", 1036 fh.Type, fh.StreamID, 1037 last.Header().Type, fr.lastHeaderStream)) 1038 } 1039 if fh.StreamID != fr.lastHeaderStream { 1040 return fr.connError(http2ErrCodeProtocol, 1041 fmt.Sprintf("got CONTINUATION for stream %d; expected stream %d", 1042 fh.StreamID, fr.lastHeaderStream)) 1043 } 1044 } else if fh.Type == http2FrameContinuation { 1045 return fr.connError(http2ErrCodeProtocol, fmt.Sprintf("unexpected CONTINUATION for stream %d", fh.StreamID)) 1046 } 1047 1048 switch fh.Type { 1049 case http2FrameHeaders, http2FrameContinuation: 1050 if fh.Flags.Has(http2FlagHeadersEndHeaders) { 1051 fr.lastHeaderStream = 0 1052 } else { 1053 fr.lastHeaderStream = fh.StreamID 1054 } 1055 } 1056 1057 return nil 1058 } 1059 1060 // A DataFrame conveys arbitrary, variable-length sequences of octets 1061 // associated with a stream. 1062 // See http://http2.github.io/http2-spec/#rfc.section.6.1 1063 type http2DataFrame struct { 1064 http2FrameHeader 1065 data []byte 1066 } 1067 1068 func (f *http2DataFrame) StreamEnded() bool { 1069 return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream) 1070 } 1071 1072 // Data returns the frame's data octets, not including any padding 1073 // size byte or padding suffix bytes. 1074 // The caller must not retain the returned memory past the next 1075 // call to ReadFrame. 1076 func (f *http2DataFrame) Data() []byte { 1077 f.checkValid() 1078 return f.data 1079 } 1080 1081 func http2parseDataFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 1082 if fh.StreamID == 0 { 1083 1084 return nil, http2connError{http2ErrCodeProtocol, "DATA frame with stream ID 0"} 1085 } 1086 f := &http2DataFrame{ 1087 http2FrameHeader: fh, 1088 } 1089 var padSize byte 1090 if fh.Flags.Has(http2FlagDataPadded) { 1091 var err error 1092 payload, padSize, err = http2readByte(payload) 1093 if err != nil { 1094 return nil, err 1095 } 1096 } 1097 if int(padSize) > len(payload) { 1098 1099 return nil, http2connError{http2ErrCodeProtocol, "pad size larger than data payload"} 1100 } 1101 f.data = payload[:len(payload)-int(padSize)] 1102 return f, nil 1103 } 1104 1105 var ( 1106 http2errStreamID = errors.New("invalid stream ID") 1107 http2errDepStreamID = errors.New("invalid dependent stream ID") 1108 ) 1109 1110 func http2validStreamIDOrZero(streamID uint32) bool { 1111 return streamID&(1<<31) == 0 1112 } 1113 1114 func http2validStreamID(streamID uint32) bool { 1115 return streamID != 0 && streamID&(1<<31) == 0 1116 } 1117 1118 // WriteData writes a DATA frame. 1119 // 1120 // It will perform exactly one Write to the underlying Writer. 1121 // It is the caller's responsibility to not call other Write methods concurrently. 1122 func (f *http2Framer) WriteData(streamID uint32, endStream bool, data []byte) error { 1123 1124 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1125 return http2errStreamID 1126 } 1127 var flags http2Flags 1128 if endStream { 1129 flags |= http2FlagDataEndStream 1130 } 1131 f.startWrite(http2FrameData, flags, streamID) 1132 f.wbuf = append(f.wbuf, data...) 1133 return f.endWrite() 1134 } 1135 1136 // A SettingsFrame conveys configuration parameters that affect how 1137 // endpoints communicate, such as preferences and constraints on peer 1138 // behavior. 1139 // 1140 // See http://http2.github.io/http2-spec/#SETTINGS 1141 type http2SettingsFrame struct { 1142 http2FrameHeader 1143 p []byte 1144 } 1145 1146 func http2parseSettingsFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1147 if fh.Flags.Has(http2FlagSettingsAck) && fh.Length > 0 { 1148 1149 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1150 } 1151 if fh.StreamID != 0 { 1152 1153 return nil, http2ConnectionError(http2ErrCodeProtocol) 1154 } 1155 if len(p)%6 != 0 { 1156 1157 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1158 } 1159 f := &http2SettingsFrame{http2FrameHeader: fh, p: p} 1160 if v, ok := f.Value(http2SettingInitialWindowSize); ok && v > (1<<31)-1 { 1161 1162 return nil, http2ConnectionError(http2ErrCodeFlowControl) 1163 } 1164 return f, nil 1165 } 1166 1167 func (f *http2SettingsFrame) IsAck() bool { 1168 return f.http2FrameHeader.Flags.Has(http2FlagSettingsAck) 1169 } 1170 1171 func (f *http2SettingsFrame) Value(s http2SettingID) (v uint32, ok bool) { 1172 f.checkValid() 1173 buf := f.p 1174 for len(buf) > 0 { 1175 settingID := http2SettingID(binary.BigEndian.Uint16(buf[:2])) 1176 if settingID == s { 1177 return binary.BigEndian.Uint32(buf[2:6]), true 1178 } 1179 buf = buf[6:] 1180 } 1181 return 0, false 1182 } 1183 1184 // ForeachSetting runs fn for each setting. 1185 // It stops and returns the first error. 1186 func (f *http2SettingsFrame) ForeachSetting(fn func(http2Setting) error) error { 1187 f.checkValid() 1188 buf := f.p 1189 for len(buf) > 0 { 1190 if err := fn(http2Setting{ 1191 http2SettingID(binary.BigEndian.Uint16(buf[:2])), 1192 binary.BigEndian.Uint32(buf[2:6]), 1193 }); err != nil { 1194 return err 1195 } 1196 buf = buf[6:] 1197 } 1198 return nil 1199 } 1200 1201 // WriteSettings writes a SETTINGS frame with zero or more settings 1202 // specified and the ACK bit not set. 1203 // 1204 // It will perform exactly one Write to the underlying Writer. 1205 // It is the caller's responsibility to not call other Write methods concurrently. 1206 func (f *http2Framer) WriteSettings(settings ...http2Setting) error { 1207 f.startWrite(http2FrameSettings, 0, 0) 1208 for _, s := range settings { 1209 f.writeUint16(uint16(s.ID)) 1210 f.writeUint32(s.Val) 1211 } 1212 return f.endWrite() 1213 } 1214 1215 // WriteSettings writes an empty SETTINGS frame with the ACK bit set. 1216 // 1217 // It will perform exactly one Write to the underlying Writer. 1218 // It is the caller's responsibility to not call other Write methods concurrently. 1219 func (f *http2Framer) WriteSettingsAck() error { 1220 f.startWrite(http2FrameSettings, http2FlagSettingsAck, 0) 1221 return f.endWrite() 1222 } 1223 1224 // A PingFrame is a mechanism for measuring a minimal round trip time 1225 // from the sender, as well as determining whether an idle connection 1226 // is still functional. 1227 // See http://http2.github.io/http2-spec/#rfc.section.6.7 1228 type http2PingFrame struct { 1229 http2FrameHeader 1230 Data [8]byte 1231 } 1232 1233 func (f *http2PingFrame) IsAck() bool { return f.Flags.Has(http2FlagPingAck) } 1234 1235 func http2parsePingFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 1236 if len(payload) != 8 { 1237 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1238 } 1239 if fh.StreamID != 0 { 1240 return nil, http2ConnectionError(http2ErrCodeProtocol) 1241 } 1242 f := &http2PingFrame{http2FrameHeader: fh} 1243 copy(f.Data[:], payload) 1244 return f, nil 1245 } 1246 1247 func (f *http2Framer) WritePing(ack bool, data [8]byte) error { 1248 var flags http2Flags 1249 if ack { 1250 flags = http2FlagPingAck 1251 } 1252 f.startWrite(http2FramePing, flags, 0) 1253 f.writeBytes(data[:]) 1254 return f.endWrite() 1255 } 1256 1257 // A GoAwayFrame informs the remote peer to stop creating streams on this connection. 1258 // See http://http2.github.io/http2-spec/#rfc.section.6.8 1259 type http2GoAwayFrame struct { 1260 http2FrameHeader 1261 LastStreamID uint32 1262 ErrCode http2ErrCode 1263 debugData []byte 1264 } 1265 1266 // DebugData returns any debug data in the GOAWAY frame. Its contents 1267 // are not defined. 1268 // The caller must not retain the returned memory past the next 1269 // call to ReadFrame. 1270 func (f *http2GoAwayFrame) DebugData() []byte { 1271 f.checkValid() 1272 return f.debugData 1273 } 1274 1275 func http2parseGoAwayFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1276 if fh.StreamID != 0 { 1277 return nil, http2ConnectionError(http2ErrCodeProtocol) 1278 } 1279 if len(p) < 8 { 1280 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1281 } 1282 return &http2GoAwayFrame{ 1283 http2FrameHeader: fh, 1284 LastStreamID: binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1), 1285 ErrCode: http2ErrCode(binary.BigEndian.Uint32(p[4:8])), 1286 debugData: p[8:], 1287 }, nil 1288 } 1289 1290 func (f *http2Framer) WriteGoAway(maxStreamID uint32, code http2ErrCode, debugData []byte) error { 1291 f.startWrite(http2FrameGoAway, 0, 0) 1292 f.writeUint32(maxStreamID & (1<<31 - 1)) 1293 f.writeUint32(uint32(code)) 1294 f.writeBytes(debugData) 1295 return f.endWrite() 1296 } 1297 1298 // An UnknownFrame is the frame type returned when the frame type is unknown 1299 // or no specific frame type parser exists. 1300 type http2UnknownFrame struct { 1301 http2FrameHeader 1302 p []byte 1303 } 1304 1305 // Payload returns the frame's payload (after the header). It is not 1306 // valid to call this method after a subsequent call to 1307 // Framer.ReadFrame, nor is it valid to retain the returned slice. 1308 // The memory is owned by the Framer and is invalidated when the next 1309 // frame is read. 1310 func (f *http2UnknownFrame) Payload() []byte { 1311 f.checkValid() 1312 return f.p 1313 } 1314 1315 func http2parseUnknownFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1316 return &http2UnknownFrame{fh, p}, nil 1317 } 1318 1319 // A WindowUpdateFrame is used to implement flow control. 1320 // See http://http2.github.io/http2-spec/#rfc.section.6.9 1321 type http2WindowUpdateFrame struct { 1322 http2FrameHeader 1323 Increment uint32 // never read with high bit set 1324 } 1325 1326 func http2parseWindowUpdateFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1327 if len(p) != 4 { 1328 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1329 } 1330 inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff 1331 if inc == 0 { 1332 1333 if fh.StreamID == 0 { 1334 return nil, http2ConnectionError(http2ErrCodeProtocol) 1335 } 1336 return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol} 1337 } 1338 return &http2WindowUpdateFrame{ 1339 http2FrameHeader: fh, 1340 Increment: inc, 1341 }, nil 1342 } 1343 1344 // WriteWindowUpdate writes a WINDOW_UPDATE frame. 1345 // The increment value must be between 1 and 2,147,483,647, inclusive. 1346 // If the Stream ID is zero, the window update applies to the 1347 // connection as a whole. 1348 func (f *http2Framer) WriteWindowUpdate(streamID, incr uint32) error { 1349 1350 if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites { 1351 return errors.New("illegal window increment value") 1352 } 1353 f.startWrite(http2FrameWindowUpdate, 0, streamID) 1354 f.writeUint32(incr) 1355 return f.endWrite() 1356 } 1357 1358 // A HeadersFrame is used to open a stream and additionally carries a 1359 // header block fragment. 1360 type http2HeadersFrame struct { 1361 http2FrameHeader 1362 1363 // Priority is set if FlagHeadersPriority is set in the FrameHeader. 1364 Priority http2PriorityParam 1365 1366 headerFragBuf []byte // not owned 1367 } 1368 1369 func (f *http2HeadersFrame) HeaderBlockFragment() []byte { 1370 f.checkValid() 1371 return f.headerFragBuf 1372 } 1373 1374 func (f *http2HeadersFrame) HeadersEnded() bool { 1375 return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndHeaders) 1376 } 1377 1378 func (f *http2HeadersFrame) StreamEnded() bool { 1379 return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndStream) 1380 } 1381 1382 func (f *http2HeadersFrame) HasPriority() bool { 1383 return f.http2FrameHeader.Flags.Has(http2FlagHeadersPriority) 1384 } 1385 1386 func http2parseHeadersFrame(fh http2FrameHeader, p []byte) (_ http2Frame, err error) { 1387 hf := &http2HeadersFrame{ 1388 http2FrameHeader: fh, 1389 } 1390 if fh.StreamID == 0 { 1391 1392 return nil, http2connError{http2ErrCodeProtocol, "HEADERS frame with stream ID 0"} 1393 } 1394 var padLength uint8 1395 if fh.Flags.Has(http2FlagHeadersPadded) { 1396 if p, padLength, err = http2readByte(p); err != nil { 1397 return 1398 } 1399 } 1400 if fh.Flags.Has(http2FlagHeadersPriority) { 1401 var v uint32 1402 p, v, err = http2readUint32(p) 1403 if err != nil { 1404 return nil, err 1405 } 1406 hf.Priority.StreamDep = v & 0x7fffffff 1407 hf.Priority.Exclusive = (v != hf.Priority.StreamDep) 1408 p, hf.Priority.Weight, err = http2readByte(p) 1409 if err != nil { 1410 return nil, err 1411 } 1412 } 1413 if len(p)-int(padLength) <= 0 { 1414 return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol} 1415 } 1416 hf.headerFragBuf = p[:len(p)-int(padLength)] 1417 return hf, nil 1418 } 1419 1420 // HeadersFrameParam are the parameters for writing a HEADERS frame. 1421 type http2HeadersFrameParam struct { 1422 // StreamID is the required Stream ID to initiate. 1423 StreamID uint32 1424 // BlockFragment is part (or all) of a Header Block. 1425 BlockFragment []byte 1426 1427 // EndStream indicates that the header block is the last that 1428 // the endpoint will send for the identified stream. Setting 1429 // this flag causes the stream to enter one of "half closed" 1430 // states. 1431 EndStream bool 1432 1433 // EndHeaders indicates that this frame contains an entire 1434 // header block and is not followed by any 1435 // CONTINUATION frames. 1436 EndHeaders bool 1437 1438 // PadLength is the optional number of bytes of zeros to add 1439 // to this frame. 1440 PadLength uint8 1441 1442 // Priority, if non-zero, includes stream priority information 1443 // in the HEADER frame. 1444 Priority http2PriorityParam 1445 } 1446 1447 // WriteHeaders writes a single HEADERS frame. 1448 // 1449 // This is a low-level header writing method. Encoding headers and 1450 // splitting them into any necessary CONTINUATION frames is handled 1451 // elsewhere. 1452 // 1453 // It will perform exactly one Write to the underlying Writer. 1454 // It is the caller's responsibility to not call other Write methods concurrently. 1455 func (f *http2Framer) WriteHeaders(p http2HeadersFrameParam) error { 1456 if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites { 1457 return http2errStreamID 1458 } 1459 var flags http2Flags 1460 if p.PadLength != 0 { 1461 flags |= http2FlagHeadersPadded 1462 } 1463 if p.EndStream { 1464 flags |= http2FlagHeadersEndStream 1465 } 1466 if p.EndHeaders { 1467 flags |= http2FlagHeadersEndHeaders 1468 } 1469 if !p.Priority.IsZero() { 1470 flags |= http2FlagHeadersPriority 1471 } 1472 f.startWrite(http2FrameHeaders, flags, p.StreamID) 1473 if p.PadLength != 0 { 1474 f.writeByte(p.PadLength) 1475 } 1476 if !p.Priority.IsZero() { 1477 v := p.Priority.StreamDep 1478 if !http2validStreamIDOrZero(v) && !f.AllowIllegalWrites { 1479 return http2errDepStreamID 1480 } 1481 if p.Priority.Exclusive { 1482 v |= 1 << 31 1483 } 1484 f.writeUint32(v) 1485 f.writeByte(p.Priority.Weight) 1486 } 1487 f.wbuf = append(f.wbuf, p.BlockFragment...) 1488 f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...) 1489 return f.endWrite() 1490 } 1491 1492 // A PriorityFrame specifies the sender-advised priority of a stream. 1493 // See http://http2.github.io/http2-spec/#rfc.section.6.3 1494 type http2PriorityFrame struct { 1495 http2FrameHeader 1496 http2PriorityParam 1497 } 1498 1499 // PriorityParam are the stream prioritzation parameters. 1500 type http2PriorityParam struct { 1501 // StreamDep is a 31-bit stream identifier for the 1502 // stream that this stream depends on. Zero means no 1503 // dependency. 1504 StreamDep uint32 1505 1506 // Exclusive is whether the dependency is exclusive. 1507 Exclusive bool 1508 1509 // Weight is the stream's zero-indexed weight. It should be 1510 // set together with StreamDep, or neither should be set. Per 1511 // the spec, "Add one to the value to obtain a weight between 1512 // 1 and 256." 1513 Weight uint8 1514 } 1515 1516 func (p http2PriorityParam) IsZero() bool { 1517 return p == http2PriorityParam{} 1518 } 1519 1520 func http2parsePriorityFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 1521 if fh.StreamID == 0 { 1522 return nil, http2connError{http2ErrCodeProtocol, "PRIORITY frame with stream ID 0"} 1523 } 1524 if len(payload) != 5 { 1525 return nil, http2connError{http2ErrCodeFrameSize, fmt.Sprintf("PRIORITY frame payload size was %d; want 5", len(payload))} 1526 } 1527 v := binary.BigEndian.Uint32(payload[:4]) 1528 streamID := v & 0x7fffffff 1529 return &http2PriorityFrame{ 1530 http2FrameHeader: fh, 1531 http2PriorityParam: http2PriorityParam{ 1532 Weight: payload[4], 1533 StreamDep: streamID, 1534 Exclusive: streamID != v, 1535 }, 1536 }, nil 1537 } 1538 1539 // WritePriority writes a PRIORITY frame. 1540 // 1541 // It will perform exactly one Write to the underlying Writer. 1542 // It is the caller's responsibility to not call other Write methods concurrently. 1543 func (f *http2Framer) WritePriority(streamID uint32, p http2PriorityParam) error { 1544 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1545 return http2errStreamID 1546 } 1547 if !http2validStreamIDOrZero(p.StreamDep) { 1548 return http2errDepStreamID 1549 } 1550 f.startWrite(http2FramePriority, 0, streamID) 1551 v := p.StreamDep 1552 if p.Exclusive { 1553 v |= 1 << 31 1554 } 1555 f.writeUint32(v) 1556 f.writeByte(p.Weight) 1557 return f.endWrite() 1558 } 1559 1560 // A RSTStreamFrame allows for abnormal termination of a stream. 1561 // See http://http2.github.io/http2-spec/#rfc.section.6.4 1562 type http2RSTStreamFrame struct { 1563 http2FrameHeader 1564 ErrCode http2ErrCode 1565 } 1566 1567 func http2parseRSTStreamFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1568 if len(p) != 4 { 1569 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1570 } 1571 if fh.StreamID == 0 { 1572 return nil, http2ConnectionError(http2ErrCodeProtocol) 1573 } 1574 return &http2RSTStreamFrame{fh, http2ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil 1575 } 1576 1577 // WriteRSTStream writes a RST_STREAM frame. 1578 // 1579 // It will perform exactly one Write to the underlying Writer. 1580 // It is the caller's responsibility to not call other Write methods concurrently. 1581 func (f *http2Framer) WriteRSTStream(streamID uint32, code http2ErrCode) error { 1582 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1583 return http2errStreamID 1584 } 1585 f.startWrite(http2FrameRSTStream, 0, streamID) 1586 f.writeUint32(uint32(code)) 1587 return f.endWrite() 1588 } 1589 1590 // A ContinuationFrame is used to continue a sequence of header block fragments. 1591 // See http://http2.github.io/http2-spec/#rfc.section.6.10 1592 type http2ContinuationFrame struct { 1593 http2FrameHeader 1594 headerFragBuf []byte 1595 } 1596 1597 func http2parseContinuationFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1598 if fh.StreamID == 0 { 1599 return nil, http2connError{http2ErrCodeProtocol, "CONTINUATION frame with stream ID 0"} 1600 } 1601 return &http2ContinuationFrame{fh, p}, nil 1602 } 1603 1604 func (f *http2ContinuationFrame) HeaderBlockFragment() []byte { 1605 f.checkValid() 1606 return f.headerFragBuf 1607 } 1608 1609 func (f *http2ContinuationFrame) HeadersEnded() bool { 1610 return f.http2FrameHeader.Flags.Has(http2FlagContinuationEndHeaders) 1611 } 1612 1613 // WriteContinuation writes a CONTINUATION frame. 1614 // 1615 // It will perform exactly one Write to the underlying Writer. 1616 // It is the caller's responsibility to not call other Write methods concurrently. 1617 func (f *http2Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error { 1618 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1619 return http2errStreamID 1620 } 1621 var flags http2Flags 1622 if endHeaders { 1623 flags |= http2FlagContinuationEndHeaders 1624 } 1625 f.startWrite(http2FrameContinuation, flags, streamID) 1626 f.wbuf = append(f.wbuf, headerBlockFragment...) 1627 return f.endWrite() 1628 } 1629 1630 // A PushPromiseFrame is used to initiate a server stream. 1631 // See http://http2.github.io/http2-spec/#rfc.section.6.6 1632 type http2PushPromiseFrame struct { 1633 http2FrameHeader 1634 PromiseID uint32 1635 headerFragBuf []byte // not owned 1636 } 1637 1638 func (f *http2PushPromiseFrame) HeaderBlockFragment() []byte { 1639 f.checkValid() 1640 return f.headerFragBuf 1641 } 1642 1643 func (f *http2PushPromiseFrame) HeadersEnded() bool { 1644 return f.http2FrameHeader.Flags.Has(http2FlagPushPromiseEndHeaders) 1645 } 1646 1647 func http2parsePushPromise(fh http2FrameHeader, p []byte) (_ http2Frame, err error) { 1648 pp := &http2PushPromiseFrame{ 1649 http2FrameHeader: fh, 1650 } 1651 if pp.StreamID == 0 { 1652 1653 return nil, http2ConnectionError(http2ErrCodeProtocol) 1654 } 1655 // The PUSH_PROMISE frame includes optional padding. 1656 // Padding fields and flags are identical to those defined for DATA frames 1657 var padLength uint8 1658 if fh.Flags.Has(http2FlagPushPromisePadded) { 1659 if p, padLength, err = http2readByte(p); err != nil { 1660 return 1661 } 1662 } 1663 1664 p, pp.PromiseID, err = http2readUint32(p) 1665 if err != nil { 1666 return 1667 } 1668 pp.PromiseID = pp.PromiseID & (1<<31 - 1) 1669 1670 if int(padLength) > len(p) { 1671 1672 return nil, http2ConnectionError(http2ErrCodeProtocol) 1673 } 1674 pp.headerFragBuf = p[:len(p)-int(padLength)] 1675 return pp, nil 1676 } 1677 1678 // PushPromiseParam are the parameters for writing a PUSH_PROMISE frame. 1679 type http2PushPromiseParam struct { 1680 // StreamID is the required Stream ID to initiate. 1681 StreamID uint32 1682 1683 // PromiseID is the required Stream ID which this 1684 // Push Promises 1685 PromiseID uint32 1686 1687 // BlockFragment is part (or all) of a Header Block. 1688 BlockFragment []byte 1689 1690 // EndHeaders indicates that this frame contains an entire 1691 // header block and is not followed by any 1692 // CONTINUATION frames. 1693 EndHeaders bool 1694 1695 // PadLength is the optional number of bytes of zeros to add 1696 // to this frame. 1697 PadLength uint8 1698 } 1699 1700 // WritePushPromise writes a single PushPromise Frame. 1701 // 1702 // As with Header Frames, This is the low level call for writing 1703 // individual frames. Continuation frames are handled elsewhere. 1704 // 1705 // It will perform exactly one Write to the underlying Writer. 1706 // It is the caller's responsibility to not call other Write methods concurrently. 1707 func (f *http2Framer) WritePushPromise(p http2PushPromiseParam) error { 1708 if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites { 1709 return http2errStreamID 1710 } 1711 var flags http2Flags 1712 if p.PadLength != 0 { 1713 flags |= http2FlagPushPromisePadded 1714 } 1715 if p.EndHeaders { 1716 flags |= http2FlagPushPromiseEndHeaders 1717 } 1718 f.startWrite(http2FramePushPromise, flags, p.StreamID) 1719 if p.PadLength != 0 { 1720 f.writeByte(p.PadLength) 1721 } 1722 if !http2validStreamID(p.PromiseID) && !f.AllowIllegalWrites { 1723 return http2errStreamID 1724 } 1725 f.writeUint32(p.PromiseID) 1726 f.wbuf = append(f.wbuf, p.BlockFragment...) 1727 f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...) 1728 return f.endWrite() 1729 } 1730 1731 // WriteRawFrame writes a raw frame. This can be used to write 1732 // extension frames unknown to this package. 1733 func (f *http2Framer) WriteRawFrame(t http2FrameType, flags http2Flags, streamID uint32, payload []byte) error { 1734 f.startWrite(t, flags, streamID) 1735 f.writeBytes(payload) 1736 return f.endWrite() 1737 } 1738 1739 func http2readByte(p []byte) (remain []byte, b byte, err error) { 1740 if len(p) == 0 { 1741 return nil, 0, io.ErrUnexpectedEOF 1742 } 1743 return p[1:], p[0], nil 1744 } 1745 1746 func http2readUint32(p []byte) (remain []byte, v uint32, err error) { 1747 if len(p) < 4 { 1748 return nil, 0, io.ErrUnexpectedEOF 1749 } 1750 return p[4:], binary.BigEndian.Uint32(p[:4]), nil 1751 } 1752 1753 type http2streamEnder interface { 1754 StreamEnded() bool 1755 } 1756 1757 type http2headersEnder interface { 1758 HeadersEnded() bool 1759 } 1760 1761 type http2headersOrContinuation interface { 1762 http2headersEnder 1763 HeaderBlockFragment() []byte 1764 } 1765 1766 // A MetaHeadersFrame is the representation of one HEADERS frame and 1767 // zero or more contiguous CONTINUATION frames and the decoding of 1768 // their HPACK-encoded contents. 1769 // 1770 // This type of frame does not appear on the wire and is only returned 1771 // by the Framer when Framer.ReadMetaHeaders is set. 1772 type http2MetaHeadersFrame struct { 1773 *http2HeadersFrame 1774 1775 // Fields are the fields contained in the HEADERS and 1776 // CONTINUATION frames. The underlying slice is owned by the 1777 // Framer and must not be retained after the next call to 1778 // ReadFrame. 1779 // 1780 // Fields are guaranteed to be in the correct http2 order and 1781 // not have unknown pseudo header fields or invalid header 1782 // field names or values. Required pseudo header fields may be 1783 // missing, however. Use the MetaHeadersFrame.Pseudo accessor 1784 // method access pseudo headers. 1785 Fields []hpack.HeaderField 1786 1787 // Truncated is whether the max header list size limit was hit 1788 // and Fields is incomplete. The hpack decoder state is still 1789 // valid, however. 1790 Truncated bool 1791 } 1792 1793 // PseudoValue returns the given pseudo header field's value. 1794 // The provided pseudo field should not contain the leading colon. 1795 func (mh *http2MetaHeadersFrame) PseudoValue(pseudo string) string { 1796 for _, hf := range mh.Fields { 1797 if !hf.IsPseudo() { 1798 return "" 1799 } 1800 if hf.Name[1:] == pseudo { 1801 return hf.Value 1802 } 1803 } 1804 return "" 1805 } 1806 1807 // RegularFields returns the regular (non-pseudo) header fields of mh. 1808 // The caller does not own the returned slice. 1809 func (mh *http2MetaHeadersFrame) RegularFields() []hpack.HeaderField { 1810 for i, hf := range mh.Fields { 1811 if !hf.IsPseudo() { 1812 return mh.Fields[i:] 1813 } 1814 } 1815 return nil 1816 } 1817 1818 // PseudoFields returns the pseudo header fields of mh. 1819 // The caller does not own the returned slice. 1820 func (mh *http2MetaHeadersFrame) PseudoFields() []hpack.HeaderField { 1821 for i, hf := range mh.Fields { 1822 if !hf.IsPseudo() { 1823 return mh.Fields[:i] 1824 } 1825 } 1826 return mh.Fields 1827 } 1828 1829 func (mh *http2MetaHeadersFrame) checkPseudos() error { 1830 var isRequest, isResponse bool 1831 pf := mh.PseudoFields() 1832 for i, hf := range pf { 1833 switch hf.Name { 1834 case ":method", ":path", ":scheme", ":authority": 1835 isRequest = true 1836 case ":status": 1837 isResponse = true 1838 default: 1839 return http2pseudoHeaderError(hf.Name) 1840 } 1841 1842 for _, hf2 := range pf[:i] { 1843 if hf.Name == hf2.Name { 1844 return http2duplicatePseudoHeaderError(hf.Name) 1845 } 1846 } 1847 } 1848 if isRequest && isResponse { 1849 return http2errMixPseudoHeaderTypes 1850 } 1851 return nil 1852 } 1853 1854 func (fr *http2Framer) maxHeaderStringLen() int { 1855 v := fr.maxHeaderListSize() 1856 if uint32(int(v)) == v { 1857 return int(v) 1858 } 1859 1860 return 0 1861 } 1862 1863 // readMetaFrame returns 0 or more CONTINUATION frames from fr and 1864 // merge them into into the provided hf and returns a MetaHeadersFrame 1865 // with the decoded hpack values. 1866 func (fr *http2Framer) readMetaFrame(hf *http2HeadersFrame) (*http2MetaHeadersFrame, error) { 1867 if fr.AllowIllegalReads { 1868 return nil, errors.New("illegal use of AllowIllegalReads with ReadMetaHeaders") 1869 } 1870 mh := &http2MetaHeadersFrame{ 1871 http2HeadersFrame: hf, 1872 } 1873 var remainSize = fr.maxHeaderListSize() 1874 var sawRegular bool 1875 1876 var invalid error // pseudo header field errors 1877 hdec := fr.ReadMetaHeaders 1878 hdec.SetEmitEnabled(true) 1879 hdec.SetMaxStringLength(fr.maxHeaderStringLen()) 1880 hdec.SetEmitFunc(func(hf hpack.HeaderField) { 1881 if !httplex.ValidHeaderFieldValue(hf.Value) { 1882 invalid = http2headerFieldValueError(hf.Value) 1883 } 1884 isPseudo := strings.HasPrefix(hf.Name, ":") 1885 if isPseudo { 1886 if sawRegular { 1887 invalid = http2errPseudoAfterRegular 1888 } 1889 } else { 1890 sawRegular = true 1891 if !http2validWireHeaderFieldName(hf.Name) { 1892 invalid = http2headerFieldNameError(hf.Name) 1893 } 1894 } 1895 1896 if invalid != nil { 1897 hdec.SetEmitEnabled(false) 1898 return 1899 } 1900 1901 size := hf.Size() 1902 if size > remainSize { 1903 hdec.SetEmitEnabled(false) 1904 mh.Truncated = true 1905 return 1906 } 1907 remainSize -= size 1908 1909 mh.Fields = append(mh.Fields, hf) 1910 }) 1911 1912 defer hdec.SetEmitFunc(func(hf hpack.HeaderField) {}) 1913 1914 var hc http2headersOrContinuation = hf 1915 for { 1916 frag := hc.HeaderBlockFragment() 1917 if _, err := hdec.Write(frag); err != nil { 1918 return nil, http2ConnectionError(http2ErrCodeCompression) 1919 } 1920 1921 if hc.HeadersEnded() { 1922 break 1923 } 1924 if f, err := fr.ReadFrame(); err != nil { 1925 return nil, err 1926 } else { 1927 hc = f.(*http2ContinuationFrame) 1928 } 1929 } 1930 1931 mh.http2HeadersFrame.headerFragBuf = nil 1932 mh.http2HeadersFrame.invalidate() 1933 1934 if err := hdec.Close(); err != nil { 1935 return nil, http2ConnectionError(http2ErrCodeCompression) 1936 } 1937 if invalid != nil { 1938 fr.errDetail = invalid 1939 return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol} 1940 } 1941 if err := mh.checkPseudos(); err != nil { 1942 fr.errDetail = err 1943 return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol} 1944 } 1945 return mh, nil 1946 } 1947 1948 func http2summarizeFrame(f http2Frame) string { 1949 var buf bytes.Buffer 1950 f.Header().writeDebug(&buf) 1951 switch f := f.(type) { 1952 case *http2SettingsFrame: 1953 n := 0 1954 f.ForeachSetting(func(s http2Setting) error { 1955 n++ 1956 if n == 1 { 1957 buf.WriteString(", settings:") 1958 } 1959 fmt.Fprintf(&buf, " %v=%v,", s.ID, s.Val) 1960 return nil 1961 }) 1962 if n > 0 { 1963 buf.Truncate(buf.Len() - 1) 1964 } 1965 case *http2DataFrame: 1966 data := f.Data() 1967 const max = 256 1968 if len(data) > max { 1969 data = data[:max] 1970 } 1971 fmt.Fprintf(&buf, " data=%q", data) 1972 if len(f.Data()) > max { 1973 fmt.Fprintf(&buf, " (%d bytes omitted)", len(f.Data())-max) 1974 } 1975 case *http2WindowUpdateFrame: 1976 if f.StreamID == 0 { 1977 buf.WriteString(" (conn)") 1978 } 1979 fmt.Fprintf(&buf, " incr=%v", f.Increment) 1980 case *http2PingFrame: 1981 fmt.Fprintf(&buf, " ping=%q", f.Data[:]) 1982 case *http2GoAwayFrame: 1983 fmt.Fprintf(&buf, " LastStreamID=%v ErrCode=%v Debug=%q", 1984 f.LastStreamID, f.ErrCode, f.debugData) 1985 case *http2RSTStreamFrame: 1986 fmt.Fprintf(&buf, " ErrCode=%v", f.ErrCode) 1987 } 1988 return buf.String() 1989 } 1990 1991 func http2transportExpectContinueTimeout(t1 *Transport) time.Duration { 1992 return t1.ExpectContinueTimeout 1993 } 1994 1995 type http2contextContext interface { 1996 context.Context 1997 } 1998 1999 func http2serverConnBaseContext(c net.Conn, opts *http2ServeConnOpts) (ctx http2contextContext, cancel func()) { 2000 ctx, cancel = context.WithCancel(context.Background()) 2001 ctx = context.WithValue(ctx, LocalAddrContextKey, c.LocalAddr()) 2002 if hs := opts.baseConfig(); hs != nil { 2003 ctx = context.WithValue(ctx, ServerContextKey, hs) 2004 } 2005 return 2006 } 2007 2008 func http2contextWithCancel(ctx http2contextContext) (_ http2contextContext, cancel func()) { 2009 return context.WithCancel(ctx) 2010 } 2011 2012 func http2requestWithContext(req *Request, ctx http2contextContext) *Request { 2013 return req.WithContext(ctx) 2014 } 2015 2016 type http2clientTrace httptrace.ClientTrace 2017 2018 func http2reqContext(r *Request) context.Context { return r.Context() } 2019 2020 func http2setResponseUncompressed(res *Response) { res.Uncompressed = true } 2021 2022 func http2traceGotConn(req *Request, cc *http2ClientConn) { 2023 trace := httptrace.ContextClientTrace(req.Context()) 2024 if trace == nil || trace.GotConn == nil { 2025 return 2026 } 2027 ci := httptrace.GotConnInfo{Conn: cc.tconn} 2028 cc.mu.Lock() 2029 ci.Reused = cc.nextStreamID > 1 2030 ci.WasIdle = len(cc.streams) == 0 && ci.Reused 2031 if ci.WasIdle && !cc.lastActive.IsZero() { 2032 ci.IdleTime = time.Now().Sub(cc.lastActive) 2033 } 2034 cc.mu.Unlock() 2035 2036 trace.GotConn(ci) 2037 } 2038 2039 func http2traceWroteHeaders(trace *http2clientTrace) { 2040 if trace != nil && trace.WroteHeaders != nil { 2041 trace.WroteHeaders() 2042 } 2043 } 2044 2045 func http2traceGot100Continue(trace *http2clientTrace) { 2046 if trace != nil && trace.Got100Continue != nil { 2047 trace.Got100Continue() 2048 } 2049 } 2050 2051 func http2traceWait100Continue(trace *http2clientTrace) { 2052 if trace != nil && trace.Wait100Continue != nil { 2053 trace.Wait100Continue() 2054 } 2055 } 2056 2057 func http2traceWroteRequest(trace *http2clientTrace, err error) { 2058 if trace != nil && trace.WroteRequest != nil { 2059 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err}) 2060 } 2061 } 2062 2063 func http2traceFirstResponseByte(trace *http2clientTrace) { 2064 if trace != nil && trace.GotFirstResponseByte != nil { 2065 trace.GotFirstResponseByte() 2066 } 2067 } 2068 2069 func http2requestTrace(req *Request) *http2clientTrace { 2070 trace := httptrace.ContextClientTrace(req.Context()) 2071 return (*http2clientTrace)(trace) 2072 } 2073 2074 var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1" 2075 2076 type http2goroutineLock uint64 2077 2078 func http2newGoroutineLock() http2goroutineLock { 2079 if !http2DebugGoroutines { 2080 return 0 2081 } 2082 return http2goroutineLock(http2curGoroutineID()) 2083 } 2084 2085 func (g http2goroutineLock) check() { 2086 if !http2DebugGoroutines { 2087 return 2088 } 2089 if http2curGoroutineID() != uint64(g) { 2090 panic("running on the wrong goroutine") 2091 } 2092 } 2093 2094 func (g http2goroutineLock) checkNotOn() { 2095 if !http2DebugGoroutines { 2096 return 2097 } 2098 if http2curGoroutineID() == uint64(g) { 2099 panic("running on the wrong goroutine") 2100 } 2101 } 2102 2103 var http2goroutineSpace = []byte("goroutine ") 2104 2105 func http2curGoroutineID() uint64 { 2106 bp := http2littleBuf.Get().(*[]byte) 2107 defer http2littleBuf.Put(bp) 2108 b := *bp 2109 b = b[:runtime.Stack(b, false)] 2110 2111 b = bytes.TrimPrefix(b, http2goroutineSpace) 2112 i := bytes.IndexByte(b, ' ') 2113 if i < 0 { 2114 panic(fmt.Sprintf("No space found in %q", b)) 2115 } 2116 b = b[:i] 2117 n, err := http2parseUintBytes(b, 10, 64) 2118 if err != nil { 2119 panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err)) 2120 } 2121 return n 2122 } 2123 2124 var http2littleBuf = sync.Pool{ 2125 New: func() interface{} { 2126 buf := make([]byte, 64) 2127 return &buf 2128 }, 2129 } 2130 2131 // parseUintBytes is like strconv.ParseUint, but using a []byte. 2132 func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) { 2133 var cutoff, maxVal uint64 2134 2135 if bitSize == 0 { 2136 bitSize = int(strconv.IntSize) 2137 } 2138 2139 s0 := s 2140 switch { 2141 case len(s) < 1: 2142 err = strconv.ErrSyntax 2143 goto Error 2144 2145 case 2 <= base && base <= 36: 2146 2147 case base == 0: 2148 2149 switch { 2150 case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'): 2151 base = 16 2152 s = s[2:] 2153 if len(s) < 1 { 2154 err = strconv.ErrSyntax 2155 goto Error 2156 } 2157 case s[0] == '0': 2158 base = 8 2159 default: 2160 base = 10 2161 } 2162 2163 default: 2164 err = errors.New("invalid base " + strconv.Itoa(base)) 2165 goto Error 2166 } 2167 2168 n = 0 2169 cutoff = http2cutoff64(base) 2170 maxVal = 1<<uint(bitSize) - 1 2171 2172 for i := 0; i < len(s); i++ { 2173 var v byte 2174 d := s[i] 2175 switch { 2176 case '0' <= d && d <= '9': 2177 v = d - '0' 2178 case 'a' <= d && d <= 'z': 2179 v = d - 'a' + 10 2180 case 'A' <= d && d <= 'Z': 2181 v = d - 'A' + 10 2182 default: 2183 n = 0 2184 err = strconv.ErrSyntax 2185 goto Error 2186 } 2187 if int(v) >= base { 2188 n = 0 2189 err = strconv.ErrSyntax 2190 goto Error 2191 } 2192 2193 if n >= cutoff { 2194 2195 n = 1<<64 - 1 2196 err = strconv.ErrRange 2197 goto Error 2198 } 2199 n *= uint64(base) 2200 2201 n1 := n + uint64(v) 2202 if n1 < n || n1 > maxVal { 2203 2204 n = 1<<64 - 1 2205 err = strconv.ErrRange 2206 goto Error 2207 } 2208 n = n1 2209 } 2210 2211 return n, nil 2212 2213 Error: 2214 return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err} 2215 } 2216 2217 // Return the first number n such that n*base >= 1<<64. 2218 func http2cutoff64(base int) uint64 { 2219 if base < 2 { 2220 return 0 2221 } 2222 return (1<<64-1)/uint64(base) + 1 2223 } 2224 2225 var ( 2226 http2commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case 2227 http2commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case 2228 ) 2229 2230 func init() { 2231 for _, v := range []string{ 2232 "accept", 2233 "accept-charset", 2234 "accept-encoding", 2235 "accept-language", 2236 "accept-ranges", 2237 "age", 2238 "access-control-allow-origin", 2239 "allow", 2240 "authorization", 2241 "cache-control", 2242 "content-disposition", 2243 "content-encoding", 2244 "content-language", 2245 "content-length", 2246 "content-location", 2247 "content-range", 2248 "content-type", 2249 "cookie", 2250 "date", 2251 "etag", 2252 "expect", 2253 "expires", 2254 "from", 2255 "host", 2256 "if-match", 2257 "if-modified-since", 2258 "if-none-match", 2259 "if-unmodified-since", 2260 "last-modified", 2261 "link", 2262 "location", 2263 "max-forwards", 2264 "proxy-authenticate", 2265 "proxy-authorization", 2266 "range", 2267 "referer", 2268 "refresh", 2269 "retry-after", 2270 "server", 2271 "set-cookie", 2272 "strict-transport-security", 2273 "trailer", 2274 "transfer-encoding", 2275 "user-agent", 2276 "vary", 2277 "via", 2278 "www-authenticate", 2279 } { 2280 chk := CanonicalHeaderKey(v) 2281 http2commonLowerHeader[chk] = v 2282 http2commonCanonHeader[v] = chk 2283 } 2284 } 2285 2286 func http2lowerHeader(v string) string { 2287 if s, ok := http2commonLowerHeader[v]; ok { 2288 return s 2289 } 2290 return strings.ToLower(v) 2291 } 2292 2293 var ( 2294 http2VerboseLogs bool 2295 http2logFrameWrites bool 2296 http2logFrameReads bool 2297 ) 2298 2299 func init() { 2300 e := os.Getenv("GODEBUG") 2301 if strings.Contains(e, "http2debug=1") { 2302 http2VerboseLogs = true 2303 } 2304 if strings.Contains(e, "http2debug=2") { 2305 http2VerboseLogs = true 2306 http2logFrameWrites = true 2307 http2logFrameReads = true 2308 } 2309 } 2310 2311 const ( 2312 // ClientPreface is the string that must be sent by new 2313 // connections from clients. 2314 http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" 2315 2316 // SETTINGS_MAX_FRAME_SIZE default 2317 // http://http2.github.io/http2-spec/#rfc.section.6.5.2 2318 http2initialMaxFrameSize = 16384 2319 2320 // NextProtoTLS is the NPN/ALPN protocol negotiated during 2321 // HTTP/2's TLS setup. 2322 http2NextProtoTLS = "h2" 2323 2324 // http://http2.github.io/http2-spec/#SettingValues 2325 http2initialHeaderTableSize = 4096 2326 2327 http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size 2328 2329 http2defaultMaxReadFrameSize = 1 << 20 2330 ) 2331 2332 var ( 2333 http2clientPreface = []byte(http2ClientPreface) 2334 ) 2335 2336 type http2streamState int 2337 2338 const ( 2339 http2stateIdle http2streamState = iota 2340 http2stateOpen 2341 http2stateHalfClosedLocal 2342 http2stateHalfClosedRemote 2343 http2stateResvLocal 2344 http2stateResvRemote 2345 http2stateClosed 2346 ) 2347 2348 var http2stateName = [...]string{ 2349 http2stateIdle: "Idle", 2350 http2stateOpen: "Open", 2351 http2stateHalfClosedLocal: "HalfClosedLocal", 2352 http2stateHalfClosedRemote: "HalfClosedRemote", 2353 http2stateResvLocal: "ResvLocal", 2354 http2stateResvRemote: "ResvRemote", 2355 http2stateClosed: "Closed", 2356 } 2357 2358 func (st http2streamState) String() string { 2359 return http2stateName[st] 2360 } 2361 2362 // Setting is a setting parameter: which setting it is, and its value. 2363 type http2Setting struct { 2364 // ID is which setting is being set. 2365 // See http://http2.github.io/http2-spec/#SettingValues 2366 ID http2SettingID 2367 2368 // Val is the value. 2369 Val uint32 2370 } 2371 2372 func (s http2Setting) String() string { 2373 return fmt.Sprintf("[%v = %d]", s.ID, s.Val) 2374 } 2375 2376 // Valid reports whether the setting is valid. 2377 func (s http2Setting) Valid() error { 2378 2379 switch s.ID { 2380 case http2SettingEnablePush: 2381 if s.Val != 1 && s.Val != 0 { 2382 return http2ConnectionError(http2ErrCodeProtocol) 2383 } 2384 case http2SettingInitialWindowSize: 2385 if s.Val > 1<<31-1 { 2386 return http2ConnectionError(http2ErrCodeFlowControl) 2387 } 2388 case http2SettingMaxFrameSize: 2389 if s.Val < 16384 || s.Val > 1<<24-1 { 2390 return http2ConnectionError(http2ErrCodeProtocol) 2391 } 2392 } 2393 return nil 2394 } 2395 2396 // A SettingID is an HTTP/2 setting as defined in 2397 // http://http2.github.io/http2-spec/#iana-settings 2398 type http2SettingID uint16 2399 2400 const ( 2401 http2SettingHeaderTableSize http2SettingID = 0x1 2402 http2SettingEnablePush http2SettingID = 0x2 2403 http2SettingMaxConcurrentStreams http2SettingID = 0x3 2404 http2SettingInitialWindowSize http2SettingID = 0x4 2405 http2SettingMaxFrameSize http2SettingID = 0x5 2406 http2SettingMaxHeaderListSize http2SettingID = 0x6 2407 ) 2408 2409 var http2settingName = map[http2SettingID]string{ 2410 http2SettingHeaderTableSize: "HEADER_TABLE_SIZE", 2411 http2SettingEnablePush: "ENABLE_PUSH", 2412 http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS", 2413 http2SettingInitialWindowSize: "INITIAL_WINDOW_SIZE", 2414 http2SettingMaxFrameSize: "MAX_FRAME_SIZE", 2415 http2SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE", 2416 } 2417 2418 func (s http2SettingID) String() string { 2419 if v, ok := http2settingName[s]; ok { 2420 return v 2421 } 2422 return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s)) 2423 } 2424 2425 var ( 2426 http2errInvalidHeaderFieldName = errors.New("http2: invalid header field name") 2427 http2errInvalidHeaderFieldValue = errors.New("http2: invalid header field value") 2428 ) 2429 2430 // validWireHeaderFieldName reports whether v is a valid header field 2431 // name (key). See httplex.ValidHeaderName for the base rules. 2432 // 2433 // Further, http2 says: 2434 // "Just as in HTTP/1.x, header field names are strings of ASCII 2435 // characters that are compared in a case-insensitive 2436 // fashion. However, header field names MUST be converted to 2437 // lowercase prior to their encoding in HTTP/2. " 2438 func http2validWireHeaderFieldName(v string) bool { 2439 if len(v) == 0 { 2440 return false 2441 } 2442 for _, r := range v { 2443 if !httplex.IsTokenRune(r) { 2444 return false 2445 } 2446 if 'A' <= r && r <= 'Z' { 2447 return false 2448 } 2449 } 2450 return true 2451 } 2452 2453 var http2httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n) 2454 2455 func init() { 2456 for i := 100; i <= 999; i++ { 2457 if v := StatusText(i); v != "" { 2458 http2httpCodeStringCommon[i] = strconv.Itoa(i) 2459 } 2460 } 2461 } 2462 2463 func http2httpCodeString(code int) string { 2464 if s, ok := http2httpCodeStringCommon[code]; ok { 2465 return s 2466 } 2467 return strconv.Itoa(code) 2468 } 2469 2470 // from pkg io 2471 type http2stringWriter interface { 2472 WriteString(s string) (n int, err error) 2473 } 2474 2475 // A gate lets two goroutines coordinate their activities. 2476 type http2gate chan struct{} 2477 2478 func (g http2gate) Done() { g <- struct{}{} } 2479 2480 func (g http2gate) Wait() { <-g } 2481 2482 // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed). 2483 type http2closeWaiter chan struct{} 2484 2485 // Init makes a closeWaiter usable. 2486 // It exists because so a closeWaiter value can be placed inside a 2487 // larger struct and have the Mutex and Cond's memory in the same 2488 // allocation. 2489 func (cw *http2closeWaiter) Init() { 2490 *cw = make(chan struct{}) 2491 } 2492 2493 // Close marks the closeWaiter as closed and unblocks any waiters. 2494 func (cw http2closeWaiter) Close() { 2495 close(cw) 2496 } 2497 2498 // Wait waits for the closeWaiter to become closed. 2499 func (cw http2closeWaiter) Wait() { 2500 <-cw 2501 } 2502 2503 // bufferedWriter is a buffered writer that writes to w. 2504 // Its buffered writer is lazily allocated as needed, to minimize 2505 // idle memory usage with many connections. 2506 type http2bufferedWriter struct { 2507 w io.Writer // immutable 2508 bw *bufio.Writer // non-nil when data is buffered 2509 } 2510 2511 func http2newBufferedWriter(w io.Writer) *http2bufferedWriter { 2512 return &http2bufferedWriter{w: w} 2513 } 2514 2515 var http2bufWriterPool = sync.Pool{ 2516 New: func() interface{} { 2517 2518 return bufio.NewWriterSize(nil, 4<<10) 2519 }, 2520 } 2521 2522 func (w *http2bufferedWriter) Write(p []byte) (n int, err error) { 2523 if w.bw == nil { 2524 bw := http2bufWriterPool.Get().(*bufio.Writer) 2525 bw.Reset(w.w) 2526 w.bw = bw 2527 } 2528 return w.bw.Write(p) 2529 } 2530 2531 func (w *http2bufferedWriter) Flush() error { 2532 bw := w.bw 2533 if bw == nil { 2534 return nil 2535 } 2536 err := bw.Flush() 2537 bw.Reset(nil) 2538 http2bufWriterPool.Put(bw) 2539 w.bw = nil 2540 return err 2541 } 2542 2543 func http2mustUint31(v int32) uint32 { 2544 if v < 0 || v > 2147483647 { 2545 panic("out of range") 2546 } 2547 return uint32(v) 2548 } 2549 2550 // bodyAllowedForStatus reports whether a given response status code 2551 // permits a body. See RFC 2616, section 4.4. 2552 func http2bodyAllowedForStatus(status int) bool { 2553 switch { 2554 case status >= 100 && status <= 199: 2555 return false 2556 case status == 204: 2557 return false 2558 case status == 304: 2559 return false 2560 } 2561 return true 2562 } 2563 2564 type http2httpError struct { 2565 msg string 2566 timeout bool 2567 } 2568 2569 func (e *http2httpError) Error() string { return e.msg } 2570 2571 func (e *http2httpError) Timeout() bool { return e.timeout } 2572 2573 func (e *http2httpError) Temporary() bool { return true } 2574 2575 var http2errTimeout error = &http2httpError{msg: "http2: timeout awaiting response headers", timeout: true} 2576 2577 type http2connectionStater interface { 2578 ConnectionState() tls.ConnectionState 2579 } 2580 2581 var http2sorterPool = sync.Pool{New: func() interface{} { return new(http2sorter) }} 2582 2583 type http2sorter struct { 2584 v []string // owned by sorter 2585 } 2586 2587 func (s *http2sorter) Len() int { return len(s.v) } 2588 2589 func (s *http2sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] } 2590 2591 func (s *http2sorter) Less(i, j int) bool { return s.v[i] < s.v[j] } 2592 2593 // Keys returns the sorted keys of h. 2594 // 2595 // The returned slice is only valid until s used again or returned to 2596 // its pool. 2597 func (s *http2sorter) Keys(h Header) []string { 2598 keys := s.v[:0] 2599 for k := range h { 2600 keys = append(keys, k) 2601 } 2602 s.v = keys 2603 sort.Sort(s) 2604 return keys 2605 } 2606 2607 func (s *http2sorter) SortStrings(ss []string) { 2608 2609 save := s.v 2610 s.v = ss 2611 sort.Sort(s) 2612 s.v = save 2613 } 2614 2615 // pipe is a goroutine-safe io.Reader/io.Writer pair. It's like 2616 // io.Pipe except there are no PipeReader/PipeWriter halves, and the 2617 // underlying buffer is an interface. (io.Pipe is always unbuffered) 2618 type http2pipe struct { 2619 mu sync.Mutex 2620 c sync.Cond // c.L lazily initialized to &p.mu 2621 b http2pipeBuffer 2622 err error // read error once empty. non-nil means closed. 2623 breakErr error // immediate read error (caller doesn't see rest of b) 2624 donec chan struct{} // closed on error 2625 readFn func() // optional code to run in Read before error 2626 } 2627 2628 type http2pipeBuffer interface { 2629 Len() int 2630 io.Writer 2631 io.Reader 2632 } 2633 2634 // Read waits until data is available and copies bytes 2635 // from the buffer into p. 2636 func (p *http2pipe) Read(d []byte) (n int, err error) { 2637 p.mu.Lock() 2638 defer p.mu.Unlock() 2639 if p.c.L == nil { 2640 p.c.L = &p.mu 2641 } 2642 for { 2643 if p.breakErr != nil { 2644 return 0, p.breakErr 2645 } 2646 if p.b.Len() > 0 { 2647 return p.b.Read(d) 2648 } 2649 if p.err != nil { 2650 if p.readFn != nil { 2651 p.readFn() 2652 p.readFn = nil 2653 } 2654 return 0, p.err 2655 } 2656 p.c.Wait() 2657 } 2658 } 2659 2660 var http2errClosedPipeWrite = errors.New("write on closed buffer") 2661 2662 // Write copies bytes from p into the buffer and wakes a reader. 2663 // It is an error to write more data than the buffer can hold. 2664 func (p *http2pipe) Write(d []byte) (n int, err error) { 2665 p.mu.Lock() 2666 defer p.mu.Unlock() 2667 if p.c.L == nil { 2668 p.c.L = &p.mu 2669 } 2670 defer p.c.Signal() 2671 if p.err != nil { 2672 return 0, http2errClosedPipeWrite 2673 } 2674 return p.b.Write(d) 2675 } 2676 2677 // CloseWithError causes the next Read (waking up a current blocked 2678 // Read if needed) to return the provided err after all data has been 2679 // read. 2680 // 2681 // The error must be non-nil. 2682 func (p *http2pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) } 2683 2684 // BreakWithError causes the next Read (waking up a current blocked 2685 // Read if needed) to return the provided err immediately, without 2686 // waiting for unread data. 2687 func (p *http2pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) } 2688 2689 // closeWithErrorAndCode is like CloseWithError but also sets some code to run 2690 // in the caller's goroutine before returning the error. 2691 func (p *http2pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) } 2692 2693 func (p *http2pipe) closeWithError(dst *error, err error, fn func()) { 2694 if err == nil { 2695 panic("err must be non-nil") 2696 } 2697 p.mu.Lock() 2698 defer p.mu.Unlock() 2699 if p.c.L == nil { 2700 p.c.L = &p.mu 2701 } 2702 defer p.c.Signal() 2703 if *dst != nil { 2704 2705 return 2706 } 2707 p.readFn = fn 2708 *dst = err 2709 p.closeDoneLocked() 2710 } 2711 2712 // requires p.mu be held. 2713 func (p *http2pipe) closeDoneLocked() { 2714 if p.donec == nil { 2715 return 2716 } 2717 2718 select { 2719 case <-p.donec: 2720 default: 2721 close(p.donec) 2722 } 2723 } 2724 2725 // Err returns the error (if any) first set by BreakWithError or CloseWithError. 2726 func (p *http2pipe) Err() error { 2727 p.mu.Lock() 2728 defer p.mu.Unlock() 2729 if p.breakErr != nil { 2730 return p.breakErr 2731 } 2732 return p.err 2733 } 2734 2735 // Done returns a channel which is closed if and when this pipe is closed 2736 // with CloseWithError. 2737 func (p *http2pipe) Done() <-chan struct{} { 2738 p.mu.Lock() 2739 defer p.mu.Unlock() 2740 if p.donec == nil { 2741 p.donec = make(chan struct{}) 2742 if p.err != nil || p.breakErr != nil { 2743 2744 p.closeDoneLocked() 2745 } 2746 } 2747 return p.donec 2748 } 2749 2750 const ( 2751 http2prefaceTimeout = 10 * time.Second 2752 http2firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway 2753 http2handlerChunkWriteSize = 4 << 10 2754 http2defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to? 2755 ) 2756 2757 var ( 2758 http2errClientDisconnected = errors.New("client disconnected") 2759 http2errClosedBody = errors.New("body closed by handler") 2760 http2errHandlerComplete = errors.New("http2: request body closed due to handler exiting") 2761 http2errStreamClosed = errors.New("http2: stream closed") 2762 ) 2763 2764 var http2responseWriterStatePool = sync.Pool{ 2765 New: func() interface{} { 2766 rws := &http2responseWriterState{} 2767 rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize) 2768 return rws 2769 }, 2770 } 2771 2772 // Test hooks. 2773 var ( 2774 http2testHookOnConn func() 2775 http2testHookGetServerConn func(*http2serverConn) 2776 http2testHookOnPanicMu *sync.Mutex // nil except in tests 2777 http2testHookOnPanic func(sc *http2serverConn, panicVal interface{}) (rePanic bool) 2778 ) 2779 2780 // Server is an HTTP/2 server. 2781 type http2Server struct { 2782 // MaxHandlers limits the number of http.Handler ServeHTTP goroutines 2783 // which may run at a time over all connections. 2784 // Negative or zero no limit. 2785 // TODO: implement 2786 MaxHandlers int 2787 2788 // MaxConcurrentStreams optionally specifies the number of 2789 // concurrent streams that each client may have open at a 2790 // time. This is unrelated to the number of http.Handler goroutines 2791 // which may be active globally, which is MaxHandlers. 2792 // If zero, MaxConcurrentStreams defaults to at least 100, per 2793 // the HTTP/2 spec's recommendations. 2794 MaxConcurrentStreams uint32 2795 2796 // MaxReadFrameSize optionally specifies the largest frame 2797 // this server is willing to read. A valid value is between 2798 // 16k and 16M, inclusive. If zero or otherwise invalid, a 2799 // default value is used. 2800 MaxReadFrameSize uint32 2801 2802 // PermitProhibitedCipherSuites, if true, permits the use of 2803 // cipher suites prohibited by the HTTP/2 spec. 2804 PermitProhibitedCipherSuites bool 2805 } 2806 2807 func (s *http2Server) maxReadFrameSize() uint32 { 2808 if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize { 2809 return v 2810 } 2811 return http2defaultMaxReadFrameSize 2812 } 2813 2814 func (s *http2Server) maxConcurrentStreams() uint32 { 2815 if v := s.MaxConcurrentStreams; v > 0 { 2816 return v 2817 } 2818 return http2defaultMaxStreams 2819 } 2820 2821 // ConfigureServer adds HTTP/2 support to a net/http Server. 2822 // 2823 // The configuration conf may be nil. 2824 // 2825 // ConfigureServer must be called before s begins serving. 2826 func http2ConfigureServer(s *Server, conf *http2Server) error { 2827 if conf == nil { 2828 conf = new(http2Server) 2829 } 2830 2831 if s.TLSConfig == nil { 2832 s.TLSConfig = new(tls.Config) 2833 } else if s.TLSConfig.CipherSuites != nil { 2834 // If they already provided a CipherSuite list, return 2835 // an error if it has a bad order or is missing 2836 // ECDHE_RSA_WITH_AES_128_GCM_SHA256. 2837 const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 2838 haveRequired := false 2839 sawBad := false 2840 for i, cs := range s.TLSConfig.CipherSuites { 2841 if cs == requiredCipher { 2842 haveRequired = true 2843 } 2844 if http2isBadCipher(cs) { 2845 sawBad = true 2846 } else if sawBad { 2847 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) 2848 } 2849 } 2850 if !haveRequired { 2851 return fmt.Errorf("http2: TLSConfig.CipherSuites is missing HTTP/2-required TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256") 2852 } 2853 } 2854 2855 s.TLSConfig.PreferServerCipherSuites = true 2856 2857 haveNPN := false 2858 for _, p := range s.TLSConfig.NextProtos { 2859 if p == http2NextProtoTLS { 2860 haveNPN = true 2861 break 2862 } 2863 } 2864 if !haveNPN { 2865 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS) 2866 } 2867 2868 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2-14") 2869 2870 if s.TLSNextProto == nil { 2871 s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){} 2872 } 2873 protoHandler := func(hs *Server, c *tls.Conn, h Handler) { 2874 if http2testHookOnConn != nil { 2875 http2testHookOnConn() 2876 } 2877 conf.ServeConn(c, &http2ServeConnOpts{ 2878 Handler: h, 2879 BaseConfig: hs, 2880 }) 2881 } 2882 s.TLSNextProto[http2NextProtoTLS] = protoHandler 2883 s.TLSNextProto["h2-14"] = protoHandler 2884 return nil 2885 } 2886 2887 // ServeConnOpts are options for the Server.ServeConn method. 2888 type http2ServeConnOpts struct { 2889 // BaseConfig optionally sets the base configuration 2890 // for values. If nil, defaults are used. 2891 BaseConfig *Server 2892 2893 // Handler specifies which handler to use for processing 2894 // requests. If nil, BaseConfig.Handler is used. If BaseConfig 2895 // or BaseConfig.Handler is nil, http.DefaultServeMux is used. 2896 Handler Handler 2897 } 2898 2899 func (o *http2ServeConnOpts) baseConfig() *Server { 2900 if o != nil && o.BaseConfig != nil { 2901 return o.BaseConfig 2902 } 2903 return new(Server) 2904 } 2905 2906 func (o *http2ServeConnOpts) handler() Handler { 2907 if o != nil { 2908 if o.Handler != nil { 2909 return o.Handler 2910 } 2911 if o.BaseConfig != nil && o.BaseConfig.Handler != nil { 2912 return o.BaseConfig.Handler 2913 } 2914 } 2915 return DefaultServeMux 2916 } 2917 2918 // ServeConn serves HTTP/2 requests on the provided connection and 2919 // blocks until the connection is no longer readable. 2920 // 2921 // ServeConn starts speaking HTTP/2 assuming that c has not had any 2922 // reads or writes. It writes its initial settings frame and expects 2923 // to be able to read the preface and settings frame from the 2924 // client. If c has a ConnectionState method like a *tls.Conn, the 2925 // ConnectionState is used to verify the TLS ciphersuite and to set 2926 // the Request.TLS field in Handlers. 2927 // 2928 // ServeConn does not support h2c by itself. Any h2c support must be 2929 // implemented in terms of providing a suitably-behaving net.Conn. 2930 // 2931 // The opts parameter is optional. If nil, default values are used. 2932 func (s *http2Server) ServeConn(c net.Conn, opts *http2ServeConnOpts) { 2933 baseCtx, cancel := http2serverConnBaseContext(c, opts) 2934 defer cancel() 2935 2936 sc := &http2serverConn{ 2937 srv: s, 2938 hs: opts.baseConfig(), 2939 conn: c, 2940 baseCtx: baseCtx, 2941 remoteAddrStr: c.RemoteAddr().String(), 2942 bw: http2newBufferedWriter(c), 2943 handler: opts.handler(), 2944 streams: make(map[uint32]*http2stream), 2945 readFrameCh: make(chan http2readFrameResult), 2946 wantWriteFrameCh: make(chan http2frameWriteMsg, 8), 2947 wroteFrameCh: make(chan http2frameWriteResult, 1), 2948 bodyReadCh: make(chan http2bodyReadMsg), 2949 doneServing: make(chan struct{}), 2950 advMaxStreams: s.maxConcurrentStreams(), 2951 writeSched: http2writeScheduler{ 2952 maxFrameSize: http2initialMaxFrameSize, 2953 }, 2954 initialWindowSize: http2initialWindowSize, 2955 headerTableSize: http2initialHeaderTableSize, 2956 serveG: http2newGoroutineLock(), 2957 pushEnabled: true, 2958 } 2959 2960 sc.flow.add(http2initialWindowSize) 2961 sc.inflow.add(http2initialWindowSize) 2962 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf) 2963 2964 fr := http2NewFramer(sc.bw, c) 2965 fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil) 2966 fr.MaxHeaderListSize = sc.maxHeaderListSize() 2967 fr.SetMaxReadFrameSize(s.maxReadFrameSize()) 2968 sc.framer = fr 2969 2970 if tc, ok := c.(http2connectionStater); ok { 2971 sc.tlsState = new(tls.ConnectionState) 2972 *sc.tlsState = tc.ConnectionState() 2973 2974 if sc.tlsState.Version < tls.VersionTLS12 { 2975 sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low") 2976 return 2977 } 2978 2979 if sc.tlsState.ServerName == "" { 2980 2981 } 2982 2983 if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) { 2984 2985 sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite)) 2986 return 2987 } 2988 } 2989 2990 if hook := http2testHookGetServerConn; hook != nil { 2991 hook(sc) 2992 } 2993 sc.serve() 2994 } 2995 2996 // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec. 2997 func http2isBadCipher(cipher uint16) bool { 2998 switch cipher { 2999 case tls.TLS_RSA_WITH_RC4_128_SHA, 3000 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, 3001 tls.TLS_RSA_WITH_AES_128_CBC_SHA, 3002 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 3003 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 3004 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 3005 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 3006 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, 3007 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 3008 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 3009 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: 3010 3011 return true 3012 default: 3013 return false 3014 } 3015 } 3016 3017 func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) { 3018 sc.vlogf("http2: server rejecting conn: %v, %s", err, debug) 3019 3020 sc.framer.WriteGoAway(0, err, []byte(debug)) 3021 sc.bw.Flush() 3022 sc.conn.Close() 3023 } 3024 3025 type http2serverConn struct { 3026 // Immutable: 3027 srv *http2Server 3028 hs *Server 3029 conn net.Conn 3030 bw *http2bufferedWriter // writing to conn 3031 handler Handler 3032 baseCtx http2contextContext 3033 framer *http2Framer 3034 doneServing chan struct{} // closed when serverConn.serve ends 3035 readFrameCh chan http2readFrameResult // written by serverConn.readFrames 3036 wantWriteFrameCh chan http2frameWriteMsg // from handlers -> serve 3037 wroteFrameCh chan http2frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes 3038 bodyReadCh chan http2bodyReadMsg // from handlers -> serve 3039 testHookCh chan func(int) // code to run on the serve loop 3040 flow http2flow // conn-wide (not stream-specific) outbound flow control 3041 inflow http2flow // conn-wide inbound flow control 3042 tlsState *tls.ConnectionState // shared by all handlers, like net/http 3043 remoteAddrStr string 3044 3045 // Everything following is owned by the serve loop; use serveG.check(): 3046 serveG http2goroutineLock // used to verify funcs are on serve() 3047 pushEnabled bool 3048 sawFirstSettings bool // got the initial SETTINGS frame after the preface 3049 needToSendSettingsAck bool 3050 unackedSettings int // how many SETTINGS have we sent without ACKs? 3051 clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit) 3052 advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client 3053 curOpenStreams uint32 // client's number of open streams 3054 maxStreamID uint32 // max ever seen 3055 streams map[uint32]*http2stream 3056 initialWindowSize int32 3057 headerTableSize uint32 3058 peerMaxHeaderListSize uint32 // zero means unknown (default) 3059 canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case 3060 writingFrame bool // started write goroutine but haven't heard back on wroteFrameCh 3061 needsFrameFlush bool // last frame write wasn't a flush 3062 writeSched http2writeScheduler 3063 inGoAway bool // we've started to or sent GOAWAY 3064 needToSendGoAway bool // we need to schedule a GOAWAY frame write 3065 goAwayCode http2ErrCode 3066 shutdownTimerCh <-chan time.Time // nil until used 3067 shutdownTimer *time.Timer // nil until used 3068 freeRequestBodyBuf []byte // if non-nil, a free initialWindowSize buffer for getRequestBodyBuf 3069 3070 // Owned by the writeFrameAsync goroutine: 3071 headerWriteBuf bytes.Buffer 3072 hpackEncoder *hpack.Encoder 3073 } 3074 3075 func (sc *http2serverConn) maxHeaderListSize() uint32 { 3076 n := sc.hs.MaxHeaderBytes 3077 if n <= 0 { 3078 n = DefaultMaxHeaderBytes 3079 } 3080 // http2's count is in a slightly different unit and includes 32 bytes per pair. 3081 // So, take the net/http.Server value and pad it up a bit, assuming 10 headers. 3082 const perFieldOverhead = 32 // per http2 spec 3083 const typicalHeaders = 10 // conservative 3084 return uint32(n + typicalHeaders*perFieldOverhead) 3085 } 3086 3087 // stream represents a stream. This is the minimal metadata needed by 3088 // the serve goroutine. Most of the actual stream state is owned by 3089 // the http.Handler's goroutine in the responseWriter. Because the 3090 // responseWriter's responseWriterState is recycled at the end of a 3091 // handler, this struct intentionally has no pointer to the 3092 // *responseWriter{,State} itself, as the Handler ending nils out the 3093 // responseWriter's state field. 3094 type http2stream struct { 3095 // immutable: 3096 sc *http2serverConn 3097 id uint32 3098 body *http2pipe // non-nil if expecting DATA frames 3099 cw http2closeWaiter // closed wait stream transitions to closed state 3100 ctx http2contextContext 3101 cancelCtx func() 3102 3103 // owned by serverConn's serve loop: 3104 bodyBytes int64 // body bytes seen so far 3105 declBodyBytes int64 // or -1 if undeclared 3106 flow http2flow // limits writing from Handler to client 3107 inflow http2flow // what the client is allowed to POST/etc to us 3108 parent *http2stream // or nil 3109 numTrailerValues int64 3110 weight uint8 3111 state http2streamState 3112 sentReset bool // only true once detached from streams map 3113 gotReset bool // only true once detacted from streams map 3114 gotTrailerHeader bool // HEADER frame for trailers was seen 3115 wroteHeaders bool // whether we wrote headers (not status 100) 3116 reqBuf []byte 3117 3118 trailer Header // accumulated trailers 3119 reqTrailer Header // handler's Request.Trailer 3120 } 3121 3122 func (sc *http2serverConn) Framer() *http2Framer { return sc.framer } 3123 3124 func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() } 3125 3126 func (sc *http2serverConn) Flush() error { return sc.bw.Flush() } 3127 3128 func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) { 3129 return sc.hpackEncoder, &sc.headerWriteBuf 3130 } 3131 3132 func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) { 3133 sc.serveG.check() 3134 3135 if st, ok := sc.streams[streamID]; ok { 3136 return st.state, st 3137 } 3138 3139 if streamID <= sc.maxStreamID { 3140 return http2stateClosed, nil 3141 } 3142 return http2stateIdle, nil 3143 } 3144 3145 // setConnState calls the net/http ConnState hook for this connection, if configured. 3146 // Note that the net/http package does StateNew and StateClosed for us. 3147 // There is currently no plan for StateHijacked or hijacking HTTP/2 connections. 3148 func (sc *http2serverConn) setConnState(state ConnState) { 3149 if sc.hs.ConnState != nil { 3150 sc.hs.ConnState(sc.conn, state) 3151 } 3152 } 3153 3154 func (sc *http2serverConn) vlogf(format string, args ...interface{}) { 3155 if http2VerboseLogs { 3156 sc.logf(format, args...) 3157 } 3158 } 3159 3160 func (sc *http2serverConn) logf(format string, args ...interface{}) { 3161 if lg := sc.hs.ErrorLog; lg != nil { 3162 lg.Printf(format, args...) 3163 } else { 3164 log.Printf(format, args...) 3165 } 3166 } 3167 3168 // errno returns v's underlying uintptr, else 0. 3169 // 3170 // TODO: remove this helper function once http2 can use build 3171 // tags. See comment in isClosedConnError. 3172 func http2errno(v error) uintptr { 3173 if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr { 3174 return uintptr(rv.Uint()) 3175 } 3176 return 0 3177 } 3178 3179 // isClosedConnError reports whether err is an error from use of a closed 3180 // network connection. 3181 func http2isClosedConnError(err error) bool { 3182 if err == nil { 3183 return false 3184 } 3185 3186 str := err.Error() 3187 if strings.Contains(str, "use of closed network connection") { 3188 return true 3189 } 3190 3191 if runtime.GOOS == "windows" { 3192 if oe, ok := err.(*net.OpError); ok && oe.Op == "read" { 3193 if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" { 3194 const WSAECONNABORTED = 10053 3195 const WSAECONNRESET = 10054 3196 if n := http2errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED { 3197 return true 3198 } 3199 } 3200 } 3201 } 3202 return false 3203 } 3204 3205 func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) { 3206 if err == nil { 3207 return 3208 } 3209 if err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) { 3210 3211 sc.vlogf(format, args...) 3212 } else { 3213 sc.logf(format, args...) 3214 } 3215 } 3216 3217 func (sc *http2serverConn) canonicalHeader(v string) string { 3218 sc.serveG.check() 3219 cv, ok := http2commonCanonHeader[v] 3220 if ok { 3221 return cv 3222 } 3223 cv, ok = sc.canonHeader[v] 3224 if ok { 3225 return cv 3226 } 3227 if sc.canonHeader == nil { 3228 sc.canonHeader = make(map[string]string) 3229 } 3230 cv = CanonicalHeaderKey(v) 3231 sc.canonHeader[v] = cv 3232 return cv 3233 } 3234 3235 type http2readFrameResult struct { 3236 f http2Frame // valid until readMore is called 3237 err error 3238 3239 // readMore should be called once the consumer no longer needs or 3240 // retains f. After readMore, f is invalid and more frames can be 3241 // read. 3242 readMore func() 3243 } 3244 3245 // readFrames is the loop that reads incoming frames. 3246 // It takes care to only read one frame at a time, blocking until the 3247 // consumer is done with the frame. 3248 // It's run on its own goroutine. 3249 func (sc *http2serverConn) readFrames() { 3250 gate := make(http2gate) 3251 gateDone := gate.Done 3252 for { 3253 f, err := sc.framer.ReadFrame() 3254 select { 3255 case sc.readFrameCh <- http2readFrameResult{f, err, gateDone}: 3256 case <-sc.doneServing: 3257 return 3258 } 3259 select { 3260 case <-gate: 3261 case <-sc.doneServing: 3262 return 3263 } 3264 if http2terminalReadFrameError(err) { 3265 return 3266 } 3267 } 3268 } 3269 3270 // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine. 3271 type http2frameWriteResult struct { 3272 wm http2frameWriteMsg // what was written (or attempted) 3273 err error // result of the writeFrame call 3274 } 3275 3276 // writeFrameAsync runs in its own goroutine and writes a single frame 3277 // and then reports when it's done. 3278 // At most one goroutine can be running writeFrameAsync at a time per 3279 // serverConn. 3280 func (sc *http2serverConn) writeFrameAsync(wm http2frameWriteMsg) { 3281 err := wm.write.writeFrame(sc) 3282 sc.wroteFrameCh <- http2frameWriteResult{wm, err} 3283 } 3284 3285 func (sc *http2serverConn) closeAllStreamsOnConnClose() { 3286 sc.serveG.check() 3287 for _, st := range sc.streams { 3288 sc.closeStream(st, http2errClientDisconnected) 3289 } 3290 } 3291 3292 func (sc *http2serverConn) stopShutdownTimer() { 3293 sc.serveG.check() 3294 if t := sc.shutdownTimer; t != nil { 3295 t.Stop() 3296 } 3297 } 3298 3299 func (sc *http2serverConn) notePanic() { 3300 3301 if http2testHookOnPanicMu != nil { 3302 http2testHookOnPanicMu.Lock() 3303 defer http2testHookOnPanicMu.Unlock() 3304 } 3305 if http2testHookOnPanic != nil { 3306 if e := recover(); e != nil { 3307 if http2testHookOnPanic(sc, e) { 3308 panic(e) 3309 } 3310 } 3311 } 3312 } 3313 3314 func (sc *http2serverConn) serve() { 3315 sc.serveG.check() 3316 defer sc.notePanic() 3317 defer sc.conn.Close() 3318 defer sc.closeAllStreamsOnConnClose() 3319 defer sc.stopShutdownTimer() 3320 defer close(sc.doneServing) 3321 3322 if http2VerboseLogs { 3323 sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs) 3324 } 3325 3326 sc.writeFrame(http2frameWriteMsg{ 3327 write: http2writeSettings{ 3328 {http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()}, 3329 {http2SettingMaxConcurrentStreams, sc.advMaxStreams}, 3330 {http2SettingMaxHeaderListSize, sc.maxHeaderListSize()}, 3331 }, 3332 }) 3333 sc.unackedSettings++ 3334 3335 if err := sc.readPreface(); err != nil { 3336 sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err) 3337 return 3338 } 3339 3340 sc.setConnState(StateActive) 3341 sc.setConnState(StateIdle) 3342 3343 go sc.readFrames() 3344 3345 settingsTimer := time.NewTimer(http2firstSettingsTimeout) 3346 loopNum := 0 3347 for { 3348 loopNum++ 3349 select { 3350 case wm := <-sc.wantWriteFrameCh: 3351 sc.writeFrame(wm) 3352 case res := <-sc.wroteFrameCh: 3353 sc.wroteFrame(res) 3354 case res := <-sc.readFrameCh: 3355 if !sc.processFrameFromReader(res) { 3356 return 3357 } 3358 res.readMore() 3359 if settingsTimer.C != nil { 3360 settingsTimer.Stop() 3361 settingsTimer.C = nil 3362 } 3363 case m := <-sc.bodyReadCh: 3364 sc.noteBodyRead(m.st, m.n) 3365 case <-settingsTimer.C: 3366 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr()) 3367 return 3368 case <-sc.shutdownTimerCh: 3369 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr()) 3370 return 3371 case fn := <-sc.testHookCh: 3372 fn(loopNum) 3373 } 3374 } 3375 } 3376 3377 // readPreface reads the ClientPreface greeting from the peer 3378 // or returns an error on timeout or an invalid greeting. 3379 func (sc *http2serverConn) readPreface() error { 3380 errc := make(chan error, 1) 3381 go func() { 3382 3383 buf := make([]byte, len(http2ClientPreface)) 3384 if _, err := io.ReadFull(sc.conn, buf); err != nil { 3385 errc <- err 3386 } else if !bytes.Equal(buf, http2clientPreface) { 3387 errc <- fmt.Errorf("bogus greeting %q", buf) 3388 } else { 3389 errc <- nil 3390 } 3391 }() 3392 timer := time.NewTimer(http2prefaceTimeout) 3393 defer timer.Stop() 3394 select { 3395 case <-timer.C: 3396 return errors.New("timeout waiting for client preface") 3397 case err := <-errc: 3398 if err == nil { 3399 if http2VerboseLogs { 3400 sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr()) 3401 } 3402 } 3403 return err 3404 } 3405 } 3406 3407 var http2errChanPool = sync.Pool{ 3408 New: func() interface{} { return make(chan error, 1) }, 3409 } 3410 3411 var http2writeDataPool = sync.Pool{ 3412 New: func() interface{} { return new(http2writeData) }, 3413 } 3414 3415 // writeDataFromHandler writes DATA response frames from a handler on 3416 // the given stream. 3417 func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error { 3418 ch := http2errChanPool.Get().(chan error) 3419 writeArg := http2writeDataPool.Get().(*http2writeData) 3420 *writeArg = http2writeData{stream.id, data, endStream} 3421 err := sc.writeFrameFromHandler(http2frameWriteMsg{ 3422 write: writeArg, 3423 stream: stream, 3424 done: ch, 3425 }) 3426 if err != nil { 3427 return err 3428 } 3429 var frameWriteDone bool // the frame write is done (successfully or not) 3430 select { 3431 case err = <-ch: 3432 frameWriteDone = true 3433 case <-sc.doneServing: 3434 return http2errClientDisconnected 3435 case <-stream.cw: 3436 3437 select { 3438 case err = <-ch: 3439 frameWriteDone = true 3440 default: 3441 return http2errStreamClosed 3442 } 3443 } 3444 http2errChanPool.Put(ch) 3445 if frameWriteDone { 3446 http2writeDataPool.Put(writeArg) 3447 } 3448 return err 3449 } 3450 3451 // writeFrameFromHandler sends wm to sc.wantWriteFrameCh, but aborts 3452 // if the connection has gone away. 3453 // 3454 // This must not be run from the serve goroutine itself, else it might 3455 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 3456 // buffered and is read by serve itself). If you're on the serve 3457 // goroutine, call writeFrame instead. 3458 func (sc *http2serverConn) writeFrameFromHandler(wm http2frameWriteMsg) error { 3459 sc.serveG.checkNotOn() 3460 select { 3461 case sc.wantWriteFrameCh <- wm: 3462 return nil 3463 case <-sc.doneServing: 3464 3465 return http2errClientDisconnected 3466 } 3467 } 3468 3469 // writeFrame schedules a frame to write and sends it if there's nothing 3470 // already being written. 3471 // 3472 // There is no pushback here (the serve goroutine never blocks). It's 3473 // the http.Handlers that block, waiting for their previous frames to 3474 // make it onto the wire 3475 // 3476 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 3477 func (sc *http2serverConn) writeFrame(wm http2frameWriteMsg) { 3478 sc.serveG.check() 3479 3480 var ignoreWrite bool 3481 3482 switch wm.write.(type) { 3483 case *http2writeResHeaders: 3484 wm.stream.wroteHeaders = true 3485 case http2write100ContinueHeadersFrame: 3486 if wm.stream.wroteHeaders { 3487 ignoreWrite = true 3488 } 3489 } 3490 3491 if !ignoreWrite { 3492 sc.writeSched.add(wm) 3493 } 3494 sc.scheduleFrameWrite() 3495 } 3496 3497 // startFrameWrite starts a goroutine to write wm (in a separate 3498 // goroutine since that might block on the network), and updates the 3499 // serve goroutine's state about the world, updated from info in wm. 3500 func (sc *http2serverConn) startFrameWrite(wm http2frameWriteMsg) { 3501 sc.serveG.check() 3502 if sc.writingFrame { 3503 panic("internal error: can only be writing one frame at a time") 3504 } 3505 3506 st := wm.stream 3507 if st != nil { 3508 switch st.state { 3509 case http2stateHalfClosedLocal: 3510 panic("internal error: attempt to send frame on half-closed-local stream") 3511 case http2stateClosed: 3512 if st.sentReset || st.gotReset { 3513 3514 sc.scheduleFrameWrite() 3515 return 3516 } 3517 panic(fmt.Sprintf("internal error: attempt to send a write %v on a closed stream", wm)) 3518 } 3519 } 3520 3521 sc.writingFrame = true 3522 sc.needsFrameFlush = true 3523 go sc.writeFrameAsync(wm) 3524 } 3525 3526 // errHandlerPanicked is the error given to any callers blocked in a read from 3527 // Request.Body when the main goroutine panics. Since most handlers read in the 3528 // the main ServeHTTP goroutine, this will show up rarely. 3529 var http2errHandlerPanicked = errors.New("http2: handler panicked") 3530 3531 // wroteFrame is called on the serve goroutine with the result of 3532 // whatever happened on writeFrameAsync. 3533 func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) { 3534 sc.serveG.check() 3535 if !sc.writingFrame { 3536 panic("internal error: expected to be already writing a frame") 3537 } 3538 sc.writingFrame = false 3539 3540 wm := res.wm 3541 st := wm.stream 3542 3543 closeStream := http2endsStream(wm.write) 3544 3545 if _, ok := wm.write.(http2handlerPanicRST); ok { 3546 sc.closeStream(st, http2errHandlerPanicked) 3547 } 3548 3549 if ch := wm.done; ch != nil { 3550 select { 3551 case ch <- res.err: 3552 default: 3553 panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.write)) 3554 } 3555 } 3556 wm.write = nil 3557 3558 if closeStream { 3559 if st == nil { 3560 panic("internal error: expecting non-nil stream") 3561 } 3562 switch st.state { 3563 case http2stateOpen: 3564 3565 st.state = http2stateHalfClosedLocal 3566 errCancel := http2StreamError{st.id, http2ErrCodeCancel} 3567 sc.resetStream(errCancel) 3568 case http2stateHalfClosedRemote: 3569 sc.closeStream(st, http2errHandlerComplete) 3570 } 3571 } 3572 3573 sc.scheduleFrameWrite() 3574 } 3575 3576 // scheduleFrameWrite tickles the frame writing scheduler. 3577 // 3578 // If a frame is already being written, nothing happens. This will be called again 3579 // when the frame is done being written. 3580 // 3581 // If a frame isn't being written we need to send one, the best frame 3582 // to send is selected, preferring first things that aren't 3583 // stream-specific (e.g. ACKing settings), and then finding the 3584 // highest priority stream. 3585 // 3586 // If a frame isn't being written and there's nothing else to send, we 3587 // flush the write buffer. 3588 func (sc *http2serverConn) scheduleFrameWrite() { 3589 sc.serveG.check() 3590 if sc.writingFrame { 3591 return 3592 } 3593 if sc.needToSendGoAway { 3594 sc.needToSendGoAway = false 3595 sc.startFrameWrite(http2frameWriteMsg{ 3596 write: &http2writeGoAway{ 3597 maxStreamID: sc.maxStreamID, 3598 code: sc.goAwayCode, 3599 }, 3600 }) 3601 return 3602 } 3603 if sc.needToSendSettingsAck { 3604 sc.needToSendSettingsAck = false 3605 sc.startFrameWrite(http2frameWriteMsg{write: http2writeSettingsAck{}}) 3606 return 3607 } 3608 if !sc.inGoAway { 3609 if wm, ok := sc.writeSched.take(); ok { 3610 sc.startFrameWrite(wm) 3611 return 3612 } 3613 } 3614 if sc.needsFrameFlush { 3615 sc.startFrameWrite(http2frameWriteMsg{write: http2flushFrameWriter{}}) 3616 sc.needsFrameFlush = false 3617 return 3618 } 3619 } 3620 3621 func (sc *http2serverConn) goAway(code http2ErrCode) { 3622 sc.serveG.check() 3623 if sc.inGoAway { 3624 return 3625 } 3626 if code != http2ErrCodeNo { 3627 sc.shutDownIn(250 * time.Millisecond) 3628 } else { 3629 3630 sc.shutDownIn(1 * time.Second) 3631 } 3632 sc.inGoAway = true 3633 sc.needToSendGoAway = true 3634 sc.goAwayCode = code 3635 sc.scheduleFrameWrite() 3636 } 3637 3638 func (sc *http2serverConn) shutDownIn(d time.Duration) { 3639 sc.serveG.check() 3640 sc.shutdownTimer = time.NewTimer(d) 3641 sc.shutdownTimerCh = sc.shutdownTimer.C 3642 } 3643 3644 func (sc *http2serverConn) resetStream(se http2StreamError) { 3645 sc.serveG.check() 3646 sc.writeFrame(http2frameWriteMsg{write: se}) 3647 if st, ok := sc.streams[se.StreamID]; ok { 3648 st.sentReset = true 3649 sc.closeStream(st, se) 3650 } 3651 } 3652 3653 // processFrameFromReader processes the serve loop's read from readFrameCh from the 3654 // frame-reading goroutine. 3655 // processFrameFromReader returns whether the connection should be kept open. 3656 func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool { 3657 sc.serveG.check() 3658 err := res.err 3659 if err != nil { 3660 if err == http2ErrFrameTooLarge { 3661 sc.goAway(http2ErrCodeFrameSize) 3662 return true 3663 } 3664 clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) 3665 if clientGone { 3666 3667 return false 3668 } 3669 } else { 3670 f := res.f 3671 if http2VerboseLogs { 3672 sc.vlogf("http2: server read frame %v", http2summarizeFrame(f)) 3673 } 3674 err = sc.processFrame(f) 3675 if err == nil { 3676 return true 3677 } 3678 } 3679 3680 switch ev := err.(type) { 3681 case http2StreamError: 3682 sc.resetStream(ev) 3683 return true 3684 case http2goAwayFlowError: 3685 sc.goAway(http2ErrCodeFlowControl) 3686 return true 3687 case http2ConnectionError: 3688 sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev) 3689 sc.goAway(http2ErrCode(ev)) 3690 return true 3691 default: 3692 if res.err != nil { 3693 sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 3694 } else { 3695 sc.logf("http2: server closing client connection: %v", err) 3696 } 3697 return false 3698 } 3699 } 3700 3701 func (sc *http2serverConn) processFrame(f http2Frame) error { 3702 sc.serveG.check() 3703 3704 if !sc.sawFirstSettings { 3705 if _, ok := f.(*http2SettingsFrame); !ok { 3706 return http2ConnectionError(http2ErrCodeProtocol) 3707 } 3708 sc.sawFirstSettings = true 3709 } 3710 3711 switch f := f.(type) { 3712 case *http2SettingsFrame: 3713 return sc.processSettings(f) 3714 case *http2MetaHeadersFrame: 3715 return sc.processHeaders(f) 3716 case *http2WindowUpdateFrame: 3717 return sc.processWindowUpdate(f) 3718 case *http2PingFrame: 3719 return sc.processPing(f) 3720 case *http2DataFrame: 3721 return sc.processData(f) 3722 case *http2RSTStreamFrame: 3723 return sc.processResetStream(f) 3724 case *http2PriorityFrame: 3725 return sc.processPriority(f) 3726 case *http2PushPromiseFrame: 3727 3728 return http2ConnectionError(http2ErrCodeProtocol) 3729 default: 3730 sc.vlogf("http2: server ignoring frame: %v", f.Header()) 3731 return nil 3732 } 3733 } 3734 3735 func (sc *http2serverConn) processPing(f *http2PingFrame) error { 3736 sc.serveG.check() 3737 if f.IsAck() { 3738 3739 return nil 3740 } 3741 if f.StreamID != 0 { 3742 3743 return http2ConnectionError(http2ErrCodeProtocol) 3744 } 3745 sc.writeFrame(http2frameWriteMsg{write: http2writePingAck{f}}) 3746 return nil 3747 } 3748 3749 func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error { 3750 sc.serveG.check() 3751 switch { 3752 case f.StreamID != 0: 3753 st := sc.streams[f.StreamID] 3754 if st == nil { 3755 3756 return nil 3757 } 3758 if !st.flow.add(int32(f.Increment)) { 3759 return http2StreamError{f.StreamID, http2ErrCodeFlowControl} 3760 } 3761 default: 3762 if !sc.flow.add(int32(f.Increment)) { 3763 return http2goAwayFlowError{} 3764 } 3765 } 3766 sc.scheduleFrameWrite() 3767 return nil 3768 } 3769 3770 func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error { 3771 sc.serveG.check() 3772 3773 state, st := sc.state(f.StreamID) 3774 if state == http2stateIdle { 3775 3776 return http2ConnectionError(http2ErrCodeProtocol) 3777 } 3778 if st != nil { 3779 st.gotReset = true 3780 st.cancelCtx() 3781 sc.closeStream(st, http2StreamError{f.StreamID, f.ErrCode}) 3782 } 3783 return nil 3784 } 3785 3786 func (sc *http2serverConn) closeStream(st *http2stream, err error) { 3787 sc.serveG.check() 3788 if st.state == http2stateIdle || st.state == http2stateClosed { 3789 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 3790 } 3791 st.state = http2stateClosed 3792 sc.curOpenStreams-- 3793 if sc.curOpenStreams == 0 { 3794 sc.setConnState(StateIdle) 3795 } 3796 delete(sc.streams, st.id) 3797 if p := st.body; p != nil { 3798 p.CloseWithError(err) 3799 } 3800 st.cw.Close() 3801 sc.writeSched.forgetStream(st.id) 3802 if st.reqBuf != nil { 3803 3804 sc.freeRequestBodyBuf = st.reqBuf 3805 } 3806 } 3807 3808 func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error { 3809 sc.serveG.check() 3810 if f.IsAck() { 3811 sc.unackedSettings-- 3812 if sc.unackedSettings < 0 { 3813 3814 return http2ConnectionError(http2ErrCodeProtocol) 3815 } 3816 return nil 3817 } 3818 if err := f.ForeachSetting(sc.processSetting); err != nil { 3819 return err 3820 } 3821 sc.needToSendSettingsAck = true 3822 sc.scheduleFrameWrite() 3823 return nil 3824 } 3825 3826 func (sc *http2serverConn) processSetting(s http2Setting) error { 3827 sc.serveG.check() 3828 if err := s.Valid(); err != nil { 3829 return err 3830 } 3831 if http2VerboseLogs { 3832 sc.vlogf("http2: server processing setting %v", s) 3833 } 3834 switch s.ID { 3835 case http2SettingHeaderTableSize: 3836 sc.headerTableSize = s.Val 3837 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 3838 case http2SettingEnablePush: 3839 sc.pushEnabled = s.Val != 0 3840 case http2SettingMaxConcurrentStreams: 3841 sc.clientMaxStreams = s.Val 3842 case http2SettingInitialWindowSize: 3843 return sc.processSettingInitialWindowSize(s.Val) 3844 case http2SettingMaxFrameSize: 3845 sc.writeSched.maxFrameSize = s.Val 3846 case http2SettingMaxHeaderListSize: 3847 sc.peerMaxHeaderListSize = s.Val 3848 default: 3849 3850 if http2VerboseLogs { 3851 sc.vlogf("http2: server ignoring unknown setting %v", s) 3852 } 3853 } 3854 return nil 3855 } 3856 3857 func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error { 3858 sc.serveG.check() 3859 3860 old := sc.initialWindowSize 3861 sc.initialWindowSize = int32(val) 3862 growth := sc.initialWindowSize - old 3863 for _, st := range sc.streams { 3864 if !st.flow.add(growth) { 3865 3866 return http2ConnectionError(http2ErrCodeFlowControl) 3867 } 3868 } 3869 return nil 3870 } 3871 3872 func (sc *http2serverConn) processData(f *http2DataFrame) error { 3873 sc.serveG.check() 3874 3875 id := f.Header().StreamID 3876 st, ok := sc.streams[id] 3877 if !ok || st.state != http2stateOpen || st.gotTrailerHeader { 3878 3879 return http2StreamError{id, http2ErrCodeStreamClosed} 3880 } 3881 if st.body == nil { 3882 panic("internal error: should have a body in this state") 3883 } 3884 data := f.Data() 3885 3886 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 3887 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 3888 return http2StreamError{id, http2ErrCodeStreamClosed} 3889 } 3890 if len(data) > 0 { 3891 3892 if int(st.inflow.available()) < len(data) { 3893 return http2StreamError{id, http2ErrCodeFlowControl} 3894 } 3895 st.inflow.take(int32(len(data))) 3896 wrote, err := st.body.Write(data) 3897 if err != nil { 3898 return http2StreamError{id, http2ErrCodeStreamClosed} 3899 } 3900 if wrote != len(data) { 3901 panic("internal error: bad Writer") 3902 } 3903 st.bodyBytes += int64(len(data)) 3904 } 3905 if f.StreamEnded() { 3906 st.endStream() 3907 } 3908 return nil 3909 } 3910 3911 // endStream closes a Request.Body's pipe. It is called when a DATA 3912 // frame says a request body is over (or after trailers). 3913 func (st *http2stream) endStream() { 3914 sc := st.sc 3915 sc.serveG.check() 3916 3917 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 3918 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 3919 st.declBodyBytes, st.bodyBytes)) 3920 } else { 3921 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest) 3922 st.body.CloseWithError(io.EOF) 3923 } 3924 st.state = http2stateHalfClosedRemote 3925 } 3926 3927 // copyTrailersToHandlerRequest is run in the Handler's goroutine in 3928 // its Request.Body.Read just before it gets io.EOF. 3929 func (st *http2stream) copyTrailersToHandlerRequest() { 3930 for k, vv := range st.trailer { 3931 if _, ok := st.reqTrailer[k]; ok { 3932 3933 st.reqTrailer[k] = vv 3934 } 3935 } 3936 } 3937 3938 func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error { 3939 sc.serveG.check() 3940 id := f.Header().StreamID 3941 if sc.inGoAway { 3942 3943 return nil 3944 } 3945 3946 if id%2 != 1 { 3947 return http2ConnectionError(http2ErrCodeProtocol) 3948 } 3949 3950 st := sc.streams[f.Header().StreamID] 3951 if st != nil { 3952 return st.processTrailerHeaders(f) 3953 } 3954 3955 if id <= sc.maxStreamID { 3956 return http2ConnectionError(http2ErrCodeProtocol) 3957 } 3958 sc.maxStreamID = id 3959 3960 ctx, cancelCtx := http2contextWithCancel(sc.baseCtx) 3961 st = &http2stream{ 3962 sc: sc, 3963 id: id, 3964 state: http2stateOpen, 3965 ctx: ctx, 3966 cancelCtx: cancelCtx, 3967 } 3968 if f.StreamEnded() { 3969 st.state = http2stateHalfClosedRemote 3970 } 3971 st.cw.Init() 3972 3973 st.flow.conn = &sc.flow 3974 st.flow.add(sc.initialWindowSize) 3975 st.inflow.conn = &sc.inflow 3976 st.inflow.add(http2initialWindowSize) 3977 3978 sc.streams[id] = st 3979 if f.HasPriority() { 3980 http2adjustStreamPriority(sc.streams, st.id, f.Priority) 3981 } 3982 sc.curOpenStreams++ 3983 if sc.curOpenStreams == 1 { 3984 sc.setConnState(StateActive) 3985 } 3986 if sc.curOpenStreams > sc.advMaxStreams { 3987 3988 if sc.unackedSettings == 0 { 3989 3990 return http2StreamError{st.id, http2ErrCodeProtocol} 3991 } 3992 3993 return http2StreamError{st.id, http2ErrCodeRefusedStream} 3994 } 3995 3996 rw, req, err := sc.newWriterAndRequest(st, f) 3997 if err != nil { 3998 return err 3999 } 4000 st.reqTrailer = req.Trailer 4001 if st.reqTrailer != nil { 4002 st.trailer = make(Header) 4003 } 4004 st.body = req.Body.(*http2requestBody).pipe 4005 st.declBodyBytes = req.ContentLength 4006 4007 handler := sc.handler.ServeHTTP 4008 if f.Truncated { 4009 4010 handler = http2handleHeaderListTooLong 4011 } else if err := http2checkValidHTTP2Request(req); err != nil { 4012 handler = http2new400Handler(err) 4013 } 4014 4015 go sc.runHandler(rw, req, handler) 4016 return nil 4017 } 4018 4019 func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error { 4020 sc := st.sc 4021 sc.serveG.check() 4022 if st.gotTrailerHeader { 4023 return http2ConnectionError(http2ErrCodeProtocol) 4024 } 4025 st.gotTrailerHeader = true 4026 if !f.StreamEnded() { 4027 return http2StreamError{st.id, http2ErrCodeProtocol} 4028 } 4029 4030 if len(f.PseudoFields()) > 0 { 4031 return http2StreamError{st.id, http2ErrCodeProtocol} 4032 } 4033 if st.trailer != nil { 4034 for _, hf := range f.RegularFields() { 4035 key := sc.canonicalHeader(hf.Name) 4036 if !http2ValidTrailerHeader(key) { 4037 4038 return http2StreamError{st.id, http2ErrCodeProtocol} 4039 } 4040 st.trailer[key] = append(st.trailer[key], hf.Value) 4041 } 4042 } 4043 st.endStream() 4044 return nil 4045 } 4046 4047 func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error { 4048 http2adjustStreamPriority(sc.streams, f.StreamID, f.http2PriorityParam) 4049 return nil 4050 } 4051 4052 func http2adjustStreamPriority(streams map[uint32]*http2stream, streamID uint32, priority http2PriorityParam) { 4053 st, ok := streams[streamID] 4054 if !ok { 4055 4056 return 4057 } 4058 st.weight = priority.Weight 4059 parent := streams[priority.StreamDep] 4060 if parent == st { 4061 4062 return 4063 } 4064 4065 for piter := parent; piter != nil; piter = piter.parent { 4066 if piter == st { 4067 parent.parent = st.parent 4068 break 4069 } 4070 } 4071 st.parent = parent 4072 if priority.Exclusive && (st.parent != nil || priority.StreamDep == 0) { 4073 for _, openStream := range streams { 4074 if openStream != st && openStream.parent == st.parent { 4075 openStream.parent = st 4076 } 4077 } 4078 } 4079 } 4080 4081 func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) { 4082 sc.serveG.check() 4083 4084 method := f.PseudoValue("method") 4085 path := f.PseudoValue("path") 4086 scheme := f.PseudoValue("scheme") 4087 authority := f.PseudoValue("authority") 4088 4089 isConnect := method == "CONNECT" 4090 if isConnect { 4091 if path != "" || scheme != "" || authority == "" { 4092 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4093 } 4094 } else if method == "" || path == "" || 4095 (scheme != "https" && scheme != "http") { 4096 4097 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4098 } 4099 4100 bodyOpen := !f.StreamEnded() 4101 if method == "HEAD" && bodyOpen { 4102 4103 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4104 } 4105 var tlsState *tls.ConnectionState // nil if not scheme https 4106 4107 if scheme == "https" { 4108 tlsState = sc.tlsState 4109 } 4110 4111 header := make(Header) 4112 for _, hf := range f.RegularFields() { 4113 header.Add(sc.canonicalHeader(hf.Name), hf.Value) 4114 } 4115 4116 if authority == "" { 4117 authority = header.Get("Host") 4118 } 4119 needsContinue := header.Get("Expect") == "100-continue" 4120 if needsContinue { 4121 header.Del("Expect") 4122 } 4123 4124 if cookies := header["Cookie"]; len(cookies) > 1 { 4125 header.Set("Cookie", strings.Join(cookies, "; ")) 4126 } 4127 4128 // Setup Trailers 4129 var trailer Header 4130 for _, v := range header["Trailer"] { 4131 for _, key := range strings.Split(v, ",") { 4132 key = CanonicalHeaderKey(strings.TrimSpace(key)) 4133 switch key { 4134 case "Transfer-Encoding", "Trailer", "Content-Length": 4135 4136 default: 4137 if trailer == nil { 4138 trailer = make(Header) 4139 } 4140 trailer[key] = nil 4141 } 4142 } 4143 } 4144 delete(header, "Trailer") 4145 4146 body := &http2requestBody{ 4147 conn: sc, 4148 stream: st, 4149 needsContinue: needsContinue, 4150 } 4151 var url_ *url.URL 4152 var requestURI string 4153 if isConnect { 4154 url_ = &url.URL{Host: authority} 4155 requestURI = authority 4156 } else { 4157 var err error 4158 url_, err = url.ParseRequestURI(path) 4159 if err != nil { 4160 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4161 } 4162 requestURI = path 4163 } 4164 req := &Request{ 4165 Method: method, 4166 URL: url_, 4167 RemoteAddr: sc.remoteAddrStr, 4168 Header: header, 4169 RequestURI: requestURI, 4170 Proto: "HTTP/2.0", 4171 ProtoMajor: 2, 4172 ProtoMinor: 0, 4173 TLS: tlsState, 4174 Host: authority, 4175 Body: body, 4176 Trailer: trailer, 4177 } 4178 req = http2requestWithContext(req, st.ctx) 4179 if bodyOpen { 4180 4181 buf := make([]byte, http2initialWindowSize) 4182 4183 body.pipe = &http2pipe{ 4184 b: &http2fixedBuffer{buf: buf}, 4185 } 4186 4187 if vv, ok := header["Content-Length"]; ok { 4188 req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64) 4189 } else { 4190 req.ContentLength = -1 4191 } 4192 } 4193 4194 rws := http2responseWriterStatePool.Get().(*http2responseWriterState) 4195 bwSave := rws.bw 4196 *rws = http2responseWriterState{} 4197 rws.conn = sc 4198 rws.bw = bwSave 4199 rws.bw.Reset(http2chunkWriter{rws}) 4200 rws.stream = st 4201 rws.req = req 4202 rws.body = body 4203 4204 rw := &http2responseWriter{rws: rws} 4205 return rw, req, nil 4206 } 4207 4208 func (sc *http2serverConn) getRequestBodyBuf() []byte { 4209 sc.serveG.check() 4210 if buf := sc.freeRequestBodyBuf; buf != nil { 4211 sc.freeRequestBodyBuf = nil 4212 return buf 4213 } 4214 return make([]byte, http2initialWindowSize) 4215 } 4216 4217 // Run on its own goroutine. 4218 func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) { 4219 didPanic := true 4220 defer func() { 4221 rw.rws.stream.cancelCtx() 4222 if didPanic { 4223 e := recover() 4224 // Same as net/http: 4225 const size = 64 << 10 4226 buf := make([]byte, size) 4227 buf = buf[:runtime.Stack(buf, false)] 4228 sc.writeFrameFromHandler(http2frameWriteMsg{ 4229 write: http2handlerPanicRST{rw.rws.stream.id}, 4230 stream: rw.rws.stream, 4231 }) 4232 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf) 4233 return 4234 } 4235 rw.handlerDone() 4236 }() 4237 handler(rw, req) 4238 didPanic = false 4239 } 4240 4241 func http2handleHeaderListTooLong(w ResponseWriter, r *Request) { 4242 // 10.5.1 Limits on Header Block Size: 4243 // .. "A server that receives a larger header block than it is 4244 // willing to handle can send an HTTP 431 (Request Header Fields Too 4245 // Large) status code" 4246 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 4247 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 4248 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 4249 } 4250 4251 // called from handler goroutines. 4252 // h may be nil. 4253 func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error { 4254 sc.serveG.checkNotOn() 4255 var errc chan error 4256 if headerData.h != nil { 4257 4258 errc = http2errChanPool.Get().(chan error) 4259 } 4260 if err := sc.writeFrameFromHandler(http2frameWriteMsg{ 4261 write: headerData, 4262 stream: st, 4263 done: errc, 4264 }); err != nil { 4265 return err 4266 } 4267 if errc != nil { 4268 select { 4269 case err := <-errc: 4270 http2errChanPool.Put(errc) 4271 return err 4272 case <-sc.doneServing: 4273 return http2errClientDisconnected 4274 case <-st.cw: 4275 return http2errStreamClosed 4276 } 4277 } 4278 return nil 4279 } 4280 4281 // called from handler goroutines. 4282 func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) { 4283 sc.writeFrameFromHandler(http2frameWriteMsg{ 4284 write: http2write100ContinueHeadersFrame{st.id}, 4285 stream: st, 4286 }) 4287 } 4288 4289 // A bodyReadMsg tells the server loop that the http.Handler read n 4290 // bytes of the DATA from the client on the given stream. 4291 type http2bodyReadMsg struct { 4292 st *http2stream 4293 n int 4294 } 4295 4296 // called from handler goroutines. 4297 // Notes that the handler for the given stream ID read n bytes of its body 4298 // and schedules flow control tokens to be sent. 4299 func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int) { 4300 sc.serveG.checkNotOn() 4301 select { 4302 case sc.bodyReadCh <- http2bodyReadMsg{st, n}: 4303 case <-sc.doneServing: 4304 } 4305 } 4306 4307 func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) { 4308 sc.serveG.check() 4309 sc.sendWindowUpdate(nil, n) 4310 if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed { 4311 4312 sc.sendWindowUpdate(st, n) 4313 } 4314 } 4315 4316 // st may be nil for conn-level 4317 func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) { 4318 sc.serveG.check() 4319 // "The legal range for the increment to the flow control 4320 // window is 1 to 2^31-1 (2,147,483,647) octets." 4321 // A Go Read call on 64-bit machines could in theory read 4322 // a larger Read than this. Very unlikely, but we handle it here 4323 // rather than elsewhere for now. 4324 const maxUint31 = 1<<31 - 1 4325 for n >= maxUint31 { 4326 sc.sendWindowUpdate32(st, maxUint31) 4327 n -= maxUint31 4328 } 4329 sc.sendWindowUpdate32(st, int32(n)) 4330 } 4331 4332 // st may be nil for conn-level 4333 func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) { 4334 sc.serveG.check() 4335 if n == 0 { 4336 return 4337 } 4338 if n < 0 { 4339 panic("negative update") 4340 } 4341 var streamID uint32 4342 if st != nil { 4343 streamID = st.id 4344 } 4345 sc.writeFrame(http2frameWriteMsg{ 4346 write: http2writeWindowUpdate{streamID: streamID, n: uint32(n)}, 4347 stream: st, 4348 }) 4349 var ok bool 4350 if st == nil { 4351 ok = sc.inflow.add(n) 4352 } else { 4353 ok = st.inflow.add(n) 4354 } 4355 if !ok { 4356 panic("internal error; sent too many window updates without decrements?") 4357 } 4358 } 4359 4360 type http2requestBody struct { 4361 stream *http2stream 4362 conn *http2serverConn 4363 closed bool 4364 pipe *http2pipe // non-nil if we have a HTTP entity message body 4365 needsContinue bool // need to send a 100-continue 4366 } 4367 4368 func (b *http2requestBody) Close() error { 4369 if b.pipe != nil { 4370 b.pipe.BreakWithError(http2errClosedBody) 4371 } 4372 b.closed = true 4373 return nil 4374 } 4375 4376 func (b *http2requestBody) Read(p []byte) (n int, err error) { 4377 if b.needsContinue { 4378 b.needsContinue = false 4379 b.conn.write100ContinueHeaders(b.stream) 4380 } 4381 if b.pipe == nil { 4382 return 0, io.EOF 4383 } 4384 n, err = b.pipe.Read(p) 4385 if n > 0 { 4386 b.conn.noteBodyReadFromHandler(b.stream, n) 4387 } 4388 return 4389 } 4390 4391 // responseWriter is the http.ResponseWriter implementation. It's 4392 // intentionally small (1 pointer wide) to minimize garbage. The 4393 // responseWriterState pointer inside is zeroed at the end of a 4394 // request (in handlerDone) and calls on the responseWriter thereafter 4395 // simply crash (caller's mistake), but the much larger responseWriterState 4396 // and buffers are reused between multiple requests. 4397 type http2responseWriter struct { 4398 rws *http2responseWriterState 4399 } 4400 4401 // Optional http.ResponseWriter interfaces implemented. 4402 var ( 4403 _ CloseNotifier = (*http2responseWriter)(nil) 4404 _ Flusher = (*http2responseWriter)(nil) 4405 _ http2stringWriter = (*http2responseWriter)(nil) 4406 ) 4407 4408 type http2responseWriterState struct { 4409 // immutable within a request: 4410 stream *http2stream 4411 req *Request 4412 body *http2requestBody // to close at end of request, if DATA frames didn't 4413 conn *http2serverConn 4414 4415 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 4416 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 4417 4418 // mutated by http.Handler goroutine: 4419 handlerHeader Header // nil until called 4420 snapHeader Header // snapshot of handlerHeader at WriteHeader time 4421 trailers []string // set in writeChunk 4422 status int // status code passed to WriteHeader 4423 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 4424 sentHeader bool // have we sent the header frame? 4425 handlerDone bool // handler has finished 4426 4427 sentContentLen int64 // non-zero if handler set a Content-Length header 4428 wroteBytes int64 4429 4430 closeNotifierMu sync.Mutex // guards closeNotifierCh 4431 closeNotifierCh chan bool // nil until first used 4432 } 4433 4434 type http2chunkWriter struct{ rws *http2responseWriterState } 4435 4436 func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 4437 4438 func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 } 4439 4440 // declareTrailer is called for each Trailer header when the 4441 // response header is written. It notes that a header will need to be 4442 // written in the trailers at the end of the response. 4443 func (rws *http2responseWriterState) declareTrailer(k string) { 4444 k = CanonicalHeaderKey(k) 4445 if !http2ValidTrailerHeader(k) { 4446 4447 rws.conn.logf("ignoring invalid trailer %q", k) 4448 return 4449 } 4450 if !http2strSliceContains(rws.trailers, k) { 4451 rws.trailers = append(rws.trailers, k) 4452 } 4453 } 4454 4455 // writeChunk writes chunks from the bufio.Writer. But because 4456 // bufio.Writer may bypass its chunking, sometimes p may be 4457 // arbitrarily large. 4458 // 4459 // writeChunk is also responsible (on the first chunk) for sending the 4460 // HEADER response. 4461 func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) { 4462 if !rws.wroteHeader { 4463 rws.writeHeader(200) 4464 } 4465 4466 isHeadResp := rws.req.Method == "HEAD" 4467 if !rws.sentHeader { 4468 rws.sentHeader = true 4469 var ctype, clen string 4470 if clen = rws.snapHeader.Get("Content-Length"); clen != "" { 4471 rws.snapHeader.Del("Content-Length") 4472 clen64, err := strconv.ParseInt(clen, 10, 64) 4473 if err == nil && clen64 >= 0 { 4474 rws.sentContentLen = clen64 4475 } else { 4476 clen = "" 4477 } 4478 } 4479 if clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) { 4480 clen = strconv.Itoa(len(p)) 4481 } 4482 _, hasContentType := rws.snapHeader["Content-Type"] 4483 if !hasContentType && http2bodyAllowedForStatus(rws.status) { 4484 ctype = DetectContentType(p) 4485 } 4486 var date string 4487 if _, ok := rws.snapHeader["Date"]; !ok { 4488 4489 date = time.Now().UTC().Format(TimeFormat) 4490 } 4491 4492 for _, v := range rws.snapHeader["Trailer"] { 4493 http2foreachHeaderElement(v, rws.declareTrailer) 4494 } 4495 4496 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp 4497 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 4498 streamID: rws.stream.id, 4499 httpResCode: rws.status, 4500 h: rws.snapHeader, 4501 endStream: endStream, 4502 contentType: ctype, 4503 contentLength: clen, 4504 date: date, 4505 }) 4506 if err != nil { 4507 return 0, err 4508 } 4509 if endStream { 4510 return 0, nil 4511 } 4512 } 4513 if isHeadResp { 4514 return len(p), nil 4515 } 4516 if len(p) == 0 && !rws.handlerDone { 4517 return 0, nil 4518 } 4519 4520 if rws.handlerDone { 4521 rws.promoteUndeclaredTrailers() 4522 } 4523 4524 endStream := rws.handlerDone && !rws.hasTrailers() 4525 if len(p) > 0 || endStream { 4526 4527 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil { 4528 return 0, err 4529 } 4530 } 4531 4532 if rws.handlerDone && rws.hasTrailers() { 4533 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 4534 streamID: rws.stream.id, 4535 h: rws.handlerHeader, 4536 trailers: rws.trailers, 4537 endStream: true, 4538 }) 4539 return len(p), err 4540 } 4541 return len(p), nil 4542 } 4543 4544 // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys 4545 // that, if present, signals that the map entry is actually for 4546 // the response trailers, and not the response headers. The prefix 4547 // is stripped after the ServeHTTP call finishes and the values are 4548 // sent in the trailers. 4549 // 4550 // This mechanism is intended only for trailers that are not known 4551 // prior to the headers being written. If the set of trailers is fixed 4552 // or known before the header is written, the normal Go trailers mechanism 4553 // is preferred: 4554 // https://golang.org/pkg/net/http/#ResponseWriter 4555 // https://golang.org/pkg/net/http/#example_ResponseWriter_trailers 4556 const http2TrailerPrefix = "Trailer:" 4557 4558 // promoteUndeclaredTrailers permits http.Handlers to set trailers 4559 // after the header has already been flushed. Because the Go 4560 // ResponseWriter interface has no way to set Trailers (only the 4561 // Header), and because we didn't want to expand the ResponseWriter 4562 // interface, and because nobody used trailers, and because RFC 2616 4563 // says you SHOULD (but not must) predeclare any trailers in the 4564 // header, the official ResponseWriter rules said trailers in Go must 4565 // be predeclared, and then we reuse the same ResponseWriter.Header() 4566 // map to mean both Headers and Trailers. When it's time to write the 4567 // Trailers, we pick out the fields of Headers that were declared as 4568 // trailers. That worked for a while, until we found the first major 4569 // user of Trailers in the wild: gRPC (using them only over http2), 4570 // and gRPC libraries permit setting trailers mid-stream without 4571 // predeclarnig them. So: change of plans. We still permit the old 4572 // way, but we also permit this hack: if a Header() key begins with 4573 // "Trailer:", the suffix of that key is a Trailer. Because ':' is an 4574 // invalid token byte anyway, there is no ambiguity. (And it's already 4575 // filtered out) It's mildly hacky, but not terrible. 4576 // 4577 // This method runs after the Handler is done and promotes any Header 4578 // fields to be trailers. 4579 func (rws *http2responseWriterState) promoteUndeclaredTrailers() { 4580 for k, vv := range rws.handlerHeader { 4581 if !strings.HasPrefix(k, http2TrailerPrefix) { 4582 continue 4583 } 4584 trailerKey := strings.TrimPrefix(k, http2TrailerPrefix) 4585 rws.declareTrailer(trailerKey) 4586 rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv 4587 } 4588 4589 if len(rws.trailers) > 1 { 4590 sorter := http2sorterPool.Get().(*http2sorter) 4591 sorter.SortStrings(rws.trailers) 4592 http2sorterPool.Put(sorter) 4593 } 4594 } 4595 4596 func (w *http2responseWriter) Flush() { 4597 rws := w.rws 4598 if rws == nil { 4599 panic("Header called after Handler finished") 4600 } 4601 if rws.bw.Buffered() > 0 { 4602 if err := rws.bw.Flush(); err != nil { 4603 4604 return 4605 } 4606 } else { 4607 4608 rws.writeChunk(nil) 4609 } 4610 } 4611 4612 func (w *http2responseWriter) CloseNotify() <-chan bool { 4613 rws := w.rws 4614 if rws == nil { 4615 panic("CloseNotify called after Handler finished") 4616 } 4617 rws.closeNotifierMu.Lock() 4618 ch := rws.closeNotifierCh 4619 if ch == nil { 4620 ch = make(chan bool, 1) 4621 rws.closeNotifierCh = ch 4622 go func() { 4623 rws.stream.cw.Wait() 4624 ch <- true 4625 }() 4626 } 4627 rws.closeNotifierMu.Unlock() 4628 return ch 4629 } 4630 4631 func (w *http2responseWriter) Header() Header { 4632 rws := w.rws 4633 if rws == nil { 4634 panic("Header called after Handler finished") 4635 } 4636 if rws.handlerHeader == nil { 4637 rws.handlerHeader = make(Header) 4638 } 4639 return rws.handlerHeader 4640 } 4641 4642 func (w *http2responseWriter) WriteHeader(code int) { 4643 rws := w.rws 4644 if rws == nil { 4645 panic("WriteHeader called after Handler finished") 4646 } 4647 rws.writeHeader(code) 4648 } 4649 4650 func (rws *http2responseWriterState) writeHeader(code int) { 4651 if !rws.wroteHeader { 4652 rws.wroteHeader = true 4653 rws.status = code 4654 if len(rws.handlerHeader) > 0 { 4655 rws.snapHeader = http2cloneHeader(rws.handlerHeader) 4656 } 4657 } 4658 } 4659 4660 func http2cloneHeader(h Header) Header { 4661 h2 := make(Header, len(h)) 4662 for k, vv := range h { 4663 vv2 := make([]string, len(vv)) 4664 copy(vv2, vv) 4665 h2[k] = vv2 4666 } 4667 return h2 4668 } 4669 4670 // The Life Of A Write is like this: 4671 // 4672 // * Handler calls w.Write or w.WriteString -> 4673 // * -> rws.bw (*bufio.Writer) -> 4674 // * (Handler migth call Flush) 4675 // * -> chunkWriter{rws} 4676 // * -> responseWriterState.writeChunk(p []byte) 4677 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 4678 func (w *http2responseWriter) Write(p []byte) (n int, err error) { 4679 return w.write(len(p), p, "") 4680 } 4681 4682 func (w *http2responseWriter) WriteString(s string) (n int, err error) { 4683 return w.write(len(s), nil, s) 4684 } 4685 4686 // either dataB or dataS is non-zero. 4687 func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 4688 rws := w.rws 4689 if rws == nil { 4690 panic("Write called after Handler finished") 4691 } 4692 if !rws.wroteHeader { 4693 w.WriteHeader(200) 4694 } 4695 if !http2bodyAllowedForStatus(rws.status) { 4696 return 0, ErrBodyNotAllowed 4697 } 4698 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) 4699 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen { 4700 4701 return 0, errors.New("http2: handler wrote more than declared Content-Length") 4702 } 4703 4704 if dataB != nil { 4705 return rws.bw.Write(dataB) 4706 } else { 4707 return rws.bw.WriteString(dataS) 4708 } 4709 } 4710 4711 func (w *http2responseWriter) handlerDone() { 4712 rws := w.rws 4713 rws.handlerDone = true 4714 w.Flush() 4715 w.rws = nil 4716 http2responseWriterStatePool.Put(rws) 4717 } 4718 4719 // foreachHeaderElement splits v according to the "#rule" construction 4720 // in RFC 2616 section 2.1 and calls fn for each non-empty element. 4721 func http2foreachHeaderElement(v string, fn func(string)) { 4722 v = textproto.TrimString(v) 4723 if v == "" { 4724 return 4725 } 4726 if !strings.Contains(v, ",") { 4727 fn(v) 4728 return 4729 } 4730 for _, f := range strings.Split(v, ",") { 4731 if f = textproto.TrimString(f); f != "" { 4732 fn(f) 4733 } 4734 } 4735 } 4736 4737 // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2 4738 var http2connHeaders = []string{ 4739 "Connection", 4740 "Keep-Alive", 4741 "Proxy-Connection", 4742 "Transfer-Encoding", 4743 "Upgrade", 4744 } 4745 4746 // checkValidHTTP2Request checks whether req is a valid HTTP/2 request, 4747 // per RFC 7540 Section 8.1.2.2. 4748 // The returned error is reported to users. 4749 func http2checkValidHTTP2Request(req *Request) error { 4750 for _, h := range http2connHeaders { 4751 if _, ok := req.Header[h]; ok { 4752 return fmt.Errorf("request header %q is not valid in HTTP/2", h) 4753 } 4754 } 4755 te := req.Header["Te"] 4756 if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) { 4757 return errors.New(`request header "TE" may only be "trailers" in HTTP/2`) 4758 } 4759 return nil 4760 } 4761 4762 func http2new400Handler(err error) HandlerFunc { 4763 return func(w ResponseWriter, r *Request) { 4764 Error(w, err.Error(), StatusBadRequest) 4765 } 4766 } 4767 4768 // ValidTrailerHeader reports whether name is a valid header field name to appear 4769 // in trailers. 4770 // See: http://tools.ietf.org/html/rfc7230#section-4.1.2 4771 func http2ValidTrailerHeader(name string) bool { 4772 name = CanonicalHeaderKey(name) 4773 if strings.HasPrefix(name, "If-") || http2badTrailer[name] { 4774 return false 4775 } 4776 return true 4777 } 4778 4779 var http2badTrailer = map[string]bool{ 4780 "Authorization": true, 4781 "Cache-Control": true, 4782 "Connection": true, 4783 "Content-Encoding": true, 4784 "Content-Length": true, 4785 "Content-Range": true, 4786 "Content-Type": true, 4787 "Expect": true, 4788 "Host": true, 4789 "Keep-Alive": true, 4790 "Max-Forwards": true, 4791 "Pragma": true, 4792 "Proxy-Authenticate": true, 4793 "Proxy-Authorization": true, 4794 "Proxy-Connection": true, 4795 "Range": true, 4796 "Realm": true, 4797 "Te": true, 4798 "Trailer": true, 4799 "Transfer-Encoding": true, 4800 "Www-Authenticate": true, 4801 } 4802 4803 const ( 4804 // transportDefaultConnFlow is how many connection-level flow control 4805 // tokens we give the server at start-up, past the default 64k. 4806 http2transportDefaultConnFlow = 1 << 30 4807 4808 // transportDefaultStreamFlow is how many stream-level flow 4809 // control tokens we announce to the peer, and how many bytes 4810 // we buffer per stream. 4811 http2transportDefaultStreamFlow = 4 << 20 4812 4813 // transportDefaultStreamMinRefresh is the minimum number of bytes we'll send 4814 // a stream-level WINDOW_UPDATE for at a time. 4815 http2transportDefaultStreamMinRefresh = 4 << 10 4816 4817 http2defaultUserAgent = "Go-http-client/2.0" 4818 ) 4819 4820 // Transport is an HTTP/2 Transport. 4821 // 4822 // A Transport internally caches connections to servers. It is safe 4823 // for concurrent use by multiple goroutines. 4824 type http2Transport struct { 4825 // DialTLS specifies an optional dial function for creating 4826 // TLS connections for requests. 4827 // 4828 // If DialTLS is nil, tls.Dial is used. 4829 // 4830 // If the returned net.Conn has a ConnectionState method like tls.Conn, 4831 // it will be used to set http.Response.TLS. 4832 DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error) 4833 4834 // TLSClientConfig specifies the TLS configuration to use with 4835 // tls.Client. If nil, the default configuration is used. 4836 TLSClientConfig *tls.Config 4837 4838 // ConnPool optionally specifies an alternate connection pool to use. 4839 // If nil, the default is used. 4840 ConnPool http2ClientConnPool 4841 4842 // DisableCompression, if true, prevents the Transport from 4843 // requesting compression with an "Accept-Encoding: gzip" 4844 // request header when the Request contains no existing 4845 // Accept-Encoding value. If the Transport requests gzip on 4846 // its own and gets a gzipped response, it's transparently 4847 // decoded in the Response.Body. However, if the user 4848 // explicitly requested gzip it is not automatically 4849 // uncompressed. 4850 DisableCompression bool 4851 4852 // AllowHTTP, if true, permits HTTP/2 requests using the insecure, 4853 // plain-text "http" scheme. Note that this does not enable h2c support. 4854 AllowHTTP bool 4855 4856 // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to 4857 // send in the initial settings frame. It is how many bytes 4858 // of response headers are allow. Unlike the http2 spec, zero here 4859 // means to use a default limit (currently 10MB). If you actually 4860 // want to advertise an ulimited value to the peer, Transport 4861 // interprets the highest possible value here (0xffffffff or 1<<32-1) 4862 // to mean no limit. 4863 MaxHeaderListSize uint32 4864 4865 // t1, if non-nil, is the standard library Transport using 4866 // this transport. Its settings are used (but not its 4867 // RoundTrip method, etc). 4868 t1 *Transport 4869 4870 connPoolOnce sync.Once 4871 connPoolOrDef http2ClientConnPool // non-nil version of ConnPool 4872 } 4873 4874 func (t *http2Transport) maxHeaderListSize() uint32 { 4875 if t.MaxHeaderListSize == 0 { 4876 return 10 << 20 4877 } 4878 if t.MaxHeaderListSize == 0xffffffff { 4879 return 0 4880 } 4881 return t.MaxHeaderListSize 4882 } 4883 4884 func (t *http2Transport) disableCompression() bool { 4885 return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression) 4886 } 4887 4888 var http2errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6") 4889 4890 // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2. 4891 // It requires Go 1.6 or later and returns an error if the net/http package is too old 4892 // or if t1 has already been HTTP/2-enabled. 4893 func http2ConfigureTransport(t1 *Transport) error { 4894 _, err := http2configureTransport(t1) 4895 return err 4896 } 4897 4898 func (t *http2Transport) connPool() http2ClientConnPool { 4899 t.connPoolOnce.Do(t.initConnPool) 4900 return t.connPoolOrDef 4901 } 4902 4903 func (t *http2Transport) initConnPool() { 4904 if t.ConnPool != nil { 4905 t.connPoolOrDef = t.ConnPool 4906 } else { 4907 t.connPoolOrDef = &http2clientConnPool{t: t} 4908 } 4909 } 4910 4911 // ClientConn is the state of a single HTTP/2 client connection to an 4912 // HTTP/2 server. 4913 type http2ClientConn struct { 4914 t *http2Transport 4915 tconn net.Conn // usually *tls.Conn, except specialized impls 4916 tlsState *tls.ConnectionState // nil only for specialized impls 4917 4918 // readLoop goroutine fields: 4919 readerDone chan struct{} // closed on error 4920 readerErr error // set before readerDone is closed 4921 4922 mu sync.Mutex // guards following 4923 cond *sync.Cond // hold mu; broadcast on flow/closed changes 4924 flow http2flow // our conn-level flow control quota (cs.flow is per stream) 4925 inflow http2flow // peer's conn-level flow control 4926 closed bool 4927 goAway *http2GoAwayFrame // if non-nil, the GoAwayFrame we received 4928 streams map[uint32]*http2clientStream // client-initiated 4929 nextStreamID uint32 4930 bw *bufio.Writer 4931 br *bufio.Reader 4932 fr *http2Framer 4933 lastActive time.Time 4934 4935 // Settings from peer: 4936 maxFrameSize uint32 4937 maxConcurrentStreams uint32 4938 initialWindowSize uint32 4939 hbuf bytes.Buffer // HPACK encoder writes into this 4940 henc *hpack.Encoder 4941 freeBuf [][]byte 4942 4943 wmu sync.Mutex // held while writing; acquire AFTER mu if holding both 4944 werr error // first write error that has occurred 4945 } 4946 4947 // clientStream is the state for a single HTTP/2 stream. One of these 4948 // is created for each Transport.RoundTrip call. 4949 type http2clientStream struct { 4950 cc *http2ClientConn 4951 req *Request 4952 trace *http2clientTrace // or nil 4953 ID uint32 4954 resc chan http2resAndError 4955 bufPipe http2pipe // buffered pipe with the flow-controlled response payload 4956 requestedGzip bool 4957 on100 func() // optional code to run if get a 100 continue response 4958 4959 flow http2flow // guarded by cc.mu 4960 inflow http2flow // guarded by cc.mu 4961 bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read 4962 readErr error // sticky read error; owned by transportResponseBody.Read 4963 stopReqBody error // if non-nil, stop writing req body; guarded by cc.mu 4964 4965 peerReset chan struct{} // closed on peer reset 4966 resetErr error // populated before peerReset is closed 4967 4968 done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu 4969 4970 // owned by clientConnReadLoop: 4971 firstByte bool // got the first response byte 4972 pastHeaders bool // got first MetaHeadersFrame (actual headers) 4973 pastTrailers bool // got optional second MetaHeadersFrame (trailers) 4974 4975 trailer Header // accumulated trailers 4976 resTrailer *Header // client's Response.Trailer 4977 } 4978 4979 // awaitRequestCancel runs in its own goroutine and waits for the user 4980 // to cancel a RoundTrip request, its context to expire, or for the 4981 // request to be done (any way it might be removed from the cc.streams 4982 // map: peer reset, successful completion, TCP connection breakage, 4983 // etc) 4984 func (cs *http2clientStream) awaitRequestCancel(req *Request) { 4985 ctx := http2reqContext(req) 4986 if req.Cancel == nil && ctx.Done() == nil { 4987 return 4988 } 4989 select { 4990 case <-req.Cancel: 4991 cs.bufPipe.CloseWithError(http2errRequestCanceled) 4992 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 4993 case <-ctx.Done(): 4994 cs.bufPipe.CloseWithError(ctx.Err()) 4995 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 4996 case <-cs.done: 4997 } 4998 } 4999 5000 // checkResetOrDone reports any error sent in a RST_STREAM frame by the 5001 // server, or errStreamClosed if the stream is complete. 5002 func (cs *http2clientStream) checkResetOrDone() error { 5003 select { 5004 case <-cs.peerReset: 5005 return cs.resetErr 5006 case <-cs.done: 5007 return http2errStreamClosed 5008 default: 5009 return nil 5010 } 5011 } 5012 5013 func (cs *http2clientStream) abortRequestBodyWrite(err error) { 5014 if err == nil { 5015 panic("nil error") 5016 } 5017 cc := cs.cc 5018 cc.mu.Lock() 5019 cs.stopReqBody = err 5020 cc.cond.Broadcast() 5021 cc.mu.Unlock() 5022 } 5023 5024 type http2stickyErrWriter struct { 5025 w io.Writer 5026 err *error 5027 } 5028 5029 func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) { 5030 if *sew.err != nil { 5031 return 0, *sew.err 5032 } 5033 n, err = sew.w.Write(p) 5034 *sew.err = err 5035 return 5036 } 5037 5038 var http2ErrNoCachedConn = errors.New("http2: no cached connection was available") 5039 5040 // RoundTripOpt are options for the Transport.RoundTripOpt method. 5041 type http2RoundTripOpt struct { 5042 // OnlyCachedConn controls whether RoundTripOpt may 5043 // create a new TCP connection. If set true and 5044 // no cached connection is available, RoundTripOpt 5045 // will return ErrNoCachedConn. 5046 OnlyCachedConn bool 5047 } 5048 5049 func (t *http2Transport) RoundTrip(req *Request) (*Response, error) { 5050 return t.RoundTripOpt(req, http2RoundTripOpt{}) 5051 } 5052 5053 // authorityAddr returns a given authority (a host/IP, or host:port / ip:port) 5054 // and returns a host:port. The port 443 is added if needed. 5055 func http2authorityAddr(scheme string, authority string) (addr string) { 5056 if _, _, err := net.SplitHostPort(authority); err == nil { 5057 return authority 5058 } 5059 port := "443" 5060 if scheme == "http" { 5061 port = "80" 5062 } 5063 return net.JoinHostPort(authority, port) 5064 } 5065 5066 // RoundTripOpt is like RoundTrip, but takes options. 5067 func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) { 5068 if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) { 5069 return nil, errors.New("http2: unsupported scheme") 5070 } 5071 5072 addr := http2authorityAddr(req.URL.Scheme, req.URL.Host) 5073 for { 5074 cc, err := t.connPool().GetClientConn(req, addr) 5075 if err != nil { 5076 t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err) 5077 return nil, err 5078 } 5079 http2traceGotConn(req, cc) 5080 res, err := cc.RoundTrip(req) 5081 if http2shouldRetryRequest(req, err) { 5082 continue 5083 } 5084 if err != nil { 5085 t.vlogf("RoundTrip failure: %v", err) 5086 return nil, err 5087 } 5088 return res, nil 5089 } 5090 } 5091 5092 // CloseIdleConnections closes any connections which were previously 5093 // connected from previous requests but are now sitting idle. 5094 // It does not interrupt any connections currently in use. 5095 func (t *http2Transport) CloseIdleConnections() { 5096 if cp, ok := t.connPool().(http2clientConnPoolIdleCloser); ok { 5097 cp.closeIdleConnections() 5098 } 5099 } 5100 5101 var ( 5102 http2errClientConnClosed = errors.New("http2: client conn is closed") 5103 http2errClientConnUnusable = errors.New("http2: client conn not usable") 5104 ) 5105 5106 func http2shouldRetryRequest(req *Request, err error) bool { 5107 5108 return err == http2errClientConnUnusable 5109 } 5110 5111 func (t *http2Transport) dialClientConn(addr string) (*http2ClientConn, error) { 5112 host, _, err := net.SplitHostPort(addr) 5113 if err != nil { 5114 return nil, err 5115 } 5116 tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host)) 5117 if err != nil { 5118 return nil, err 5119 } 5120 return t.NewClientConn(tconn) 5121 } 5122 5123 func (t *http2Transport) newTLSConfig(host string) *tls.Config { 5124 cfg := new(tls.Config) 5125 if t.TLSClientConfig != nil { 5126 *cfg = *t.TLSClientConfig 5127 } 5128 if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) { 5129 cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...) 5130 } 5131 if cfg.ServerName == "" { 5132 cfg.ServerName = host 5133 } 5134 return cfg 5135 } 5136 5137 func (t *http2Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) { 5138 if t.DialTLS != nil { 5139 return t.DialTLS 5140 } 5141 return t.dialTLSDefault 5142 } 5143 5144 func (t *http2Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) { 5145 cn, err := tls.Dial(network, addr, cfg) 5146 if err != nil { 5147 return nil, err 5148 } 5149 if err := cn.Handshake(); err != nil { 5150 return nil, err 5151 } 5152 if !cfg.InsecureSkipVerify { 5153 if err := cn.VerifyHostname(cfg.ServerName); err != nil { 5154 return nil, err 5155 } 5156 } 5157 state := cn.ConnectionState() 5158 if p := state.NegotiatedProtocol; p != http2NextProtoTLS { 5159 return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS) 5160 } 5161 if !state.NegotiatedProtocolIsMutual { 5162 return nil, errors.New("http2: could not negotiate protocol mutually") 5163 } 5164 return cn, nil 5165 } 5166 5167 // disableKeepAlives reports whether connections should be closed as 5168 // soon as possible after handling the first request. 5169 func (t *http2Transport) disableKeepAlives() bool { 5170 return t.t1 != nil && t.t1.DisableKeepAlives 5171 } 5172 5173 func (t *http2Transport) expectContinueTimeout() time.Duration { 5174 if t.t1 == nil { 5175 return 0 5176 } 5177 return http2transportExpectContinueTimeout(t.t1) 5178 } 5179 5180 func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) { 5181 if http2VerboseLogs { 5182 t.vlogf("http2: Transport creating client conn to %v", c.RemoteAddr()) 5183 } 5184 if _, err := c.Write(http2clientPreface); err != nil { 5185 t.vlogf("client preface write error: %v", err) 5186 return nil, err 5187 } 5188 5189 cc := &http2ClientConn{ 5190 t: t, 5191 tconn: c, 5192 readerDone: make(chan struct{}), 5193 nextStreamID: 1, 5194 maxFrameSize: 16 << 10, 5195 initialWindowSize: 65535, 5196 maxConcurrentStreams: 1000, 5197 streams: make(map[uint32]*http2clientStream), 5198 } 5199 cc.cond = sync.NewCond(&cc.mu) 5200 cc.flow.add(int32(http2initialWindowSize)) 5201 5202 cc.bw = bufio.NewWriter(http2stickyErrWriter{c, &cc.werr}) 5203 cc.br = bufio.NewReader(c) 5204 cc.fr = http2NewFramer(cc.bw, cc.br) 5205 cc.fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil) 5206 cc.fr.MaxHeaderListSize = t.maxHeaderListSize() 5207 5208 cc.henc = hpack.NewEncoder(&cc.hbuf) 5209 5210 if cs, ok := c.(http2connectionStater); ok { 5211 state := cs.ConnectionState() 5212 cc.tlsState = &state 5213 } 5214 5215 initialSettings := []http2Setting{ 5216 {ID: http2SettingEnablePush, Val: 0}, 5217 {ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow}, 5218 } 5219 if max := t.maxHeaderListSize(); max != 0 { 5220 initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max}) 5221 } 5222 cc.fr.WriteSettings(initialSettings...) 5223 cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow) 5224 cc.inflow.add(http2transportDefaultConnFlow + http2initialWindowSize) 5225 cc.bw.Flush() 5226 if cc.werr != nil { 5227 return nil, cc.werr 5228 } 5229 5230 f, err := cc.fr.ReadFrame() 5231 if err != nil { 5232 return nil, err 5233 } 5234 sf, ok := f.(*http2SettingsFrame) 5235 if !ok { 5236 return nil, fmt.Errorf("expected settings frame, got: %T", f) 5237 } 5238 cc.fr.WriteSettingsAck() 5239 cc.bw.Flush() 5240 5241 sf.ForeachSetting(func(s http2Setting) error { 5242 switch s.ID { 5243 case http2SettingMaxFrameSize: 5244 cc.maxFrameSize = s.Val 5245 case http2SettingMaxConcurrentStreams: 5246 cc.maxConcurrentStreams = s.Val 5247 case http2SettingInitialWindowSize: 5248 cc.initialWindowSize = s.Val 5249 default: 5250 5251 t.vlogf("Unhandled Setting: %v", s) 5252 } 5253 return nil 5254 }) 5255 5256 go cc.readLoop() 5257 return cc, nil 5258 } 5259 5260 func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) { 5261 cc.mu.Lock() 5262 defer cc.mu.Unlock() 5263 cc.goAway = f 5264 } 5265 5266 func (cc *http2ClientConn) CanTakeNewRequest() bool { 5267 cc.mu.Lock() 5268 defer cc.mu.Unlock() 5269 return cc.canTakeNewRequestLocked() 5270 } 5271 5272 func (cc *http2ClientConn) canTakeNewRequestLocked() bool { 5273 return cc.goAway == nil && !cc.closed && 5274 int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) && 5275 cc.nextStreamID < 2147483647 5276 } 5277 5278 func (cc *http2ClientConn) closeIfIdle() { 5279 cc.mu.Lock() 5280 if len(cc.streams) > 0 { 5281 cc.mu.Unlock() 5282 return 5283 } 5284 cc.closed = true 5285 5286 cc.mu.Unlock() 5287 5288 cc.tconn.Close() 5289 } 5290 5291 const http2maxAllocFrameSize = 512 << 10 5292 5293 // frameBuffer returns a scratch buffer suitable for writing DATA frames. 5294 // They're capped at the min of the peer's max frame size or 512KB 5295 // (kinda arbitrarily), but definitely capped so we don't allocate 4GB 5296 // bufers. 5297 func (cc *http2ClientConn) frameScratchBuffer() []byte { 5298 cc.mu.Lock() 5299 size := cc.maxFrameSize 5300 if size > http2maxAllocFrameSize { 5301 size = http2maxAllocFrameSize 5302 } 5303 for i, buf := range cc.freeBuf { 5304 if len(buf) >= int(size) { 5305 cc.freeBuf[i] = nil 5306 cc.mu.Unlock() 5307 return buf[:size] 5308 } 5309 } 5310 cc.mu.Unlock() 5311 return make([]byte, size) 5312 } 5313 5314 func (cc *http2ClientConn) putFrameScratchBuffer(buf []byte) { 5315 cc.mu.Lock() 5316 defer cc.mu.Unlock() 5317 const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate. 5318 if len(cc.freeBuf) < maxBufs { 5319 cc.freeBuf = append(cc.freeBuf, buf) 5320 return 5321 } 5322 for i, old := range cc.freeBuf { 5323 if old == nil { 5324 cc.freeBuf[i] = buf 5325 return 5326 } 5327 } 5328 5329 } 5330 5331 // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not 5332 // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests. 5333 var http2errRequestCanceled = errors.New("net/http: request canceled") 5334 5335 func http2commaSeparatedTrailers(req *Request) (string, error) { 5336 keys := make([]string, 0, len(req.Trailer)) 5337 for k := range req.Trailer { 5338 k = CanonicalHeaderKey(k) 5339 switch k { 5340 case "Transfer-Encoding", "Trailer", "Content-Length": 5341 return "", &http2badStringError{"invalid Trailer key", k} 5342 } 5343 keys = append(keys, k) 5344 } 5345 if len(keys) > 0 { 5346 sort.Strings(keys) 5347 5348 return strings.Join(keys, ","), nil 5349 } 5350 return "", nil 5351 } 5352 5353 func (cc *http2ClientConn) responseHeaderTimeout() time.Duration { 5354 if cc.t.t1 != nil { 5355 return cc.t.t1.ResponseHeaderTimeout 5356 } 5357 5358 return 0 5359 } 5360 5361 // checkConnHeaders checks whether req has any invalid connection-level headers. 5362 // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields. 5363 // Certain headers are special-cased as okay but not transmitted later. 5364 func http2checkConnHeaders(req *Request) error { 5365 if v := req.Header.Get("Upgrade"); v != "" { 5366 return errors.New("http2: invalid Upgrade request header") 5367 } 5368 if v := req.Header.Get("Transfer-Encoding"); (v != "" && v != "chunked") || len(req.Header["Transfer-Encoding"]) > 1 { 5369 return errors.New("http2: invalid Transfer-Encoding request header") 5370 } 5371 if v := req.Header.Get("Connection"); (v != "" && v != "close" && v != "keep-alive") || len(req.Header["Connection"]) > 1 { 5372 return errors.New("http2: invalid Connection request header") 5373 } 5374 return nil 5375 } 5376 5377 func http2bodyAndLength(req *Request) (body io.Reader, contentLen int64) { 5378 body = req.Body 5379 if body == nil { 5380 return nil, 0 5381 } 5382 if req.ContentLength != 0 { 5383 return req.Body, req.ContentLength 5384 } 5385 5386 // We have a body but a zero content length. Test to see if 5387 // it's actually zero or just unset. 5388 var buf [1]byte 5389 n, rerr := io.ReadFull(body, buf[:]) 5390 if rerr != nil && rerr != io.EOF { 5391 return http2errorReader{rerr}, -1 5392 } 5393 if n == 1 { 5394 5395 return io.MultiReader(bytes.NewReader(buf[:]), body), -1 5396 } 5397 5398 return nil, 0 5399 } 5400 5401 func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) { 5402 if err := http2checkConnHeaders(req); err != nil { 5403 return nil, err 5404 } 5405 5406 trailers, err := http2commaSeparatedTrailers(req) 5407 if err != nil { 5408 return nil, err 5409 } 5410 hasTrailers := trailers != "" 5411 5412 body, contentLen := http2bodyAndLength(req) 5413 hasBody := body != nil 5414 5415 cc.mu.Lock() 5416 cc.lastActive = time.Now() 5417 if cc.closed || !cc.canTakeNewRequestLocked() { 5418 cc.mu.Unlock() 5419 return nil, http2errClientConnUnusable 5420 } 5421 5422 // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere? 5423 var requestedGzip bool 5424 if !cc.t.disableCompression() && 5425 req.Header.Get("Accept-Encoding") == "" && 5426 req.Header.Get("Range") == "" && 5427 req.Method != "HEAD" { 5428 5429 requestedGzip = true 5430 } 5431 5432 hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen) 5433 if err != nil { 5434 cc.mu.Unlock() 5435 return nil, err 5436 } 5437 5438 cs := cc.newStream() 5439 cs.req = req 5440 cs.trace = http2requestTrace(req) 5441 cs.requestedGzip = requestedGzip 5442 bodyWriter := cc.t.getBodyWriterState(cs, body) 5443 cs.on100 = bodyWriter.on100 5444 5445 cc.wmu.Lock() 5446 endStream := !hasBody && !hasTrailers 5447 werr := cc.writeHeaders(cs.ID, endStream, hdrs) 5448 cc.wmu.Unlock() 5449 http2traceWroteHeaders(cs.trace) 5450 cc.mu.Unlock() 5451 5452 if werr != nil { 5453 if hasBody { 5454 req.Body.Close() 5455 bodyWriter.cancel() 5456 } 5457 cc.forgetStreamID(cs.ID) 5458 5459 http2traceWroteRequest(cs.trace, werr) 5460 return nil, werr 5461 } 5462 5463 var respHeaderTimer <-chan time.Time 5464 if hasBody { 5465 bodyWriter.scheduleBodyWrite() 5466 } else { 5467 http2traceWroteRequest(cs.trace, nil) 5468 if d := cc.responseHeaderTimeout(); d != 0 { 5469 timer := time.NewTimer(d) 5470 defer timer.Stop() 5471 respHeaderTimer = timer.C 5472 } 5473 } 5474 5475 readLoopResCh := cs.resc 5476 bodyWritten := false 5477 ctx := http2reqContext(req) 5478 5479 for { 5480 select { 5481 case re := <-readLoopResCh: 5482 res := re.res 5483 if re.err != nil || res.StatusCode > 299 { 5484 5485 bodyWriter.cancel() 5486 cs.abortRequestBodyWrite(http2errStopReqBodyWrite) 5487 } 5488 if re.err != nil { 5489 cc.forgetStreamID(cs.ID) 5490 return nil, re.err 5491 } 5492 res.Request = req 5493 res.TLS = cc.tlsState 5494 return res, nil 5495 case <-respHeaderTimer: 5496 cc.forgetStreamID(cs.ID) 5497 if !hasBody || bodyWritten { 5498 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5499 } else { 5500 bodyWriter.cancel() 5501 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 5502 } 5503 return nil, http2errTimeout 5504 case <-ctx.Done(): 5505 cc.forgetStreamID(cs.ID) 5506 if !hasBody || bodyWritten { 5507 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5508 } else { 5509 bodyWriter.cancel() 5510 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 5511 } 5512 return nil, ctx.Err() 5513 case <-req.Cancel: 5514 cc.forgetStreamID(cs.ID) 5515 if !hasBody || bodyWritten { 5516 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5517 } else { 5518 bodyWriter.cancel() 5519 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 5520 } 5521 return nil, http2errRequestCanceled 5522 case <-cs.peerReset: 5523 5524 return nil, cs.resetErr 5525 case err := <-bodyWriter.resc: 5526 if err != nil { 5527 return nil, err 5528 } 5529 bodyWritten = true 5530 if d := cc.responseHeaderTimeout(); d != 0 { 5531 timer := time.NewTimer(d) 5532 defer timer.Stop() 5533 respHeaderTimer = timer.C 5534 } 5535 } 5536 } 5537 } 5538 5539 // requires cc.wmu be held 5540 func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error { 5541 first := true 5542 frameSize := int(cc.maxFrameSize) 5543 for len(hdrs) > 0 && cc.werr == nil { 5544 chunk := hdrs 5545 if len(chunk) > frameSize { 5546 chunk = chunk[:frameSize] 5547 } 5548 hdrs = hdrs[len(chunk):] 5549 endHeaders := len(hdrs) == 0 5550 if first { 5551 cc.fr.WriteHeaders(http2HeadersFrameParam{ 5552 StreamID: streamID, 5553 BlockFragment: chunk, 5554 EndStream: endStream, 5555 EndHeaders: endHeaders, 5556 }) 5557 first = false 5558 } else { 5559 cc.fr.WriteContinuation(streamID, endHeaders, chunk) 5560 } 5561 } 5562 5563 cc.bw.Flush() 5564 return cc.werr 5565 } 5566 5567 // internal error values; they don't escape to callers 5568 var ( 5569 // abort request body write; don't send cancel 5570 http2errStopReqBodyWrite = errors.New("http2: aborting request body write") 5571 5572 // abort request body write, but send stream reset of cancel. 5573 http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request") 5574 ) 5575 5576 func (cs *http2clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) { 5577 cc := cs.cc 5578 sentEnd := false 5579 buf := cc.frameScratchBuffer() 5580 defer cc.putFrameScratchBuffer(buf) 5581 5582 defer func() { 5583 http2traceWroteRequest(cs.trace, err) 5584 5585 cerr := bodyCloser.Close() 5586 if err == nil { 5587 err = cerr 5588 } 5589 }() 5590 5591 req := cs.req 5592 hasTrailers := req.Trailer != nil 5593 5594 var sawEOF bool 5595 for !sawEOF { 5596 n, err := body.Read(buf) 5597 if err == io.EOF { 5598 sawEOF = true 5599 err = nil 5600 } else if err != nil { 5601 return err 5602 } 5603 5604 remain := buf[:n] 5605 for len(remain) > 0 && err == nil { 5606 var allowed int32 5607 allowed, err = cs.awaitFlowControl(len(remain)) 5608 switch { 5609 case err == http2errStopReqBodyWrite: 5610 return err 5611 case err == http2errStopReqBodyWriteAndCancel: 5612 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5613 return err 5614 case err != nil: 5615 return err 5616 } 5617 cc.wmu.Lock() 5618 data := remain[:allowed] 5619 remain = remain[allowed:] 5620 sentEnd = sawEOF && len(remain) == 0 && !hasTrailers 5621 err = cc.fr.WriteData(cs.ID, sentEnd, data) 5622 if err == nil { 5623 5624 err = cc.bw.Flush() 5625 } 5626 cc.wmu.Unlock() 5627 } 5628 if err != nil { 5629 return err 5630 } 5631 } 5632 5633 cc.wmu.Lock() 5634 if !sentEnd { 5635 var trls []byte 5636 if hasTrailers { 5637 cc.mu.Lock() 5638 trls = cc.encodeTrailers(req) 5639 cc.mu.Unlock() 5640 } 5641 5642 if len(trls) > 0 { 5643 err = cc.writeHeaders(cs.ID, true, trls) 5644 } else { 5645 err = cc.fr.WriteData(cs.ID, true, nil) 5646 } 5647 } 5648 if ferr := cc.bw.Flush(); ferr != nil && err == nil { 5649 err = ferr 5650 } 5651 cc.wmu.Unlock() 5652 5653 return err 5654 } 5655 5656 // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow 5657 // control tokens from the server. 5658 // It returns either the non-zero number of tokens taken or an error 5659 // if the stream is dead. 5660 func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) { 5661 cc := cs.cc 5662 cc.mu.Lock() 5663 defer cc.mu.Unlock() 5664 for { 5665 if cc.closed { 5666 return 0, http2errClientConnClosed 5667 } 5668 if cs.stopReqBody != nil { 5669 return 0, cs.stopReqBody 5670 } 5671 if err := cs.checkResetOrDone(); err != nil { 5672 return 0, err 5673 } 5674 if a := cs.flow.available(); a > 0 { 5675 take := a 5676 if int(take) > maxBytes { 5677 5678 take = int32(maxBytes) 5679 } 5680 if take > int32(cc.maxFrameSize) { 5681 take = int32(cc.maxFrameSize) 5682 } 5683 cs.flow.take(take) 5684 return take, nil 5685 } 5686 cc.cond.Wait() 5687 } 5688 } 5689 5690 type http2badStringError struct { 5691 what string 5692 str string 5693 } 5694 5695 func (e *http2badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 5696 5697 // requires cc.mu be held. 5698 func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) { 5699 cc.hbuf.Reset() 5700 5701 host := req.Host 5702 if host == "" { 5703 host = req.URL.Host 5704 } 5705 5706 for k, vv := range req.Header { 5707 if !httplex.ValidHeaderFieldName(k) { 5708 return nil, fmt.Errorf("invalid HTTP header name %q", k) 5709 } 5710 for _, v := range vv { 5711 if !httplex.ValidHeaderFieldValue(v) { 5712 return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k) 5713 } 5714 } 5715 } 5716 5717 cc.writeHeader(":authority", host) 5718 cc.writeHeader(":method", req.Method) 5719 if req.Method != "CONNECT" { 5720 cc.writeHeader(":path", req.URL.RequestURI()) 5721 cc.writeHeader(":scheme", "https") 5722 } 5723 if trailers != "" { 5724 cc.writeHeader("trailer", trailers) 5725 } 5726 5727 var didUA bool 5728 for k, vv := range req.Header { 5729 lowKey := strings.ToLower(k) 5730 switch lowKey { 5731 case "host", "content-length": 5732 5733 continue 5734 case "connection", "proxy-connection", "transfer-encoding", "upgrade", "keep-alive": 5735 5736 continue 5737 case "user-agent": 5738 5739 didUA = true 5740 if len(vv) < 1 { 5741 continue 5742 } 5743 vv = vv[:1] 5744 if vv[0] == "" { 5745 continue 5746 } 5747 } 5748 for _, v := range vv { 5749 cc.writeHeader(lowKey, v) 5750 } 5751 } 5752 if http2shouldSendReqContentLength(req.Method, contentLength) { 5753 cc.writeHeader("content-length", strconv.FormatInt(contentLength, 10)) 5754 } 5755 if addGzipHeader { 5756 cc.writeHeader("accept-encoding", "gzip") 5757 } 5758 if !didUA { 5759 cc.writeHeader("user-agent", http2defaultUserAgent) 5760 } 5761 return cc.hbuf.Bytes(), nil 5762 } 5763 5764 // shouldSendReqContentLength reports whether the http2.Transport should send 5765 // a "content-length" request header. This logic is basically a copy of the net/http 5766 // transferWriter.shouldSendContentLength. 5767 // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown). 5768 // -1 means unknown. 5769 func http2shouldSendReqContentLength(method string, contentLength int64) bool { 5770 if contentLength > 0 { 5771 return true 5772 } 5773 if contentLength < 0 { 5774 return false 5775 } 5776 5777 switch method { 5778 case "POST", "PUT", "PATCH": 5779 return true 5780 default: 5781 return false 5782 } 5783 } 5784 5785 // requires cc.mu be held. 5786 func (cc *http2ClientConn) encodeTrailers(req *Request) []byte { 5787 cc.hbuf.Reset() 5788 for k, vv := range req.Trailer { 5789 5790 lowKey := strings.ToLower(k) 5791 for _, v := range vv { 5792 cc.writeHeader(lowKey, v) 5793 } 5794 } 5795 return cc.hbuf.Bytes() 5796 } 5797 5798 func (cc *http2ClientConn) writeHeader(name, value string) { 5799 if http2VerboseLogs { 5800 log.Printf("http2: Transport encoding header %q = %q", name, value) 5801 } 5802 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 5803 } 5804 5805 type http2resAndError struct { 5806 res *Response 5807 err error 5808 } 5809 5810 // requires cc.mu be held. 5811 func (cc *http2ClientConn) newStream() *http2clientStream { 5812 cs := &http2clientStream{ 5813 cc: cc, 5814 ID: cc.nextStreamID, 5815 resc: make(chan http2resAndError, 1), 5816 peerReset: make(chan struct{}), 5817 done: make(chan struct{}), 5818 } 5819 cs.flow.add(int32(cc.initialWindowSize)) 5820 cs.flow.setConnFlow(&cc.flow) 5821 cs.inflow.add(http2transportDefaultStreamFlow) 5822 cs.inflow.setConnFlow(&cc.inflow) 5823 cc.nextStreamID += 2 5824 cc.streams[cs.ID] = cs 5825 return cs 5826 } 5827 5828 func (cc *http2ClientConn) forgetStreamID(id uint32) { 5829 cc.streamByID(id, true) 5830 } 5831 5832 func (cc *http2ClientConn) streamByID(id uint32, andRemove bool) *http2clientStream { 5833 cc.mu.Lock() 5834 defer cc.mu.Unlock() 5835 cs := cc.streams[id] 5836 if andRemove && cs != nil && !cc.closed { 5837 cc.lastActive = time.Now() 5838 delete(cc.streams, id) 5839 close(cs.done) 5840 cc.cond.Broadcast() 5841 } 5842 return cs 5843 } 5844 5845 // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop. 5846 type http2clientConnReadLoop struct { 5847 cc *http2ClientConn 5848 activeRes map[uint32]*http2clientStream // keyed by streamID 5849 closeWhenIdle bool 5850 } 5851 5852 // readLoop runs in its own goroutine and reads and dispatches frames. 5853 func (cc *http2ClientConn) readLoop() { 5854 rl := &http2clientConnReadLoop{ 5855 cc: cc, 5856 activeRes: make(map[uint32]*http2clientStream), 5857 } 5858 5859 defer rl.cleanup() 5860 cc.readerErr = rl.run() 5861 if ce, ok := cc.readerErr.(http2ConnectionError); ok { 5862 cc.wmu.Lock() 5863 cc.fr.WriteGoAway(0, http2ErrCode(ce), nil) 5864 cc.wmu.Unlock() 5865 } 5866 } 5867 5868 func (rl *http2clientConnReadLoop) cleanup() { 5869 cc := rl.cc 5870 defer cc.tconn.Close() 5871 defer cc.t.connPool().MarkDead(cc) 5872 defer close(cc.readerDone) 5873 5874 err := cc.readerErr 5875 if err == io.EOF { 5876 err = io.ErrUnexpectedEOF 5877 } 5878 cc.mu.Lock() 5879 for _, cs := range rl.activeRes { 5880 cs.bufPipe.CloseWithError(err) 5881 } 5882 for _, cs := range cc.streams { 5883 select { 5884 case cs.resc <- http2resAndError{err: err}: 5885 default: 5886 } 5887 close(cs.done) 5888 } 5889 cc.closed = true 5890 cc.cond.Broadcast() 5891 cc.mu.Unlock() 5892 } 5893 5894 func (rl *http2clientConnReadLoop) run() error { 5895 cc := rl.cc 5896 rl.closeWhenIdle = cc.t.disableKeepAlives() 5897 gotReply := false 5898 for { 5899 f, err := cc.fr.ReadFrame() 5900 if err != nil { 5901 cc.vlogf("Transport readFrame error: (%T) %v", err, err) 5902 } 5903 if se, ok := err.(http2StreamError); ok { 5904 if cs := cc.streamByID(se.StreamID, true); cs != nil { 5905 rl.endStreamError(cs, cc.fr.errDetail) 5906 } 5907 continue 5908 } else if err != nil { 5909 return err 5910 } 5911 if http2VerboseLogs { 5912 cc.vlogf("http2: Transport received %s", http2summarizeFrame(f)) 5913 } 5914 maybeIdle := false 5915 5916 switch f := f.(type) { 5917 case *http2MetaHeadersFrame: 5918 err = rl.processHeaders(f) 5919 maybeIdle = true 5920 gotReply = true 5921 case *http2DataFrame: 5922 err = rl.processData(f) 5923 maybeIdle = true 5924 case *http2GoAwayFrame: 5925 err = rl.processGoAway(f) 5926 maybeIdle = true 5927 case *http2RSTStreamFrame: 5928 err = rl.processResetStream(f) 5929 maybeIdle = true 5930 case *http2SettingsFrame: 5931 err = rl.processSettings(f) 5932 case *http2PushPromiseFrame: 5933 err = rl.processPushPromise(f) 5934 case *http2WindowUpdateFrame: 5935 err = rl.processWindowUpdate(f) 5936 case *http2PingFrame: 5937 err = rl.processPing(f) 5938 default: 5939 cc.logf("Transport: unhandled response frame type %T", f) 5940 } 5941 if err != nil { 5942 return err 5943 } 5944 if rl.closeWhenIdle && gotReply && maybeIdle && len(rl.activeRes) == 0 { 5945 cc.closeIfIdle() 5946 } 5947 } 5948 } 5949 5950 func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error { 5951 cc := rl.cc 5952 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 5953 if cs == nil { 5954 5955 return nil 5956 } 5957 if !cs.firstByte { 5958 if cs.trace != nil { 5959 5960 http2traceFirstResponseByte(cs.trace) 5961 } 5962 cs.firstByte = true 5963 } 5964 if !cs.pastHeaders { 5965 cs.pastHeaders = true 5966 } else { 5967 return rl.processTrailers(cs, f) 5968 } 5969 5970 res, err := rl.handleResponse(cs, f) 5971 if err != nil { 5972 if _, ok := err.(http2ConnectionError); ok { 5973 return err 5974 } 5975 5976 cs.cc.writeStreamReset(f.StreamID, http2ErrCodeProtocol, err) 5977 cs.resc <- http2resAndError{err: err} 5978 return nil 5979 } 5980 if res == nil { 5981 5982 return nil 5983 } 5984 if res.Body != http2noBody { 5985 rl.activeRes[cs.ID] = cs 5986 } 5987 cs.resTrailer = &res.Trailer 5988 cs.resc <- http2resAndError{res: res} 5989 return nil 5990 } 5991 5992 // may return error types nil, or ConnectionError. Any other error value 5993 // is a StreamError of type ErrCodeProtocol. The returned error in that case 5994 // is the detail. 5995 // 5996 // As a special case, handleResponse may return (nil, nil) to skip the 5997 // frame (currently only used for 100 expect continue). This special 5998 // case is going away after Issue 13851 is fixed. 5999 func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) { 6000 if f.Truncated { 6001 return nil, http2errResponseHeaderListSize 6002 } 6003 6004 status := f.PseudoValue("status") 6005 if status == "" { 6006 return nil, errors.New("missing status pseudo header") 6007 } 6008 statusCode, err := strconv.Atoi(status) 6009 if err != nil { 6010 return nil, errors.New("malformed non-numeric status pseudo header") 6011 } 6012 6013 if statusCode == 100 { 6014 http2traceGot100Continue(cs.trace) 6015 if cs.on100 != nil { 6016 cs.on100() 6017 } 6018 cs.pastHeaders = false 6019 return nil, nil 6020 } 6021 6022 header := make(Header) 6023 res := &Response{ 6024 Proto: "HTTP/2.0", 6025 ProtoMajor: 2, 6026 Header: header, 6027 StatusCode: statusCode, 6028 Status: status + " " + StatusText(statusCode), 6029 } 6030 for _, hf := range f.RegularFields() { 6031 key := CanonicalHeaderKey(hf.Name) 6032 if key == "Trailer" { 6033 t := res.Trailer 6034 if t == nil { 6035 t = make(Header) 6036 res.Trailer = t 6037 } 6038 http2foreachHeaderElement(hf.Value, func(v string) { 6039 t[CanonicalHeaderKey(v)] = nil 6040 }) 6041 } else { 6042 header[key] = append(header[key], hf.Value) 6043 } 6044 } 6045 6046 streamEnded := f.StreamEnded() 6047 isHead := cs.req.Method == "HEAD" 6048 if !streamEnded || isHead { 6049 res.ContentLength = -1 6050 if clens := res.Header["Content-Length"]; len(clens) == 1 { 6051 if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil { 6052 res.ContentLength = clen64 6053 } else { 6054 6055 } 6056 } else if len(clens) > 1 { 6057 6058 } 6059 } 6060 6061 if streamEnded || isHead { 6062 res.Body = http2noBody 6063 return res, nil 6064 } 6065 6066 buf := new(bytes.Buffer) 6067 cs.bufPipe = http2pipe{b: buf} 6068 cs.bytesRemain = res.ContentLength 6069 res.Body = http2transportResponseBody{cs} 6070 go cs.awaitRequestCancel(cs.req) 6071 6072 if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" { 6073 res.Header.Del("Content-Encoding") 6074 res.Header.Del("Content-Length") 6075 res.ContentLength = -1 6076 res.Body = &http2gzipReader{body: res.Body} 6077 http2setResponseUncompressed(res) 6078 } 6079 return res, nil 6080 } 6081 6082 func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error { 6083 if cs.pastTrailers { 6084 6085 return http2ConnectionError(http2ErrCodeProtocol) 6086 } 6087 cs.pastTrailers = true 6088 if !f.StreamEnded() { 6089 6090 return http2ConnectionError(http2ErrCodeProtocol) 6091 } 6092 if len(f.PseudoFields()) > 0 { 6093 6094 return http2ConnectionError(http2ErrCodeProtocol) 6095 } 6096 6097 trailer := make(Header) 6098 for _, hf := range f.RegularFields() { 6099 key := CanonicalHeaderKey(hf.Name) 6100 trailer[key] = append(trailer[key], hf.Value) 6101 } 6102 cs.trailer = trailer 6103 6104 rl.endStream(cs) 6105 return nil 6106 } 6107 6108 // transportResponseBody is the concrete type of Transport.RoundTrip's 6109 // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body. 6110 // On Close it sends RST_STREAM if EOF wasn't already seen. 6111 type http2transportResponseBody struct { 6112 cs *http2clientStream 6113 } 6114 6115 func (b http2transportResponseBody) Read(p []byte) (n int, err error) { 6116 cs := b.cs 6117 cc := cs.cc 6118 6119 if cs.readErr != nil { 6120 return 0, cs.readErr 6121 } 6122 n, err = b.cs.bufPipe.Read(p) 6123 if cs.bytesRemain != -1 { 6124 if int64(n) > cs.bytesRemain { 6125 n = int(cs.bytesRemain) 6126 if err == nil { 6127 err = errors.New("net/http: server replied with more than declared Content-Length; truncated") 6128 cc.writeStreamReset(cs.ID, http2ErrCodeProtocol, err) 6129 } 6130 cs.readErr = err 6131 return int(cs.bytesRemain), err 6132 } 6133 cs.bytesRemain -= int64(n) 6134 if err == io.EOF && cs.bytesRemain > 0 { 6135 err = io.ErrUnexpectedEOF 6136 cs.readErr = err 6137 return n, err 6138 } 6139 } 6140 if n == 0 { 6141 6142 return 6143 } 6144 6145 cc.mu.Lock() 6146 defer cc.mu.Unlock() 6147 6148 var connAdd, streamAdd int32 6149 6150 if v := cc.inflow.available(); v < http2transportDefaultConnFlow/2 { 6151 connAdd = http2transportDefaultConnFlow - v 6152 cc.inflow.add(connAdd) 6153 } 6154 if err == nil { 6155 if v := cs.inflow.available(); v < http2transportDefaultStreamFlow-http2transportDefaultStreamMinRefresh { 6156 streamAdd = http2transportDefaultStreamFlow - v 6157 cs.inflow.add(streamAdd) 6158 } 6159 } 6160 if connAdd != 0 || streamAdd != 0 { 6161 cc.wmu.Lock() 6162 defer cc.wmu.Unlock() 6163 if connAdd != 0 { 6164 cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd)) 6165 } 6166 if streamAdd != 0 { 6167 cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd)) 6168 } 6169 cc.bw.Flush() 6170 } 6171 return 6172 } 6173 6174 var http2errClosedResponseBody = errors.New("http2: response body closed") 6175 6176 func (b http2transportResponseBody) Close() error { 6177 cs := b.cs 6178 if cs.bufPipe.Err() != io.EOF { 6179 6180 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 6181 } 6182 cs.bufPipe.BreakWithError(http2errClosedResponseBody) 6183 return nil 6184 } 6185 6186 func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error { 6187 cc := rl.cc 6188 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 6189 if cs == nil { 6190 cc.mu.Lock() 6191 neverSent := cc.nextStreamID 6192 cc.mu.Unlock() 6193 if f.StreamID >= neverSent { 6194 6195 cc.logf("http2: Transport received unsolicited DATA frame; closing connection") 6196 return http2ConnectionError(http2ErrCodeProtocol) 6197 } 6198 6199 return nil 6200 } 6201 if data := f.Data(); len(data) > 0 { 6202 if cs.bufPipe.b == nil { 6203 6204 cc.logf("http2: Transport received DATA frame for closed stream; closing connection") 6205 return http2ConnectionError(http2ErrCodeProtocol) 6206 } 6207 6208 cc.mu.Lock() 6209 if cs.inflow.available() >= int32(len(data)) { 6210 cs.inflow.take(int32(len(data))) 6211 } else { 6212 cc.mu.Unlock() 6213 return http2ConnectionError(http2ErrCodeFlowControl) 6214 } 6215 cc.mu.Unlock() 6216 6217 if _, err := cs.bufPipe.Write(data); err != nil { 6218 rl.endStreamError(cs, err) 6219 return err 6220 } 6221 } 6222 6223 if f.StreamEnded() { 6224 rl.endStream(cs) 6225 } 6226 return nil 6227 } 6228 6229 var http2errInvalidTrailers = errors.New("http2: invalid trailers") 6230 6231 func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) { 6232 6233 rl.endStreamError(cs, nil) 6234 } 6235 6236 func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) { 6237 var code func() 6238 if err == nil { 6239 err = io.EOF 6240 code = cs.copyTrailers 6241 } 6242 cs.bufPipe.closeWithErrorAndCode(err, code) 6243 delete(rl.activeRes, cs.ID) 6244 if cs.req.Close || cs.req.Header.Get("Connection") == "close" { 6245 rl.closeWhenIdle = true 6246 } 6247 } 6248 6249 func (cs *http2clientStream) copyTrailers() { 6250 for k, vv := range cs.trailer { 6251 t := cs.resTrailer 6252 if *t == nil { 6253 *t = make(Header) 6254 } 6255 (*t)[k] = vv 6256 } 6257 } 6258 6259 func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error { 6260 cc := rl.cc 6261 cc.t.connPool().MarkDead(cc) 6262 if f.ErrCode != 0 { 6263 6264 cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode) 6265 } 6266 cc.setGoAway(f) 6267 return nil 6268 } 6269 6270 func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error { 6271 cc := rl.cc 6272 cc.mu.Lock() 6273 defer cc.mu.Unlock() 6274 return f.ForeachSetting(func(s http2Setting) error { 6275 switch s.ID { 6276 case http2SettingMaxFrameSize: 6277 cc.maxFrameSize = s.Val 6278 case http2SettingMaxConcurrentStreams: 6279 cc.maxConcurrentStreams = s.Val 6280 case http2SettingInitialWindowSize: 6281 6282 cc.initialWindowSize = s.Val 6283 default: 6284 6285 cc.vlogf("Unhandled Setting: %v", s) 6286 } 6287 return nil 6288 }) 6289 } 6290 6291 func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error { 6292 cc := rl.cc 6293 cs := cc.streamByID(f.StreamID, false) 6294 if f.StreamID != 0 && cs == nil { 6295 return nil 6296 } 6297 6298 cc.mu.Lock() 6299 defer cc.mu.Unlock() 6300 6301 fl := &cc.flow 6302 if cs != nil { 6303 fl = &cs.flow 6304 } 6305 if !fl.add(int32(f.Increment)) { 6306 return http2ConnectionError(http2ErrCodeFlowControl) 6307 } 6308 cc.cond.Broadcast() 6309 return nil 6310 } 6311 6312 func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error { 6313 cs := rl.cc.streamByID(f.StreamID, true) 6314 if cs == nil { 6315 6316 return nil 6317 } 6318 select { 6319 case <-cs.peerReset: 6320 6321 default: 6322 err := http2StreamError{cs.ID, f.ErrCode} 6323 cs.resetErr = err 6324 close(cs.peerReset) 6325 cs.bufPipe.CloseWithError(err) 6326 cs.cc.cond.Broadcast() 6327 } 6328 delete(rl.activeRes, cs.ID) 6329 return nil 6330 } 6331 6332 func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error { 6333 if f.IsAck() { 6334 6335 return nil 6336 } 6337 cc := rl.cc 6338 cc.wmu.Lock() 6339 defer cc.wmu.Unlock() 6340 if err := cc.fr.WritePing(true, f.Data); err != nil { 6341 return err 6342 } 6343 return cc.bw.Flush() 6344 } 6345 6346 func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error { 6347 6348 return http2ConnectionError(http2ErrCodeProtocol) 6349 } 6350 6351 func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) { 6352 6353 cc.wmu.Lock() 6354 cc.fr.WriteRSTStream(streamID, code) 6355 cc.bw.Flush() 6356 cc.wmu.Unlock() 6357 } 6358 6359 var ( 6360 http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit") 6361 http2errPseudoTrailers = errors.New("http2: invalid pseudo header in trailers") 6362 ) 6363 6364 func (cc *http2ClientConn) logf(format string, args ...interface{}) { 6365 cc.t.logf(format, args...) 6366 } 6367 6368 func (cc *http2ClientConn) vlogf(format string, args ...interface{}) { 6369 cc.t.vlogf(format, args...) 6370 } 6371 6372 func (t *http2Transport) vlogf(format string, args ...interface{}) { 6373 if http2VerboseLogs { 6374 t.logf(format, args...) 6375 } 6376 } 6377 6378 func (t *http2Transport) logf(format string, args ...interface{}) { 6379 log.Printf(format, args...) 6380 } 6381 6382 var http2noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil)) 6383 6384 func http2strSliceContains(ss []string, s string) bool { 6385 for _, v := range ss { 6386 if v == s { 6387 return true 6388 } 6389 } 6390 return false 6391 } 6392 6393 type http2erringRoundTripper struct{ err error } 6394 6395 func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err } 6396 6397 // gzipReader wraps a response body so it can lazily 6398 // call gzip.NewReader on the first call to Read 6399 type http2gzipReader struct { 6400 body io.ReadCloser // underlying Response.Body 6401 zr *gzip.Reader // lazily-initialized gzip reader 6402 zerr error // sticky error 6403 } 6404 6405 func (gz *http2gzipReader) Read(p []byte) (n int, err error) { 6406 if gz.zerr != nil { 6407 return 0, gz.zerr 6408 } 6409 if gz.zr == nil { 6410 gz.zr, err = gzip.NewReader(gz.body) 6411 if err != nil { 6412 gz.zerr = err 6413 return 0, err 6414 } 6415 } 6416 return gz.zr.Read(p) 6417 } 6418 6419 func (gz *http2gzipReader) Close() error { 6420 return gz.body.Close() 6421 } 6422 6423 type http2errorReader struct{ err error } 6424 6425 func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err } 6426 6427 // bodyWriterState encapsulates various state around the Transport's writing 6428 // of the request body, particularly regarding doing delayed writes of the body 6429 // when the request contains "Expect: 100-continue". 6430 type http2bodyWriterState struct { 6431 cs *http2clientStream 6432 timer *time.Timer // if non-nil, we're doing a delayed write 6433 fnonce *sync.Once // to call fn with 6434 fn func() // the code to run in the goroutine, writing the body 6435 resc chan error // result of fn's execution 6436 delay time.Duration // how long we should delay a delayed write for 6437 } 6438 6439 func (t *http2Transport) getBodyWriterState(cs *http2clientStream, body io.Reader) (s http2bodyWriterState) { 6440 s.cs = cs 6441 if body == nil { 6442 return 6443 } 6444 resc := make(chan error, 1) 6445 s.resc = resc 6446 s.fn = func() { 6447 resc <- cs.writeRequestBody(body, cs.req.Body) 6448 } 6449 s.delay = t.expectContinueTimeout() 6450 if s.delay == 0 || 6451 !httplex.HeaderValuesContainsToken( 6452 cs.req.Header["Expect"], 6453 "100-continue") { 6454 return 6455 } 6456 s.fnonce = new(sync.Once) 6457 6458 // Arm the timer with a very large duration, which we'll 6459 // intentionally lower later. It has to be large now because 6460 // we need a handle to it before writing the headers, but the 6461 // s.delay value is defined to not start until after the 6462 // request headers were written. 6463 const hugeDuration = 365 * 24 * time.Hour 6464 s.timer = time.AfterFunc(hugeDuration, func() { 6465 s.fnonce.Do(s.fn) 6466 }) 6467 return 6468 } 6469 6470 func (s http2bodyWriterState) cancel() { 6471 if s.timer != nil { 6472 s.timer.Stop() 6473 } 6474 } 6475 6476 func (s http2bodyWriterState) on100() { 6477 if s.timer == nil { 6478 6479 return 6480 } 6481 s.timer.Stop() 6482 go func() { s.fnonce.Do(s.fn) }() 6483 } 6484 6485 // scheduleBodyWrite starts writing the body, either immediately (in 6486 // the common case) or after the delay timeout. It should not be 6487 // called until after the headers have been written. 6488 func (s http2bodyWriterState) scheduleBodyWrite() { 6489 if s.timer == nil { 6490 6491 go s.fn() 6492 return 6493 } 6494 http2traceWait100Continue(s.cs.trace) 6495 if s.timer.Stop() { 6496 s.timer.Reset(s.delay) 6497 } 6498 } 6499 6500 // writeFramer is implemented by any type that is used to write frames. 6501 type http2writeFramer interface { 6502 writeFrame(http2writeContext) error 6503 } 6504 6505 // writeContext is the interface needed by the various frame writer 6506 // types below. All the writeFrame methods below are scheduled via the 6507 // frame writing scheduler (see writeScheduler in writesched.go). 6508 // 6509 // This interface is implemented by *serverConn. 6510 // 6511 // TODO: decide whether to a) use this in the client code (which didn't 6512 // end up using this yet, because it has a simpler design, not 6513 // currently implementing priorities), or b) delete this and 6514 // make the server code a bit more concrete. 6515 type http2writeContext interface { 6516 Framer() *http2Framer 6517 Flush() error 6518 CloseConn() error 6519 // HeaderEncoder returns an HPACK encoder that writes to the 6520 // returned buffer. 6521 HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) 6522 } 6523 6524 // endsStream reports whether the given frame writer w will locally 6525 // close the stream. 6526 func http2endsStream(w http2writeFramer) bool { 6527 switch v := w.(type) { 6528 case *http2writeData: 6529 return v.endStream 6530 case *http2writeResHeaders: 6531 return v.endStream 6532 case nil: 6533 6534 panic("endsStream called on nil writeFramer") 6535 } 6536 return false 6537 } 6538 6539 type http2flushFrameWriter struct{} 6540 6541 func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error { 6542 return ctx.Flush() 6543 } 6544 6545 type http2writeSettings []http2Setting 6546 6547 func (s http2writeSettings) writeFrame(ctx http2writeContext) error { 6548 return ctx.Framer().WriteSettings([]http2Setting(s)...) 6549 } 6550 6551 type http2writeGoAway struct { 6552 maxStreamID uint32 6553 code http2ErrCode 6554 } 6555 6556 func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error { 6557 err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil) 6558 if p.code != 0 { 6559 ctx.Flush() 6560 time.Sleep(50 * time.Millisecond) 6561 ctx.CloseConn() 6562 } 6563 return err 6564 } 6565 6566 type http2writeData struct { 6567 streamID uint32 6568 p []byte 6569 endStream bool 6570 } 6571 6572 func (w *http2writeData) String() string { 6573 return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream) 6574 } 6575 6576 func (w *http2writeData) writeFrame(ctx http2writeContext) error { 6577 return ctx.Framer().WriteData(w.streamID, w.endStream, w.p) 6578 } 6579 6580 // handlerPanicRST is the message sent from handler goroutines when 6581 // the handler panics. 6582 type http2handlerPanicRST struct { 6583 StreamID uint32 6584 } 6585 6586 func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error { 6587 return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal) 6588 } 6589 6590 func (se http2StreamError) writeFrame(ctx http2writeContext) error { 6591 return ctx.Framer().WriteRSTStream(se.StreamID, se.Code) 6592 } 6593 6594 type http2writePingAck struct{ pf *http2PingFrame } 6595 6596 func (w http2writePingAck) writeFrame(ctx http2writeContext) error { 6597 return ctx.Framer().WritePing(true, w.pf.Data) 6598 } 6599 6600 type http2writeSettingsAck struct{} 6601 6602 func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error { 6603 return ctx.Framer().WriteSettingsAck() 6604 } 6605 6606 // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames 6607 // for HTTP response headers or trailers from a server handler. 6608 type http2writeResHeaders struct { 6609 streamID uint32 6610 httpResCode int // 0 means no ":status" line 6611 h Header // may be nil 6612 trailers []string // if non-nil, which keys of h to write. nil means all. 6613 endStream bool 6614 6615 date string 6616 contentType string 6617 contentLength string 6618 } 6619 6620 func http2encKV(enc *hpack.Encoder, k, v string) { 6621 if http2VerboseLogs { 6622 log.Printf("http2: server encoding header %q = %q", k, v) 6623 } 6624 enc.WriteField(hpack.HeaderField{Name: k, Value: v}) 6625 } 6626 6627 func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error { 6628 enc, buf := ctx.HeaderEncoder() 6629 buf.Reset() 6630 6631 if w.httpResCode != 0 { 6632 http2encKV(enc, ":status", http2httpCodeString(w.httpResCode)) 6633 } 6634 6635 http2encodeHeaders(enc, w.h, w.trailers) 6636 6637 if w.contentType != "" { 6638 http2encKV(enc, "content-type", w.contentType) 6639 } 6640 if w.contentLength != "" { 6641 http2encKV(enc, "content-length", w.contentLength) 6642 } 6643 if w.date != "" { 6644 http2encKV(enc, "date", w.date) 6645 } 6646 6647 headerBlock := buf.Bytes() 6648 if len(headerBlock) == 0 && w.trailers == nil { 6649 panic("unexpected empty hpack") 6650 } 6651 6652 // For now we're lazy and just pick the minimum MAX_FRAME_SIZE 6653 // that all peers must support (16KB). Later we could care 6654 // more and send larger frames if the peer advertised it, but 6655 // there's little point. Most headers are small anyway (so we 6656 // generally won't have CONTINUATION frames), and extra frames 6657 // only waste 9 bytes anyway. 6658 const maxFrameSize = 16384 6659 6660 first := true 6661 for len(headerBlock) > 0 { 6662 frag := headerBlock 6663 if len(frag) > maxFrameSize { 6664 frag = frag[:maxFrameSize] 6665 } 6666 headerBlock = headerBlock[len(frag):] 6667 endHeaders := len(headerBlock) == 0 6668 var err error 6669 if first { 6670 first = false 6671 err = ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 6672 StreamID: w.streamID, 6673 BlockFragment: frag, 6674 EndStream: w.endStream, 6675 EndHeaders: endHeaders, 6676 }) 6677 } else { 6678 err = ctx.Framer().WriteContinuation(w.streamID, endHeaders, frag) 6679 } 6680 if err != nil { 6681 return err 6682 } 6683 } 6684 return nil 6685 } 6686 6687 type http2write100ContinueHeadersFrame struct { 6688 streamID uint32 6689 } 6690 6691 func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error { 6692 enc, buf := ctx.HeaderEncoder() 6693 buf.Reset() 6694 http2encKV(enc, ":status", "100") 6695 return ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 6696 StreamID: w.streamID, 6697 BlockFragment: buf.Bytes(), 6698 EndStream: false, 6699 EndHeaders: true, 6700 }) 6701 } 6702 6703 type http2writeWindowUpdate struct { 6704 streamID uint32 // or 0 for conn-level 6705 n uint32 6706 } 6707 6708 func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error { 6709 return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n) 6710 } 6711 6712 func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) { 6713 if keys == nil { 6714 sorter := http2sorterPool.Get().(*http2sorter) 6715 6716 defer http2sorterPool.Put(sorter) 6717 keys = sorter.Keys(h) 6718 } 6719 for _, k := range keys { 6720 vv := h[k] 6721 k = http2lowerHeader(k) 6722 if !http2validWireHeaderFieldName(k) { 6723 6724 continue 6725 } 6726 isTE := k == "transfer-encoding" 6727 for _, v := range vv { 6728 if !httplex.ValidHeaderFieldValue(v) { 6729 6730 continue 6731 } 6732 6733 if isTE && v != "trailers" { 6734 continue 6735 } 6736 http2encKV(enc, k, v) 6737 } 6738 } 6739 } 6740 6741 // frameWriteMsg is a request to write a frame. 6742 type http2frameWriteMsg struct { 6743 // write is the interface value that does the writing, once the 6744 // writeScheduler (below) has decided to select this frame 6745 // to write. The write functions are all defined in write.go. 6746 write http2writeFramer 6747 6748 stream *http2stream // used for prioritization. nil for non-stream frames. 6749 6750 // done, if non-nil, must be a buffered channel with space for 6751 // 1 message and is sent the return value from write (or an 6752 // earlier error) when the frame has been written. 6753 done chan error 6754 } 6755 6756 // for debugging only: 6757 func (wm http2frameWriteMsg) String() string { 6758 var streamID uint32 6759 if wm.stream != nil { 6760 streamID = wm.stream.id 6761 } 6762 var des string 6763 if s, ok := wm.write.(fmt.Stringer); ok { 6764 des = s.String() 6765 } else { 6766 des = fmt.Sprintf("%T", wm.write) 6767 } 6768 return fmt.Sprintf("[frameWriteMsg stream=%d, ch=%v, type: %v]", streamID, wm.done != nil, des) 6769 } 6770 6771 // writeScheduler tracks pending frames to write, priorities, and decides 6772 // the next one to use. It is not thread-safe. 6773 type http2writeScheduler struct { 6774 // zero are frames not associated with a specific stream. 6775 // They're sent before any stream-specific freams. 6776 zero http2writeQueue 6777 6778 // maxFrameSize is the maximum size of a DATA frame 6779 // we'll write. Must be non-zero and between 16K-16M. 6780 maxFrameSize uint32 6781 6782 // sq contains the stream-specific queues, keyed by stream ID. 6783 // when a stream is idle, it's deleted from the map. 6784 sq map[uint32]*http2writeQueue 6785 6786 // canSend is a slice of memory that's reused between frame 6787 // scheduling decisions to hold the list of writeQueues (from sq) 6788 // which have enough flow control data to send. After canSend is 6789 // built, the best is selected. 6790 canSend []*http2writeQueue 6791 6792 // pool of empty queues for reuse. 6793 queuePool []*http2writeQueue 6794 } 6795 6796 func (ws *http2writeScheduler) putEmptyQueue(q *http2writeQueue) { 6797 if len(q.s) != 0 { 6798 panic("queue must be empty") 6799 } 6800 ws.queuePool = append(ws.queuePool, q) 6801 } 6802 6803 func (ws *http2writeScheduler) getEmptyQueue() *http2writeQueue { 6804 ln := len(ws.queuePool) 6805 if ln == 0 { 6806 return new(http2writeQueue) 6807 } 6808 q := ws.queuePool[ln-1] 6809 ws.queuePool = ws.queuePool[:ln-1] 6810 return q 6811 } 6812 6813 func (ws *http2writeScheduler) empty() bool { return ws.zero.empty() && len(ws.sq) == 0 } 6814 6815 func (ws *http2writeScheduler) add(wm http2frameWriteMsg) { 6816 st := wm.stream 6817 if st == nil { 6818 ws.zero.push(wm) 6819 } else { 6820 ws.streamQueue(st.id).push(wm) 6821 } 6822 } 6823 6824 func (ws *http2writeScheduler) streamQueue(streamID uint32) *http2writeQueue { 6825 if q, ok := ws.sq[streamID]; ok { 6826 return q 6827 } 6828 if ws.sq == nil { 6829 ws.sq = make(map[uint32]*http2writeQueue) 6830 } 6831 q := ws.getEmptyQueue() 6832 ws.sq[streamID] = q 6833 return q 6834 } 6835 6836 // take returns the most important frame to write and removes it from the scheduler. 6837 // It is illegal to call this if the scheduler is empty or if there are no connection-level 6838 // flow control bytes available. 6839 func (ws *http2writeScheduler) take() (wm http2frameWriteMsg, ok bool) { 6840 if ws.maxFrameSize == 0 { 6841 panic("internal error: ws.maxFrameSize not initialized or invalid") 6842 } 6843 6844 if !ws.zero.empty() { 6845 return ws.zero.shift(), true 6846 } 6847 if len(ws.sq) == 0 { 6848 return 6849 } 6850 6851 for id, q := range ws.sq { 6852 if q.firstIsNoCost() { 6853 return ws.takeFrom(id, q) 6854 } 6855 } 6856 6857 if len(ws.canSend) != 0 { 6858 panic("should be empty") 6859 } 6860 for _, q := range ws.sq { 6861 if n := ws.streamWritableBytes(q); n > 0 { 6862 ws.canSend = append(ws.canSend, q) 6863 } 6864 } 6865 if len(ws.canSend) == 0 { 6866 return 6867 } 6868 defer ws.zeroCanSend() 6869 6870 q := ws.canSend[0] 6871 6872 return ws.takeFrom(q.streamID(), q) 6873 } 6874 6875 // zeroCanSend is defered from take. 6876 func (ws *http2writeScheduler) zeroCanSend() { 6877 for i := range ws.canSend { 6878 ws.canSend[i] = nil 6879 } 6880 ws.canSend = ws.canSend[:0] 6881 } 6882 6883 // streamWritableBytes returns the number of DATA bytes we could write 6884 // from the given queue's stream, if this stream/queue were 6885 // selected. It is an error to call this if q's head isn't a 6886 // *writeData. 6887 func (ws *http2writeScheduler) streamWritableBytes(q *http2writeQueue) int32 { 6888 wm := q.head() 6889 ret := wm.stream.flow.available() 6890 if ret == 0 { 6891 return 0 6892 } 6893 if int32(ws.maxFrameSize) < ret { 6894 ret = int32(ws.maxFrameSize) 6895 } 6896 if ret == 0 { 6897 panic("internal error: ws.maxFrameSize not initialized or invalid") 6898 } 6899 wd := wm.write.(*http2writeData) 6900 if len(wd.p) < int(ret) { 6901 ret = int32(len(wd.p)) 6902 } 6903 return ret 6904 } 6905 6906 func (ws *http2writeScheduler) takeFrom(id uint32, q *http2writeQueue) (wm http2frameWriteMsg, ok bool) { 6907 wm = q.head() 6908 6909 if wd, ok := wm.write.(*http2writeData); ok && len(wd.p) > 0 { 6910 allowed := wm.stream.flow.available() 6911 if allowed == 0 { 6912 6913 return http2frameWriteMsg{}, false 6914 } 6915 if int32(ws.maxFrameSize) < allowed { 6916 allowed = int32(ws.maxFrameSize) 6917 } 6918 6919 if len(wd.p) > int(allowed) { 6920 wm.stream.flow.take(allowed) 6921 chunk := wd.p[:allowed] 6922 wd.p = wd.p[allowed:] 6923 6924 return http2frameWriteMsg{ 6925 stream: wm.stream, 6926 write: &http2writeData{ 6927 streamID: wd.streamID, 6928 p: chunk, 6929 6930 endStream: false, 6931 }, 6932 6933 done: nil, 6934 }, true 6935 } 6936 wm.stream.flow.take(int32(len(wd.p))) 6937 } 6938 6939 q.shift() 6940 if q.empty() { 6941 ws.putEmptyQueue(q) 6942 delete(ws.sq, id) 6943 } 6944 return wm, true 6945 } 6946 6947 func (ws *http2writeScheduler) forgetStream(id uint32) { 6948 q, ok := ws.sq[id] 6949 if !ok { 6950 return 6951 } 6952 delete(ws.sq, id) 6953 6954 for i := range q.s { 6955 q.s[i] = http2frameWriteMsg{} 6956 } 6957 q.s = q.s[:0] 6958 ws.putEmptyQueue(q) 6959 } 6960 6961 type http2writeQueue struct { 6962 s []http2frameWriteMsg 6963 } 6964 6965 // streamID returns the stream ID for a non-empty stream-specific queue. 6966 func (q *http2writeQueue) streamID() uint32 { return q.s[0].stream.id } 6967 6968 func (q *http2writeQueue) empty() bool { return len(q.s) == 0 } 6969 6970 func (q *http2writeQueue) push(wm http2frameWriteMsg) { 6971 q.s = append(q.s, wm) 6972 } 6973 6974 // head returns the next item that would be removed by shift. 6975 func (q *http2writeQueue) head() http2frameWriteMsg { 6976 if len(q.s) == 0 { 6977 panic("invalid use of queue") 6978 } 6979 return q.s[0] 6980 } 6981 6982 func (q *http2writeQueue) shift() http2frameWriteMsg { 6983 if len(q.s) == 0 { 6984 panic("invalid use of queue") 6985 } 6986 wm := q.s[0] 6987 6988 copy(q.s, q.s[1:]) 6989 q.s[len(q.s)-1] = http2frameWriteMsg{} 6990 q.s = q.s[:len(q.s)-1] 6991 return wm 6992 } 6993 6994 func (q *http2writeQueue) firstIsNoCost() bool { 6995 if df, ok := q.s[0].write.(*http2writeData); ok { 6996 return len(df.p) == 0 6997 } 6998 return true 6999 }