server.go 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  1. // Copyright 2014 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. // TODO: turn off the serve goroutine when idle, so
  5. // an idle conn only has the readFrames goroutine active. (which could
  6. // also be optimized probably to pin less memory in crypto/tls). This
  7. // would involve tracking when the serve goroutine is active (atomic
  8. // int32 read/CAS probably?) and starting it up when frames arrive,
  9. // and shutting it down when all handlers exit. the occasional PING
  10. // packets could use time.AfterFunc to call sc.wakeStartServeLoop()
  11. // (which is a no-op if already running) and then queue the PING write
  12. // as normal. The serve loop would then exit in most cases (if no
  13. // Handlers running) and not be woken up again until the PING packet
  14. // returns.
  15. // TODO (maybe): add a mechanism for Handlers to going into
  16. // half-closed-local mode (rw.(io.Closer) test?) but not exit their
  17. // handler, and continue to be able to read from the
  18. // Request.Body. This would be a somewhat semantic change from HTTP/1
  19. // (or at least what we expose in net/http), so I'd probably want to
  20. // add it there too. For now, this package says that returning from
  21. // the Handler ServeHTTP function means you're both done reading and
  22. // done writing, without a way to stop just one or the other.
  23. package http2
  24. import (
  25. "bufio"
  26. "bytes"
  27. "context"
  28. "crypto/tls"
  29. "errors"
  30. "fmt"
  31. "io"
  32. "log"
  33. "math"
  34. "net"
  35. "net/http"
  36. "net/textproto"
  37. "net/url"
  38. "os"
  39. "reflect"
  40. "runtime"
  41. "strconv"
  42. "strings"
  43. "sync"
  44. "time"
  45. "golang.org/x/net/http/httpguts"
  46. "golang.org/x/net/http2/hpack"
  47. )
  48. const (
  49. prefaceTimeout = 10 * time.Second
  50. firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway
  51. handlerChunkWriteSize = 4 << 10
  52. defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to?
  53. )
  54. var (
  55. errClientDisconnected = errors.New("client disconnected")
  56. errClosedBody = errors.New("body closed by handler")
  57. errHandlerComplete = errors.New("http2: request body closed due to handler exiting")
  58. errStreamClosed = errors.New("http2: stream closed")
  59. )
  60. var responseWriterStatePool = sync.Pool{
  61. New: func() interface{} {
  62. rws := &responseWriterState{}
  63. rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize)
  64. return rws
  65. },
  66. }
  67. // Test hooks.
  68. var (
  69. testHookOnConn func()
  70. testHookGetServerConn func(*serverConn)
  71. testHookOnPanicMu *sync.Mutex // nil except in tests
  72. testHookOnPanic func(sc *serverConn, panicVal interface{}) (rePanic bool)
  73. )
  74. // Server is an HTTP/2 server.
  75. type Server struct {
  76. // MaxHandlers limits the number of http.Handler ServeHTTP goroutines
  77. // which may run at a time over all connections.
  78. // Negative or zero no limit.
  79. // TODO: implement
  80. MaxHandlers int
  81. // MaxConcurrentStreams optionally specifies the number of
  82. // concurrent streams that each client may have open at a
  83. // time. This is unrelated to the number of http.Handler goroutines
  84. // which may be active globally, which is MaxHandlers.
  85. // If zero, MaxConcurrentStreams defaults to at least 100, per
  86. // the HTTP/2 spec's recommendations.
  87. MaxConcurrentStreams uint32
  88. // MaxReadFrameSize optionally specifies the largest frame
  89. // this server is willing to read. A valid value is between
  90. // 16k and 16M, inclusive. If zero or otherwise invalid, a
  91. // default value is used.
  92. MaxReadFrameSize uint32
  93. // PermitProhibitedCipherSuites, if true, permits the use of
  94. // cipher suites prohibited by the HTTP/2 spec.
  95. PermitProhibitedCipherSuites bool
  96. // IdleTimeout specifies how long until idle clients should be
  97. // closed with a GOAWAY frame. PING frames are not considered
  98. // activity for the purposes of IdleTimeout.
  99. IdleTimeout time.Duration
  100. // MaxUploadBufferPerConnection is the size of the initial flow
  101. // control window for each connections. The HTTP/2 spec does not
  102. // allow this to be smaller than 65535 or larger than 2^32-1.
  103. // If the value is outside this range, a default value will be
  104. // used instead.
  105. MaxUploadBufferPerConnection int32
  106. // MaxUploadBufferPerStream is the size of the initial flow control
  107. // window for each stream. The HTTP/2 spec does not allow this to
  108. // be larger than 2^32-1. If the value is zero or larger than the
  109. // maximum, a default value will be used instead.
  110. MaxUploadBufferPerStream int32
  111. // NewWriteScheduler constructs a write scheduler for a connection.
  112. // If nil, a default scheduler is chosen.
  113. NewWriteScheduler func() WriteScheduler
  114. // Internal state. This is a pointer (rather than embedded directly)
  115. // so that we don't embed a Mutex in this struct, which will make the
  116. // struct non-copyable, which might break some callers.
  117. state *serverInternalState
  118. }
  119. func (s *Server) initialConnRecvWindowSize() int32 {
  120. if s.MaxUploadBufferPerConnection > initialWindowSize {
  121. return s.MaxUploadBufferPerConnection
  122. }
  123. return 1 << 20
  124. }
  125. func (s *Server) initialStreamRecvWindowSize() int32 {
  126. if s.MaxUploadBufferPerStream > 0 {
  127. return s.MaxUploadBufferPerStream
  128. }
  129. return 1 << 20
  130. }
  131. func (s *Server) maxReadFrameSize() uint32 {
  132. if v := s.MaxReadFrameSize; v >= minMaxFrameSize && v <= maxFrameSize {
  133. return v
  134. }
  135. return defaultMaxReadFrameSize
  136. }
  137. func (s *Server) maxConcurrentStreams() uint32 {
  138. if v := s.MaxConcurrentStreams; v > 0 {
  139. return v
  140. }
  141. return defaultMaxStreams
  142. }
  143. type serverInternalState struct {
  144. mu sync.Mutex
  145. activeConns map[*serverConn]struct{}
  146. }
  147. func (s *serverInternalState) registerConn(sc *serverConn) {
  148. if s == nil {
  149. return // if the Server was used without calling ConfigureServer
  150. }
  151. s.mu.Lock()
  152. s.activeConns[sc] = struct{}{}
  153. s.mu.Unlock()
  154. }
  155. func (s *serverInternalState) unregisterConn(sc *serverConn) {
  156. if s == nil {
  157. return // if the Server was used without calling ConfigureServer
  158. }
  159. s.mu.Lock()
  160. delete(s.activeConns, sc)
  161. s.mu.Unlock()
  162. }
  163. func (s *serverInternalState) startGracefulShutdown() {
  164. if s == nil {
  165. return // if the Server was used without calling ConfigureServer
  166. }
  167. s.mu.Lock()
  168. for sc := range s.activeConns {
  169. sc.startGracefulShutdown()
  170. }
  171. s.mu.Unlock()
  172. }
  173. // ConfigureServer adds HTTP/2 support to a net/http Server.
  174. //
  175. // The configuration conf may be nil.
  176. //
  177. // ConfigureServer must be called before s begins serving.
  178. func ConfigureServer(s *http.Server, conf *Server) error {
  179. if s == nil {
  180. panic("nil *http.Server")
  181. }
  182. if conf == nil {
  183. conf = new(Server)
  184. }
  185. conf.state = &serverInternalState{activeConns: make(map[*serverConn]struct{})}
  186. if h1, h2 := s, conf; h2.IdleTimeout == 0 {
  187. if h1.IdleTimeout != 0 {
  188. h2.IdleTimeout = h1.IdleTimeout
  189. } else {
  190. h2.IdleTimeout = h1.ReadTimeout
  191. }
  192. }
  193. s.RegisterOnShutdown(conf.state.startGracefulShutdown)
  194. if s.TLSConfig == nil {
  195. s.TLSConfig = new(tls.Config)
  196. } else if s.TLSConfig.CipherSuites != nil {
  197. // If they already provided a CipherSuite list, return
  198. // an error if it has a bad order or is missing
  199. // ECDHE_RSA_WITH_AES_128_GCM_SHA256 or ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
  200. haveRequired := false
  201. sawBad := false
  202. for i, cs := range s.TLSConfig.CipherSuites {
  203. switch cs {
  204. case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  205. // Alternative MTI cipher to not discourage ECDSA-only servers.
  206. // See http://golang.org/cl/30721 for further information.
  207. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  208. haveRequired = true
  209. }
  210. if isBadCipher(cs) {
  211. sawBad = true
  212. } else if sawBad {
  213. return fmt.Errorf("http2: TLSConfig.CipherSuites index %d contains an HTTP/2-approved cipher suite (%#04x), but it comes after unapproved cipher suites. With this configuration, clients that don't support previous, approved cipher suites may be given an unapproved one and reject the connection.", i, cs)
  214. }
  215. }
  216. if !haveRequired {
  217. return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher.")
  218. }
  219. }
  220. // Note: not setting MinVersion to tls.VersionTLS12,
  221. // as we don't want to interfere with HTTP/1.1 traffic
  222. // on the user's server. We enforce TLS 1.2 later once
  223. // we accept a connection. Ideally this should be done
  224. // during next-proto selection, but using TLS <1.2 with
  225. // HTTP/2 is still the client's bug.
  226. s.TLSConfig.PreferServerCipherSuites = true
  227. haveNPN := false
  228. for _, p := range s.TLSConfig.NextProtos {
  229. if p == NextProtoTLS {
  230. haveNPN = true
  231. break
  232. }
  233. }
  234. if !haveNPN {
  235. s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, NextProtoTLS)
  236. }
  237. if s.TLSNextProto == nil {
  238. s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){}
  239. }
  240. protoHandler := func(hs *http.Server, c *tls.Conn, h http.Handler) {
  241. if testHookOnConn != nil {
  242. testHookOnConn()
  243. }
  244. // The TLSNextProto interface predates contexts, so
  245. // the net/http package passes down its per-connection
  246. // base context via an exported but unadvertised
  247. // method on the Handler. This is for internal
  248. // net/http<=>http2 use only.
  249. var ctx context.Context
  250. type baseContexter interface {
  251. BaseContext() context.Context
  252. }
  253. if bc, ok := h.(baseContexter); ok {
  254. ctx = bc.BaseContext()
  255. }
  256. conf.ServeConn(c, &ServeConnOpts{
  257. Context: ctx,
  258. Handler: h,
  259. BaseConfig: hs,
  260. })
  261. }
  262. s.TLSNextProto[NextProtoTLS] = protoHandler
  263. return nil
  264. }
  265. // ServeConnOpts are options for the Server.ServeConn method.
  266. type ServeConnOpts struct {
  267. // Context is the base context to use.
  268. // If nil, context.Background is used.
  269. Context context.Context
  270. // BaseConfig optionally sets the base configuration
  271. // for values. If nil, defaults are used.
  272. BaseConfig *http.Server
  273. // Handler specifies which handler to use for processing
  274. // requests. If nil, BaseConfig.Handler is used. If BaseConfig
  275. // or BaseConfig.Handler is nil, http.DefaultServeMux is used.
  276. Handler http.Handler
  277. }
  278. func (o *ServeConnOpts) context() context.Context {
  279. if o.Context != nil {
  280. return o.Context
  281. }
  282. return context.Background()
  283. }
  284. func (o *ServeConnOpts) baseConfig() *http.Server {
  285. if o != nil && o.BaseConfig != nil {
  286. return o.BaseConfig
  287. }
  288. return new(http.Server)
  289. }
  290. func (o *ServeConnOpts) handler() http.Handler {
  291. if o != nil {
  292. if o.Handler != nil {
  293. return o.Handler
  294. }
  295. if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
  296. return o.BaseConfig.Handler
  297. }
  298. }
  299. return http.DefaultServeMux
  300. }
  301. // ServeConn serves HTTP/2 requests on the provided connection and
  302. // blocks until the connection is no longer readable.
  303. //
  304. // ServeConn starts speaking HTTP/2 assuming that c has not had any
  305. // reads or writes. It writes its initial settings frame and expects
  306. // to be able to read the preface and settings frame from the
  307. // client. If c has a ConnectionState method like a *tls.Conn, the
  308. // ConnectionState is used to verify the TLS ciphersuite and to set
  309. // the Request.TLS field in Handlers.
  310. //
  311. // ServeConn does not support h2c by itself. Any h2c support must be
  312. // implemented in terms of providing a suitably-behaving net.Conn.
  313. //
  314. // The opts parameter is optional. If nil, default values are used.
  315. func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) {
  316. baseCtx, cancel := serverConnBaseContext(c, opts)
  317. defer cancel()
  318. sc := &serverConn{
  319. srv: s,
  320. hs: opts.baseConfig(),
  321. conn: c,
  322. baseCtx: baseCtx,
  323. remoteAddrStr: c.RemoteAddr().String(),
  324. bw: newBufferedWriter(c),
  325. handler: opts.handler(),
  326. streams: make(map[uint32]*stream),
  327. readFrameCh: make(chan readFrameResult),
  328. wantWriteFrameCh: make(chan FrameWriteRequest, 8),
  329. serveMsgCh: make(chan interface{}, 8),
  330. wroteFrameCh: make(chan frameWriteResult, 1), // buffered; one send in writeFrameAsync
  331. bodyReadCh: make(chan bodyReadMsg), // buffering doesn't matter either way
  332. doneServing: make(chan struct{}),
  333. clientMaxStreams: math.MaxUint32, // Section 6.5.2: "Initially, there is no limit to this value"
  334. advMaxStreams: s.maxConcurrentStreams(),
  335. initialStreamSendWindowSize: initialWindowSize,
  336. maxFrameSize: initialMaxFrameSize,
  337. headerTableSize: initialHeaderTableSize,
  338. serveG: newGoroutineLock(),
  339. pushEnabled: true,
  340. }
  341. s.state.registerConn(sc)
  342. defer s.state.unregisterConn(sc)
  343. // The net/http package sets the write deadline from the
  344. // http.Server.WriteTimeout during the TLS handshake, but then
  345. // passes the connection off to us with the deadline already set.
  346. // Write deadlines are set per stream in serverConn.newStream.
  347. // Disarm the net.Conn write deadline here.
  348. if sc.hs.WriteTimeout != 0 {
  349. sc.conn.SetWriteDeadline(time.Time{})
  350. }
  351. if s.NewWriteScheduler != nil {
  352. sc.writeSched = s.NewWriteScheduler()
  353. } else {
  354. sc.writeSched = NewRandomWriteScheduler()
  355. }
  356. // These start at the RFC-specified defaults. If there is a higher
  357. // configured value for inflow, that will be updated when we send a
  358. // WINDOW_UPDATE shortly after sending SETTINGS.
  359. sc.flow.add(initialWindowSize)
  360. sc.inflow.add(initialWindowSize)
  361. sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
  362. fr := NewFramer(sc.bw, c)
  363. fr.ReadMetaHeaders = hpack.NewDecoder(initialHeaderTableSize, nil)
  364. fr.MaxHeaderListSize = sc.maxHeaderListSize()
  365. fr.SetMaxReadFrameSize(s.maxReadFrameSize())
  366. sc.framer = fr
  367. if tc, ok := c.(connectionStater); ok {
  368. sc.tlsState = new(tls.ConnectionState)
  369. *sc.tlsState = tc.ConnectionState()
  370. // 9.2 Use of TLS Features
  371. // An implementation of HTTP/2 over TLS MUST use TLS
  372. // 1.2 or higher with the restrictions on feature set
  373. // and cipher suite described in this section. Due to
  374. // implementation limitations, it might not be
  375. // possible to fail TLS negotiation. An endpoint MUST
  376. // immediately terminate an HTTP/2 connection that
  377. // does not meet the TLS requirements described in
  378. // this section with a connection error (Section
  379. // 5.4.1) of type INADEQUATE_SECURITY.
  380. if sc.tlsState.Version < tls.VersionTLS12 {
  381. sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low")
  382. return
  383. }
  384. if sc.tlsState.ServerName == "" {
  385. // Client must use SNI, but we don't enforce that anymore,
  386. // since it was causing problems when connecting to bare IP
  387. // addresses during development.
  388. //
  389. // TODO: optionally enforce? Or enforce at the time we receive
  390. // a new request, and verify the ServerName matches the :authority?
  391. // But that precludes proxy situations, perhaps.
  392. //
  393. // So for now, do nothing here again.
  394. }
  395. if !s.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) {
  396. // "Endpoints MAY choose to generate a connection error
  397. // (Section 5.4.1) of type INADEQUATE_SECURITY if one of
  398. // the prohibited cipher suites are negotiated."
  399. //
  400. // We choose that. In my opinion, the spec is weak
  401. // here. It also says both parties must support at least
  402. // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no
  403. // excuses here. If we really must, we could allow an
  404. // "AllowInsecureWeakCiphers" option on the server later.
  405. // Let's see how it plays out first.
  406. sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
  407. return
  408. }
  409. }
  410. if hook := testHookGetServerConn; hook != nil {
  411. hook(sc)
  412. }
  413. sc.serve()
  414. }
  415. func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) {
  416. ctx, cancel = context.WithCancel(opts.context())
  417. ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
  418. if hs := opts.baseConfig(); hs != nil {
  419. ctx = context.WithValue(ctx, http.ServerContextKey, hs)
  420. }
  421. return
  422. }
  423. func (sc *serverConn) rejectConn(err ErrCode, debug string) {
  424. sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
  425. // ignoring errors. hanging up anyway.
  426. sc.framer.WriteGoAway(0, err, []byte(debug))
  427. sc.bw.Flush()
  428. sc.conn.Close()
  429. }
  430. type serverConn struct {
  431. // Immutable:
  432. srv *Server
  433. hs *http.Server
  434. conn net.Conn
  435. bw *bufferedWriter // writing to conn
  436. handler http.Handler
  437. baseCtx context.Context
  438. framer *Framer
  439. doneServing chan struct{} // closed when serverConn.serve ends
  440. readFrameCh chan readFrameResult // written by serverConn.readFrames
  441. wantWriteFrameCh chan FrameWriteRequest // from handlers -> serve
  442. wroteFrameCh chan frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes
  443. bodyReadCh chan bodyReadMsg // from handlers -> serve
  444. serveMsgCh chan interface{} // misc messages & code to send to / run on the serve loop
  445. flow flow // conn-wide (not stream-specific) outbound flow control
  446. inflow flow // conn-wide inbound flow control
  447. tlsState *tls.ConnectionState // shared by all handlers, like net/http
  448. remoteAddrStr string
  449. writeSched WriteScheduler
  450. // Everything following is owned by the serve loop; use serveG.check():
  451. serveG goroutineLock // used to verify funcs are on serve()
  452. pushEnabled bool
  453. sawFirstSettings bool // got the initial SETTINGS frame after the preface
  454. needToSendSettingsAck bool
  455. unackedSettings int // how many SETTINGS have we sent without ACKs?
  456. clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
  457. advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
  458. curClientStreams uint32 // number of open streams initiated by the client
  459. curPushedStreams uint32 // number of open streams initiated by server push
  460. maxClientStreamID uint32 // max ever seen from client (odd), or 0 if there have been no client requests
  461. maxPushPromiseID uint32 // ID of the last push promise (even), or 0 if there have been no pushes
  462. streams map[uint32]*stream
  463. initialStreamSendWindowSize int32
  464. maxFrameSize int32
  465. headerTableSize uint32
  466. peerMaxHeaderListSize uint32 // zero means unknown (default)
  467. canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case
  468. writingFrame bool // started writing a frame (on serve goroutine or separate)
  469. writingFrameAsync bool // started a frame on its own goroutine but haven't heard back on wroteFrameCh
  470. needsFrameFlush bool // last frame write wasn't a flush
  471. inGoAway bool // we've started to or sent GOAWAY
  472. inFrameScheduleLoop bool // whether we're in the scheduleFrameWrite loop
  473. needToSendGoAway bool // we need to schedule a GOAWAY frame write
  474. goAwayCode ErrCode
  475. shutdownTimer *time.Timer // nil until used
  476. idleTimer *time.Timer // nil if unused
  477. // Owned by the writeFrameAsync goroutine:
  478. headerWriteBuf bytes.Buffer
  479. hpackEncoder *hpack.Encoder
  480. // Used by startGracefulShutdown.
  481. shutdownOnce sync.Once
  482. }
  483. func (sc *serverConn) maxHeaderListSize() uint32 {
  484. n := sc.hs.MaxHeaderBytes
  485. if n <= 0 {
  486. n = http.DefaultMaxHeaderBytes
  487. }
  488. // http2's count is in a slightly different unit and includes 32 bytes per pair.
  489. // So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  490. const perFieldOverhead = 32 // per http2 spec
  491. const typicalHeaders = 10 // conservative
  492. return uint32(n + typicalHeaders*perFieldOverhead)
  493. }
  494. func (sc *serverConn) curOpenStreams() uint32 {
  495. sc.serveG.check()
  496. return sc.curClientStreams + sc.curPushedStreams
  497. }
  498. // stream represents a stream. This is the minimal metadata needed by
  499. // the serve goroutine. Most of the actual stream state is owned by
  500. // the http.Handler's goroutine in the responseWriter. Because the
  501. // responseWriter's responseWriterState is recycled at the end of a
  502. // handler, this struct intentionally has no pointer to the
  503. // *responseWriter{,State} itself, as the Handler ending nils out the
  504. // responseWriter's state field.
  505. type stream struct {
  506. // immutable:
  507. sc *serverConn
  508. id uint32
  509. body *pipe // non-nil if expecting DATA frames
  510. cw closeWaiter // closed wait stream transitions to closed state
  511. ctx context.Context
  512. cancelCtx func()
  513. // owned by serverConn's serve loop:
  514. bodyBytes int64 // body bytes seen so far
  515. declBodyBytes int64 // or -1 if undeclared
  516. flow flow // limits writing from Handler to client
  517. inflow flow // what the client is allowed to POST/etc to us
  518. parent *stream // or nil
  519. numTrailerValues int64
  520. weight uint8
  521. state streamState
  522. resetQueued bool // RST_STREAM queued for write; set by sc.resetStream
  523. gotTrailerHeader bool // HEADER frame for trailers was seen
  524. wroteHeaders bool // whether we wrote headers (not status 100)
  525. writeDeadline *time.Timer // nil if unused
  526. trailer http.Header // accumulated trailers
  527. reqTrailer http.Header // handler's Request.Trailer
  528. }
  529. func (sc *serverConn) Framer() *Framer { return sc.framer }
  530. func (sc *serverConn) CloseConn() error { return sc.conn.Close() }
  531. func (sc *serverConn) Flush() error { return sc.bw.Flush() }
  532. func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
  533. return sc.hpackEncoder, &sc.headerWriteBuf
  534. }
  535. func (sc *serverConn) state(streamID uint32) (streamState, *stream) {
  536. sc.serveG.check()
  537. // http://tools.ietf.org/html/rfc7540#section-5.1
  538. if st, ok := sc.streams[streamID]; ok {
  539. return st.state, st
  540. }
  541. // "The first use of a new stream identifier implicitly closes all
  542. // streams in the "idle" state that might have been initiated by
  543. // that peer with a lower-valued stream identifier. For example, if
  544. // a client sends a HEADERS frame on stream 7 without ever sending a
  545. // frame on stream 5, then stream 5 transitions to the "closed"
  546. // state when the first frame for stream 7 is sent or received."
  547. if streamID%2 == 1 {
  548. if streamID <= sc.maxClientStreamID {
  549. return stateClosed, nil
  550. }
  551. } else {
  552. if streamID <= sc.maxPushPromiseID {
  553. return stateClosed, nil
  554. }
  555. }
  556. return stateIdle, nil
  557. }
  558. // setConnState calls the net/http ConnState hook for this connection, if configured.
  559. // Note that the net/http package does StateNew and StateClosed for us.
  560. // There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
  561. func (sc *serverConn) setConnState(state http.ConnState) {
  562. if sc.hs.ConnState != nil {
  563. sc.hs.ConnState(sc.conn, state)
  564. }
  565. }
  566. func (sc *serverConn) vlogf(format string, args ...interface{}) {
  567. if VerboseLogs {
  568. sc.logf(format, args...)
  569. }
  570. }
  571. func (sc *serverConn) logf(format string, args ...interface{}) {
  572. if lg := sc.hs.ErrorLog; lg != nil {
  573. lg.Printf(format, args...)
  574. } else {
  575. log.Printf(format, args...)
  576. }
  577. }
  578. // errno returns v's underlying uintptr, else 0.
  579. //
  580. // TODO: remove this helper function once http2 can use build
  581. // tags. See comment in isClosedConnError.
  582. func errno(v error) uintptr {
  583. if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
  584. return uintptr(rv.Uint())
  585. }
  586. return 0
  587. }
  588. // isClosedConnError reports whether err is an error from use of a closed
  589. // network connection.
  590. func isClosedConnError(err error) bool {
  591. if err == nil {
  592. return false
  593. }
  594. // TODO: remove this string search and be more like the Windows
  595. // case below. That might involve modifying the standard library
  596. // to return better error types.
  597. str := err.Error()
  598. if strings.Contains(str, "use of closed network connection") {
  599. return true
  600. }
  601. // TODO(bradfitz): x/tools/cmd/bundle doesn't really support
  602. // build tags, so I can't make an http2_windows.go file with
  603. // Windows-specific stuff. Fix that and move this, once we
  604. // have a way to bundle this into std's net/http somehow.
  605. if runtime.GOOS == "windows" {
  606. if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
  607. if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
  608. const WSAECONNABORTED = 10053
  609. const WSAECONNRESET = 10054
  610. if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
  611. return true
  612. }
  613. }
  614. }
  615. }
  616. return false
  617. }
  618. func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
  619. if err == nil {
  620. return
  621. }
  622. if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) || err == errPrefaceTimeout {
  623. // Boring, expected errors.
  624. sc.vlogf(format, args...)
  625. } else {
  626. sc.logf(format, args...)
  627. }
  628. }
  629. func (sc *serverConn) canonicalHeader(v string) string {
  630. sc.serveG.check()
  631. buildCommonHeaderMapsOnce()
  632. cv, ok := commonCanonHeader[v]
  633. if ok {
  634. return cv
  635. }
  636. cv, ok = sc.canonHeader[v]
  637. if ok {
  638. return cv
  639. }
  640. if sc.canonHeader == nil {
  641. sc.canonHeader = make(map[string]string)
  642. }
  643. cv = http.CanonicalHeaderKey(v)
  644. sc.canonHeader[v] = cv
  645. return cv
  646. }
  647. type readFrameResult struct {
  648. f Frame // valid until readMore is called
  649. err error
  650. // readMore should be called once the consumer no longer needs or
  651. // retains f. After readMore, f is invalid and more frames can be
  652. // read.
  653. readMore func()
  654. }
  655. // readFrames is the loop that reads incoming frames.
  656. // It takes care to only read one frame at a time, blocking until the
  657. // consumer is done with the frame.
  658. // It's run on its own goroutine.
  659. func (sc *serverConn) readFrames() {
  660. gate := make(gate)
  661. gateDone := gate.Done
  662. for {
  663. f, err := sc.framer.ReadFrame()
  664. select {
  665. case sc.readFrameCh <- readFrameResult{f, err, gateDone}:
  666. case <-sc.doneServing:
  667. return
  668. }
  669. select {
  670. case <-gate:
  671. case <-sc.doneServing:
  672. return
  673. }
  674. if terminalReadFrameError(err) {
  675. return
  676. }
  677. }
  678. }
  679. // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
  680. type frameWriteResult struct {
  681. wr FrameWriteRequest // what was written (or attempted)
  682. err error // result of the writeFrame call
  683. }
  684. // writeFrameAsync runs in its own goroutine and writes a single frame
  685. // and then reports when it's done.
  686. // At most one goroutine can be running writeFrameAsync at a time per
  687. // serverConn.
  688. func (sc *serverConn) writeFrameAsync(wr FrameWriteRequest) {
  689. err := wr.write.writeFrame(sc)
  690. sc.wroteFrameCh <- frameWriteResult{wr, err}
  691. }
  692. func (sc *serverConn) closeAllStreamsOnConnClose() {
  693. sc.serveG.check()
  694. for _, st := range sc.streams {
  695. sc.closeStream(st, errClientDisconnected)
  696. }
  697. }
  698. func (sc *serverConn) stopShutdownTimer() {
  699. sc.serveG.check()
  700. if t := sc.shutdownTimer; t != nil {
  701. t.Stop()
  702. }
  703. }
  704. func (sc *serverConn) notePanic() {
  705. // Note: this is for serverConn.serve panicking, not http.Handler code.
  706. if testHookOnPanicMu != nil {
  707. testHookOnPanicMu.Lock()
  708. defer testHookOnPanicMu.Unlock()
  709. }
  710. if testHookOnPanic != nil {
  711. if e := recover(); e != nil {
  712. if testHookOnPanic(sc, e) {
  713. panic(e)
  714. }
  715. }
  716. }
  717. }
  718. func (sc *serverConn) serve() {
  719. sc.serveG.check()
  720. defer sc.notePanic()
  721. defer sc.conn.Close()
  722. defer sc.closeAllStreamsOnConnClose()
  723. defer sc.stopShutdownTimer()
  724. defer close(sc.doneServing) // unblocks handlers trying to send
  725. if VerboseLogs {
  726. sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
  727. }
  728. sc.writeFrame(FrameWriteRequest{
  729. write: writeSettings{
  730. {SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
  731. {SettingMaxConcurrentStreams, sc.advMaxStreams},
  732. {SettingMaxHeaderListSize, sc.maxHeaderListSize()},
  733. {SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())},
  734. },
  735. })
  736. sc.unackedSettings++
  737. // Each connection starts with intialWindowSize inflow tokens.
  738. // If a higher value is configured, we add more tokens.
  739. if diff := sc.srv.initialConnRecvWindowSize() - initialWindowSize; diff > 0 {
  740. sc.sendWindowUpdate(nil, int(diff))
  741. }
  742. if err := sc.readPreface(); err != nil {
  743. sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
  744. return
  745. }
  746. // Now that we've got the preface, get us out of the
  747. // "StateNew" state. We can't go directly to idle, though.
  748. // Active means we read some data and anticipate a request. We'll
  749. // do another Active when we get a HEADERS frame.
  750. sc.setConnState(http.StateActive)
  751. sc.setConnState(http.StateIdle)
  752. if sc.srv.IdleTimeout != 0 {
  753. sc.idleTimer = time.AfterFunc(sc.srv.IdleTimeout, sc.onIdleTimer)
  754. defer sc.idleTimer.Stop()
  755. }
  756. go sc.readFrames() // closed by defer sc.conn.Close above
  757. settingsTimer := time.AfterFunc(firstSettingsTimeout, sc.onSettingsTimer)
  758. defer settingsTimer.Stop()
  759. loopNum := 0
  760. for {
  761. loopNum++
  762. select {
  763. case wr := <-sc.wantWriteFrameCh:
  764. if se, ok := wr.write.(StreamError); ok {
  765. sc.resetStream(se)
  766. break
  767. }
  768. sc.writeFrame(wr)
  769. case res := <-sc.wroteFrameCh:
  770. sc.wroteFrame(res)
  771. case res := <-sc.readFrameCh:
  772. if !sc.processFrameFromReader(res) {
  773. return
  774. }
  775. res.readMore()
  776. if settingsTimer != nil {
  777. settingsTimer.Stop()
  778. settingsTimer = nil
  779. }
  780. case m := <-sc.bodyReadCh:
  781. sc.noteBodyRead(m.st, m.n)
  782. case msg := <-sc.serveMsgCh:
  783. switch v := msg.(type) {
  784. case func(int):
  785. v(loopNum) // for testing
  786. case *serverMessage:
  787. switch v {
  788. case settingsTimerMsg:
  789. sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
  790. return
  791. case idleTimerMsg:
  792. sc.vlogf("connection is idle")
  793. sc.goAway(ErrCodeNo)
  794. case shutdownTimerMsg:
  795. sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
  796. return
  797. case gracefulShutdownMsg:
  798. sc.startGracefulShutdownInternal()
  799. default:
  800. panic("unknown timer")
  801. }
  802. case *startPushRequest:
  803. sc.startPush(v)
  804. default:
  805. panic(fmt.Sprintf("unexpected type %T", v))
  806. }
  807. }
  808. // Start the shutdown timer after sending a GOAWAY. When sending GOAWAY
  809. // with no error code (graceful shutdown), don't start the timer until
  810. // all open streams have been completed.
  811. sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame
  812. gracefulShutdownComplete := sc.goAwayCode == ErrCodeNo && sc.curOpenStreams() == 0
  813. if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != ErrCodeNo || gracefulShutdownComplete) {
  814. sc.shutDownIn(goAwayTimeout)
  815. }
  816. }
  817. }
  818. func (sc *serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) {
  819. select {
  820. case <-sc.doneServing:
  821. case <-sharedCh:
  822. close(privateCh)
  823. }
  824. }
  825. type serverMessage int
  826. // Message values sent to serveMsgCh.
  827. var (
  828. settingsTimerMsg = new(serverMessage)
  829. idleTimerMsg = new(serverMessage)
  830. shutdownTimerMsg = new(serverMessage)
  831. gracefulShutdownMsg = new(serverMessage)
  832. )
  833. func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) }
  834. func (sc *serverConn) onIdleTimer() { sc.sendServeMsg(idleTimerMsg) }
  835. func (sc *serverConn) onShutdownTimer() { sc.sendServeMsg(shutdownTimerMsg) }
  836. func (sc *serverConn) sendServeMsg(msg interface{}) {
  837. sc.serveG.checkNotOn() // NOT
  838. select {
  839. case sc.serveMsgCh <- msg:
  840. case <-sc.doneServing:
  841. }
  842. }
  843. var errPrefaceTimeout = errors.New("timeout waiting for client preface")
  844. // readPreface reads the ClientPreface greeting from the peer or
  845. // returns errPrefaceTimeout on timeout, or an error if the greeting
  846. // is invalid.
  847. func (sc *serverConn) readPreface() error {
  848. errc := make(chan error, 1)
  849. go func() {
  850. // Read the client preface
  851. buf := make([]byte, len(ClientPreface))
  852. if _, err := io.ReadFull(sc.conn, buf); err != nil {
  853. errc <- err
  854. } else if !bytes.Equal(buf, clientPreface) {
  855. errc <- fmt.Errorf("bogus greeting %q", buf)
  856. } else {
  857. errc <- nil
  858. }
  859. }()
  860. timer := time.NewTimer(prefaceTimeout) // TODO: configurable on *Server?
  861. defer timer.Stop()
  862. select {
  863. case <-timer.C:
  864. return errPrefaceTimeout
  865. case err := <-errc:
  866. if err == nil {
  867. if VerboseLogs {
  868. sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
  869. }
  870. }
  871. return err
  872. }
  873. }
  874. var errChanPool = sync.Pool{
  875. New: func() interface{} { return make(chan error, 1) },
  876. }
  877. var writeDataPool = sync.Pool{
  878. New: func() interface{} { return new(writeData) },
  879. }
  880. // writeDataFromHandler writes DATA response frames from a handler on
  881. // the given stream.
  882. func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error {
  883. ch := errChanPool.Get().(chan error)
  884. writeArg := writeDataPool.Get().(*writeData)
  885. *writeArg = writeData{stream.id, data, endStream}
  886. err := sc.writeFrameFromHandler(FrameWriteRequest{
  887. write: writeArg,
  888. stream: stream,
  889. done: ch,
  890. })
  891. if err != nil {
  892. return err
  893. }
  894. var frameWriteDone bool // the frame write is done (successfully or not)
  895. select {
  896. case err = <-ch:
  897. frameWriteDone = true
  898. case <-sc.doneServing:
  899. return errClientDisconnected
  900. case <-stream.cw:
  901. // If both ch and stream.cw were ready (as might
  902. // happen on the final Write after an http.Handler
  903. // ends), prefer the write result. Otherwise this
  904. // might just be us successfully closing the stream.
  905. // The writeFrameAsync and serve goroutines guarantee
  906. // that the ch send will happen before the stream.cw
  907. // close.
  908. select {
  909. case err = <-ch:
  910. frameWriteDone = true
  911. default:
  912. return errStreamClosed
  913. }
  914. }
  915. errChanPool.Put(ch)
  916. if frameWriteDone {
  917. writeDataPool.Put(writeArg)
  918. }
  919. return err
  920. }
  921. // writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts
  922. // if the connection has gone away.
  923. //
  924. // This must not be run from the serve goroutine itself, else it might
  925. // deadlock writing to sc.wantWriteFrameCh (which is only mildly
  926. // buffered and is read by serve itself). If you're on the serve
  927. // goroutine, call writeFrame instead.
  928. func (sc *serverConn) writeFrameFromHandler(wr FrameWriteRequest) error {
  929. sc.serveG.checkNotOn() // NOT
  930. select {
  931. case sc.wantWriteFrameCh <- wr:
  932. return nil
  933. case <-sc.doneServing:
  934. // Serve loop is gone.
  935. // Client has closed their connection to the server.
  936. return errClientDisconnected
  937. }
  938. }
  939. // writeFrame schedules a frame to write and sends it if there's nothing
  940. // already being written.
  941. //
  942. // There is no pushback here (the serve goroutine never blocks). It's
  943. // the http.Handlers that block, waiting for their previous frames to
  944. // make it onto the wire
  945. //
  946. // If you're not on the serve goroutine, use writeFrameFromHandler instead.
  947. func (sc *serverConn) writeFrame(wr FrameWriteRequest) {
  948. sc.serveG.check()
  949. // If true, wr will not be written and wr.done will not be signaled.
  950. var ignoreWrite bool
  951. // We are not allowed to write frames on closed streams. RFC 7540 Section
  952. // 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on
  953. // a closed stream." Our server never sends PRIORITY, so that exception
  954. // does not apply.
  955. //
  956. // The serverConn might close an open stream while the stream's handler
  957. // is still running. For example, the server might close a stream when it
  958. // receives bad data from the client. If this happens, the handler might
  959. // attempt to write a frame after the stream has been closed (since the
  960. // handler hasn't yet been notified of the close). In this case, we simply
  961. // ignore the frame. The handler will notice that the stream is closed when
  962. // it waits for the frame to be written.
  963. //
  964. // As an exception to this rule, we allow sending RST_STREAM after close.
  965. // This allows us to immediately reject new streams without tracking any
  966. // state for those streams (except for the queued RST_STREAM frame). This
  967. // may result in duplicate RST_STREAMs in some cases, but the client should
  968. // ignore those.
  969. if wr.StreamID() != 0 {
  970. _, isReset := wr.write.(StreamError)
  971. if state, _ := sc.state(wr.StreamID()); state == stateClosed && !isReset {
  972. ignoreWrite = true
  973. }
  974. }
  975. // Don't send a 100-continue response if we've already sent headers.
  976. // See golang.org/issue/14030.
  977. switch wr.write.(type) {
  978. case *writeResHeaders:
  979. wr.stream.wroteHeaders = true
  980. case write100ContinueHeadersFrame:
  981. if wr.stream.wroteHeaders {
  982. // We do not need to notify wr.done because this frame is
  983. // never written with wr.done != nil.
  984. if wr.done != nil {
  985. panic("wr.done != nil for write100ContinueHeadersFrame")
  986. }
  987. ignoreWrite = true
  988. }
  989. }
  990. if !ignoreWrite {
  991. sc.writeSched.Push(wr)
  992. }
  993. sc.scheduleFrameWrite()
  994. }
  995. // startFrameWrite starts a goroutine to write wr (in a separate
  996. // goroutine since that might block on the network), and updates the
  997. // serve goroutine's state about the world, updated from info in wr.
  998. func (sc *serverConn) startFrameWrite(wr FrameWriteRequest) {
  999. sc.serveG.check()
  1000. if sc.writingFrame {
  1001. panic("internal error: can only be writing one frame at a time")
  1002. }
  1003. st := wr.stream
  1004. if st != nil {
  1005. switch st.state {
  1006. case stateHalfClosedLocal:
  1007. switch wr.write.(type) {
  1008. case StreamError, handlerPanicRST, writeWindowUpdate:
  1009. // RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE
  1010. // in this state. (We never send PRIORITY from the server, so that is not checked.)
  1011. default:
  1012. panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
  1013. }
  1014. case stateClosed:
  1015. panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
  1016. }
  1017. }
  1018. if wpp, ok := wr.write.(*writePushPromise); ok {
  1019. var err error
  1020. wpp.promisedID, err = wpp.allocatePromisedID()
  1021. if err != nil {
  1022. sc.writingFrameAsync = false
  1023. wr.replyToWriter(err)
  1024. return
  1025. }
  1026. }
  1027. sc.writingFrame = true
  1028. sc.needsFrameFlush = true
  1029. if wr.write.staysWithinBuffer(sc.bw.Available()) {
  1030. sc.writingFrameAsync = false
  1031. err := wr.write.writeFrame(sc)
  1032. sc.wroteFrame(frameWriteResult{wr, err})
  1033. } else {
  1034. sc.writingFrameAsync = true
  1035. go sc.writeFrameAsync(wr)
  1036. }
  1037. }
  1038. // errHandlerPanicked is the error given to any callers blocked in a read from
  1039. // Request.Body when the main goroutine panics. Since most handlers read in the
  1040. // main ServeHTTP goroutine, this will show up rarely.
  1041. var errHandlerPanicked = errors.New("http2: handler panicked")
  1042. // wroteFrame is called on the serve goroutine with the result of
  1043. // whatever happened on writeFrameAsync.
  1044. func (sc *serverConn) wroteFrame(res frameWriteResult) {
  1045. sc.serveG.check()
  1046. if !sc.writingFrame {
  1047. panic("internal error: expected to be already writing a frame")
  1048. }
  1049. sc.writingFrame = false
  1050. sc.writingFrameAsync = false
  1051. wr := res.wr
  1052. if writeEndsStream(wr.write) {
  1053. st := wr.stream
  1054. if st == nil {
  1055. panic("internal error: expecting non-nil stream")
  1056. }
  1057. switch st.state {
  1058. case stateOpen:
  1059. // Here we would go to stateHalfClosedLocal in
  1060. // theory, but since our handler is done and
  1061. // the net/http package provides no mechanism
  1062. // for closing a ResponseWriter while still
  1063. // reading data (see possible TODO at top of
  1064. // this file), we go into closed state here
  1065. // anyway, after telling the peer we're
  1066. // hanging up on them. We'll transition to
  1067. // stateClosed after the RST_STREAM frame is
  1068. // written.
  1069. st.state = stateHalfClosedLocal
  1070. // Section 8.1: a server MAY request that the client abort
  1071. // transmission of a request without error by sending a
  1072. // RST_STREAM with an error code of NO_ERROR after sending
  1073. // a complete response.
  1074. sc.resetStream(streamError(st.id, ErrCodeNo))
  1075. case stateHalfClosedRemote:
  1076. sc.closeStream(st, errHandlerComplete)
  1077. }
  1078. } else {
  1079. switch v := wr.write.(type) {
  1080. case StreamError:
  1081. // st may be unknown if the RST_STREAM was generated to reject bad input.
  1082. if st, ok := sc.streams[v.StreamID]; ok {
  1083. sc.closeStream(st, v)
  1084. }
  1085. case handlerPanicRST:
  1086. sc.closeStream(wr.stream, errHandlerPanicked)
  1087. }
  1088. }
  1089. // Reply (if requested) to unblock the ServeHTTP goroutine.
  1090. wr.replyToWriter(res.err)
  1091. sc.scheduleFrameWrite()
  1092. }
  1093. // scheduleFrameWrite tickles the frame writing scheduler.
  1094. //
  1095. // If a frame is already being written, nothing happens. This will be called again
  1096. // when the frame is done being written.
  1097. //
  1098. // If a frame isn't being written we need to send one, the best frame
  1099. // to send is selected, preferring first things that aren't
  1100. // stream-specific (e.g. ACKing settings), and then finding the
  1101. // highest priority stream.
  1102. //
  1103. // If a frame isn't being written and there's nothing else to send, we
  1104. // flush the write buffer.
  1105. func (sc *serverConn) scheduleFrameWrite() {
  1106. sc.serveG.check()
  1107. if sc.writingFrame || sc.inFrameScheduleLoop {
  1108. return
  1109. }
  1110. sc.inFrameScheduleLoop = true
  1111. for !sc.writingFrameAsync {
  1112. if sc.needToSendGoAway {
  1113. sc.needToSendGoAway = false
  1114. sc.startFrameWrite(FrameWriteRequest{
  1115. write: &writeGoAway{
  1116. maxStreamID: sc.maxClientStreamID,
  1117. code: sc.goAwayCode,
  1118. },
  1119. })
  1120. continue
  1121. }
  1122. if sc.needToSendSettingsAck {
  1123. sc.needToSendSettingsAck = false
  1124. sc.startFrameWrite(FrameWriteRequest{write: writeSettingsAck{}})
  1125. continue
  1126. }
  1127. if !sc.inGoAway || sc.goAwayCode == ErrCodeNo {
  1128. if wr, ok := sc.writeSched.Pop(); ok {
  1129. sc.startFrameWrite(wr)
  1130. continue
  1131. }
  1132. }
  1133. if sc.needsFrameFlush {
  1134. sc.startFrameWrite(FrameWriteRequest{write: flushFrameWriter{}})
  1135. sc.needsFrameFlush = false // after startFrameWrite, since it sets this true
  1136. continue
  1137. }
  1138. break
  1139. }
  1140. sc.inFrameScheduleLoop = false
  1141. }
  1142. // startGracefulShutdown gracefully shuts down a connection. This
  1143. // sends GOAWAY with ErrCodeNo to tell the client we're gracefully
  1144. // shutting down. The connection isn't closed until all current
  1145. // streams are done.
  1146. //
  1147. // startGracefulShutdown returns immediately; it does not wait until
  1148. // the connection has shut down.
  1149. func (sc *serverConn) startGracefulShutdown() {
  1150. sc.serveG.checkNotOn() // NOT
  1151. sc.shutdownOnce.Do(func() { sc.sendServeMsg(gracefulShutdownMsg) })
  1152. }
  1153. // After sending GOAWAY, the connection will close after goAwayTimeout.
  1154. // If we close the connection immediately after sending GOAWAY, there may
  1155. // be unsent data in our kernel receive buffer, which will cause the kernel
  1156. // to send a TCP RST on close() instead of a FIN. This RST will abort the
  1157. // connection immediately, whether or not the client had received the GOAWAY.
  1158. //
  1159. // Ideally we should delay for at least 1 RTT + epsilon so the client has
  1160. // a chance to read the GOAWAY and stop sending messages. Measuring RTT
  1161. // is hard, so we approximate with 1 second. See golang.org/issue/18701.
  1162. //
  1163. // This is a var so it can be shorter in tests, where all requests uses the
  1164. // loopback interface making the expected RTT very small.
  1165. //
  1166. // TODO: configurable?
  1167. var goAwayTimeout = 1 * time.Second
  1168. func (sc *serverConn) startGracefulShutdownInternal() {
  1169. sc.goAway(ErrCodeNo)
  1170. }
  1171. func (sc *serverConn) goAway(code ErrCode) {
  1172. sc.serveG.check()
  1173. if sc.inGoAway {
  1174. return
  1175. }
  1176. sc.inGoAway = true
  1177. sc.needToSendGoAway = true
  1178. sc.goAwayCode = code
  1179. sc.scheduleFrameWrite()
  1180. }
  1181. func (sc *serverConn) shutDownIn(d time.Duration) {
  1182. sc.serveG.check()
  1183. sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer)
  1184. }
  1185. func (sc *serverConn) resetStream(se StreamError) {
  1186. sc.serveG.check()
  1187. sc.writeFrame(FrameWriteRequest{write: se})
  1188. if st, ok := sc.streams[se.StreamID]; ok {
  1189. st.resetQueued = true
  1190. }
  1191. }
  1192. // processFrameFromReader processes the serve loop's read from readFrameCh from the
  1193. // frame-reading goroutine.
  1194. // processFrameFromReader returns whether the connection should be kept open.
  1195. func (sc *serverConn) processFrameFromReader(res readFrameResult) bool {
  1196. sc.serveG.check()
  1197. err := res.err
  1198. if err != nil {
  1199. if err == ErrFrameTooLarge {
  1200. sc.goAway(ErrCodeFrameSize)
  1201. return true // goAway will close the loop
  1202. }
  1203. clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err)
  1204. if clientGone {
  1205. // TODO: could we also get into this state if
  1206. // the peer does a half close
  1207. // (e.g. CloseWrite) because they're done
  1208. // sending frames but they're still wanting
  1209. // our open replies? Investigate.
  1210. // TODO: add CloseWrite to crypto/tls.Conn first
  1211. // so we have a way to test this? I suppose
  1212. // just for testing we could have a non-TLS mode.
  1213. return false
  1214. }
  1215. } else {
  1216. f := res.f
  1217. if VerboseLogs {
  1218. sc.vlogf("http2: server read frame %v", summarizeFrame(f))
  1219. }
  1220. err = sc.processFrame(f)
  1221. if err == nil {
  1222. return true
  1223. }
  1224. }
  1225. switch ev := err.(type) {
  1226. case StreamError:
  1227. sc.resetStream(ev)
  1228. return true
  1229. case goAwayFlowError:
  1230. sc.goAway(ErrCodeFlowControl)
  1231. return true
  1232. case ConnectionError:
  1233. sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
  1234. sc.goAway(ErrCode(ev))
  1235. return true // goAway will handle shutdown
  1236. default:
  1237. if res.err != nil {
  1238. sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
  1239. } else {
  1240. sc.logf("http2: server closing client connection: %v", err)
  1241. }
  1242. return false
  1243. }
  1244. }
  1245. func (sc *serverConn) processFrame(f Frame) error {
  1246. sc.serveG.check()
  1247. // First frame received must be SETTINGS.
  1248. if !sc.sawFirstSettings {
  1249. if _, ok := f.(*SettingsFrame); !ok {
  1250. return ConnectionError(ErrCodeProtocol)
  1251. }
  1252. sc.sawFirstSettings = true
  1253. }
  1254. switch f := f.(type) {
  1255. case *SettingsFrame:
  1256. return sc.processSettings(f)
  1257. case *MetaHeadersFrame:
  1258. return sc.processHeaders(f)
  1259. case *WindowUpdateFrame:
  1260. return sc.processWindowUpdate(f)
  1261. case *PingFrame:
  1262. return sc.processPing(f)
  1263. case *DataFrame:
  1264. return sc.processData(f)
  1265. case *RSTStreamFrame:
  1266. return sc.processResetStream(f)
  1267. case *PriorityFrame:
  1268. return sc.processPriority(f)
  1269. case *GoAwayFrame:
  1270. return sc.processGoAway(f)
  1271. case *PushPromiseFrame:
  1272. // A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE
  1273. // frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1274. return ConnectionError(ErrCodeProtocol)
  1275. default:
  1276. sc.vlogf("http2: server ignoring frame: %v", f.Header())
  1277. return nil
  1278. }
  1279. }
  1280. func (sc *serverConn) processPing(f *PingFrame) error {
  1281. sc.serveG.check()
  1282. if f.IsAck() {
  1283. // 6.7 PING: " An endpoint MUST NOT respond to PING frames
  1284. // containing this flag."
  1285. return nil
  1286. }
  1287. if f.StreamID != 0 {
  1288. // "PING frames are not associated with any individual
  1289. // stream. If a PING frame is received with a stream
  1290. // identifier field value other than 0x0, the recipient MUST
  1291. // respond with a connection error (Section 5.4.1) of type
  1292. // PROTOCOL_ERROR."
  1293. return ConnectionError(ErrCodeProtocol)
  1294. }
  1295. if sc.inGoAway && sc.goAwayCode != ErrCodeNo {
  1296. return nil
  1297. }
  1298. sc.writeFrame(FrameWriteRequest{write: writePingAck{f}})
  1299. return nil
  1300. }
  1301. func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error {
  1302. sc.serveG.check()
  1303. switch {
  1304. case f.StreamID != 0: // stream-level flow control
  1305. state, st := sc.state(f.StreamID)
  1306. if state == stateIdle {
  1307. // Section 5.1: "Receiving any frame other than HEADERS
  1308. // or PRIORITY on a stream in this state MUST be
  1309. // treated as a connection error (Section 5.4.1) of
  1310. // type PROTOCOL_ERROR."
  1311. return ConnectionError(ErrCodeProtocol)
  1312. }
  1313. if st == nil {
  1314. // "WINDOW_UPDATE can be sent by a peer that has sent a
  1315. // frame bearing the END_STREAM flag. This means that a
  1316. // receiver could receive a WINDOW_UPDATE frame on a "half
  1317. // closed (remote)" or "closed" stream. A receiver MUST
  1318. // NOT treat this as an error, see Section 5.1."
  1319. return nil
  1320. }
  1321. if !st.flow.add(int32(f.Increment)) {
  1322. return streamError(f.StreamID, ErrCodeFlowControl)
  1323. }
  1324. default: // connection-level flow control
  1325. if !sc.flow.add(int32(f.Increment)) {
  1326. return goAwayFlowError{}
  1327. }
  1328. }
  1329. sc.scheduleFrameWrite()
  1330. return nil
  1331. }
  1332. func (sc *serverConn) processResetStream(f *RSTStreamFrame) error {
  1333. sc.serveG.check()
  1334. state, st := sc.state(f.StreamID)
  1335. if state == stateIdle {
  1336. // 6.4 "RST_STREAM frames MUST NOT be sent for a
  1337. // stream in the "idle" state. If a RST_STREAM frame
  1338. // identifying an idle stream is received, the
  1339. // recipient MUST treat this as a connection error
  1340. // (Section 5.4.1) of type PROTOCOL_ERROR.
  1341. return ConnectionError(ErrCodeProtocol)
  1342. }
  1343. if st != nil {
  1344. st.cancelCtx()
  1345. sc.closeStream(st, streamError(f.StreamID, f.ErrCode))
  1346. }
  1347. return nil
  1348. }
  1349. func (sc *serverConn) closeStream(st *stream, err error) {
  1350. sc.serveG.check()
  1351. if st.state == stateIdle || st.state == stateClosed {
  1352. panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
  1353. }
  1354. st.state = stateClosed
  1355. if st.writeDeadline != nil {
  1356. st.writeDeadline.Stop()
  1357. }
  1358. if st.isPushed() {
  1359. sc.curPushedStreams--
  1360. } else {
  1361. sc.curClientStreams--
  1362. }
  1363. delete(sc.streams, st.id)
  1364. if len(sc.streams) == 0 {
  1365. sc.setConnState(http.StateIdle)
  1366. if sc.srv.IdleTimeout != 0 {
  1367. sc.idleTimer.Reset(sc.srv.IdleTimeout)
  1368. }
  1369. if h1ServerKeepAlivesDisabled(sc.hs) {
  1370. sc.startGracefulShutdownInternal()
  1371. }
  1372. }
  1373. if p := st.body; p != nil {
  1374. // Return any buffered unread bytes worth of conn-level flow control.
  1375. // See golang.org/issue/16481
  1376. sc.sendWindowUpdate(nil, p.Len())
  1377. p.CloseWithError(err)
  1378. }
  1379. st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc
  1380. sc.writeSched.CloseStream(st.id)
  1381. }
  1382. func (sc *serverConn) processSettings(f *SettingsFrame) error {
  1383. sc.serveG.check()
  1384. if f.IsAck() {
  1385. sc.unackedSettings--
  1386. if sc.unackedSettings < 0 {
  1387. // Why is the peer ACKing settings we never sent?
  1388. // The spec doesn't mention this case, but
  1389. // hang up on them anyway.
  1390. return ConnectionError(ErrCodeProtocol)
  1391. }
  1392. return nil
  1393. }
  1394. if f.NumSettings() > 100 || f.HasDuplicates() {
  1395. // This isn't actually in the spec, but hang up on
  1396. // suspiciously large settings frames or those with
  1397. // duplicate entries.
  1398. return ConnectionError(ErrCodeProtocol)
  1399. }
  1400. if err := f.ForeachSetting(sc.processSetting); err != nil {
  1401. return err
  1402. }
  1403. sc.needToSendSettingsAck = true
  1404. sc.scheduleFrameWrite()
  1405. return nil
  1406. }
  1407. func (sc *serverConn) processSetting(s Setting) error {
  1408. sc.serveG.check()
  1409. if err := s.Valid(); err != nil {
  1410. return err
  1411. }
  1412. if VerboseLogs {
  1413. sc.vlogf("http2: server processing setting %v", s)
  1414. }
  1415. switch s.ID {
  1416. case SettingHeaderTableSize:
  1417. sc.headerTableSize = s.Val
  1418. sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
  1419. case SettingEnablePush:
  1420. sc.pushEnabled = s.Val != 0
  1421. case SettingMaxConcurrentStreams:
  1422. sc.clientMaxStreams = s.Val
  1423. case SettingInitialWindowSize:
  1424. return sc.processSettingInitialWindowSize(s.Val)
  1425. case SettingMaxFrameSize:
  1426. sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31
  1427. case SettingMaxHeaderListSize:
  1428. sc.peerMaxHeaderListSize = s.Val
  1429. default:
  1430. // Unknown setting: "An endpoint that receives a SETTINGS
  1431. // frame with any unknown or unsupported identifier MUST
  1432. // ignore that setting."
  1433. if VerboseLogs {
  1434. sc.vlogf("http2: server ignoring unknown setting %v", s)
  1435. }
  1436. }
  1437. return nil
  1438. }
  1439. func (sc *serverConn) processSettingInitialWindowSize(val uint32) error {
  1440. sc.serveG.check()
  1441. // Note: val already validated to be within range by
  1442. // processSetting's Valid call.
  1443. // "A SETTINGS frame can alter the initial flow control window
  1444. // size for all current streams. When the value of
  1445. // SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST
  1446. // adjust the size of all stream flow control windows that it
  1447. // maintains by the difference between the new value and the
  1448. // old value."
  1449. old := sc.initialStreamSendWindowSize
  1450. sc.initialStreamSendWindowSize = int32(val)
  1451. growth := int32(val) - old // may be negative
  1452. for _, st := range sc.streams {
  1453. if !st.flow.add(growth) {
  1454. // 6.9.2 Initial Flow Control Window Size
  1455. // "An endpoint MUST treat a change to
  1456. // SETTINGS_INITIAL_WINDOW_SIZE that causes any flow
  1457. // control window to exceed the maximum size as a
  1458. // connection error (Section 5.4.1) of type
  1459. // FLOW_CONTROL_ERROR."
  1460. return ConnectionError(ErrCodeFlowControl)
  1461. }
  1462. }
  1463. return nil
  1464. }
  1465. func (sc *serverConn) processData(f *DataFrame) error {
  1466. sc.serveG.check()
  1467. if sc.inGoAway && sc.goAwayCode != ErrCodeNo {
  1468. return nil
  1469. }
  1470. data := f.Data()
  1471. // "If a DATA frame is received whose stream is not in "open"
  1472. // or "half closed (local)" state, the recipient MUST respond
  1473. // with a stream error (Section 5.4.2) of type STREAM_CLOSED."
  1474. id := f.Header().StreamID
  1475. state, st := sc.state(id)
  1476. if id == 0 || state == stateIdle {
  1477. // Section 5.1: "Receiving any frame other than HEADERS
  1478. // or PRIORITY on a stream in this state MUST be
  1479. // treated as a connection error (Section 5.4.1) of
  1480. // type PROTOCOL_ERROR."
  1481. return ConnectionError(ErrCodeProtocol)
  1482. }
  1483. if st == nil || state != stateOpen || st.gotTrailerHeader || st.resetQueued {
  1484. // This includes sending a RST_STREAM if the stream is
  1485. // in stateHalfClosedLocal (which currently means that
  1486. // the http.Handler returned, so it's done reading &
  1487. // done writing). Try to stop the client from sending
  1488. // more DATA.
  1489. // But still enforce their connection-level flow control,
  1490. // and return any flow control bytes since we're not going
  1491. // to consume them.
  1492. if sc.inflow.available() < int32(f.Length) {
  1493. return streamError(id, ErrCodeFlowControl)
  1494. }
  1495. // Deduct the flow control from inflow, since we're
  1496. // going to immediately add it back in
  1497. // sendWindowUpdate, which also schedules sending the
  1498. // frames.
  1499. sc.inflow.take(int32(f.Length))
  1500. sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
  1501. if st != nil && st.resetQueued {
  1502. // Already have a stream error in flight. Don't send another.
  1503. return nil
  1504. }
  1505. return streamError(id, ErrCodeStreamClosed)
  1506. }
  1507. if st.body == nil {
  1508. panic("internal error: should have a body in this state")
  1509. }
  1510. // Sender sending more than they'd declared?
  1511. if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
  1512. st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
  1513. // RFC 7540, sec 8.1.2.6: A request or response is also malformed if the
  1514. // value of a content-length header field does not equal the sum of the
  1515. // DATA frame payload lengths that form the body.
  1516. return streamError(id, ErrCodeProtocol)
  1517. }
  1518. if f.Length > 0 {
  1519. // Check whether the client has flow control quota.
  1520. if st.inflow.available() < int32(f.Length) {
  1521. return streamError(id, ErrCodeFlowControl)
  1522. }
  1523. st.inflow.take(int32(f.Length))
  1524. if len(data) > 0 {
  1525. wrote, err := st.body.Write(data)
  1526. if err != nil {
  1527. return streamError(id, ErrCodeStreamClosed)
  1528. }
  1529. if wrote != len(data) {
  1530. panic("internal error: bad Writer")
  1531. }
  1532. st.bodyBytes += int64(len(data))
  1533. }
  1534. // Return any padded flow control now, since we won't
  1535. // refund it later on body reads.
  1536. if pad := int32(f.Length) - int32(len(data)); pad > 0 {
  1537. sc.sendWindowUpdate32(nil, pad)
  1538. sc.sendWindowUpdate32(st, pad)
  1539. }
  1540. }
  1541. if f.StreamEnded() {
  1542. st.endStream()
  1543. }
  1544. return nil
  1545. }
  1546. func (sc *serverConn) processGoAway(f *GoAwayFrame) error {
  1547. sc.serveG.check()
  1548. if f.ErrCode != ErrCodeNo {
  1549. sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f)
  1550. } else {
  1551. sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f)
  1552. }
  1553. sc.startGracefulShutdownInternal()
  1554. // http://tools.ietf.org/html/rfc7540#section-6.8
  1555. // We should not create any new streams, which means we should disable push.
  1556. sc.pushEnabled = false
  1557. return nil
  1558. }
  1559. // isPushed reports whether the stream is server-initiated.
  1560. func (st *stream) isPushed() bool {
  1561. return st.id%2 == 0
  1562. }
  1563. // endStream closes a Request.Body's pipe. It is called when a DATA
  1564. // frame says a request body is over (or after trailers).
  1565. func (st *stream) endStream() {
  1566. sc := st.sc
  1567. sc.serveG.check()
  1568. if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
  1569. st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
  1570. st.declBodyBytes, st.bodyBytes))
  1571. } else {
  1572. st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
  1573. st.body.CloseWithError(io.EOF)
  1574. }
  1575. st.state = stateHalfClosedRemote
  1576. }
  1577. // copyTrailersToHandlerRequest is run in the Handler's goroutine in
  1578. // its Request.Body.Read just before it gets io.EOF.
  1579. func (st *stream) copyTrailersToHandlerRequest() {
  1580. for k, vv := range st.trailer {
  1581. if _, ok := st.reqTrailer[k]; ok {
  1582. // Only copy it over it was pre-declared.
  1583. st.reqTrailer[k] = vv
  1584. }
  1585. }
  1586. }
  1587. // onWriteTimeout is run on its own goroutine (from time.AfterFunc)
  1588. // when the stream's WriteTimeout has fired.
  1589. func (st *stream) onWriteTimeout() {
  1590. st.sc.writeFrameFromHandler(FrameWriteRequest{write: streamError(st.id, ErrCodeInternal)})
  1591. }
  1592. func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error {
  1593. sc.serveG.check()
  1594. id := f.StreamID
  1595. if sc.inGoAway {
  1596. // Ignore.
  1597. return nil
  1598. }
  1599. // http://tools.ietf.org/html/rfc7540#section-5.1.1
  1600. // Streams initiated by a client MUST use odd-numbered stream
  1601. // identifiers. [...] An endpoint that receives an unexpected
  1602. // stream identifier MUST respond with a connection error
  1603. // (Section 5.4.1) of type PROTOCOL_ERROR.
  1604. if id%2 != 1 {
  1605. return ConnectionError(ErrCodeProtocol)
  1606. }
  1607. // A HEADERS frame can be used to create a new stream or
  1608. // send a trailer for an open one. If we already have a stream
  1609. // open, let it process its own HEADERS frame (trailers at this
  1610. // point, if it's valid).
  1611. if st := sc.streams[f.StreamID]; st != nil {
  1612. if st.resetQueued {
  1613. // We're sending RST_STREAM to close the stream, so don't bother
  1614. // processing this frame.
  1615. return nil
  1616. }
  1617. // RFC 7540, sec 5.1: If an endpoint receives additional frames, other than
  1618. // WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
  1619. // this state, it MUST respond with a stream error (Section 5.4.2) of
  1620. // type STREAM_CLOSED.
  1621. if st.state == stateHalfClosedRemote {
  1622. return streamError(id, ErrCodeStreamClosed)
  1623. }
  1624. return st.processTrailerHeaders(f)
  1625. }
  1626. // [...] The identifier of a newly established stream MUST be
  1627. // numerically greater than all streams that the initiating
  1628. // endpoint has opened or reserved. [...] An endpoint that
  1629. // receives an unexpected stream identifier MUST respond with
  1630. // a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1631. if id <= sc.maxClientStreamID {
  1632. return ConnectionError(ErrCodeProtocol)
  1633. }
  1634. sc.maxClientStreamID = id
  1635. if sc.idleTimer != nil {
  1636. sc.idleTimer.Stop()
  1637. }
  1638. // http://tools.ietf.org/html/rfc7540#section-5.1.2
  1639. // [...] Endpoints MUST NOT exceed the limit set by their peer. An
  1640. // endpoint that receives a HEADERS frame that causes their
  1641. // advertised concurrent stream limit to be exceeded MUST treat
  1642. // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR
  1643. // or REFUSED_STREAM.
  1644. if sc.curClientStreams+1 > sc.advMaxStreams {
  1645. if sc.unackedSettings == 0 {
  1646. // They should know better.
  1647. return streamError(id, ErrCodeProtocol)
  1648. }
  1649. // Assume it's a network race, where they just haven't
  1650. // received our last SETTINGS update. But actually
  1651. // this can't happen yet, because we don't yet provide
  1652. // a way for users to adjust server parameters at
  1653. // runtime.
  1654. return streamError(id, ErrCodeRefusedStream)
  1655. }
  1656. initialState := stateOpen
  1657. if f.StreamEnded() {
  1658. initialState = stateHalfClosedRemote
  1659. }
  1660. st := sc.newStream(id, 0, initialState)
  1661. if f.HasPriority() {
  1662. if err := checkPriority(f.StreamID, f.Priority); err != nil {
  1663. return err
  1664. }
  1665. sc.writeSched.AdjustStream(st.id, f.Priority)
  1666. }
  1667. rw, req, err := sc.newWriterAndRequest(st, f)
  1668. if err != nil {
  1669. return err
  1670. }
  1671. st.reqTrailer = req.Trailer
  1672. if st.reqTrailer != nil {
  1673. st.trailer = make(http.Header)
  1674. }
  1675. st.body = req.Body.(*requestBody).pipe // may be nil
  1676. st.declBodyBytes = req.ContentLength
  1677. handler := sc.handler.ServeHTTP
  1678. if f.Truncated {
  1679. // Their header list was too long. Send a 431 error.
  1680. handler = handleHeaderListTooLong
  1681. } else if err := checkValidHTTP2RequestHeaders(req.Header); err != nil {
  1682. handler = new400Handler(err)
  1683. }
  1684. // The net/http package sets the read deadline from the
  1685. // http.Server.ReadTimeout during the TLS handshake, but then
  1686. // passes the connection off to us with the deadline already
  1687. // set. Disarm it here after the request headers are read,
  1688. // similar to how the http1 server works. Here it's
  1689. // technically more like the http1 Server's ReadHeaderTimeout
  1690. // (in Go 1.8), though. That's a more sane option anyway.
  1691. if sc.hs.ReadTimeout != 0 {
  1692. sc.conn.SetReadDeadline(time.Time{})
  1693. }
  1694. go sc.runHandler(rw, req, handler)
  1695. return nil
  1696. }
  1697. func (st *stream) processTrailerHeaders(f *MetaHeadersFrame) error {
  1698. sc := st.sc
  1699. sc.serveG.check()
  1700. if st.gotTrailerHeader {
  1701. return ConnectionError(ErrCodeProtocol)
  1702. }
  1703. st.gotTrailerHeader = true
  1704. if !f.StreamEnded() {
  1705. return streamError(st.id, ErrCodeProtocol)
  1706. }
  1707. if len(f.PseudoFields()) > 0 {
  1708. return streamError(st.id, ErrCodeProtocol)
  1709. }
  1710. if st.trailer != nil {
  1711. for _, hf := range f.RegularFields() {
  1712. key := sc.canonicalHeader(hf.Name)
  1713. if !httpguts.ValidTrailerHeader(key) {
  1714. // TODO: send more details to the peer somehow. But http2 has
  1715. // no way to send debug data at a stream level. Discuss with
  1716. // HTTP folk.
  1717. return streamError(st.id, ErrCodeProtocol)
  1718. }
  1719. st.trailer[key] = append(st.trailer[key], hf.Value)
  1720. }
  1721. }
  1722. st.endStream()
  1723. return nil
  1724. }
  1725. func checkPriority(streamID uint32, p PriorityParam) error {
  1726. if streamID == p.StreamDep {
  1727. // Section 5.3.1: "A stream cannot depend on itself. An endpoint MUST treat
  1728. // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR."
  1729. // Section 5.3.3 says that a stream can depend on one of its dependencies,
  1730. // so it's only self-dependencies that are forbidden.
  1731. return streamError(streamID, ErrCodeProtocol)
  1732. }
  1733. return nil
  1734. }
  1735. func (sc *serverConn) processPriority(f *PriorityFrame) error {
  1736. if sc.inGoAway {
  1737. return nil
  1738. }
  1739. if err := checkPriority(f.StreamID, f.PriorityParam); err != nil {
  1740. return err
  1741. }
  1742. sc.writeSched.AdjustStream(f.StreamID, f.PriorityParam)
  1743. return nil
  1744. }
  1745. func (sc *serverConn) newStream(id, pusherID uint32, state streamState) *stream {
  1746. sc.serveG.check()
  1747. if id == 0 {
  1748. panic("internal error: cannot create stream with id 0")
  1749. }
  1750. ctx, cancelCtx := context.WithCancel(sc.baseCtx)
  1751. st := &stream{
  1752. sc: sc,
  1753. id: id,
  1754. state: state,
  1755. ctx: ctx,
  1756. cancelCtx: cancelCtx,
  1757. }
  1758. st.cw.Init()
  1759. st.flow.conn = &sc.flow // link to conn-level counter
  1760. st.flow.add(sc.initialStreamSendWindowSize)
  1761. st.inflow.conn = &sc.inflow // link to conn-level counter
  1762. st.inflow.add(sc.srv.initialStreamRecvWindowSize())
  1763. if sc.hs.WriteTimeout != 0 {
  1764. st.writeDeadline = time.AfterFunc(sc.hs.WriteTimeout, st.onWriteTimeout)
  1765. }
  1766. sc.streams[id] = st
  1767. sc.writeSched.OpenStream(st.id, OpenStreamOptions{PusherID: pusherID})
  1768. if st.isPushed() {
  1769. sc.curPushedStreams++
  1770. } else {
  1771. sc.curClientStreams++
  1772. }
  1773. if sc.curOpenStreams() == 1 {
  1774. sc.setConnState(http.StateActive)
  1775. }
  1776. return st
  1777. }
  1778. func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*responseWriter, *http.Request, error) {
  1779. sc.serveG.check()
  1780. rp := requestParam{
  1781. method: f.PseudoValue("method"),
  1782. scheme: f.PseudoValue("scheme"),
  1783. authority: f.PseudoValue("authority"),
  1784. path: f.PseudoValue("path"),
  1785. }
  1786. isConnect := rp.method == "CONNECT"
  1787. if isConnect {
  1788. if rp.path != "" || rp.scheme != "" || rp.authority == "" {
  1789. return nil, nil, streamError(f.StreamID, ErrCodeProtocol)
  1790. }
  1791. } else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") {
  1792. // See 8.1.2.6 Malformed Requests and Responses:
  1793. //
  1794. // Malformed requests or responses that are detected
  1795. // MUST be treated as a stream error (Section 5.4.2)
  1796. // of type PROTOCOL_ERROR."
  1797. //
  1798. // 8.1.2.3 Request Pseudo-Header Fields
  1799. // "All HTTP/2 requests MUST include exactly one valid
  1800. // value for the :method, :scheme, and :path
  1801. // pseudo-header fields"
  1802. return nil, nil, streamError(f.StreamID, ErrCodeProtocol)
  1803. }
  1804. bodyOpen := !f.StreamEnded()
  1805. if rp.method == "HEAD" && bodyOpen {
  1806. // HEAD requests can't have bodies
  1807. return nil, nil, streamError(f.StreamID, ErrCodeProtocol)
  1808. }
  1809. rp.header = make(http.Header)
  1810. for _, hf := range f.RegularFields() {
  1811. rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value)
  1812. }
  1813. if rp.authority == "" {
  1814. rp.authority = rp.header.Get("Host")
  1815. }
  1816. rw, req, err := sc.newWriterAndRequestNoBody(st, rp)
  1817. if err != nil {
  1818. return nil, nil, err
  1819. }
  1820. if bodyOpen {
  1821. if vv, ok := rp.header["Content-Length"]; ok {
  1822. req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64)
  1823. } else {
  1824. req.ContentLength = -1
  1825. }
  1826. req.Body.(*requestBody).pipe = &pipe{
  1827. b: &dataBuffer{expected: req.ContentLength},
  1828. }
  1829. }
  1830. return rw, req, nil
  1831. }
  1832. type requestParam struct {
  1833. method string
  1834. scheme, authority, path string
  1835. header http.Header
  1836. }
  1837. func (sc *serverConn) newWriterAndRequestNoBody(st *stream, rp requestParam) (*responseWriter, *http.Request, error) {
  1838. sc.serveG.check()
  1839. var tlsState *tls.ConnectionState // nil if not scheme https
  1840. if rp.scheme == "https" {
  1841. tlsState = sc.tlsState
  1842. }
  1843. needsContinue := rp.header.Get("Expect") == "100-continue"
  1844. if needsContinue {
  1845. rp.header.Del("Expect")
  1846. }
  1847. // Merge Cookie headers into one "; "-delimited value.
  1848. if cookies := rp.header["Cookie"]; len(cookies) > 1 {
  1849. rp.header.Set("Cookie", strings.Join(cookies, "; "))
  1850. }
  1851. // Setup Trailers
  1852. var trailer http.Header
  1853. for _, v := range rp.header["Trailer"] {
  1854. for _, key := range strings.Split(v, ",") {
  1855. key = http.CanonicalHeaderKey(strings.TrimSpace(key))
  1856. switch key {
  1857. case "Transfer-Encoding", "Trailer", "Content-Length":
  1858. // Bogus. (copy of http1 rules)
  1859. // Ignore.
  1860. default:
  1861. if trailer == nil {
  1862. trailer = make(http.Header)
  1863. }
  1864. trailer[key] = nil
  1865. }
  1866. }
  1867. }
  1868. delete(rp.header, "Trailer")
  1869. var url_ *url.URL
  1870. var requestURI string
  1871. if rp.method == "CONNECT" {
  1872. url_ = &url.URL{Host: rp.authority}
  1873. requestURI = rp.authority // mimic HTTP/1 server behavior
  1874. } else {
  1875. var err error
  1876. url_, err = url.ParseRequestURI(rp.path)
  1877. if err != nil {
  1878. return nil, nil, streamError(st.id, ErrCodeProtocol)
  1879. }
  1880. requestURI = rp.path
  1881. }
  1882. body := &requestBody{
  1883. conn: sc,
  1884. stream: st,
  1885. needsContinue: needsContinue,
  1886. }
  1887. req := &http.Request{
  1888. Method: rp.method,
  1889. URL: url_,
  1890. RemoteAddr: sc.remoteAddrStr,
  1891. Header: rp.header,
  1892. RequestURI: requestURI,
  1893. Proto: "HTTP/2.0",
  1894. ProtoMajor: 2,
  1895. ProtoMinor: 0,
  1896. TLS: tlsState,
  1897. Host: rp.authority,
  1898. Body: body,
  1899. Trailer: trailer,
  1900. }
  1901. req = req.WithContext(st.ctx)
  1902. rws := responseWriterStatePool.Get().(*responseWriterState)
  1903. bwSave := rws.bw
  1904. *rws = responseWriterState{} // zero all the fields
  1905. rws.conn = sc
  1906. rws.bw = bwSave
  1907. rws.bw.Reset(chunkWriter{rws})
  1908. rws.stream = st
  1909. rws.req = req
  1910. rws.body = body
  1911. rw := &responseWriter{rws: rws}
  1912. return rw, req, nil
  1913. }
  1914. // Run on its own goroutine.
  1915. func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) {
  1916. didPanic := true
  1917. defer func() {
  1918. rw.rws.stream.cancelCtx()
  1919. if didPanic {
  1920. e := recover()
  1921. sc.writeFrameFromHandler(FrameWriteRequest{
  1922. write: handlerPanicRST{rw.rws.stream.id},
  1923. stream: rw.rws.stream,
  1924. })
  1925. // Same as net/http:
  1926. if e != nil && e != http.ErrAbortHandler {
  1927. const size = 64 << 10
  1928. buf := make([]byte, size)
  1929. buf = buf[:runtime.Stack(buf, false)]
  1930. sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
  1931. }
  1932. return
  1933. }
  1934. rw.handlerDone()
  1935. }()
  1936. handler(rw, req)
  1937. didPanic = false
  1938. }
  1939. func handleHeaderListTooLong(w http.ResponseWriter, r *http.Request) {
  1940. // 10.5.1 Limits on Header Block Size:
  1941. // .. "A server that receives a larger header block than it is
  1942. // willing to handle can send an HTTP 431 (Request Header Fields Too
  1943. // Large) status code"
  1944. const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+
  1945. w.WriteHeader(statusRequestHeaderFieldsTooLarge)
  1946. io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
  1947. }
  1948. // called from handler goroutines.
  1949. // h may be nil.
  1950. func (sc *serverConn) writeHeaders(st *stream, headerData *writeResHeaders) error {
  1951. sc.serveG.checkNotOn() // NOT on
  1952. var errc chan error
  1953. if headerData.h != nil {
  1954. // If there's a header map (which we don't own), so we have to block on
  1955. // waiting for this frame to be written, so an http.Flush mid-handler
  1956. // writes out the correct value of keys, before a handler later potentially
  1957. // mutates it.
  1958. errc = errChanPool.Get().(chan error)
  1959. }
  1960. if err := sc.writeFrameFromHandler(FrameWriteRequest{
  1961. write: headerData,
  1962. stream: st,
  1963. done: errc,
  1964. }); err != nil {
  1965. return err
  1966. }
  1967. if errc != nil {
  1968. select {
  1969. case err := <-errc:
  1970. errChanPool.Put(errc)
  1971. return err
  1972. case <-sc.doneServing:
  1973. return errClientDisconnected
  1974. case <-st.cw:
  1975. return errStreamClosed
  1976. }
  1977. }
  1978. return nil
  1979. }
  1980. // called from handler goroutines.
  1981. func (sc *serverConn) write100ContinueHeaders(st *stream) {
  1982. sc.writeFrameFromHandler(FrameWriteRequest{
  1983. write: write100ContinueHeadersFrame{st.id},
  1984. stream: st,
  1985. })
  1986. }
  1987. // A bodyReadMsg tells the server loop that the http.Handler read n
  1988. // bytes of the DATA from the client on the given stream.
  1989. type bodyReadMsg struct {
  1990. st *stream
  1991. n int
  1992. }
  1993. // called from handler goroutines.
  1994. // Notes that the handler for the given stream ID read n bytes of its body
  1995. // and schedules flow control tokens to be sent.
  1996. func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int, err error) {
  1997. sc.serveG.checkNotOn() // NOT on
  1998. if n > 0 {
  1999. select {
  2000. case sc.bodyReadCh <- bodyReadMsg{st, n}:
  2001. case <-sc.doneServing:
  2002. }
  2003. }
  2004. }
  2005. func (sc *serverConn) noteBodyRead(st *stream, n int) {
  2006. sc.serveG.check()
  2007. sc.sendWindowUpdate(nil, n) // conn-level
  2008. if st.state != stateHalfClosedRemote && st.state != stateClosed {
  2009. // Don't send this WINDOW_UPDATE if the stream is closed
  2010. // remotely.
  2011. sc.sendWindowUpdate(st, n)
  2012. }
  2013. }
  2014. // st may be nil for conn-level
  2015. func (sc *serverConn) sendWindowUpdate(st *stream, n int) {
  2016. sc.serveG.check()
  2017. // "The legal range for the increment to the flow control
  2018. // window is 1 to 2^31-1 (2,147,483,647) octets."
  2019. // A Go Read call on 64-bit machines could in theory read
  2020. // a larger Read than this. Very unlikely, but we handle it here
  2021. // rather than elsewhere for now.
  2022. const maxUint31 = 1<<31 - 1
  2023. for n >= maxUint31 {
  2024. sc.sendWindowUpdate32(st, maxUint31)
  2025. n -= maxUint31
  2026. }
  2027. sc.sendWindowUpdate32(st, int32(n))
  2028. }
  2029. // st may be nil for conn-level
  2030. func (sc *serverConn) sendWindowUpdate32(st *stream, n int32) {
  2031. sc.serveG.check()
  2032. if n == 0 {
  2033. return
  2034. }
  2035. if n < 0 {
  2036. panic("negative update")
  2037. }
  2038. var streamID uint32
  2039. if st != nil {
  2040. streamID = st.id
  2041. }
  2042. sc.writeFrame(FrameWriteRequest{
  2043. write: writeWindowUpdate{streamID: streamID, n: uint32(n)},
  2044. stream: st,
  2045. })
  2046. var ok bool
  2047. if st == nil {
  2048. ok = sc.inflow.add(n)
  2049. } else {
  2050. ok = st.inflow.add(n)
  2051. }
  2052. if !ok {
  2053. panic("internal error; sent too many window updates without decrements?")
  2054. }
  2055. }
  2056. // requestBody is the Handler's Request.Body type.
  2057. // Read and Close may be called concurrently.
  2058. type requestBody struct {
  2059. stream *stream
  2060. conn *serverConn
  2061. closed bool // for use by Close only
  2062. sawEOF bool // for use by Read only
  2063. pipe *pipe // non-nil if we have a HTTP entity message body
  2064. needsContinue bool // need to send a 100-continue
  2065. }
  2066. func (b *requestBody) Close() error {
  2067. if b.pipe != nil && !b.closed {
  2068. b.pipe.BreakWithError(errClosedBody)
  2069. }
  2070. b.closed = true
  2071. return nil
  2072. }
  2073. func (b *requestBody) Read(p []byte) (n int, err error) {
  2074. if b.needsContinue {
  2075. b.needsContinue = false
  2076. b.conn.write100ContinueHeaders(b.stream)
  2077. }
  2078. if b.pipe == nil || b.sawEOF {
  2079. return 0, io.EOF
  2080. }
  2081. n, err = b.pipe.Read(p)
  2082. if err == io.EOF {
  2083. b.sawEOF = true
  2084. }
  2085. if b.conn == nil && inTests {
  2086. return
  2087. }
  2088. b.conn.noteBodyReadFromHandler(b.stream, n, err)
  2089. return
  2090. }
  2091. // responseWriter is the http.ResponseWriter implementation. It's
  2092. // intentionally small (1 pointer wide) to minimize garbage. The
  2093. // responseWriterState pointer inside is zeroed at the end of a
  2094. // request (in handlerDone) and calls on the responseWriter thereafter
  2095. // simply crash (caller's mistake), but the much larger responseWriterState
  2096. // and buffers are reused between multiple requests.
  2097. type responseWriter struct {
  2098. rws *responseWriterState
  2099. }
  2100. // Optional http.ResponseWriter interfaces implemented.
  2101. var (
  2102. _ http.CloseNotifier = (*responseWriter)(nil)
  2103. _ http.Flusher = (*responseWriter)(nil)
  2104. _ stringWriter = (*responseWriter)(nil)
  2105. )
  2106. type responseWriterState struct {
  2107. // immutable within a request:
  2108. stream *stream
  2109. req *http.Request
  2110. body *requestBody // to close at end of request, if DATA frames didn't
  2111. conn *serverConn
  2112. // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
  2113. bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
  2114. // mutated by http.Handler goroutine:
  2115. handlerHeader http.Header // nil until called
  2116. snapHeader http.Header // snapshot of handlerHeader at WriteHeader time
  2117. trailers []string // set in writeChunk
  2118. status int // status code passed to WriteHeader
  2119. wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
  2120. sentHeader bool // have we sent the header frame?
  2121. handlerDone bool // handler has finished
  2122. dirty bool // a Write failed; don't reuse this responseWriterState
  2123. sentContentLen int64 // non-zero if handler set a Content-Length header
  2124. wroteBytes int64
  2125. closeNotifierMu sync.Mutex // guards closeNotifierCh
  2126. closeNotifierCh chan bool // nil until first used
  2127. }
  2128. type chunkWriter struct{ rws *responseWriterState }
  2129. func (cw chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) }
  2130. func (rws *responseWriterState) hasTrailers() bool { return len(rws.trailers) > 0 }
  2131. func (rws *responseWriterState) hasNonemptyTrailers() bool {
  2132. for _, trailer := range rws.trailers {
  2133. if _, ok := rws.handlerHeader[trailer]; ok {
  2134. return true
  2135. }
  2136. }
  2137. return false
  2138. }
  2139. // declareTrailer is called for each Trailer header when the
  2140. // response header is written. It notes that a header will need to be
  2141. // written in the trailers at the end of the response.
  2142. func (rws *responseWriterState) declareTrailer(k string) {
  2143. k = http.CanonicalHeaderKey(k)
  2144. if !httpguts.ValidTrailerHeader(k) {
  2145. // Forbidden by RFC 7230, section 4.1.2.
  2146. rws.conn.logf("ignoring invalid trailer %q", k)
  2147. return
  2148. }
  2149. if !strSliceContains(rws.trailers, k) {
  2150. rws.trailers = append(rws.trailers, k)
  2151. }
  2152. }
  2153. // writeChunk writes chunks from the bufio.Writer. But because
  2154. // bufio.Writer may bypass its chunking, sometimes p may be
  2155. // arbitrarily large.
  2156. //
  2157. // writeChunk is also responsible (on the first chunk) for sending the
  2158. // HEADER response.
  2159. func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
  2160. if !rws.wroteHeader {
  2161. rws.writeHeader(200)
  2162. }
  2163. isHeadResp := rws.req.Method == "HEAD"
  2164. if !rws.sentHeader {
  2165. rws.sentHeader = true
  2166. var ctype, clen string
  2167. if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
  2168. rws.snapHeader.Del("Content-Length")
  2169. clen64, err := strconv.ParseInt(clen, 10, 64)
  2170. if err == nil && clen64 >= 0 {
  2171. rws.sentContentLen = clen64
  2172. } else {
  2173. clen = ""
  2174. }
  2175. }
  2176. if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
  2177. clen = strconv.Itoa(len(p))
  2178. }
  2179. _, hasContentType := rws.snapHeader["Content-Type"]
  2180. if !hasContentType && bodyAllowedForStatus(rws.status) && len(p) > 0 {
  2181. ctype = http.DetectContentType(p)
  2182. }
  2183. var date string
  2184. if _, ok := rws.snapHeader["Date"]; !ok {
  2185. // TODO(bradfitz): be faster here, like net/http? measure.
  2186. date = time.Now().UTC().Format(http.TimeFormat)
  2187. }
  2188. for _, v := range rws.snapHeader["Trailer"] {
  2189. foreachHeaderElement(v, rws.declareTrailer)
  2190. }
  2191. // "Connection" headers aren't allowed in HTTP/2 (RFC 7540, 8.1.2.2),
  2192. // but respect "Connection" == "close" to mean sending a GOAWAY and tearing
  2193. // down the TCP connection when idle, like we do for HTTP/1.
  2194. // TODO: remove more Connection-specific header fields here, in addition
  2195. // to "Connection".
  2196. if _, ok := rws.snapHeader["Connection"]; ok {
  2197. v := rws.snapHeader.Get("Connection")
  2198. delete(rws.snapHeader, "Connection")
  2199. if v == "close" {
  2200. rws.conn.startGracefulShutdown()
  2201. }
  2202. }
  2203. endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
  2204. err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
  2205. streamID: rws.stream.id,
  2206. httpResCode: rws.status,
  2207. h: rws.snapHeader,
  2208. endStream: endStream,
  2209. contentType: ctype,
  2210. contentLength: clen,
  2211. date: date,
  2212. })
  2213. if err != nil {
  2214. rws.dirty = true
  2215. return 0, err
  2216. }
  2217. if endStream {
  2218. return 0, nil
  2219. }
  2220. }
  2221. if isHeadResp {
  2222. return len(p), nil
  2223. }
  2224. if len(p) == 0 && !rws.handlerDone {
  2225. return 0, nil
  2226. }
  2227. if rws.handlerDone {
  2228. rws.promoteUndeclaredTrailers()
  2229. }
  2230. // only send trailers if they have actually been defined by the
  2231. // server handler.
  2232. hasNonemptyTrailers := rws.hasNonemptyTrailers()
  2233. endStream := rws.handlerDone && !hasNonemptyTrailers
  2234. if len(p) > 0 || endStream {
  2235. // only send a 0 byte DATA frame if we're ending the stream.
  2236. if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
  2237. rws.dirty = true
  2238. return 0, err
  2239. }
  2240. }
  2241. if rws.handlerDone && hasNonemptyTrailers {
  2242. err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
  2243. streamID: rws.stream.id,
  2244. h: rws.handlerHeader,
  2245. trailers: rws.trailers,
  2246. endStream: true,
  2247. })
  2248. if err != nil {
  2249. rws.dirty = true
  2250. }
  2251. return len(p), err
  2252. }
  2253. return len(p), nil
  2254. }
  2255. // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
  2256. // that, if present, signals that the map entry is actually for
  2257. // the response trailers, and not the response headers. The prefix
  2258. // is stripped after the ServeHTTP call finishes and the values are
  2259. // sent in the trailers.
  2260. //
  2261. // This mechanism is intended only for trailers that are not known
  2262. // prior to the headers being written. If the set of trailers is fixed
  2263. // or known before the header is written, the normal Go trailers mechanism
  2264. // is preferred:
  2265. // https://golang.org/pkg/net/http/#ResponseWriter
  2266. // https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
  2267. const TrailerPrefix = "Trailer:"
  2268. // promoteUndeclaredTrailers permits http.Handlers to set trailers
  2269. // after the header has already been flushed. Because the Go
  2270. // ResponseWriter interface has no way to set Trailers (only the
  2271. // Header), and because we didn't want to expand the ResponseWriter
  2272. // interface, and because nobody used trailers, and because RFC 7230
  2273. // says you SHOULD (but not must) predeclare any trailers in the
  2274. // header, the official ResponseWriter rules said trailers in Go must
  2275. // be predeclared, and then we reuse the same ResponseWriter.Header()
  2276. // map to mean both Headers and Trailers. When it's time to write the
  2277. // Trailers, we pick out the fields of Headers that were declared as
  2278. // trailers. That worked for a while, until we found the first major
  2279. // user of Trailers in the wild: gRPC (using them only over http2),
  2280. // and gRPC libraries permit setting trailers mid-stream without
  2281. // predeclarnig them. So: change of plans. We still permit the old
  2282. // way, but we also permit this hack: if a Header() key begins with
  2283. // "Trailer:", the suffix of that key is a Trailer. Because ':' is an
  2284. // invalid token byte anyway, there is no ambiguity. (And it's already
  2285. // filtered out) It's mildly hacky, but not terrible.
  2286. //
  2287. // This method runs after the Handler is done and promotes any Header
  2288. // fields to be trailers.
  2289. func (rws *responseWriterState) promoteUndeclaredTrailers() {
  2290. for k, vv := range rws.handlerHeader {
  2291. if !strings.HasPrefix(k, TrailerPrefix) {
  2292. continue
  2293. }
  2294. trailerKey := strings.TrimPrefix(k, TrailerPrefix)
  2295. rws.declareTrailer(trailerKey)
  2296. rws.handlerHeader[http.CanonicalHeaderKey(trailerKey)] = vv
  2297. }
  2298. if len(rws.trailers) > 1 {
  2299. sorter := sorterPool.Get().(*sorter)
  2300. sorter.SortStrings(rws.trailers)
  2301. sorterPool.Put(sorter)
  2302. }
  2303. }
  2304. func (w *responseWriter) Flush() {
  2305. rws := w.rws
  2306. if rws == nil {
  2307. panic("Header called after Handler finished")
  2308. }
  2309. if rws.bw.Buffered() > 0 {
  2310. if err := rws.bw.Flush(); err != nil {
  2311. // Ignore the error. The frame writer already knows.
  2312. return
  2313. }
  2314. } else {
  2315. // The bufio.Writer won't call chunkWriter.Write
  2316. // (writeChunk with zero bytes, so we have to do it
  2317. // ourselves to force the HTTP response header and/or
  2318. // final DATA frame (with END_STREAM) to be sent.
  2319. rws.writeChunk(nil)
  2320. }
  2321. }
  2322. func (w *responseWriter) CloseNotify() <-chan bool {
  2323. rws := w.rws
  2324. if rws == nil {
  2325. panic("CloseNotify called after Handler finished")
  2326. }
  2327. rws.closeNotifierMu.Lock()
  2328. ch := rws.closeNotifierCh
  2329. if ch == nil {
  2330. ch = make(chan bool, 1)
  2331. rws.closeNotifierCh = ch
  2332. cw := rws.stream.cw
  2333. go func() {
  2334. cw.Wait() // wait for close
  2335. ch <- true
  2336. }()
  2337. }
  2338. rws.closeNotifierMu.Unlock()
  2339. return ch
  2340. }
  2341. func (w *responseWriter) Header() http.Header {
  2342. rws := w.rws
  2343. if rws == nil {
  2344. panic("Header called after Handler finished")
  2345. }
  2346. if rws.handlerHeader == nil {
  2347. rws.handlerHeader = make(http.Header)
  2348. }
  2349. return rws.handlerHeader
  2350. }
  2351. // checkWriteHeaderCode is a copy of net/http's checkWriteHeaderCode.
  2352. func checkWriteHeaderCode(code int) {
  2353. // Issue 22880: require valid WriteHeader status codes.
  2354. // For now we only enforce that it's three digits.
  2355. // In the future we might block things over 599 (600 and above aren't defined
  2356. // at http://httpwg.org/specs/rfc7231.html#status.codes)
  2357. // and we might block under 200 (once we have more mature 1xx support).
  2358. // But for now any three digits.
  2359. //
  2360. // We used to send "HTTP/1.1 000 0" on the wire in responses but there's
  2361. // no equivalent bogus thing we can realistically send in HTTP/2,
  2362. // so we'll consistently panic instead and help people find their bugs
  2363. // early. (We can't return an error from WriteHeader even if we wanted to.)
  2364. if code < 100 || code > 999 {
  2365. panic(fmt.Sprintf("invalid WriteHeader code %v", code))
  2366. }
  2367. }
  2368. func (w *responseWriter) WriteHeader(code int) {
  2369. rws := w.rws
  2370. if rws == nil {
  2371. panic("WriteHeader called after Handler finished")
  2372. }
  2373. rws.writeHeader(code)
  2374. }
  2375. func (rws *responseWriterState) writeHeader(code int) {
  2376. if !rws.wroteHeader {
  2377. checkWriteHeaderCode(code)
  2378. rws.wroteHeader = true
  2379. rws.status = code
  2380. if len(rws.handlerHeader) > 0 {
  2381. rws.snapHeader = cloneHeader(rws.handlerHeader)
  2382. }
  2383. }
  2384. }
  2385. func cloneHeader(h http.Header) http.Header {
  2386. h2 := make(http.Header, len(h))
  2387. for k, vv := range h {
  2388. vv2 := make([]string, len(vv))
  2389. copy(vv2, vv)
  2390. h2[k] = vv2
  2391. }
  2392. return h2
  2393. }
  2394. // The Life Of A Write is like this:
  2395. //
  2396. // * Handler calls w.Write or w.WriteString ->
  2397. // * -> rws.bw (*bufio.Writer) ->
  2398. // * (Handler might call Flush)
  2399. // * -> chunkWriter{rws}
  2400. // * -> responseWriterState.writeChunk(p []byte)
  2401. // * -> responseWriterState.writeChunk (most of the magic; see comment there)
  2402. func (w *responseWriter) Write(p []byte) (n int, err error) {
  2403. return w.write(len(p), p, "")
  2404. }
  2405. func (w *responseWriter) WriteString(s string) (n int, err error) {
  2406. return w.write(len(s), nil, s)
  2407. }
  2408. // either dataB or dataS is non-zero.
  2409. func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
  2410. rws := w.rws
  2411. if rws == nil {
  2412. panic("Write called after Handler finished")
  2413. }
  2414. if !rws.wroteHeader {
  2415. w.WriteHeader(200)
  2416. }
  2417. if !bodyAllowedForStatus(rws.status) {
  2418. return 0, http.ErrBodyNotAllowed
  2419. }
  2420. rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set
  2421. if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
  2422. // TODO: send a RST_STREAM
  2423. return 0, errors.New("http2: handler wrote more than declared Content-Length")
  2424. }
  2425. if dataB != nil {
  2426. return rws.bw.Write(dataB)
  2427. } else {
  2428. return rws.bw.WriteString(dataS)
  2429. }
  2430. }
  2431. func (w *responseWriter) handlerDone() {
  2432. rws := w.rws
  2433. dirty := rws.dirty
  2434. rws.handlerDone = true
  2435. w.Flush()
  2436. w.rws = nil
  2437. if !dirty {
  2438. // Only recycle the pool if all prior Write calls to
  2439. // the serverConn goroutine completed successfully. If
  2440. // they returned earlier due to resets from the peer
  2441. // there might still be write goroutines outstanding
  2442. // from the serverConn referencing the rws memory. See
  2443. // issue 20704.
  2444. responseWriterStatePool.Put(rws)
  2445. }
  2446. }
  2447. // Push errors.
  2448. var (
  2449. ErrRecursivePush = errors.New("http2: recursive push not allowed")
  2450. ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS")
  2451. )
  2452. var _ http.Pusher = (*responseWriter)(nil)
  2453. func (w *responseWriter) Push(target string, opts *http.PushOptions) error {
  2454. st := w.rws.stream
  2455. sc := st.sc
  2456. sc.serveG.checkNotOn()
  2457. // No recursive pushes: "PUSH_PROMISE frames MUST only be sent on a peer-initiated stream."
  2458. // http://tools.ietf.org/html/rfc7540#section-6.6
  2459. if st.isPushed() {
  2460. return ErrRecursivePush
  2461. }
  2462. if opts == nil {
  2463. opts = new(http.PushOptions)
  2464. }
  2465. // Default options.
  2466. if opts.Method == "" {
  2467. opts.Method = "GET"
  2468. }
  2469. if opts.Header == nil {
  2470. opts.Header = http.Header{}
  2471. }
  2472. wantScheme := "http"
  2473. if w.rws.req.TLS != nil {
  2474. wantScheme = "https"
  2475. }
  2476. // Validate the request.
  2477. u, err := url.Parse(target)
  2478. if err != nil {
  2479. return err
  2480. }
  2481. if u.Scheme == "" {
  2482. if !strings.HasPrefix(target, "/") {
  2483. return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target)
  2484. }
  2485. u.Scheme = wantScheme
  2486. u.Host = w.rws.req.Host
  2487. } else {
  2488. if u.Scheme != wantScheme {
  2489. return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme)
  2490. }
  2491. if u.Host == "" {
  2492. return errors.New("URL must have a host")
  2493. }
  2494. }
  2495. for k := range opts.Header {
  2496. if strings.HasPrefix(k, ":") {
  2497. return fmt.Errorf("promised request headers cannot include pseudo header %q", k)
  2498. }
  2499. // These headers are meaningful only if the request has a body,
  2500. // but PUSH_PROMISE requests cannot have a body.
  2501. // http://tools.ietf.org/html/rfc7540#section-8.2
  2502. // Also disallow Host, since the promised URL must be absolute.
  2503. switch strings.ToLower(k) {
  2504. case "content-length", "content-encoding", "trailer", "te", "expect", "host":
  2505. return fmt.Errorf("promised request headers cannot include %q", k)
  2506. }
  2507. }
  2508. if err := checkValidHTTP2RequestHeaders(opts.Header); err != nil {
  2509. return err
  2510. }
  2511. // The RFC effectively limits promised requests to GET and HEAD:
  2512. // "Promised requests MUST be cacheable [GET, HEAD, or POST], and MUST be safe [GET or HEAD]"
  2513. // http://tools.ietf.org/html/rfc7540#section-8.2
  2514. if opts.Method != "GET" && opts.Method != "HEAD" {
  2515. return fmt.Errorf("method %q must be GET or HEAD", opts.Method)
  2516. }
  2517. msg := &startPushRequest{
  2518. parent: st,
  2519. method: opts.Method,
  2520. url: u,
  2521. header: cloneHeader(opts.Header),
  2522. done: errChanPool.Get().(chan error),
  2523. }
  2524. select {
  2525. case <-sc.doneServing:
  2526. return errClientDisconnected
  2527. case <-st.cw:
  2528. return errStreamClosed
  2529. case sc.serveMsgCh <- msg:
  2530. }
  2531. select {
  2532. case <-sc.doneServing:
  2533. return errClientDisconnected
  2534. case <-st.cw:
  2535. return errStreamClosed
  2536. case err := <-msg.done:
  2537. errChanPool.Put(msg.done)
  2538. return err
  2539. }
  2540. }
  2541. type startPushRequest struct {
  2542. parent *stream
  2543. method string
  2544. url *url.URL
  2545. header http.Header
  2546. done chan error
  2547. }
  2548. func (sc *serverConn) startPush(msg *startPushRequest) {
  2549. sc.serveG.check()
  2550. // http://tools.ietf.org/html/rfc7540#section-6.6.
  2551. // PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
  2552. // is in either the "open" or "half-closed (remote)" state.
  2553. if msg.parent.state != stateOpen && msg.parent.state != stateHalfClosedRemote {
  2554. // responseWriter.Push checks that the stream is peer-initiaed.
  2555. msg.done <- errStreamClosed
  2556. return
  2557. }
  2558. // http://tools.ietf.org/html/rfc7540#section-6.6.
  2559. if !sc.pushEnabled {
  2560. msg.done <- http.ErrNotSupported
  2561. return
  2562. }
  2563. // PUSH_PROMISE frames must be sent in increasing order by stream ID, so
  2564. // we allocate an ID for the promised stream lazily, when the PUSH_PROMISE
  2565. // is written. Once the ID is allocated, we start the request handler.
  2566. allocatePromisedID := func() (uint32, error) {
  2567. sc.serveG.check()
  2568. // Check this again, just in case. Technically, we might have received
  2569. // an updated SETTINGS by the time we got around to writing this frame.
  2570. if !sc.pushEnabled {
  2571. return 0, http.ErrNotSupported
  2572. }
  2573. // http://tools.ietf.org/html/rfc7540#section-6.5.2.
  2574. if sc.curPushedStreams+1 > sc.clientMaxStreams {
  2575. return 0, ErrPushLimitReached
  2576. }
  2577. // http://tools.ietf.org/html/rfc7540#section-5.1.1.
  2578. // Streams initiated by the server MUST use even-numbered identifiers.
  2579. // A server that is unable to establish a new stream identifier can send a GOAWAY
  2580. // frame so that the client is forced to open a new connection for new streams.
  2581. if sc.maxPushPromiseID+2 >= 1<<31 {
  2582. sc.startGracefulShutdownInternal()
  2583. return 0, ErrPushLimitReached
  2584. }
  2585. sc.maxPushPromiseID += 2
  2586. promisedID := sc.maxPushPromiseID
  2587. // http://tools.ietf.org/html/rfc7540#section-8.2.
  2588. // Strictly speaking, the new stream should start in "reserved (local)", then
  2589. // transition to "half closed (remote)" after sending the initial HEADERS, but
  2590. // we start in "half closed (remote)" for simplicity.
  2591. // See further comments at the definition of stateHalfClosedRemote.
  2592. promised := sc.newStream(promisedID, msg.parent.id, stateHalfClosedRemote)
  2593. rw, req, err := sc.newWriterAndRequestNoBody(promised, requestParam{
  2594. method: msg.method,
  2595. scheme: msg.url.Scheme,
  2596. authority: msg.url.Host,
  2597. path: msg.url.RequestURI(),
  2598. header: cloneHeader(msg.header), // clone since handler runs concurrently with writing the PUSH_PROMISE
  2599. })
  2600. if err != nil {
  2601. // Should not happen, since we've already validated msg.url.
  2602. panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err))
  2603. }
  2604. go sc.runHandler(rw, req, sc.handler.ServeHTTP)
  2605. return promisedID, nil
  2606. }
  2607. sc.writeFrame(FrameWriteRequest{
  2608. write: &writePushPromise{
  2609. streamID: msg.parent.id,
  2610. method: msg.method,
  2611. url: msg.url,
  2612. h: msg.header,
  2613. allocatePromisedID: allocatePromisedID,
  2614. },
  2615. stream: msg.parent,
  2616. done: msg.done,
  2617. })
  2618. }
  2619. // foreachHeaderElement splits v according to the "#rule" construction
  2620. // in RFC 7230 section 7 and calls fn for each non-empty element.
  2621. func foreachHeaderElement(v string, fn func(string)) {
  2622. v = textproto.TrimString(v)
  2623. if v == "" {
  2624. return
  2625. }
  2626. if !strings.Contains(v, ",") {
  2627. fn(v)
  2628. return
  2629. }
  2630. for _, f := range strings.Split(v, ",") {
  2631. if f = textproto.TrimString(f); f != "" {
  2632. fn(f)
  2633. }
  2634. }
  2635. }
  2636. // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2
  2637. var connHeaders = []string{
  2638. "Connection",
  2639. "Keep-Alive",
  2640. "Proxy-Connection",
  2641. "Transfer-Encoding",
  2642. "Upgrade",
  2643. }
  2644. // checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request,
  2645. // per RFC 7540 Section 8.1.2.2.
  2646. // The returned error is reported to users.
  2647. func checkValidHTTP2RequestHeaders(h http.Header) error {
  2648. for _, k := range connHeaders {
  2649. if _, ok := h[k]; ok {
  2650. return fmt.Errorf("request header %q is not valid in HTTP/2", k)
  2651. }
  2652. }
  2653. te := h["Te"]
  2654. if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) {
  2655. return errors.New(`request header "TE" may only be "trailers" in HTTP/2`)
  2656. }
  2657. return nil
  2658. }
  2659. func new400Handler(err error) http.HandlerFunc {
  2660. return func(w http.ResponseWriter, r *http.Request) {
  2661. http.Error(w, err.Error(), http.StatusBadRequest)
  2662. }
  2663. }
  2664. // h1ServerKeepAlivesDisabled reports whether hs has its keep-alives
  2665. // disabled. See comments on h1ServerShutdownChan above for why
  2666. // the code is written this way.
  2667. func h1ServerKeepAlivesDisabled(hs *http.Server) bool {
  2668. var x interface{} = hs
  2669. type I interface {
  2670. doKeepAlives() bool
  2671. }
  2672. if hs, ok := x.(I); ok {
  2673. return !hs.doKeepAlives()
  2674. }
  2675. return false
  2676. }