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