hpack.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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.allowedMaxSize = maxDynamicTableSize
  89. d.dynTab.setMaxSize(maxDynamicTableSize)
  90. return d
  91. }
  92. // ErrStringLength is returned by Decoder.Write when the max string length
  93. // (as configured by Decoder.SetMaxStringLength) would be violated.
  94. var ErrStringLength = errors.New("hpack: string too long")
  95. // SetMaxStringLength sets the maximum size of a HeaderField name or
  96. // value string. If a string exceeds this length (even after any
  97. // decompression), Write will return ErrStringLength.
  98. // A value of 0 means unlimited and is the default from NewDecoder.
  99. func (d *Decoder) SetMaxStringLength(n int) {
  100. d.maxStrLen = n
  101. }
  102. // SetEmitFunc changes the callback used when new header fields
  103. // are decoded.
  104. // It must be non-nil. It does not affect EmitEnabled.
  105. func (d *Decoder) SetEmitFunc(emitFunc func(f HeaderField)) {
  106. d.emit = emitFunc
  107. }
  108. // SetEmitEnabled controls whether the emitFunc provided to NewDecoder
  109. // should be called. The default is true.
  110. //
  111. // This facility exists to let servers enforce MAX_HEADER_LIST_SIZE
  112. // while still decoding and keeping in-sync with decoder state, but
  113. // without doing unnecessary decompression or generating unnecessary
  114. // garbage for header fields past the limit.
  115. func (d *Decoder) SetEmitEnabled(v bool) { d.emitEnabled = v }
  116. // EmitEnabled reports whether calls to the emitFunc provided to NewDecoder
  117. // are currently enabled. The default is true.
  118. func (d *Decoder) EmitEnabled() bool { return d.emitEnabled }
  119. // TODO: add method *Decoder.Reset(maxSize, emitFunc) to let callers re-use Decoders and their
  120. // underlying buffers for garbage reasons.
  121. func (d *Decoder) SetMaxDynamicTableSize(v uint32) {
  122. d.dynTab.setMaxSize(v)
  123. }
  124. // SetAllowedMaxDynamicTableSize sets the upper bound that the encoded
  125. // stream (via dynamic table size updates) may set the maximum size
  126. // to.
  127. func (d *Decoder) SetAllowedMaxDynamicTableSize(v uint32) {
  128. d.dynTab.allowedMaxSize = v
  129. }
  130. type dynamicTable struct {
  131. // ents is the FIFO described at
  132. // http://http2.github.io/http2-spec/compression.html#rfc.section.2.3.2
  133. // The newest (low index) is append at the end, and items are
  134. // evicted from the front.
  135. ents []HeaderField
  136. size uint32
  137. maxSize uint32 // current maxSize
  138. allowedMaxSize uint32 // maxSize may go up to this, inclusive
  139. }
  140. func (dt *dynamicTable) setMaxSize(v uint32) {
  141. dt.maxSize = v
  142. dt.evict()
  143. }
  144. // TODO: change dynamicTable to be a struct with a slice and a size int field,
  145. // per http://http2.github.io/http2-spec/compression.html#rfc.section.4.1:
  146. //
  147. //
  148. // Then make add increment the size. maybe the max size should move from Decoder to
  149. // dynamicTable and add should return an ok bool if there was enough space.
  150. //
  151. // Later we'll need a remove operation on dynamicTable.
  152. func (dt *dynamicTable) add(f HeaderField) {
  153. dt.ents = append(dt.ents, f)
  154. dt.size += f.Size()
  155. dt.evict()
  156. }
  157. // If we're too big, evict old stuff (front of the slice)
  158. func (dt *dynamicTable) evict() {
  159. base := dt.ents // keep base pointer of slice
  160. for dt.size > dt.maxSize {
  161. dt.size -= dt.ents[0].Size()
  162. dt.ents = dt.ents[1:]
  163. }
  164. // Shift slice contents down if we evicted things.
  165. if len(dt.ents) != len(base) {
  166. copy(base, dt.ents)
  167. dt.ents = base[:len(dt.ents)]
  168. }
  169. }
  170. // Search searches f in the table. The return value i is 0 if there is
  171. // no name match. If there is name match or name/value match, i is the
  172. // index of that entry (1-based). If both name and value match,
  173. // nameValueMatch becomes true.
  174. func (dt *dynamicTable) search(f HeaderField) (i uint64, nameValueMatch bool) {
  175. l := len(dt.ents)
  176. for j := l - 1; j >= 0; j-- {
  177. ent := dt.ents[j]
  178. if ent.Name != f.Name {
  179. continue
  180. }
  181. if i == 0 {
  182. i = uint64(l - j)
  183. }
  184. if f.Sensitive {
  185. continue
  186. }
  187. if ent.Value != f.Value {
  188. continue
  189. }
  190. return uint64(l - j), true
  191. }
  192. return i, false
  193. }
  194. func (d *Decoder) maxTableIndex() int {
  195. return len(d.dynTab.ents) + len(staticTable)
  196. }
  197. func (d *Decoder) at(i uint64) (hf HeaderField, ok bool) {
  198. if i < 1 {
  199. return
  200. }
  201. if i > uint64(d.maxTableIndex()) {
  202. return
  203. }
  204. if i <= uint64(len(staticTable)) {
  205. return staticTable[i-1], true
  206. }
  207. dents := d.dynTab.ents
  208. return dents[len(dents)-(int(i)-len(staticTable))], true
  209. }
  210. // Decode decodes an entire block.
  211. //
  212. // TODO: remove this method and make it incremental later? This is
  213. // easier for debugging now.
  214. func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) {
  215. var hf []HeaderField
  216. saveFunc := d.emit
  217. defer func() { d.emit = saveFunc }()
  218. d.emit = func(f HeaderField) { hf = append(hf, f) }
  219. if _, err := d.Write(p); err != nil {
  220. return nil, err
  221. }
  222. if err := d.Close(); err != nil {
  223. return nil, err
  224. }
  225. return hf, nil
  226. }
  227. func (d *Decoder) Close() error {
  228. if d.saveBuf.Len() > 0 {
  229. d.saveBuf.Reset()
  230. return DecodingError{errors.New("truncated headers")}
  231. }
  232. return nil
  233. }
  234. func (d *Decoder) Write(p []byte) (n int, err error) {
  235. if len(p) == 0 {
  236. // Prevent state machine CPU attacks (making us redo
  237. // work up to the point of finding out we don't have
  238. // enough data)
  239. return
  240. }
  241. // Only copy the data if we have to. Optimistically assume
  242. // that p will contain a complete header block.
  243. if d.saveBuf.Len() == 0 {
  244. d.buf = p
  245. } else {
  246. d.saveBuf.Write(p)
  247. d.buf = d.saveBuf.Bytes()
  248. d.saveBuf.Reset()
  249. }
  250. for len(d.buf) > 0 {
  251. err = d.parseHeaderFieldRepr()
  252. if err == errNeedMore {
  253. // Extra paranoia, making sure saveBuf won't
  254. // get too large. All the varint and string
  255. // reading code earlier should already catch
  256. // overlong things and return ErrStringLength,
  257. // but keep this as a last resort.
  258. const varIntOverhead = 8 // conservative
  259. if d.maxStrLen != 0 && int64(len(d.buf)) > 2*(int64(d.maxStrLen)+varIntOverhead) {
  260. return 0, ErrStringLength
  261. }
  262. d.saveBuf.Write(d.buf)
  263. return len(p), nil
  264. }
  265. if err != nil {
  266. break
  267. }
  268. }
  269. return len(p), err
  270. }
  271. // errNeedMore is an internal sentinel error value that means the
  272. // buffer is truncated and we need to read more data before we can
  273. // continue parsing.
  274. var errNeedMore = errors.New("need more data")
  275. type indexType int
  276. const (
  277. indexedTrue indexType = iota
  278. indexedFalse
  279. indexedNever
  280. )
  281. func (v indexType) indexed() bool { return v == indexedTrue }
  282. func (v indexType) sensitive() bool { return v == indexedNever }
  283. // returns errNeedMore if there isn't enough data available.
  284. // any other error is fatal.
  285. // consumes d.buf iff it returns nil.
  286. // precondition: must be called with len(d.buf) > 0
  287. func (d *Decoder) parseHeaderFieldRepr() error {
  288. b := d.buf[0]
  289. switch {
  290. case b&128 != 0:
  291. // Indexed representation.
  292. // High bit set?
  293. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.1
  294. return d.parseFieldIndexed()
  295. case b&192 == 64:
  296. // 6.2.1 Literal Header Field with Incremental Indexing
  297. // 0b10xxxxxx: top two bits are 10
  298. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.1
  299. return d.parseFieldLiteral(6, indexedTrue)
  300. case b&240 == 0:
  301. // 6.2.2 Literal Header Field without Indexing
  302. // 0b0000xxxx: top four bits are 0000
  303. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.2
  304. return d.parseFieldLiteral(4, indexedFalse)
  305. case b&240 == 16:
  306. // 6.2.3 Literal Header Field never Indexed
  307. // 0b0001xxxx: top four bits are 0001
  308. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.3
  309. return d.parseFieldLiteral(4, indexedNever)
  310. case b&224 == 32:
  311. // 6.3 Dynamic Table Size Update
  312. // Top three bits are '001'.
  313. // http://http2.github.io/http2-spec/compression.html#rfc.section.6.3
  314. return d.parseDynamicTableSizeUpdate()
  315. }
  316. return DecodingError{errors.New("invalid encoding")}
  317. }
  318. // (same invariants and behavior as parseHeaderFieldRepr)
  319. func (d *Decoder) parseFieldIndexed() error {
  320. buf := d.buf
  321. idx, buf, err := readVarInt(7, buf)
  322. if err != nil {
  323. return err
  324. }
  325. hf, ok := d.at(idx)
  326. if !ok {
  327. return DecodingError{InvalidIndexError(idx)}
  328. }
  329. d.buf = buf
  330. return d.callEmit(HeaderField{Name: hf.Name, Value: hf.Value})
  331. }
  332. // (same invariants and behavior as parseHeaderFieldRepr)
  333. func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error {
  334. buf := d.buf
  335. nameIdx, buf, err := readVarInt(n, buf)
  336. if err != nil {
  337. return err
  338. }
  339. var hf HeaderField
  340. wantStr := d.emitEnabled || it.indexed()
  341. if nameIdx > 0 {
  342. ihf, ok := d.at(nameIdx)
  343. if !ok {
  344. return DecodingError{InvalidIndexError(nameIdx)}
  345. }
  346. hf.Name = ihf.Name
  347. } else {
  348. hf.Name, buf, err = d.readString(buf, wantStr)
  349. if err != nil {
  350. return err
  351. }
  352. }
  353. hf.Value, buf, err = d.readString(buf, wantStr)
  354. if err != nil {
  355. return err
  356. }
  357. d.buf = buf
  358. if it.indexed() {
  359. d.dynTab.add(hf)
  360. }
  361. hf.Sensitive = it.sensitive()
  362. return d.callEmit(hf)
  363. }
  364. func (d *Decoder) callEmit(hf HeaderField) error {
  365. if d.maxStrLen != 0 {
  366. if len(hf.Name) > d.maxStrLen || len(hf.Value) > d.maxStrLen {
  367. return ErrStringLength
  368. }
  369. }
  370. if d.emitEnabled {
  371. d.emit(hf)
  372. }
  373. return nil
  374. }
  375. // (same invariants and behavior as parseHeaderFieldRepr)
  376. func (d *Decoder) parseDynamicTableSizeUpdate() error {
  377. buf := d.buf
  378. size, buf, err := readVarInt(5, buf)
  379. if err != nil {
  380. return err
  381. }
  382. if size > uint64(d.dynTab.allowedMaxSize) {
  383. return DecodingError{errors.New("dynamic table size update too large")}
  384. }
  385. d.dynTab.setMaxSize(uint32(size))
  386. d.buf = buf
  387. return nil
  388. }
  389. var errVarintOverflow = DecodingError{errors.New("varint integer overflow")}
  390. // readVarInt reads an unsigned variable length integer off the
  391. // beginning of p. n is the parameter as described in
  392. // http://http2.github.io/http2-spec/compression.html#rfc.section.5.1.
  393. //
  394. // n must always be between 1 and 8.
  395. //
  396. // The returned remain buffer is either a smaller suffix of p, or err != nil.
  397. // The error is errNeedMore if p doesn't contain a complete integer.
  398. func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) {
  399. if n < 1 || n > 8 {
  400. panic("bad n")
  401. }
  402. if len(p) == 0 {
  403. return 0, p, errNeedMore
  404. }
  405. i = uint64(p[0])
  406. if n < 8 {
  407. i &= (1 << uint64(n)) - 1
  408. }
  409. if i < (1<<uint64(n))-1 {
  410. return i, p[1:], nil
  411. }
  412. origP := p
  413. p = p[1:]
  414. var m uint64
  415. for len(p) > 0 {
  416. b := p[0]
  417. p = p[1:]
  418. i += uint64(b&127) << m
  419. if b&128 == 0 {
  420. return i, p, nil
  421. }
  422. m += 7
  423. if m >= 63 { // TODO: proper overflow check. making this up.
  424. return 0, origP, errVarintOverflow
  425. }
  426. }
  427. return 0, origP, errNeedMore
  428. }
  429. // readString decodes an hpack string from p.
  430. //
  431. // wantStr is whether s will be used. If false, decompression and
  432. // []byte->string garbage are skipped if s will be ignored
  433. // anyway. This does mean that huffman decoding errors for non-indexed
  434. // strings past the MAX_HEADER_LIST_SIZE are ignored, but the server
  435. // is returning an error anyway, and because they're not indexed, the error
  436. // won't affect the decoding state.
  437. func (d *Decoder) readString(p []byte, wantStr bool) (s string, remain []byte, err error) {
  438. if len(p) == 0 {
  439. return "", p, errNeedMore
  440. }
  441. isHuff := p[0]&128 != 0
  442. strLen, p, err := readVarInt(7, p)
  443. if err != nil {
  444. return "", p, err
  445. }
  446. if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) {
  447. return "", nil, ErrStringLength
  448. }
  449. if uint64(len(p)) < strLen {
  450. return "", p, errNeedMore
  451. }
  452. if !isHuff {
  453. if wantStr {
  454. s = string(p[:strLen])
  455. }
  456. return s, p[strLen:], nil
  457. }
  458. if wantStr {
  459. buf := bufPool.Get().(*bytes.Buffer)
  460. buf.Reset() // don't trust others
  461. defer bufPool.Put(buf)
  462. if err := huffmanDecode(buf, d.maxStrLen, p[:strLen]); err != nil {
  463. buf.Reset()
  464. return "", nil, err
  465. }
  466. s = buf.String()
  467. buf.Reset() // be nice to GC
  468. }
  469. return s, p[strLen:], nil
  470. }