github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/http2/transport.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Transport code. 6 7 package http2 8 9 import ( 10 "bufio" 11 "bytes" 12 "compress/gzip" 13 "context" 14 "crypto/rand" 15 "crypto/tls" 16 "errors" 17 "fmt" 18 "io" 19 "io/ioutil" 20 "log" 21 "math" 22 mathrand "math/rand" 23 "net" 24 "net/http" 25 "net/http/httptrace" 26 "net/textproto" 27 "sort" 28 "strconv" 29 "strings" 30 "sync" 31 "sync/atomic" 32 "time" 33 34 "github.com/Andyfoo/golang/x/net/http/httpguts" 35 "github.com/Andyfoo/golang/x/net/http2/hpack" 36 "github.com/Andyfoo/golang/x/net/idna" 37 ) 38 39 const ( 40 // transportDefaultConnFlow is how many connection-level flow control 41 // tokens we give the server at start-up, past the default 64k. 42 transportDefaultConnFlow = 1 << 30 43 44 // transportDefaultStreamFlow is how many stream-level flow 45 // control tokens we announce to the peer, and how many bytes 46 // we buffer per stream. 47 transportDefaultStreamFlow = 4 << 20 48 49 // transportDefaultStreamMinRefresh is the minimum number of bytes we'll send 50 // a stream-level WINDOW_UPDATE for at a time. 51 transportDefaultStreamMinRefresh = 4 << 10 52 53 defaultUserAgent = "Go-http-client/2.0" 54 ) 55 56 // Transport is an HTTP/2 Transport. 57 // 58 // A Transport internally caches connections to servers. It is safe 59 // for concurrent use by multiple goroutines. 60 type Transport struct { 61 // DialTLS specifies an optional dial function for creating 62 // TLS connections for requests. 63 // 64 // If DialTLS is nil, tls.Dial is used. 65 // 66 // If the returned net.Conn has a ConnectionState method like tls.Conn, 67 // it will be used to set http.Response.TLS. 68 DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error) 69 70 // TLSClientConfig specifies the TLS configuration to use with 71 // tls.Client. If nil, the default configuration is used. 72 TLSClientConfig *tls.Config 73 74 // ConnPool optionally specifies an alternate connection pool to use. 75 // If nil, the default is used. 76 ConnPool ClientConnPool 77 78 // DisableCompression, if true, prevents the Transport from 79 // requesting compression with an "Accept-Encoding: gzip" 80 // request header when the Request contains no existing 81 // Accept-Encoding value. If the Transport requests gzip on 82 // its own and gets a gzipped response, it's transparently 83 // decoded in the Response.Body. However, if the user 84 // explicitly requested gzip it is not automatically 85 // uncompressed. 86 DisableCompression bool 87 88 // AllowHTTP, if true, permits HTTP/2 requests using the insecure, 89 // plain-text "http" scheme. Note that this does not enable h2c support. 90 AllowHTTP bool 91 92 // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to 93 // send in the initial settings frame. It is how many bytes 94 // of response headers are allowed. Unlike the http2 spec, zero here 95 // means to use a default limit (currently 10MB). If you actually 96 // want to advertise an ulimited value to the peer, Transport 97 // interprets the highest possible value here (0xffffffff or 1<<32-1) 98 // to mean no limit. 99 MaxHeaderListSize uint32 100 101 // StrictMaxConcurrentStreams controls whether the server's 102 // SETTINGS_MAX_CONCURRENT_STREAMS should be respected 103 // globally. If false, new TCP connections are created to the 104 // server as needed to keep each under the per-connection 105 // SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the 106 // server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as 107 // a global limit and callers of RoundTrip block when needed, 108 // waiting for their turn. 109 StrictMaxConcurrentStreams bool 110 111 // t1, if non-nil, is the standard library Transport using 112 // this transport. Its settings are used (but not its 113 // RoundTrip method, etc). 114 t1 *http.Transport 115 116 connPoolOnce sync.Once 117 connPoolOrDef ClientConnPool // non-nil version of ConnPool 118 } 119 120 func (t *Transport) maxHeaderListSize() uint32 { 121 if t.MaxHeaderListSize == 0 { 122 return 10 << 20 123 } 124 if t.MaxHeaderListSize == 0xffffffff { 125 return 0 126 } 127 return t.MaxHeaderListSize 128 } 129 130 func (t *Transport) disableCompression() bool { 131 return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression) 132 } 133 134 // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2. 135 // It returns an error if t1 has already been HTTP/2-enabled. 136 func ConfigureTransport(t1 *http.Transport) error { 137 _, err := configureTransport(t1) 138 return err 139 } 140 141 func configureTransport(t1 *http.Transport) (*Transport, error) { 142 connPool := new(clientConnPool) 143 t2 := &Transport{ 144 ConnPool: noDialClientConnPool{connPool}, 145 t1: t1, 146 } 147 connPool.t = t2 148 if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil { 149 return nil, err 150 } 151 if t1.TLSClientConfig == nil { 152 t1.TLSClientConfig = new(tls.Config) 153 } 154 if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") { 155 t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...) 156 } 157 if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") { 158 t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1") 159 } 160 upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper { 161 addr := authorityAddr("https", authority) 162 if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil { 163 go c.Close() 164 return erringRoundTripper{err} 165 } else if !used { 166 // Turns out we don't need this c. 167 // For example, two goroutines made requests to the same host 168 // at the same time, both kicking off TCP dials. (since protocol 169 // was unknown) 170 go c.Close() 171 } 172 return t2 173 } 174 if m := t1.TLSNextProto; len(m) == 0 { 175 t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{ 176 "h2": upgradeFn, 177 } 178 } else { 179 m["h2"] = upgradeFn 180 } 181 return t2, nil 182 } 183 184 func (t *Transport) connPool() ClientConnPool { 185 t.connPoolOnce.Do(t.initConnPool) 186 return t.connPoolOrDef 187 } 188 189 func (t *Transport) initConnPool() { 190 if t.ConnPool != nil { 191 t.connPoolOrDef = t.ConnPool 192 } else { 193 t.connPoolOrDef = &clientConnPool{t: t} 194 } 195 } 196 197 // ClientConn is the state of a single HTTP/2 client connection to an 198 // HTTP/2 server. 199 type ClientConn struct { 200 t *Transport 201 tconn net.Conn // usually *tls.Conn, except specialized impls 202 tlsState *tls.ConnectionState // nil only for specialized impls 203 reused uint32 // whether conn is being reused; atomic 204 singleUse bool // whether being used for a single http.Request 205 206 // readLoop goroutine fields: 207 readerDone chan struct{} // closed on error 208 readerErr error // set before readerDone is closed 209 210 idleTimeout time.Duration // or 0 for never 211 idleTimer *time.Timer 212 213 mu sync.Mutex // guards following 214 cond *sync.Cond // hold mu; broadcast on flow/closed changes 215 flow flow // our conn-level flow control quota (cs.flow is per stream) 216 inflow flow // peer's conn-level flow control 217 closing bool 218 closed bool 219 wantSettingsAck bool // we sent a SETTINGS frame and haven't heard back 220 goAway *GoAwayFrame // if non-nil, the GoAwayFrame we received 221 goAwayDebug string // goAway frame's debug data, retained as a string 222 streams map[uint32]*clientStream // client-initiated 223 nextStreamID uint32 224 pendingRequests int // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams 225 pings map[[8]byte]chan struct{} // in flight ping data to notification channel 226 bw *bufio.Writer 227 br *bufio.Reader 228 fr *Framer 229 lastActive time.Time 230 // Settings from peer: (also guarded by mu) 231 maxFrameSize uint32 232 maxConcurrentStreams uint32 233 peerMaxHeaderListSize uint64 234 initialWindowSize uint32 235 236 hbuf bytes.Buffer // HPACK encoder writes into this 237 henc *hpack.Encoder 238 freeBuf [][]byte 239 240 wmu sync.Mutex // held while writing; acquire AFTER mu if holding both 241 werr error // first write error that has occurred 242 } 243 244 // clientStream is the state for a single HTTP/2 stream. One of these 245 // is created for each Transport.RoundTrip call. 246 type clientStream struct { 247 cc *ClientConn 248 req *http.Request 249 trace *httptrace.ClientTrace // or nil 250 ID uint32 251 resc chan resAndError 252 bufPipe pipe // buffered pipe with the flow-controlled response payload 253 startedWrite bool // started request body write; guarded by cc.mu 254 requestedGzip bool 255 on100 func() // optional code to run if get a 100 continue response 256 257 flow flow // guarded by cc.mu 258 inflow flow // guarded by cc.mu 259 bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read 260 readErr error // sticky read error; owned by transportResponseBody.Read 261 stopReqBody error // if non-nil, stop writing req body; guarded by cc.mu 262 didReset bool // whether we sent a RST_STREAM to the server; guarded by cc.mu 263 264 peerReset chan struct{} // closed on peer reset 265 resetErr error // populated before peerReset is closed 266 267 done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu 268 269 // owned by clientConnReadLoop: 270 firstByte bool // got the first response byte 271 pastHeaders bool // got first MetaHeadersFrame (actual headers) 272 pastTrailers bool // got optional second MetaHeadersFrame (trailers) 273 num1xx uint8 // number of 1xx responses seen 274 275 trailer http.Header // accumulated trailers 276 resTrailer *http.Header // client's Response.Trailer 277 } 278 279 // awaitRequestCancel waits for the user to cancel a request or for the done 280 // channel to be signaled. A non-nil error is returned only if the request was 281 // canceled. 282 func awaitRequestCancel(req *http.Request, done <-chan struct{}) error { 283 ctx := req.Context() 284 if req.Cancel == nil && ctx.Done() == nil { 285 return nil 286 } 287 select { 288 case <-req.Cancel: 289 return errRequestCanceled 290 case <-ctx.Done(): 291 return ctx.Err() 292 case <-done: 293 return nil 294 } 295 } 296 297 var got1xxFuncForTests func(int, textproto.MIMEHeader) error 298 299 // get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func, 300 // if any. It returns nil if not set or if the Go version is too old. 301 func (cs *clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error { 302 if fn := got1xxFuncForTests; fn != nil { 303 return fn 304 } 305 return traceGot1xxResponseFunc(cs.trace) 306 } 307 308 // awaitRequestCancel waits for the user to cancel a request, its context to 309 // expire, or for the request to be done (any way it might be removed from the 310 // cc.streams map: peer reset, successful completion, TCP connection breakage, 311 // etc). If the request is canceled, then cs will be canceled and closed. 312 func (cs *clientStream) awaitRequestCancel(req *http.Request) { 313 if err := awaitRequestCancel(req, cs.done); err != nil { 314 cs.cancelStream() 315 cs.bufPipe.CloseWithError(err) 316 } 317 } 318 319 func (cs *clientStream) cancelStream() { 320 cc := cs.cc 321 cc.mu.Lock() 322 didReset := cs.didReset 323 cs.didReset = true 324 cc.mu.Unlock() 325 326 if !didReset { 327 cc.writeStreamReset(cs.ID, ErrCodeCancel, nil) 328 cc.forgetStreamID(cs.ID) 329 } 330 } 331 332 // checkResetOrDone reports any error sent in a RST_STREAM frame by the 333 // server, or errStreamClosed if the stream is complete. 334 func (cs *clientStream) checkResetOrDone() error { 335 select { 336 case <-cs.peerReset: 337 return cs.resetErr 338 case <-cs.done: 339 return errStreamClosed 340 default: 341 return nil 342 } 343 } 344 345 func (cs *clientStream) getStartedWrite() bool { 346 cc := cs.cc 347 cc.mu.Lock() 348 defer cc.mu.Unlock() 349 return cs.startedWrite 350 } 351 352 func (cs *clientStream) abortRequestBodyWrite(err error) { 353 if err == nil { 354 panic("nil error") 355 } 356 cc := cs.cc 357 cc.mu.Lock() 358 cs.stopReqBody = err 359 cc.cond.Broadcast() 360 cc.mu.Unlock() 361 } 362 363 type stickyErrWriter struct { 364 w io.Writer 365 err *error 366 } 367 368 func (sew stickyErrWriter) Write(p []byte) (n int, err error) { 369 if *sew.err != nil { 370 return 0, *sew.err 371 } 372 n, err = sew.w.Write(p) 373 *sew.err = err 374 return 375 } 376 377 // noCachedConnError is the concrete type of ErrNoCachedConn, which 378 // needs to be detected by net/http regardless of whether it's its 379 // bundled version (in h2_bundle.go with a rewritten type name) or 380 // from a user's x/net/http2. As such, as it has a unique method name 381 // (IsHTTP2NoCachedConnError) that net/http sniffs for via func 382 // isNoCachedConnError. 383 type noCachedConnError struct{} 384 385 func (noCachedConnError) IsHTTP2NoCachedConnError() {} 386 func (noCachedConnError) Error() string { return "http2: no cached connection was available" } 387 388 // isNoCachedConnError reports whether err is of type noCachedConnError 389 // or its equivalent renamed type in net/http2's h2_bundle.go. Both types 390 // may coexist in the same running program. 391 func isNoCachedConnError(err error) bool { 392 _, ok := err.(interface{ IsHTTP2NoCachedConnError() }) 393 return ok 394 } 395 396 var ErrNoCachedConn error = noCachedConnError{} 397 398 // RoundTripOpt are options for the Transport.RoundTripOpt method. 399 type RoundTripOpt struct { 400 // OnlyCachedConn controls whether RoundTripOpt may 401 // create a new TCP connection. If set true and 402 // no cached connection is available, RoundTripOpt 403 // will return ErrNoCachedConn. 404 OnlyCachedConn bool 405 } 406 407 func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { 408 return t.RoundTripOpt(req, RoundTripOpt{}) 409 } 410 411 // authorityAddr returns a given authority (a host/IP, or host:port / ip:port) 412 // and returns a host:port. The port 443 is added if needed. 413 func authorityAddr(scheme string, authority string) (addr string) { 414 host, port, err := net.SplitHostPort(authority) 415 if err != nil { // authority didn't have a port 416 port = "443" 417 if scheme == "http" { 418 port = "80" 419 } 420 host = authority 421 } 422 if a, err := idna.ToASCII(host); err == nil { 423 host = a 424 } 425 // IPv6 address literal, without a port: 426 if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { 427 return host + ":" + port 428 } 429 return net.JoinHostPort(host, port) 430 } 431 432 // RoundTripOpt is like RoundTrip, but takes options. 433 func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) { 434 if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) { 435 return nil, errors.New("http2: unsupported scheme") 436 } 437 438 addr := authorityAddr(req.URL.Scheme, req.URL.Host) 439 for retry := 0; ; retry++ { 440 cc, err := t.connPool().GetClientConn(req, addr) 441 if err != nil { 442 t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err) 443 return nil, err 444 } 445 reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1) 446 traceGotConn(req, cc, reused) 447 res, gotErrAfterReqBodyWrite, err := cc.roundTrip(req) 448 if err != nil && retry <= 6 { 449 if req, err = shouldRetryRequest(req, err, gotErrAfterReqBodyWrite); err == nil { 450 // After the first retry, do exponential backoff with 10% jitter. 451 if retry == 0 { 452 continue 453 } 454 backoff := float64(uint(1) << (uint(retry) - 1)) 455 backoff += backoff * (0.1 * mathrand.Float64()) 456 select { 457 case <-time.After(time.Second * time.Duration(backoff)): 458 continue 459 case <-req.Context().Done(): 460 return nil, req.Context().Err() 461 } 462 } 463 } 464 if err != nil { 465 t.vlogf("RoundTrip failure: %v", err) 466 return nil, err 467 } 468 return res, nil 469 } 470 } 471 472 // CloseIdleConnections closes any connections which were previously 473 // connected from previous requests but are now sitting idle. 474 // It does not interrupt any connections currently in use. 475 func (t *Transport) CloseIdleConnections() { 476 if cp, ok := t.connPool().(clientConnPoolIdleCloser); ok { 477 cp.closeIdleConnections() 478 } 479 } 480 481 var ( 482 errClientConnClosed = errors.New("http2: client conn is closed") 483 errClientConnUnusable = errors.New("http2: client conn not usable") 484 errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY") 485 ) 486 487 // shouldRetryRequest is called by RoundTrip when a request fails to get 488 // response headers. It is always called with a non-nil error. 489 // It returns either a request to retry (either the same request, or a 490 // modified clone), or an error if the request can't be replayed. 491 func shouldRetryRequest(req *http.Request, err error, afterBodyWrite bool) (*http.Request, error) { 492 if !canRetryError(err) { 493 return nil, err 494 } 495 // If the Body is nil (or http.NoBody), it's safe to reuse 496 // this request and its Body. 497 if req.Body == nil || req.Body == http.NoBody { 498 return req, nil 499 } 500 501 // If the request body can be reset back to its original 502 // state via the optional req.GetBody, do that. 503 if req.GetBody != nil { 504 // TODO: consider a req.Body.Close here? or audit that all caller paths do? 505 body, err := req.GetBody() 506 if err != nil { 507 return nil, err 508 } 509 newReq := *req 510 newReq.Body = body 511 return &newReq, nil 512 } 513 514 // The Request.Body can't reset back to the beginning, but we 515 // don't seem to have started to read from it yet, so reuse 516 // the request directly. The "afterBodyWrite" means the 517 // bodyWrite process has started, which becomes true before 518 // the first Read. 519 if !afterBodyWrite { 520 return req, nil 521 } 522 523 return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err) 524 } 525 526 func canRetryError(err error) bool { 527 if err == errClientConnUnusable || err == errClientConnGotGoAway { 528 return true 529 } 530 if se, ok := err.(StreamError); ok { 531 return se.Code == ErrCodeRefusedStream 532 } 533 return false 534 } 535 536 func (t *Transport) dialClientConn(addr string, singleUse bool) (*ClientConn, error) { 537 host, _, err := net.SplitHostPort(addr) 538 if err != nil { 539 return nil, err 540 } 541 tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host)) 542 if err != nil { 543 return nil, err 544 } 545 return t.newClientConn(tconn, singleUse) 546 } 547 548 func (t *Transport) newTLSConfig(host string) *tls.Config { 549 cfg := new(tls.Config) 550 if t.TLSClientConfig != nil { 551 *cfg = *t.TLSClientConfig.Clone() 552 } 553 if !strSliceContains(cfg.NextProtos, NextProtoTLS) { 554 cfg.NextProtos = append([]string{NextProtoTLS}, cfg.NextProtos...) 555 } 556 if cfg.ServerName == "" { 557 cfg.ServerName = host 558 } 559 return cfg 560 } 561 562 func (t *Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) { 563 if t.DialTLS != nil { 564 return t.DialTLS 565 } 566 return t.dialTLSDefault 567 } 568 569 func (t *Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) { 570 cn, err := tls.Dial(network, addr, cfg) 571 if err != nil { 572 return nil, err 573 } 574 if err := cn.Handshake(); err != nil { 575 return nil, err 576 } 577 if !cfg.InsecureSkipVerify { 578 if err := cn.VerifyHostname(cfg.ServerName); err != nil { 579 return nil, err 580 } 581 } 582 state := cn.ConnectionState() 583 if p := state.NegotiatedProtocol; p != NextProtoTLS { 584 return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS) 585 } 586 if !state.NegotiatedProtocolIsMutual { 587 return nil, errors.New("http2: could not negotiate protocol mutually") 588 } 589 return cn, nil 590 } 591 592 // disableKeepAlives reports whether connections should be closed as 593 // soon as possible after handling the first request. 594 func (t *Transport) disableKeepAlives() bool { 595 return t.t1 != nil && t.t1.DisableKeepAlives 596 } 597 598 func (t *Transport) expectContinueTimeout() time.Duration { 599 if t.t1 == nil { 600 return 0 601 } 602 return t.t1.ExpectContinueTimeout 603 } 604 605 func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) { 606 return t.newClientConn(c, false) 607 } 608 609 func (t *Transport) newClientConn(c net.Conn, singleUse bool) (*ClientConn, error) { 610 cc := &ClientConn{ 611 t: t, 612 tconn: c, 613 readerDone: make(chan struct{}), 614 nextStreamID: 1, 615 maxFrameSize: 16 << 10, // spec default 616 initialWindowSize: 65535, // spec default 617 maxConcurrentStreams: 1000, // "infinite", per spec. 1000 seems good enough. 618 peerMaxHeaderListSize: 0xffffffffffffffff, // "infinite", per spec. Use 2^64-1 instead. 619 streams: make(map[uint32]*clientStream), 620 singleUse: singleUse, 621 wantSettingsAck: true, 622 pings: make(map[[8]byte]chan struct{}), 623 } 624 if d := t.idleConnTimeout(); d != 0 { 625 cc.idleTimeout = d 626 cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout) 627 } 628 if VerboseLogs { 629 t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr()) 630 } 631 632 cc.cond = sync.NewCond(&cc.mu) 633 cc.flow.add(int32(initialWindowSize)) 634 635 // TODO: adjust this writer size to account for frame size + 636 // MTU + crypto/tls record padding. 637 cc.bw = bufio.NewWriter(stickyErrWriter{c, &cc.werr}) 638 cc.br = bufio.NewReader(c) 639 cc.fr = NewFramer(cc.bw, cc.br) 640 cc.fr.ReadMetaHeaders = hpack.NewDecoder(initialHeaderTableSize, nil) 641 cc.fr.MaxHeaderListSize = t.maxHeaderListSize() 642 643 // TODO: SetMaxDynamicTableSize, SetMaxDynamicTableSizeLimit on 644 // henc in response to SETTINGS frames? 645 cc.henc = hpack.NewEncoder(&cc.hbuf) 646 647 if t.AllowHTTP { 648 cc.nextStreamID = 3 649 } 650 651 if cs, ok := c.(connectionStater); ok { 652 state := cs.ConnectionState() 653 cc.tlsState = &state 654 } 655 656 initialSettings := []Setting{ 657 {ID: SettingEnablePush, Val: 0}, 658 {ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow}, 659 } 660 if max := t.maxHeaderListSize(); max != 0 { 661 initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max}) 662 } 663 664 cc.bw.Write(clientPreface) 665 cc.fr.WriteSettings(initialSettings...) 666 cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow) 667 cc.inflow.add(transportDefaultConnFlow + initialWindowSize) 668 cc.bw.Flush() 669 if cc.werr != nil { 670 return nil, cc.werr 671 } 672 673 go cc.readLoop() 674 return cc, nil 675 } 676 677 func (cc *ClientConn) setGoAway(f *GoAwayFrame) { 678 cc.mu.Lock() 679 defer cc.mu.Unlock() 680 681 old := cc.goAway 682 cc.goAway = f 683 684 // Merge the previous and current GoAway error frames. 685 if cc.goAwayDebug == "" { 686 cc.goAwayDebug = string(f.DebugData()) 687 } 688 if old != nil && old.ErrCode != ErrCodeNo { 689 cc.goAway.ErrCode = old.ErrCode 690 } 691 last := f.LastStreamID 692 for streamID, cs := range cc.streams { 693 if streamID > last { 694 select { 695 case cs.resc <- resAndError{err: errClientConnGotGoAway}: 696 default: 697 } 698 } 699 } 700 } 701 702 // CanTakeNewRequest reports whether the connection can take a new request, 703 // meaning it has not been closed or received or sent a GOAWAY. 704 func (cc *ClientConn) CanTakeNewRequest() bool { 705 cc.mu.Lock() 706 defer cc.mu.Unlock() 707 return cc.canTakeNewRequestLocked() 708 } 709 710 // clientConnIdleState describes the suitability of a client 711 // connection to initiate a new RoundTrip request. 712 type clientConnIdleState struct { 713 canTakeNewRequest bool 714 freshConn bool // whether it's unused by any previous request 715 } 716 717 func (cc *ClientConn) idleState() clientConnIdleState { 718 cc.mu.Lock() 719 defer cc.mu.Unlock() 720 return cc.idleStateLocked() 721 } 722 723 func (cc *ClientConn) idleStateLocked() (st clientConnIdleState) { 724 if cc.singleUse && cc.nextStreamID > 1 { 725 return 726 } 727 var maxConcurrentOkay bool 728 if cc.t.StrictMaxConcurrentStreams { 729 // We'll tell the caller we can take a new request to 730 // prevent the caller from dialing a new TCP 731 // connection, but then we'll block later before 732 // writing it. 733 maxConcurrentOkay = true 734 } else { 735 maxConcurrentOkay = int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) 736 } 737 738 st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay && 739 int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 740 st.freshConn = cc.nextStreamID == 1 && st.canTakeNewRequest 741 return 742 } 743 744 func (cc *ClientConn) canTakeNewRequestLocked() bool { 745 st := cc.idleStateLocked() 746 return st.canTakeNewRequest 747 } 748 749 // onIdleTimeout is called from a time.AfterFunc goroutine. It will 750 // only be called when we're idle, but because we're coming from a new 751 // goroutine, there could be a new request coming in at the same time, 752 // so this simply calls the synchronized closeIfIdle to shut down this 753 // connection. The timer could just call closeIfIdle, but this is more 754 // clear. 755 func (cc *ClientConn) onIdleTimeout() { 756 cc.closeIfIdle() 757 } 758 759 func (cc *ClientConn) closeIfIdle() { 760 cc.mu.Lock() 761 if len(cc.streams) > 0 { 762 cc.mu.Unlock() 763 return 764 } 765 cc.closed = true 766 nextID := cc.nextStreamID 767 // TODO: do clients send GOAWAY too? maybe? Just Close: 768 cc.mu.Unlock() 769 770 if VerboseLogs { 771 cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2) 772 } 773 cc.tconn.Close() 774 } 775 776 var shutdownEnterWaitStateHook = func() {} 777 778 // Shutdown gracefully close the client connection, waiting for running streams to complete. 779 func (cc *ClientConn) Shutdown(ctx context.Context) error { 780 if err := cc.sendGoAway(); err != nil { 781 return err 782 } 783 // Wait for all in-flight streams to complete or connection to close 784 done := make(chan error, 1) 785 cancelled := false // guarded by cc.mu 786 go func() { 787 cc.mu.Lock() 788 defer cc.mu.Unlock() 789 for { 790 if len(cc.streams) == 0 || cc.closed { 791 cc.closed = true 792 done <- cc.tconn.Close() 793 break 794 } 795 if cancelled { 796 break 797 } 798 cc.cond.Wait() 799 } 800 }() 801 shutdownEnterWaitStateHook() 802 select { 803 case err := <-done: 804 return err 805 case <-ctx.Done(): 806 cc.mu.Lock() 807 // Free the goroutine above 808 cancelled = true 809 cc.cond.Broadcast() 810 cc.mu.Unlock() 811 return ctx.Err() 812 } 813 } 814 815 func (cc *ClientConn) sendGoAway() error { 816 cc.mu.Lock() 817 defer cc.mu.Unlock() 818 cc.wmu.Lock() 819 defer cc.wmu.Unlock() 820 if cc.closing { 821 // GOAWAY sent already 822 return nil 823 } 824 // Send a graceful shutdown frame to server 825 maxStreamID := cc.nextStreamID 826 if err := cc.fr.WriteGoAway(maxStreamID, ErrCodeNo, nil); err != nil { 827 return err 828 } 829 if err := cc.bw.Flush(); err != nil { 830 return err 831 } 832 // Prevent new requests 833 cc.closing = true 834 return nil 835 } 836 837 // Close closes the client connection immediately. 838 // 839 // In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead. 840 func (cc *ClientConn) Close() error { 841 cc.mu.Lock() 842 defer cc.cond.Broadcast() 843 defer cc.mu.Unlock() 844 err := errors.New("http2: client connection force closed via ClientConn.Close") 845 for id, cs := range cc.streams { 846 select { 847 case cs.resc <- resAndError{err: err}: 848 default: 849 } 850 cs.bufPipe.CloseWithError(err) 851 delete(cc.streams, id) 852 } 853 cc.closed = true 854 return cc.tconn.Close() 855 } 856 857 const maxAllocFrameSize = 512 << 10 858 859 // frameBuffer returns a scratch buffer suitable for writing DATA frames. 860 // They're capped at the min of the peer's max frame size or 512KB 861 // (kinda arbitrarily), but definitely capped so we don't allocate 4GB 862 // bufers. 863 func (cc *ClientConn) frameScratchBuffer() []byte { 864 cc.mu.Lock() 865 size := cc.maxFrameSize 866 if size > maxAllocFrameSize { 867 size = maxAllocFrameSize 868 } 869 for i, buf := range cc.freeBuf { 870 if len(buf) >= int(size) { 871 cc.freeBuf[i] = nil 872 cc.mu.Unlock() 873 return buf[:size] 874 } 875 } 876 cc.mu.Unlock() 877 return make([]byte, size) 878 } 879 880 func (cc *ClientConn) putFrameScratchBuffer(buf []byte) { 881 cc.mu.Lock() 882 defer cc.mu.Unlock() 883 const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate. 884 if len(cc.freeBuf) < maxBufs { 885 cc.freeBuf = append(cc.freeBuf, buf) 886 return 887 } 888 for i, old := range cc.freeBuf { 889 if old == nil { 890 cc.freeBuf[i] = buf 891 return 892 } 893 } 894 // forget about it. 895 } 896 897 // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not 898 // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests. 899 var errRequestCanceled = errors.New("net/http: request canceled") 900 901 func commaSeparatedTrailers(req *http.Request) (string, error) { 902 keys := make([]string, 0, len(req.Trailer)) 903 for k := range req.Trailer { 904 k = http.CanonicalHeaderKey(k) 905 switch k { 906 case "Transfer-Encoding", "Trailer", "Content-Length": 907 return "", &badStringError{"invalid Trailer key", k} 908 } 909 keys = append(keys, k) 910 } 911 if len(keys) > 0 { 912 sort.Strings(keys) 913 return strings.Join(keys, ","), nil 914 } 915 return "", nil 916 } 917 918 func (cc *ClientConn) responseHeaderTimeout() time.Duration { 919 if cc.t.t1 != nil { 920 return cc.t.t1.ResponseHeaderTimeout 921 } 922 // No way to do this (yet?) with just an http2.Transport. Probably 923 // no need. Request.Cancel this is the new way. We only need to support 924 // this for compatibility with the old http.Transport fields when 925 // we're doing transparent http2. 926 return 0 927 } 928 929 // checkConnHeaders checks whether req has any invalid connection-level headers. 930 // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields. 931 // Certain headers are special-cased as okay but not transmitted later. 932 func checkConnHeaders(req *http.Request) error { 933 if v := req.Header.Get("Upgrade"); v != "" { 934 return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"]) 935 } 936 if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") { 937 return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv) 938 } 939 if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !strings.EqualFold(vv[0], "close") && !strings.EqualFold(vv[0], "keep-alive")) { 940 return fmt.Errorf("http2: invalid Connection request header: %q", vv) 941 } 942 return nil 943 } 944 945 // actualContentLength returns a sanitized version of 946 // req.ContentLength, where 0 actually means zero (not unknown) and -1 947 // means unknown. 948 func actualContentLength(req *http.Request) int64 { 949 if req.Body == nil || req.Body == http.NoBody { 950 return 0 951 } 952 if req.ContentLength != 0 { 953 return req.ContentLength 954 } 955 return -1 956 } 957 958 func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) { 959 resp, _, err := cc.roundTrip(req) 960 return resp, err 961 } 962 963 func (cc *ClientConn) roundTrip(req *http.Request) (res *http.Response, gotErrAfterReqBodyWrite bool, err error) { 964 if err := checkConnHeaders(req); err != nil { 965 return nil, false, err 966 } 967 if cc.idleTimer != nil { 968 cc.idleTimer.Stop() 969 } 970 971 trailers, err := commaSeparatedTrailers(req) 972 if err != nil { 973 return nil, false, err 974 } 975 hasTrailers := trailers != "" 976 977 cc.mu.Lock() 978 if err := cc.awaitOpenSlotForRequest(req); err != nil { 979 cc.mu.Unlock() 980 return nil, false, err 981 } 982 983 body := req.Body 984 contentLen := actualContentLength(req) 985 hasBody := contentLen != 0 986 987 // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere? 988 var requestedGzip bool 989 if !cc.t.disableCompression() && 990 req.Header.Get("Accept-Encoding") == "" && 991 req.Header.Get("Range") == "" && 992 req.Method != "HEAD" { 993 // Request gzip only, not deflate. Deflate is ambiguous and 994 // not as universally supported anyway. 995 // See: https://zlib.net/zlib_faq.html#faq39 996 // 997 // Note that we don't request this for HEAD requests, 998 // due to a bug in nginx: 999 // http://trac.nginx.org/nginx/ticket/358 1000 // https://golang.org/issue/5522 1001 // 1002 // We don't request gzip if the request is for a range, since 1003 // auto-decoding a portion of a gzipped document will just fail 1004 // anyway. See https://golang.org/issue/8923 1005 requestedGzip = true 1006 } 1007 1008 // we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is 1009 // sent by writeRequestBody below, along with any Trailers, 1010 // again in form HEADERS{1}, CONTINUATION{0,}) 1011 hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen) 1012 if err != nil { 1013 cc.mu.Unlock() 1014 return nil, false, err 1015 } 1016 1017 cs := cc.newStream() 1018 cs.req = req 1019 cs.trace = httptrace.ContextClientTrace(req.Context()) 1020 cs.requestedGzip = requestedGzip 1021 bodyWriter := cc.t.getBodyWriterState(cs, body) 1022 cs.on100 = bodyWriter.on100 1023 1024 cc.wmu.Lock() 1025 endStream := !hasBody && !hasTrailers 1026 werr := cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs) 1027 cc.wmu.Unlock() 1028 traceWroteHeaders(cs.trace) 1029 cc.mu.Unlock() 1030 1031 if werr != nil { 1032 if hasBody { 1033 req.Body.Close() // per RoundTripper contract 1034 bodyWriter.cancel() 1035 } 1036 cc.forgetStreamID(cs.ID) 1037 // Don't bother sending a RST_STREAM (our write already failed; 1038 // no need to keep writing) 1039 traceWroteRequest(cs.trace, werr) 1040 return nil, false, werr 1041 } 1042 1043 var respHeaderTimer <-chan time.Time 1044 if hasBody { 1045 bodyWriter.scheduleBodyWrite() 1046 } else { 1047 traceWroteRequest(cs.trace, nil) 1048 if d := cc.responseHeaderTimeout(); d != 0 { 1049 timer := time.NewTimer(d) 1050 defer timer.Stop() 1051 respHeaderTimer = timer.C 1052 } 1053 } 1054 1055 readLoopResCh := cs.resc 1056 bodyWritten := false 1057 ctx := req.Context() 1058 1059 handleReadLoopResponse := func(re resAndError) (*http.Response, bool, error) { 1060 res := re.res 1061 if re.err != nil || res.StatusCode > 299 { 1062 // On error or status code 3xx, 4xx, 5xx, etc abort any 1063 // ongoing write, assuming that the server doesn't care 1064 // about our request body. If the server replied with 1xx or 1065 // 2xx, however, then assume the server DOES potentially 1066 // want our body (e.g. full-duplex streaming: 1067 // golang.org/issue/13444). If it turns out the server 1068 // doesn't, they'll RST_STREAM us soon enough. This is a 1069 // heuristic to avoid adding knobs to Transport. Hopefully 1070 // we can keep it. 1071 bodyWriter.cancel() 1072 cs.abortRequestBodyWrite(errStopReqBodyWrite) 1073 } 1074 if re.err != nil { 1075 cc.forgetStreamID(cs.ID) 1076 return nil, cs.getStartedWrite(), re.err 1077 } 1078 res.Request = req 1079 res.TLS = cc.tlsState 1080 return res, false, nil 1081 } 1082 1083 for { 1084 select { 1085 case re := <-readLoopResCh: 1086 return handleReadLoopResponse(re) 1087 case <-respHeaderTimer: 1088 if !hasBody || bodyWritten { 1089 cc.writeStreamReset(cs.ID, ErrCodeCancel, nil) 1090 } else { 1091 bodyWriter.cancel() 1092 cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel) 1093 } 1094 cc.forgetStreamID(cs.ID) 1095 return nil, cs.getStartedWrite(), errTimeout 1096 case <-ctx.Done(): 1097 if !hasBody || bodyWritten { 1098 cc.writeStreamReset(cs.ID, ErrCodeCancel, nil) 1099 } else { 1100 bodyWriter.cancel() 1101 cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel) 1102 } 1103 cc.forgetStreamID(cs.ID) 1104 return nil, cs.getStartedWrite(), ctx.Err() 1105 case <-req.Cancel: 1106 if !hasBody || bodyWritten { 1107 cc.writeStreamReset(cs.ID, ErrCodeCancel, nil) 1108 } else { 1109 bodyWriter.cancel() 1110 cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel) 1111 } 1112 cc.forgetStreamID(cs.ID) 1113 return nil, cs.getStartedWrite(), errRequestCanceled 1114 case <-cs.peerReset: 1115 // processResetStream already removed the 1116 // stream from the streams map; no need for 1117 // forgetStreamID. 1118 return nil, cs.getStartedWrite(), cs.resetErr 1119 case err := <-bodyWriter.resc: 1120 // Prefer the read loop's response, if available. Issue 16102. 1121 select { 1122 case re := <-readLoopResCh: 1123 return handleReadLoopResponse(re) 1124 default: 1125 } 1126 if err != nil { 1127 cc.forgetStreamID(cs.ID) 1128 return nil, cs.getStartedWrite(), err 1129 } 1130 bodyWritten = true 1131 if d := cc.responseHeaderTimeout(); d != 0 { 1132 timer := time.NewTimer(d) 1133 defer timer.Stop() 1134 respHeaderTimer = timer.C 1135 } 1136 } 1137 } 1138 } 1139 1140 // awaitOpenSlotForRequest waits until len(streams) < maxConcurrentStreams. 1141 // Must hold cc.mu. 1142 func (cc *ClientConn) awaitOpenSlotForRequest(req *http.Request) error { 1143 var waitingForConn chan struct{} 1144 var waitingForConnErr error // guarded by cc.mu 1145 for { 1146 cc.lastActive = time.Now() 1147 if cc.closed || !cc.canTakeNewRequestLocked() { 1148 if waitingForConn != nil { 1149 close(waitingForConn) 1150 } 1151 return errClientConnUnusable 1152 } 1153 if int64(len(cc.streams))+1 <= int64(cc.maxConcurrentStreams) { 1154 if waitingForConn != nil { 1155 close(waitingForConn) 1156 } 1157 return nil 1158 } 1159 // Unfortunately, we cannot wait on a condition variable and channel at 1160 // the same time, so instead, we spin up a goroutine to check if the 1161 // request is canceled while we wait for a slot to open in the connection. 1162 if waitingForConn == nil { 1163 waitingForConn = make(chan struct{}) 1164 go func() { 1165 if err := awaitRequestCancel(req, waitingForConn); err != nil { 1166 cc.mu.Lock() 1167 waitingForConnErr = err 1168 cc.cond.Broadcast() 1169 cc.mu.Unlock() 1170 } 1171 }() 1172 } 1173 cc.pendingRequests++ 1174 cc.cond.Wait() 1175 cc.pendingRequests-- 1176 if waitingForConnErr != nil { 1177 return waitingForConnErr 1178 } 1179 } 1180 } 1181 1182 // requires cc.wmu be held 1183 func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error { 1184 first := true // first frame written (HEADERS is first, then CONTINUATION) 1185 for len(hdrs) > 0 && cc.werr == nil { 1186 chunk := hdrs 1187 if len(chunk) > maxFrameSize { 1188 chunk = chunk[:maxFrameSize] 1189 } 1190 hdrs = hdrs[len(chunk):] 1191 endHeaders := len(hdrs) == 0 1192 if first { 1193 cc.fr.WriteHeaders(HeadersFrameParam{ 1194 StreamID: streamID, 1195 BlockFragment: chunk, 1196 EndStream: endStream, 1197 EndHeaders: endHeaders, 1198 }) 1199 first = false 1200 } else { 1201 cc.fr.WriteContinuation(streamID, endHeaders, chunk) 1202 } 1203 } 1204 // TODO(bradfitz): this Flush could potentially block (as 1205 // could the WriteHeaders call(s) above), which means they 1206 // wouldn't respond to Request.Cancel being readable. That's 1207 // rare, but this should probably be in a goroutine. 1208 cc.bw.Flush() 1209 return cc.werr 1210 } 1211 1212 // internal error values; they don't escape to callers 1213 var ( 1214 // abort request body write; don't send cancel 1215 errStopReqBodyWrite = errors.New("http2: aborting request body write") 1216 1217 // abort request body write, but send stream reset of cancel. 1218 errStopReqBodyWriteAndCancel = errors.New("http2: canceling request") 1219 ) 1220 1221 func (cs *clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) { 1222 cc := cs.cc 1223 sentEnd := false // whether we sent the final DATA frame w/ END_STREAM 1224 buf := cc.frameScratchBuffer() 1225 defer cc.putFrameScratchBuffer(buf) 1226 1227 defer func() { 1228 traceWroteRequest(cs.trace, err) 1229 // TODO: write h12Compare test showing whether 1230 // Request.Body is closed by the Transport, 1231 // and in multiple cases: server replies <=299 and >299 1232 // while still writing request body 1233 cerr := bodyCloser.Close() 1234 if err == nil { 1235 err = cerr 1236 } 1237 }() 1238 1239 req := cs.req 1240 hasTrailers := req.Trailer != nil 1241 1242 var sawEOF bool 1243 for !sawEOF { 1244 n, err := body.Read(buf) 1245 if err == io.EOF { 1246 sawEOF = true 1247 err = nil 1248 } else if err != nil { 1249 cc.writeStreamReset(cs.ID, ErrCodeCancel, err) 1250 return err 1251 } 1252 1253 remain := buf[:n] 1254 for len(remain) > 0 && err == nil { 1255 var allowed int32 1256 allowed, err = cs.awaitFlowControl(len(remain)) 1257 switch { 1258 case err == errStopReqBodyWrite: 1259 return err 1260 case err == errStopReqBodyWriteAndCancel: 1261 cc.writeStreamReset(cs.ID, ErrCodeCancel, nil) 1262 return err 1263 case err != nil: 1264 return err 1265 } 1266 cc.wmu.Lock() 1267 data := remain[:allowed] 1268 remain = remain[allowed:] 1269 sentEnd = sawEOF && len(remain) == 0 && !hasTrailers 1270 err = cc.fr.WriteData(cs.ID, sentEnd, data) 1271 if err == nil { 1272 // TODO(bradfitz): this flush is for latency, not bandwidth. 1273 // Most requests won't need this. Make this opt-in or 1274 // opt-out? Use some heuristic on the body type? Nagel-like 1275 // timers? Based on 'n'? Only last chunk of this for loop, 1276 // unless flow control tokens are low? For now, always. 1277 // If we change this, see comment below. 1278 err = cc.bw.Flush() 1279 } 1280 cc.wmu.Unlock() 1281 } 1282 if err != nil { 1283 return err 1284 } 1285 } 1286 1287 if sentEnd { 1288 // Already sent END_STREAM (which implies we have no 1289 // trailers) and flushed, because currently all 1290 // WriteData frames above get a flush. So we're done. 1291 return nil 1292 } 1293 1294 var trls []byte 1295 if hasTrailers { 1296 cc.mu.Lock() 1297 trls, err = cc.encodeTrailers(req) 1298 cc.mu.Unlock() 1299 if err != nil { 1300 cc.writeStreamReset(cs.ID, ErrCodeInternal, err) 1301 cc.forgetStreamID(cs.ID) 1302 return err 1303 } 1304 } 1305 1306 cc.mu.Lock() 1307 maxFrameSize := int(cc.maxFrameSize) 1308 cc.mu.Unlock() 1309 1310 cc.wmu.Lock() 1311 defer cc.wmu.Unlock() 1312 1313 // Two ways to send END_STREAM: either with trailers, or 1314 // with an empty DATA frame. 1315 if len(trls) > 0 { 1316 err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls) 1317 } else { 1318 err = cc.fr.WriteData(cs.ID, true, nil) 1319 } 1320 if ferr := cc.bw.Flush(); ferr != nil && err == nil { 1321 err = ferr 1322 } 1323 return err 1324 } 1325 1326 // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow 1327 // control tokens from the server. 1328 // It returns either the non-zero number of tokens taken or an error 1329 // if the stream is dead. 1330 func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) { 1331 cc := cs.cc 1332 cc.mu.Lock() 1333 defer cc.mu.Unlock() 1334 for { 1335 if cc.closed { 1336 return 0, errClientConnClosed 1337 } 1338 if cs.stopReqBody != nil { 1339 return 0, cs.stopReqBody 1340 } 1341 if err := cs.checkResetOrDone(); err != nil { 1342 return 0, err 1343 } 1344 if a := cs.flow.available(); a > 0 { 1345 take := a 1346 if int(take) > maxBytes { 1347 1348 take = int32(maxBytes) // can't truncate int; take is int32 1349 } 1350 if take > int32(cc.maxFrameSize) { 1351 take = int32(cc.maxFrameSize) 1352 } 1353 cs.flow.take(take) 1354 return take, nil 1355 } 1356 cc.cond.Wait() 1357 } 1358 } 1359 1360 type badStringError struct { 1361 what string 1362 str string 1363 } 1364 1365 func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 1366 1367 // requires cc.mu be held. 1368 func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) { 1369 cc.hbuf.Reset() 1370 1371 host := req.Host 1372 if host == "" { 1373 host = req.URL.Host 1374 } 1375 host, err := httpguts.PunycodeHostPort(host) 1376 if err != nil { 1377 return nil, err 1378 } 1379 1380 var path string 1381 if req.Method != "CONNECT" { 1382 path = req.URL.RequestURI() 1383 if !validPseudoPath(path) { 1384 orig := path 1385 path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host) 1386 if !validPseudoPath(path) { 1387 if req.URL.Opaque != "" { 1388 return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque) 1389 } else { 1390 return nil, fmt.Errorf("invalid request :path %q", orig) 1391 } 1392 } 1393 } 1394 } 1395 1396 // Check for any invalid headers and return an error before we 1397 // potentially pollute our hpack state. (We want to be able to 1398 // continue to reuse the hpack encoder for future requests) 1399 for k, vv := range req.Header { 1400 if !httpguts.ValidHeaderFieldName(k) { 1401 return nil, fmt.Errorf("invalid HTTP header name %q", k) 1402 } 1403 for _, v := range vv { 1404 if !httpguts.ValidHeaderFieldValue(v) { 1405 return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k) 1406 } 1407 } 1408 } 1409 1410 enumerateHeaders := func(f func(name, value string)) { 1411 // 8.1.2.3 Request Pseudo-Header Fields 1412 // The :path pseudo-header field includes the path and query parts of the 1413 // target URI (the path-absolute production and optionally a '?' character 1414 // followed by the query production (see Sections 3.3 and 3.4 of 1415 // [RFC3986]). 1416 f(":authority", host) 1417 m := req.Method 1418 if m == "" { 1419 m = http.MethodGet 1420 } 1421 f(":method", m) 1422 if req.Method != "CONNECT" { 1423 f(":path", path) 1424 f(":scheme", req.URL.Scheme) 1425 } 1426 if trailers != "" { 1427 f("trailer", trailers) 1428 } 1429 1430 var didUA bool 1431 for k, vv := range req.Header { 1432 if strings.EqualFold(k, "host") || strings.EqualFold(k, "content-length") { 1433 // Host is :authority, already sent. 1434 // Content-Length is automatic, set below. 1435 continue 1436 } else if strings.EqualFold(k, "connection") || strings.EqualFold(k, "proxy-connection") || 1437 strings.EqualFold(k, "transfer-encoding") || strings.EqualFold(k, "upgrade") || 1438 strings.EqualFold(k, "keep-alive") { 1439 // Per 8.1.2.2 Connection-Specific Header 1440 // Fields, don't send connection-specific 1441 // fields. We have already checked if any 1442 // are error-worthy so just ignore the rest. 1443 continue 1444 } else if strings.EqualFold(k, "user-agent") { 1445 // Match Go's http1 behavior: at most one 1446 // User-Agent. If set to nil or empty string, 1447 // then omit it. Otherwise if not mentioned, 1448 // include the default (below). 1449 didUA = true 1450 if len(vv) < 1 { 1451 continue 1452 } 1453 vv = vv[:1] 1454 if vv[0] == "" { 1455 continue 1456 } 1457 1458 } 1459 1460 for _, v := range vv { 1461 f(k, v) 1462 } 1463 } 1464 if shouldSendReqContentLength(req.Method, contentLength) { 1465 f("content-length", strconv.FormatInt(contentLength, 10)) 1466 } 1467 if addGzipHeader { 1468 f("accept-encoding", "gzip") 1469 } 1470 if !didUA { 1471 f("user-agent", defaultUserAgent) 1472 } 1473 } 1474 1475 // Do a first pass over the headers counting bytes to ensure 1476 // we don't exceed cc.peerMaxHeaderListSize. This is done as a 1477 // separate pass before encoding the headers to prevent 1478 // modifying the hpack state. 1479 hlSize := uint64(0) 1480 enumerateHeaders(func(name, value string) { 1481 hf := hpack.HeaderField{Name: name, Value: value} 1482 hlSize += uint64(hf.Size()) 1483 }) 1484 1485 if hlSize > cc.peerMaxHeaderListSize { 1486 return nil, errRequestHeaderListSize 1487 } 1488 1489 trace := httptrace.ContextClientTrace(req.Context()) 1490 traceHeaders := traceHasWroteHeaderField(trace) 1491 1492 // Header list size is ok. Write the headers. 1493 enumerateHeaders(func(name, value string) { 1494 name = strings.ToLower(name) 1495 cc.writeHeader(name, value) 1496 if traceHeaders { 1497 traceWroteHeaderField(trace, name, value) 1498 } 1499 }) 1500 1501 return cc.hbuf.Bytes(), nil 1502 } 1503 1504 // shouldSendReqContentLength reports whether the http2.Transport should send 1505 // a "content-length" request header. This logic is basically a copy of the net/http 1506 // transferWriter.shouldSendContentLength. 1507 // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown). 1508 // -1 means unknown. 1509 func shouldSendReqContentLength(method string, contentLength int64) bool { 1510 if contentLength > 0 { 1511 return true 1512 } 1513 if contentLength < 0 { 1514 return false 1515 } 1516 // For zero bodies, whether we send a content-length depends on the method. 1517 // It also kinda doesn't matter for http2 either way, with END_STREAM. 1518 switch method { 1519 case "POST", "PUT", "PATCH": 1520 return true 1521 default: 1522 return false 1523 } 1524 } 1525 1526 // requires cc.mu be held. 1527 func (cc *ClientConn) encodeTrailers(req *http.Request) ([]byte, error) { 1528 cc.hbuf.Reset() 1529 1530 hlSize := uint64(0) 1531 for k, vv := range req.Trailer { 1532 for _, v := range vv { 1533 hf := hpack.HeaderField{Name: k, Value: v} 1534 hlSize += uint64(hf.Size()) 1535 } 1536 } 1537 if hlSize > cc.peerMaxHeaderListSize { 1538 return nil, errRequestHeaderListSize 1539 } 1540 1541 for k, vv := range req.Trailer { 1542 // Transfer-Encoding, etc.. have already been filtered at the 1543 // start of RoundTrip 1544 lowKey := strings.ToLower(k) 1545 for _, v := range vv { 1546 cc.writeHeader(lowKey, v) 1547 } 1548 } 1549 return cc.hbuf.Bytes(), nil 1550 } 1551 1552 func (cc *ClientConn) writeHeader(name, value string) { 1553 if VerboseLogs { 1554 log.Printf("http2: Transport encoding header %q = %q", name, value) 1555 } 1556 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 1557 } 1558 1559 type resAndError struct { 1560 res *http.Response 1561 err error 1562 } 1563 1564 // requires cc.mu be held. 1565 func (cc *ClientConn) newStream() *clientStream { 1566 cs := &clientStream{ 1567 cc: cc, 1568 ID: cc.nextStreamID, 1569 resc: make(chan resAndError, 1), 1570 peerReset: make(chan struct{}), 1571 done: make(chan struct{}), 1572 } 1573 cs.flow.add(int32(cc.initialWindowSize)) 1574 cs.flow.setConnFlow(&cc.flow) 1575 cs.inflow.add(transportDefaultStreamFlow) 1576 cs.inflow.setConnFlow(&cc.inflow) 1577 cc.nextStreamID += 2 1578 cc.streams[cs.ID] = cs 1579 return cs 1580 } 1581 1582 func (cc *ClientConn) forgetStreamID(id uint32) { 1583 cc.streamByID(id, true) 1584 } 1585 1586 func (cc *ClientConn) streamByID(id uint32, andRemove bool) *clientStream { 1587 cc.mu.Lock() 1588 defer cc.mu.Unlock() 1589 cs := cc.streams[id] 1590 if andRemove && cs != nil && !cc.closed { 1591 cc.lastActive = time.Now() 1592 delete(cc.streams, id) 1593 if len(cc.streams) == 0 && cc.idleTimer != nil { 1594 cc.idleTimer.Reset(cc.idleTimeout) 1595 } 1596 close(cs.done) 1597 // Wake up checkResetOrDone via clientStream.awaitFlowControl and 1598 // wake up RoundTrip if there is a pending request. 1599 cc.cond.Broadcast() 1600 } 1601 return cs 1602 } 1603 1604 // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop. 1605 type clientConnReadLoop struct { 1606 cc *ClientConn 1607 closeWhenIdle bool 1608 } 1609 1610 // readLoop runs in its own goroutine and reads and dispatches frames. 1611 func (cc *ClientConn) readLoop() { 1612 rl := &clientConnReadLoop{cc: cc} 1613 defer rl.cleanup() 1614 cc.readerErr = rl.run() 1615 if ce, ok := cc.readerErr.(ConnectionError); ok { 1616 cc.wmu.Lock() 1617 cc.fr.WriteGoAway(0, ErrCode(ce), nil) 1618 cc.wmu.Unlock() 1619 } 1620 } 1621 1622 // GoAwayError is returned by the Transport when the server closes the 1623 // TCP connection after sending a GOAWAY frame. 1624 type GoAwayError struct { 1625 LastStreamID uint32 1626 ErrCode ErrCode 1627 DebugData string 1628 } 1629 1630 func (e GoAwayError) Error() string { 1631 return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q", 1632 e.LastStreamID, e.ErrCode, e.DebugData) 1633 } 1634 1635 func isEOFOrNetReadError(err error) bool { 1636 if err == io.EOF { 1637 return true 1638 } 1639 ne, ok := err.(*net.OpError) 1640 return ok && ne.Op == "read" 1641 } 1642 1643 func (rl *clientConnReadLoop) cleanup() { 1644 cc := rl.cc 1645 defer cc.tconn.Close() 1646 defer cc.t.connPool().MarkDead(cc) 1647 defer close(cc.readerDone) 1648 1649 if cc.idleTimer != nil { 1650 cc.idleTimer.Stop() 1651 } 1652 1653 // Close any response bodies if the server closes prematurely. 1654 // TODO: also do this if we've written the headers but not 1655 // gotten a response yet. 1656 err := cc.readerErr 1657 cc.mu.Lock() 1658 if cc.goAway != nil && isEOFOrNetReadError(err) { 1659 err = GoAwayError{ 1660 LastStreamID: cc.goAway.LastStreamID, 1661 ErrCode: cc.goAway.ErrCode, 1662 DebugData: cc.goAwayDebug, 1663 } 1664 } else if err == io.EOF { 1665 err = io.ErrUnexpectedEOF 1666 } 1667 for _, cs := range cc.streams { 1668 cs.bufPipe.CloseWithError(err) // no-op if already closed 1669 select { 1670 case cs.resc <- resAndError{err: err}: 1671 default: 1672 } 1673 close(cs.done) 1674 } 1675 cc.closed = true 1676 cc.cond.Broadcast() 1677 cc.mu.Unlock() 1678 } 1679 1680 func (rl *clientConnReadLoop) run() error { 1681 cc := rl.cc 1682 rl.closeWhenIdle = cc.t.disableKeepAlives() || cc.singleUse 1683 gotReply := false // ever saw a HEADERS reply 1684 gotSettings := false 1685 for { 1686 f, err := cc.fr.ReadFrame() 1687 if err != nil { 1688 cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err) 1689 } 1690 if se, ok := err.(StreamError); ok { 1691 if cs := cc.streamByID(se.StreamID, false); cs != nil { 1692 cs.cc.writeStreamReset(cs.ID, se.Code, err) 1693 cs.cc.forgetStreamID(cs.ID) 1694 if se.Cause == nil { 1695 se.Cause = cc.fr.errDetail 1696 } 1697 rl.endStreamError(cs, se) 1698 } 1699 continue 1700 } else if err != nil { 1701 return err 1702 } 1703 if VerboseLogs { 1704 cc.vlogf("http2: Transport received %s", summarizeFrame(f)) 1705 } 1706 if !gotSettings { 1707 if _, ok := f.(*SettingsFrame); !ok { 1708 cc.logf("protocol error: received %T before a SETTINGS frame", f) 1709 return ConnectionError(ErrCodeProtocol) 1710 } 1711 gotSettings = true 1712 } 1713 maybeIdle := false // whether frame might transition us to idle 1714 1715 switch f := f.(type) { 1716 case *MetaHeadersFrame: 1717 err = rl.processHeaders(f) 1718 maybeIdle = true 1719 gotReply = true 1720 case *DataFrame: 1721 err = rl.processData(f) 1722 maybeIdle = true 1723 case *GoAwayFrame: 1724 err = rl.processGoAway(f) 1725 maybeIdle = true 1726 case *RSTStreamFrame: 1727 err = rl.processResetStream(f) 1728 maybeIdle = true 1729 case *SettingsFrame: 1730 err = rl.processSettings(f) 1731 case *PushPromiseFrame: 1732 err = rl.processPushPromise(f) 1733 case *WindowUpdateFrame: 1734 err = rl.processWindowUpdate(f) 1735 case *PingFrame: 1736 err = rl.processPing(f) 1737 default: 1738 cc.logf("Transport: unhandled response frame type %T", f) 1739 } 1740 if err != nil { 1741 if VerboseLogs { 1742 cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, summarizeFrame(f), err) 1743 } 1744 return err 1745 } 1746 if rl.closeWhenIdle && gotReply && maybeIdle { 1747 cc.closeIfIdle() 1748 } 1749 } 1750 } 1751 1752 func (rl *clientConnReadLoop) processHeaders(f *MetaHeadersFrame) error { 1753 cc := rl.cc 1754 cs := cc.streamByID(f.StreamID, false) 1755 if cs == nil { 1756 // We'd get here if we canceled a request while the 1757 // server had its response still in flight. So if this 1758 // was just something we canceled, ignore it. 1759 return nil 1760 } 1761 if f.StreamEnded() { 1762 // Issue 20521: If the stream has ended, streamByID() causes 1763 // clientStream.done to be closed, which causes the request's bodyWriter 1764 // to be closed with an errStreamClosed, which may be received by 1765 // clientConn.RoundTrip before the result of processing these headers. 1766 // Deferring stream closure allows the header processing to occur first. 1767 // clientConn.RoundTrip may still receive the bodyWriter error first, but 1768 // the fix for issue 16102 prioritises any response. 1769 // 1770 // Issue 22413: If there is no request body, we should close the 1771 // stream before writing to cs.resc so that the stream is closed 1772 // immediately once RoundTrip returns. 1773 if cs.req.Body != nil { 1774 defer cc.forgetStreamID(f.StreamID) 1775 } else { 1776 cc.forgetStreamID(f.StreamID) 1777 } 1778 } 1779 if !cs.firstByte { 1780 if cs.trace != nil { 1781 // TODO(bradfitz): move first response byte earlier, 1782 // when we first read the 9 byte header, not waiting 1783 // until all the HEADERS+CONTINUATION frames have been 1784 // merged. This works for now. 1785 traceFirstResponseByte(cs.trace) 1786 } 1787 cs.firstByte = true 1788 } 1789 if !cs.pastHeaders { 1790 cs.pastHeaders = true 1791 } else { 1792 return rl.processTrailers(cs, f) 1793 } 1794 1795 res, err := rl.handleResponse(cs, f) 1796 if err != nil { 1797 if _, ok := err.(ConnectionError); ok { 1798 return err 1799 } 1800 // Any other error type is a stream error. 1801 cs.cc.writeStreamReset(f.StreamID, ErrCodeProtocol, err) 1802 cc.forgetStreamID(cs.ID) 1803 cs.resc <- resAndError{err: err} 1804 return nil // return nil from process* funcs to keep conn alive 1805 } 1806 if res == nil { 1807 // (nil, nil) special case. See handleResponse docs. 1808 return nil 1809 } 1810 cs.resTrailer = &res.Trailer 1811 cs.resc <- resAndError{res: res} 1812 return nil 1813 } 1814 1815 // may return error types nil, or ConnectionError. Any other error value 1816 // is a StreamError of type ErrCodeProtocol. The returned error in that case 1817 // is the detail. 1818 // 1819 // As a special case, handleResponse may return (nil, nil) to skip the 1820 // frame (currently only used for 1xx responses). 1821 func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFrame) (*http.Response, error) { 1822 if f.Truncated { 1823 return nil, errResponseHeaderListSize 1824 } 1825 1826 status := f.PseudoValue("status") 1827 if status == "" { 1828 return nil, errors.New("malformed response from server: missing status pseudo header") 1829 } 1830 statusCode, err := strconv.Atoi(status) 1831 if err != nil { 1832 return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header") 1833 } 1834 1835 header := make(http.Header) 1836 res := &http.Response{ 1837 Proto: "HTTP/2.0", 1838 ProtoMajor: 2, 1839 Header: header, 1840 StatusCode: statusCode, 1841 Status: status + " " + http.StatusText(statusCode), 1842 } 1843 for _, hf := range f.RegularFields() { 1844 key := http.CanonicalHeaderKey(hf.Name) 1845 if key == "Trailer" { 1846 t := res.Trailer 1847 if t == nil { 1848 t = make(http.Header) 1849 res.Trailer = t 1850 } 1851 foreachHeaderElement(hf.Value, func(v string) { 1852 t[http.CanonicalHeaderKey(v)] = nil 1853 }) 1854 } else { 1855 header[key] = append(header[key], hf.Value) 1856 } 1857 } 1858 1859 if statusCode >= 100 && statusCode <= 199 { 1860 cs.num1xx++ 1861 const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http 1862 if cs.num1xx > max1xxResponses { 1863 return nil, errors.New("http2: too many 1xx informational responses") 1864 } 1865 if fn := cs.get1xxTraceFunc(); fn != nil { 1866 if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil { 1867 return nil, err 1868 } 1869 } 1870 if statusCode == 100 { 1871 traceGot100Continue(cs.trace) 1872 if cs.on100 != nil { 1873 cs.on100() // forces any write delay timer to fire 1874 } 1875 } 1876 cs.pastHeaders = false // do it all again 1877 return nil, nil 1878 } 1879 1880 streamEnded := f.StreamEnded() 1881 isHead := cs.req.Method == "HEAD" 1882 if !streamEnded || isHead { 1883 res.ContentLength = -1 1884 if clens := res.Header["Content-Length"]; len(clens) == 1 { 1885 if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil { 1886 res.ContentLength = clen64 1887 } else { 1888 // TODO: care? unlike http/1, it won't mess up our framing, so it's 1889 // more safe smuggling-wise to ignore. 1890 } 1891 } else if len(clens) > 1 { 1892 // TODO: care? unlike http/1, it won't mess up our framing, so it's 1893 // more safe smuggling-wise to ignore. 1894 } 1895 } 1896 1897 if streamEnded || isHead { 1898 res.Body = noBody 1899 return res, nil 1900 } 1901 1902 cs.bufPipe = pipe{b: &dataBuffer{expected: res.ContentLength}} 1903 cs.bytesRemain = res.ContentLength 1904 res.Body = transportResponseBody{cs} 1905 go cs.awaitRequestCancel(cs.req) 1906 1907 if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" { 1908 res.Header.Del("Content-Encoding") 1909 res.Header.Del("Content-Length") 1910 res.ContentLength = -1 1911 res.Body = &gzipReader{body: res.Body} 1912 res.Uncompressed = true 1913 } 1914 return res, nil 1915 } 1916 1917 func (rl *clientConnReadLoop) processTrailers(cs *clientStream, f *MetaHeadersFrame) error { 1918 if cs.pastTrailers { 1919 // Too many HEADERS frames for this stream. 1920 return ConnectionError(ErrCodeProtocol) 1921 } 1922 cs.pastTrailers = true 1923 if !f.StreamEnded() { 1924 // We expect that any headers for trailers also 1925 // has END_STREAM. 1926 return ConnectionError(ErrCodeProtocol) 1927 } 1928 if len(f.PseudoFields()) > 0 { 1929 // No pseudo header fields are defined for trailers. 1930 // TODO: ConnectionError might be overly harsh? Check. 1931 return ConnectionError(ErrCodeProtocol) 1932 } 1933 1934 trailer := make(http.Header) 1935 for _, hf := range f.RegularFields() { 1936 key := http.CanonicalHeaderKey(hf.Name) 1937 trailer[key] = append(trailer[key], hf.Value) 1938 } 1939 cs.trailer = trailer 1940 1941 rl.endStream(cs) 1942 return nil 1943 } 1944 1945 // transportResponseBody is the concrete type of Transport.RoundTrip's 1946 // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body. 1947 // On Close it sends RST_STREAM if EOF wasn't already seen. 1948 type transportResponseBody struct { 1949 cs *clientStream 1950 } 1951 1952 func (b transportResponseBody) Read(p []byte) (n int, err error) { 1953 cs := b.cs 1954 cc := cs.cc 1955 1956 if cs.readErr != nil { 1957 return 0, cs.readErr 1958 } 1959 n, err = b.cs.bufPipe.Read(p) 1960 if cs.bytesRemain != -1 { 1961 if int64(n) > cs.bytesRemain { 1962 n = int(cs.bytesRemain) 1963 if err == nil { 1964 err = errors.New("net/http: server replied with more than declared Content-Length; truncated") 1965 cc.writeStreamReset(cs.ID, ErrCodeProtocol, err) 1966 } 1967 cs.readErr = err 1968 return int(cs.bytesRemain), err 1969 } 1970 cs.bytesRemain -= int64(n) 1971 if err == io.EOF && cs.bytesRemain > 0 { 1972 err = io.ErrUnexpectedEOF 1973 cs.readErr = err 1974 return n, err 1975 } 1976 } 1977 if n == 0 { 1978 // No flow control tokens to send back. 1979 return 1980 } 1981 1982 cc.mu.Lock() 1983 defer cc.mu.Unlock() 1984 1985 var connAdd, streamAdd int32 1986 // Check the conn-level first, before the stream-level. 1987 if v := cc.inflow.available(); v < transportDefaultConnFlow/2 { 1988 connAdd = transportDefaultConnFlow - v 1989 cc.inflow.add(connAdd) 1990 } 1991 if err == nil { // No need to refresh if the stream is over or failed. 1992 // Consider any buffered body data (read from the conn but not 1993 // consumed by the client) when computing flow control for this 1994 // stream. 1995 v := int(cs.inflow.available()) + cs.bufPipe.Len() 1996 if v < transportDefaultStreamFlow-transportDefaultStreamMinRefresh { 1997 streamAdd = int32(transportDefaultStreamFlow - v) 1998 cs.inflow.add(streamAdd) 1999 } 2000 } 2001 if connAdd != 0 || streamAdd != 0 { 2002 cc.wmu.Lock() 2003 defer cc.wmu.Unlock() 2004 if connAdd != 0 { 2005 cc.fr.WriteWindowUpdate(0, mustUint31(connAdd)) 2006 } 2007 if streamAdd != 0 { 2008 cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd)) 2009 } 2010 cc.bw.Flush() 2011 } 2012 return 2013 } 2014 2015 var errClosedResponseBody = errors.New("http2: response body closed") 2016 2017 func (b transportResponseBody) Close() error { 2018 cs := b.cs 2019 cc := cs.cc 2020 2021 serverSentStreamEnd := cs.bufPipe.Err() == io.EOF 2022 unread := cs.bufPipe.Len() 2023 2024 if unread > 0 || !serverSentStreamEnd { 2025 cc.mu.Lock() 2026 cc.wmu.Lock() 2027 if !serverSentStreamEnd { 2028 cc.fr.WriteRSTStream(cs.ID, ErrCodeCancel) 2029 cs.didReset = true 2030 } 2031 // Return connection-level flow control. 2032 if unread > 0 { 2033 cc.inflow.add(int32(unread)) 2034 cc.fr.WriteWindowUpdate(0, uint32(unread)) 2035 } 2036 cc.bw.Flush() 2037 cc.wmu.Unlock() 2038 cc.mu.Unlock() 2039 } 2040 2041 cs.bufPipe.BreakWithError(errClosedResponseBody) 2042 cc.forgetStreamID(cs.ID) 2043 return nil 2044 } 2045 2046 func (rl *clientConnReadLoop) processData(f *DataFrame) error { 2047 cc := rl.cc 2048 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 2049 data := f.Data() 2050 if cs == nil { 2051 cc.mu.Lock() 2052 neverSent := cc.nextStreamID 2053 cc.mu.Unlock() 2054 if f.StreamID >= neverSent { 2055 // We never asked for this. 2056 cc.logf("http2: Transport received unsolicited DATA frame; closing connection") 2057 return ConnectionError(ErrCodeProtocol) 2058 } 2059 // We probably did ask for this, but canceled. Just ignore it. 2060 // TODO: be stricter here? only silently ignore things which 2061 // we canceled, but not things which were closed normally 2062 // by the peer? Tough without accumulating too much state. 2063 2064 // But at least return their flow control: 2065 if f.Length > 0 { 2066 cc.mu.Lock() 2067 cc.inflow.add(int32(f.Length)) 2068 cc.mu.Unlock() 2069 2070 cc.wmu.Lock() 2071 cc.fr.WriteWindowUpdate(0, uint32(f.Length)) 2072 cc.bw.Flush() 2073 cc.wmu.Unlock() 2074 } 2075 return nil 2076 } 2077 if !cs.firstByte { 2078 cc.logf("protocol error: received DATA before a HEADERS frame") 2079 rl.endStreamError(cs, StreamError{ 2080 StreamID: f.StreamID, 2081 Code: ErrCodeProtocol, 2082 }) 2083 return nil 2084 } 2085 if f.Length > 0 { 2086 if cs.req.Method == "HEAD" && len(data) > 0 { 2087 cc.logf("protocol error: received DATA on a HEAD request") 2088 rl.endStreamError(cs, StreamError{ 2089 StreamID: f.StreamID, 2090 Code: ErrCodeProtocol, 2091 }) 2092 return nil 2093 } 2094 // Check connection-level flow control. 2095 cc.mu.Lock() 2096 if cs.inflow.available() >= int32(f.Length) { 2097 cs.inflow.take(int32(f.Length)) 2098 } else { 2099 cc.mu.Unlock() 2100 return ConnectionError(ErrCodeFlowControl) 2101 } 2102 // Return any padded flow control now, since we won't 2103 // refund it later on body reads. 2104 var refund int 2105 if pad := int(f.Length) - len(data); pad > 0 { 2106 refund += pad 2107 } 2108 // Return len(data) now if the stream is already closed, 2109 // since data will never be read. 2110 didReset := cs.didReset 2111 if didReset { 2112 refund += len(data) 2113 } 2114 if refund > 0 { 2115 cc.inflow.add(int32(refund)) 2116 cc.wmu.Lock() 2117 cc.fr.WriteWindowUpdate(0, uint32(refund)) 2118 if !didReset { 2119 cs.inflow.add(int32(refund)) 2120 cc.fr.WriteWindowUpdate(cs.ID, uint32(refund)) 2121 } 2122 cc.bw.Flush() 2123 cc.wmu.Unlock() 2124 } 2125 cc.mu.Unlock() 2126 2127 if len(data) > 0 && !didReset { 2128 if _, err := cs.bufPipe.Write(data); err != nil { 2129 rl.endStreamError(cs, err) 2130 return err 2131 } 2132 } 2133 } 2134 2135 if f.StreamEnded() { 2136 rl.endStream(cs) 2137 } 2138 return nil 2139 } 2140 2141 var errInvalidTrailers = errors.New("http2: invalid trailers") 2142 2143 func (rl *clientConnReadLoop) endStream(cs *clientStream) { 2144 // TODO: check that any declared content-length matches, like 2145 // server.go's (*stream).endStream method. 2146 rl.endStreamError(cs, nil) 2147 } 2148 2149 func (rl *clientConnReadLoop) endStreamError(cs *clientStream, err error) { 2150 var code func() 2151 if err == nil { 2152 err = io.EOF 2153 code = cs.copyTrailers 2154 } 2155 if isConnectionCloseRequest(cs.req) { 2156 rl.closeWhenIdle = true 2157 } 2158 cs.bufPipe.closeWithErrorAndCode(err, code) 2159 2160 select { 2161 case cs.resc <- resAndError{err: err}: 2162 default: 2163 } 2164 } 2165 2166 func (cs *clientStream) copyTrailers() { 2167 for k, vv := range cs.trailer { 2168 t := cs.resTrailer 2169 if *t == nil { 2170 *t = make(http.Header) 2171 } 2172 (*t)[k] = vv 2173 } 2174 } 2175 2176 func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error { 2177 cc := rl.cc 2178 cc.t.connPool().MarkDead(cc) 2179 if f.ErrCode != 0 { 2180 // TODO: deal with GOAWAY more. particularly the error code 2181 cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode) 2182 } 2183 cc.setGoAway(f) 2184 return nil 2185 } 2186 2187 func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error { 2188 cc := rl.cc 2189 cc.mu.Lock() 2190 defer cc.mu.Unlock() 2191 2192 if f.IsAck() { 2193 if cc.wantSettingsAck { 2194 cc.wantSettingsAck = false 2195 return nil 2196 } 2197 return ConnectionError(ErrCodeProtocol) 2198 } 2199 2200 err := f.ForeachSetting(func(s Setting) error { 2201 switch s.ID { 2202 case SettingMaxFrameSize: 2203 cc.maxFrameSize = s.Val 2204 case SettingMaxConcurrentStreams: 2205 cc.maxConcurrentStreams = s.Val 2206 case SettingMaxHeaderListSize: 2207 cc.peerMaxHeaderListSize = uint64(s.Val) 2208 case SettingInitialWindowSize: 2209 // Values above the maximum flow-control 2210 // window size of 2^31-1 MUST be treated as a 2211 // connection error (Section 5.4.1) of type 2212 // FLOW_CONTROL_ERROR. 2213 if s.Val > math.MaxInt32 { 2214 return ConnectionError(ErrCodeFlowControl) 2215 } 2216 2217 // Adjust flow control of currently-open 2218 // frames by the difference of the old initial 2219 // window size and this one. 2220 delta := int32(s.Val) - int32(cc.initialWindowSize) 2221 for _, cs := range cc.streams { 2222 cs.flow.add(delta) 2223 } 2224 cc.cond.Broadcast() 2225 2226 cc.initialWindowSize = s.Val 2227 default: 2228 // TODO(bradfitz): handle more settings? SETTINGS_HEADER_TABLE_SIZE probably. 2229 cc.vlogf("Unhandled Setting: %v", s) 2230 } 2231 return nil 2232 }) 2233 if err != nil { 2234 return err 2235 } 2236 2237 cc.wmu.Lock() 2238 defer cc.wmu.Unlock() 2239 2240 cc.fr.WriteSettingsAck() 2241 cc.bw.Flush() 2242 return cc.werr 2243 } 2244 2245 func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error { 2246 cc := rl.cc 2247 cs := cc.streamByID(f.StreamID, false) 2248 if f.StreamID != 0 && cs == nil { 2249 return nil 2250 } 2251 2252 cc.mu.Lock() 2253 defer cc.mu.Unlock() 2254 2255 fl := &cc.flow 2256 if cs != nil { 2257 fl = &cs.flow 2258 } 2259 if !fl.add(int32(f.Increment)) { 2260 return ConnectionError(ErrCodeFlowControl) 2261 } 2262 cc.cond.Broadcast() 2263 return nil 2264 } 2265 2266 func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error { 2267 cs := rl.cc.streamByID(f.StreamID, true) 2268 if cs == nil { 2269 // TODO: return error if server tries to RST_STEAM an idle stream 2270 return nil 2271 } 2272 select { 2273 case <-cs.peerReset: 2274 // Already reset. 2275 // This is the only goroutine 2276 // which closes this, so there 2277 // isn't a race. 2278 default: 2279 err := streamError(cs.ID, f.ErrCode) 2280 cs.resetErr = err 2281 close(cs.peerReset) 2282 cs.bufPipe.CloseWithError(err) 2283 cs.cc.cond.Broadcast() // wake up checkResetOrDone via clientStream.awaitFlowControl 2284 } 2285 return nil 2286 } 2287 2288 // Ping sends a PING frame to the server and waits for the ack. 2289 func (cc *ClientConn) Ping(ctx context.Context) error { 2290 c := make(chan struct{}) 2291 // Generate a random payload 2292 var p [8]byte 2293 for { 2294 if _, err := rand.Read(p[:]); err != nil { 2295 return err 2296 } 2297 cc.mu.Lock() 2298 // check for dup before insert 2299 if _, found := cc.pings[p]; !found { 2300 cc.pings[p] = c 2301 cc.mu.Unlock() 2302 break 2303 } 2304 cc.mu.Unlock() 2305 } 2306 cc.wmu.Lock() 2307 if err := cc.fr.WritePing(false, p); err != nil { 2308 cc.wmu.Unlock() 2309 return err 2310 } 2311 if err := cc.bw.Flush(); err != nil { 2312 cc.wmu.Unlock() 2313 return err 2314 } 2315 cc.wmu.Unlock() 2316 select { 2317 case <-c: 2318 return nil 2319 case <-ctx.Done(): 2320 return ctx.Err() 2321 case <-cc.readerDone: 2322 // connection closed 2323 return cc.readerErr 2324 } 2325 } 2326 2327 func (rl *clientConnReadLoop) processPing(f *PingFrame) error { 2328 if f.IsAck() { 2329 cc := rl.cc 2330 cc.mu.Lock() 2331 defer cc.mu.Unlock() 2332 // If ack, notify listener if any 2333 if c, ok := cc.pings[f.Data]; ok { 2334 close(c) 2335 delete(cc.pings, f.Data) 2336 } 2337 return nil 2338 } 2339 cc := rl.cc 2340 cc.wmu.Lock() 2341 defer cc.wmu.Unlock() 2342 if err := cc.fr.WritePing(true, f.Data); err != nil { 2343 return err 2344 } 2345 return cc.bw.Flush() 2346 } 2347 2348 func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error { 2349 // We told the peer we don't want them. 2350 // Spec says: 2351 // "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH 2352 // setting of the peer endpoint is set to 0. An endpoint that 2353 // has set this setting and has received acknowledgement MUST 2354 // treat the receipt of a PUSH_PROMISE frame as a connection 2355 // error (Section 5.4.1) of type PROTOCOL_ERROR." 2356 return ConnectionError(ErrCodeProtocol) 2357 } 2358 2359 func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) { 2360 // TODO: map err to more interesting error codes, once the 2361 // HTTP community comes up with some. But currently for 2362 // RST_STREAM there's no equivalent to GOAWAY frame's debug 2363 // data, and the error codes are all pretty vague ("cancel"). 2364 cc.wmu.Lock() 2365 cc.fr.WriteRSTStream(streamID, code) 2366 cc.bw.Flush() 2367 cc.wmu.Unlock() 2368 } 2369 2370 var ( 2371 errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit") 2372 errRequestHeaderListSize = errors.New("http2: request header list larger than peer's advertised limit") 2373 errPseudoTrailers = errors.New("http2: invalid pseudo header in trailers") 2374 ) 2375 2376 func (cc *ClientConn) logf(format string, args ...interface{}) { 2377 cc.t.logf(format, args...) 2378 } 2379 2380 func (cc *ClientConn) vlogf(format string, args ...interface{}) { 2381 cc.t.vlogf(format, args...) 2382 } 2383 2384 func (t *Transport) vlogf(format string, args ...interface{}) { 2385 if VerboseLogs { 2386 t.logf(format, args...) 2387 } 2388 } 2389 2390 func (t *Transport) logf(format string, args ...interface{}) { 2391 log.Printf(format, args...) 2392 } 2393 2394 var noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil)) 2395 2396 func strSliceContains(ss []string, s string) bool { 2397 for _, v := range ss { 2398 if v == s { 2399 return true 2400 } 2401 } 2402 return false 2403 } 2404 2405 type erringRoundTripper struct{ err error } 2406 2407 func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err } 2408 2409 // gzipReader wraps a response body so it can lazily 2410 // call gzip.NewReader on the first call to Read 2411 type gzipReader struct { 2412 body io.ReadCloser // underlying Response.Body 2413 zr *gzip.Reader // lazily-initialized gzip reader 2414 zerr error // sticky error 2415 } 2416 2417 func (gz *gzipReader) Read(p []byte) (n int, err error) { 2418 if gz.zerr != nil { 2419 return 0, gz.zerr 2420 } 2421 if gz.zr == nil { 2422 gz.zr, err = gzip.NewReader(gz.body) 2423 if err != nil { 2424 gz.zerr = err 2425 return 0, err 2426 } 2427 } 2428 return gz.zr.Read(p) 2429 } 2430 2431 func (gz *gzipReader) Close() error { 2432 return gz.body.Close() 2433 } 2434 2435 type errorReader struct{ err error } 2436 2437 func (r errorReader) Read(p []byte) (int, error) { return 0, r.err } 2438 2439 // bodyWriterState encapsulates various state around the Transport's writing 2440 // of the request body, particularly regarding doing delayed writes of the body 2441 // when the request contains "Expect: 100-continue". 2442 type bodyWriterState struct { 2443 cs *clientStream 2444 timer *time.Timer // if non-nil, we're doing a delayed write 2445 fnonce *sync.Once // to call fn with 2446 fn func() // the code to run in the goroutine, writing the body 2447 resc chan error // result of fn's execution 2448 delay time.Duration // how long we should delay a delayed write for 2449 } 2450 2451 func (t *Transport) getBodyWriterState(cs *clientStream, body io.Reader) (s bodyWriterState) { 2452 s.cs = cs 2453 if body == nil { 2454 return 2455 } 2456 resc := make(chan error, 1) 2457 s.resc = resc 2458 s.fn = func() { 2459 cs.cc.mu.Lock() 2460 cs.startedWrite = true 2461 cs.cc.mu.Unlock() 2462 resc <- cs.writeRequestBody(body, cs.req.Body) 2463 } 2464 s.delay = t.expectContinueTimeout() 2465 if s.delay == 0 || 2466 !httpguts.HeaderValuesContainsToken( 2467 cs.req.Header["Expect"], 2468 "100-continue") { 2469 return 2470 } 2471 s.fnonce = new(sync.Once) 2472 2473 // Arm the timer with a very large duration, which we'll 2474 // intentionally lower later. It has to be large now because 2475 // we need a handle to it before writing the headers, but the 2476 // s.delay value is defined to not start until after the 2477 // request headers were written. 2478 const hugeDuration = 365 * 24 * time.Hour 2479 s.timer = time.AfterFunc(hugeDuration, func() { 2480 s.fnonce.Do(s.fn) 2481 }) 2482 return 2483 } 2484 2485 func (s bodyWriterState) cancel() { 2486 if s.timer != nil { 2487 s.timer.Stop() 2488 } 2489 } 2490 2491 func (s bodyWriterState) on100() { 2492 if s.timer == nil { 2493 // If we didn't do a delayed write, ignore the server's 2494 // bogus 100 continue response. 2495 return 2496 } 2497 s.timer.Stop() 2498 go func() { s.fnonce.Do(s.fn) }() 2499 } 2500 2501 // scheduleBodyWrite starts writing the body, either immediately (in 2502 // the common case) or after the delay timeout. It should not be 2503 // called until after the headers have been written. 2504 func (s bodyWriterState) scheduleBodyWrite() { 2505 if s.timer == nil { 2506 // We're not doing a delayed write (see 2507 // getBodyWriterState), so just start the writing 2508 // goroutine immediately. 2509 go s.fn() 2510 return 2511 } 2512 traceWait100Continue(s.cs.trace) 2513 if s.timer.Stop() { 2514 s.timer.Reset(s.delay) 2515 } 2516 } 2517 2518 // isConnectionCloseRequest reports whether req should use its own 2519 // connection for a single request and then close the connection. 2520 func isConnectionCloseRequest(req *http.Request) bool { 2521 return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close") 2522 } 2523 2524 // registerHTTPSProtocol calls Transport.RegisterProtocol but 2525 // converting panics into errors. 2526 func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) { 2527 defer func() { 2528 if e := recover(); e != nil { 2529 err = fmt.Errorf("%v", e) 2530 } 2531 }() 2532 t.RegisterProtocol("https", rt) 2533 return nil 2534 } 2535 2536 // noDialH2RoundTripper is a RoundTripper which only tries to complete the request 2537 // if there's already has a cached connection to the host. 2538 // (The field is exported so it can be accessed via reflect from net/http; tested 2539 // by TestNoDialH2RoundTripperType) 2540 type noDialH2RoundTripper struct{ *Transport } 2541 2542 func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { 2543 res, err := rt.Transport.RoundTrip(req) 2544 if isNoCachedConnError(err) { 2545 return nil, http.ErrSkipAltProtocol 2546 } 2547 return res, err 2548 } 2549 2550 func (t *Transport) idleConnTimeout() time.Duration { 2551 if t.t1 != nil { 2552 return t.t1.IdleConnTimeout 2553 } 2554 return 0 2555 } 2556 2557 func traceGetConn(req *http.Request, hostPort string) { 2558 trace := httptrace.ContextClientTrace(req.Context()) 2559 if trace == nil || trace.GetConn == nil { 2560 return 2561 } 2562 trace.GetConn(hostPort) 2563 } 2564 2565 func traceGotConn(req *http.Request, cc *ClientConn, reused bool) { 2566 trace := httptrace.ContextClientTrace(req.Context()) 2567 if trace == nil || trace.GotConn == nil { 2568 return 2569 } 2570 ci := httptrace.GotConnInfo{Conn: cc.tconn} 2571 ci.Reused = reused 2572 cc.mu.Lock() 2573 ci.WasIdle = len(cc.streams) == 0 && reused 2574 if ci.WasIdle && !cc.lastActive.IsZero() { 2575 ci.IdleTime = time.Now().Sub(cc.lastActive) 2576 } 2577 cc.mu.Unlock() 2578 2579 trace.GotConn(ci) 2580 } 2581 2582 func traceWroteHeaders(trace *httptrace.ClientTrace) { 2583 if trace != nil && trace.WroteHeaders != nil { 2584 trace.WroteHeaders() 2585 } 2586 } 2587 2588 func traceGot100Continue(trace *httptrace.ClientTrace) { 2589 if trace != nil && trace.Got100Continue != nil { 2590 trace.Got100Continue() 2591 } 2592 } 2593 2594 func traceWait100Continue(trace *httptrace.ClientTrace) { 2595 if trace != nil && trace.Wait100Continue != nil { 2596 trace.Wait100Continue() 2597 } 2598 } 2599 2600 func traceWroteRequest(trace *httptrace.ClientTrace, err error) { 2601 if trace != nil && trace.WroteRequest != nil { 2602 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err}) 2603 } 2604 } 2605 2606 func traceFirstResponseByte(trace *httptrace.ClientTrace) { 2607 if trace != nil && trace.GotFirstResponseByte != nil { 2608 trace.GotFirstResponseByte() 2609 } 2610 }