transport_test.go 75 KB

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