write.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. package http2
  5. import (
  6. "bytes"
  7. "fmt"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "time"
  12. "golang.org/x/net/http2/hpack"
  13. "golang.org/x/net/lex/httplex"
  14. )
  15. // writeFramer is implemented by any type that is used to write frames.
  16. type writeFramer interface {
  17. writeFrame(writeContext) error
  18. // staysWithinBuffer reports whether this writer promises that
  19. // it will only write less than or equal to size bytes, and it
  20. // won't Flush the write context.
  21. staysWithinBuffer(size int) bool
  22. }
  23. // writeContext is the interface needed by the various frame writer
  24. // types below. All the writeFrame methods below are scheduled via the
  25. // frame writing scheduler (see writeScheduler in writesched.go).
  26. //
  27. // This interface is implemented by *serverConn.
  28. //
  29. // TODO: decide whether to a) use this in the client code (which didn't
  30. // end up using this yet, because it has a simpler design, not
  31. // currently implementing priorities), or b) delete this and
  32. // make the server code a bit more concrete.
  33. type writeContext interface {
  34. Framer() *Framer
  35. Flush() error
  36. CloseConn() error
  37. // HeaderEncoder returns an HPACK encoder that writes to the
  38. // returned buffer.
  39. HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
  40. }
  41. // writeEndsStream reports whether w writes a frame that will transition
  42. // the stream to a half-closed local state. This returns false for RST_STREAM,
  43. // which closes the entire stream (not just the local half).
  44. func writeEndsStream(w writeFramer) bool {
  45. switch v := w.(type) {
  46. case *writeData:
  47. return v.endStream
  48. case *writeResHeaders:
  49. return v.endStream
  50. case nil:
  51. // This can only happen if the caller reuses w after it's
  52. // been intentionally nil'ed out to prevent use. Keep this
  53. // here to catch future refactoring breaking it.
  54. panic("writeEndsStream called on nil writeFramer")
  55. }
  56. return false
  57. }
  58. type flushFrameWriter struct{}
  59. func (flushFrameWriter) writeFrame(ctx writeContext) error {
  60. return ctx.Flush()
  61. }
  62. func (flushFrameWriter) staysWithinBuffer(max int) bool { return false }
  63. type writeSettings []Setting
  64. func (s writeSettings) staysWithinBuffer(max int) bool {
  65. const settingSize = 6 // uint16 + uint32
  66. return frameHeaderLen+settingSize*len(s) <= max
  67. }
  68. func (s writeSettings) writeFrame(ctx writeContext) error {
  69. return ctx.Framer().WriteSettings([]Setting(s)...)
  70. }
  71. type writeGoAway struct {
  72. maxStreamID uint32
  73. code ErrCode
  74. }
  75. func (p *writeGoAway) writeFrame(ctx writeContext) error {
  76. err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
  77. if p.code != 0 {
  78. ctx.Flush() // ignore error: we're hanging up on them anyway
  79. time.Sleep(50 * time.Millisecond)
  80. ctx.CloseConn()
  81. }
  82. return err
  83. }
  84. func (*writeGoAway) staysWithinBuffer(max int) bool { return false } // flushes
  85. type writeData struct {
  86. streamID uint32
  87. p []byte
  88. endStream bool
  89. }
  90. func (w *writeData) String() string {
  91. return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
  92. }
  93. func (w *writeData) writeFrame(ctx writeContext) error {
  94. return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
  95. }
  96. func (w *writeData) staysWithinBuffer(max int) bool {
  97. return frameHeaderLen+len(w.p) <= max
  98. }
  99. // handlerPanicRST is the message sent from handler goroutines when
  100. // the handler panics.
  101. type handlerPanicRST struct {
  102. StreamID uint32
  103. }
  104. func (hp handlerPanicRST) writeFrame(ctx writeContext) error {
  105. return ctx.Framer().WriteRSTStream(hp.StreamID, ErrCodeInternal)
  106. }
  107. func (hp handlerPanicRST) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
  108. func (se StreamError) writeFrame(ctx writeContext) error {
  109. return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
  110. }
  111. func (se StreamError) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
  112. type writePingAck struct{ pf *PingFrame }
  113. func (w writePingAck) writeFrame(ctx writeContext) error {
  114. return ctx.Framer().WritePing(true, w.pf.Data)
  115. }
  116. func (w writePingAck) staysWithinBuffer(max int) bool { return frameHeaderLen+len(w.pf.Data) <= max }
  117. type writeSettingsAck struct{}
  118. func (writeSettingsAck) writeFrame(ctx writeContext) error {
  119. return ctx.Framer().WriteSettingsAck()
  120. }
  121. func (writeSettingsAck) staysWithinBuffer(max int) bool { return frameHeaderLen <= max }
  122. // splitHeaderBlock splits headerBlock into fragments so that each fragment fits
  123. // in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true
  124. // for the first/last fragment, respectively.
  125. func splitHeaderBlock(ctx writeContext, headerBlock []byte, fn func(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error) error {
  126. // For now we're lazy and just pick the minimum MAX_FRAME_SIZE
  127. // that all peers must support (16KB). Later we could care
  128. // more and send larger frames if the peer advertised it, but
  129. // there's little point. Most headers are small anyway (so we
  130. // generally won't have CONTINUATION frames), and extra frames
  131. // only waste 9 bytes anyway.
  132. const maxFrameSize = 16384
  133. first := true
  134. for len(headerBlock) > 0 {
  135. frag := headerBlock
  136. if len(frag) > maxFrameSize {
  137. frag = frag[:maxFrameSize]
  138. }
  139. headerBlock = headerBlock[len(frag):]
  140. if err := fn(ctx, frag, first, len(headerBlock) == 0); err != nil {
  141. return err
  142. }
  143. first = false
  144. }
  145. return nil
  146. }
  147. // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
  148. // for HTTP response headers or trailers from a server handler.
  149. type writeResHeaders struct {
  150. streamID uint32
  151. httpResCode int // 0 means no ":status" line
  152. h http.Header // may be nil
  153. trailers []string // if non-nil, which keys of h to write. nil means all.
  154. endStream bool
  155. date string
  156. contentType string
  157. contentLength string
  158. }
  159. func encKV(enc *hpack.Encoder, k, v string) {
  160. if VerboseLogs {
  161. log.Printf("http2: server encoding header %q = %q", k, v)
  162. }
  163. enc.WriteField(hpack.HeaderField{Name: k, Value: v})
  164. }
  165. func (w *writeResHeaders) staysWithinBuffer(max int) bool {
  166. // TODO: this is a common one. It'd be nice to return true
  167. // here and get into the fast path if we could be clever and
  168. // calculate the size fast enough, or at least a conservative
  169. // uppper bound that usually fires. (Maybe if w.h and
  170. // w.trailers are nil, so we don't need to enumerate it.)
  171. // Otherwise I'm afraid that just calculating the length to
  172. // answer this question would be slower than the ~2µs benefit.
  173. return false
  174. }
  175. func (w *writeResHeaders) writeFrame(ctx writeContext) error {
  176. enc, buf := ctx.HeaderEncoder()
  177. buf.Reset()
  178. if w.httpResCode != 0 {
  179. encKV(enc, ":status", httpCodeString(w.httpResCode))
  180. }
  181. encodeHeaders(enc, w.h, w.trailers)
  182. if w.contentType != "" {
  183. encKV(enc, "content-type", w.contentType)
  184. }
  185. if w.contentLength != "" {
  186. encKV(enc, "content-length", w.contentLength)
  187. }
  188. if w.date != "" {
  189. encKV(enc, "date", w.date)
  190. }
  191. headerBlock := buf.Bytes()
  192. if len(headerBlock) == 0 && w.trailers == nil {
  193. panic("unexpected empty hpack")
  194. }
  195. return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
  196. }
  197. func (w *writeResHeaders) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
  198. if firstFrag {
  199. return ctx.Framer().WriteHeaders(HeadersFrameParam{
  200. StreamID: w.streamID,
  201. BlockFragment: frag,
  202. EndStream: w.endStream,
  203. EndHeaders: lastFrag,
  204. })
  205. } else {
  206. return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
  207. }
  208. }
  209. // writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames.
  210. type writePushPromise struct {
  211. streamID uint32 // pusher stream
  212. method string // for :method
  213. url *url.URL // for :scheme, :authority, :path
  214. h http.Header
  215. // Creates an ID for a pushed stream. This runs on serveG just before
  216. // the frame is written. The returned ID is copied to promisedID.
  217. allocatePromisedID func() (uint32, error)
  218. promisedID uint32
  219. }
  220. func (w *writePushPromise) staysWithinBuffer(max int) bool {
  221. // TODO: see writeResHeaders.staysWithinBuffer
  222. return false
  223. }
  224. func (w *writePushPromise) writeFrame(ctx writeContext) error {
  225. enc, buf := ctx.HeaderEncoder()
  226. buf.Reset()
  227. encKV(enc, ":method", w.method)
  228. encKV(enc, ":scheme", w.url.Scheme)
  229. encKV(enc, ":authority", w.url.Host)
  230. encKV(enc, ":path", w.url.RequestURI())
  231. encodeHeaders(enc, w.h, nil)
  232. headerBlock := buf.Bytes()
  233. if len(headerBlock) == 0 {
  234. panic("unexpected empty hpack")
  235. }
  236. return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
  237. }
  238. func (w *writePushPromise) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
  239. if firstFrag {
  240. return ctx.Framer().WritePushPromise(PushPromiseParam{
  241. StreamID: w.streamID,
  242. PromiseID: w.promisedID,
  243. BlockFragment: frag,
  244. EndHeaders: lastFrag,
  245. })
  246. } else {
  247. return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
  248. }
  249. }
  250. type write100ContinueHeadersFrame struct {
  251. streamID uint32
  252. }
  253. func (w write100ContinueHeadersFrame) writeFrame(ctx writeContext) error {
  254. enc, buf := ctx.HeaderEncoder()
  255. buf.Reset()
  256. encKV(enc, ":status", "100")
  257. return ctx.Framer().WriteHeaders(HeadersFrameParam{
  258. StreamID: w.streamID,
  259. BlockFragment: buf.Bytes(),
  260. EndStream: false,
  261. EndHeaders: true,
  262. })
  263. }
  264. func (w write100ContinueHeadersFrame) staysWithinBuffer(max int) bool {
  265. // Sloppy but conservative:
  266. return 9+2*(len(":status")+len("100")) <= max
  267. }
  268. type writeWindowUpdate struct {
  269. streamID uint32 // or 0 for conn-level
  270. n uint32
  271. }
  272. func (wu writeWindowUpdate) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
  273. func (wu writeWindowUpdate) writeFrame(ctx writeContext) error {
  274. return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
  275. }
  276. // encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
  277. // is encoded only only if k is in keys.
  278. func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
  279. if keys == nil {
  280. sorter := sorterPool.Get().(*sorter)
  281. // Using defer here, since the returned keys from the
  282. // sorter.Keys method is only valid until the sorter
  283. // is returned:
  284. defer sorterPool.Put(sorter)
  285. keys = sorter.Keys(h)
  286. }
  287. for _, k := range keys {
  288. vv := h[k]
  289. k = lowerHeader(k)
  290. if !validWireHeaderFieldName(k) {
  291. // Skip it as backup paranoia. Per
  292. // golang.org/issue/14048, these should
  293. // already be rejected at a higher level.
  294. continue
  295. }
  296. isTE := k == "transfer-encoding"
  297. for _, v := range vv {
  298. if !httplex.ValidHeaderFieldValue(v) {
  299. // TODO: return an error? golang.org/issue/14048
  300. // For now just omit it.
  301. continue
  302. }
  303. // TODO: more of "8.1.2.2 Connection-Specific Header Fields"
  304. if isTE && v != "trailers" {
  305. continue
  306. }
  307. encKV(enc, k, v)
  308. }
  309. }
  310. }