transport.go 45 KB

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