transport_test.go 74 KB

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