server.go 70 KB

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