Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

479 wiersze
10 KiB

  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. t.byName[f.Name] = 0
  63. }
  64. if p := (pairNameValue{f.Name, f.Value}); t.byNameValue[p] == id {
  65. t.byNameValue[p] = 0
  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
  89. // staticTable pointer.
  90. //
  91. // See Section 2.3.3.
  92. func (t *headerFieldTable) search(f HeaderField) (i uint64, nameValueMatch bool) {
  93. if !f.Sensitive {
  94. if id := t.byNameValue[pairNameValue{f.Name, f.Value}]; id != 0 {
  95. return t.idToIndex(id), true
  96. }
  97. }
  98. if id := t.byName[f.Name]; id != 0 {
  99. return t.idToIndex(id), false
  100. }
  101. return 0, false
  102. }
  103. // idToIndex converts a unique id to an HPACK index.
  104. // See Section 2.3.3.
  105. func (t *headerFieldTable) idToIndex(id uint64) uint64 {
  106. if id <= t.evictCount {
  107. panic(fmt.Sprintf("id (%v) <= evictCount (%v)", id, t.evictCount))
  108. }
  109. k := id - t.evictCount - 1 // convert id to an index t.ents[k]
  110. if t != staticTable {
  111. return uint64(t.len()) - k // dynamic table
  112. }
  113. return k + 1
  114. }
  115. func pair(name, value string) HeaderField {
  116. return HeaderField{Name: name, Value: value}
  117. }
  118. // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-07#appendix-B
  119. var staticTable = newStaticTable()
  120. func newStaticTable() *headerFieldTable {
  121. t := &headerFieldTable{}
  122. t.init()
  123. t.addEntry(pair(":authority", ""))
  124. t.addEntry(pair(":method", "GET"))
  125. t.addEntry(pair(":method", "POST"))
  126. t.addEntry(pair(":path", "/"))
  127. t.addEntry(pair(":path", "/index.html"))
  128. t.addEntry(pair(":scheme", "http"))
  129. t.addEntry(pair(":scheme", "https"))
  130. t.addEntry(pair(":status", "200"))
  131. t.addEntry(pair(":status", "204"))
  132. t.addEntry(pair(":status", "206"))
  133. t.addEntry(pair(":status", "304"))
  134. t.addEntry(pair(":status", "400"))
  135. t.addEntry(pair(":status", "404"))
  136. t.addEntry(pair(":status", "500"))
  137. t.addEntry(pair("accept-charset", ""))
  138. t.addEntry(pair("accept-encoding", "gzip, deflate"))
  139. t.addEntry(pair("accept-language", ""))
  140. t.addEntry(pair("accept-ranges", ""))
  141. t.addEntry(pair("accept", ""))
  142. t.addEntry(pair("access-control-allow-origin", ""))
  143. t.addEntry(pair("age", ""))
  144. t.addEntry(pair("allow", ""))
  145. t.addEntry(pair("authorization", ""))
  146. t.addEntry(pair("cache-control", ""))
  147. t.addEntry(pair("content-disposition", ""))
  148. t.addEntry(pair("content-encoding", ""))
  149. t.addEntry(pair("content-language", ""))
  150. t.addEntry(pair("content-length", ""))
  151. t.addEntry(pair("content-location", ""))
  152. t.addEntry(pair("content-range", ""))
  153. t.addEntry(pair("content-type", ""))
  154. t.addEntry(pair("cookie", ""))
  155. t.addEntry(pair("date", ""))
  156. t.addEntry(pair("etag", ""))
  157. t.addEntry(pair("expect", ""))
  158. t.addEntry(pair("expires", ""))
  159. t.addEntry(pair("from", ""))
  160. t.addEntry(pair("host", ""))
  161. t.addEntry(pair("if-match", ""))
  162. t.addEntry(pair("if-modified-since", ""))
  163. t.addEntry(pair("if-none-match", ""))
  164. t.addEntry(pair("if-range", ""))
  165. t.addEntry(pair("if-unmodified-since", ""))
  166. t.addEntry(pair("last-modified", ""))
  167. t.addEntry(pair("link", ""))
  168. t.addEntry(pair("location", ""))
  169. t.addEntry(pair("max-forwards", ""))
  170. t.addEntry(pair("proxy-authenticate", ""))
  171. t.addEntry(pair("proxy-authorization", ""))
  172. t.addEntry(pair("range", ""))
  173. t.addEntry(pair("referer", ""))
  174. t.addEntry(pair("refresh", ""))
  175. t.addEntry(pair("retry-after", ""))
  176. t.addEntry(pair("server", ""))
  177. t.addEntry(pair("set-cookie", ""))
  178. t.addEntry(pair("strict-transport-security", ""))
  179. t.addEntry(pair("transfer-encoding", ""))
  180. t.addEntry(pair("user-agent", ""))
  181. t.addEntry(pair("vary", ""))
  182. t.addEntry(pair("via", ""))
  183. t.addEntry(pair("www-authenticate", ""))
  184. return t
  185. }
  186. var huffmanCodes = [256]uint32{
  187. 0x1ff8,
  188. 0x7fffd8,
  189. 0xfffffe2,
  190. 0xfffffe3,
  191. 0xfffffe4,
  192. 0xfffffe5,
  193. 0xfffffe6,
  194. 0xfffffe7,
  195. 0xfffffe8,
  196. 0xffffea,
  197. 0x3ffffffc,
  198. 0xfffffe9,
  199. 0xfffffea,
  200. 0x3ffffffd,
  201. 0xfffffeb,
  202. 0xfffffec,
  203. 0xfffffed,
  204. 0xfffffee,
  205. 0xfffffef,
  206. 0xffffff0,
  207. 0xffffff1,
  208. 0xffffff2,
  209. 0x3ffffffe,
  210. 0xffffff3,
  211. 0xffffff4,
  212. 0xffffff5,
  213. 0xffffff6,
  214. 0xffffff7,
  215. 0xffffff8,
  216. 0xffffff9,
  217. 0xffffffa,
  218. 0xffffffb,
  219. 0x14,
  220. 0x3f8,
  221. 0x3f9,
  222. 0xffa,
  223. 0x1ff9,
  224. 0x15,
  225. 0xf8,
  226. 0x7fa,
  227. 0x3fa,
  228. 0x3fb,
  229. 0xf9,
  230. 0x7fb,
  231. 0xfa,
  232. 0x16,
  233. 0x17,
  234. 0x18,
  235. 0x0,
  236. 0x1,
  237. 0x2,
  238. 0x19,
  239. 0x1a,
  240. 0x1b,
  241. 0x1c,
  242. 0x1d,
  243. 0x1e,
  244. 0x1f,
  245. 0x5c,
  246. 0xfb,
  247. 0x7ffc,
  248. 0x20,
  249. 0xffb,
  250. 0x3fc,
  251. 0x1ffa,
  252. 0x21,
  253. 0x5d,
  254. 0x5e,
  255. 0x5f,
  256. 0x60,
  257. 0x61,
  258. 0x62,
  259. 0x63,
  260. 0x64,
  261. 0x65,
  262. 0x66,
  263. 0x67,
  264. 0x68,
  265. 0x69,
  266. 0x6a,
  267. 0x6b,
  268. 0x6c,
  269. 0x6d,
  270. 0x6e,
  271. 0x6f,
  272. 0x70,
  273. 0x71,
  274. 0x72,
  275. 0xfc,
  276. 0x73,
  277. 0xfd,
  278. 0x1ffb,
  279. 0x7fff0,
  280. 0x1ffc,
  281. 0x3ffc,
  282. 0x22,
  283. 0x7ffd,
  284. 0x3,
  285. 0x23,
  286. 0x4,
  287. 0x24,
  288. 0x5,
  289. 0x25,
  290. 0x26,
  291. 0x27,
  292. 0x6,
  293. 0x74,
  294. 0x75,
  295. 0x28,
  296. 0x29,
  297. 0x2a,
  298. 0x7,
  299. 0x2b,
  300. 0x76,
  301. 0x2c,
  302. 0x8,
  303. 0x9,
  304. 0x2d,
  305. 0x77,
  306. 0x78,
  307. 0x79,
  308. 0x7a,
  309. 0x7b,
  310. 0x7ffe,
  311. 0x7fc,
  312. 0x3ffd,
  313. 0x1ffd,
  314. 0xffffffc,
  315. 0xfffe6,
  316. 0x3fffd2,
  317. 0xfffe7,
  318. 0xfffe8,
  319. 0x3fffd3,
  320. 0x3fffd4,
  321. 0x3fffd5,
  322. 0x7fffd9,
  323. 0x3fffd6,
  324. 0x7fffda,
  325. 0x7fffdb,
  326. 0x7fffdc,
  327. 0x7fffdd,
  328. 0x7fffde,
  329. 0xffffeb,
  330. 0x7fffdf,
  331. 0xffffec,
  332. 0xffffed,
  333. 0x3fffd7,
  334. 0x7fffe0,
  335. 0xffffee,
  336. 0x7fffe1,
  337. 0x7fffe2,
  338. 0x7fffe3,
  339. 0x7fffe4,
  340. 0x1fffdc,
  341. 0x3fffd8,
  342. 0x7fffe5,
  343. 0x3fffd9,
  344. 0x7fffe6,
  345. 0x7fffe7,
  346. 0xffffef,
  347. 0x3fffda,
  348. 0x1fffdd,
  349. 0xfffe9,
  350. 0x3fffdb,
  351. 0x3fffdc,
  352. 0x7fffe8,
  353. 0x7fffe9,
  354. 0x1fffde,
  355. 0x7fffea,
  356. 0x3fffdd,
  357. 0x3fffde,
  358. 0xfffff0,
  359. 0x1fffdf,
  360. 0x3fffdf,
  361. 0x7fffeb,
  362. 0x7fffec,
  363. 0x1fffe0,
  364. 0x1fffe1,
  365. 0x3fffe0,
  366. 0x1fffe2,
  367. 0x7fffed,
  368. 0x3fffe1,
  369. 0x7fffee,
  370. 0x7fffef,
  371. 0xfffea,
  372. 0x3fffe2,
  373. 0x3fffe3,
  374. 0x3fffe4,
  375. 0x7ffff0,
  376. 0x3fffe5,
  377. 0x3fffe6,
  378. 0x7ffff1,
  379. 0x3ffffe0,
  380. 0x3ffffe1,
  381. 0xfffeb,
  382. 0x7fff1,
  383. 0x3fffe7,
  384. 0x7ffff2,
  385. 0x3fffe8,
  386. 0x1ffffec,
  387. 0x3ffffe2,
  388. 0x3ffffe3,
  389. 0x3ffffe4,
  390. 0x7ffffde,
  391. 0x7ffffdf,
  392. 0x3ffffe5,
  393. 0xfffff1,
  394. 0x1ffffed,
  395. 0x7fff2,
  396. 0x1fffe3,
  397. 0x3ffffe6,
  398. 0x7ffffe0,
  399. 0x7ffffe1,
  400. 0x3ffffe7,
  401. 0x7ffffe2,
  402. 0xfffff2,
  403. 0x1fffe4,
  404. 0x1fffe5,
  405. 0x3ffffe8,
  406. 0x3ffffe9,
  407. 0xffffffd,
  408. 0x7ffffe3,
  409. 0x7ffffe4,
  410. 0x7ffffe5,
  411. 0xfffec,
  412. 0xfffff3,
  413. 0xfffed,
  414. 0x1fffe6,
  415. 0x3fffe9,
  416. 0x1fffe7,
  417. 0x1fffe8,
  418. 0x7ffff3,
  419. 0x3fffea,
  420. 0x3fffeb,
  421. 0x1ffffee,
  422. 0x1ffffef,
  423. 0xfffff4,
  424. 0xfffff5,
  425. 0x3ffffea,
  426. 0x7ffff4,
  427. 0x3ffffeb,
  428. 0x7ffffe6,
  429. 0x3ffffec,
  430. 0x3ffffed,
  431. 0x7ffffe7,
  432. 0x7ffffe8,
  433. 0x7ffffe9,
  434. 0x7ffffea,
  435. 0x7ffffeb,
  436. 0xffffffe,
  437. 0x7ffffec,
  438. 0x7ffffed,
  439. 0x7ffffee,
  440. 0x7ffffef,
  441. 0x7fffff0,
  442. 0x3ffffee,
  443. }
  444. var huffmanCodeLen = [256]uint8{
  445. 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
  446. 28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  447. 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6,
  448. 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10,
  449. 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  450. 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6,
  451. 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5,
  452. 6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28,
  453. 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
  454. 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
  455. 22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
  456. 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
  457. 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
  458. 19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
  459. 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
  460. 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26,
  461. }