write.go 10 KB

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