hpack.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 hpack implements HPACK, a compression format for
  5. // efficiently representing HTTP header fields in the context of HTTP/2.
  6. //
  7. // See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-09
  8. package hpack
  9. import (
  10. "bytes"
  11. "errors"
  12. "fmt"
  13. )
  14. // A DecodingError is something the spec defines as a decoding error.
  15. type DecodingError struct {
  16. Err error
  17. }
  18. func (de DecodingError) Error() string {
  19. return fmt.Sprintf("decoding error: %v", de.Err)
  20. }
  21. // An InvalidIndexError is returned when an encoder references a table
  22. // entry before the static table or after the end of the dynamic table.
  23. type InvalidIndexError int
  24. func (e InvalidIndexError) Error() string {
  25. return fmt.Sprintf("invalid indexed representation index %d", int(e))
  26. }
  27. // A HeaderField is a name-value pair. Both the name and value are
  28. // treated as opaque sequences of octets.
  29. type HeaderField struct {
  30. Name, Value string
  31. // Sensitive means that this header field should never be
  32. // indexed.
  33. Sensitive bool
  34. }
  35. // IsPseudo reports whether the header field is an http2 pseudo header.
  36. // That is, it reports whether it starts with a colon.
  37. // It is not otherwise guaranteed to be a valid pseudo header field,
  38. // though.
  39. func (hf HeaderField) IsPseudo() bool {
  40. return len(hf.Name) != 0 && hf.Name[0] == ':'
  41. }
  42. func (hf HeaderField) String() string {
  43. var suffix string
  44. if hf.Sensitive {
  45. suffix = " (sensitive)"
  46. }
  47. return fmt.Sprintf("header field %q = %q%s", hf.Name, hf.Value, suffix)
  48. }
  49. // Size returns the size of an entry per RFC 7541 section 4.1.
  50. func (hf HeaderField) Size() uint32 {
  51. // http://http2.github.io/http2-spec/compression.html#rfc.section.4.1
  52. // "The size of the dynamic table is the sum of the size of
  53. // its entries. The size of an entry is the sum of its name's
  54. // length in octets (as defined in Section 5.2), its value's
  55. // length in octets (see Section 5.2), plus 32. The size of
  56. // an entry is calculated using the length of the name and
  57. // value without any Huffman encoding applied."
  58. // This can overflow if somebody makes a large HeaderField
  59. // Name and/or Value by hand, but we don't care, because that
  60. // won't happen on the wire because the encoding doesn't allow
  61. // it.
  62. return uint32(len(hf.Name) + len(hf.Value) + 32)
  63. }
  64. // A Decoder is the decoding context for incremental processing of
  65. // header blocks.
  66. type Decoder struct {
  67. dynTab dynamicTable
  68. emit func(f HeaderField)
  69. emitEnabled bool // whether calls to emit are enabled
  70. maxStrLen int // 0 means unlimited
  71. // buf is the unparsed buffer. It's only written to
  72. // saveBuf if it was truncated in the middle of a header
  73. // block. Because it's usually not owned, we can only
  74. // process it under Write.
  75. buf []byte // not owned; only valid during Write
  76. // saveBuf is previous data passed to Write which we weren't able
  77. // to fully parse before. Unlike buf, we own this data.
  78. saveBuf bytes.Buffer
  79. }
  80. // NewDecoder returns a new decoder with the provided maximum dynamic
  81. // table size. The emitFunc will be called for each valid field
  82. // parsed, in the same goroutine as calls to Write, before Write returns.
  83. func NewDecoder(maxDynamicTableSize uint32, emitFunc func(f HeaderField)) *Decoder {
  84. d := &Decoder{
  85. emit: emitFunc,
  86. emitEnabled: true,
  87. }
  88. d.dynTab.table.init()
  89. d.dynTab.allowedMaxSize = maxDynamicTableSize
  90. d.dynTab.setMaxSize(maxDynamicTableSize)
  91. return d
  92. }
  93. // ErrStringLength is returned by Decoder.Write when the max string length
  94. // (as configured by Decoder.SetMaxStringLength) would be violated.
  95. var ErrStringLength = errors.New("hpack: string too long")
  96. // SetMaxStringLength sets the maximum size of a HeaderField name or
  97. // value string. If a string exceeds this length (even after any
  98. // decompression), Write will return ErrStringLength.
  99. // A value of 0 means unlimited and is the default from NewDecoder.
  100. func (d *Decoder) SetMaxStringLength(n int) {
  101. d.maxStrLen = n
  102. }
  103. // SetEmitFunc changes the callback used when new header fields
  104. // are decoded.
  105. // It must be non-nil. It does not affect EmitEnabled.
  106. func (d *Decoder) SetEmitFunc(emitFunc func(f HeaderField)) {
  107. d.emit = emitFunc
  108. }
  109. // SetEmitEnabled controls whether the emitFunc provided to NewDecoder
  110. // should be called. The default is true.
  111. //
  112. // This facility exists to let servers enforce MAX_HEADER_LIST_SIZE
  113. // while still decoding and keeping in-sync with decoder state, but
  114. // without doing unnecessary decompression or generating unnecessary
  115. // garbage for header fields past the limit.
  116. func (d *Decoder) SetEmitEnabled(v bool) { d.emitEnabled = v }
  117. // EmitEnabled reports whether calls to the emitFunc provided to NewDecoder
  118. // are currently enabled. The default is true.
  119. func (d *Decoder) EmitEnabled() bool { return d.emitEnabled }
  120. // TODO: add method *Decoder.Reset(maxSize, emitFunc) to let callers re-use Decoders and their
  121. // underlying buffers for garbage reasons.
  122. func (d *Decoder) SetMaxDynamicTableSize(v uint32) {
  123. d.dynTab.setMaxSize(v)
  124. }
  125. // SetAllowedMaxDynamicTableSize sets the upper bound that the encoded
  126. // stream (via dynamic table size updates) may set the maximum size
  127. // to.
  128. func (d *Decoder) SetAllowedMaxDynamicTableSize(v uint32) {
  129. d.dynTab.allowedMaxSize = v
  130. }
  131. type dynamicTable struct {
  132. // http://http2.github.io/http2-spec/compression.html#rfc.section.2.3.2
  133. table headerFieldTable
  134. size uint32 // in bytes
  135. maxSize uint32 // current maxSize
  136. allowedMaxSize uint32 // maxSize may go up to this, inclusive
  137. }
  138. func (dt *dynamicTable) setMaxSize(v uint32) {
  139. dt.maxSize = v
  140. dt.evict()
  141. }
  142. func (dt *dynamicTable) add(f HeaderField) {
  143. dt.table.addEntry(f)
  144. dt.size += f.Size()
  145. dt.evict()
  146. }
  147. // If we're too big, evict old stuff.
  148. func (dt *dynamicTable) evict() {
  149. var n int
  150. for dt.size > dt.maxSize && n < dt.table.len() {
  151. dt.size -= dt.table.ents[n].Size()
  152. n++
  153. }
  154. dt.table.evictOldest(n)
  155. }
  156. func (d *Decoder) maxTableIndex() int {
  157. // This should never overflow. RFC 7540 Section 6.5.2 limits the size of
  158. // the dynamic table to 2^32 bytes, where each entry will occupy more than
  159. // one byte. Further, the staticTable has a fixed, small length.
  160. return d.dynTab.table.len() + staticTable.len()
  161. }
  162. func (d *Decoder) at(i uint64) (hf HeaderField, ok bool) {
  163. // See Section 2.3.3.
  164. if i == 0 {
  165. return
  166. }
  167. if i <= uint64(staticTable.len()) {
  168. return staticTable.ents[i-1], true
  169. }
  170. if i > uint64(d.maxTableIndex()) {
  171. return
  172. }
  173. // In the dynamic table, newer entries have lower indices.
  174. // However, dt.ents[0] is the oldest entry. Hence, dt.ents is
  175. // the reversed dynamic table.
  176. dt := d.dynTab.table
  177. return dt.ents[dt.len()-(int(i)-staticTable.len())], true
  178. }
  179. // Decode decodes an entire block.
  180. //
  181. // TODO: remove this method and make it incremental later? This is
  182. // easier for debugging now.
  183. func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) {
  184. var hf []HeaderField
  185. saveFunc := d.emit
  186. defer func() { d.emit = saveFunc }()
  187. d.emit = func(f HeaderField) { hf = append(hf, f) }
  188. if _, err := d.Write(p); err != nil {
  189. return nil, err
  190. }
  191. if err := d.Close(); err != nil {
  192. return nil, err
  193. }
  194. return hf, nil
  195. }
  196. func (d *Decoder) Close() error {
  197. if d.saveBuf.Len() > 0 {
  198. d.saveBuf.Reset()
  199. return DecodingError{errors.New("truncated headers")}
  200. }
  201. return nil
  202. }
  203. func (d *Decoder) Write(p []byte) (n int, err error) {
  204. if len(p) == 0 {
  205. // Prevent state machine CPU attacks (making us redo
  206. // work up to the point of finding out we don't have
  207. // enough data)
  208. return
  209. }
  210. // Only copy the data if we have to. Optimistically assume
  211. // that p will contain a complete header block.
  212. if d.saveBuf.Len() == 0 {
  213. d.buf = p
  214. } else {
  215. d.saveBuf.Write(p)
  216. d.buf = d.saveBuf.Bytes()
  217. d.saveBuf.Reset()
  218. }
  219. for len(d.buf) > 0 {
  220. err = d.parseHeaderFieldRepr()
  221. if err == errNeedMore {
  222. // Extra paranoia, making sure saveBuf won't
  223. // get too large. All the varint and string
  224. // reading code earlier should already catch
  225. // overlong things and return ErrStringLength,
  226. // but keep this as a last resort.
  227. const varIntOverhead = 8 // conservative
  228. if d.maxStrLen != 0 && int64(len(d.buf)) > 2*(int64(d.maxStrLen)+varIntOverhead) {
  229. return 0, ErrStringLength
  230. }
  231. d.saveBuf.Write(d.buf)
  232. return len(p), nil
  233. }
  234. if err != nil {
  235. break
  236. }
  237. }
  238. return len(p), err
  239. }
  240. // errNeedMore is an internal sentinel error value that means the
  241. // buffer is truncated and we need to read more data before we can
  242. // continue parsing.
  243. var errNeedMore = errors.New("need more data")
  244. type indexType int
  245. const (
  246. indexedTrue indexType = iota
  247. indexedFalse
  248. indexedNever
  249. )
  250. func (v indexType) indexed() bool { return v == indexedTrue }
  251. func (v indexType) sensitive() bool { return v == indexedNever }
  252. // returns errNeedMore if there isn't enough data available.
  253. // any other error is fatal.
  254. // consumes d.buf iff it returns nil.
  255. // precondition: must be called with len(d.buf) > 0
  256. func (d *Decoder) parseHeaderFieldRepr() error {
  257. b := d.buf[0]
  258. switch {
  259. case b&128 != 0:
  260. // Indexed representation.
  261. // High bit set?
  262. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.1
  263. return d.parseFieldIndexed()
  264. case b&192 == 64:
  265. // 6.2.1 Literal Header Field with Incremental Indexing
  266. // 0b10xxxxxx: top two bits are 10
  267. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.1
  268. return d.parseFieldLiteral(6, indexedTrue)
  269. case b&240 == 0:
  270. // 6.2.2 Literal Header Field without Indexing
  271. // 0b0000xxxx: top four bits are 0000
  272. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.2
  273. return d.parseFieldLiteral(4, indexedFalse)
  274. case b&240 == 16:
  275. // 6.2.3 Literal Header Field never Indexed
  276. // 0b0001xxxx: top four bits are 0001
  277. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.3
  278. return d.parseFieldLiteral(4, indexedNever)
  279. case b&224 == 32:
  280. // 6.3 Dynamic Table Size Update
  281. // Top three bits are '001'.
  282. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.3
  283. return d.parseDynamicTableSizeUpdate()
  284. }
  285. return DecodingError{errors.New("invalid encoding")}
  286. }
  287. // (same invariants and behavior as parseHeaderFieldRepr)
  288. func (d *Decoder) parseFieldIndexed() error {
  289. buf := d.buf
  290. idx, buf, err := readVarInt(7, buf)
  291. if err != nil {
  292. return err
  293. }
  294. hf, ok := d.at(idx)
  295. if !ok {
  296. return DecodingError{InvalidIndexError(idx)}
  297. }
  298. d.buf = buf
  299. return d.callEmit(HeaderField{Name: hf.Name, Value: hf.Value})
  300. }
  301. // (same invariants and behavior as parseHeaderFieldRepr)
  302. func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error {
  303. buf := d.buf
  304. nameIdx, buf, err := readVarInt(n, buf)
  305. if err != nil {
  306. return err
  307. }
  308. var hf HeaderField
  309. wantStr := d.emitEnabled || it.indexed()
  310. if nameIdx > 0 {
  311. ihf, ok := d.at(nameIdx)
  312. if !ok {
  313. return DecodingError{InvalidIndexError(nameIdx)}
  314. }
  315. hf.Name = ihf.Name
  316. } else {
  317. hf.Name, buf, err = d.readString(buf, wantStr)
  318. if err != nil {
  319. return err
  320. }
  321. }
  322. hf.Value, buf, err = d.readString(buf, wantStr)
  323. if err != nil {
  324. return err
  325. }
  326. d.buf = buf
  327. if it.indexed() {
  328. d.dynTab.add(hf)
  329. }
  330. hf.Sensitive = it.sensitive()
  331. return d.callEmit(hf)
  332. }
  333. func (d *Decoder) callEmit(hf HeaderField) error {
  334. if d.maxStrLen != 0 {
  335. if len(hf.Name) > d.maxStrLen || len(hf.Value) > d.maxStrLen {
  336. return ErrStringLength
  337. }
  338. }
  339. if d.emitEnabled {
  340. d.emit(hf)
  341. }
  342. return nil
  343. }
  344. // (same invariants and behavior as parseHeaderFieldRepr)
  345. func (d *Decoder) parseDynamicTableSizeUpdate() error {
  346. buf := d.buf
  347. size, buf, err := readVarInt(5, buf)
  348. if err != nil {
  349. return err
  350. }
  351. if size > uint64(d.dynTab.allowedMaxSize) {
  352. return DecodingError{errors.New("dynamic table size update too large")}
  353. }
  354. d.dynTab.setMaxSize(uint32(size))
  355. d.buf = buf
  356. return nil
  357. }
  358. var errVarintOverflow = DecodingError{errors.New("varint integer overflow")}
  359. // readVarInt reads an unsigned variable length integer off the
  360. // beginning of p. n is the parameter as described in
  361. // http://http2.github.io/http2-spec/compression.html#rfc.section.5.1.
  362. //
  363. // n must always be between 1 and 8.
  364. //
  365. // The returned remain buffer is either a smaller suffix of p, or err != nil.
  366. // The error is errNeedMore if p doesn't contain a complete integer.
  367. func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) {
  368. if n < 1 || n > 8 {
  369. panic("bad n")
  370. }
  371. if len(p) == 0 {
  372. return 0, p, errNeedMore
  373. }
  374. i = uint64(p[0])
  375. if n < 8 {
  376. i &= (1 << uint64(n)) - 1
  377. }
  378. if i < (1<<uint64(n))-1 {
  379. return i, p[1:], nil
  380. }
  381. origP := p
  382. p = p[1:]
  383. var m uint64
  384. for len(p) > 0 {
  385. b := p[0]
  386. p = p[1:]
  387. i += uint64(b&127) << m
  388. if b&128 == 0 {
  389. return i, p, nil
  390. }
  391. m += 7
  392. if m >= 63 { // TODO: proper overflow check. making this up.
  393. return 0, origP, errVarintOverflow
  394. }
  395. }
  396. return 0, origP, errNeedMore
  397. }
  398. // readString decodes an hpack string from p.
  399. //
  400. // wantStr is whether s will be used. If false, decompression and
  401. // []byte->string garbage are skipped if s will be ignored
  402. // anyway. This does mean that huffman decoding errors for non-indexed
  403. // strings past the MAX_HEADER_LIST_SIZE are ignored, but the server
  404. // is returning an error anyway, and because they're not indexed, the error
  405. // won't affect the decoding state.
  406. func (d *Decoder) readString(p []byte, wantStr bool) (s string, remain []byte, err error) {
  407. if len(p) == 0 {
  408. return "", p, errNeedMore
  409. }
  410. isHuff := p[0]&128 != 0
  411. strLen, p, err := readVarInt(7, p)
  412. if err != nil {
  413. return "", p, err
  414. }
  415. if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) {
  416. return "", nil, ErrStringLength
  417. }
  418. if uint64(len(p)) < strLen {
  419. return "", p, errNeedMore
  420. }
  421. if !isHuff {
  422. if wantStr {
  423. s = string(p[:strLen])
  424. }
  425. return s, p[strLen:], nil
  426. }
  427. if wantStr {
  428. buf := bufPool.Get().(*bytes.Buffer)
  429. buf.Reset() // don't trust others
  430. defer bufPool.Put(buf)
  431. if err := huffmanDecode(buf, d.maxStrLen, p[:strLen]); err != nil {
  432. buf.Reset()
  433. return "", nil, err
  434. }
  435. s = buf.String()
  436. buf.Reset() // be nice to GC
  437. }
  438. return s, p[strLen:], nil
  439. }