raiha 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (347) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/LICENSE +202 -0
  4. data/README.md +94 -22
  5. data/docs/configuration.md +227 -0
  6. data/docs/development.md +99 -0
  7. data/docs/fiber-scheduler.md +591 -0
  8. data/docs/getting-started.md +190 -0
  9. data/docs/http3-client.md +303 -0
  10. data/docs/http3-server.md +301 -0
  11. data/docs/platforms.md +98 -0
  12. data/docs/qlog.md +86 -0
  13. data/docs/quic.md +268 -0
  14. data/docs/threads.md +127 -0
  15. data/examples/_certificate.rb +55 -0
  16. data/examples/high_level_loopback.rb +59 -0
  17. data/examples/http3_get_public.rb +68 -0
  18. data/examples/in_memory_handshake.rb +38 -0
  19. data/examples/qlog_output.rb +58 -0
  20. data/examples/qlog_roundtrips.rb +66 -0
  21. data/examples/quic_gem_loopback.rb +62 -0
  22. data/examples/stream_echo.rb +68 -0
  23. data/examples/udp_handshake.rb +76 -0
  24. data/lib/raiha/http3/action.rb +21 -0
  25. data/lib/raiha/http3/capsule.rb +86 -0
  26. data/lib/raiha/http3/connection.rb +983 -0
  27. data/lib/raiha/http3/error.rb +24 -0
  28. data/lib/raiha/http3/error_code.rb +27 -0
  29. data/lib/raiha/http3/event.rb +63 -0
  30. data/lib/raiha/http3/frame.rb +148 -0
  31. data/lib/raiha/http3/frame_reader.rb +114 -0
  32. data/lib/raiha/http3/message.rb +137 -0
  33. data/lib/raiha/http3/output.rb +61 -0
  34. data/lib/raiha/http3/priority.rb +52 -0
  35. data/lib/raiha/http3/qlog.rb +145 -0
  36. data/lib/raiha/http3/qpack/decoder.rb +302 -0
  37. data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
  38. data/lib/raiha/http3/qpack/encoder.rb +94 -0
  39. data/lib/raiha/http3/qpack/error.rb +39 -0
  40. data/lib/raiha/http3/qpack/error_code.rb +13 -0
  41. data/lib/raiha/http3/qpack/field.rb +15 -0
  42. data/lib/raiha/http3/qpack/huffman.rb +397 -0
  43. data/lib/raiha/http3/qpack/reader.rb +82 -0
  44. data/lib/raiha/http3/qpack/result.rb +26 -0
  45. data/lib/raiha/http3/qpack/static_table.rb +139 -0
  46. data/lib/raiha/http3/qpack/writer.rb +44 -0
  47. data/lib/raiha/http3/qpack.rb +26 -0
  48. data/lib/raiha/http3/receiving.rb +46 -0
  49. data/lib/raiha/http3/sending.rb +23 -0
  50. data/lib/raiha/http3/session.rb +31 -0
  51. data/lib/raiha/http3/settings.rb +81 -0
  52. data/lib/raiha/http3/structured_field.rb +120 -0
  53. data/lib/raiha/http3/tracer.rb +26 -0
  54. data/lib/raiha/http3/varint.rb +45 -0
  55. data/lib/raiha/http3.rb +19 -0
  56. data/lib/raiha/http3_binding.rb +139 -0
  57. data/lib/raiha/namespace.rb +11 -0
  58. data/lib/raiha/quic/config.rb +229 -0
  59. data/lib/raiha/quic/congestion/algorithms.rb +17 -0
  60. data/lib/raiha/quic/congestion/cubic.rb +258 -0
  61. data/lib/raiha/quic/congestion/new_reno.rb +161 -0
  62. data/lib/raiha/quic/congestion/pacing.rb +21 -0
  63. data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
  64. data/lib/raiha/quic/congestion/startup.rb +13 -0
  65. data/lib/raiha/quic/congestion.rb +15 -0
  66. data/lib/raiha/quic/connection.rb +2022 -0
  67. data/lib/raiha/quic/connection_ids.rb +62 -0
  68. data/lib/raiha/quic/crypto/aead.rb +68 -0
  69. data/lib/raiha/quic/crypto/header_protection.rb +42 -0
  70. data/lib/raiha/quic/crypto/initial.rb +23 -0
  71. data/lib/raiha/quic/crypto/keys.rb +32 -0
  72. data/lib/raiha/quic/crypto/mac.rb +15 -0
  73. data/lib/raiha/quic/crypto/random.rb +13 -0
  74. data/lib/raiha/quic/crypto.rb +18 -0
  75. data/lib/raiha/quic/crypto_stream.rb +161 -0
  76. data/lib/raiha/quic/datagrams.rb +91 -0
  77. data/lib/raiha/quic/endpoint.rb +785 -0
  78. data/lib/raiha/quic/error.rb +61 -0
  79. data/lib/raiha/quic/error_code.rb +34 -0
  80. data/lib/raiha/quic/event.rb +59 -0
  81. data/lib/raiha/quic/issued_connection_ids.rb +137 -0
  82. data/lib/raiha/quic/kept_packets.rb +41 -0
  83. data/lib/raiha/quic/mtu_search.rb +75 -0
  84. data/lib/raiha/quic/negotiation.rb +143 -0
  85. data/lib/raiha/quic/new_token.rb +55 -0
  86. data/lib/raiha/quic/pacer.rb +52 -0
  87. data/lib/raiha/quic/packet_protection.rb +310 -0
  88. data/lib/raiha/quic/path.rb +473 -0
  89. data/lib/raiha/quic/peer_connection_ids.rb +160 -0
  90. data/lib/raiha/quic/qlog.rb +260 -0
  91. data/lib/raiha/quic/ranges.rb +32 -0
  92. data/lib/raiha/quic/recovery/duty.rb +102 -0
  93. data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
  94. data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
  95. data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
  96. data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
  97. data/lib/raiha/quic/recovery.rb +641 -0
  98. data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
  99. data/lib/raiha/quic/retry_token.rb +54 -0
  100. data/lib/raiha/quic/route.rb +37 -0
  101. data/lib/raiha/quic/scheduler.rb +112 -0
  102. data/lib/raiha/quic/session.rb +47 -0
  103. data/lib/raiha/quic/stateless_reset.rb +38 -0
  104. data/lib/raiha/quic/stats.rb +24 -0
  105. data/lib/raiha/quic/streams/receive_window.rb +119 -0
  106. data/lib/raiha/quic/streams/receiver.rb +217 -0
  107. data/lib/raiha/quic/streams/sender.rb +200 -0
  108. data/lib/raiha/quic/streams.rb +687 -0
  109. data/lib/raiha/quic/test/bottleneck.rb +55 -0
  110. data/lib/raiha/quic/test/congestion_contract.rb +106 -0
  111. data/lib/raiha/quic/test/network.rb +244 -0
  112. data/lib/raiha/quic/test/random_loss.rb +34 -0
  113. data/lib/raiha/quic/test/reorder.rb +51 -0
  114. data/lib/raiha/quic/test/server.rb +54 -0
  115. data/lib/raiha/quic/test/vectors.rb +337 -0
  116. data/lib/raiha/quic/test/virtual_clock.rb +21 -0
  117. data/lib/raiha/quic/test.rb +17 -0
  118. data/lib/raiha/quic/tls_binding.rb +179 -0
  119. data/lib/raiha/quic/token.rb +28 -0
  120. data/lib/raiha/quic/tracer.rb +127 -0
  121. data/lib/raiha/quic/transmit.rb +14 -0
  122. data/lib/raiha/quic/transport_parameters.rb +244 -0
  123. data/lib/raiha/quic/version.rb +63 -0
  124. data/lib/raiha/quic/wire/codec.rb +136 -0
  125. data/lib/raiha/quic/wire/frame.rb +521 -0
  126. data/lib/raiha/quic/wire/header.rb +90 -0
  127. data/lib/raiha/quic/wire/packet.rb +130 -0
  128. data/lib/raiha/quic/wire/packet_number.rb +47 -0
  129. data/lib/raiha/quic/wire/retry.rb +62 -0
  130. data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
  131. data/lib/raiha/quic/wire.rb +9 -0
  132. data/lib/raiha/quic.rb +46 -0
  133. data/lib/raiha/runtime/addresses.rb +35 -0
  134. data/lib/raiha/runtime/admission.rb +37 -0
  135. data/lib/raiha/runtime/client.rb +190 -0
  136. data/lib/raiha/runtime/driver.rb +165 -0
  137. data/lib/raiha/runtime/error.rb +16 -0
  138. data/lib/raiha/runtime/http3.rb +363 -0
  139. data/lib/raiha/runtime/http3_fields.rb +38 -0
  140. data/lib/raiha/runtime/http3_server.rb +416 -0
  141. data/lib/raiha/runtime/kept.rb +36 -0
  142. data/lib/raiha/runtime/qlog.rb +204 -0
  143. data/lib/raiha/runtime/rack.rb +180 -0
  144. data/lib/raiha/runtime/server.rb +334 -0
  145. data/lib/raiha/runtime/session.rb +447 -0
  146. data/lib/raiha/runtime/threaded/client.rb +161 -0
  147. data/lib/raiha/runtime/threaded/driver.rb +230 -0
  148. data/lib/raiha/runtime/threaded/http3.rb +287 -0
  149. data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
  150. data/lib/raiha/runtime/threaded/server.rb +290 -0
  151. data/lib/raiha/runtime/threaded/session.rb +604 -0
  152. data/lib/raiha/runtime/threaded.rb +45 -0
  153. data/lib/raiha/runtime/udp.rb +410 -0
  154. data/lib/raiha/runtime/worker.rb +138 -0
  155. data/lib/raiha/runtime/workers.rb +248 -0
  156. data/lib/raiha/runtime.rb +37 -0
  157. data/lib/raiha/tls/anti_replay.rb +139 -0
  158. data/lib/raiha/tls/cipher_suite.rb +62 -0
  159. data/lib/raiha/tls/codec/error.rb +43 -0
  160. data/lib/raiha/tls/codec/reader.rb +95 -0
  161. data/lib/raiha/tls/codec/writer.rb +81 -0
  162. data/lib/raiha/tls/codec.rb +5 -0
  163. data/lib/raiha/tls/config.rb +318 -0
  164. data/lib/raiha/tls/crypto/aead.rb +64 -0
  165. data/lib/raiha/tls/crypto/certificate.rb +28 -0
  166. data/lib/raiha/tls/crypto/digest.rb +30 -0
  167. data/lib/raiha/tls/crypto/ecdh.rb +74 -0
  168. data/lib/raiha/tls/crypto/hash.rb +22 -0
  169. data/lib/raiha/tls/crypto/hkdf.rb +28 -0
  170. data/lib/raiha/tls/crypto/signature.rb +87 -0
  171. data/lib/raiha/tls/crypto.rb +19 -0
  172. data/lib/raiha/tls/error.rb +36 -0
  173. data/lib/raiha/tls/event.rb +45 -0
  174. data/lib/raiha/tls/extension/alpn.rb +35 -0
  175. data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
  176. data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
  177. data/lib/raiha/tls/extension/cookie.rb +27 -0
  178. data/lib/raiha/tls/extension/early_data.rb +54 -0
  179. data/lib/raiha/tls/extension/key_share.rb +90 -0
  180. data/lib/raiha/tls/extension/list.rb +49 -0
  181. data/lib/raiha/tls/extension/opaque.rb +21 -0
  182. data/lib/raiha/tls/extension/placement.rb +55 -0
  183. data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
  184. data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
  185. data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
  186. data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
  187. data/lib/raiha/tls/extension/registry.rb +50 -0
  188. data/lib/raiha/tls/extension/server_name.rb +63 -0
  189. data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
  190. data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
  191. data/lib/raiha/tls/extension/supported_groups.rb +32 -0
  192. data/lib/raiha/tls/extension/supported_versions.rb +53 -0
  193. data/lib/raiha/tls/extension.rb +32 -0
  194. data/lib/raiha/tls/handshake/acceptor.rb +164 -0
  195. data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
  196. data/lib/raiha/tls/handshake/certificate.rb +56 -0
  197. data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
  198. data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
  199. data/lib/raiha/tls/handshake/client_hello.rb +208 -0
  200. data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
  201. data/lib/raiha/tls/handshake/client_side.rb +578 -0
  202. data/lib/raiha/tls/handshake/conductor.rb +390 -0
  203. data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
  204. data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
  205. data/lib/raiha/tls/handshake/finished.rb +36 -0
  206. data/lib/raiha/tls/handshake/key_update.rb +38 -0
  207. data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
  208. data/lib/raiha/tls/handshake/machine.rb +238 -0
  209. data/lib/raiha/tls/handshake/message.rb +25 -0
  210. data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
  211. data/lib/raiha/tls/handshake/reassembler.rb +88 -0
  212. data/lib/raiha/tls/handshake/server_hello.rb +98 -0
  213. data/lib/raiha/tls/handshake/server_side.rb +672 -0
  214. data/lib/raiha/tls/handshake/violation.rb +12 -0
  215. data/lib/raiha/tls/handshake.rb +62 -0
  216. data/lib/raiha/tls/iana.rb +113 -0
  217. data/lib/raiha/tls/key_exchange.rb +58 -0
  218. data/lib/raiha/tls/key_schedule.rb +269 -0
  219. data/lib/raiha/tls/keylog.rb +40 -0
  220. data/lib/raiha/tls/keyring.rb +180 -0
  221. data/lib/raiha/tls/masked_inspect.rb +33 -0
  222. data/lib/raiha/tls/output.rb +32 -0
  223. data/lib/raiha/tls/profile.rb +82 -0
  224. data/lib/raiha/tls/record/layer.rb +228 -0
  225. data/lib/raiha/tls/record/protection.rb +67 -0
  226. data/lib/raiha/tls/record.rb +11 -0
  227. data/lib/raiha/tls/server_name.rb +42 -0
  228. data/lib/raiha/tls/session.rb +106 -0
  229. data/lib/raiha/tls/signature_scheme.rb +35 -0
  230. data/lib/raiha/tls/tcp/acceptor.rb +121 -0
  231. data/lib/raiha/tls/tcp/connection.rb +371 -0
  232. data/lib/raiha/tls/tcp/event.rb +15 -0
  233. data/lib/raiha/tls/tcp/socket.rb +161 -0
  234. data/lib/raiha/tls/tcp.rb +14 -0
  235. data/lib/raiha/tls/test/fixed_kem.rb +30 -0
  236. data/lib/raiha/tls/test/fixed_random.rb +18 -0
  237. data/lib/raiha/tls/test/rfc8448.rb +923 -0
  238. data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
  239. data/lib/raiha/tls/test.rb +13 -0
  240. data/lib/raiha/tls/ticket.rb +184 -0
  241. data/lib/raiha/tls/ticket_keys.rb +206 -0
  242. data/lib/raiha/tls/trace.rb +58 -0
  243. data/lib/raiha/tls/transcript.rb +94 -0
  244. data/lib/raiha/tls/verifier/default.rb +72 -0
  245. data/lib/raiha/tls/verifier/host_name.rb +75 -0
  246. data/lib/raiha/tls/verifier/none.rb +14 -0
  247. data/lib/raiha/tls/verifier.rb +24 -0
  248. data/lib/raiha/tls.rb +34 -0
  249. data/lib/raiha/version.rb +3 -1
  250. data/lib/raiha.rb +8 -5
  251. data/sig/patches/array.rbs +9 -0
  252. data/sig/patches/io_buffer.rbs +13 -0
  253. data/sig/patches/openssl.rbs +25 -0
  254. data/sig/patches/socket.rbs +29 -0
  255. data/sig/patches/string.rbs +26 -0
  256. data/sig/patches/strscan.rbs +12 -0
  257. data/sig/raiha/http3/capsule.rbs +32 -0
  258. data/sig/raiha/http3/connection.rbs +324 -0
  259. data/sig/raiha/http3/frame.rbs +138 -0
  260. data/sig/raiha/http3/frame_reader.rbs +39 -0
  261. data/sig/raiha/http3/message.rbs +23 -0
  262. data/sig/raiha/http3/priority.rbs +40 -0
  263. data/sig/raiha/http3/qlog.rbs +54 -0
  264. data/sig/raiha/http3/qpack.rbs +246 -0
  265. data/sig/raiha/http3/session.rbs +11 -0
  266. data/sig/raiha/http3/settings.rbs +26 -0
  267. data/sig/raiha/http3.rbs +2 -0
  268. data/sig/raiha/http3_binding.rbs +26 -0
  269. data/sig/raiha/namespace.rbs +2 -0
  270. data/sig/raiha/quic/config.rbs +89 -0
  271. data/sig/raiha/quic/congestion.rbs +213 -0
  272. data/sig/raiha/quic/connection.rbs +249 -0
  273. data/sig/raiha/quic/connection_ids.rbs +34 -0
  274. data/sig/raiha/quic/crypto.rbs +78 -0
  275. data/sig/raiha/quic/crypto_stream.rbs +34 -0
  276. data/sig/raiha/quic/datagrams.rbs +32 -0
  277. data/sig/raiha/quic/endpoint.rbs +179 -0
  278. data/sig/raiha/quic/frame.rbs +387 -0
  279. data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
  280. data/sig/raiha/quic/kept_packets.rbs +25 -0
  281. data/sig/raiha/quic/mtu_search.rbs +21 -0
  282. data/sig/raiha/quic/negotiation.rbs +33 -0
  283. data/sig/raiha/quic/new_token.rbs +14 -0
  284. data/sig/raiha/quic/pacer.rbs +16 -0
  285. data/sig/raiha/quic/packet_protection.rbs +63 -0
  286. data/sig/raiha/quic/path.rbs +106 -0
  287. data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
  288. data/sig/raiha/quic/qlog.rbs +74 -0
  289. data/sig/raiha/quic/ranges.rbs +7 -0
  290. data/sig/raiha/quic/recovery.rbs +376 -0
  291. data/sig/raiha/quic/retry_token.rbs +21 -0
  292. data/sig/raiha/quic/route.rbs +51 -0
  293. data/sig/raiha/quic/scheduler.rbs +36 -0
  294. data/sig/raiha/quic/session.rbs +17 -0
  295. data/sig/raiha/quic/stateless_reset.rbs +10 -0
  296. data/sig/raiha/quic/streams.rbs +244 -0
  297. data/sig/raiha/quic/test.rbs +354 -0
  298. data/sig/raiha/quic/tls_binding.rbs +58 -0
  299. data/sig/raiha/quic/token.rbs +11 -0
  300. data/sig/raiha/quic/tracer.rbs +71 -0
  301. data/sig/raiha/quic/transport_parameters.rbs +113 -0
  302. data/sig/raiha/quic/values.rbs +200 -0
  303. data/sig/raiha/quic/version.rbs +30 -0
  304. data/sig/raiha/quic/wire.rbs +199 -0
  305. data/sig/raiha/quic.rbs +2 -0
  306. data/sig/raiha/runtime/threaded.rbs +616 -0
  307. data/sig/raiha/runtime.rbs +982 -0
  308. data/sig/raiha/tls/anti_replay.rbs +41 -0
  309. data/sig/raiha/tls/cipher_suite.rbs +41 -0
  310. data/sig/raiha/tls/codec.rbs +66 -0
  311. data/sig/raiha/tls/config.rbs +122 -0
  312. data/sig/raiha/tls/crypto.rbs +86 -0
  313. data/sig/raiha/tls/error.rbs +21 -0
  314. data/sig/raiha/tls/event.rbs +61 -0
  315. data/sig/raiha/tls/extension.rbs +331 -0
  316. data/sig/raiha/tls/handshake.rbs +311 -0
  317. data/sig/raiha/tls/iana.rbs +13 -0
  318. data/sig/raiha/tls/key_exchange.rbs +33 -0
  319. data/sig/raiha/tls/key_schedule.rbs +58 -0
  320. data/sig/raiha/tls/keylog.rbs +20 -0
  321. data/sig/raiha/tls/keyring.rbs +64 -0
  322. data/sig/raiha/tls/machine.rbs +429 -0
  323. data/sig/raiha/tls/masked_inspect.rbs +12 -0
  324. data/sig/raiha/tls/output.rbs +37 -0
  325. data/sig/raiha/tls/profile.rbs +26 -0
  326. data/sig/raiha/tls/record.rbs +79 -0
  327. data/sig/raiha/tls/server_name.rbs +10 -0
  328. data/sig/raiha/tls/session.rbs +48 -0
  329. data/sig/raiha/tls/signature_scheme.rbs +20 -0
  330. data/sig/raiha/tls/tcp.rbs +134 -0
  331. data/sig/raiha/tls/test.rbs +143 -0
  332. data/sig/raiha/tls/ticket.rbs +68 -0
  333. data/sig/raiha/tls/ticket_keys.rbs +67 -0
  334. data/sig/raiha/tls/trace.rbs +99 -0
  335. data/sig/raiha/tls/transcript.rbs +22 -0
  336. data/sig/raiha/tls/verifier.rbs +54 -0
  337. data/sig/raiha/tls.rbs +5 -0
  338. data/sig/raiha.rbs +3 -0
  339. metadata +355 -57
  340. data/.gitignore +0 -10
  341. data/CODE_OF_CONDUCT.md +0 -74
  342. data/Gemfile +0 -4
  343. data/Gemfile.lock +0 -22
  344. data/Rakefile +0 -10
  345. data/bin/console +0 -14
  346. data/bin/setup +0 -8
  347. data/raiha.gemspec +0 -40
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3::QPACK
4
+ # A field line. `never_indexed` is the N bit of RFC 9204 "Literal Field
5
+ # Line with Name Reference": the line is always sent as a literal, here
6
+ # and on every hop after. The Strings are frozen, copied first when the
7
+ # caller's are not.
8
+ class Field < Data.define(:name, :value, :never_indexed)
9
+ def initialize(name:, value:, never_indexed: false)
10
+ name = name.dup.freeze unless name.frozen?
11
+ value = value.dup.freeze unless value.frozen?
12
+ super
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,397 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+
5
+ module Raiha::HTTP3::QPACK
6
+ # The static Huffman code of RFC 7541 Appendix B, which QPACK uses
7
+ # unmodified for string literals (RFC 9204 "String Literals").
8
+ module Huffman
9
+ # [code, length in bits] of each symbol, the 256 octets and EOS.
10
+ CODES = Ractor.make_shareable([
11
+ [0x1ff8, 13], # 0
12
+ [0x7fffd8, 23], # 1
13
+ [0xfffffe2, 28], # 2
14
+ [0xfffffe3, 28], # 3
15
+ [0xfffffe4, 28], # 4
16
+ [0xfffffe5, 28], # 5
17
+ [0xfffffe6, 28], # 6
18
+ [0xfffffe7, 28], # 7
19
+ [0xfffffe8, 28], # 8
20
+ [0xffffea, 24], # 9
21
+ [0x3ffffffc, 30], # 10
22
+ [0xfffffe9, 28], # 11
23
+ [0xfffffea, 28], # 12
24
+ [0x3ffffffd, 30], # 13
25
+ [0xfffffeb, 28], # 14
26
+ [0xfffffec, 28], # 15
27
+ [0xfffffed, 28], # 16
28
+ [0xfffffee, 28], # 17
29
+ [0xfffffef, 28], # 18
30
+ [0xffffff0, 28], # 19
31
+ [0xffffff1, 28], # 20
32
+ [0xffffff2, 28], # 21
33
+ [0x3ffffffe, 30], # 22
34
+ [0xffffff3, 28], # 23
35
+ [0xffffff4, 28], # 24
36
+ [0xffffff5, 28], # 25
37
+ [0xffffff6, 28], # 26
38
+ [0xffffff7, 28], # 27
39
+ [0xffffff8, 28], # 28
40
+ [0xffffff9, 28], # 29
41
+ [0xffffffa, 28], # 30
42
+ [0xffffffb, 28], # 31
43
+ [0x14, 6], # 32
44
+ [0x3f8, 10], # 33 "!"
45
+ [0x3f9, 10], # 34 "\""
46
+ [0xffa, 12], # 35 "#"
47
+ [0x1ff9, 13], # 36 "$"
48
+ [0x15, 6], # 37 "%"
49
+ [0xf8, 8], # 38 "&"
50
+ [0x7fa, 11], # 39 "'"
51
+ [0x3fa, 10], # 40 "("
52
+ [0x3fb, 10], # 41 ")"
53
+ [0xf9, 8], # 42 "*"
54
+ [0x7fb, 11], # 43 "+"
55
+ [0xfa, 8], # 44 ","
56
+ [0x16, 6], # 45 "-"
57
+ [0x17, 6], # 46 "."
58
+ [0x18, 6], # 47 "/"
59
+ [0x0, 5], # 48 "0"
60
+ [0x1, 5], # 49 "1"
61
+ [0x2, 5], # 50 "2"
62
+ [0x19, 6], # 51 "3"
63
+ [0x1a, 6], # 52 "4"
64
+ [0x1b, 6], # 53 "5"
65
+ [0x1c, 6], # 54 "6"
66
+ [0x1d, 6], # 55 "7"
67
+ [0x1e, 6], # 56 "8"
68
+ [0x1f, 6], # 57 "9"
69
+ [0x5c, 7], # 58 ":"
70
+ [0xfb, 8], # 59 ";"
71
+ [0x7ffc, 15], # 60 "<"
72
+ [0x20, 6], # 61 "="
73
+ [0xffb, 12], # 62 ">"
74
+ [0x3fc, 10], # 63 "?"
75
+ [0x1ffa, 13], # 64 "@"
76
+ [0x21, 6], # 65 "A"
77
+ [0x5d, 7], # 66 "B"
78
+ [0x5e, 7], # 67 "C"
79
+ [0x5f, 7], # 68 "D"
80
+ [0x60, 7], # 69 "E"
81
+ [0x61, 7], # 70 "F"
82
+ [0x62, 7], # 71 "G"
83
+ [0x63, 7], # 72 "H"
84
+ [0x64, 7], # 73 "I"
85
+ [0x65, 7], # 74 "J"
86
+ [0x66, 7], # 75 "K"
87
+ [0x67, 7], # 76 "L"
88
+ [0x68, 7], # 77 "M"
89
+ [0x69, 7], # 78 "N"
90
+ [0x6a, 7], # 79 "O"
91
+ [0x6b, 7], # 80 "P"
92
+ [0x6c, 7], # 81 "Q"
93
+ [0x6d, 7], # 82 "R"
94
+ [0x6e, 7], # 83 "S"
95
+ [0x6f, 7], # 84 "T"
96
+ [0x70, 7], # 85 "U"
97
+ [0x71, 7], # 86 "V"
98
+ [0x72, 7], # 87 "W"
99
+ [0xfc, 8], # 88 "X"
100
+ [0x73, 7], # 89 "Y"
101
+ [0xfd, 8], # 90 "Z"
102
+ [0x1ffb, 13], # 91 "["
103
+ [0x7fff0, 19], # 92 "\\"
104
+ [0x1ffc, 13], # 93 "]"
105
+ [0x3ffc, 14], # 94 "^"
106
+ [0x22, 6], # 95 "_"
107
+ [0x7ffd, 15], # 96 "`"
108
+ [0x3, 5], # 97 "a"
109
+ [0x23, 6], # 98 "b"
110
+ [0x4, 5], # 99 "c"
111
+ [0x24, 6], # 100 "d"
112
+ [0x5, 5], # 101 "e"
113
+ [0x25, 6], # 102 "f"
114
+ [0x26, 6], # 103 "g"
115
+ [0x27, 6], # 104 "h"
116
+ [0x6, 5], # 105 "i"
117
+ [0x74, 7], # 106 "j"
118
+ [0x75, 7], # 107 "k"
119
+ [0x28, 6], # 108 "l"
120
+ [0x29, 6], # 109 "m"
121
+ [0x2a, 6], # 110 "n"
122
+ [0x7, 5], # 111 "o"
123
+ [0x2b, 6], # 112 "p"
124
+ [0x76, 7], # 113 "q"
125
+ [0x2c, 6], # 114 "r"
126
+ [0x8, 5], # 115 "s"
127
+ [0x9, 5], # 116 "t"
128
+ [0x2d, 6], # 117 "u"
129
+ [0x77, 7], # 118 "v"
130
+ [0x78, 7], # 119 "w"
131
+ [0x79, 7], # 120 "x"
132
+ [0x7a, 7], # 121 "y"
133
+ [0x7b, 7], # 122 "z"
134
+ [0x7ffe, 15], # 123 "{"
135
+ [0x7fc, 11], # 124 "|"
136
+ [0x3ffd, 14], # 125 "}"
137
+ [0x1ffd, 13], # 126 "~"
138
+ [0xffffffc, 28], # 127
139
+ [0xfffe6, 20], # 128
140
+ [0x3fffd2, 22], # 129
141
+ [0xfffe7, 20], # 130
142
+ [0xfffe8, 20], # 131
143
+ [0x3fffd3, 22], # 132
144
+ [0x3fffd4, 22], # 133
145
+ [0x3fffd5, 22], # 134
146
+ [0x7fffd9, 23], # 135
147
+ [0x3fffd6, 22], # 136
148
+ [0x7fffda, 23], # 137
149
+ [0x7fffdb, 23], # 138
150
+ [0x7fffdc, 23], # 139
151
+ [0x7fffdd, 23], # 140
152
+ [0x7fffde, 23], # 141
153
+ [0xffffeb, 24], # 142
154
+ [0x7fffdf, 23], # 143
155
+ [0xffffec, 24], # 144
156
+ [0xffffed, 24], # 145
157
+ [0x3fffd7, 22], # 146
158
+ [0x7fffe0, 23], # 147
159
+ [0xffffee, 24], # 148
160
+ [0x7fffe1, 23], # 149
161
+ [0x7fffe2, 23], # 150
162
+ [0x7fffe3, 23], # 151
163
+ [0x7fffe4, 23], # 152
164
+ [0x1fffdc, 21], # 153
165
+ [0x3fffd8, 22], # 154
166
+ [0x7fffe5, 23], # 155
167
+ [0x3fffd9, 22], # 156
168
+ [0x7fffe6, 23], # 157
169
+ [0x7fffe7, 23], # 158
170
+ [0xffffef, 24], # 159
171
+ [0x3fffda, 22], # 160
172
+ [0x1fffdd, 21], # 161
173
+ [0xfffe9, 20], # 162
174
+ [0x3fffdb, 22], # 163
175
+ [0x3fffdc, 22], # 164
176
+ [0x7fffe8, 23], # 165
177
+ [0x7fffe9, 23], # 166
178
+ [0x1fffde, 21], # 167
179
+ [0x7fffea, 23], # 168
180
+ [0x3fffdd, 22], # 169
181
+ [0x3fffde, 22], # 170
182
+ [0xfffff0, 24], # 171
183
+ [0x1fffdf, 21], # 172
184
+ [0x3fffdf, 22], # 173
185
+ [0x7fffeb, 23], # 174
186
+ [0x7fffec, 23], # 175
187
+ [0x1fffe0, 21], # 176
188
+ [0x1fffe1, 21], # 177
189
+ [0x3fffe0, 22], # 178
190
+ [0x1fffe2, 21], # 179
191
+ [0x7fffed, 23], # 180
192
+ [0x3fffe1, 22], # 181
193
+ [0x7fffee, 23], # 182
194
+ [0x7fffef, 23], # 183
195
+ [0xfffea, 20], # 184
196
+ [0x3fffe2, 22], # 185
197
+ [0x3fffe3, 22], # 186
198
+ [0x3fffe4, 22], # 187
199
+ [0x7ffff0, 23], # 188
200
+ [0x3fffe5, 22], # 189
201
+ [0x3fffe6, 22], # 190
202
+ [0x7ffff1, 23], # 191
203
+ [0x3ffffe0, 26], # 192
204
+ [0x3ffffe1, 26], # 193
205
+ [0xfffeb, 20], # 194
206
+ [0x7fff1, 19], # 195
207
+ [0x3fffe7, 22], # 196
208
+ [0x7ffff2, 23], # 197
209
+ [0x3fffe8, 22], # 198
210
+ [0x1ffffec, 25], # 199
211
+ [0x3ffffe2, 26], # 200
212
+ [0x3ffffe3, 26], # 201
213
+ [0x3ffffe4, 26], # 202
214
+ [0x7ffffde, 27], # 203
215
+ [0x7ffffdf, 27], # 204
216
+ [0x3ffffe5, 26], # 205
217
+ [0xfffff1, 24], # 206
218
+ [0x1ffffed, 25], # 207
219
+ [0x7fff2, 19], # 208
220
+ [0x1fffe3, 21], # 209
221
+ [0x3ffffe6, 26], # 210
222
+ [0x7ffffe0, 27], # 211
223
+ [0x7ffffe1, 27], # 212
224
+ [0x3ffffe7, 26], # 213
225
+ [0x7ffffe2, 27], # 214
226
+ [0xfffff2, 24], # 215
227
+ [0x1fffe4, 21], # 216
228
+ [0x1fffe5, 21], # 217
229
+ [0x3ffffe8, 26], # 218
230
+ [0x3ffffe9, 26], # 219
231
+ [0xffffffd, 28], # 220
232
+ [0x7ffffe3, 27], # 221
233
+ [0x7ffffe4, 27], # 222
234
+ [0x7ffffe5, 27], # 223
235
+ [0xfffec, 20], # 224
236
+ [0xfffff3, 24], # 225
237
+ [0xfffed, 20], # 226
238
+ [0x1fffe6, 21], # 227
239
+ [0x3fffe9, 22], # 228
240
+ [0x1fffe7, 21], # 229
241
+ [0x1fffe8, 21], # 230
242
+ [0x7ffff3, 23], # 231
243
+ [0x3fffea, 22], # 232
244
+ [0x3fffeb, 22], # 233
245
+ [0x1ffffee, 25], # 234
246
+ [0x1ffffef, 25], # 235
247
+ [0xfffff4, 24], # 236
248
+ [0xfffff5, 24], # 237
249
+ [0x3ffffea, 26], # 238
250
+ [0x7ffff4, 23], # 239
251
+ [0x3ffffeb, 26], # 240
252
+ [0x7ffffe6, 27], # 241
253
+ [0x3ffffec, 26], # 242
254
+ [0x3ffffed, 26], # 243
255
+ [0x7ffffe7, 27], # 244
256
+ [0x7ffffe8, 27], # 245
257
+ [0x7ffffe9, 27], # 246
258
+ [0x7ffffea, 27], # 247
259
+ [0x7ffffeb, 27], # 248
260
+ [0xffffffe, 28], # 249
261
+ [0x7ffffec, 27], # 250
262
+ [0x7ffffed, 27], # 251
263
+ [0x7ffffee, 27], # 252
264
+ [0x7ffffef, 27], # 253
265
+ [0x7fffff0, 27], # 254
266
+ [0x3ffffee, 26], # 255
267
+ [0x3fffffff, 30], # EOS
268
+ ])
269
+
270
+ # The code of each octet as a String of "0" and "1", for the encoder to
271
+ # append; the bits are packed once at the end.
272
+ BITS = CODES.first(256).map { |code, length| code.to_s(2).rjust(length, "0").freeze }.freeze
273
+ LENGTHS = CODES.first(256).map(&:last).freeze
274
+
275
+ # `string` in the Huffman code, padded to a whole octet with the most
276
+ # significant bits of EOS (RFC 7541 "String Literal Representation").
277
+ def self.encode(string)
278
+ bits = String.new(capacity: string.bytesize * 8)
279
+ string.each_byte { |byte| bits << BITS.fetch(byte) }
280
+ bits << ("1" * (-bits.bytesize % 8))
281
+ [bits].pack("B*")
282
+ end
283
+
284
+ # How many octets `string` takes in the Huffman code, to choose it only
285
+ # when it is shorter.
286
+ def self.encoded_length(string)
287
+ bits = 0
288
+ string.each_byte { |byte| bits += LENGTHS.fetch(byte) }
289
+ (bits + 7) / 8
290
+ end
291
+
292
+ EOS = 256
293
+
294
+ # The tree of the code: the two children of each inner node, the root
295
+ # first. A child is the index of an inner node, or ~symbol for a leaf.
296
+ # No node points back to the root, so 0 marks a child not yet made.
297
+ def self.tree_of(codes)
298
+ tree = [[0, 0]]
299
+ codes.each_with_index do |(code, length), symbol|
300
+ node = 0
301
+ (length - 1).downto(1) do |shift|
302
+ bit = (code >> shift) & 1
303
+ child = tree.fetch(node).fetch(bit)
304
+ if child.zero?
305
+ child = tree.size
306
+ tree << [0, 0]
307
+ tree.fetch(node)[bit] = child
308
+ end
309
+ node = child
310
+ end
311
+ tree.fetch(node)[code & 1] = ~symbol
312
+ end
313
+ tree
314
+ end
315
+
316
+ # The decoder reads a byte at a time: Ruby's cost follows the
317
+ # instructions it runs, not the bits, so a byte's transition looked up
318
+ # at once beats walking the tree a bit at a time by an order of
319
+ # magnitude. The states are the 256 inner nodes and POISON, which EOS
320
+ # leads to and never leaves; each of the 257 × 256 transitions has the
321
+ # state it leads to and the octets it emits, so the loop has no branch.
322
+ POISON = 256
323
+
324
+ # The state after each [state, byte] at `(state << 8) | byte`, the
325
+ # octets emitted on the way, and the states a string may end in: the
326
+ # root and the first seven nodes of the all-ones path, which padding
327
+ # of up to seven bits of EOS leads to (RFC 7541 "String Literal
328
+ # Representation"). A byte's transition is two of a nibble's, which
329
+ # take a sixteenth of the walking to make.
330
+ def self.decoder_of(tree)
331
+ nibble_states, nibble_emits = nibble_steps_of(tree)
332
+ next_states = Array.new((POISON + 1) << 8, POISON)
333
+ emits = Array.new((POISON + 1) << 8, "")
334
+ POISON.times do |state|
335
+ 256.times do |byte|
336
+ high = (state << 4) | (byte >> 4)
337
+ middle = nibble_states.fetch(high)
338
+ next if middle == POISON
339
+ low = (middle << 4) | (byte & 0x0f)
340
+ next_states[(state << 8) | byte] = nibble_states.fetch(low)
341
+ emits[(state << 8) | byte] = -(nibble_emits.fetch(high) + nibble_emits.fetch(low))
342
+ end
343
+ end
344
+ accepting = Array.new(POISON + 1, false)
345
+ node = 0
346
+ 8.times do
347
+ accepting[node] = true
348
+ node = tree.fetch(node).fetch(1)
349
+ end
350
+ [next_states, emits, accepting]
351
+ end
352
+
353
+ # The state after each [state, nibble] at `(state << 4) | nibble`, and
354
+ # the octets emitted on the way.
355
+ def self.nibble_steps_of(tree)
356
+ states = Array.new(POISON << 4, POISON)
357
+ emits = Array.new(POISON << 4, "")
358
+ POISON.times do |state|
359
+ 16.times do |nibble|
360
+ node = state
361
+ emitted = String.new(encoding: Encoding::BINARY)
362
+ 3.downto(0) do |shift|
363
+ child = tree.fetch(node).fetch((nibble >> shift) & 1)
364
+ if child.positive?
365
+ node = child
366
+ elsif child == ~EOS
367
+ node = POISON
368
+ break
369
+ else
370
+ emitted << ~child
371
+ node = 0
372
+ end
373
+ end
374
+ states[(state << 4) | nibble] = node
375
+ emits[(state << 4) | nibble] = emitted
376
+ end
377
+ end
378
+ [states, emits]
379
+ end
380
+
381
+ NEXT_STATES, EMITS, ACCEPTING = Ractor.make_shareable(decoder_of(tree_of(CODES)))
382
+
383
+ # The octets `bytes` encode. Bits that end inside a code other than the
384
+ # padding, or EOS anywhere, raise Malformed.
385
+ def self.decode(bytes)
386
+ decoded = String.new(encoding: Encoding::BINARY, capacity: bytes.bytesize * 8 / 5)
387
+ state = 0
388
+ bytes.each_byte do |byte|
389
+ index = (state << 8) | byte
390
+ decoded << EMITS[index]
391
+ state = NEXT_STATES[index]
392
+ end
393
+ raise Malformed, "the Huffman code ends inside a code or holds EOS" unless ACCEPTING[state]
394
+ decoded
395
+ end
396
+ end
397
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+ require_relative "huffman"
5
+
6
+ module Raiha::HTTP3::QPACK
7
+ # Reads the primitives of RFC 9204 "Primitives" from `bytes` between
8
+ # `offset` and `limit` without copying them first. A field past the limit
9
+ # raises Truncated.
10
+ class Reader
11
+ # RFC 9204 "Prefixed Integers": up to 62 bits must decode.
12
+ MAX_INTEGER = (1 << 62) - 1
13
+ # Continuation bytes enough for MAX_INTEGER after any prefix (7 × 9 = 63
14
+ # bits); an encoding longer than that is refused before its value is.
15
+ MAX_CONTINUATIONS = 9
16
+
17
+ attr_reader :offset
18
+
19
+ def initialize(bytes, offset = 0, limit = bytes.bytesize)
20
+ @bytes = bytes
21
+ @offset = offset
22
+ @limit = limit
23
+ end
24
+
25
+ def eof? = @offset >= @limit
26
+
27
+ # The next byte, left unread: the first byte of an instruction or a
28
+ # representation tells which one it is.
29
+ def peek
30
+ raise Truncated.new("a byte wanted, none left", needed: @offset + 1) if eof?
31
+ @bytes.getbyte(@offset) #: Integer
32
+ end
33
+
34
+ # RFC 7541 "Integer Representation" with a `prefix`-bit prefix. The bits
35
+ # of the first byte above the prefix belong to the caller, which has
36
+ # peeked at them already.
37
+ def integer(prefix)
38
+ mask = (1 << prefix) - 1
39
+ value = u8 & mask
40
+ return value if value < mask
41
+ MAX_CONTINUATIONS.times do |index|
42
+ byte = u8
43
+ value += (byte & 0x7f) << (7 * index)
44
+ next if byte >= 0x80
45
+ raise IntegerOverflow, "an integer above 2^62 - 1" if value > MAX_INTEGER
46
+ return value
47
+ end
48
+ raise IntegerOverflow, "an integer of more than #{MAX_CONTINUATIONS} continuation bytes"
49
+ end
50
+
51
+ # RFC 9204 "String Literals" with a `prefix`-bit prefix: the H bit, the
52
+ # length as a (prefix - 1)-bit integer, then the octets, which come out
53
+ # decoded when H says Huffman, frozen and binary. A string sure to be
54
+ # longer than `max` octets raises TooLong before its octets are taken;
55
+ # for a Huffman one that is when even 30-bit codes would decode to more.
56
+ def string(prefix, max = nil)
57
+ huffman = peek[prefix - 1] == 1
58
+ length = integer(prefix - 1)
59
+ if max
60
+ shortest = huffman ? (length * 8 - 7) / 30 : length
61
+ raise TooLong, "a string of at least #{shortest} octets, #{max} allowed" if shortest > max
62
+ end
63
+ octets = take(length)
64
+ decoded = huffman ? Huffman.decode(octets) : octets.force_encoding(Encoding::BINARY)
65
+ raise TooLong, "a string of #{decoded.bytesize} octets, #{max} allowed" if max && decoded.bytesize > max
66
+ decoded.freeze
67
+ end
68
+
69
+ private def take(length)
70
+ raise Truncated.new("#{length} bytes wanted, #{@limit - @offset} left", needed: @offset + length) if length > @limit - @offset
71
+ octets = @bytes.byteslice(@offset, length) #: String
72
+ @offset += length
73
+ octets
74
+ end
75
+
76
+ private def u8
77
+ byte = peek
78
+ @offset += 1
79
+ byte
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error_code"
4
+
5
+ module Raiha::HTTP3::QPACK
6
+ # A field section decoded: its fields in order, and their size as RFC
7
+ # 9114 "Header Size Constraints" counts it, name and value and 32 more
8
+ # for each.
9
+ class Decoded < Data.define(:stream_id, :fields, :size); end
10
+
11
+ # A field section that refers to entries yet to come on the encoder
12
+ # stream (RFC 9204 "Blocked Decoding"); it comes out decoded once they
13
+ # do.
14
+ class Blocked < Data.define(:stream_id); end
15
+
16
+ # A field section whose fields grew past the budget, at which the
17
+ # decoder stopped.
18
+ class Oversized < Data.define(:stream_id); end
19
+
20
+ # The peer broke a rule of QPACK: `code` is the ErrorCode to close the
21
+ # connection with, or to reset the stream `stream_id` with when there is
22
+ # one (RFC 9204 "Implementation Limits"), and `reason` says what broke.
23
+ class Failure < Data.define(:code, :reason, :stream_id)
24
+ def initialize(code:, reason:, stream_id: nil) = super
25
+ end
26
+ end
@@ -0,0 +1,139 @@
1
+ # encoding: binary
2
+ # frozen_string_literal: true
3
+
4
+ module Raiha::HTTP3::QPACK
5
+ # The static table of RFC 9204 Appendix A, indexed from 0.
6
+ module StaticTable
7
+ # [name, value] of each entry. The literals of this file are binary, so
8
+ # the decoder hands the Strings out as they are.
9
+ ENTRIES = Ractor.make_shareable([
10
+ [":authority", ""], # 0
11
+ [":path", "/"], # 1
12
+ ["age", "0"], # 2
13
+ ["content-disposition", ""], # 3
14
+ ["content-length", "0"], # 4
15
+ ["cookie", ""], # 5
16
+ ["date", ""], # 6
17
+ ["etag", ""], # 7
18
+ ["if-modified-since", ""], # 8
19
+ ["if-none-match", ""], # 9
20
+ ["last-modified", ""], # 10
21
+ ["link", ""], # 11
22
+ ["location", ""], # 12
23
+ ["referer", ""], # 13
24
+ ["set-cookie", ""], # 14
25
+ [":method", "CONNECT"], # 15
26
+ [":method", "DELETE"], # 16
27
+ [":method", "GET"], # 17
28
+ [":method", "HEAD"], # 18
29
+ [":method", "OPTIONS"], # 19
30
+ [":method", "POST"], # 20
31
+ [":method", "PUT"], # 21
32
+ [":scheme", "http"], # 22
33
+ [":scheme", "https"], # 23
34
+ [":status", "103"], # 24
35
+ [":status", "200"], # 25
36
+ [":status", "304"], # 26
37
+ [":status", "404"], # 27
38
+ [":status", "503"], # 28
39
+ ["accept", "*/*"], # 29
40
+ ["accept", "application/dns-message"], # 30
41
+ ["accept-encoding", "gzip, deflate, br"], # 31
42
+ ["accept-ranges", "bytes"], # 32
43
+ ["access-control-allow-headers", "cache-control"], # 33
44
+ ["access-control-allow-headers", "content-type"], # 34
45
+ ["access-control-allow-origin", "*"], # 35
46
+ ["cache-control", "max-age=0"], # 36
47
+ ["cache-control", "max-age=2592000"], # 37
48
+ ["cache-control", "max-age=604800"], # 38
49
+ ["cache-control", "no-cache"], # 39
50
+ ["cache-control", "no-store"], # 40
51
+ ["cache-control", "public, max-age=31536000"], # 41
52
+ ["content-encoding", "br"], # 42
53
+ ["content-encoding", "gzip"], # 43
54
+ ["content-type", "application/dns-message"], # 44
55
+ ["content-type", "application/javascript"], # 45
56
+ ["content-type", "application/json"], # 46
57
+ ["content-type", "application/x-www-form-urlencoded"], # 47
58
+ ["content-type", "image/gif"], # 48
59
+ ["content-type", "image/jpeg"], # 49
60
+ ["content-type", "image/png"], # 50
61
+ ["content-type", "text/css"], # 51
62
+ ["content-type", "text/html; charset=utf-8"], # 52
63
+ ["content-type", "text/plain"], # 53
64
+ ["content-type", "text/plain;charset=utf-8"], # 54
65
+ ["range", "bytes=0-"], # 55
66
+ ["strict-transport-security", "max-age=31536000"], # 56
67
+ ["strict-transport-security", "max-age=31536000; includesubdomains"], # 57
68
+ ["strict-transport-security", "max-age=31536000; includesubdomains; preload"], # 58
69
+ ["vary", "accept-encoding"], # 59
70
+ ["vary", "origin"], # 60
71
+ ["x-content-type-options", "nosniff"], # 61
72
+ ["x-xss-protection", "1; mode=block"], # 62
73
+ [":status", "100"], # 63
74
+ [":status", "204"], # 64
75
+ [":status", "206"], # 65
76
+ [":status", "302"], # 66
77
+ [":status", "400"], # 67
78
+ [":status", "403"], # 68
79
+ [":status", "421"], # 69
80
+ [":status", "425"], # 70
81
+ [":status", "500"], # 71
82
+ ["accept-language", ""], # 72
83
+ ["access-control-allow-credentials", "FALSE"], # 73
84
+ ["access-control-allow-credentials", "TRUE"], # 74
85
+ ["access-control-allow-headers", "*"], # 75
86
+ ["access-control-allow-methods", "get"], # 76
87
+ ["access-control-allow-methods", "get, post, options"], # 77
88
+ ["access-control-allow-methods", "options"], # 78
89
+ ["access-control-expose-headers", "content-length"], # 79
90
+ ["access-control-request-headers", "content-type"], # 80
91
+ ["access-control-request-method", "get"], # 81
92
+ ["access-control-request-method", "post"], # 82
93
+ ["alt-svc", "clear"], # 83
94
+ ["authorization", ""], # 84
95
+ ["content-security-policy", "script-src 'none'; object-src 'none'; base-uri 'none'"], # 85
96
+ ["early-data", "1"], # 86
97
+ ["expect-ct", ""], # 87
98
+ ["forwarded", ""], # 88
99
+ ["if-range", ""], # 89
100
+ ["origin", ""], # 90
101
+ ["purpose", "prefetch"], # 91
102
+ ["server", ""], # 92
103
+ ["timing-allow-origin", "*"], # 93
104
+ ["upgrade-insecure-requests", "1"], # 94
105
+ ["user-agent", ""], # 95
106
+ ["x-forwarded-for", ""], # 96
107
+ ["x-frame-options", "deny"], # 97
108
+ ["x-frame-options", "sameorigin"] # 98
109
+ ])
110
+
111
+ def self.index_of(entries)
112
+ index = {} #: Hash[String, Hash[String, Integer]]
113
+ entries.each_with_index { |(name, value), position| (index[name] ||= {})[value] ||= position }
114
+ index
115
+ end
116
+
117
+ def self.names_of(entries)
118
+ names = {} #: Hash[String, Integer]
119
+ entries.each_with_index { |(name, _), position| names[name] ||= position }
120
+ names
121
+ end
122
+
123
+ # name => { value => index }, nested rather than keyed by [name, value],
124
+ # which hashes an Array on each lookup and measured 5.1 times as slow.
125
+ INDEX = Ractor.make_shareable(index_of(ENTRIES))
126
+
127
+ # name => the index of the first entry with that name.
128
+ NAMES = Ractor.make_shareable(names_of(ENTRIES))
129
+
130
+ # The entry at `index`, or nil outside the table.
131
+ def self.entry(index) = index.negative? ? nil : ENTRIES[index]
132
+
133
+ # The index of the entry with `name` and `value`, or nil.
134
+ def self.find(name, value) = INDEX[name]&.[](value)
135
+
136
+ # The index of an entry with `name`, or nil.
137
+ def self.find_name(name) = NAMES[name]
138
+ end
139
+ end