github.com/letsencrypt/go@v0.0.0-20160714163537-4054769a31f6/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 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 // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec. 1996 func http2isBadCipher(cipher uint16) bool { 1997 switch cipher { 1998 case tls.TLS_RSA_WITH_RC4_128_SHA, 1999 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, 2000 tls.TLS_RSA_WITH_AES_128_CBC_SHA, 2001 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 2002 tls.TLS_RSA_WITH_AES_128_GCM_SHA256, 2003 tls.TLS_RSA_WITH_AES_256_GCM_SHA384, 2004 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 2005 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 2006 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 2007 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, 2008 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 2009 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 2010 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: 2011 2012 return true 2013 default: 2014 return false 2015 } 2016 } 2017 2018 type http2contextContext interface { 2019 context.Context 2020 } 2021 2022 func http2serverConnBaseContext(c net.Conn, opts *http2ServeConnOpts) (ctx http2contextContext, cancel func()) { 2023 ctx, cancel = context.WithCancel(context.Background()) 2024 ctx = context.WithValue(ctx, LocalAddrContextKey, c.LocalAddr()) 2025 if hs := opts.baseConfig(); hs != nil { 2026 ctx = context.WithValue(ctx, ServerContextKey, hs) 2027 } 2028 return 2029 } 2030 2031 func http2contextWithCancel(ctx http2contextContext) (_ http2contextContext, cancel func()) { 2032 return context.WithCancel(ctx) 2033 } 2034 2035 func http2requestWithContext(req *Request, ctx http2contextContext) *Request { 2036 return req.WithContext(ctx) 2037 } 2038 2039 type http2clientTrace httptrace.ClientTrace 2040 2041 func http2reqContext(r *Request) context.Context { return r.Context() } 2042 2043 func http2setResponseUncompressed(res *Response) { res.Uncompressed = true } 2044 2045 func http2traceGotConn(req *Request, cc *http2ClientConn) { 2046 trace := httptrace.ContextClientTrace(req.Context()) 2047 if trace == nil || trace.GotConn == nil { 2048 return 2049 } 2050 ci := httptrace.GotConnInfo{Conn: cc.tconn} 2051 cc.mu.Lock() 2052 ci.Reused = cc.nextStreamID > 1 2053 ci.WasIdle = len(cc.streams) == 0 && ci.Reused 2054 if ci.WasIdle && !cc.lastActive.IsZero() { 2055 ci.IdleTime = time.Now().Sub(cc.lastActive) 2056 } 2057 cc.mu.Unlock() 2058 2059 trace.GotConn(ci) 2060 } 2061 2062 func http2traceWroteHeaders(trace *http2clientTrace) { 2063 if trace != nil && trace.WroteHeaders != nil { 2064 trace.WroteHeaders() 2065 } 2066 } 2067 2068 func http2traceGot100Continue(trace *http2clientTrace) { 2069 if trace != nil && trace.Got100Continue != nil { 2070 trace.Got100Continue() 2071 } 2072 } 2073 2074 func http2traceWait100Continue(trace *http2clientTrace) { 2075 if trace != nil && trace.Wait100Continue != nil { 2076 trace.Wait100Continue() 2077 } 2078 } 2079 2080 func http2traceWroteRequest(trace *http2clientTrace, err error) { 2081 if trace != nil && trace.WroteRequest != nil { 2082 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err}) 2083 } 2084 } 2085 2086 func http2traceFirstResponseByte(trace *http2clientTrace) { 2087 if trace != nil && trace.GotFirstResponseByte != nil { 2088 trace.GotFirstResponseByte() 2089 } 2090 } 2091 2092 func http2requestTrace(req *Request) *http2clientTrace { 2093 trace := httptrace.ContextClientTrace(req.Context()) 2094 return (*http2clientTrace)(trace) 2095 } 2096 2097 var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1" 2098 2099 type http2goroutineLock uint64 2100 2101 func http2newGoroutineLock() http2goroutineLock { 2102 if !http2DebugGoroutines { 2103 return 0 2104 } 2105 return http2goroutineLock(http2curGoroutineID()) 2106 } 2107 2108 func (g http2goroutineLock) check() { 2109 if !http2DebugGoroutines { 2110 return 2111 } 2112 if http2curGoroutineID() != uint64(g) { 2113 panic("running on the wrong goroutine") 2114 } 2115 } 2116 2117 func (g http2goroutineLock) checkNotOn() { 2118 if !http2DebugGoroutines { 2119 return 2120 } 2121 if http2curGoroutineID() == uint64(g) { 2122 panic("running on the wrong goroutine") 2123 } 2124 } 2125 2126 var http2goroutineSpace = []byte("goroutine ") 2127 2128 func http2curGoroutineID() uint64 { 2129 bp := http2littleBuf.Get().(*[]byte) 2130 defer http2littleBuf.Put(bp) 2131 b := *bp 2132 b = b[:runtime.Stack(b, false)] 2133 2134 b = bytes.TrimPrefix(b, http2goroutineSpace) 2135 i := bytes.IndexByte(b, ' ') 2136 if i < 0 { 2137 panic(fmt.Sprintf("No space found in %q", b)) 2138 } 2139 b = b[:i] 2140 n, err := http2parseUintBytes(b, 10, 64) 2141 if err != nil { 2142 panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err)) 2143 } 2144 return n 2145 } 2146 2147 var http2littleBuf = sync.Pool{ 2148 New: func() interface{} { 2149 buf := make([]byte, 64) 2150 return &buf 2151 }, 2152 } 2153 2154 // parseUintBytes is like strconv.ParseUint, but using a []byte. 2155 func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) { 2156 var cutoff, maxVal uint64 2157 2158 if bitSize == 0 { 2159 bitSize = int(strconv.IntSize) 2160 } 2161 2162 s0 := s 2163 switch { 2164 case len(s) < 1: 2165 err = strconv.ErrSyntax 2166 goto Error 2167 2168 case 2 <= base && base <= 36: 2169 2170 case base == 0: 2171 2172 switch { 2173 case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'): 2174 base = 16 2175 s = s[2:] 2176 if len(s) < 1 { 2177 err = strconv.ErrSyntax 2178 goto Error 2179 } 2180 case s[0] == '0': 2181 base = 8 2182 default: 2183 base = 10 2184 } 2185 2186 default: 2187 err = errors.New("invalid base " + strconv.Itoa(base)) 2188 goto Error 2189 } 2190 2191 n = 0 2192 cutoff = http2cutoff64(base) 2193 maxVal = 1<<uint(bitSize) - 1 2194 2195 for i := 0; i < len(s); i++ { 2196 var v byte 2197 d := s[i] 2198 switch { 2199 case '0' <= d && d <= '9': 2200 v = d - '0' 2201 case 'a' <= d && d <= 'z': 2202 v = d - 'a' + 10 2203 case 'A' <= d && d <= 'Z': 2204 v = d - 'A' + 10 2205 default: 2206 n = 0 2207 err = strconv.ErrSyntax 2208 goto Error 2209 } 2210 if int(v) >= base { 2211 n = 0 2212 err = strconv.ErrSyntax 2213 goto Error 2214 } 2215 2216 if n >= cutoff { 2217 2218 n = 1<<64 - 1 2219 err = strconv.ErrRange 2220 goto Error 2221 } 2222 n *= uint64(base) 2223 2224 n1 := n + uint64(v) 2225 if n1 < n || n1 > maxVal { 2226 2227 n = 1<<64 - 1 2228 err = strconv.ErrRange 2229 goto Error 2230 } 2231 n = n1 2232 } 2233 2234 return n, nil 2235 2236 Error: 2237 return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err} 2238 } 2239 2240 // Return the first number n such that n*base >= 1<<64. 2241 func http2cutoff64(base int) uint64 { 2242 if base < 2 { 2243 return 0 2244 } 2245 return (1<<64-1)/uint64(base) + 1 2246 } 2247 2248 var ( 2249 http2commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case 2250 http2commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case 2251 ) 2252 2253 func init() { 2254 for _, v := range []string{ 2255 "accept", 2256 "accept-charset", 2257 "accept-encoding", 2258 "accept-language", 2259 "accept-ranges", 2260 "age", 2261 "access-control-allow-origin", 2262 "allow", 2263 "authorization", 2264 "cache-control", 2265 "content-disposition", 2266 "content-encoding", 2267 "content-language", 2268 "content-length", 2269 "content-location", 2270 "content-range", 2271 "content-type", 2272 "cookie", 2273 "date", 2274 "etag", 2275 "expect", 2276 "expires", 2277 "from", 2278 "host", 2279 "if-match", 2280 "if-modified-since", 2281 "if-none-match", 2282 "if-unmodified-since", 2283 "last-modified", 2284 "link", 2285 "location", 2286 "max-forwards", 2287 "proxy-authenticate", 2288 "proxy-authorization", 2289 "range", 2290 "referer", 2291 "refresh", 2292 "retry-after", 2293 "server", 2294 "set-cookie", 2295 "strict-transport-security", 2296 "trailer", 2297 "transfer-encoding", 2298 "user-agent", 2299 "vary", 2300 "via", 2301 "www-authenticate", 2302 } { 2303 chk := CanonicalHeaderKey(v) 2304 http2commonLowerHeader[chk] = v 2305 http2commonCanonHeader[v] = chk 2306 } 2307 } 2308 2309 func http2lowerHeader(v string) string { 2310 if s, ok := http2commonLowerHeader[v]; ok { 2311 return s 2312 } 2313 return strings.ToLower(v) 2314 } 2315 2316 var ( 2317 http2VerboseLogs bool 2318 http2logFrameWrites bool 2319 http2logFrameReads bool 2320 ) 2321 2322 func init() { 2323 e := os.Getenv("GODEBUG") 2324 if strings.Contains(e, "http2debug=1") { 2325 http2VerboseLogs = true 2326 } 2327 if strings.Contains(e, "http2debug=2") { 2328 http2VerboseLogs = true 2329 http2logFrameWrites = true 2330 http2logFrameReads = true 2331 } 2332 } 2333 2334 const ( 2335 // ClientPreface is the string that must be sent by new 2336 // connections from clients. 2337 http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" 2338 2339 // SETTINGS_MAX_FRAME_SIZE default 2340 // http://http2.github.io/http2-spec/#rfc.section.6.5.2 2341 http2initialMaxFrameSize = 16384 2342 2343 // NextProtoTLS is the NPN/ALPN protocol negotiated during 2344 // HTTP/2's TLS setup. 2345 http2NextProtoTLS = "h2" 2346 2347 // http://http2.github.io/http2-spec/#SettingValues 2348 http2initialHeaderTableSize = 4096 2349 2350 http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size 2351 2352 http2defaultMaxReadFrameSize = 1 << 20 2353 ) 2354 2355 var ( 2356 http2clientPreface = []byte(http2ClientPreface) 2357 ) 2358 2359 type http2streamState int 2360 2361 const ( 2362 http2stateIdle http2streamState = iota 2363 http2stateOpen 2364 http2stateHalfClosedLocal 2365 http2stateHalfClosedRemote 2366 http2stateResvLocal 2367 http2stateResvRemote 2368 http2stateClosed 2369 ) 2370 2371 var http2stateName = [...]string{ 2372 http2stateIdle: "Idle", 2373 http2stateOpen: "Open", 2374 http2stateHalfClosedLocal: "HalfClosedLocal", 2375 http2stateHalfClosedRemote: "HalfClosedRemote", 2376 http2stateResvLocal: "ResvLocal", 2377 http2stateResvRemote: "ResvRemote", 2378 http2stateClosed: "Closed", 2379 } 2380 2381 func (st http2streamState) String() string { 2382 return http2stateName[st] 2383 } 2384 2385 // Setting is a setting parameter: which setting it is, and its value. 2386 type http2Setting struct { 2387 // ID is which setting is being set. 2388 // See http://http2.github.io/http2-spec/#SettingValues 2389 ID http2SettingID 2390 2391 // Val is the value. 2392 Val uint32 2393 } 2394 2395 func (s http2Setting) String() string { 2396 return fmt.Sprintf("[%v = %d]", s.ID, s.Val) 2397 } 2398 2399 // Valid reports whether the setting is valid. 2400 func (s http2Setting) Valid() error { 2401 2402 switch s.ID { 2403 case http2SettingEnablePush: 2404 if s.Val != 1 && s.Val != 0 { 2405 return http2ConnectionError(http2ErrCodeProtocol) 2406 } 2407 case http2SettingInitialWindowSize: 2408 if s.Val > 1<<31-1 { 2409 return http2ConnectionError(http2ErrCodeFlowControl) 2410 } 2411 case http2SettingMaxFrameSize: 2412 if s.Val < 16384 || s.Val > 1<<24-1 { 2413 return http2ConnectionError(http2ErrCodeProtocol) 2414 } 2415 } 2416 return nil 2417 } 2418 2419 // A SettingID is an HTTP/2 setting as defined in 2420 // http://http2.github.io/http2-spec/#iana-settings 2421 type http2SettingID uint16 2422 2423 const ( 2424 http2SettingHeaderTableSize http2SettingID = 0x1 2425 http2SettingEnablePush http2SettingID = 0x2 2426 http2SettingMaxConcurrentStreams http2SettingID = 0x3 2427 http2SettingInitialWindowSize http2SettingID = 0x4 2428 http2SettingMaxFrameSize http2SettingID = 0x5 2429 http2SettingMaxHeaderListSize http2SettingID = 0x6 2430 ) 2431 2432 var http2settingName = map[http2SettingID]string{ 2433 http2SettingHeaderTableSize: "HEADER_TABLE_SIZE", 2434 http2SettingEnablePush: "ENABLE_PUSH", 2435 http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS", 2436 http2SettingInitialWindowSize: "INITIAL_WINDOW_SIZE", 2437 http2SettingMaxFrameSize: "MAX_FRAME_SIZE", 2438 http2SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE", 2439 } 2440 2441 func (s http2SettingID) String() string { 2442 if v, ok := http2settingName[s]; ok { 2443 return v 2444 } 2445 return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s)) 2446 } 2447 2448 var ( 2449 http2errInvalidHeaderFieldName = errors.New("http2: invalid header field name") 2450 http2errInvalidHeaderFieldValue = errors.New("http2: invalid header field value") 2451 ) 2452 2453 // validWireHeaderFieldName reports whether v is a valid header field 2454 // name (key). See httplex.ValidHeaderName for the base rules. 2455 // 2456 // Further, http2 says: 2457 // "Just as in HTTP/1.x, header field names are strings of ASCII 2458 // characters that are compared in a case-insensitive 2459 // fashion. However, header field names MUST be converted to 2460 // lowercase prior to their encoding in HTTP/2. " 2461 func http2validWireHeaderFieldName(v string) bool { 2462 if len(v) == 0 { 2463 return false 2464 } 2465 for _, r := range v { 2466 if !httplex.IsTokenRune(r) { 2467 return false 2468 } 2469 if 'A' <= r && r <= 'Z' { 2470 return false 2471 } 2472 } 2473 return true 2474 } 2475 2476 var http2httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n) 2477 2478 func init() { 2479 for i := 100; i <= 999; i++ { 2480 if v := StatusText(i); v != "" { 2481 http2httpCodeStringCommon[i] = strconv.Itoa(i) 2482 } 2483 } 2484 } 2485 2486 func http2httpCodeString(code int) string { 2487 if s, ok := http2httpCodeStringCommon[code]; ok { 2488 return s 2489 } 2490 return strconv.Itoa(code) 2491 } 2492 2493 // from pkg io 2494 type http2stringWriter interface { 2495 WriteString(s string) (n int, err error) 2496 } 2497 2498 // A gate lets two goroutines coordinate their activities. 2499 type http2gate chan struct{} 2500 2501 func (g http2gate) Done() { g <- struct{}{} } 2502 2503 func (g http2gate) Wait() { <-g } 2504 2505 // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed). 2506 type http2closeWaiter chan struct{} 2507 2508 // Init makes a closeWaiter usable. 2509 // It exists because so a closeWaiter value can be placed inside a 2510 // larger struct and have the Mutex and Cond's memory in the same 2511 // allocation. 2512 func (cw *http2closeWaiter) Init() { 2513 *cw = make(chan struct{}) 2514 } 2515 2516 // Close marks the closeWaiter as closed and unblocks any waiters. 2517 func (cw http2closeWaiter) Close() { 2518 close(cw) 2519 } 2520 2521 // Wait waits for the closeWaiter to become closed. 2522 func (cw http2closeWaiter) Wait() { 2523 <-cw 2524 } 2525 2526 // bufferedWriter is a buffered writer that writes to w. 2527 // Its buffered writer is lazily allocated as needed, to minimize 2528 // idle memory usage with many connections. 2529 type http2bufferedWriter struct { 2530 w io.Writer // immutable 2531 bw *bufio.Writer // non-nil when data is buffered 2532 } 2533 2534 func http2newBufferedWriter(w io.Writer) *http2bufferedWriter { 2535 return &http2bufferedWriter{w: w} 2536 } 2537 2538 var http2bufWriterPool = sync.Pool{ 2539 New: func() interface{} { 2540 2541 return bufio.NewWriterSize(nil, 4<<10) 2542 }, 2543 } 2544 2545 func (w *http2bufferedWriter) Write(p []byte) (n int, err error) { 2546 if w.bw == nil { 2547 bw := http2bufWriterPool.Get().(*bufio.Writer) 2548 bw.Reset(w.w) 2549 w.bw = bw 2550 } 2551 return w.bw.Write(p) 2552 } 2553 2554 func (w *http2bufferedWriter) Flush() error { 2555 bw := w.bw 2556 if bw == nil { 2557 return nil 2558 } 2559 err := bw.Flush() 2560 bw.Reset(nil) 2561 http2bufWriterPool.Put(bw) 2562 w.bw = nil 2563 return err 2564 } 2565 2566 func http2mustUint31(v int32) uint32 { 2567 if v < 0 || v > 2147483647 { 2568 panic("out of range") 2569 } 2570 return uint32(v) 2571 } 2572 2573 // bodyAllowedForStatus reports whether a given response status code 2574 // permits a body. See RFC 2616, section 4.4. 2575 func http2bodyAllowedForStatus(status int) bool { 2576 switch { 2577 case status >= 100 && status <= 199: 2578 return false 2579 case status == 204: 2580 return false 2581 case status == 304: 2582 return false 2583 } 2584 return true 2585 } 2586 2587 type http2httpError struct { 2588 msg string 2589 timeout bool 2590 } 2591 2592 func (e *http2httpError) Error() string { return e.msg } 2593 2594 func (e *http2httpError) Timeout() bool { return e.timeout } 2595 2596 func (e *http2httpError) Temporary() bool { return true } 2597 2598 var http2errTimeout error = &http2httpError{msg: "http2: timeout awaiting response headers", timeout: true} 2599 2600 type http2connectionStater interface { 2601 ConnectionState() tls.ConnectionState 2602 } 2603 2604 var http2sorterPool = sync.Pool{New: func() interface{} { return new(http2sorter) }} 2605 2606 type http2sorter struct { 2607 v []string // owned by sorter 2608 } 2609 2610 func (s *http2sorter) Len() int { return len(s.v) } 2611 2612 func (s *http2sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] } 2613 2614 func (s *http2sorter) Less(i, j int) bool { return s.v[i] < s.v[j] } 2615 2616 // Keys returns the sorted keys of h. 2617 // 2618 // The returned slice is only valid until s used again or returned to 2619 // its pool. 2620 func (s *http2sorter) Keys(h Header) []string { 2621 keys := s.v[:0] 2622 for k := range h { 2623 keys = append(keys, k) 2624 } 2625 s.v = keys 2626 sort.Sort(s) 2627 return keys 2628 } 2629 2630 func (s *http2sorter) SortStrings(ss []string) { 2631 2632 save := s.v 2633 s.v = ss 2634 sort.Sort(s) 2635 s.v = save 2636 } 2637 2638 // pipe is a goroutine-safe io.Reader/io.Writer pair. It's like 2639 // io.Pipe except there are no PipeReader/PipeWriter halves, and the 2640 // underlying buffer is an interface. (io.Pipe is always unbuffered) 2641 type http2pipe struct { 2642 mu sync.Mutex 2643 c sync.Cond // c.L lazily initialized to &p.mu 2644 b http2pipeBuffer 2645 err error // read error once empty. non-nil means closed. 2646 breakErr error // immediate read error (caller doesn't see rest of b) 2647 donec chan struct{} // closed on error 2648 readFn func() // optional code to run in Read before error 2649 } 2650 2651 type http2pipeBuffer interface { 2652 Len() int 2653 io.Writer 2654 io.Reader 2655 } 2656 2657 func (p *http2pipe) Len() int { 2658 p.mu.Lock() 2659 defer p.mu.Unlock() 2660 return p.b.Len() 2661 } 2662 2663 // Read waits until data is available and copies bytes 2664 // from the buffer into p. 2665 func (p *http2pipe) Read(d []byte) (n int, err error) { 2666 p.mu.Lock() 2667 defer p.mu.Unlock() 2668 if p.c.L == nil { 2669 p.c.L = &p.mu 2670 } 2671 for { 2672 if p.breakErr != nil { 2673 return 0, p.breakErr 2674 } 2675 if p.b.Len() > 0 { 2676 return p.b.Read(d) 2677 } 2678 if p.err != nil { 2679 if p.readFn != nil { 2680 p.readFn() 2681 p.readFn = nil 2682 } 2683 return 0, p.err 2684 } 2685 p.c.Wait() 2686 } 2687 } 2688 2689 var http2errClosedPipeWrite = errors.New("write on closed buffer") 2690 2691 // Write copies bytes from p into the buffer and wakes a reader. 2692 // It is an error to write more data than the buffer can hold. 2693 func (p *http2pipe) Write(d []byte) (n int, err error) { 2694 p.mu.Lock() 2695 defer p.mu.Unlock() 2696 if p.c.L == nil { 2697 p.c.L = &p.mu 2698 } 2699 defer p.c.Signal() 2700 if p.err != nil { 2701 return 0, http2errClosedPipeWrite 2702 } 2703 return p.b.Write(d) 2704 } 2705 2706 // CloseWithError causes the next Read (waking up a current blocked 2707 // Read if needed) to return the provided err after all data has been 2708 // read. 2709 // 2710 // The error must be non-nil. 2711 func (p *http2pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) } 2712 2713 // BreakWithError causes the next Read (waking up a current blocked 2714 // Read if needed) to return the provided err immediately, without 2715 // waiting for unread data. 2716 func (p *http2pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) } 2717 2718 // closeWithErrorAndCode is like CloseWithError but also sets some code to run 2719 // in the caller's goroutine before returning the error. 2720 func (p *http2pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) } 2721 2722 func (p *http2pipe) closeWithError(dst *error, err error, fn func()) { 2723 if err == nil { 2724 panic("err must be non-nil") 2725 } 2726 p.mu.Lock() 2727 defer p.mu.Unlock() 2728 if p.c.L == nil { 2729 p.c.L = &p.mu 2730 } 2731 defer p.c.Signal() 2732 if *dst != nil { 2733 2734 return 2735 } 2736 p.readFn = fn 2737 *dst = err 2738 p.closeDoneLocked() 2739 } 2740 2741 // requires p.mu be held. 2742 func (p *http2pipe) closeDoneLocked() { 2743 if p.donec == nil { 2744 return 2745 } 2746 2747 select { 2748 case <-p.donec: 2749 default: 2750 close(p.donec) 2751 } 2752 } 2753 2754 // Err returns the error (if any) first set by BreakWithError or CloseWithError. 2755 func (p *http2pipe) Err() error { 2756 p.mu.Lock() 2757 defer p.mu.Unlock() 2758 if p.breakErr != nil { 2759 return p.breakErr 2760 } 2761 return p.err 2762 } 2763 2764 // Done returns a channel which is closed if and when this pipe is closed 2765 // with CloseWithError. 2766 func (p *http2pipe) Done() <-chan struct{} { 2767 p.mu.Lock() 2768 defer p.mu.Unlock() 2769 if p.donec == nil { 2770 p.donec = make(chan struct{}) 2771 if p.err != nil || p.breakErr != nil { 2772 2773 p.closeDoneLocked() 2774 } 2775 } 2776 return p.donec 2777 } 2778 2779 const ( 2780 http2prefaceTimeout = 10 * time.Second 2781 http2firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway 2782 http2handlerChunkWriteSize = 4 << 10 2783 http2defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to? 2784 ) 2785 2786 var ( 2787 http2errClientDisconnected = errors.New("client disconnected") 2788 http2errClosedBody = errors.New("body closed by handler") 2789 http2errHandlerComplete = errors.New("http2: request body closed due to handler exiting") 2790 http2errStreamClosed = errors.New("http2: stream closed") 2791 ) 2792 2793 var http2responseWriterStatePool = sync.Pool{ 2794 New: func() interface{} { 2795 rws := &http2responseWriterState{} 2796 rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize) 2797 return rws 2798 }, 2799 } 2800 2801 // Test hooks. 2802 var ( 2803 http2testHookOnConn func() 2804 http2testHookGetServerConn func(*http2serverConn) 2805 http2testHookOnPanicMu *sync.Mutex // nil except in tests 2806 http2testHookOnPanic func(sc *http2serverConn, panicVal interface{}) (rePanic bool) 2807 ) 2808 2809 // Server is an HTTP/2 server. 2810 type http2Server struct { 2811 // MaxHandlers limits the number of http.Handler ServeHTTP goroutines 2812 // which may run at a time over all connections. 2813 // Negative or zero no limit. 2814 // TODO: implement 2815 MaxHandlers int 2816 2817 // MaxConcurrentStreams optionally specifies the number of 2818 // concurrent streams that each client may have open at a 2819 // time. This is unrelated to the number of http.Handler goroutines 2820 // which may be active globally, which is MaxHandlers. 2821 // If zero, MaxConcurrentStreams defaults to at least 100, per 2822 // the HTTP/2 spec's recommendations. 2823 MaxConcurrentStreams uint32 2824 2825 // MaxReadFrameSize optionally specifies the largest frame 2826 // this server is willing to read. A valid value is between 2827 // 16k and 16M, inclusive. If zero or otherwise invalid, a 2828 // default value is used. 2829 MaxReadFrameSize uint32 2830 2831 // PermitProhibitedCipherSuites, if true, permits the use of 2832 // cipher suites prohibited by the HTTP/2 spec. 2833 PermitProhibitedCipherSuites bool 2834 } 2835 2836 func (s *http2Server) maxReadFrameSize() uint32 { 2837 if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize { 2838 return v 2839 } 2840 return http2defaultMaxReadFrameSize 2841 } 2842 2843 func (s *http2Server) maxConcurrentStreams() uint32 { 2844 if v := s.MaxConcurrentStreams; v > 0 { 2845 return v 2846 } 2847 return http2defaultMaxStreams 2848 } 2849 2850 // ConfigureServer adds HTTP/2 support to a net/http Server. 2851 // 2852 // The configuration conf may be nil. 2853 // 2854 // ConfigureServer must be called before s begins serving. 2855 func http2ConfigureServer(s *Server, conf *http2Server) error { 2856 if conf == nil { 2857 conf = new(http2Server) 2858 } 2859 2860 if s.TLSConfig == nil { 2861 s.TLSConfig = new(tls.Config) 2862 } else if s.TLSConfig.CipherSuites != nil { 2863 // If they already provided a CipherSuite list, return 2864 // an error if it has a bad order or is missing 2865 // ECDHE_RSA_WITH_AES_128_GCM_SHA256. 2866 const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 2867 haveRequired := false 2868 sawBad := false 2869 for i, cs := range s.TLSConfig.CipherSuites { 2870 if cs == requiredCipher { 2871 haveRequired = true 2872 } 2873 if http2isBadCipher(cs) { 2874 sawBad = true 2875 } else if sawBad { 2876 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) 2877 } 2878 } 2879 if !haveRequired { 2880 return fmt.Errorf("http2: TLSConfig.CipherSuites is missing HTTP/2-required TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256") 2881 } 2882 } 2883 2884 s.TLSConfig.PreferServerCipherSuites = true 2885 2886 haveNPN := false 2887 for _, p := range s.TLSConfig.NextProtos { 2888 if p == http2NextProtoTLS { 2889 haveNPN = true 2890 break 2891 } 2892 } 2893 if !haveNPN { 2894 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS) 2895 } 2896 2897 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2-14") 2898 2899 if s.TLSNextProto == nil { 2900 s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){} 2901 } 2902 protoHandler := func(hs *Server, c *tls.Conn, h Handler) { 2903 if http2testHookOnConn != nil { 2904 http2testHookOnConn() 2905 } 2906 conf.ServeConn(c, &http2ServeConnOpts{ 2907 Handler: h, 2908 BaseConfig: hs, 2909 }) 2910 } 2911 s.TLSNextProto[http2NextProtoTLS] = protoHandler 2912 s.TLSNextProto["h2-14"] = protoHandler 2913 return nil 2914 } 2915 2916 // ServeConnOpts are options for the Server.ServeConn method. 2917 type http2ServeConnOpts struct { 2918 // BaseConfig optionally sets the base configuration 2919 // for values. If nil, defaults are used. 2920 BaseConfig *Server 2921 2922 // Handler specifies which handler to use for processing 2923 // requests. If nil, BaseConfig.Handler is used. If BaseConfig 2924 // or BaseConfig.Handler is nil, http.DefaultServeMux is used. 2925 Handler Handler 2926 } 2927 2928 func (o *http2ServeConnOpts) baseConfig() *Server { 2929 if o != nil && o.BaseConfig != nil { 2930 return o.BaseConfig 2931 } 2932 return new(Server) 2933 } 2934 2935 func (o *http2ServeConnOpts) handler() Handler { 2936 if o != nil { 2937 if o.Handler != nil { 2938 return o.Handler 2939 } 2940 if o.BaseConfig != nil && o.BaseConfig.Handler != nil { 2941 return o.BaseConfig.Handler 2942 } 2943 } 2944 return DefaultServeMux 2945 } 2946 2947 // ServeConn serves HTTP/2 requests on the provided connection and 2948 // blocks until the connection is no longer readable. 2949 // 2950 // ServeConn starts speaking HTTP/2 assuming that c has not had any 2951 // reads or writes. It writes its initial settings frame and expects 2952 // to be able to read the preface and settings frame from the 2953 // client. If c has a ConnectionState method like a *tls.Conn, the 2954 // ConnectionState is used to verify the TLS ciphersuite and to set 2955 // the Request.TLS field in Handlers. 2956 // 2957 // ServeConn does not support h2c by itself. Any h2c support must be 2958 // implemented in terms of providing a suitably-behaving net.Conn. 2959 // 2960 // The opts parameter is optional. If nil, default values are used. 2961 func (s *http2Server) ServeConn(c net.Conn, opts *http2ServeConnOpts) { 2962 baseCtx, cancel := http2serverConnBaseContext(c, opts) 2963 defer cancel() 2964 2965 sc := &http2serverConn{ 2966 srv: s, 2967 hs: opts.baseConfig(), 2968 conn: c, 2969 baseCtx: baseCtx, 2970 remoteAddrStr: c.RemoteAddr().String(), 2971 bw: http2newBufferedWriter(c), 2972 handler: opts.handler(), 2973 streams: make(map[uint32]*http2stream), 2974 readFrameCh: make(chan http2readFrameResult), 2975 wantWriteFrameCh: make(chan http2frameWriteMsg, 8), 2976 wroteFrameCh: make(chan http2frameWriteResult, 1), 2977 bodyReadCh: make(chan http2bodyReadMsg), 2978 doneServing: make(chan struct{}), 2979 advMaxStreams: s.maxConcurrentStreams(), 2980 writeSched: http2writeScheduler{ 2981 maxFrameSize: http2initialMaxFrameSize, 2982 }, 2983 initialWindowSize: http2initialWindowSize, 2984 headerTableSize: http2initialHeaderTableSize, 2985 serveG: http2newGoroutineLock(), 2986 pushEnabled: true, 2987 } 2988 2989 sc.flow.add(http2initialWindowSize) 2990 sc.inflow.add(http2initialWindowSize) 2991 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf) 2992 2993 fr := http2NewFramer(sc.bw, c) 2994 fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil) 2995 fr.MaxHeaderListSize = sc.maxHeaderListSize() 2996 fr.SetMaxReadFrameSize(s.maxReadFrameSize()) 2997 sc.framer = fr 2998 2999 if tc, ok := c.(http2connectionStater); ok { 3000 sc.tlsState = new(tls.ConnectionState) 3001 *sc.tlsState = tc.ConnectionState() 3002 3003 if sc.tlsState.Version < tls.VersionTLS12 { 3004 sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low") 3005 return 3006 } 3007 3008 if sc.tlsState.ServerName == "" { 3009 3010 } 3011 3012 if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) { 3013 3014 sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite)) 3015 return 3016 } 3017 } 3018 3019 if hook := http2testHookGetServerConn; hook != nil { 3020 hook(sc) 3021 } 3022 sc.serve() 3023 } 3024 3025 func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) { 3026 sc.vlogf("http2: server rejecting conn: %v, %s", err, debug) 3027 3028 sc.framer.WriteGoAway(0, err, []byte(debug)) 3029 sc.bw.Flush() 3030 sc.conn.Close() 3031 } 3032 3033 type http2serverConn struct { 3034 // Immutable: 3035 srv *http2Server 3036 hs *Server 3037 conn net.Conn 3038 bw *http2bufferedWriter // writing to conn 3039 handler Handler 3040 baseCtx http2contextContext 3041 framer *http2Framer 3042 doneServing chan struct{} // closed when serverConn.serve ends 3043 readFrameCh chan http2readFrameResult // written by serverConn.readFrames 3044 wantWriteFrameCh chan http2frameWriteMsg // from handlers -> serve 3045 wroteFrameCh chan http2frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes 3046 bodyReadCh chan http2bodyReadMsg // from handlers -> serve 3047 testHookCh chan func(int) // code to run on the serve loop 3048 flow http2flow // conn-wide (not stream-specific) outbound flow control 3049 inflow http2flow // conn-wide inbound flow control 3050 tlsState *tls.ConnectionState // shared by all handlers, like net/http 3051 remoteAddrStr string 3052 3053 // Everything following is owned by the serve loop; use serveG.check(): 3054 serveG http2goroutineLock // used to verify funcs are on serve() 3055 pushEnabled bool 3056 sawFirstSettings bool // got the initial SETTINGS frame after the preface 3057 needToSendSettingsAck bool 3058 unackedSettings int // how many SETTINGS have we sent without ACKs? 3059 clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit) 3060 advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client 3061 curOpenStreams uint32 // client's number of open streams 3062 maxStreamID uint32 // max ever seen 3063 streams map[uint32]*http2stream 3064 initialWindowSize int32 3065 headerTableSize uint32 3066 peerMaxHeaderListSize uint32 // zero means unknown (default) 3067 canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case 3068 writingFrame bool // started write goroutine but haven't heard back on wroteFrameCh 3069 needsFrameFlush bool // last frame write wasn't a flush 3070 writeSched http2writeScheduler 3071 inGoAway bool // we've started to or sent GOAWAY 3072 needToSendGoAway bool // we need to schedule a GOAWAY frame write 3073 goAwayCode http2ErrCode 3074 shutdownTimerCh <-chan time.Time // nil until used 3075 shutdownTimer *time.Timer // nil until used 3076 freeRequestBodyBuf []byte // if non-nil, a free initialWindowSize buffer for getRequestBodyBuf 3077 3078 // Owned by the writeFrameAsync goroutine: 3079 headerWriteBuf bytes.Buffer 3080 hpackEncoder *hpack.Encoder 3081 } 3082 3083 func (sc *http2serverConn) maxHeaderListSize() uint32 { 3084 n := sc.hs.MaxHeaderBytes 3085 if n <= 0 { 3086 n = DefaultMaxHeaderBytes 3087 } 3088 // http2's count is in a slightly different unit and includes 32 bytes per pair. 3089 // So, take the net/http.Server value and pad it up a bit, assuming 10 headers. 3090 const perFieldOverhead = 32 // per http2 spec 3091 const typicalHeaders = 10 // conservative 3092 return uint32(n + typicalHeaders*perFieldOverhead) 3093 } 3094 3095 // stream represents a stream. This is the minimal metadata needed by 3096 // the serve goroutine. Most of the actual stream state is owned by 3097 // the http.Handler's goroutine in the responseWriter. Because the 3098 // responseWriter's responseWriterState is recycled at the end of a 3099 // handler, this struct intentionally has no pointer to the 3100 // *responseWriter{,State} itself, as the Handler ending nils out the 3101 // responseWriter's state field. 3102 type http2stream struct { 3103 // immutable: 3104 sc *http2serverConn 3105 id uint32 3106 body *http2pipe // non-nil if expecting DATA frames 3107 cw http2closeWaiter // closed wait stream transitions to closed state 3108 ctx http2contextContext 3109 cancelCtx func() 3110 3111 // owned by serverConn's serve loop: 3112 bodyBytes int64 // body bytes seen so far 3113 declBodyBytes int64 // or -1 if undeclared 3114 flow http2flow // limits writing from Handler to client 3115 inflow http2flow // what the client is allowed to POST/etc to us 3116 parent *http2stream // or nil 3117 numTrailerValues int64 3118 weight uint8 3119 state http2streamState 3120 sentReset bool // only true once detached from streams map 3121 gotReset bool // only true once detacted from streams map 3122 gotTrailerHeader bool // HEADER frame for trailers was seen 3123 wroteHeaders bool // whether we wrote headers (not status 100) 3124 reqBuf []byte 3125 3126 trailer Header // accumulated trailers 3127 reqTrailer Header // handler's Request.Trailer 3128 } 3129 3130 func (sc *http2serverConn) Framer() *http2Framer { return sc.framer } 3131 3132 func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() } 3133 3134 func (sc *http2serverConn) Flush() error { return sc.bw.Flush() } 3135 3136 func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) { 3137 return sc.hpackEncoder, &sc.headerWriteBuf 3138 } 3139 3140 func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) { 3141 sc.serveG.check() 3142 3143 if st, ok := sc.streams[streamID]; ok { 3144 return st.state, st 3145 } 3146 3147 if streamID <= sc.maxStreamID { 3148 return http2stateClosed, nil 3149 } 3150 return http2stateIdle, nil 3151 } 3152 3153 // setConnState calls the net/http ConnState hook for this connection, if configured. 3154 // Note that the net/http package does StateNew and StateClosed for us. 3155 // There is currently no plan for StateHijacked or hijacking HTTP/2 connections. 3156 func (sc *http2serverConn) setConnState(state ConnState) { 3157 if sc.hs.ConnState != nil { 3158 sc.hs.ConnState(sc.conn, state) 3159 } 3160 } 3161 3162 func (sc *http2serverConn) vlogf(format string, args ...interface{}) { 3163 if http2VerboseLogs { 3164 sc.logf(format, args...) 3165 } 3166 } 3167 3168 func (sc *http2serverConn) logf(format string, args ...interface{}) { 3169 if lg := sc.hs.ErrorLog; lg != nil { 3170 lg.Printf(format, args...) 3171 } else { 3172 log.Printf(format, args...) 3173 } 3174 } 3175 3176 // errno returns v's underlying uintptr, else 0. 3177 // 3178 // TODO: remove this helper function once http2 can use build 3179 // tags. See comment in isClosedConnError. 3180 func http2errno(v error) uintptr { 3181 if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr { 3182 return uintptr(rv.Uint()) 3183 } 3184 return 0 3185 } 3186 3187 // isClosedConnError reports whether err is an error from use of a closed 3188 // network connection. 3189 func http2isClosedConnError(err error) bool { 3190 if err == nil { 3191 return false 3192 } 3193 3194 str := err.Error() 3195 if strings.Contains(str, "use of closed network connection") { 3196 return true 3197 } 3198 3199 if runtime.GOOS == "windows" { 3200 if oe, ok := err.(*net.OpError); ok && oe.Op == "read" { 3201 if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" { 3202 const WSAECONNABORTED = 10053 3203 const WSAECONNRESET = 10054 3204 if n := http2errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED { 3205 return true 3206 } 3207 } 3208 } 3209 } 3210 return false 3211 } 3212 3213 func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) { 3214 if err == nil { 3215 return 3216 } 3217 if err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) { 3218 3219 sc.vlogf(format, args...) 3220 } else { 3221 sc.logf(format, args...) 3222 } 3223 } 3224 3225 func (sc *http2serverConn) canonicalHeader(v string) string { 3226 sc.serveG.check() 3227 cv, ok := http2commonCanonHeader[v] 3228 if ok { 3229 return cv 3230 } 3231 cv, ok = sc.canonHeader[v] 3232 if ok { 3233 return cv 3234 } 3235 if sc.canonHeader == nil { 3236 sc.canonHeader = make(map[string]string) 3237 } 3238 cv = CanonicalHeaderKey(v) 3239 sc.canonHeader[v] = cv 3240 return cv 3241 } 3242 3243 type http2readFrameResult struct { 3244 f http2Frame // valid until readMore is called 3245 err error 3246 3247 // readMore should be called once the consumer no longer needs or 3248 // retains f. After readMore, f is invalid and more frames can be 3249 // read. 3250 readMore func() 3251 } 3252 3253 // readFrames is the loop that reads incoming frames. 3254 // It takes care to only read one frame at a time, blocking until the 3255 // consumer is done with the frame. 3256 // It's run on its own goroutine. 3257 func (sc *http2serverConn) readFrames() { 3258 gate := make(http2gate) 3259 gateDone := gate.Done 3260 for { 3261 f, err := sc.framer.ReadFrame() 3262 select { 3263 case sc.readFrameCh <- http2readFrameResult{f, err, gateDone}: 3264 case <-sc.doneServing: 3265 return 3266 } 3267 select { 3268 case <-gate: 3269 case <-sc.doneServing: 3270 return 3271 } 3272 if http2terminalReadFrameError(err) { 3273 return 3274 } 3275 } 3276 } 3277 3278 // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine. 3279 type http2frameWriteResult struct { 3280 wm http2frameWriteMsg // what was written (or attempted) 3281 err error // result of the writeFrame call 3282 } 3283 3284 // writeFrameAsync runs in its own goroutine and writes a single frame 3285 // and then reports when it's done. 3286 // At most one goroutine can be running writeFrameAsync at a time per 3287 // serverConn. 3288 func (sc *http2serverConn) writeFrameAsync(wm http2frameWriteMsg) { 3289 err := wm.write.writeFrame(sc) 3290 sc.wroteFrameCh <- http2frameWriteResult{wm, err} 3291 } 3292 3293 func (sc *http2serverConn) closeAllStreamsOnConnClose() { 3294 sc.serveG.check() 3295 for _, st := range sc.streams { 3296 sc.closeStream(st, http2errClientDisconnected) 3297 } 3298 } 3299 3300 func (sc *http2serverConn) stopShutdownTimer() { 3301 sc.serveG.check() 3302 if t := sc.shutdownTimer; t != nil { 3303 t.Stop() 3304 } 3305 } 3306 3307 func (sc *http2serverConn) notePanic() { 3308 3309 if http2testHookOnPanicMu != nil { 3310 http2testHookOnPanicMu.Lock() 3311 defer http2testHookOnPanicMu.Unlock() 3312 } 3313 if http2testHookOnPanic != nil { 3314 if e := recover(); e != nil { 3315 if http2testHookOnPanic(sc, e) { 3316 panic(e) 3317 } 3318 } 3319 } 3320 } 3321 3322 func (sc *http2serverConn) serve() { 3323 sc.serveG.check() 3324 defer sc.notePanic() 3325 defer sc.conn.Close() 3326 defer sc.closeAllStreamsOnConnClose() 3327 defer sc.stopShutdownTimer() 3328 defer close(sc.doneServing) 3329 3330 if http2VerboseLogs { 3331 sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs) 3332 } 3333 3334 sc.writeFrame(http2frameWriteMsg{ 3335 write: http2writeSettings{ 3336 {http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()}, 3337 {http2SettingMaxConcurrentStreams, sc.advMaxStreams}, 3338 {http2SettingMaxHeaderListSize, sc.maxHeaderListSize()}, 3339 }, 3340 }) 3341 sc.unackedSettings++ 3342 3343 if err := sc.readPreface(); err != nil { 3344 sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err) 3345 return 3346 } 3347 3348 sc.setConnState(StateActive) 3349 sc.setConnState(StateIdle) 3350 3351 go sc.readFrames() 3352 3353 settingsTimer := time.NewTimer(http2firstSettingsTimeout) 3354 loopNum := 0 3355 for { 3356 loopNum++ 3357 select { 3358 case wm := <-sc.wantWriteFrameCh: 3359 sc.writeFrame(wm) 3360 case res := <-sc.wroteFrameCh: 3361 sc.wroteFrame(res) 3362 case res := <-sc.readFrameCh: 3363 if !sc.processFrameFromReader(res) { 3364 return 3365 } 3366 res.readMore() 3367 if settingsTimer.C != nil { 3368 settingsTimer.Stop() 3369 settingsTimer.C = nil 3370 } 3371 case m := <-sc.bodyReadCh: 3372 sc.noteBodyRead(m.st, m.n) 3373 case <-settingsTimer.C: 3374 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr()) 3375 return 3376 case <-sc.shutdownTimerCh: 3377 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr()) 3378 return 3379 case fn := <-sc.testHookCh: 3380 fn(loopNum) 3381 } 3382 } 3383 } 3384 3385 // readPreface reads the ClientPreface greeting from the peer 3386 // or returns an error on timeout or an invalid greeting. 3387 func (sc *http2serverConn) readPreface() error { 3388 errc := make(chan error, 1) 3389 go func() { 3390 3391 buf := make([]byte, len(http2ClientPreface)) 3392 if _, err := io.ReadFull(sc.conn, buf); err != nil { 3393 errc <- err 3394 } else if !bytes.Equal(buf, http2clientPreface) { 3395 errc <- fmt.Errorf("bogus greeting %q", buf) 3396 } else { 3397 errc <- nil 3398 } 3399 }() 3400 timer := time.NewTimer(http2prefaceTimeout) 3401 defer timer.Stop() 3402 select { 3403 case <-timer.C: 3404 return errors.New("timeout waiting for client preface") 3405 case err := <-errc: 3406 if err == nil { 3407 if http2VerboseLogs { 3408 sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr()) 3409 } 3410 } 3411 return err 3412 } 3413 } 3414 3415 var http2errChanPool = sync.Pool{ 3416 New: func() interface{} { return make(chan error, 1) }, 3417 } 3418 3419 var http2writeDataPool = sync.Pool{ 3420 New: func() interface{} { return new(http2writeData) }, 3421 } 3422 3423 // writeDataFromHandler writes DATA response frames from a handler on 3424 // the given stream. 3425 func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error { 3426 ch := http2errChanPool.Get().(chan error) 3427 writeArg := http2writeDataPool.Get().(*http2writeData) 3428 *writeArg = http2writeData{stream.id, data, endStream} 3429 err := sc.writeFrameFromHandler(http2frameWriteMsg{ 3430 write: writeArg, 3431 stream: stream, 3432 done: ch, 3433 }) 3434 if err != nil { 3435 return err 3436 } 3437 var frameWriteDone bool // the frame write is done (successfully or not) 3438 select { 3439 case err = <-ch: 3440 frameWriteDone = true 3441 case <-sc.doneServing: 3442 return http2errClientDisconnected 3443 case <-stream.cw: 3444 3445 select { 3446 case err = <-ch: 3447 frameWriteDone = true 3448 default: 3449 return http2errStreamClosed 3450 } 3451 } 3452 http2errChanPool.Put(ch) 3453 if frameWriteDone { 3454 http2writeDataPool.Put(writeArg) 3455 } 3456 return err 3457 } 3458 3459 // writeFrameFromHandler sends wm to sc.wantWriteFrameCh, but aborts 3460 // if the connection has gone away. 3461 // 3462 // This must not be run from the serve goroutine itself, else it might 3463 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 3464 // buffered and is read by serve itself). If you're on the serve 3465 // goroutine, call writeFrame instead. 3466 func (sc *http2serverConn) writeFrameFromHandler(wm http2frameWriteMsg) error { 3467 sc.serveG.checkNotOn() 3468 select { 3469 case sc.wantWriteFrameCh <- wm: 3470 return nil 3471 case <-sc.doneServing: 3472 3473 return http2errClientDisconnected 3474 } 3475 } 3476 3477 // writeFrame schedules a frame to write and sends it if there's nothing 3478 // already being written. 3479 // 3480 // There is no pushback here (the serve goroutine never blocks). It's 3481 // the http.Handlers that block, waiting for their previous frames to 3482 // make it onto the wire 3483 // 3484 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 3485 func (sc *http2serverConn) writeFrame(wm http2frameWriteMsg) { 3486 sc.serveG.check() 3487 3488 var ignoreWrite bool 3489 3490 switch wm.write.(type) { 3491 case *http2writeResHeaders: 3492 wm.stream.wroteHeaders = true 3493 case http2write100ContinueHeadersFrame: 3494 if wm.stream.wroteHeaders { 3495 ignoreWrite = true 3496 } 3497 } 3498 3499 if !ignoreWrite { 3500 sc.writeSched.add(wm) 3501 } 3502 sc.scheduleFrameWrite() 3503 } 3504 3505 // startFrameWrite starts a goroutine to write wm (in a separate 3506 // goroutine since that might block on the network), and updates the 3507 // serve goroutine's state about the world, updated from info in wm. 3508 func (sc *http2serverConn) startFrameWrite(wm http2frameWriteMsg) { 3509 sc.serveG.check() 3510 if sc.writingFrame { 3511 panic("internal error: can only be writing one frame at a time") 3512 } 3513 3514 st := wm.stream 3515 if st != nil { 3516 switch st.state { 3517 case http2stateHalfClosedLocal: 3518 panic("internal error: attempt to send frame on half-closed-local stream") 3519 case http2stateClosed: 3520 if st.sentReset || st.gotReset { 3521 3522 sc.scheduleFrameWrite() 3523 return 3524 } 3525 panic(fmt.Sprintf("internal error: attempt to send a write %v on a closed stream", wm)) 3526 } 3527 } 3528 3529 sc.writingFrame = true 3530 sc.needsFrameFlush = true 3531 go sc.writeFrameAsync(wm) 3532 } 3533 3534 // errHandlerPanicked is the error given to any callers blocked in a read from 3535 // Request.Body when the main goroutine panics. Since most handlers read in the 3536 // the main ServeHTTP goroutine, this will show up rarely. 3537 var http2errHandlerPanicked = errors.New("http2: handler panicked") 3538 3539 // wroteFrame is called on the serve goroutine with the result of 3540 // whatever happened on writeFrameAsync. 3541 func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) { 3542 sc.serveG.check() 3543 if !sc.writingFrame { 3544 panic("internal error: expected to be already writing a frame") 3545 } 3546 sc.writingFrame = false 3547 3548 wm := res.wm 3549 st := wm.stream 3550 3551 closeStream := http2endsStream(wm.write) 3552 3553 if _, ok := wm.write.(http2handlerPanicRST); ok { 3554 sc.closeStream(st, http2errHandlerPanicked) 3555 } 3556 3557 if ch := wm.done; ch != nil { 3558 select { 3559 case ch <- res.err: 3560 default: 3561 panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.write)) 3562 } 3563 } 3564 wm.write = nil 3565 3566 if closeStream { 3567 if st == nil { 3568 panic("internal error: expecting non-nil stream") 3569 } 3570 switch st.state { 3571 case http2stateOpen: 3572 3573 st.state = http2stateHalfClosedLocal 3574 errCancel := http2StreamError{st.id, http2ErrCodeCancel} 3575 sc.resetStream(errCancel) 3576 case http2stateHalfClosedRemote: 3577 sc.closeStream(st, http2errHandlerComplete) 3578 } 3579 } 3580 3581 sc.scheduleFrameWrite() 3582 } 3583 3584 // scheduleFrameWrite tickles the frame writing scheduler. 3585 // 3586 // If a frame is already being written, nothing happens. This will be called again 3587 // when the frame is done being written. 3588 // 3589 // If a frame isn't being written we need to send one, the best frame 3590 // to send is selected, preferring first things that aren't 3591 // stream-specific (e.g. ACKing settings), and then finding the 3592 // highest priority stream. 3593 // 3594 // If a frame isn't being written and there's nothing else to send, we 3595 // flush the write buffer. 3596 func (sc *http2serverConn) scheduleFrameWrite() { 3597 sc.serveG.check() 3598 if sc.writingFrame { 3599 return 3600 } 3601 if sc.needToSendGoAway { 3602 sc.needToSendGoAway = false 3603 sc.startFrameWrite(http2frameWriteMsg{ 3604 write: &http2writeGoAway{ 3605 maxStreamID: sc.maxStreamID, 3606 code: sc.goAwayCode, 3607 }, 3608 }) 3609 return 3610 } 3611 if sc.needToSendSettingsAck { 3612 sc.needToSendSettingsAck = false 3613 sc.startFrameWrite(http2frameWriteMsg{write: http2writeSettingsAck{}}) 3614 return 3615 } 3616 if !sc.inGoAway { 3617 if wm, ok := sc.writeSched.take(); ok { 3618 sc.startFrameWrite(wm) 3619 return 3620 } 3621 } 3622 if sc.needsFrameFlush { 3623 sc.startFrameWrite(http2frameWriteMsg{write: http2flushFrameWriter{}}) 3624 sc.needsFrameFlush = false 3625 return 3626 } 3627 } 3628 3629 func (sc *http2serverConn) goAway(code http2ErrCode) { 3630 sc.serveG.check() 3631 if sc.inGoAway { 3632 return 3633 } 3634 if code != http2ErrCodeNo { 3635 sc.shutDownIn(250 * time.Millisecond) 3636 } else { 3637 3638 sc.shutDownIn(1 * time.Second) 3639 } 3640 sc.inGoAway = true 3641 sc.needToSendGoAway = true 3642 sc.goAwayCode = code 3643 sc.scheduleFrameWrite() 3644 } 3645 3646 func (sc *http2serverConn) shutDownIn(d time.Duration) { 3647 sc.serveG.check() 3648 sc.shutdownTimer = time.NewTimer(d) 3649 sc.shutdownTimerCh = sc.shutdownTimer.C 3650 } 3651 3652 func (sc *http2serverConn) resetStream(se http2StreamError) { 3653 sc.serveG.check() 3654 sc.writeFrame(http2frameWriteMsg{write: se}) 3655 if st, ok := sc.streams[se.StreamID]; ok { 3656 st.sentReset = true 3657 sc.closeStream(st, se) 3658 } 3659 } 3660 3661 // processFrameFromReader processes the serve loop's read from readFrameCh from the 3662 // frame-reading goroutine. 3663 // processFrameFromReader returns whether the connection should be kept open. 3664 func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool { 3665 sc.serveG.check() 3666 err := res.err 3667 if err != nil { 3668 if err == http2ErrFrameTooLarge { 3669 sc.goAway(http2ErrCodeFrameSize) 3670 return true 3671 } 3672 clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) 3673 if clientGone { 3674 3675 return false 3676 } 3677 } else { 3678 f := res.f 3679 if http2VerboseLogs { 3680 sc.vlogf("http2: server read frame %v", http2summarizeFrame(f)) 3681 } 3682 err = sc.processFrame(f) 3683 if err == nil { 3684 return true 3685 } 3686 } 3687 3688 switch ev := err.(type) { 3689 case http2StreamError: 3690 sc.resetStream(ev) 3691 return true 3692 case http2goAwayFlowError: 3693 sc.goAway(http2ErrCodeFlowControl) 3694 return true 3695 case http2ConnectionError: 3696 sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev) 3697 sc.goAway(http2ErrCode(ev)) 3698 return true 3699 default: 3700 if res.err != nil { 3701 sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 3702 } else { 3703 sc.logf("http2: server closing client connection: %v", err) 3704 } 3705 return false 3706 } 3707 } 3708 3709 func (sc *http2serverConn) processFrame(f http2Frame) error { 3710 sc.serveG.check() 3711 3712 if !sc.sawFirstSettings { 3713 if _, ok := f.(*http2SettingsFrame); !ok { 3714 return http2ConnectionError(http2ErrCodeProtocol) 3715 } 3716 sc.sawFirstSettings = true 3717 } 3718 3719 switch f := f.(type) { 3720 case *http2SettingsFrame: 3721 return sc.processSettings(f) 3722 case *http2MetaHeadersFrame: 3723 return sc.processHeaders(f) 3724 case *http2WindowUpdateFrame: 3725 return sc.processWindowUpdate(f) 3726 case *http2PingFrame: 3727 return sc.processPing(f) 3728 case *http2DataFrame: 3729 return sc.processData(f) 3730 case *http2RSTStreamFrame: 3731 return sc.processResetStream(f) 3732 case *http2PriorityFrame: 3733 return sc.processPriority(f) 3734 case *http2PushPromiseFrame: 3735 3736 return http2ConnectionError(http2ErrCodeProtocol) 3737 default: 3738 sc.vlogf("http2: server ignoring frame: %v", f.Header()) 3739 return nil 3740 } 3741 } 3742 3743 func (sc *http2serverConn) processPing(f *http2PingFrame) error { 3744 sc.serveG.check() 3745 if f.IsAck() { 3746 3747 return nil 3748 } 3749 if f.StreamID != 0 { 3750 3751 return http2ConnectionError(http2ErrCodeProtocol) 3752 } 3753 sc.writeFrame(http2frameWriteMsg{write: http2writePingAck{f}}) 3754 return nil 3755 } 3756 3757 func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error { 3758 sc.serveG.check() 3759 switch { 3760 case f.StreamID != 0: 3761 st := sc.streams[f.StreamID] 3762 if st == nil { 3763 3764 return nil 3765 } 3766 if !st.flow.add(int32(f.Increment)) { 3767 return http2StreamError{f.StreamID, http2ErrCodeFlowControl} 3768 } 3769 default: 3770 if !sc.flow.add(int32(f.Increment)) { 3771 return http2goAwayFlowError{} 3772 } 3773 } 3774 sc.scheduleFrameWrite() 3775 return nil 3776 } 3777 3778 func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error { 3779 sc.serveG.check() 3780 3781 state, st := sc.state(f.StreamID) 3782 if state == http2stateIdle { 3783 3784 return http2ConnectionError(http2ErrCodeProtocol) 3785 } 3786 if st != nil { 3787 st.gotReset = true 3788 st.cancelCtx() 3789 sc.closeStream(st, http2StreamError{f.StreamID, f.ErrCode}) 3790 } 3791 return nil 3792 } 3793 3794 func (sc *http2serverConn) closeStream(st *http2stream, err error) { 3795 sc.serveG.check() 3796 if st.state == http2stateIdle || st.state == http2stateClosed { 3797 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 3798 } 3799 st.state = http2stateClosed 3800 sc.curOpenStreams-- 3801 if sc.curOpenStreams == 0 { 3802 sc.setConnState(StateIdle) 3803 } 3804 delete(sc.streams, st.id) 3805 if p := st.body; p != nil { 3806 p.CloseWithError(err) 3807 } 3808 st.cw.Close() 3809 sc.writeSched.forgetStream(st.id) 3810 if st.reqBuf != nil { 3811 3812 sc.freeRequestBodyBuf = st.reqBuf 3813 } 3814 } 3815 3816 func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error { 3817 sc.serveG.check() 3818 if f.IsAck() { 3819 sc.unackedSettings-- 3820 if sc.unackedSettings < 0 { 3821 3822 return http2ConnectionError(http2ErrCodeProtocol) 3823 } 3824 return nil 3825 } 3826 if err := f.ForeachSetting(sc.processSetting); err != nil { 3827 return err 3828 } 3829 sc.needToSendSettingsAck = true 3830 sc.scheduleFrameWrite() 3831 return nil 3832 } 3833 3834 func (sc *http2serverConn) processSetting(s http2Setting) error { 3835 sc.serveG.check() 3836 if err := s.Valid(); err != nil { 3837 return err 3838 } 3839 if http2VerboseLogs { 3840 sc.vlogf("http2: server processing setting %v", s) 3841 } 3842 switch s.ID { 3843 case http2SettingHeaderTableSize: 3844 sc.headerTableSize = s.Val 3845 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 3846 case http2SettingEnablePush: 3847 sc.pushEnabled = s.Val != 0 3848 case http2SettingMaxConcurrentStreams: 3849 sc.clientMaxStreams = s.Val 3850 case http2SettingInitialWindowSize: 3851 return sc.processSettingInitialWindowSize(s.Val) 3852 case http2SettingMaxFrameSize: 3853 sc.writeSched.maxFrameSize = s.Val 3854 case http2SettingMaxHeaderListSize: 3855 sc.peerMaxHeaderListSize = s.Val 3856 default: 3857 3858 if http2VerboseLogs { 3859 sc.vlogf("http2: server ignoring unknown setting %v", s) 3860 } 3861 } 3862 return nil 3863 } 3864 3865 func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error { 3866 sc.serveG.check() 3867 3868 old := sc.initialWindowSize 3869 sc.initialWindowSize = int32(val) 3870 growth := sc.initialWindowSize - old 3871 for _, st := range sc.streams { 3872 if !st.flow.add(growth) { 3873 3874 return http2ConnectionError(http2ErrCodeFlowControl) 3875 } 3876 } 3877 return nil 3878 } 3879 3880 func (sc *http2serverConn) processData(f *http2DataFrame) error { 3881 sc.serveG.check() 3882 3883 id := f.Header().StreamID 3884 st, ok := sc.streams[id] 3885 if !ok || st.state != http2stateOpen || st.gotTrailerHeader { 3886 3887 return http2StreamError{id, http2ErrCodeStreamClosed} 3888 } 3889 if st.body == nil { 3890 panic("internal error: should have a body in this state") 3891 } 3892 data := f.Data() 3893 3894 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 3895 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 3896 return http2StreamError{id, http2ErrCodeStreamClosed} 3897 } 3898 if len(data) > 0 { 3899 3900 if int(st.inflow.available()) < len(data) { 3901 return http2StreamError{id, http2ErrCodeFlowControl} 3902 } 3903 st.inflow.take(int32(len(data))) 3904 wrote, err := st.body.Write(data) 3905 if err != nil { 3906 return http2StreamError{id, http2ErrCodeStreamClosed} 3907 } 3908 if wrote != len(data) { 3909 panic("internal error: bad Writer") 3910 } 3911 st.bodyBytes += int64(len(data)) 3912 } 3913 if f.StreamEnded() { 3914 st.endStream() 3915 } 3916 return nil 3917 } 3918 3919 // endStream closes a Request.Body's pipe. It is called when a DATA 3920 // frame says a request body is over (or after trailers). 3921 func (st *http2stream) endStream() { 3922 sc := st.sc 3923 sc.serveG.check() 3924 3925 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 3926 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 3927 st.declBodyBytes, st.bodyBytes)) 3928 } else { 3929 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest) 3930 st.body.CloseWithError(io.EOF) 3931 } 3932 st.state = http2stateHalfClosedRemote 3933 } 3934 3935 // copyTrailersToHandlerRequest is run in the Handler's goroutine in 3936 // its Request.Body.Read just before it gets io.EOF. 3937 func (st *http2stream) copyTrailersToHandlerRequest() { 3938 for k, vv := range st.trailer { 3939 if _, ok := st.reqTrailer[k]; ok { 3940 3941 st.reqTrailer[k] = vv 3942 } 3943 } 3944 } 3945 3946 func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error { 3947 sc.serveG.check() 3948 id := f.Header().StreamID 3949 if sc.inGoAway { 3950 3951 return nil 3952 } 3953 3954 if id%2 != 1 { 3955 return http2ConnectionError(http2ErrCodeProtocol) 3956 } 3957 3958 st := sc.streams[f.Header().StreamID] 3959 if st != nil { 3960 return st.processTrailerHeaders(f) 3961 } 3962 3963 if id <= sc.maxStreamID { 3964 return http2ConnectionError(http2ErrCodeProtocol) 3965 } 3966 sc.maxStreamID = id 3967 3968 ctx, cancelCtx := http2contextWithCancel(sc.baseCtx) 3969 st = &http2stream{ 3970 sc: sc, 3971 id: id, 3972 state: http2stateOpen, 3973 ctx: ctx, 3974 cancelCtx: cancelCtx, 3975 } 3976 if f.StreamEnded() { 3977 st.state = http2stateHalfClosedRemote 3978 } 3979 st.cw.Init() 3980 3981 st.flow.conn = &sc.flow 3982 st.flow.add(sc.initialWindowSize) 3983 st.inflow.conn = &sc.inflow 3984 st.inflow.add(http2initialWindowSize) 3985 3986 sc.streams[id] = st 3987 if f.HasPriority() { 3988 http2adjustStreamPriority(sc.streams, st.id, f.Priority) 3989 } 3990 sc.curOpenStreams++ 3991 if sc.curOpenStreams == 1 { 3992 sc.setConnState(StateActive) 3993 } 3994 if sc.curOpenStreams > sc.advMaxStreams { 3995 3996 if sc.unackedSettings == 0 { 3997 3998 return http2StreamError{st.id, http2ErrCodeProtocol} 3999 } 4000 4001 return http2StreamError{st.id, http2ErrCodeRefusedStream} 4002 } 4003 4004 rw, req, err := sc.newWriterAndRequest(st, f) 4005 if err != nil { 4006 return err 4007 } 4008 st.reqTrailer = req.Trailer 4009 if st.reqTrailer != nil { 4010 st.trailer = make(Header) 4011 } 4012 st.body = req.Body.(*http2requestBody).pipe 4013 st.declBodyBytes = req.ContentLength 4014 4015 handler := sc.handler.ServeHTTP 4016 if f.Truncated { 4017 4018 handler = http2handleHeaderListTooLong 4019 } else if err := http2checkValidHTTP2Request(req); err != nil { 4020 handler = http2new400Handler(err) 4021 } 4022 4023 go sc.runHandler(rw, req, handler) 4024 return nil 4025 } 4026 4027 func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error { 4028 sc := st.sc 4029 sc.serveG.check() 4030 if st.gotTrailerHeader { 4031 return http2ConnectionError(http2ErrCodeProtocol) 4032 } 4033 st.gotTrailerHeader = true 4034 if !f.StreamEnded() { 4035 return http2StreamError{st.id, http2ErrCodeProtocol} 4036 } 4037 4038 if len(f.PseudoFields()) > 0 { 4039 return http2StreamError{st.id, http2ErrCodeProtocol} 4040 } 4041 if st.trailer != nil { 4042 for _, hf := range f.RegularFields() { 4043 key := sc.canonicalHeader(hf.Name) 4044 if !http2ValidTrailerHeader(key) { 4045 4046 return http2StreamError{st.id, http2ErrCodeProtocol} 4047 } 4048 st.trailer[key] = append(st.trailer[key], hf.Value) 4049 } 4050 } 4051 st.endStream() 4052 return nil 4053 } 4054 4055 func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error { 4056 http2adjustStreamPriority(sc.streams, f.StreamID, f.http2PriorityParam) 4057 return nil 4058 } 4059 4060 func http2adjustStreamPriority(streams map[uint32]*http2stream, streamID uint32, priority http2PriorityParam) { 4061 st, ok := streams[streamID] 4062 if !ok { 4063 4064 return 4065 } 4066 st.weight = priority.Weight 4067 parent := streams[priority.StreamDep] 4068 if parent == st { 4069 4070 return 4071 } 4072 4073 for piter := parent; piter != nil; piter = piter.parent { 4074 if piter == st { 4075 parent.parent = st.parent 4076 break 4077 } 4078 } 4079 st.parent = parent 4080 if priority.Exclusive && (st.parent != nil || priority.StreamDep == 0) { 4081 for _, openStream := range streams { 4082 if openStream != st && openStream.parent == st.parent { 4083 openStream.parent = st 4084 } 4085 } 4086 } 4087 } 4088 4089 func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) { 4090 sc.serveG.check() 4091 4092 method := f.PseudoValue("method") 4093 path := f.PseudoValue("path") 4094 scheme := f.PseudoValue("scheme") 4095 authority := f.PseudoValue("authority") 4096 4097 isConnect := method == "CONNECT" 4098 if isConnect { 4099 if path != "" || scheme != "" || authority == "" { 4100 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4101 } 4102 } else if method == "" || path == "" || 4103 (scheme != "https" && scheme != "http") { 4104 4105 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4106 } 4107 4108 bodyOpen := !f.StreamEnded() 4109 if method == "HEAD" && bodyOpen { 4110 4111 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4112 } 4113 var tlsState *tls.ConnectionState // nil if not scheme https 4114 4115 if scheme == "https" { 4116 tlsState = sc.tlsState 4117 } 4118 4119 header := make(Header) 4120 for _, hf := range f.RegularFields() { 4121 header.Add(sc.canonicalHeader(hf.Name), hf.Value) 4122 } 4123 4124 if authority == "" { 4125 authority = header.Get("Host") 4126 } 4127 needsContinue := header.Get("Expect") == "100-continue" 4128 if needsContinue { 4129 header.Del("Expect") 4130 } 4131 4132 if cookies := header["Cookie"]; len(cookies) > 1 { 4133 header.Set("Cookie", strings.Join(cookies, "; ")) 4134 } 4135 4136 // Setup Trailers 4137 var trailer Header 4138 for _, v := range header["Trailer"] { 4139 for _, key := range strings.Split(v, ",") { 4140 key = CanonicalHeaderKey(strings.TrimSpace(key)) 4141 switch key { 4142 case "Transfer-Encoding", "Trailer", "Content-Length": 4143 4144 default: 4145 if trailer == nil { 4146 trailer = make(Header) 4147 } 4148 trailer[key] = nil 4149 } 4150 } 4151 } 4152 delete(header, "Trailer") 4153 4154 body := &http2requestBody{ 4155 conn: sc, 4156 stream: st, 4157 needsContinue: needsContinue, 4158 } 4159 var url_ *url.URL 4160 var requestURI string 4161 if isConnect { 4162 url_ = &url.URL{Host: authority} 4163 requestURI = authority 4164 } else { 4165 var err error 4166 url_, err = url.ParseRequestURI(path) 4167 if err != nil { 4168 return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol} 4169 } 4170 requestURI = path 4171 } 4172 req := &Request{ 4173 Method: method, 4174 URL: url_, 4175 RemoteAddr: sc.remoteAddrStr, 4176 Header: header, 4177 RequestURI: requestURI, 4178 Proto: "HTTP/2.0", 4179 ProtoMajor: 2, 4180 ProtoMinor: 0, 4181 TLS: tlsState, 4182 Host: authority, 4183 Body: body, 4184 Trailer: trailer, 4185 } 4186 req = http2requestWithContext(req, st.ctx) 4187 if bodyOpen { 4188 4189 buf := make([]byte, http2initialWindowSize) 4190 4191 body.pipe = &http2pipe{ 4192 b: &http2fixedBuffer{buf: buf}, 4193 } 4194 4195 if vv, ok := header["Content-Length"]; ok { 4196 req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64) 4197 } else { 4198 req.ContentLength = -1 4199 } 4200 } 4201 4202 rws := http2responseWriterStatePool.Get().(*http2responseWriterState) 4203 bwSave := rws.bw 4204 *rws = http2responseWriterState{} 4205 rws.conn = sc 4206 rws.bw = bwSave 4207 rws.bw.Reset(http2chunkWriter{rws}) 4208 rws.stream = st 4209 rws.req = req 4210 rws.body = body 4211 4212 rw := &http2responseWriter{rws: rws} 4213 return rw, req, nil 4214 } 4215 4216 func (sc *http2serverConn) getRequestBodyBuf() []byte { 4217 sc.serveG.check() 4218 if buf := sc.freeRequestBodyBuf; buf != nil { 4219 sc.freeRequestBodyBuf = nil 4220 return buf 4221 } 4222 return make([]byte, http2initialWindowSize) 4223 } 4224 4225 // Run on its own goroutine. 4226 func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) { 4227 didPanic := true 4228 defer func() { 4229 rw.rws.stream.cancelCtx() 4230 if didPanic { 4231 e := recover() 4232 // Same as net/http: 4233 const size = 64 << 10 4234 buf := make([]byte, size) 4235 buf = buf[:runtime.Stack(buf, false)] 4236 sc.writeFrameFromHandler(http2frameWriteMsg{ 4237 write: http2handlerPanicRST{rw.rws.stream.id}, 4238 stream: rw.rws.stream, 4239 }) 4240 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf) 4241 return 4242 } 4243 rw.handlerDone() 4244 }() 4245 handler(rw, req) 4246 didPanic = false 4247 } 4248 4249 func http2handleHeaderListTooLong(w ResponseWriter, r *Request) { 4250 // 10.5.1 Limits on Header Block Size: 4251 // .. "A server that receives a larger header block than it is 4252 // willing to handle can send an HTTP 431 (Request Header Fields Too 4253 // Large) status code" 4254 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 4255 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 4256 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 4257 } 4258 4259 // called from handler goroutines. 4260 // h may be nil. 4261 func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error { 4262 sc.serveG.checkNotOn() 4263 var errc chan error 4264 if headerData.h != nil { 4265 4266 errc = http2errChanPool.Get().(chan error) 4267 } 4268 if err := sc.writeFrameFromHandler(http2frameWriteMsg{ 4269 write: headerData, 4270 stream: st, 4271 done: errc, 4272 }); err != nil { 4273 return err 4274 } 4275 if errc != nil { 4276 select { 4277 case err := <-errc: 4278 http2errChanPool.Put(errc) 4279 return err 4280 case <-sc.doneServing: 4281 return http2errClientDisconnected 4282 case <-st.cw: 4283 return http2errStreamClosed 4284 } 4285 } 4286 return nil 4287 } 4288 4289 // called from handler goroutines. 4290 func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) { 4291 sc.writeFrameFromHandler(http2frameWriteMsg{ 4292 write: http2write100ContinueHeadersFrame{st.id}, 4293 stream: st, 4294 }) 4295 } 4296 4297 // A bodyReadMsg tells the server loop that the http.Handler read n 4298 // bytes of the DATA from the client on the given stream. 4299 type http2bodyReadMsg struct { 4300 st *http2stream 4301 n int 4302 } 4303 4304 // called from handler goroutines. 4305 // Notes that the handler for the given stream ID read n bytes of its body 4306 // and schedules flow control tokens to be sent. 4307 func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int) { 4308 sc.serveG.checkNotOn() 4309 select { 4310 case sc.bodyReadCh <- http2bodyReadMsg{st, n}: 4311 case <-sc.doneServing: 4312 } 4313 } 4314 4315 func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) { 4316 sc.serveG.check() 4317 sc.sendWindowUpdate(nil, n) 4318 if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed { 4319 4320 sc.sendWindowUpdate(st, n) 4321 } 4322 } 4323 4324 // st may be nil for conn-level 4325 func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) { 4326 sc.serveG.check() 4327 // "The legal range for the increment to the flow control 4328 // window is 1 to 2^31-1 (2,147,483,647) octets." 4329 // A Go Read call on 64-bit machines could in theory read 4330 // a larger Read than this. Very unlikely, but we handle it here 4331 // rather than elsewhere for now. 4332 const maxUint31 = 1<<31 - 1 4333 for n >= maxUint31 { 4334 sc.sendWindowUpdate32(st, maxUint31) 4335 n -= maxUint31 4336 } 4337 sc.sendWindowUpdate32(st, int32(n)) 4338 } 4339 4340 // st may be nil for conn-level 4341 func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) { 4342 sc.serveG.check() 4343 if n == 0 { 4344 return 4345 } 4346 if n < 0 { 4347 panic("negative update") 4348 } 4349 var streamID uint32 4350 if st != nil { 4351 streamID = st.id 4352 } 4353 sc.writeFrame(http2frameWriteMsg{ 4354 write: http2writeWindowUpdate{streamID: streamID, n: uint32(n)}, 4355 stream: st, 4356 }) 4357 var ok bool 4358 if st == nil { 4359 ok = sc.inflow.add(n) 4360 } else { 4361 ok = st.inflow.add(n) 4362 } 4363 if !ok { 4364 panic("internal error; sent too many window updates without decrements?") 4365 } 4366 } 4367 4368 type http2requestBody struct { 4369 stream *http2stream 4370 conn *http2serverConn 4371 closed bool 4372 pipe *http2pipe // non-nil if we have a HTTP entity message body 4373 needsContinue bool // need to send a 100-continue 4374 } 4375 4376 func (b *http2requestBody) Close() error { 4377 if b.pipe != nil { 4378 b.pipe.BreakWithError(http2errClosedBody) 4379 } 4380 b.closed = true 4381 return nil 4382 } 4383 4384 func (b *http2requestBody) Read(p []byte) (n int, err error) { 4385 if b.needsContinue { 4386 b.needsContinue = false 4387 b.conn.write100ContinueHeaders(b.stream) 4388 } 4389 if b.pipe == nil { 4390 return 0, io.EOF 4391 } 4392 n, err = b.pipe.Read(p) 4393 if n > 0 { 4394 b.conn.noteBodyReadFromHandler(b.stream, n) 4395 } 4396 return 4397 } 4398 4399 // responseWriter is the http.ResponseWriter implementation. It's 4400 // intentionally small (1 pointer wide) to minimize garbage. The 4401 // responseWriterState pointer inside is zeroed at the end of a 4402 // request (in handlerDone) and calls on the responseWriter thereafter 4403 // simply crash (caller's mistake), but the much larger responseWriterState 4404 // and buffers are reused between multiple requests. 4405 type http2responseWriter struct { 4406 rws *http2responseWriterState 4407 } 4408 4409 // Optional http.ResponseWriter interfaces implemented. 4410 var ( 4411 _ CloseNotifier = (*http2responseWriter)(nil) 4412 _ Flusher = (*http2responseWriter)(nil) 4413 _ http2stringWriter = (*http2responseWriter)(nil) 4414 ) 4415 4416 type http2responseWriterState struct { 4417 // immutable within a request: 4418 stream *http2stream 4419 req *Request 4420 body *http2requestBody // to close at end of request, if DATA frames didn't 4421 conn *http2serverConn 4422 4423 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 4424 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 4425 4426 // mutated by http.Handler goroutine: 4427 handlerHeader Header // nil until called 4428 snapHeader Header // snapshot of handlerHeader at WriteHeader time 4429 trailers []string // set in writeChunk 4430 status int // status code passed to WriteHeader 4431 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 4432 sentHeader bool // have we sent the header frame? 4433 handlerDone bool // handler has finished 4434 4435 sentContentLen int64 // non-zero if handler set a Content-Length header 4436 wroteBytes int64 4437 4438 closeNotifierMu sync.Mutex // guards closeNotifierCh 4439 closeNotifierCh chan bool // nil until first used 4440 } 4441 4442 type http2chunkWriter struct{ rws *http2responseWriterState } 4443 4444 func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 4445 4446 func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 } 4447 4448 // declareTrailer is called for each Trailer header when the 4449 // response header is written. It notes that a header will need to be 4450 // written in the trailers at the end of the response. 4451 func (rws *http2responseWriterState) declareTrailer(k string) { 4452 k = CanonicalHeaderKey(k) 4453 if !http2ValidTrailerHeader(k) { 4454 4455 rws.conn.logf("ignoring invalid trailer %q", k) 4456 return 4457 } 4458 if !http2strSliceContains(rws.trailers, k) { 4459 rws.trailers = append(rws.trailers, k) 4460 } 4461 } 4462 4463 // writeChunk writes chunks from the bufio.Writer. But because 4464 // bufio.Writer may bypass its chunking, sometimes p may be 4465 // arbitrarily large. 4466 // 4467 // writeChunk is also responsible (on the first chunk) for sending the 4468 // HEADER response. 4469 func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) { 4470 if !rws.wroteHeader { 4471 rws.writeHeader(200) 4472 } 4473 4474 isHeadResp := rws.req.Method == "HEAD" 4475 if !rws.sentHeader { 4476 rws.sentHeader = true 4477 var ctype, clen string 4478 if clen = rws.snapHeader.Get("Content-Length"); clen != "" { 4479 rws.snapHeader.Del("Content-Length") 4480 clen64, err := strconv.ParseInt(clen, 10, 64) 4481 if err == nil && clen64 >= 0 { 4482 rws.sentContentLen = clen64 4483 } else { 4484 clen = "" 4485 } 4486 } 4487 if clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) { 4488 clen = strconv.Itoa(len(p)) 4489 } 4490 _, hasContentType := rws.snapHeader["Content-Type"] 4491 if !hasContentType && http2bodyAllowedForStatus(rws.status) { 4492 ctype = DetectContentType(p) 4493 } 4494 var date string 4495 if _, ok := rws.snapHeader["Date"]; !ok { 4496 4497 date = time.Now().UTC().Format(TimeFormat) 4498 } 4499 4500 for _, v := range rws.snapHeader["Trailer"] { 4501 http2foreachHeaderElement(v, rws.declareTrailer) 4502 } 4503 4504 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp 4505 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 4506 streamID: rws.stream.id, 4507 httpResCode: rws.status, 4508 h: rws.snapHeader, 4509 endStream: endStream, 4510 contentType: ctype, 4511 contentLength: clen, 4512 date: date, 4513 }) 4514 if err != nil { 4515 return 0, err 4516 } 4517 if endStream { 4518 return 0, nil 4519 } 4520 } 4521 if isHeadResp { 4522 return len(p), nil 4523 } 4524 if len(p) == 0 && !rws.handlerDone { 4525 return 0, nil 4526 } 4527 4528 if rws.handlerDone { 4529 rws.promoteUndeclaredTrailers() 4530 } 4531 4532 endStream := rws.handlerDone && !rws.hasTrailers() 4533 if len(p) > 0 || endStream { 4534 4535 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil { 4536 return 0, err 4537 } 4538 } 4539 4540 if rws.handlerDone && rws.hasTrailers() { 4541 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 4542 streamID: rws.stream.id, 4543 h: rws.handlerHeader, 4544 trailers: rws.trailers, 4545 endStream: true, 4546 }) 4547 return len(p), err 4548 } 4549 return len(p), nil 4550 } 4551 4552 // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys 4553 // that, if present, signals that the map entry is actually for 4554 // the response trailers, and not the response headers. The prefix 4555 // is stripped after the ServeHTTP call finishes and the values are 4556 // sent in the trailers. 4557 // 4558 // This mechanism is intended only for trailers that are not known 4559 // prior to the headers being written. If the set of trailers is fixed 4560 // or known before the header is written, the normal Go trailers mechanism 4561 // is preferred: 4562 // https://golang.org/pkg/net/http/#ResponseWriter 4563 // https://golang.org/pkg/net/http/#example_ResponseWriter_trailers 4564 const http2TrailerPrefix = "Trailer:" 4565 4566 // promoteUndeclaredTrailers permits http.Handlers to set trailers 4567 // after the header has already been flushed. Because the Go 4568 // ResponseWriter interface has no way to set Trailers (only the 4569 // Header), and because we didn't want to expand the ResponseWriter 4570 // interface, and because nobody used trailers, and because RFC 2616 4571 // says you SHOULD (but not must) predeclare any trailers in the 4572 // header, the official ResponseWriter rules said trailers in Go must 4573 // be predeclared, and then we reuse the same ResponseWriter.Header() 4574 // map to mean both Headers and Trailers. When it's time to write the 4575 // Trailers, we pick out the fields of Headers that were declared as 4576 // trailers. That worked for a while, until we found the first major 4577 // user of Trailers in the wild: gRPC (using them only over http2), 4578 // and gRPC libraries permit setting trailers mid-stream without 4579 // predeclarnig them. So: change of plans. We still permit the old 4580 // way, but we also permit this hack: if a Header() key begins with 4581 // "Trailer:", the suffix of that key is a Trailer. Because ':' is an 4582 // invalid token byte anyway, there is no ambiguity. (And it's already 4583 // filtered out) It's mildly hacky, but not terrible. 4584 // 4585 // This method runs after the Handler is done and promotes any Header 4586 // fields to be trailers. 4587 func (rws *http2responseWriterState) promoteUndeclaredTrailers() { 4588 for k, vv := range rws.handlerHeader { 4589 if !strings.HasPrefix(k, http2TrailerPrefix) { 4590 continue 4591 } 4592 trailerKey := strings.TrimPrefix(k, http2TrailerPrefix) 4593 rws.declareTrailer(trailerKey) 4594 rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv 4595 } 4596 4597 if len(rws.trailers) > 1 { 4598 sorter := http2sorterPool.Get().(*http2sorter) 4599 sorter.SortStrings(rws.trailers) 4600 http2sorterPool.Put(sorter) 4601 } 4602 } 4603 4604 func (w *http2responseWriter) Flush() { 4605 rws := w.rws 4606 if rws == nil { 4607 panic("Header called after Handler finished") 4608 } 4609 if rws.bw.Buffered() > 0 { 4610 if err := rws.bw.Flush(); err != nil { 4611 4612 return 4613 } 4614 } else { 4615 4616 rws.writeChunk(nil) 4617 } 4618 } 4619 4620 func (w *http2responseWriter) CloseNotify() <-chan bool { 4621 rws := w.rws 4622 if rws == nil { 4623 panic("CloseNotify called after Handler finished") 4624 } 4625 rws.closeNotifierMu.Lock() 4626 ch := rws.closeNotifierCh 4627 if ch == nil { 4628 ch = make(chan bool, 1) 4629 rws.closeNotifierCh = ch 4630 go func() { 4631 rws.stream.cw.Wait() 4632 ch <- true 4633 }() 4634 } 4635 rws.closeNotifierMu.Unlock() 4636 return ch 4637 } 4638 4639 func (w *http2responseWriter) Header() Header { 4640 rws := w.rws 4641 if rws == nil { 4642 panic("Header called after Handler finished") 4643 } 4644 if rws.handlerHeader == nil { 4645 rws.handlerHeader = make(Header) 4646 } 4647 return rws.handlerHeader 4648 } 4649 4650 func (w *http2responseWriter) WriteHeader(code int) { 4651 rws := w.rws 4652 if rws == nil { 4653 panic("WriteHeader called after Handler finished") 4654 } 4655 rws.writeHeader(code) 4656 } 4657 4658 func (rws *http2responseWriterState) writeHeader(code int) { 4659 if !rws.wroteHeader { 4660 rws.wroteHeader = true 4661 rws.status = code 4662 if len(rws.handlerHeader) > 0 { 4663 rws.snapHeader = http2cloneHeader(rws.handlerHeader) 4664 } 4665 } 4666 } 4667 4668 func http2cloneHeader(h Header) Header { 4669 h2 := make(Header, len(h)) 4670 for k, vv := range h { 4671 vv2 := make([]string, len(vv)) 4672 copy(vv2, vv) 4673 h2[k] = vv2 4674 } 4675 return h2 4676 } 4677 4678 // The Life Of A Write is like this: 4679 // 4680 // * Handler calls w.Write or w.WriteString -> 4681 // * -> rws.bw (*bufio.Writer) -> 4682 // * (Handler migth call Flush) 4683 // * -> chunkWriter{rws} 4684 // * -> responseWriterState.writeChunk(p []byte) 4685 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 4686 func (w *http2responseWriter) Write(p []byte) (n int, err error) { 4687 return w.write(len(p), p, "") 4688 } 4689 4690 func (w *http2responseWriter) WriteString(s string) (n int, err error) { 4691 return w.write(len(s), nil, s) 4692 } 4693 4694 // either dataB or dataS is non-zero. 4695 func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 4696 rws := w.rws 4697 if rws == nil { 4698 panic("Write called after Handler finished") 4699 } 4700 if !rws.wroteHeader { 4701 w.WriteHeader(200) 4702 } 4703 if !http2bodyAllowedForStatus(rws.status) { 4704 return 0, ErrBodyNotAllowed 4705 } 4706 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) 4707 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen { 4708 4709 return 0, errors.New("http2: handler wrote more than declared Content-Length") 4710 } 4711 4712 if dataB != nil { 4713 return rws.bw.Write(dataB) 4714 } else { 4715 return rws.bw.WriteString(dataS) 4716 } 4717 } 4718 4719 func (w *http2responseWriter) handlerDone() { 4720 rws := w.rws 4721 rws.handlerDone = true 4722 w.Flush() 4723 w.rws = nil 4724 http2responseWriterStatePool.Put(rws) 4725 } 4726 4727 // foreachHeaderElement splits v according to the "#rule" construction 4728 // in RFC 2616 section 2.1 and calls fn for each non-empty element. 4729 func http2foreachHeaderElement(v string, fn func(string)) { 4730 v = textproto.TrimString(v) 4731 if v == "" { 4732 return 4733 } 4734 if !strings.Contains(v, ",") { 4735 fn(v) 4736 return 4737 } 4738 for _, f := range strings.Split(v, ",") { 4739 if f = textproto.TrimString(f); f != "" { 4740 fn(f) 4741 } 4742 } 4743 } 4744 4745 // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2 4746 var http2connHeaders = []string{ 4747 "Connection", 4748 "Keep-Alive", 4749 "Proxy-Connection", 4750 "Transfer-Encoding", 4751 "Upgrade", 4752 } 4753 4754 // checkValidHTTP2Request checks whether req is a valid HTTP/2 request, 4755 // per RFC 7540 Section 8.1.2.2. 4756 // The returned error is reported to users. 4757 func http2checkValidHTTP2Request(req *Request) error { 4758 for _, h := range http2connHeaders { 4759 if _, ok := req.Header[h]; ok { 4760 return fmt.Errorf("request header %q is not valid in HTTP/2", h) 4761 } 4762 } 4763 te := req.Header["Te"] 4764 if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) { 4765 return errors.New(`request header "TE" may only be "trailers" in HTTP/2`) 4766 } 4767 return nil 4768 } 4769 4770 func http2new400Handler(err error) HandlerFunc { 4771 return func(w ResponseWriter, r *Request) { 4772 Error(w, err.Error(), StatusBadRequest) 4773 } 4774 } 4775 4776 // ValidTrailerHeader reports whether name is a valid header field name to appear 4777 // in trailers. 4778 // See: http://tools.ietf.org/html/rfc7230#section-4.1.2 4779 func http2ValidTrailerHeader(name string) bool { 4780 name = CanonicalHeaderKey(name) 4781 if strings.HasPrefix(name, "If-") || http2badTrailer[name] { 4782 return false 4783 } 4784 return true 4785 } 4786 4787 var http2badTrailer = map[string]bool{ 4788 "Authorization": true, 4789 "Cache-Control": true, 4790 "Connection": true, 4791 "Content-Encoding": true, 4792 "Content-Length": true, 4793 "Content-Range": true, 4794 "Content-Type": true, 4795 "Expect": true, 4796 "Host": true, 4797 "Keep-Alive": true, 4798 "Max-Forwards": true, 4799 "Pragma": true, 4800 "Proxy-Authenticate": true, 4801 "Proxy-Authorization": true, 4802 "Proxy-Connection": true, 4803 "Range": true, 4804 "Realm": true, 4805 "Te": true, 4806 "Trailer": true, 4807 "Transfer-Encoding": true, 4808 "Www-Authenticate": true, 4809 } 4810 4811 const ( 4812 // transportDefaultConnFlow is how many connection-level flow control 4813 // tokens we give the server at start-up, past the default 64k. 4814 http2transportDefaultConnFlow = 1 << 30 4815 4816 // transportDefaultStreamFlow is how many stream-level flow 4817 // control tokens we announce to the peer, and how many bytes 4818 // we buffer per stream. 4819 http2transportDefaultStreamFlow = 4 << 20 4820 4821 // transportDefaultStreamMinRefresh is the minimum number of bytes we'll send 4822 // a stream-level WINDOW_UPDATE for at a time. 4823 http2transportDefaultStreamMinRefresh = 4 << 10 4824 4825 http2defaultUserAgent = "Go-http-client/2.0" 4826 ) 4827 4828 // Transport is an HTTP/2 Transport. 4829 // 4830 // A Transport internally caches connections to servers. It is safe 4831 // for concurrent use by multiple goroutines. 4832 type http2Transport struct { 4833 // DialTLS specifies an optional dial function for creating 4834 // TLS connections for requests. 4835 // 4836 // If DialTLS is nil, tls.Dial is used. 4837 // 4838 // If the returned net.Conn has a ConnectionState method like tls.Conn, 4839 // it will be used to set http.Response.TLS. 4840 DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error) 4841 4842 // TLSClientConfig specifies the TLS configuration to use with 4843 // tls.Client. If nil, the default configuration is used. 4844 TLSClientConfig *tls.Config 4845 4846 // ConnPool optionally specifies an alternate connection pool to use. 4847 // If nil, the default is used. 4848 ConnPool http2ClientConnPool 4849 4850 // DisableCompression, if true, prevents the Transport from 4851 // requesting compression with an "Accept-Encoding: gzip" 4852 // request header when the Request contains no existing 4853 // Accept-Encoding value. If the Transport requests gzip on 4854 // its own and gets a gzipped response, it's transparently 4855 // decoded in the Response.Body. However, if the user 4856 // explicitly requested gzip it is not automatically 4857 // uncompressed. 4858 DisableCompression bool 4859 4860 // AllowHTTP, if true, permits HTTP/2 requests using the insecure, 4861 // plain-text "http" scheme. Note that this does not enable h2c support. 4862 AllowHTTP bool 4863 4864 // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to 4865 // send in the initial settings frame. It is how many bytes 4866 // of response headers are allow. Unlike the http2 spec, zero here 4867 // means to use a default limit (currently 10MB). If you actually 4868 // want to advertise an ulimited value to the peer, Transport 4869 // interprets the highest possible value here (0xffffffff or 1<<32-1) 4870 // to mean no limit. 4871 MaxHeaderListSize uint32 4872 4873 // t1, if non-nil, is the standard library Transport using 4874 // this transport. Its settings are used (but not its 4875 // RoundTrip method, etc). 4876 t1 *Transport 4877 4878 connPoolOnce sync.Once 4879 connPoolOrDef http2ClientConnPool // non-nil version of ConnPool 4880 } 4881 4882 func (t *http2Transport) maxHeaderListSize() uint32 { 4883 if t.MaxHeaderListSize == 0 { 4884 return 10 << 20 4885 } 4886 if t.MaxHeaderListSize == 0xffffffff { 4887 return 0 4888 } 4889 return t.MaxHeaderListSize 4890 } 4891 4892 func (t *http2Transport) disableCompression() bool { 4893 return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression) 4894 } 4895 4896 var http2errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6") 4897 4898 // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2. 4899 // It requires Go 1.6 or later and returns an error if the net/http package is too old 4900 // or if t1 has already been HTTP/2-enabled. 4901 func http2ConfigureTransport(t1 *Transport) error { 4902 _, err := http2configureTransport(t1) 4903 return err 4904 } 4905 4906 func (t *http2Transport) connPool() http2ClientConnPool { 4907 t.connPoolOnce.Do(t.initConnPool) 4908 return t.connPoolOrDef 4909 } 4910 4911 func (t *http2Transport) initConnPool() { 4912 if t.ConnPool != nil { 4913 t.connPoolOrDef = t.ConnPool 4914 } else { 4915 t.connPoolOrDef = &http2clientConnPool{t: t} 4916 } 4917 } 4918 4919 // ClientConn is the state of a single HTTP/2 client connection to an 4920 // HTTP/2 server. 4921 type http2ClientConn struct { 4922 t *http2Transport 4923 tconn net.Conn // usually *tls.Conn, except specialized impls 4924 tlsState *tls.ConnectionState // nil only for specialized impls 4925 4926 // readLoop goroutine fields: 4927 readerDone chan struct{} // closed on error 4928 readerErr error // set before readerDone is closed 4929 4930 mu sync.Mutex // guards following 4931 cond *sync.Cond // hold mu; broadcast on flow/closed changes 4932 flow http2flow // our conn-level flow control quota (cs.flow is per stream) 4933 inflow http2flow // peer's conn-level flow control 4934 closed bool 4935 goAway *http2GoAwayFrame // if non-nil, the GoAwayFrame we received 4936 goAwayDebug string // goAway frame's debug data, retained as a string 4937 streams map[uint32]*http2clientStream // client-initiated 4938 nextStreamID uint32 4939 bw *bufio.Writer 4940 br *bufio.Reader 4941 fr *http2Framer 4942 lastActive time.Time 4943 4944 // Settings from peer: 4945 maxFrameSize uint32 4946 maxConcurrentStreams uint32 4947 initialWindowSize uint32 4948 hbuf bytes.Buffer // HPACK encoder writes into this 4949 henc *hpack.Encoder 4950 freeBuf [][]byte 4951 4952 wmu sync.Mutex // held while writing; acquire AFTER mu if holding both 4953 werr error // first write error that has occurred 4954 } 4955 4956 // clientStream is the state for a single HTTP/2 stream. One of these 4957 // is created for each Transport.RoundTrip call. 4958 type http2clientStream struct { 4959 cc *http2ClientConn 4960 req *Request 4961 trace *http2clientTrace // or nil 4962 ID uint32 4963 resc chan http2resAndError 4964 bufPipe http2pipe // buffered pipe with the flow-controlled response payload 4965 requestedGzip bool 4966 on100 func() // optional code to run if get a 100 continue response 4967 4968 flow http2flow // guarded by cc.mu 4969 inflow http2flow // guarded by cc.mu 4970 bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read 4971 readErr error // sticky read error; owned by transportResponseBody.Read 4972 stopReqBody error // if non-nil, stop writing req body; guarded by cc.mu 4973 4974 peerReset chan struct{} // closed on peer reset 4975 resetErr error // populated before peerReset is closed 4976 4977 done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu 4978 4979 // owned by clientConnReadLoop: 4980 firstByte bool // got the first response byte 4981 pastHeaders bool // got first MetaHeadersFrame (actual headers) 4982 pastTrailers bool // got optional second MetaHeadersFrame (trailers) 4983 4984 trailer Header // accumulated trailers 4985 resTrailer *Header // client's Response.Trailer 4986 } 4987 4988 // awaitRequestCancel runs in its own goroutine and waits for the user 4989 // to cancel a RoundTrip request, its context to expire, or for the 4990 // request to be done (any way it might be removed from the cc.streams 4991 // map: peer reset, successful completion, TCP connection breakage, 4992 // etc) 4993 func (cs *http2clientStream) awaitRequestCancel(req *Request) { 4994 ctx := http2reqContext(req) 4995 if req.Cancel == nil && ctx.Done() == nil { 4996 return 4997 } 4998 select { 4999 case <-req.Cancel: 5000 cs.bufPipe.CloseWithError(http2errRequestCanceled) 5001 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5002 case <-ctx.Done(): 5003 cs.bufPipe.CloseWithError(ctx.Err()) 5004 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5005 case <-cs.done: 5006 } 5007 } 5008 5009 // checkResetOrDone reports any error sent in a RST_STREAM frame by the 5010 // server, or errStreamClosed if the stream is complete. 5011 func (cs *http2clientStream) checkResetOrDone() error { 5012 select { 5013 case <-cs.peerReset: 5014 return cs.resetErr 5015 case <-cs.done: 5016 return http2errStreamClosed 5017 default: 5018 return nil 5019 } 5020 } 5021 5022 func (cs *http2clientStream) abortRequestBodyWrite(err error) { 5023 if err == nil { 5024 panic("nil error") 5025 } 5026 cc := cs.cc 5027 cc.mu.Lock() 5028 cs.stopReqBody = err 5029 cc.cond.Broadcast() 5030 cc.mu.Unlock() 5031 } 5032 5033 type http2stickyErrWriter struct { 5034 w io.Writer 5035 err *error 5036 } 5037 5038 func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) { 5039 if *sew.err != nil { 5040 return 0, *sew.err 5041 } 5042 n, err = sew.w.Write(p) 5043 *sew.err = err 5044 return 5045 } 5046 5047 var http2ErrNoCachedConn = errors.New("http2: no cached connection was available") 5048 5049 // RoundTripOpt are options for the Transport.RoundTripOpt method. 5050 type http2RoundTripOpt struct { 5051 // OnlyCachedConn controls whether RoundTripOpt may 5052 // create a new TCP connection. If set true and 5053 // no cached connection is available, RoundTripOpt 5054 // will return ErrNoCachedConn. 5055 OnlyCachedConn bool 5056 } 5057 5058 func (t *http2Transport) RoundTrip(req *Request) (*Response, error) { 5059 return t.RoundTripOpt(req, http2RoundTripOpt{}) 5060 } 5061 5062 // authorityAddr returns a given authority (a host/IP, or host:port / ip:port) 5063 // and returns a host:port. The port 443 is added if needed. 5064 func http2authorityAddr(scheme string, authority string) (addr string) { 5065 if _, _, err := net.SplitHostPort(authority); err == nil { 5066 return authority 5067 } 5068 port := "443" 5069 if scheme == "http" { 5070 port = "80" 5071 } 5072 return net.JoinHostPort(authority, port) 5073 } 5074 5075 // RoundTripOpt is like RoundTrip, but takes options. 5076 func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) { 5077 if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) { 5078 return nil, errors.New("http2: unsupported scheme") 5079 } 5080 5081 addr := http2authorityAddr(req.URL.Scheme, req.URL.Host) 5082 for { 5083 cc, err := t.connPool().GetClientConn(req, addr) 5084 if err != nil { 5085 t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err) 5086 return nil, err 5087 } 5088 http2traceGotConn(req, cc) 5089 res, err := cc.RoundTrip(req) 5090 if http2shouldRetryRequest(req, err) { 5091 continue 5092 } 5093 if err != nil { 5094 t.vlogf("RoundTrip failure: %v", err) 5095 return nil, err 5096 } 5097 return res, nil 5098 } 5099 } 5100 5101 // CloseIdleConnections closes any connections which were previously 5102 // connected from previous requests but are now sitting idle. 5103 // It does not interrupt any connections currently in use. 5104 func (t *http2Transport) CloseIdleConnections() { 5105 if cp, ok := t.connPool().(http2clientConnPoolIdleCloser); ok { 5106 cp.closeIdleConnections() 5107 } 5108 } 5109 5110 var ( 5111 http2errClientConnClosed = errors.New("http2: client conn is closed") 5112 http2errClientConnUnusable = errors.New("http2: client conn not usable") 5113 ) 5114 5115 func http2shouldRetryRequest(req *Request, err error) bool { 5116 5117 return err == http2errClientConnUnusable 5118 } 5119 5120 func (t *http2Transport) dialClientConn(addr string) (*http2ClientConn, error) { 5121 host, _, err := net.SplitHostPort(addr) 5122 if err != nil { 5123 return nil, err 5124 } 5125 tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host)) 5126 if err != nil { 5127 return nil, err 5128 } 5129 return t.NewClientConn(tconn) 5130 } 5131 5132 func (t *http2Transport) newTLSConfig(host string) *tls.Config { 5133 cfg := new(tls.Config) 5134 if t.TLSClientConfig != nil { 5135 *cfg = *t.TLSClientConfig 5136 } 5137 if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) { 5138 cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...) 5139 } 5140 if cfg.ServerName == "" { 5141 cfg.ServerName = host 5142 } 5143 return cfg 5144 } 5145 5146 func (t *http2Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) { 5147 if t.DialTLS != nil { 5148 return t.DialTLS 5149 } 5150 return t.dialTLSDefault 5151 } 5152 5153 func (t *http2Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) { 5154 cn, err := tls.Dial(network, addr, cfg) 5155 if err != nil { 5156 return nil, err 5157 } 5158 if err := cn.Handshake(); err != nil { 5159 return nil, err 5160 } 5161 if !cfg.InsecureSkipVerify { 5162 if err := cn.VerifyHostname(cfg.ServerName); err != nil { 5163 return nil, err 5164 } 5165 } 5166 state := cn.ConnectionState() 5167 if p := state.NegotiatedProtocol; p != http2NextProtoTLS { 5168 return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS) 5169 } 5170 if !state.NegotiatedProtocolIsMutual { 5171 return nil, errors.New("http2: could not negotiate protocol mutually") 5172 } 5173 return cn, nil 5174 } 5175 5176 // disableKeepAlives reports whether connections should be closed as 5177 // soon as possible after handling the first request. 5178 func (t *http2Transport) disableKeepAlives() bool { 5179 return t.t1 != nil && t.t1.DisableKeepAlives 5180 } 5181 5182 func (t *http2Transport) expectContinueTimeout() time.Duration { 5183 if t.t1 == nil { 5184 return 0 5185 } 5186 return http2transportExpectContinueTimeout(t.t1) 5187 } 5188 5189 func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) { 5190 if http2VerboseLogs { 5191 t.vlogf("http2: Transport creating client conn to %v", c.RemoteAddr()) 5192 } 5193 if _, err := c.Write(http2clientPreface); err != nil { 5194 t.vlogf("client preface write error: %v", err) 5195 return nil, err 5196 } 5197 5198 cc := &http2ClientConn{ 5199 t: t, 5200 tconn: c, 5201 readerDone: make(chan struct{}), 5202 nextStreamID: 1, 5203 maxFrameSize: 16 << 10, 5204 initialWindowSize: 65535, 5205 maxConcurrentStreams: 1000, 5206 streams: make(map[uint32]*http2clientStream), 5207 } 5208 cc.cond = sync.NewCond(&cc.mu) 5209 cc.flow.add(int32(http2initialWindowSize)) 5210 5211 cc.bw = bufio.NewWriter(http2stickyErrWriter{c, &cc.werr}) 5212 cc.br = bufio.NewReader(c) 5213 cc.fr = http2NewFramer(cc.bw, cc.br) 5214 cc.fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil) 5215 cc.fr.MaxHeaderListSize = t.maxHeaderListSize() 5216 5217 cc.henc = hpack.NewEncoder(&cc.hbuf) 5218 5219 if cs, ok := c.(http2connectionStater); ok { 5220 state := cs.ConnectionState() 5221 cc.tlsState = &state 5222 } 5223 5224 initialSettings := []http2Setting{ 5225 {ID: http2SettingEnablePush, Val: 0}, 5226 {ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow}, 5227 } 5228 if max := t.maxHeaderListSize(); max != 0 { 5229 initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max}) 5230 } 5231 cc.fr.WriteSettings(initialSettings...) 5232 cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow) 5233 cc.inflow.add(http2transportDefaultConnFlow + http2initialWindowSize) 5234 cc.bw.Flush() 5235 if cc.werr != nil { 5236 return nil, cc.werr 5237 } 5238 5239 f, err := cc.fr.ReadFrame() 5240 if err != nil { 5241 return nil, err 5242 } 5243 sf, ok := f.(*http2SettingsFrame) 5244 if !ok { 5245 return nil, fmt.Errorf("expected settings frame, got: %T", f) 5246 } 5247 cc.fr.WriteSettingsAck() 5248 cc.bw.Flush() 5249 5250 sf.ForeachSetting(func(s http2Setting) error { 5251 switch s.ID { 5252 case http2SettingMaxFrameSize: 5253 cc.maxFrameSize = s.Val 5254 case http2SettingMaxConcurrentStreams: 5255 cc.maxConcurrentStreams = s.Val 5256 case http2SettingInitialWindowSize: 5257 cc.initialWindowSize = s.Val 5258 default: 5259 5260 t.vlogf("Unhandled Setting: %v", s) 5261 } 5262 return nil 5263 }) 5264 5265 go cc.readLoop() 5266 return cc, nil 5267 } 5268 5269 func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) { 5270 cc.mu.Lock() 5271 defer cc.mu.Unlock() 5272 5273 old := cc.goAway 5274 cc.goAway = f 5275 5276 if cc.goAwayDebug == "" { 5277 cc.goAwayDebug = string(f.DebugData()) 5278 } 5279 if old != nil && old.ErrCode != http2ErrCodeNo { 5280 cc.goAway.ErrCode = old.ErrCode 5281 } 5282 } 5283 5284 func (cc *http2ClientConn) CanTakeNewRequest() bool { 5285 cc.mu.Lock() 5286 defer cc.mu.Unlock() 5287 return cc.canTakeNewRequestLocked() 5288 } 5289 5290 func (cc *http2ClientConn) canTakeNewRequestLocked() bool { 5291 return cc.goAway == nil && !cc.closed && 5292 int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) && 5293 cc.nextStreamID < 2147483647 5294 } 5295 5296 func (cc *http2ClientConn) closeIfIdle() { 5297 cc.mu.Lock() 5298 if len(cc.streams) > 0 { 5299 cc.mu.Unlock() 5300 return 5301 } 5302 cc.closed = true 5303 5304 cc.mu.Unlock() 5305 5306 cc.tconn.Close() 5307 } 5308 5309 const http2maxAllocFrameSize = 512 << 10 5310 5311 // frameBuffer returns a scratch buffer suitable for writing DATA frames. 5312 // They're capped at the min of the peer's max frame size or 512KB 5313 // (kinda arbitrarily), but definitely capped so we don't allocate 4GB 5314 // bufers. 5315 func (cc *http2ClientConn) frameScratchBuffer() []byte { 5316 cc.mu.Lock() 5317 size := cc.maxFrameSize 5318 if size > http2maxAllocFrameSize { 5319 size = http2maxAllocFrameSize 5320 } 5321 for i, buf := range cc.freeBuf { 5322 if len(buf) >= int(size) { 5323 cc.freeBuf[i] = nil 5324 cc.mu.Unlock() 5325 return buf[:size] 5326 } 5327 } 5328 cc.mu.Unlock() 5329 return make([]byte, size) 5330 } 5331 5332 func (cc *http2ClientConn) putFrameScratchBuffer(buf []byte) { 5333 cc.mu.Lock() 5334 defer cc.mu.Unlock() 5335 const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate. 5336 if len(cc.freeBuf) < maxBufs { 5337 cc.freeBuf = append(cc.freeBuf, buf) 5338 return 5339 } 5340 for i, old := range cc.freeBuf { 5341 if old == nil { 5342 cc.freeBuf[i] = buf 5343 return 5344 } 5345 } 5346 5347 } 5348 5349 // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not 5350 // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests. 5351 var http2errRequestCanceled = errors.New("net/http: request canceled") 5352 5353 func http2commaSeparatedTrailers(req *Request) (string, error) { 5354 keys := make([]string, 0, len(req.Trailer)) 5355 for k := range req.Trailer { 5356 k = CanonicalHeaderKey(k) 5357 switch k { 5358 case "Transfer-Encoding", "Trailer", "Content-Length": 5359 return "", &http2badStringError{"invalid Trailer key", k} 5360 } 5361 keys = append(keys, k) 5362 } 5363 if len(keys) > 0 { 5364 sort.Strings(keys) 5365 5366 return strings.Join(keys, ","), nil 5367 } 5368 return "", nil 5369 } 5370 5371 func (cc *http2ClientConn) responseHeaderTimeout() time.Duration { 5372 if cc.t.t1 != nil { 5373 return cc.t.t1.ResponseHeaderTimeout 5374 } 5375 5376 return 0 5377 } 5378 5379 // checkConnHeaders checks whether req has any invalid connection-level headers. 5380 // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields. 5381 // Certain headers are special-cased as okay but not transmitted later. 5382 func http2checkConnHeaders(req *Request) error { 5383 if v := req.Header.Get("Upgrade"); v != "" { 5384 return errors.New("http2: invalid Upgrade request header") 5385 } 5386 if v := req.Header.Get("Transfer-Encoding"); (v != "" && v != "chunked") || len(req.Header["Transfer-Encoding"]) > 1 { 5387 return errors.New("http2: invalid Transfer-Encoding request header") 5388 } 5389 if v := req.Header.Get("Connection"); (v != "" && v != "close" && v != "keep-alive") || len(req.Header["Connection"]) > 1 { 5390 return errors.New("http2: invalid Connection request header") 5391 } 5392 return nil 5393 } 5394 5395 func http2bodyAndLength(req *Request) (body io.Reader, contentLen int64) { 5396 body = req.Body 5397 if body == nil { 5398 return nil, 0 5399 } 5400 if req.ContentLength != 0 { 5401 return req.Body, req.ContentLength 5402 } 5403 5404 // We have a body but a zero content length. Test to see if 5405 // it's actually zero or just unset. 5406 var buf [1]byte 5407 n, rerr := io.ReadFull(body, buf[:]) 5408 if rerr != nil && rerr != io.EOF { 5409 return http2errorReader{rerr}, -1 5410 } 5411 if n == 1 { 5412 5413 return io.MultiReader(bytes.NewReader(buf[:]), body), -1 5414 } 5415 5416 return nil, 0 5417 } 5418 5419 func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) { 5420 if err := http2checkConnHeaders(req); err != nil { 5421 return nil, err 5422 } 5423 5424 trailers, err := http2commaSeparatedTrailers(req) 5425 if err != nil { 5426 return nil, err 5427 } 5428 hasTrailers := trailers != "" 5429 5430 body, contentLen := http2bodyAndLength(req) 5431 hasBody := body != nil 5432 5433 cc.mu.Lock() 5434 cc.lastActive = time.Now() 5435 if cc.closed || !cc.canTakeNewRequestLocked() { 5436 cc.mu.Unlock() 5437 return nil, http2errClientConnUnusable 5438 } 5439 5440 // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere? 5441 var requestedGzip bool 5442 if !cc.t.disableCompression() && 5443 req.Header.Get("Accept-Encoding") == "" && 5444 req.Header.Get("Range") == "" && 5445 req.Method != "HEAD" { 5446 5447 requestedGzip = true 5448 } 5449 5450 hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen) 5451 if err != nil { 5452 cc.mu.Unlock() 5453 return nil, err 5454 } 5455 5456 cs := cc.newStream() 5457 cs.req = req 5458 cs.trace = http2requestTrace(req) 5459 cs.requestedGzip = requestedGzip 5460 bodyWriter := cc.t.getBodyWriterState(cs, body) 5461 cs.on100 = bodyWriter.on100 5462 5463 cc.wmu.Lock() 5464 endStream := !hasBody && !hasTrailers 5465 werr := cc.writeHeaders(cs.ID, endStream, hdrs) 5466 cc.wmu.Unlock() 5467 http2traceWroteHeaders(cs.trace) 5468 cc.mu.Unlock() 5469 5470 if werr != nil { 5471 if hasBody { 5472 req.Body.Close() 5473 bodyWriter.cancel() 5474 } 5475 cc.forgetStreamID(cs.ID) 5476 5477 http2traceWroteRequest(cs.trace, werr) 5478 return nil, werr 5479 } 5480 5481 var respHeaderTimer <-chan time.Time 5482 if hasBody { 5483 bodyWriter.scheduleBodyWrite() 5484 } else { 5485 http2traceWroteRequest(cs.trace, nil) 5486 if d := cc.responseHeaderTimeout(); d != 0 { 5487 timer := time.NewTimer(d) 5488 defer timer.Stop() 5489 respHeaderTimer = timer.C 5490 } 5491 } 5492 5493 readLoopResCh := cs.resc 5494 bodyWritten := false 5495 ctx := http2reqContext(req) 5496 5497 for { 5498 select { 5499 case re := <-readLoopResCh: 5500 res := re.res 5501 if re.err != nil || res.StatusCode > 299 { 5502 5503 bodyWriter.cancel() 5504 cs.abortRequestBodyWrite(http2errStopReqBodyWrite) 5505 } 5506 if re.err != nil { 5507 cc.forgetStreamID(cs.ID) 5508 return nil, re.err 5509 } 5510 res.Request = req 5511 res.TLS = cc.tlsState 5512 return res, nil 5513 case <-respHeaderTimer: 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, http2errTimeout 5522 case <-ctx.Done(): 5523 cc.forgetStreamID(cs.ID) 5524 if !hasBody || bodyWritten { 5525 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5526 } else { 5527 bodyWriter.cancel() 5528 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 5529 } 5530 return nil, ctx.Err() 5531 case <-req.Cancel: 5532 cc.forgetStreamID(cs.ID) 5533 if !hasBody || bodyWritten { 5534 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5535 } else { 5536 bodyWriter.cancel() 5537 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 5538 } 5539 return nil, http2errRequestCanceled 5540 case <-cs.peerReset: 5541 5542 return nil, cs.resetErr 5543 case err := <-bodyWriter.resc: 5544 if err != nil { 5545 return nil, err 5546 } 5547 bodyWritten = true 5548 if d := cc.responseHeaderTimeout(); d != 0 { 5549 timer := time.NewTimer(d) 5550 defer timer.Stop() 5551 respHeaderTimer = timer.C 5552 } 5553 } 5554 } 5555 } 5556 5557 // requires cc.wmu be held 5558 func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error { 5559 first := true 5560 frameSize := int(cc.maxFrameSize) 5561 for len(hdrs) > 0 && cc.werr == nil { 5562 chunk := hdrs 5563 if len(chunk) > frameSize { 5564 chunk = chunk[:frameSize] 5565 } 5566 hdrs = hdrs[len(chunk):] 5567 endHeaders := len(hdrs) == 0 5568 if first { 5569 cc.fr.WriteHeaders(http2HeadersFrameParam{ 5570 StreamID: streamID, 5571 BlockFragment: chunk, 5572 EndStream: endStream, 5573 EndHeaders: endHeaders, 5574 }) 5575 first = false 5576 } else { 5577 cc.fr.WriteContinuation(streamID, endHeaders, chunk) 5578 } 5579 } 5580 5581 cc.bw.Flush() 5582 return cc.werr 5583 } 5584 5585 // internal error values; they don't escape to callers 5586 var ( 5587 // abort request body write; don't send cancel 5588 http2errStopReqBodyWrite = errors.New("http2: aborting request body write") 5589 5590 // abort request body write, but send stream reset of cancel. 5591 http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request") 5592 ) 5593 5594 func (cs *http2clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) { 5595 cc := cs.cc 5596 sentEnd := false 5597 buf := cc.frameScratchBuffer() 5598 defer cc.putFrameScratchBuffer(buf) 5599 5600 defer func() { 5601 http2traceWroteRequest(cs.trace, err) 5602 5603 cerr := bodyCloser.Close() 5604 if err == nil { 5605 err = cerr 5606 } 5607 }() 5608 5609 req := cs.req 5610 hasTrailers := req.Trailer != nil 5611 5612 var sawEOF bool 5613 for !sawEOF { 5614 n, err := body.Read(buf) 5615 if err == io.EOF { 5616 sawEOF = true 5617 err = nil 5618 } else if err != nil { 5619 return err 5620 } 5621 5622 remain := buf[:n] 5623 for len(remain) > 0 && err == nil { 5624 var allowed int32 5625 allowed, err = cs.awaitFlowControl(len(remain)) 5626 switch { 5627 case err == http2errStopReqBodyWrite: 5628 return err 5629 case err == http2errStopReqBodyWriteAndCancel: 5630 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 5631 return err 5632 case err != nil: 5633 return err 5634 } 5635 cc.wmu.Lock() 5636 data := remain[:allowed] 5637 remain = remain[allowed:] 5638 sentEnd = sawEOF && len(remain) == 0 && !hasTrailers 5639 err = cc.fr.WriteData(cs.ID, sentEnd, data) 5640 if err == nil { 5641 5642 err = cc.bw.Flush() 5643 } 5644 cc.wmu.Unlock() 5645 } 5646 if err != nil { 5647 return err 5648 } 5649 } 5650 5651 cc.wmu.Lock() 5652 if !sentEnd { 5653 var trls []byte 5654 if hasTrailers { 5655 cc.mu.Lock() 5656 trls = cc.encodeTrailers(req) 5657 cc.mu.Unlock() 5658 } 5659 5660 if len(trls) > 0 { 5661 err = cc.writeHeaders(cs.ID, true, trls) 5662 } else { 5663 err = cc.fr.WriteData(cs.ID, true, nil) 5664 } 5665 } 5666 if ferr := cc.bw.Flush(); ferr != nil && err == nil { 5667 err = ferr 5668 } 5669 cc.wmu.Unlock() 5670 5671 return err 5672 } 5673 5674 // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow 5675 // control tokens from the server. 5676 // It returns either the non-zero number of tokens taken or an error 5677 // if the stream is dead. 5678 func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) { 5679 cc := cs.cc 5680 cc.mu.Lock() 5681 defer cc.mu.Unlock() 5682 for { 5683 if cc.closed { 5684 return 0, http2errClientConnClosed 5685 } 5686 if cs.stopReqBody != nil { 5687 return 0, cs.stopReqBody 5688 } 5689 if err := cs.checkResetOrDone(); err != nil { 5690 return 0, err 5691 } 5692 if a := cs.flow.available(); a > 0 { 5693 take := a 5694 if int(take) > maxBytes { 5695 5696 take = int32(maxBytes) 5697 } 5698 if take > int32(cc.maxFrameSize) { 5699 take = int32(cc.maxFrameSize) 5700 } 5701 cs.flow.take(take) 5702 return take, nil 5703 } 5704 cc.cond.Wait() 5705 } 5706 } 5707 5708 type http2badStringError struct { 5709 what string 5710 str string 5711 } 5712 5713 func (e *http2badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 5714 5715 // requires cc.mu be held. 5716 func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) { 5717 cc.hbuf.Reset() 5718 5719 host := req.Host 5720 if host == "" { 5721 host = req.URL.Host 5722 } 5723 5724 for k, vv := range req.Header { 5725 if !httplex.ValidHeaderFieldName(k) { 5726 return nil, fmt.Errorf("invalid HTTP header name %q", k) 5727 } 5728 for _, v := range vv { 5729 if !httplex.ValidHeaderFieldValue(v) { 5730 return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k) 5731 } 5732 } 5733 } 5734 5735 cc.writeHeader(":authority", host) 5736 cc.writeHeader(":method", req.Method) 5737 if req.Method != "CONNECT" { 5738 cc.writeHeader(":path", req.URL.RequestURI()) 5739 cc.writeHeader(":scheme", "https") 5740 } 5741 if trailers != "" { 5742 cc.writeHeader("trailer", trailers) 5743 } 5744 5745 var didUA bool 5746 for k, vv := range req.Header { 5747 lowKey := strings.ToLower(k) 5748 switch lowKey { 5749 case "host", "content-length": 5750 5751 continue 5752 case "connection", "proxy-connection", "transfer-encoding", "upgrade", "keep-alive": 5753 5754 continue 5755 case "user-agent": 5756 5757 didUA = true 5758 if len(vv) < 1 { 5759 continue 5760 } 5761 vv = vv[:1] 5762 if vv[0] == "" { 5763 continue 5764 } 5765 } 5766 for _, v := range vv { 5767 cc.writeHeader(lowKey, v) 5768 } 5769 } 5770 if http2shouldSendReqContentLength(req.Method, contentLength) { 5771 cc.writeHeader("content-length", strconv.FormatInt(contentLength, 10)) 5772 } 5773 if addGzipHeader { 5774 cc.writeHeader("accept-encoding", "gzip") 5775 } 5776 if !didUA { 5777 cc.writeHeader("user-agent", http2defaultUserAgent) 5778 } 5779 return cc.hbuf.Bytes(), nil 5780 } 5781 5782 // shouldSendReqContentLength reports whether the http2.Transport should send 5783 // a "content-length" request header. This logic is basically a copy of the net/http 5784 // transferWriter.shouldSendContentLength. 5785 // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown). 5786 // -1 means unknown. 5787 func http2shouldSendReqContentLength(method string, contentLength int64) bool { 5788 if contentLength > 0 { 5789 return true 5790 } 5791 if contentLength < 0 { 5792 return false 5793 } 5794 5795 switch method { 5796 case "POST", "PUT", "PATCH": 5797 return true 5798 default: 5799 return false 5800 } 5801 } 5802 5803 // requires cc.mu be held. 5804 func (cc *http2ClientConn) encodeTrailers(req *Request) []byte { 5805 cc.hbuf.Reset() 5806 for k, vv := range req.Trailer { 5807 5808 lowKey := strings.ToLower(k) 5809 for _, v := range vv { 5810 cc.writeHeader(lowKey, v) 5811 } 5812 } 5813 return cc.hbuf.Bytes() 5814 } 5815 5816 func (cc *http2ClientConn) writeHeader(name, value string) { 5817 if http2VerboseLogs { 5818 log.Printf("http2: Transport encoding header %q = %q", name, value) 5819 } 5820 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 5821 } 5822 5823 type http2resAndError struct { 5824 res *Response 5825 err error 5826 } 5827 5828 // requires cc.mu be held. 5829 func (cc *http2ClientConn) newStream() *http2clientStream { 5830 cs := &http2clientStream{ 5831 cc: cc, 5832 ID: cc.nextStreamID, 5833 resc: make(chan http2resAndError, 1), 5834 peerReset: make(chan struct{}), 5835 done: make(chan struct{}), 5836 } 5837 cs.flow.add(int32(cc.initialWindowSize)) 5838 cs.flow.setConnFlow(&cc.flow) 5839 cs.inflow.add(http2transportDefaultStreamFlow) 5840 cs.inflow.setConnFlow(&cc.inflow) 5841 cc.nextStreamID += 2 5842 cc.streams[cs.ID] = cs 5843 return cs 5844 } 5845 5846 func (cc *http2ClientConn) forgetStreamID(id uint32) { 5847 cc.streamByID(id, true) 5848 } 5849 5850 func (cc *http2ClientConn) streamByID(id uint32, andRemove bool) *http2clientStream { 5851 cc.mu.Lock() 5852 defer cc.mu.Unlock() 5853 cs := cc.streams[id] 5854 if andRemove && cs != nil && !cc.closed { 5855 cc.lastActive = time.Now() 5856 delete(cc.streams, id) 5857 close(cs.done) 5858 cc.cond.Broadcast() 5859 } 5860 return cs 5861 } 5862 5863 // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop. 5864 type http2clientConnReadLoop struct { 5865 cc *http2ClientConn 5866 activeRes map[uint32]*http2clientStream // keyed by streamID 5867 closeWhenIdle bool 5868 } 5869 5870 // readLoop runs in its own goroutine and reads and dispatches frames. 5871 func (cc *http2ClientConn) readLoop() { 5872 rl := &http2clientConnReadLoop{ 5873 cc: cc, 5874 activeRes: make(map[uint32]*http2clientStream), 5875 } 5876 5877 defer rl.cleanup() 5878 cc.readerErr = rl.run() 5879 if ce, ok := cc.readerErr.(http2ConnectionError); ok { 5880 cc.wmu.Lock() 5881 cc.fr.WriteGoAway(0, http2ErrCode(ce), nil) 5882 cc.wmu.Unlock() 5883 } 5884 } 5885 5886 // GoAwayError is returned by the Transport when the server closes the 5887 // TCP connection after sending a GOAWAY frame. 5888 type http2GoAwayError struct { 5889 LastStreamID uint32 5890 ErrCode http2ErrCode 5891 DebugData string 5892 } 5893 5894 func (e http2GoAwayError) Error() string { 5895 return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q", 5896 e.LastStreamID, e.ErrCode, e.DebugData) 5897 } 5898 5899 func (rl *http2clientConnReadLoop) cleanup() { 5900 cc := rl.cc 5901 defer cc.tconn.Close() 5902 defer cc.t.connPool().MarkDead(cc) 5903 defer close(cc.readerDone) 5904 5905 err := cc.readerErr 5906 cc.mu.Lock() 5907 if err == io.EOF { 5908 if cc.goAway != nil { 5909 err = http2GoAwayError{ 5910 LastStreamID: cc.goAway.LastStreamID, 5911 ErrCode: cc.goAway.ErrCode, 5912 DebugData: cc.goAwayDebug, 5913 } 5914 } else { 5915 err = io.ErrUnexpectedEOF 5916 } 5917 } 5918 for _, cs := range rl.activeRes { 5919 cs.bufPipe.CloseWithError(err) 5920 } 5921 for _, cs := range cc.streams { 5922 select { 5923 case cs.resc <- http2resAndError{err: err}: 5924 default: 5925 } 5926 close(cs.done) 5927 } 5928 cc.closed = true 5929 cc.cond.Broadcast() 5930 cc.mu.Unlock() 5931 } 5932 5933 func (rl *http2clientConnReadLoop) run() error { 5934 cc := rl.cc 5935 rl.closeWhenIdle = cc.t.disableKeepAlives() 5936 gotReply := false 5937 for { 5938 f, err := cc.fr.ReadFrame() 5939 if err != nil { 5940 cc.vlogf("Transport readFrame error: (%T) %v", err, err) 5941 } 5942 if se, ok := err.(http2StreamError); ok { 5943 if cs := cc.streamByID(se.StreamID, true); cs != nil { 5944 rl.endStreamError(cs, cc.fr.errDetail) 5945 } 5946 continue 5947 } else if err != nil { 5948 return err 5949 } 5950 if http2VerboseLogs { 5951 cc.vlogf("http2: Transport received %s", http2summarizeFrame(f)) 5952 } 5953 maybeIdle := false 5954 5955 switch f := f.(type) { 5956 case *http2MetaHeadersFrame: 5957 err = rl.processHeaders(f) 5958 maybeIdle = true 5959 gotReply = true 5960 case *http2DataFrame: 5961 err = rl.processData(f) 5962 maybeIdle = true 5963 case *http2GoAwayFrame: 5964 err = rl.processGoAway(f) 5965 maybeIdle = true 5966 case *http2RSTStreamFrame: 5967 err = rl.processResetStream(f) 5968 maybeIdle = true 5969 case *http2SettingsFrame: 5970 err = rl.processSettings(f) 5971 case *http2PushPromiseFrame: 5972 err = rl.processPushPromise(f) 5973 case *http2WindowUpdateFrame: 5974 err = rl.processWindowUpdate(f) 5975 case *http2PingFrame: 5976 err = rl.processPing(f) 5977 default: 5978 cc.logf("Transport: unhandled response frame type %T", f) 5979 } 5980 if err != nil { 5981 return err 5982 } 5983 if rl.closeWhenIdle && gotReply && maybeIdle && len(rl.activeRes) == 0 { 5984 cc.closeIfIdle() 5985 } 5986 } 5987 } 5988 5989 func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error { 5990 cc := rl.cc 5991 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 5992 if cs == nil { 5993 5994 return nil 5995 } 5996 if !cs.firstByte { 5997 if cs.trace != nil { 5998 5999 http2traceFirstResponseByte(cs.trace) 6000 } 6001 cs.firstByte = true 6002 } 6003 if !cs.pastHeaders { 6004 cs.pastHeaders = true 6005 } else { 6006 return rl.processTrailers(cs, f) 6007 } 6008 6009 res, err := rl.handleResponse(cs, f) 6010 if err != nil { 6011 if _, ok := err.(http2ConnectionError); ok { 6012 return err 6013 } 6014 6015 cs.cc.writeStreamReset(f.StreamID, http2ErrCodeProtocol, err) 6016 cs.resc <- http2resAndError{err: err} 6017 return nil 6018 } 6019 if res == nil { 6020 6021 return nil 6022 } 6023 if res.Body != http2noBody { 6024 rl.activeRes[cs.ID] = cs 6025 } 6026 cs.resTrailer = &res.Trailer 6027 cs.resc <- http2resAndError{res: res} 6028 return nil 6029 } 6030 6031 // may return error types nil, or ConnectionError. Any other error value 6032 // is a StreamError of type ErrCodeProtocol. The returned error in that case 6033 // is the detail. 6034 // 6035 // As a special case, handleResponse may return (nil, nil) to skip the 6036 // frame (currently only used for 100 expect continue). This special 6037 // case is going away after Issue 13851 is fixed. 6038 func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) { 6039 if f.Truncated { 6040 return nil, http2errResponseHeaderListSize 6041 } 6042 6043 status := f.PseudoValue("status") 6044 if status == "" { 6045 return nil, errors.New("missing status pseudo header") 6046 } 6047 statusCode, err := strconv.Atoi(status) 6048 if err != nil { 6049 return nil, errors.New("malformed non-numeric status pseudo header") 6050 } 6051 6052 if statusCode == 100 { 6053 http2traceGot100Continue(cs.trace) 6054 if cs.on100 != nil { 6055 cs.on100() 6056 } 6057 cs.pastHeaders = false 6058 return nil, nil 6059 } 6060 6061 header := make(Header) 6062 res := &Response{ 6063 Proto: "HTTP/2.0", 6064 ProtoMajor: 2, 6065 Header: header, 6066 StatusCode: statusCode, 6067 Status: status + " " + StatusText(statusCode), 6068 } 6069 for _, hf := range f.RegularFields() { 6070 key := CanonicalHeaderKey(hf.Name) 6071 if key == "Trailer" { 6072 t := res.Trailer 6073 if t == nil { 6074 t = make(Header) 6075 res.Trailer = t 6076 } 6077 http2foreachHeaderElement(hf.Value, func(v string) { 6078 t[CanonicalHeaderKey(v)] = nil 6079 }) 6080 } else { 6081 header[key] = append(header[key], hf.Value) 6082 } 6083 } 6084 6085 streamEnded := f.StreamEnded() 6086 isHead := cs.req.Method == "HEAD" 6087 if !streamEnded || isHead { 6088 res.ContentLength = -1 6089 if clens := res.Header["Content-Length"]; len(clens) == 1 { 6090 if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil { 6091 res.ContentLength = clen64 6092 } else { 6093 6094 } 6095 } else if len(clens) > 1 { 6096 6097 } 6098 } 6099 6100 if streamEnded || isHead { 6101 res.Body = http2noBody 6102 return res, nil 6103 } 6104 6105 buf := new(bytes.Buffer) 6106 cs.bufPipe = http2pipe{b: buf} 6107 cs.bytesRemain = res.ContentLength 6108 res.Body = http2transportResponseBody{cs} 6109 go cs.awaitRequestCancel(cs.req) 6110 6111 if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" { 6112 res.Header.Del("Content-Encoding") 6113 res.Header.Del("Content-Length") 6114 res.ContentLength = -1 6115 res.Body = &http2gzipReader{body: res.Body} 6116 http2setResponseUncompressed(res) 6117 } 6118 return res, nil 6119 } 6120 6121 func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error { 6122 if cs.pastTrailers { 6123 6124 return http2ConnectionError(http2ErrCodeProtocol) 6125 } 6126 cs.pastTrailers = true 6127 if !f.StreamEnded() { 6128 6129 return http2ConnectionError(http2ErrCodeProtocol) 6130 } 6131 if len(f.PseudoFields()) > 0 { 6132 6133 return http2ConnectionError(http2ErrCodeProtocol) 6134 } 6135 6136 trailer := make(Header) 6137 for _, hf := range f.RegularFields() { 6138 key := CanonicalHeaderKey(hf.Name) 6139 trailer[key] = append(trailer[key], hf.Value) 6140 } 6141 cs.trailer = trailer 6142 6143 rl.endStream(cs) 6144 return nil 6145 } 6146 6147 // transportResponseBody is the concrete type of Transport.RoundTrip's 6148 // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body. 6149 // On Close it sends RST_STREAM if EOF wasn't already seen. 6150 type http2transportResponseBody struct { 6151 cs *http2clientStream 6152 } 6153 6154 func (b http2transportResponseBody) Read(p []byte) (n int, err error) { 6155 cs := b.cs 6156 cc := cs.cc 6157 6158 if cs.readErr != nil { 6159 return 0, cs.readErr 6160 } 6161 n, err = b.cs.bufPipe.Read(p) 6162 if cs.bytesRemain != -1 { 6163 if int64(n) > cs.bytesRemain { 6164 n = int(cs.bytesRemain) 6165 if err == nil { 6166 err = errors.New("net/http: server replied with more than declared Content-Length; truncated") 6167 cc.writeStreamReset(cs.ID, http2ErrCodeProtocol, err) 6168 } 6169 cs.readErr = err 6170 return int(cs.bytesRemain), err 6171 } 6172 cs.bytesRemain -= int64(n) 6173 if err == io.EOF && cs.bytesRemain > 0 { 6174 err = io.ErrUnexpectedEOF 6175 cs.readErr = err 6176 return n, err 6177 } 6178 } 6179 if n == 0 { 6180 6181 return 6182 } 6183 6184 cc.mu.Lock() 6185 defer cc.mu.Unlock() 6186 6187 var connAdd, streamAdd int32 6188 6189 if v := cc.inflow.available(); v < http2transportDefaultConnFlow/2 { 6190 connAdd = http2transportDefaultConnFlow - v 6191 cc.inflow.add(connAdd) 6192 } 6193 if err == nil { 6194 6195 v := int(cs.inflow.available()) + cs.bufPipe.Len() 6196 if v < http2transportDefaultStreamFlow-http2transportDefaultStreamMinRefresh { 6197 streamAdd = int32(http2transportDefaultStreamFlow - v) 6198 cs.inflow.add(streamAdd) 6199 } 6200 } 6201 if connAdd != 0 || streamAdd != 0 { 6202 cc.wmu.Lock() 6203 defer cc.wmu.Unlock() 6204 if connAdd != 0 { 6205 cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd)) 6206 } 6207 if streamAdd != 0 { 6208 cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd)) 6209 } 6210 cc.bw.Flush() 6211 } 6212 return 6213 } 6214 6215 var http2errClosedResponseBody = errors.New("http2: response body closed") 6216 6217 func (b http2transportResponseBody) Close() error { 6218 cs := b.cs 6219 if cs.bufPipe.Err() != io.EOF { 6220 6221 cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 6222 } 6223 cs.bufPipe.BreakWithError(http2errClosedResponseBody) 6224 return nil 6225 } 6226 6227 func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error { 6228 cc := rl.cc 6229 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 6230 if cs == nil { 6231 cc.mu.Lock() 6232 neverSent := cc.nextStreamID 6233 cc.mu.Unlock() 6234 if f.StreamID >= neverSent { 6235 6236 cc.logf("http2: Transport received unsolicited DATA frame; closing connection") 6237 return http2ConnectionError(http2ErrCodeProtocol) 6238 } 6239 6240 return nil 6241 } 6242 if data := f.Data(); len(data) > 0 { 6243 if cs.bufPipe.b == nil { 6244 6245 cc.logf("http2: Transport received DATA frame for closed stream; closing connection") 6246 return http2ConnectionError(http2ErrCodeProtocol) 6247 } 6248 6249 cc.mu.Lock() 6250 if cs.inflow.available() >= int32(len(data)) { 6251 cs.inflow.take(int32(len(data))) 6252 } else { 6253 cc.mu.Unlock() 6254 return http2ConnectionError(http2ErrCodeFlowControl) 6255 } 6256 cc.mu.Unlock() 6257 6258 if _, err := cs.bufPipe.Write(data); err != nil { 6259 rl.endStreamError(cs, err) 6260 return err 6261 } 6262 } 6263 6264 if f.StreamEnded() { 6265 rl.endStream(cs) 6266 } 6267 return nil 6268 } 6269 6270 var http2errInvalidTrailers = errors.New("http2: invalid trailers") 6271 6272 func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) { 6273 6274 rl.endStreamError(cs, nil) 6275 } 6276 6277 func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) { 6278 var code func() 6279 if err == nil { 6280 err = io.EOF 6281 code = cs.copyTrailers 6282 } 6283 cs.bufPipe.closeWithErrorAndCode(err, code) 6284 delete(rl.activeRes, cs.ID) 6285 if cs.req.Close || cs.req.Header.Get("Connection") == "close" { 6286 rl.closeWhenIdle = true 6287 } 6288 } 6289 6290 func (cs *http2clientStream) copyTrailers() { 6291 for k, vv := range cs.trailer { 6292 t := cs.resTrailer 6293 if *t == nil { 6294 *t = make(Header) 6295 } 6296 (*t)[k] = vv 6297 } 6298 } 6299 6300 func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error { 6301 cc := rl.cc 6302 cc.t.connPool().MarkDead(cc) 6303 if f.ErrCode != 0 { 6304 6305 cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode) 6306 } 6307 cc.setGoAway(f) 6308 return nil 6309 } 6310 6311 func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error { 6312 cc := rl.cc 6313 cc.mu.Lock() 6314 defer cc.mu.Unlock() 6315 return f.ForeachSetting(func(s http2Setting) error { 6316 switch s.ID { 6317 case http2SettingMaxFrameSize: 6318 cc.maxFrameSize = s.Val 6319 case http2SettingMaxConcurrentStreams: 6320 cc.maxConcurrentStreams = s.Val 6321 case http2SettingInitialWindowSize: 6322 6323 cc.initialWindowSize = s.Val 6324 default: 6325 6326 cc.vlogf("Unhandled Setting: %v", s) 6327 } 6328 return nil 6329 }) 6330 } 6331 6332 func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error { 6333 cc := rl.cc 6334 cs := cc.streamByID(f.StreamID, false) 6335 if f.StreamID != 0 && cs == nil { 6336 return nil 6337 } 6338 6339 cc.mu.Lock() 6340 defer cc.mu.Unlock() 6341 6342 fl := &cc.flow 6343 if cs != nil { 6344 fl = &cs.flow 6345 } 6346 if !fl.add(int32(f.Increment)) { 6347 return http2ConnectionError(http2ErrCodeFlowControl) 6348 } 6349 cc.cond.Broadcast() 6350 return nil 6351 } 6352 6353 func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error { 6354 cs := rl.cc.streamByID(f.StreamID, true) 6355 if cs == nil { 6356 6357 return nil 6358 } 6359 select { 6360 case <-cs.peerReset: 6361 6362 default: 6363 err := http2StreamError{cs.ID, f.ErrCode} 6364 cs.resetErr = err 6365 close(cs.peerReset) 6366 cs.bufPipe.CloseWithError(err) 6367 cs.cc.cond.Broadcast() 6368 } 6369 delete(rl.activeRes, cs.ID) 6370 return nil 6371 } 6372 6373 func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error { 6374 if f.IsAck() { 6375 6376 return nil 6377 } 6378 cc := rl.cc 6379 cc.wmu.Lock() 6380 defer cc.wmu.Unlock() 6381 if err := cc.fr.WritePing(true, f.Data); err != nil { 6382 return err 6383 } 6384 return cc.bw.Flush() 6385 } 6386 6387 func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error { 6388 6389 return http2ConnectionError(http2ErrCodeProtocol) 6390 } 6391 6392 func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) { 6393 6394 cc.wmu.Lock() 6395 cc.fr.WriteRSTStream(streamID, code) 6396 cc.bw.Flush() 6397 cc.wmu.Unlock() 6398 } 6399 6400 var ( 6401 http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit") 6402 http2errPseudoTrailers = errors.New("http2: invalid pseudo header in trailers") 6403 ) 6404 6405 func (cc *http2ClientConn) logf(format string, args ...interface{}) { 6406 cc.t.logf(format, args...) 6407 } 6408 6409 func (cc *http2ClientConn) vlogf(format string, args ...interface{}) { 6410 cc.t.vlogf(format, args...) 6411 } 6412 6413 func (t *http2Transport) vlogf(format string, args ...interface{}) { 6414 if http2VerboseLogs { 6415 t.logf(format, args...) 6416 } 6417 } 6418 6419 func (t *http2Transport) logf(format string, args ...interface{}) { 6420 log.Printf(format, args...) 6421 } 6422 6423 var http2noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil)) 6424 6425 func http2strSliceContains(ss []string, s string) bool { 6426 for _, v := range ss { 6427 if v == s { 6428 return true 6429 } 6430 } 6431 return false 6432 } 6433 6434 type http2erringRoundTripper struct{ err error } 6435 6436 func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err } 6437 6438 // gzipReader wraps a response body so it can lazily 6439 // call gzip.NewReader on the first call to Read 6440 type http2gzipReader struct { 6441 body io.ReadCloser // underlying Response.Body 6442 zr *gzip.Reader // lazily-initialized gzip reader 6443 zerr error // sticky error 6444 } 6445 6446 func (gz *http2gzipReader) Read(p []byte) (n int, err error) { 6447 if gz.zerr != nil { 6448 return 0, gz.zerr 6449 } 6450 if gz.zr == nil { 6451 gz.zr, err = gzip.NewReader(gz.body) 6452 if err != nil { 6453 gz.zerr = err 6454 return 0, err 6455 } 6456 } 6457 return gz.zr.Read(p) 6458 } 6459 6460 func (gz *http2gzipReader) Close() error { 6461 return gz.body.Close() 6462 } 6463 6464 type http2errorReader struct{ err error } 6465 6466 func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err } 6467 6468 // bodyWriterState encapsulates various state around the Transport's writing 6469 // of the request body, particularly regarding doing delayed writes of the body 6470 // when the request contains "Expect: 100-continue". 6471 type http2bodyWriterState struct { 6472 cs *http2clientStream 6473 timer *time.Timer // if non-nil, we're doing a delayed write 6474 fnonce *sync.Once // to call fn with 6475 fn func() // the code to run in the goroutine, writing the body 6476 resc chan error // result of fn's execution 6477 delay time.Duration // how long we should delay a delayed write for 6478 } 6479 6480 func (t *http2Transport) getBodyWriterState(cs *http2clientStream, body io.Reader) (s http2bodyWriterState) { 6481 s.cs = cs 6482 if body == nil { 6483 return 6484 } 6485 resc := make(chan error, 1) 6486 s.resc = resc 6487 s.fn = func() { 6488 resc <- cs.writeRequestBody(body, cs.req.Body) 6489 } 6490 s.delay = t.expectContinueTimeout() 6491 if s.delay == 0 || 6492 !httplex.HeaderValuesContainsToken( 6493 cs.req.Header["Expect"], 6494 "100-continue") { 6495 return 6496 } 6497 s.fnonce = new(sync.Once) 6498 6499 // Arm the timer with a very large duration, which we'll 6500 // intentionally lower later. It has to be large now because 6501 // we need a handle to it before writing the headers, but the 6502 // s.delay value is defined to not start until after the 6503 // request headers were written. 6504 const hugeDuration = 365 * 24 * time.Hour 6505 s.timer = time.AfterFunc(hugeDuration, func() { 6506 s.fnonce.Do(s.fn) 6507 }) 6508 return 6509 } 6510 6511 func (s http2bodyWriterState) cancel() { 6512 if s.timer != nil { 6513 s.timer.Stop() 6514 } 6515 } 6516 6517 func (s http2bodyWriterState) on100() { 6518 if s.timer == nil { 6519 6520 return 6521 } 6522 s.timer.Stop() 6523 go func() { s.fnonce.Do(s.fn) }() 6524 } 6525 6526 // scheduleBodyWrite starts writing the body, either immediately (in 6527 // the common case) or after the delay timeout. It should not be 6528 // called until after the headers have been written. 6529 func (s http2bodyWriterState) scheduleBodyWrite() { 6530 if s.timer == nil { 6531 6532 go s.fn() 6533 return 6534 } 6535 http2traceWait100Continue(s.cs.trace) 6536 if s.timer.Stop() { 6537 s.timer.Reset(s.delay) 6538 } 6539 } 6540 6541 // writeFramer is implemented by any type that is used to write frames. 6542 type http2writeFramer interface { 6543 writeFrame(http2writeContext) error 6544 } 6545 6546 // writeContext is the interface needed by the various frame writer 6547 // types below. All the writeFrame methods below are scheduled via the 6548 // frame writing scheduler (see writeScheduler in writesched.go). 6549 // 6550 // This interface is implemented by *serverConn. 6551 // 6552 // TODO: decide whether to a) use this in the client code (which didn't 6553 // end up using this yet, because it has a simpler design, not 6554 // currently implementing priorities), or b) delete this and 6555 // make the server code a bit more concrete. 6556 type http2writeContext interface { 6557 Framer() *http2Framer 6558 Flush() error 6559 CloseConn() error 6560 // HeaderEncoder returns an HPACK encoder that writes to the 6561 // returned buffer. 6562 HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) 6563 } 6564 6565 // endsStream reports whether the given frame writer w will locally 6566 // close the stream. 6567 func http2endsStream(w http2writeFramer) bool { 6568 switch v := w.(type) { 6569 case *http2writeData: 6570 return v.endStream 6571 case *http2writeResHeaders: 6572 return v.endStream 6573 case nil: 6574 6575 panic("endsStream called on nil writeFramer") 6576 } 6577 return false 6578 } 6579 6580 type http2flushFrameWriter struct{} 6581 6582 func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error { 6583 return ctx.Flush() 6584 } 6585 6586 type http2writeSettings []http2Setting 6587 6588 func (s http2writeSettings) writeFrame(ctx http2writeContext) error { 6589 return ctx.Framer().WriteSettings([]http2Setting(s)...) 6590 } 6591 6592 type http2writeGoAway struct { 6593 maxStreamID uint32 6594 code http2ErrCode 6595 } 6596 6597 func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error { 6598 err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil) 6599 if p.code != 0 { 6600 ctx.Flush() 6601 time.Sleep(50 * time.Millisecond) 6602 ctx.CloseConn() 6603 } 6604 return err 6605 } 6606 6607 type http2writeData struct { 6608 streamID uint32 6609 p []byte 6610 endStream bool 6611 } 6612 6613 func (w *http2writeData) String() string { 6614 return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream) 6615 } 6616 6617 func (w *http2writeData) writeFrame(ctx http2writeContext) error { 6618 return ctx.Framer().WriteData(w.streamID, w.endStream, w.p) 6619 } 6620 6621 // handlerPanicRST is the message sent from handler goroutines when 6622 // the handler panics. 6623 type http2handlerPanicRST struct { 6624 StreamID uint32 6625 } 6626 6627 func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error { 6628 return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal) 6629 } 6630 6631 func (se http2StreamError) writeFrame(ctx http2writeContext) error { 6632 return ctx.Framer().WriteRSTStream(se.StreamID, se.Code) 6633 } 6634 6635 type http2writePingAck struct{ pf *http2PingFrame } 6636 6637 func (w http2writePingAck) writeFrame(ctx http2writeContext) error { 6638 return ctx.Framer().WritePing(true, w.pf.Data) 6639 } 6640 6641 type http2writeSettingsAck struct{} 6642 6643 func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error { 6644 return ctx.Framer().WriteSettingsAck() 6645 } 6646 6647 // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames 6648 // for HTTP response headers or trailers from a server handler. 6649 type http2writeResHeaders struct { 6650 streamID uint32 6651 httpResCode int // 0 means no ":status" line 6652 h Header // may be nil 6653 trailers []string // if non-nil, which keys of h to write. nil means all. 6654 endStream bool 6655 6656 date string 6657 contentType string 6658 contentLength string 6659 } 6660 6661 func http2encKV(enc *hpack.Encoder, k, v string) { 6662 if http2VerboseLogs { 6663 log.Printf("http2: server encoding header %q = %q", k, v) 6664 } 6665 enc.WriteField(hpack.HeaderField{Name: k, Value: v}) 6666 } 6667 6668 func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error { 6669 enc, buf := ctx.HeaderEncoder() 6670 buf.Reset() 6671 6672 if w.httpResCode != 0 { 6673 http2encKV(enc, ":status", http2httpCodeString(w.httpResCode)) 6674 } 6675 6676 http2encodeHeaders(enc, w.h, w.trailers) 6677 6678 if w.contentType != "" { 6679 http2encKV(enc, "content-type", w.contentType) 6680 } 6681 if w.contentLength != "" { 6682 http2encKV(enc, "content-length", w.contentLength) 6683 } 6684 if w.date != "" { 6685 http2encKV(enc, "date", w.date) 6686 } 6687 6688 headerBlock := buf.Bytes() 6689 if len(headerBlock) == 0 && w.trailers == nil { 6690 panic("unexpected empty hpack") 6691 } 6692 6693 // For now we're lazy and just pick the minimum MAX_FRAME_SIZE 6694 // that all peers must support (16KB). Later we could care 6695 // more and send larger frames if the peer advertised it, but 6696 // there's little point. Most headers are small anyway (so we 6697 // generally won't have CONTINUATION frames), and extra frames 6698 // only waste 9 bytes anyway. 6699 const maxFrameSize = 16384 6700 6701 first := true 6702 for len(headerBlock) > 0 { 6703 frag := headerBlock 6704 if len(frag) > maxFrameSize { 6705 frag = frag[:maxFrameSize] 6706 } 6707 headerBlock = headerBlock[len(frag):] 6708 endHeaders := len(headerBlock) == 0 6709 var err error 6710 if first { 6711 first = false 6712 err = ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 6713 StreamID: w.streamID, 6714 BlockFragment: frag, 6715 EndStream: w.endStream, 6716 EndHeaders: endHeaders, 6717 }) 6718 } else { 6719 err = ctx.Framer().WriteContinuation(w.streamID, endHeaders, frag) 6720 } 6721 if err != nil { 6722 return err 6723 } 6724 } 6725 return nil 6726 } 6727 6728 type http2write100ContinueHeadersFrame struct { 6729 streamID uint32 6730 } 6731 6732 func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error { 6733 enc, buf := ctx.HeaderEncoder() 6734 buf.Reset() 6735 http2encKV(enc, ":status", "100") 6736 return ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 6737 StreamID: w.streamID, 6738 BlockFragment: buf.Bytes(), 6739 EndStream: false, 6740 EndHeaders: true, 6741 }) 6742 } 6743 6744 type http2writeWindowUpdate struct { 6745 streamID uint32 // or 0 for conn-level 6746 n uint32 6747 } 6748 6749 func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error { 6750 return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n) 6751 } 6752 6753 func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) { 6754 if keys == nil { 6755 sorter := http2sorterPool.Get().(*http2sorter) 6756 6757 defer http2sorterPool.Put(sorter) 6758 keys = sorter.Keys(h) 6759 } 6760 for _, k := range keys { 6761 vv := h[k] 6762 k = http2lowerHeader(k) 6763 if !http2validWireHeaderFieldName(k) { 6764 6765 continue 6766 } 6767 isTE := k == "transfer-encoding" 6768 for _, v := range vv { 6769 if !httplex.ValidHeaderFieldValue(v) { 6770 6771 continue 6772 } 6773 6774 if isTE && v != "trailers" { 6775 continue 6776 } 6777 http2encKV(enc, k, v) 6778 } 6779 } 6780 } 6781 6782 // frameWriteMsg is a request to write a frame. 6783 type http2frameWriteMsg struct { 6784 // write is the interface value that does the writing, once the 6785 // writeScheduler (below) has decided to select this frame 6786 // to write. The write functions are all defined in write.go. 6787 write http2writeFramer 6788 6789 stream *http2stream // used for prioritization. nil for non-stream frames. 6790 6791 // done, if non-nil, must be a buffered channel with space for 6792 // 1 message and is sent the return value from write (or an 6793 // earlier error) when the frame has been written. 6794 done chan error 6795 } 6796 6797 // for debugging only: 6798 func (wm http2frameWriteMsg) String() string { 6799 var streamID uint32 6800 if wm.stream != nil { 6801 streamID = wm.stream.id 6802 } 6803 var des string 6804 if s, ok := wm.write.(fmt.Stringer); ok { 6805 des = s.String() 6806 } else { 6807 des = fmt.Sprintf("%T", wm.write) 6808 } 6809 return fmt.Sprintf("[frameWriteMsg stream=%d, ch=%v, type: %v]", streamID, wm.done != nil, des) 6810 } 6811 6812 // writeScheduler tracks pending frames to write, priorities, and decides 6813 // the next one to use. It is not thread-safe. 6814 type http2writeScheduler struct { 6815 // zero are frames not associated with a specific stream. 6816 // They're sent before any stream-specific freams. 6817 zero http2writeQueue 6818 6819 // maxFrameSize is the maximum size of a DATA frame 6820 // we'll write. Must be non-zero and between 16K-16M. 6821 maxFrameSize uint32 6822 6823 // sq contains the stream-specific queues, keyed by stream ID. 6824 // when a stream is idle, it's deleted from the map. 6825 sq map[uint32]*http2writeQueue 6826 6827 // canSend is a slice of memory that's reused between frame 6828 // scheduling decisions to hold the list of writeQueues (from sq) 6829 // which have enough flow control data to send. After canSend is 6830 // built, the best is selected. 6831 canSend []*http2writeQueue 6832 6833 // pool of empty queues for reuse. 6834 queuePool []*http2writeQueue 6835 } 6836 6837 func (ws *http2writeScheduler) putEmptyQueue(q *http2writeQueue) { 6838 if len(q.s) != 0 { 6839 panic("queue must be empty") 6840 } 6841 ws.queuePool = append(ws.queuePool, q) 6842 } 6843 6844 func (ws *http2writeScheduler) getEmptyQueue() *http2writeQueue { 6845 ln := len(ws.queuePool) 6846 if ln == 0 { 6847 return new(http2writeQueue) 6848 } 6849 q := ws.queuePool[ln-1] 6850 ws.queuePool = ws.queuePool[:ln-1] 6851 return q 6852 } 6853 6854 func (ws *http2writeScheduler) empty() bool { return ws.zero.empty() && len(ws.sq) == 0 } 6855 6856 func (ws *http2writeScheduler) add(wm http2frameWriteMsg) { 6857 st := wm.stream 6858 if st == nil { 6859 ws.zero.push(wm) 6860 } else { 6861 ws.streamQueue(st.id).push(wm) 6862 } 6863 } 6864 6865 func (ws *http2writeScheduler) streamQueue(streamID uint32) *http2writeQueue { 6866 if q, ok := ws.sq[streamID]; ok { 6867 return q 6868 } 6869 if ws.sq == nil { 6870 ws.sq = make(map[uint32]*http2writeQueue) 6871 } 6872 q := ws.getEmptyQueue() 6873 ws.sq[streamID] = q 6874 return q 6875 } 6876 6877 // take returns the most important frame to write and removes it from the scheduler. 6878 // It is illegal to call this if the scheduler is empty or if there are no connection-level 6879 // flow control bytes available. 6880 func (ws *http2writeScheduler) take() (wm http2frameWriteMsg, ok bool) { 6881 if ws.maxFrameSize == 0 { 6882 panic("internal error: ws.maxFrameSize not initialized or invalid") 6883 } 6884 6885 if !ws.zero.empty() { 6886 return ws.zero.shift(), true 6887 } 6888 if len(ws.sq) == 0 { 6889 return 6890 } 6891 6892 for id, q := range ws.sq { 6893 if q.firstIsNoCost() { 6894 return ws.takeFrom(id, q) 6895 } 6896 } 6897 6898 if len(ws.canSend) != 0 { 6899 panic("should be empty") 6900 } 6901 for _, q := range ws.sq { 6902 if n := ws.streamWritableBytes(q); n > 0 { 6903 ws.canSend = append(ws.canSend, q) 6904 } 6905 } 6906 if len(ws.canSend) == 0 { 6907 return 6908 } 6909 defer ws.zeroCanSend() 6910 6911 q := ws.canSend[0] 6912 6913 return ws.takeFrom(q.streamID(), q) 6914 } 6915 6916 // zeroCanSend is defered from take. 6917 func (ws *http2writeScheduler) zeroCanSend() { 6918 for i := range ws.canSend { 6919 ws.canSend[i] = nil 6920 } 6921 ws.canSend = ws.canSend[:0] 6922 } 6923 6924 // streamWritableBytes returns the number of DATA bytes we could write 6925 // from the given queue's stream, if this stream/queue were 6926 // selected. It is an error to call this if q's head isn't a 6927 // *writeData. 6928 func (ws *http2writeScheduler) streamWritableBytes(q *http2writeQueue) int32 { 6929 wm := q.head() 6930 ret := wm.stream.flow.available() 6931 if ret == 0 { 6932 return 0 6933 } 6934 if int32(ws.maxFrameSize) < ret { 6935 ret = int32(ws.maxFrameSize) 6936 } 6937 if ret == 0 { 6938 panic("internal error: ws.maxFrameSize not initialized or invalid") 6939 } 6940 wd := wm.write.(*http2writeData) 6941 if len(wd.p) < int(ret) { 6942 ret = int32(len(wd.p)) 6943 } 6944 return ret 6945 } 6946 6947 func (ws *http2writeScheduler) takeFrom(id uint32, q *http2writeQueue) (wm http2frameWriteMsg, ok bool) { 6948 wm = q.head() 6949 6950 if wd, ok := wm.write.(*http2writeData); ok && len(wd.p) > 0 { 6951 allowed := wm.stream.flow.available() 6952 if allowed == 0 { 6953 6954 return http2frameWriteMsg{}, false 6955 } 6956 if int32(ws.maxFrameSize) < allowed { 6957 allowed = int32(ws.maxFrameSize) 6958 } 6959 6960 if len(wd.p) > int(allowed) { 6961 wm.stream.flow.take(allowed) 6962 chunk := wd.p[:allowed] 6963 wd.p = wd.p[allowed:] 6964 6965 return http2frameWriteMsg{ 6966 stream: wm.stream, 6967 write: &http2writeData{ 6968 streamID: wd.streamID, 6969 p: chunk, 6970 6971 endStream: false, 6972 }, 6973 6974 done: nil, 6975 }, true 6976 } 6977 wm.stream.flow.take(int32(len(wd.p))) 6978 } 6979 6980 q.shift() 6981 if q.empty() { 6982 ws.putEmptyQueue(q) 6983 delete(ws.sq, id) 6984 } 6985 return wm, true 6986 } 6987 6988 func (ws *http2writeScheduler) forgetStream(id uint32) { 6989 q, ok := ws.sq[id] 6990 if !ok { 6991 return 6992 } 6993 delete(ws.sq, id) 6994 6995 for i := range q.s { 6996 q.s[i] = http2frameWriteMsg{} 6997 } 6998 q.s = q.s[:0] 6999 ws.putEmptyQueue(q) 7000 } 7001 7002 type http2writeQueue struct { 7003 s []http2frameWriteMsg 7004 } 7005 7006 // streamID returns the stream ID for a non-empty stream-specific queue. 7007 func (q *http2writeQueue) streamID() uint32 { return q.s[0].stream.id } 7008 7009 func (q *http2writeQueue) empty() bool { return len(q.s) == 0 } 7010 7011 func (q *http2writeQueue) push(wm http2frameWriteMsg) { 7012 q.s = append(q.s, wm) 7013 } 7014 7015 // head returns the next item that would be removed by shift. 7016 func (q *http2writeQueue) head() http2frameWriteMsg { 7017 if len(q.s) == 0 { 7018 panic("invalid use of queue") 7019 } 7020 return q.s[0] 7021 } 7022 7023 func (q *http2writeQueue) shift() http2frameWriteMsg { 7024 if len(q.s) == 0 { 7025 panic("invalid use of queue") 7026 } 7027 wm := q.s[0] 7028 7029 copy(q.s, q.s[1:]) 7030 q.s[len(q.s)-1] = http2frameWriteMsg{} 7031 q.s = q.s[:len(q.s)-1] 7032 return wm 7033 } 7034 7035 func (q *http2writeQueue) firstIsNoCost() bool { 7036 if df, ok := q.s[0].write.(*http2writeData); ok { 7037 return len(df.p) == 0 7038 } 7039 return true 7040 }