transport_test.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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. package http2
  5. import (
  6. "bufio"
  7. "bytes"
  8. "crypto/tls"
  9. "errors"
  10. "flag"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "math/rand"
  15. "net"
  16. "net/http"
  17. "net/url"
  18. "os"
  19. "reflect"
  20. "runtime"
  21. "sort"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "sync/atomic"
  26. "testing"
  27. "time"
  28. "golang.org/x/net/http2/hpack"
  29. )
  30. var (
  31. extNet = flag.Bool("extnet", false, "do external network tests")
  32. transportHost = flag.String("transporthost", "http2.golang.org", "hostname to use for TestTransport")
  33. insecure = flag.Bool("insecure", false, "insecure TLS dials") // TODO: dead code. remove?
  34. )
  35. var tlsConfigInsecure = &tls.Config{InsecureSkipVerify: true}
  36. func TestTransportExternal(t *testing.T) {
  37. if !*extNet {
  38. t.Skip("skipping external network test")
  39. }
  40. req, _ := http.NewRequest("GET", "https://"+*transportHost+"/", nil)
  41. rt := &Transport{TLSClientConfig: tlsConfigInsecure}
  42. res, err := rt.RoundTrip(req)
  43. if err != nil {
  44. t.Fatalf("%v", err)
  45. }
  46. res.Write(os.Stdout)
  47. }
  48. func startH2cServer(t *testing.T) net.Listener {
  49. h2Server := &Server{}
  50. l := newLocalListener(t)
  51. go func() {
  52. conn, err := l.Accept()
  53. if err != nil {
  54. t.Error(err)
  55. return
  56. }
  57. h2Server.ServeConn(conn, &ServeConnOpts{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  58. fmt.Fprintf(w, "Hello, %v", r.URL.Path)
  59. })})
  60. }()
  61. return l
  62. }
  63. func TestTransportH2c(t *testing.T) {
  64. l := startH2cServer(t)
  65. defer l.Close()
  66. req, err := http.NewRequest("GET", "http://"+l.Addr().String()+"/foobar", nil)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. tr := &Transport{
  71. AllowHTTP: true,
  72. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  73. return net.Dial(network, addr)
  74. },
  75. }
  76. res, err := tr.RoundTrip(req)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if res.ProtoMajor != 2 {
  81. t.Fatal("proto not h2c")
  82. }
  83. body, err := ioutil.ReadAll(res.Body)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if got, want := string(body), "Hello, /foobar"; got != want {
  88. t.Fatalf("response got %v, want %v", got, want)
  89. }
  90. }
  91. func TestTransport(t *testing.T) {
  92. const body = "sup"
  93. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  94. io.WriteString(w, body)
  95. }, optOnlyServer)
  96. defer st.Close()
  97. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  98. defer tr.CloseIdleConnections()
  99. req, err := http.NewRequest("GET", st.ts.URL, nil)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. res, err := tr.RoundTrip(req)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. defer res.Body.Close()
  108. t.Logf("Got res: %+v", res)
  109. if g, w := res.StatusCode, 200; g != w {
  110. t.Errorf("StatusCode = %v; want %v", g, w)
  111. }
  112. if g, w := res.Status, "200 OK"; g != w {
  113. t.Errorf("Status = %q; want %q", g, w)
  114. }
  115. wantHeader := http.Header{
  116. "Content-Length": []string{"3"},
  117. "Content-Type": []string{"text/plain; charset=utf-8"},
  118. "Date": []string{"XXX"}, // see cleanDate
  119. }
  120. cleanDate(res)
  121. if !reflect.DeepEqual(res.Header, wantHeader) {
  122. t.Errorf("res Header = %v; want %v", res.Header, wantHeader)
  123. }
  124. if res.Request != req {
  125. t.Errorf("Response.Request = %p; want %p", res.Request, req)
  126. }
  127. if res.TLS == nil {
  128. t.Error("Response.TLS = nil; want non-nil")
  129. }
  130. slurp, err := ioutil.ReadAll(res.Body)
  131. if err != nil {
  132. t.Errorf("Body read: %v", err)
  133. } else if string(slurp) != body {
  134. t.Errorf("Body = %q; want %q", slurp, body)
  135. }
  136. }
  137. func onSameConn(t *testing.T, modReq func(*http.Request)) bool {
  138. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  139. io.WriteString(w, r.RemoteAddr)
  140. }, optOnlyServer, func(c net.Conn, st http.ConnState) {
  141. t.Logf("conn %v is now state %v", c.RemoteAddr(), st)
  142. })
  143. defer st.Close()
  144. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  145. defer tr.CloseIdleConnections()
  146. get := func() string {
  147. req, err := http.NewRequest("GET", st.ts.URL, nil)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. modReq(req)
  152. res, err := tr.RoundTrip(req)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. defer res.Body.Close()
  157. slurp, err := ioutil.ReadAll(res.Body)
  158. if err != nil {
  159. t.Fatalf("Body read: %v", err)
  160. }
  161. addr := strings.TrimSpace(string(slurp))
  162. if addr == "" {
  163. t.Fatalf("didn't get an addr in response")
  164. }
  165. return addr
  166. }
  167. first := get()
  168. second := get()
  169. return first == second
  170. }
  171. func TestTransportReusesConns(t *testing.T) {
  172. if !onSameConn(t, func(*http.Request) {}) {
  173. t.Errorf("first and second responses were on different connections")
  174. }
  175. }
  176. func TestTransportReusesConn_RequestClose(t *testing.T) {
  177. if onSameConn(t, func(r *http.Request) { r.Close = true }) {
  178. t.Errorf("first and second responses were not on different connections")
  179. }
  180. }
  181. func TestTransportReusesConn_ConnClose(t *testing.T) {
  182. if onSameConn(t, func(r *http.Request) { r.Header.Set("Connection", "close") }) {
  183. t.Errorf("first and second responses were not on different connections")
  184. }
  185. }
  186. // Tests that the Transport only keeps one pending dial open per destination address.
  187. // https://golang.org/issue/13397
  188. func TestTransportGroupsPendingDials(t *testing.T) {
  189. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  190. io.WriteString(w, r.RemoteAddr)
  191. }, optOnlyServer)
  192. defer st.Close()
  193. tr := &Transport{
  194. TLSClientConfig: tlsConfigInsecure,
  195. }
  196. defer tr.CloseIdleConnections()
  197. var (
  198. mu sync.Mutex
  199. dials = map[string]int{}
  200. )
  201. var wg sync.WaitGroup
  202. for i := 0; i < 10; i++ {
  203. wg.Add(1)
  204. go func() {
  205. defer wg.Done()
  206. req, err := http.NewRequest("GET", st.ts.URL, nil)
  207. if err != nil {
  208. t.Error(err)
  209. return
  210. }
  211. res, err := tr.RoundTrip(req)
  212. if err != nil {
  213. t.Error(err)
  214. return
  215. }
  216. defer res.Body.Close()
  217. slurp, err := ioutil.ReadAll(res.Body)
  218. if err != nil {
  219. t.Errorf("Body read: %v", err)
  220. }
  221. addr := strings.TrimSpace(string(slurp))
  222. if addr == "" {
  223. t.Errorf("didn't get an addr in response")
  224. }
  225. mu.Lock()
  226. dials[addr]++
  227. mu.Unlock()
  228. }()
  229. }
  230. wg.Wait()
  231. if len(dials) != 1 {
  232. t.Errorf("saw %d dials; want 1: %v", len(dials), dials)
  233. }
  234. tr.CloseIdleConnections()
  235. if err := retry(50, 10*time.Millisecond, func() error {
  236. cp, ok := tr.connPool().(*clientConnPool)
  237. if !ok {
  238. return fmt.Errorf("Conn pool is %T; want *clientConnPool", tr.connPool())
  239. }
  240. cp.mu.Lock()
  241. defer cp.mu.Unlock()
  242. if len(cp.dialing) != 0 {
  243. return fmt.Errorf("dialing map = %v; want empty", cp.dialing)
  244. }
  245. if len(cp.conns) != 0 {
  246. return fmt.Errorf("conns = %v; want empty", cp.conns)
  247. }
  248. if len(cp.keys) != 0 {
  249. return fmt.Errorf("keys = %v; want empty", cp.keys)
  250. }
  251. return nil
  252. }); err != nil {
  253. t.Errorf("State of pool after CloseIdleConnections: %v", err)
  254. }
  255. }
  256. func retry(tries int, delay time.Duration, fn func() error) error {
  257. var err error
  258. for i := 0; i < tries; i++ {
  259. err = fn()
  260. if err == nil {
  261. return nil
  262. }
  263. time.Sleep(delay)
  264. }
  265. return err
  266. }
  267. func TestTransportAbortClosesPipes(t *testing.T) {
  268. shutdown := make(chan struct{})
  269. st := newServerTester(t,
  270. func(w http.ResponseWriter, r *http.Request) {
  271. w.(http.Flusher).Flush()
  272. <-shutdown
  273. },
  274. optOnlyServer,
  275. )
  276. defer st.Close()
  277. defer close(shutdown) // we must shutdown before st.Close() to avoid hanging
  278. done := make(chan struct{})
  279. requestMade := make(chan struct{})
  280. go func() {
  281. defer close(done)
  282. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  283. req, err := http.NewRequest("GET", st.ts.URL, nil)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. res, err := tr.RoundTrip(req)
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. defer res.Body.Close()
  292. close(requestMade)
  293. _, err = ioutil.ReadAll(res.Body)
  294. if err == nil {
  295. t.Error("expected error from res.Body.Read")
  296. }
  297. }()
  298. <-requestMade
  299. // Now force the serve loop to end, via closing the connection.
  300. st.closeConn()
  301. // deadlock? that's a bug.
  302. select {
  303. case <-done:
  304. case <-time.After(3 * time.Second):
  305. t.Fatal("timeout")
  306. }
  307. }
  308. // TODO: merge this with TestTransportBody to make TestTransportRequest? This
  309. // could be a table-driven test with extra goodies.
  310. func TestTransportPath(t *testing.T) {
  311. gotc := make(chan *url.URL, 1)
  312. st := newServerTester(t,
  313. func(w http.ResponseWriter, r *http.Request) {
  314. gotc <- r.URL
  315. },
  316. optOnlyServer,
  317. )
  318. defer st.Close()
  319. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  320. defer tr.CloseIdleConnections()
  321. const (
  322. path = "/testpath"
  323. query = "q=1"
  324. )
  325. surl := st.ts.URL + path + "?" + query
  326. req, err := http.NewRequest("POST", surl, nil)
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. c := &http.Client{Transport: tr}
  331. res, err := c.Do(req)
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. defer res.Body.Close()
  336. got := <-gotc
  337. if got.Path != path {
  338. t.Errorf("Read Path = %q; want %q", got.Path, path)
  339. }
  340. if got.RawQuery != query {
  341. t.Errorf("Read RawQuery = %q; want %q", got.RawQuery, query)
  342. }
  343. }
  344. func randString(n int) string {
  345. rnd := rand.New(rand.NewSource(int64(n)))
  346. b := make([]byte, n)
  347. for i := range b {
  348. b[i] = byte(rnd.Intn(256))
  349. }
  350. return string(b)
  351. }
  352. type panicReader struct{}
  353. func (panicReader) Read([]byte) (int, error) { panic("unexpected Read") }
  354. func (panicReader) Close() error { panic("unexpected Close") }
  355. func TestActualContentLength(t *testing.T) {
  356. tests := []struct {
  357. req *http.Request
  358. want int64
  359. }{
  360. // Verify we don't read from Body:
  361. 0: {
  362. req: &http.Request{Body: panicReader{}},
  363. want: -1,
  364. },
  365. // nil Body means 0, regardless of ContentLength:
  366. 1: {
  367. req: &http.Request{Body: nil, ContentLength: 5},
  368. want: 0,
  369. },
  370. // ContentLength is used if set.
  371. 2: {
  372. req: &http.Request{Body: panicReader{}, ContentLength: 5},
  373. want: 5,
  374. },
  375. }
  376. for i, tt := range tests {
  377. got := actualContentLength(tt.req)
  378. if got != tt.want {
  379. t.Errorf("test[%d]: got %d; want %d", i, got, tt.want)
  380. }
  381. }
  382. }
  383. func TestTransportBody(t *testing.T) {
  384. bodyTests := []struct {
  385. body string
  386. noContentLen bool
  387. }{
  388. {body: "some message"},
  389. {body: "some message", noContentLen: true},
  390. {body: strings.Repeat("a", 1<<20), noContentLen: true},
  391. {body: strings.Repeat("a", 1<<20)},
  392. {body: randString(16<<10 - 1)},
  393. {body: randString(16 << 10)},
  394. {body: randString(16<<10 + 1)},
  395. {body: randString(512<<10 - 1)},
  396. {body: randString(512 << 10)},
  397. {body: randString(512<<10 + 1)},
  398. {body: randString(1<<20 - 1)},
  399. {body: randString(1 << 20)},
  400. {body: randString(1<<20 + 2)},
  401. }
  402. type reqInfo struct {
  403. req *http.Request
  404. slurp []byte
  405. err error
  406. }
  407. gotc := make(chan reqInfo, 1)
  408. st := newServerTester(t,
  409. func(w http.ResponseWriter, r *http.Request) {
  410. slurp, err := ioutil.ReadAll(r.Body)
  411. if err != nil {
  412. gotc <- reqInfo{err: err}
  413. } else {
  414. gotc <- reqInfo{req: r, slurp: slurp}
  415. }
  416. },
  417. optOnlyServer,
  418. )
  419. defer st.Close()
  420. for i, tt := range bodyTests {
  421. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  422. defer tr.CloseIdleConnections()
  423. var body io.Reader = strings.NewReader(tt.body)
  424. if tt.noContentLen {
  425. body = struct{ io.Reader }{body} // just a Reader, hiding concrete type and other methods
  426. }
  427. req, err := http.NewRequest("POST", st.ts.URL, body)
  428. if err != nil {
  429. t.Fatalf("#%d: %v", i, err)
  430. }
  431. c := &http.Client{Transport: tr}
  432. res, err := c.Do(req)
  433. if err != nil {
  434. t.Fatalf("#%d: %v", i, err)
  435. }
  436. defer res.Body.Close()
  437. ri := <-gotc
  438. if ri.err != nil {
  439. t.Errorf("#%d: read error: %v", i, ri.err)
  440. continue
  441. }
  442. if got := string(ri.slurp); got != tt.body {
  443. t.Errorf("#%d: Read body mismatch.\n got: %q (len %d)\nwant: %q (len %d)", i, shortString(got), len(got), shortString(tt.body), len(tt.body))
  444. }
  445. wantLen := int64(len(tt.body))
  446. if tt.noContentLen && tt.body != "" {
  447. wantLen = -1
  448. }
  449. if ri.req.ContentLength != wantLen {
  450. t.Errorf("#%d. handler got ContentLength = %v; want %v", i, ri.req.ContentLength, wantLen)
  451. }
  452. }
  453. }
  454. func shortString(v string) string {
  455. const maxLen = 100
  456. if len(v) <= maxLen {
  457. return v
  458. }
  459. return fmt.Sprintf("%v[...%d bytes omitted...]%v", v[:maxLen/2], len(v)-maxLen, v[len(v)-maxLen/2:])
  460. }
  461. func TestTransportDialTLS(t *testing.T) {
  462. var mu sync.Mutex // guards following
  463. var gotReq, didDial bool
  464. ts := newServerTester(t,
  465. func(w http.ResponseWriter, r *http.Request) {
  466. mu.Lock()
  467. gotReq = true
  468. mu.Unlock()
  469. },
  470. optOnlyServer,
  471. )
  472. defer ts.Close()
  473. tr := &Transport{
  474. DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
  475. mu.Lock()
  476. didDial = true
  477. mu.Unlock()
  478. cfg.InsecureSkipVerify = true
  479. c, err := tls.Dial(netw, addr, cfg)
  480. if err != nil {
  481. return nil, err
  482. }
  483. return c, c.Handshake()
  484. },
  485. }
  486. defer tr.CloseIdleConnections()
  487. client := &http.Client{Transport: tr}
  488. res, err := client.Get(ts.ts.URL)
  489. if err != nil {
  490. t.Fatal(err)
  491. }
  492. res.Body.Close()
  493. mu.Lock()
  494. if !gotReq {
  495. t.Error("didn't get request")
  496. }
  497. if !didDial {
  498. t.Error("didn't use dial hook")
  499. }
  500. }
  501. func TestConfigureTransport(t *testing.T) {
  502. t1 := &http.Transport{}
  503. err := ConfigureTransport(t1)
  504. if err == errTransportVersion {
  505. t.Skip(err)
  506. }
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. if got := fmt.Sprintf("%#v", *t1); !strings.Contains(got, `"h2"`) {
  511. // Laziness, to avoid buildtags.
  512. t.Errorf("stringification of HTTP/1 transport didn't contain \"h2\": %v", got)
  513. }
  514. wantNextProtos := []string{"h2", "http/1.1"}
  515. if t1.TLSClientConfig == nil {
  516. t.Errorf("nil t1.TLSClientConfig")
  517. } else if !reflect.DeepEqual(t1.TLSClientConfig.NextProtos, wantNextProtos) {
  518. t.Errorf("TLSClientConfig.NextProtos = %q; want %q", t1.TLSClientConfig.NextProtos, wantNextProtos)
  519. }
  520. if err := ConfigureTransport(t1); err == nil {
  521. t.Error("unexpected success on second call to ConfigureTransport")
  522. }
  523. // And does it work?
  524. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  525. io.WriteString(w, r.Proto)
  526. }, optOnlyServer)
  527. defer st.Close()
  528. t1.TLSClientConfig.InsecureSkipVerify = true
  529. c := &http.Client{Transport: t1}
  530. res, err := c.Get(st.ts.URL)
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. slurp, err := ioutil.ReadAll(res.Body)
  535. if err != nil {
  536. t.Fatal(err)
  537. }
  538. if got, want := string(slurp), "HTTP/2.0"; got != want {
  539. t.Errorf("body = %q; want %q", got, want)
  540. }
  541. }
  542. type capitalizeReader struct {
  543. r io.Reader
  544. }
  545. func (cr capitalizeReader) Read(p []byte) (n int, err error) {
  546. n, err = cr.r.Read(p)
  547. for i, b := range p[:n] {
  548. if b >= 'a' && b <= 'z' {
  549. p[i] = b - ('a' - 'A')
  550. }
  551. }
  552. return
  553. }
  554. type flushWriter struct {
  555. w io.Writer
  556. }
  557. func (fw flushWriter) Write(p []byte) (n int, err error) {
  558. n, err = fw.w.Write(p)
  559. if f, ok := fw.w.(http.Flusher); ok {
  560. f.Flush()
  561. }
  562. return
  563. }
  564. type clientTester struct {
  565. t *testing.T
  566. tr *Transport
  567. sc, cc net.Conn // server and client conn
  568. fr *Framer // server's framer
  569. client func() error
  570. server func() error
  571. }
  572. func newClientTester(t *testing.T) *clientTester {
  573. var dialOnce struct {
  574. sync.Mutex
  575. dialed bool
  576. }
  577. ct := &clientTester{
  578. t: t,
  579. }
  580. ct.tr = &Transport{
  581. TLSClientConfig: tlsConfigInsecure,
  582. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  583. dialOnce.Lock()
  584. defer dialOnce.Unlock()
  585. if dialOnce.dialed {
  586. return nil, errors.New("only one dial allowed in test mode")
  587. }
  588. dialOnce.dialed = true
  589. return ct.cc, nil
  590. },
  591. }
  592. ln := newLocalListener(t)
  593. cc, err := net.Dial("tcp", ln.Addr().String())
  594. if err != nil {
  595. t.Fatal(err)
  596. }
  597. sc, err := ln.Accept()
  598. if err != nil {
  599. t.Fatal(err)
  600. }
  601. ln.Close()
  602. ct.cc = cc
  603. ct.sc = sc
  604. ct.fr = NewFramer(sc, sc)
  605. return ct
  606. }
  607. func newLocalListener(t *testing.T) net.Listener {
  608. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  609. if err == nil {
  610. return ln
  611. }
  612. ln, err = net.Listen("tcp6", "[::1]:0")
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. return ln
  617. }
  618. func (ct *clientTester) greet() {
  619. buf := make([]byte, len(ClientPreface))
  620. _, err := io.ReadFull(ct.sc, buf)
  621. if err != nil {
  622. ct.t.Fatalf("reading client preface: %v", err)
  623. }
  624. f, err := ct.fr.ReadFrame()
  625. if err != nil {
  626. ct.t.Fatalf("Reading client settings frame: %v", err)
  627. }
  628. if sf, ok := f.(*SettingsFrame); !ok {
  629. ct.t.Fatalf("Wanted client settings frame; got %v", f)
  630. _ = sf // stash it away?
  631. }
  632. if err := ct.fr.WriteSettings(); err != nil {
  633. ct.t.Fatal(err)
  634. }
  635. if err := ct.fr.WriteSettingsAck(); err != nil {
  636. ct.t.Fatal(err)
  637. }
  638. }
  639. func (ct *clientTester) readNonSettingsFrame() (Frame, error) {
  640. for {
  641. f, err := ct.fr.ReadFrame()
  642. if err != nil {
  643. return nil, err
  644. }
  645. if _, ok := f.(*SettingsFrame); ok {
  646. continue
  647. }
  648. return f, nil
  649. }
  650. }
  651. func (ct *clientTester) cleanup() {
  652. ct.tr.CloseIdleConnections()
  653. }
  654. func (ct *clientTester) run() {
  655. errc := make(chan error, 2)
  656. ct.start("client", errc, ct.client)
  657. ct.start("server", errc, ct.server)
  658. defer ct.cleanup()
  659. for i := 0; i < 2; i++ {
  660. if err := <-errc; err != nil {
  661. ct.t.Error(err)
  662. return
  663. }
  664. }
  665. }
  666. func (ct *clientTester) start(which string, errc chan<- error, fn func() error) {
  667. go func() {
  668. finished := false
  669. var err error
  670. defer func() {
  671. if !finished {
  672. err = fmt.Errorf("%s goroutine didn't finish.", which)
  673. } else if err != nil {
  674. err = fmt.Errorf("%s: %v", which, err)
  675. }
  676. errc <- err
  677. }()
  678. err = fn()
  679. finished = true
  680. }()
  681. }
  682. func (ct *clientTester) readFrame() (Frame, error) {
  683. return readFrameTimeout(ct.fr, 2*time.Second)
  684. }
  685. func (ct *clientTester) firstHeaders() (*HeadersFrame, error) {
  686. for {
  687. f, err := ct.readFrame()
  688. if err != nil {
  689. return nil, fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  690. }
  691. switch f.(type) {
  692. case *WindowUpdateFrame, *SettingsFrame:
  693. continue
  694. }
  695. hf, ok := f.(*HeadersFrame)
  696. if !ok {
  697. return nil, fmt.Errorf("Got %T; want HeadersFrame", f)
  698. }
  699. return hf, nil
  700. }
  701. }
  702. type countingReader struct {
  703. n *int64
  704. }
  705. func (r countingReader) Read(p []byte) (n int, err error) {
  706. for i := range p {
  707. p[i] = byte(i)
  708. }
  709. atomic.AddInt64(r.n, int64(len(p)))
  710. return len(p), err
  711. }
  712. func TestTransportReqBodyAfterResponse_200(t *testing.T) { testTransportReqBodyAfterResponse(t, 200) }
  713. func TestTransportReqBodyAfterResponse_403(t *testing.T) { testTransportReqBodyAfterResponse(t, 403) }
  714. func testTransportReqBodyAfterResponse(t *testing.T, status int) {
  715. const bodySize = 10 << 20
  716. clientDone := make(chan struct{})
  717. ct := newClientTester(t)
  718. ct.client = func() error {
  719. defer ct.cc.(*net.TCPConn).CloseWrite()
  720. defer close(clientDone)
  721. var n int64 // atomic
  722. req, err := http.NewRequest("PUT", "https://dummy.tld/", io.LimitReader(countingReader{&n}, bodySize))
  723. if err != nil {
  724. return err
  725. }
  726. res, err := ct.tr.RoundTrip(req)
  727. if err != nil {
  728. return fmt.Errorf("RoundTrip: %v", err)
  729. }
  730. defer res.Body.Close()
  731. if res.StatusCode != status {
  732. return fmt.Errorf("status code = %v; want %v", res.StatusCode, status)
  733. }
  734. slurp, err := ioutil.ReadAll(res.Body)
  735. if err != nil {
  736. return fmt.Errorf("Slurp: %v", err)
  737. }
  738. if len(slurp) > 0 {
  739. return fmt.Errorf("unexpected body: %q", slurp)
  740. }
  741. if status == 200 {
  742. if got := atomic.LoadInt64(&n); got != bodySize {
  743. return fmt.Errorf("For 200 response, Transport wrote %d bytes; want %d", got, bodySize)
  744. }
  745. } else {
  746. if got := atomic.LoadInt64(&n); got == 0 || got >= bodySize {
  747. return fmt.Errorf("For %d response, Transport wrote %d bytes; want (0,%d) exclusive", status, got, bodySize)
  748. }
  749. }
  750. return nil
  751. }
  752. ct.server = func() error {
  753. ct.greet()
  754. var buf bytes.Buffer
  755. enc := hpack.NewEncoder(&buf)
  756. var dataRecv int64
  757. var closed bool
  758. for {
  759. f, err := ct.fr.ReadFrame()
  760. if err != nil {
  761. select {
  762. case <-clientDone:
  763. // If the client's done, it
  764. // will have reported any
  765. // errors on its side.
  766. return nil
  767. default:
  768. return err
  769. }
  770. }
  771. //println(fmt.Sprintf("server got frame: %v", f))
  772. switch f := f.(type) {
  773. case *WindowUpdateFrame, *SettingsFrame:
  774. case *HeadersFrame:
  775. if !f.HeadersEnded() {
  776. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  777. }
  778. if f.StreamEnded() {
  779. return fmt.Errorf("headers contains END_STREAM unexpectedly: %v", f)
  780. }
  781. case *DataFrame:
  782. dataLen := len(f.Data())
  783. if dataLen > 0 {
  784. if dataRecv == 0 {
  785. enc.WriteField(hpack.HeaderField{Name: ":status", Value: strconv.Itoa(status)})
  786. ct.fr.WriteHeaders(HeadersFrameParam{
  787. StreamID: f.StreamID,
  788. EndHeaders: true,
  789. EndStream: false,
  790. BlockFragment: buf.Bytes(),
  791. })
  792. }
  793. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  794. return err
  795. }
  796. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  797. return err
  798. }
  799. }
  800. dataRecv += int64(dataLen)
  801. if !closed && ((status != 200 && dataRecv > 0) ||
  802. (status == 200 && dataRecv == bodySize)) {
  803. closed = true
  804. if err := ct.fr.WriteData(f.StreamID, true, nil); err != nil {
  805. return err
  806. }
  807. }
  808. default:
  809. return fmt.Errorf("Unexpected client frame %v", f)
  810. }
  811. }
  812. }
  813. ct.run()
  814. }
  815. // See golang.org/issue/13444
  816. func TestTransportFullDuplex(t *testing.T) {
  817. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  818. w.WriteHeader(200) // redundant but for clarity
  819. w.(http.Flusher).Flush()
  820. io.Copy(flushWriter{w}, capitalizeReader{r.Body})
  821. fmt.Fprintf(w, "bye.\n")
  822. }, optOnlyServer)
  823. defer st.Close()
  824. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  825. defer tr.CloseIdleConnections()
  826. c := &http.Client{Transport: tr}
  827. pr, pw := io.Pipe()
  828. req, err := http.NewRequest("PUT", st.ts.URL, ioutil.NopCloser(pr))
  829. if err != nil {
  830. t.Fatal(err)
  831. }
  832. req.ContentLength = -1
  833. res, err := c.Do(req)
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. defer res.Body.Close()
  838. if res.StatusCode != 200 {
  839. t.Fatalf("StatusCode = %v; want %v", res.StatusCode, 200)
  840. }
  841. bs := bufio.NewScanner(res.Body)
  842. want := func(v string) {
  843. if !bs.Scan() {
  844. t.Fatalf("wanted to read %q but Scan() = false, err = %v", v, bs.Err())
  845. }
  846. }
  847. write := func(v string) {
  848. _, err := io.WriteString(pw, v)
  849. if err != nil {
  850. t.Fatalf("pipe write: %v", err)
  851. }
  852. }
  853. write("foo\n")
  854. want("FOO")
  855. write("bar\n")
  856. want("BAR")
  857. pw.Close()
  858. want("bye.")
  859. if err := bs.Err(); err != nil {
  860. t.Fatal(err)
  861. }
  862. }
  863. func TestTransportConnectRequest(t *testing.T) {
  864. gotc := make(chan *http.Request, 1)
  865. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  866. gotc <- r
  867. }, optOnlyServer)
  868. defer st.Close()
  869. u, err := url.Parse(st.ts.URL)
  870. if err != nil {
  871. t.Fatal(err)
  872. }
  873. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  874. defer tr.CloseIdleConnections()
  875. c := &http.Client{Transport: tr}
  876. tests := []struct {
  877. req *http.Request
  878. want string
  879. }{
  880. {
  881. req: &http.Request{
  882. Method: "CONNECT",
  883. Header: http.Header{},
  884. URL: u,
  885. },
  886. want: u.Host,
  887. },
  888. {
  889. req: &http.Request{
  890. Method: "CONNECT",
  891. Header: http.Header{},
  892. URL: u,
  893. Host: "example.com:123",
  894. },
  895. want: "example.com:123",
  896. },
  897. }
  898. for i, tt := range tests {
  899. res, err := c.Do(tt.req)
  900. if err != nil {
  901. t.Errorf("%d. RoundTrip = %v", i, err)
  902. continue
  903. }
  904. res.Body.Close()
  905. req := <-gotc
  906. if req.Method != "CONNECT" {
  907. t.Errorf("method = %q; want CONNECT", req.Method)
  908. }
  909. if req.Host != tt.want {
  910. t.Errorf("Host = %q; want %q", req.Host, tt.want)
  911. }
  912. if req.URL.Host != tt.want {
  913. t.Errorf("URL.Host = %q; want %q", req.URL.Host, tt.want)
  914. }
  915. }
  916. }
  917. type headerType int
  918. const (
  919. noHeader headerType = iota // omitted
  920. oneHeader
  921. splitHeader // broken into continuation on purpose
  922. )
  923. const (
  924. f0 = noHeader
  925. f1 = oneHeader
  926. f2 = splitHeader
  927. d0 = false
  928. d1 = true
  929. )
  930. // Test all 36 combinations of response frame orders:
  931. // (3 ways of 100-continue) * (2 ways of headers) * (2 ways of data) * (3 ways of trailers):func TestTransportResponsePattern_00f0(t *testing.T) { testTransportResponsePattern(h0, h1, false, h0) }
  932. // Generated by http://play.golang.org/p/SScqYKJYXd
  933. func TestTransportResPattern_c0h1d0t0(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f0) }
  934. func TestTransportResPattern_c0h1d0t1(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f1) }
  935. func TestTransportResPattern_c0h1d0t2(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f2) }
  936. func TestTransportResPattern_c0h1d1t0(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f0) }
  937. func TestTransportResPattern_c0h1d1t1(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f1) }
  938. func TestTransportResPattern_c0h1d1t2(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f2) }
  939. func TestTransportResPattern_c0h2d0t0(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f0) }
  940. func TestTransportResPattern_c0h2d0t1(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f1) }
  941. func TestTransportResPattern_c0h2d0t2(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f2) }
  942. func TestTransportResPattern_c0h2d1t0(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f0) }
  943. func TestTransportResPattern_c0h2d1t1(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f1) }
  944. func TestTransportResPattern_c0h2d1t2(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f2) }
  945. func TestTransportResPattern_c1h1d0t0(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f0) }
  946. func TestTransportResPattern_c1h1d0t1(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f1) }
  947. func TestTransportResPattern_c1h1d0t2(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f2) }
  948. func TestTransportResPattern_c1h1d1t0(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f0) }
  949. func TestTransportResPattern_c1h1d1t1(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f1) }
  950. func TestTransportResPattern_c1h1d1t2(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f2) }
  951. func TestTransportResPattern_c1h2d0t0(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f0) }
  952. func TestTransportResPattern_c1h2d0t1(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f1) }
  953. func TestTransportResPattern_c1h2d0t2(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f2) }
  954. func TestTransportResPattern_c1h2d1t0(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f0) }
  955. func TestTransportResPattern_c1h2d1t1(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f1) }
  956. func TestTransportResPattern_c1h2d1t2(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f2) }
  957. func TestTransportResPattern_c2h1d0t0(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f0) }
  958. func TestTransportResPattern_c2h1d0t1(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f1) }
  959. func TestTransportResPattern_c2h1d0t2(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f2) }
  960. func TestTransportResPattern_c2h1d1t0(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f0) }
  961. func TestTransportResPattern_c2h1d1t1(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f1) }
  962. func TestTransportResPattern_c2h1d1t2(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f2) }
  963. func TestTransportResPattern_c2h2d0t0(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f0) }
  964. func TestTransportResPattern_c2h2d0t1(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f1) }
  965. func TestTransportResPattern_c2h2d0t2(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f2) }
  966. func TestTransportResPattern_c2h2d1t0(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f0) }
  967. func TestTransportResPattern_c2h2d1t1(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f1) }
  968. func TestTransportResPattern_c2h2d1t2(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f2) }
  969. func testTransportResPattern(t *testing.T, expect100Continue, resHeader headerType, withData bool, trailers headerType) {
  970. const reqBody = "some request body"
  971. const resBody = "some response body"
  972. if resHeader == noHeader {
  973. // TODO: test 100-continue followed by immediate
  974. // server stream reset, without headers in the middle?
  975. panic("invalid combination")
  976. }
  977. ct := newClientTester(t)
  978. ct.client = func() error {
  979. req, _ := http.NewRequest("POST", "https://dummy.tld/", strings.NewReader(reqBody))
  980. if expect100Continue != noHeader {
  981. req.Header.Set("Expect", "100-continue")
  982. }
  983. res, err := ct.tr.RoundTrip(req)
  984. if err != nil {
  985. return fmt.Errorf("RoundTrip: %v", err)
  986. }
  987. defer res.Body.Close()
  988. if res.StatusCode != 200 {
  989. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  990. }
  991. slurp, err := ioutil.ReadAll(res.Body)
  992. if err != nil {
  993. return fmt.Errorf("Slurp: %v", err)
  994. }
  995. wantBody := resBody
  996. if !withData {
  997. wantBody = ""
  998. }
  999. if string(slurp) != wantBody {
  1000. return fmt.Errorf("body = %q; want %q", slurp, wantBody)
  1001. }
  1002. if trailers == noHeader {
  1003. if len(res.Trailer) > 0 {
  1004. t.Errorf("Trailer = %v; want none", res.Trailer)
  1005. }
  1006. } else {
  1007. want := http.Header{"Some-Trailer": {"some-value"}}
  1008. if !reflect.DeepEqual(res.Trailer, want) {
  1009. t.Errorf("Trailer = %v; want %v", res.Trailer, want)
  1010. }
  1011. }
  1012. return nil
  1013. }
  1014. ct.server = func() error {
  1015. ct.greet()
  1016. var buf bytes.Buffer
  1017. enc := hpack.NewEncoder(&buf)
  1018. for {
  1019. f, err := ct.fr.ReadFrame()
  1020. if err != nil {
  1021. return err
  1022. }
  1023. endStream := false
  1024. send := func(mode headerType) {
  1025. hbf := buf.Bytes()
  1026. switch mode {
  1027. case oneHeader:
  1028. ct.fr.WriteHeaders(HeadersFrameParam{
  1029. StreamID: f.Header().StreamID,
  1030. EndHeaders: true,
  1031. EndStream: endStream,
  1032. BlockFragment: hbf,
  1033. })
  1034. case splitHeader:
  1035. if len(hbf) < 2 {
  1036. panic("too small")
  1037. }
  1038. ct.fr.WriteHeaders(HeadersFrameParam{
  1039. StreamID: f.Header().StreamID,
  1040. EndHeaders: false,
  1041. EndStream: endStream,
  1042. BlockFragment: hbf[:1],
  1043. })
  1044. ct.fr.WriteContinuation(f.Header().StreamID, true, hbf[1:])
  1045. default:
  1046. panic("bogus mode")
  1047. }
  1048. }
  1049. switch f := f.(type) {
  1050. case *WindowUpdateFrame, *SettingsFrame:
  1051. case *DataFrame:
  1052. if !f.StreamEnded() {
  1053. // No need to send flow control tokens. The test request body is tiny.
  1054. continue
  1055. }
  1056. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1057. {
  1058. buf.Reset()
  1059. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1060. enc.WriteField(hpack.HeaderField{Name: "x-foo", Value: "blah"})
  1061. enc.WriteField(hpack.HeaderField{Name: "x-bar", Value: "more"})
  1062. if trailers != noHeader {
  1063. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "some-trailer"})
  1064. }
  1065. endStream = withData == false && trailers == noHeader
  1066. send(resHeader)
  1067. }
  1068. if withData {
  1069. endStream = trailers == noHeader
  1070. ct.fr.WriteData(f.StreamID, endStream, []byte(resBody))
  1071. }
  1072. if trailers != noHeader {
  1073. endStream = true
  1074. buf.Reset()
  1075. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "some-value"})
  1076. send(trailers)
  1077. }
  1078. if endStream {
  1079. return nil
  1080. }
  1081. case *HeadersFrame:
  1082. if expect100Continue != noHeader {
  1083. buf.Reset()
  1084. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "100"})
  1085. send(expect100Continue)
  1086. }
  1087. }
  1088. }
  1089. }
  1090. ct.run()
  1091. }
  1092. func TestTransportReceiveUndeclaredTrailer(t *testing.T) {
  1093. ct := newClientTester(t)
  1094. ct.client = func() error {
  1095. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1096. res, err := ct.tr.RoundTrip(req)
  1097. if err != nil {
  1098. return fmt.Errorf("RoundTrip: %v", err)
  1099. }
  1100. defer res.Body.Close()
  1101. if res.StatusCode != 200 {
  1102. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1103. }
  1104. slurp, err := ioutil.ReadAll(res.Body)
  1105. if err != nil {
  1106. return fmt.Errorf("res.Body ReadAll error = %q, %v; want %v", slurp, err, nil)
  1107. }
  1108. if len(slurp) > 0 {
  1109. return fmt.Errorf("body = %q; want nothing", slurp)
  1110. }
  1111. if _, ok := res.Trailer["Some-Trailer"]; !ok {
  1112. return fmt.Errorf("expected Some-Trailer")
  1113. }
  1114. return nil
  1115. }
  1116. ct.server = func() error {
  1117. ct.greet()
  1118. var n int
  1119. var hf *HeadersFrame
  1120. for hf == nil && n < 10 {
  1121. f, err := ct.fr.ReadFrame()
  1122. if err != nil {
  1123. return err
  1124. }
  1125. hf, _ = f.(*HeadersFrame)
  1126. n++
  1127. }
  1128. var buf bytes.Buffer
  1129. enc := hpack.NewEncoder(&buf)
  1130. // send headers without Trailer header
  1131. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1132. ct.fr.WriteHeaders(HeadersFrameParam{
  1133. StreamID: hf.StreamID,
  1134. EndHeaders: true,
  1135. EndStream: false,
  1136. BlockFragment: buf.Bytes(),
  1137. })
  1138. // send trailers
  1139. buf.Reset()
  1140. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "I'm an undeclared Trailer!"})
  1141. ct.fr.WriteHeaders(HeadersFrameParam{
  1142. StreamID: hf.StreamID,
  1143. EndHeaders: true,
  1144. EndStream: true,
  1145. BlockFragment: buf.Bytes(),
  1146. })
  1147. return nil
  1148. }
  1149. ct.run()
  1150. }
  1151. func TestTransportInvalidTrailer_Pseudo1(t *testing.T) {
  1152. testTransportInvalidTrailer_Pseudo(t, oneHeader)
  1153. }
  1154. func TestTransportInvalidTrailer_Pseudo2(t *testing.T) {
  1155. testTransportInvalidTrailer_Pseudo(t, splitHeader)
  1156. }
  1157. func testTransportInvalidTrailer_Pseudo(t *testing.T, trailers headerType) {
  1158. testInvalidTrailer(t, trailers, pseudoHeaderError(":colon"), func(enc *hpack.Encoder) {
  1159. enc.WriteField(hpack.HeaderField{Name: ":colon", Value: "foo"})
  1160. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1161. })
  1162. }
  1163. func TestTransportInvalidTrailer_Capital1(t *testing.T) {
  1164. testTransportInvalidTrailer_Capital(t, oneHeader)
  1165. }
  1166. func TestTransportInvalidTrailer_Capital2(t *testing.T) {
  1167. testTransportInvalidTrailer_Capital(t, splitHeader)
  1168. }
  1169. func testTransportInvalidTrailer_Capital(t *testing.T, trailers headerType) {
  1170. testInvalidTrailer(t, trailers, headerFieldNameError("Capital"), func(enc *hpack.Encoder) {
  1171. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1172. enc.WriteField(hpack.HeaderField{Name: "Capital", Value: "bad"})
  1173. })
  1174. }
  1175. func TestTransportInvalidTrailer_EmptyFieldName(t *testing.T) {
  1176. testInvalidTrailer(t, oneHeader, headerFieldNameError(""), func(enc *hpack.Encoder) {
  1177. enc.WriteField(hpack.HeaderField{Name: "", Value: "bad"})
  1178. })
  1179. }
  1180. func TestTransportInvalidTrailer_BinaryFieldValue(t *testing.T) {
  1181. testInvalidTrailer(t, oneHeader, headerFieldValueError("has\nnewline"), func(enc *hpack.Encoder) {
  1182. enc.WriteField(hpack.HeaderField{Name: "x", Value: "has\nnewline"})
  1183. })
  1184. }
  1185. func testInvalidTrailer(t *testing.T, trailers headerType, wantErr error, writeTrailer func(*hpack.Encoder)) {
  1186. ct := newClientTester(t)
  1187. ct.client = func() error {
  1188. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1189. res, err := ct.tr.RoundTrip(req)
  1190. if err != nil {
  1191. return fmt.Errorf("RoundTrip: %v", err)
  1192. }
  1193. defer res.Body.Close()
  1194. if res.StatusCode != 200 {
  1195. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1196. }
  1197. slurp, err := ioutil.ReadAll(res.Body)
  1198. se, ok := err.(StreamError)
  1199. if !ok || se.Cause != wantErr {
  1200. return fmt.Errorf("res.Body ReadAll error = %q, %#v; want StreamError with cause %T, %#v", slurp, err, wantErr, wantErr)
  1201. }
  1202. if len(slurp) > 0 {
  1203. return fmt.Errorf("body = %q; want nothing", slurp)
  1204. }
  1205. return nil
  1206. }
  1207. ct.server = func() error {
  1208. ct.greet()
  1209. var buf bytes.Buffer
  1210. enc := hpack.NewEncoder(&buf)
  1211. for {
  1212. f, err := ct.fr.ReadFrame()
  1213. if err != nil {
  1214. return err
  1215. }
  1216. switch f := f.(type) {
  1217. case *HeadersFrame:
  1218. var endStream bool
  1219. send := func(mode headerType) {
  1220. hbf := buf.Bytes()
  1221. switch mode {
  1222. case oneHeader:
  1223. ct.fr.WriteHeaders(HeadersFrameParam{
  1224. StreamID: f.StreamID,
  1225. EndHeaders: true,
  1226. EndStream: endStream,
  1227. BlockFragment: hbf,
  1228. })
  1229. case splitHeader:
  1230. if len(hbf) < 2 {
  1231. panic("too small")
  1232. }
  1233. ct.fr.WriteHeaders(HeadersFrameParam{
  1234. StreamID: f.StreamID,
  1235. EndHeaders: false,
  1236. EndStream: endStream,
  1237. BlockFragment: hbf[:1],
  1238. })
  1239. ct.fr.WriteContinuation(f.StreamID, true, hbf[1:])
  1240. default:
  1241. panic("bogus mode")
  1242. }
  1243. }
  1244. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1245. {
  1246. buf.Reset()
  1247. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1248. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "declared"})
  1249. endStream = false
  1250. send(oneHeader)
  1251. }
  1252. // Trailers:
  1253. {
  1254. endStream = true
  1255. buf.Reset()
  1256. writeTrailer(enc)
  1257. send(trailers)
  1258. }
  1259. return nil
  1260. }
  1261. }
  1262. }
  1263. ct.run()
  1264. }
  1265. func TestTransportChecksResponseHeaderListSize(t *testing.T) {
  1266. ct := newClientTester(t)
  1267. ct.client = func() error {
  1268. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1269. res, err := ct.tr.RoundTrip(req)
  1270. if err != errResponseHeaderListSize {
  1271. if res != nil {
  1272. res.Body.Close()
  1273. }
  1274. size := int64(0)
  1275. for k, vv := range res.Header {
  1276. for _, v := range vv {
  1277. size += int64(len(k)) + int64(len(v)) + 32
  1278. }
  1279. }
  1280. return fmt.Errorf("RoundTrip Error = %v (and %d bytes of response headers); want errResponseHeaderListSize", err, size)
  1281. }
  1282. return nil
  1283. }
  1284. ct.server = func() error {
  1285. ct.greet()
  1286. var buf bytes.Buffer
  1287. enc := hpack.NewEncoder(&buf)
  1288. for {
  1289. f, err := ct.fr.ReadFrame()
  1290. if err != nil {
  1291. return err
  1292. }
  1293. switch f := f.(type) {
  1294. case *HeadersFrame:
  1295. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1296. large := strings.Repeat("a", 1<<10)
  1297. for i := 0; i < 5042; i++ {
  1298. enc.WriteField(hpack.HeaderField{Name: large, Value: large})
  1299. }
  1300. if size, want := buf.Len(), 6329; size != want {
  1301. // Note: this number might change if
  1302. // our hpack implementation
  1303. // changes. That's fine. This is
  1304. // just a sanity check that our
  1305. // response can fit in a single
  1306. // header block fragment frame.
  1307. return fmt.Errorf("encoding over 10MB of duplicate keypairs took %d bytes; expected %d", size, want)
  1308. }
  1309. ct.fr.WriteHeaders(HeadersFrameParam{
  1310. StreamID: f.StreamID,
  1311. EndHeaders: true,
  1312. EndStream: true,
  1313. BlockFragment: buf.Bytes(),
  1314. })
  1315. return nil
  1316. }
  1317. }
  1318. }
  1319. ct.run()
  1320. }
  1321. // Test that the the Transport returns a typed error from Response.Body.Read calls
  1322. // when the server sends an error. (here we use a panic, since that should generate
  1323. // a stream error, but others like cancel should be similar)
  1324. func TestTransportBodyReadErrorType(t *testing.T) {
  1325. doPanic := make(chan bool, 1)
  1326. st := newServerTester(t,
  1327. func(w http.ResponseWriter, r *http.Request) {
  1328. w.(http.Flusher).Flush() // force headers out
  1329. <-doPanic
  1330. panic("boom")
  1331. },
  1332. optOnlyServer,
  1333. optQuiet,
  1334. )
  1335. defer st.Close()
  1336. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1337. defer tr.CloseIdleConnections()
  1338. c := &http.Client{Transport: tr}
  1339. res, err := c.Get(st.ts.URL)
  1340. if err != nil {
  1341. t.Fatal(err)
  1342. }
  1343. defer res.Body.Close()
  1344. doPanic <- true
  1345. buf := make([]byte, 100)
  1346. n, err := res.Body.Read(buf)
  1347. want := StreamError{StreamID: 0x1, Code: 0x2}
  1348. if !reflect.DeepEqual(want, err) {
  1349. t.Errorf("Read = %v, %#v; want error %#v", n, err, want)
  1350. }
  1351. }
  1352. // golang.org/issue/13924
  1353. // This used to fail after many iterations, especially with -race:
  1354. // go test -v -run=TestTransportDoubleCloseOnWriteError -count=500 -race
  1355. func TestTransportDoubleCloseOnWriteError(t *testing.T) {
  1356. var (
  1357. mu sync.Mutex
  1358. conn net.Conn // to close if set
  1359. )
  1360. st := newServerTester(t,
  1361. func(w http.ResponseWriter, r *http.Request) {
  1362. mu.Lock()
  1363. defer mu.Unlock()
  1364. if conn != nil {
  1365. conn.Close()
  1366. }
  1367. },
  1368. optOnlyServer,
  1369. )
  1370. defer st.Close()
  1371. tr := &Transport{
  1372. TLSClientConfig: tlsConfigInsecure,
  1373. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1374. tc, err := tls.Dial(network, addr, cfg)
  1375. if err != nil {
  1376. return nil, err
  1377. }
  1378. mu.Lock()
  1379. defer mu.Unlock()
  1380. conn = tc
  1381. return tc, nil
  1382. },
  1383. }
  1384. defer tr.CloseIdleConnections()
  1385. c := &http.Client{Transport: tr}
  1386. c.Get(st.ts.URL)
  1387. }
  1388. // Test that the http1 Transport.DisableKeepAlives option is respected
  1389. // and connections are closed as soon as idle.
  1390. // See golang.org/issue/14008
  1391. func TestTransportDisableKeepAlives(t *testing.T) {
  1392. st := newServerTester(t,
  1393. func(w http.ResponseWriter, r *http.Request) {
  1394. io.WriteString(w, "hi")
  1395. },
  1396. optOnlyServer,
  1397. )
  1398. defer st.Close()
  1399. connClosed := make(chan struct{}) // closed on tls.Conn.Close
  1400. tr := &Transport{
  1401. t1: &http.Transport{
  1402. DisableKeepAlives: true,
  1403. },
  1404. TLSClientConfig: tlsConfigInsecure,
  1405. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1406. tc, err := tls.Dial(network, addr, cfg)
  1407. if err != nil {
  1408. return nil, err
  1409. }
  1410. return &noteCloseConn{Conn: tc, closefn: func() { close(connClosed) }}, nil
  1411. },
  1412. }
  1413. c := &http.Client{Transport: tr}
  1414. res, err := c.Get(st.ts.URL)
  1415. if err != nil {
  1416. t.Fatal(err)
  1417. }
  1418. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1419. t.Fatal(err)
  1420. }
  1421. defer res.Body.Close()
  1422. select {
  1423. case <-connClosed:
  1424. case <-time.After(1 * time.Second):
  1425. t.Errorf("timeout")
  1426. }
  1427. }
  1428. // Test concurrent requests with Transport.DisableKeepAlives. We can share connections,
  1429. // but when things are totally idle, it still needs to close.
  1430. func TestTransportDisableKeepAlives_Concurrency(t *testing.T) {
  1431. const D = 25 * time.Millisecond
  1432. st := newServerTester(t,
  1433. func(w http.ResponseWriter, r *http.Request) {
  1434. time.Sleep(D)
  1435. io.WriteString(w, "hi")
  1436. },
  1437. optOnlyServer,
  1438. )
  1439. defer st.Close()
  1440. var dials int32
  1441. var conns sync.WaitGroup
  1442. tr := &Transport{
  1443. t1: &http.Transport{
  1444. DisableKeepAlives: true,
  1445. },
  1446. TLSClientConfig: tlsConfigInsecure,
  1447. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1448. tc, err := tls.Dial(network, addr, cfg)
  1449. if err != nil {
  1450. return nil, err
  1451. }
  1452. atomic.AddInt32(&dials, 1)
  1453. conns.Add(1)
  1454. return &noteCloseConn{Conn: tc, closefn: func() { conns.Done() }}, nil
  1455. },
  1456. }
  1457. c := &http.Client{Transport: tr}
  1458. var reqs sync.WaitGroup
  1459. const N = 20
  1460. for i := 0; i < N; i++ {
  1461. reqs.Add(1)
  1462. if i == N-1 {
  1463. // For the final request, try to make all the
  1464. // others close. This isn't verified in the
  1465. // count, other than the Log statement, since
  1466. // it's so timing dependent. This test is
  1467. // really to make sure we don't interrupt a
  1468. // valid request.
  1469. time.Sleep(D * 2)
  1470. }
  1471. go func() {
  1472. defer reqs.Done()
  1473. res, err := c.Get(st.ts.URL)
  1474. if err != nil {
  1475. t.Error(err)
  1476. return
  1477. }
  1478. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1479. t.Error(err)
  1480. return
  1481. }
  1482. res.Body.Close()
  1483. }()
  1484. }
  1485. reqs.Wait()
  1486. conns.Wait()
  1487. t.Logf("did %d dials, %d requests", atomic.LoadInt32(&dials), N)
  1488. }
  1489. type noteCloseConn struct {
  1490. net.Conn
  1491. onceClose sync.Once
  1492. closefn func()
  1493. }
  1494. func (c *noteCloseConn) Close() error {
  1495. c.onceClose.Do(c.closefn)
  1496. return c.Conn.Close()
  1497. }
  1498. func isTimeout(err error) bool {
  1499. switch err := err.(type) {
  1500. case nil:
  1501. return false
  1502. case *url.Error:
  1503. return isTimeout(err.Err)
  1504. case net.Error:
  1505. return err.Timeout()
  1506. }
  1507. return false
  1508. }
  1509. // Test that the http1 Transport.ResponseHeaderTimeout option and cancel is sent.
  1510. func TestTransportResponseHeaderTimeout_NoBody(t *testing.T) {
  1511. testTransportResponseHeaderTimeout(t, false)
  1512. }
  1513. func TestTransportResponseHeaderTimeout_Body(t *testing.T) {
  1514. testTransportResponseHeaderTimeout(t, true)
  1515. }
  1516. func testTransportResponseHeaderTimeout(t *testing.T, body bool) {
  1517. ct := newClientTester(t)
  1518. ct.tr.t1 = &http.Transport{
  1519. ResponseHeaderTimeout: 5 * time.Millisecond,
  1520. }
  1521. ct.client = func() error {
  1522. c := &http.Client{Transport: ct.tr}
  1523. var err error
  1524. var n int64
  1525. const bodySize = 4 << 20
  1526. if body {
  1527. _, err = c.Post("https://dummy.tld/", "text/foo", io.LimitReader(countingReader{&n}, bodySize))
  1528. } else {
  1529. _, err = c.Get("https://dummy.tld/")
  1530. }
  1531. if !isTimeout(err) {
  1532. t.Errorf("client expected timeout error; got %#v", err)
  1533. }
  1534. if body && n != bodySize {
  1535. t.Errorf("only read %d bytes of body; want %d", n, bodySize)
  1536. }
  1537. return nil
  1538. }
  1539. ct.server = func() error {
  1540. ct.greet()
  1541. for {
  1542. f, err := ct.fr.ReadFrame()
  1543. if err != nil {
  1544. t.Logf("ReadFrame: %v", err)
  1545. return nil
  1546. }
  1547. switch f := f.(type) {
  1548. case *DataFrame:
  1549. dataLen := len(f.Data())
  1550. if dataLen > 0 {
  1551. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  1552. return err
  1553. }
  1554. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  1555. return err
  1556. }
  1557. }
  1558. case *RSTStreamFrame:
  1559. if f.StreamID == 1 && f.ErrCode == ErrCodeCancel {
  1560. return nil
  1561. }
  1562. }
  1563. }
  1564. }
  1565. ct.run()
  1566. }
  1567. func TestTransportDisableCompression(t *testing.T) {
  1568. const body = "sup"
  1569. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1570. want := http.Header{
  1571. "User-Agent": []string{"Go-http-client/2.0"},
  1572. }
  1573. if !reflect.DeepEqual(r.Header, want) {
  1574. t.Errorf("request headers = %v; want %v", r.Header, want)
  1575. }
  1576. }, optOnlyServer)
  1577. defer st.Close()
  1578. tr := &Transport{
  1579. TLSClientConfig: tlsConfigInsecure,
  1580. t1: &http.Transport{
  1581. DisableCompression: true,
  1582. },
  1583. }
  1584. defer tr.CloseIdleConnections()
  1585. req, err := http.NewRequest("GET", st.ts.URL, nil)
  1586. if err != nil {
  1587. t.Fatal(err)
  1588. }
  1589. res, err := tr.RoundTrip(req)
  1590. if err != nil {
  1591. t.Fatal(err)
  1592. }
  1593. defer res.Body.Close()
  1594. }
  1595. // RFC 7540 section 8.1.2.2
  1596. func TestTransportRejectsConnHeaders(t *testing.T) {
  1597. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1598. var got []string
  1599. for k := range r.Header {
  1600. got = append(got, k)
  1601. }
  1602. sort.Strings(got)
  1603. w.Header().Set("Got-Header", strings.Join(got, ","))
  1604. }, optOnlyServer)
  1605. defer st.Close()
  1606. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1607. defer tr.CloseIdleConnections()
  1608. tests := []struct {
  1609. key string
  1610. value []string
  1611. want string
  1612. }{
  1613. {
  1614. key: "Upgrade",
  1615. value: []string{"anything"},
  1616. want: "ERROR: http2: invalid Upgrade request header",
  1617. },
  1618. {
  1619. key: "Connection",
  1620. value: []string{"foo"},
  1621. want: "ERROR: http2: invalid Connection request header",
  1622. },
  1623. {
  1624. key: "Connection",
  1625. value: []string{"close"},
  1626. want: "Accept-Encoding,User-Agent",
  1627. },
  1628. {
  1629. key: "Connection",
  1630. value: []string{"close", "something-else"},
  1631. want: "ERROR: http2: invalid Connection request header",
  1632. },
  1633. {
  1634. key: "Connection",
  1635. value: []string{"keep-alive"},
  1636. want: "Accept-Encoding,User-Agent",
  1637. },
  1638. {
  1639. key: "Proxy-Connection", // just deleted and ignored
  1640. value: []string{"keep-alive"},
  1641. want: "Accept-Encoding,User-Agent",
  1642. },
  1643. {
  1644. key: "Transfer-Encoding",
  1645. value: []string{""},
  1646. want: "Accept-Encoding,User-Agent",
  1647. },
  1648. {
  1649. key: "Transfer-Encoding",
  1650. value: []string{"foo"},
  1651. want: "ERROR: http2: invalid Transfer-Encoding request header",
  1652. },
  1653. {
  1654. key: "Transfer-Encoding",
  1655. value: []string{"chunked"},
  1656. want: "Accept-Encoding,User-Agent",
  1657. },
  1658. {
  1659. key: "Transfer-Encoding",
  1660. value: []string{"chunked", "other"},
  1661. want: "ERROR: http2: invalid Transfer-Encoding request header",
  1662. },
  1663. {
  1664. key: "Content-Length",
  1665. value: []string{"123"},
  1666. want: "Accept-Encoding,User-Agent",
  1667. },
  1668. {
  1669. key: "Keep-Alive",
  1670. value: []string{"doop"},
  1671. want: "Accept-Encoding,User-Agent",
  1672. },
  1673. }
  1674. for _, tt := range tests {
  1675. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  1676. req.Header[tt.key] = tt.value
  1677. res, err := tr.RoundTrip(req)
  1678. var got string
  1679. if err != nil {
  1680. got = fmt.Sprintf("ERROR: %v", err)
  1681. } else {
  1682. got = res.Header.Get("Got-Header")
  1683. res.Body.Close()
  1684. }
  1685. if got != tt.want {
  1686. t.Errorf("For key %q, value %q, got = %q; want %q", tt.key, tt.value, got, tt.want)
  1687. }
  1688. }
  1689. }
  1690. // golang.org/issue/14048
  1691. func TestTransportFailsOnInvalidHeaders(t *testing.T) {
  1692. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1693. var got []string
  1694. for k := range r.Header {
  1695. got = append(got, k)
  1696. }
  1697. sort.Strings(got)
  1698. w.Header().Set("Got-Header", strings.Join(got, ","))
  1699. }, optOnlyServer)
  1700. defer st.Close()
  1701. tests := [...]struct {
  1702. h http.Header
  1703. wantErr string
  1704. }{
  1705. 0: {
  1706. h: http.Header{"with space": {"foo"}},
  1707. wantErr: `invalid HTTP header name "with space"`,
  1708. },
  1709. 1: {
  1710. h: http.Header{"name": {"Брэд"}},
  1711. wantErr: "", // okay
  1712. },
  1713. 2: {
  1714. h: http.Header{"имя": {"Brad"}},
  1715. wantErr: `invalid HTTP header name "имя"`,
  1716. },
  1717. 3: {
  1718. h: http.Header{"foo": {"foo\x01bar"}},
  1719. wantErr: `invalid HTTP header value "foo\x01bar" for header "foo"`,
  1720. },
  1721. }
  1722. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1723. defer tr.CloseIdleConnections()
  1724. for i, tt := range tests {
  1725. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  1726. req.Header = tt.h
  1727. res, err := tr.RoundTrip(req)
  1728. var bad bool
  1729. if tt.wantErr == "" {
  1730. if err != nil {
  1731. bad = true
  1732. t.Errorf("case %d: error = %v; want no error", i, err)
  1733. }
  1734. } else {
  1735. if !strings.Contains(fmt.Sprint(err), tt.wantErr) {
  1736. bad = true
  1737. t.Errorf("case %d: error = %v; want error %q", i, err, tt.wantErr)
  1738. }
  1739. }
  1740. if err == nil {
  1741. if bad {
  1742. t.Logf("case %d: server got headers %q", i, res.Header.Get("Got-Header"))
  1743. }
  1744. res.Body.Close()
  1745. }
  1746. }
  1747. }
  1748. // Tests that gzipReader doesn't crash on a second Read call following
  1749. // the first Read call's gzip.NewReader returning an error.
  1750. func TestGzipReader_DoubleReadCrash(t *testing.T) {
  1751. gz := &gzipReader{
  1752. body: ioutil.NopCloser(strings.NewReader("0123456789")),
  1753. }
  1754. var buf [1]byte
  1755. n, err1 := gz.Read(buf[:])
  1756. if n != 0 || !strings.Contains(fmt.Sprint(err1), "invalid header") {
  1757. t.Fatalf("Read = %v, %v; want 0, invalid header", n, err1)
  1758. }
  1759. n, err2 := gz.Read(buf[:])
  1760. if n != 0 || err2 != err1 {
  1761. t.Fatalf("second Read = %v, %v; want 0, %v", n, err2, err1)
  1762. }
  1763. }
  1764. func TestTransportNewTLSConfig(t *testing.T) {
  1765. tests := [...]struct {
  1766. conf *tls.Config
  1767. host string
  1768. want *tls.Config
  1769. }{
  1770. // Normal case.
  1771. 0: {
  1772. conf: nil,
  1773. host: "foo.com",
  1774. want: &tls.Config{
  1775. ServerName: "foo.com",
  1776. NextProtos: []string{NextProtoTLS},
  1777. },
  1778. },
  1779. // User-provided name (bar.com) takes precedence:
  1780. 1: {
  1781. conf: &tls.Config{
  1782. ServerName: "bar.com",
  1783. },
  1784. host: "foo.com",
  1785. want: &tls.Config{
  1786. ServerName: "bar.com",
  1787. NextProtos: []string{NextProtoTLS},
  1788. },
  1789. },
  1790. // NextProto is prepended:
  1791. 2: {
  1792. conf: &tls.Config{
  1793. NextProtos: []string{"foo", "bar"},
  1794. },
  1795. host: "example.com",
  1796. want: &tls.Config{
  1797. ServerName: "example.com",
  1798. NextProtos: []string{NextProtoTLS, "foo", "bar"},
  1799. },
  1800. },
  1801. // NextProto is not duplicated:
  1802. 3: {
  1803. conf: &tls.Config{
  1804. NextProtos: []string{"foo", "bar", NextProtoTLS},
  1805. },
  1806. host: "example.com",
  1807. want: &tls.Config{
  1808. ServerName: "example.com",
  1809. NextProtos: []string{"foo", "bar", NextProtoTLS},
  1810. },
  1811. },
  1812. }
  1813. for i, tt := range tests {
  1814. tr := &Transport{TLSClientConfig: tt.conf}
  1815. got := tr.newTLSConfig(tt.host)
  1816. if !reflect.DeepEqual(got, tt.want) {
  1817. t.Errorf("%d. got %#v; want %#v", i, got, tt.want)
  1818. }
  1819. }
  1820. }
  1821. // The Google GFE responds to HEAD requests with a HEADERS frame
  1822. // without END_STREAM, followed by a 0-length DATA frame with
  1823. // END_STREAM. Make sure we don't get confused by that. (We did.)
  1824. func TestTransportReadHeadResponse(t *testing.T) {
  1825. ct := newClientTester(t)
  1826. clientDone := make(chan struct{})
  1827. ct.client = func() error {
  1828. defer close(clientDone)
  1829. req, _ := http.NewRequest("HEAD", "https://dummy.tld/", nil)
  1830. res, err := ct.tr.RoundTrip(req)
  1831. if err != nil {
  1832. return err
  1833. }
  1834. if res.ContentLength != 123 {
  1835. return fmt.Errorf("Content-Length = %d; want 123", res.ContentLength)
  1836. }
  1837. slurp, err := ioutil.ReadAll(res.Body)
  1838. if err != nil {
  1839. return fmt.Errorf("ReadAll: %v", err)
  1840. }
  1841. if len(slurp) > 0 {
  1842. return fmt.Errorf("Unexpected non-empty ReadAll body: %q", slurp)
  1843. }
  1844. return nil
  1845. }
  1846. ct.server = func() error {
  1847. ct.greet()
  1848. for {
  1849. f, err := ct.fr.ReadFrame()
  1850. if err != nil {
  1851. t.Logf("ReadFrame: %v", err)
  1852. return nil
  1853. }
  1854. hf, ok := f.(*HeadersFrame)
  1855. if !ok {
  1856. continue
  1857. }
  1858. var buf bytes.Buffer
  1859. enc := hpack.NewEncoder(&buf)
  1860. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1861. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  1862. ct.fr.WriteHeaders(HeadersFrameParam{
  1863. StreamID: hf.StreamID,
  1864. EndHeaders: true,
  1865. EndStream: false, // as the GFE does
  1866. BlockFragment: buf.Bytes(),
  1867. })
  1868. ct.fr.WriteData(hf.StreamID, true, nil)
  1869. <-clientDone
  1870. return nil
  1871. }
  1872. }
  1873. ct.run()
  1874. }
  1875. type neverEnding byte
  1876. func (b neverEnding) Read(p []byte) (int, error) {
  1877. for i := range p {
  1878. p[i] = byte(b)
  1879. }
  1880. return len(p), nil
  1881. }
  1882. // golang.org/issue/15425: test that a handler closing the request
  1883. // body doesn't terminate the stream to the peer. (It just stops
  1884. // readability from the handler's side, and eventually the client
  1885. // runs out of flow control tokens)
  1886. func TestTransportHandlerBodyClose(t *testing.T) {
  1887. const bodySize = 10 << 20
  1888. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1889. r.Body.Close()
  1890. io.Copy(w, io.LimitReader(neverEnding('A'), bodySize))
  1891. }, optOnlyServer)
  1892. defer st.Close()
  1893. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1894. defer tr.CloseIdleConnections()
  1895. g0 := runtime.NumGoroutine()
  1896. const numReq = 10
  1897. for i := 0; i < numReq; i++ {
  1898. req, err := http.NewRequest("POST", st.ts.URL, struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  1899. if err != nil {
  1900. t.Fatal(err)
  1901. }
  1902. res, err := tr.RoundTrip(req)
  1903. if err != nil {
  1904. t.Fatal(err)
  1905. }
  1906. n, err := io.Copy(ioutil.Discard, res.Body)
  1907. res.Body.Close()
  1908. if n != bodySize || err != nil {
  1909. t.Fatalf("req#%d: Copy = %d, %v; want %d, nil", i, n, err, bodySize)
  1910. }
  1911. }
  1912. tr.CloseIdleConnections()
  1913. gd := runtime.NumGoroutine() - g0
  1914. if gd > numReq/2 {
  1915. t.Errorf("appeared to leak goroutines")
  1916. }
  1917. }
  1918. // https://golang.org/issue/15930
  1919. func TestTransportFlowControl(t *testing.T) {
  1920. const (
  1921. total = 100 << 20 // 100MB
  1922. bufLen = 1 << 16
  1923. )
  1924. var wrote int64 // updated atomically
  1925. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1926. b := make([]byte, bufLen)
  1927. for wrote < total {
  1928. n, err := w.Write(b)
  1929. atomic.AddInt64(&wrote, int64(n))
  1930. if err != nil {
  1931. t.Errorf("ResponseWriter.Write error: %v", err)
  1932. break
  1933. }
  1934. w.(http.Flusher).Flush()
  1935. }
  1936. }, optOnlyServer)
  1937. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1938. defer tr.CloseIdleConnections()
  1939. req, err := http.NewRequest("GET", st.ts.URL, nil)
  1940. if err != nil {
  1941. t.Fatal("NewRequest error:", err)
  1942. }
  1943. resp, err := tr.RoundTrip(req)
  1944. if err != nil {
  1945. t.Fatal("RoundTrip error:", err)
  1946. }
  1947. defer resp.Body.Close()
  1948. var read int64
  1949. b := make([]byte, bufLen)
  1950. for {
  1951. n, err := resp.Body.Read(b)
  1952. if err == io.EOF {
  1953. break
  1954. }
  1955. if err != nil {
  1956. t.Fatal("Read error:", err)
  1957. }
  1958. read += int64(n)
  1959. const max = transportDefaultStreamFlow
  1960. if w := atomic.LoadInt64(&wrote); -max > read-w || read-w > max {
  1961. t.Fatalf("Too much data inflight: server wrote %v bytes but client only received %v", w, read)
  1962. }
  1963. // Let the server get ahead of the client.
  1964. time.Sleep(1 * time.Millisecond)
  1965. }
  1966. }
  1967. // golang.org/issue/14627 -- if the server sends a GOAWAY frame, make
  1968. // the Transport remember it and return it back to users (via
  1969. // RoundTrip or request body reads) if needed (e.g. if the server
  1970. // proceeds to close the TCP connection before the client gets its
  1971. // response)
  1972. func TestTransportUsesGoAwayDebugError_RoundTrip(t *testing.T) {
  1973. testTransportUsesGoAwayDebugError(t, false)
  1974. }
  1975. func TestTransportUsesGoAwayDebugError_Body(t *testing.T) {
  1976. testTransportUsesGoAwayDebugError(t, true)
  1977. }
  1978. func testTransportUsesGoAwayDebugError(t *testing.T, failMidBody bool) {
  1979. ct := newClientTester(t)
  1980. clientDone := make(chan struct{})
  1981. const goAwayErrCode = ErrCodeHTTP11Required // arbitrary
  1982. const goAwayDebugData = "some debug data"
  1983. ct.client = func() error {
  1984. defer close(clientDone)
  1985. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1986. res, err := ct.tr.RoundTrip(req)
  1987. if failMidBody {
  1988. if err != nil {
  1989. return fmt.Errorf("unexpected client RoundTrip error: %v", err)
  1990. }
  1991. _, err = io.Copy(ioutil.Discard, res.Body)
  1992. res.Body.Close()
  1993. }
  1994. want := GoAwayError{
  1995. LastStreamID: 5,
  1996. ErrCode: goAwayErrCode,
  1997. DebugData: goAwayDebugData,
  1998. }
  1999. if !reflect.DeepEqual(err, want) {
  2000. t.Errorf("RoundTrip error = %T: %#v, want %T (%#T)", err, err, want, want)
  2001. }
  2002. return nil
  2003. }
  2004. ct.server = func() error {
  2005. ct.greet()
  2006. for {
  2007. f, err := ct.fr.ReadFrame()
  2008. if err != nil {
  2009. t.Logf("ReadFrame: %v", err)
  2010. return nil
  2011. }
  2012. hf, ok := f.(*HeadersFrame)
  2013. if !ok {
  2014. continue
  2015. }
  2016. if failMidBody {
  2017. var buf bytes.Buffer
  2018. enc := hpack.NewEncoder(&buf)
  2019. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2020. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  2021. ct.fr.WriteHeaders(HeadersFrameParam{
  2022. StreamID: hf.StreamID,
  2023. EndHeaders: true,
  2024. EndStream: false,
  2025. BlockFragment: buf.Bytes(),
  2026. })
  2027. }
  2028. // Write two GOAWAY frames, to test that the Transport takes
  2029. // the interesting parts of both.
  2030. ct.fr.WriteGoAway(5, ErrCodeNo, []byte(goAwayDebugData))
  2031. ct.fr.WriteGoAway(5, goAwayErrCode, nil)
  2032. ct.sc.(*net.TCPConn).CloseWrite()
  2033. <-clientDone
  2034. return nil
  2035. }
  2036. }
  2037. ct.run()
  2038. }
  2039. // See golang.org/issue/16481
  2040. func TestTransportReturnsUnusedFlowControl(t *testing.T) {
  2041. ct := newClientTester(t)
  2042. clientClosed := make(chan bool, 1)
  2043. serverWroteBody := make(chan bool, 1)
  2044. ct.client = func() error {
  2045. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2046. res, err := ct.tr.RoundTrip(req)
  2047. if err != nil {
  2048. return err
  2049. }
  2050. <-serverWroteBody
  2051. if n, err := res.Body.Read(make([]byte, 1)); err != nil || n != 1 {
  2052. return fmt.Errorf("body read = %v, %v; want 1, nil", n, err)
  2053. }
  2054. res.Body.Close() // leaving 4999 bytes unread
  2055. clientClosed <- true
  2056. return nil
  2057. }
  2058. ct.server = func() error {
  2059. ct.greet()
  2060. var hf *HeadersFrame
  2061. for {
  2062. f, err := ct.fr.ReadFrame()
  2063. if err != nil {
  2064. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2065. }
  2066. switch f.(type) {
  2067. case *WindowUpdateFrame, *SettingsFrame:
  2068. continue
  2069. }
  2070. var ok bool
  2071. hf, ok = f.(*HeadersFrame)
  2072. if !ok {
  2073. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2074. }
  2075. break
  2076. }
  2077. var buf bytes.Buffer
  2078. enc := hpack.NewEncoder(&buf)
  2079. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2080. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2081. ct.fr.WriteHeaders(HeadersFrameParam{
  2082. StreamID: hf.StreamID,
  2083. EndHeaders: true,
  2084. EndStream: false,
  2085. BlockFragment: buf.Bytes(),
  2086. })
  2087. ct.fr.WriteData(hf.StreamID, false, make([]byte, 5000)) // without ending stream
  2088. serverWroteBody <- true
  2089. <-clientClosed
  2090. waitingFor := "RSTStreamFrame"
  2091. for {
  2092. f, err := ct.fr.ReadFrame()
  2093. if err != nil {
  2094. return fmt.Errorf("ReadFrame while waiting for %s: %v", waitingFor, err)
  2095. }
  2096. if _, ok := f.(*SettingsFrame); ok {
  2097. continue
  2098. }
  2099. switch waitingFor {
  2100. case "RSTStreamFrame":
  2101. if rf, ok := f.(*RSTStreamFrame); !ok || rf.ErrCode != ErrCodeCancel {
  2102. return fmt.Errorf("Expected a WindowUpdateFrame with code cancel; got %v", summarizeFrame(f))
  2103. }
  2104. waitingFor = "WindowUpdateFrame"
  2105. case "WindowUpdateFrame":
  2106. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != 4999 {
  2107. return fmt.Errorf("Expected WindowUpdateFrame for 4999 bytes; got %v", summarizeFrame(f))
  2108. }
  2109. return nil
  2110. }
  2111. }
  2112. }
  2113. ct.run()
  2114. }
  2115. // Issue 16612: adjust flow control on open streams when transport
  2116. // receives SETTINGS with INITIAL_WINDOW_SIZE from server.
  2117. func TestTransportAdjustsFlowControl(t *testing.T) {
  2118. ct := newClientTester(t)
  2119. clientDone := make(chan struct{})
  2120. const bodySize = 1 << 20
  2121. ct.client = func() error {
  2122. defer ct.cc.(*net.TCPConn).CloseWrite()
  2123. defer close(clientDone)
  2124. req, _ := http.NewRequest("POST", "https://dummy.tld/", struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  2125. res, err := ct.tr.RoundTrip(req)
  2126. if err != nil {
  2127. return err
  2128. }
  2129. res.Body.Close()
  2130. return nil
  2131. }
  2132. ct.server = func() error {
  2133. _, err := io.ReadFull(ct.sc, make([]byte, len(ClientPreface)))
  2134. if err != nil {
  2135. return fmt.Errorf("reading client preface: %v", err)
  2136. }
  2137. var gotBytes int64
  2138. var sentSettings bool
  2139. for {
  2140. f, err := ct.fr.ReadFrame()
  2141. if err != nil {
  2142. select {
  2143. case <-clientDone:
  2144. return nil
  2145. default:
  2146. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2147. }
  2148. }
  2149. switch f := f.(type) {
  2150. case *DataFrame:
  2151. gotBytes += int64(len(f.Data()))
  2152. // After we've got half the client's
  2153. // initial flow control window's worth
  2154. // of request body data, give it just
  2155. // enough flow control to finish.
  2156. if gotBytes >= initialWindowSize/2 && !sentSettings {
  2157. sentSettings = true
  2158. ct.fr.WriteSettings(Setting{ID: SettingInitialWindowSize, Val: bodySize})
  2159. ct.fr.WriteWindowUpdate(0, bodySize)
  2160. ct.fr.WriteSettingsAck()
  2161. }
  2162. if f.StreamEnded() {
  2163. var buf bytes.Buffer
  2164. enc := hpack.NewEncoder(&buf)
  2165. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2166. ct.fr.WriteHeaders(HeadersFrameParam{
  2167. StreamID: f.StreamID,
  2168. EndHeaders: true,
  2169. EndStream: true,
  2170. BlockFragment: buf.Bytes(),
  2171. })
  2172. }
  2173. }
  2174. }
  2175. }
  2176. ct.run()
  2177. }
  2178. // See golang.org/issue/16556
  2179. func TestTransportReturnsDataPaddingFlowControl(t *testing.T) {
  2180. ct := newClientTester(t)
  2181. unblockClient := make(chan bool, 1)
  2182. ct.client = func() error {
  2183. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2184. res, err := ct.tr.RoundTrip(req)
  2185. if err != nil {
  2186. return err
  2187. }
  2188. defer res.Body.Close()
  2189. <-unblockClient
  2190. return nil
  2191. }
  2192. ct.server = func() error {
  2193. ct.greet()
  2194. var hf *HeadersFrame
  2195. for {
  2196. f, err := ct.fr.ReadFrame()
  2197. if err != nil {
  2198. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2199. }
  2200. switch f.(type) {
  2201. case *WindowUpdateFrame, *SettingsFrame:
  2202. continue
  2203. }
  2204. var ok bool
  2205. hf, ok = f.(*HeadersFrame)
  2206. if !ok {
  2207. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2208. }
  2209. break
  2210. }
  2211. var buf bytes.Buffer
  2212. enc := hpack.NewEncoder(&buf)
  2213. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2214. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2215. ct.fr.WriteHeaders(HeadersFrameParam{
  2216. StreamID: hf.StreamID,
  2217. EndHeaders: true,
  2218. EndStream: false,
  2219. BlockFragment: buf.Bytes(),
  2220. })
  2221. pad := []byte("12345")
  2222. ct.fr.WriteDataPadded(hf.StreamID, false, make([]byte, 5000), pad) // without ending stream
  2223. f, err := ct.readNonSettingsFrame()
  2224. if err != nil {
  2225. return fmt.Errorf("ReadFrame while waiting for first WindowUpdateFrame: %v", err)
  2226. }
  2227. wantBack := uint32(len(pad)) + 1 // one byte for the length of the padding
  2228. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID != 0 {
  2229. return fmt.Errorf("Expected conn WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2230. }
  2231. f, err = ct.readNonSettingsFrame()
  2232. if err != nil {
  2233. return fmt.Errorf("ReadFrame while waiting for second WindowUpdateFrame: %v", err)
  2234. }
  2235. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID == 0 {
  2236. return fmt.Errorf("Expected stream WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2237. }
  2238. unblockClient <- true
  2239. return nil
  2240. }
  2241. ct.run()
  2242. }
  2243. // golang.org/issue/16572 -- RoundTrip shouldn't hang when it gets a
  2244. // StreamError as a result of the response HEADERS
  2245. func TestTransportReturnsErrorOnBadResponseHeaders(t *testing.T) {
  2246. ct := newClientTester(t)
  2247. ct.client = func() error {
  2248. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2249. res, err := ct.tr.RoundTrip(req)
  2250. if err == nil {
  2251. res.Body.Close()
  2252. return errors.New("unexpected successful GET")
  2253. }
  2254. want := StreamError{1, ErrCodeProtocol, headerFieldNameError(" content-type")}
  2255. if !reflect.DeepEqual(want, err) {
  2256. t.Errorf("RoundTrip error = %#v; want %#v", err, want)
  2257. }
  2258. return nil
  2259. }
  2260. ct.server = func() error {
  2261. ct.greet()
  2262. hf, err := ct.firstHeaders()
  2263. if err != nil {
  2264. return err
  2265. }
  2266. var buf bytes.Buffer
  2267. enc := hpack.NewEncoder(&buf)
  2268. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2269. enc.WriteField(hpack.HeaderField{Name: " content-type", Value: "bogus"}) // bogus spaces
  2270. ct.fr.WriteHeaders(HeadersFrameParam{
  2271. StreamID: hf.StreamID,
  2272. EndHeaders: true,
  2273. EndStream: false,
  2274. BlockFragment: buf.Bytes(),
  2275. })
  2276. for {
  2277. fr, err := ct.readFrame()
  2278. if err != nil {
  2279. return fmt.Errorf("error waiting for RST_STREAM from client: %v", err)
  2280. }
  2281. if _, ok := fr.(*SettingsFrame); ok {
  2282. continue
  2283. }
  2284. if rst, ok := fr.(*RSTStreamFrame); !ok || rst.StreamID != 1 || rst.ErrCode != ErrCodeProtocol {
  2285. t.Errorf("Frame = %v; want RST_STREAM for stream 1 with ErrCodeProtocol", summarizeFrame(fr))
  2286. }
  2287. break
  2288. }
  2289. return nil
  2290. }
  2291. ct.run()
  2292. }
  2293. // byteAndEOFReader returns is in an io.Reader which reads one byte
  2294. // (the underlying byte) and io.EOF at once in its Read call.
  2295. type byteAndEOFReader byte
  2296. func (b byteAndEOFReader) Read(p []byte) (n int, err error) {
  2297. if len(p) == 0 {
  2298. panic("unexpected useless call")
  2299. }
  2300. p[0] = byte(b)
  2301. return 1, io.EOF
  2302. }
  2303. // Issue 16788: the Transport had a regression where it started
  2304. // sending a spurious DATA frame with a duplicate END_STREAM bit after
  2305. // the request body writer goroutine had already read an EOF from the
  2306. // Request.Body and included the END_STREAM on a data-carrying DATA
  2307. // frame.
  2308. //
  2309. // Notably, to trigger this, the requests need to use a Request.Body
  2310. // which returns (non-0, io.EOF) and also needs to set the ContentLength
  2311. // explicitly.
  2312. func TestTransportBodyDoubleEndStream(t *testing.T) {
  2313. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2314. // Nothing.
  2315. }, optOnlyServer)
  2316. defer st.Close()
  2317. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2318. defer tr.CloseIdleConnections()
  2319. for i := 0; i < 2; i++ {
  2320. req, _ := http.NewRequest("POST", st.ts.URL, byteAndEOFReader('a'))
  2321. req.ContentLength = 1
  2322. res, err := tr.RoundTrip(req)
  2323. if err != nil {
  2324. t.Fatalf("failure on req %d: %v", i+1, err)
  2325. }
  2326. defer res.Body.Close()
  2327. }
  2328. }
  2329. // golang.org/issue/17071 -- don't sniff the first byte of the request body
  2330. // before we've determined that the ClientConn is usable.
  2331. func TestRoundTripDoesntConsumeRequestBodyEarly(t *testing.T) {
  2332. const body = "foo"
  2333. req, _ := http.NewRequest("POST", "http://foo.com/", ioutil.NopCloser(strings.NewReader(body)))
  2334. cc := &ClientConn{
  2335. closed: true,
  2336. }
  2337. _, err := cc.RoundTrip(req)
  2338. if err != errClientConnUnusable {
  2339. t.Fatalf("RoundTrip = %v; want errClientConnUnusable", err)
  2340. }
  2341. slurp, err := ioutil.ReadAll(req.Body)
  2342. if err != nil {
  2343. t.Errorf("ReadAll = %v", err)
  2344. }
  2345. if string(slurp) != body {
  2346. t.Errorf("Body = %q; want %q", slurp, body)
  2347. }
  2348. }