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