| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384 |
- // Copyright 2014 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
- // Licensed under the same terms as Go itself:
- // https://code.google.com/p/go/source/browse/LICENSE
- package http2
- import (
- "bufio"
- "bytes"
- "crypto/tls"
- "errors"
- "fmt"
- "io"
- "log"
- "net"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "sync"
- "time"
- "github.com/bradfitz/http2/hpack"
- )
- const (
- prefaceTimeout = 5 * time.Second
- firstSettingsTimeout = 2 * time.Second // should be in-flight with preface anyway
- )
- // TODO: finish GOAWAY support. Consider each incoming frame type and
- // whether it should be ignored during a shutdown race.
- // TODO: (edge case?) if peer sends a SETTINGS frame with e.g. a
- // SETTINGS_MAX_FRAME_SIZE that's lower than what we had before,
- // before we ACK it we have to make sure all currently-active streams
- // know about that and don't have existing too-large frames in flight?
- // Perhaps the settings processing should just wait for new frame to
- // be in-flight and then the frame scheduler in the serve goroutine
- // will be responsible for splitting things.
- // Server is an HTTP/2 server.
- type Server struct {
- // MaxStreams optionally ...
- MaxStreams int
- }
- var testHookOnConn func() // for testing
- // ConfigureServer adds HTTP/2 support to a net/http Server.
- //
- // The configuration conf may be nil.
- //
- // ConfigureServer must be called before s begins serving.
- func ConfigureServer(s *http.Server, conf *Server) {
- if conf == nil {
- conf = new(Server)
- }
- if s.TLSConfig == nil {
- s.TLSConfig = new(tls.Config)
- }
- haveNPN := false
- for _, p := range s.TLSConfig.NextProtos {
- if p == npnProto {
- haveNPN = true
- break
- }
- }
- if !haveNPN {
- s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, npnProto)
- }
- if s.TLSNextProto == nil {
- s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){}
- }
- s.TLSNextProto[npnProto] = func(hs *http.Server, c *tls.Conn, h http.Handler) {
- if testHookOnConn != nil {
- testHookOnConn()
- }
- conf.handleConn(hs, c, h)
- }
- }
- var testHookGetServerConn func(*serverConn)
- func (srv *Server) handleConn(hs *http.Server, c net.Conn, h http.Handler) {
- sc := &serverConn{
- hs: hs,
- conn: c,
- handler: h,
- framer: NewFramer(c, c), // TODO: write to a (custom?) buffered writer that can alternate when it's in buffered mode.
- streams: make(map[uint32]*stream),
- readFrameCh: make(chan frameAndGate),
- readFrameErrCh: make(chan error, 1), // must be buffered for 1
- wantWriteFrameCh: make(chan frameWriteMsg, 8),
- writeFrameCh: make(chan frameWriteMsg, 1), // may be 0 or 1, but more is useless. (max 1 in flight)
- wroteFrameCh: make(chan struct{}, 1),
- flow: newFlow(initialWindowSize),
- doneServing: make(chan struct{}),
- maxWriteFrameSize: initialMaxFrameSize,
- initialWindowSize: initialWindowSize,
- serveG: newGoroutineLock(),
- }
- sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
- sc.hpackDecoder = hpack.NewDecoder(initialHeaderTableSize, sc.onNewHeaderField)
- if hook := testHookGetServerConn; hook != nil {
- hook(sc)
- }
- sc.serve()
- }
- // frameAndGates coordinates the readFrames and serve
- // goroutines. Because the Framer interface only permits the most
- // recently-read Frame from being accessed, the readFrames goroutine
- // blocks until it has a frame, passes it to serve, and then waits for
- // serve to be done with it before reading the next one.
- type frameAndGate struct {
- f Frame
- g gate
- }
- type serverConn struct {
- // Immutable:
- hs *http.Server
- conn net.Conn
- handler http.Handler
- framer *Framer
- hpackDecoder *hpack.Decoder
- doneServing chan struct{} // closed when serverConn.serve ends
- readFrameCh chan frameAndGate // written by serverConn.readFrames
- readFrameErrCh chan error
- wantWriteFrameCh chan frameWriteMsg // from handlers -> serve
- writeFrameCh chan frameWriteMsg // from serve -> writeFrames
- wroteFrameCh chan struct{} // from writeFrames -> serve, tickles more sends on writeFrameCh
- testHookCh chan func() // code to run on the serve loop
- serveG goroutineLock // used to verify funcs are on serve()
- writeG goroutineLock // used to verify things running on writeLoop
- flow *flow // connection-wide (not stream-specific) flow control
- // Everything following is owned by the serve loop; use serveG.check():
- sawFirstSettings bool // got the initial SETTINGS frame after the preface
- needToSendSettingsAck bool
- maxStreamID uint32 // max ever seen
- streams map[uint32]*stream
- maxWriteFrameSize uint32 // TODO: update this when settings come in
- initialWindowSize int32
- canonHeader map[string]string // http2-lower-case -> Go-Canonical-Case
- sentGoAway bool
- req requestParam // non-zero while reading request headers
- writingFrame bool // sent on writeFrameCh but haven't heard back on wroteFrameCh yet
- writeQueue []frameWriteMsg // TODO: proper scheduler, not a queue
- // Owned by the writeFrames goroutine; use writeG.check():
- headerWriteBuf bytes.Buffer
- hpackEncoder *hpack.Encoder
- }
- // requestParam is the state of the next request, initialized over
- // potentially several frames HEADERS + zero or more CONTINUATION
- // frames.
- type requestParam struct {
- // stream is non-nil if we're reading (HEADER or CONTINUATION)
- // frames for a request (but not DATA).
- stream *stream
- header http.Header
- method, path string
- scheme, authority string
- sawRegularHeader bool // saw a non-pseudo header already
- invalidHeader bool // an invalid header was seen
- }
- type stream struct {
- id uint32
- state streamState // owned by serverConn's serve loop
- flow *flow // limits writing from Handler to client
- body *pipe // non-nil if expecting DATA frames
- bodyBytes int64 // body bytes seen so far
- declBodyBytes int64 // or -1 if undeclared
- }
- func (sc *serverConn) state(streamID uint32) streamState {
- sc.serveG.check()
- // http://http2.github.io/http2-spec/#rfc.section.5.1
- if st, ok := sc.streams[streamID]; ok {
- return st.state
- }
- // "The first use of a new stream identifier implicitly closes all
- // streams in the "idle" state that might have been initiated by
- // that peer with a lower-valued stream identifier. For example, if
- // a client sends a HEADERS frame on stream 7 without ever sending a
- // frame on stream 5, then stream 5 transitions to the "closed"
- // state when the first frame for stream 7 is sent or received."
- if streamID <= sc.maxStreamID {
- return stateClosed
- }
- return stateIdle
- }
- func (sc *serverConn) vlogf(format string, args ...interface{}) {
- if VerboseLogs {
- sc.logf(format, args...)
- }
- }
- func (sc *serverConn) logf(format string, args ...interface{}) {
- if lg := sc.hs.ErrorLog; lg != nil {
- lg.Printf(format, args...)
- } else {
- log.Printf(format, args...)
- }
- }
- func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
- if err == nil {
- return
- }
- str := err.Error()
- if err == io.EOF || strings.Contains(str, "use of closed network connection") {
- // Boring, expected errors.
- sc.vlogf(format, args...)
- } else {
- sc.logf(format, args...)
- }
- }
- func (sc *serverConn) onNewHeaderField(f hpack.HeaderField) {
- sc.serveG.check()
- switch {
- case !validHeader(f.Name):
- sc.req.invalidHeader = true
- case strings.HasPrefix(f.Name, ":"):
- if sc.req.sawRegularHeader {
- sc.logf("pseudo-header after regular header")
- sc.req.invalidHeader = true
- return
- }
- var dst *string
- switch f.Name {
- case ":method":
- dst = &sc.req.method
- case ":path":
- dst = &sc.req.path
- case ":scheme":
- dst = &sc.req.scheme
- case ":authority":
- dst = &sc.req.authority
- default:
- // 8.1.2.1 Pseudo-Header Fields
- // "Endpoints MUST treat a request or response
- // that contains undefined or invalid
- // pseudo-header fields as malformed (Section
- // 8.1.2.6)."
- sc.logf("invalid pseudo-header %q", f.Name)
- sc.req.invalidHeader = true
- return
- }
- if *dst != "" {
- sc.logf("duplicate pseudo-header %q sent", f.Name)
- sc.req.invalidHeader = true
- return
- }
- *dst = f.Value
- case f.Name == "cookie":
- sc.req.sawRegularHeader = true
- if s, ok := sc.req.header["Cookie"]; ok && len(s) == 1 {
- s[0] = s[0] + "; " + f.Value
- } else {
- sc.req.header.Add("Cookie", f.Value)
- }
- default:
- sc.req.sawRegularHeader = true
- sc.req.header.Add(sc.canonicalHeader(f.Name), f.Value)
- }
- }
- func (sc *serverConn) canonicalHeader(v string) string {
- sc.serveG.check()
- cv, ok := commonCanonHeader[v]
- if ok {
- return cv
- }
- cv, ok = sc.canonHeader[v]
- if ok {
- return cv
- }
- if sc.canonHeader == nil {
- sc.canonHeader = make(map[string]string)
- }
- cv = http.CanonicalHeaderKey(v)
- sc.canonHeader[v] = cv
- return cv
- }
- // readFrames is the loop that reads incoming frames.
- // It's run on its own goroutine.
- func (sc *serverConn) readFrames() {
- g := make(gate, 1)
- for {
- f, err := sc.framer.ReadFrame()
- if err != nil {
- sc.readFrameErrCh <- err // BEFORE the close
- close(sc.readFrameCh)
- return
- }
- sc.readFrameCh <- frameAndGate{f, g}
- g.Wait()
- }
- }
- // writeFrames is the loop that writes frames to the peer
- // and is responsible for prioritization and buffering.
- // It's run on its own goroutine.
- func (sc *serverConn) writeFrames() {
- sc.writeG = newGoroutineLock()
- for wm := range sc.writeFrameCh {
- err := wm.write(sc, wm.v)
- if ch := wm.done; ch != nil {
- select {
- case ch <- err:
- default:
- panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.v))
- }
- }
- sc.wroteFrameCh <- struct{}{} // tickle frame selection scheduler
- }
- }
- func (sc *serverConn) stopServing() {
- sc.serveG.check()
- close(sc.writeFrameCh) // stop the writeFrames loop
- err := errors.New("client disconnected")
- for id := range sc.streams {
- sc.closeStream(id, err)
- }
- }
- func (sc *serverConn) serve() {
- sc.serveG.check()
- defer sc.conn.Close()
- defer sc.stopServing()
- sc.vlogf("HTTP/2 connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
- sc.enqueueFrameWrite(frameWriteMsg{write: (*serverConn).sendInitialSettings})
- if err := sc.readPreface(); err != nil {
- sc.condlogf(err, "Error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
- return
- }
- go sc.readFrames() // closed by defer sc.conn.Close above
- go sc.writeFrames() // source closed in stopServing
- settingsTimer := time.NewTimer(firstSettingsTimeout)
- for {
- select {
- case wm := <-sc.wantWriteFrameCh:
- sc.enqueueFrameWrite(wm)
- case <-sc.wroteFrameCh:
- sc.writingFrame = false
- sc.scheduleFrameWrite()
- case fg, ok := <-sc.readFrameCh:
- if !sc.processFrameFromReader(fg, ok) {
- return
- }
- if settingsTimer.C != nil {
- settingsTimer.Stop()
- settingsTimer.C = nil
- }
- case <-settingsTimer.C:
- sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
- return
- case fn := <-sc.testHookCh:
- fn()
- }
- }
- }
- func (sc *serverConn) sendInitialSettings(_ interface{}) error {
- sc.writeG.check()
- return sc.framer.WriteSettings( /* TODO: actual settings */ )
- }
- // readPreface reads the ClientPreface greeting from the peer
- // or returns an error on timeout or an invalid greeting.
- func (sc *serverConn) readPreface() error {
- errc := make(chan error, 1)
- go func() {
- // Read the client preface
- buf := make([]byte, len(ClientPreface))
- // TODO: timeout reading from the client
- if _, err := io.ReadFull(sc.conn, buf); err != nil {
- errc <- err
- } else if !bytes.Equal(buf, clientPreface) {
- errc <- fmt.Errorf("bogus greeting %q", buf)
- } else {
- errc <- nil
- }
- }()
- timer := time.NewTimer(5 * time.Second) // TODO: configurable on *Server?
- defer timer.Stop()
- select {
- case <-timer.C:
- return errors.New("timeout waiting for client preface")
- case err := <-errc:
- if err == nil {
- sc.vlogf("client %v said hello", sc.conn.RemoteAddr())
- }
- return err
- }
- }
- // should be called from non-serve() goroutines, otherwise the ends may deadlock
- // the serve loop. (it's only buffered a little bit).
- func (sc *serverConn) writeFrame(wm frameWriteMsg) {
- sc.serveG.checkNotOn() // note the "NOT"
- sc.wantWriteFrameCh <- wm
- }
- func (sc *serverConn) enqueueFrameWrite(wm frameWriteMsg) {
- sc.serveG.check()
- // Fast path for common case:
- if !sc.writingFrame {
- sc.sendFrameWrite(wm)
- return
- }
- sc.writeQueue = append(sc.writeQueue, wm) // TODO: proper scheduler
- }
- // sendFrameWrite sends a frame to the writeFrames goroutine.
- // Only one frame can be in-flight at a time.
- // sendFrameWrite also updates stream state right before the frame is
- // sent to be written.
- func (sc *serverConn) sendFrameWrite(wm frameWriteMsg) {
- sc.serveG.check()
- if sc.writingFrame {
- panic("invariant")
- }
- sc.writingFrame = true
- if wm.endStream {
- st, ok := sc.streams[wm.streamID]
- if ok {
- switch st.state {
- case stateOpen:
- st.state = stateHalfClosedLocal
- case stateHalfClosedRemote:
- st.state = stateClosed
- delete(sc.streams, wm.streamID)
- }
- }
- }
- sc.writeFrameCh <- wm
- }
- func (sc *serverConn) enqueueSettingsAck() {
- sc.serveG.check()
- // Fast path for common case:
- if !sc.writingFrame {
- sc.writeFrameCh <- frameWriteMsg{write: (*serverConn).writeSettingsAck}
- return
- }
- sc.needToSendSettingsAck = true
- }
- func (sc *serverConn) scheduleFrameWrite() {
- sc.serveG.check()
- if sc.writingFrame {
- panic("invariant")
- }
- if sc.needToSendSettingsAck {
- sc.needToSendSettingsAck = false
- sc.enqueueSettingsAck()
- return
- }
- if len(sc.writeQueue) == 0 {
- // TODO: flush Framer's underlying buffered writer, once that's added
- return
- }
- // TODO: proper scheduler
- wm := sc.writeQueue[0]
- // shift it all down. kinda lame. will be removed later anyway.
- copy(sc.writeQueue, sc.writeQueue[1:])
- sc.writeQueue = sc.writeQueue[:len(sc.writeQueue)-1]
- // TODO: if wm is a data frame, make sure it's not too big
- // (because a SETTINGS frame changed our max frame size while
- // a stream was open and writing) and cut it up into smaller
- // bits.
- sc.sendFrameWrite(wm)
- }
- func (sc *serverConn) goAway(code ErrCode) {
- sc.serveG.check()
- if sc.sentGoAway {
- return
- }
- sc.sentGoAway = true
- // TODO: set a timer to see if they're gone at some point?
- sc.enqueueFrameWrite(frameWriteMsg{
- write: (*serverConn).writeGoAwayFrame,
- v: &goAwayParams{
- maxStreamID: sc.maxStreamID,
- code: code,
- },
- })
- }
- type goAwayParams struct {
- maxStreamID uint32
- code ErrCode
- }
- func (sc *serverConn) writeGoAwayFrame(v interface{}) error {
- sc.writeG.check()
- p := v.(*goAwayParams)
- return sc.framer.WriteGoAway(p.maxStreamID, p.code, nil)
- }
- func (sc *serverConn) resetStreamInLoop(se StreamError) error {
- sc.serveG.check()
- delete(sc.streams, se.streamID)
- sc.enqueueFrameWrite(frameWriteMsg{
- write: (*serverConn).writeRSTStreamFrame,
- v: &se,
- })
- return nil
- }
- func (sc *serverConn) writeRSTStreamFrame(v interface{}) error {
- sc.writeG.check()
- se := v.(*StreamError)
- return sc.framer.WriteRSTStream(se.streamID, se.code)
- }
- func (sc *serverConn) curHeaderStreamID() uint32 {
- sc.serveG.check()
- st := sc.req.stream
- if st == nil {
- return 0
- }
- return st.id
- }
- // processFrameFromReader processes the serve loop's read from readFrameCh from the
- // frame-reading goroutine.
- // processFrameFromReader returns whether the connection should be kept open.
- func (sc *serverConn) processFrameFromReader(fg frameAndGate, fgValid bool) bool {
- sc.serveG.check()
- if !fgValid {
- err := <-sc.readFrameErrCh
- if err != io.EOF {
- errstr := err.Error()
- if !strings.Contains(errstr, "use of closed network connection") {
- sc.logf("client %s stopped sending frames: %v", sc.conn.RemoteAddr(), errstr)
- }
- }
- // TODO: could we also get into this state if the peer does a half close (e.g. CloseWrite)
- // because they're done sending frames but they're still wanting our open replies?
- // Investigate.
- return false
- }
- f := fg.f
- sc.vlogf("got %v: %#v", f.Header(), f)
- err := sc.processFrame(f)
- fg.g.Done() // unblock the readFrames goroutine
- if err == nil {
- return true
- }
- switch ev := err.(type) {
- case StreamError:
- if err := sc.resetStreamInLoop(ev); err != nil {
- sc.logf("Error writing RSTSTream: %v", err)
- return false
- }
- return true
- case goAwayFlowError:
- sc.goAway(ErrCodeFlowControl)
- return true
- case ConnectionError:
- sc.logf("disconnecting; %v", ev)
- default:
- sc.logf("Disconnection due to other error: %v", err)
- }
- return false
- }
- func (sc *serverConn) processFrame(f Frame) error {
- sc.serveG.check()
- // First frame received must be SETTINGS.
- if !sc.sawFirstSettings {
- if _, ok := f.(*SettingsFrame); !ok {
- return ConnectionError(ErrCodeProtocol)
- }
- sc.sawFirstSettings = true
- }
- if s := sc.curHeaderStreamID(); s != 0 {
- if cf, ok := f.(*ContinuationFrame); !ok {
- return ConnectionError(ErrCodeProtocol)
- } else if cf.Header().StreamID != s {
- return ConnectionError(ErrCodeProtocol)
- }
- }
- switch f := f.(type) {
- case *SettingsFrame:
- return sc.processSettings(f)
- case *HeadersFrame:
- return sc.processHeaders(f)
- case *ContinuationFrame:
- return sc.processContinuation(f)
- case *WindowUpdateFrame:
- return sc.processWindowUpdate(f)
- case *PingFrame:
- return sc.processPing(f)
- case *DataFrame:
- return sc.processData(f)
- case *RSTStreamFrame:
- return sc.processResetStream(f)
- default:
- log.Printf("Ignoring unknown frame %#v", f)
- return nil
- }
- }
- func (sc *serverConn) processPing(f *PingFrame) error {
- sc.serveG.check()
- if f.Flags.Has(FlagSettingsAck) {
- // 6.7 PING: " An endpoint MUST NOT respond to PING frames
- // containing this flag."
- return nil
- }
- if f.StreamID != 0 {
- // "PING frames are not associated with any individual
- // stream. If a PING frame is received with a stream
- // identifier field value other than 0x0, the recipient MUST
- // respond with a connection error (Section 5.4.1) of type
- // PROTOCOL_ERROR."
- return ConnectionError(ErrCodeProtocol)
- }
- sc.enqueueFrameWrite(frameWriteMsg{
- write: (*serverConn).writePingAck,
- v: f,
- })
- return nil
- }
- func (sc *serverConn) writePingAck(v interface{}) error {
- sc.writeG.check()
- pf := v.(*PingFrame) // contains the data we need to write back
- return sc.framer.WritePing(true, pf.Data)
- }
- func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error {
- sc.serveG.check()
- switch {
- case f.StreamID != 0: // stream-level flow control
- st := sc.streams[f.StreamID]
- if st == nil {
- // "WINDOW_UPDATE can be sent by a peer that has sent a
- // frame bearing the END_STREAM flag. This means that a
- // receiver could receive a WINDOW_UPDATE frame on a "half
- // closed (remote)" or "closed" stream. A receiver MUST
- // NOT treat this as an error, see Section 5.1."
- return nil
- }
- if !st.flow.add(int32(f.Increment)) {
- return StreamError{f.StreamID, ErrCodeFlowControl}
- }
- default: // connection-level flow control
- if !sc.flow.add(int32(f.Increment)) {
- return goAwayFlowError{}
- }
- }
- return nil
- }
- func (sc *serverConn) processResetStream(f *RSTStreamFrame) error {
- sc.serveG.check()
- sc.closeStream(f.StreamID, StreamError{f.StreamID, f.ErrCode})
- return nil
- }
- func (sc *serverConn) closeStream(streamID uint32, err error) {
- sc.serveG.check()
- st, ok := sc.streams[streamID]
- if !ok {
- return
- }
- st.state = stateClosed // kinda useless
- delete(sc.streams, streamID)
- st.flow.close()
- if p := st.body; p != nil {
- p.Close(err)
- }
- }
- func (sc *serverConn) processSettings(f *SettingsFrame) error {
- sc.serveG.check()
- if f.IsAck() {
- // TODO: do we need to do anything?
- return nil
- }
- if err := f.ForeachSetting(sc.processSetting); err != nil {
- return err
- }
- sc.enqueueSettingsAck()
- return nil
- }
- func (sc *serverConn) writeSettingsAck(_ interface{}) error {
- return sc.framer.WriteSettingsAck()
- }
- func (sc *serverConn) processSetting(s Setting) error {
- sc.serveG.check()
- sc.vlogf("processing setting %v", s)
- switch s.ID {
- case SettingInitialWindowSize:
- return sc.processSettingInitialWindowSize(s.Val)
- }
- log.Printf("TODO: handle %v", s)
- return nil
- }
- func (sc *serverConn) processSettingInitialWindowSize(val uint32) error {
- sc.serveG.check()
- if val > (1<<31 - 1) {
- // 6.5.2 Defined SETTINGS Parameters
- // "Values above the maximum flow control window size of
- // 231-1 MUST be treated as a connection error (Section
- // 5.4.1) of type FLOW_CONTROL_ERROR."
- return ConnectionError(ErrCodeFlowControl)
- }
- // "A SETTINGS frame can alter the initial flow control window
- // size for all current streams. When the value of
- // SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST
- // adjust the size of all stream flow control windows that it
- // maintains by the difference between the new value and the
- // old value."
- old := sc.initialWindowSize
- sc.initialWindowSize = int32(val)
- growth := sc.initialWindowSize - old // may be negative
- for _, st := range sc.streams {
- if !st.flow.add(growth) {
- // 6.9.2 Initial Flow Control Window Size
- // "An endpoint MUST treat a change to
- // SETTINGS_INITIAL_WINDOW_SIZE that causes any flow
- // control window to exceed the maximum size as a
- // connection error (Section 5.4.1) of type
- // FLOW_CONTROL_ERROR."
- return ConnectionError(ErrCodeFlowControl)
- }
- }
- return nil
- }
- func (sc *serverConn) processData(f *DataFrame) error {
- sc.serveG.check()
- // "If a DATA frame is received whose stream is not in "open"
- // or "half closed (local)" state, the recipient MUST respond
- // with a stream error (Section 5.4.2) of type STREAM_CLOSED."
- id := f.Header().StreamID
- st, ok := sc.streams[id]
- if !ok || (st.state != stateOpen && st.state != stateHalfClosedLocal) {
- return StreamError{id, ErrCodeStreamClosed}
- }
- if st.body == nil {
- // Not expecting data.
- // TODO: which error code?
- return StreamError{id, ErrCodeStreamClosed}
- }
- data := f.Data()
- // Sender sending more than they'd declared?
- if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
- st.body.Close(fmt.Errorf("Sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
- return StreamError{id, ErrCodeStreamClosed}
- }
- if len(data) > 0 {
- // TODO: verify they're allowed to write with the flow control
- // window we'd advertised to them.
- // TODO: verify n from Write
- if _, err := st.body.Write(data); err != nil {
- return StreamError{id, ErrCodeStreamClosed}
- }
- st.bodyBytes += int64(len(data))
- }
- if f.Header().Flags.Has(FlagDataEndStream) {
- if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
- st.body.Close(fmt.Errorf("Request declared a Content-Length of %d but only wrote %d bytes",
- st.declBodyBytes, st.bodyBytes))
- } else {
- st.body.Close(io.EOF)
- }
- switch st.state {
- case stateOpen:
- st.state = stateHalfClosedRemote
- case stateHalfClosedLocal:
- st.state = stateClosed
- }
- }
- return nil
- }
- func (sc *serverConn) processHeaders(f *HeadersFrame) error {
- sc.serveG.check()
- id := f.Header().StreamID
- if sc.sentGoAway {
- // Ignore.
- return nil
- }
- // http://http2.github.io/http2-spec/#rfc.section.5.1.1
- if id%2 != 1 || id <= sc.maxStreamID || sc.req.stream != nil {
- // Streams initiated by a client MUST use odd-numbered
- // stream identifiers. [...] The identifier of a newly
- // established stream MUST be numerically greater than all
- // streams that the initiating endpoint has opened or
- // reserved. [...] An endpoint that receives an unexpected
- // stream identifier MUST respond with a connection error
- // (Section 5.4.1) of type PROTOCOL_ERROR.
- return ConnectionError(ErrCodeProtocol)
- }
- if id > sc.maxStreamID {
- sc.maxStreamID = id
- }
- st := &stream{
- id: id,
- state: stateOpen,
- flow: newFlow(sc.initialWindowSize),
- }
- if f.Header().Flags.Has(FlagHeadersEndStream) {
- st.state = stateHalfClosedRemote
- }
- sc.streams[id] = st
- sc.req = requestParam{
- stream: st,
- header: make(http.Header),
- }
- return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded())
- }
- func (sc *serverConn) processContinuation(f *ContinuationFrame) error {
- sc.serveG.check()
- st := sc.streams[f.Header().StreamID]
- if st == nil || sc.curHeaderStreamID() != st.id {
- return ConnectionError(ErrCodeProtocol)
- }
- return sc.processHeaderBlockFragment(st, f.HeaderBlockFragment(), f.HeadersEnded())
- }
- func (sc *serverConn) processHeaderBlockFragment(st *stream, frag []byte, end bool) error {
- sc.serveG.check()
- if _, err := sc.hpackDecoder.Write(frag); err != nil {
- // TODO: convert to stream error I assume?
- return err
- }
- if !end {
- return nil
- }
- if err := sc.hpackDecoder.Close(); err != nil {
- // TODO: convert to stream error I assume?
- return err
- }
- rw, req, err := sc.newWriterAndRequest()
- sc.req = requestParam{}
- if err != nil {
- return err
- }
- st.body = req.Body.(*requestBody).pipe // may be nil
- st.declBodyBytes = req.ContentLength
- go sc.runHandler(rw, req)
- return nil
- }
- func (sc *serverConn) newWriterAndRequest() (*responseWriter, *http.Request, error) {
- sc.serveG.check()
- rp := &sc.req
- if rp.invalidHeader || rp.method == "" || rp.path == "" ||
- (rp.scheme != "https" && rp.scheme != "http") {
- // See 8.1.2.6 Malformed Requests and Responses:
- //
- // Malformed requests or responses that are detected
- // MUST be treated as a stream error (Section 5.4.2)
- // of type PROTOCOL_ERROR."
- //
- // 8.1.2.3 Request Pseudo-Header Fields
- // "All HTTP/2 requests MUST include exactly one valid
- // value for the :method, :scheme, and :path
- // pseudo-header fields"
- return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol}
- }
- var tlsState *tls.ConnectionState // make this non-nil if https
- if rp.scheme == "https" {
- // TODO: get from sc's ConnectionState
- tlsState = &tls.ConnectionState{}
- }
- authority := rp.authority
- if authority == "" {
- authority = rp.header.Get("Host")
- }
- needsContinue := rp.header.Get("Expect") == "100-continue"
- if needsContinue {
- rp.header.Del("Expect")
- }
- bodyOpen := rp.stream.state == stateOpen
- body := &requestBody{
- sc: sc,
- streamID: rp.stream.id,
- needsContinue: needsContinue,
- }
- url, err := url.ParseRequestURI(rp.path)
- if err != nil {
- // TODO: find the right error code?
- return nil, nil, StreamError{rp.stream.id, ErrCodeProtocol}
- }
- req := &http.Request{
- Method: rp.method,
- URL: url,
- RemoteAddr: sc.conn.RemoteAddr().String(),
- Header: rp.header,
- RequestURI: rp.path,
- Proto: "HTTP/2.0",
- ProtoMajor: 2,
- ProtoMinor: 0,
- TLS: tlsState,
- Host: authority,
- Body: body,
- }
- if bodyOpen {
- body.pipe = &pipe{
- b: buffer{buf: make([]byte, 65536)}, // TODO: share/remove
- }
- body.pipe.c.L = &body.pipe.m
- if vv, ok := rp.header["Content-Length"]; ok {
- req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64)
- } else {
- req.ContentLength = -1
- }
- }
- rws := responseWriterStatePool.Get().(*responseWriterState)
- bwSave := rws.bw
- *rws = responseWriterState{} // zero all the fields
- rws.bw = bwSave
- rws.bw.Reset(chunkWriter{rws})
- rws.sc = sc
- rws.streamID = rp.stream.id
- rws.req = req
- rws.body = body
- rws.chunkWrittenCh = make(chan error, 1)
- rw := &responseWriter{rws: rws}
- return rw, req, nil
- }
- const handlerChunkWriteSize = 4 << 10
- var responseWriterStatePool = sync.Pool{
- New: func() interface{} {
- rws := &responseWriterState{}
- rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize)
- return rws
- },
- }
- // Run on its own goroutine.
- func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request) {
- defer rw.handlerDone()
- // TODO: catch panics like net/http.Server
- sc.handler.ServeHTTP(rw, req)
- }
- type frameWriteMsg struct {
- // write runs on the writeFrames goroutine.
- write func(sc *serverConn, v interface{}) error
- v interface{} // passed to write
- cost uint32 // number of flow control bytes required
- streamID uint32 // used for prioritization
- endStream bool // streamID is being closed locally
- // done, if non-nil, must be a buffered channel with space for
- // 1 message and is sent the return value from write (or an
- // earlier error) when the frame has been written.
- done chan error
- }
- // headerWriteReq is a request to write an HTTP response header from a server Handler.
- type headerWriteReq struct {
- streamID uint32
- httpResCode int
- h http.Header // may be nil
- endStream bool
- contentType string
- contentLength string
- }
- // called from handler goroutines.
- // h may be nil.
- func (sc *serverConn) writeHeaders(req headerWriteReq) {
- sc.serveG.checkNotOn()
- var errc chan error
- if req.h != nil {
- // If there's a header map (which we don't own), so we have to block on
- // waiting for this frame to be written, so an http.Flush mid-handler
- // writes out the correct value of keys, before a handler later potentially
- // mutates it.
- errc = make(chan error, 1)
- }
- sc.writeFrame(frameWriteMsg{
- write: (*serverConn).writeHeadersFrame,
- v: req,
- streamID: req.streamID,
- done: errc,
- endStream: req.endStream,
- })
- if errc != nil {
- <-errc
- }
- }
- func (sc *serverConn) writeHeadersFrame(v interface{}) error {
- sc.writeG.check()
- req := v.(headerWriteReq)
- sc.headerWriteBuf.Reset()
- sc.hpackEncoder.WriteField(hpack.HeaderField{Name: ":status", Value: httpCodeString(req.httpResCode)})
- for k, vv := range req.h {
- k = lowerHeader(k)
- for _, v := range vv {
- // TODO: more of "8.1.2.2 Connection-Specific Header Fields"
- if k == "transfer-encoding" && v != "trailers" {
- continue
- }
- sc.hpackEncoder.WriteField(hpack.HeaderField{Name: k, Value: v})
- }
- }
- if req.contentType != "" {
- sc.hpackEncoder.WriteField(hpack.HeaderField{Name: "content-type", Value: req.contentType})
- }
- if req.contentLength != "" {
- sc.hpackEncoder.WriteField(hpack.HeaderField{Name: "content-length", Value: req.contentLength})
- }
- headerBlock := sc.headerWriteBuf.Bytes()
- if len(headerBlock) > int(sc.maxWriteFrameSize) {
- // we'll need continuation ones.
- panic("TODO")
- }
- return sc.framer.WriteHeaders(HeadersFrameParam{
- StreamID: req.streamID,
- BlockFragment: headerBlock,
- EndStream: req.endStream,
- EndHeaders: true, // no continuation yet
- })
- }
- // called from handler goroutines.
- // h may be nil.
- func (sc *serverConn) write100ContinueHeaders(streamID uint32) {
- sc.serveG.checkNotOn()
- sc.writeFrame(frameWriteMsg{
- write: (*serverConn).write100ContinueHeadersFrame,
- v: &streamID,
- streamID: streamID,
- })
- }
- func (sc *serverConn) write100ContinueHeadersFrame(v interface{}) error {
- sc.writeG.check()
- streamID := *(v.(*uint32))
- sc.headerWriteBuf.Reset()
- sc.hpackEncoder.WriteField(hpack.HeaderField{Name: ":status", Value: "100"})
- return sc.framer.WriteHeaders(HeadersFrameParam{
- StreamID: streamID,
- BlockFragment: sc.headerWriteBuf.Bytes(),
- EndStream: false,
- EndHeaders: true,
- })
- }
- func (sc *serverConn) writeDataFrame(v interface{}) error {
- sc.writeG.check()
- rws := v.(*responseWriterState)
- return sc.framer.WriteData(rws.streamID, rws.curChunkIsFinal, rws.curChunk)
- }
- type windowUpdateReq struct {
- streamID uint32
- n uint32
- }
- // called from handler goroutines
- func (sc *serverConn) sendWindowUpdate(streamID uint32, n int) {
- const maxUint32 = 2147483647
- for n >= maxUint32 {
- sc.writeFrame(frameWriteMsg{
- write: (*serverConn).sendWindowUpdateInLoop,
- v: windowUpdateReq{streamID, maxUint32},
- streamID: streamID,
- })
- n -= maxUint32
- }
- if n > 0 {
- sc.writeFrame(frameWriteMsg{
- write: (*serverConn).sendWindowUpdateInLoop,
- v: windowUpdateReq{streamID, uint32(n)},
- streamID: streamID,
- })
- }
- }
- func (sc *serverConn) sendWindowUpdateInLoop(v interface{}) error {
- sc.writeG.check()
- wu := v.(windowUpdateReq)
- if err := sc.framer.WriteWindowUpdate(0, wu.n); err != nil {
- return err
- }
- if err := sc.framer.WriteWindowUpdate(wu.streamID, wu.n); err != nil {
- return err
- }
- return nil
- }
- type requestBody struct {
- sc *serverConn
- streamID uint32
- closed bool
- pipe *pipe // non-nil if we have a HTTP entity message body
- needsContinue bool // need to send a 100-continue
- }
- var errClosedBody = errors.New("body closed by handler")
- func (b *requestBody) Close() error {
- if b.pipe != nil {
- b.pipe.Close(errClosedBody)
- }
- b.closed = true
- return nil
- }
- func (b *requestBody) Read(p []byte) (n int, err error) {
- if b.needsContinue {
- b.needsContinue = false
- b.sc.write100ContinueHeaders(b.streamID)
- }
- if b.pipe == nil {
- return 0, io.EOF
- }
- n, err = b.pipe.Read(p)
- if n > 0 {
- b.sc.sendWindowUpdate(b.streamID, n)
- // TODO: tell b.sc to send back 'n' flow control quota credits to the sender
- }
- return
- }
- // responseWriter is the http.ResponseWriter implementation. It's
- // intentionally small (1 pointer wide) to minimize garbage. The
- // responseWriterState pointer inside is zeroed at the end of a
- // request (in handlerDone) and calls on the responseWriter thereafter
- // simply crash (caller's mistake), but the much larger responseWriterState
- // and buffers are reused between multiple requests.
- type responseWriter struct {
- rws *responseWriterState
- }
- // Optional http.ResponseWriter interfaces implemented.
- var (
- _ http.Flusher = (*responseWriter)(nil)
- _ stringWriter = (*responseWriter)(nil)
- // TODO: hijacker for websockets?
- )
- type responseWriterState struct {
- // immutable within a request:
- sc *serverConn
- streamID uint32
- req *http.Request
- body *requestBody // to close at end of request, if DATA frames didn't
- // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
- bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
- // mutated by http.Handler goroutine:
- handlerHeader http.Header // nil until called
- snapHeader http.Header // snapshot of handlerHeader at WriteHeader time
- wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
- status int // status code passed to WriteHeader
- sentHeader bool // have we sent the header frame?
- handlerDone bool // handler has finished
- curChunk []byte // current chunk we're writing
- curChunkIsFinal bool
- chunkWrittenCh chan error
- }
- type chunkWriter struct{ rws *responseWriterState }
- func (cw chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) }
- // writeChunk writes chunks from the bufio.Writer. But because
- // bufio.Writer may bypass its chunking, sometimes p may be
- // arbitrarily large.
- //
- // writeChunk is also responsible (on the first chunk) for sending the
- // HEADER response.
- func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
- if !rws.wroteHeader {
- rws.writeHeader(200)
- }
- if !rws.sentHeader {
- rws.sentHeader = true
- var ctype, clen string // implicit ones, if we can calculate it
- if rws.handlerDone && rws.snapHeader.Get("Content-Length") == "" {
- clen = strconv.Itoa(len(p))
- }
- if rws.snapHeader.Get("Content-Type") == "" {
- ctype = http.DetectContentType(p)
- }
- endStream := rws.handlerDone && len(p) == 0
- rws.sc.writeHeaders(headerWriteReq{
- streamID: rws.streamID,
- httpResCode: rws.status,
- h: rws.snapHeader,
- endStream: endStream,
- contentType: ctype,
- contentLength: clen,
- })
- if endStream {
- return
- }
- }
- if len(p) == 0 {
- if rws.handlerDone {
- rws.curChunk = nil
- rws.curChunkIsFinal = true
- rws.sc.writeFrame(frameWriteMsg{
- write: (*serverConn).writeDataFrame,
- cost: 0,
- streamID: rws.streamID,
- endStream: true,
- v: rws, // writeDataInLoop uses only rws.curChunk and rws.curChunkIsFinal
- })
- }
- return
- }
- for len(p) > 0 {
- chunk := p
- if len(chunk) > handlerChunkWriteSize {
- chunk = chunk[:handlerChunkWriteSize]
- }
- p = p[len(chunk):]
- rws.curChunk = chunk
- rws.curChunkIsFinal = rws.handlerDone && len(p) == 0
- // TODO: await flow control tokens for both stream and conn
- rws.sc.writeFrame(frameWriteMsg{
- write: (*serverConn).writeDataFrame,
- cost: uint32(len(chunk)),
- streamID: rws.streamID,
- endStream: rws.curChunkIsFinal,
- done: rws.chunkWrittenCh,
- v: rws, // writeDataInLoop uses only rws.curChunk and rws.curChunkIsFinal
- })
- err = <-rws.chunkWrittenCh // block until it's written
- if err != nil {
- break
- }
- n += len(chunk)
- }
- return
- }
- func (w *responseWriter) Flush() {
- rws := w.rws
- if rws == nil {
- panic("Header called after Handler finished")
- }
- if rws.bw.Buffered() > 0 {
- if err := rws.bw.Flush(); err != nil {
- // Ignore the error. The frame writer already knows.
- return
- }
- } else {
- // The bufio.Writer won't call chunkWriter.Write
- // (writeChunk with zero bytes, so we have to do it
- // ourselves to force the HTTP response header and/or
- // final DATA frame (with END_STREAM) to be sent.
- rws.writeChunk(nil)
- }
- }
- func (w *responseWriter) Header() http.Header {
- rws := w.rws
- if rws == nil {
- panic("Header called after Handler finished")
- }
- if rws.handlerHeader == nil {
- rws.handlerHeader = make(http.Header)
- }
- return rws.handlerHeader
- }
- func (w *responseWriter) WriteHeader(code int) {
- rws := w.rws
- if rws == nil {
- panic("WriteHeader called after Handler finished")
- }
- rws.writeHeader(code)
- }
- func (rws *responseWriterState) writeHeader(code int) {
- if !rws.wroteHeader {
- rws.wroteHeader = true
- rws.status = code
- if len(rws.handlerHeader) > 0 {
- rws.snapHeader = cloneHeader(rws.handlerHeader)
- }
- }
- }
- func cloneHeader(h http.Header) http.Header {
- h2 := make(http.Header, len(h))
- for k, vv := range h {
- vv2 := make([]string, len(vv))
- copy(vv2, vv)
- h2[k] = vv2
- }
- return h2
- }
- // The Life Of A Write is like this:
- //
- // TODO: copy/adapt the similar comment from Go's http server.go
- func (w *responseWriter) Write(p []byte) (n int, err error) {
- return w.write(len(p), p, "")
- }
- func (w *responseWriter) WriteString(s string) (n int, err error) {
- return w.write(len(s), nil, s)
- }
- // either dataB or dataS is non-zero.
- func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
- rws := w.rws
- if rws == nil {
- panic("Write called after Handler finished")
- }
- if !rws.wroteHeader {
- w.WriteHeader(200)
- }
- if dataB != nil {
- return rws.bw.Write(dataB)
- } else {
- return rws.bw.WriteString(dataS)
- }
- }
- func (w *responseWriter) handlerDone() {
- rws := w.rws
- if rws == nil {
- panic("handlerDone called twice")
- }
- rws.handlerDone = true
- w.Flush()
- w.rws = nil
- responseWriterStatePool.Put(rws)
- }
|