transport.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. "crypto/tls"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "io/ioutil"
  15. "log"
  16. "net"
  17. "net/http"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "golang.org/x/net/http2/hpack"
  23. )
  24. const (
  25. // transportDefaultConnFlow is how many connection-level flow control
  26. // tokens we give the server at start-up, past the default 64k.
  27. transportDefaultConnFlow = 1 << 30
  28. // transportDefaultStreamFlow is how many stream-level flow
  29. // control tokens we announce to the peer, and how many bytes
  30. // we buffer per stream.
  31. transportDefaultStreamFlow = 4 << 20
  32. // transportDefaultStreamMinRefresh is the minimum number of bytes we'll send
  33. // a stream-level WINDOW_UPDATE for at a time.
  34. transportDefaultStreamMinRefresh = 4 << 10
  35. defaultUserAgent = "Go-http-client/2.0"
  36. )
  37. // Transport is an HTTP/2 Transport.
  38. //
  39. // A Transport internally caches connections to servers. It is safe
  40. // for concurrent use by multiple goroutines.
  41. type Transport struct {
  42. // DialTLS specifies an optional dial function for creating
  43. // TLS connections for requests.
  44. //
  45. // If DialTLS is nil, tls.Dial is used.
  46. //
  47. // If the returned net.Conn has a ConnectionState method like tls.Conn,
  48. // it will be used to set http.Response.TLS.
  49. DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
  50. // TLSClientConfig specifies the TLS configuration to use with
  51. // tls.Client. If nil, the default configuration is used.
  52. TLSClientConfig *tls.Config
  53. // ConnPool optionally specifies an alternate connection pool to use.
  54. // If nil, the default is used.
  55. ConnPool ClientConnPool
  56. // DisableCompression, if true, prevents the Transport from
  57. // requesting compression with an "Accept-Encoding: gzip"
  58. // request header when the Request contains no existing
  59. // Accept-Encoding value. If the Transport requests gzip on
  60. // its own and gets a gzipped response, it's transparently
  61. // decoded in the Response.Body. However, if the user
  62. // explicitly requested gzip it is not automatically
  63. // uncompressed.
  64. DisableCompression bool
  65. // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
  66. // send in the initial settings frame. It is how many bytes
  67. // of response headers are allow. Unlike the http2 spec, zero here
  68. // means to use a default limit (currently 10MB). If you actually
  69. // want to advertise an ulimited value to the peer, Transport
  70. // interprets the highest possible value here (0xffffffff or 1<<32-1)
  71. // to mean no limit.
  72. MaxHeaderListSize uint32
  73. connPoolOnce sync.Once
  74. connPoolOrDef ClientConnPool // non-nil version of ConnPool
  75. }
  76. func (t *Transport) maxHeaderListSize() uint32 {
  77. if t.MaxHeaderListSize == 0 {
  78. return 10 << 20
  79. }
  80. if t.MaxHeaderListSize == 0xffffffff {
  81. return 0
  82. }
  83. return t.MaxHeaderListSize
  84. }
  85. func (t *Transport) disableCompression() bool {
  86. if t.DisableCompression {
  87. return true
  88. }
  89. // TODO: also disable if this transport is somehow linked to an http1 Transport
  90. // and it's configured there?
  91. return false
  92. }
  93. var errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6")
  94. // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
  95. // It requires Go 1.6 or later and returns an error if the net/http package is too old
  96. // or if t1 has already been HTTP/2-enabled.
  97. func ConfigureTransport(t1 *http.Transport) error {
  98. return configureTransport(t1) // in configure_transport.go (go1.6) or go15.go
  99. }
  100. func (t *Transport) connPool() ClientConnPool {
  101. t.connPoolOnce.Do(t.initConnPool)
  102. return t.connPoolOrDef
  103. }
  104. func (t *Transport) initConnPool() {
  105. if t.ConnPool != nil {
  106. t.connPoolOrDef = t.ConnPool
  107. } else {
  108. t.connPoolOrDef = &clientConnPool{t: t}
  109. }
  110. }
  111. // ClientConn is the state of a single HTTP/2 client connection to an
  112. // HTTP/2 server.
  113. type ClientConn struct {
  114. t *Transport
  115. tconn net.Conn // usually *tls.Conn, except specialized impls
  116. tlsState *tls.ConnectionState // nil only for specialized impls
  117. // readLoop goroutine fields:
  118. readerDone chan struct{} // closed on error
  119. readerErr error // set before readerDone is closed
  120. mu sync.Mutex // guards following
  121. cond *sync.Cond // hold mu; broadcast on flow/closed changes
  122. flow flow // our conn-level flow control quota (cs.flow is per stream)
  123. inflow flow // peer's conn-level flow control
  124. closed bool
  125. goAway *GoAwayFrame // if non-nil, the GoAwayFrame we received
  126. streams map[uint32]*clientStream // client-initiated
  127. nextStreamID uint32
  128. bw *bufio.Writer
  129. br *bufio.Reader
  130. fr *Framer
  131. // Settings from peer:
  132. maxFrameSize uint32
  133. maxConcurrentStreams uint32
  134. initialWindowSize uint32
  135. hbuf bytes.Buffer // HPACK encoder writes into this
  136. henc *hpack.Encoder
  137. freeBuf [][]byte
  138. wmu sync.Mutex // held while writing; acquire AFTER wmu if holding both
  139. werr error // first write error that has occurred
  140. }
  141. // clientStream is the state for a single HTTP/2 stream. One of these
  142. // is created for each Transport.RoundTrip call.
  143. type clientStream struct {
  144. cc *ClientConn
  145. req *http.Request
  146. ID uint32
  147. resc chan resAndError
  148. bufPipe pipe // buffered pipe with the flow-controlled response payload
  149. requestedGzip bool
  150. flow flow // guarded by cc.mu
  151. inflow flow // guarded by cc.mu
  152. bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read
  153. readErr error // sticky read error; owned by transportResponseBody.Read
  154. stopReqBody bool // stop writing req body; guarded by cc.mu
  155. peerReset chan struct{} // closed on peer reset
  156. resetErr error // populated before peerReset is closed
  157. done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu
  158. // owned by clientConnReadLoop:
  159. pastHeaders bool // got HEADERS w/ END_HEADERS
  160. pastTrailers bool // got second HEADERS frame w/ END_HEADERS
  161. trailer http.Header // accumulated trailers
  162. resTrailer *http.Header // client's Response.Trailer
  163. }
  164. // awaitRequestCancel runs in its own goroutine and waits for the user
  165. // to either cancel a RoundTrip request (using the provided
  166. // Request.Cancel channel), or for the request to be done (any way it
  167. // might be removed from the cc.streams map: peer reset, successful
  168. // completion, TCP connection breakage, etc)
  169. func (cs *clientStream) awaitRequestCancel(cancel <-chan struct{}) {
  170. if cancel == nil {
  171. return
  172. }
  173. select {
  174. case <-cancel:
  175. cs.bufPipe.CloseWithError(errRequestCanceled)
  176. cs.cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
  177. case <-cs.done:
  178. }
  179. }
  180. // checkReset reports any error sent in a RST_STREAM frame by the
  181. // server.
  182. func (cs *clientStream) checkReset() error {
  183. select {
  184. case <-cs.peerReset:
  185. return cs.resetErr
  186. default:
  187. return nil
  188. }
  189. }
  190. func (cs *clientStream) abortRequestBodyWrite() {
  191. cc := cs.cc
  192. cc.mu.Lock()
  193. cs.stopReqBody = true
  194. cc.cond.Broadcast()
  195. cc.mu.Unlock()
  196. }
  197. type stickyErrWriter struct {
  198. w io.Writer
  199. err *error
  200. }
  201. func (sew stickyErrWriter) Write(p []byte) (n int, err error) {
  202. if *sew.err != nil {
  203. return 0, *sew.err
  204. }
  205. n, err = sew.w.Write(p)
  206. *sew.err = err
  207. return
  208. }
  209. var ErrNoCachedConn = errors.New("http2: no cached connection was available")
  210. // RoundTripOpt are options for the Transport.RoundTripOpt method.
  211. type RoundTripOpt struct {
  212. // OnlyCachedConn controls whether RoundTripOpt may
  213. // create a new TCP connection. If set true and
  214. // no cached connection is available, RoundTripOpt
  215. // will return ErrNoCachedConn.
  216. OnlyCachedConn bool
  217. }
  218. func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
  219. return t.RoundTripOpt(req, RoundTripOpt{})
  220. }
  221. // authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
  222. // and returns a host:port. The port 443 is added if needed.
  223. func authorityAddr(authority string) (addr string) {
  224. if _, _, err := net.SplitHostPort(authority); err == nil {
  225. return authority
  226. }
  227. return net.JoinHostPort(authority, "443")
  228. }
  229. // RoundTripOpt is like RoundTrip, but takes options.
  230. func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
  231. if req.URL.Scheme != "https" {
  232. return nil, errors.New("http2: unsupported scheme")
  233. }
  234. addr := authorityAddr(req.URL.Host)
  235. for {
  236. cc, err := t.connPool().GetClientConn(req, addr)
  237. if err != nil {
  238. t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
  239. return nil, err
  240. }
  241. res, err := cc.RoundTrip(req)
  242. if shouldRetryRequest(req, err) {
  243. continue
  244. }
  245. if err != nil {
  246. t.vlogf("RoundTrip failure: %v", err)
  247. return nil, err
  248. }
  249. return res, nil
  250. }
  251. }
  252. // CloseIdleConnections closes any connections which were previously
  253. // connected from previous requests but are now sitting idle.
  254. // It does not interrupt any connections currently in use.
  255. func (t *Transport) CloseIdleConnections() {
  256. if cp, ok := t.connPool().(*clientConnPool); ok {
  257. cp.closeIdleConnections()
  258. }
  259. }
  260. var (
  261. errClientConnClosed = errors.New("http2: client conn is closed")
  262. errClientConnUnusable = errors.New("http2: client conn not usable")
  263. )
  264. func shouldRetryRequest(req *http.Request, err error) bool {
  265. // TODO: retry GET requests (no bodies) more aggressively, if shutdown
  266. // before response.
  267. return err == errClientConnUnusable
  268. }
  269. func (t *Transport) dialClientConn(addr string) (*ClientConn, error) {
  270. host, _, err := net.SplitHostPort(addr)
  271. if err != nil {
  272. return nil, err
  273. }
  274. tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host))
  275. if err != nil {
  276. return nil, err
  277. }
  278. return t.NewClientConn(tconn)
  279. }
  280. func (t *Transport) newTLSConfig(host string) *tls.Config {
  281. cfg := new(tls.Config)
  282. if t.TLSClientConfig != nil {
  283. *cfg = *t.TLSClientConfig
  284. }
  285. cfg.NextProtos = []string{NextProtoTLS} // TODO: don't override if already in list
  286. cfg.ServerName = host
  287. return cfg
  288. }
  289. func (t *Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) {
  290. if t.DialTLS != nil {
  291. return t.DialTLS
  292. }
  293. return t.dialTLSDefault
  294. }
  295. func (t *Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) {
  296. cn, err := tls.Dial(network, addr, cfg)
  297. if err != nil {
  298. return nil, err
  299. }
  300. if err := cn.Handshake(); err != nil {
  301. return nil, err
  302. }
  303. if !cfg.InsecureSkipVerify {
  304. if err := cn.VerifyHostname(cfg.ServerName); err != nil {
  305. return nil, err
  306. }
  307. }
  308. state := cn.ConnectionState()
  309. if p := state.NegotiatedProtocol; p != NextProtoTLS {
  310. return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
  311. }
  312. if !state.NegotiatedProtocolIsMutual {
  313. return nil, errors.New("http2: could not negotiate protocol mutually")
  314. }
  315. return cn, nil
  316. }
  317. func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) {
  318. if VerboseLogs {
  319. t.vlogf("http2: Transport creating client conn to %v", c.RemoteAddr())
  320. }
  321. if _, err := c.Write(clientPreface); err != nil {
  322. t.vlogf("client preface write error: %v", err)
  323. return nil, err
  324. }
  325. cc := &ClientConn{
  326. t: t,
  327. tconn: c,
  328. readerDone: make(chan struct{}),
  329. nextStreamID: 1,
  330. maxFrameSize: 16 << 10, // spec default
  331. initialWindowSize: 65535, // spec default
  332. maxConcurrentStreams: 1000, // "infinite", per spec. 1000 seems good enough.
  333. streams: make(map[uint32]*clientStream),
  334. }
  335. cc.cond = sync.NewCond(&cc.mu)
  336. cc.flow.add(int32(initialWindowSize))
  337. // TODO: adjust this writer size to account for frame size +
  338. // MTU + crypto/tls record padding.
  339. cc.bw = bufio.NewWriter(stickyErrWriter{c, &cc.werr})
  340. cc.br = bufio.NewReader(c)
  341. cc.fr = NewFramer(cc.bw, cc.br)
  342. // TODO: SetMaxDynamicTableSize, SetMaxDynamicTableSizeLimit on
  343. // henc in response to SETTINGS frames?
  344. cc.henc = hpack.NewEncoder(&cc.hbuf)
  345. type connectionStater interface {
  346. ConnectionState() tls.ConnectionState
  347. }
  348. if cs, ok := c.(connectionStater); ok {
  349. state := cs.ConnectionState()
  350. cc.tlsState = &state
  351. }
  352. initialSettings := []Setting{
  353. Setting{ID: SettingEnablePush, Val: 0},
  354. Setting{ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow},
  355. }
  356. if max := t.maxHeaderListSize(); max != 0 {
  357. initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max})
  358. }
  359. cc.fr.WriteSettings(initialSettings...)
  360. cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow)
  361. cc.inflow.add(transportDefaultConnFlow + initialWindowSize)
  362. cc.bw.Flush()
  363. if cc.werr != nil {
  364. return nil, cc.werr
  365. }
  366. // Read the obligatory SETTINGS frame
  367. f, err := cc.fr.ReadFrame()
  368. if err != nil {
  369. return nil, err
  370. }
  371. sf, ok := f.(*SettingsFrame)
  372. if !ok {
  373. return nil, fmt.Errorf("expected settings frame, got: %T", f)
  374. }
  375. cc.fr.WriteSettingsAck()
  376. cc.bw.Flush()
  377. sf.ForeachSetting(func(s Setting) error {
  378. switch s.ID {
  379. case SettingMaxFrameSize:
  380. cc.maxFrameSize = s.Val
  381. case SettingMaxConcurrentStreams:
  382. cc.maxConcurrentStreams = s.Val
  383. case SettingInitialWindowSize:
  384. cc.initialWindowSize = s.Val
  385. default:
  386. // TODO(bradfitz): handle more; at least SETTINGS_HEADER_TABLE_SIZE?
  387. t.vlogf("Unhandled Setting: %v", s)
  388. }
  389. return nil
  390. })
  391. go cc.readLoop()
  392. return cc, nil
  393. }
  394. func (cc *ClientConn) setGoAway(f *GoAwayFrame) {
  395. cc.mu.Lock()
  396. defer cc.mu.Unlock()
  397. cc.goAway = f
  398. }
  399. func (cc *ClientConn) CanTakeNewRequest() bool {
  400. cc.mu.Lock()
  401. defer cc.mu.Unlock()
  402. return cc.canTakeNewRequestLocked()
  403. }
  404. func (cc *ClientConn) canTakeNewRequestLocked() bool {
  405. return cc.goAway == nil &&
  406. int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) &&
  407. cc.nextStreamID < 2147483647
  408. }
  409. func (cc *ClientConn) closeIfIdle() {
  410. cc.mu.Lock()
  411. if len(cc.streams) > 0 {
  412. cc.mu.Unlock()
  413. return
  414. }
  415. cc.closed = true
  416. // TODO: do clients send GOAWAY too? maybe? Just Close:
  417. cc.mu.Unlock()
  418. cc.tconn.Close()
  419. }
  420. const maxAllocFrameSize = 512 << 10
  421. // frameBuffer returns a scratch buffer suitable for writing DATA frames.
  422. // They're capped at the min of the peer's max frame size or 512KB
  423. // (kinda arbitrarily), but definitely capped so we don't allocate 4GB
  424. // bufers.
  425. func (cc *ClientConn) frameScratchBuffer() []byte {
  426. cc.mu.Lock()
  427. size := cc.maxFrameSize
  428. if size > maxAllocFrameSize {
  429. size = maxAllocFrameSize
  430. }
  431. for i, buf := range cc.freeBuf {
  432. if len(buf) >= int(size) {
  433. cc.freeBuf[i] = nil
  434. cc.mu.Unlock()
  435. return buf[:size]
  436. }
  437. }
  438. cc.mu.Unlock()
  439. return make([]byte, size)
  440. }
  441. func (cc *ClientConn) putFrameScratchBuffer(buf []byte) {
  442. cc.mu.Lock()
  443. defer cc.mu.Unlock()
  444. const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate.
  445. if len(cc.freeBuf) < maxBufs {
  446. cc.freeBuf = append(cc.freeBuf, buf)
  447. return
  448. }
  449. for i, old := range cc.freeBuf {
  450. if old == nil {
  451. cc.freeBuf[i] = buf
  452. return
  453. }
  454. }
  455. // forget about it.
  456. }
  457. // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
  458. // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
  459. var errRequestCanceled = errors.New("net/http: request canceled")
  460. func commaSeparatedTrailers(req *http.Request) (string, error) {
  461. keys := make([]string, 0, len(req.Trailer))
  462. for k := range req.Trailer {
  463. k = http.CanonicalHeaderKey(k)
  464. switch k {
  465. case "Transfer-Encoding", "Trailer", "Content-Length":
  466. return "", &badStringError{"invalid Trailer key", k}
  467. }
  468. keys = append(keys, k)
  469. }
  470. if len(keys) > 0 {
  471. sort.Strings(keys)
  472. // TODO: could do better allocation-wise here, but trailers are rare,
  473. // so being lazy for now.
  474. return strings.Join(keys, ","), nil
  475. }
  476. return "", nil
  477. }
  478. func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
  479. trailers, err := commaSeparatedTrailers(req)
  480. if err != nil {
  481. return nil, err
  482. }
  483. hasTrailers := trailers != ""
  484. cc.mu.Lock()
  485. if cc.closed || !cc.canTakeNewRequestLocked() {
  486. cc.mu.Unlock()
  487. return nil, errClientConnUnusable
  488. }
  489. cs := cc.newStream()
  490. cs.req = req
  491. hasBody := req.Body != nil
  492. // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
  493. if !cc.t.disableCompression() &&
  494. req.Header.Get("Accept-Encoding") == "" &&
  495. req.Header.Get("Range") == "" &&
  496. req.Method != "HEAD" {
  497. // Request gzip only, not deflate. Deflate is ambiguous and
  498. // not as universally supported anyway.
  499. // See: http://www.gzip.org/zlib/zlib_faq.html#faq38
  500. //
  501. // Note that we don't request this for HEAD requests,
  502. // due to a bug in nginx:
  503. // http://trac.nginx.org/nginx/ticket/358
  504. // https://golang.org/issue/5522
  505. //
  506. // We don't request gzip if the request is for a range, since
  507. // auto-decoding a portion of a gzipped document will just fail
  508. // anyway. See https://golang.org/issue/8923
  509. cs.requestedGzip = true
  510. }
  511. // we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is
  512. // sent by writeRequestBody below, along with any Trailers,
  513. // again in form HEADERS{1}, CONTINUATION{0,})
  514. hdrs := cc.encodeHeaders(req, cs.requestedGzip, trailers)
  515. cc.wmu.Lock()
  516. endStream := !hasBody && !hasTrailers
  517. werr := cc.writeHeaders(cs.ID, endStream, hdrs)
  518. cc.wmu.Unlock()
  519. cc.mu.Unlock()
  520. if werr != nil {
  521. if hasBody {
  522. req.Body.Close() // per RoundTripper contract
  523. }
  524. cc.forgetStreamID(cs.ID)
  525. // Don't bother sending a RST_STREAM (our write already failed;
  526. // no need to keep writing)
  527. return nil, werr
  528. }
  529. var bodyCopyErrc chan error // result of body copy
  530. if hasBody {
  531. bodyCopyErrc = make(chan error, 1)
  532. go func() {
  533. bodyCopyErrc <- cs.writeRequestBody(req.Body)
  534. }()
  535. }
  536. readLoopResCh := cs.resc
  537. requestCanceledCh := requestCancel(req)
  538. requestCanceled := false
  539. for {
  540. select {
  541. case re := <-readLoopResCh:
  542. res := re.res
  543. if re.err != nil || res.StatusCode > 299 {
  544. // On error or status code 3xx, 4xx, 5xx, etc abort any
  545. // ongoing write, assuming that the server doesn't care
  546. // about our request body. If the server replied with 1xx or
  547. // 2xx, however, then assume the server DOES potentially
  548. // want our body (e.g. full-duplex streaming:
  549. // golang.org/issue/13444). If it turns out the server
  550. // doesn't, they'll RST_STREAM us soon enough. This is a
  551. // heuristic to avoid adding knobs to Transport. Hopefully
  552. // we can keep it.
  553. cs.abortRequestBodyWrite()
  554. }
  555. if re.err != nil {
  556. cc.forgetStreamID(cs.ID)
  557. return nil, re.err
  558. }
  559. res.Request = req
  560. res.TLS = cc.tlsState
  561. return res, nil
  562. case <-requestCanceledCh:
  563. cc.forgetStreamID(cs.ID)
  564. cs.abortRequestBodyWrite()
  565. if !hasBody {
  566. cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
  567. return nil, errRequestCanceled
  568. }
  569. // If we have a body, wait for the body write to be
  570. // finished before sending the RST_STREAM frame.
  571. requestCanceled = true
  572. requestCanceledCh = nil // to prevent spins
  573. readLoopResCh = nil // ignore responses at this point
  574. case <-cs.peerReset:
  575. if requestCanceled {
  576. // They hung up on us first. No need to write a RST_STREAM.
  577. // But prioritize the request canceled error value, since
  578. // it's likely related. (same spirit as http1 code)
  579. return nil, errRequestCanceled
  580. }
  581. // processResetStream already removed the
  582. // stream from the streams map; no need for
  583. // forgetStreamID.
  584. return nil, cs.resetErr
  585. case err := <-bodyCopyErrc:
  586. if requestCanceled {
  587. cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
  588. return nil, errRequestCanceled
  589. }
  590. if err != nil {
  591. return nil, err
  592. }
  593. }
  594. }
  595. }
  596. // requires cc.wmu be held
  597. func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error {
  598. first := true // first frame written (HEADERS is first, then CONTINUATION)
  599. frameSize := int(cc.maxFrameSize)
  600. for len(hdrs) > 0 && cc.werr == nil {
  601. chunk := hdrs
  602. if len(chunk) > frameSize {
  603. chunk = chunk[:frameSize]
  604. }
  605. hdrs = hdrs[len(chunk):]
  606. endHeaders := len(hdrs) == 0
  607. if first {
  608. cc.fr.WriteHeaders(HeadersFrameParam{
  609. StreamID: streamID,
  610. BlockFragment: chunk,
  611. EndStream: endStream,
  612. EndHeaders: endHeaders,
  613. })
  614. first = false
  615. } else {
  616. cc.fr.WriteContinuation(streamID, endHeaders, chunk)
  617. }
  618. }
  619. // TODO(bradfitz): this Flush could potentially block (as
  620. // could the WriteHeaders call(s) above), which means they
  621. // wouldn't respond to Request.Cancel being readable. That's
  622. // rare, but this should probably be in a goroutine.
  623. cc.bw.Flush()
  624. return cc.werr
  625. }
  626. // errAbortReqBodyWrite is an internal error value.
  627. // It doesn't escape to callers.
  628. var errAbortReqBodyWrite = errors.New("http2: aborting request body write")
  629. func (cs *clientStream) writeRequestBody(body io.ReadCloser) (err error) {
  630. cc := cs.cc
  631. sentEnd := false // whether we sent the final DATA frame w/ END_STREAM
  632. buf := cc.frameScratchBuffer()
  633. defer cc.putFrameScratchBuffer(buf)
  634. defer func() {
  635. // TODO: write h12Compare test showing whether
  636. // Request.Body is closed by the Transport,
  637. // and in multiple cases: server replies <=299 and >299
  638. // while still writing request body
  639. cerr := body.Close()
  640. if err == nil {
  641. err = cerr
  642. }
  643. }()
  644. req := cs.req
  645. hasTrailers := req.Trailer != nil
  646. var sawEOF bool
  647. for !sawEOF {
  648. n, err := body.Read(buf)
  649. if err == io.EOF {
  650. sawEOF = true
  651. err = nil
  652. } else if err != nil {
  653. return err
  654. }
  655. remain := buf[:n]
  656. for len(remain) > 0 && err == nil {
  657. var allowed int32
  658. allowed, err = cs.awaitFlowControl(len(remain))
  659. if err != nil {
  660. return err
  661. }
  662. cc.wmu.Lock()
  663. data := remain[:allowed]
  664. remain = remain[allowed:]
  665. sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
  666. err = cc.fr.WriteData(cs.ID, sentEnd, data)
  667. if err == nil {
  668. // TODO(bradfitz): this flush is for latency, not bandwidth.
  669. // Most requests won't need this. Make this opt-in or opt-out?
  670. // Use some heuristic on the body type? Nagel-like timers?
  671. // Based on 'n'? Only last chunk of this for loop, unless flow control
  672. // tokens are low? For now, always:
  673. err = cc.bw.Flush()
  674. }
  675. cc.wmu.Unlock()
  676. }
  677. if err != nil {
  678. return err
  679. }
  680. }
  681. cc.wmu.Lock()
  682. if !sentEnd {
  683. var trls []byte
  684. if hasTrailers {
  685. cc.mu.Lock()
  686. trls = cc.encodeTrailers(req)
  687. cc.mu.Unlock()
  688. }
  689. // Avoid forgetting to send an END_STREAM if the encoded
  690. // trailers are 0 bytes. Both results produce and END_STREAM.
  691. if len(trls) > 0 {
  692. err = cc.writeHeaders(cs.ID, true, trls)
  693. } else {
  694. err = cc.fr.WriteData(cs.ID, true, nil)
  695. }
  696. }
  697. if ferr := cc.bw.Flush(); ferr != nil && err == nil {
  698. err = ferr
  699. }
  700. cc.wmu.Unlock()
  701. return err
  702. }
  703. // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
  704. // control tokens from the server.
  705. // It returns either the non-zero number of tokens taken or an error
  706. // if the stream is dead.
  707. func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
  708. cc := cs.cc
  709. cc.mu.Lock()
  710. defer cc.mu.Unlock()
  711. for {
  712. if cc.closed {
  713. return 0, errClientConnClosed
  714. }
  715. if cs.stopReqBody {
  716. return 0, errAbortReqBodyWrite
  717. }
  718. if err := cs.checkReset(); err != nil {
  719. return 0, err
  720. }
  721. if a := cs.flow.available(); a > 0 {
  722. take := a
  723. if int(take) > maxBytes {
  724. take = int32(maxBytes) // can't truncate int; take is int32
  725. }
  726. if take > int32(cc.maxFrameSize) {
  727. take = int32(cc.maxFrameSize)
  728. }
  729. cs.flow.take(take)
  730. return take, nil
  731. }
  732. cc.cond.Wait()
  733. }
  734. }
  735. type badStringError struct {
  736. what string
  737. str string
  738. }
  739. func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
  740. // requires cc.mu be held.
  741. func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string) []byte {
  742. cc.hbuf.Reset()
  743. host := req.Host
  744. if host == "" {
  745. host = req.URL.Host
  746. }
  747. // 8.1.2.3 Request Pseudo-Header Fields
  748. // The :path pseudo-header field includes the path and query parts of the
  749. // target URI (the path-absolute production and optionally a '?' character
  750. // followed by the query production (see Sections 3.3 and 3.4 of
  751. // [RFC3986]).
  752. cc.writeHeader(":authority", host)
  753. cc.writeHeader(":method", req.Method)
  754. if req.Method != "CONNECT" {
  755. cc.writeHeader(":path", req.URL.RequestURI())
  756. cc.writeHeader(":scheme", "https")
  757. }
  758. if trailers != "" {
  759. cc.writeHeader("trailer", trailers)
  760. }
  761. var didUA bool
  762. for k, vv := range req.Header {
  763. lowKey := strings.ToLower(k)
  764. if lowKey == "host" {
  765. continue
  766. }
  767. if lowKey == "user-agent" {
  768. // Match Go's http1 behavior: at most one
  769. // User-Agent. If set to nil or empty string,
  770. // then omit it. Otherwise if not mentioned,
  771. // include the default (below).
  772. didUA = true
  773. if len(vv) < 1 {
  774. continue
  775. }
  776. vv = vv[:1]
  777. if vv[0] == "" {
  778. continue
  779. }
  780. }
  781. for _, v := range vv {
  782. cc.writeHeader(lowKey, v)
  783. }
  784. }
  785. if addGzipHeader {
  786. cc.writeHeader("accept-encoding", "gzip")
  787. }
  788. if !didUA {
  789. cc.writeHeader("user-agent", defaultUserAgent)
  790. }
  791. return cc.hbuf.Bytes()
  792. }
  793. // requires cc.mu be held.
  794. func (cc *ClientConn) encodeTrailers(req *http.Request) []byte {
  795. cc.hbuf.Reset()
  796. for k, vv := range req.Trailer {
  797. // Transfer-Encoding, etc.. have already been filter at the
  798. // start of RoundTrip
  799. lowKey := strings.ToLower(k)
  800. for _, v := range vv {
  801. cc.writeHeader(lowKey, v)
  802. }
  803. }
  804. return cc.hbuf.Bytes()
  805. }
  806. func (cc *ClientConn) writeHeader(name, value string) {
  807. if VerboseLogs {
  808. log.Printf("http2: Transport encoding header %q = %q", name, value)
  809. }
  810. cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
  811. }
  812. type resAndError struct {
  813. res *http.Response
  814. err error
  815. }
  816. // requires cc.mu be held.
  817. func (cc *ClientConn) newStream() *clientStream {
  818. cs := &clientStream{
  819. cc: cc,
  820. ID: cc.nextStreamID,
  821. resc: make(chan resAndError, 1),
  822. peerReset: make(chan struct{}),
  823. done: make(chan struct{}),
  824. }
  825. cs.flow.add(int32(cc.initialWindowSize))
  826. cs.flow.setConnFlow(&cc.flow)
  827. cs.inflow.add(transportDefaultStreamFlow)
  828. cs.inflow.setConnFlow(&cc.inflow)
  829. cc.nextStreamID += 2
  830. cc.streams[cs.ID] = cs
  831. return cs
  832. }
  833. func (cc *ClientConn) forgetStreamID(id uint32) {
  834. cc.streamByID(id, true)
  835. }
  836. func (cc *ClientConn) streamByID(id uint32, andRemove bool) *clientStream {
  837. cc.mu.Lock()
  838. defer cc.mu.Unlock()
  839. cs := cc.streams[id]
  840. if andRemove && cs != nil && !cc.closed {
  841. delete(cc.streams, id)
  842. close(cs.done)
  843. }
  844. return cs
  845. }
  846. // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
  847. type clientConnReadLoop struct {
  848. cc *ClientConn
  849. activeRes map[uint32]*clientStream // keyed by streamID
  850. hdec *hpack.Decoder
  851. // Fields reset on each HEADERS:
  852. nextRes *http.Response
  853. sawRegHeader bool // saw non-pseudo header
  854. reqMalformed error // non-nil once known to be malformed
  855. lastHeaderEndsStream bool
  856. headerListSize int64 // actually uint32, but easier math this way
  857. }
  858. // readLoop runs in its own goroutine and reads and dispatches frames.
  859. func (cc *ClientConn) readLoop() {
  860. rl := &clientConnReadLoop{
  861. cc: cc,
  862. activeRes: make(map[uint32]*clientStream),
  863. }
  864. rl.hdec = hpack.NewDecoder(initialHeaderTableSize, rl.onNewHeaderField)
  865. defer rl.cleanup()
  866. cc.readerErr = rl.run()
  867. if ce, ok := cc.readerErr.(ConnectionError); ok {
  868. cc.wmu.Lock()
  869. cc.fr.WriteGoAway(0, ErrCode(ce), nil)
  870. cc.wmu.Unlock()
  871. }
  872. }
  873. func (rl *clientConnReadLoop) cleanup() {
  874. cc := rl.cc
  875. defer cc.tconn.Close()
  876. defer cc.t.connPool().MarkDead(cc)
  877. defer close(cc.readerDone)
  878. // Close any response bodies if the server closes prematurely.
  879. // TODO: also do this if we've written the headers but not
  880. // gotten a response yet.
  881. err := cc.readerErr
  882. if err == io.EOF {
  883. err = io.ErrUnexpectedEOF
  884. }
  885. cc.mu.Lock()
  886. for _, cs := range rl.activeRes {
  887. cs.bufPipe.CloseWithError(err)
  888. }
  889. for _, cs := range cc.streams {
  890. select {
  891. case cs.resc <- resAndError{err: err}:
  892. default:
  893. }
  894. close(cs.done)
  895. }
  896. cc.closed = true
  897. cc.cond.Broadcast()
  898. cc.mu.Unlock()
  899. }
  900. func (rl *clientConnReadLoop) run() error {
  901. cc := rl.cc
  902. for {
  903. f, err := cc.fr.ReadFrame()
  904. if err != nil {
  905. cc.vlogf("Transport readFrame error: (%T) %v", err, err)
  906. }
  907. if se, ok := err.(StreamError); ok {
  908. // TODO: deal with stream errors from the framer.
  909. return se
  910. } else if err != nil {
  911. return err
  912. }
  913. if VerboseLogs {
  914. cc.vlogf("http2: Transport received %s", summarizeFrame(f))
  915. }
  916. switch f := f.(type) {
  917. case *HeadersFrame:
  918. err = rl.processHeaders(f)
  919. case *ContinuationFrame:
  920. err = rl.processContinuation(f)
  921. case *DataFrame:
  922. err = rl.processData(f)
  923. case *GoAwayFrame:
  924. err = rl.processGoAway(f)
  925. case *RSTStreamFrame:
  926. err = rl.processResetStream(f)
  927. case *SettingsFrame:
  928. err = rl.processSettings(f)
  929. case *PushPromiseFrame:
  930. err = rl.processPushPromise(f)
  931. case *WindowUpdateFrame:
  932. err = rl.processWindowUpdate(f)
  933. case *PingFrame:
  934. err = rl.processPing(f)
  935. default:
  936. cc.logf("Transport: unhandled response frame type %T", f)
  937. }
  938. if err != nil {
  939. return err
  940. }
  941. }
  942. }
  943. func (rl *clientConnReadLoop) processHeaders(f *HeadersFrame) error {
  944. rl.sawRegHeader = false
  945. rl.reqMalformed = nil
  946. rl.lastHeaderEndsStream = f.StreamEnded()
  947. rl.headerListSize = 0
  948. rl.nextRes = &http.Response{
  949. Proto: "HTTP/2.0",
  950. ProtoMajor: 2,
  951. Header: make(http.Header),
  952. }
  953. rl.hdec.SetEmitEnabled(true)
  954. return rl.processHeaderBlockFragment(f.HeaderBlockFragment(), f.StreamID, f.HeadersEnded())
  955. }
  956. func (rl *clientConnReadLoop) processContinuation(f *ContinuationFrame) error {
  957. return rl.processHeaderBlockFragment(f.HeaderBlockFragment(), f.StreamID, f.HeadersEnded())
  958. }
  959. func (rl *clientConnReadLoop) processHeaderBlockFragment(frag []byte, streamID uint32, finalFrag bool) error {
  960. cc := rl.cc
  961. streamEnded := rl.lastHeaderEndsStream
  962. cs := cc.streamByID(streamID, streamEnded && finalFrag)
  963. if cs == nil {
  964. // We'd get here if we canceled a request while the
  965. // server was mid-way through replying with its
  966. // headers. (The case of a CONTINUATION arriving
  967. // without HEADERS would be rejected earlier by the
  968. // Framer). So if this was just something we canceled,
  969. // ignore it.
  970. return nil
  971. }
  972. if cs.pastHeaders {
  973. rl.hdec.SetEmitFunc(func(f hpack.HeaderField) { rl.onNewTrailerField(cs, f) })
  974. } else {
  975. rl.hdec.SetEmitFunc(rl.onNewHeaderField)
  976. }
  977. _, err := rl.hdec.Write(frag)
  978. if err != nil {
  979. return ConnectionError(ErrCodeCompression)
  980. }
  981. if finalFrag {
  982. if err := rl.hdec.Close(); err != nil {
  983. return ConnectionError(ErrCodeCompression)
  984. }
  985. }
  986. if !finalFrag {
  987. return nil
  988. }
  989. if !cs.pastHeaders {
  990. cs.pastHeaders = true
  991. } else {
  992. // We're dealing with trailers. (and specifically the
  993. // final frame of headers)
  994. if cs.pastTrailers {
  995. // Too many HEADERS frames for this stream.
  996. return ConnectionError(ErrCodeProtocol)
  997. }
  998. cs.pastTrailers = true
  999. if !streamEnded {
  1000. // We expect that any header block fragment
  1001. // frame for trailers with END_HEADERS also
  1002. // has END_STREAM.
  1003. return ConnectionError(ErrCodeProtocol)
  1004. }
  1005. rl.endStream(cs)
  1006. return nil
  1007. }
  1008. if rl.reqMalformed != nil {
  1009. cs.resc <- resAndError{err: rl.reqMalformed}
  1010. rl.cc.writeStreamReset(cs.ID, ErrCodeProtocol, rl.reqMalformed)
  1011. return nil
  1012. }
  1013. res := rl.nextRes
  1014. if res.StatusCode == 100 {
  1015. // Just skip 100-continue response headers for now.
  1016. // TODO: golang.org/issue/13851 for doing it properly.
  1017. cs.pastHeaders = false // do it all again
  1018. return nil
  1019. }
  1020. if !streamEnded || cs.req.Method == "HEAD" {
  1021. res.ContentLength = -1
  1022. if clens := res.Header["Content-Length"]; len(clens) == 1 {
  1023. if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil {
  1024. res.ContentLength = clen64
  1025. } else {
  1026. // TODO: care? unlike http/1, it won't mess up our framing, so it's
  1027. // more safe smuggling-wise to ignore.
  1028. }
  1029. } else if len(clens) > 1 {
  1030. // TODO: care? unlike http/1, it won't mess up our framing, so it's
  1031. // more safe smuggling-wise to ignore.
  1032. }
  1033. }
  1034. if streamEnded {
  1035. res.Body = noBody
  1036. } else {
  1037. buf := new(bytes.Buffer) // TODO(bradfitz): recycle this garbage
  1038. cs.bufPipe = pipe{b: buf}
  1039. cs.bytesRemain = res.ContentLength
  1040. res.Body = transportResponseBody{cs}
  1041. go cs.awaitRequestCancel(requestCancel(cs.req))
  1042. if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" {
  1043. res.Header.Del("Content-Encoding")
  1044. res.Header.Del("Content-Length")
  1045. res.ContentLength = -1
  1046. res.Body = &gzipReader{body: res.Body}
  1047. }
  1048. }
  1049. cs.resTrailer = &res.Trailer
  1050. rl.activeRes[cs.ID] = cs
  1051. cs.resc <- resAndError{res: res}
  1052. rl.nextRes = nil // unused now; will be reset next HEADERS frame
  1053. return nil
  1054. }
  1055. // transportResponseBody is the concrete type of Transport.RoundTrip's
  1056. // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body.
  1057. // On Close it sends RST_STREAM if EOF wasn't already seen.
  1058. type transportResponseBody struct {
  1059. cs *clientStream
  1060. }
  1061. func (b transportResponseBody) Read(p []byte) (n int, err error) {
  1062. cs := b.cs
  1063. cc := cs.cc
  1064. if cs.readErr != nil {
  1065. return 0, cs.readErr
  1066. }
  1067. n, err = b.cs.bufPipe.Read(p)
  1068. if cs.bytesRemain != -1 {
  1069. if int64(n) > cs.bytesRemain {
  1070. n = int(cs.bytesRemain)
  1071. if err == nil {
  1072. err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
  1073. cc.writeStreamReset(cs.ID, ErrCodeProtocol, err)
  1074. }
  1075. cs.readErr = err
  1076. return int(cs.bytesRemain), err
  1077. }
  1078. cs.bytesRemain -= int64(n)
  1079. if err == io.EOF && cs.bytesRemain > 0 {
  1080. err = io.ErrUnexpectedEOF
  1081. cs.readErr = err
  1082. return n, err
  1083. }
  1084. }
  1085. if n == 0 {
  1086. // No flow control tokens to send back.
  1087. return
  1088. }
  1089. cc.mu.Lock()
  1090. defer cc.mu.Unlock()
  1091. var connAdd, streamAdd int32
  1092. // Check the conn-level first, before the stream-level.
  1093. if v := cc.inflow.available(); v < transportDefaultConnFlow/2 {
  1094. connAdd = transportDefaultConnFlow - v
  1095. cc.inflow.add(connAdd)
  1096. }
  1097. if err == nil { // No need to refresh if the stream is over or failed.
  1098. if v := cs.inflow.available(); v < transportDefaultStreamFlow-transportDefaultStreamMinRefresh {
  1099. streamAdd = transportDefaultStreamFlow - v
  1100. cs.inflow.add(streamAdd)
  1101. }
  1102. }
  1103. if connAdd != 0 || streamAdd != 0 {
  1104. cc.wmu.Lock()
  1105. defer cc.wmu.Unlock()
  1106. if connAdd != 0 {
  1107. cc.fr.WriteWindowUpdate(0, mustUint31(connAdd))
  1108. }
  1109. if streamAdd != 0 {
  1110. cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd))
  1111. }
  1112. cc.bw.Flush()
  1113. }
  1114. return
  1115. }
  1116. var errClosedResponseBody = errors.New("http2: response body closed")
  1117. func (b transportResponseBody) Close() error {
  1118. cs := b.cs
  1119. if cs.bufPipe.Err() != io.EOF {
  1120. // TODO: write test for this
  1121. cs.cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
  1122. }
  1123. cs.bufPipe.BreakWithError(errClosedResponseBody)
  1124. return nil
  1125. }
  1126. func (rl *clientConnReadLoop) processData(f *DataFrame) error {
  1127. cc := rl.cc
  1128. cs := cc.streamByID(f.StreamID, f.StreamEnded())
  1129. if cs == nil {
  1130. return nil
  1131. }
  1132. data := f.Data()
  1133. // Check connection-level flow control.
  1134. cc.mu.Lock()
  1135. if cs.inflow.available() >= int32(len(data)) {
  1136. cs.inflow.take(int32(len(data)))
  1137. } else {
  1138. cc.mu.Unlock()
  1139. return ConnectionError(ErrCodeFlowControl)
  1140. }
  1141. cc.mu.Unlock()
  1142. if _, err := cs.bufPipe.Write(data); err != nil {
  1143. return err
  1144. }
  1145. if f.StreamEnded() {
  1146. rl.endStream(cs)
  1147. }
  1148. return nil
  1149. }
  1150. var errInvalidTrailers = errors.New("http2: invalid trailers")
  1151. func (rl *clientConnReadLoop) endStream(cs *clientStream) {
  1152. // TODO: check that any declared content-length matches, like
  1153. // server.go's (*stream).endStream method.
  1154. err := io.EOF
  1155. code := cs.copyTrailers
  1156. if rl.reqMalformed != nil {
  1157. err = rl.reqMalformed
  1158. code = nil
  1159. }
  1160. cs.bufPipe.closeWithErrorAndCode(err, code)
  1161. delete(rl.activeRes, cs.ID)
  1162. }
  1163. func (cs *clientStream) copyTrailers() {
  1164. for k, vv := range cs.trailer {
  1165. t := cs.resTrailer
  1166. if *t == nil {
  1167. *t = make(http.Header)
  1168. }
  1169. (*t)[k] = vv
  1170. }
  1171. }
  1172. func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error {
  1173. cc := rl.cc
  1174. cc.t.connPool().MarkDead(cc)
  1175. if f.ErrCode != 0 {
  1176. // TODO: deal with GOAWAY more. particularly the error code
  1177. cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
  1178. }
  1179. cc.setGoAway(f)
  1180. return nil
  1181. }
  1182. func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error {
  1183. cc := rl.cc
  1184. cc.mu.Lock()
  1185. defer cc.mu.Unlock()
  1186. return f.ForeachSetting(func(s Setting) error {
  1187. switch s.ID {
  1188. case SettingMaxFrameSize:
  1189. cc.maxFrameSize = s.Val
  1190. case SettingMaxConcurrentStreams:
  1191. cc.maxConcurrentStreams = s.Val
  1192. case SettingInitialWindowSize:
  1193. // TODO: error if this is too large.
  1194. // TODO: adjust flow control of still-open
  1195. // frames by the difference of the old initial
  1196. // window size and this one.
  1197. cc.initialWindowSize = s.Val
  1198. default:
  1199. // TODO(bradfitz): handle more settings? SETTINGS_HEADER_TABLE_SIZE probably.
  1200. cc.vlogf("Unhandled Setting: %v", s)
  1201. }
  1202. return nil
  1203. })
  1204. }
  1205. func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error {
  1206. cc := rl.cc
  1207. cs := cc.streamByID(f.StreamID, false)
  1208. if f.StreamID != 0 && cs == nil {
  1209. return nil
  1210. }
  1211. cc.mu.Lock()
  1212. defer cc.mu.Unlock()
  1213. fl := &cc.flow
  1214. if cs != nil {
  1215. fl = &cs.flow
  1216. }
  1217. if !fl.add(int32(f.Increment)) {
  1218. return ConnectionError(ErrCodeFlowControl)
  1219. }
  1220. cc.cond.Broadcast()
  1221. return nil
  1222. }
  1223. func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error {
  1224. cs := rl.cc.streamByID(f.StreamID, true)
  1225. if cs == nil {
  1226. // TODO: return error if server tries to RST_STEAM an idle stream
  1227. return nil
  1228. }
  1229. select {
  1230. case <-cs.peerReset:
  1231. // Already reset.
  1232. // This is the only goroutine
  1233. // which closes this, so there
  1234. // isn't a race.
  1235. default:
  1236. err := StreamError{cs.ID, f.ErrCode}
  1237. cs.resetErr = err
  1238. close(cs.peerReset)
  1239. cs.bufPipe.CloseWithError(err)
  1240. cs.cc.cond.Broadcast() // wake up checkReset via clientStream.awaitFlowControl
  1241. }
  1242. delete(rl.activeRes, cs.ID)
  1243. return nil
  1244. }
  1245. func (rl *clientConnReadLoop) processPing(f *PingFrame) error {
  1246. if f.IsAck() {
  1247. // 6.7 PING: " An endpoint MUST NOT respond to PING frames
  1248. // containing this flag."
  1249. return nil
  1250. }
  1251. cc := rl.cc
  1252. cc.wmu.Lock()
  1253. defer cc.wmu.Unlock()
  1254. if err := cc.fr.WritePing(true, f.Data); err != nil {
  1255. return err
  1256. }
  1257. return cc.bw.Flush()
  1258. }
  1259. func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error {
  1260. // We told the peer we don't want them.
  1261. // Spec says:
  1262. // "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH
  1263. // setting of the peer endpoint is set to 0. An endpoint that
  1264. // has set this setting and has received acknowledgement MUST
  1265. // treat the receipt of a PUSH_PROMISE frame as a connection
  1266. // error (Section 5.4.1) of type PROTOCOL_ERROR."
  1267. return ConnectionError(ErrCodeProtocol)
  1268. }
  1269. func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) {
  1270. // TODO: do something with err? send it as a debug frame to the peer?
  1271. // But that's only in GOAWAY. Invent a new frame type? Is there one already?
  1272. cc.wmu.Lock()
  1273. cc.fr.WriteRSTStream(streamID, code)
  1274. cc.bw.Flush()
  1275. cc.wmu.Unlock()
  1276. }
  1277. var (
  1278. errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
  1279. errInvalidHeaderKey = errors.New("http2: invalid header key")
  1280. errPseudoTrailers = errors.New("http2: invalid pseudo header in trailers")
  1281. )
  1282. func (rl *clientConnReadLoop) checkHeaderField(f hpack.HeaderField) bool {
  1283. if rl.reqMalformed != nil {
  1284. return false
  1285. }
  1286. const headerFieldOverhead = 32 // per spec
  1287. rl.headerListSize += int64(len(f.Name)) + int64(len(f.Value)) + headerFieldOverhead
  1288. if max := rl.cc.t.maxHeaderListSize(); max != 0 && rl.headerListSize > int64(max) {
  1289. rl.hdec.SetEmitEnabled(false)
  1290. rl.reqMalformed = errResponseHeaderListSize
  1291. return false
  1292. }
  1293. if !validHeader(f.Name) {
  1294. rl.reqMalformed = errInvalidHeaderKey
  1295. return false
  1296. }
  1297. isPseudo := strings.HasPrefix(f.Name, ":")
  1298. if isPseudo {
  1299. if rl.sawRegHeader {
  1300. rl.reqMalformed = errors.New("http2: invalid pseudo header after regular header")
  1301. return false
  1302. }
  1303. } else {
  1304. rl.sawRegHeader = true
  1305. }
  1306. return true
  1307. }
  1308. // onNewHeaderField runs on the readLoop goroutine whenever a new
  1309. // hpack header field is decoded.
  1310. func (rl *clientConnReadLoop) onNewHeaderField(f hpack.HeaderField) {
  1311. cc := rl.cc
  1312. if VerboseLogs {
  1313. cc.logf("http2: Transport decoded %v", f)
  1314. }
  1315. if !rl.checkHeaderField(f) {
  1316. return
  1317. }
  1318. isPseudo := strings.HasPrefix(f.Name, ":")
  1319. if isPseudo {
  1320. switch f.Name {
  1321. case ":status":
  1322. code, err := strconv.Atoi(f.Value)
  1323. if err != nil {
  1324. rl.reqMalformed = errors.New("http2: invalid :status")
  1325. return
  1326. }
  1327. rl.nextRes.Status = f.Value + " " + http.StatusText(code)
  1328. rl.nextRes.StatusCode = code
  1329. default:
  1330. // "Endpoints MUST NOT generate pseudo-header
  1331. // fields other than those defined in this
  1332. // document."
  1333. rl.reqMalformed = fmt.Errorf("http2: unknown response pseudo header %q", f.Name)
  1334. }
  1335. return
  1336. }
  1337. key := http.CanonicalHeaderKey(f.Name)
  1338. if key == "Trailer" {
  1339. t := rl.nextRes.Trailer
  1340. if t == nil {
  1341. t = make(http.Header)
  1342. rl.nextRes.Trailer = t
  1343. }
  1344. foreachHeaderElement(f.Value, func(v string) {
  1345. t[http.CanonicalHeaderKey(v)] = nil
  1346. })
  1347. } else {
  1348. rl.nextRes.Header.Add(key, f.Value)
  1349. }
  1350. }
  1351. func (rl *clientConnReadLoop) onNewTrailerField(cs *clientStream, f hpack.HeaderField) {
  1352. if VerboseLogs {
  1353. rl.cc.logf("http2: Transport decoded trailer %v", f)
  1354. }
  1355. if !rl.checkHeaderField(f) {
  1356. return
  1357. }
  1358. if strings.HasPrefix(f.Name, ":") {
  1359. // Pseudo-header fields MUST NOT appear in
  1360. // trailers. Endpoints MUST treat a request or
  1361. // response that contains undefined or invalid
  1362. // pseudo-header fields as malformed.
  1363. rl.reqMalformed = errPseudoTrailers
  1364. return
  1365. }
  1366. key := http.CanonicalHeaderKey(f.Name)
  1367. // The spec says one must predeclare their trailers but in practice
  1368. // popular users (which is to say the only user we found) do not so we
  1369. // violate the spec and accept all of them.
  1370. const acceptAllTrailers = true
  1371. if _, ok := (*cs.resTrailer)[key]; ok || acceptAllTrailers {
  1372. if cs.trailer == nil {
  1373. cs.trailer = make(http.Header)
  1374. }
  1375. cs.trailer[key] = append(cs.trailer[key], f.Value)
  1376. }
  1377. }
  1378. func (cc *ClientConn) logf(format string, args ...interface{}) {
  1379. cc.t.logf(format, args...)
  1380. }
  1381. func (cc *ClientConn) vlogf(format string, args ...interface{}) {
  1382. cc.t.vlogf(format, args...)
  1383. }
  1384. func (t *Transport) vlogf(format string, args ...interface{}) {
  1385. if VerboseLogs {
  1386. t.logf(format, args...)
  1387. }
  1388. }
  1389. func (t *Transport) logf(format string, args ...interface{}) {
  1390. log.Printf(format, args...)
  1391. }
  1392. var noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
  1393. func strSliceContains(ss []string, s string) bool {
  1394. for _, v := range ss {
  1395. if v == s {
  1396. return true
  1397. }
  1398. }
  1399. return false
  1400. }
  1401. type erringRoundTripper struct{ err error }
  1402. func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err }
  1403. // gzipReader wraps a response body so it can lazily
  1404. // call gzip.NewReader on the first call to Read
  1405. type gzipReader struct {
  1406. body io.ReadCloser // underlying Response.Body
  1407. zr io.Reader // lazily-initialized gzip reader
  1408. }
  1409. func (gz *gzipReader) Read(p []byte) (n int, err error) {
  1410. if gz.zr == nil {
  1411. gz.zr, err = gzip.NewReader(gz.body)
  1412. if err != nil {
  1413. return 0, err
  1414. }
  1415. }
  1416. return gz.zr.Read(p)
  1417. }
  1418. func (gz *gzipReader) Close() error {
  1419. return gz.body.Close()
  1420. }