server.go 99 KB

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