github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/net/http/h2_bundle.go (about) 1 // This file is autogenerated using x/tools/cmd/bundle from 2 // https://go-review.googlesource.com/#/c/15850/ 3 // Usage: 4 // $ bundle golang.org/x/net/http2 net/http http2 > /tmp/x.go; mv /tmp/x.go $GOROOT/src/net/http/h2_bundle.go 5 6 // Copyright 2015 The Go Authors. All rights reserved. 7 // Use of this source code is governed by a BSD-style 8 // license that can be found in the LICENSE file. 9 10 package http 11 12 import ( 13 "bufio" 14 "bytes" 15 "crypto/tls" 16 "encoding/binary" 17 "errors" 18 "fmt" 19 "io" 20 "log" 21 "net" 22 "net/url" 23 "os" 24 "runtime" 25 "strconv" 26 "strings" 27 "sync" 28 "time" 29 30 "golang.org/x/net/http2/hpack" 31 ) 32 33 // buffer is an io.ReadWriteCloser backed by a fixed size buffer. 34 // It never allocates, but moves old data as new data is written. 35 type http2buffer struct { 36 buf []byte 37 r, w int 38 closed bool 39 err error // err to return to reader 40 } 41 42 var ( 43 http2errReadEmpty = errors.New("read from empty buffer") 44 http2errWriteClosed = errors.New("write on closed buffer") 45 http2errWriteFull = errors.New("write on full buffer") 46 ) 47 48 // Read copies bytes from the buffer into p. 49 // It is an error to read when no data is available. 50 func (b *http2buffer) Read(p []byte) (n int, err error) { 51 n = copy(p, b.buf[b.r:b.w]) 52 b.r += n 53 if b.closed && b.r == b.w { 54 err = b.err 55 } else if b.r == b.w && n == 0 { 56 err = http2errReadEmpty 57 } 58 return n, err 59 } 60 61 // Len returns the number of bytes of the unread portion of the buffer. 62 func (b *http2buffer) Len() int { 63 return b.w - b.r 64 } 65 66 // Write copies bytes from p into the buffer. 67 // It is an error to write more data than the buffer can hold. 68 func (b *http2buffer) Write(p []byte) (n int, err error) { 69 if b.closed { 70 return 0, http2errWriteClosed 71 } 72 73 if b.r > 0 && len(p) > len(b.buf)-b.w { 74 copy(b.buf, b.buf[b.r:b.w]) 75 b.w -= b.r 76 b.r = 0 77 } 78 79 n = copy(b.buf[b.w:], p) 80 b.w += n 81 if n < len(p) { 82 err = http2errWriteFull 83 } 84 return n, err 85 } 86 87 // Close marks the buffer as closed. Future calls to Write will 88 // return an error. Future calls to Read, once the buffer is 89 // empty, will return err. 90 func (b *http2buffer) Close(err error) { 91 if !b.closed { 92 b.closed = true 93 b.err = err 94 } 95 } 96 97 // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec. 98 type http2ErrCode uint32 99 100 const ( 101 http2ErrCodeNo http2ErrCode = 0x0 102 http2ErrCodeProtocol http2ErrCode = 0x1 103 http2ErrCodeInternal http2ErrCode = 0x2 104 http2ErrCodeFlowControl http2ErrCode = 0x3 105 http2ErrCodeSettingsTimeout http2ErrCode = 0x4 106 http2ErrCodeStreamClosed http2ErrCode = 0x5 107 http2ErrCodeFrameSize http2ErrCode = 0x6 108 http2ErrCodeRefusedStream http2ErrCode = 0x7 109 http2ErrCodeCancel http2ErrCode = 0x8 110 http2ErrCodeCompression http2ErrCode = 0x9 111 http2ErrCodeConnect http2ErrCode = 0xa 112 http2ErrCodeEnhanceYourCalm http2ErrCode = 0xb 113 http2ErrCodeInadequateSecurity http2ErrCode = 0xc 114 http2ErrCodeHTTP11Required http2ErrCode = 0xd 115 ) 116 117 var http2errCodeName = map[http2ErrCode]string{ 118 http2ErrCodeNo: "NO_ERROR", 119 http2ErrCodeProtocol: "PROTOCOL_ERROR", 120 http2ErrCodeInternal: "INTERNAL_ERROR", 121 http2ErrCodeFlowControl: "FLOW_CONTROL_ERROR", 122 http2ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT", 123 http2ErrCodeStreamClosed: "STREAM_CLOSED", 124 http2ErrCodeFrameSize: "FRAME_SIZE_ERROR", 125 http2ErrCodeRefusedStream: "REFUSED_STREAM", 126 http2ErrCodeCancel: "CANCEL", 127 http2ErrCodeCompression: "COMPRESSION_ERROR", 128 http2ErrCodeConnect: "CONNECT_ERROR", 129 http2ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM", 130 http2ErrCodeInadequateSecurity: "INADEQUATE_SECURITY", 131 http2ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED", 132 } 133 134 func (e http2ErrCode) String() string { 135 if s, ok := http2errCodeName[e]; ok { 136 return s 137 } 138 return fmt.Sprintf("unknown error code 0x%x", uint32(e)) 139 } 140 141 // ConnectionError is an error that results in the termination of the 142 // entire connection. 143 type http2ConnectionError http2ErrCode 144 145 func (e http2ConnectionError) Error() string { 146 return fmt.Sprintf("connection error: %s", http2ErrCode(e)) 147 } 148 149 // StreamError is an error that only affects one stream within an 150 // HTTP/2 connection. 151 type http2StreamError struct { 152 StreamID uint32 153 Code http2ErrCode 154 } 155 156 func (e http2StreamError) Error() string { 157 return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code) 158 } 159 160 // 6.9.1 The Flow Control Window 161 // "If a sender receives a WINDOW_UPDATE that causes a flow control 162 // window to exceed this maximum it MUST terminate either the stream 163 // or the connection, as appropriate. For streams, [...]; for the 164 // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code." 165 type http2goAwayFlowError struct{} 166 167 func (http2goAwayFlowError) Error() string { return "connection exceeded flow control window size" } 168 169 // flow is the flow control window's size. 170 type http2flow struct { 171 // n is the number of DATA bytes we're allowed to send. 172 // A flow is kept both on a conn and a per-stream. 173 n int32 174 175 // conn points to the shared connection-level flow that is 176 // shared by all streams on that conn. It is nil for the flow 177 // that's on the conn directly. 178 conn *http2flow 179 } 180 181 func (f *http2flow) setConnFlow(cf *http2flow) { f.conn = cf } 182 183 func (f *http2flow) available() int32 { 184 n := f.n 185 if f.conn != nil && f.conn.n < n { 186 n = f.conn.n 187 } 188 return n 189 } 190 191 func (f *http2flow) take(n int32) { 192 if n > f.available() { 193 panic("internal error: took too much") 194 } 195 f.n -= n 196 if f.conn != nil { 197 f.conn.n -= n 198 } 199 } 200 201 // add adds n bytes (positive or negative) to the flow control window. 202 // It returns false if the sum would exceed 2^31-1. 203 func (f *http2flow) add(n int32) bool { 204 remain := (1<<31 - 1) - f.n 205 if n > remain { 206 return false 207 } 208 f.n += n 209 return true 210 } 211 212 const http2frameHeaderLen = 9 213 214 var http2padZeros = make([]byte, 255) // zeros for padding 215 216 // A FrameType is a registered frame type as defined in 217 // http://http2.github.io/http2-spec/#rfc.section.11.2 218 type http2FrameType uint8 219 220 const ( 221 http2FrameData http2FrameType = 0x0 222 http2FrameHeaders http2FrameType = 0x1 223 http2FramePriority http2FrameType = 0x2 224 http2FrameRSTStream http2FrameType = 0x3 225 http2FrameSettings http2FrameType = 0x4 226 http2FramePushPromise http2FrameType = 0x5 227 http2FramePing http2FrameType = 0x6 228 http2FrameGoAway http2FrameType = 0x7 229 http2FrameWindowUpdate http2FrameType = 0x8 230 http2FrameContinuation http2FrameType = 0x9 231 ) 232 233 var http2frameName = map[http2FrameType]string{ 234 http2FrameData: "DATA", 235 http2FrameHeaders: "HEADERS", 236 http2FramePriority: "PRIORITY", 237 http2FrameRSTStream: "RST_STREAM", 238 http2FrameSettings: "SETTINGS", 239 http2FramePushPromise: "PUSH_PROMISE", 240 http2FramePing: "PING", 241 http2FrameGoAway: "GOAWAY", 242 http2FrameWindowUpdate: "WINDOW_UPDATE", 243 http2FrameContinuation: "CONTINUATION", 244 } 245 246 func (t http2FrameType) String() string { 247 if s, ok := http2frameName[t]; ok { 248 return s 249 } 250 return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t)) 251 } 252 253 // Flags is a bitmask of HTTP/2 flags. 254 // The meaning of flags varies depending on the frame type. 255 type http2Flags uint8 256 257 // Has reports whether f contains all (0 or more) flags in v. 258 func (f http2Flags) Has(v http2Flags) bool { 259 return (f & v) == v 260 } 261 262 // Frame-specific FrameHeader flag bits. 263 const ( 264 // Data Frame 265 http2FlagDataEndStream http2Flags = 0x1 266 http2FlagDataPadded http2Flags = 0x8 267 268 // Headers Frame 269 http2FlagHeadersEndStream http2Flags = 0x1 270 http2FlagHeadersEndHeaders http2Flags = 0x4 271 http2FlagHeadersPadded http2Flags = 0x8 272 http2FlagHeadersPriority http2Flags = 0x20 273 274 // Settings Frame 275 http2FlagSettingsAck http2Flags = 0x1 276 277 // Ping Frame 278 http2FlagPingAck http2Flags = 0x1 279 280 // Continuation Frame 281 http2FlagContinuationEndHeaders http2Flags = 0x4 282 283 http2FlagPushPromiseEndHeaders http2Flags = 0x4 284 http2FlagPushPromisePadded http2Flags = 0x8 285 ) 286 287 var http2flagName = map[http2FrameType]map[http2Flags]string{ 288 http2FrameData: { 289 http2FlagDataEndStream: "END_STREAM", 290 http2FlagDataPadded: "PADDED", 291 }, 292 http2FrameHeaders: { 293 http2FlagHeadersEndStream: "END_STREAM", 294 http2FlagHeadersEndHeaders: "END_HEADERS", 295 http2FlagHeadersPadded: "PADDED", 296 http2FlagHeadersPriority: "PRIORITY", 297 }, 298 http2FrameSettings: { 299 http2FlagSettingsAck: "ACK", 300 }, 301 http2FramePing: { 302 http2FlagPingAck: "ACK", 303 }, 304 http2FrameContinuation: { 305 http2FlagContinuationEndHeaders: "END_HEADERS", 306 }, 307 http2FramePushPromise: { 308 http2FlagPushPromiseEndHeaders: "END_HEADERS", 309 http2FlagPushPromisePadded: "PADDED", 310 }, 311 } 312 313 // a frameParser parses a frame given its FrameHeader and payload 314 // bytes. The length of payload will always equal fh.Length (which 315 // might be 0). 316 type http2frameParser func(fh http2FrameHeader, payload []byte) (http2Frame, error) 317 318 var http2frameParsers = map[http2FrameType]http2frameParser{ 319 http2FrameData: http2parseDataFrame, 320 http2FrameHeaders: http2parseHeadersFrame, 321 http2FramePriority: http2parsePriorityFrame, 322 http2FrameRSTStream: http2parseRSTStreamFrame, 323 http2FrameSettings: http2parseSettingsFrame, 324 http2FramePushPromise: http2parsePushPromise, 325 http2FramePing: http2parsePingFrame, 326 http2FrameGoAway: http2parseGoAwayFrame, 327 http2FrameWindowUpdate: http2parseWindowUpdateFrame, 328 http2FrameContinuation: http2parseContinuationFrame, 329 } 330 331 func http2typeFrameParser(t http2FrameType) http2frameParser { 332 if f := http2frameParsers[t]; f != nil { 333 return f 334 } 335 return http2parseUnknownFrame 336 } 337 338 // A FrameHeader is the 9 byte header of all HTTP/2 frames. 339 // 340 // See http://http2.github.io/http2-spec/#FrameHeader 341 type http2FrameHeader struct { 342 valid bool // caller can access []byte fields in the Frame 343 344 // Type is the 1 byte frame type. There are ten standard frame 345 // types, but extension frame types may be written by WriteRawFrame 346 // and will be returned by ReadFrame (as UnknownFrame). 347 Type http2FrameType 348 349 // Flags are the 1 byte of 8 potential bit flags per frame. 350 // They are specific to the frame type. 351 Flags http2Flags 352 353 // Length is the length of the frame, not including the 9 byte header. 354 // The maximum size is one byte less than 16MB (uint24), but only 355 // frames up to 16KB are allowed without peer agreement. 356 Length uint32 357 358 // StreamID is which stream this frame is for. Certain frames 359 // are not stream-specific, in which case this field is 0. 360 StreamID uint32 361 } 362 363 // Header returns h. It exists so FrameHeaders can be embedded in other 364 // specific frame types and implement the Frame interface. 365 func (h http2FrameHeader) Header() http2FrameHeader { return h } 366 367 func (h http2FrameHeader) String() string { 368 var buf bytes.Buffer 369 buf.WriteString("[FrameHeader ") 370 buf.WriteString(h.Type.String()) 371 if h.Flags != 0 { 372 buf.WriteString(" flags=") 373 set := 0 374 for i := uint8(0); i < 8; i++ { 375 if h.Flags&(1<<i) == 0 { 376 continue 377 } 378 set++ 379 if set > 1 { 380 buf.WriteByte('|') 381 } 382 name := http2flagName[h.Type][http2Flags(1<<i)] 383 if name != "" { 384 buf.WriteString(name) 385 } else { 386 fmt.Fprintf(&buf, "0x%x", 1<<i) 387 } 388 } 389 } 390 if h.StreamID != 0 { 391 fmt.Fprintf(&buf, " stream=%d", h.StreamID) 392 } 393 fmt.Fprintf(&buf, " len=%d]", h.Length) 394 return buf.String() 395 } 396 397 func (h *http2FrameHeader) checkValid() { 398 if !h.valid { 399 panic("Frame accessor called on non-owned Frame") 400 } 401 } 402 403 func (h *http2FrameHeader) invalidate() { h.valid = false } 404 405 // frame header bytes. 406 // Used only by ReadFrameHeader. 407 var http2fhBytes = sync.Pool{ 408 New: func() interface{} { 409 buf := make([]byte, http2frameHeaderLen) 410 return &buf 411 }, 412 } 413 414 // ReadFrameHeader reads 9 bytes from r and returns a FrameHeader. 415 // Most users should use Framer.ReadFrame instead. 416 func http2ReadFrameHeader(r io.Reader) (http2FrameHeader, error) { 417 bufp := http2fhBytes.Get().(*[]byte) 418 defer http2fhBytes.Put(bufp) 419 return http2readFrameHeader(*bufp, r) 420 } 421 422 func http2readFrameHeader(buf []byte, r io.Reader) (http2FrameHeader, error) { 423 _, err := io.ReadFull(r, buf[:http2frameHeaderLen]) 424 if err != nil { 425 return http2FrameHeader{}, err 426 } 427 return http2FrameHeader{ 428 Length: (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])), 429 Type: http2FrameType(buf[3]), 430 Flags: http2Flags(buf[4]), 431 StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1), 432 valid: true, 433 }, nil 434 } 435 436 // A Frame is the base interface implemented by all frame types. 437 // Callers will generally type-assert the specific frame type: 438 // *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc. 439 // 440 // Frames are only valid until the next call to Framer.ReadFrame. 441 type http2Frame interface { 442 Header() http2FrameHeader 443 444 // invalidate is called by Framer.ReadFrame to make this 445 // frame's buffers as being invalid, since the subsequent 446 // frame will reuse them. 447 invalidate() 448 } 449 450 // A Framer reads and writes Frames. 451 type http2Framer struct { 452 r io.Reader 453 lastFrame http2Frame 454 455 maxReadSize uint32 456 headerBuf [http2frameHeaderLen]byte 457 458 // TODO: let getReadBuf be configurable, and use a less memory-pinning 459 // allocator in server.go to minimize memory pinned for many idle conns. 460 // Will probably also need to make frame invalidation have a hook too. 461 getReadBuf func(size uint32) []byte 462 readBuf []byte // cache for default getReadBuf 463 464 maxWriteSize uint32 // zero means unlimited; TODO: implement 465 466 w io.Writer 467 wbuf []byte 468 469 // AllowIllegalWrites permits the Framer's Write methods to 470 // write frames that do not conform to the HTTP/2 spec. This 471 // permits using the Framer to test other HTTP/2 472 // implementations' conformance to the spec. 473 // If false, the Write methods will prefer to return an error 474 // rather than comply. 475 AllowIllegalWrites bool 476 } 477 478 func (f *http2Framer) startWrite(ftype http2FrameType, flags http2Flags, streamID uint32) { 479 480 f.wbuf = append(f.wbuf[:0], 481 0, 482 0, 483 0, 484 byte(ftype), 485 byte(flags), 486 byte(streamID>>24), 487 byte(streamID>>16), 488 byte(streamID>>8), 489 byte(streamID)) 490 } 491 492 func (f *http2Framer) endWrite() error { 493 494 length := len(f.wbuf) - http2frameHeaderLen 495 if length >= (1 << 24) { 496 return http2ErrFrameTooLarge 497 } 498 _ = append(f.wbuf[:0], 499 byte(length>>16), 500 byte(length>>8), 501 byte(length)) 502 n, err := f.w.Write(f.wbuf) 503 if err == nil && n != len(f.wbuf) { 504 err = io.ErrShortWrite 505 } 506 return err 507 } 508 509 func (f *http2Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) } 510 511 func (f *http2Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) } 512 513 func (f *http2Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) } 514 515 func (f *http2Framer) writeUint32(v uint32) { 516 f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 517 } 518 519 const ( 520 http2minMaxFrameSize = 1 << 14 521 http2maxFrameSize = 1<<24 - 1 522 ) 523 524 // NewFramer returns a Framer that writes frames to w and reads them from r. 525 func http2NewFramer(w io.Writer, r io.Reader) *http2Framer { 526 fr := &http2Framer{ 527 w: w, 528 r: r, 529 } 530 fr.getReadBuf = func(size uint32) []byte { 531 if cap(fr.readBuf) >= int(size) { 532 return fr.readBuf[:size] 533 } 534 fr.readBuf = make([]byte, size) 535 return fr.readBuf 536 } 537 fr.SetMaxReadFrameSize(http2maxFrameSize) 538 return fr 539 } 540 541 // SetMaxReadFrameSize sets the maximum size of a frame 542 // that will be read by a subsequent call to ReadFrame. 543 // It is the caller's responsibility to advertise this 544 // limit with a SETTINGS frame. 545 func (fr *http2Framer) SetMaxReadFrameSize(v uint32) { 546 if v > http2maxFrameSize { 547 v = http2maxFrameSize 548 } 549 fr.maxReadSize = v 550 } 551 552 // ErrFrameTooLarge is returned from Framer.ReadFrame when the peer 553 // sends a frame that is larger than declared with SetMaxReadFrameSize. 554 var http2ErrFrameTooLarge = errors.New("http2: frame too large") 555 556 // ReadFrame reads a single frame. The returned Frame is only valid 557 // until the next call to ReadFrame. 558 // If the frame is larger than previously set with SetMaxReadFrameSize, 559 // the returned error is ErrFrameTooLarge. 560 func (fr *http2Framer) ReadFrame() (http2Frame, error) { 561 if fr.lastFrame != nil { 562 fr.lastFrame.invalidate() 563 } 564 fh, err := http2readFrameHeader(fr.headerBuf[:], fr.r) 565 if err != nil { 566 return nil, err 567 } 568 if fh.Length > fr.maxReadSize { 569 return nil, http2ErrFrameTooLarge 570 } 571 payload := fr.getReadBuf(fh.Length) 572 if _, err := io.ReadFull(fr.r, payload); err != nil { 573 return nil, err 574 } 575 f, err := http2typeFrameParser(fh.Type)(fh, payload) 576 if err != nil { 577 return nil, err 578 } 579 fr.lastFrame = f 580 return f, nil 581 } 582 583 // A DataFrame conveys arbitrary, variable-length sequences of octets 584 // associated with a stream. 585 // See http://http2.github.io/http2-spec/#rfc.section.6.1 586 type http2DataFrame struct { 587 http2FrameHeader 588 data []byte 589 } 590 591 func (f *http2DataFrame) StreamEnded() bool { 592 return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream) 593 } 594 595 // Data returns the frame's data octets, not including any padding 596 // size byte or padding suffix bytes. 597 // The caller must not retain the returned memory past the next 598 // call to ReadFrame. 599 func (f *http2DataFrame) Data() []byte { 600 f.checkValid() 601 return f.data 602 } 603 604 func http2parseDataFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 605 if fh.StreamID == 0 { 606 607 return nil, http2ConnectionError(http2ErrCodeProtocol) 608 } 609 f := &http2DataFrame{ 610 http2FrameHeader: fh, 611 } 612 var padSize byte 613 if fh.Flags.Has(http2FlagDataPadded) { 614 var err error 615 payload, padSize, err = http2readByte(payload) 616 if err != nil { 617 return nil, err 618 } 619 } 620 if int(padSize) > len(payload) { 621 622 return nil, http2ConnectionError(http2ErrCodeProtocol) 623 } 624 f.data = payload[:len(payload)-int(padSize)] 625 return f, nil 626 } 627 628 var http2errStreamID = errors.New("invalid streamid") 629 630 func http2validStreamID(streamID uint32) bool { 631 return streamID != 0 && streamID&(1<<31) == 0 632 } 633 634 // WriteData writes a DATA frame. 635 // 636 // It will perform exactly one Write to the underlying Writer. 637 // It is the caller's responsibility to not call other Write methods concurrently. 638 func (f *http2Framer) WriteData(streamID uint32, endStream bool, data []byte) error { 639 640 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 641 return http2errStreamID 642 } 643 var flags http2Flags 644 if endStream { 645 flags |= http2FlagDataEndStream 646 } 647 f.startWrite(http2FrameData, flags, streamID) 648 f.wbuf = append(f.wbuf, data...) 649 return f.endWrite() 650 } 651 652 // A SettingsFrame conveys configuration parameters that affect how 653 // endpoints communicate, such as preferences and constraints on peer 654 // behavior. 655 // 656 // See http://http2.github.io/http2-spec/#SETTINGS 657 type http2SettingsFrame struct { 658 http2FrameHeader 659 p []byte 660 } 661 662 func http2parseSettingsFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 663 if fh.Flags.Has(http2FlagSettingsAck) && fh.Length > 0 { 664 665 return nil, http2ConnectionError(http2ErrCodeFrameSize) 666 } 667 if fh.StreamID != 0 { 668 669 return nil, http2ConnectionError(http2ErrCodeProtocol) 670 } 671 if len(p)%6 != 0 { 672 673 return nil, http2ConnectionError(http2ErrCodeFrameSize) 674 } 675 f := &http2SettingsFrame{http2FrameHeader: fh, p: p} 676 if v, ok := f.Value(http2SettingInitialWindowSize); ok && v > (1<<31)-1 { 677 678 return nil, http2ConnectionError(http2ErrCodeFlowControl) 679 } 680 return f, nil 681 } 682 683 func (f *http2SettingsFrame) IsAck() bool { 684 return f.http2FrameHeader.Flags.Has(http2FlagSettingsAck) 685 } 686 687 func (f *http2SettingsFrame) Value(s http2SettingID) (v uint32, ok bool) { 688 f.checkValid() 689 buf := f.p 690 for len(buf) > 0 { 691 settingID := http2SettingID(binary.BigEndian.Uint16(buf[:2])) 692 if settingID == s { 693 return binary.BigEndian.Uint32(buf[2:6]), true 694 } 695 buf = buf[6:] 696 } 697 return 0, false 698 } 699 700 // ForeachSetting runs fn for each setting. 701 // It stops and returns the first error. 702 func (f *http2SettingsFrame) ForeachSetting(fn func(http2Setting) error) error { 703 f.checkValid() 704 buf := f.p 705 for len(buf) > 0 { 706 if err := fn(http2Setting{ 707 http2SettingID(binary.BigEndian.Uint16(buf[:2])), 708 binary.BigEndian.Uint32(buf[2:6]), 709 }); err != nil { 710 return err 711 } 712 buf = buf[6:] 713 } 714 return nil 715 } 716 717 // WriteSettings writes a SETTINGS frame with zero or more settings 718 // specified and the ACK bit not set. 719 // 720 // It will perform exactly one Write to the underlying Writer. 721 // It is the caller's responsibility to not call other Write methods concurrently. 722 func (f *http2Framer) WriteSettings(settings ...http2Setting) error { 723 f.startWrite(http2FrameSettings, 0, 0) 724 for _, s := range settings { 725 f.writeUint16(uint16(s.ID)) 726 f.writeUint32(s.Val) 727 } 728 return f.endWrite() 729 } 730 731 // WriteSettings writes an empty SETTINGS frame with the ACK bit set. 732 // 733 // It will perform exactly one Write to the underlying Writer. 734 // It is the caller's responsibility to not call other Write methods concurrently. 735 func (f *http2Framer) WriteSettingsAck() error { 736 f.startWrite(http2FrameSettings, http2FlagSettingsAck, 0) 737 return f.endWrite() 738 } 739 740 // A PingFrame is a mechanism for measuring a minimal round trip time 741 // from the sender, as well as determining whether an idle connection 742 // is still functional. 743 // See http://http2.github.io/http2-spec/#rfc.section.6.7 744 type http2PingFrame struct { 745 http2FrameHeader 746 Data [8]byte 747 } 748 749 func http2parsePingFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 750 if len(payload) != 8 { 751 return nil, http2ConnectionError(http2ErrCodeFrameSize) 752 } 753 if fh.StreamID != 0 { 754 return nil, http2ConnectionError(http2ErrCodeProtocol) 755 } 756 f := &http2PingFrame{http2FrameHeader: fh} 757 copy(f.Data[:], payload) 758 return f, nil 759 } 760 761 func (f *http2Framer) WritePing(ack bool, data [8]byte) error { 762 var flags http2Flags 763 if ack { 764 flags = http2FlagPingAck 765 } 766 f.startWrite(http2FramePing, flags, 0) 767 f.writeBytes(data[:]) 768 return f.endWrite() 769 } 770 771 // A GoAwayFrame informs the remote peer to stop creating streams on this connection. 772 // See http://http2.github.io/http2-spec/#rfc.section.6.8 773 type http2GoAwayFrame struct { 774 http2FrameHeader 775 LastStreamID uint32 776 ErrCode http2ErrCode 777 debugData []byte 778 } 779 780 // DebugData returns any debug data in the GOAWAY frame. Its contents 781 // are not defined. 782 // The caller must not retain the returned memory past the next 783 // call to ReadFrame. 784 func (f *http2GoAwayFrame) DebugData() []byte { 785 f.checkValid() 786 return f.debugData 787 } 788 789 func http2parseGoAwayFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 790 if fh.StreamID != 0 { 791 return nil, http2ConnectionError(http2ErrCodeProtocol) 792 } 793 if len(p) < 8 { 794 return nil, http2ConnectionError(http2ErrCodeFrameSize) 795 } 796 return &http2GoAwayFrame{ 797 http2FrameHeader: fh, 798 LastStreamID: binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1), 799 ErrCode: http2ErrCode(binary.BigEndian.Uint32(p[4:8])), 800 debugData: p[8:], 801 }, nil 802 } 803 804 func (f *http2Framer) WriteGoAway(maxStreamID uint32, code http2ErrCode, debugData []byte) error { 805 f.startWrite(http2FrameGoAway, 0, 0) 806 f.writeUint32(maxStreamID & (1<<31 - 1)) 807 f.writeUint32(uint32(code)) 808 f.writeBytes(debugData) 809 return f.endWrite() 810 } 811 812 // An UnknownFrame is the frame type returned when the frame type is unknown 813 // or no specific frame type parser exists. 814 type http2UnknownFrame struct { 815 http2FrameHeader 816 p []byte 817 } 818 819 // Payload returns the frame's payload (after the header). It is not 820 // valid to call this method after a subsequent call to 821 // Framer.ReadFrame, nor is it valid to retain the returned slice. 822 // The memory is owned by the Framer and is invalidated when the next 823 // frame is read. 824 func (f *http2UnknownFrame) Payload() []byte { 825 f.checkValid() 826 return f.p 827 } 828 829 func http2parseUnknownFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 830 return &http2UnknownFrame{fh, p}, nil 831 } 832 833 // A WindowUpdateFrame is used to implement flow control. 834 // See http://http2.github.io/http2-spec/#rfc.section.6.9 835 type http2WindowUpdateFrame struct { 836 http2FrameHeader 837 Increment uint32 838 } 839 840 func http2parseWindowUpdateFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 841 if len(p) != 4 { 842 return nil, http2ConnectionError(http2ErrCodeFrameSize) 843 } 844 inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff 845 if inc == 0 { 846 847 if fh.StreamID == 0 { 848 return nil, http2ConnectionError(http2ErrCodeProtocol) 849 } 850 return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol} 851 } 852 return &http2WindowUpdateFrame{ 853 http2FrameHeader: fh, 854 Increment: inc, 855 }, nil 856 } 857 858 // WriteWindowUpdate writes a WINDOW_UPDATE frame. 859 // The increment value must be between 1 and 2,147,483,647, inclusive. 860 // If the Stream ID is zero, the window update applies to the 861 // connection as a whole. 862 func (f *http2Framer) WriteWindowUpdate(streamID, incr uint32) error { 863 864 if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites { 865 return errors.New("illegal window increment value") 866 } 867 f.startWrite(http2FrameWindowUpdate, 0, streamID) 868 f.writeUint32(incr) 869 return f.endWrite() 870 } 871 872 // A HeadersFrame is used to open a stream and additionally carries a 873 // header block fragment. 874 type http2HeadersFrame struct { 875 http2FrameHeader 876 877 // Priority is set if FlagHeadersPriority is set in the FrameHeader. 878 Priority http2PriorityParam 879 880 headerFragBuf []byte // not owned 881 } 882 883 func (f *http2HeadersFrame) HeaderBlockFragment() []byte { 884 f.checkValid() 885 return f.headerFragBuf 886 } 887 888 func (f *http2HeadersFrame) HeadersEnded() bool { 889 return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndHeaders) 890 } 891 892 func (f *http2HeadersFrame) StreamEnded() bool { 893 return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndStream) 894 } 895 896 func (f *http2HeadersFrame) HasPriority() bool { 897 return f.http2FrameHeader.Flags.Has(http2FlagHeadersPriority) 898 } 899 900 func http2parseHeadersFrame(fh http2FrameHeader, p []byte) (_ http2Frame, err error) { 901 hf := &http2HeadersFrame{ 902 http2FrameHeader: fh, 903 } 904 if fh.StreamID == 0 { 905 906 return nil, http2ConnectionError(http2ErrCodeProtocol) 907 } 908 var padLength uint8 909 if fh.Flags.Has(http2FlagHeadersPadded) { 910 if p, padLength, err = http2readByte(p); err != nil { 911 return 912 } 913 } 914 if fh.Flags.Has(http2FlagHeadersPriority) { 915 var v uint32 916 p, v, err = http2readUint32(p) 917 if err != nil { 918 return nil, err 919 } 920 hf.Priority.StreamDep = v & 0x7fffffff 921 hf.Priority.Exclusive = (v != hf.Priority.StreamDep) 922 p, hf.Priority.Weight, err = http2readByte(p) 923 if err != nil { 924 return nil, err 925 } 926 } 927 if len(p)-int(padLength) <= 0 { 928 return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol} 929 } 930 hf.headerFragBuf = p[:len(p)-int(padLength)] 931 return hf, nil 932 } 933 934 // HeadersFrameParam are the parameters for writing a HEADERS frame. 935 type http2HeadersFrameParam struct { 936 // StreamID is the required Stream ID to initiate. 937 StreamID uint32 938 // BlockFragment is part (or all) of a Header Block. 939 BlockFragment []byte 940 941 // EndStream indicates that the header block is the last that 942 // the endpoint will send for the identified stream. Setting 943 // this flag causes the stream to enter one of "half closed" 944 // states. 945 EndStream bool 946 947 // EndHeaders indicates that this frame contains an entire 948 // header block and is not followed by any 949 // CONTINUATION frames. 950 EndHeaders bool 951 952 // PadLength is the optional number of bytes of zeros to add 953 // to this frame. 954 PadLength uint8 955 956 // Priority, if non-zero, includes stream priority information 957 // in the HEADER frame. 958 Priority http2PriorityParam 959 } 960 961 // WriteHeaders writes a single HEADERS frame. 962 // 963 // This is a low-level header writing method. Encoding headers and 964 // splitting them into any necessary CONTINUATION frames is handled 965 // elsewhere. 966 // 967 // It will perform exactly one Write to the underlying Writer. 968 // It is the caller's responsibility to not call other Write methods concurrently. 969 func (f *http2Framer) WriteHeaders(p http2HeadersFrameParam) error { 970 if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites { 971 return http2errStreamID 972 } 973 var flags http2Flags 974 if p.PadLength != 0 { 975 flags |= http2FlagHeadersPadded 976 } 977 if p.EndStream { 978 flags |= http2FlagHeadersEndStream 979 } 980 if p.EndHeaders { 981 flags |= http2FlagHeadersEndHeaders 982 } 983 if !p.Priority.IsZero() { 984 flags |= http2FlagHeadersPriority 985 } 986 f.startWrite(http2FrameHeaders, flags, p.StreamID) 987 if p.PadLength != 0 { 988 f.writeByte(p.PadLength) 989 } 990 if !p.Priority.IsZero() { 991 v := p.Priority.StreamDep 992 if !http2validStreamID(v) && !f.AllowIllegalWrites { 993 return errors.New("invalid dependent stream id") 994 } 995 if p.Priority.Exclusive { 996 v |= 1 << 31 997 } 998 f.writeUint32(v) 999 f.writeByte(p.Priority.Weight) 1000 } 1001 f.wbuf = append(f.wbuf, p.BlockFragment...) 1002 f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...) 1003 return f.endWrite() 1004 } 1005 1006 // A PriorityFrame specifies the sender-advised priority of a stream. 1007 // See http://http2.github.io/http2-spec/#rfc.section.6.3 1008 type http2PriorityFrame struct { 1009 http2FrameHeader 1010 http2PriorityParam 1011 } 1012 1013 // PriorityParam are the stream prioritzation parameters. 1014 type http2PriorityParam struct { 1015 // StreamDep is a 31-bit stream identifier for the 1016 // stream that this stream depends on. Zero means no 1017 // dependency. 1018 StreamDep uint32 1019 1020 // Exclusive is whether the dependency is exclusive. 1021 Exclusive bool 1022 1023 // Weight is the stream's zero-indexed weight. It should be 1024 // set together with StreamDep, or neither should be set. Per 1025 // the spec, "Add one to the value to obtain a weight between 1026 // 1 and 256." 1027 Weight uint8 1028 } 1029 1030 func (p http2PriorityParam) IsZero() bool { 1031 return p == http2PriorityParam{} 1032 } 1033 1034 func http2parsePriorityFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) { 1035 if fh.StreamID == 0 { 1036 return nil, http2ConnectionError(http2ErrCodeProtocol) 1037 } 1038 if len(payload) != 5 { 1039 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1040 } 1041 v := binary.BigEndian.Uint32(payload[:4]) 1042 streamID := v & 0x7fffffff 1043 return &http2PriorityFrame{ 1044 http2FrameHeader: fh, 1045 http2PriorityParam: http2PriorityParam{ 1046 Weight: payload[4], 1047 StreamDep: streamID, 1048 Exclusive: streamID != v, 1049 }, 1050 }, nil 1051 } 1052 1053 // WritePriority writes a PRIORITY frame. 1054 // 1055 // It will perform exactly one Write to the underlying Writer. 1056 // It is the caller's responsibility to not call other Write methods concurrently. 1057 func (f *http2Framer) WritePriority(streamID uint32, p http2PriorityParam) error { 1058 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1059 return http2errStreamID 1060 } 1061 f.startWrite(http2FramePriority, 0, streamID) 1062 v := p.StreamDep 1063 if p.Exclusive { 1064 v |= 1 << 31 1065 } 1066 f.writeUint32(v) 1067 f.writeByte(p.Weight) 1068 return f.endWrite() 1069 } 1070 1071 // A RSTStreamFrame allows for abnormal termination of a stream. 1072 // See http://http2.github.io/http2-spec/#rfc.section.6.4 1073 type http2RSTStreamFrame struct { 1074 http2FrameHeader 1075 ErrCode http2ErrCode 1076 } 1077 1078 func http2parseRSTStreamFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1079 if len(p) != 4 { 1080 return nil, http2ConnectionError(http2ErrCodeFrameSize) 1081 } 1082 if fh.StreamID == 0 { 1083 return nil, http2ConnectionError(http2ErrCodeProtocol) 1084 } 1085 return &http2RSTStreamFrame{fh, http2ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil 1086 } 1087 1088 // WriteRSTStream writes a RST_STREAM frame. 1089 // 1090 // It will perform exactly one Write to the underlying Writer. 1091 // It is the caller's responsibility to not call other Write methods concurrently. 1092 func (f *http2Framer) WriteRSTStream(streamID uint32, code http2ErrCode) error { 1093 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1094 return http2errStreamID 1095 } 1096 f.startWrite(http2FrameRSTStream, 0, streamID) 1097 f.writeUint32(uint32(code)) 1098 return f.endWrite() 1099 } 1100 1101 // A ContinuationFrame is used to continue a sequence of header block fragments. 1102 // See http://http2.github.io/http2-spec/#rfc.section.6.10 1103 type http2ContinuationFrame struct { 1104 http2FrameHeader 1105 headerFragBuf []byte 1106 } 1107 1108 func http2parseContinuationFrame(fh http2FrameHeader, p []byte) (http2Frame, error) { 1109 return &http2ContinuationFrame{fh, p}, nil 1110 } 1111 1112 func (f *http2ContinuationFrame) StreamEnded() bool { 1113 return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream) 1114 } 1115 1116 func (f *http2ContinuationFrame) HeaderBlockFragment() []byte { 1117 f.checkValid() 1118 return f.headerFragBuf 1119 } 1120 1121 func (f *http2ContinuationFrame) HeadersEnded() bool { 1122 return f.http2FrameHeader.Flags.Has(http2FlagContinuationEndHeaders) 1123 } 1124 1125 // WriteContinuation writes a CONTINUATION frame. 1126 // 1127 // It will perform exactly one Write to the underlying Writer. 1128 // It is the caller's responsibility to not call other Write methods concurrently. 1129 func (f *http2Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error { 1130 if !http2validStreamID(streamID) && !f.AllowIllegalWrites { 1131 return http2errStreamID 1132 } 1133 var flags http2Flags 1134 if endHeaders { 1135 flags |= http2FlagContinuationEndHeaders 1136 } 1137 f.startWrite(http2FrameContinuation, flags, streamID) 1138 f.wbuf = append(f.wbuf, headerBlockFragment...) 1139 return f.endWrite() 1140 } 1141 1142 // A PushPromiseFrame is used to initiate a server stream. 1143 // See http://http2.github.io/http2-spec/#rfc.section.6.6 1144 type http2PushPromiseFrame struct { 1145 http2FrameHeader 1146 PromiseID uint32 1147 headerFragBuf []byte // not owned 1148 } 1149 1150 func (f *http2PushPromiseFrame) HeaderBlockFragment() []byte { 1151 f.checkValid() 1152 return f.headerFragBuf 1153 } 1154 1155 func (f *http2PushPromiseFrame) HeadersEnded() bool { 1156 return f.http2FrameHeader.Flags.Has(http2FlagPushPromiseEndHeaders) 1157 } 1158 1159 func http2parsePushPromise(fh http2FrameHeader, p []byte) (_ http2Frame, err error) { 1160 pp := &http2PushPromiseFrame{ 1161 http2FrameHeader: fh, 1162 } 1163 if pp.StreamID == 0 { 1164 1165 return nil, http2ConnectionError(http2ErrCodeProtocol) 1166 } 1167 // The PUSH_PROMISE frame includes optional padding. 1168 // Padding fields and flags are identical to those defined for DATA frames 1169 var padLength uint8 1170 if fh.Flags.Has(http2FlagPushPromisePadded) { 1171 if p, padLength, err = http2readByte(p); err != nil { 1172 return 1173 } 1174 } 1175 1176 p, pp.PromiseID, err = http2readUint32(p) 1177 if err != nil { 1178 return 1179 } 1180 pp.PromiseID = pp.PromiseID & (1<<31 - 1) 1181 1182 if int(padLength) > len(p) { 1183 1184 return nil, http2ConnectionError(http2ErrCodeProtocol) 1185 } 1186 pp.headerFragBuf = p[:len(p)-int(padLength)] 1187 return pp, nil 1188 } 1189 1190 // PushPromiseParam are the parameters for writing a PUSH_PROMISE frame. 1191 type http2PushPromiseParam struct { 1192 // StreamID is the required Stream ID to initiate. 1193 StreamID uint32 1194 1195 // PromiseID is the required Stream ID which this 1196 // Push Promises 1197 PromiseID uint32 1198 1199 // BlockFragment is part (or all) of a Header Block. 1200 BlockFragment []byte 1201 1202 // EndHeaders indicates that this frame contains an entire 1203 // header block and is not followed by any 1204 // CONTINUATION frames. 1205 EndHeaders bool 1206 1207 // PadLength is the optional number of bytes of zeros to add 1208 // to this frame. 1209 PadLength uint8 1210 } 1211 1212 // WritePushPromise writes a single PushPromise Frame. 1213 // 1214 // As with Header Frames, This is the low level call for writing 1215 // individual frames. Continuation frames are handled elsewhere. 1216 // 1217 // It will perform exactly one Write to the underlying Writer. 1218 // It is the caller's responsibility to not call other Write methods concurrently. 1219 func (f *http2Framer) WritePushPromise(p http2PushPromiseParam) error { 1220 if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites { 1221 return http2errStreamID 1222 } 1223 var flags http2Flags 1224 if p.PadLength != 0 { 1225 flags |= http2FlagPushPromisePadded 1226 } 1227 if p.EndHeaders { 1228 flags |= http2FlagPushPromiseEndHeaders 1229 } 1230 f.startWrite(http2FramePushPromise, flags, p.StreamID) 1231 if p.PadLength != 0 { 1232 f.writeByte(p.PadLength) 1233 } 1234 if !http2validStreamID(p.PromiseID) && !f.AllowIllegalWrites { 1235 return http2errStreamID 1236 } 1237 f.writeUint32(p.PromiseID) 1238 f.wbuf = append(f.wbuf, p.BlockFragment...) 1239 f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...) 1240 return f.endWrite() 1241 } 1242 1243 // WriteRawFrame writes a raw frame. This can be used to write 1244 // extension frames unknown to this package. 1245 func (f *http2Framer) WriteRawFrame(t http2FrameType, flags http2Flags, streamID uint32, payload []byte) error { 1246 f.startWrite(t, flags, streamID) 1247 f.writeBytes(payload) 1248 return f.endWrite() 1249 } 1250 1251 func http2readByte(p []byte) (remain []byte, b byte, err error) { 1252 if len(p) == 0 { 1253 return nil, 0, io.ErrUnexpectedEOF 1254 } 1255 return p[1:], p[0], nil 1256 } 1257 1258 func http2readUint32(p []byte) (remain []byte, v uint32, err error) { 1259 if len(p) < 4 { 1260 return nil, 0, io.ErrUnexpectedEOF 1261 } 1262 return p[4:], binary.BigEndian.Uint32(p[:4]), nil 1263 } 1264 1265 type http2streamEnder interface { 1266 StreamEnded() bool 1267 } 1268 1269 type http2headersEnder interface { 1270 HeadersEnded() bool 1271 } 1272 1273 var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1" 1274 1275 type http2goroutineLock uint64 1276 1277 func http2newGoroutineLock() http2goroutineLock { 1278 if !http2DebugGoroutines { 1279 return 0 1280 } 1281 return http2goroutineLock(http2curGoroutineID()) 1282 } 1283 1284 func (g http2goroutineLock) check() { 1285 if !http2DebugGoroutines { 1286 return 1287 } 1288 if http2curGoroutineID() != uint64(g) { 1289 panic("running on the wrong goroutine") 1290 } 1291 } 1292 1293 func (g http2goroutineLock) checkNotOn() { 1294 if !http2DebugGoroutines { 1295 return 1296 } 1297 if http2curGoroutineID() == uint64(g) { 1298 panic("running on the wrong goroutine") 1299 } 1300 } 1301 1302 var http2goroutineSpace = []byte("goroutine ") 1303 1304 func http2curGoroutineID() uint64 { 1305 bp := http2littleBuf.Get().(*[]byte) 1306 defer http2littleBuf.Put(bp) 1307 b := *bp 1308 b = b[:runtime.Stack(b, false)] 1309 1310 b = bytes.TrimPrefix(b, http2goroutineSpace) 1311 i := bytes.IndexByte(b, ' ') 1312 if i < 0 { 1313 panic(fmt.Sprintf("No space found in %q", b)) 1314 } 1315 b = b[:i] 1316 n, err := http2parseUintBytes(b, 10, 64) 1317 if err != nil { 1318 panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err)) 1319 } 1320 return n 1321 } 1322 1323 var http2littleBuf = sync.Pool{ 1324 New: func() interface{} { 1325 buf := make([]byte, 64) 1326 return &buf 1327 }, 1328 } 1329 1330 // parseUintBytes is like strconv.ParseUint, but using a []byte. 1331 func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) { 1332 var cutoff, maxVal uint64 1333 1334 if bitSize == 0 { 1335 bitSize = int(strconv.IntSize) 1336 } 1337 1338 s0 := s 1339 switch { 1340 case len(s) < 1: 1341 err = strconv.ErrSyntax 1342 goto Error 1343 1344 case 2 <= base && base <= 36: 1345 1346 case base == 0: 1347 1348 switch { 1349 case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'): 1350 base = 16 1351 s = s[2:] 1352 if len(s) < 1 { 1353 err = strconv.ErrSyntax 1354 goto Error 1355 } 1356 case s[0] == '0': 1357 base = 8 1358 default: 1359 base = 10 1360 } 1361 1362 default: 1363 err = errors.New("invalid base " + strconv.Itoa(base)) 1364 goto Error 1365 } 1366 1367 n = 0 1368 cutoff = http2cutoff64(base) 1369 maxVal = 1<<uint(bitSize) - 1 1370 1371 for i := 0; i < len(s); i++ { 1372 var v byte 1373 d := s[i] 1374 switch { 1375 case '0' <= d && d <= '9': 1376 v = d - '0' 1377 case 'a' <= d && d <= 'z': 1378 v = d - 'a' + 10 1379 case 'A' <= d && d <= 'Z': 1380 v = d - 'A' + 10 1381 default: 1382 n = 0 1383 err = strconv.ErrSyntax 1384 goto Error 1385 } 1386 if int(v) >= base { 1387 n = 0 1388 err = strconv.ErrSyntax 1389 goto Error 1390 } 1391 1392 if n >= cutoff { 1393 1394 n = 1<<64 - 1 1395 err = strconv.ErrRange 1396 goto Error 1397 } 1398 n *= uint64(base) 1399 1400 n1 := n + uint64(v) 1401 if n1 < n || n1 > maxVal { 1402 1403 n = 1<<64 - 1 1404 err = strconv.ErrRange 1405 goto Error 1406 } 1407 n = n1 1408 } 1409 1410 return n, nil 1411 1412 Error: 1413 return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err} 1414 } 1415 1416 // Return the first number n such that n*base >= 1<<64. 1417 func http2cutoff64(base int) uint64 { 1418 if base < 2 { 1419 return 0 1420 } 1421 return (1<<64-1)/uint64(base) + 1 1422 } 1423 1424 var ( 1425 http2commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case 1426 http2commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case 1427 ) 1428 1429 func init() { 1430 for _, v := range []string{ 1431 "accept", 1432 "accept-charset", 1433 "accept-encoding", 1434 "accept-language", 1435 "accept-ranges", 1436 "age", 1437 "access-control-allow-origin", 1438 "allow", 1439 "authorization", 1440 "cache-control", 1441 "content-disposition", 1442 "content-encoding", 1443 "content-language", 1444 "content-length", 1445 "content-location", 1446 "content-range", 1447 "content-type", 1448 "cookie", 1449 "date", 1450 "etag", 1451 "expect", 1452 "expires", 1453 "from", 1454 "host", 1455 "if-match", 1456 "if-modified-since", 1457 "if-none-match", 1458 "if-unmodified-since", 1459 "last-modified", 1460 "link", 1461 "location", 1462 "max-forwards", 1463 "proxy-authenticate", 1464 "proxy-authorization", 1465 "range", 1466 "referer", 1467 "refresh", 1468 "retry-after", 1469 "server", 1470 "set-cookie", 1471 "strict-transport-security", 1472 "transfer-encoding", 1473 "user-agent", 1474 "vary", 1475 "via", 1476 "www-authenticate", 1477 } { 1478 chk := CanonicalHeaderKey(v) 1479 http2commonLowerHeader[chk] = v 1480 http2commonCanonHeader[v] = chk 1481 } 1482 } 1483 1484 func http2lowerHeader(v string) string { 1485 if s, ok := http2commonLowerHeader[v]; ok { 1486 return s 1487 } 1488 return strings.ToLower(v) 1489 } 1490 1491 var http2VerboseLogs = false 1492 1493 const ( 1494 // ClientPreface is the string that must be sent by new 1495 // connections from clients. 1496 http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" 1497 1498 // SETTINGS_MAX_FRAME_SIZE default 1499 // http://http2.github.io/http2-spec/#rfc.section.6.5.2 1500 http2initialMaxFrameSize = 16384 1501 1502 // NextProtoTLS is the NPN/ALPN protocol negotiated during 1503 // HTTP/2's TLS setup. 1504 http2NextProtoTLS = "h2" 1505 1506 // http://http2.github.io/http2-spec/#SettingValues 1507 http2initialHeaderTableSize = 4096 1508 1509 http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size 1510 1511 http2defaultMaxReadFrameSize = 1 << 20 1512 ) 1513 1514 var ( 1515 http2clientPreface = []byte(http2ClientPreface) 1516 ) 1517 1518 type http2streamState int 1519 1520 const ( 1521 http2stateIdle http2streamState = iota 1522 http2stateOpen 1523 http2stateHalfClosedLocal 1524 http2stateHalfClosedRemote 1525 http2stateResvLocal 1526 http2stateResvRemote 1527 http2stateClosed 1528 ) 1529 1530 var http2stateName = [...]string{ 1531 http2stateIdle: "Idle", 1532 http2stateOpen: "Open", 1533 http2stateHalfClosedLocal: "HalfClosedLocal", 1534 http2stateHalfClosedRemote: "HalfClosedRemote", 1535 http2stateResvLocal: "ResvLocal", 1536 http2stateResvRemote: "ResvRemote", 1537 http2stateClosed: "Closed", 1538 } 1539 1540 func (st http2streamState) String() string { 1541 return http2stateName[st] 1542 } 1543 1544 // Setting is a setting parameter: which setting it is, and its value. 1545 type http2Setting struct { 1546 // ID is which setting is being set. 1547 // See http://http2.github.io/http2-spec/#SettingValues 1548 ID http2SettingID 1549 1550 // Val is the value. 1551 Val uint32 1552 } 1553 1554 func (s http2Setting) String() string { 1555 return fmt.Sprintf("[%v = %d]", s.ID, s.Val) 1556 } 1557 1558 // Valid reports whether the setting is valid. 1559 func (s http2Setting) Valid() error { 1560 1561 switch s.ID { 1562 case http2SettingEnablePush: 1563 if s.Val != 1 && s.Val != 0 { 1564 return http2ConnectionError(http2ErrCodeProtocol) 1565 } 1566 case http2SettingInitialWindowSize: 1567 if s.Val > 1<<31-1 { 1568 return http2ConnectionError(http2ErrCodeFlowControl) 1569 } 1570 case http2SettingMaxFrameSize: 1571 if s.Val < 16384 || s.Val > 1<<24-1 { 1572 return http2ConnectionError(http2ErrCodeProtocol) 1573 } 1574 } 1575 return nil 1576 } 1577 1578 // A SettingID is an HTTP/2 setting as defined in 1579 // http://http2.github.io/http2-spec/#iana-settings 1580 type http2SettingID uint16 1581 1582 const ( 1583 http2SettingHeaderTableSize http2SettingID = 0x1 1584 http2SettingEnablePush http2SettingID = 0x2 1585 http2SettingMaxConcurrentStreams http2SettingID = 0x3 1586 http2SettingInitialWindowSize http2SettingID = 0x4 1587 http2SettingMaxFrameSize http2SettingID = 0x5 1588 http2SettingMaxHeaderListSize http2SettingID = 0x6 1589 ) 1590 1591 var http2settingName = map[http2SettingID]string{ 1592 http2SettingHeaderTableSize: "HEADER_TABLE_SIZE", 1593 http2SettingEnablePush: "ENABLE_PUSH", 1594 http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS", 1595 http2SettingInitialWindowSize: "INITIAL_WINDOW_SIZE", 1596 http2SettingMaxFrameSize: "MAX_FRAME_SIZE", 1597 http2SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE", 1598 } 1599 1600 func (s http2SettingID) String() string { 1601 if v, ok := http2settingName[s]; ok { 1602 return v 1603 } 1604 return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s)) 1605 } 1606 1607 func http2validHeader(v string) bool { 1608 if len(v) == 0 { 1609 return false 1610 } 1611 for _, r := range v { 1612 1613 if r >= 127 || ('A' <= r && r <= 'Z') { 1614 return false 1615 } 1616 } 1617 return true 1618 } 1619 1620 var http2httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n) 1621 1622 func init() { 1623 for i := 100; i <= 999; i++ { 1624 if v := StatusText(i); v != "" { 1625 http2httpCodeStringCommon[i] = strconv.Itoa(i) 1626 } 1627 } 1628 } 1629 1630 func http2httpCodeString(code int) string { 1631 if s, ok := http2httpCodeStringCommon[code]; ok { 1632 return s 1633 } 1634 return strconv.Itoa(code) 1635 } 1636 1637 // from pkg io 1638 type http2stringWriter interface { 1639 WriteString(s string) (n int, err error) 1640 } 1641 1642 // A gate lets two goroutines coordinate their activities. 1643 type http2gate chan struct{} 1644 1645 func (g http2gate) Done() { g <- struct{}{} } 1646 1647 func (g http2gate) Wait() { <-g } 1648 1649 // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed). 1650 type http2closeWaiter chan struct{} 1651 1652 // Init makes a closeWaiter usable. 1653 // It exists because so a closeWaiter value can be placed inside a 1654 // larger struct and have the Mutex and Cond's memory in the same 1655 // allocation. 1656 func (cw *http2closeWaiter) Init() { 1657 *cw = make(chan struct{}) 1658 } 1659 1660 // Close marks the closeWaiter as closed and unblocks any waiters. 1661 func (cw http2closeWaiter) Close() { 1662 close(cw) 1663 } 1664 1665 // Wait waits for the closeWaiter to become closed. 1666 func (cw http2closeWaiter) Wait() { 1667 <-cw 1668 } 1669 1670 // bufferedWriter is a buffered writer that writes to w. 1671 // Its buffered writer is lazily allocated as needed, to minimize 1672 // idle memory usage with many connections. 1673 type http2bufferedWriter struct { 1674 w io.Writer // immutable 1675 bw *bufio.Writer // non-nil when data is buffered 1676 } 1677 1678 func http2newBufferedWriter(w io.Writer) *http2bufferedWriter { 1679 return &http2bufferedWriter{w: w} 1680 } 1681 1682 var http2bufWriterPool = sync.Pool{ 1683 New: func() interface{} { 1684 1685 return bufio.NewWriterSize(nil, 4<<10) 1686 }, 1687 } 1688 1689 func (w *http2bufferedWriter) Write(p []byte) (n int, err error) { 1690 if w.bw == nil { 1691 bw := http2bufWriterPool.Get().(*bufio.Writer) 1692 bw.Reset(w.w) 1693 w.bw = bw 1694 } 1695 return w.bw.Write(p) 1696 } 1697 1698 func (w *http2bufferedWriter) Flush() error { 1699 bw := w.bw 1700 if bw == nil { 1701 return nil 1702 } 1703 err := bw.Flush() 1704 bw.Reset(nil) 1705 http2bufWriterPool.Put(bw) 1706 w.bw = nil 1707 return err 1708 } 1709 1710 type http2pipe struct { 1711 b http2buffer 1712 c sync.Cond 1713 m sync.Mutex 1714 } 1715 1716 // Read waits until data is available and copies bytes 1717 // from the buffer into p. 1718 func (r *http2pipe) Read(p []byte) (n int, err error) { 1719 r.c.L.Lock() 1720 defer r.c.L.Unlock() 1721 for r.b.Len() == 0 && !r.b.closed { 1722 r.c.Wait() 1723 } 1724 return r.b.Read(p) 1725 } 1726 1727 // Write copies bytes from p into the buffer and wakes a reader. 1728 // It is an error to write more data than the buffer can hold. 1729 func (w *http2pipe) Write(p []byte) (n int, err error) { 1730 w.c.L.Lock() 1731 defer w.c.L.Unlock() 1732 defer w.c.Signal() 1733 return w.b.Write(p) 1734 } 1735 1736 func (c *http2pipe) Close(err error) { 1737 c.c.L.Lock() 1738 defer c.c.L.Unlock() 1739 defer c.c.Signal() 1740 c.b.Close(err) 1741 } 1742 1743 const ( 1744 http2prefaceTimeout = 10 * time.Second 1745 http2firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway 1746 http2handlerChunkWriteSize = 4 << 10 1747 http2defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to? 1748 ) 1749 1750 var ( 1751 http2errClientDisconnected = errors.New("client disconnected") 1752 http2errClosedBody = errors.New("body closed by handler") 1753 http2errStreamBroken = errors.New("http2: stream broken") 1754 ) 1755 1756 var http2responseWriterStatePool = sync.Pool{ 1757 New: func() interface{} { 1758 rws := &http2responseWriterState{} 1759 rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize) 1760 return rws 1761 }, 1762 } 1763 1764 // Test hooks. 1765 var ( 1766 http2testHookOnConn func() 1767 http2testHookGetServerConn func(*http2serverConn) 1768 http2testHookOnPanicMu *sync.Mutex // nil except in tests 1769 http2testHookOnPanic func(sc *http2serverConn, panicVal interface{}) (rePanic bool) 1770 ) 1771 1772 // Server is an HTTP/2 server. 1773 type http2Server struct { 1774 // MaxHandlers limits the number of http.Handler ServeHTTP goroutines 1775 // which may run at a time over all connections. 1776 // Negative or zero no limit. 1777 // TODO: implement 1778 MaxHandlers int 1779 1780 // MaxConcurrentStreams optionally specifies the number of 1781 // concurrent streams that each client may have open at a 1782 // time. This is unrelated to the number of http.Handler goroutines 1783 // which may be active globally, which is MaxHandlers. 1784 // If zero, MaxConcurrentStreams defaults to at least 100, per 1785 // the HTTP/2 spec's recommendations. 1786 MaxConcurrentStreams uint32 1787 1788 // MaxReadFrameSize optionally specifies the largest frame 1789 // this server is willing to read. A valid value is between 1790 // 16k and 16M, inclusive. If zero or otherwise invalid, a 1791 // default value is used. 1792 MaxReadFrameSize uint32 1793 1794 // PermitProhibitedCipherSuites, if true, permits the use of 1795 // cipher suites prohibited by the HTTP/2 spec. 1796 PermitProhibitedCipherSuites bool 1797 } 1798 1799 func (s *http2Server) maxReadFrameSize() uint32 { 1800 if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize { 1801 return v 1802 } 1803 return http2defaultMaxReadFrameSize 1804 } 1805 1806 func (s *http2Server) maxConcurrentStreams() uint32 { 1807 if v := s.MaxConcurrentStreams; v > 0 { 1808 return v 1809 } 1810 return http2defaultMaxStreams 1811 } 1812 1813 // ConfigureServer adds HTTP/2 support to a net/http Server. 1814 // 1815 // The configuration conf may be nil. 1816 // 1817 // ConfigureServer must be called before s begins serving. 1818 func http2ConfigureServer(s *Server, conf *http2Server) { 1819 if conf == nil { 1820 conf = new(http2Server) 1821 } 1822 if s.TLSConfig == nil { 1823 s.TLSConfig = new(tls.Config) 1824 } 1825 1826 if s.TLSConfig.CipherSuites != nil { 1827 const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 1828 haveRequired := false 1829 for _, v := range s.TLSConfig.CipherSuites { 1830 if v == requiredCipher { 1831 haveRequired = true 1832 break 1833 } 1834 } 1835 if !haveRequired { 1836 s.TLSConfig.CipherSuites = append(s.TLSConfig.CipherSuites, requiredCipher) 1837 } 1838 } 1839 1840 haveNPN := false 1841 for _, p := range s.TLSConfig.NextProtos { 1842 if p == http2NextProtoTLS { 1843 haveNPN = true 1844 break 1845 } 1846 } 1847 if !haveNPN { 1848 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS) 1849 } 1850 1851 s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2-14") 1852 1853 if s.TLSNextProto == nil { 1854 s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){} 1855 } 1856 protoHandler := func(hs *Server, c *tls.Conn, h Handler) { 1857 if http2testHookOnConn != nil { 1858 http2testHookOnConn() 1859 } 1860 conf.handleConn(hs, c, h) 1861 } 1862 s.TLSNextProto[http2NextProtoTLS] = protoHandler 1863 s.TLSNextProto["h2-14"] = protoHandler 1864 } 1865 1866 func (srv *http2Server) handleConn(hs *Server, c net.Conn, h Handler) { 1867 sc := &http2serverConn{ 1868 srv: srv, 1869 hs: hs, 1870 conn: c, 1871 remoteAddrStr: c.RemoteAddr().String(), 1872 bw: http2newBufferedWriter(c), 1873 handler: h, 1874 streams: make(map[uint32]*http2stream), 1875 readFrameCh: make(chan http2readFrameResult), 1876 wantWriteFrameCh: make(chan http2frameWriteMsg, 8), 1877 wroteFrameCh: make(chan struct{}, 1), 1878 bodyReadCh: make(chan http2bodyReadMsg), 1879 doneServing: make(chan struct{}), 1880 advMaxStreams: srv.maxConcurrentStreams(), 1881 writeSched: http2writeScheduler{ 1882 maxFrameSize: http2initialMaxFrameSize, 1883 }, 1884 initialWindowSize: http2initialWindowSize, 1885 headerTableSize: http2initialHeaderTableSize, 1886 serveG: http2newGoroutineLock(), 1887 pushEnabled: true, 1888 } 1889 sc.flow.add(http2initialWindowSize) 1890 sc.inflow.add(http2initialWindowSize) 1891 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf) 1892 sc.hpackDecoder = hpack.NewDecoder(http2initialHeaderTableSize, sc.onNewHeaderField) 1893 sc.hpackDecoder.SetMaxStringLength(sc.maxHeaderStringLen()) 1894 1895 fr := http2NewFramer(sc.bw, c) 1896 fr.SetMaxReadFrameSize(srv.maxReadFrameSize()) 1897 sc.framer = fr 1898 1899 if tc, ok := c.(*tls.Conn); ok { 1900 sc.tlsState = new(tls.ConnectionState) 1901 *sc.tlsState = tc.ConnectionState() 1902 1903 if sc.tlsState.Version < tls.VersionTLS12 { 1904 sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low") 1905 return 1906 } 1907 1908 if sc.tlsState.ServerName == "" { 1909 1910 } 1911 1912 if !srv.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) { 1913 1914 sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite)) 1915 return 1916 } 1917 } 1918 1919 if hook := http2testHookGetServerConn; hook != nil { 1920 hook(sc) 1921 } 1922 sc.serve() 1923 } 1924 1925 // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec. 1926 func http2isBadCipher(cipher uint16) bool { 1927 switch cipher { 1928 case tls.TLS_RSA_WITH_RC4_128_SHA, 1929 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, 1930 tls.TLS_RSA_WITH_AES_128_CBC_SHA, 1931 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 1932 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 1933 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 1934 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 1935 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, 1936 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 1937 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 1938 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: 1939 1940 return true 1941 default: 1942 return false 1943 } 1944 } 1945 1946 func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) { 1947 sc.vlogf("REJECTING conn: %v, %s", err, debug) 1948 1949 sc.framer.WriteGoAway(0, err, []byte(debug)) 1950 sc.bw.Flush() 1951 sc.conn.Close() 1952 } 1953 1954 type http2serverConn struct { 1955 // Immutable: 1956 srv *http2Server 1957 hs *Server 1958 conn net.Conn 1959 bw *http2bufferedWriter // writing to conn 1960 handler Handler 1961 framer *http2Framer 1962 hpackDecoder *hpack.Decoder 1963 doneServing chan struct{} // closed when serverConn.serve ends 1964 readFrameCh chan http2readFrameResult // written by serverConn.readFrames 1965 wantWriteFrameCh chan http2frameWriteMsg // from handlers -> serve 1966 wroteFrameCh chan struct{} // from writeFrameAsync -> serve, tickles more frame writes 1967 bodyReadCh chan http2bodyReadMsg // from handlers -> serve 1968 testHookCh chan func(int) // code to run on the serve loop 1969 flow http2flow // conn-wide (not stream-specific) outbound flow control 1970 inflow http2flow // conn-wide inbound flow control 1971 tlsState *tls.ConnectionState // shared by all handlers, like net/http 1972 remoteAddrStr string 1973 1974 // Everything following is owned by the serve loop; use serveG.check(): 1975 serveG http2goroutineLock // used to verify funcs are on serve() 1976 pushEnabled bool 1977 sawFirstSettings bool // got the initial SETTINGS frame after the preface 1978 needToSendSettingsAck bool 1979 unackedSettings int // how many SETTINGS have we sent without ACKs? 1980 clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit) 1981 advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client 1982 curOpenStreams uint32 // client's number of open streams 1983 maxStreamID uint32 // max ever seen 1984 streams map[uint32]*http2stream 1985 initialWindowSize int32 1986 headerTableSize uint32 1987 peerMaxHeaderListSize uint32 // zero means unknown (default) 1988 canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case 1989 req http2requestParam // non-zero while reading request headers 1990 writingFrame bool // started write goroutine but haven't heard back on wroteFrameCh 1991 needsFrameFlush bool // last frame write wasn't a flush 1992 writeSched http2writeScheduler 1993 inGoAway bool // we've started to or sent GOAWAY 1994 needToSendGoAway bool // we need to schedule a GOAWAY frame write 1995 goAwayCode http2ErrCode 1996 shutdownTimerCh <-chan time.Time // nil until used 1997 shutdownTimer *time.Timer // nil until used 1998 1999 // Owned by the writeFrameAsync goroutine: 2000 headerWriteBuf bytes.Buffer 2001 hpackEncoder *hpack.Encoder 2002 } 2003 2004 func (sc *http2serverConn) maxHeaderStringLen() int { 2005 v := sc.maxHeaderListSize() 2006 if uint32(int(v)) == v { 2007 return int(v) 2008 } 2009 2010 return 0 2011 } 2012 2013 func (sc *http2serverConn) maxHeaderListSize() uint32 { 2014 n := sc.hs.MaxHeaderBytes 2015 if n <= 0 { 2016 n = DefaultMaxHeaderBytes 2017 } 2018 // http2's count is in a slightly different unit and includes 32 bytes per pair. 2019 // So, take the net/http.Server value and pad it up a bit, assuming 10 headers. 2020 const perFieldOverhead = 32 // per http2 spec 2021 const typicalHeaders = 10 // conservative 2022 return uint32(n + typicalHeaders*perFieldOverhead) 2023 } 2024 2025 // requestParam is the state of the next request, initialized over 2026 // potentially several frames HEADERS + zero or more CONTINUATION 2027 // frames. 2028 type http2requestParam struct { 2029 // stream is non-nil if we're reading (HEADER or CONTINUATION) 2030 // frames for a request (but not DATA). 2031 stream *http2stream 2032 header Header 2033 method, path string 2034 scheme, authority string 2035 sawRegularHeader bool // saw a non-pseudo header already 2036 invalidHeader bool // an invalid header was seen 2037 headerListSize int64 // actually uint32, but easier math this way 2038 } 2039 2040 // stream represents a stream. This is the minimal metadata needed by 2041 // the serve goroutine. Most of the actual stream state is owned by 2042 // the http.Handler's goroutine in the responseWriter. Because the 2043 // responseWriter's responseWriterState is recycled at the end of a 2044 // handler, this struct intentionally has no pointer to the 2045 // *responseWriter{,State} itself, as the Handler ending nils out the 2046 // responseWriter's state field. 2047 type http2stream struct { 2048 // immutable: 2049 id uint32 2050 body *http2pipe // non-nil if expecting DATA frames 2051 cw http2closeWaiter // closed wait stream transitions to closed state 2052 2053 // owned by serverConn's serve loop: 2054 bodyBytes int64 // body bytes seen so far 2055 declBodyBytes int64 // or -1 if undeclared 2056 flow http2flow // limits writing from Handler to client 2057 inflow http2flow // what the client is allowed to POST/etc to us 2058 parent *http2stream // or nil 2059 weight uint8 2060 state http2streamState 2061 sentReset bool // only true once detached from streams map 2062 gotReset bool // only true once detacted from streams map 2063 } 2064 2065 func (sc *http2serverConn) Framer() *http2Framer { return sc.framer } 2066 2067 func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() } 2068 2069 func (sc *http2serverConn) Flush() error { return sc.bw.Flush() } 2070 2071 func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) { 2072 return sc.hpackEncoder, &sc.headerWriteBuf 2073 } 2074 2075 func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) { 2076 sc.serveG.check() 2077 2078 if st, ok := sc.streams[streamID]; ok { 2079 return st.state, st 2080 } 2081 2082 if streamID <= sc.maxStreamID { 2083 return http2stateClosed, nil 2084 } 2085 return http2stateIdle, nil 2086 } 2087 2088 func (sc *http2serverConn) vlogf(format string, args ...interface{}) { 2089 if http2VerboseLogs { 2090 sc.logf(format, args...) 2091 } 2092 } 2093 2094 func (sc *http2serverConn) logf(format string, args ...interface{}) { 2095 if lg := sc.hs.ErrorLog; lg != nil { 2096 lg.Printf(format, args...) 2097 } else { 2098 log.Printf(format, args...) 2099 } 2100 } 2101 2102 func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) { 2103 if err == nil { 2104 return 2105 } 2106 str := err.Error() 2107 if err == io.EOF || strings.Contains(str, "use of closed network connection") { 2108 2109 sc.vlogf(format, args...) 2110 } else { 2111 sc.logf(format, args...) 2112 } 2113 } 2114 2115 func (sc *http2serverConn) onNewHeaderField(f hpack.HeaderField) { 2116 sc.serveG.check() 2117 sc.vlogf("got header field %+v", f) 2118 switch { 2119 case !http2validHeader(f.Name): 2120 sc.req.invalidHeader = true 2121 case strings.HasPrefix(f.Name, ":"): 2122 if sc.req.sawRegularHeader { 2123 sc.logf("pseudo-header after regular header") 2124 sc.req.invalidHeader = true 2125 return 2126 } 2127 var dst *string 2128 switch f.Name { 2129 case ":method": 2130 dst = &sc.req.method 2131 case ":path": 2132 dst = &sc.req.path 2133 case ":scheme": 2134 dst = &sc.req.scheme 2135 case ":authority": 2136 dst = &sc.req.authority 2137 default: 2138 2139 sc.logf("invalid pseudo-header %q", f.Name) 2140 sc.req.invalidHeader = true 2141 return 2142 } 2143 if *dst != "" { 2144 sc.logf("duplicate pseudo-header %q sent", f.Name) 2145 sc.req.invalidHeader = true 2146 return 2147 } 2148 *dst = f.Value 2149 default: 2150 sc.req.sawRegularHeader = true 2151 sc.req.header.Add(sc.canonicalHeader(f.Name), f.Value) 2152 const headerFieldOverhead = 32 // per spec 2153 sc.req.headerListSize += int64(len(f.Name)) + int64(len(f.Value)) + headerFieldOverhead 2154 if sc.req.headerListSize > int64(sc.maxHeaderListSize()) { 2155 sc.hpackDecoder.SetEmitEnabled(false) 2156 } 2157 } 2158 } 2159 2160 func (sc *http2serverConn) canonicalHeader(v string) string { 2161 sc.serveG.check() 2162 cv, ok := http2commonCanonHeader[v] 2163 if ok { 2164 return cv 2165 } 2166 cv, ok = sc.canonHeader[v] 2167 if ok { 2168 return cv 2169 } 2170 if sc.canonHeader == nil { 2171 sc.canonHeader = make(map[string]string) 2172 } 2173 cv = CanonicalHeaderKey(v) 2174 sc.canonHeader[v] = cv 2175 return cv 2176 } 2177 2178 type http2readFrameResult struct { 2179 f http2Frame // valid until readMore is called 2180 err error 2181 2182 // readMore should be called once the consumer no longer needs or 2183 // retains f. After readMore, f is invalid and more frames can be 2184 // read. 2185 readMore func() 2186 } 2187 2188 // readFrames is the loop that reads incoming frames. 2189 // It takes care to only read one frame at a time, blocking until the 2190 // consumer is done with the frame. 2191 // It's run on its own goroutine. 2192 func (sc *http2serverConn) readFrames() { 2193 gate := make(http2gate) 2194 for { 2195 f, err := sc.framer.ReadFrame() 2196 select { 2197 case sc.readFrameCh <- http2readFrameResult{f, err, gate.Done}: 2198 case <-sc.doneServing: 2199 return 2200 } 2201 select { 2202 case <-gate: 2203 case <-sc.doneServing: 2204 return 2205 } 2206 } 2207 } 2208 2209 // writeFrameAsync runs in its own goroutine and writes a single frame 2210 // and then reports when it's done. 2211 // At most one goroutine can be running writeFrameAsync at a time per 2212 // serverConn. 2213 func (sc *http2serverConn) writeFrameAsync(wm http2frameWriteMsg) { 2214 err := wm.write.writeFrame(sc) 2215 if ch := wm.done; ch != nil { 2216 select { 2217 case ch <- err: 2218 default: 2219 panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.write)) 2220 } 2221 } 2222 sc.wroteFrameCh <- struct{}{} 2223 } 2224 2225 func (sc *http2serverConn) closeAllStreamsOnConnClose() { 2226 sc.serveG.check() 2227 for _, st := range sc.streams { 2228 sc.closeStream(st, http2errClientDisconnected) 2229 } 2230 } 2231 2232 func (sc *http2serverConn) stopShutdownTimer() { 2233 sc.serveG.check() 2234 if t := sc.shutdownTimer; t != nil { 2235 t.Stop() 2236 } 2237 } 2238 2239 func (sc *http2serverConn) notePanic() { 2240 if http2testHookOnPanicMu != nil { 2241 http2testHookOnPanicMu.Lock() 2242 defer http2testHookOnPanicMu.Unlock() 2243 } 2244 if http2testHookOnPanic != nil { 2245 if e := recover(); e != nil { 2246 if http2testHookOnPanic(sc, e) { 2247 panic(e) 2248 } 2249 } 2250 } 2251 } 2252 2253 func (sc *http2serverConn) serve() { 2254 sc.serveG.check() 2255 defer sc.notePanic() 2256 defer sc.conn.Close() 2257 defer sc.closeAllStreamsOnConnClose() 2258 defer sc.stopShutdownTimer() 2259 defer close(sc.doneServing) 2260 2261 sc.vlogf("HTTP/2 connection from %v on %p", sc.conn.RemoteAddr(), sc.hs) 2262 2263 sc.writeFrame(http2frameWriteMsg{ 2264 write: http2writeSettings{ 2265 {http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()}, 2266 {http2SettingMaxConcurrentStreams, sc.advMaxStreams}, 2267 {http2SettingMaxHeaderListSize, sc.maxHeaderListSize()}, 2268 }, 2269 }) 2270 sc.unackedSettings++ 2271 2272 if err := sc.readPreface(); err != nil { 2273 sc.condlogf(err, "error reading preface from client %v: %v", sc.conn.RemoteAddr(), err) 2274 return 2275 } 2276 2277 go sc.readFrames() 2278 2279 settingsTimer := time.NewTimer(http2firstSettingsTimeout) 2280 loopNum := 0 2281 for { 2282 loopNum++ 2283 select { 2284 case wm := <-sc.wantWriteFrameCh: 2285 sc.writeFrame(wm) 2286 case <-sc.wroteFrameCh: 2287 if sc.writingFrame != true { 2288 panic("internal error: expected to be already writing a frame") 2289 } 2290 sc.writingFrame = false 2291 sc.scheduleFrameWrite() 2292 case res := <-sc.readFrameCh: 2293 if !sc.processFrameFromReader(res) { 2294 return 2295 } 2296 res.readMore() 2297 if settingsTimer.C != nil { 2298 settingsTimer.Stop() 2299 settingsTimer.C = nil 2300 } 2301 case m := <-sc.bodyReadCh: 2302 sc.noteBodyRead(m.st, m.n) 2303 case <-settingsTimer.C: 2304 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr()) 2305 return 2306 case <-sc.shutdownTimerCh: 2307 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr()) 2308 return 2309 case fn := <-sc.testHookCh: 2310 fn(loopNum) 2311 } 2312 } 2313 } 2314 2315 // readPreface reads the ClientPreface greeting from the peer 2316 // or returns an error on timeout or an invalid greeting. 2317 func (sc *http2serverConn) readPreface() error { 2318 errc := make(chan error, 1) 2319 go func() { 2320 2321 buf := make([]byte, len(http2ClientPreface)) 2322 if _, err := io.ReadFull(sc.conn, buf); err != nil { 2323 errc <- err 2324 } else if !bytes.Equal(buf, http2clientPreface) { 2325 errc <- fmt.Errorf("bogus greeting %q", buf) 2326 } else { 2327 errc <- nil 2328 } 2329 }() 2330 timer := time.NewTimer(http2prefaceTimeout) 2331 defer timer.Stop() 2332 select { 2333 case <-timer.C: 2334 return errors.New("timeout waiting for client preface") 2335 case err := <-errc: 2336 if err == nil { 2337 sc.vlogf("client %v said hello", sc.conn.RemoteAddr()) 2338 } 2339 return err 2340 } 2341 } 2342 2343 var http2errChanPool = sync.Pool{ 2344 New: func() interface{} { return make(chan error, 1) }, 2345 } 2346 2347 // writeDataFromHandler writes the data described in req to stream.id. 2348 // 2349 // The flow control currently happens in the Handler where it waits 2350 // for 1 or more bytes to be available to then write here. So at this 2351 // point we know that we have flow control. But this might have to 2352 // change when priority is implemented, so the serve goroutine knows 2353 // the total amount of bytes waiting to be sent and can can have more 2354 // scheduling decisions available. 2355 func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, writeData *http2writeData) error { 2356 ch := http2errChanPool.Get().(chan error) 2357 sc.writeFrameFromHandler(http2frameWriteMsg{ 2358 write: writeData, 2359 stream: stream, 2360 done: ch, 2361 }) 2362 select { 2363 case err := <-ch: 2364 http2errChanPool.Put(ch) 2365 return err 2366 case <-sc.doneServing: 2367 return http2errClientDisconnected 2368 case <-stream.cw: 2369 return http2errStreamBroken 2370 } 2371 } 2372 2373 // writeFrameFromHandler sends wm to sc.wantWriteFrameCh, but aborts 2374 // if the connection has gone away. 2375 // 2376 // This must not be run from the serve goroutine itself, else it might 2377 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 2378 // buffered and is read by serve itself). If you're on the serve 2379 // goroutine, call writeFrame instead. 2380 func (sc *http2serverConn) writeFrameFromHandler(wm http2frameWriteMsg) { 2381 sc.serveG.checkNotOn() 2382 var scheduled bool 2383 select { 2384 case sc.wantWriteFrameCh <- wm: 2385 scheduled = true 2386 case <-sc.doneServing: 2387 2388 case <-wm.stream.cw: 2389 2390 } 2391 2392 if !scheduled && wm.done != nil { 2393 select { 2394 case wm.done <- http2errStreamBroken: 2395 default: 2396 panic("expected buffered channel") 2397 } 2398 } 2399 } 2400 2401 // writeFrame schedules a frame to write and sends it if there's nothing 2402 // already being written. 2403 // 2404 // There is no pushback here (the serve goroutine never blocks). It's 2405 // the http.Handlers that block, waiting for their previous frames to 2406 // make it onto the wire 2407 // 2408 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 2409 func (sc *http2serverConn) writeFrame(wm http2frameWriteMsg) { 2410 sc.serveG.check() 2411 sc.writeSched.add(wm) 2412 sc.scheduleFrameWrite() 2413 } 2414 2415 // startFrameWrite starts a goroutine to write wm (in a separate 2416 // goroutine since that might block on the network), and updates the 2417 // serve goroutine's state about the world, updated from info in wm. 2418 func (sc *http2serverConn) startFrameWrite(wm http2frameWriteMsg) { 2419 sc.serveG.check() 2420 if sc.writingFrame { 2421 panic("internal error: can only be writing one frame at a time") 2422 } 2423 sc.writingFrame = true 2424 2425 st := wm.stream 2426 if st != nil { 2427 switch st.state { 2428 case http2stateHalfClosedLocal: 2429 panic("internal error: attempt to send frame on half-closed-local stream") 2430 case http2stateClosed: 2431 if st.sentReset || st.gotReset { 2432 2433 sc.wroteFrameCh <- struct{}{} 2434 return 2435 } 2436 panic(fmt.Sprintf("internal error: attempt to send a write %v on a closed stream", wm)) 2437 } 2438 } 2439 2440 sc.needsFrameFlush = true 2441 if http2endsStream(wm.write) { 2442 if st == nil { 2443 panic("internal error: expecting non-nil stream") 2444 } 2445 switch st.state { 2446 case http2stateOpen: 2447 2448 st.state = http2stateHalfClosedLocal 2449 errCancel := http2StreamError{st.id, http2ErrCodeCancel} 2450 sc.resetStream(errCancel) 2451 case http2stateHalfClosedRemote: 2452 sc.closeStream(st, nil) 2453 } 2454 } 2455 go sc.writeFrameAsync(wm) 2456 } 2457 2458 // scheduleFrameWrite tickles the frame writing scheduler. 2459 // 2460 // If a frame is already being written, nothing happens. This will be called again 2461 // when the frame is done being written. 2462 // 2463 // If a frame isn't being written we need to send one, the best frame 2464 // to send is selected, preferring first things that aren't 2465 // stream-specific (e.g. ACKing settings), and then finding the 2466 // highest priority stream. 2467 // 2468 // If a frame isn't being written and there's nothing else to send, we 2469 // flush the write buffer. 2470 func (sc *http2serverConn) scheduleFrameWrite() { 2471 sc.serveG.check() 2472 if sc.writingFrame { 2473 return 2474 } 2475 if sc.needToSendGoAway { 2476 sc.needToSendGoAway = false 2477 sc.startFrameWrite(http2frameWriteMsg{ 2478 write: &http2writeGoAway{ 2479 maxStreamID: sc.maxStreamID, 2480 code: sc.goAwayCode, 2481 }, 2482 }) 2483 return 2484 } 2485 if sc.needToSendSettingsAck { 2486 sc.needToSendSettingsAck = false 2487 sc.startFrameWrite(http2frameWriteMsg{write: http2writeSettingsAck{}}) 2488 return 2489 } 2490 if !sc.inGoAway { 2491 if wm, ok := sc.writeSched.take(); ok { 2492 sc.startFrameWrite(wm) 2493 return 2494 } 2495 } 2496 if sc.needsFrameFlush { 2497 sc.startFrameWrite(http2frameWriteMsg{write: http2flushFrameWriter{}}) 2498 sc.needsFrameFlush = false 2499 return 2500 } 2501 } 2502 2503 func (sc *http2serverConn) goAway(code http2ErrCode) { 2504 sc.serveG.check() 2505 if sc.inGoAway { 2506 return 2507 } 2508 if code != http2ErrCodeNo { 2509 sc.shutDownIn(250 * time.Millisecond) 2510 } else { 2511 2512 sc.shutDownIn(1 * time.Second) 2513 } 2514 sc.inGoAway = true 2515 sc.needToSendGoAway = true 2516 sc.goAwayCode = code 2517 sc.scheduleFrameWrite() 2518 } 2519 2520 func (sc *http2serverConn) shutDownIn(d time.Duration) { 2521 sc.serveG.check() 2522 sc.shutdownTimer = time.NewTimer(d) 2523 sc.shutdownTimerCh = sc.shutdownTimer.C 2524 } 2525 2526 func (sc *http2serverConn) resetStream(se http2StreamError) { 2527 sc.serveG.check() 2528 sc.writeFrame(http2frameWriteMsg{write: se}) 2529 if st, ok := sc.streams[se.StreamID]; ok { 2530 st.sentReset = true 2531 sc.closeStream(st, se) 2532 } 2533 } 2534 2535 // curHeaderStreamID returns the stream ID of the header block we're 2536 // currently in the middle of reading. If this returns non-zero, the 2537 // next frame must be a CONTINUATION with this stream id. 2538 func (sc *http2serverConn) curHeaderStreamID() uint32 { 2539 sc.serveG.check() 2540 st := sc.req.stream 2541 if st == nil { 2542 return 0 2543 } 2544 return st.id 2545 } 2546 2547 // processFrameFromReader processes the serve loop's read from readFrameCh from the 2548 // frame-reading goroutine. 2549 // processFrameFromReader returns whether the connection should be kept open. 2550 func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool { 2551 sc.serveG.check() 2552 err := res.err 2553 if err != nil { 2554 if err == http2ErrFrameTooLarge { 2555 sc.goAway(http2ErrCodeFrameSize) 2556 return true 2557 } 2558 clientGone := err == io.EOF || strings.Contains(err.Error(), "use of closed network connection") 2559 if clientGone { 2560 2561 return false 2562 } 2563 } else { 2564 f := res.f 2565 sc.vlogf("got %v: %#v", f.Header(), f) 2566 err = sc.processFrame(f) 2567 if err == nil { 2568 return true 2569 } 2570 } 2571 2572 switch ev := err.(type) { 2573 case http2StreamError: 2574 sc.resetStream(ev) 2575 return true 2576 case http2goAwayFlowError: 2577 sc.goAway(http2ErrCodeFlowControl) 2578 return true 2579 case http2ConnectionError: 2580 sc.logf("%v: %v", sc.conn.RemoteAddr(), ev) 2581 sc.goAway(http2ErrCode(ev)) 2582 return true 2583 default: 2584 if res.err != nil { 2585 sc.logf("disconnecting; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 2586 } else { 2587 sc.logf("disconnection due to other error: %v", err) 2588 } 2589 return false 2590 } 2591 } 2592 2593 func (sc *http2serverConn) processFrame(f http2Frame) error { 2594 sc.serveG.check() 2595 2596 if !sc.sawFirstSettings { 2597 if _, ok := f.(*http2SettingsFrame); !ok { 2598 return http2ConnectionError(http2ErrCodeProtocol) 2599 } 2600 sc.sawFirstSettings = true 2601 } 2602 2603 if s := sc.curHeaderStreamID(); s != 0 { 2604 if cf, ok := f.(*http2ContinuationFrame); !ok { 2605 return http2ConnectionError(http2ErrCodeProtocol) 2606 } else if cf.Header().StreamID != s { 2607 return http2ConnectionError(http2ErrCodeProtocol) 2608 } 2609 } 2610 2611 switch f := f.(type) { 2612 case *http2SettingsFrame: 2613 return sc.processSettings(f) 2614 case *http2HeadersFrame: 2615 return sc.processHeaders(f) 2616 case *http2ContinuationFrame: 2617 return sc.processContinuation(f) 2618 case *http2WindowUpdateFrame: 2619 return sc.processWindowUpdate(f) 2620 case *http2PingFrame: 2621 return sc.processPing(f) 2622 case *http2DataFrame: 2623 return sc.processData(f) 2624 case *http2RSTStreamFrame: 2625 return sc.processResetStream(f) 2626 case *http2PriorityFrame: 2627 return sc.processPriority(f) 2628 case *http2PushPromiseFrame: 2629 2630 return http2ConnectionError(http2ErrCodeProtocol) 2631 default: 2632 sc.vlogf("Ignoring frame: %v", f.Header()) 2633 return nil 2634 } 2635 } 2636 2637 func (sc *http2serverConn) processPing(f *http2PingFrame) error { 2638 sc.serveG.check() 2639 if f.Flags.Has(http2FlagSettingsAck) { 2640 2641 return nil 2642 } 2643 if f.StreamID != 0 { 2644 2645 return http2ConnectionError(http2ErrCodeProtocol) 2646 } 2647 sc.writeFrame(http2frameWriteMsg{write: http2writePingAck{f}}) 2648 return nil 2649 } 2650 2651 func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error { 2652 sc.serveG.check() 2653 switch { 2654 case f.StreamID != 0: 2655 st := sc.streams[f.StreamID] 2656 if st == nil { 2657 2658 return nil 2659 } 2660 if !st.flow.add(int32(f.Increment)) { 2661 return http2StreamError{f.StreamID, http2ErrCodeFlowControl} 2662 } 2663 default: 2664 if !sc.flow.add(int32(f.Increment)) { 2665 return http2goAwayFlowError{} 2666 } 2667 } 2668 sc.scheduleFrameWrite() 2669 return nil 2670 } 2671 2672 func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error { 2673 sc.serveG.check() 2674 2675 state, st := sc.state(f.StreamID) 2676 if state == http2stateIdle { 2677 2678 return http2ConnectionError(http2ErrCodeProtocol) 2679 } 2680 if st != nil { 2681 st.gotReset = true 2682 sc.closeStream(st, http2StreamError{f.StreamID, f.ErrCode}) 2683 } 2684 return nil 2685 } 2686 2687 func (sc *http2serverConn) closeStream(st *http2stream, err error) { 2688 sc.serveG.check() 2689 if st.state == http2stateIdle || st.state == http2stateClosed { 2690 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 2691 } 2692 st.state = http2stateClosed 2693 sc.curOpenStreams-- 2694 delete(sc.streams, st.id) 2695 if p := st.body; p != nil { 2696 p.Close(err) 2697 } 2698 st.cw.Close() 2699 sc.writeSched.forgetStream(st.id) 2700 } 2701 2702 func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error { 2703 sc.serveG.check() 2704 if f.IsAck() { 2705 sc.unackedSettings-- 2706 if sc.unackedSettings < 0 { 2707 2708 return http2ConnectionError(http2ErrCodeProtocol) 2709 } 2710 return nil 2711 } 2712 if err := f.ForeachSetting(sc.processSetting); err != nil { 2713 return err 2714 } 2715 sc.needToSendSettingsAck = true 2716 sc.scheduleFrameWrite() 2717 return nil 2718 } 2719 2720 func (sc *http2serverConn) processSetting(s http2Setting) error { 2721 sc.serveG.check() 2722 if err := s.Valid(); err != nil { 2723 return err 2724 } 2725 sc.vlogf("processing setting %v", s) 2726 switch s.ID { 2727 case http2SettingHeaderTableSize: 2728 sc.headerTableSize = s.Val 2729 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 2730 case http2SettingEnablePush: 2731 sc.pushEnabled = s.Val != 0 2732 case http2SettingMaxConcurrentStreams: 2733 sc.clientMaxStreams = s.Val 2734 case http2SettingInitialWindowSize: 2735 return sc.processSettingInitialWindowSize(s.Val) 2736 case http2SettingMaxFrameSize: 2737 sc.writeSched.maxFrameSize = s.Val 2738 case http2SettingMaxHeaderListSize: 2739 sc.peerMaxHeaderListSize = s.Val 2740 default: 2741 2742 } 2743 return nil 2744 } 2745 2746 func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error { 2747 sc.serveG.check() 2748 2749 old := sc.initialWindowSize 2750 sc.initialWindowSize = int32(val) 2751 growth := sc.initialWindowSize - old 2752 for _, st := range sc.streams { 2753 if !st.flow.add(growth) { 2754 2755 return http2ConnectionError(http2ErrCodeFlowControl) 2756 } 2757 } 2758 return nil 2759 } 2760 2761 func (sc *http2serverConn) processData(f *http2DataFrame) error { 2762 sc.serveG.check() 2763 2764 id := f.Header().StreamID 2765 st, ok := sc.streams[id] 2766 if !ok || st.state != http2stateOpen { 2767 2768 return http2StreamError{id, http2ErrCodeStreamClosed} 2769 } 2770 if st.body == nil { 2771 panic("internal error: should have a body in this state") 2772 } 2773 data := f.Data() 2774 2775 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 2776 st.body.Close(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 2777 return http2StreamError{id, http2ErrCodeStreamClosed} 2778 } 2779 if len(data) > 0 { 2780 2781 if int(st.inflow.available()) < len(data) { 2782 return http2StreamError{id, http2ErrCodeFlowControl} 2783 } 2784 st.inflow.take(int32(len(data))) 2785 wrote, err := st.body.Write(data) 2786 if err != nil { 2787 return http2StreamError{id, http2ErrCodeStreamClosed} 2788 } 2789 if wrote != len(data) { 2790 panic("internal error: bad Writer") 2791 } 2792 st.bodyBytes += int64(len(data)) 2793 } 2794 if f.StreamEnded() { 2795 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 2796 st.body.Close(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 2797 st.declBodyBytes, st.bodyBytes)) 2798 } else { 2799 st.body.Close(io.EOF) 2800 } 2801 st.state = http2stateHalfClosedRemote 2802 } 2803 return nil 2804 } 2805 2806 func (sc *http2serverConn) processHeaders(f *http2HeadersFrame) error { 2807 sc.serveG.check() 2808 id := f.Header().StreamID 2809 if sc.inGoAway { 2810 2811 return nil 2812 } 2813 2814 if id%2 != 1 || id <= sc.maxStreamID || sc.req.stream != nil { 2815 2816 return http2ConnectionError(http2ErrCodeProtocol) 2817 } 2818 if id > sc.maxStreamID { 2819 sc.maxStreamID = id 2820 } 2821 st := &http2stream{ 2822 id: id, 2823 state: http2stateOpen, 2824 } 2825 if f.StreamEnded() { 2826 st.state = http2stateHalfClosedRemote 2827 } 2828 st.cw.Init() 2829 2830 st.flow.conn = &sc.flow 2831 st.flow.add(sc.initialWindowSize) 2832 st.inflow.conn = &sc.inflow 2833 st.inflow.add(http2initialWindowSize) 2834 2835 sc.streams[id] = st 2836 if f.HasPriority() { 2837 http2adjustStreamPriority(sc.streams, st.id, f.Priority) 2838 } 2839 sc.curOpenStreams++ 2840 sc.req = http2requestParam{ 2841 stream: st, 2842 header: make(Header), 2843 } 2844 sc.hpackDecoder.SetEmitEnabled(true) 2845 return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded()) 2846 } 2847 2848 func (sc *http2serverConn) processContinuation(f *http2ContinuationFrame) error { 2849 sc.serveG.check() 2850 st := sc.streams[f.Header().StreamID] 2851 if st == nil || sc.curHeaderStreamID() != st.id { 2852 return http2ConnectionError(http2ErrCodeProtocol) 2853 } 2854 return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded()) 2855 } 2856 2857 func (sc *http2serverConn) processHeaderBlockFragment(st *http2stream, frag []byte, end bool) error { 2858 sc.serveG.check() 2859 if _, err := sc.hpackDecoder.Write(frag); err != nil { 2860 return http2ConnectionError(http2ErrCodeCompression) 2861 } 2862 if !end { 2863 return nil 2864 } 2865 if err := sc.hpackDecoder.Close(); err != nil { 2866 return http2ConnectionError(http2ErrCodeCompression) 2867 } 2868 defer sc.resetPendingRequest() 2869 if sc.curOpenStreams > sc.advMaxStreams { 2870 2871 if sc.unackedSettings == 0 { 2872 2873 return http2StreamError{st.id, http2ErrCodeProtocol} 2874 } 2875 2876 return http2StreamError{st.id, http2ErrCodeRefusedStream} 2877 } 2878 2879 rw, req, err := sc.newWriterAndRequest() 2880 if err != nil { 2881 return err 2882 } 2883 st.body = req.Body.(*http2requestBody).pipe 2884 st.declBodyBytes = req.ContentLength 2885 2886 handler := sc.handler.ServeHTTP 2887 if !sc.hpackDecoder.EmitEnabled() { 2888 2889 handler = http2handleHeaderListTooLong 2890 } 2891 2892 go sc.runHandler(rw, req, handler) 2893 return nil 2894 } 2895 2896 func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error { 2897 http2adjustStreamPriority(sc.streams, f.StreamID, f.http2PriorityParam) 2898 return nil 2899 } 2900 2901 func http2adjustStreamPriority(streams map[uint32]*http2stream, streamID uint32, priority http2PriorityParam) { 2902 st, ok := streams[streamID] 2903 if !ok { 2904 2905 return 2906 } 2907 st.weight = priority.Weight 2908 parent := streams[priority.StreamDep] 2909 if parent == st { 2910 2911 return 2912 } 2913 2914 for piter := parent; piter != nil; piter = piter.parent { 2915 if piter == st { 2916 parent.parent = st.parent 2917 break 2918 } 2919 } 2920 st.parent = parent 2921 if priority.Exclusive && (st.parent != nil || priority.StreamDep == 0) { 2922 for _, openStream := range streams { 2923 if openStream != st && openStream.parent == st.parent { 2924 openStream.parent = st 2925 } 2926 } 2927 } 2928 } 2929 2930 // resetPendingRequest zeros out all state related to a HEADERS frame 2931 // and its zero or more CONTINUATION frames sent to start a new 2932 // request. 2933 func (sc *http2serverConn) resetPendingRequest() { 2934 sc.serveG.check() 2935 sc.req = http2requestParam{} 2936 } 2937 2938 func (sc *http2serverConn) newWriterAndRequest() (*http2responseWriter, *Request, error) { 2939 sc.serveG.check() 2940 rp := &sc.req 2941 if rp.invalidHeader || rp.method == "" || rp.path == "" || 2942 (rp.scheme != "https" && rp.scheme != "http") { 2943 2944 return nil, nil, http2StreamError{rp.stream.id, http2ErrCodeProtocol} 2945 } 2946 var tlsState *tls.ConnectionState // nil if not scheme https 2947 if rp.scheme == "https" { 2948 tlsState = sc.tlsState 2949 } 2950 authority := rp.authority 2951 if authority == "" { 2952 authority = rp.header.Get("Host") 2953 } 2954 needsContinue := rp.header.Get("Expect") == "100-continue" 2955 if needsContinue { 2956 rp.header.Del("Expect") 2957 } 2958 2959 if cookies := rp.header["Cookie"]; len(cookies) > 1 { 2960 rp.header.Set("Cookie", strings.Join(cookies, "; ")) 2961 } 2962 bodyOpen := rp.stream.state == http2stateOpen 2963 body := &http2requestBody{ 2964 conn: sc, 2965 stream: rp.stream, 2966 needsContinue: needsContinue, 2967 } 2968 2969 url, err := url.ParseRequestURI(rp.path) 2970 if err != nil { 2971 2972 return nil, nil, http2StreamError{rp.stream.id, http2ErrCodeProtocol} 2973 } 2974 req := &Request{ 2975 Method: rp.method, 2976 URL: url, 2977 RemoteAddr: sc.remoteAddrStr, 2978 Header: rp.header, 2979 RequestURI: rp.path, 2980 Proto: "HTTP/2.0", 2981 ProtoMajor: 2, 2982 ProtoMinor: 0, 2983 TLS: tlsState, 2984 Host: authority, 2985 Body: body, 2986 } 2987 if bodyOpen { 2988 body.pipe = &http2pipe{ 2989 b: http2buffer{buf: make([]byte, http2initialWindowSize)}, 2990 } 2991 body.pipe.c.L = &body.pipe.m 2992 2993 if vv, ok := rp.header["Content-Length"]; ok { 2994 req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64) 2995 } else { 2996 req.ContentLength = -1 2997 } 2998 } 2999 3000 rws := http2responseWriterStatePool.Get().(*http2responseWriterState) 3001 bwSave := rws.bw 3002 *rws = http2responseWriterState{} 3003 rws.conn = sc 3004 rws.bw = bwSave 3005 rws.bw.Reset(http2chunkWriter{rws}) 3006 rws.stream = rp.stream 3007 rws.req = req 3008 rws.body = body 3009 3010 rw := &http2responseWriter{rws: rws} 3011 return rw, req, nil 3012 } 3013 3014 // Run on its own goroutine. 3015 func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) { 3016 defer rw.handlerDone() 3017 3018 handler(rw, req) 3019 } 3020 3021 func http2handleHeaderListTooLong(w ResponseWriter, r *Request) { 3022 // 10.5.1 Limits on Header Block Size: 3023 // .. "A server that receives a larger header block than it is 3024 // willing to handle can send an HTTP 431 (Request Header Fields Too 3025 // Large) status code" 3026 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 3027 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 3028 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 3029 } 3030 3031 // called from handler goroutines. 3032 // h may be nil. 3033 func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) { 3034 sc.serveG.checkNotOn() 3035 var errc chan error 3036 if headerData.h != nil { 3037 3038 errc = http2errChanPool.Get().(chan error) 3039 } 3040 sc.writeFrameFromHandler(http2frameWriteMsg{ 3041 write: headerData, 3042 stream: st, 3043 done: errc, 3044 }) 3045 if errc != nil { 3046 select { 3047 case <-errc: 3048 3049 http2errChanPool.Put(errc) 3050 case <-sc.doneServing: 3051 3052 case <-st.cw: 3053 3054 } 3055 } 3056 } 3057 3058 // called from handler goroutines. 3059 func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) { 3060 sc.writeFrameFromHandler(http2frameWriteMsg{ 3061 write: http2write100ContinueHeadersFrame{st.id}, 3062 stream: st, 3063 }) 3064 } 3065 3066 // A bodyReadMsg tells the server loop that the http.Handler read n 3067 // bytes of the DATA from the client on the given stream. 3068 type http2bodyReadMsg struct { 3069 st *http2stream 3070 n int 3071 } 3072 3073 // called from handler goroutines. 3074 // Notes that the handler for the given stream ID read n bytes of its body 3075 // and schedules flow control tokens to be sent. 3076 func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int) { 3077 sc.serveG.checkNotOn() 3078 sc.bodyReadCh <- http2bodyReadMsg{st, n} 3079 } 3080 3081 func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) { 3082 sc.serveG.check() 3083 sc.sendWindowUpdate(nil, n) 3084 if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed { 3085 3086 sc.sendWindowUpdate(st, n) 3087 } 3088 } 3089 3090 // st may be nil for conn-level 3091 func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) { 3092 sc.serveG.check() 3093 // "The legal range for the increment to the flow control 3094 // window is 1 to 2^31-1 (2,147,483,647) octets." 3095 // A Go Read call on 64-bit machines could in theory read 3096 // a larger Read than this. Very unlikely, but we handle it here 3097 // rather than elsewhere for now. 3098 const maxUint31 = 1<<31 - 1 3099 for n >= maxUint31 { 3100 sc.sendWindowUpdate32(st, maxUint31) 3101 n -= maxUint31 3102 } 3103 sc.sendWindowUpdate32(st, int32(n)) 3104 } 3105 3106 // st may be nil for conn-level 3107 func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) { 3108 sc.serveG.check() 3109 if n == 0 { 3110 return 3111 } 3112 if n < 0 { 3113 panic("negative update") 3114 } 3115 var streamID uint32 3116 if st != nil { 3117 streamID = st.id 3118 } 3119 sc.writeFrame(http2frameWriteMsg{ 3120 write: http2writeWindowUpdate{streamID: streamID, n: uint32(n)}, 3121 stream: st, 3122 }) 3123 var ok bool 3124 if st == nil { 3125 ok = sc.inflow.add(n) 3126 } else { 3127 ok = st.inflow.add(n) 3128 } 3129 if !ok { 3130 panic("internal error; sent too many window updates without decrements?") 3131 } 3132 } 3133 3134 type http2requestBody struct { 3135 stream *http2stream 3136 conn *http2serverConn 3137 closed bool 3138 pipe *http2pipe // non-nil if we have a HTTP entity message body 3139 needsContinue bool // need to send a 100-continue 3140 } 3141 3142 func (b *http2requestBody) Close() error { 3143 if b.pipe != nil { 3144 b.pipe.Close(http2errClosedBody) 3145 } 3146 b.closed = true 3147 return nil 3148 } 3149 3150 func (b *http2requestBody) Read(p []byte) (n int, err error) { 3151 if b.needsContinue { 3152 b.needsContinue = false 3153 b.conn.write100ContinueHeaders(b.stream) 3154 } 3155 if b.pipe == nil { 3156 return 0, io.EOF 3157 } 3158 n, err = b.pipe.Read(p) 3159 if n > 0 { 3160 b.conn.noteBodyReadFromHandler(b.stream, n) 3161 } 3162 return 3163 } 3164 3165 // responseWriter is the http.ResponseWriter implementation. It's 3166 // intentionally small (1 pointer wide) to minimize garbage. The 3167 // responseWriterState pointer inside is zeroed at the end of a 3168 // request (in handlerDone) and calls on the responseWriter thereafter 3169 // simply crash (caller's mistake), but the much larger responseWriterState 3170 // and buffers are reused between multiple requests. 3171 type http2responseWriter struct { 3172 rws *http2responseWriterState 3173 } 3174 3175 // Optional http.ResponseWriter interfaces implemented. 3176 var ( 3177 _ CloseNotifier = (*http2responseWriter)(nil) 3178 _ Flusher = (*http2responseWriter)(nil) 3179 _ http2stringWriter = (*http2responseWriter)(nil) 3180 ) 3181 3182 type http2responseWriterState struct { 3183 // immutable within a request: 3184 stream *http2stream 3185 req *Request 3186 body *http2requestBody // to close at end of request, if DATA frames didn't 3187 conn *http2serverConn 3188 3189 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 3190 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 3191 3192 // mutated by http.Handler goroutine: 3193 handlerHeader Header // nil until called 3194 snapHeader Header // snapshot of handlerHeader at WriteHeader time 3195 status int // status code passed to WriteHeader 3196 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 3197 sentHeader bool // have we sent the header frame? 3198 handlerDone bool // handler has finished 3199 curWrite http2writeData 3200 3201 closeNotifierMu sync.Mutex // guards closeNotifierCh 3202 closeNotifierCh chan bool // nil until first used 3203 } 3204 3205 type http2chunkWriter struct{ rws *http2responseWriterState } 3206 3207 func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 3208 3209 // writeChunk writes chunks from the bufio.Writer. But because 3210 // bufio.Writer may bypass its chunking, sometimes p may be 3211 // arbitrarily large. 3212 // 3213 // writeChunk is also responsible (on the first chunk) for sending the 3214 // HEADER response. 3215 func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) { 3216 if !rws.wroteHeader { 3217 rws.writeHeader(200) 3218 } 3219 if !rws.sentHeader { 3220 rws.sentHeader = true 3221 var ctype, clen string // implicit ones, if we can calculate it 3222 if rws.handlerDone && rws.snapHeader.Get("Content-Length") == "" { 3223 clen = strconv.Itoa(len(p)) 3224 } 3225 if rws.snapHeader.Get("Content-Type") == "" { 3226 ctype = DetectContentType(p) 3227 } 3228 endStream := rws.handlerDone && len(p) == 0 3229 rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 3230 streamID: rws.stream.id, 3231 httpResCode: rws.status, 3232 h: rws.snapHeader, 3233 endStream: endStream, 3234 contentType: ctype, 3235 contentLength: clen, 3236 }) 3237 if endStream { 3238 return 0, nil 3239 } 3240 } 3241 if len(p) == 0 && !rws.handlerDone { 3242 return 0, nil 3243 } 3244 curWrite := &rws.curWrite 3245 curWrite.streamID = rws.stream.id 3246 curWrite.p = p 3247 curWrite.endStream = rws.handlerDone 3248 if err := rws.conn.writeDataFromHandler(rws.stream, curWrite); err != nil { 3249 return 0, err 3250 } 3251 return len(p), nil 3252 } 3253 3254 func (w *http2responseWriter) Flush() { 3255 rws := w.rws 3256 if rws == nil { 3257 panic("Header called after Handler finished") 3258 } 3259 if rws.bw.Buffered() > 0 { 3260 if err := rws.bw.Flush(); err != nil { 3261 3262 return 3263 } 3264 } else { 3265 3266 rws.writeChunk(nil) 3267 } 3268 } 3269 3270 func (w *http2responseWriter) CloseNotify() <-chan bool { 3271 rws := w.rws 3272 if rws == nil { 3273 panic("CloseNotify called after Handler finished") 3274 } 3275 rws.closeNotifierMu.Lock() 3276 ch := rws.closeNotifierCh 3277 if ch == nil { 3278 ch = make(chan bool, 1) 3279 rws.closeNotifierCh = ch 3280 go func() { 3281 rws.stream.cw.Wait() 3282 ch <- true 3283 }() 3284 } 3285 rws.closeNotifierMu.Unlock() 3286 return ch 3287 } 3288 3289 func (w *http2responseWriter) Header() Header { 3290 rws := w.rws 3291 if rws == nil { 3292 panic("Header called after Handler finished") 3293 } 3294 if rws.handlerHeader == nil { 3295 rws.handlerHeader = make(Header) 3296 } 3297 return rws.handlerHeader 3298 } 3299 3300 func (w *http2responseWriter) WriteHeader(code int) { 3301 rws := w.rws 3302 if rws == nil { 3303 panic("WriteHeader called after Handler finished") 3304 } 3305 rws.writeHeader(code) 3306 } 3307 3308 func (rws *http2responseWriterState) writeHeader(code int) { 3309 if !rws.wroteHeader { 3310 rws.wroteHeader = true 3311 rws.status = code 3312 if len(rws.handlerHeader) > 0 { 3313 rws.snapHeader = http2cloneHeader(rws.handlerHeader) 3314 } 3315 } 3316 } 3317 3318 func http2cloneHeader(h Header) Header { 3319 h2 := make(Header, len(h)) 3320 for k, vv := range h { 3321 vv2 := make([]string, len(vv)) 3322 copy(vv2, vv) 3323 h2[k] = vv2 3324 } 3325 return h2 3326 } 3327 3328 // The Life Of A Write is like this: 3329 // 3330 // * Handler calls w.Write or w.WriteString -> 3331 // * -> rws.bw (*bufio.Writer) -> 3332 // * (Handler migth call Flush) 3333 // * -> chunkWriter{rws} 3334 // * -> responseWriterState.writeChunk(p []byte) 3335 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 3336 func (w *http2responseWriter) Write(p []byte) (n int, err error) { 3337 return w.write(len(p), p, "") 3338 } 3339 3340 func (w *http2responseWriter) WriteString(s string) (n int, err error) { 3341 return w.write(len(s), nil, s) 3342 } 3343 3344 // either dataB or dataS is non-zero. 3345 func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 3346 rws := w.rws 3347 if rws == nil { 3348 panic("Write called after Handler finished") 3349 } 3350 if !rws.wroteHeader { 3351 w.WriteHeader(200) 3352 } 3353 if dataB != nil { 3354 return rws.bw.Write(dataB) 3355 } else { 3356 return rws.bw.WriteString(dataS) 3357 } 3358 } 3359 3360 func (w *http2responseWriter) handlerDone() { 3361 rws := w.rws 3362 if rws == nil { 3363 panic("handlerDone called twice") 3364 } 3365 rws.handlerDone = true 3366 w.Flush() 3367 w.rws = nil 3368 http2responseWriterStatePool.Put(rws) 3369 } 3370 3371 type http2Transport struct { 3372 Fallback RoundTripper 3373 3374 // TODO: remove this and make more general with a TLS dial hook, like http 3375 InsecureTLSDial bool 3376 3377 connMu sync.Mutex 3378 conns map[string][]*http2clientConn // key is host:port 3379 } 3380 3381 type http2clientConn struct { 3382 t *http2Transport 3383 tconn *tls.Conn 3384 tlsState *tls.ConnectionState 3385 connKey []string // key(s) this connection is cached in, in t.conns 3386 3387 readerDone chan struct{} // closed on error 3388 readerErr error // set before readerDone is closed 3389 hdec *hpack.Decoder 3390 nextRes *Response 3391 3392 mu sync.Mutex 3393 closed bool 3394 goAway *http2GoAwayFrame // if non-nil, the GoAwayFrame we received 3395 streams map[uint32]*http2clientStream 3396 nextStreamID uint32 3397 bw *bufio.Writer 3398 werr error // first write error that has occurred 3399 br *bufio.Reader 3400 fr *http2Framer 3401 // Settings from peer: 3402 maxFrameSize uint32 3403 maxConcurrentStreams uint32 3404 initialWindowSize uint32 3405 hbuf bytes.Buffer // HPACK encoder writes into this 3406 henc *hpack.Encoder 3407 } 3408 3409 type http2clientStream struct { 3410 ID uint32 3411 resc chan http2resAndError 3412 pw *io.PipeWriter 3413 pr *io.PipeReader 3414 } 3415 3416 type http2stickyErrWriter struct { 3417 w io.Writer 3418 err *error 3419 } 3420 3421 func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) { 3422 if *sew.err != nil { 3423 return 0, *sew.err 3424 } 3425 n, err = sew.w.Write(p) 3426 *sew.err = err 3427 return 3428 } 3429 3430 func (t *http2Transport) RoundTrip(req *Request) (*Response, error) { 3431 if req.URL.Scheme != "https" { 3432 if t.Fallback == nil { 3433 return nil, errors.New("http2: unsupported scheme and no Fallback") 3434 } 3435 return t.Fallback.RoundTrip(req) 3436 } 3437 3438 host, port, err := net.SplitHostPort(req.URL.Host) 3439 if err != nil { 3440 host = req.URL.Host 3441 port = "443" 3442 } 3443 3444 for { 3445 cc, err := t.getClientConn(host, port) 3446 if err != nil { 3447 return nil, err 3448 } 3449 res, err := cc.roundTrip(req) 3450 if http2shouldRetryRequest(err) { 3451 continue 3452 } 3453 if err != nil { 3454 return nil, err 3455 } 3456 return res, nil 3457 } 3458 } 3459 3460 // CloseIdleConnections closes any connections which were previously 3461 // connected from previous requests but are now sitting idle. 3462 // It does not interrupt any connections currently in use. 3463 func (t *http2Transport) CloseIdleConnections() { 3464 t.connMu.Lock() 3465 defer t.connMu.Unlock() 3466 for _, vv := range t.conns { 3467 for _, cc := range vv { 3468 cc.closeIfIdle() 3469 } 3470 } 3471 } 3472 3473 var http2errClientConnClosed = errors.New("http2: client conn is closed") 3474 3475 func http2shouldRetryRequest(err error) bool { 3476 3477 return err == http2errClientConnClosed 3478 } 3479 3480 func (t *http2Transport) removeClientConn(cc *http2clientConn) { 3481 t.connMu.Lock() 3482 defer t.connMu.Unlock() 3483 for _, key := range cc.connKey { 3484 vv, ok := t.conns[key] 3485 if !ok { 3486 continue 3487 } 3488 newList := http2filterOutClientConn(vv, cc) 3489 if len(newList) > 0 { 3490 t.conns[key] = newList 3491 } else { 3492 delete(t.conns, key) 3493 } 3494 } 3495 } 3496 3497 func http2filterOutClientConn(in []*http2clientConn, exclude *http2clientConn) []*http2clientConn { 3498 out := in[:0] 3499 for _, v := range in { 3500 if v != exclude { 3501 out = append(out, v) 3502 } 3503 } 3504 return out 3505 } 3506 3507 func (t *http2Transport) getClientConn(host, port string) (*http2clientConn, error) { 3508 t.connMu.Lock() 3509 defer t.connMu.Unlock() 3510 3511 key := net.JoinHostPort(host, port) 3512 3513 for _, cc := range t.conns[key] { 3514 if cc.canTakeNewRequest() { 3515 return cc, nil 3516 } 3517 } 3518 if t.conns == nil { 3519 t.conns = make(map[string][]*http2clientConn) 3520 } 3521 cc, err := t.newClientConn(host, port, key) 3522 if err != nil { 3523 return nil, err 3524 } 3525 t.conns[key] = append(t.conns[key], cc) 3526 return cc, nil 3527 } 3528 3529 func (t *http2Transport) newClientConn(host, port, key string) (*http2clientConn, error) { 3530 cfg := &tls.Config{ 3531 ServerName: host, 3532 NextProtos: []string{http2NextProtoTLS}, 3533 InsecureSkipVerify: t.InsecureTLSDial, 3534 } 3535 tconn, err := tls.Dial("tcp", net.JoinHostPort(host, port), cfg) 3536 if err != nil { 3537 return nil, err 3538 } 3539 if err := tconn.Handshake(); err != nil { 3540 return nil, err 3541 } 3542 if !t.InsecureTLSDial { 3543 if err := tconn.VerifyHostname(cfg.ServerName); err != nil { 3544 return nil, err 3545 } 3546 } 3547 state := tconn.ConnectionState() 3548 if p := state.NegotiatedProtocol; p != http2NextProtoTLS { 3549 3550 return nil, fmt.Errorf("bad protocol: %v", p) 3551 } 3552 if !state.NegotiatedProtocolIsMutual { 3553 return nil, errors.New("could not negotiate protocol mutually") 3554 } 3555 if _, err := tconn.Write(http2clientPreface); err != nil { 3556 return nil, err 3557 } 3558 3559 cc := &http2clientConn{ 3560 t: t, 3561 tconn: tconn, 3562 connKey: []string{key}, 3563 tlsState: &state, 3564 readerDone: make(chan struct{}), 3565 nextStreamID: 1, 3566 maxFrameSize: 16 << 10, 3567 initialWindowSize: 65535, 3568 maxConcurrentStreams: 1000, 3569 streams: make(map[uint32]*http2clientStream), 3570 } 3571 cc.bw = bufio.NewWriter(http2stickyErrWriter{tconn, &cc.werr}) 3572 cc.br = bufio.NewReader(tconn) 3573 cc.fr = http2NewFramer(cc.bw, cc.br) 3574 cc.henc = hpack.NewEncoder(&cc.hbuf) 3575 3576 cc.fr.WriteSettings() 3577 3578 cc.fr.WriteWindowUpdate(0, 1<<30) 3579 cc.bw.Flush() 3580 if cc.werr != nil { 3581 return nil, cc.werr 3582 } 3583 3584 f, err := cc.fr.ReadFrame() 3585 if err != nil { 3586 return nil, err 3587 } 3588 sf, ok := f.(*http2SettingsFrame) 3589 if !ok { 3590 return nil, fmt.Errorf("expected settings frame, got: %T", f) 3591 } 3592 cc.fr.WriteSettingsAck() 3593 cc.bw.Flush() 3594 3595 sf.ForeachSetting(func(s http2Setting) error { 3596 switch s.ID { 3597 case http2SettingMaxFrameSize: 3598 cc.maxFrameSize = s.Val 3599 case http2SettingMaxConcurrentStreams: 3600 cc.maxConcurrentStreams = s.Val 3601 case http2SettingInitialWindowSize: 3602 cc.initialWindowSize = s.Val 3603 default: 3604 3605 log.Printf("Unhandled Setting: %v", s) 3606 } 3607 return nil 3608 }) 3609 3610 cc.hdec = hpack.NewDecoder(http2initialHeaderTableSize, cc.onNewHeaderField) 3611 3612 go cc.readLoop() 3613 return cc, nil 3614 } 3615 3616 func (cc *http2clientConn) setGoAway(f *http2GoAwayFrame) { 3617 cc.mu.Lock() 3618 defer cc.mu.Unlock() 3619 cc.goAway = f 3620 } 3621 3622 func (cc *http2clientConn) canTakeNewRequest() bool { 3623 cc.mu.Lock() 3624 defer cc.mu.Unlock() 3625 return cc.goAway == nil && 3626 int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) && 3627 cc.nextStreamID < 2147483647 3628 } 3629 3630 func (cc *http2clientConn) closeIfIdle() { 3631 cc.mu.Lock() 3632 if len(cc.streams) > 0 { 3633 cc.mu.Unlock() 3634 return 3635 } 3636 cc.closed = true 3637 3638 cc.mu.Unlock() 3639 3640 cc.tconn.Close() 3641 } 3642 3643 func (cc *http2clientConn) roundTrip(req *Request) (*Response, error) { 3644 cc.mu.Lock() 3645 3646 if cc.closed { 3647 cc.mu.Unlock() 3648 return nil, http2errClientConnClosed 3649 } 3650 3651 cs := cc.newStream() 3652 hasBody := false 3653 3654 hdrs := cc.encodeHeaders(req) 3655 first := true 3656 for len(hdrs) > 0 { 3657 chunk := hdrs 3658 if len(chunk) > int(cc.maxFrameSize) { 3659 chunk = chunk[:cc.maxFrameSize] 3660 } 3661 hdrs = hdrs[len(chunk):] 3662 endHeaders := len(hdrs) == 0 3663 if first { 3664 cc.fr.WriteHeaders(http2HeadersFrameParam{ 3665 StreamID: cs.ID, 3666 BlockFragment: chunk, 3667 EndStream: !hasBody, 3668 EndHeaders: endHeaders, 3669 }) 3670 first = false 3671 } else { 3672 cc.fr.WriteContinuation(cs.ID, endHeaders, chunk) 3673 } 3674 } 3675 cc.bw.Flush() 3676 werr := cc.werr 3677 cc.mu.Unlock() 3678 3679 if hasBody { 3680 3681 } 3682 3683 if werr != nil { 3684 return nil, werr 3685 } 3686 3687 re := <-cs.resc 3688 if re.err != nil { 3689 return nil, re.err 3690 } 3691 res := re.res 3692 res.Request = req 3693 res.TLS = cc.tlsState 3694 return res, nil 3695 } 3696 3697 // requires cc.mu be held. 3698 func (cc *http2clientConn) encodeHeaders(req *Request) []byte { 3699 cc.hbuf.Reset() 3700 3701 host := req.Host 3702 if host == "" { 3703 host = req.URL.Host 3704 } 3705 3706 path := req.URL.Path 3707 if path == "" { 3708 path = "/" 3709 } 3710 3711 cc.writeHeader(":authority", host) 3712 cc.writeHeader(":method", req.Method) 3713 cc.writeHeader(":path", path) 3714 cc.writeHeader(":scheme", "https") 3715 3716 for k, vv := range req.Header { 3717 lowKey := strings.ToLower(k) 3718 if lowKey == "host" { 3719 continue 3720 } 3721 for _, v := range vv { 3722 cc.writeHeader(lowKey, v) 3723 } 3724 } 3725 return cc.hbuf.Bytes() 3726 } 3727 3728 func (cc *http2clientConn) writeHeader(name, value string) { 3729 log.Printf("sending %q = %q", name, value) 3730 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 3731 } 3732 3733 type http2resAndError struct { 3734 res *Response 3735 err error 3736 } 3737 3738 // requires cc.mu be held. 3739 func (cc *http2clientConn) newStream() *http2clientStream { 3740 cs := &http2clientStream{ 3741 ID: cc.nextStreamID, 3742 resc: make(chan http2resAndError, 1), 3743 } 3744 cc.nextStreamID += 2 3745 cc.streams[cs.ID] = cs 3746 return cs 3747 } 3748 3749 func (cc *http2clientConn) streamByID(id uint32, andRemove bool) *http2clientStream { 3750 cc.mu.Lock() 3751 defer cc.mu.Unlock() 3752 cs := cc.streams[id] 3753 if andRemove { 3754 delete(cc.streams, id) 3755 } 3756 return cs 3757 } 3758 3759 // runs in its own goroutine. 3760 func (cc *http2clientConn) readLoop() { 3761 defer cc.t.removeClientConn(cc) 3762 defer close(cc.readerDone) 3763 3764 activeRes := map[uint32]*http2clientStream{} 3765 3766 defer func() { 3767 err := cc.readerErr 3768 if err == io.EOF { 3769 err = io.ErrUnexpectedEOF 3770 } 3771 for _, cs := range activeRes { 3772 cs.pw.CloseWithError(err) 3773 } 3774 }() 3775 3776 // continueStreamID is the stream ID we're waiting for 3777 // continuation frames for. 3778 var continueStreamID uint32 3779 3780 for { 3781 f, err := cc.fr.ReadFrame() 3782 if err != nil { 3783 cc.readerErr = err 3784 return 3785 } 3786 log.Printf("Transport received %v: %#v", f.Header(), f) 3787 3788 streamID := f.Header().StreamID 3789 3790 _, isContinue := f.(*http2ContinuationFrame) 3791 if isContinue { 3792 if streamID != continueStreamID { 3793 log.Printf("Protocol violation: got CONTINUATION with id %d; want %d", streamID, continueStreamID) 3794 cc.readerErr = http2ConnectionError(http2ErrCodeProtocol) 3795 return 3796 } 3797 } else if continueStreamID != 0 { 3798 3799 log.Printf("Protocol violation: got %T for stream %d, want CONTINUATION for %d", f, streamID, continueStreamID) 3800 cc.readerErr = http2ConnectionError(http2ErrCodeProtocol) 3801 return 3802 } 3803 3804 if streamID%2 == 0 { 3805 3806 continue 3807 } 3808 streamEnded := false 3809 if ff, ok := f.(http2streamEnder); ok { 3810 streamEnded = ff.StreamEnded() 3811 } 3812 3813 cs := cc.streamByID(streamID, streamEnded) 3814 if cs == nil { 3815 log.Printf("Received frame for untracked stream ID %d", streamID) 3816 continue 3817 } 3818 3819 switch f := f.(type) { 3820 case *http2HeadersFrame: 3821 cc.nextRes = &Response{ 3822 Proto: "HTTP/2.0", 3823 ProtoMajor: 2, 3824 Header: make(Header), 3825 } 3826 cs.pr, cs.pw = io.Pipe() 3827 cc.hdec.Write(f.HeaderBlockFragment()) 3828 case *http2ContinuationFrame: 3829 cc.hdec.Write(f.HeaderBlockFragment()) 3830 case *http2DataFrame: 3831 log.Printf("DATA: %q", f.Data()) 3832 cs.pw.Write(f.Data()) 3833 case *http2GoAwayFrame: 3834 cc.t.removeClientConn(cc) 3835 if f.ErrCode != 0 { 3836 3837 log.Printf("transport got GOAWAY with error code = %v", f.ErrCode) 3838 } 3839 cc.setGoAway(f) 3840 default: 3841 log.Printf("Transport: unhandled response frame type %T", f) 3842 } 3843 headersEnded := false 3844 if he, ok := f.(http2headersEnder); ok { 3845 headersEnded = he.HeadersEnded() 3846 if headersEnded { 3847 continueStreamID = 0 3848 } else { 3849 continueStreamID = streamID 3850 } 3851 } 3852 3853 if streamEnded { 3854 cs.pw.Close() 3855 delete(activeRes, streamID) 3856 } 3857 if headersEnded { 3858 if cs == nil { 3859 panic("couldn't find stream") 3860 } 3861 3862 cc.nextRes.Body = cs.pr 3863 res := cc.nextRes 3864 activeRes[streamID] = cs 3865 cs.resc <- http2resAndError{res: res} 3866 } 3867 } 3868 } 3869 3870 func (cc *http2clientConn) onNewHeaderField(f hpack.HeaderField) { 3871 3872 log.Printf("Header field: %+v", f) 3873 if f.Name == ":status" { 3874 code, err := strconv.Atoi(f.Value) 3875 if err != nil { 3876 panic("TODO: be graceful") 3877 } 3878 cc.nextRes.Status = f.Value + " " + StatusText(code) 3879 cc.nextRes.StatusCode = code 3880 return 3881 } 3882 if strings.HasPrefix(f.Name, ":") { 3883 3884 return 3885 } 3886 cc.nextRes.Header.Add(CanonicalHeaderKey(f.Name), f.Value) 3887 } 3888 3889 // writeFramer is implemented by any type that is used to write frames. 3890 type http2writeFramer interface { 3891 writeFrame(http2writeContext) error 3892 } 3893 3894 // writeContext is the interface needed by the various frame writer 3895 // types below. All the writeFrame methods below are scheduled via the 3896 // frame writing scheduler (see writeScheduler in writesched.go). 3897 // 3898 // This interface is implemented by *serverConn. 3899 // TODO: use it from the client code too, once it exists. 3900 type http2writeContext interface { 3901 Framer() *http2Framer 3902 Flush() error 3903 CloseConn() error 3904 // HeaderEncoder returns an HPACK encoder that writes to the 3905 // returned buffer. 3906 HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) 3907 } 3908 3909 // endsStream reports whether the given frame writer w will locally 3910 // close the stream. 3911 func http2endsStream(w http2writeFramer) bool { 3912 switch v := w.(type) { 3913 case *http2writeData: 3914 return v.endStream 3915 case *http2writeResHeaders: 3916 return v.endStream 3917 } 3918 return false 3919 } 3920 3921 type http2flushFrameWriter struct{} 3922 3923 func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error { 3924 return ctx.Flush() 3925 } 3926 3927 type http2writeSettings []http2Setting 3928 3929 func (s http2writeSettings) writeFrame(ctx http2writeContext) error { 3930 return ctx.Framer().WriteSettings([]http2Setting(s)...) 3931 } 3932 3933 type http2writeGoAway struct { 3934 maxStreamID uint32 3935 code http2ErrCode 3936 } 3937 3938 func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error { 3939 err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil) 3940 if p.code != 0 { 3941 ctx.Flush() 3942 time.Sleep(50 * time.Millisecond) 3943 ctx.CloseConn() 3944 } 3945 return err 3946 } 3947 3948 type http2writeData struct { 3949 streamID uint32 3950 p []byte 3951 endStream bool 3952 } 3953 3954 func (w *http2writeData) String() string { 3955 return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream) 3956 } 3957 3958 func (w *http2writeData) writeFrame(ctx http2writeContext) error { 3959 return ctx.Framer().WriteData(w.streamID, w.endStream, w.p) 3960 } 3961 3962 func (se http2StreamError) writeFrame(ctx http2writeContext) error { 3963 return ctx.Framer().WriteRSTStream(se.StreamID, se.Code) 3964 } 3965 3966 type http2writePingAck struct{ pf *http2PingFrame } 3967 3968 func (w http2writePingAck) writeFrame(ctx http2writeContext) error { 3969 return ctx.Framer().WritePing(true, w.pf.Data) 3970 } 3971 3972 type http2writeSettingsAck struct{} 3973 3974 func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error { 3975 return ctx.Framer().WriteSettingsAck() 3976 } 3977 3978 // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames 3979 // for HTTP response headers from a server handler. 3980 type http2writeResHeaders struct { 3981 streamID uint32 3982 httpResCode int 3983 h Header // may be nil 3984 endStream bool 3985 3986 contentType string 3987 contentLength string 3988 } 3989 3990 func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error { 3991 enc, buf := ctx.HeaderEncoder() 3992 buf.Reset() 3993 enc.WriteField(hpack.HeaderField{Name: ":status", Value: http2httpCodeString(w.httpResCode)}) 3994 for k, vv := range w.h { 3995 k = http2lowerHeader(k) 3996 for _, v := range vv { 3997 3998 if k == "transfer-encoding" && v != "trailers" { 3999 continue 4000 } 4001 enc.WriteField(hpack.HeaderField{Name: k, Value: v}) 4002 } 4003 } 4004 if w.contentType != "" { 4005 enc.WriteField(hpack.HeaderField{Name: "content-type", Value: w.contentType}) 4006 } 4007 if w.contentLength != "" { 4008 enc.WriteField(hpack.HeaderField{Name: "content-length", Value: w.contentLength}) 4009 } 4010 4011 headerBlock := buf.Bytes() 4012 if len(headerBlock) == 0 { 4013 panic("unexpected empty hpack") 4014 } 4015 4016 // For now we're lazy and just pick the minimum MAX_FRAME_SIZE 4017 // that all peers must support (16KB). Later we could care 4018 // more and send larger frames if the peer advertised it, but 4019 // there's little point. Most headers are small anyway (so we 4020 // generally won't have CONTINUATION frames), and extra frames 4021 // only waste 9 bytes anyway. 4022 const maxFrameSize = 16384 4023 4024 first := true 4025 for len(headerBlock) > 0 { 4026 frag := headerBlock 4027 if len(frag) > maxFrameSize { 4028 frag = frag[:maxFrameSize] 4029 } 4030 headerBlock = headerBlock[len(frag):] 4031 endHeaders := len(headerBlock) == 0 4032 var err error 4033 if first { 4034 first = false 4035 err = ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 4036 StreamID: w.streamID, 4037 BlockFragment: frag, 4038 EndStream: w.endStream, 4039 EndHeaders: endHeaders, 4040 }) 4041 } else { 4042 err = ctx.Framer().WriteContinuation(w.streamID, endHeaders, frag) 4043 } 4044 if err != nil { 4045 return err 4046 } 4047 } 4048 return nil 4049 } 4050 4051 type http2write100ContinueHeadersFrame struct { 4052 streamID uint32 4053 } 4054 4055 func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error { 4056 enc, buf := ctx.HeaderEncoder() 4057 buf.Reset() 4058 enc.WriteField(hpack.HeaderField{Name: ":status", Value: "100"}) 4059 return ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 4060 StreamID: w.streamID, 4061 BlockFragment: buf.Bytes(), 4062 EndStream: false, 4063 EndHeaders: true, 4064 }) 4065 } 4066 4067 type http2writeWindowUpdate struct { 4068 streamID uint32 // or 0 for conn-level 4069 n uint32 4070 } 4071 4072 func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error { 4073 return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n) 4074 } 4075 4076 // frameWriteMsg is a request to write a frame. 4077 type http2frameWriteMsg struct { 4078 // write is the interface value that does the writing, once the 4079 // writeScheduler (below) has decided to select this frame 4080 // to write. The write functions are all defined in write.go. 4081 write http2writeFramer 4082 4083 stream *http2stream // used for prioritization. nil for non-stream frames. 4084 4085 // done, if non-nil, must be a buffered channel with space for 4086 // 1 message and is sent the return value from write (or an 4087 // earlier error) when the frame has been written. 4088 done chan error 4089 } 4090 4091 // for debugging only: 4092 func (wm http2frameWriteMsg) String() string { 4093 var streamID uint32 4094 if wm.stream != nil { 4095 streamID = wm.stream.id 4096 } 4097 var des string 4098 if s, ok := wm.write.(fmt.Stringer); ok { 4099 des = s.String() 4100 } else { 4101 des = fmt.Sprintf("%T", wm.write) 4102 } 4103 return fmt.Sprintf("[frameWriteMsg stream=%d, ch=%v, type: %v]", streamID, wm.done != nil, des) 4104 } 4105 4106 // writeScheduler tracks pending frames to write, priorities, and decides 4107 // the next one to use. It is not thread-safe. 4108 type http2writeScheduler struct { 4109 // zero are frames not associated with a specific stream. 4110 // They're sent before any stream-specific freams. 4111 zero http2writeQueue 4112 4113 // maxFrameSize is the maximum size of a DATA frame 4114 // we'll write. Must be non-zero and between 16K-16M. 4115 maxFrameSize uint32 4116 4117 // sq contains the stream-specific queues, keyed by stream ID. 4118 // when a stream is idle, it's deleted from the map. 4119 sq map[uint32]*http2writeQueue 4120 4121 // canSend is a slice of memory that's reused between frame 4122 // scheduling decisions to hold the list of writeQueues (from sq) 4123 // which have enough flow control data to send. After canSend is 4124 // built, the best is selected. 4125 canSend []*http2writeQueue 4126 4127 // pool of empty queues for reuse. 4128 queuePool []*http2writeQueue 4129 } 4130 4131 func (ws *http2writeScheduler) putEmptyQueue(q *http2writeQueue) { 4132 if len(q.s) != 0 { 4133 panic("queue must be empty") 4134 } 4135 ws.queuePool = append(ws.queuePool, q) 4136 } 4137 4138 func (ws *http2writeScheduler) getEmptyQueue() *http2writeQueue { 4139 ln := len(ws.queuePool) 4140 if ln == 0 { 4141 return new(http2writeQueue) 4142 } 4143 q := ws.queuePool[ln-1] 4144 ws.queuePool = ws.queuePool[:ln-1] 4145 return q 4146 } 4147 4148 func (ws *http2writeScheduler) empty() bool { return ws.zero.empty() && len(ws.sq) == 0 } 4149 4150 func (ws *http2writeScheduler) add(wm http2frameWriteMsg) { 4151 st := wm.stream 4152 if st == nil { 4153 ws.zero.push(wm) 4154 } else { 4155 ws.streamQueue(st.id).push(wm) 4156 } 4157 } 4158 4159 func (ws *http2writeScheduler) streamQueue(streamID uint32) *http2writeQueue { 4160 if q, ok := ws.sq[streamID]; ok { 4161 return q 4162 } 4163 if ws.sq == nil { 4164 ws.sq = make(map[uint32]*http2writeQueue) 4165 } 4166 q := ws.getEmptyQueue() 4167 ws.sq[streamID] = q 4168 return q 4169 } 4170 4171 // take returns the most important frame to write and removes it from the scheduler. 4172 // It is illegal to call this if the scheduler is empty or if there are no connection-level 4173 // flow control bytes available. 4174 func (ws *http2writeScheduler) take() (wm http2frameWriteMsg, ok bool) { 4175 if ws.maxFrameSize == 0 { 4176 panic("internal error: ws.maxFrameSize not initialized or invalid") 4177 } 4178 4179 if !ws.zero.empty() { 4180 return ws.zero.shift(), true 4181 } 4182 if len(ws.sq) == 0 { 4183 return 4184 } 4185 4186 for id, q := range ws.sq { 4187 if q.firstIsNoCost() { 4188 return ws.takeFrom(id, q) 4189 } 4190 } 4191 4192 if len(ws.canSend) != 0 { 4193 panic("should be empty") 4194 } 4195 for _, q := range ws.sq { 4196 if n := ws.streamWritableBytes(q); n > 0 { 4197 ws.canSend = append(ws.canSend, q) 4198 } 4199 } 4200 if len(ws.canSend) == 0 { 4201 return 4202 } 4203 defer ws.zeroCanSend() 4204 4205 q := ws.canSend[0] 4206 4207 return ws.takeFrom(q.streamID(), q) 4208 } 4209 4210 // zeroCanSend is defered from take. 4211 func (ws *http2writeScheduler) zeroCanSend() { 4212 for i := range ws.canSend { 4213 ws.canSend[i] = nil 4214 } 4215 ws.canSend = ws.canSend[:0] 4216 } 4217 4218 // streamWritableBytes returns the number of DATA bytes we could write 4219 // from the given queue's stream, if this stream/queue were 4220 // selected. It is an error to call this if q's head isn't a 4221 // *writeData. 4222 func (ws *http2writeScheduler) streamWritableBytes(q *http2writeQueue) int32 { 4223 wm := q.head() 4224 ret := wm.stream.flow.available() 4225 if ret == 0 { 4226 return 0 4227 } 4228 if int32(ws.maxFrameSize) < ret { 4229 ret = int32(ws.maxFrameSize) 4230 } 4231 if ret == 0 { 4232 panic("internal error: ws.maxFrameSize not initialized or invalid") 4233 } 4234 wd := wm.write.(*http2writeData) 4235 if len(wd.p) < int(ret) { 4236 ret = int32(len(wd.p)) 4237 } 4238 return ret 4239 } 4240 4241 func (ws *http2writeScheduler) takeFrom(id uint32, q *http2writeQueue) (wm http2frameWriteMsg, ok bool) { 4242 wm = q.head() 4243 4244 if wd, ok := wm.write.(*http2writeData); ok && len(wd.p) > 0 { 4245 allowed := wm.stream.flow.available() 4246 if allowed == 0 { 4247 4248 return http2frameWriteMsg{}, false 4249 } 4250 if int32(ws.maxFrameSize) < allowed { 4251 allowed = int32(ws.maxFrameSize) 4252 } 4253 4254 if len(wd.p) > int(allowed) { 4255 wm.stream.flow.take(allowed) 4256 chunk := wd.p[:allowed] 4257 wd.p = wd.p[allowed:] 4258 4259 return http2frameWriteMsg{ 4260 stream: wm.stream, 4261 write: &http2writeData{ 4262 streamID: wd.streamID, 4263 p: chunk, 4264 4265 endStream: false, 4266 }, 4267 4268 done: nil, 4269 }, true 4270 } 4271 wm.stream.flow.take(int32(len(wd.p))) 4272 } 4273 4274 q.shift() 4275 if q.empty() { 4276 ws.putEmptyQueue(q) 4277 delete(ws.sq, id) 4278 } 4279 return wm, true 4280 } 4281 4282 func (ws *http2writeScheduler) forgetStream(id uint32) { 4283 q, ok := ws.sq[id] 4284 if !ok { 4285 return 4286 } 4287 delete(ws.sq, id) 4288 4289 for i := range q.s { 4290 q.s[i] = http2frameWriteMsg{} 4291 } 4292 q.s = q.s[:0] 4293 ws.putEmptyQueue(q) 4294 } 4295 4296 type http2writeQueue struct { 4297 s []http2frameWriteMsg 4298 } 4299 4300 // streamID returns the stream ID for a non-empty stream-specific queue. 4301 func (q *http2writeQueue) streamID() uint32 { return q.s[0].stream.id } 4302 4303 func (q *http2writeQueue) empty() bool { return len(q.s) == 0 } 4304 4305 func (q *http2writeQueue) push(wm http2frameWriteMsg) { 4306 q.s = append(q.s, wm) 4307 } 4308 4309 // head returns the next item that would be removed by shift. 4310 func (q *http2writeQueue) head() http2frameWriteMsg { 4311 if len(q.s) == 0 { 4312 panic("invalid use of queue") 4313 } 4314 return q.s[0] 4315 } 4316 4317 func (q *http2writeQueue) shift() http2frameWriteMsg { 4318 if len(q.s) == 0 { 4319 panic("invalid use of queue") 4320 } 4321 wm := q.s[0] 4322 4323 copy(q.s, q.s[1:]) 4324 q.s[len(q.s)-1] = http2frameWriteMsg{} 4325 q.s = q.s[:len(q.s)-1] 4326 return wm 4327 } 4328 4329 func (q *http2writeQueue) firstIsNoCost() bool { 4330 if df, ok := q.s[0].write.(*http2writeData); ok { 4331 return len(df.p) == 0 4332 } 4333 return true 4334 }