hpack.go 15 KB

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