transport.go 84 KB

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