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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error_code"
4
+
5
+ module Raiha::HTTP3
6
+ # Exceptions are for misuse of the API only. What the peer sends never
7
+ # raises; it closes the connection, or resets a stream.
8
+ module Error
9
+ # A call the API does not allow, as data for a stream already ended.
10
+ class Usage < StandardError; end
11
+ end
12
+
13
+ # The peer broke a rule that closes the connection with `code`, an
14
+ # ErrorCode. Raised inside this layer and turned into the closing of the
15
+ # connection where the bytes came in; the application never sees it.
16
+ class Violation < StandardError
17
+ attr_reader :code
18
+
19
+ def initialize(message = nil, code:)
20
+ super(message)
21
+ @code = code
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # The error codes of HTTP/3 (RFC 9114 "HTTP/3 Error Codes"), for closing
5
+ # the connection and resetting streams, and the one HTTP Datagrams add
6
+ # (RFC 9297). QPACK's are QPACK::ErrorCode.
7
+ module ErrorCode
8
+ NO_ERROR = 0x0100
9
+ GENERAL_PROTOCOL_ERROR = 0x0101
10
+ INTERNAL_ERROR = 0x0102
11
+ STREAM_CREATION_ERROR = 0x0103
12
+ CLOSED_CRITICAL_STREAM = 0x0104
13
+ FRAME_UNEXPECTED = 0x0105
14
+ FRAME_ERROR = 0x0106
15
+ EXCESSIVE_LOAD = 0x0107
16
+ ID_ERROR = 0x0108
17
+ SETTINGS_ERROR = 0x0109
18
+ MISSING_SETTINGS = 0x010a
19
+ REQUEST_REJECTED = 0x010b
20
+ REQUEST_CANCELLED = 0x010c
21
+ REQUEST_INCOMPLETE = 0x010d
22
+ MESSAGE_ERROR = 0x010e
23
+ CONNECT_ERROR = 0x010f
24
+ VERSION_FALLBACK = 0x0110
25
+ DATAGRAM_ERROR = 0x33
26
+ end
27
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # What happened on an HTTP/3 connection, for the application.
5
+ module Event
6
+ # The peer's settings came (Settings).
7
+ class Settings < ::Data.define(:settings); end
8
+
9
+ # A field section came on `stream_id`: a request's, a response's or an
10
+ # interim response's, as [name, value] pairs, the pseudo-header fields
11
+ # first. `fin` if the stream ended with it.
12
+ class Headers < ::Data.define(:stream_id, :headers, :fin)
13
+ def initialize(stream_id:, headers:, fin:)
14
+ headers = Ractor.make_shareable(headers.map(&:dup))
15
+ super
16
+ end
17
+ end
18
+
19
+ # Content came on `stream_id`, as it arrived; `fin` if the stream ended
20
+ # with it, which may come with no content.
21
+ class Data < ::Data.define(:stream_id, :data, :fin); end
22
+
23
+ # The trailer section of the message on `stream_id` came, which ends
24
+ # it.
25
+ class Trailers < ::Data.define(:stream_id, :headers)
26
+ def initialize(stream_id:, headers:)
27
+ headers = Ractor.make_shareable(headers.map(&:dup))
28
+ super
29
+ end
30
+ end
31
+
32
+ # What the peer sends on `stream_id` ended with `error_code`, the peer
33
+ # having reset it, or this layer having stopped the stream, after the
34
+ # application had heard of it.
35
+ class Reset < ::Data.define(:stream_id, :error_code); end
36
+
37
+ # The peer asked this end to stop sending on `stream_id`, with
38
+ # `error_code`; nothing more goes on it, though the peer's side may
39
+ # still come.
40
+ class Stopped < ::Data.define(:stream_id, :error_code); end
41
+
42
+ # `send_data` takes content on `stream_id` again, after it took less
43
+ # than given.
44
+ class Writable < ::Data.define(:stream_id); end
45
+
46
+ # A Capsule other than DATAGRAM came on `stream_id` (RFC 9297 "The
47
+ # Capsule Protocol").
48
+ class Capsule < ::Data.define(:stream_id, :capsule); end
49
+
50
+ # An HTTP Datagram for the request on `stream_id` came (RFC 9297), in a
51
+ # DATAGRAM of the transport or a DATAGRAM Capsule.
52
+ class Datagram < ::Data.define(:stream_id, :payload); end
53
+
54
+ # The peer goes away (RFC 9114 "Connection Shutdown"): a server's
55
+ # `stream_id`, from which on requests were not processed, or a
56
+ # client's push ID.
57
+ class GoAway < ::Data.define(:stream_id); end
58
+
59
+ # The field section on `stream_id` waits for entries the QPACK encoder
60
+ # stream has yet to bring (RFC 9204 "Blocked Decoding").
61
+ class Blocked < ::Data.define(:stream_id); end
62
+ end
63
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+ require_relative "varint"
5
+
6
+ module Raiha::HTTP3
7
+ # The frames of HTTP/3 (RFC 9114 "Frame Definitions"), and those of
8
+ # PRIORITY_UPDATE (RFC 9218 "HTTP/3 PRIORITY_UPDATE Frame"): a type and
9
+ # a length, both varints, then the payload. Values in, bytes out, and
10
+ # back; which stream a frame may go on is not decided here.
11
+ module Frame
12
+ DATA = 0x00
13
+ HEADERS = 0x01
14
+ CANCEL_PUSH = 0x03
15
+ SETTINGS = 0x04
16
+ PUSH_PROMISE = 0x05
17
+ GOAWAY = 0x07
18
+ MAX_PUSH_ID = 0x0d
19
+ # For a request stream, and for a push.
20
+ PRIORITY_UPDATE = 0xf0700
21
+ PRIORITY_UPDATE_PUSH = 0xf0701
22
+ # The types of HTTP/2's PRIORITY, PING, WINDOW_UPDATE and CONTINUATION,
23
+ # which HTTP/3 reserves and never takes (RFC 9114 "Reserved Frame
24
+ # Types").
25
+ HTTP2_TYPES = [0x02, 0x06, 0x08, 0x09].freeze
26
+
27
+ # A payload that does not hold exactly the fields of its frame (RFC
28
+ # 9114 "Frame Layout"), which closes the connection with
29
+ # H3_FRAME_ERROR.
30
+ class Malformed < Violation
31
+ def initialize(message = nil) = super(message, code: ErrorCode::FRAME_ERROR)
32
+ end
33
+
34
+ # The Strings of a frame are frozen and binary, copied first when the
35
+ # caller's are not.
36
+ def self.binary(string) = string.frozen? && string.encoding == Encoding::BINARY ? string : string.b.freeze
37
+
38
+ class Data < ::Data.define(:data)
39
+ def initialize(data:)
40
+ data = Frame.binary(data)
41
+ super
42
+ end
43
+ end
44
+
45
+ # An encoded field section, as QPACK gives and takes it.
46
+ class Headers < ::Data.define(:field_section)
47
+ def initialize(field_section:)
48
+ field_section = Frame.binary(field_section)
49
+ super
50
+ end
51
+ end
52
+
53
+ class CancelPush < ::Data.define(:push_id); end
54
+
55
+ # The identifiers and values, in the order they came, each pair once or
56
+ # more: whether one repeats is for the reader of SETTINGS to say.
57
+ class Settings < ::Data.define(:settings)
58
+ def initialize(settings:)
59
+ settings = Ractor.make_shareable(settings.map(&:dup))
60
+ super
61
+ end
62
+ end
63
+
64
+ class PushPromise < ::Data.define(:push_id, :field_section)
65
+ def initialize(push_id:, field_section:)
66
+ field_section = Frame.binary(field_section)
67
+ super
68
+ end
69
+ end
70
+
71
+ # A stream ID from a server, a push ID from a client.
72
+ class GoAway < ::Data.define(:id); end
73
+
74
+ class MaxPushId < ::Data.define(:push_id); end
75
+
76
+ # `priority` is the Priority Field Value, the ASCII text of the
77
+ # Priority header field; `push` whether `element_id` is a push ID
78
+ # rather than a stream ID.
79
+ class PriorityUpdate < ::Data.define(:element_id, :priority, :push)
80
+ def initialize(element_id:, priority:, push: false)
81
+ priority = Frame.binary(priority)
82
+ super
83
+ end
84
+ end
85
+
86
+ # The type and length that go before a payload of `length` bytes.
87
+ def self.header(type, length) = Varint.encode(type) << Varint.encode(length)
88
+
89
+ # `frame` as it goes on a stream.
90
+ def self.encode(frame)
91
+ type, payload = case frame
92
+ in Data then [DATA, frame.data]
93
+ in Headers then [HEADERS, frame.field_section]
94
+ in CancelPush then [CANCEL_PUSH, Varint.encode(frame.push_id)]
95
+ in Settings then [SETTINGS, frame.settings.map { |identifier, value| Varint.encode(identifier) << Varint.encode(value) }.join]
96
+ in PushPromise then [PUSH_PROMISE, Varint.encode(frame.push_id) << frame.field_section]
97
+ in GoAway then [GOAWAY, Varint.encode(frame.id)]
98
+ in MaxPushId then [MAX_PUSH_ID, Varint.encode(frame.push_id)]
99
+ in PriorityUpdate then [frame.push ? PRIORITY_UPDATE_PUSH : PRIORITY_UPDATE, Varint.encode(frame.element_id) << frame.priority]
100
+ end
101
+ header(type, payload.bytesize) << payload
102
+ end
103
+
104
+ # The frame of `type` whose payload is `payload`, whole. Malformed when
105
+ # the payload holds less or more than the frame's fields.
106
+ def self.decode(type, payload)
107
+ case type
108
+ when DATA then Data.new(data: payload)
109
+ when HEADERS then Headers.new(field_section: payload)
110
+ when CANCEL_PUSH then CancelPush.new(push_id: only_varint(payload))
111
+ when SETTINGS then Settings.new(settings: pairs(payload))
112
+ when PUSH_PROMISE
113
+ push_id, offset = varint(payload, 0)
114
+ PushPromise.new(push_id:, field_section: payload.byteslice(offset..)) # steep:ignore
115
+ when GOAWAY then GoAway.new(id: only_varint(payload))
116
+ when MAX_PUSH_ID then MaxPushId.new(push_id: only_varint(payload))
117
+ when PRIORITY_UPDATE, PRIORITY_UPDATE_PUSH
118
+ element_id, offset = varint(payload, 0)
119
+ PriorityUpdate.new(element_id:, priority: payload.byteslice(offset..), push: type == PRIORITY_UPDATE_PUSH) # steep:ignore
120
+ else raise ArgumentError, "no frame of type 0x#{type.to_s(16)} to decode"
121
+ end
122
+ end
123
+
124
+ def self.varint(payload, offset)
125
+ Varint.decode(payload, offset) or raise Malformed, "a varint is cut short"
126
+ end
127
+
128
+ # The one varint `payload` holds, and nothing more.
129
+ def self.only_varint(payload)
130
+ value, offset = varint(payload, 0)
131
+ raise Malformed, "#{payload.bytesize - offset} bytes past the end" unless offset == payload.bytesize
132
+ value
133
+ end
134
+
135
+ def self.pairs(payload)
136
+ settings = [] #: Array[[Integer, Integer]]
137
+ offset = 0
138
+ while offset < payload.bytesize
139
+ identifier, offset = varint(payload, offset)
140
+ value, offset = varint(payload, offset)
141
+ settings << [identifier, value]
142
+ end
143
+ settings
144
+ end
145
+
146
+ private_class_method :varint, :only_varint, :pairs
147
+ end
148
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "frame"
4
+
5
+ module Raiha::HTTP3
6
+ # Reads the frames of one stream from its bytes as they come, without
7
+ # trusting a length to set aside room first (RFC 9114 "Frame Layout").
8
+ #
9
+ # Each frame tells it began, with its type and length, as soon as both
10
+ # are known, so that a frame the stream must not carry is refused before
11
+ # its payload comes. A DATA frame then comes in the pieces it arrives in,
12
+ # never held; a frame of the other types this layer reads is held until
13
+ # whole and comes decoded, unless its length is past `max_payload`, when
14
+ # it is told so and its payload passed over. The payloads of the frame
15
+ # types of HTTP/2 and of types unknown are passed over as they come.
16
+ class FrameReader
17
+ # A frame began.
18
+ class Started < Data.define(:type, :length); end
19
+
20
+ # A frame of a type that is held is longer than `max_payload`.
21
+ class TooLong < Data.define(:type, :length); end
22
+
23
+ HELD = [
24
+ Frame::HEADERS, Frame::CANCEL_PUSH, Frame::SETTINGS, Frame::PUSH_PROMISE, Frame::GOAWAY, Frame::MAX_PUSH_ID,
25
+ Frame::PRIORITY_UPDATE, Frame::PRIORITY_UPDATE_PUSH
26
+ ].freeze
27
+ # The longest a type and a length take together.
28
+ MAX_HEADER = 16
29
+
30
+ def initialize(max_payload:)
31
+ @max_payload = max_payload
32
+ # The start of a type and length cut short, and the frame being read:
33
+ # its type, the bytes of it to come, and whether it is held, passed
34
+ # on as it comes, or passed over.
35
+ @header = "".b
36
+ @type = nil #: Integer?
37
+ @left = 0
38
+ @mode = :skip #: :hold | :pass | :skip
39
+ @payload = "".b
40
+ end
41
+
42
+ # Whether the bytes so far end inside a frame, which is H3_FRAME_ERROR
43
+ # if the stream ends there (RFC 9114 "Frame Layout").
44
+ def mid_frame? = !@header.empty? || !@type.nil?
45
+
46
+ # Reads `bytes`, yielding a Started for each frame that begins, the
47
+ # pieces of DATA frames as Frame::Data, other frames whole, and a
48
+ # TooLong for each held frame past `max_payload`. A payload that does
49
+ # not decode raises Frame::Malformed.
50
+ def feed(bytes, &)
51
+ offset = 0
52
+ while offset < bytes.bytesize
53
+ offset = @type ? read_payload(bytes, offset, &) : read_header(bytes, offset, &)
54
+ end
55
+ end
56
+
57
+ private def read_header(bytes, offset, &)
58
+ held = @header.bytesize
59
+ if held.zero?
60
+ source = bytes
61
+ start = offset
62
+ else
63
+ source = @header << bytes.byteslice(offset, MAX_HEADER - held) # steep:ignore
64
+ start = 0
65
+ end
66
+ type, after = Varint.decode(source, start)
67
+ length, after = Varint.decode(source, after) if type && after
68
+ unless length && after
69
+ @header = source.byteslice(start..) if held.zero?
70
+ return bytes.bytesize
71
+ end
72
+ @header = "".b
73
+ begin_frame(type, length, &) # steep:ignore
74
+ held.zero? ? after : offset + after - held
75
+ end
76
+
77
+ private def begin_frame(type, length, &)
78
+ yield Started.new(type:, length:)
79
+ @type = type
80
+ @left = length
81
+ @mode = if type == Frame::DATA
82
+ :pass
83
+ elsif HELD.include?(type) && length <= @max_payload
84
+ :hold
85
+ else
86
+ yield TooLong.new(type:, length:) if HELD.include?(type)
87
+ :skip
88
+ end
89
+ return unless length.zero?
90
+ yield Frame::Data.new(data: "") if @mode == :pass
91
+ finish_frame(&)
92
+ end
93
+
94
+ private def read_payload(bytes, offset, &)
95
+ taken = [@left, bytes.bytesize - offset].min
96
+ case @mode
97
+ when :pass then yield Frame::Data.new(data: bytes.byteslice(offset, taken).freeze) # steep:ignore
98
+ when :hold then @payload << bytes.byteslice(offset, taken) # steep:ignore
99
+ end
100
+ @left -= taken
101
+ finish_frame(&) if @left.zero?
102
+ offset + taken
103
+ end
104
+
105
+ private def finish_frame
106
+ type = @type or return
107
+ payload = @payload
108
+ held = @mode == :hold
109
+ @type = nil
110
+ @payload = "".b
111
+ yield Frame.decode(type, payload.freeze) if held
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "structured_field"
4
+
5
+ module Raiha::HTTP3
6
+ # The checks of RFC 9114 "Malformed Requests and Responses" on the field
7
+ # sections of a message: what fields and pseudo-header fields it holds,
8
+ # in what order, and with what names and values. A message that fails
9
+ # one is a stream error of type H3_MESSAGE_ERROR.
10
+ module Message
11
+ # A field section that fails a check.
12
+ class Malformed < StandardError; end
13
+
14
+ REQUEST_PSEUDO = %w[:method :scheme :authority :path :protocol].freeze
15
+ RESPONSE_PSEUDO = %w[:status].freeze
16
+ # RFC 9114 "HTTP Fields": these hold for one connection only, and are
17
+ # never in an HTTP/3 message; TE is, in a request, as "trailers".
18
+ CONNECTION_SPECIFIC = %w[connection keep-alive proxy-connection transfer-encoding upgrade].freeze
19
+ # A token of RFC 9110 "Field Names" in lowercase, after a ":" for a
20
+ # pseudo-header field.
21
+ NAME = /\A:?[!#$%&'*+\-.^_`|~0-9a-z]+\z/n
22
+ # What never goes in a field value (RFC 9110 "Field Values").
23
+ NOT_IN_VALUE = /[\x00\r\n]/n
24
+ SCHEMES_WITH_AUTHORITY = %w[http https].freeze
25
+ STATUS = /\A[1-5][0-9][0-9]\z/n
26
+
27
+ # Checks the header section of a request: its Content-Length, if it has
28
+ # one. `extended_connect` is whether this end took Extended CONNECT
29
+ # (RFC 9220), for :protocol.
30
+ def self.request(headers, extended_connect:)
31
+ pseudo = pseudo_header_fields(headers, REQUEST_PSEUDO, request: true)
32
+ method = pseudo[":method"] or raise Malformed, "no :method"
33
+ if pseudo.key?(":protocol")
34
+ raise Malformed, ":protocol, with Extended CONNECT not taken" unless extended_connect
35
+ raise Malformed, ":protocol with #{method}" unless method == "CONNECT"
36
+ %w[:scheme :path :authority].each { |name| raise Malformed, "Extended CONNECT with no #{name}" unless pseudo.key?(name) }
37
+ elsif method == "CONNECT"
38
+ raise Malformed, "CONNECT with :scheme or :path" if pseudo.key?(":scheme") || pseudo.key?(":path")
39
+ raise Malformed, "CONNECT with no :authority" unless pseudo[":authority"]&.then { !_1.empty? }
40
+ return content_length(headers)
41
+ else
42
+ %w[:scheme :path].each { |name| raise Malformed, "no #{name}" unless pseudo.key?(name) }
43
+ end
44
+ authority(headers, pseudo)
45
+ path(pseudo)
46
+ content_length(headers)
47
+ end
48
+
49
+ # Checks the header section of a response: its status and its
50
+ # Content-Length, if it has one.
51
+ def self.response(headers)
52
+ status = pseudo_header_fields(headers, RESPONSE_PSEUDO, request: false)[":status"] or raise Malformed, "no :status"
53
+ raise Malformed, ":status #{status.inspect}" unless STATUS.match?(status)
54
+ [Integer(status, 10), content_length(headers)]
55
+ end
56
+
57
+ # Whether the message of `headers` says it uses the Capsule Protocol
58
+ # (RFC 9297 "The Capsule-Protocol Header Field"): one Capsule-Protocol
59
+ # field, an Item that is true; one of another type is as none. Such a
60
+ # message carries no Content-Length nor Content-Type, and a response
61
+ # using it, as `status` says, is none of 204, 205 and 206 ("The Capsule
62
+ # Protocol").
63
+ def self.capsule_protocol?(headers, status: nil)
64
+ values = headers.filter_map { |name, value| value if name == "capsule-protocol" }
65
+ return false unless values.size == 1 && StructuredField.item(values.first) == true
66
+ if headers.any? { |name, _| name == "content-length" || name == "content-type" }
67
+ raise Malformed, "the Capsule Protocol with content"
68
+ end
69
+ raise Malformed, "the Capsule Protocol on a #{status}" if [204, 205, 206].include?(status)
70
+ true
71
+ end
72
+
73
+ # Checks a trailer section, which has no pseudo-header field.
74
+ def self.trailers(headers)
75
+ pseudo_header_fields(headers, [], request: false)
76
+ nil
77
+ end
78
+
79
+ # The pseudo-header fields of `headers`, among `allowed`, each once and
80
+ # before the fields, by name; every name and value checked on the way.
81
+ def self.pseudo_header_fields(headers, allowed, request:)
82
+ pseudo = {} #: Hash[String, String]
83
+ regular = false
84
+ headers.each do |name, value|
85
+ raise Malformed, "the name #{name.inspect}" unless NAME.match?(name)
86
+ raise Malformed, "the value of #{name}" if NOT_IN_VALUE.match?(value)
87
+ if name.start_with?(":")
88
+ raise Malformed, "#{name} after a field" if regular
89
+ raise Malformed, "#{name}, not of this message" unless allowed.include?(name)
90
+ raise Malformed, "#{name} twice" if pseudo.key?(name)
91
+ pseudo[name] = value
92
+ else
93
+ regular = true
94
+ raise Malformed, "the field #{name}, of one connection" if CONNECTION_SPECIFIC.include?(name)
95
+ raise Malformed, "te: #{value}" if name == "te" && !(request && value == "trailers")
96
+ end
97
+ end
98
+ pseudo
99
+ end
100
+
101
+ # RFC 9114 "Request Pseudo-Header Fields": a scheme that has an
102
+ # authority needs :authority or Host, not empty, the same if both, with
103
+ # no userinfo.
104
+ def self.authority(headers, pseudo)
105
+ return unless SCHEMES_WITH_AUTHORITY.include?(pseudo[":scheme"])
106
+ hosts = headers.filter_map { |name, value| value if name == "host" }
107
+ raise Malformed, "Host #{hosts.size} times" if hosts.size > 1
108
+ authority = pseudo[":authority"]
109
+ host = hosts.first
110
+ raise Malformed, "no :authority nor Host" if authority.nil? && host.nil?
111
+ raise Malformed, "an empty :authority or Host" if authority&.empty? || host&.empty?
112
+ raise Malformed, ":authority and Host differ" if authority && host && authority != host
113
+ raise Malformed, "userinfo in the authority" if (authority || host)&.include?("@")
114
+ end
115
+
116
+ # RFC 9114 "Request Pseudo-Header Fields": the :path of a scheme that
117
+ # has an authority is an absolute path, with a query or not, or "*" for
118
+ # an OPTIONS of no path (RFC 9110 "OPTIONS").
119
+ def self.path(pseudo)
120
+ return unless SCHEMES_WITH_AUTHORITY.include?(pseudo[":scheme"])
121
+ path = pseudo[":path"] or return
122
+ return if path.start_with?("/") || (path == "*" && pseudo[":method"] == "OPTIONS")
123
+ raise Malformed, ":path #{path.inspect}"
124
+ end
125
+
126
+ # The value of Content-Length, the same in every field line; nil when
127
+ # there is none.
128
+ def self.content_length(headers)
129
+ values = headers.filter_map { |name, value| value if name == "content-length" }
130
+ return nil if values.empty?
131
+ raise Malformed, "Content-Length #{values.uniq.inspect}" unless values.uniq.size == 1 && values.first.match?(/\A[0-9]{1,19}\z/n)
132
+ Integer(values.first, 10)
133
+ end
134
+
135
+ private_class_method :pseudo_header_fields, :authority, :path, :content_length
136
+ end
137
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # What waits to go on one stream: the bytes of its frames in order, and
5
+ # whether the stream ends after them. Given to the transport a piece at
6
+ # a time; when it takes less than a piece, the rest waits, and so does
7
+ # the stream, until the transport can take more.
8
+ class Output
9
+ # The bytes waiting.
10
+ attr_reader :buffered
11
+
12
+ def initialize
13
+ @pieces = [] #: Array[String]
14
+ @buffered = 0
15
+ @fin = false
16
+ @blocked = false
17
+ end
18
+
19
+ def <<(bytes)
20
+ raise Error::Usage, "the stream has ended" if @fin
21
+ @pieces << bytes unless bytes.empty?
22
+ @buffered += bytes.bytesize
23
+ self
24
+ end
25
+
26
+ # The stream ends after what waits.
27
+ def finish
28
+ @fin = true
29
+ end
30
+
31
+ # Whether the transport is to be given something now.
32
+ def ready? = !@blocked && (!@pieces.empty? || @fin)
33
+
34
+ # The transport can take more again.
35
+ def unblock
36
+ @blocked = false
37
+ end
38
+
39
+ # Gives the waiting pieces to the block, the last with the end of the
40
+ # stream if it ends, until the block, which returns how many bytes it
41
+ # took, takes less than a piece. Whether all went, the end with them.
42
+ def drain
43
+ until @blocked
44
+ piece = @pieces.first || ""
45
+ fin = @fin && @pieces.size <= 1
46
+ return false if piece.empty? && !fin
47
+ taken = yield(piece, fin)
48
+ raise ArgumentError, "#{taken.inspect} bytes taken of #{piece.bytesize}" unless taken.is_a?(Integer) && taken.between?(0, piece.bytesize)
49
+ @buffered -= taken
50
+ if taken < piece.bytesize
51
+ @pieces[0] = piece.byteslice(taken..) # steep:ignore
52
+ @blocked = true
53
+ return false
54
+ end
55
+ @pieces.shift
56
+ return true if fin
57
+ end
58
+ false
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "structured_field"
4
+
5
+ module Raiha::HTTP3
6
+ # The priority of a response (RFC 9218 "Priority Parameters"): its
7
+ # urgency, from 0, the most urgent, to 7, and whether it is incremental.
8
+ class Priority < Data.define(:urgency, :incremental)
9
+ DEFAULT_URGENCY = 3
10
+ URGENCIES = 0..7
11
+
12
+ def initialize(urgency: DEFAULT_URGENCY, incremental: false)
13
+ raise ArgumentError, "urgency is 0 to 7, not #{urgency.inspect}" unless urgency.is_a?(Integer) && URGENCIES.cover?(urgency)
14
+ raise ArgumentError, "incremental is true or false, not #{incremental.inspect}" unless incremental == true || incremental == false
15
+ super
16
+ end
17
+
18
+ # The urgency and the incremental a Priority Field Value gives, each nil
19
+ # where it gives none, or one out of range or of another type, which
20
+ # is ignored; nil for both when the value is not a Dictionary.
21
+ def self.parameters(text)
22
+ members = StructuredField.dictionary(text) or return nil
23
+ urgency = members["u"]
24
+ incremental = members["i"]
25
+ [
26
+ urgency.is_a?(Integer) && URGENCIES.cover?(urgency) ? urgency : nil,
27
+ incremental == true || incremental == false ? incremental : nil
28
+ ] #: [Integer?, bool?]
29
+ end
30
+
31
+ # The priority a request with the Priority header field `text`, or a
32
+ # PRIORITY_UPDATE with it, asks for: the defaults where it gives none,
33
+ # and all of them when it is no Dictionary.
34
+ def self.parse(text)
35
+ urgency, incremental = parameters(text)
36
+ new(urgency: urgency || DEFAULT_URGENCY, incremental: incremental.nil? ? false : incremental)
37
+ end
38
+
39
+ # This one, with what `urgency` and `incremental` give in place of its
40
+ # own; nil leaves one as it is, as a Priority in a response does
41
+ # (RFC 9218 "Merging Client- and Server-Driven Priority Parameters").
42
+ def merge(urgency, incremental) = with(urgency: urgency || self.urgency, incremental: incremental.nil? ? self.incremental : incremental)
43
+
44
+ # The Priority Field Value that gives this one, empty for the defaults.
45
+ def to_field
46
+ parameters = [] #: Array[String]
47
+ parameters << "u=#{urgency}" unless urgency == DEFAULT_URGENCY
48
+ parameters << "i" if incremental
49
+ parameters.join(", ")
50
+ end
51
+ end
52
+ end