PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/golang.org/x/net/http2/server.go

http://github.com/bradfitz/camlistore
Go | 2907 lines | 2023 code | 253 blank | 631 comment | 590 complexity | ac9e87f649da0c9928d128a24e9c1a8c MD5 | raw file
Possible License(s): CC0-1.0, MIT, BSD-3-Clause, 0BSD, MPL-2.0-no-copyleft-exception, BSD-2-Clause, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // TODO: turn off the serve goroutine when idle, so
  5. // an idle conn only has the readFrames goroutine active. (which could
  6. // also be optimized probably to pin less memory in crypto/tls). This
  7. // would involve tracking when the serve goroutine is active (atomic
  8. // int32 read/CAS probably?) and starting it up when frames arrive,
  9. // and shutting it down when all handlers exit. the occasional PING
  10. // packets could use time.AfterFunc to call sc.wakeStartServeLoop()
  11. // (which is a no-op if already running) and then queue the PING write
  12. // as normal. The serve loop would then exit in most cases (if no
  13. // Handlers running) and not be woken up again until the PING packet
  14. // returns.
  15. // TODO (maybe): add a mechanism for Handlers to going into
  16. // half-closed-local mode (rw.(io.Closer) test?) but not exit their
  17. // handler, and continue to be able to read from the
  18. // Request.Body. This would be a somewhat semantic change from HTTP/1
  19. // (or at least what we expose in net/http), so I'd probably want to
  20. // add it there too. For now, this package says that returning from
  21. // the Handler ServeHTTP function means you're both done reading and
  22. // done writing, without a way to stop just one or the other.
  23. package http2
  24. import (
  25. "bufio"
  26. "bytes"
  27. "context"
  28. "crypto/tls"
  29. "errors"
  30. "fmt"
  31. "io"
  32. "log"
  33. "math"
  34. "net"
  35. "net/http"
  36. "net/textproto"
  37. "net/url"
  38. "os"
  39. "reflect"
  40. "runtime"
  41. "strconv"
  42. "strings"
  43. "sync"
  44. "time"
  45. "golang.org/x/net/http/httpguts"
  46. "golang.org/x/net/http2/hpack"
  47. )
  48. const (
  49. prefaceTimeout = 10 * time.Second
  50. firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway
  51. handlerChunkWriteSize = 4 << 10
  52. defaultMaxStreams = 250 // TODO: make this 100 as the GFE seems to?
  53. )
  54. var (
  55. errClientDisconnected = errors.New("client disconnected")
  56. errClosedBody = errors.New("body closed by handler")
  57. errHandlerComplete = errors.New("http2: request body closed due to handler exiting")
  58. errStreamClosed = errors.New("http2: stream closed")
  59. )
  60. var responseWriterStatePool = sync.Pool{
  61. New: func() interface{} {
  62. rws := &responseWriterState{}
  63. rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize)
  64. return rws
  65. },
  66. }
  67. // Test hooks.
  68. var (
  69. testHookOnConn func()
  70. testHookGetServerConn func(*serverConn)
  71. testHookOnPanicMu *sync.Mutex // nil except in tests
  72. testHookOnPanic func(sc *serverConn, panicVal interface{}) (rePanic bool)
  73. )
  74. // Server is an HTTP/2 server.
  75. type Server struct {
  76. // MaxHandlers limits the number of http.Handler ServeHTTP goroutines
  77. // which may run at a time over all connections.
  78. // Negative or zero no limit.
  79. // TODO: implement
  80. MaxHandlers int
  81. // MaxConcurrentStreams optionally specifies the number of
  82. // concurrent streams that each client may have open at a
  83. // time. This is unrelated to the number of http.Handler goroutines
  84. // which may be active globally, which is MaxHandlers.
  85. // If zero, MaxConcurrentStreams defaults to at least 100, per
  86. // the HTTP/2 spec's recommendations.
  87. MaxConcurrentStreams uint32
  88. // MaxReadFrameSize optionally specifies the largest frame
  89. // this server is willing to read. A valid value is between
  90. // 16k and 16M, inclusive. If zero or otherwise invalid, a
  91. // default value is used.
  92. MaxReadFrameSize uint32
  93. // PermitProhibitedCipherSuites, if true, permits the use of
  94. // cipher suites prohibited by the HTTP/2 spec.
  95. PermitProhibitedCipherSuites bool
  96. // IdleTimeout specifies how long until idle clients should be
  97. // closed with a GOAWAY frame. PING frames are not considered
  98. // activity for the purposes of IdleTimeout.
  99. IdleTimeout time.Duration
  100. // MaxUploadBufferPerConnection is the size of the initial flow
  101. // control window for each connections. The HTTP/2 spec does not
  102. // allow this to be smaller than 65535 or larger than 2^32-1.
  103. // If the value is outside this range, a default value will be
  104. // used instead.
  105. MaxUploadBufferPerConnection int32
  106. // MaxUploadBufferPerStream is the size of the initial flow control
  107. // window for each stream. The HTTP/2 spec does not allow this to
  108. // be larger than 2^32-1. If the value is zero or larger than the
  109. // maximum, a default value will be used instead.
  110. MaxUploadBufferPerStream int32
  111. // NewWriteScheduler constructs a write scheduler for a connection.
  112. // If nil, a default scheduler is chosen.
  113. NewWriteScheduler func() WriteScheduler
  114. // Internal state. This is a pointer (rather than embedded directly)
  115. // so that we don't embed a Mutex in this struct, which will make the
  116. // struct non-copyable, which might break some callers.
  117. state *serverInternalState
  118. }
  119. func (s *Server) initialConnRecvWindowSize() int32 {
  120. if s.MaxUploadBufferPerConnection > initialWindowSize {
  121. return s.MaxUploadBufferPerConnection
  122. }
  123. return 1 << 20
  124. }
  125. func (s *Server) initialStreamRecvWindowSize() int32 {
  126. if s.MaxUploadBufferPerStream > 0 {
  127. return s.MaxUploadBufferPerStream
  128. }
  129. return 1 << 20
  130. }
  131. func (s *Server) maxReadFrameSize() uint32 {
  132. if v := s.MaxReadFrameSize; v >= minMaxFrameSize && v <= maxFrameSize {
  133. return v
  134. }
  135. return defaultMaxReadFrameSize
  136. }
  137. func (s *Server) maxConcurrentStreams() uint32 {
  138. if v := s.MaxConcurrentStreams; v > 0 {
  139. return v
  140. }
  141. return defaultMaxStreams
  142. }
  143. type serverInternalState struct {
  144. mu sync.Mutex
  145. activeConns map[*serverConn]struct{}
  146. }
  147. func (s *serverInternalState) registerConn(sc *serverConn) {
  148. if s == nil {
  149. return // if the Server was used without calling ConfigureServer
  150. }
  151. s.mu.Lock()
  152. s.activeConns[sc] = struct{}{}
  153. s.mu.Unlock()
  154. }
  155. func (s *serverInternalState) unregisterConn(sc *serverConn) {
  156. if s == nil {
  157. return // if the Server was used without calling ConfigureServer
  158. }
  159. s.mu.Lock()
  160. delete(s.activeConns, sc)
  161. s.mu.Unlock()
  162. }
  163. func (s *serverInternalState) startGracefulShutdown() {
  164. if s == nil {
  165. return // if the Server was used without calling ConfigureServer
  166. }
  167. s.mu.Lock()
  168. for sc := range s.activeConns {
  169. sc.startGracefulShutdown()
  170. }
  171. s.mu.Unlock()
  172. }
  173. // ConfigureServer adds HTTP/2 support to a net/http Server.
  174. //
  175. // The configuration conf may be nil.
  176. //
  177. // ConfigureServer must be called before s begins serving.
  178. func ConfigureServer(s *http.Server, conf *Server) error {
  179. if s == nil {
  180. panic("nil *http.Server")
  181. }
  182. if conf == nil {
  183. conf = new(Server)
  184. }
  185. conf.state = &serverInternalState{activeConns: make(map[*serverConn]struct{})}
  186. if h1, h2 := s, conf; h2.IdleTimeout == 0 {
  187. if h1.IdleTimeout != 0 {
  188. h2.IdleTimeout = h1.IdleTimeout
  189. } else {
  190. h2.IdleTimeout = h1.ReadTimeout
  191. }
  192. }
  193. s.RegisterOnShutdown(conf.state.startGracefulShutdown)
  194. if s.TLSConfig == nil {
  195. s.TLSConfig = new(tls.Config)
  196. } else if s.TLSConfig.CipherSuites != nil {
  197. // If they already provided a CipherSuite list, return
  198. // an error if it has a bad order or is missing
  199. // ECDHE_RSA_WITH_AES_128_GCM_SHA256 or ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
  200. haveRequired := false
  201. sawBad := false
  202. for i, cs := range s.TLSConfig.CipherSuites {
  203. switch cs {
  204. case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  205. // Alternative MTI cipher to not discourage ECDSA-only servers.
  206. // See http://golang.org/cl/30721 for further information.
  207. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  208. haveRequired = true
  209. }
  210. if isBadCipher(cs) {
  211. sawBad = true
  212. } else if sawBad {
  213. return fmt.Errorf("http2: TLSConfig.CipherSuites index %d contains an HTTP/2-approved cipher suite (%#04x), but it comes after unapproved cipher suites. With this configuration, clients that don't support previous, approved cipher suites may be given an unapproved one and reject the connection.", i, cs)
  214. }
  215. }
  216. if !haveRequired {
  217. return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher.")
  218. }
  219. }
  220. // Note: not setting MinVersion to tls.VersionTLS12,
  221. // as we don't want to interfere with HTTP/1.1 traffic
  222. // on the user's server. We enforce TLS 1.2 later once
  223. // we accept a connection. Ideally this should be done
  224. // during next-proto selection, but using TLS <1.2 with
  225. // HTTP/2 is still the client's bug.
  226. s.TLSConfig.PreferServerCipherSuites = true
  227. haveNPN := false
  228. for _, p := range s.TLSConfig.NextProtos {
  229. if p == NextProtoTLS {
  230. haveNPN = true
  231. break
  232. }
  233. }
  234. if !haveNPN {
  235. s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, NextProtoTLS)
  236. }
  237. if s.TLSNextProto == nil {
  238. s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){}
  239. }
  240. protoHandler := func(hs *http.Server, c *tls.Conn, h http.Handler) {
  241. if testHookOnConn != nil {
  242. testHookOnConn()
  243. }
  244. conf.ServeConn(c, &ServeConnOpts{
  245. Handler: h,
  246. BaseConfig: hs,
  247. })
  248. }
  249. s.TLSNextProto[NextProtoTLS] = protoHandler
  250. return nil
  251. }
  252. // ServeConnOpts are options for the Server.ServeConn method.
  253. type ServeConnOpts struct {
  254. // BaseConfig optionally sets the base configuration
  255. // for values. If nil, defaults are used.
  256. BaseConfig *http.Server
  257. // Handler specifies which handler to use for processing
  258. // requests. If nil, BaseConfig.Handler is used. If BaseConfig
  259. // or BaseConfig.Handler is nil, http.DefaultServeMux is used.
  260. Handler http.Handler
  261. }
  262. func (o *ServeConnOpts) baseConfig() *http.Server {
  263. if o != nil && o.BaseConfig != nil {
  264. return o.BaseConfig
  265. }
  266. return new(http.Server)
  267. }
  268. func (o *ServeConnOpts) handler() http.Handler {
  269. if o != nil {
  270. if o.Handler != nil {
  271. return o.Handler
  272. }
  273. if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
  274. return o.BaseConfig.Handler
  275. }
  276. }
  277. return http.DefaultServeMux
  278. }
  279. // ServeConn serves HTTP/2 requests on the provided connection and
  280. // blocks until the connection is no longer readable.
  281. //
  282. // ServeConn starts speaking HTTP/2 assuming that c has not had any
  283. // reads or writes. It writes its initial settings frame and expects
  284. // to be able to read the preface and settings frame from the
  285. // client. If c has a ConnectionState method like a *tls.Conn, the
  286. // ConnectionState is used to verify the TLS ciphersuite and to set
  287. // the Request.TLS field in Handlers.
  288. //
  289. // ServeConn does not support h2c by itself. Any h2c support must be
  290. // implemented in terms of providing a suitably-behaving net.Conn.
  291. //
  292. // The opts parameter is optional. If nil, default values are used.
  293. func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) {
  294. baseCtx, cancel := serverConnBaseContext(c, opts)
  295. defer cancel()
  296. sc := &serverConn{
  297. srv: s,
  298. hs: opts.baseConfig(),
  299. conn: c,
  300. baseCtx: baseCtx,
  301. remoteAddrStr: c.RemoteAddr().String(),
  302. bw: newBufferedWriter(c),
  303. handler: opts.handler(),
  304. streams: make(map[uint32]*stream),
  305. readFrameCh: make(chan readFrameResult),
  306. wantWriteFrameCh: make(chan FrameWriteRequest, 8),
  307. serveMsgCh: make(chan interface{}, 8),
  308. wroteFrameCh: make(chan frameWriteResult, 1), // buffered; one send in writeFrameAsync
  309. bodyReadCh: make(chan bodyReadMsg), // buffering doesn't matter either way
  310. doneServing: make(chan struct{}),
  311. clientMaxStreams: math.MaxUint32, // Section 6.5.2: "Initially, there is no limit to this value"
  312. advMaxStreams: s.maxConcurrentStreams(),
  313. initialStreamSendWindowSize: initialWindowSize,
  314. maxFrameSize: initialMaxFrameSize,
  315. headerTableSize: initialHeaderTableSize,
  316. serveG: newGoroutineLock(),
  317. pushEnabled: true,
  318. }
  319. s.state.registerConn(sc)
  320. defer s.state.unregisterConn(sc)
  321. // The net/http package sets the write deadline from the
  322. // http.Server.WriteTimeout during the TLS handshake, but then
  323. // passes the connection off to us with the deadline already set.
  324. // Write deadlines are set per stream in serverConn.newStream.
  325. // Disarm the net.Conn write deadline here.
  326. if sc.hs.WriteTimeout != 0 {
  327. sc.conn.SetWriteDeadline(time.Time{})
  328. }
  329. if s.NewWriteScheduler != nil {
  330. sc.writeSched = s.NewWriteScheduler()
  331. } else {
  332. sc.writeSched = NewRandomWriteScheduler()
  333. }
  334. // These start at the RFC-specified defaults. If there is a higher
  335. // configured value for inflow, that will be updated when we send a
  336. // WINDOW_UPDATE shortly after sending SETTINGS.
  337. sc.flow.add(initialWindowSize)
  338. sc.inflow.add(initialWindowSize)
  339. sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
  340. fr := NewFramer(sc.bw, c)
  341. fr.ReadMetaHeaders = hpack.NewDecoder(initialHeaderTableSize, nil)
  342. fr.MaxHeaderListSize = sc.maxHeaderListSize()
  343. fr.SetMaxReadFrameSize(s.maxReadFrameSize())
  344. sc.framer = fr
  345. if tc, ok := c.(connectionStater); ok {
  346. sc.tlsState = new(tls.ConnectionState)
  347. *sc.tlsState = tc.ConnectionState()
  348. // 9.2 Use of TLS Features
  349. // An implementation of HTTP/2 over TLS MUST use TLS
  350. // 1.2 or higher with the restrictions on feature set
  351. // and cipher suite described in this section. Due to
  352. // implementation limitations, it might not be
  353. // possible to fail TLS negotiation. An endpoint MUST
  354. // immediately terminate an HTTP/2 connection that
  355. // does not meet the TLS requirements described in
  356. // this section with a connection error (Section
  357. // 5.4.1) of type INADEQUATE_SECURITY.
  358. if sc.tlsState.Version < tls.VersionTLS12 {
  359. sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low")
  360. return
  361. }
  362. if sc.tlsState.ServerName == "" {
  363. // Client must use SNI, but we don't enforce that anymore,
  364. // since it was causing problems when connecting to bare IP
  365. // addresses during development.
  366. //
  367. // TODO: optionally enforce? Or enforce at the time we receive
  368. // a new request, and verify the ServerName matches the :authority?
  369. // But that precludes proxy situations, perhaps.
  370. //
  371. // So for now, do nothing here again.
  372. }
  373. if !s.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) {
  374. // "Endpoints MAY choose to generate a connection error
  375. // (Section 5.4.1) of type INADEQUATE_SECURITY if one of
  376. // the prohibited cipher suites are negotiated."
  377. //
  378. // We choose that. In my opinion, the spec is weak
  379. // here. It also says both parties must support at least
  380. // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no
  381. // excuses here. If we really must, we could allow an
  382. // "AllowInsecureWeakCiphers" option on the server later.
  383. // Let's see how it plays out first.
  384. sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
  385. return
  386. }
  387. }
  388. if hook := testHookGetServerConn; hook != nil {
  389. hook(sc)
  390. }
  391. sc.serve()
  392. }
  393. func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) {
  394. ctx, cancel = context.WithCancel(context.Background())
  395. ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
  396. if hs := opts.baseConfig(); hs != nil {
  397. ctx = context.WithValue(ctx, http.ServerContextKey, hs)
  398. }
  399. return
  400. }
  401. func (sc *serverConn) rejectConn(err ErrCode, debug string) {
  402. sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
  403. // ignoring errors. hanging up anyway.
  404. sc.framer.WriteGoAway(0, err, []byte(debug))
  405. sc.bw.Flush()
  406. sc.conn.Close()
  407. }
  408. type serverConn struct {
  409. // Immutable:
  410. srv *Server
  411. hs *http.Server
  412. conn net.Conn
  413. bw *bufferedWriter // writing to conn
  414. handler http.Handler
  415. baseCtx context.Context
  416. framer *Framer
  417. doneServing chan struct{} // closed when serverConn.serve ends
  418. readFrameCh chan readFrameResult // written by serverConn.readFrames
  419. wantWriteFrameCh chan FrameWriteRequest // from handlers -> serve
  420. wroteFrameCh chan frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes
  421. bodyReadCh chan bodyReadMsg // from handlers -> serve
  422. serveMsgCh chan interface{} // misc messages & code to send to / run on the serve loop
  423. flow flow // conn-wide (not stream-specific) outbound flow control
  424. inflow flow // conn-wide inbound flow control
  425. tlsState *tls.ConnectionState // shared by all handlers, like net/http
  426. remoteAddrStr string
  427. writeSched WriteScheduler
  428. // Everything following is owned by the serve loop; use serveG.check():
  429. serveG goroutineLock // used to verify funcs are on serve()
  430. pushEnabled bool
  431. sawFirstSettings bool // got the initial SETTINGS frame after the preface
  432. needToSendSettingsAck bool
  433. unackedSettings int // how many SETTINGS have we sent without ACKs?
  434. clientMaxStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
  435. advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
  436. curClientStreams uint32 // number of open streams initiated by the client
  437. curPushedStreams uint32 // number of open streams initiated by server push
  438. maxClientStreamID uint32 // max ever seen from client (odd), or 0 if there have been no client requests
  439. maxPushPromiseID uint32 // ID of the last push promise (even), or 0 if there have been no pushes
  440. streams map[uint32]*stream
  441. initialStreamSendWindowSize int32
  442. maxFrameSize int32
  443. headerTableSize uint32
  444. peerMaxHeaderListSize uint32 // zero means unknown (default)
  445. canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case
  446. writingFrame bool // started writing a frame (on serve goroutine or separate)
  447. writingFrameAsync bool // started a frame on its own goroutine but haven't heard back on wroteFrameCh
  448. needsFrameFlush bool // last frame write wasn't a flush
  449. inGoAway bool // we've started to or sent GOAWAY
  450. inFrameScheduleLoop bool // whether we're in the scheduleFrameWrite loop
  451. needToSendGoAway bool // we need to schedule a GOAWAY frame write
  452. goAwayCode ErrCode
  453. shutdownTimer *time.Timer // nil until used
  454. idleTimer *time.Timer // nil if unused
  455. // Owned by the writeFrameAsync goroutine:
  456. headerWriteBuf bytes.Buffer
  457. hpackEncoder *hpack.Encoder
  458. // Used by startGracefulShutdown.
  459. shutdownOnce sync.Once
  460. }
  461. func (sc *serverConn) maxHeaderListSize() uint32 {
  462. n := sc.hs.MaxHeaderBytes
  463. if n <= 0 {
  464. n = http.DefaultMaxHeaderBytes
  465. }
  466. // http2's count is in a slightly different unit and includes 32 bytes per pair.
  467. // So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  468. const perFieldOverhead = 32 // per http2 spec
  469. const typicalHeaders = 10 // conservative
  470. return uint32(n + typicalHeaders*perFieldOverhead)
  471. }
  472. func (sc *serverConn) curOpenStreams() uint32 {
  473. sc.serveG.check()
  474. return sc.curClientStreams + sc.curPushedStreams
  475. }
  476. // stream represents a stream. This is the minimal metadata needed by
  477. // the serve goroutine. Most of the actual stream state is owned by
  478. // the http.Handler's goroutine in the responseWriter. Because the
  479. // responseWriter's responseWriterState is recycled at the end of a
  480. // handler, this struct intentionally has no pointer to the
  481. // *responseWriter{,State} itself, as the Handler ending nils out the
  482. // responseWriter's state field.
  483. type stream struct {
  484. // immutable:
  485. sc *serverConn
  486. id uint32
  487. body *pipe // non-nil if expecting DATA frames
  488. cw closeWaiter // closed wait stream transitions to closed state
  489. ctx context.Context
  490. cancelCtx func()
  491. // owned by serverConn's serve loop:
  492. bodyBytes int64 // body bytes seen so far
  493. declBodyBytes int64 // or -1 if undeclared
  494. flow flow // limits writing from Handler to client
  495. inflow flow // what the client is allowed to POST/etc to us
  496. parent *stream // or nil
  497. numTrailerValues int64
  498. weight uint8
  499. state streamState
  500. resetQueued bool // RST_STREAM queued for write; set by sc.resetStream
  501. gotTrailerHeader bool // HEADER frame for trailers was seen
  502. wroteHeaders bool // whether we wrote headers (not status 100)
  503. writeDeadline *time.Timer // nil if unused
  504. trailer http.Header // accumulated trailers
  505. reqTrailer http.Header // handler's Request.Trailer
  506. }
  507. func (sc *serverConn) Framer() *Framer { return sc.framer }
  508. func (sc *serverConn) CloseConn() error { return sc.conn.Close() }
  509. func (sc *serverConn) Flush() error { return sc.bw.Flush() }
  510. func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
  511. return sc.hpackEncoder, &sc.headerWriteBuf
  512. }
  513. func (sc *serverConn) state(streamID uint32) (streamState, *stream) {
  514. sc.serveG.check()
  515. // http://tools.ietf.org/html/rfc7540#section-5.1
  516. if st, ok := sc.streams[streamID]; ok {
  517. return st.state, st
  518. }
  519. // "The first use of a new stream identifier implicitly closes all
  520. // streams in the "idle" state that might have been initiated by
  521. // that peer with a lower-valued stream identifier. For example, if
  522. // a client sends a HEADERS frame on stream 7 without ever sending a
  523. // frame on stream 5, then stream 5 transitions to the "closed"
  524. // state when the first frame for stream 7 is sent or received."
  525. if streamID%2 == 1 {
  526. if streamID <= sc.maxClientStreamID {
  527. return stateClosed, nil
  528. }
  529. } else {
  530. if streamID <= sc.maxPushPromiseID {
  531. return stateClosed, nil
  532. }
  533. }
  534. return stateIdle, nil
  535. }
  536. // setConnState calls the net/http ConnState hook for this connection, if configured.
  537. // Note that the net/http package does StateNew and StateClosed for us.
  538. // There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
  539. func (sc *serverConn) setConnState(state http.ConnState) {
  540. if sc.hs.ConnState != nil {
  541. sc.hs.ConnState(sc.conn, state)
  542. }
  543. }
  544. func (sc *serverConn) vlogf(format string, args ...interface{}) {
  545. if VerboseLogs {
  546. sc.logf(format, args...)
  547. }
  548. }
  549. func (sc *serverConn) logf(format string, args ...interface{}) {
  550. if lg := sc.hs.ErrorLog; lg != nil {
  551. lg.Printf(format, args...)
  552. } else {
  553. log.Printf(format, args...)
  554. }
  555. }
  556. // errno returns v's underlying uintptr, else 0.
  557. //
  558. // TODO: remove this helper function once http2 can use build
  559. // tags. See comment in isClosedConnError.
  560. func errno(v error) uintptr {
  561. if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
  562. return uintptr(rv.Uint())
  563. }
  564. return 0
  565. }
  566. // isClosedConnError reports whether err is an error from use of a closed
  567. // network connection.
  568. func isClosedConnError(err error) bool {
  569. if err == nil {
  570. return false
  571. }
  572. // TODO: remove this string search and be more like the Windows
  573. // case below. That might involve modifying the standard library
  574. // to return better error types.
  575. str := err.Error()
  576. if strings.Contains(str, "use of closed network connection") {
  577. return true
  578. }
  579. // TODO(bradfitz): x/tools/cmd/bundle doesn't really support
  580. // build tags, so I can't make an http2_windows.go file with
  581. // Windows-specific stuff. Fix that and move this, once we
  582. // have a way to bundle this into std's net/http somehow.
  583. if runtime.GOOS == "windows" {
  584. if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
  585. if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
  586. const WSAECONNABORTED = 10053
  587. const WSAECONNRESET = 10054
  588. if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
  589. return true
  590. }
  591. }
  592. }
  593. }
  594. return false
  595. }
  596. func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
  597. if err == nil {
  598. return
  599. }
  600. if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) || err == errPrefaceTimeout {
  601. // Boring, expected errors.
  602. sc.vlogf(format, args...)
  603. } else {
  604. sc.logf(format, args...)
  605. }
  606. }
  607. func (sc *serverConn) canonicalHeader(v string) string {
  608. sc.serveG.check()
  609. buildCommonHeaderMapsOnce()
  610. cv, ok := commonCanonHeader[v]
  611. if ok {
  612. return cv
  613. }
  614. cv, ok = sc.canonHeader[v]
  615. if ok {
  616. return cv
  617. }
  618. if sc.canonHeader == nil {
  619. sc.canonHeader = make(map[string]string)
  620. }
  621. cv = http.CanonicalHeaderKey(v)
  622. sc.canonHeader[v] = cv
  623. return cv
  624. }
  625. type readFrameResult struct {
  626. f Frame // valid until readMore is called
  627. err error
  628. // readMore should be called once the consumer no longer needs or
  629. // retains f. After readMore, f is invalid and more frames can be
  630. // read.
  631. readMore func()
  632. }
  633. // readFrames is the loop that reads incoming frames.
  634. // It takes care to only read one frame at a time, blocking until the
  635. // consumer is done with the frame.
  636. // It's run on its own goroutine.
  637. func (sc *serverConn) readFrames() {
  638. gate := make(gate)
  639. gateDone := gate.Done
  640. for {
  641. f, err := sc.framer.ReadFrame()
  642. select {
  643. case sc.readFrameCh <- readFrameResult{f, err, gateDone}:
  644. case <-sc.doneServing:
  645. return
  646. }
  647. select {
  648. case <-gate:
  649. case <-sc.doneServing:
  650. return
  651. }
  652. if terminalReadFrameError(err) {
  653. return
  654. }
  655. }
  656. }
  657. // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
  658. type frameWriteResult struct {
  659. wr FrameWriteRequest // what was written (or attempted)
  660. err error // result of the writeFrame call
  661. }
  662. // writeFrameAsync runs in its own goroutine and writes a single frame
  663. // and then reports when it's done.
  664. // At most one goroutine can be running writeFrameAsync at a time per
  665. // serverConn.
  666. func (sc *serverConn) writeFrameAsync(wr FrameWriteRequest) {
  667. err := wr.write.writeFrame(sc)
  668. sc.wroteFrameCh <- frameWriteResult{wr, err}
  669. }
  670. func (sc *serverConn) closeAllStreamsOnConnClose() {
  671. sc.serveG.check()
  672. for _, st := range sc.streams {
  673. sc.closeStream(st, errClientDisconnected)
  674. }
  675. }
  676. func (sc *serverConn) stopShutdownTimer() {
  677. sc.serveG.check()
  678. if t := sc.shutdownTimer; t != nil {
  679. t.Stop()
  680. }
  681. }
  682. func (sc *serverConn) notePanic() {
  683. // Note: this is for serverConn.serve panicking, not http.Handler code.
  684. if testHookOnPanicMu != nil {
  685. testHookOnPanicMu.Lock()
  686. defer testHookOnPanicMu.Unlock()
  687. }
  688. if testHookOnPanic != nil {
  689. if e := recover(); e != nil {
  690. if testHookOnPanic(sc, e) {
  691. panic(e)
  692. }
  693. }
  694. }
  695. }
  696. func (sc *serverConn) serve() {
  697. sc.serveG.check()
  698. defer sc.notePanic()
  699. defer sc.conn.Close()
  700. defer sc.closeAllStreamsOnConnClose()
  701. defer sc.stopShutdownTimer()
  702. defer close(sc.doneServing) // unblocks handlers trying to send
  703. if VerboseLogs {
  704. sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
  705. }
  706. sc.writeFrame(FrameWriteRequest{
  707. write: writeSettings{
  708. {SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
  709. {SettingMaxConcurrentStreams, sc.advMaxStreams},
  710. {SettingMaxHeaderListSize, sc.maxHeaderListSize()},
  711. {SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())},
  712. },
  713. })
  714. sc.unackedSettings++
  715. // Each connection starts with intialWindowSize inflow tokens.
  716. // If a higher value is configured, we add more tokens.
  717. if diff := sc.srv.initialConnRecvWindowSize() - initialWindowSize; diff > 0 {
  718. sc.sendWindowUpdate(nil, int(diff))
  719. }
  720. if err := sc.readPreface(); err != nil {
  721. sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
  722. return
  723. }
  724. // Now that we've got the preface, get us out of the
  725. // "StateNew" state. We can't go directly to idle, though.
  726. // Active means we read some data and anticipate a request. We'll
  727. // do another Active when we get a HEADERS frame.
  728. sc.setConnState(http.StateActive)
  729. sc.setConnState(http.StateIdle)
  730. if sc.srv.IdleTimeout != 0 {
  731. sc.idleTimer = time.AfterFunc(sc.srv.IdleTimeout, sc.onIdleTimer)
  732. defer sc.idleTimer.Stop()
  733. }
  734. go sc.readFrames() // closed by defer sc.conn.Close above
  735. settingsTimer := time.AfterFunc(firstSettingsTimeout, sc.onSettingsTimer)
  736. defer settingsTimer.Stop()
  737. loopNum := 0
  738. for {
  739. loopNum++
  740. select {
  741. case wr := <-sc.wantWriteFrameCh:
  742. if se, ok := wr.write.(StreamError); ok {
  743. sc.resetStream(se)
  744. break
  745. }
  746. sc.writeFrame(wr)
  747. case res := <-sc.wroteFrameCh:
  748. sc.wroteFrame(res)
  749. case res := <-sc.readFrameCh:
  750. if !sc.processFrameFromReader(res) {
  751. return
  752. }
  753. res.readMore()
  754. if settingsTimer != nil {
  755. settingsTimer.Stop()
  756. settingsTimer = nil
  757. }
  758. case m := <-sc.bodyReadCh:
  759. sc.noteBodyRead(m.st, m.n)
  760. case msg := <-sc.serveMsgCh:
  761. switch v := msg.(type) {
  762. case func(int):
  763. v(loopNum) // for testing
  764. case *serverMessage:
  765. switch v {
  766. case settingsTimerMsg:
  767. sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
  768. return
  769. case idleTimerMsg:
  770. sc.vlogf("connection is idle")
  771. sc.goAway(ErrCodeNo)
  772. case shutdownTimerMsg:
  773. sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
  774. return
  775. case gracefulShutdownMsg:
  776. sc.startGracefulShutdownInternal()
  777. default:
  778. panic("unknown timer")
  779. }
  780. case *startPushRequest:
  781. sc.startPush(v)
  782. default:
  783. panic(fmt.Sprintf("unexpected type %T", v))
  784. }
  785. }
  786. // Start the shutdown timer after sending a GOAWAY. When sending GOAWAY
  787. // with no error code (graceful shutdown), don't start the timer until
  788. // all open streams have been completed.
  789. sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame
  790. gracefulShutdownComplete := sc.goAwayCode == ErrCodeNo && sc.curOpenStreams() == 0
  791. if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != ErrCodeNo || gracefulShutdownComplete) {
  792. sc.shutDownIn(goAwayTimeout)
  793. }
  794. }
  795. }
  796. func (sc *serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) {
  797. select {
  798. case <-sc.doneServing:
  799. case <-sharedCh:
  800. close(privateCh)
  801. }
  802. }
  803. type serverMessage int
  804. // Message values sent to serveMsgCh.
  805. var (
  806. settingsTimerMsg = new(serverMessage)
  807. idleTimerMsg = new(serverMessage)
  808. shutdownTimerMsg = new(serverMessage)
  809. gracefulShutdownMsg = new(serverMessage)
  810. )
  811. func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) }
  812. func (sc *serverConn) onIdleTimer() { sc.sendServeMsg(idleTimerMsg) }
  813. func (sc *serverConn) onShutdownTimer() { sc.sendServeMsg(shutdownTimerMsg) }
  814. func (sc *serverConn) sendServeMsg(msg interface{}) {
  815. sc.serveG.checkNotOn() // NOT
  816. select {
  817. case sc.serveMsgCh <- msg:
  818. case <-sc.doneServing:
  819. }
  820. }
  821. var errPrefaceTimeout = errors.New("timeout waiting for client preface")
  822. // readPreface reads the ClientPreface greeting from the peer or
  823. // returns errPrefaceTimeout on timeout, or an error if the greeting
  824. // is invalid.
  825. func (sc *serverConn) readPreface() error {
  826. errc := make(chan error, 1)
  827. go func() {
  828. // Read the client preface
  829. buf := make([]byte, len(ClientPreface))
  830. if _, err := io.ReadFull(sc.conn, buf); err != nil {
  831. errc <- err
  832. } else if !bytes.Equal(buf, clientPreface) {
  833. errc <- fmt.Errorf("bogus greeting %q", buf)
  834. } else {
  835. errc <- nil
  836. }
  837. }()
  838. timer := time.NewTimer(prefaceTimeout) // TODO: configurable on *Server?
  839. defer timer.Stop()
  840. select {
  841. case <-timer.C:
  842. return errPrefaceTimeout
  843. case err := <-errc:
  844. if err == nil {
  845. if VerboseLogs {
  846. sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
  847. }
  848. }
  849. return err
  850. }
  851. }
  852. var errChanPool = sync.Pool{
  853. New: func() interface{} { return make(chan error, 1) },
  854. }
  855. var writeDataPool = sync.Pool{
  856. New: func() interface{} { return new(writeData) },
  857. }
  858. // writeDataFromHandler writes DATA response frames from a handler on
  859. // the given stream.
  860. func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error {
  861. ch := errChanPool.Get().(chan error)
  862. writeArg := writeDataPool.Get().(*writeData)
  863. *writeArg = writeData{stream.id, data, endStream}
  864. err := sc.writeFrameFromHandler(FrameWriteRequest{
  865. write: writeArg,
  866. stream: stream,
  867. done: ch,
  868. })
  869. if err != nil {
  870. return err
  871. }
  872. var frameWriteDone bool // the frame write is done (successfully or not)
  873. select {
  874. case err = <-ch:
  875. frameWriteDone = true
  876. case <-sc.doneServing:
  877. return errClientDisconnected
  878. case <-stream.cw:
  879. // If both ch and stream.cw were ready (as might
  880. // happen on the final Write after an http.Handler
  881. // ends), prefer the write result. Otherwise this
  882. // might just be us successfully closing the stream.
  883. // The writeFrameAsync and serve goroutines guarantee
  884. // that the ch send will happen before the stream.cw
  885. // close.
  886. select {
  887. case err = <-ch:
  888. frameWriteDone = true
  889. default:
  890. return errStreamClosed
  891. }
  892. }
  893. errChanPool.Put(ch)
  894. if frameWriteDone {
  895. writeDataPool.Put(writeArg)
  896. }
  897. return err
  898. }
  899. // writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts
  900. // if the connection has gone away.
  901. //
  902. // This must not be run from the serve goroutine itself, else it might
  903. // deadlock writing to sc.wantWriteFrameCh (which is only mildly
  904. // buffered and is read by serve itself). If you're on the serve
  905. // goroutine, call writeFrame instead.
  906. func (sc *serverConn) writeFrameFromHandler(wr FrameWriteRequest) error {
  907. sc.serveG.checkNotOn() // NOT
  908. select {
  909. case sc.wantWriteFrameCh <- wr:
  910. return nil
  911. case <-sc.doneServing:
  912. // Serve loop is gone.
  913. // Client has closed their connection to the server.
  914. return errClientDisconnected
  915. }
  916. }
  917. // writeFrame schedules a frame to write and sends it if there's nothing
  918. // already being written.
  919. //
  920. // There is no pushback here (the serve goroutine never blocks). It's
  921. // the http.Handlers that block, waiting for their previous frames to
  922. // make it onto the wire
  923. //
  924. // If you're not on the serve goroutine, use writeFrameFromHandler instead.
  925. func (sc *serverConn) writeFrame(wr FrameWriteRequest) {
  926. sc.serveG.check()
  927. // If true, wr will not be written and wr.done will not be signaled.
  928. var ignoreWrite bool
  929. // We are not allowed to write frames on closed streams. RFC 7540 Section
  930. // 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on
  931. // a closed stream." Our server never sends PRIORITY, so that exception
  932. // does not apply.
  933. //
  934. // The serverConn might close an open stream while the stream's handler
  935. // is still running. For example, the server might close a stream when it
  936. // receives bad data from the client. If this happens, the handler might
  937. // attempt to write a frame after the stream has been closed (since the
  938. // handler hasn't yet been notified of the close). In this case, we simply
  939. // ignore the frame. The handler will notice that the stream is closed when
  940. // it waits for the frame to be written.
  941. //
  942. // As an exception to this rule, we allow sending RST_STREAM after close.
  943. // This allows us to immediately reject new streams without tracking any
  944. // state for those streams (except for the queued RST_STREAM frame). This
  945. // may result in duplicate RST_STREAMs in some cases, but the client should
  946. // ignore those.
  947. if wr.StreamID() != 0 {
  948. _, isReset := wr.write.(StreamError)
  949. if state, _ := sc.state(wr.StreamID()); state == stateClosed && !isReset {
  950. ignoreWrite = true
  951. }
  952. }
  953. // Don't send a 100-continue response if we've already sent headers.
  954. // See golang.org/issue/14030.
  955. switch wr.write.(type) {
  956. case *writeResHeaders:
  957. wr.stream.wroteHeaders = true
  958. case write100ContinueHeadersFrame:
  959. if wr.stream.wroteHeaders {
  960. // We do not need to notify wr.done because this frame is
  961. // never written with wr.done != nil.
  962. if wr.done != nil {
  963. panic("wr.done != nil for write100ContinueHeadersFrame")
  964. }
  965. ignoreWrite = true
  966. }
  967. }
  968. if !ignoreWrite {
  969. sc.writeSched.Push(wr)
  970. }
  971. sc.scheduleFrameWrite()
  972. }
  973. // startFrameWrite starts a goroutine to write wr (in a separate
  974. // goroutine since that might block on the network), and updates the
  975. // serve goroutine's state about the world, updated from info in wr.
  976. func (sc *serverConn) startFrameWrite(wr FrameWriteRequest) {
  977. sc.serveG.check()
  978. if sc.writingFrame {
  979. panic("internal error: can only be writing one frame at a time")
  980. }
  981. st := wr.stream
  982. if st != nil {
  983. switch st.state {
  984. case stateHalfClosedLocal:
  985. switch wr.write.(type) {
  986. case StreamError, handlerPanicRST, writeWindowUpdate:
  987. // RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE
  988. // in this state. (We never send PRIORITY from the server, so that is not checked.)
  989. default:
  990. panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
  991. }
  992. case stateClosed:
  993. panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
  994. }
  995. }
  996. if wpp, ok := wr.write.(*writePushPromise); ok {
  997. var err error
  998. wpp.promisedID, err = wpp.allocatePromisedID()
  999. if err != nil {
  1000. sc.writingFrameAsync = false
  1001. wr.replyToWriter(err)
  1002. return
  1003. }
  1004. }
  1005. sc.writingFrame = true
  1006. sc.needsFrameFlush = true
  1007. if wr.write.staysWithinBuffer(sc.bw.Available()) {
  1008. sc.writingFrameAsync = false
  1009. err := wr.write.writeFrame(sc)
  1010. sc.wroteFrame(frameWriteResult{wr, err})
  1011. } else {
  1012. sc.writingFrameAsync = true
  1013. go sc.writeFrameAsync(wr)
  1014. }
  1015. }
  1016. // errHandlerPanicked is the error given to any callers blocked in a read from
  1017. // Request.Body when the main goroutine panics. Since most handlers read in the
  1018. // main ServeHTTP goroutine, this will show up rarely.
  1019. var errHandlerPanicked = errors.New("http2: handler panicked")
  1020. // wroteFrame is called on the serve goroutine with the result of
  1021. // whatever happened on writeFrameAsync.
  1022. func (sc *serverConn) wroteFrame(res frameWriteResult) {
  1023. sc.serveG.check()
  1024. if !sc.writingFrame {
  1025. panic("internal error: expected to be already writing a frame")
  1026. }
  1027. sc.writingFrame = false
  1028. sc.writingFrameAsync = false
  1029. wr := res.wr
  1030. if writeEndsStream(wr.write) {
  1031. st := wr.stream
  1032. if st == nil {
  1033. panic("internal error: expecting non-nil stream")
  1034. }
  1035. switch st.state {
  1036. case stateOpen:
  1037. // Here we would go to stateHalfClosedLocal in
  1038. // theory, but since our handler is done and
  1039. // the net/http package provides no mechanism
  1040. // for closing a ResponseWriter while still
  1041. // reading data (see possible TODO at top of
  1042. // this file), we go into closed state here
  1043. // anyway, after telling the peer we're
  1044. // hanging up on them. We'll transition to
  1045. // stateClosed after the RST_STREAM frame is
  1046. // written.
  1047. st.state = stateHalfClosedLocal
  1048. // Section 8.1: a server MAY request that the client abort
  1049. // transmission of a request without error by sending a
  1050. // RST_STREAM with an error code of NO_ERROR after sending
  1051. // a complete response.
  1052. sc.resetStream(streamError(st.id, ErrCodeNo))
  1053. case stateHalfClosedRemote:
  1054. sc.closeStream(st, errHandlerComplete)
  1055. }
  1056. } else {
  1057. switch v := wr.write.(type) {
  1058. case StreamError:
  1059. // st may be unknown if the RST_STREAM was generated to reject bad input.
  1060. if st, ok := sc.streams[v.StreamID]; ok {
  1061. sc.closeStream(st, v)
  1062. }
  1063. case handlerPanicRST:
  1064. sc.closeStream(wr.stream, errHandlerPanicked)
  1065. }
  1066. }
  1067. // Reply (if requested) to unblock the ServeHTTP goroutine.
  1068. wr.replyToWriter(res.err)
  1069. sc.scheduleFrameWrite()
  1070. }
  1071. // scheduleFrameWrite tickles the frame writing scheduler.
  1072. //
  1073. // If a frame is already being written, nothing happens. This will be called again
  1074. // when the frame is done being written.
  1075. //
  1076. // If a frame isn't being written we need to send one, the best frame
  1077. // to send is selected, preferring first things that aren't
  1078. // stream-specific (e.g. ACKing settings), and then finding the
  1079. // highest priority stream.
  1080. //
  1081. // If a frame isn't being written and there's nothing else to send, we
  1082. // flush the write buffer.
  1083. func (sc *serverConn) scheduleFrameWrite() {
  1084. sc.serveG.check()
  1085. if sc.writingFrame || sc.inFrameScheduleLoop {
  1086. return
  1087. }
  1088. sc.inFrameScheduleLoop = true
  1089. for !sc.writingFrameAsync {
  1090. if sc.needToSendGoAway {
  1091. sc.needToSendGoAway = false
  1092. sc.startFrameWrite(FrameWriteRequest{
  1093. write: &writeGoAway{
  1094. maxStreamID: sc.maxClientStreamID,
  1095. code: sc.goAwayCode,
  1096. },
  1097. })
  1098. continue
  1099. }
  1100. if sc.needToSendSettingsAck {
  1101. sc.needToSendSettingsAck = false
  1102. sc.startFrameWrite(FrameWriteRequest{write: writeSettingsAck{}})
  1103. continue
  1104. }
  1105. if !sc.inGoAway || sc.goAwayCode == ErrCodeNo {
  1106. if wr, ok := sc.writeSched.Pop(); ok {
  1107. sc.startFrameWrite(wr)
  1108. continue
  1109. }
  1110. }
  1111. if sc.needsFrameFlush {
  1112. sc.startFrameWrite(FrameWriteRequest{write: flushFrameWriter{}})
  1113. sc.needsFrameFlush = false // after startFrameWrite, since it sets this true
  1114. continue
  1115. }
  1116. break
  1117. }
  1118. sc.inFrameScheduleLoop = false
  1119. }
  1120. // startGracefulShutdown gracefully shuts down a connection. This
  1121. // sends GOAWAY with ErrCodeNo to tell the client we're gracefully
  1122. // shutting down. The connection isn't closed until all current
  1123. // streams are done.
  1124. //
  1125. // startGracefulShutdown returns immediately; it does not wait until
  1126. // the connection has shut down.
  1127. func (sc *serverConn) startGracefulShutdown() {
  1128. sc.serveG.checkNotOn() // NOT
  1129. sc.shutdownOnce.Do(func() { sc.sendServeMsg(gracefulShutdownMsg) })
  1130. }
  1131. // After sending GOAWAY, the connection will close after goAwayTimeout.
  1132. // If we close the connection immediately after sending GOAWAY, there may
  1133. // be unsent data in our kernel receive buffer, which will cause the kernel
  1134. // to send a TCP RST on close() instead of a FIN. This RST will abort the
  1135. // connection immediately, whether or not the client had received the GOAWAY.
  1136. //
  1137. // Ideally we should delay for at least 1 RTT + epsilon so the client has
  1138. // a chance to read the GOAWAY and stop sending messages. Measuring RTT
  1139. // is hard, so we approximate with 1 second. See golang.org/issue/18701.
  1140. //
  1141. // This is a var so it can be shorter in tests, where all requests uses the
  1142. // loopback interface making the expected RTT very small.
  1143. //
  1144. // TODO: configurable?
  1145. var goAwayTimeout = 1 * time.Second
  1146. func (sc *serverConn) startGracefulShutdownInternal() {
  1147. sc.goAway(ErrCodeNo)
  1148. }
  1149. func (sc *serverConn) goAway(code ErrCode) {
  1150. sc.serveG.check()
  1151. if sc.inGoAway {
  1152. return
  1153. }
  1154. sc.inGoAway = true
  1155. sc.needToSendGoAway = true
  1156. sc.goAwayCode = code
  1157. sc.scheduleFrameWrite()
  1158. }
  1159. func (sc *serverConn) shutDownIn(d time.Duration) {
  1160. sc.serveG.check()
  1161. sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer)
  1162. }
  1163. func (sc *serverConn) resetStream(se StreamError) {
  1164. sc.serveG.check()
  1165. sc.writeFrame(FrameWriteRequest{write: se})
  1166. if st, ok := sc.streams[se.StreamID]; ok {
  1167. st.resetQueued = true
  1168. }
  1169. }
  1170. // processFrameFromReader processes the serve loop's read from readFrameCh from the
  1171. // frame-reading goroutine.
  1172. // processFrameFromReader returns whether the connection should be kept open.
  1173. func (sc *serverConn) processFrameFromReader(res readFrameResult) bool {
  1174. sc.serveG.check()
  1175. err := res.err
  1176. if err != nil {
  1177. if err == ErrFrameTooLarge {
  1178. sc.goAway(ErrCodeFrameSize)
  1179. return true // goAway will close the loop
  1180. }
  1181. clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err)
  1182. if clientGone {
  1183. // TODO: could we also get into this state if
  1184. // the peer does a half close
  1185. // (e.g. CloseWrite) because they're done
  1186. // sending frames but they're still wanting
  1187. // our open replies? Investigate.
  1188. // TODO: add CloseWrite to crypto/tls.Conn first
  1189. // so we have a way to test this? I suppose
  1190. // just for testing we could have a non-TLS mode.
  1191. return false
  1192. }
  1193. } else {
  1194. f := res.f
  1195. if VerboseLogs {
  1196. sc.vlogf("http2: server read frame %v", summarizeFrame(f))
  1197. }
  1198. err = sc.processFrame(f)
  1199. if err == nil {
  1200. return true
  1201. }
  1202. }
  1203. switch ev := err.(type) {
  1204. case StreamError:
  1205. sc.resetStream(ev)
  1206. return true
  1207. case goAwayFlowError:
  1208. sc.goAway(ErrCodeFlowControl)
  1209. return true
  1210. case ConnectionError:
  1211. sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
  1212. sc.goAway(ErrCode(ev))
  1213. return true // goAway will handle shutdown
  1214. default:
  1215. if res.err != nil {
  1216. sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
  1217. } else {
  1218. sc.logf("http2: server closing client connection: %v", err)
  1219. }
  1220. return false
  1221. }
  1222. }
  1223. func (sc *serverConn) processFrame(f Frame) error {
  1224. sc.serveG.check()
  1225. // First frame received must be SETTINGS.
  1226. if !sc.sawFirstSettings {
  1227. if _, ok := f.(*SettingsFrame); !ok {
  1228. return ConnectionError(ErrCodeProtocol)
  1229. }
  1230. sc.sawFirstSettings = true
  1231. }
  1232. switch f := f.(type) {
  1233. case *SettingsFrame:
  1234. return sc.processSettings(f)
  1235. case *MetaHeadersFrame:
  1236. return sc.processHeaders(f)
  1237. case *WindowUpdateFrame:
  1238. return sc.processWindowUpdate(f)
  1239. case *PingFrame:
  1240. return sc.processPing(f)
  1241. case *DataFrame:
  1242. return sc.processData(f)
  1243. case *RSTStreamFrame:
  1244. return sc.processResetStream(f)
  1245. case *PriorityFrame:
  1246. return sc.processPriority(f)
  1247. case *GoAwayFrame:
  1248. return sc.processGoAway(f)
  1249. case *PushPromiseFrame:
  1250. // A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE
  1251. // frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1252. return ConnectionError(ErrCodeProtocol)
  1253. default:
  1254. sc.vlogf("http2: server ignoring frame: %v", f.Header())
  1255. return nil
  1256. }
  1257. }
  1258. func (sc *serverConn) processPing(f *PingFrame) error {
  1259. sc.serveG.check()
  1260. if f.IsAck() {
  1261. // 6.7 PING: " An endpoint MUST NOT respond to PING frames
  1262. // containing this flag."
  1263. return nil
  1264. }
  1265. if f.StreamID != 0 {
  1266. // "PING frames are not associated with any individual
  1267. // stream. If a PING frame is received with a stream
  1268. // identifier field value other than 0x0, the recipient MUST
  1269. // respond with a connection error (Section 5.4.1) of type
  1270. // PROTOCOL_ERROR."
  1271. return ConnectionError(ErrCodeProtocol)
  1272. }
  1273. if sc.inGoAway && sc.goAwayCode != ErrCodeNo {
  1274. return nil
  1275. }
  1276. sc.writeFrame(FrameWriteRequest{write: writePingAck{f}})
  1277. return nil
  1278. }
  1279. func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error {
  1280. sc.serveG.check()
  1281. switch {
  1282. case f.StreamID != 0: // stream-level flow control
  1283. state, st := sc.state(f.StreamID)
  1284. if state == stateIdle {
  1285. // Section 5.1: "Receiving any frame other than HEADERS
  1286. // or PRIORITY on a stream in this state MUST be
  1287. // treated as a connection error (Section 5.4.1) of
  1288. // type PROTOCOL_ERROR."
  1289. return ConnectionError(ErrCodeProtocol)
  1290. }
  1291. if st == nil {
  1292. // "WINDOW_UPDATE can be sent by a peer that has sent a
  1293. // frame bearing the END_STREAM flag. This means that a
  1294. // receiver could receive a WINDOW_UPDATE frame on a "half
  1295. // closed (remote)" or "closed" stream. A receiver MUST
  1296. // NOT treat this as an error, see Section 5.1."
  1297. return nil
  1298. }
  1299. if !st.flow.add(int32(f.Increment)) {
  1300. return streamError(f.StreamID, ErrCodeFlowControl)
  1301. }
  1302. default: // connection-level flow control
  1303. if !sc.flow.add(int32(f.Increment)) {
  1304. return goAwayFlowError{}
  1305. }
  1306. }
  1307. sc.scheduleFrameWrite()
  1308. return nil
  1309. }
  1310. func (sc *serverConn) processResetStream(f *RSTStreamFrame) error {
  1311. sc.serveG.check()
  1312. state, st := sc.state(f.StreamID)
  1313. if state == stateIdle {
  1314. // 6.4 "RST_STREAM frames MUST NOT be sent for a
  1315. // stream in the "idle" state. If a RST_STREAM frame
  1316. // identifying an idle stream is received, the
  1317. // recipient MUST treat this as a connection error
  1318. // (Section 5.4.1) of type PROTOCOL_ERROR.
  1319. return ConnectionError(ErrCodeProtocol)
  1320. }
  1321. if st != nil {
  1322. st.cancelCtx()
  1323. sc.closeStream(st, streamError(f.StreamID, f.ErrCode))
  1324. }
  1325. return nil
  1326. }
  1327. func (sc *serverConn) closeStream(st *stream, err error) {
  1328. sc.serveG.check()
  1329. if st.state == stateIdle || st.state == stateClosed {
  1330. panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
  1331. }
  1332. st.state = stateClosed
  1333. if st.writeDeadline != nil {
  1334. st.writeDeadline.Stop()
  1335. }
  1336. if st.isPushed() {
  1337. sc.curPushedStreams--
  1338. } else {
  1339. sc.curClientStreams--
  1340. }
  1341. delete(sc.streams, st.id)
  1342. if len(sc.streams) == 0 {
  1343. sc.setConnState(http.StateIdle)
  1344. if sc.srv.IdleTimeout != 0 {
  1345. sc.idleTimer.Reset(sc.srv.IdleTimeout)
  1346. }
  1347. if h1ServerKeepAlivesDisabled(sc.hs) {
  1348. sc.startGracefulShutdownInternal()
  1349. }
  1350. }
  1351. if p := st.body; p != nil {
  1352. // Return any buffered unread bytes worth of conn-level flow control.
  1353. // See golang.org/issue/16481
  1354. sc.sendWindowUpdate(nil, p.Len())
  1355. p.CloseWithError(err)
  1356. }
  1357. st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc
  1358. sc.writeSched.CloseStream(st.id)
  1359. }
  1360. func (sc *serverConn) processSettings(f *SettingsFrame) error {
  1361. sc.serveG.check()
  1362. if f.IsAck() {
  1363. sc.unackedSettings--
  1364. if sc.unackedSettings < 0 {
  1365. // Why is the peer ACKing settings we never sent?
  1366. // The spec doesn't mention this case, but
  1367. // hang up on them anyway.
  1368. return ConnectionError(ErrCodeProtocol)
  1369. }
  1370. return nil
  1371. }
  1372. if f.NumSettings() > 100 || f.HasDuplicates() {
  1373. // This isn't actually in the spec, but hang up on
  1374. // suspiciously large settings frames or those with
  1375. // duplicate entries.
  1376. return ConnectionError(ErrCodeProtocol)
  1377. }
  1378. if err := f.ForeachSetting(sc.processSetting); err != nil {
  1379. return err
  1380. }
  1381. sc.needToSendSettingsAck = true
  1382. sc.scheduleFrameWrite()
  1383. return nil
  1384. }
  1385. func (sc *serverConn) processSetting(s Setting) error {
  1386. sc.serveG.check()
  1387. if err := s.Valid(); err != nil {
  1388. return err
  1389. }
  1390. if VerboseLogs {
  1391. sc.vlogf("http2: server processing setting %v", s)
  1392. }
  1393. switch s.ID {
  1394. case SettingHeaderTableSize:
  1395. sc.headerTableSize = s.Val
  1396. sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
  1397. case SettingEnablePush:
  1398. sc.pushEnabled = s.Val != 0
  1399. case SettingMaxConcurrentStreams:
  1400. sc.clientMaxStreams = s.Val
  1401. case SettingInitialWindowSize:
  1402. return sc.processSettingInitialWindowSize(s.Val)
  1403. case SettingMaxFrameSize:
  1404. sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31
  1405. case SettingMaxHeaderListSize:
  1406. sc.peerMaxHeaderListSize = s.Val
  1407. default:
  1408. // Unknown setting: "An endpoint that receives a SETTINGS
  1409. // frame with any unknown or unsupported iden…

Large files files are truncated, but you can click here to view the full file