tables.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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
  5. import (
  6. "fmt"
  7. )
  8. // headerFieldTable implements a list of HeaderFields.
  9. // This is used to implement the static and dynamic tables.
  10. type headerFieldTable struct {
  11. // For static tables, entries are never evicted.
  12. //
  13. // For dynamic tables, entries are evicted from ents[0] and added to the end.
  14. // Each entry has a unique id that starts at one and increments for each
  15. // entry that is added. This unique id is stable across evictions, meaning
  16. // it can be used as a pointer to a specific entry. As in hpack, unique ids
  17. // are 1-based. The unique id for ents[k] is k + evictCount + 1.
  18. //
  19. // Zero is not a valid unique id.
  20. //
  21. // evictCount should not overflow in any remotely practical situation. In
  22. // practice, we will have one dynamic table per HTTP/2 connection. If we
  23. // assume a very powerful server that handles 1M QPS per connection and each
  24. // request adds (then evicts) 100 entries from the table, it would still take
  25. // 2M years for evictCount to overflow.
  26. ents []HeaderField
  27. evictCount uint64
  28. // byName maps a HeaderField name to the unique id of the newest entry with
  29. // the same name. See above for a definition of "unique id".
  30. byName map[string]uint64
  31. // byNameValue maps a HeaderField name/value pair to the unique id of the newest
  32. // entry with the same name and value. See above for a definition of "unique id".
  33. byNameValue map[pairNameValue]uint64
  34. }
  35. type pairNameValue struct {
  36. name, value string
  37. }
  38. func (t *headerFieldTable) init() {
  39. t.byName = make(map[string]uint64)
  40. t.byNameValue = make(map[pairNameValue]uint64)
  41. }
  42. // len reports the number of entries in the table.
  43. func (t *headerFieldTable) len() int {
  44. return len(t.ents)
  45. }
  46. // addEntry adds a new entry.
  47. func (t *headerFieldTable) addEntry(f HeaderField) {
  48. id := uint64(t.len()) + t.evictCount + 1
  49. t.byName[f.Name] = id
  50. t.byNameValue[pairNameValue{f.Name, f.Value}] = id
  51. t.ents = append(t.ents, f)
  52. }
  53. // evictOldest evicts the n oldest entries in the table.
  54. func (t *headerFieldTable) evictOldest(n int) {
  55. if n > t.len() {
  56. panic(fmt.Sprintf("evictOldest(%v) on table with %v entries", n, t.len()))
  57. }
  58. for k := 0; k < n; k++ {
  59. f := t.ents[k]
  60. id := t.evictCount + uint64(k) + 1
  61. if t.byName[f.Name] == id {
  62. delete(t.byName, f.Name)
  63. }
  64. if p := (pairNameValue{f.Name, f.Value}); t.byNameValue[p] == id {
  65. delete(t.byNameValue, p)
  66. }
  67. }
  68. copy(t.ents, t.ents[n:])
  69. for k := t.len() - n; k < t.len(); k++ {
  70. t.ents[k] = HeaderField{} // so strings can be garbage collected
  71. }
  72. t.ents = t.ents[:t.len()-n]
  73. if t.evictCount+uint64(n) < t.evictCount {
  74. panic("evictCount overflow")
  75. }
  76. t.evictCount += uint64(n)
  77. }
  78. // search finds f in the table. If there is no match, i is 0.
  79. // If both name and value match, i is the matched index and nameValueMatch
  80. // becomes true. If only name matches, i points to that index and
  81. // nameValueMatch becomes false.
  82. //
  83. // The returned index is a 1-based HPACK index. For dynamic tables, HPACK says
  84. // that index 1 should be the newest entry, but t.ents[0] is the oldest entry,
  85. // meaning t.ents is reversed for dynamic tables. Hence, when t is a dynamic
  86. // table, the return value i actually refers to the entry t.ents[t.len()-i].
  87. //
  88. // All tables are assumed to be a dynamic tables except for the global staticTable.
  89. //
  90. // See Section 2.3.3.
  91. func (t *headerFieldTable) search(f HeaderField) (i uint64, nameValueMatch bool) {
  92. if !f.Sensitive {
  93. if id := t.byNameValue[pairNameValue{f.Name, f.Value}]; id != 0 {
  94. return t.idToIndex(id), true
  95. }
  96. }
  97. if id := t.byName[f.Name]; id != 0 {
  98. return t.idToIndex(id), false
  99. }
  100. return 0, false
  101. }
  102. // idToIndex converts a unique id to an HPACK index.
  103. // See Section 2.3.3.
  104. func (t *headerFieldTable) idToIndex(id uint64) uint64 {
  105. if id <= t.evictCount {
  106. panic(fmt.Sprintf("id (%v) <= evictCount (%v)", id, t.evictCount))
  107. }
  108. k := id - t.evictCount - 1 // convert id to an index t.ents[k]
  109. if t != staticTable {
  110. return uint64(t.len()) - k // dynamic table
  111. }
  112. return k + 1
  113. }
  114. var huffmanCodes = [256]uint32{
  115. 0x1ff8,
  116. 0x7fffd8,
  117. 0xfffffe2,
  118. 0xfffffe3,
  119. 0xfffffe4,
  120. 0xfffffe5,
  121. 0xfffffe6,
  122. 0xfffffe7,
  123. 0xfffffe8,
  124. 0xffffea,
  125. 0x3ffffffc,
  126. 0xfffffe9,
  127. 0xfffffea,
  128. 0x3ffffffd,
  129. 0xfffffeb,
  130. 0xfffffec,
  131. 0xfffffed,
  132. 0xfffffee,
  133. 0xfffffef,
  134. 0xffffff0,
  135. 0xffffff1,
  136. 0xffffff2,
  137. 0x3ffffffe,
  138. 0xffffff3,
  139. 0xffffff4,
  140. 0xffffff5,
  141. 0xffffff6,
  142. 0xffffff7,
  143. 0xffffff8,
  144. 0xffffff9,
  145. 0xffffffa,
  146. 0xffffffb,
  147. 0x14,
  148. 0x3f8,
  149. 0x3f9,
  150. 0xffa,
  151. 0x1ff9,
  152. 0x15,
  153. 0xf8,
  154. 0x7fa,
  155. 0x3fa,
  156. 0x3fb,
  157. 0xf9,
  158. 0x7fb,
  159. 0xfa,
  160. 0x16,
  161. 0x17,
  162. 0x18,
  163. 0x0,
  164. 0x1,
  165. 0x2,
  166. 0x19,
  167. 0x1a,
  168. 0x1b,
  169. 0x1c,
  170. 0x1d,
  171. 0x1e,
  172. 0x1f,
  173. 0x5c,
  174. 0xfb,
  175. 0x7ffc,
  176. 0x20,
  177. 0xffb,
  178. 0x3fc,
  179. 0x1ffa,
  180. 0x21,
  181. 0x5d,
  182. 0x5e,
  183. 0x5f,
  184. 0x60,
  185. 0x61,
  186. 0x62,
  187. 0x63,
  188. 0x64,
  189. 0x65,
  190. 0x66,
  191. 0x67,
  192. 0x68,
  193. 0x69,
  194. 0x6a,
  195. 0x6b,
  196. 0x6c,
  197. 0x6d,
  198. 0x6e,
  199. 0x6f,
  200. 0x70,
  201. 0x71,
  202. 0x72,
  203. 0xfc,
  204. 0x73,
  205. 0xfd,
  206. 0x1ffb,
  207. 0x7fff0,
  208. 0x1ffc,
  209. 0x3ffc,
  210. 0x22,
  211. 0x7ffd,
  212. 0x3,
  213. 0x23,
  214. 0x4,
  215. 0x24,
  216. 0x5,
  217. 0x25,
  218. 0x26,
  219. 0x27,
  220. 0x6,
  221. 0x74,
  222. 0x75,
  223. 0x28,
  224. 0x29,
  225. 0x2a,
  226. 0x7,
  227. 0x2b,
  228. 0x76,
  229. 0x2c,
  230. 0x8,
  231. 0x9,
  232. 0x2d,
  233. 0x77,
  234. 0x78,
  235. 0x79,
  236. 0x7a,
  237. 0x7b,
  238. 0x7ffe,
  239. 0x7fc,
  240. 0x3ffd,
  241. 0x1ffd,
  242. 0xffffffc,
  243. 0xfffe6,
  244. 0x3fffd2,
  245. 0xfffe7,
  246. 0xfffe8,
  247. 0x3fffd3,
  248. 0x3fffd4,
  249. 0x3fffd5,
  250. 0x7fffd9,
  251. 0x3fffd6,
  252. 0x7fffda,
  253. 0x7fffdb,
  254. 0x7fffdc,
  255. 0x7fffdd,
  256. 0x7fffde,
  257. 0xffffeb,
  258. 0x7fffdf,
  259. 0xffffec,
  260. 0xffffed,
  261. 0x3fffd7,
  262. 0x7fffe0,
  263. 0xffffee,
  264. 0x7fffe1,
  265. 0x7fffe2,
  266. 0x7fffe3,
  267. 0x7fffe4,
  268. 0x1fffdc,
  269. 0x3fffd8,
  270. 0x7fffe5,
  271. 0x3fffd9,
  272. 0x7fffe6,
  273. 0x7fffe7,
  274. 0xffffef,
  275. 0x3fffda,
  276. 0x1fffdd,
  277. 0xfffe9,
  278. 0x3fffdb,
  279. 0x3fffdc,
  280. 0x7fffe8,
  281. 0x7fffe9,
  282. 0x1fffde,
  283. 0x7fffea,
  284. 0x3fffdd,
  285. 0x3fffde,
  286. 0xfffff0,
  287. 0x1fffdf,
  288. 0x3fffdf,
  289. 0x7fffeb,
  290. 0x7fffec,
  291. 0x1fffe0,
  292. 0x1fffe1,
  293. 0x3fffe0,
  294. 0x1fffe2,
  295. 0x7fffed,
  296. 0x3fffe1,
  297. 0x7fffee,
  298. 0x7fffef,
  299. 0xfffea,
  300. 0x3fffe2,
  301. 0x3fffe3,
  302. 0x3fffe4,
  303. 0x7ffff0,
  304. 0x3fffe5,
  305. 0x3fffe6,
  306. 0x7ffff1,
  307. 0x3ffffe0,
  308. 0x3ffffe1,
  309. 0xfffeb,
  310. 0x7fff1,
  311. 0x3fffe7,
  312. 0x7ffff2,
  313. 0x3fffe8,
  314. 0x1ffffec,
  315. 0x3ffffe2,
  316. 0x3ffffe3,
  317. 0x3ffffe4,
  318. 0x7ffffde,
  319. 0x7ffffdf,
  320. 0x3ffffe5,
  321. 0xfffff1,
  322. 0x1ffffed,
  323. 0x7fff2,
  324. 0x1fffe3,
  325. 0x3ffffe6,
  326. 0x7ffffe0,
  327. 0x7ffffe1,
  328. 0x3ffffe7,
  329. 0x7ffffe2,
  330. 0xfffff2,
  331. 0x1fffe4,
  332. 0x1fffe5,
  333. 0x3ffffe8,
  334. 0x3ffffe9,
  335. 0xffffffd,
  336. 0x7ffffe3,
  337. 0x7ffffe4,
  338. 0x7ffffe5,
  339. 0xfffec,
  340. 0xfffff3,
  341. 0xfffed,
  342. 0x1fffe6,
  343. 0x3fffe9,
  344. 0x1fffe7,
  345. 0x1fffe8,
  346. 0x7ffff3,
  347. 0x3fffea,
  348. 0x3fffeb,
  349. 0x1ffffee,
  350. 0x1ffffef,
  351. 0xfffff4,
  352. 0xfffff5,
  353. 0x3ffffea,
  354. 0x7ffff4,
  355. 0x3ffffeb,
  356. 0x7ffffe6,
  357. 0x3ffffec,
  358. 0x3ffffed,
  359. 0x7ffffe7,
  360. 0x7ffffe8,
  361. 0x7ffffe9,
  362. 0x7ffffea,
  363. 0x7ffffeb,
  364. 0xffffffe,
  365. 0x7ffffec,
  366. 0x7ffffed,
  367. 0x7ffffee,
  368. 0x7ffffef,
  369. 0x7fffff0,
  370. 0x3ffffee,
  371. }
  372. var huffmanCodeLen = [256]uint8{
  373. 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
  374. 28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  375. 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6,
  376. 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10,
  377. 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  378. 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6,
  379. 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5,
  380. 6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28,
  381. 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
  382. 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
  383. 22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
  384. 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
  385. 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
  386. 19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
  387. 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
  388. 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26,
  389. }