hpack.go 12 KB

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