transport.go 72 KB

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