transport_test.go 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797
  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. "context"
  9. "crypto/tls"
  10. "errors"
  11. "flag"
  12. "fmt"
  13. "io"
  14. "io/ioutil"
  15. "log"
  16. "math/rand"
  17. "net"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/http/httptrace"
  21. "net/textproto"
  22. "net/url"
  23. "os"
  24. "reflect"
  25. "runtime"
  26. "sort"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "sync/atomic"
  31. "testing"
  32. "time"
  33. "golang.org/x/net/http2/hpack"
  34. )
  35. var (
  36. extNet = flag.Bool("extnet", false, "do external network tests")
  37. transportHost = flag.String("transporthost", "http2.golang.org", "hostname to use for TestTransport")
  38. insecure = flag.Bool("insecure", false, "insecure TLS dials") // TODO: dead code. remove?
  39. )
  40. var tlsConfigInsecure = &tls.Config{InsecureSkipVerify: true}
  41. var canceledCtx context.Context
  42. func init() {
  43. ctx, cancel := context.WithCancel(context.Background())
  44. cancel()
  45. canceledCtx = ctx
  46. }
  47. func TestTransportExternal(t *testing.T) {
  48. if !*extNet {
  49. t.Skip("skipping external network test")
  50. }
  51. req, _ := http.NewRequest("GET", "https://"+*transportHost+"/", nil)
  52. rt := &Transport{TLSClientConfig: tlsConfigInsecure}
  53. res, err := rt.RoundTrip(req)
  54. if err != nil {
  55. t.Fatalf("%v", err)
  56. }
  57. res.Write(os.Stdout)
  58. }
  59. type fakeTLSConn struct {
  60. net.Conn
  61. }
  62. func (c *fakeTLSConn) ConnectionState() tls.ConnectionState {
  63. return tls.ConnectionState{
  64. Version: tls.VersionTLS12,
  65. CipherSuite: cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  66. }
  67. }
  68. func startH2cServer(t *testing.T) net.Listener {
  69. h2Server := &Server{}
  70. l := newLocalListener(t)
  71. go func() {
  72. conn, err := l.Accept()
  73. if err != nil {
  74. t.Error(err)
  75. return
  76. }
  77. h2Server.ServeConn(&fakeTLSConn{conn}, &ServeConnOpts{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  78. fmt.Fprintf(w, "Hello, %v, http: %v", r.URL.Path, r.TLS == nil)
  79. })})
  80. }()
  81. return l
  82. }
  83. func TestTransportH2c(t *testing.T) {
  84. l := startH2cServer(t)
  85. defer l.Close()
  86. req, err := http.NewRequest("GET", "http://"+l.Addr().String()+"/foobar", nil)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. var gotConnCnt int32
  91. trace := &httptrace.ClientTrace{
  92. GotConn: func(connInfo httptrace.GotConnInfo) {
  93. if !connInfo.Reused {
  94. atomic.AddInt32(&gotConnCnt, 1)
  95. }
  96. },
  97. }
  98. req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  99. tr := &Transport{
  100. AllowHTTP: true,
  101. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  102. return net.Dial(network, addr)
  103. },
  104. }
  105. res, err := tr.RoundTrip(req)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if res.ProtoMajor != 2 {
  110. t.Fatal("proto not h2c")
  111. }
  112. body, err := ioutil.ReadAll(res.Body)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if got, want := string(body), "Hello, /foobar, http: true"; got != want {
  117. t.Fatalf("response got %v, want %v", got, want)
  118. }
  119. if got, want := gotConnCnt, int32(1); got != want {
  120. t.Errorf("Too many got connections: %d", gotConnCnt)
  121. }
  122. }
  123. func TestTransport(t *testing.T) {
  124. const body = "sup"
  125. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  126. io.WriteString(w, body)
  127. }, optOnlyServer)
  128. defer st.Close()
  129. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  130. defer tr.CloseIdleConnections()
  131. u, err := url.Parse(st.ts.URL)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. for i, m := range []string{"GET", ""} {
  136. req := &http.Request{
  137. Method: m,
  138. URL: u,
  139. }
  140. res, err := tr.RoundTrip(req)
  141. if err != nil {
  142. t.Fatalf("%d: %s", i, err)
  143. }
  144. t.Logf("%d: Got res: %+v", i, res)
  145. if g, w := res.StatusCode, 200; g != w {
  146. t.Errorf("%d: StatusCode = %v; want %v", i, g, w)
  147. }
  148. if g, w := res.Status, "200 OK"; g != w {
  149. t.Errorf("%d: Status = %q; want %q", i, g, w)
  150. }
  151. wantHeader := http.Header{
  152. "Content-Length": []string{"3"},
  153. "Content-Type": []string{"text/plain; charset=utf-8"},
  154. "Date": []string{"XXX"}, // see cleanDate
  155. }
  156. cleanDate(res)
  157. if !reflect.DeepEqual(res.Header, wantHeader) {
  158. t.Errorf("%d: res Header = %v; want %v", i, res.Header, wantHeader)
  159. }
  160. if res.Request != req {
  161. t.Errorf("%d: Response.Request = %p; want %p", i, res.Request, req)
  162. }
  163. if res.TLS == nil {
  164. t.Errorf("%d: Response.TLS = nil; want non-nil", i)
  165. }
  166. slurp, err := ioutil.ReadAll(res.Body)
  167. if err != nil {
  168. t.Errorf("%d: Body read: %v", i, err)
  169. } else if string(slurp) != body {
  170. t.Errorf("%d: Body = %q; want %q", i, slurp, body)
  171. }
  172. res.Body.Close()
  173. }
  174. }
  175. func onSameConn(t *testing.T, modReq func(*http.Request)) bool {
  176. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  177. io.WriteString(w, r.RemoteAddr)
  178. }, optOnlyServer, func(c net.Conn, st http.ConnState) {
  179. t.Logf("conn %v is now state %v", c.RemoteAddr(), st)
  180. })
  181. defer st.Close()
  182. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  183. defer tr.CloseIdleConnections()
  184. get := func() string {
  185. req, err := http.NewRequest("GET", st.ts.URL, nil)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. modReq(req)
  190. res, err := tr.RoundTrip(req)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. defer res.Body.Close()
  195. slurp, err := ioutil.ReadAll(res.Body)
  196. if err != nil {
  197. t.Fatalf("Body read: %v", err)
  198. }
  199. addr := strings.TrimSpace(string(slurp))
  200. if addr == "" {
  201. t.Fatalf("didn't get an addr in response")
  202. }
  203. return addr
  204. }
  205. first := get()
  206. second := get()
  207. return first == second
  208. }
  209. func TestTransportReusesConns(t *testing.T) {
  210. if !onSameConn(t, func(*http.Request) {}) {
  211. t.Errorf("first and second responses were on different connections")
  212. }
  213. }
  214. func TestTransportReusesConn_RequestClose(t *testing.T) {
  215. if onSameConn(t, func(r *http.Request) { r.Close = true }) {
  216. t.Errorf("first and second responses were not on different connections")
  217. }
  218. }
  219. func TestTransportReusesConn_ConnClose(t *testing.T) {
  220. if onSameConn(t, func(r *http.Request) { r.Header.Set("Connection", "close") }) {
  221. t.Errorf("first and second responses were not on different connections")
  222. }
  223. }
  224. // Tests that the Transport only keeps one pending dial open per destination address.
  225. // https://golang.org/issue/13397
  226. func TestTransportGroupsPendingDials(t *testing.T) {
  227. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  228. io.WriteString(w, r.RemoteAddr)
  229. }, optOnlyServer)
  230. defer st.Close()
  231. tr := &Transport{
  232. TLSClientConfig: tlsConfigInsecure,
  233. }
  234. defer tr.CloseIdleConnections()
  235. var (
  236. mu sync.Mutex
  237. dials = map[string]int{}
  238. )
  239. var gotConnCnt int32
  240. trace := &httptrace.ClientTrace{
  241. GotConn: func(connInfo httptrace.GotConnInfo) {
  242. if !connInfo.Reused {
  243. atomic.AddInt32(&gotConnCnt, 1)
  244. }
  245. },
  246. }
  247. var wg sync.WaitGroup
  248. for i := 0; i < 10; i++ {
  249. wg.Add(1)
  250. go func() {
  251. defer wg.Done()
  252. req, err := http.NewRequest("GET", st.ts.URL, nil)
  253. if err != nil {
  254. t.Error(err)
  255. return
  256. }
  257. req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  258. res, err := tr.RoundTrip(req)
  259. if err != nil {
  260. t.Error(err)
  261. return
  262. }
  263. defer res.Body.Close()
  264. slurp, err := ioutil.ReadAll(res.Body)
  265. if err != nil {
  266. t.Errorf("Body read: %v", err)
  267. }
  268. addr := strings.TrimSpace(string(slurp))
  269. if addr == "" {
  270. t.Errorf("didn't get an addr in response")
  271. }
  272. mu.Lock()
  273. dials[addr]++
  274. mu.Unlock()
  275. }()
  276. }
  277. wg.Wait()
  278. if len(dials) != 1 {
  279. t.Errorf("saw %d dials; want 1: %v", len(dials), dials)
  280. }
  281. tr.CloseIdleConnections()
  282. if err := retry(50, 10*time.Millisecond, func() error {
  283. cp, ok := tr.connPool().(*clientConnPool)
  284. if !ok {
  285. return fmt.Errorf("Conn pool is %T; want *clientConnPool", tr.connPool())
  286. }
  287. cp.mu.Lock()
  288. defer cp.mu.Unlock()
  289. if len(cp.dialing) != 0 {
  290. return fmt.Errorf("dialing map = %v; want empty", cp.dialing)
  291. }
  292. if len(cp.conns) != 0 {
  293. return fmt.Errorf("conns = %v; want empty", cp.conns)
  294. }
  295. if len(cp.keys) != 0 {
  296. return fmt.Errorf("keys = %v; want empty", cp.keys)
  297. }
  298. return nil
  299. }); err != nil {
  300. t.Errorf("State of pool after CloseIdleConnections: %v", err)
  301. }
  302. if got, want := gotConnCnt, int32(1); got != want {
  303. t.Errorf("Too many got connections: %d", gotConnCnt)
  304. }
  305. }
  306. func retry(tries int, delay time.Duration, fn func() error) error {
  307. var err error
  308. for i := 0; i < tries; i++ {
  309. err = fn()
  310. if err == nil {
  311. return nil
  312. }
  313. time.Sleep(delay)
  314. }
  315. return err
  316. }
  317. func TestTransportAbortClosesPipes(t *testing.T) {
  318. shutdown := make(chan struct{})
  319. st := newServerTester(t,
  320. func(w http.ResponseWriter, r *http.Request) {
  321. w.(http.Flusher).Flush()
  322. <-shutdown
  323. },
  324. optOnlyServer,
  325. )
  326. defer st.Close()
  327. defer close(shutdown) // we must shutdown before st.Close() to avoid hanging
  328. errCh := make(chan error)
  329. go func() {
  330. defer close(errCh)
  331. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  332. req, err := http.NewRequest("GET", st.ts.URL, nil)
  333. if err != nil {
  334. errCh <- err
  335. return
  336. }
  337. res, err := tr.RoundTrip(req)
  338. if err != nil {
  339. errCh <- err
  340. return
  341. }
  342. defer res.Body.Close()
  343. st.closeConn()
  344. _, err = ioutil.ReadAll(res.Body)
  345. if err == nil {
  346. errCh <- errors.New("expected error from res.Body.Read")
  347. return
  348. }
  349. }()
  350. select {
  351. case err := <-errCh:
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. // deadlock? that's a bug.
  356. case <-time.After(3 * time.Second):
  357. t.Fatal("timeout")
  358. }
  359. }
  360. // TODO: merge this with TestTransportBody to make TestTransportRequest? This
  361. // could be a table-driven test with extra goodies.
  362. func TestTransportPath(t *testing.T) {
  363. gotc := make(chan *url.URL, 1)
  364. st := newServerTester(t,
  365. func(w http.ResponseWriter, r *http.Request) {
  366. gotc <- r.URL
  367. },
  368. optOnlyServer,
  369. )
  370. defer st.Close()
  371. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  372. defer tr.CloseIdleConnections()
  373. const (
  374. path = "/testpath"
  375. query = "q=1"
  376. )
  377. surl := st.ts.URL + path + "?" + query
  378. req, err := http.NewRequest("POST", surl, nil)
  379. if err != nil {
  380. t.Fatal(err)
  381. }
  382. c := &http.Client{Transport: tr}
  383. res, err := c.Do(req)
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. defer res.Body.Close()
  388. got := <-gotc
  389. if got.Path != path {
  390. t.Errorf("Read Path = %q; want %q", got.Path, path)
  391. }
  392. if got.RawQuery != query {
  393. t.Errorf("Read RawQuery = %q; want %q", got.RawQuery, query)
  394. }
  395. }
  396. func randString(n int) string {
  397. rnd := rand.New(rand.NewSource(int64(n)))
  398. b := make([]byte, n)
  399. for i := range b {
  400. b[i] = byte(rnd.Intn(256))
  401. }
  402. return string(b)
  403. }
  404. type panicReader struct{}
  405. func (panicReader) Read([]byte) (int, error) { panic("unexpected Read") }
  406. func (panicReader) Close() error { panic("unexpected Close") }
  407. func TestActualContentLength(t *testing.T) {
  408. tests := []struct {
  409. req *http.Request
  410. want int64
  411. }{
  412. // Verify we don't read from Body:
  413. 0: {
  414. req: &http.Request{Body: panicReader{}},
  415. want: -1,
  416. },
  417. // nil Body means 0, regardless of ContentLength:
  418. 1: {
  419. req: &http.Request{Body: nil, ContentLength: 5},
  420. want: 0,
  421. },
  422. // ContentLength is used if set.
  423. 2: {
  424. req: &http.Request{Body: panicReader{}, ContentLength: 5},
  425. want: 5,
  426. },
  427. // http.NoBody means 0, not -1.
  428. 3: {
  429. req: &http.Request{Body: http.NoBody},
  430. want: 0,
  431. },
  432. }
  433. for i, tt := range tests {
  434. got := actualContentLength(tt.req)
  435. if got != tt.want {
  436. t.Errorf("test[%d]: got %d; want %d", i, got, tt.want)
  437. }
  438. }
  439. }
  440. func TestTransportBody(t *testing.T) {
  441. bodyTests := []struct {
  442. body string
  443. noContentLen bool
  444. }{
  445. {body: "some message"},
  446. {body: "some message", noContentLen: true},
  447. {body: strings.Repeat("a", 1<<20), noContentLen: true},
  448. {body: strings.Repeat("a", 1<<20)},
  449. {body: randString(16<<10 - 1)},
  450. {body: randString(16 << 10)},
  451. {body: randString(16<<10 + 1)},
  452. {body: randString(512<<10 - 1)},
  453. {body: randString(512 << 10)},
  454. {body: randString(512<<10 + 1)},
  455. {body: randString(1<<20 - 1)},
  456. {body: randString(1 << 20)},
  457. {body: randString(1<<20 + 2)},
  458. }
  459. type reqInfo struct {
  460. req *http.Request
  461. slurp []byte
  462. err error
  463. }
  464. gotc := make(chan reqInfo, 1)
  465. st := newServerTester(t,
  466. func(w http.ResponseWriter, r *http.Request) {
  467. slurp, err := ioutil.ReadAll(r.Body)
  468. if err != nil {
  469. gotc <- reqInfo{err: err}
  470. } else {
  471. gotc <- reqInfo{req: r, slurp: slurp}
  472. }
  473. },
  474. optOnlyServer,
  475. )
  476. defer st.Close()
  477. for i, tt := range bodyTests {
  478. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  479. defer tr.CloseIdleConnections()
  480. var body io.Reader = strings.NewReader(tt.body)
  481. if tt.noContentLen {
  482. body = struct{ io.Reader }{body} // just a Reader, hiding concrete type and other methods
  483. }
  484. req, err := http.NewRequest("POST", st.ts.URL, body)
  485. if err != nil {
  486. t.Fatalf("#%d: %v", i, err)
  487. }
  488. c := &http.Client{Transport: tr}
  489. res, err := c.Do(req)
  490. if err != nil {
  491. t.Fatalf("#%d: %v", i, err)
  492. }
  493. defer res.Body.Close()
  494. ri := <-gotc
  495. if ri.err != nil {
  496. t.Errorf("#%d: read error: %v", i, ri.err)
  497. continue
  498. }
  499. if got := string(ri.slurp); got != tt.body {
  500. 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))
  501. }
  502. wantLen := int64(len(tt.body))
  503. if tt.noContentLen && tt.body != "" {
  504. wantLen = -1
  505. }
  506. if ri.req.ContentLength != wantLen {
  507. t.Errorf("#%d. handler got ContentLength = %v; want %v", i, ri.req.ContentLength, wantLen)
  508. }
  509. }
  510. }
  511. func shortString(v string) string {
  512. const maxLen = 100
  513. if len(v) <= maxLen {
  514. return v
  515. }
  516. return fmt.Sprintf("%v[...%d bytes omitted...]%v", v[:maxLen/2], len(v)-maxLen, v[len(v)-maxLen/2:])
  517. }
  518. func TestTransportDialTLS(t *testing.T) {
  519. var mu sync.Mutex // guards following
  520. var gotReq, didDial bool
  521. ts := newServerTester(t,
  522. func(w http.ResponseWriter, r *http.Request) {
  523. mu.Lock()
  524. gotReq = true
  525. mu.Unlock()
  526. },
  527. optOnlyServer,
  528. )
  529. defer ts.Close()
  530. tr := &Transport{
  531. DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
  532. mu.Lock()
  533. didDial = true
  534. mu.Unlock()
  535. cfg.InsecureSkipVerify = true
  536. c, err := tls.Dial(netw, addr, cfg)
  537. if err != nil {
  538. return nil, err
  539. }
  540. return c, c.Handshake()
  541. },
  542. }
  543. defer tr.CloseIdleConnections()
  544. client := &http.Client{Transport: tr}
  545. res, err := client.Get(ts.ts.URL)
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. res.Body.Close()
  550. mu.Lock()
  551. if !gotReq {
  552. t.Error("didn't get request")
  553. }
  554. if !didDial {
  555. t.Error("didn't use dial hook")
  556. }
  557. }
  558. func TestConfigureTransport(t *testing.T) {
  559. t1 := &http.Transport{}
  560. err := ConfigureTransport(t1)
  561. if err != nil {
  562. t.Fatal(err)
  563. }
  564. if got := fmt.Sprintf("%#v", t1); !strings.Contains(got, `"h2"`) {
  565. // Laziness, to avoid buildtags.
  566. t.Errorf("stringification of HTTP/1 transport didn't contain \"h2\": %v", got)
  567. }
  568. wantNextProtos := []string{"h2", "http/1.1"}
  569. if t1.TLSClientConfig == nil {
  570. t.Errorf("nil t1.TLSClientConfig")
  571. } else if !reflect.DeepEqual(t1.TLSClientConfig.NextProtos, wantNextProtos) {
  572. t.Errorf("TLSClientConfig.NextProtos = %q; want %q", t1.TLSClientConfig.NextProtos, wantNextProtos)
  573. }
  574. if err := ConfigureTransport(t1); err == nil {
  575. t.Error("unexpected success on second call to ConfigureTransport")
  576. }
  577. // And does it work?
  578. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  579. io.WriteString(w, r.Proto)
  580. }, optOnlyServer)
  581. defer st.Close()
  582. t1.TLSClientConfig.InsecureSkipVerify = true
  583. c := &http.Client{Transport: t1}
  584. res, err := c.Get(st.ts.URL)
  585. if err != nil {
  586. t.Fatal(err)
  587. }
  588. slurp, err := ioutil.ReadAll(res.Body)
  589. if err != nil {
  590. t.Fatal(err)
  591. }
  592. if got, want := string(slurp), "HTTP/2.0"; got != want {
  593. t.Errorf("body = %q; want %q", got, want)
  594. }
  595. }
  596. type capitalizeReader struct {
  597. r io.Reader
  598. }
  599. func (cr capitalizeReader) Read(p []byte) (n int, err error) {
  600. n, err = cr.r.Read(p)
  601. for i, b := range p[:n] {
  602. if b >= 'a' && b <= 'z' {
  603. p[i] = b - ('a' - 'A')
  604. }
  605. }
  606. return
  607. }
  608. type flushWriter struct {
  609. w io.Writer
  610. }
  611. func (fw flushWriter) Write(p []byte) (n int, err error) {
  612. n, err = fw.w.Write(p)
  613. if f, ok := fw.w.(http.Flusher); ok {
  614. f.Flush()
  615. }
  616. return
  617. }
  618. type clientTester struct {
  619. t *testing.T
  620. tr *Transport
  621. sc, cc net.Conn // server and client conn
  622. fr *Framer // server's framer
  623. client func() error
  624. server func() error
  625. }
  626. func newClientTester(t *testing.T) *clientTester {
  627. var dialOnce struct {
  628. sync.Mutex
  629. dialed bool
  630. }
  631. ct := &clientTester{
  632. t: t,
  633. }
  634. ct.tr = &Transport{
  635. TLSClientConfig: tlsConfigInsecure,
  636. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  637. dialOnce.Lock()
  638. defer dialOnce.Unlock()
  639. if dialOnce.dialed {
  640. return nil, errors.New("only one dial allowed in test mode")
  641. }
  642. dialOnce.dialed = true
  643. return ct.cc, nil
  644. },
  645. }
  646. ln := newLocalListener(t)
  647. cc, err := net.Dial("tcp", ln.Addr().String())
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. sc, err := ln.Accept()
  652. if err != nil {
  653. t.Fatal(err)
  654. }
  655. ln.Close()
  656. ct.cc = cc
  657. ct.sc = sc
  658. ct.fr = NewFramer(sc, sc)
  659. return ct
  660. }
  661. func newLocalListener(t *testing.T) net.Listener {
  662. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  663. if err == nil {
  664. return ln
  665. }
  666. ln, err = net.Listen("tcp6", "[::1]:0")
  667. if err != nil {
  668. t.Fatal(err)
  669. }
  670. return ln
  671. }
  672. func (ct *clientTester) greet(settings ...Setting) {
  673. buf := make([]byte, len(ClientPreface))
  674. _, err := io.ReadFull(ct.sc, buf)
  675. if err != nil {
  676. ct.t.Fatalf("reading client preface: %v", err)
  677. }
  678. f, err := ct.fr.ReadFrame()
  679. if err != nil {
  680. ct.t.Fatalf("Reading client settings frame: %v", err)
  681. }
  682. if sf, ok := f.(*SettingsFrame); !ok {
  683. ct.t.Fatalf("Wanted client settings frame; got %v", f)
  684. _ = sf // stash it away?
  685. }
  686. if err := ct.fr.WriteSettings(settings...); err != nil {
  687. ct.t.Fatal(err)
  688. }
  689. if err := ct.fr.WriteSettingsAck(); err != nil {
  690. ct.t.Fatal(err)
  691. }
  692. }
  693. func (ct *clientTester) readNonSettingsFrame() (Frame, error) {
  694. for {
  695. f, err := ct.fr.ReadFrame()
  696. if err != nil {
  697. return nil, err
  698. }
  699. if _, ok := f.(*SettingsFrame); ok {
  700. continue
  701. }
  702. return f, nil
  703. }
  704. }
  705. func (ct *clientTester) cleanup() {
  706. ct.tr.CloseIdleConnections()
  707. }
  708. func (ct *clientTester) run() {
  709. errc := make(chan error, 2)
  710. ct.start("client", errc, ct.client)
  711. ct.start("server", errc, ct.server)
  712. defer ct.cleanup()
  713. for i := 0; i < 2; i++ {
  714. if err := <-errc; err != nil {
  715. ct.t.Error(err)
  716. return
  717. }
  718. }
  719. }
  720. func (ct *clientTester) start(which string, errc chan<- error, fn func() error) {
  721. go func() {
  722. finished := false
  723. var err error
  724. defer func() {
  725. if !finished {
  726. err = fmt.Errorf("%s goroutine didn't finish.", which)
  727. } else if err != nil {
  728. err = fmt.Errorf("%s: %v", which, err)
  729. }
  730. errc <- err
  731. }()
  732. err = fn()
  733. finished = true
  734. }()
  735. }
  736. func (ct *clientTester) readFrame() (Frame, error) {
  737. return readFrameTimeout(ct.fr, 2*time.Second)
  738. }
  739. func (ct *clientTester) firstHeaders() (*HeadersFrame, error) {
  740. for {
  741. f, err := ct.readFrame()
  742. if err != nil {
  743. return nil, fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  744. }
  745. switch f.(type) {
  746. case *WindowUpdateFrame, *SettingsFrame:
  747. continue
  748. }
  749. hf, ok := f.(*HeadersFrame)
  750. if !ok {
  751. return nil, fmt.Errorf("Got %T; want HeadersFrame", f)
  752. }
  753. return hf, nil
  754. }
  755. }
  756. type countingReader struct {
  757. n *int64
  758. }
  759. func (r countingReader) Read(p []byte) (n int, err error) {
  760. for i := range p {
  761. p[i] = byte(i)
  762. }
  763. atomic.AddInt64(r.n, int64(len(p)))
  764. return len(p), err
  765. }
  766. func TestTransportReqBodyAfterResponse_200(t *testing.T) { testTransportReqBodyAfterResponse(t, 200) }
  767. func TestTransportReqBodyAfterResponse_403(t *testing.T) { testTransportReqBodyAfterResponse(t, 403) }
  768. func testTransportReqBodyAfterResponse(t *testing.T, status int) {
  769. const bodySize = 10 << 20
  770. clientDone := make(chan struct{})
  771. ct := newClientTester(t)
  772. ct.client = func() error {
  773. defer ct.cc.(*net.TCPConn).CloseWrite()
  774. if runtime.GOOS == "plan9" {
  775. // CloseWrite not supported on Plan 9; Issue 17906
  776. defer ct.cc.(*net.TCPConn).Close()
  777. }
  778. defer close(clientDone)
  779. var n int64 // atomic
  780. req, err := http.NewRequest("PUT", "https://dummy.tld/", io.LimitReader(countingReader{&n}, bodySize))
  781. if err != nil {
  782. return err
  783. }
  784. res, err := ct.tr.RoundTrip(req)
  785. if err != nil {
  786. return fmt.Errorf("RoundTrip: %v", err)
  787. }
  788. defer res.Body.Close()
  789. if res.StatusCode != status {
  790. return fmt.Errorf("status code = %v; want %v", res.StatusCode, status)
  791. }
  792. slurp, err := ioutil.ReadAll(res.Body)
  793. if err != nil {
  794. return fmt.Errorf("Slurp: %v", err)
  795. }
  796. if len(slurp) > 0 {
  797. return fmt.Errorf("unexpected body: %q", slurp)
  798. }
  799. if status == 200 {
  800. if got := atomic.LoadInt64(&n); got != bodySize {
  801. return fmt.Errorf("For 200 response, Transport wrote %d bytes; want %d", got, bodySize)
  802. }
  803. } else {
  804. if got := atomic.LoadInt64(&n); got == 0 || got >= bodySize {
  805. return fmt.Errorf("For %d response, Transport wrote %d bytes; want (0,%d) exclusive", status, got, bodySize)
  806. }
  807. }
  808. return nil
  809. }
  810. ct.server = func() error {
  811. ct.greet()
  812. var buf bytes.Buffer
  813. enc := hpack.NewEncoder(&buf)
  814. var dataRecv int64
  815. var closed bool
  816. for {
  817. f, err := ct.fr.ReadFrame()
  818. if err != nil {
  819. select {
  820. case <-clientDone:
  821. // If the client's done, it
  822. // will have reported any
  823. // errors on its side.
  824. return nil
  825. default:
  826. return err
  827. }
  828. }
  829. //println(fmt.Sprintf("server got frame: %v", f))
  830. switch f := f.(type) {
  831. case *WindowUpdateFrame, *SettingsFrame:
  832. case *HeadersFrame:
  833. if !f.HeadersEnded() {
  834. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  835. }
  836. if f.StreamEnded() {
  837. return fmt.Errorf("headers contains END_STREAM unexpectedly: %v", f)
  838. }
  839. case *DataFrame:
  840. dataLen := len(f.Data())
  841. if dataLen > 0 {
  842. if dataRecv == 0 {
  843. enc.WriteField(hpack.HeaderField{Name: ":status", Value: strconv.Itoa(status)})
  844. ct.fr.WriteHeaders(HeadersFrameParam{
  845. StreamID: f.StreamID,
  846. EndHeaders: true,
  847. EndStream: false,
  848. BlockFragment: buf.Bytes(),
  849. })
  850. }
  851. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  852. return err
  853. }
  854. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  855. return err
  856. }
  857. }
  858. dataRecv += int64(dataLen)
  859. if !closed && ((status != 200 && dataRecv > 0) ||
  860. (status == 200 && dataRecv == bodySize)) {
  861. closed = true
  862. if err := ct.fr.WriteData(f.StreamID, true, nil); err != nil {
  863. return err
  864. }
  865. }
  866. default:
  867. return fmt.Errorf("Unexpected client frame %v", f)
  868. }
  869. }
  870. }
  871. ct.run()
  872. }
  873. // See golang.org/issue/13444
  874. func TestTransportFullDuplex(t *testing.T) {
  875. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  876. w.WriteHeader(200) // redundant but for clarity
  877. w.(http.Flusher).Flush()
  878. io.Copy(flushWriter{w}, capitalizeReader{r.Body})
  879. fmt.Fprintf(w, "bye.\n")
  880. }, optOnlyServer)
  881. defer st.Close()
  882. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  883. defer tr.CloseIdleConnections()
  884. c := &http.Client{Transport: tr}
  885. pr, pw := io.Pipe()
  886. req, err := http.NewRequest("PUT", st.ts.URL, ioutil.NopCloser(pr))
  887. if err != nil {
  888. t.Fatal(err)
  889. }
  890. req.ContentLength = -1
  891. res, err := c.Do(req)
  892. if err != nil {
  893. t.Fatal(err)
  894. }
  895. defer res.Body.Close()
  896. if res.StatusCode != 200 {
  897. t.Fatalf("StatusCode = %v; want %v", res.StatusCode, 200)
  898. }
  899. bs := bufio.NewScanner(res.Body)
  900. want := func(v string) {
  901. if !bs.Scan() {
  902. t.Fatalf("wanted to read %q but Scan() = false, err = %v", v, bs.Err())
  903. }
  904. }
  905. write := func(v string) {
  906. _, err := io.WriteString(pw, v)
  907. if err != nil {
  908. t.Fatalf("pipe write: %v", err)
  909. }
  910. }
  911. write("foo\n")
  912. want("FOO")
  913. write("bar\n")
  914. want("BAR")
  915. pw.Close()
  916. want("bye.")
  917. if err := bs.Err(); err != nil {
  918. t.Fatal(err)
  919. }
  920. }
  921. func TestTransportConnectRequest(t *testing.T) {
  922. gotc := make(chan *http.Request, 1)
  923. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  924. gotc <- r
  925. }, optOnlyServer)
  926. defer st.Close()
  927. u, err := url.Parse(st.ts.URL)
  928. if err != nil {
  929. t.Fatal(err)
  930. }
  931. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  932. defer tr.CloseIdleConnections()
  933. c := &http.Client{Transport: tr}
  934. tests := []struct {
  935. req *http.Request
  936. want string
  937. }{
  938. {
  939. req: &http.Request{
  940. Method: "CONNECT",
  941. Header: http.Header{},
  942. URL: u,
  943. },
  944. want: u.Host,
  945. },
  946. {
  947. req: &http.Request{
  948. Method: "CONNECT",
  949. Header: http.Header{},
  950. URL: u,
  951. Host: "example.com:123",
  952. },
  953. want: "example.com:123",
  954. },
  955. }
  956. for i, tt := range tests {
  957. res, err := c.Do(tt.req)
  958. if err != nil {
  959. t.Errorf("%d. RoundTrip = %v", i, err)
  960. continue
  961. }
  962. res.Body.Close()
  963. req := <-gotc
  964. if req.Method != "CONNECT" {
  965. t.Errorf("method = %q; want CONNECT", req.Method)
  966. }
  967. if req.Host != tt.want {
  968. t.Errorf("Host = %q; want %q", req.Host, tt.want)
  969. }
  970. if req.URL.Host != tt.want {
  971. t.Errorf("URL.Host = %q; want %q", req.URL.Host, tt.want)
  972. }
  973. }
  974. }
  975. type headerType int
  976. const (
  977. noHeader headerType = iota // omitted
  978. oneHeader
  979. splitHeader // broken into continuation on purpose
  980. )
  981. const (
  982. f0 = noHeader
  983. f1 = oneHeader
  984. f2 = splitHeader
  985. d0 = false
  986. d1 = true
  987. )
  988. // Test all 36 combinations of response frame orders:
  989. // (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) }
  990. // Generated by http://play.golang.org/p/SScqYKJYXd
  991. func TestTransportResPattern_c0h1d0t0(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f0) }
  992. func TestTransportResPattern_c0h1d0t1(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f1) }
  993. func TestTransportResPattern_c0h1d0t2(t *testing.T) { testTransportResPattern(t, f0, f1, d0, f2) }
  994. func TestTransportResPattern_c0h1d1t0(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f0) }
  995. func TestTransportResPattern_c0h1d1t1(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f1) }
  996. func TestTransportResPattern_c0h1d1t2(t *testing.T) { testTransportResPattern(t, f0, f1, d1, f2) }
  997. func TestTransportResPattern_c0h2d0t0(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f0) }
  998. func TestTransportResPattern_c0h2d0t1(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f1) }
  999. func TestTransportResPattern_c0h2d0t2(t *testing.T) { testTransportResPattern(t, f0, f2, d0, f2) }
  1000. func TestTransportResPattern_c0h2d1t0(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f0) }
  1001. func TestTransportResPattern_c0h2d1t1(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f1) }
  1002. func TestTransportResPattern_c0h2d1t2(t *testing.T) { testTransportResPattern(t, f0, f2, d1, f2) }
  1003. func TestTransportResPattern_c1h1d0t0(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f0) }
  1004. func TestTransportResPattern_c1h1d0t1(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f1) }
  1005. func TestTransportResPattern_c1h1d0t2(t *testing.T) { testTransportResPattern(t, f1, f1, d0, f2) }
  1006. func TestTransportResPattern_c1h1d1t0(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f0) }
  1007. func TestTransportResPattern_c1h1d1t1(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f1) }
  1008. func TestTransportResPattern_c1h1d1t2(t *testing.T) { testTransportResPattern(t, f1, f1, d1, f2) }
  1009. func TestTransportResPattern_c1h2d0t0(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f0) }
  1010. func TestTransportResPattern_c1h2d0t1(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f1) }
  1011. func TestTransportResPattern_c1h2d0t2(t *testing.T) { testTransportResPattern(t, f1, f2, d0, f2) }
  1012. func TestTransportResPattern_c1h2d1t0(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f0) }
  1013. func TestTransportResPattern_c1h2d1t1(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f1) }
  1014. func TestTransportResPattern_c1h2d1t2(t *testing.T) { testTransportResPattern(t, f1, f2, d1, f2) }
  1015. func TestTransportResPattern_c2h1d0t0(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f0) }
  1016. func TestTransportResPattern_c2h1d0t1(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f1) }
  1017. func TestTransportResPattern_c2h1d0t2(t *testing.T) { testTransportResPattern(t, f2, f1, d0, f2) }
  1018. func TestTransportResPattern_c2h1d1t0(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f0) }
  1019. func TestTransportResPattern_c2h1d1t1(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f1) }
  1020. func TestTransportResPattern_c2h1d1t2(t *testing.T) { testTransportResPattern(t, f2, f1, d1, f2) }
  1021. func TestTransportResPattern_c2h2d0t0(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f0) }
  1022. func TestTransportResPattern_c2h2d0t1(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f1) }
  1023. func TestTransportResPattern_c2h2d0t2(t *testing.T) { testTransportResPattern(t, f2, f2, d0, f2) }
  1024. func TestTransportResPattern_c2h2d1t0(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f0) }
  1025. func TestTransportResPattern_c2h2d1t1(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f1) }
  1026. func TestTransportResPattern_c2h2d1t2(t *testing.T) { testTransportResPattern(t, f2, f2, d1, f2) }
  1027. func testTransportResPattern(t *testing.T, expect100Continue, resHeader headerType, withData bool, trailers headerType) {
  1028. const reqBody = "some request body"
  1029. const resBody = "some response body"
  1030. if resHeader == noHeader {
  1031. // TODO: test 100-continue followed by immediate
  1032. // server stream reset, without headers in the middle?
  1033. panic("invalid combination")
  1034. }
  1035. ct := newClientTester(t)
  1036. ct.client = func() error {
  1037. req, _ := http.NewRequest("POST", "https://dummy.tld/", strings.NewReader(reqBody))
  1038. if expect100Continue != noHeader {
  1039. req.Header.Set("Expect", "100-continue")
  1040. }
  1041. res, err := ct.tr.RoundTrip(req)
  1042. if err != nil {
  1043. return fmt.Errorf("RoundTrip: %v", err)
  1044. }
  1045. defer res.Body.Close()
  1046. if res.StatusCode != 200 {
  1047. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1048. }
  1049. slurp, err := ioutil.ReadAll(res.Body)
  1050. if err != nil {
  1051. return fmt.Errorf("Slurp: %v", err)
  1052. }
  1053. wantBody := resBody
  1054. if !withData {
  1055. wantBody = ""
  1056. }
  1057. if string(slurp) != wantBody {
  1058. return fmt.Errorf("body = %q; want %q", slurp, wantBody)
  1059. }
  1060. if trailers == noHeader {
  1061. if len(res.Trailer) > 0 {
  1062. t.Errorf("Trailer = %v; want none", res.Trailer)
  1063. }
  1064. } else {
  1065. want := http.Header{"Some-Trailer": {"some-value"}}
  1066. if !reflect.DeepEqual(res.Trailer, want) {
  1067. t.Errorf("Trailer = %v; want %v", res.Trailer, want)
  1068. }
  1069. }
  1070. return nil
  1071. }
  1072. ct.server = func() error {
  1073. ct.greet()
  1074. var buf bytes.Buffer
  1075. enc := hpack.NewEncoder(&buf)
  1076. for {
  1077. f, err := ct.fr.ReadFrame()
  1078. if err != nil {
  1079. return err
  1080. }
  1081. endStream := false
  1082. send := func(mode headerType) {
  1083. hbf := buf.Bytes()
  1084. switch mode {
  1085. case oneHeader:
  1086. ct.fr.WriteHeaders(HeadersFrameParam{
  1087. StreamID: f.Header().StreamID,
  1088. EndHeaders: true,
  1089. EndStream: endStream,
  1090. BlockFragment: hbf,
  1091. })
  1092. case splitHeader:
  1093. if len(hbf) < 2 {
  1094. panic("too small")
  1095. }
  1096. ct.fr.WriteHeaders(HeadersFrameParam{
  1097. StreamID: f.Header().StreamID,
  1098. EndHeaders: false,
  1099. EndStream: endStream,
  1100. BlockFragment: hbf[:1],
  1101. })
  1102. ct.fr.WriteContinuation(f.Header().StreamID, true, hbf[1:])
  1103. default:
  1104. panic("bogus mode")
  1105. }
  1106. }
  1107. switch f := f.(type) {
  1108. case *WindowUpdateFrame, *SettingsFrame:
  1109. case *DataFrame:
  1110. if !f.StreamEnded() {
  1111. // No need to send flow control tokens. The test request body is tiny.
  1112. continue
  1113. }
  1114. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1115. {
  1116. buf.Reset()
  1117. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1118. enc.WriteField(hpack.HeaderField{Name: "x-foo", Value: "blah"})
  1119. enc.WriteField(hpack.HeaderField{Name: "x-bar", Value: "more"})
  1120. if trailers != noHeader {
  1121. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "some-trailer"})
  1122. }
  1123. endStream = withData == false && trailers == noHeader
  1124. send(resHeader)
  1125. }
  1126. if withData {
  1127. endStream = trailers == noHeader
  1128. ct.fr.WriteData(f.StreamID, endStream, []byte(resBody))
  1129. }
  1130. if trailers != noHeader {
  1131. endStream = true
  1132. buf.Reset()
  1133. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "some-value"})
  1134. send(trailers)
  1135. }
  1136. if endStream {
  1137. return nil
  1138. }
  1139. case *HeadersFrame:
  1140. if expect100Continue != noHeader {
  1141. buf.Reset()
  1142. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "100"})
  1143. send(expect100Continue)
  1144. }
  1145. }
  1146. }
  1147. }
  1148. ct.run()
  1149. }
  1150. // Issue 26189, Issue 17739: ignore unknown 1xx responses
  1151. func TestTransportUnknown1xx(t *testing.T) {
  1152. var buf bytes.Buffer
  1153. defer func() { got1xxFuncForTests = nil }()
  1154. got1xxFuncForTests = func(code int, header textproto.MIMEHeader) error {
  1155. fmt.Fprintf(&buf, "code=%d header=%v\n", code, header)
  1156. return nil
  1157. }
  1158. ct := newClientTester(t)
  1159. ct.client = func() error {
  1160. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1161. res, err := ct.tr.RoundTrip(req)
  1162. if err != nil {
  1163. return fmt.Errorf("RoundTrip: %v", err)
  1164. }
  1165. defer res.Body.Close()
  1166. if res.StatusCode != 204 {
  1167. return fmt.Errorf("status code = %v; want 204", res.StatusCode)
  1168. }
  1169. want := `code=110 header=map[Foo-Bar:[110]]
  1170. code=111 header=map[Foo-Bar:[111]]
  1171. code=112 header=map[Foo-Bar:[112]]
  1172. code=113 header=map[Foo-Bar:[113]]
  1173. code=114 header=map[Foo-Bar:[114]]
  1174. `
  1175. if got := buf.String(); got != want {
  1176. t.Errorf("Got trace:\n%s\nWant:\n%s", got, want)
  1177. }
  1178. return nil
  1179. }
  1180. ct.server = func() error {
  1181. ct.greet()
  1182. var buf bytes.Buffer
  1183. enc := hpack.NewEncoder(&buf)
  1184. for {
  1185. f, err := ct.fr.ReadFrame()
  1186. if err != nil {
  1187. return err
  1188. }
  1189. switch f := f.(type) {
  1190. case *WindowUpdateFrame, *SettingsFrame:
  1191. case *HeadersFrame:
  1192. for i := 110; i <= 114; i++ {
  1193. buf.Reset()
  1194. enc.WriteField(hpack.HeaderField{Name: ":status", Value: fmt.Sprint(i)})
  1195. enc.WriteField(hpack.HeaderField{Name: "foo-bar", Value: fmt.Sprint(i)})
  1196. ct.fr.WriteHeaders(HeadersFrameParam{
  1197. StreamID: f.StreamID,
  1198. EndHeaders: true,
  1199. EndStream: false,
  1200. BlockFragment: buf.Bytes(),
  1201. })
  1202. }
  1203. buf.Reset()
  1204. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  1205. ct.fr.WriteHeaders(HeadersFrameParam{
  1206. StreamID: f.StreamID,
  1207. EndHeaders: true,
  1208. EndStream: false,
  1209. BlockFragment: buf.Bytes(),
  1210. })
  1211. return nil
  1212. }
  1213. }
  1214. }
  1215. ct.run()
  1216. }
  1217. func TestTransportReceiveUndeclaredTrailer(t *testing.T) {
  1218. ct := newClientTester(t)
  1219. ct.client = func() error {
  1220. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1221. res, err := ct.tr.RoundTrip(req)
  1222. if err != nil {
  1223. return fmt.Errorf("RoundTrip: %v", err)
  1224. }
  1225. defer res.Body.Close()
  1226. if res.StatusCode != 200 {
  1227. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1228. }
  1229. slurp, err := ioutil.ReadAll(res.Body)
  1230. if err != nil {
  1231. return fmt.Errorf("res.Body ReadAll error = %q, %v; want %v", slurp, err, nil)
  1232. }
  1233. if len(slurp) > 0 {
  1234. return fmt.Errorf("body = %q; want nothing", slurp)
  1235. }
  1236. if _, ok := res.Trailer["Some-Trailer"]; !ok {
  1237. return fmt.Errorf("expected Some-Trailer")
  1238. }
  1239. return nil
  1240. }
  1241. ct.server = func() error {
  1242. ct.greet()
  1243. var n int
  1244. var hf *HeadersFrame
  1245. for hf == nil && n < 10 {
  1246. f, err := ct.fr.ReadFrame()
  1247. if err != nil {
  1248. return err
  1249. }
  1250. hf, _ = f.(*HeadersFrame)
  1251. n++
  1252. }
  1253. var buf bytes.Buffer
  1254. enc := hpack.NewEncoder(&buf)
  1255. // send headers without Trailer header
  1256. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1257. ct.fr.WriteHeaders(HeadersFrameParam{
  1258. StreamID: hf.StreamID,
  1259. EndHeaders: true,
  1260. EndStream: false,
  1261. BlockFragment: buf.Bytes(),
  1262. })
  1263. // send trailers
  1264. buf.Reset()
  1265. enc.WriteField(hpack.HeaderField{Name: "some-trailer", Value: "I'm an undeclared Trailer!"})
  1266. ct.fr.WriteHeaders(HeadersFrameParam{
  1267. StreamID: hf.StreamID,
  1268. EndHeaders: true,
  1269. EndStream: true,
  1270. BlockFragment: buf.Bytes(),
  1271. })
  1272. return nil
  1273. }
  1274. ct.run()
  1275. }
  1276. func TestTransportInvalidTrailer_Pseudo1(t *testing.T) {
  1277. testTransportInvalidTrailer_Pseudo(t, oneHeader)
  1278. }
  1279. func TestTransportInvalidTrailer_Pseudo2(t *testing.T) {
  1280. testTransportInvalidTrailer_Pseudo(t, splitHeader)
  1281. }
  1282. func testTransportInvalidTrailer_Pseudo(t *testing.T, trailers headerType) {
  1283. testInvalidTrailer(t, trailers, pseudoHeaderError(":colon"), func(enc *hpack.Encoder) {
  1284. enc.WriteField(hpack.HeaderField{Name: ":colon", Value: "foo"})
  1285. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1286. })
  1287. }
  1288. func TestTransportInvalidTrailer_Capital1(t *testing.T) {
  1289. testTransportInvalidTrailer_Capital(t, oneHeader)
  1290. }
  1291. func TestTransportInvalidTrailer_Capital2(t *testing.T) {
  1292. testTransportInvalidTrailer_Capital(t, splitHeader)
  1293. }
  1294. func testTransportInvalidTrailer_Capital(t *testing.T, trailers headerType) {
  1295. testInvalidTrailer(t, trailers, headerFieldNameError("Capital"), func(enc *hpack.Encoder) {
  1296. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  1297. enc.WriteField(hpack.HeaderField{Name: "Capital", Value: "bad"})
  1298. })
  1299. }
  1300. func TestTransportInvalidTrailer_EmptyFieldName(t *testing.T) {
  1301. testInvalidTrailer(t, oneHeader, headerFieldNameError(""), func(enc *hpack.Encoder) {
  1302. enc.WriteField(hpack.HeaderField{Name: "", Value: "bad"})
  1303. })
  1304. }
  1305. func TestTransportInvalidTrailer_BinaryFieldValue(t *testing.T) {
  1306. testInvalidTrailer(t, oneHeader, headerFieldValueError("has\nnewline"), func(enc *hpack.Encoder) {
  1307. enc.WriteField(hpack.HeaderField{Name: "x", Value: "has\nnewline"})
  1308. })
  1309. }
  1310. func testInvalidTrailer(t *testing.T, trailers headerType, wantErr error, writeTrailer func(*hpack.Encoder)) {
  1311. ct := newClientTester(t)
  1312. ct.client = func() error {
  1313. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1314. res, err := ct.tr.RoundTrip(req)
  1315. if err != nil {
  1316. return fmt.Errorf("RoundTrip: %v", err)
  1317. }
  1318. defer res.Body.Close()
  1319. if res.StatusCode != 200 {
  1320. return fmt.Errorf("status code = %v; want 200", res.StatusCode)
  1321. }
  1322. slurp, err := ioutil.ReadAll(res.Body)
  1323. se, ok := err.(StreamError)
  1324. if !ok || se.Cause != wantErr {
  1325. return fmt.Errorf("res.Body ReadAll error = %q, %#v; want StreamError with cause %T, %#v", slurp, err, wantErr, wantErr)
  1326. }
  1327. if len(slurp) > 0 {
  1328. return fmt.Errorf("body = %q; want nothing", slurp)
  1329. }
  1330. return nil
  1331. }
  1332. ct.server = func() error {
  1333. ct.greet()
  1334. var buf bytes.Buffer
  1335. enc := hpack.NewEncoder(&buf)
  1336. for {
  1337. f, err := ct.fr.ReadFrame()
  1338. if err != nil {
  1339. return err
  1340. }
  1341. switch f := f.(type) {
  1342. case *HeadersFrame:
  1343. var endStream bool
  1344. send := func(mode headerType) {
  1345. hbf := buf.Bytes()
  1346. switch mode {
  1347. case oneHeader:
  1348. ct.fr.WriteHeaders(HeadersFrameParam{
  1349. StreamID: f.StreamID,
  1350. EndHeaders: true,
  1351. EndStream: endStream,
  1352. BlockFragment: hbf,
  1353. })
  1354. case splitHeader:
  1355. if len(hbf) < 2 {
  1356. panic("too small")
  1357. }
  1358. ct.fr.WriteHeaders(HeadersFrameParam{
  1359. StreamID: f.StreamID,
  1360. EndHeaders: false,
  1361. EndStream: endStream,
  1362. BlockFragment: hbf[:1],
  1363. })
  1364. ct.fr.WriteContinuation(f.StreamID, true, hbf[1:])
  1365. default:
  1366. panic("bogus mode")
  1367. }
  1368. }
  1369. // Response headers (1+ frames; 1 or 2 in this test, but never 0)
  1370. {
  1371. buf.Reset()
  1372. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1373. enc.WriteField(hpack.HeaderField{Name: "trailer", Value: "declared"})
  1374. endStream = false
  1375. send(oneHeader)
  1376. }
  1377. // Trailers:
  1378. {
  1379. endStream = true
  1380. buf.Reset()
  1381. writeTrailer(enc)
  1382. send(trailers)
  1383. }
  1384. return nil
  1385. }
  1386. }
  1387. }
  1388. ct.run()
  1389. }
  1390. // headerListSize returns the HTTP2 header list size of h.
  1391. // http://httpwg.org/specs/rfc7540.html#SETTINGS_MAX_HEADER_LIST_SIZE
  1392. // http://httpwg.org/specs/rfc7540.html#MaxHeaderBlock
  1393. func headerListSize(h http.Header) (size uint32) {
  1394. for k, vv := range h {
  1395. for _, v := range vv {
  1396. hf := hpack.HeaderField{Name: k, Value: v}
  1397. size += hf.Size()
  1398. }
  1399. }
  1400. return size
  1401. }
  1402. // padHeaders adds data to an http.Header until headerListSize(h) ==
  1403. // limit. Due to the way header list sizes are calculated, padHeaders
  1404. // cannot add fewer than len("Pad-Headers") + 32 bytes to h, and will
  1405. // call t.Fatal if asked to do so. PadHeaders first reserves enough
  1406. // space for an empty "Pad-Headers" key, then adds as many copies of
  1407. // filler as possible. Any remaining bytes necessary to push the
  1408. // header list size up to limit are added to h["Pad-Headers"].
  1409. func padHeaders(t *testing.T, h http.Header, limit uint64, filler string) {
  1410. if limit > 0xffffffff {
  1411. t.Fatalf("padHeaders: refusing to pad to more than 2^32-1 bytes. limit = %v", limit)
  1412. }
  1413. hf := hpack.HeaderField{Name: "Pad-Headers", Value: ""}
  1414. minPadding := uint64(hf.Size())
  1415. size := uint64(headerListSize(h))
  1416. minlimit := size + minPadding
  1417. if limit < minlimit {
  1418. t.Fatalf("padHeaders: limit %v < %v", limit, minlimit)
  1419. }
  1420. // Use a fixed-width format for name so that fieldSize
  1421. // remains constant.
  1422. nameFmt := "Pad-Headers-%06d"
  1423. hf = hpack.HeaderField{Name: fmt.Sprintf(nameFmt, 1), Value: filler}
  1424. fieldSize := uint64(hf.Size())
  1425. // Add as many complete filler values as possible, leaving
  1426. // room for at least one empty "Pad-Headers" key.
  1427. limit = limit - minPadding
  1428. for i := 0; size+fieldSize < limit; i++ {
  1429. name := fmt.Sprintf(nameFmt, i)
  1430. h.Add(name, filler)
  1431. size += fieldSize
  1432. }
  1433. // Add enough bytes to reach limit.
  1434. remain := limit - size
  1435. lastValue := strings.Repeat("*", int(remain))
  1436. h.Add("Pad-Headers", lastValue)
  1437. }
  1438. func TestPadHeaders(t *testing.T) {
  1439. check := func(h http.Header, limit uint32, fillerLen int) {
  1440. if h == nil {
  1441. h = make(http.Header)
  1442. }
  1443. filler := strings.Repeat("f", fillerLen)
  1444. padHeaders(t, h, uint64(limit), filler)
  1445. gotSize := headerListSize(h)
  1446. if gotSize != limit {
  1447. t.Errorf("Got size = %v; want %v", gotSize, limit)
  1448. }
  1449. }
  1450. // Try all possible combinations for small fillerLen and limit.
  1451. hf := hpack.HeaderField{Name: "Pad-Headers", Value: ""}
  1452. minLimit := hf.Size()
  1453. for limit := minLimit; limit <= 128; limit++ {
  1454. for fillerLen := 0; uint32(fillerLen) <= limit; fillerLen++ {
  1455. check(nil, limit, fillerLen)
  1456. }
  1457. }
  1458. // Try a few tests with larger limits, plus cumulative
  1459. // tests. Since these tests are cumulative, tests[i+1].limit
  1460. // must be >= tests[i].limit + minLimit. See the comment on
  1461. // padHeaders for more info on why the limit arg has this
  1462. // restriction.
  1463. tests := []struct {
  1464. fillerLen int
  1465. limit uint32
  1466. }{
  1467. {
  1468. fillerLen: 64,
  1469. limit: 1024,
  1470. },
  1471. {
  1472. fillerLen: 1024,
  1473. limit: 1286,
  1474. },
  1475. {
  1476. fillerLen: 256,
  1477. limit: 2048,
  1478. },
  1479. {
  1480. fillerLen: 1024,
  1481. limit: 10 * 1024,
  1482. },
  1483. {
  1484. fillerLen: 1023,
  1485. limit: 11 * 1024,
  1486. },
  1487. }
  1488. h := make(http.Header)
  1489. for _, tc := range tests {
  1490. check(nil, tc.limit, tc.fillerLen)
  1491. check(h, tc.limit, tc.fillerLen)
  1492. }
  1493. }
  1494. func TestTransportChecksRequestHeaderListSize(t *testing.T) {
  1495. st := newServerTester(t,
  1496. func(w http.ResponseWriter, r *http.Request) {
  1497. // Consume body & force client to send
  1498. // trailers before writing response.
  1499. // ioutil.ReadAll returns non-nil err for
  1500. // requests that attempt to send greater than
  1501. // maxHeaderListSize bytes of trailers, since
  1502. // those requests generate a stream reset.
  1503. ioutil.ReadAll(r.Body)
  1504. r.Body.Close()
  1505. },
  1506. func(ts *httptest.Server) {
  1507. ts.Config.MaxHeaderBytes = 16 << 10
  1508. },
  1509. optOnlyServer,
  1510. optQuiet,
  1511. )
  1512. defer st.Close()
  1513. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1514. defer tr.CloseIdleConnections()
  1515. checkRoundTrip := func(req *http.Request, wantErr error, desc string) {
  1516. res, err := tr.RoundTrip(req)
  1517. if err != wantErr {
  1518. if res != nil {
  1519. res.Body.Close()
  1520. }
  1521. t.Errorf("%v: RoundTrip err = %v; want %v", desc, err, wantErr)
  1522. return
  1523. }
  1524. if err == nil {
  1525. if res == nil {
  1526. t.Errorf("%v: response nil; want non-nil.", desc)
  1527. return
  1528. }
  1529. defer res.Body.Close()
  1530. if res.StatusCode != http.StatusOK {
  1531. t.Errorf("%v: response status = %v; want %v", desc, res.StatusCode, http.StatusOK)
  1532. }
  1533. return
  1534. }
  1535. if res != nil {
  1536. t.Errorf("%v: RoundTrip err = %v but response non-nil", desc, err)
  1537. }
  1538. }
  1539. headerListSizeForRequest := func(req *http.Request) (size uint64) {
  1540. contentLen := actualContentLength(req)
  1541. trailers, err := commaSeparatedTrailers(req)
  1542. if err != nil {
  1543. t.Fatalf("headerListSizeForRequest: %v", err)
  1544. }
  1545. cc := &ClientConn{peerMaxHeaderListSize: 0xffffffffffffffff}
  1546. cc.henc = hpack.NewEncoder(&cc.hbuf)
  1547. cc.mu.Lock()
  1548. hdrs, err := cc.encodeHeaders(req, true, trailers, contentLen)
  1549. cc.mu.Unlock()
  1550. if err != nil {
  1551. t.Fatalf("headerListSizeForRequest: %v", err)
  1552. }
  1553. hpackDec := hpack.NewDecoder(initialHeaderTableSize, func(hf hpack.HeaderField) {
  1554. size += uint64(hf.Size())
  1555. })
  1556. if len(hdrs) > 0 {
  1557. if _, err := hpackDec.Write(hdrs); err != nil {
  1558. t.Fatalf("headerListSizeForRequest: %v", err)
  1559. }
  1560. }
  1561. return size
  1562. }
  1563. // Create a new Request for each test, rather than reusing the
  1564. // same Request, to avoid a race when modifying req.Headers.
  1565. // See https://github.com/golang/go/issues/21316
  1566. newRequest := func() *http.Request {
  1567. // Body must be non-nil to enable writing trailers.
  1568. body := strings.NewReader("hello")
  1569. req, err := http.NewRequest("POST", st.ts.URL, body)
  1570. if err != nil {
  1571. t.Fatalf("newRequest: NewRequest: %v", err)
  1572. }
  1573. return req
  1574. }
  1575. // Make an arbitrary request to ensure we get the server's
  1576. // settings frame and initialize peerMaxHeaderListSize.
  1577. req := newRequest()
  1578. checkRoundTrip(req, nil, "Initial request")
  1579. // Get the ClientConn associated with the request and validate
  1580. // peerMaxHeaderListSize.
  1581. addr := authorityAddr(req.URL.Scheme, req.URL.Host)
  1582. cc, err := tr.connPool().GetClientConn(req, addr)
  1583. if err != nil {
  1584. t.Fatalf("GetClientConn: %v", err)
  1585. }
  1586. cc.mu.Lock()
  1587. peerSize := cc.peerMaxHeaderListSize
  1588. cc.mu.Unlock()
  1589. st.scMu.Lock()
  1590. wantSize := uint64(st.sc.maxHeaderListSize())
  1591. st.scMu.Unlock()
  1592. if peerSize != wantSize {
  1593. t.Errorf("peerMaxHeaderListSize = %v; want %v", peerSize, wantSize)
  1594. }
  1595. // Sanity check peerSize. (*serverConn) maxHeaderListSize adds
  1596. // 320 bytes of padding.
  1597. wantHeaderBytes := uint64(st.ts.Config.MaxHeaderBytes) + 320
  1598. if peerSize != wantHeaderBytes {
  1599. t.Errorf("peerMaxHeaderListSize = %v; want %v.", peerSize, wantHeaderBytes)
  1600. }
  1601. // Pad headers & trailers, but stay under peerSize.
  1602. req = newRequest()
  1603. req.Header = make(http.Header)
  1604. req.Trailer = make(http.Header)
  1605. filler := strings.Repeat("*", 1024)
  1606. padHeaders(t, req.Trailer, peerSize, filler)
  1607. // cc.encodeHeaders adds some default headers to the request,
  1608. // so we need to leave room for those.
  1609. defaultBytes := headerListSizeForRequest(req)
  1610. padHeaders(t, req.Header, peerSize-defaultBytes, filler)
  1611. checkRoundTrip(req, nil, "Headers & Trailers under limit")
  1612. // Add enough header bytes to push us over peerSize.
  1613. req = newRequest()
  1614. req.Header = make(http.Header)
  1615. padHeaders(t, req.Header, peerSize, filler)
  1616. checkRoundTrip(req, errRequestHeaderListSize, "Headers over limit")
  1617. // Push trailers over the limit.
  1618. req = newRequest()
  1619. req.Trailer = make(http.Header)
  1620. padHeaders(t, req.Trailer, peerSize+1, filler)
  1621. checkRoundTrip(req, errRequestHeaderListSize, "Trailers over limit")
  1622. // Send headers with a single large value.
  1623. req = newRequest()
  1624. filler = strings.Repeat("*", int(peerSize))
  1625. req.Header = make(http.Header)
  1626. req.Header.Set("Big", filler)
  1627. checkRoundTrip(req, errRequestHeaderListSize, "Single large header")
  1628. // Send trailers with a single large value.
  1629. req = newRequest()
  1630. req.Trailer = make(http.Header)
  1631. req.Trailer.Set("Big", filler)
  1632. checkRoundTrip(req, errRequestHeaderListSize, "Single large trailer")
  1633. }
  1634. func TestTransportChecksResponseHeaderListSize(t *testing.T) {
  1635. ct := newClientTester(t)
  1636. ct.client = func() error {
  1637. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1638. res, err := ct.tr.RoundTrip(req)
  1639. if err != errResponseHeaderListSize {
  1640. if res != nil {
  1641. res.Body.Close()
  1642. }
  1643. size := int64(0)
  1644. for k, vv := range res.Header {
  1645. for _, v := range vv {
  1646. size += int64(len(k)) + int64(len(v)) + 32
  1647. }
  1648. }
  1649. return fmt.Errorf("RoundTrip Error = %v (and %d bytes of response headers); want errResponseHeaderListSize", err, size)
  1650. }
  1651. return nil
  1652. }
  1653. ct.server = func() error {
  1654. ct.greet()
  1655. var buf bytes.Buffer
  1656. enc := hpack.NewEncoder(&buf)
  1657. for {
  1658. f, err := ct.fr.ReadFrame()
  1659. if err != nil {
  1660. return err
  1661. }
  1662. switch f := f.(type) {
  1663. case *HeadersFrame:
  1664. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1665. large := strings.Repeat("a", 1<<10)
  1666. for i := 0; i < 5042; i++ {
  1667. enc.WriteField(hpack.HeaderField{Name: large, Value: large})
  1668. }
  1669. if size, want := buf.Len(), 6329; size != want {
  1670. // Note: this number might change if
  1671. // our hpack implementation
  1672. // changes. That's fine. This is
  1673. // just a sanity check that our
  1674. // response can fit in a single
  1675. // header block fragment frame.
  1676. return fmt.Errorf("encoding over 10MB of duplicate keypairs took %d bytes; expected %d", size, want)
  1677. }
  1678. ct.fr.WriteHeaders(HeadersFrameParam{
  1679. StreamID: f.StreamID,
  1680. EndHeaders: true,
  1681. EndStream: true,
  1682. BlockFragment: buf.Bytes(),
  1683. })
  1684. return nil
  1685. }
  1686. }
  1687. }
  1688. ct.run()
  1689. }
  1690. func TestTransportCookieHeaderSplit(t *testing.T) {
  1691. ct := newClientTester(t)
  1692. ct.client = func() error {
  1693. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  1694. req.Header.Add("Cookie", "a=b;c=d; e=f;")
  1695. req.Header.Add("Cookie", "e=f;g=h; ")
  1696. req.Header.Add("Cookie", "i=j")
  1697. _, err := ct.tr.RoundTrip(req)
  1698. return err
  1699. }
  1700. ct.server = func() error {
  1701. ct.greet()
  1702. for {
  1703. f, err := ct.fr.ReadFrame()
  1704. if err != nil {
  1705. return err
  1706. }
  1707. switch f := f.(type) {
  1708. case *HeadersFrame:
  1709. dec := hpack.NewDecoder(initialHeaderTableSize, nil)
  1710. hfs, err := dec.DecodeFull(f.HeaderBlockFragment())
  1711. if err != nil {
  1712. return err
  1713. }
  1714. got := []string{}
  1715. want := []string{"a=b", "c=d", "e=f", "e=f", "g=h", "i=j"}
  1716. for _, hf := range hfs {
  1717. if hf.Name == "cookie" {
  1718. got = append(got, hf.Value)
  1719. }
  1720. }
  1721. if !reflect.DeepEqual(got, want) {
  1722. t.Errorf("Cookies = %#v, want %#v", got, want)
  1723. }
  1724. var buf bytes.Buffer
  1725. enc := hpack.NewEncoder(&buf)
  1726. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  1727. ct.fr.WriteHeaders(HeadersFrameParam{
  1728. StreamID: f.StreamID,
  1729. EndHeaders: true,
  1730. EndStream: true,
  1731. BlockFragment: buf.Bytes(),
  1732. })
  1733. return nil
  1734. }
  1735. }
  1736. }
  1737. ct.run()
  1738. }
  1739. // Test that the Transport returns a typed error from Response.Body.Read calls
  1740. // when the server sends an error. (here we use a panic, since that should generate
  1741. // a stream error, but others like cancel should be similar)
  1742. func TestTransportBodyReadErrorType(t *testing.T) {
  1743. doPanic := make(chan bool, 1)
  1744. st := newServerTester(t,
  1745. func(w http.ResponseWriter, r *http.Request) {
  1746. w.(http.Flusher).Flush() // force headers out
  1747. <-doPanic
  1748. panic("boom")
  1749. },
  1750. optOnlyServer,
  1751. optQuiet,
  1752. )
  1753. defer st.Close()
  1754. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  1755. defer tr.CloseIdleConnections()
  1756. c := &http.Client{Transport: tr}
  1757. res, err := c.Get(st.ts.URL)
  1758. if err != nil {
  1759. t.Fatal(err)
  1760. }
  1761. defer res.Body.Close()
  1762. doPanic <- true
  1763. buf := make([]byte, 100)
  1764. n, err := res.Body.Read(buf)
  1765. want := StreamError{StreamID: 0x1, Code: 0x2}
  1766. if !reflect.DeepEqual(want, err) {
  1767. t.Errorf("Read = %v, %#v; want error %#v", n, err, want)
  1768. }
  1769. }
  1770. // golang.org/issue/13924
  1771. // This used to fail after many iterations, especially with -race:
  1772. // go test -v -run=TestTransportDoubleCloseOnWriteError -count=500 -race
  1773. func TestTransportDoubleCloseOnWriteError(t *testing.T) {
  1774. var (
  1775. mu sync.Mutex
  1776. conn net.Conn // to close if set
  1777. )
  1778. st := newServerTester(t,
  1779. func(w http.ResponseWriter, r *http.Request) {
  1780. mu.Lock()
  1781. defer mu.Unlock()
  1782. if conn != nil {
  1783. conn.Close()
  1784. }
  1785. },
  1786. optOnlyServer,
  1787. )
  1788. defer st.Close()
  1789. tr := &Transport{
  1790. TLSClientConfig: tlsConfigInsecure,
  1791. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1792. tc, err := tls.Dial(network, addr, cfg)
  1793. if err != nil {
  1794. return nil, err
  1795. }
  1796. mu.Lock()
  1797. defer mu.Unlock()
  1798. conn = tc
  1799. return tc, nil
  1800. },
  1801. }
  1802. defer tr.CloseIdleConnections()
  1803. c := &http.Client{Transport: tr}
  1804. c.Get(st.ts.URL)
  1805. }
  1806. // Test that the http1 Transport.DisableKeepAlives option is respected
  1807. // and connections are closed as soon as idle.
  1808. // See golang.org/issue/14008
  1809. func TestTransportDisableKeepAlives(t *testing.T) {
  1810. st := newServerTester(t,
  1811. func(w http.ResponseWriter, r *http.Request) {
  1812. io.WriteString(w, "hi")
  1813. },
  1814. optOnlyServer,
  1815. )
  1816. defer st.Close()
  1817. connClosed := make(chan struct{}) // closed on tls.Conn.Close
  1818. tr := &Transport{
  1819. t1: &http.Transport{
  1820. DisableKeepAlives: true,
  1821. },
  1822. TLSClientConfig: tlsConfigInsecure,
  1823. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1824. tc, err := tls.Dial(network, addr, cfg)
  1825. if err != nil {
  1826. return nil, err
  1827. }
  1828. return &noteCloseConn{Conn: tc, closefn: func() { close(connClosed) }}, nil
  1829. },
  1830. }
  1831. c := &http.Client{Transport: tr}
  1832. res, err := c.Get(st.ts.URL)
  1833. if err != nil {
  1834. t.Fatal(err)
  1835. }
  1836. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1837. t.Fatal(err)
  1838. }
  1839. defer res.Body.Close()
  1840. select {
  1841. case <-connClosed:
  1842. case <-time.After(1 * time.Second):
  1843. t.Errorf("timeout")
  1844. }
  1845. }
  1846. // Test concurrent requests with Transport.DisableKeepAlives. We can share connections,
  1847. // but when things are totally idle, it still needs to close.
  1848. func TestTransportDisableKeepAlives_Concurrency(t *testing.T) {
  1849. const D = 25 * time.Millisecond
  1850. st := newServerTester(t,
  1851. func(w http.ResponseWriter, r *http.Request) {
  1852. time.Sleep(D)
  1853. io.WriteString(w, "hi")
  1854. },
  1855. optOnlyServer,
  1856. )
  1857. defer st.Close()
  1858. var dials int32
  1859. var conns sync.WaitGroup
  1860. tr := &Transport{
  1861. t1: &http.Transport{
  1862. DisableKeepAlives: true,
  1863. },
  1864. TLSClientConfig: tlsConfigInsecure,
  1865. DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  1866. tc, err := tls.Dial(network, addr, cfg)
  1867. if err != nil {
  1868. return nil, err
  1869. }
  1870. atomic.AddInt32(&dials, 1)
  1871. conns.Add(1)
  1872. return &noteCloseConn{Conn: tc, closefn: func() { conns.Done() }}, nil
  1873. },
  1874. }
  1875. c := &http.Client{Transport: tr}
  1876. var reqs sync.WaitGroup
  1877. const N = 20
  1878. for i := 0; i < N; i++ {
  1879. reqs.Add(1)
  1880. if i == N-1 {
  1881. // For the final request, try to make all the
  1882. // others close. This isn't verified in the
  1883. // count, other than the Log statement, since
  1884. // it's so timing dependent. This test is
  1885. // really to make sure we don't interrupt a
  1886. // valid request.
  1887. time.Sleep(D * 2)
  1888. }
  1889. go func() {
  1890. defer reqs.Done()
  1891. res, err := c.Get(st.ts.URL)
  1892. if err != nil {
  1893. t.Error(err)
  1894. return
  1895. }
  1896. if _, err := ioutil.ReadAll(res.Body); err != nil {
  1897. t.Error(err)
  1898. return
  1899. }
  1900. res.Body.Close()
  1901. }()
  1902. }
  1903. reqs.Wait()
  1904. conns.Wait()
  1905. t.Logf("did %d dials, %d requests", atomic.LoadInt32(&dials), N)
  1906. }
  1907. type noteCloseConn struct {
  1908. net.Conn
  1909. onceClose sync.Once
  1910. closefn func()
  1911. }
  1912. func (c *noteCloseConn) Close() error {
  1913. c.onceClose.Do(c.closefn)
  1914. return c.Conn.Close()
  1915. }
  1916. func isTimeout(err error) bool {
  1917. switch err := err.(type) {
  1918. case nil:
  1919. return false
  1920. case *url.Error:
  1921. return isTimeout(err.Err)
  1922. case net.Error:
  1923. return err.Timeout()
  1924. }
  1925. return false
  1926. }
  1927. // Test that the http1 Transport.ResponseHeaderTimeout option and cancel is sent.
  1928. func TestTransportResponseHeaderTimeout_NoBody(t *testing.T) {
  1929. testTransportResponseHeaderTimeout(t, false)
  1930. }
  1931. func TestTransportResponseHeaderTimeout_Body(t *testing.T) {
  1932. testTransportResponseHeaderTimeout(t, true)
  1933. }
  1934. func testTransportResponseHeaderTimeout(t *testing.T, body bool) {
  1935. ct := newClientTester(t)
  1936. ct.tr.t1 = &http.Transport{
  1937. ResponseHeaderTimeout: 5 * time.Millisecond,
  1938. }
  1939. ct.client = func() error {
  1940. c := &http.Client{Transport: ct.tr}
  1941. var err error
  1942. var n int64
  1943. const bodySize = 4 << 20
  1944. if body {
  1945. _, err = c.Post("https://dummy.tld/", "text/foo", io.LimitReader(countingReader{&n}, bodySize))
  1946. } else {
  1947. _, err = c.Get("https://dummy.tld/")
  1948. }
  1949. if !isTimeout(err) {
  1950. t.Errorf("client expected timeout error; got %#v", err)
  1951. }
  1952. if body && n != bodySize {
  1953. t.Errorf("only read %d bytes of body; want %d", n, bodySize)
  1954. }
  1955. return nil
  1956. }
  1957. ct.server = func() error {
  1958. ct.greet()
  1959. for {
  1960. f, err := ct.fr.ReadFrame()
  1961. if err != nil {
  1962. t.Logf("ReadFrame: %v", err)
  1963. return nil
  1964. }
  1965. switch f := f.(type) {
  1966. case *DataFrame:
  1967. dataLen := len(f.Data())
  1968. if dataLen > 0 {
  1969. if err := ct.fr.WriteWindowUpdate(0, uint32(dataLen)); err != nil {
  1970. return err
  1971. }
  1972. if err := ct.fr.WriteWindowUpdate(f.StreamID, uint32(dataLen)); err != nil {
  1973. return err
  1974. }
  1975. }
  1976. case *RSTStreamFrame:
  1977. if f.StreamID == 1 && f.ErrCode == ErrCodeCancel {
  1978. return nil
  1979. }
  1980. }
  1981. }
  1982. }
  1983. ct.run()
  1984. }
  1985. func TestTransportDisableCompression(t *testing.T) {
  1986. const body = "sup"
  1987. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  1988. want := http.Header{
  1989. "User-Agent": []string{"Go-http-client/2.0"},
  1990. }
  1991. if !reflect.DeepEqual(r.Header, want) {
  1992. t.Errorf("request headers = %v; want %v", r.Header, want)
  1993. }
  1994. }, optOnlyServer)
  1995. defer st.Close()
  1996. tr := &Transport{
  1997. TLSClientConfig: tlsConfigInsecure,
  1998. t1: &http.Transport{
  1999. DisableCompression: true,
  2000. },
  2001. }
  2002. defer tr.CloseIdleConnections()
  2003. req, err := http.NewRequest("GET", st.ts.URL, nil)
  2004. if err != nil {
  2005. t.Fatal(err)
  2006. }
  2007. res, err := tr.RoundTrip(req)
  2008. if err != nil {
  2009. t.Fatal(err)
  2010. }
  2011. defer res.Body.Close()
  2012. }
  2013. // RFC 7540 section 8.1.2.2
  2014. func TestTransportRejectsConnHeaders(t *testing.T) {
  2015. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2016. var got []string
  2017. for k := range r.Header {
  2018. got = append(got, k)
  2019. }
  2020. sort.Strings(got)
  2021. w.Header().Set("Got-Header", strings.Join(got, ","))
  2022. }, optOnlyServer)
  2023. defer st.Close()
  2024. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2025. defer tr.CloseIdleConnections()
  2026. tests := []struct {
  2027. key string
  2028. value []string
  2029. want string
  2030. }{
  2031. {
  2032. key: "Upgrade",
  2033. value: []string{"anything"},
  2034. want: "ERROR: http2: invalid Upgrade request header: [\"anything\"]",
  2035. },
  2036. {
  2037. key: "Connection",
  2038. value: []string{"foo"},
  2039. want: "ERROR: http2: invalid Connection request header: [\"foo\"]",
  2040. },
  2041. {
  2042. key: "Connection",
  2043. value: []string{"close"},
  2044. want: "Accept-Encoding,User-Agent",
  2045. },
  2046. {
  2047. key: "Connection",
  2048. value: []string{"CLoSe"},
  2049. want: "Accept-Encoding,User-Agent",
  2050. },
  2051. {
  2052. key: "Connection",
  2053. value: []string{"close", "something-else"},
  2054. want: "ERROR: http2: invalid Connection request header: [\"close\" \"something-else\"]",
  2055. },
  2056. {
  2057. key: "Connection",
  2058. value: []string{"keep-alive"},
  2059. want: "Accept-Encoding,User-Agent",
  2060. },
  2061. {
  2062. key: "Connection",
  2063. value: []string{"Keep-ALIVE"},
  2064. want: "Accept-Encoding,User-Agent",
  2065. },
  2066. {
  2067. key: "Proxy-Connection", // just deleted and ignored
  2068. value: []string{"keep-alive"},
  2069. want: "Accept-Encoding,User-Agent",
  2070. },
  2071. {
  2072. key: "Transfer-Encoding",
  2073. value: []string{""},
  2074. want: "Accept-Encoding,User-Agent",
  2075. },
  2076. {
  2077. key: "Transfer-Encoding",
  2078. value: []string{"foo"},
  2079. want: "ERROR: http2: invalid Transfer-Encoding request header: [\"foo\"]",
  2080. },
  2081. {
  2082. key: "Transfer-Encoding",
  2083. value: []string{"chunked"},
  2084. want: "Accept-Encoding,User-Agent",
  2085. },
  2086. {
  2087. key: "Transfer-Encoding",
  2088. value: []string{"chunked", "other"},
  2089. want: "ERROR: http2: invalid Transfer-Encoding request header: [\"chunked\" \"other\"]",
  2090. },
  2091. {
  2092. key: "Content-Length",
  2093. value: []string{"123"},
  2094. want: "Accept-Encoding,User-Agent",
  2095. },
  2096. {
  2097. key: "Keep-Alive",
  2098. value: []string{"doop"},
  2099. want: "Accept-Encoding,User-Agent",
  2100. },
  2101. }
  2102. for _, tt := range tests {
  2103. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2104. req.Header[tt.key] = tt.value
  2105. res, err := tr.RoundTrip(req)
  2106. var got string
  2107. if err != nil {
  2108. got = fmt.Sprintf("ERROR: %v", err)
  2109. } else {
  2110. got = res.Header.Get("Got-Header")
  2111. res.Body.Close()
  2112. }
  2113. if got != tt.want {
  2114. t.Errorf("For key %q, value %q, got = %q; want %q", tt.key, tt.value, got, tt.want)
  2115. }
  2116. }
  2117. }
  2118. // Reject content-length headers containing a sign.
  2119. // See https://golang.org/issue/39017
  2120. func TestTransportRejectsContentLengthWithSign(t *testing.T) {
  2121. tests := []struct {
  2122. name string
  2123. cl []string
  2124. wantCL string
  2125. }{
  2126. {
  2127. name: "proper content-length",
  2128. cl: []string{"3"},
  2129. wantCL: "3",
  2130. },
  2131. {
  2132. name: "ignore cl with plus sign",
  2133. cl: []string{"+3"},
  2134. wantCL: "",
  2135. },
  2136. {
  2137. name: "ignore cl with minus sign",
  2138. cl: []string{"-3"},
  2139. wantCL: "",
  2140. },
  2141. {
  2142. name: "max int64, for safe uint64->int64 conversion",
  2143. cl: []string{"9223372036854775807"},
  2144. wantCL: "9223372036854775807",
  2145. },
  2146. {
  2147. name: "overflows int64, so ignored",
  2148. cl: []string{"9223372036854775808"},
  2149. wantCL: "",
  2150. },
  2151. }
  2152. for _, tt := range tests {
  2153. tt := tt
  2154. t.Run(tt.name, func(t *testing.T) {
  2155. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2156. w.Header().Set("Content-Length", tt.cl[0])
  2157. }, optOnlyServer)
  2158. defer st.Close()
  2159. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2160. defer tr.CloseIdleConnections()
  2161. req, _ := http.NewRequest("HEAD", st.ts.URL, nil)
  2162. res, err := tr.RoundTrip(req)
  2163. var got string
  2164. if err != nil {
  2165. got = fmt.Sprintf("ERROR: %v", err)
  2166. } else {
  2167. got = res.Header.Get("Content-Length")
  2168. res.Body.Close()
  2169. }
  2170. if got != tt.wantCL {
  2171. t.Fatalf("Got: %q\nWant: %q", got, tt.wantCL)
  2172. }
  2173. })
  2174. }
  2175. }
  2176. // golang.org/issue/14048
  2177. func TestTransportFailsOnInvalidHeaders(t *testing.T) {
  2178. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2179. var got []string
  2180. for k := range r.Header {
  2181. got = append(got, k)
  2182. }
  2183. sort.Strings(got)
  2184. w.Header().Set("Got-Header", strings.Join(got, ","))
  2185. }, optOnlyServer)
  2186. defer st.Close()
  2187. tests := [...]struct {
  2188. h http.Header
  2189. wantErr string
  2190. }{
  2191. 0: {
  2192. h: http.Header{"with space": {"foo"}},
  2193. wantErr: `invalid HTTP header name "with space"`,
  2194. },
  2195. 1: {
  2196. h: http.Header{"name": {"Брэд"}},
  2197. wantErr: "", // okay
  2198. },
  2199. 2: {
  2200. h: http.Header{"имя": {"Brad"}},
  2201. wantErr: `invalid HTTP header name "имя"`,
  2202. },
  2203. 3: {
  2204. h: http.Header{"foo": {"foo\x01bar"}},
  2205. wantErr: `invalid HTTP header value "foo\x01bar" for header "foo"`,
  2206. },
  2207. }
  2208. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2209. defer tr.CloseIdleConnections()
  2210. for i, tt := range tests {
  2211. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  2212. req.Header = tt.h
  2213. res, err := tr.RoundTrip(req)
  2214. var bad bool
  2215. if tt.wantErr == "" {
  2216. if err != nil {
  2217. bad = true
  2218. t.Errorf("case %d: error = %v; want no error", i, err)
  2219. }
  2220. } else {
  2221. if !strings.Contains(fmt.Sprint(err), tt.wantErr) {
  2222. bad = true
  2223. t.Errorf("case %d: error = %v; want error %q", i, err, tt.wantErr)
  2224. }
  2225. }
  2226. if err == nil {
  2227. if bad {
  2228. t.Logf("case %d: server got headers %q", i, res.Header.Get("Got-Header"))
  2229. }
  2230. res.Body.Close()
  2231. }
  2232. }
  2233. }
  2234. // Tests that gzipReader doesn't crash on a second Read call following
  2235. // the first Read call's gzip.NewReader returning an error.
  2236. func TestGzipReader_DoubleReadCrash(t *testing.T) {
  2237. gz := &gzipReader{
  2238. body: ioutil.NopCloser(strings.NewReader("0123456789")),
  2239. }
  2240. var buf [1]byte
  2241. n, err1 := gz.Read(buf[:])
  2242. if n != 0 || !strings.Contains(fmt.Sprint(err1), "invalid header") {
  2243. t.Fatalf("Read = %v, %v; want 0, invalid header", n, err1)
  2244. }
  2245. n, err2 := gz.Read(buf[:])
  2246. if n != 0 || err2 != err1 {
  2247. t.Fatalf("second Read = %v, %v; want 0, %v", n, err2, err1)
  2248. }
  2249. }
  2250. func TestTransportNewTLSConfig(t *testing.T) {
  2251. tests := [...]struct {
  2252. conf *tls.Config
  2253. host string
  2254. want *tls.Config
  2255. }{
  2256. // Normal case.
  2257. 0: {
  2258. conf: nil,
  2259. host: "foo.com",
  2260. want: &tls.Config{
  2261. ServerName: "foo.com",
  2262. NextProtos: []string{NextProtoTLS},
  2263. },
  2264. },
  2265. // User-provided name (bar.com) takes precedence:
  2266. 1: {
  2267. conf: &tls.Config{
  2268. ServerName: "bar.com",
  2269. },
  2270. host: "foo.com",
  2271. want: &tls.Config{
  2272. ServerName: "bar.com",
  2273. NextProtos: []string{NextProtoTLS},
  2274. },
  2275. },
  2276. // NextProto is prepended:
  2277. 2: {
  2278. conf: &tls.Config{
  2279. NextProtos: []string{"foo", "bar"},
  2280. },
  2281. host: "example.com",
  2282. want: &tls.Config{
  2283. ServerName: "example.com",
  2284. NextProtos: []string{NextProtoTLS, "foo", "bar"},
  2285. },
  2286. },
  2287. // NextProto is not duplicated:
  2288. 3: {
  2289. conf: &tls.Config{
  2290. NextProtos: []string{"foo", "bar", NextProtoTLS},
  2291. },
  2292. host: "example.com",
  2293. want: &tls.Config{
  2294. ServerName: "example.com",
  2295. NextProtos: []string{"foo", "bar", NextProtoTLS},
  2296. },
  2297. },
  2298. }
  2299. for i, tt := range tests {
  2300. // Ignore the session ticket keys part, which ends up populating
  2301. // unexported fields in the Config:
  2302. if tt.conf != nil {
  2303. tt.conf.SessionTicketsDisabled = true
  2304. }
  2305. tr := &Transport{TLSClientConfig: tt.conf}
  2306. got := tr.newTLSConfig(tt.host)
  2307. got.SessionTicketsDisabled = false
  2308. if !reflect.DeepEqual(got, tt.want) {
  2309. t.Errorf("%d. got %#v; want %#v", i, got, tt.want)
  2310. }
  2311. }
  2312. }
  2313. // The Google GFE responds to HEAD requests with a HEADERS frame
  2314. // without END_STREAM, followed by a 0-length DATA frame with
  2315. // END_STREAM. Make sure we don't get confused by that. (We did.)
  2316. func TestTransportReadHeadResponse(t *testing.T) {
  2317. ct := newClientTester(t)
  2318. clientDone := make(chan struct{})
  2319. ct.client = func() error {
  2320. defer close(clientDone)
  2321. req, _ := http.NewRequest("HEAD", "https://dummy.tld/", nil)
  2322. res, err := ct.tr.RoundTrip(req)
  2323. if err != nil {
  2324. return err
  2325. }
  2326. if res.ContentLength != 123 {
  2327. return fmt.Errorf("Content-Length = %d; want 123", res.ContentLength)
  2328. }
  2329. slurp, err := ioutil.ReadAll(res.Body)
  2330. if err != nil {
  2331. return fmt.Errorf("ReadAll: %v", err)
  2332. }
  2333. if len(slurp) > 0 {
  2334. return fmt.Errorf("Unexpected non-empty ReadAll body: %q", slurp)
  2335. }
  2336. return nil
  2337. }
  2338. ct.server = func() error {
  2339. ct.greet()
  2340. for {
  2341. f, err := ct.fr.ReadFrame()
  2342. if err != nil {
  2343. t.Logf("ReadFrame: %v", err)
  2344. return nil
  2345. }
  2346. hf, ok := f.(*HeadersFrame)
  2347. if !ok {
  2348. continue
  2349. }
  2350. var buf bytes.Buffer
  2351. enc := hpack.NewEncoder(&buf)
  2352. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2353. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  2354. ct.fr.WriteHeaders(HeadersFrameParam{
  2355. StreamID: hf.StreamID,
  2356. EndHeaders: true,
  2357. EndStream: false, // as the GFE does
  2358. BlockFragment: buf.Bytes(),
  2359. })
  2360. ct.fr.WriteData(hf.StreamID, true, nil)
  2361. <-clientDone
  2362. return nil
  2363. }
  2364. }
  2365. ct.run()
  2366. }
  2367. func TestTransportReadHeadResponseWithBody(t *testing.T) {
  2368. // This test use not valid response format.
  2369. // Discarding logger output to not spam tests output.
  2370. log.SetOutput(ioutil.Discard)
  2371. defer log.SetOutput(os.Stderr)
  2372. response := "redirecting to /elsewhere"
  2373. ct := newClientTester(t)
  2374. clientDone := make(chan struct{})
  2375. ct.client = func() error {
  2376. defer close(clientDone)
  2377. req, _ := http.NewRequest("HEAD", "https://dummy.tld/", nil)
  2378. res, err := ct.tr.RoundTrip(req)
  2379. if err != nil {
  2380. return err
  2381. }
  2382. if res.ContentLength != int64(len(response)) {
  2383. return fmt.Errorf("Content-Length = %d; want %d", res.ContentLength, len(response))
  2384. }
  2385. slurp, err := ioutil.ReadAll(res.Body)
  2386. if err != nil {
  2387. return fmt.Errorf("ReadAll: %v", err)
  2388. }
  2389. if len(slurp) > 0 {
  2390. return fmt.Errorf("Unexpected non-empty ReadAll body: %q", slurp)
  2391. }
  2392. return nil
  2393. }
  2394. ct.server = func() error {
  2395. ct.greet()
  2396. for {
  2397. f, err := ct.fr.ReadFrame()
  2398. if err != nil {
  2399. t.Logf("ReadFrame: %v", err)
  2400. return nil
  2401. }
  2402. hf, ok := f.(*HeadersFrame)
  2403. if !ok {
  2404. continue
  2405. }
  2406. var buf bytes.Buffer
  2407. enc := hpack.NewEncoder(&buf)
  2408. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2409. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: strconv.Itoa(len(response))})
  2410. ct.fr.WriteHeaders(HeadersFrameParam{
  2411. StreamID: hf.StreamID,
  2412. EndHeaders: true,
  2413. EndStream: false,
  2414. BlockFragment: buf.Bytes(),
  2415. })
  2416. ct.fr.WriteData(hf.StreamID, true, []byte(response))
  2417. <-clientDone
  2418. return nil
  2419. }
  2420. }
  2421. ct.run()
  2422. }
  2423. type neverEnding byte
  2424. func (b neverEnding) Read(p []byte) (int, error) {
  2425. for i := range p {
  2426. p[i] = byte(b)
  2427. }
  2428. return len(p), nil
  2429. }
  2430. // golang.org/issue/15425: test that a handler closing the request
  2431. // body doesn't terminate the stream to the peer. (It just stops
  2432. // readability from the handler's side, and eventually the client
  2433. // runs out of flow control tokens)
  2434. func TestTransportHandlerBodyClose(t *testing.T) {
  2435. const bodySize = 10 << 20
  2436. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2437. r.Body.Close()
  2438. io.Copy(w, io.LimitReader(neverEnding('A'), bodySize))
  2439. }, optOnlyServer)
  2440. defer st.Close()
  2441. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2442. defer tr.CloseIdleConnections()
  2443. g0 := runtime.NumGoroutine()
  2444. const numReq = 10
  2445. for i := 0; i < numReq; i++ {
  2446. req, err := http.NewRequest("POST", st.ts.URL, struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  2447. if err != nil {
  2448. t.Fatal(err)
  2449. }
  2450. res, err := tr.RoundTrip(req)
  2451. if err != nil {
  2452. t.Fatal(err)
  2453. }
  2454. n, err := io.Copy(ioutil.Discard, res.Body)
  2455. res.Body.Close()
  2456. if n != bodySize || err != nil {
  2457. t.Fatalf("req#%d: Copy = %d, %v; want %d, nil", i, n, err, bodySize)
  2458. }
  2459. }
  2460. tr.CloseIdleConnections()
  2461. if !waitCondition(5*time.Second, 100*time.Millisecond, func() bool {
  2462. gd := runtime.NumGoroutine() - g0
  2463. return gd < numReq/2
  2464. }) {
  2465. t.Errorf("appeared to leak goroutines")
  2466. }
  2467. }
  2468. // https://golang.org/issue/15930
  2469. func TestTransportFlowControl(t *testing.T) {
  2470. const bufLen = 64 << 10
  2471. var total int64 = 100 << 20 // 100MB
  2472. if testing.Short() {
  2473. total = 10 << 20
  2474. }
  2475. var wrote int64 // updated atomically
  2476. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2477. b := make([]byte, bufLen)
  2478. for wrote < total {
  2479. n, err := w.Write(b)
  2480. atomic.AddInt64(&wrote, int64(n))
  2481. if err != nil {
  2482. t.Errorf("ResponseWriter.Write error: %v", err)
  2483. break
  2484. }
  2485. w.(http.Flusher).Flush()
  2486. }
  2487. }, optOnlyServer)
  2488. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2489. defer tr.CloseIdleConnections()
  2490. req, err := http.NewRequest("GET", st.ts.URL, nil)
  2491. if err != nil {
  2492. t.Fatal("NewRequest error:", err)
  2493. }
  2494. resp, err := tr.RoundTrip(req)
  2495. if err != nil {
  2496. t.Fatal("RoundTrip error:", err)
  2497. }
  2498. defer resp.Body.Close()
  2499. var read int64
  2500. b := make([]byte, bufLen)
  2501. for {
  2502. n, err := resp.Body.Read(b)
  2503. if err == io.EOF {
  2504. break
  2505. }
  2506. if err != nil {
  2507. t.Fatal("Read error:", err)
  2508. }
  2509. read += int64(n)
  2510. const max = transportDefaultStreamFlow
  2511. if w := atomic.LoadInt64(&wrote); -max > read-w || read-w > max {
  2512. t.Fatalf("Too much data inflight: server wrote %v bytes but client only received %v", w, read)
  2513. }
  2514. // Let the server get ahead of the client.
  2515. time.Sleep(1 * time.Millisecond)
  2516. }
  2517. }
  2518. // golang.org/issue/14627 -- if the server sends a GOAWAY frame, make
  2519. // the Transport remember it and return it back to users (via
  2520. // RoundTrip or request body reads) if needed (e.g. if the server
  2521. // proceeds to close the TCP connection before the client gets its
  2522. // response)
  2523. func TestTransportUsesGoAwayDebugError_RoundTrip(t *testing.T) {
  2524. testTransportUsesGoAwayDebugError(t, false)
  2525. }
  2526. func TestTransportUsesGoAwayDebugError_Body(t *testing.T) {
  2527. testTransportUsesGoAwayDebugError(t, true)
  2528. }
  2529. func testTransportUsesGoAwayDebugError(t *testing.T, failMidBody bool) {
  2530. ct := newClientTester(t)
  2531. clientDone := make(chan struct{})
  2532. const goAwayErrCode = ErrCodeHTTP11Required // arbitrary
  2533. const goAwayDebugData = "some debug data"
  2534. ct.client = func() error {
  2535. defer close(clientDone)
  2536. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2537. res, err := ct.tr.RoundTrip(req)
  2538. if failMidBody {
  2539. if err != nil {
  2540. return fmt.Errorf("unexpected client RoundTrip error: %v", err)
  2541. }
  2542. _, err = io.Copy(ioutil.Discard, res.Body)
  2543. res.Body.Close()
  2544. }
  2545. want := GoAwayError{
  2546. LastStreamID: 5,
  2547. ErrCode: goAwayErrCode,
  2548. DebugData: goAwayDebugData,
  2549. }
  2550. if !reflect.DeepEqual(err, want) {
  2551. t.Errorf("RoundTrip error = %T: %#v, want %T (%#v)", err, err, want, want)
  2552. }
  2553. return nil
  2554. }
  2555. ct.server = func() error {
  2556. ct.greet()
  2557. for {
  2558. f, err := ct.fr.ReadFrame()
  2559. if err != nil {
  2560. t.Logf("ReadFrame: %v", err)
  2561. return nil
  2562. }
  2563. hf, ok := f.(*HeadersFrame)
  2564. if !ok {
  2565. continue
  2566. }
  2567. if failMidBody {
  2568. var buf bytes.Buffer
  2569. enc := hpack.NewEncoder(&buf)
  2570. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2571. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "123"})
  2572. ct.fr.WriteHeaders(HeadersFrameParam{
  2573. StreamID: hf.StreamID,
  2574. EndHeaders: true,
  2575. EndStream: false,
  2576. BlockFragment: buf.Bytes(),
  2577. })
  2578. }
  2579. // Write two GOAWAY frames, to test that the Transport takes
  2580. // the interesting parts of both.
  2581. ct.fr.WriteGoAway(5, ErrCodeNo, []byte(goAwayDebugData))
  2582. ct.fr.WriteGoAway(5, goAwayErrCode, nil)
  2583. ct.sc.(*net.TCPConn).CloseWrite()
  2584. if runtime.GOOS == "plan9" {
  2585. // CloseWrite not supported on Plan 9; Issue 17906
  2586. ct.sc.(*net.TCPConn).Close()
  2587. }
  2588. <-clientDone
  2589. return nil
  2590. }
  2591. }
  2592. ct.run()
  2593. }
  2594. func testTransportReturnsUnusedFlowControl(t *testing.T, oneDataFrame bool) {
  2595. ct := newClientTester(t)
  2596. clientClosed := make(chan struct{})
  2597. serverWroteFirstByte := make(chan struct{})
  2598. ct.client = func() error {
  2599. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2600. res, err := ct.tr.RoundTrip(req)
  2601. if err != nil {
  2602. return err
  2603. }
  2604. <-serverWroteFirstByte
  2605. if n, err := res.Body.Read(make([]byte, 1)); err != nil || n != 1 {
  2606. return fmt.Errorf("body read = %v, %v; want 1, nil", n, err)
  2607. }
  2608. res.Body.Close() // leaving 4999 bytes unread
  2609. close(clientClosed)
  2610. return nil
  2611. }
  2612. ct.server = func() error {
  2613. ct.greet()
  2614. var hf *HeadersFrame
  2615. for {
  2616. f, err := ct.fr.ReadFrame()
  2617. if err != nil {
  2618. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2619. }
  2620. switch f.(type) {
  2621. case *WindowUpdateFrame, *SettingsFrame:
  2622. continue
  2623. }
  2624. var ok bool
  2625. hf, ok = f.(*HeadersFrame)
  2626. if !ok {
  2627. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2628. }
  2629. break
  2630. }
  2631. var buf bytes.Buffer
  2632. enc := hpack.NewEncoder(&buf)
  2633. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2634. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2635. ct.fr.WriteHeaders(HeadersFrameParam{
  2636. StreamID: hf.StreamID,
  2637. EndHeaders: true,
  2638. EndStream: false,
  2639. BlockFragment: buf.Bytes(),
  2640. })
  2641. // Two cases:
  2642. // - Send one DATA frame with 5000 bytes.
  2643. // - Send two DATA frames with 1 and 4999 bytes each.
  2644. //
  2645. // In both cases, the client should consume one byte of data,
  2646. // refund that byte, then refund the following 4999 bytes.
  2647. //
  2648. // In the second case, the server waits for the client connection to
  2649. // close before seconding the second DATA frame. This tests the case
  2650. // where the client receives a DATA frame after it has reset the stream.
  2651. if oneDataFrame {
  2652. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 5000))
  2653. close(serverWroteFirstByte)
  2654. <-clientClosed
  2655. } else {
  2656. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 1))
  2657. close(serverWroteFirstByte)
  2658. <-clientClosed
  2659. ct.fr.WriteData(hf.StreamID, false /* don't end stream */, make([]byte, 4999))
  2660. }
  2661. waitingFor := "RSTStreamFrame"
  2662. for {
  2663. f, err := ct.fr.ReadFrame()
  2664. if err != nil {
  2665. return fmt.Errorf("ReadFrame while waiting for %s: %v", waitingFor, err)
  2666. }
  2667. if _, ok := f.(*SettingsFrame); ok {
  2668. continue
  2669. }
  2670. switch waitingFor {
  2671. case "RSTStreamFrame":
  2672. if rf, ok := f.(*RSTStreamFrame); !ok || rf.ErrCode != ErrCodeCancel {
  2673. return fmt.Errorf("Expected a RSTStreamFrame with code cancel; got %v", summarizeFrame(f))
  2674. }
  2675. waitingFor = "WindowUpdateFrame"
  2676. case "WindowUpdateFrame":
  2677. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != 4999 {
  2678. return fmt.Errorf("Expected WindowUpdateFrame for 4999 bytes; got %v", summarizeFrame(f))
  2679. }
  2680. return nil
  2681. }
  2682. }
  2683. }
  2684. ct.run()
  2685. }
  2686. // See golang.org/issue/16481
  2687. func TestTransportReturnsUnusedFlowControlSingleWrite(t *testing.T) {
  2688. testTransportReturnsUnusedFlowControl(t, true)
  2689. }
  2690. // See golang.org/issue/20469
  2691. func TestTransportReturnsUnusedFlowControlMultipleWrites(t *testing.T) {
  2692. testTransportReturnsUnusedFlowControl(t, false)
  2693. }
  2694. // Issue 16612: adjust flow control on open streams when transport
  2695. // receives SETTINGS with INITIAL_WINDOW_SIZE from server.
  2696. func TestTransportAdjustsFlowControl(t *testing.T) {
  2697. ct := newClientTester(t)
  2698. clientDone := make(chan struct{})
  2699. const bodySize = 1 << 20
  2700. ct.client = func() error {
  2701. defer ct.cc.(*net.TCPConn).CloseWrite()
  2702. if runtime.GOOS == "plan9" {
  2703. // CloseWrite not supported on Plan 9; Issue 17906
  2704. defer ct.cc.(*net.TCPConn).Close()
  2705. }
  2706. defer close(clientDone)
  2707. req, _ := http.NewRequest("POST", "https://dummy.tld/", struct{ io.Reader }{io.LimitReader(neverEnding('A'), bodySize)})
  2708. res, err := ct.tr.RoundTrip(req)
  2709. if err != nil {
  2710. return err
  2711. }
  2712. res.Body.Close()
  2713. return nil
  2714. }
  2715. ct.server = func() error {
  2716. _, err := io.ReadFull(ct.sc, make([]byte, len(ClientPreface)))
  2717. if err != nil {
  2718. return fmt.Errorf("reading client preface: %v", err)
  2719. }
  2720. var gotBytes int64
  2721. var sentSettings bool
  2722. for {
  2723. f, err := ct.fr.ReadFrame()
  2724. if err != nil {
  2725. select {
  2726. case <-clientDone:
  2727. return nil
  2728. default:
  2729. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2730. }
  2731. }
  2732. switch f := f.(type) {
  2733. case *DataFrame:
  2734. gotBytes += int64(len(f.Data()))
  2735. // After we've got half the client's
  2736. // initial flow control window's worth
  2737. // of request body data, give it just
  2738. // enough flow control to finish.
  2739. if gotBytes >= initialWindowSize/2 && !sentSettings {
  2740. sentSettings = true
  2741. ct.fr.WriteSettings(Setting{ID: SettingInitialWindowSize, Val: bodySize})
  2742. ct.fr.WriteWindowUpdate(0, bodySize)
  2743. ct.fr.WriteSettingsAck()
  2744. }
  2745. if f.StreamEnded() {
  2746. var buf bytes.Buffer
  2747. enc := hpack.NewEncoder(&buf)
  2748. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2749. ct.fr.WriteHeaders(HeadersFrameParam{
  2750. StreamID: f.StreamID,
  2751. EndHeaders: true,
  2752. EndStream: true,
  2753. BlockFragment: buf.Bytes(),
  2754. })
  2755. }
  2756. }
  2757. }
  2758. }
  2759. ct.run()
  2760. }
  2761. // See golang.org/issue/16556
  2762. func TestTransportReturnsDataPaddingFlowControl(t *testing.T) {
  2763. ct := newClientTester(t)
  2764. unblockClient := make(chan bool, 1)
  2765. ct.client = func() error {
  2766. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2767. res, err := ct.tr.RoundTrip(req)
  2768. if err != nil {
  2769. return err
  2770. }
  2771. defer res.Body.Close()
  2772. <-unblockClient
  2773. return nil
  2774. }
  2775. ct.server = func() error {
  2776. ct.greet()
  2777. var hf *HeadersFrame
  2778. for {
  2779. f, err := ct.fr.ReadFrame()
  2780. if err != nil {
  2781. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  2782. }
  2783. switch f.(type) {
  2784. case *WindowUpdateFrame, *SettingsFrame:
  2785. continue
  2786. }
  2787. var ok bool
  2788. hf, ok = f.(*HeadersFrame)
  2789. if !ok {
  2790. return fmt.Errorf("Got %T; want HeadersFrame", f)
  2791. }
  2792. break
  2793. }
  2794. var buf bytes.Buffer
  2795. enc := hpack.NewEncoder(&buf)
  2796. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2797. enc.WriteField(hpack.HeaderField{Name: "content-length", Value: "5000"})
  2798. ct.fr.WriteHeaders(HeadersFrameParam{
  2799. StreamID: hf.StreamID,
  2800. EndHeaders: true,
  2801. EndStream: false,
  2802. BlockFragment: buf.Bytes(),
  2803. })
  2804. pad := make([]byte, 5)
  2805. ct.fr.WriteDataPadded(hf.StreamID, false, make([]byte, 5000), pad) // without ending stream
  2806. f, err := ct.readNonSettingsFrame()
  2807. if err != nil {
  2808. return fmt.Errorf("ReadFrame while waiting for first WindowUpdateFrame: %v", err)
  2809. }
  2810. wantBack := uint32(len(pad)) + 1 // one byte for the length of the padding
  2811. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID != 0 {
  2812. return fmt.Errorf("Expected conn WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2813. }
  2814. f, err = ct.readNonSettingsFrame()
  2815. if err != nil {
  2816. return fmt.Errorf("ReadFrame while waiting for second WindowUpdateFrame: %v", err)
  2817. }
  2818. if wuf, ok := f.(*WindowUpdateFrame); !ok || wuf.Increment != wantBack || wuf.StreamID == 0 {
  2819. return fmt.Errorf("Expected stream WindowUpdateFrame for %d bytes; got %v", wantBack, summarizeFrame(f))
  2820. }
  2821. unblockClient <- true
  2822. return nil
  2823. }
  2824. ct.run()
  2825. }
  2826. // golang.org/issue/16572 -- RoundTrip shouldn't hang when it gets a
  2827. // StreamError as a result of the response HEADERS
  2828. func TestTransportReturnsErrorOnBadResponseHeaders(t *testing.T) {
  2829. ct := newClientTester(t)
  2830. ct.client = func() error {
  2831. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  2832. res, err := ct.tr.RoundTrip(req)
  2833. if err == nil {
  2834. res.Body.Close()
  2835. return errors.New("unexpected successful GET")
  2836. }
  2837. want := StreamError{1, ErrCodeProtocol, headerFieldNameError(" content-type")}
  2838. if !reflect.DeepEqual(want, err) {
  2839. t.Errorf("RoundTrip error = %#v; want %#v", err, want)
  2840. }
  2841. return nil
  2842. }
  2843. ct.server = func() error {
  2844. ct.greet()
  2845. hf, err := ct.firstHeaders()
  2846. if err != nil {
  2847. return err
  2848. }
  2849. var buf bytes.Buffer
  2850. enc := hpack.NewEncoder(&buf)
  2851. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  2852. enc.WriteField(hpack.HeaderField{Name: " content-type", Value: "bogus"}) // bogus spaces
  2853. ct.fr.WriteHeaders(HeadersFrameParam{
  2854. StreamID: hf.StreamID,
  2855. EndHeaders: true,
  2856. EndStream: false,
  2857. BlockFragment: buf.Bytes(),
  2858. })
  2859. for {
  2860. fr, err := ct.readFrame()
  2861. if err != nil {
  2862. return fmt.Errorf("error waiting for RST_STREAM from client: %v", err)
  2863. }
  2864. if _, ok := fr.(*SettingsFrame); ok {
  2865. continue
  2866. }
  2867. if rst, ok := fr.(*RSTStreamFrame); !ok || rst.StreamID != 1 || rst.ErrCode != ErrCodeProtocol {
  2868. t.Errorf("Frame = %v; want RST_STREAM for stream 1 with ErrCodeProtocol", summarizeFrame(fr))
  2869. }
  2870. break
  2871. }
  2872. return nil
  2873. }
  2874. ct.run()
  2875. }
  2876. // byteAndEOFReader returns is in an io.Reader which reads one byte
  2877. // (the underlying byte) and io.EOF at once in its Read call.
  2878. type byteAndEOFReader byte
  2879. func (b byteAndEOFReader) Read(p []byte) (n int, err error) {
  2880. if len(p) == 0 {
  2881. panic("unexpected useless call")
  2882. }
  2883. p[0] = byte(b)
  2884. return 1, io.EOF
  2885. }
  2886. // Issue 16788: the Transport had a regression where it started
  2887. // sending a spurious DATA frame with a duplicate END_STREAM bit after
  2888. // the request body writer goroutine had already read an EOF from the
  2889. // Request.Body and included the END_STREAM on a data-carrying DATA
  2890. // frame.
  2891. //
  2892. // Notably, to trigger this, the requests need to use a Request.Body
  2893. // which returns (non-0, io.EOF) and also needs to set the ContentLength
  2894. // explicitly.
  2895. func TestTransportBodyDoubleEndStream(t *testing.T) {
  2896. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2897. // Nothing.
  2898. }, optOnlyServer)
  2899. defer st.Close()
  2900. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  2901. defer tr.CloseIdleConnections()
  2902. for i := 0; i < 2; i++ {
  2903. req, _ := http.NewRequest("POST", st.ts.URL, byteAndEOFReader('a'))
  2904. req.ContentLength = 1
  2905. res, err := tr.RoundTrip(req)
  2906. if err != nil {
  2907. t.Fatalf("failure on req %d: %v", i+1, err)
  2908. }
  2909. defer res.Body.Close()
  2910. }
  2911. }
  2912. // golang.org/issue/16847, golang.org/issue/19103
  2913. func TestTransportRequestPathPseudo(t *testing.T) {
  2914. type result struct {
  2915. path string
  2916. err string
  2917. }
  2918. tests := []struct {
  2919. req *http.Request
  2920. want result
  2921. }{
  2922. 0: {
  2923. req: &http.Request{
  2924. Method: "GET",
  2925. URL: &url.URL{
  2926. Host: "foo.com",
  2927. Path: "/foo",
  2928. },
  2929. },
  2930. want: result{path: "/foo"},
  2931. },
  2932. // In Go 1.7, we accepted paths of "//foo".
  2933. // In Go 1.8, we rejected it (issue 16847).
  2934. // In Go 1.9, we accepted it again (issue 19103).
  2935. 1: {
  2936. req: &http.Request{
  2937. Method: "GET",
  2938. URL: &url.URL{
  2939. Host: "foo.com",
  2940. Path: "//foo",
  2941. },
  2942. },
  2943. want: result{path: "//foo"},
  2944. },
  2945. // Opaque with //$Matching_Hostname/path
  2946. 2: {
  2947. req: &http.Request{
  2948. Method: "GET",
  2949. URL: &url.URL{
  2950. Scheme: "https",
  2951. Opaque: "//foo.com/path",
  2952. Host: "foo.com",
  2953. Path: "/ignored",
  2954. },
  2955. },
  2956. want: result{path: "/path"},
  2957. },
  2958. // Opaque with some other Request.Host instead:
  2959. 3: {
  2960. req: &http.Request{
  2961. Method: "GET",
  2962. Host: "bar.com",
  2963. URL: &url.URL{
  2964. Scheme: "https",
  2965. Opaque: "//bar.com/path",
  2966. Host: "foo.com",
  2967. Path: "/ignored",
  2968. },
  2969. },
  2970. want: result{path: "/path"},
  2971. },
  2972. // Opaque without the leading "//":
  2973. 4: {
  2974. req: &http.Request{
  2975. Method: "GET",
  2976. URL: &url.URL{
  2977. Opaque: "/path",
  2978. Host: "foo.com",
  2979. Path: "/ignored",
  2980. },
  2981. },
  2982. want: result{path: "/path"},
  2983. },
  2984. // Opaque we can't handle:
  2985. 5: {
  2986. req: &http.Request{
  2987. Method: "GET",
  2988. URL: &url.URL{
  2989. Scheme: "https",
  2990. Opaque: "//unknown_host/path",
  2991. Host: "foo.com",
  2992. Path: "/ignored",
  2993. },
  2994. },
  2995. want: result{err: `invalid request :path "https://unknown_host/path" from URL.Opaque = "//unknown_host/path"`},
  2996. },
  2997. // A CONNECT request:
  2998. 6: {
  2999. req: &http.Request{
  3000. Method: "CONNECT",
  3001. URL: &url.URL{
  3002. Host: "foo.com",
  3003. },
  3004. },
  3005. want: result{},
  3006. },
  3007. }
  3008. for i, tt := range tests {
  3009. cc := &ClientConn{peerMaxHeaderListSize: 0xffffffffffffffff}
  3010. cc.henc = hpack.NewEncoder(&cc.hbuf)
  3011. cc.mu.Lock()
  3012. hdrs, err := cc.encodeHeaders(tt.req, false, "", -1)
  3013. cc.mu.Unlock()
  3014. var got result
  3015. hpackDec := hpack.NewDecoder(initialHeaderTableSize, func(f hpack.HeaderField) {
  3016. if f.Name == ":path" {
  3017. got.path = f.Value
  3018. }
  3019. })
  3020. if err != nil {
  3021. got.err = err.Error()
  3022. } else if len(hdrs) > 0 {
  3023. if _, err := hpackDec.Write(hdrs); err != nil {
  3024. t.Errorf("%d. bogus hpack: %v", i, err)
  3025. continue
  3026. }
  3027. }
  3028. if got != tt.want {
  3029. t.Errorf("%d. got %+v; want %+v", i, got, tt.want)
  3030. }
  3031. }
  3032. }
  3033. // golang.org/issue/17071 -- don't sniff the first byte of the request body
  3034. // before we've determined that the ClientConn is usable.
  3035. func TestRoundTripDoesntConsumeRequestBodyEarly(t *testing.T) {
  3036. const body = "foo"
  3037. req, _ := http.NewRequest("POST", "http://foo.com/", ioutil.NopCloser(strings.NewReader(body)))
  3038. cc := &ClientConn{
  3039. closed: true,
  3040. }
  3041. _, err := cc.RoundTrip(req)
  3042. if err != errClientConnUnusable {
  3043. t.Fatalf("RoundTrip = %v; want errClientConnUnusable", err)
  3044. }
  3045. slurp, err := ioutil.ReadAll(req.Body)
  3046. if err != nil {
  3047. t.Errorf("ReadAll = %v", err)
  3048. }
  3049. if string(slurp) != body {
  3050. t.Errorf("Body = %q; want %q", slurp, body)
  3051. }
  3052. }
  3053. func TestClientConnPing(t *testing.T) {
  3054. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}, optOnlyServer)
  3055. defer st.Close()
  3056. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3057. defer tr.CloseIdleConnections()
  3058. cc, err := tr.dialClientConn(st.ts.Listener.Addr().String(), false)
  3059. if err != nil {
  3060. t.Fatal(err)
  3061. }
  3062. if err = cc.Ping(context.Background()); err != nil {
  3063. t.Fatal(err)
  3064. }
  3065. }
  3066. // Issue 16974: if the server sent a DATA frame after the user
  3067. // canceled the Transport's Request, the Transport previously wrote to a
  3068. // closed pipe, got an error, and ended up closing the whole TCP
  3069. // connection.
  3070. func TestTransportCancelDataResponseRace(t *testing.T) {
  3071. cancel := make(chan struct{})
  3072. clientGotError := make(chan bool, 1)
  3073. const msg = "Hello."
  3074. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3075. if strings.Contains(r.URL.Path, "/hello") {
  3076. time.Sleep(50 * time.Millisecond)
  3077. io.WriteString(w, msg)
  3078. return
  3079. }
  3080. for i := 0; i < 50; i++ {
  3081. io.WriteString(w, "Some data.")
  3082. w.(http.Flusher).Flush()
  3083. if i == 2 {
  3084. close(cancel)
  3085. <-clientGotError
  3086. }
  3087. time.Sleep(10 * time.Millisecond)
  3088. }
  3089. }, optOnlyServer)
  3090. defer st.Close()
  3091. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3092. defer tr.CloseIdleConnections()
  3093. c := &http.Client{Transport: tr}
  3094. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  3095. req.Cancel = cancel
  3096. res, err := c.Do(req)
  3097. if err != nil {
  3098. t.Fatal(err)
  3099. }
  3100. if _, err = io.Copy(ioutil.Discard, res.Body); err == nil {
  3101. t.Fatal("unexpected success")
  3102. }
  3103. clientGotError <- true
  3104. res, err = c.Get(st.ts.URL + "/hello")
  3105. if err != nil {
  3106. t.Fatal(err)
  3107. }
  3108. slurp, err := ioutil.ReadAll(res.Body)
  3109. if err != nil {
  3110. t.Fatal(err)
  3111. }
  3112. if string(slurp) != msg {
  3113. t.Errorf("Got = %q; want %q", slurp, msg)
  3114. }
  3115. }
  3116. // Issue 21316: It should be safe to reuse an http.Request after the
  3117. // request has completed.
  3118. func TestTransportNoRaceOnRequestObjectAfterRequestComplete(t *testing.T) {
  3119. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3120. w.WriteHeader(200)
  3121. io.WriteString(w, "body")
  3122. }, optOnlyServer)
  3123. defer st.Close()
  3124. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3125. defer tr.CloseIdleConnections()
  3126. req, _ := http.NewRequest("GET", st.ts.URL, nil)
  3127. resp, err := tr.RoundTrip(req)
  3128. if err != nil {
  3129. t.Fatal(err)
  3130. }
  3131. if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
  3132. t.Fatalf("error reading response body: %v", err)
  3133. }
  3134. if err := resp.Body.Close(); err != nil {
  3135. t.Fatalf("error closing response body: %v", err)
  3136. }
  3137. // This access of req.Header should not race with code in the transport.
  3138. req.Header = http.Header{}
  3139. }
  3140. func TestTransportCloseAfterLostPing(t *testing.T) {
  3141. clientDone := make(chan struct{})
  3142. ct := newClientTester(t)
  3143. ct.tr.PingTimeout = 1 * time.Second
  3144. ct.tr.ReadIdleTimeout = 1 * time.Second
  3145. ct.client = func() error {
  3146. defer ct.cc.(*net.TCPConn).CloseWrite()
  3147. defer close(clientDone)
  3148. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3149. _, err := ct.tr.RoundTrip(req)
  3150. if err == nil || !strings.Contains(err.Error(), "client connection lost") {
  3151. return fmt.Errorf("expected to get error about \"connection lost\", got %v", err)
  3152. }
  3153. return nil
  3154. }
  3155. ct.server = func() error {
  3156. ct.greet()
  3157. <-clientDone
  3158. return nil
  3159. }
  3160. ct.run()
  3161. }
  3162. func TestTransportPingWhenReading(t *testing.T) {
  3163. testCases := []struct {
  3164. name string
  3165. readIdleTimeout time.Duration
  3166. serverResponseInterval time.Duration
  3167. expectedPingCount int
  3168. }{
  3169. {
  3170. name: "two pings in each serverResponseInterval",
  3171. readIdleTimeout: 400 * time.Millisecond,
  3172. serverResponseInterval: 1000 * time.Millisecond,
  3173. expectedPingCount: 4,
  3174. },
  3175. {
  3176. name: "one ping in each serverResponseInterval",
  3177. readIdleTimeout: 700 * time.Millisecond,
  3178. serverResponseInterval: 1000 * time.Millisecond,
  3179. expectedPingCount: 2,
  3180. },
  3181. {
  3182. name: "zero ping in each serverResponseInterval",
  3183. readIdleTimeout: 1000 * time.Millisecond,
  3184. serverResponseInterval: 500 * time.Millisecond,
  3185. expectedPingCount: 0,
  3186. },
  3187. {
  3188. name: "0 readIdleTimeout means no ping",
  3189. readIdleTimeout: 0 * time.Millisecond,
  3190. serverResponseInterval: 500 * time.Millisecond,
  3191. expectedPingCount: 0,
  3192. },
  3193. }
  3194. for _, tc := range testCases {
  3195. tc := tc // capture range variable
  3196. t.Run(tc.name, func(t *testing.T) {
  3197. t.Parallel()
  3198. testTransportPingWhenReading(t, tc.readIdleTimeout, tc.serverResponseInterval, tc.expectedPingCount)
  3199. })
  3200. }
  3201. }
  3202. func testTransportPingWhenReading(t *testing.T, readIdleTimeout, serverResponseInterval time.Duration, expectedPingCount int) {
  3203. var pingCount int
  3204. clientDone := make(chan struct{})
  3205. ct := newClientTester(t)
  3206. ct.tr.PingTimeout = 10 * time.Millisecond
  3207. ct.tr.ReadIdleTimeout = readIdleTimeout
  3208. // guards the ct.fr.Write
  3209. var wmu sync.Mutex
  3210. ct.client = func() error {
  3211. defer ct.cc.(*net.TCPConn).CloseWrite()
  3212. if runtime.GOOS == "plan9" {
  3213. // CloseWrite not supported on Plan 9; Issue 17906
  3214. defer ct.cc.(*net.TCPConn).Close()
  3215. }
  3216. defer close(clientDone)
  3217. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3218. res, err := ct.tr.RoundTrip(req)
  3219. if err != nil {
  3220. return fmt.Errorf("RoundTrip: %v", err)
  3221. }
  3222. defer res.Body.Close()
  3223. if res.StatusCode != 200 {
  3224. return fmt.Errorf("status code = %v; want %v", res.StatusCode, 200)
  3225. }
  3226. _, err = ioutil.ReadAll(res.Body)
  3227. return err
  3228. }
  3229. ct.server = func() error {
  3230. ct.greet()
  3231. var buf bytes.Buffer
  3232. enc := hpack.NewEncoder(&buf)
  3233. for {
  3234. f, err := ct.fr.ReadFrame()
  3235. if err != nil {
  3236. select {
  3237. case <-clientDone:
  3238. // If the client's done, it
  3239. // will have reported any
  3240. // errors on its side.
  3241. return nil
  3242. default:
  3243. return err
  3244. }
  3245. }
  3246. switch f := f.(type) {
  3247. case *WindowUpdateFrame, *SettingsFrame:
  3248. case *HeadersFrame:
  3249. if !f.HeadersEnded() {
  3250. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3251. }
  3252. enc.WriteField(hpack.HeaderField{Name: ":status", Value: strconv.Itoa(200)})
  3253. ct.fr.WriteHeaders(HeadersFrameParam{
  3254. StreamID: f.StreamID,
  3255. EndHeaders: true,
  3256. EndStream: false,
  3257. BlockFragment: buf.Bytes(),
  3258. })
  3259. go func() {
  3260. for i := 0; i < 2; i++ {
  3261. wmu.Lock()
  3262. if err := ct.fr.WriteData(f.StreamID, false, []byte(fmt.Sprintf("hello, this is server data frame %d", i))); err != nil {
  3263. wmu.Unlock()
  3264. t.Error(err)
  3265. return
  3266. }
  3267. wmu.Unlock()
  3268. time.Sleep(serverResponseInterval)
  3269. }
  3270. wmu.Lock()
  3271. if err := ct.fr.WriteData(f.StreamID, true, []byte("hello, this is last server data frame")); err != nil {
  3272. wmu.Unlock()
  3273. t.Error(err)
  3274. return
  3275. }
  3276. wmu.Unlock()
  3277. }()
  3278. case *PingFrame:
  3279. pingCount++
  3280. wmu.Lock()
  3281. if err := ct.fr.WritePing(true, f.Data); err != nil {
  3282. wmu.Unlock()
  3283. return err
  3284. }
  3285. wmu.Unlock()
  3286. default:
  3287. return fmt.Errorf("Unexpected client frame %v", f)
  3288. }
  3289. }
  3290. }
  3291. ct.run()
  3292. if e, a := expectedPingCount, pingCount; e != a {
  3293. t.Errorf("expected receiving %d pings, got %d pings", e, a)
  3294. }
  3295. }
  3296. func TestTransportRetryAfterGOAWAY(t *testing.T) {
  3297. var dialer struct {
  3298. sync.Mutex
  3299. count int
  3300. }
  3301. ct1 := make(chan *clientTester)
  3302. ct2 := make(chan *clientTester)
  3303. ln := newLocalListener(t)
  3304. defer ln.Close()
  3305. tr := &Transport{
  3306. TLSClientConfig: tlsConfigInsecure,
  3307. }
  3308. tr.DialTLS = func(network, addr string, cfg *tls.Config) (net.Conn, error) {
  3309. dialer.Lock()
  3310. defer dialer.Unlock()
  3311. dialer.count++
  3312. if dialer.count == 3 {
  3313. return nil, errors.New("unexpected number of dials")
  3314. }
  3315. cc, err := net.Dial("tcp", ln.Addr().String())
  3316. if err != nil {
  3317. return nil, fmt.Errorf("dial error: %v", err)
  3318. }
  3319. sc, err := ln.Accept()
  3320. if err != nil {
  3321. return nil, fmt.Errorf("accept error: %v", err)
  3322. }
  3323. ct := &clientTester{
  3324. t: t,
  3325. tr: tr,
  3326. cc: cc,
  3327. sc: sc,
  3328. fr: NewFramer(sc, sc),
  3329. }
  3330. switch dialer.count {
  3331. case 1:
  3332. ct1 <- ct
  3333. case 2:
  3334. ct2 <- ct
  3335. }
  3336. return cc, nil
  3337. }
  3338. errs := make(chan error, 3)
  3339. done := make(chan struct{})
  3340. defer close(done)
  3341. // Client.
  3342. go func() {
  3343. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3344. res, err := tr.RoundTrip(req)
  3345. if res != nil {
  3346. res.Body.Close()
  3347. if got := res.Header.Get("Foo"); got != "bar" {
  3348. err = fmt.Errorf("foo header = %q; want bar", got)
  3349. }
  3350. }
  3351. if err != nil {
  3352. err = fmt.Errorf("RoundTrip: %v", err)
  3353. }
  3354. errs <- err
  3355. }()
  3356. connToClose := make(chan io.Closer, 2)
  3357. // Server for the first request.
  3358. go func() {
  3359. var ct *clientTester
  3360. select {
  3361. case ct = <-ct1:
  3362. case <-done:
  3363. return
  3364. }
  3365. connToClose <- ct.cc
  3366. ct.greet()
  3367. hf, err := ct.firstHeaders()
  3368. if err != nil {
  3369. errs <- fmt.Errorf("server1 failed reading HEADERS: %v", err)
  3370. return
  3371. }
  3372. t.Logf("server1 got %v", hf)
  3373. if err := ct.fr.WriteGoAway(0 /*max id*/, ErrCodeNo, nil); err != nil {
  3374. errs <- fmt.Errorf("server1 failed writing GOAWAY: %v", err)
  3375. return
  3376. }
  3377. errs <- nil
  3378. }()
  3379. // Server for the second request.
  3380. go func() {
  3381. var ct *clientTester
  3382. select {
  3383. case ct = <-ct2:
  3384. case <-done:
  3385. return
  3386. }
  3387. connToClose <- ct.cc
  3388. ct.greet()
  3389. hf, err := ct.firstHeaders()
  3390. if err != nil {
  3391. errs <- fmt.Errorf("server2 failed reading HEADERS: %v", err)
  3392. return
  3393. }
  3394. t.Logf("server2 got %v", hf)
  3395. var buf bytes.Buffer
  3396. enc := hpack.NewEncoder(&buf)
  3397. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  3398. enc.WriteField(hpack.HeaderField{Name: "foo", Value: "bar"})
  3399. err = ct.fr.WriteHeaders(HeadersFrameParam{
  3400. StreamID: hf.StreamID,
  3401. EndHeaders: true,
  3402. EndStream: false,
  3403. BlockFragment: buf.Bytes(),
  3404. })
  3405. if err != nil {
  3406. errs <- fmt.Errorf("server2 failed writing response HEADERS: %v", err)
  3407. } else {
  3408. errs <- nil
  3409. }
  3410. }()
  3411. for k := 0; k < 3; k++ {
  3412. select {
  3413. case err := <-errs:
  3414. if err != nil {
  3415. t.Error(err)
  3416. }
  3417. case <-time.After(1 * time.Second):
  3418. t.Errorf("timed out")
  3419. }
  3420. }
  3421. for {
  3422. select {
  3423. case c := <-connToClose:
  3424. c.Close()
  3425. default:
  3426. return
  3427. }
  3428. }
  3429. }
  3430. func TestTransportRetryAfterRefusedStream(t *testing.T) {
  3431. clientDone := make(chan struct{})
  3432. ct := newClientTester(t)
  3433. ct.client = func() error {
  3434. defer ct.cc.(*net.TCPConn).CloseWrite()
  3435. if runtime.GOOS == "plan9" {
  3436. // CloseWrite not supported on Plan 9; Issue 17906
  3437. defer ct.cc.(*net.TCPConn).Close()
  3438. }
  3439. defer close(clientDone)
  3440. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3441. resp, err := ct.tr.RoundTrip(req)
  3442. if err != nil {
  3443. return fmt.Errorf("RoundTrip: %v", err)
  3444. }
  3445. resp.Body.Close()
  3446. if resp.StatusCode != 204 {
  3447. return fmt.Errorf("Status = %v; want 204", resp.StatusCode)
  3448. }
  3449. return nil
  3450. }
  3451. ct.server = func() error {
  3452. ct.greet()
  3453. var buf bytes.Buffer
  3454. enc := hpack.NewEncoder(&buf)
  3455. nreq := 0
  3456. for {
  3457. f, err := ct.fr.ReadFrame()
  3458. if err != nil {
  3459. select {
  3460. case <-clientDone:
  3461. // If the client's done, it
  3462. // will have reported any
  3463. // errors on its side.
  3464. return nil
  3465. default:
  3466. return err
  3467. }
  3468. }
  3469. switch f := f.(type) {
  3470. case *WindowUpdateFrame, *SettingsFrame:
  3471. case *HeadersFrame:
  3472. if !f.HeadersEnded() {
  3473. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3474. }
  3475. nreq++
  3476. if nreq == 1 {
  3477. ct.fr.WriteRSTStream(f.StreamID, ErrCodeRefusedStream)
  3478. } else {
  3479. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  3480. ct.fr.WriteHeaders(HeadersFrameParam{
  3481. StreamID: f.StreamID,
  3482. EndHeaders: true,
  3483. EndStream: true,
  3484. BlockFragment: buf.Bytes(),
  3485. })
  3486. }
  3487. default:
  3488. return fmt.Errorf("Unexpected client frame %v", f)
  3489. }
  3490. }
  3491. }
  3492. ct.run()
  3493. }
  3494. func TestTransportRetryHasLimit(t *testing.T) {
  3495. // Skip in short mode because the total expected delay is 1s+2s+4s+8s+16s=29s.
  3496. if testing.Short() {
  3497. t.Skip("skipping long test in short mode")
  3498. }
  3499. clientDone := make(chan struct{})
  3500. ct := newClientTester(t)
  3501. ct.client = func() error {
  3502. defer ct.cc.(*net.TCPConn).CloseWrite()
  3503. if runtime.GOOS == "plan9" {
  3504. // CloseWrite not supported on Plan 9; Issue 17906
  3505. defer ct.cc.(*net.TCPConn).Close()
  3506. }
  3507. defer close(clientDone)
  3508. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3509. resp, err := ct.tr.RoundTrip(req)
  3510. if err == nil {
  3511. return fmt.Errorf("RoundTrip expected error, got response: %+v", resp)
  3512. }
  3513. t.Logf("expected error, got: %v", err)
  3514. return nil
  3515. }
  3516. ct.server = func() error {
  3517. ct.greet()
  3518. for {
  3519. f, err := ct.fr.ReadFrame()
  3520. if err != nil {
  3521. select {
  3522. case <-clientDone:
  3523. // If the client's done, it
  3524. // will have reported any
  3525. // errors on its side.
  3526. return nil
  3527. default:
  3528. return err
  3529. }
  3530. }
  3531. switch f := f.(type) {
  3532. case *WindowUpdateFrame, *SettingsFrame:
  3533. case *HeadersFrame:
  3534. if !f.HeadersEnded() {
  3535. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3536. }
  3537. ct.fr.WriteRSTStream(f.StreamID, ErrCodeRefusedStream)
  3538. default:
  3539. return fmt.Errorf("Unexpected client frame %v", f)
  3540. }
  3541. }
  3542. }
  3543. ct.run()
  3544. }
  3545. func TestTransportResponseDataBeforeHeaders(t *testing.T) {
  3546. // This test use not valid response format.
  3547. // Discarding logger output to not spam tests output.
  3548. log.SetOutput(ioutil.Discard)
  3549. defer log.SetOutput(os.Stderr)
  3550. ct := newClientTester(t)
  3551. ct.client = func() error {
  3552. defer ct.cc.(*net.TCPConn).CloseWrite()
  3553. if runtime.GOOS == "plan9" {
  3554. // CloseWrite not supported on Plan 9; Issue 17906
  3555. defer ct.cc.(*net.TCPConn).Close()
  3556. }
  3557. req := httptest.NewRequest("GET", "https://dummy.tld/", nil)
  3558. // First request is normal to ensure the check is per stream and not per connection.
  3559. _, err := ct.tr.RoundTrip(req)
  3560. if err != nil {
  3561. return fmt.Errorf("RoundTrip expected no error, got: %v", err)
  3562. }
  3563. // Second request returns a DATA frame with no HEADERS.
  3564. resp, err := ct.tr.RoundTrip(req)
  3565. if err == nil {
  3566. return fmt.Errorf("RoundTrip expected error, got response: %+v", resp)
  3567. }
  3568. if err, ok := err.(StreamError); !ok || err.Code != ErrCodeProtocol {
  3569. return fmt.Errorf("expected stream PROTOCOL_ERROR, got: %v", err)
  3570. }
  3571. return nil
  3572. }
  3573. ct.server = func() error {
  3574. ct.greet()
  3575. for {
  3576. f, err := ct.fr.ReadFrame()
  3577. if err == io.EOF {
  3578. return nil
  3579. } else if err != nil {
  3580. return err
  3581. }
  3582. switch f := f.(type) {
  3583. case *WindowUpdateFrame, *SettingsFrame:
  3584. case *HeadersFrame:
  3585. switch f.StreamID {
  3586. case 1:
  3587. // Send a valid response to first request.
  3588. var buf bytes.Buffer
  3589. enc := hpack.NewEncoder(&buf)
  3590. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  3591. ct.fr.WriteHeaders(HeadersFrameParam{
  3592. StreamID: f.StreamID,
  3593. EndHeaders: true,
  3594. EndStream: true,
  3595. BlockFragment: buf.Bytes(),
  3596. })
  3597. case 3:
  3598. ct.fr.WriteData(f.StreamID, true, []byte("payload"))
  3599. }
  3600. default:
  3601. return fmt.Errorf("Unexpected client frame %v", f)
  3602. }
  3603. }
  3604. }
  3605. ct.run()
  3606. }
  3607. // tests Transport.StrictMaxConcurrentStreams
  3608. func TestTransportRequestsStallAtServerLimit(t *testing.T) {
  3609. const maxConcurrent = 2
  3610. greet := make(chan struct{}) // server sends initial SETTINGS frame
  3611. gotRequest := make(chan struct{}) // server received a request
  3612. clientDone := make(chan struct{})
  3613. // Collect errors from goroutines.
  3614. var wg sync.WaitGroup
  3615. errs := make(chan error, 100)
  3616. defer func() {
  3617. wg.Wait()
  3618. close(errs)
  3619. for err := range errs {
  3620. t.Error(err)
  3621. }
  3622. }()
  3623. // We will send maxConcurrent+2 requests. This checker goroutine waits for the
  3624. // following stages:
  3625. // 1. The first maxConcurrent requests are received by the server.
  3626. // 2. The client will cancel the next request
  3627. // 3. The server is unblocked so it can service the first maxConcurrent requests
  3628. // 4. The client will send the final request
  3629. wg.Add(1)
  3630. unblockClient := make(chan struct{})
  3631. clientRequestCancelled := make(chan struct{})
  3632. unblockServer := make(chan struct{})
  3633. go func() {
  3634. defer wg.Done()
  3635. // Stage 1.
  3636. for k := 0; k < maxConcurrent; k++ {
  3637. <-gotRequest
  3638. }
  3639. // Stage 2.
  3640. close(unblockClient)
  3641. <-clientRequestCancelled
  3642. // Stage 3: give some time for the final RoundTrip call to be scheduled and
  3643. // verify that the final request is not sent.
  3644. time.Sleep(50 * time.Millisecond)
  3645. select {
  3646. case <-gotRequest:
  3647. errs <- errors.New("last request did not stall")
  3648. close(unblockServer)
  3649. return
  3650. default:
  3651. }
  3652. close(unblockServer)
  3653. // Stage 4.
  3654. <-gotRequest
  3655. }()
  3656. ct := newClientTester(t)
  3657. ct.tr.StrictMaxConcurrentStreams = true
  3658. ct.client = func() error {
  3659. var wg sync.WaitGroup
  3660. defer func() {
  3661. wg.Wait()
  3662. close(clientDone)
  3663. ct.cc.(*net.TCPConn).CloseWrite()
  3664. if runtime.GOOS == "plan9" {
  3665. // CloseWrite not supported on Plan 9; Issue 17906
  3666. ct.cc.(*net.TCPConn).Close()
  3667. }
  3668. }()
  3669. for k := 0; k < maxConcurrent+2; k++ {
  3670. wg.Add(1)
  3671. go func(k int) {
  3672. defer wg.Done()
  3673. // Don't send the second request until after receiving SETTINGS from the server
  3674. // to avoid a race where we use the default SettingMaxConcurrentStreams, which
  3675. // is much larger than maxConcurrent. We have to send the first request before
  3676. // waiting because the first request triggers the dial and greet.
  3677. if k > 0 {
  3678. <-greet
  3679. }
  3680. // Block until maxConcurrent requests are sent before sending any more.
  3681. if k >= maxConcurrent {
  3682. <-unblockClient
  3683. }
  3684. req, _ := http.NewRequest("GET", fmt.Sprintf("https://dummy.tld/%d", k), nil)
  3685. if k == maxConcurrent {
  3686. // This request will be canceled.
  3687. cancel := make(chan struct{})
  3688. req.Cancel = cancel
  3689. close(cancel)
  3690. _, err := ct.tr.RoundTrip(req)
  3691. close(clientRequestCancelled)
  3692. if err == nil {
  3693. errs <- fmt.Errorf("RoundTrip(%d) should have failed due to cancel", k)
  3694. return
  3695. }
  3696. } else {
  3697. resp, err := ct.tr.RoundTrip(req)
  3698. if err != nil {
  3699. errs <- fmt.Errorf("RoundTrip(%d): %v", k, err)
  3700. return
  3701. }
  3702. ioutil.ReadAll(resp.Body)
  3703. resp.Body.Close()
  3704. if resp.StatusCode != 204 {
  3705. errs <- fmt.Errorf("Status = %v; want 204", resp.StatusCode)
  3706. return
  3707. }
  3708. }
  3709. }(k)
  3710. }
  3711. return nil
  3712. }
  3713. ct.server = func() error {
  3714. var wg sync.WaitGroup
  3715. defer wg.Wait()
  3716. ct.greet(Setting{SettingMaxConcurrentStreams, maxConcurrent})
  3717. // Server write loop.
  3718. var buf bytes.Buffer
  3719. enc := hpack.NewEncoder(&buf)
  3720. writeResp := make(chan uint32, maxConcurrent+1)
  3721. wg.Add(1)
  3722. go func() {
  3723. defer wg.Done()
  3724. <-unblockServer
  3725. for id := range writeResp {
  3726. buf.Reset()
  3727. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "204"})
  3728. ct.fr.WriteHeaders(HeadersFrameParam{
  3729. StreamID: id,
  3730. EndHeaders: true,
  3731. EndStream: true,
  3732. BlockFragment: buf.Bytes(),
  3733. })
  3734. }
  3735. }()
  3736. // Server read loop.
  3737. var nreq int
  3738. for {
  3739. f, err := ct.fr.ReadFrame()
  3740. if err != nil {
  3741. select {
  3742. case <-clientDone:
  3743. // If the client's done, it will have reported any errors on its side.
  3744. return nil
  3745. default:
  3746. return err
  3747. }
  3748. }
  3749. switch f := f.(type) {
  3750. case *WindowUpdateFrame:
  3751. case *SettingsFrame:
  3752. // Wait for the client SETTINGS ack until ending the greet.
  3753. close(greet)
  3754. case *HeadersFrame:
  3755. if !f.HeadersEnded() {
  3756. return fmt.Errorf("headers should have END_HEADERS be ended: %v", f)
  3757. }
  3758. gotRequest <- struct{}{}
  3759. nreq++
  3760. writeResp <- f.StreamID
  3761. if nreq == maxConcurrent+1 {
  3762. close(writeResp)
  3763. }
  3764. default:
  3765. return fmt.Errorf("Unexpected client frame %v", f)
  3766. }
  3767. }
  3768. }
  3769. ct.run()
  3770. }
  3771. func TestAuthorityAddr(t *testing.T) {
  3772. tests := []struct {
  3773. scheme, authority string
  3774. want string
  3775. }{
  3776. {"http", "foo.com", "foo.com:80"},
  3777. {"https", "foo.com", "foo.com:443"},
  3778. {"https", "foo.com:1234", "foo.com:1234"},
  3779. {"https", "1.2.3.4:1234", "1.2.3.4:1234"},
  3780. {"https", "1.2.3.4", "1.2.3.4:443"},
  3781. {"https", "[::1]:1234", "[::1]:1234"},
  3782. {"https", "[::1]", "[::1]:443"},
  3783. }
  3784. for _, tt := range tests {
  3785. got := authorityAddr(tt.scheme, tt.authority)
  3786. if got != tt.want {
  3787. t.Errorf("authorityAddr(%q, %q) = %q; want %q", tt.scheme, tt.authority, got, tt.want)
  3788. }
  3789. }
  3790. }
  3791. // Issue 20448: stop allocating for DATA frames' payload after
  3792. // Response.Body.Close is called.
  3793. func TestTransportAllocationsAfterResponseBodyClose(t *testing.T) {
  3794. megabyteZero := make([]byte, 1<<20)
  3795. writeErr := make(chan error, 1)
  3796. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3797. w.(http.Flusher).Flush()
  3798. var sum int64
  3799. for i := 0; i < 100; i++ {
  3800. n, err := w.Write(megabyteZero)
  3801. sum += int64(n)
  3802. if err != nil {
  3803. writeErr <- err
  3804. return
  3805. }
  3806. }
  3807. t.Logf("wrote all %d bytes", sum)
  3808. writeErr <- nil
  3809. }, optOnlyServer)
  3810. defer st.Close()
  3811. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3812. defer tr.CloseIdleConnections()
  3813. c := &http.Client{Transport: tr}
  3814. res, err := c.Get(st.ts.URL)
  3815. if err != nil {
  3816. t.Fatal(err)
  3817. }
  3818. var buf [1]byte
  3819. if _, err := res.Body.Read(buf[:]); err != nil {
  3820. t.Error(err)
  3821. }
  3822. if err := res.Body.Close(); err != nil {
  3823. t.Error(err)
  3824. }
  3825. trb, ok := res.Body.(transportResponseBody)
  3826. if !ok {
  3827. t.Fatalf("res.Body = %T; want transportResponseBody", res.Body)
  3828. }
  3829. if trb.cs.bufPipe.b != nil {
  3830. t.Errorf("response body pipe is still open")
  3831. }
  3832. gotErr := <-writeErr
  3833. if gotErr == nil {
  3834. t.Errorf("Handler unexpectedly managed to write its entire response without getting an error")
  3835. } else if gotErr != errStreamClosed {
  3836. t.Errorf("Handler Write err = %v; want errStreamClosed", gotErr)
  3837. }
  3838. }
  3839. // Issue 18891: make sure Request.Body == NoBody means no DATA frame
  3840. // is ever sent, even if empty.
  3841. func TestTransportNoBodyMeansNoDATA(t *testing.T) {
  3842. ct := newClientTester(t)
  3843. unblockClient := make(chan bool)
  3844. ct.client = func() error {
  3845. req, _ := http.NewRequest("GET", "https://dummy.tld/", http.NoBody)
  3846. ct.tr.RoundTrip(req)
  3847. <-unblockClient
  3848. return nil
  3849. }
  3850. ct.server = func() error {
  3851. defer close(unblockClient)
  3852. defer ct.cc.(*net.TCPConn).Close()
  3853. ct.greet()
  3854. for {
  3855. f, err := ct.fr.ReadFrame()
  3856. if err != nil {
  3857. return fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
  3858. }
  3859. switch f := f.(type) {
  3860. default:
  3861. return fmt.Errorf("Got %T; want HeadersFrame", f)
  3862. case *WindowUpdateFrame, *SettingsFrame:
  3863. continue
  3864. case *HeadersFrame:
  3865. if !f.StreamEnded() {
  3866. return fmt.Errorf("got headers frame without END_STREAM")
  3867. }
  3868. return nil
  3869. }
  3870. }
  3871. }
  3872. ct.run()
  3873. }
  3874. func benchSimpleRoundTrip(b *testing.B, nReqHeaders, nResHeader int) {
  3875. defer disableGoroutineTracking()()
  3876. b.ReportAllocs()
  3877. st := newServerTester(b,
  3878. func(w http.ResponseWriter, r *http.Request) {
  3879. for i := 0; i < nResHeader; i++ {
  3880. name := fmt.Sprint("A-", i)
  3881. w.Header().Set(name, "*")
  3882. }
  3883. },
  3884. optOnlyServer,
  3885. optQuiet,
  3886. )
  3887. defer st.Close()
  3888. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3889. defer tr.CloseIdleConnections()
  3890. req, err := http.NewRequest("GET", st.ts.URL, nil)
  3891. if err != nil {
  3892. b.Fatal(err)
  3893. }
  3894. for i := 0; i < nReqHeaders; i++ {
  3895. name := fmt.Sprint("A-", i)
  3896. req.Header.Set(name, "*")
  3897. }
  3898. b.ResetTimer()
  3899. for i := 0; i < b.N; i++ {
  3900. res, err := tr.RoundTrip(req)
  3901. if err != nil {
  3902. if res != nil {
  3903. res.Body.Close()
  3904. }
  3905. b.Fatalf("RoundTrip err = %v; want nil", err)
  3906. }
  3907. res.Body.Close()
  3908. if res.StatusCode != http.StatusOK {
  3909. b.Fatalf("Response code = %v; want %v", res.StatusCode, http.StatusOK)
  3910. }
  3911. }
  3912. }
  3913. type infiniteReader struct{}
  3914. func (r infiniteReader) Read(b []byte) (int, error) {
  3915. return len(b), nil
  3916. }
  3917. // Issue 20521: it is not an error to receive a response and end stream
  3918. // from the server without the body being consumed.
  3919. func TestTransportResponseAndResetWithoutConsumingBodyRace(t *testing.T) {
  3920. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3921. w.WriteHeader(http.StatusOK)
  3922. }, optOnlyServer)
  3923. defer st.Close()
  3924. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3925. defer tr.CloseIdleConnections()
  3926. // The request body needs to be big enough to trigger flow control.
  3927. req, _ := http.NewRequest("PUT", st.ts.URL, infiniteReader{})
  3928. res, err := tr.RoundTrip(req)
  3929. if err != nil {
  3930. t.Fatal(err)
  3931. }
  3932. if res.StatusCode != http.StatusOK {
  3933. t.Fatalf("Response code = %v; want %v", res.StatusCode, http.StatusOK)
  3934. }
  3935. }
  3936. // Verify transport doesn't crash when receiving bogus response lacking a :status header.
  3937. // Issue 22880.
  3938. func TestTransportHandlesInvalidStatuslessResponse(t *testing.T) {
  3939. ct := newClientTester(t)
  3940. ct.client = func() error {
  3941. req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
  3942. _, err := ct.tr.RoundTrip(req)
  3943. const substr = "malformed response from server: missing status pseudo header"
  3944. if !strings.Contains(fmt.Sprint(err), substr) {
  3945. return fmt.Errorf("RoundTrip error = %v; want substring %q", err, substr)
  3946. }
  3947. return nil
  3948. }
  3949. ct.server = func() error {
  3950. ct.greet()
  3951. var buf bytes.Buffer
  3952. enc := hpack.NewEncoder(&buf)
  3953. for {
  3954. f, err := ct.fr.ReadFrame()
  3955. if err != nil {
  3956. return err
  3957. }
  3958. switch f := f.(type) {
  3959. case *HeadersFrame:
  3960. enc.WriteField(hpack.HeaderField{Name: "content-type", Value: "text/html"}) // no :status header
  3961. ct.fr.WriteHeaders(HeadersFrameParam{
  3962. StreamID: f.StreamID,
  3963. EndHeaders: true,
  3964. EndStream: false, // we'll send some DATA to try to crash the transport
  3965. BlockFragment: buf.Bytes(),
  3966. })
  3967. ct.fr.WriteData(f.StreamID, true, []byte("payload"))
  3968. return nil
  3969. }
  3970. }
  3971. }
  3972. ct.run()
  3973. }
  3974. func BenchmarkClientRequestHeaders(b *testing.B) {
  3975. b.Run(" 0 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0, 0) })
  3976. b.Run(" 10 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 10, 0) })
  3977. b.Run(" 100 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 100, 0) })
  3978. b.Run("1000 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 1000, 0) })
  3979. }
  3980. func BenchmarkClientResponseHeaders(b *testing.B) {
  3981. b.Run(" 0 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0, 0) })
  3982. b.Run(" 10 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0, 10) })
  3983. b.Run(" 100 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0, 100) })
  3984. b.Run("1000 Headers", func(b *testing.B) { benchSimpleRoundTrip(b, 0, 1000) })
  3985. }
  3986. func activeStreams(cc *ClientConn) int {
  3987. cc.mu.Lock()
  3988. defer cc.mu.Unlock()
  3989. return len(cc.streams)
  3990. }
  3991. type closeMode int
  3992. const (
  3993. closeAtHeaders closeMode = iota
  3994. closeAtBody
  3995. shutdown
  3996. shutdownCancel
  3997. )
  3998. // See golang.org/issue/17292
  3999. func testClientConnClose(t *testing.T, closeMode closeMode) {
  4000. clientDone := make(chan struct{})
  4001. defer close(clientDone)
  4002. handlerDone := make(chan struct{})
  4003. closeDone := make(chan struct{})
  4004. beforeHeader := func() {}
  4005. bodyWrite := func(w http.ResponseWriter) {}
  4006. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4007. defer close(handlerDone)
  4008. beforeHeader()
  4009. w.WriteHeader(http.StatusOK)
  4010. w.(http.Flusher).Flush()
  4011. bodyWrite(w)
  4012. select {
  4013. case <-w.(http.CloseNotifier).CloseNotify():
  4014. // client closed connection before completion
  4015. if closeMode == shutdown || closeMode == shutdownCancel {
  4016. t.Error("expected request to complete")
  4017. }
  4018. case <-clientDone:
  4019. if closeMode == closeAtHeaders || closeMode == closeAtBody {
  4020. t.Error("expected connection closed by client")
  4021. }
  4022. }
  4023. }, optOnlyServer)
  4024. defer st.Close()
  4025. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  4026. defer tr.CloseIdleConnections()
  4027. cc, err := tr.dialClientConn(st.ts.Listener.Addr().String(), false)
  4028. req, err := http.NewRequest("GET", st.ts.URL, nil)
  4029. if err != nil {
  4030. t.Fatal(err)
  4031. }
  4032. if closeMode == closeAtHeaders {
  4033. beforeHeader = func() {
  4034. if err := cc.Close(); err != nil {
  4035. t.Error(err)
  4036. }
  4037. close(closeDone)
  4038. }
  4039. }
  4040. var sendBody chan struct{}
  4041. if closeMode == closeAtBody {
  4042. sendBody = make(chan struct{})
  4043. bodyWrite = func(w http.ResponseWriter) {
  4044. <-sendBody
  4045. b := make([]byte, 32)
  4046. w.Write(b)
  4047. w.(http.Flusher).Flush()
  4048. if err := cc.Close(); err != nil {
  4049. t.Errorf("unexpected ClientConn close error: %v", err)
  4050. }
  4051. close(closeDone)
  4052. w.Write(b)
  4053. w.(http.Flusher).Flush()
  4054. }
  4055. }
  4056. res, err := cc.RoundTrip(req)
  4057. if res != nil {
  4058. defer res.Body.Close()
  4059. }
  4060. if closeMode == closeAtHeaders {
  4061. got := fmt.Sprint(err)
  4062. want := "http2: client connection force closed via ClientConn.Close"
  4063. if got != want {
  4064. t.Fatalf("RoundTrip error = %v, want %v", got, want)
  4065. }
  4066. } else {
  4067. if err != nil {
  4068. t.Fatalf("RoundTrip: %v", err)
  4069. }
  4070. if got, want := activeStreams(cc), 1; got != want {
  4071. t.Errorf("got %d active streams, want %d", got, want)
  4072. }
  4073. }
  4074. switch closeMode {
  4075. case shutdownCancel:
  4076. if err = cc.Shutdown(canceledCtx); err != context.Canceled {
  4077. t.Errorf("got %v, want %v", err, context.Canceled)
  4078. }
  4079. if cc.closing == false {
  4080. t.Error("expected closing to be true")
  4081. }
  4082. if cc.CanTakeNewRequest() == true {
  4083. t.Error("CanTakeNewRequest to return false")
  4084. }
  4085. if v, want := len(cc.streams), 1; v != want {
  4086. t.Errorf("expected %d active streams, got %d", want, v)
  4087. }
  4088. clientDone <- struct{}{}
  4089. <-handlerDone
  4090. case shutdown:
  4091. wait := make(chan struct{})
  4092. shutdownEnterWaitStateHook = func() {
  4093. close(wait)
  4094. shutdownEnterWaitStateHook = func() {}
  4095. }
  4096. defer func() { shutdownEnterWaitStateHook = func() {} }()
  4097. shutdown := make(chan struct{}, 1)
  4098. go func() {
  4099. if err = cc.Shutdown(context.Background()); err != nil {
  4100. t.Error(err)
  4101. }
  4102. close(shutdown)
  4103. }()
  4104. // Let the shutdown to enter wait state
  4105. <-wait
  4106. cc.mu.Lock()
  4107. if cc.closing == false {
  4108. t.Error("expected closing to be true")
  4109. }
  4110. cc.mu.Unlock()
  4111. if cc.CanTakeNewRequest() == true {
  4112. t.Error("CanTakeNewRequest to return false")
  4113. }
  4114. if got, want := activeStreams(cc), 1; got != want {
  4115. t.Errorf("got %d active streams, want %d", got, want)
  4116. }
  4117. // Let the active request finish
  4118. clientDone <- struct{}{}
  4119. // Wait for the shutdown to end
  4120. select {
  4121. case <-shutdown:
  4122. case <-time.After(2 * time.Second):
  4123. t.Fatal("expected server connection to close")
  4124. }
  4125. case closeAtHeaders, closeAtBody:
  4126. if closeMode == closeAtBody {
  4127. go close(sendBody)
  4128. if _, err := io.Copy(ioutil.Discard, res.Body); err == nil {
  4129. t.Error("expected a Copy error, got nil")
  4130. }
  4131. }
  4132. <-closeDone
  4133. if got, want := activeStreams(cc), 0; got != want {
  4134. t.Errorf("got %d active streams, want %d", got, want)
  4135. }
  4136. // wait for server to get the connection close notice
  4137. select {
  4138. case <-handlerDone:
  4139. case <-time.After(2 * time.Second):
  4140. t.Fatal("expected server connection to close")
  4141. }
  4142. }
  4143. }
  4144. // The client closes the connection just after the server got the client's HEADERS
  4145. // frame, but before the server sends its HEADERS response back. The expected
  4146. // result is an error on RoundTrip explaining the client closed the connection.
  4147. func TestClientConnCloseAtHeaders(t *testing.T) {
  4148. testClientConnClose(t, closeAtHeaders)
  4149. }
  4150. // The client closes the connection between two server's response DATA frames.
  4151. // The expected behavior is a response body io read error on the client.
  4152. func TestClientConnCloseAtBody(t *testing.T) {
  4153. testClientConnClose(t, closeAtBody)
  4154. }
  4155. // The client sends a GOAWAY frame before the server finished processing a request.
  4156. // We expect the connection not to close until the request is completed.
  4157. func TestClientConnShutdown(t *testing.T) {
  4158. testClientConnClose(t, shutdown)
  4159. }
  4160. // The client sends a GOAWAY frame before the server finishes processing a request,
  4161. // but cancels the passed context before the request is completed. The expected
  4162. // behavior is the client closing the connection after the context is canceled.
  4163. func TestClientConnShutdownCancel(t *testing.T) {
  4164. testClientConnClose(t, shutdownCancel)
  4165. }
  4166. // Issue 25009: use Request.GetBody if present, even if it seems like
  4167. // we might not need it. Apparently something else can still read from
  4168. // the original request body. Data race? In any case, rewinding
  4169. // unconditionally on retry is a nicer model anyway and should
  4170. // simplify code in the future (after the Go 1.11 freeze)
  4171. func TestTransportUsesGetBodyWhenPresent(t *testing.T) {
  4172. calls := 0
  4173. someBody := func() io.ReadCloser {
  4174. return struct{ io.ReadCloser }{ioutil.NopCloser(bytes.NewReader(nil))}
  4175. }
  4176. req := &http.Request{
  4177. Body: someBody(),
  4178. GetBody: func() (io.ReadCloser, error) {
  4179. calls++
  4180. return someBody(), nil
  4181. },
  4182. }
  4183. afterBodyWrite := false // pretend we haven't read+written the body yet
  4184. req2, err := shouldRetryRequest(req, errClientConnUnusable, afterBodyWrite)
  4185. if err != nil {
  4186. t.Fatal(err)
  4187. }
  4188. if calls != 1 {
  4189. t.Errorf("Calls = %d; want 1", calls)
  4190. }
  4191. if req2 == req {
  4192. t.Error("req2 changed")
  4193. }
  4194. if req2 == nil {
  4195. t.Fatal("req2 is nil")
  4196. }
  4197. if req2.Body == nil {
  4198. t.Fatal("req2.Body is nil")
  4199. }
  4200. if req2.GetBody == nil {
  4201. t.Fatal("req2.GetBody is nil")
  4202. }
  4203. if req2.Body == req.Body {
  4204. t.Error("req2.Body unchanged")
  4205. }
  4206. }
  4207. // Issue 22891: verify that the "https" altproto we register with net/http
  4208. // is a certain type: a struct with one field with our *http2.Transport in it.
  4209. func TestNoDialH2RoundTripperType(t *testing.T) {
  4210. t1 := new(http.Transport)
  4211. t2 := new(Transport)
  4212. rt := noDialH2RoundTripper{t2}
  4213. if err := registerHTTPSProtocol(t1, rt); err != nil {
  4214. t.Fatal(err)
  4215. }
  4216. rv := reflect.ValueOf(rt)
  4217. if rv.Type().Kind() != reflect.Struct {
  4218. t.Fatalf("kind = %v; net/http expects struct", rv.Type().Kind())
  4219. }
  4220. if n := rv.Type().NumField(); n != 1 {
  4221. t.Fatalf("fields = %d; net/http expects 1", n)
  4222. }
  4223. v := rv.Field(0)
  4224. if _, ok := v.Interface().(*Transport); !ok {
  4225. t.Fatalf("wrong kind %T; want *Transport", v.Interface())
  4226. }
  4227. }
  4228. type errReader struct {
  4229. body []byte
  4230. err error
  4231. }
  4232. func (r *errReader) Read(p []byte) (int, error) {
  4233. if len(r.body) > 0 {
  4234. n := copy(p, r.body)
  4235. r.body = r.body[n:]
  4236. return n, nil
  4237. }
  4238. return 0, r.err
  4239. }
  4240. func testTransportBodyReadError(t *testing.T, body []byte) {
  4241. if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
  4242. // So far we've only seen this be flaky on Windows and Plan 9,
  4243. // perhaps due to TCP behavior on shutdowns while
  4244. // unread data is in flight. This test should be
  4245. // fixed, but a skip is better than annoying people
  4246. // for now.
  4247. t.Skipf("skipping flaky test on %s; https://golang.org/issue/31260", runtime.GOOS)
  4248. }
  4249. clientDone := make(chan struct{})
  4250. ct := newClientTester(t)
  4251. ct.client = func() error {
  4252. defer ct.cc.(*net.TCPConn).CloseWrite()
  4253. if runtime.GOOS == "plan9" {
  4254. // CloseWrite not supported on Plan 9; Issue 17906
  4255. defer ct.cc.(*net.TCPConn).Close()
  4256. }
  4257. defer close(clientDone)
  4258. checkNoStreams := func() error {
  4259. cp, ok := ct.tr.connPool().(*clientConnPool)
  4260. if !ok {
  4261. return fmt.Errorf("conn pool is %T; want *clientConnPool", ct.tr.connPool())
  4262. }
  4263. cp.mu.Lock()
  4264. defer cp.mu.Unlock()
  4265. conns, ok := cp.conns["dummy.tld:443"]
  4266. if !ok {
  4267. return fmt.Errorf("missing connection")
  4268. }
  4269. if len(conns) != 1 {
  4270. return fmt.Errorf("conn pool size: %v; expect 1", len(conns))
  4271. }
  4272. if activeStreams(conns[0]) != 0 {
  4273. return fmt.Errorf("active streams count: %v; want 0", activeStreams(conns[0]))
  4274. }
  4275. return nil
  4276. }
  4277. bodyReadError := errors.New("body read error")
  4278. body := &errReader{body, bodyReadError}
  4279. req, err := http.NewRequest("PUT", "https://dummy.tld/", body)
  4280. if err != nil {
  4281. return err
  4282. }
  4283. _, err = ct.tr.RoundTrip(req)
  4284. if err != bodyReadError {
  4285. return fmt.Errorf("err = %v; want %v", err, bodyReadError)
  4286. }
  4287. if err = checkNoStreams(); err != nil {
  4288. return err
  4289. }
  4290. return nil
  4291. }
  4292. ct.server = func() error {
  4293. ct.greet()
  4294. var receivedBody []byte
  4295. var resetCount int
  4296. for {
  4297. f, err := ct.fr.ReadFrame()
  4298. t.Logf("server: ReadFrame = %v, %v", f, err)
  4299. if err != nil {
  4300. select {
  4301. case <-clientDone:
  4302. // If the client's done, it
  4303. // will have reported any
  4304. // errors on its side.
  4305. if bytes.Compare(receivedBody, body) != 0 {
  4306. return fmt.Errorf("body: %q; expected %q", receivedBody, body)
  4307. }
  4308. if resetCount != 1 {
  4309. return fmt.Errorf("stream reset count: %v; expected: 1", resetCount)
  4310. }
  4311. return nil
  4312. default:
  4313. return err
  4314. }
  4315. }
  4316. switch f := f.(type) {
  4317. case *WindowUpdateFrame, *SettingsFrame:
  4318. case *HeadersFrame:
  4319. case *DataFrame:
  4320. receivedBody = append(receivedBody, f.Data()...)
  4321. case *RSTStreamFrame:
  4322. resetCount++
  4323. default:
  4324. return fmt.Errorf("Unexpected client frame %v", f)
  4325. }
  4326. }
  4327. }
  4328. ct.run()
  4329. }
  4330. func TestTransportBodyReadError_Immediately(t *testing.T) { testTransportBodyReadError(t, nil) }
  4331. func TestTransportBodyReadError_Some(t *testing.T) { testTransportBodyReadError(t, []byte("123")) }
  4332. // Issue 32254: verify that the client sends END_STREAM flag eagerly with the last
  4333. // (or in this test-case the only one) request body data frame, and does not send
  4334. // extra zero-len data frames.
  4335. func TestTransportBodyEagerEndStream(t *testing.T) {
  4336. const reqBody = "some request body"
  4337. const resBody = "some response body"
  4338. ct := newClientTester(t)
  4339. ct.client = func() error {
  4340. defer ct.cc.(*net.TCPConn).CloseWrite()
  4341. if runtime.GOOS == "plan9" {
  4342. // CloseWrite not supported on Plan 9; Issue 17906
  4343. defer ct.cc.(*net.TCPConn).Close()
  4344. }
  4345. body := strings.NewReader(reqBody)
  4346. req, err := http.NewRequest("PUT", "https://dummy.tld/", body)
  4347. if err != nil {
  4348. return err
  4349. }
  4350. _, err = ct.tr.RoundTrip(req)
  4351. if err != nil {
  4352. return err
  4353. }
  4354. return nil
  4355. }
  4356. ct.server = func() error {
  4357. ct.greet()
  4358. for {
  4359. f, err := ct.fr.ReadFrame()
  4360. if err != nil {
  4361. return err
  4362. }
  4363. switch f := f.(type) {
  4364. case *WindowUpdateFrame, *SettingsFrame:
  4365. case *HeadersFrame:
  4366. case *DataFrame:
  4367. if !f.StreamEnded() {
  4368. ct.fr.WriteRSTStream(f.StreamID, ErrCodeRefusedStream)
  4369. return fmt.Errorf("data frame without END_STREAM %v", f)
  4370. }
  4371. var buf bytes.Buffer
  4372. enc := hpack.NewEncoder(&buf)
  4373. enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
  4374. ct.fr.WriteHeaders(HeadersFrameParam{
  4375. StreamID: f.Header().StreamID,
  4376. EndHeaders: true,
  4377. EndStream: false,
  4378. BlockFragment: buf.Bytes(),
  4379. })
  4380. ct.fr.WriteData(f.StreamID, true, []byte(resBody))
  4381. return nil
  4382. case *RSTStreamFrame:
  4383. default:
  4384. return fmt.Errorf("Unexpected client frame %v", f)
  4385. }
  4386. }
  4387. }
  4388. ct.run()
  4389. }
  4390. type chunkReader struct {
  4391. chunks [][]byte
  4392. }
  4393. func (r *chunkReader) Read(p []byte) (int, error) {
  4394. if len(r.chunks) > 0 {
  4395. n := copy(p, r.chunks[0])
  4396. r.chunks = r.chunks[1:]
  4397. return n, nil
  4398. }
  4399. panic("shouldn't read this many times")
  4400. }
  4401. // Issue 32254: if the request body is larger than the specified
  4402. // content length, the client should refuse to send the extra part
  4403. // and abort the stream.
  4404. //
  4405. // In _len3 case, the first Read() matches the expected content length
  4406. // but the second read returns more data.
  4407. //
  4408. // In _len2 case, the first Read() exceeds the expected content length.
  4409. func TestTransportBodyLargerThanSpecifiedContentLength_len3(t *testing.T) {
  4410. body := &chunkReader{[][]byte{
  4411. []byte("123"),
  4412. []byte("456"),
  4413. }}
  4414. testTransportBodyLargerThanSpecifiedContentLength(t, body, 3)
  4415. }
  4416. func TestTransportBodyLargerThanSpecifiedContentLength_len2(t *testing.T) {
  4417. body := &chunkReader{[][]byte{
  4418. []byte("123"),
  4419. }}
  4420. testTransportBodyLargerThanSpecifiedContentLength(t, body, 2)
  4421. }
  4422. func testTransportBodyLargerThanSpecifiedContentLength(t *testing.T, body *chunkReader, contentLen int64) {
  4423. st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4424. r.Body.Read(make([]byte, 6))
  4425. }, optOnlyServer)
  4426. defer st.Close()
  4427. tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  4428. defer tr.CloseIdleConnections()
  4429. req, _ := http.NewRequest("POST", st.ts.URL, body)
  4430. req.ContentLength = contentLen
  4431. _, err := tr.RoundTrip(req)
  4432. if err != errReqBodyTooLong {
  4433. t.Fatalf("expected %v, got %v", errReqBodyTooLong, err)
  4434. }
  4435. }
  4436. func TestClientConnTooIdle(t *testing.T) {
  4437. tests := []struct {
  4438. cc func() *ClientConn
  4439. want bool
  4440. }{
  4441. {
  4442. func() *ClientConn {
  4443. return &ClientConn{idleTimeout: 5 * time.Second, lastIdle: time.Now().Add(-10 * time.Second)}
  4444. },
  4445. true,
  4446. },
  4447. {
  4448. func() *ClientConn {
  4449. return &ClientConn{idleTimeout: 5 * time.Second, lastIdle: time.Time{}}
  4450. },
  4451. false,
  4452. },
  4453. {
  4454. func() *ClientConn {
  4455. return &ClientConn{idleTimeout: 60 * time.Second, lastIdle: time.Now().Add(-10 * time.Second)}
  4456. },
  4457. false,
  4458. },
  4459. {
  4460. func() *ClientConn {
  4461. return &ClientConn{idleTimeout: 0, lastIdle: time.Now().Add(-10 * time.Second)}
  4462. },
  4463. false,
  4464. },
  4465. }
  4466. for i, tt := range tests {
  4467. got := tt.cc().tooIdleLocked()
  4468. if got != tt.want {
  4469. t.Errorf("%d. got %v; want %v", i, got, tt.want)
  4470. }
  4471. }
  4472. }