server.go 79 KB

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