transport.go (90652B)
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/fs" 20 "log" 21 "math" 22 mathrand "math/rand" 23 "net" 24 "net/http" 25 "net/http/httptrace" 26 "net/textproto" 27 "os" 28 "sort" 29 "strconv" 30 "strings" 31 "sync" 32 "sync/atomic" 33 "time" 34 35 "golang.org/x/net/http/httpguts" 36 "golang.org/x/net/http2/hpack" 37 "golang.org/x/net/idna" 38 ) 39 40 const ( 41 // transportDefaultConnFlow is how many connection-level flow control 42 // tokens we give the server at start-up, past the default 64k. 43 transportDefaultConnFlow = 1 << 30 44 45 // transportDefaultStreamFlow is how many stream-level flow 46 // control tokens we announce to the peer, and how many bytes 47 // we buffer per stream. 48 transportDefaultStreamFlow = 4 << 20 49 50 defaultUserAgent = "Go-http-client/2.0" 51 52 // initialMaxConcurrentStreams is a connections maxConcurrentStreams until 53 // it's received servers initial SETTINGS frame, which corresponds with the 54 // spec's minimum recommended value. 55 initialMaxConcurrentStreams = 100 56 57 // defaultMaxConcurrentStreams is a connections default maxConcurrentStreams 58 // if the server doesn't include one in its initial SETTINGS frame. 59 defaultMaxConcurrentStreams = 1000 60 ) 61 62 // Transport is an HTTP/2 Transport. 63 // 64 // A Transport internally caches connections to servers. It is safe 65 // for concurrent use by multiple goroutines. 66 type Transport struct { 67 // DialTLSContext specifies an optional dial function with context for 68 // creating TLS connections for requests. 69 // 70 // If DialTLSContext and DialTLS is nil, tls.Dial is used. 71 // 72 // If the returned net.Conn has a ConnectionState method like tls.Conn, 73 // it will be used to set http.Response.TLS. 74 DialTLSContext func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) 75 76 // DialTLS specifies an optional dial function for creating 77 // TLS connections for requests. 78 // 79 // If DialTLSContext and DialTLS is nil, tls.Dial is used. 80 // 81 // Deprecated: Use DialTLSContext instead, which allows the transport 82 // to cancel dials as soon as they are no longer needed. 83 // If both are set, DialTLSContext takes priority. 84 DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error) 85 86 // TLSClientConfig specifies the TLS configuration to use with 87 // tls.Client. If nil, the default configuration is used. 88 TLSClientConfig *tls.Config 89 90 // ConnPool optionally specifies an alternate connection pool to use. 91 // If nil, the default is used. 92 ConnPool ClientConnPool 93 94 // DisableCompression, if true, prevents the Transport from 95 // requesting compression with an "Accept-Encoding: gzip" 96 // request header when the Request contains no existing 97 // Accept-Encoding value. If the Transport requests gzip on 98 // its own and gets a gzipped response, it's transparently 99 // decoded in the Response.Body. However, if the user 100 // explicitly requested gzip it is not automatically 101 // uncompressed. 102 DisableCompression bool 103 104 // AllowHTTP, if true, permits HTTP/2 requests using the insecure, 105 // plain-text "http" scheme. Note that this does not enable h2c support. 106 AllowHTTP bool 107 108 // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to 109 // send in the initial settings frame. It is how many bytes 110 // of response headers are allowed. Unlike the http2 spec, zero here 111 // means to use a default limit (currently 10MB). If you actually 112 // want to advertise an unlimited value to the peer, Transport 113 // interprets the highest possible value here (0xffffffff or 1<<32-1) 114 // to mean no limit. 115 MaxHeaderListSize uint32 116 117 // MaxReadFrameSize is the http2 SETTINGS_MAX_FRAME_SIZE to send in the 118 // initial settings frame. It is the size in bytes of the largest frame 119 // payload that the sender is willing to receive. If 0, no setting is 120 // sent, and the value is provided by the peer, which should be 16384 121 // according to the spec: 122 // https://datatracker.ietf.org/doc/html/rfc7540#section-6.5.2. 123 // Values are bounded in the range 16k to 16M. 124 MaxReadFrameSize uint32 125 126 // MaxDecoderHeaderTableSize optionally specifies the http2 127 // SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It 128 // informs the remote endpoint of the maximum size of the header compression 129 // table used to decode header blocks, in octets. If zero, the default value 130 // of 4096 is used. 131 MaxDecoderHeaderTableSize uint32 132 133 // MaxEncoderHeaderTableSize optionally specifies an upper limit for the 134 // header compression table used for encoding request headers. Received 135 // SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero, 136 // the default value of 4096 is used. 137 MaxEncoderHeaderTableSize uint32 138 139 // StrictMaxConcurrentStreams controls whether the server's 140 // SETTINGS_MAX_CONCURRENT_STREAMS should be respected 141 // globally. If false, new TCP connections are created to the 142 // server as needed to keep each under the per-connection 143 // SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the 144 // server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as 145 // a global limit and callers of RoundTrip block when needed, 146 // waiting for their turn. 147 StrictMaxConcurrentStreams bool 148 149 // ReadIdleTimeout is the timeout after which a health check using ping 150 // frame will be carried out if no frame is received on the connection. 151 // Note that a ping response will is considered a received frame, so if 152 // there is no other traffic on the connection, the health check will 153 // be performed every ReadIdleTimeout interval. 154 // If zero, no health check is performed. 155 ReadIdleTimeout time.Duration 156 157 // PingTimeout is the timeout after which the connection will be closed 158 // if a response to Ping is not received. 159 // Defaults to 15s. 160 PingTimeout time.Duration 161 162 // WriteByteTimeout is the timeout after which the connection will be 163 // closed no data can be written to it. The timeout begins when data is 164 // available to write, and is extended whenever any bytes are written. 165 WriteByteTimeout time.Duration 166 167 // CountError, if non-nil, is called on HTTP/2 transport errors. 168 // It's intended to increment a metric for monitoring, such 169 // as an expvar or Prometheus metric. 170 // The errType consists of only ASCII word characters. 171 CountError func(errType string) 172 173 // t1, if non-nil, is the standard library Transport using 174 // this transport. Its settings are used (but not its 175 // RoundTrip method, etc). 176 t1 *http.Transport 177 178 connPoolOnce sync.Once 179 connPoolOrDef ClientConnPool // non-nil version of ConnPool 180 } 181 182 func (t *Transport) maxHeaderListSize() uint32 { 183 if t.MaxHeaderListSize == 0 { 184 return 10 << 20 185 } 186 if t.MaxHeaderListSize == 0xffffffff { 187 return 0 188 } 189 return t.MaxHeaderListSize 190 } 191 192 func (t *Transport) maxFrameReadSize() uint32 { 193 if t.MaxReadFrameSize == 0 { 194 return 0 // use the default provided by the peer 195 } 196 if t.MaxReadFrameSize < minMaxFrameSize { 197 return minMaxFrameSize 198 } 199 if t.MaxReadFrameSize > maxFrameSize { 200 return maxFrameSize 201 } 202 return t.MaxReadFrameSize 203 } 204 205 func (t *Transport) disableCompression() bool { 206 return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression) 207 } 208 209 func (t *Transport) pingTimeout() time.Duration { 210 if t.PingTimeout == 0 { 211 return 15 * time.Second 212 } 213 return t.PingTimeout 214 215 } 216 217 // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2. 218 // It returns an error if t1 has already been HTTP/2-enabled. 219 // 220 // Use ConfigureTransports instead to configure the HTTP/2 Transport. 221 func ConfigureTransport(t1 *http.Transport) error { 222 _, err := ConfigureTransports(t1) 223 return err 224 } 225 226 // ConfigureTransports configures a net/http HTTP/1 Transport to use HTTP/2. 227 // It returns a new HTTP/2 Transport for further configuration. 228 // It returns an error if t1 has already been HTTP/2-enabled. 229 func ConfigureTransports(t1 *http.Transport) (*Transport, error) { 230 return configureTransports(t1) 231 } 232 233 func configureTransports(t1 *http.Transport) (*Transport, error) { 234 connPool := new(clientConnPool) 235 t2 := &Transport{ 236 ConnPool: noDialClientConnPool{connPool}, 237 t1: t1, 238 } 239 connPool.t = t2 240 if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil { 241 return nil, err 242 } 243 if t1.TLSClientConfig == nil { 244 t1.TLSClientConfig = new(tls.Config) 245 } 246 if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") { 247 t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...) 248 } 249 if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") { 250 t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1") 251 } 252 upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper { 253 addr := authorityAddr("https", authority) 254 if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil { 255 go c.Close() 256 return erringRoundTripper{err} 257 } else if !used { 258 // Turns out we don't need this c. 259 // For example, two goroutines made requests to the same host 260 // at the same time, both kicking off TCP dials. (since protocol 261 // was unknown) 262 go c.Close() 263 } 264 return t2 265 } 266 if m := t1.TLSNextProto; len(m) == 0 { 267 t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{ 268 "h2": upgradeFn, 269 } 270 } else { 271 m["h2"] = upgradeFn 272 } 273 return t2, nil 274 } 275 276 func (t *Transport) connPool() ClientConnPool { 277 t.connPoolOnce.Do(t.initConnPool) 278 return t.connPoolOrDef 279 } 280 281 func (t *Transport) initConnPool() { 282 if t.ConnPool != nil { 283 t.connPoolOrDef = t.ConnPool 284 } else { 285 t.connPoolOrDef = &clientConnPool{t: t} 286 } 287 } 288 289 // ClientConn is the state of a single HTTP/2 client connection to an 290 // HTTP/2 server. 291 type ClientConn struct { 292 t *Transport 293 tconn net.Conn // usually *tls.Conn, except specialized impls 294 tconnClosed bool 295 tlsState *tls.ConnectionState // nil only for specialized impls 296 reused uint32 // whether conn is being reused; atomic 297 singleUse bool // whether being used for a single http.Request 298 getConnCalled bool // used by clientConnPool 299 300 // readLoop goroutine fields: 301 readerDone chan struct{} // closed on error 302 readerErr error // set before readerDone is closed 303 304 idleTimeout time.Duration // or 0 for never 305 idleTimer *time.Timer 306 307 mu sync.Mutex // guards following 308 cond *sync.Cond // hold mu; broadcast on flow/closed changes 309 flow outflow // our conn-level flow control quota (cs.outflow is per stream) 310 inflow inflow // peer's conn-level flow control 311 doNotReuse bool // whether conn is marked to not be reused for any future requests 312 closing bool 313 closed bool 314 seenSettings bool // true if we've seen a settings frame, false otherwise 315 wantSettingsAck bool // we sent a SETTINGS frame and haven't heard back 316 goAway *GoAwayFrame // if non-nil, the GoAwayFrame we received 317 goAwayDebug string // goAway frame's debug data, retained as a string 318 streams map[uint32]*clientStream // client-initiated 319 streamsReserved int // incr by ReserveNewRequest; decr on RoundTrip 320 nextStreamID uint32 321 pendingRequests int // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams 322 pings map[[8]byte]chan struct{} // in flight ping data to notification channel 323 br *bufio.Reader 324 lastActive time.Time 325 lastIdle time.Time // time last idle 326 // Settings from peer: (also guarded by wmu) 327 maxFrameSize uint32 328 maxConcurrentStreams uint32 329 peerMaxHeaderListSize uint64 330 peerMaxHeaderTableSize uint32 331 initialWindowSize uint32 332 333 // reqHeaderMu is a 1-element semaphore channel controlling access to sending new requests. 334 // Write to reqHeaderMu to lock it, read from it to unlock. 335 // Lock reqmu BEFORE mu or wmu. 336 reqHeaderMu chan struct{} 337 338 // wmu is held while writing. 339 // Acquire BEFORE mu when holding both, to avoid blocking mu on network writes. 340 // Only acquire both at the same time when changing peer settings. 341 wmu sync.Mutex 342 bw *bufio.Writer 343 fr *Framer 344 werr error // first write error that has occurred 345 hbuf bytes.Buffer // HPACK encoder writes into this 346 henc *hpack.Encoder 347 } 348 349 // clientStream is the state for a single HTTP/2 stream. One of these 350 // is created for each Transport.RoundTrip call. 351 type clientStream struct { 352 cc *ClientConn 353 354 // Fields of Request that we may access even after the response body is closed. 355 ctx context.Context 356 reqCancel <-chan struct{} 357 358 trace *httptrace.ClientTrace // or nil 359 ID uint32 360 bufPipe pipe // buffered pipe with the flow-controlled response payload 361 requestedGzip bool 362 isHead bool 363 364 abortOnce sync.Once 365 abort chan struct{} // closed to signal stream should end immediately 366 abortErr error // set if abort is closed 367 368 peerClosed chan struct{} // closed when the peer sends an END_STREAM flag 369 donec chan struct{} // closed after the stream is in the closed state 370 on100 chan struct{} // buffered; written to if a 100 is received 371 372 respHeaderRecv chan struct{} // closed when headers are received 373 res *http.Response // set if respHeaderRecv is closed 374 375 flow outflow // guarded by cc.mu 376 inflow inflow // guarded by cc.mu 377 bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read 378 readErr error // sticky read error; owned by transportResponseBody.Read 379 380 reqBody io.ReadCloser 381 reqBodyContentLength int64 // -1 means unknown 382 reqBodyClosed chan struct{} // guarded by cc.mu; non-nil on Close, closed when done 383 384 // owned by writeRequest: 385 sentEndStream bool // sent an END_STREAM flag to the peer 386 sentHeaders bool 387 388 // owned by clientConnReadLoop: 389 firstByte bool // got the first response byte 390 pastHeaders bool // got first MetaHeadersFrame (actual headers) 391 pastTrailers bool // got optional second MetaHeadersFrame (trailers) 392 num1xx uint8 // number of 1xx responses seen 393 readClosed bool // peer sent an END_STREAM flag 394 readAborted bool // read loop reset the stream 395 396 trailer http.Header // accumulated trailers 397 resTrailer *http.Header // client's Response.Trailer 398 } 399 400 var got1xxFuncForTests func(int, textproto.MIMEHeader) error 401 402 // get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func, 403 // if any. It returns nil if not set or if the Go version is too old. 404 func (cs *clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error { 405 if fn := got1xxFuncForTests; fn != nil { 406 return fn 407 } 408 return traceGot1xxResponseFunc(cs.trace) 409 } 410 411 func (cs *clientStream) abortStream(err error) { 412 cs.cc.mu.Lock() 413 defer cs.cc.mu.Unlock() 414 cs.abortStreamLocked(err) 415 } 416 417 func (cs *clientStream) abortStreamLocked(err error) { 418 cs.abortOnce.Do(func() { 419 cs.abortErr = err 420 close(cs.abort) 421 }) 422 if cs.reqBody != nil { 423 cs.closeReqBodyLocked() 424 } 425 // TODO(dneil): Clean up tests where cs.cc.cond is nil. 426 if cs.cc.cond != nil { 427 // Wake up writeRequestBody if it is waiting on flow control. 428 cs.cc.cond.Broadcast() 429 } 430 } 431 432 func (cs *clientStream) abortRequestBodyWrite() { 433 cc := cs.cc 434 cc.mu.Lock() 435 defer cc.mu.Unlock() 436 if cs.reqBody != nil && cs.reqBodyClosed == nil { 437 cs.closeReqBodyLocked() 438 cc.cond.Broadcast() 439 } 440 } 441 442 func (cs *clientStream) closeReqBodyLocked() { 443 if cs.reqBodyClosed != nil { 444 return 445 } 446 cs.reqBodyClosed = make(chan struct{}) 447 reqBodyClosed := cs.reqBodyClosed 448 go func() { 449 cs.reqBody.Close() 450 close(reqBodyClosed) 451 }() 452 } 453 454 type stickyErrWriter struct { 455 conn net.Conn 456 timeout time.Duration 457 err *error 458 } 459 460 func (sew stickyErrWriter) Write(p []byte) (n int, err error) { 461 if *sew.err != nil { 462 return 0, *sew.err 463 } 464 for { 465 if sew.timeout != 0 { 466 sew.conn.SetWriteDeadline(time.Now().Add(sew.timeout)) 467 } 468 nn, err := sew.conn.Write(p[n:]) 469 n += nn 470 if n < len(p) && nn > 0 && errors.Is(err, os.ErrDeadlineExceeded) { 471 // Keep extending the deadline so long as we're making progress. 472 continue 473 } 474 if sew.timeout != 0 { 475 sew.conn.SetWriteDeadline(time.Time{}) 476 } 477 *sew.err = err 478 return n, err 479 } 480 } 481 482 // noCachedConnError is the concrete type of ErrNoCachedConn, which 483 // needs to be detected by net/http regardless of whether it's its 484 // bundled version (in h2_bundle.go with a rewritten type name) or 485 // from a user's x/net/http2. As such, as it has a unique method name 486 // (IsHTTP2NoCachedConnError) that net/http sniffs for via func 487 // isNoCachedConnError. 488 type noCachedConnError struct{} 489 490 func (noCachedConnError) IsHTTP2NoCachedConnError() {} 491 func (noCachedConnError) Error() string { return "http2: no cached connection was available" } 492 493 // isNoCachedConnError reports whether err is of type noCachedConnError 494 // or its equivalent renamed type in net/http2's h2_bundle.go. Both types 495 // may coexist in the same running program. 496 func isNoCachedConnError(err error) bool { 497 _, ok := err.(interface{ IsHTTP2NoCachedConnError() }) 498 return ok 499 } 500 501 var ErrNoCachedConn error = noCachedConnError{} 502 503 // RoundTripOpt are options for the Transport.RoundTripOpt method. 504 type RoundTripOpt struct { 505 // OnlyCachedConn controls whether RoundTripOpt may 506 // create a new TCP connection. If set true and 507 // no cached connection is available, RoundTripOpt 508 // will return ErrNoCachedConn. 509 OnlyCachedConn bool 510 } 511 512 func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { 513 return t.RoundTripOpt(req, RoundTripOpt{}) 514 } 515 516 // authorityAddr returns a given authority (a host/IP, or host:port / ip:port) 517 // and returns a host:port. The port 443 is added if needed. 518 func authorityAddr(scheme string, authority string) (addr string) { 519 host, port, err := net.SplitHostPort(authority) 520 if err != nil { // authority didn't have a port 521 port = "443" 522 if scheme == "http" { 523 port = "80" 524 } 525 host = authority 526 } 527 if a, err := idna.ToASCII(host); err == nil { 528 host = a 529 } 530 // IPv6 address literal, without a port: 531 if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { 532 return host + ":" + port 533 } 534 return net.JoinHostPort(host, port) 535 } 536 537 var retryBackoffHook func(time.Duration) *time.Timer 538 539 func backoffNewTimer(d time.Duration) *time.Timer { 540 if retryBackoffHook != nil { 541 return retryBackoffHook(d) 542 } 543 return time.NewTimer(d) 544 } 545 546 // RoundTripOpt is like RoundTrip, but takes options. 547 func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) { 548 if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) { 549 return nil, errors.New("http2: unsupported scheme") 550 } 551 552 addr := authorityAddr(req.URL.Scheme, req.URL.Host) 553 for retry := 0; ; retry++ { 554 cc, err := t.connPool().GetClientConn(req, addr) 555 if err != nil { 556 t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err) 557 return nil, err 558 } 559 reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1) 560 traceGotConn(req, cc, reused) 561 res, err := cc.RoundTrip(req) 562 if err != nil && retry <= 6 { 563 roundTripErr := err 564 if req, err = shouldRetryRequest(req, err); err == nil { 565 // After the first retry, do exponential backoff with 10% jitter. 566 if retry == 0 { 567 t.vlogf("RoundTrip retrying after failure: %v", roundTripErr) 568 continue 569 } 570 backoff := float64(uint(1) << (uint(retry) - 1)) 571 backoff += backoff * (0.1 * mathrand.Float64()) 572 d := time.Second * time.Duration(backoff) 573 timer := backoffNewTimer(d) 574 select { 575 case <-timer.C: 576 t.vlogf("RoundTrip retrying after failure: %v", roundTripErr) 577 continue 578 case <-req.Context().Done(): 579 timer.Stop() 580 err = req.Context().Err() 581 } 582 } 583 } 584 if err != nil { 585 t.vlogf("RoundTrip failure: %v", err) 586 return nil, err 587 } 588 return res, nil 589 } 590 } 591 592 // CloseIdleConnections closes any connections which were previously 593 // connected from previous requests but are now sitting idle. 594 // It does not interrupt any connections currently in use. 595 func (t *Transport) CloseIdleConnections() { 596 if cp, ok := t.connPool().(clientConnPoolIdleCloser); ok { 597 cp.closeIdleConnections() 598 } 599 } 600 601 var ( 602 errClientConnClosed = errors.New("http2: client conn is closed") 603 errClientConnUnusable = errors.New("http2: client conn not usable") 604 errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY") 605 ) 606 607 // shouldRetryRequest is called by RoundTrip when a request fails to get 608 // response headers. It is always called with a non-nil error. 609 // It returns either a request to retry (either the same request, or a 610 // modified clone), or an error if the request can't be replayed. 611 func shouldRetryRequest(req *http.Request, err error) (*http.Request, error) { 612 if !canRetryError(err) { 613 return nil, err 614 } 615 // If the Body is nil (or http.NoBody), it's safe to reuse 616 // this request and its Body. 617 if req.Body == nil || req.Body == http.NoBody { 618 return req, nil 619 } 620 621 // If the request body can be reset back to its original 622 // state via the optional req.GetBody, do that. 623 if req.GetBody != nil { 624 body, err := req.GetBody() 625 if err != nil { 626 return nil, err 627 } 628 newReq := *req 629 newReq.Body = body 630 return &newReq, nil 631 } 632 633 // The Request.Body can't reset back to the beginning, but we 634 // don't seem to have started to read from it yet, so reuse 635 // the request directly. 636 if err == errClientConnUnusable { 637 return req, nil 638 } 639 640 return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err) 641 } 642 643 func canRetryError(err error) bool { 644 if err == errClientConnUnusable || err == errClientConnGotGoAway { 645 return true 646 } 647 if se, ok := err.(StreamError); ok { 648 if se.Code == ErrCodeProtocol && se.Cause == errFromPeer { 649 // See golang/go#47635, golang/go#42777 650 return true 651 } 652 return se.Code == ErrCodeRefusedStream 653 } 654 return false 655 } 656 657 func (t *Transport) dialClientConn(ctx context.Context, addr string, singleUse bool) (*ClientConn, error) { 658 host, _, err := net.SplitHostPort(addr) 659 if err != nil { 660 return nil, err 661 } 662 tconn, err := t.dialTLS(ctx, "tcp", addr, t.newTLSConfig(host)) 663 if err != nil { 664 return nil, err 665 } 666 return t.newClientConn(tconn, singleUse) 667 } 668 669 func (t *Transport) newTLSConfig(host string) *tls.Config { 670 cfg := new(tls.Config) 671 if t.TLSClientConfig != nil { 672 *cfg = *t.TLSClientConfig.Clone() 673 } 674 if !strSliceContains(cfg.NextProtos, NextProtoTLS) { 675 cfg.NextProtos = append([]string{NextProtoTLS}, cfg.NextProtos...) 676 } 677 if cfg.ServerName == "" { 678 cfg.ServerName = host 679 } 680 return cfg 681 } 682 683 func (t *Transport) dialTLS(ctx context.Context, network, addr string, tlsCfg *tls.Config) (net.Conn, error) { 684 if t.DialTLSContext != nil { 685 return t.DialTLSContext(ctx, network, addr, tlsCfg) 686 } else if t.DialTLS != nil { 687 return t.DialTLS(network, addr, tlsCfg) 688 } 689 690 tlsCn, err := t.dialTLSWithContext(ctx, network, addr, tlsCfg) 691 if err != nil { 692 return nil, err 693 } 694 state := tlsCn.ConnectionState() 695 if p := state.NegotiatedProtocol; p != NextProtoTLS { 696 return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS) 697 } 698 if !state.NegotiatedProtocolIsMutual { 699 return nil, errors.New("http2: could not negotiate protocol mutually") 700 } 701 return tlsCn, nil 702 } 703 704 // disableKeepAlives reports whether connections should be closed as 705 // soon as possible after handling the first request. 706 func (t *Transport) disableKeepAlives() bool { 707 return t.t1 != nil && t.t1.DisableKeepAlives 708 } 709 710 func (t *Transport) expectContinueTimeout() time.Duration { 711 if t.t1 == nil { 712 return 0 713 } 714 return t.t1.ExpectContinueTimeout 715 } 716 717 func (t *Transport) maxDecoderHeaderTableSize() uint32 { 718 if v := t.MaxDecoderHeaderTableSize; v > 0 { 719 return v 720 } 721 return initialHeaderTableSize 722 } 723 724 func (t *Transport) maxEncoderHeaderTableSize() uint32 { 725 if v := t.MaxEncoderHeaderTableSize; v > 0 { 726 return v 727 } 728 return initialHeaderTableSize 729 } 730 731 func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) { 732 return t.newClientConn(c, t.disableKeepAlives()) 733 } 734 735 func (t *Transport) newClientConn(c net.Conn, singleUse bool) (*ClientConn, error) { 736 cc := &ClientConn{ 737 t: t, 738 tconn: c, 739 readerDone: make(chan struct{}), 740 nextStreamID: 1, 741 maxFrameSize: 16 << 10, // spec default 742 initialWindowSize: 65535, // spec default 743 maxConcurrentStreams: initialMaxConcurrentStreams, // "infinite", per spec. Use a smaller value until we have received server settings. 744 peerMaxHeaderListSize: 0xffffffffffffffff, // "infinite", per spec. Use 2^64-1 instead. 745 streams: make(map[uint32]*clientStream), 746 singleUse: singleUse, 747 wantSettingsAck: true, 748 pings: make(map[[8]byte]chan struct{}), 749 reqHeaderMu: make(chan struct{}, 1), 750 } 751 if d := t.idleConnTimeout(); d != 0 { 752 cc.idleTimeout = d 753 cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout) 754 } 755 if VerboseLogs { 756 t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr()) 757 } 758 759 cc.cond = sync.NewCond(&cc.mu) 760 cc.flow.add(int32(initialWindowSize)) 761 762 // TODO: adjust this writer size to account for frame size + 763 // MTU + crypto/tls record padding. 764 cc.bw = bufio.NewWriter(stickyErrWriter{ 765 conn: c, 766 timeout: t.WriteByteTimeout, 767 err: &cc.werr, 768 }) 769 cc.br = bufio.NewReader(c) 770 cc.fr = NewFramer(cc.bw, cc.br) 771 if t.maxFrameReadSize() != 0 { 772 cc.fr.SetMaxReadFrameSize(t.maxFrameReadSize()) 773 } 774 if t.CountError != nil { 775 cc.fr.countError = t.CountError 776 } 777 maxHeaderTableSize := t.maxDecoderHeaderTableSize() 778 cc.fr.ReadMetaHeaders = hpack.NewDecoder(maxHeaderTableSize, nil) 779 cc.fr.MaxHeaderListSize = t.maxHeaderListSize() 780 781 cc.henc = hpack.NewEncoder(&cc.hbuf) 782 cc.henc.SetMaxDynamicTableSizeLimit(t.maxEncoderHeaderTableSize()) 783 cc.peerMaxHeaderTableSize = initialHeaderTableSize 784 785 if t.AllowHTTP { 786 cc.nextStreamID = 3 787 } 788 789 if cs, ok := c.(connectionStater); ok { 790 state := cs.ConnectionState() 791 cc.tlsState = &state 792 } 793 794 initialSettings := []Setting{ 795 {ID: SettingEnablePush, Val: 0}, 796 {ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow}, 797 } 798 if max := t.maxFrameReadSize(); max != 0 { 799 initialSettings = append(initialSettings, Setting{ID: SettingMaxFrameSize, Val: max}) 800 } 801 if max := t.maxHeaderListSize(); max != 0 { 802 initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max}) 803 } 804 if maxHeaderTableSize != initialHeaderTableSize { 805 initialSettings = append(initialSettings, Setting{ID: SettingHeaderTableSize, Val: maxHeaderTableSize}) 806 } 807 808 cc.bw.Write(clientPreface) 809 cc.fr.WriteSettings(initialSettings...) 810 cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow) 811 cc.inflow.init(transportDefaultConnFlow + initialWindowSize) 812 cc.bw.Flush() 813 if cc.werr != nil { 814 cc.Close() 815 return nil, cc.werr 816 } 817 818 go cc.readLoop() 819 return cc, nil 820 } 821 822 func (cc *ClientConn) healthCheck() { 823 pingTimeout := cc.t.pingTimeout() 824 // We don't need to periodically ping in the health check, because the readLoop of ClientConn will 825 // trigger the healthCheck again if there is no frame received. 826 ctx, cancel := context.WithTimeout(context.Background(), pingTimeout) 827 defer cancel() 828 cc.vlogf("http2: Transport sending health check") 829 err := cc.Ping(ctx) 830 if err != nil { 831 cc.vlogf("http2: Transport health check failure: %v", err) 832 cc.closeForLostPing() 833 } else { 834 cc.vlogf("http2: Transport health check success") 835 } 836 } 837 838 // SetDoNotReuse marks cc as not reusable for future HTTP requests. 839 func (cc *ClientConn) SetDoNotReuse() { 840 cc.mu.Lock() 841 defer cc.mu.Unlock() 842 cc.doNotReuse = true 843 } 844 845 func (cc *ClientConn) setGoAway(f *GoAwayFrame) { 846 cc.mu.Lock() 847 defer cc.mu.Unlock() 848 849 old := cc.goAway 850 cc.goAway = f 851 852 // Merge the previous and current GoAway error frames. 853 if cc.goAwayDebug == "" { 854 cc.goAwayDebug = string(f.DebugData()) 855 } 856 if old != nil && old.ErrCode != ErrCodeNo { 857 cc.goAway.ErrCode = old.ErrCode 858 } 859 last := f.LastStreamID 860 for streamID, cs := range cc.streams { 861 if streamID > last { 862 cs.abortStreamLocked(errClientConnGotGoAway) 863 } 864 } 865 } 866 867 // CanTakeNewRequest reports whether the connection can take a new request, 868 // meaning it has not been closed or received or sent a GOAWAY. 869 // 870 // If the caller is going to immediately make a new request on this 871 // connection, use ReserveNewRequest instead. 872 func (cc *ClientConn) CanTakeNewRequest() bool { 873 cc.mu.Lock() 874 defer cc.mu.Unlock() 875 return cc.canTakeNewRequestLocked() 876 } 877 878 // ReserveNewRequest is like CanTakeNewRequest but also reserves a 879 // concurrent stream in cc. The reservation is decremented on the 880 // next call to RoundTrip. 881 func (cc *ClientConn) ReserveNewRequest() bool { 882 cc.mu.Lock() 883 defer cc.mu.Unlock() 884 if st := cc.idleStateLocked(); !st.canTakeNewRequest { 885 return false 886 } 887 cc.streamsReserved++ 888 return true 889 } 890 891 // ClientConnState describes the state of a ClientConn. 892 type ClientConnState struct { 893 // Closed is whether the connection is closed. 894 Closed bool 895 896 // Closing is whether the connection is in the process of 897 // closing. It may be closing due to shutdown, being a 898 // single-use connection, being marked as DoNotReuse, or 899 // having received a GOAWAY frame. 900 Closing bool 901 902 // StreamsActive is how many streams are active. 903 StreamsActive int 904 905 // StreamsReserved is how many streams have been reserved via 906 // ClientConn.ReserveNewRequest. 907 StreamsReserved int 908 909 // StreamsPending is how many requests have been sent in excess 910 // of the peer's advertised MaxConcurrentStreams setting and 911 // are waiting for other streams to complete. 912 StreamsPending int 913 914 // MaxConcurrentStreams is how many concurrent streams the 915 // peer advertised as acceptable. Zero means no SETTINGS 916 // frame has been received yet. 917 MaxConcurrentStreams uint32 918 919 // LastIdle, if non-zero, is when the connection last 920 // transitioned to idle state. 921 LastIdle time.Time 922 } 923 924 // State returns a snapshot of cc's state. 925 func (cc *ClientConn) State() ClientConnState { 926 cc.wmu.Lock() 927 maxConcurrent := cc.maxConcurrentStreams 928 if !cc.seenSettings { 929 maxConcurrent = 0 930 } 931 cc.wmu.Unlock() 932 933 cc.mu.Lock() 934 defer cc.mu.Unlock() 935 return ClientConnState{ 936 Closed: cc.closed, 937 Closing: cc.closing || cc.singleUse || cc.doNotReuse || cc.goAway != nil, 938 StreamsActive: len(cc.streams), 939 StreamsReserved: cc.streamsReserved, 940 StreamsPending: cc.pendingRequests, 941 LastIdle: cc.lastIdle, 942 MaxConcurrentStreams: maxConcurrent, 943 } 944 } 945 946 // clientConnIdleState describes the suitability of a client 947 // connection to initiate a new RoundTrip request. 948 type clientConnIdleState struct { 949 canTakeNewRequest bool 950 } 951 952 func (cc *ClientConn) idleState() clientConnIdleState { 953 cc.mu.Lock() 954 defer cc.mu.Unlock() 955 return cc.idleStateLocked() 956 } 957 958 func (cc *ClientConn) idleStateLocked() (st clientConnIdleState) { 959 if cc.singleUse && cc.nextStreamID > 1 { 960 return 961 } 962 var maxConcurrentOkay bool 963 if cc.t.StrictMaxConcurrentStreams { 964 // We'll tell the caller we can take a new request to 965 // prevent the caller from dialing a new TCP 966 // connection, but then we'll block later before 967 // writing it. 968 maxConcurrentOkay = true 969 } else { 970 maxConcurrentOkay = int64(len(cc.streams)+cc.streamsReserved+1) <= int64(cc.maxConcurrentStreams) 971 } 972 973 st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay && 974 !cc.doNotReuse && 975 int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 && 976 !cc.tooIdleLocked() 977 return 978 } 979 980 func (cc *ClientConn) canTakeNewRequestLocked() bool { 981 st := cc.idleStateLocked() 982 return st.canTakeNewRequest 983 } 984 985 // tooIdleLocked reports whether this connection has been been sitting idle 986 // for too much wall time. 987 func (cc *ClientConn) tooIdleLocked() bool { 988 // The Round(0) strips the monontonic clock reading so the 989 // times are compared based on their wall time. We don't want 990 // to reuse a connection that's been sitting idle during 991 // VM/laptop suspend if monotonic time was also frozen. 992 return cc.idleTimeout != 0 && !cc.lastIdle.IsZero() && time.Since(cc.lastIdle.Round(0)) > cc.idleTimeout 993 } 994 995 // onIdleTimeout is called from a time.AfterFunc goroutine. It will 996 // only be called when we're idle, but because we're coming from a new 997 // goroutine, there could be a new request coming in at the same time, 998 // so this simply calls the synchronized closeIfIdle to shut down this 999 // connection. The timer could just call closeIfIdle, but this is more 1000 // clear. 1001 func (cc *ClientConn) onIdleTimeout() { 1002 cc.closeIfIdle() 1003 } 1004 1005 func (cc *ClientConn) closeConn() { 1006 t := time.AfterFunc(250*time.Millisecond, cc.forceCloseConn) 1007 defer t.Stop() 1008 cc.tconn.Close() 1009 } 1010 1011 // A tls.Conn.Close can hang for a long time if the peer is unresponsive. 1012 // Try to shut it down more aggressively. 1013 func (cc *ClientConn) forceCloseConn() { 1014 tc, ok := cc.tconn.(*tls.Conn) 1015 if !ok { 1016 return 1017 } 1018 if nc := tlsUnderlyingConn(tc); nc != nil { 1019 nc.Close() 1020 } 1021 } 1022 1023 func (cc *ClientConn) closeIfIdle() { 1024 cc.mu.Lock() 1025 if len(cc.streams) > 0 || cc.streamsReserved > 0 { 1026 cc.mu.Unlock() 1027 return 1028 } 1029 cc.closed = true 1030 nextID := cc.nextStreamID 1031 // TODO: do clients send GOAWAY too? maybe? Just Close: 1032 cc.mu.Unlock() 1033 1034 if VerboseLogs { 1035 cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2) 1036 } 1037 cc.closeConn() 1038 } 1039 1040 func (cc *ClientConn) isDoNotReuseAndIdle() bool { 1041 cc.mu.Lock() 1042 defer cc.mu.Unlock() 1043 return cc.doNotReuse && len(cc.streams) == 0 1044 } 1045 1046 var shutdownEnterWaitStateHook = func() {} 1047 1048 // Shutdown gracefully closes the client connection, waiting for running streams to complete. 1049 func (cc *ClientConn) Shutdown(ctx context.Context) error { 1050 if err := cc.sendGoAway(); err != nil { 1051 return err 1052 } 1053 // Wait for all in-flight streams to complete or connection to close 1054 done := make(chan struct{}) 1055 cancelled := false // guarded by cc.mu 1056 go func() { 1057 cc.mu.Lock() 1058 defer cc.mu.Unlock() 1059 for { 1060 if len(cc.streams) == 0 || cc.closed { 1061 cc.closed = true 1062 close(done) 1063 break 1064 } 1065 if cancelled { 1066 break 1067 } 1068 cc.cond.Wait() 1069 } 1070 }() 1071 shutdownEnterWaitStateHook() 1072 select { 1073 case <-done: 1074 cc.closeConn() 1075 return nil 1076 case <-ctx.Done(): 1077 cc.mu.Lock() 1078 // Free the goroutine above 1079 cancelled = true 1080 cc.cond.Broadcast() 1081 cc.mu.Unlock() 1082 return ctx.Err() 1083 } 1084 } 1085 1086 func (cc *ClientConn) sendGoAway() error { 1087 cc.mu.Lock() 1088 closing := cc.closing 1089 cc.closing = true 1090 maxStreamID := cc.nextStreamID 1091 cc.mu.Unlock() 1092 if closing { 1093 // GOAWAY sent already 1094 return nil 1095 } 1096 1097 cc.wmu.Lock() 1098 defer cc.wmu.Unlock() 1099 // Send a graceful shutdown frame to server 1100 if err := cc.fr.WriteGoAway(maxStreamID, ErrCodeNo, nil); err != nil { 1101 return err 1102 } 1103 if err := cc.bw.Flush(); err != nil { 1104 return err 1105 } 1106 // Prevent new requests 1107 return nil 1108 } 1109 1110 // closes the client connection immediately. In-flight requests are interrupted. 1111 // err is sent to streams. 1112 func (cc *ClientConn) closeForError(err error) { 1113 cc.mu.Lock() 1114 cc.closed = true 1115 for _, cs := range cc.streams { 1116 cs.abortStreamLocked(err) 1117 } 1118 cc.cond.Broadcast() 1119 cc.mu.Unlock() 1120 cc.closeConn() 1121 } 1122 1123 // Close closes the client connection immediately. 1124 // 1125 // In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead. 1126 func (cc *ClientConn) Close() error { 1127 err := errors.New("http2: client connection force closed via ClientConn.Close") 1128 cc.closeForError(err) 1129 return nil 1130 } 1131 1132 // closes the client connection immediately. In-flight requests are interrupted. 1133 func (cc *ClientConn) closeForLostPing() { 1134 err := errors.New("http2: client connection lost") 1135 if f := cc.t.CountError; f != nil { 1136 f("conn_close_lost_ping") 1137 } 1138 cc.closeForError(err) 1139 } 1140 1141 // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not 1142 // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests. 1143 var errRequestCanceled = errors.New("net/http: request canceled") 1144 1145 func commaSeparatedTrailers(req *http.Request) (string, error) { 1146 keys := make([]string, 0, len(req.Trailer)) 1147 for k := range req.Trailer { 1148 k = canonicalHeader(k) 1149 switch k { 1150 case "Transfer-Encoding", "Trailer", "Content-Length": 1151 return "", fmt.Errorf("invalid Trailer key %q", k) 1152 } 1153 keys = append(keys, k) 1154 } 1155 if len(keys) > 0 { 1156 sort.Strings(keys) 1157 return strings.Join(keys, ","), nil 1158 } 1159 return "", nil 1160 } 1161 1162 func (cc *ClientConn) responseHeaderTimeout() time.Duration { 1163 if cc.t.t1 != nil { 1164 return cc.t.t1.ResponseHeaderTimeout 1165 } 1166 // No way to do this (yet?) with just an http2.Transport. Probably 1167 // no need. Request.Cancel this is the new way. We only need to support 1168 // this for compatibility with the old http.Transport fields when 1169 // we're doing transparent http2. 1170 return 0 1171 } 1172 1173 // checkConnHeaders checks whether req has any invalid connection-level headers. 1174 // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields. 1175 // Certain headers are special-cased as okay but not transmitted later. 1176 func checkConnHeaders(req *http.Request) error { 1177 if v := req.Header.Get("Upgrade"); v != "" { 1178 return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"]) 1179 } 1180 if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") { 1181 return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv) 1182 } 1183 if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !asciiEqualFold(vv[0], "close") && !asciiEqualFold(vv[0], "keep-alive")) { 1184 return fmt.Errorf("http2: invalid Connection request header: %q", vv) 1185 } 1186 return nil 1187 } 1188 1189 // actualContentLength returns a sanitized version of 1190 // req.ContentLength, where 0 actually means zero (not unknown) and -1 1191 // means unknown. 1192 func actualContentLength(req *http.Request) int64 { 1193 if req.Body == nil || req.Body == http.NoBody { 1194 return 0 1195 } 1196 if req.ContentLength != 0 { 1197 return req.ContentLength 1198 } 1199 return -1 1200 } 1201 1202 func (cc *ClientConn) decrStreamReservations() { 1203 cc.mu.Lock() 1204 defer cc.mu.Unlock() 1205 cc.decrStreamReservationsLocked() 1206 } 1207 1208 func (cc *ClientConn) decrStreamReservationsLocked() { 1209 if cc.streamsReserved > 0 { 1210 cc.streamsReserved-- 1211 } 1212 } 1213 1214 func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) { 1215 ctx := req.Context() 1216 cs := &clientStream{ 1217 cc: cc, 1218 ctx: ctx, 1219 reqCancel: req.Cancel, 1220 isHead: req.Method == "HEAD", 1221 reqBody: req.Body, 1222 reqBodyContentLength: actualContentLength(req), 1223 trace: httptrace.ContextClientTrace(ctx), 1224 peerClosed: make(chan struct{}), 1225 abort: make(chan struct{}), 1226 respHeaderRecv: make(chan struct{}), 1227 donec: make(chan struct{}), 1228 } 1229 go cs.doRequest(req) 1230 1231 waitDone := func() error { 1232 select { 1233 case <-cs.donec: 1234 return nil 1235 case <-ctx.Done(): 1236 return ctx.Err() 1237 case <-cs.reqCancel: 1238 return errRequestCanceled 1239 } 1240 } 1241 1242 handleResponseHeaders := func() (*http.Response, error) { 1243 res := cs.res 1244 if res.StatusCode > 299 { 1245 // On error or status code 3xx, 4xx, 5xx, etc abort any 1246 // ongoing write, assuming that the server doesn't care 1247 // about our request body. If the server replied with 1xx or 1248 // 2xx, however, then assume the server DOES potentially 1249 // want our body (e.g. full-duplex streaming: 1250 // golang.org/issue/13444). If it turns out the server 1251 // doesn't, they'll RST_STREAM us soon enough. This is a 1252 // heuristic to avoid adding knobs to Transport. Hopefully 1253 // we can keep it. 1254 cs.abortRequestBodyWrite() 1255 } 1256 res.Request = req 1257 res.TLS = cc.tlsState 1258 if res.Body == noBody && actualContentLength(req) == 0 { 1259 // If there isn't a request or response body still being 1260 // written, then wait for the stream to be closed before 1261 // RoundTrip returns. 1262 if err := waitDone(); err != nil { 1263 return nil, err 1264 } 1265 } 1266 return res, nil 1267 } 1268 1269 cancelRequest := func(cs *clientStream, err error) error { 1270 cs.cc.mu.Lock() 1271 cs.abortStreamLocked(err) 1272 bodyClosed := cs.reqBodyClosed 1273 if cs.ID != 0 { 1274 // This request may have failed because of a problem with the connection, 1275 // or for some unrelated reason. (For example, the user might have canceled 1276 // the request without waiting for a response.) Mark the connection as 1277 // not reusable, since trying to reuse a dead connection is worse than 1278 // unnecessarily creating a new one. 1279 // 1280 // If cs.ID is 0, then the request was never allocated a stream ID and 1281 // whatever went wrong was unrelated to the connection. We might have 1282 // timed out waiting for a stream slot when StrictMaxConcurrentStreams 1283 // is set, for example, in which case retrying on a different connection 1284 // will not help. 1285 cs.cc.doNotReuse = true 1286 } 1287 cs.cc.mu.Unlock() 1288 // Wait for the request body to be closed. 1289 // 1290 // If nothing closed the body before now, abortStreamLocked 1291 // will have started a goroutine to close it. 1292 // 1293 // Closing the body before returning avoids a race condition 1294 // with net/http checking its readTrackingBody to see if the 1295 // body was read from or closed. See golang/go#60041. 1296 // 1297 // The body is closed in a separate goroutine without the 1298 // connection mutex held, but dropping the mutex before waiting 1299 // will keep us from holding it indefinitely if the body 1300 // close is slow for some reason. 1301 if bodyClosed != nil { 1302 <-bodyClosed 1303 } 1304 return err 1305 } 1306 1307 for { 1308 select { 1309 case <-cs.respHeaderRecv: 1310 return handleResponseHeaders() 1311 case <-cs.abort: 1312 select { 1313 case <-cs.respHeaderRecv: 1314 // If both cs.respHeaderRecv and cs.abort are signaling, 1315 // pick respHeaderRecv. The server probably wrote the 1316 // response and immediately reset the stream. 1317 // golang.org/issue/49645 1318 return handleResponseHeaders() 1319 default: 1320 waitDone() 1321 return nil, cancelRequest(cs, cs.abortErr) 1322 } 1323 case <-ctx.Done(): 1324 return nil, cancelRequest(cs, ctx.Err()) 1325 case <-cs.reqCancel: 1326 return nil, cancelRequest(cs, errRequestCanceled) 1327 } 1328 } 1329 } 1330 1331 // doRequest runs for the duration of the request lifetime. 1332 // 1333 // It sends the request and performs post-request cleanup (closing Request.Body, etc.). 1334 func (cs *clientStream) doRequest(req *http.Request) { 1335 err := cs.writeRequest(req) 1336 cs.cleanupWriteRequest(err) 1337 } 1338 1339 // writeRequest sends a request. 1340 // 1341 // It returns nil after the request is written, the response read, 1342 // and the request stream is half-closed by the peer. 1343 // 1344 // It returns non-nil if the request ends otherwise. 1345 // If the returned error is StreamError, the error Code may be used in resetting the stream. 1346 func (cs *clientStream) writeRequest(req *http.Request) (err error) { 1347 cc := cs.cc 1348 ctx := cs.ctx 1349 1350 if err := checkConnHeaders(req); err != nil { 1351 return err 1352 } 1353 1354 // Acquire the new-request lock by writing to reqHeaderMu. 1355 // This lock guards the critical section covering allocating a new stream ID 1356 // (requires mu) and creating the stream (requires wmu). 1357 if cc.reqHeaderMu == nil { 1358 panic("RoundTrip on uninitialized ClientConn") // for tests 1359 } 1360 select { 1361 case cc.reqHeaderMu <- struct{}{}: 1362 case <-cs.reqCancel: 1363 return errRequestCanceled 1364 case <-ctx.Done(): 1365 return ctx.Err() 1366 } 1367 1368 cc.mu.Lock() 1369 if cc.idleTimer != nil { 1370 cc.idleTimer.Stop() 1371 } 1372 cc.decrStreamReservationsLocked() 1373 if err := cc.awaitOpenSlotForStreamLocked(cs); err != nil { 1374 cc.mu.Unlock() 1375 <-cc.reqHeaderMu 1376 return err 1377 } 1378 cc.addStreamLocked(cs) // assigns stream ID 1379 if isConnectionCloseRequest(req) { 1380 cc.doNotReuse = true 1381 } 1382 cc.mu.Unlock() 1383 1384 // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere? 1385 if !cc.t.disableCompression() && 1386 req.Header.Get("Accept-Encoding") == "" && 1387 req.Header.Get("Range") == "" && 1388 !cs.isHead { 1389 // Request gzip only, not deflate. Deflate is ambiguous and 1390 // not as universally supported anyway. 1391 // See: https://zlib.net/zlib_faq.html#faq39 1392 // 1393 // Note that we don't request this for HEAD requests, 1394 // due to a bug in nginx: 1395 // http://trac.nginx.org/nginx/ticket/358 1396 // https://golang.org/issue/5522 1397 // 1398 // We don't request gzip if the request is for a range, since 1399 // auto-decoding a portion of a gzipped document will just fail 1400 // anyway. See https://golang.org/issue/8923 1401 cs.requestedGzip = true 1402 } 1403 1404 continueTimeout := cc.t.expectContinueTimeout() 1405 if continueTimeout != 0 { 1406 if !httpguts.HeaderValuesContainsToken(req.Header["Expect"], "100-continue") { 1407 continueTimeout = 0 1408 } else { 1409 cs.on100 = make(chan struct{}, 1) 1410 } 1411 } 1412 1413 // Past this point (where we send request headers), it is possible for 1414 // RoundTrip to return successfully. Since the RoundTrip contract permits 1415 // the caller to "mutate or reuse" the Request after closing the Response's Body, 1416 // we must take care when referencing the Request from here on. 1417 err = cs.encodeAndWriteHeaders(req) 1418 <-cc.reqHeaderMu 1419 if err != nil { 1420 return err 1421 } 1422 1423 hasBody := cs.reqBodyContentLength != 0 1424 if !hasBody { 1425 cs.sentEndStream = true 1426 } else { 1427 if continueTimeout != 0 { 1428 traceWait100Continue(cs.trace) 1429 timer := time.NewTimer(continueTimeout) 1430 select { 1431 case <-timer.C: 1432 err = nil 1433 case <-cs.on100: 1434 err = nil 1435 case <-cs.abort: 1436 err = cs.abortErr 1437 case <-ctx.Done(): 1438 err = ctx.Err() 1439 case <-cs.reqCancel: 1440 err = errRequestCanceled 1441 } 1442 timer.Stop() 1443 if err != nil { 1444 traceWroteRequest(cs.trace, err) 1445 return err 1446 } 1447 } 1448 1449 if err = cs.writeRequestBody(req); err != nil { 1450 if err != errStopReqBodyWrite { 1451 traceWroteRequest(cs.trace, err) 1452 return err 1453 } 1454 } else { 1455 cs.sentEndStream = true 1456 } 1457 } 1458 1459 traceWroteRequest(cs.trace, err) 1460 1461 var respHeaderTimer <-chan time.Time 1462 var respHeaderRecv chan struct{} 1463 if d := cc.responseHeaderTimeout(); d != 0 { 1464 timer := time.NewTimer(d) 1465 defer timer.Stop() 1466 respHeaderTimer = timer.C 1467 respHeaderRecv = cs.respHeaderRecv 1468 } 1469 // Wait until the peer half-closes its end of the stream, 1470 // or until the request is aborted (via context, error, or otherwise), 1471 // whichever comes first. 1472 for { 1473 select { 1474 case <-cs.peerClosed: 1475 return nil 1476 case <-respHeaderTimer: 1477 return errTimeout 1478 case <-respHeaderRecv: 1479 respHeaderRecv = nil 1480 respHeaderTimer = nil // keep waiting for END_STREAM 1481 case <-cs.abort: 1482 return cs.abortErr 1483 case <-ctx.Done(): 1484 return ctx.Err() 1485 case <-cs.reqCancel: 1486 return errRequestCanceled 1487 } 1488 } 1489 } 1490 1491 func (cs *clientStream) encodeAndWriteHeaders(req *http.Request) error { 1492 cc := cs.cc 1493 ctx := cs.ctx 1494 1495 cc.wmu.Lock() 1496 defer cc.wmu.Unlock() 1497 1498 // If the request was canceled while waiting for cc.mu, just quit. 1499 select { 1500 case <-cs.abort: 1501 return cs.abortErr 1502 case <-ctx.Done(): 1503 return ctx.Err() 1504 case <-cs.reqCancel: 1505 return errRequestCanceled 1506 default: 1507 } 1508 1509 // Encode headers. 1510 // 1511 // we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is 1512 // sent by writeRequestBody below, along with any Trailers, 1513 // again in form HEADERS{1}, CONTINUATION{0,}) 1514 trailers, err := commaSeparatedTrailers(req) 1515 if err != nil { 1516 return err 1517 } 1518 hasTrailers := trailers != "" 1519 contentLen := actualContentLength(req) 1520 hasBody := contentLen != 0 1521 hdrs, err := cc.encodeHeaders(req, cs.requestedGzip, trailers, contentLen) 1522 if err != nil { 1523 return err 1524 } 1525 1526 // Write the request. 1527 endStream := !hasBody && !hasTrailers 1528 cs.sentHeaders = true 1529 err = cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs) 1530 traceWroteHeaders(cs.trace) 1531 return err 1532 } 1533 1534 // cleanupWriteRequest performs post-request tasks. 1535 // 1536 // If err (the result of writeRequest) is non-nil and the stream is not closed, 1537 // cleanupWriteRequest will send a reset to the peer. 1538 func (cs *clientStream) cleanupWriteRequest(err error) { 1539 cc := cs.cc 1540 1541 if cs.ID == 0 { 1542 // We were canceled before creating the stream, so return our reservation. 1543 cc.decrStreamReservations() 1544 } 1545 1546 // TODO: write h12Compare test showing whether 1547 // Request.Body is closed by the Transport, 1548 // and in multiple cases: server replies <=299 and >299 1549 // while still writing request body 1550 cc.mu.Lock() 1551 mustCloseBody := false 1552 if cs.reqBody != nil && cs.reqBodyClosed == nil { 1553 mustCloseBody = true 1554 cs.reqBodyClosed = make(chan struct{}) 1555 } 1556 bodyClosed := cs.reqBodyClosed 1557 cc.mu.Unlock() 1558 if mustCloseBody { 1559 cs.reqBody.Close() 1560 close(bodyClosed) 1561 } 1562 if bodyClosed != nil { 1563 <-bodyClosed 1564 } 1565 1566 if err != nil && cs.sentEndStream { 1567 // If the connection is closed immediately after the response is read, 1568 // we may be aborted before finishing up here. If the stream was closed 1569 // cleanly on both sides, there is no error. 1570 select { 1571 case <-cs.peerClosed: 1572 err = nil 1573 default: 1574 } 1575 } 1576 if err != nil { 1577 cs.abortStream(err) // possibly redundant, but harmless 1578 if cs.sentHeaders { 1579 if se, ok := err.(StreamError); ok { 1580 if se.Cause != errFromPeer { 1581 cc.writeStreamReset(cs.ID, se.Code, err) 1582 } 1583 } else { 1584 cc.writeStreamReset(cs.ID, ErrCodeCancel, err) 1585 } 1586 } 1587 cs.bufPipe.CloseWithError(err) // no-op if already closed 1588 } else { 1589 if cs.sentHeaders && !cs.sentEndStream { 1590 cc.writeStreamReset(cs.ID, ErrCodeNo, nil) 1591 } 1592 cs.bufPipe.CloseWithError(errRequestCanceled) 1593 } 1594 if cs.ID != 0 { 1595 cc.forgetStreamID(cs.ID) 1596 } 1597 1598 cc.wmu.Lock() 1599 werr := cc.werr 1600 cc.wmu.Unlock() 1601 if werr != nil { 1602 cc.Close() 1603 } 1604 1605 close(cs.donec) 1606 } 1607 1608 // awaitOpenSlotForStreamLocked waits until len(streams) < maxConcurrentStreams. 1609 // Must hold cc.mu. 1610 func (cc *ClientConn) awaitOpenSlotForStreamLocked(cs *clientStream) error { 1611 for { 1612 cc.lastActive = time.Now() 1613 if cc.closed || !cc.canTakeNewRequestLocked() { 1614 return errClientConnUnusable 1615 } 1616 cc.lastIdle = time.Time{} 1617 if int64(len(cc.streams)) < int64(cc.maxConcurrentStreams) { 1618 return nil 1619 } 1620 cc.pendingRequests++ 1621 cc.cond.Wait() 1622 cc.pendingRequests-- 1623 select { 1624 case <-cs.abort: 1625 return cs.abortErr 1626 default: 1627 } 1628 } 1629 } 1630 1631 // requires cc.wmu be held 1632 func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error { 1633 first := true // first frame written (HEADERS is first, then CONTINUATION) 1634 for len(hdrs) > 0 && cc.werr == nil { 1635 chunk := hdrs 1636 if len(chunk) > maxFrameSize { 1637 chunk = chunk[:maxFrameSize] 1638 } 1639 hdrs = hdrs[len(chunk):] 1640 endHeaders := len(hdrs) == 0 1641 if first { 1642 cc.fr.WriteHeaders(HeadersFrameParam{ 1643 StreamID: streamID, 1644 BlockFragment: chunk, 1645 EndStream: endStream, 1646 EndHeaders: endHeaders, 1647 }) 1648 first = false 1649 } else { 1650 cc.fr.WriteContinuation(streamID, endHeaders, chunk) 1651 } 1652 } 1653 cc.bw.Flush() 1654 return cc.werr 1655 } 1656 1657 // internal error values; they don't escape to callers 1658 var ( 1659 // abort request body write; don't send cancel 1660 errStopReqBodyWrite = errors.New("http2: aborting request body write") 1661 1662 // abort request body write, but send stream reset of cancel. 1663 errStopReqBodyWriteAndCancel = errors.New("http2: canceling request") 1664 1665 errReqBodyTooLong = errors.New("http2: request body larger than specified content length") 1666 ) 1667 1668 // frameScratchBufferLen returns the length of a buffer to use for 1669 // outgoing request bodies to read/write to/from. 1670 // 1671 // It returns max(1, min(peer's advertised max frame size, 1672 // Request.ContentLength+1, 512KB)). 1673 func (cs *clientStream) frameScratchBufferLen(maxFrameSize int) int { 1674 const max = 512 << 10 1675 n := int64(maxFrameSize) 1676 if n > max { 1677 n = max 1678 } 1679 if cl := cs.reqBodyContentLength; cl != -1 && cl+1 < n { 1680 // Add an extra byte past the declared content-length to 1681 // give the caller's Request.Body io.Reader a chance to 1682 // give us more bytes than they declared, so we can catch it 1683 // early. 1684 n = cl + 1 1685 } 1686 if n < 1 { 1687 return 1 1688 } 1689 return int(n) // doesn't truncate; max is 512K 1690 } 1691 1692 var bufPool sync.Pool // of *[]byte 1693 1694 func (cs *clientStream) writeRequestBody(req *http.Request) (err error) { 1695 cc := cs.cc 1696 body := cs.reqBody 1697 sentEnd := false // whether we sent the final DATA frame w/ END_STREAM 1698 1699 hasTrailers := req.Trailer != nil 1700 remainLen := cs.reqBodyContentLength 1701 hasContentLen := remainLen != -1 1702 1703 cc.mu.Lock() 1704 maxFrameSize := int(cc.maxFrameSize) 1705 cc.mu.Unlock() 1706 1707 // Scratch buffer for reading into & writing from. 1708 scratchLen := cs.frameScratchBufferLen(maxFrameSize) 1709 var buf []byte 1710 if bp, ok := bufPool.Get().(*[]byte); ok && len(*bp) >= scratchLen { 1711 defer bufPool.Put(bp) 1712 buf = *bp 1713 } else { 1714 buf = make([]byte, scratchLen) 1715 defer bufPool.Put(&buf) 1716 } 1717 1718 var sawEOF bool 1719 for !sawEOF { 1720 n, err := body.Read(buf) 1721 if hasContentLen { 1722 remainLen -= int64(n) 1723 if remainLen == 0 && err == nil { 1724 // The request body's Content-Length was predeclared and 1725 // we just finished reading it all, but the underlying io.Reader 1726 // returned the final chunk with a nil error (which is one of 1727 // the two valid things a Reader can do at EOF). Because we'd prefer 1728 // to send the END_STREAM bit early, double-check that we're actually 1729 // at EOF. Subsequent reads should return (0, EOF) at this point. 1730 // If either value is different, we return an error in one of two ways below. 1731 var scratch [1]byte 1732 var n1 int 1733 n1, err = body.Read(scratch[:]) 1734 remainLen -= int64(n1) 1735 } 1736 if remainLen < 0 { 1737 err = errReqBodyTooLong 1738 return err 1739 } 1740 } 1741 if err != nil { 1742 cc.mu.Lock() 1743 bodyClosed := cs.reqBodyClosed != nil 1744 cc.mu.Unlock() 1745 switch { 1746 case bodyClosed: 1747 return errStopReqBodyWrite 1748 case err == io.EOF: 1749 sawEOF = true 1750 err = nil 1751 default: 1752 return err 1753 } 1754 } 1755 1756 remain := buf[:n] 1757 for len(remain) > 0 && err == nil { 1758 var allowed int32 1759 allowed, err = cs.awaitFlowControl(len(remain)) 1760 if err != nil { 1761 return err 1762 } 1763 cc.wmu.Lock() 1764 data := remain[:allowed] 1765 remain = remain[allowed:] 1766 sentEnd = sawEOF && len(remain) == 0 && !hasTrailers 1767 err = cc.fr.WriteData(cs.ID, sentEnd, data) 1768 if err == nil { 1769 // TODO(bradfitz): this flush is for latency, not bandwidth. 1770 // Most requests won't need this. Make this opt-in or 1771 // opt-out? Use some heuristic on the body type? Nagel-like 1772 // timers? Based on 'n'? Only last chunk of this for loop, 1773 // unless flow control tokens are low? For now, always. 1774 // If we change this, see comment below. 1775 err = cc.bw.Flush() 1776 } 1777 cc.wmu.Unlock() 1778 } 1779 if err != nil { 1780 return err 1781 } 1782 } 1783 1784 if sentEnd { 1785 // Already sent END_STREAM (which implies we have no 1786 // trailers) and flushed, because currently all 1787 // WriteData frames above get a flush. So we're done. 1788 return nil 1789 } 1790 1791 // Since the RoundTrip contract permits the caller to "mutate or reuse" 1792 // a request after the Response's Body is closed, verify that this hasn't 1793 // happened before accessing the trailers. 1794 cc.mu.Lock() 1795 trailer := req.Trailer 1796 err = cs.abortErr 1797 cc.mu.Unlock() 1798 if err != nil { 1799 return err 1800 } 1801 1802 cc.wmu.Lock() 1803 defer cc.wmu.Unlock() 1804 var trls []byte 1805 if len(trailer) > 0 { 1806 trls, err = cc.encodeTrailers(trailer) 1807 if err != nil { 1808 return err 1809 } 1810 } 1811 1812 // Two ways to send END_STREAM: either with trailers, or 1813 // with an empty DATA frame. 1814 if len(trls) > 0 { 1815 err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls) 1816 } else { 1817 err = cc.fr.WriteData(cs.ID, true, nil) 1818 } 1819 if ferr := cc.bw.Flush(); ferr != nil && err == nil { 1820 err = ferr 1821 } 1822 return err 1823 } 1824 1825 // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow 1826 // control tokens from the server. 1827 // It returns either the non-zero number of tokens taken or an error 1828 // if the stream is dead. 1829 func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) { 1830 cc := cs.cc 1831 ctx := cs.ctx 1832 cc.mu.Lock() 1833 defer cc.mu.Unlock() 1834 for { 1835 if cc.closed { 1836 return 0, errClientConnClosed 1837 } 1838 if cs.reqBodyClosed != nil { 1839 return 0, errStopReqBodyWrite 1840 } 1841 select { 1842 case <-cs.abort: 1843 return 0, cs.abortErr 1844 case <-ctx.Done(): 1845 return 0, ctx.Err() 1846 case <-cs.reqCancel: 1847 return 0, errRequestCanceled 1848 default: 1849 } 1850 if a := cs.flow.available(); a > 0 { 1851 take := a 1852 if int(take) > maxBytes { 1853 1854 take = int32(maxBytes) // can't truncate int; take is int32 1855 } 1856 if take > int32(cc.maxFrameSize) { 1857 take = int32(cc.maxFrameSize) 1858 } 1859 cs.flow.take(take) 1860 return take, nil 1861 } 1862 cc.cond.Wait() 1863 } 1864 } 1865 1866 var errNilRequestURL = errors.New("http2: Request.URI is nil") 1867 1868 // requires cc.wmu be held. 1869 func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) { 1870 cc.hbuf.Reset() 1871 if req.URL == nil { 1872 return nil, errNilRequestURL 1873 } 1874 1875 host := req.Host 1876 if host == "" { 1877 host = req.URL.Host 1878 } 1879 host, err := httpguts.PunycodeHostPort(host) 1880 if err != nil { 1881 return nil, err 1882 } 1883 1884 var path string 1885 if req.Method != "CONNECT" { 1886 path = req.URL.RequestURI() 1887 if !validPseudoPath(path) { 1888 orig := path 1889 path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host) 1890 if !validPseudoPath(path) { 1891 if req.URL.Opaque != "" { 1892 return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque) 1893 } else { 1894 return nil, fmt.Errorf("invalid request :path %q", orig) 1895 } 1896 } 1897 } 1898 } 1899 1900 // Check for any invalid headers and return an error before we 1901 // potentially pollute our hpack state. (We want to be able to 1902 // continue to reuse the hpack encoder for future requests) 1903 for k, vv := range req.Header { 1904 if !httpguts.ValidHeaderFieldName(k) { 1905 return nil, fmt.Errorf("invalid HTTP header name %q", k) 1906 } 1907 for _, v := range vv { 1908 if !httpguts.ValidHeaderFieldValue(v) { 1909 // Don't include the value in the error, because it may be sensitive. 1910 return nil, fmt.Errorf("invalid HTTP header value for header %q", k) 1911 } 1912 } 1913 } 1914 1915 enumerateHeaders := func(f func(name, value string)) { 1916 // 8.1.2.3 Request Pseudo-Header Fields 1917 // The :path pseudo-header field includes the path and query parts of the 1918 // target URI (the path-absolute production and optionally a '?' character 1919 // followed by the query production, see Sections 3.3 and 3.4 of 1920 // [RFC3986]). 1921 f(":authority", host) 1922 m := req.Method 1923 if m == "" { 1924 m = http.MethodGet 1925 } 1926 f(":method", m) 1927 if req.Method != "CONNECT" { 1928 f(":path", path) 1929 f(":scheme", req.URL.Scheme) 1930 } 1931 if trailers != "" { 1932 f("trailer", trailers) 1933 } 1934 1935 var didUA bool 1936 for k, vv := range req.Header { 1937 if asciiEqualFold(k, "host") || asciiEqualFold(k, "content-length") { 1938 // Host is :authority, already sent. 1939 // Content-Length is automatic, set below. 1940 continue 1941 } else if asciiEqualFold(k, "connection") || 1942 asciiEqualFold(k, "proxy-connection") || 1943 asciiEqualFold(k, "transfer-encoding") || 1944 asciiEqualFold(k, "upgrade") || 1945 asciiEqualFold(k, "keep-alive") { 1946 // Per 8.1.2.2 Connection-Specific Header 1947 // Fields, don't send connection-specific 1948 // fields. We have already checked if any 1949 // are error-worthy so just ignore the rest. 1950 continue 1951 } else if asciiEqualFold(k, "user-agent") { 1952 // Match Go's http1 behavior: at most one 1953 // User-Agent. If set to nil or empty string, 1954 // then omit it. Otherwise if not mentioned, 1955 // include the default (below). 1956 didUA = true 1957 if len(vv) < 1 { 1958 continue 1959 } 1960 vv = vv[:1] 1961 if vv[0] == "" { 1962 continue 1963 } 1964 } else if asciiEqualFold(k, "cookie") { 1965 // Per 8.1.2.5 To allow for better compression efficiency, the 1966 // Cookie header field MAY be split into separate header fields, 1967 // each with one or more cookie-pairs. 1968 for _, v := range vv { 1969 for { 1970 p := strings.IndexByte(v, ';') 1971 if p < 0 { 1972 break 1973 } 1974 f("cookie", v[:p]) 1975 p++ 1976 // strip space after semicolon if any. 1977 for p+1 <= len(v) && v[p] == ' ' { 1978 p++ 1979 } 1980 v = v[p:] 1981 } 1982 if len(v) > 0 { 1983 f("cookie", v) 1984 } 1985 } 1986 continue 1987 } 1988 1989 for _, v := range vv { 1990 f(k, v) 1991 } 1992 } 1993 if shouldSendReqContentLength(req.Method, contentLength) { 1994 f("content-length", strconv.FormatInt(contentLength, 10)) 1995 } 1996 if addGzipHeader { 1997 f("accept-encoding", "gzip") 1998 } 1999 if !didUA { 2000 f("user-agent", defaultUserAgent) 2001 } 2002 } 2003 2004 // Do a first pass over the headers counting bytes to ensure 2005 // we don't exceed cc.peerMaxHeaderListSize. This is done as a 2006 // separate pass before encoding the headers to prevent 2007 // modifying the hpack state. 2008 hlSize := uint64(0) 2009 enumerateHeaders(func(name, value string) { 2010 hf := hpack.HeaderField{Name: name, Value: value} 2011 hlSize += uint64(hf.Size()) 2012 }) 2013 2014 if hlSize > cc.peerMaxHeaderListSize { 2015 return nil, errRequestHeaderListSize 2016 } 2017 2018 trace := httptrace.ContextClientTrace(req.Context()) 2019 traceHeaders := traceHasWroteHeaderField(trace) 2020 2021 // Header list size is ok. Write the headers. 2022 enumerateHeaders(func(name, value string) { 2023 name, ascii := lowerHeader(name) 2024 if !ascii { 2025 // Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header 2026 // field names have to be ASCII characters (just as in HTTP/1.x). 2027 return 2028 } 2029 cc.writeHeader(name, value) 2030 if traceHeaders { 2031 traceWroteHeaderField(trace, name, value) 2032 } 2033 }) 2034 2035 return cc.hbuf.Bytes(), nil 2036 } 2037 2038 // shouldSendReqContentLength reports whether the http2.Transport should send 2039 // a "content-length" request header. This logic is basically a copy of the net/http 2040 // transferWriter.shouldSendContentLength. 2041 // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown). 2042 // -1 means unknown. 2043 func shouldSendReqContentLength(method string, contentLength int64) bool { 2044 if contentLength > 0 { 2045 return true 2046 } 2047 if contentLength < 0 { 2048 return false 2049 } 2050 // For zero bodies, whether we send a content-length depends on the method. 2051 // It also kinda doesn't matter for http2 either way, with END_STREAM. 2052 switch method { 2053 case "POST", "PUT", "PATCH": 2054 return true 2055 default: 2056 return false 2057 } 2058 } 2059 2060 // requires cc.wmu be held. 2061 func (cc *ClientConn) encodeTrailers(trailer http.Header) ([]byte, error) { 2062 cc.hbuf.Reset() 2063 2064 hlSize := uint64(0) 2065 for k, vv := range trailer { 2066 for _, v := range vv { 2067 hf := hpack.HeaderField{Name: k, Value: v} 2068 hlSize += uint64(hf.Size()) 2069 } 2070 } 2071 if hlSize > cc.peerMaxHeaderListSize { 2072 return nil, errRequestHeaderListSize 2073 } 2074 2075 for k, vv := range trailer { 2076 lowKey, ascii := lowerHeader(k) 2077 if !ascii { 2078 // Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header 2079 // field names have to be ASCII characters (just as in HTTP/1.x). 2080 continue 2081 } 2082 // Transfer-Encoding, etc.. have already been filtered at the 2083 // start of RoundTrip 2084 for _, v := range vv { 2085 cc.writeHeader(lowKey, v) 2086 } 2087 } 2088 return cc.hbuf.Bytes(), nil 2089 } 2090 2091 func (cc *ClientConn) writeHeader(name, value string) { 2092 if VerboseLogs { 2093 log.Printf("http2: Transport encoding header %q = %q", name, value) 2094 } 2095 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 2096 } 2097 2098 type resAndError struct { 2099 _ incomparable 2100 res *http.Response 2101 err error 2102 } 2103 2104 // requires cc.mu be held. 2105 func (cc *ClientConn) addStreamLocked(cs *clientStream) { 2106 cs.flow.add(int32(cc.initialWindowSize)) 2107 cs.flow.setConnFlow(&cc.flow) 2108 cs.inflow.init(transportDefaultStreamFlow) 2109 cs.ID = cc.nextStreamID 2110 cc.nextStreamID += 2 2111 cc.streams[cs.ID] = cs 2112 if cs.ID == 0 { 2113 panic("assigned stream ID 0") 2114 } 2115 } 2116 2117 func (cc *ClientConn) forgetStreamID(id uint32) { 2118 cc.mu.Lock() 2119 slen := len(cc.streams) 2120 delete(cc.streams, id) 2121 if len(cc.streams) != slen-1 { 2122 panic("forgetting unknown stream id") 2123 } 2124 cc.lastActive = time.Now() 2125 if len(cc.streams) == 0 && cc.idleTimer != nil { 2126 cc.idleTimer.Reset(cc.idleTimeout) 2127 cc.lastIdle = time.Now() 2128 } 2129 // Wake up writeRequestBody via clientStream.awaitFlowControl and 2130 // wake up RoundTrip if there is a pending request. 2131 cc.cond.Broadcast() 2132 2133 closeOnIdle := cc.singleUse || cc.doNotReuse || cc.t.disableKeepAlives() || cc.goAway != nil 2134 if closeOnIdle && cc.streamsReserved == 0 && len(cc.streams) == 0 { 2135 if VerboseLogs { 2136 cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, cc.nextStreamID-2) 2137 } 2138 cc.closed = true 2139 defer cc.closeConn() 2140 } 2141 2142 cc.mu.Unlock() 2143 } 2144 2145 // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop. 2146 type clientConnReadLoop struct { 2147 _ incomparable 2148 cc *ClientConn 2149 } 2150 2151 // readLoop runs in its own goroutine and reads and dispatches frames. 2152 func (cc *ClientConn) readLoop() { 2153 rl := &clientConnReadLoop{cc: cc} 2154 defer rl.cleanup() 2155 cc.readerErr = rl.run() 2156 if ce, ok := cc.readerErr.(ConnectionError); ok { 2157 cc.wmu.Lock() 2158 cc.fr.WriteGoAway(0, ErrCode(ce), nil) 2159 cc.wmu.Unlock() 2160 } 2161 } 2162 2163 // GoAwayError is returned by the Transport when the server closes the 2164 // TCP connection after sending a GOAWAY frame. 2165 type GoAwayError struct { 2166 LastStreamID uint32 2167 ErrCode ErrCode 2168 DebugData string 2169 } 2170 2171 func (e GoAwayError) Error() string { 2172 return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q", 2173 e.LastStreamID, e.ErrCode, e.DebugData) 2174 } 2175 2176 func isEOFOrNetReadError(err error) bool { 2177 if err == io.EOF { 2178 return true 2179 } 2180 ne, ok := err.(*net.OpError) 2181 return ok && ne.Op == "read" 2182 } 2183 2184 func (rl *clientConnReadLoop) cleanup() { 2185 cc := rl.cc 2186 cc.t.connPool().MarkDead(cc) 2187 defer cc.closeConn() 2188 defer close(cc.readerDone) 2189 2190 if cc.idleTimer != nil { 2191 cc.idleTimer.Stop() 2192 } 2193 2194 // Close any response bodies if the server closes prematurely. 2195 // TODO: also do this if we've written the headers but not 2196 // gotten a response yet. 2197 err := cc.readerErr 2198 cc.mu.Lock() 2199 if cc.goAway != nil && isEOFOrNetReadError(err) { 2200 err = GoAwayError{ 2201 LastStreamID: cc.goAway.LastStreamID, 2202 ErrCode: cc.goAway.ErrCode, 2203 DebugData: cc.goAwayDebug, 2204 } 2205 } else if err == io.EOF { 2206 err = io.ErrUnexpectedEOF 2207 } 2208 cc.closed = true 2209 2210 for _, cs := range cc.streams { 2211 select { 2212 case <-cs.peerClosed: 2213 // The server closed the stream before closing the conn, 2214 // so no need to interrupt it. 2215 default: 2216 cs.abortStreamLocked(err) 2217 } 2218 } 2219 cc.cond.Broadcast() 2220 cc.mu.Unlock() 2221 } 2222 2223 // countReadFrameError calls Transport.CountError with a string 2224 // representing err. 2225 func (cc *ClientConn) countReadFrameError(err error) { 2226 f := cc.t.CountError 2227 if f == nil || err == nil { 2228 return 2229 } 2230 if ce, ok := err.(ConnectionError); ok { 2231 errCode := ErrCode(ce) 2232 f(fmt.Sprintf("read_frame_conn_error_%s", errCode.stringToken())) 2233 return 2234 } 2235 if errors.Is(err, io.EOF) { 2236 f("read_frame_eof") 2237 return 2238 } 2239 if errors.Is(err, io.ErrUnexpectedEOF) { 2240 f("read_frame_unexpected_eof") 2241 return 2242 } 2243 if errors.Is(err, ErrFrameTooLarge) { 2244 f("read_frame_too_large") 2245 return 2246 } 2247 f("read_frame_other") 2248 } 2249 2250 func (rl *clientConnReadLoop) run() error { 2251 cc := rl.cc 2252 gotSettings := false 2253 readIdleTimeout := cc.t.ReadIdleTimeout 2254 var t *time.Timer 2255 if readIdleTimeout != 0 { 2256 t = time.AfterFunc(readIdleTimeout, cc.healthCheck) 2257 defer t.Stop() 2258 } 2259 for { 2260 f, err := cc.fr.ReadFrame() 2261 if t != nil { 2262 t.Reset(readIdleTimeout) 2263 } 2264 if err != nil { 2265 cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err) 2266 } 2267 if se, ok := err.(StreamError); ok { 2268 if cs := rl.streamByID(se.StreamID); cs != nil { 2269 if se.Cause == nil { 2270 se.Cause = cc.fr.errDetail 2271 } 2272 rl.endStreamError(cs, se) 2273 } 2274 continue 2275 } else if err != nil { 2276 cc.countReadFrameError(err) 2277 return err 2278 } 2279 if VerboseLogs { 2280 cc.vlogf("http2: Transport received %s", summarizeFrame(f)) 2281 } 2282 if !gotSettings { 2283 if _, ok := f.(*SettingsFrame); !ok { 2284 cc.logf("protocol error: received %T before a SETTINGS frame", f) 2285 return ConnectionError(ErrCodeProtocol) 2286 } 2287 gotSettings = true 2288 } 2289 2290 switch f := f.(type) { 2291 case *MetaHeadersFrame: 2292 err = rl.processHeaders(f) 2293 case *DataFrame: 2294 err = rl.processData(f) 2295 case *GoAwayFrame: 2296 err = rl.processGoAway(f) 2297 case *RSTStreamFrame: 2298 err = rl.processResetStream(f) 2299 case *SettingsFrame: 2300 err = rl.processSettings(f) 2301 case *PushPromiseFrame: 2302 err = rl.processPushPromise(f) 2303 case *WindowUpdateFrame: 2304 err = rl.processWindowUpdate(f) 2305 case *PingFrame: 2306 err = rl.processPing(f) 2307 default: 2308 cc.logf("Transport: unhandled response frame type %T", f) 2309 } 2310 if err != nil { 2311 if VerboseLogs { 2312 cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, summarizeFrame(f), err) 2313 } 2314 return err 2315 } 2316 } 2317 } 2318 2319 func (rl *clientConnReadLoop) processHeaders(f *MetaHeadersFrame) error { 2320 cs := rl.streamByID(f.StreamID) 2321 if cs == nil { 2322 // We'd get here if we canceled a request while the 2323 // server had its response still in flight. So if this 2324 // was just something we canceled, ignore it. 2325 return nil 2326 } 2327 if cs.readClosed { 2328 rl.endStreamError(cs, StreamError{ 2329 StreamID: f.StreamID, 2330 Code: ErrCodeProtocol, 2331 Cause: errors.New("protocol error: headers after END_STREAM"), 2332 }) 2333 return nil 2334 } 2335 if !cs.firstByte { 2336 if cs.trace != nil { 2337 // TODO(bradfitz): move first response byte earlier, 2338 // when we first read the 9 byte header, not waiting 2339 // until all the HEADERS+CONTINUATION frames have been 2340 // merged. This works for now. 2341 traceFirstResponseByte(cs.trace) 2342 } 2343 cs.firstByte = true 2344 } 2345 if !cs.pastHeaders { 2346 cs.pastHeaders = true 2347 } else { 2348 return rl.processTrailers(cs, f) 2349 } 2350 2351 res, err := rl.handleResponse(cs, f) 2352 if err != nil { 2353 if _, ok := err.(ConnectionError); ok { 2354 return err 2355 } 2356 // Any other error type is a stream error. 2357 rl.endStreamError(cs, StreamError{ 2358 StreamID: f.StreamID, 2359 Code: ErrCodeProtocol, 2360 Cause: err, 2361 }) 2362 return nil // return nil from process* funcs to keep conn alive 2363 } 2364 if res == nil { 2365 // (nil, nil) special case. See handleResponse docs. 2366 return nil 2367 } 2368 cs.resTrailer = &res.Trailer 2369 cs.res = res 2370 close(cs.respHeaderRecv) 2371 if f.StreamEnded() { 2372 rl.endStream(cs) 2373 } 2374 return nil 2375 } 2376 2377 // may return error types nil, or ConnectionError. Any other error value 2378 // is a StreamError of type ErrCodeProtocol. The returned error in that case 2379 // is the detail. 2380 // 2381 // As a special case, handleResponse may return (nil, nil) to skip the 2382 // frame (currently only used for 1xx responses). 2383 func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFrame) (*http.Response, error) { 2384 if f.Truncated { 2385 return nil, errResponseHeaderListSize 2386 } 2387 2388 status := f.PseudoValue("status") 2389 if status == "" { 2390 return nil, errors.New("malformed response from server: missing status pseudo header") 2391 } 2392 statusCode, err := strconv.Atoi(status) 2393 if err != nil { 2394 return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header") 2395 } 2396 2397 regularFields := f.RegularFields() 2398 strs := make([]string, len(regularFields)) 2399 header := make(http.Header, len(regularFields)) 2400 res := &http.Response{ 2401 Proto: "HTTP/2.0", 2402 ProtoMajor: 2, 2403 Header: header, 2404 StatusCode: statusCode, 2405 Status: status + " " + http.StatusText(statusCode), 2406 } 2407 for _, hf := range regularFields { 2408 key := canonicalHeader(hf.Name) 2409 if key == "Trailer" { 2410 t := res.Trailer 2411 if t == nil { 2412 t = make(http.Header) 2413 res.Trailer = t 2414 } 2415 foreachHeaderElement(hf.Value, func(v string) { 2416 t[canonicalHeader(v)] = nil 2417 }) 2418 } else { 2419 vv := header[key] 2420 if vv == nil && len(strs) > 0 { 2421 // More than likely this will be a single-element key. 2422 // Most headers aren't multi-valued. 2423 // Set the capacity on strs[0] to 1, so any future append 2424 // won't extend the slice into the other strings. 2425 vv, strs = strs[:1:1], strs[1:] 2426 vv[0] = hf.Value 2427 header[key] = vv 2428 } else { 2429 header[key] = append(vv, hf.Value) 2430 } 2431 } 2432 } 2433 2434 if statusCode >= 100 && statusCode <= 199 { 2435 if f.StreamEnded() { 2436 return nil, errors.New("1xx informational response with END_STREAM flag") 2437 } 2438 cs.num1xx++ 2439 const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http 2440 if cs.num1xx > max1xxResponses { 2441 return nil, errors.New("http2: too many 1xx informational responses") 2442 } 2443 if fn := cs.get1xxTraceFunc(); fn != nil { 2444 if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil { 2445 return nil, err 2446 } 2447 } 2448 if statusCode == 100 { 2449 traceGot100Continue(cs.trace) 2450 select { 2451 case cs.on100 <- struct{}{}: 2452 default: 2453 } 2454 } 2455 cs.pastHeaders = false // do it all again 2456 return nil, nil 2457 } 2458 2459 res.ContentLength = -1 2460 if clens := res.Header["Content-Length"]; len(clens) == 1 { 2461 if cl, err := strconv.ParseUint(clens[0], 10, 63); err == nil { 2462 res.ContentLength = int64(cl) 2463 } else { 2464 // TODO: care? unlike http/1, it won't mess up our framing, so it's 2465 // more safe smuggling-wise to ignore. 2466 } 2467 } else if len(clens) > 1 { 2468 // TODO: care? unlike http/1, it won't mess up our framing, so it's 2469 // more safe smuggling-wise to ignore. 2470 } else if f.StreamEnded() && !cs.isHead { 2471 res.ContentLength = 0 2472 } 2473 2474 if cs.isHead { 2475 res.Body = noBody 2476 return res, nil 2477 } 2478 2479 if f.StreamEnded() { 2480 if res.ContentLength > 0 { 2481 res.Body = missingBody{} 2482 } else { 2483 res.Body = noBody 2484 } 2485 return res, nil 2486 } 2487 2488 cs.bufPipe.setBuffer(&dataBuffer{expected: res.ContentLength}) 2489 cs.bytesRemain = res.ContentLength 2490 res.Body = transportResponseBody{cs} 2491 2492 if cs.requestedGzip && asciiEqualFold(res.Header.Get("Content-Encoding"), "gzip") { 2493 res.Header.Del("Content-Encoding") 2494 res.Header.Del("Content-Length") 2495 res.ContentLength = -1 2496 res.Body = &gzipReader{body: res.Body} 2497 res.Uncompressed = true 2498 } 2499 return res, nil 2500 } 2501 2502 func (rl *clientConnReadLoop) processTrailers(cs *clientStream, f *MetaHeadersFrame) error { 2503 if cs.pastTrailers { 2504 // Too many HEADERS frames for this stream. 2505 return ConnectionError(ErrCodeProtocol) 2506 } 2507 cs.pastTrailers = true 2508 if !f.StreamEnded() { 2509 // We expect that any headers for trailers also 2510 // has END_STREAM. 2511 return ConnectionError(ErrCodeProtocol) 2512 } 2513 if len(f.PseudoFields()) > 0 { 2514 // No pseudo header fields are defined for trailers. 2515 // TODO: ConnectionError might be overly harsh? Check. 2516 return ConnectionError(ErrCodeProtocol) 2517 } 2518 2519 trailer := make(http.Header) 2520 for _, hf := range f.RegularFields() { 2521 key := canonicalHeader(hf.Name) 2522 trailer[key] = append(trailer[key], hf.Value) 2523 } 2524 cs.trailer = trailer 2525 2526 rl.endStream(cs) 2527 return nil 2528 } 2529 2530 // transportResponseBody is the concrete type of Transport.RoundTrip's 2531 // Response.Body. It is an io.ReadCloser. 2532 type transportResponseBody struct { 2533 cs *clientStream 2534 } 2535 2536 func (b transportResponseBody) Read(p []byte) (n int, err error) { 2537 cs := b.cs 2538 cc := cs.cc 2539 2540 if cs.readErr != nil { 2541 return 0, cs.readErr 2542 } 2543 n, err = b.cs.bufPipe.Read(p) 2544 if cs.bytesRemain != -1 { 2545 if int64(n) > cs.bytesRemain { 2546 n = int(cs.bytesRemain) 2547 if err == nil { 2548 err = errors.New("net/http: server replied with more than declared Content-Length; truncated") 2549 cs.abortStream(err) 2550 } 2551 cs.readErr = err 2552 return int(cs.bytesRemain), err 2553 } 2554 cs.bytesRemain -= int64(n) 2555 if err == io.EOF && cs.bytesRemain > 0 { 2556 err = io.ErrUnexpectedEOF 2557 cs.readErr = err 2558 return n, err 2559 } 2560 } 2561 if n == 0 { 2562 // No flow control tokens to send back. 2563 return 2564 } 2565 2566 cc.mu.Lock() 2567 connAdd := cc.inflow.add(n) 2568 var streamAdd int32 2569 if err == nil { // No need to refresh if the stream is over or failed. 2570 streamAdd = cs.inflow.add(n) 2571 } 2572 cc.mu.Unlock() 2573 2574 if connAdd != 0 || streamAdd != 0 { 2575 cc.wmu.Lock() 2576 defer cc.wmu.Unlock() 2577 if connAdd != 0 { 2578 cc.fr.WriteWindowUpdate(0, mustUint31(connAdd)) 2579 } 2580 if streamAdd != 0 { 2581 cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd)) 2582 } 2583 cc.bw.Flush() 2584 } 2585 return 2586 } 2587 2588 var errClosedResponseBody = errors.New("http2: response body closed") 2589 2590 func (b transportResponseBody) Close() error { 2591 cs := b.cs 2592 cc := cs.cc 2593 2594 cs.bufPipe.BreakWithError(errClosedResponseBody) 2595 cs.abortStream(errClosedResponseBody) 2596 2597 unread := cs.bufPipe.Len() 2598 if unread > 0 { 2599 cc.mu.Lock() 2600 // Return connection-level flow control. 2601 connAdd := cc.inflow.add(unread) 2602 cc.mu.Unlock() 2603 2604 // TODO(dneil): Acquiring this mutex can block indefinitely. 2605 // Move flow control return to a goroutine? 2606 cc.wmu.Lock() 2607 // Return connection-level flow control. 2608 if connAdd > 0 { 2609 cc.fr.WriteWindowUpdate(0, uint32(connAdd)) 2610 } 2611 cc.bw.Flush() 2612 cc.wmu.Unlock() 2613 } 2614 2615 select { 2616 case <-cs.donec: 2617 case <-cs.ctx.Done(): 2618 // See golang/go#49366: The net/http package can cancel the 2619 // request context after the response body is fully read. 2620 // Don't treat this as an error. 2621 return nil 2622 case <-cs.reqCancel: 2623 return errRequestCanceled 2624 } 2625 return nil 2626 } 2627 2628 func (rl *clientConnReadLoop) processData(f *DataFrame) error { 2629 cc := rl.cc 2630 cs := rl.streamByID(f.StreamID) 2631 data := f.Data() 2632 if cs == nil { 2633 cc.mu.Lock() 2634 neverSent := cc.nextStreamID 2635 cc.mu.Unlock() 2636 if f.StreamID >= neverSent { 2637 // We never asked for this. 2638 cc.logf("http2: Transport received unsolicited DATA frame; closing connection") 2639 return ConnectionError(ErrCodeProtocol) 2640 } 2641 // We probably did ask for this, but canceled. Just ignore it. 2642 // TODO: be stricter here? only silently ignore things which 2643 // we canceled, but not things which were closed normally 2644 // by the peer? Tough without accumulating too much state. 2645 2646 // But at least return their flow control: 2647 if f.Length > 0 { 2648 cc.mu.Lock() 2649 ok := cc.inflow.take(f.Length) 2650 connAdd := cc.inflow.add(int(f.Length)) 2651 cc.mu.Unlock() 2652 if !ok { 2653 return ConnectionError(ErrCodeFlowControl) 2654 } 2655 if connAdd > 0 { 2656 cc.wmu.Lock() 2657 cc.fr.WriteWindowUpdate(0, uint32(connAdd)) 2658 cc.bw.Flush() 2659 cc.wmu.Unlock() 2660 } 2661 } 2662 return nil 2663 } 2664 if cs.readClosed { 2665 cc.logf("protocol error: received DATA after END_STREAM") 2666 rl.endStreamError(cs, StreamError{ 2667 StreamID: f.StreamID, 2668 Code: ErrCodeProtocol, 2669 }) 2670 return nil 2671 } 2672 if !cs.firstByte { 2673 cc.logf("protocol error: received DATA before a HEADERS frame") 2674 rl.endStreamError(cs, StreamError{ 2675 StreamID: f.StreamID, 2676 Code: ErrCodeProtocol, 2677 }) 2678 return nil 2679 } 2680 if f.Length > 0 { 2681 if cs.isHead && len(data) > 0 { 2682 cc.logf("protocol error: received DATA on a HEAD request") 2683 rl.endStreamError(cs, StreamError{ 2684 StreamID: f.StreamID, 2685 Code: ErrCodeProtocol, 2686 }) 2687 return nil 2688 } 2689 // Check connection-level flow control. 2690 cc.mu.Lock() 2691 if !takeInflows(&cc.inflow, &cs.inflow, f.Length) { 2692 cc.mu.Unlock() 2693 return ConnectionError(ErrCodeFlowControl) 2694 } 2695 // Return any padded flow control now, since we won't 2696 // refund it later on body reads. 2697 var refund int 2698 if pad := int(f.Length) - len(data); pad > 0 { 2699 refund += pad 2700 } 2701 2702 didReset := false 2703 var err error 2704 if len(data) > 0 { 2705 if _, err = cs.bufPipe.Write(data); err != nil { 2706 // Return len(data) now if the stream is already closed, 2707 // since data will never be read. 2708 didReset = true 2709 refund += len(data) 2710 } 2711 } 2712 2713 sendConn := cc.inflow.add(refund) 2714 var sendStream int32 2715 if !didReset { 2716 sendStream = cs.inflow.add(refund) 2717 } 2718 cc.mu.Unlock() 2719 2720 if sendConn > 0 || sendStream > 0 { 2721 cc.wmu.Lock() 2722 if sendConn > 0 { 2723 cc.fr.WriteWindowUpdate(0, uint32(sendConn)) 2724 } 2725 if sendStream > 0 { 2726 cc.fr.WriteWindowUpdate(cs.ID, uint32(sendStream)) 2727 } 2728 cc.bw.Flush() 2729 cc.wmu.Unlock() 2730 } 2731 2732 if err != nil { 2733 rl.endStreamError(cs, err) 2734 return nil 2735 } 2736 } 2737 2738 if f.StreamEnded() { 2739 rl.endStream(cs) 2740 } 2741 return nil 2742 } 2743 2744 func (rl *clientConnReadLoop) endStream(cs *clientStream) { 2745 // TODO: check that any declared content-length matches, like 2746 // server.go's (*stream).endStream method. 2747 if !cs.readClosed { 2748 cs.readClosed = true 2749 // Close cs.bufPipe and cs.peerClosed with cc.mu held to avoid a 2750 // race condition: The caller can read io.EOF from Response.Body 2751 // and close the body before we close cs.peerClosed, causing 2752 // cleanupWriteRequest to send a RST_STREAM. 2753 rl.cc.mu.Lock() 2754 defer rl.cc.mu.Unlock() 2755 cs.bufPipe.closeWithErrorAndCode(io.EOF, cs.copyTrailers) 2756 close(cs.peerClosed) 2757 } 2758 } 2759 2760 func (rl *clientConnReadLoop) endStreamError(cs *clientStream, err error) { 2761 cs.readAborted = true 2762 cs.abortStream(err) 2763 } 2764 2765 func (rl *clientConnReadLoop) streamByID(id uint32) *clientStream { 2766 rl.cc.mu.Lock() 2767 defer rl.cc.mu.Unlock() 2768 cs := rl.cc.streams[id] 2769 if cs != nil && !cs.readAborted { 2770 return cs 2771 } 2772 return nil 2773 } 2774 2775 func (cs *clientStream) copyTrailers() { 2776 for k, vv := range cs.trailer { 2777 t := cs.resTrailer 2778 if *t == nil { 2779 *t = make(http.Header) 2780 } 2781 (*t)[k] = vv 2782 } 2783 } 2784 2785 func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error { 2786 cc := rl.cc 2787 cc.t.connPool().MarkDead(cc) 2788 if f.ErrCode != 0 { 2789 // TODO: deal with GOAWAY more. particularly the error code 2790 cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode) 2791 if fn := cc.t.CountError; fn != nil { 2792 fn("recv_goaway_" + f.ErrCode.stringToken()) 2793 } 2794 } 2795 cc.setGoAway(f) 2796 return nil 2797 } 2798 2799 func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error { 2800 cc := rl.cc 2801 // Locking both mu and wmu here allows frame encoding to read settings with only wmu held. 2802 // Acquiring wmu when f.IsAck() is unnecessary, but convenient and mostly harmless. 2803 cc.wmu.Lock() 2804 defer cc.wmu.Unlock() 2805 2806 if err := rl.processSettingsNoWrite(f); err != nil { 2807 return err 2808 } 2809 if !f.IsAck() { 2810 cc.fr.WriteSettingsAck() 2811 cc.bw.Flush() 2812 } 2813 return nil 2814 } 2815 2816 func (rl *clientConnReadLoop) processSettingsNoWrite(f *SettingsFrame) error { 2817 cc := rl.cc 2818 cc.mu.Lock() 2819 defer cc.mu.Unlock() 2820 2821 if f.IsAck() { 2822 if cc.wantSettingsAck { 2823 cc.wantSettingsAck = false 2824 return nil 2825 } 2826 return ConnectionError(ErrCodeProtocol) 2827 } 2828 2829 var seenMaxConcurrentStreams bool 2830 err := f.ForeachSetting(func(s Setting) error { 2831 switch s.ID { 2832 case SettingMaxFrameSize: 2833 cc.maxFrameSize = s.Val 2834 case SettingMaxConcurrentStreams: 2835 cc.maxConcurrentStreams = s.Val 2836 seenMaxConcurrentStreams = true 2837 case SettingMaxHeaderListSize: 2838 cc.peerMaxHeaderListSize = uint64(s.Val) 2839 case SettingInitialWindowSize: 2840 // Values above the maximum flow-control 2841 // window size of 2^31-1 MUST be treated as a 2842 // connection error (Section 5.4.1) of type 2843 // FLOW_CONTROL_ERROR. 2844 if s.Val > math.MaxInt32 { 2845 return ConnectionError(ErrCodeFlowControl) 2846 } 2847 2848 // Adjust flow control of currently-open 2849 // frames by the difference of the old initial 2850 // window size and this one. 2851 delta := int32(s.Val) - int32(cc.initialWindowSize) 2852 for _, cs := range cc.streams { 2853 cs.flow.add(delta) 2854 } 2855 cc.cond.Broadcast() 2856 2857 cc.initialWindowSize = s.Val 2858 case SettingHeaderTableSize: 2859 cc.henc.SetMaxDynamicTableSize(s.Val) 2860 cc.peerMaxHeaderTableSize = s.Val 2861 default: 2862 cc.vlogf("Unhandled Setting: %v", s) 2863 } 2864 return nil 2865 }) 2866 if err != nil { 2867 return err 2868 } 2869 2870 if !cc.seenSettings { 2871 if !seenMaxConcurrentStreams { 2872 // This was the servers initial SETTINGS frame and it 2873 // didn't contain a MAX_CONCURRENT_STREAMS field so 2874 // increase the number of concurrent streams this 2875 // connection can establish to our default. 2876 cc.maxConcurrentStreams = defaultMaxConcurrentStreams 2877 } 2878 cc.seenSettings = true 2879 } 2880 2881 return nil 2882 } 2883 2884 func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error { 2885 cc := rl.cc 2886 cs := rl.streamByID(f.StreamID) 2887 if f.StreamID != 0 && cs == nil { 2888 return nil 2889 } 2890 2891 cc.mu.Lock() 2892 defer cc.mu.Unlock() 2893 2894 fl := &cc.flow 2895 if cs != nil { 2896 fl = &cs.flow 2897 } 2898 if !fl.add(int32(f.Increment)) { 2899 return ConnectionError(ErrCodeFlowControl) 2900 } 2901 cc.cond.Broadcast() 2902 return nil 2903 } 2904 2905 func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error { 2906 cs := rl.streamByID(f.StreamID) 2907 if cs == nil { 2908 // TODO: return error if server tries to RST_STREAM an idle stream 2909 return nil 2910 } 2911 serr := streamError(cs.ID, f.ErrCode) 2912 serr.Cause = errFromPeer 2913 if f.ErrCode == ErrCodeProtocol { 2914 rl.cc.SetDoNotReuse() 2915 } 2916 if fn := cs.cc.t.CountError; fn != nil { 2917 fn("recv_rststream_" + f.ErrCode.stringToken()) 2918 } 2919 cs.abortStream(serr) 2920 2921 cs.bufPipe.CloseWithError(serr) 2922 return nil 2923 } 2924 2925 // Ping sends a PING frame to the server and waits for the ack. 2926 func (cc *ClientConn) Ping(ctx context.Context) error { 2927 c := make(chan struct{}) 2928 // Generate a random payload 2929 var p [8]byte 2930 for { 2931 if _, err := rand.Read(p[:]); err != nil { 2932 return err 2933 } 2934 cc.mu.Lock() 2935 // check for dup before insert 2936 if _, found := cc.pings[p]; !found { 2937 cc.pings[p] = c 2938 cc.mu.Unlock() 2939 break 2940 } 2941 cc.mu.Unlock() 2942 } 2943 errc := make(chan error, 1) 2944 go func() { 2945 cc.wmu.Lock() 2946 defer cc.wmu.Unlock() 2947 if err := cc.fr.WritePing(false, p); err != nil { 2948 errc <- err 2949 return 2950 } 2951 if err := cc.bw.Flush(); err != nil { 2952 errc <- err 2953 return 2954 } 2955 }() 2956 select { 2957 case <-c: 2958 return nil 2959 case err := <-errc: 2960 return err 2961 case <-ctx.Done(): 2962 return ctx.Err() 2963 case <-cc.readerDone: 2964 // connection closed 2965 return cc.readerErr 2966 } 2967 } 2968 2969 func (rl *clientConnReadLoop) processPing(f *PingFrame) error { 2970 if f.IsAck() { 2971 cc := rl.cc 2972 cc.mu.Lock() 2973 defer cc.mu.Unlock() 2974 // If ack, notify listener if any 2975 if c, ok := cc.pings[f.Data]; ok { 2976 close(c) 2977 delete(cc.pings, f.Data) 2978 } 2979 return nil 2980 } 2981 cc := rl.cc 2982 cc.wmu.Lock() 2983 defer cc.wmu.Unlock() 2984 if err := cc.fr.WritePing(true, f.Data); err != nil { 2985 return err 2986 } 2987 return cc.bw.Flush() 2988 } 2989 2990 func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error { 2991 // We told the peer we don't want them. 2992 // Spec says: 2993 // "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH 2994 // setting of the peer endpoint is set to 0. An endpoint that 2995 // has set this setting and has received acknowledgement MUST 2996 // treat the receipt of a PUSH_PROMISE frame as a connection 2997 // error (Section 5.4.1) of type PROTOCOL_ERROR." 2998 return ConnectionError(ErrCodeProtocol) 2999 } 3000 3001 func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) { 3002 // TODO: map err to more interesting error codes, once the 3003 // HTTP community comes up with some. But currently for 3004 // RST_STREAM there's no equivalent to GOAWAY frame's debug 3005 // data, and the error codes are all pretty vague ("cancel"). 3006 cc.wmu.Lock() 3007 cc.fr.WriteRSTStream(streamID, code) 3008 cc.bw.Flush() 3009 cc.wmu.Unlock() 3010 } 3011 3012 var ( 3013 errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit") 3014 errRequestHeaderListSize = errors.New("http2: request header list larger than peer's advertised limit") 3015 ) 3016 3017 func (cc *ClientConn) logf(format string, args ...interface{}) { 3018 cc.t.logf(format, args...) 3019 } 3020 3021 func (cc *ClientConn) vlogf(format string, args ...interface{}) { 3022 cc.t.vlogf(format, args...) 3023 } 3024 3025 func (t *Transport) vlogf(format string, args ...interface{}) { 3026 if VerboseLogs { 3027 t.logf(format, args...) 3028 } 3029 } 3030 3031 func (t *Transport) logf(format string, args ...interface{}) { 3032 log.Printf(format, args...) 3033 } 3034 3035 var noBody io.ReadCloser = noBodyReader{} 3036 3037 type noBodyReader struct{} 3038 3039 func (noBodyReader) Close() error { return nil } 3040 func (noBodyReader) Read([]byte) (int, error) { return 0, io.EOF } 3041 3042 type missingBody struct{} 3043 3044 func (missingBody) Close() error { return nil } 3045 func (missingBody) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF } 3046 3047 func strSliceContains(ss []string, s string) bool { 3048 for _, v := range ss { 3049 if v == s { 3050 return true 3051 } 3052 } 3053 return false 3054 } 3055 3056 type erringRoundTripper struct{ err error } 3057 3058 func (rt erringRoundTripper) RoundTripErr() error { return rt.err } 3059 func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err } 3060 3061 // gzipReader wraps a response body so it can lazily 3062 // call gzip.NewReader on the first call to Read 3063 type gzipReader struct { 3064 _ incomparable 3065 body io.ReadCloser // underlying Response.Body 3066 zr *gzip.Reader // lazily-initialized gzip reader 3067 zerr error // sticky error 3068 } 3069 3070 func (gz *gzipReader) Read(p []byte) (n int, err error) { 3071 if gz.zerr != nil { 3072 return 0, gz.zerr 3073 } 3074 if gz.zr == nil { 3075 gz.zr, err = gzip.NewReader(gz.body) 3076 if err != nil { 3077 gz.zerr = err 3078 return 0, err 3079 } 3080 } 3081 return gz.zr.Read(p) 3082 } 3083 3084 func (gz *gzipReader) Close() error { 3085 if err := gz.body.Close(); err != nil { 3086 return err 3087 } 3088 gz.zerr = fs.ErrClosed 3089 return nil 3090 } 3091 3092 type errorReader struct{ err error } 3093 3094 func (r errorReader) Read(p []byte) (int, error) { return 0, r.err } 3095 3096 // isConnectionCloseRequest reports whether req should use its own 3097 // connection for a single request and then close the connection. 3098 func isConnectionCloseRequest(req *http.Request) bool { 3099 return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close") 3100 } 3101 3102 // registerHTTPSProtocol calls Transport.RegisterProtocol but 3103 // converting panics into errors. 3104 func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) { 3105 defer func() { 3106 if e := recover(); e != nil { 3107 err = fmt.Errorf("%v", e) 3108 } 3109 }() 3110 t.RegisterProtocol("https", rt) 3111 return nil 3112 } 3113 3114 // noDialH2RoundTripper is a RoundTripper which only tries to complete the request 3115 // if there's already has a cached connection to the host. 3116 // (The field is exported so it can be accessed via reflect from net/http; tested 3117 // by TestNoDialH2RoundTripperType) 3118 type noDialH2RoundTripper struct{ *Transport } 3119 3120 func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { 3121 res, err := rt.Transport.RoundTrip(req) 3122 if isNoCachedConnError(err) { 3123 return nil, http.ErrSkipAltProtocol 3124 } 3125 return res, err 3126 } 3127 3128 func (t *Transport) idleConnTimeout() time.Duration { 3129 if t.t1 != nil { 3130 return t.t1.IdleConnTimeout 3131 } 3132 return 0 3133 } 3134 3135 func traceGetConn(req *http.Request, hostPort string) { 3136 trace := httptrace.ContextClientTrace(req.Context()) 3137 if trace == nil || trace.GetConn == nil { 3138 return 3139 } 3140 trace.GetConn(hostPort) 3141 } 3142 3143 func traceGotConn(req *http.Request, cc *ClientConn, reused bool) { 3144 trace := httptrace.ContextClientTrace(req.Context()) 3145 if trace == nil || trace.GotConn == nil { 3146 return 3147 } 3148 ci := httptrace.GotConnInfo{Conn: cc.tconn} 3149 ci.Reused = reused 3150 cc.mu.Lock() 3151 ci.WasIdle = len(cc.streams) == 0 && reused 3152 if ci.WasIdle && !cc.lastActive.IsZero() { 3153 ci.IdleTime = time.Since(cc.lastActive) 3154 } 3155 cc.mu.Unlock() 3156 3157 trace.GotConn(ci) 3158 } 3159 3160 func traceWroteHeaders(trace *httptrace.ClientTrace) { 3161 if trace != nil && trace.WroteHeaders != nil { 3162 trace.WroteHeaders() 3163 } 3164 } 3165 3166 func traceGot100Continue(trace *httptrace.ClientTrace) { 3167 if trace != nil && trace.Got100Continue != nil { 3168 trace.Got100Continue() 3169 } 3170 } 3171 3172 func traceWait100Continue(trace *httptrace.ClientTrace) { 3173 if trace != nil && trace.Wait100Continue != nil { 3174 trace.Wait100Continue() 3175 } 3176 } 3177 3178 func traceWroteRequest(trace *httptrace.ClientTrace, err error) { 3179 if trace != nil && trace.WroteRequest != nil { 3180 trace.WroteRequest(httptrace.WroteRequestInfo{Err: err}) 3181 } 3182 } 3183 3184 func traceFirstResponseByte(trace *httptrace.ClientTrace) { 3185 if trace != nil && trace.GotFirstResponseByte != nil { 3186 trace.GotFirstResponseByte() 3187 } 3188 }