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,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "reader"
4
+
5
+ module Raiha::HTTP3::QPACK
6
+ # Writes the primitives of RFC 9204 "Primitives" into a binary String.
7
+ class Writer
8
+ def initialize
9
+ @buffer = String.new(encoding: Encoding::BINARY)
10
+ end
11
+
12
+ # RFC 7541 "Integer Representation" with a `prefix`-bit prefix, under
13
+ # `flags`, the bits of the first byte above the prefix.
14
+ def integer(value, prefix, flags = 0)
15
+ unless value.is_a?(Integer) && value.between?(0, Reader::MAX_INTEGER)
16
+ raise ArgumentError, "a QPACK integer is 0..2^62-1, not #{value.inspect}"
17
+ end
18
+ mask = (1 << prefix) - 1
19
+ return tap { @buffer << (flags | value) } if value < mask
20
+ @buffer << (flags | mask)
21
+ value -= mask
22
+ while value >= 0x80
23
+ @buffer << ((value & 0x7f) | 0x80)
24
+ value >>= 7
25
+ end
26
+ tap { @buffer << value }
27
+ end
28
+
29
+ # RFC 9204 "String Literals" with a `prefix`-bit prefix, under `flags`:
30
+ # in the Huffman code when that is shorter, as it is otherwise.
31
+ def string(value, prefix, flags = 0)
32
+ huffman_length = Huffman.encoded_length(value)
33
+ if huffman_length < value.bytesize
34
+ integer(huffman_length, prefix - 1, flags | (1 << (prefix - 1)))
35
+ tap { @buffer << Huffman.encode(value) }
36
+ else
37
+ integer(value.bytesize, prefix - 1, flags)
38
+ tap { @buffer.append_as_bytes(value) }
39
+ end
40
+ end
41
+
42
+ def to_s = @buffer
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../namespace"
4
+
5
+ # The HTTP/3 layer's module, opened here rather than through "../http3" so
6
+ # that QPACK loads alone, without the frames of HTTP/3 or anything of QUIC.
7
+ module Raiha::HTTP3
8
+ end
9
+
10
+ # QPACK (RFC 9204), field compression for HTTP/3. Knows neither the frames
11
+ # of HTTP/3 nor QUIC: it takes and gives the bytes of field sections and of
12
+ # the encoder and decoder streams.
13
+ module Raiha::HTTP3::QPACK
14
+ end
15
+
16
+ require_relative "qpack/error_code"
17
+ require_relative "qpack/error"
18
+ require_relative "qpack/reader"
19
+ require_relative "qpack/writer"
20
+ require_relative "qpack/huffman"
21
+ require_relative "qpack/static_table"
22
+ require_relative "qpack/dynamic_table"
23
+ require_relative "qpack/field"
24
+ require_relative "qpack/result"
25
+ require_relative "qpack/encoder"
26
+ require_relative "qpack/decoder"
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "frame_reader"
4
+
5
+ module Raiha::HTTP3
6
+ # The receiving side of one request stream: its frames, how far the
7
+ # message has come, and what waits while a field section of it is
8
+ # blocked on the QPACK encoder stream.
9
+ class Receiving
10
+ attr_reader :reader, :waiting
11
+ # The frames seen: :headers until a HEADERS frame, :body after it, and
12
+ # :trailers after a second one.
13
+ attr_accessor :phase
14
+ # Whether a field section waits on QPACK; and behind it, the bytes of
15
+ # the DATA and field sections waiting, and whether the stream ended.
16
+ attr_accessor :blocked, :waiting_bytes, :fin_waiting
17
+ # Whether the application knows of the message: a server's once it has
18
+ # its header section, a client's from the start, as it sent the
19
+ # request. The Content-Length the header section gave, the bytes of DATA
20
+ # so far, and what the trailers may take of the limit on field
21
+ # sections; and whether the message never has content, as the response
22
+ # to a HEAD.
23
+ attr_accessor :surfaced, :content_length, :received, :budget, :no_content
24
+ # Whether the request has a use for HTTP Datagrams (RFC 9297): Extended
25
+ # CONNECT; whether, at a client, it asked for the Capsule Protocol; and
26
+ # the Capsules of the data stream once the Capsule Protocol is in use.
27
+ attr_accessor :datagrams, :capsules_asked, :capsule_reader
28
+
29
+ def initialize(max_payload:)
30
+ @reader = FrameReader.new(max_payload:)
31
+ @phase = :headers #: :headers | :body | :trailers
32
+ @blocked = false
33
+ @waiting = [] #: Array[FrameReader::item]
34
+ @waiting_bytes = 0
35
+ @fin_waiting = false
36
+ @surfaced = false
37
+ @content_length = nil #: Integer?
38
+ @received = 0
39
+ @budget = 0
40
+ @no_content = false
41
+ @datagrams = false
42
+ @capsules_asked = false
43
+ @capsule_reader = nil #: CapsuleReader?
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # The sending side of one request stream, as far as the application has
5
+ # written: :headers until the header section of its message, which
6
+ # interim responses may come before, :body after it.
7
+ class Sending
8
+ attr_accessor :phase
9
+ # Whether a write took less than given, so that the application hears
10
+ # when the stream takes more.
11
+ attr_accessor :wants_writable
12
+ # Whether the request has a use for HTTP Datagrams, and whether its data
13
+ # stream carries Capsules.
14
+ attr_accessor :datagrams, :capsules
15
+
16
+ def initialize
17
+ @phase = :headers #: :headers | :body
18
+ @wants_writable = false
19
+ @datagrams = false
20
+ @capsules = false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../quic"
4
+ require_relative "settings"
5
+
6
+ module Raiha::HTTP3
7
+ # A session an HTTP/3 client may resume, as a value: the QUIC layer's,
8
+ # and the server's SETTINGS, which a 0-RTT attempt's first HTTP/3 layer
9
+ # keeps to until the server's come (RFC 9114 "Initialization"; RFC 9204
10
+ # "Maximum Dynamic Table Capacity"). The application keeps it where it
11
+ # likes, `to_h` and `from_h` turning it into plain values and back.
12
+ #
13
+ # The one value of HTTP/3 that knows the QUIC layer, it is not loaded with
14
+ # the rest of HTTP/3, which does not; the Runtime loads it.
15
+ class Session < Data.define(:quic, :settings)
16
+ def to_h = { quic: quic.to_h, settings: settings.to_h }
17
+
18
+ def self.from_h(hash)
19
+ raise ArgumentError, "a session is a Hash" unless hash.is_a?(Hash)
20
+ quic = hash.fetch(:quic)
21
+ settings = hash.fetch(:settings)
22
+ raise ArgumentError, "quic and settings must be Hashes" unless quic.is_a?(Hash) && settings.is_a?(Hash)
23
+ unless settings.keys.sort == Settings.members.sort
24
+ raise ArgumentError, "settings must name every setting"
25
+ end
26
+ new(quic: Raiha::QUIC::Session.from_h(quic), settings: Settings.new(**settings))
27
+ rescue KeyError => e
28
+ raise ArgumentError, "a session lacks #{e.key}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+ require_relative "frame"
5
+
6
+ module Raiha::HTTP3
7
+ # The settings one end tells the other on its control stream (RFC 9114
8
+ # "SETTINGS"): those of HTTP/3, of QPACK (RFC 9204 "Configuration"), of
9
+ # Extended CONNECT (RFC 9220) and of HTTP Datagrams (RFC 9297).
10
+ #
11
+ # `max_field_section_size` nil is no limit, as a peer that does not say
12
+ # gives. A setting at its default does not go in the frame.
13
+ class Settings < Data.define(:qpack_max_table_capacity, :qpack_blocked_streams, :max_field_section_size, :h3_datagram, :enable_connect_protocol)
14
+ QPACK_MAX_TABLE_CAPACITY = 0x01
15
+ MAX_FIELD_SECTION_SIZE = 0x06
16
+ QPACK_BLOCKED_STREAMS = 0x07
17
+ ENABLE_CONNECT_PROTOCOL = 0x08
18
+ H3_DATAGRAM = 0x33
19
+ # The identifiers of HTTP/2's settings that HTTP/3 has none for, which
20
+ # are never to come (RFC 9114 "Defined SETTINGS Parameters").
21
+ HTTP2_IDENTIFIERS = [0x00, 0x02, 0x03, 0x04, 0x05].freeze
22
+ # The settings that are 0 or 1.
23
+ FLAGS = [ENABLE_CONNECT_PROTOCOL, H3_DATAGRAM].freeze
24
+
25
+ # This end's: QPACK with no dynamic table, a field section of 64 KiB at
26
+ # most, and no HTTP Datagrams. An end takes them only over a transport
27
+ # that takes DATAGRAM frames, which QUIC does not by default; one that
28
+ # does is RECOMMENDED to take them whether it uses them or not, so as
29
+ # not to stand out (RFC 9297 "The SETTINGS_H3_DATAGRAM HTTP/3 Setting").
30
+ def initialize(qpack_max_table_capacity: 0, qpack_blocked_streams: 0, max_field_section_size: 65_536, h3_datagram: false, enable_connect_protocol: false)
31
+ [qpack_max_table_capacity, qpack_blocked_streams, max_field_section_size].compact.each do |value|
32
+ raise ArgumentError, "a setting is 0..2^62-1, not #{value.inspect}" unless value.is_a?(Integer) && value.between?(0, Varint::MAX)
33
+ end
34
+ [h3_datagram, enable_connect_protocol].each do |value|
35
+ raise ArgumentError, "a flag is true or false, not #{value.inspect}" unless value == true || value == false
36
+ end
37
+ super
38
+ end
39
+
40
+ # The peer's, from its SETTINGS frame. An identifier that repeats, one
41
+ # of HTTP/2, or a flag neither 0 nor 1 is H3_SETTINGS_ERROR; those not
42
+ # known are ignored.
43
+ def self.from_frame(frame)
44
+ values = {} #: Hash[Integer, Integer]
45
+ frame.settings.each do |identifier, value|
46
+ if HTTP2_IDENTIFIERS.include?(identifier)
47
+ raise Violation.new("the HTTP/2 setting 0x#{identifier.to_s(16)}", code: ErrorCode::SETTINGS_ERROR)
48
+ end
49
+ raise Violation.new("the setting 0x#{identifier.to_s(16)} twice", code: ErrorCode::SETTINGS_ERROR) if values.key?(identifier)
50
+ if FLAGS.include?(identifier) && value > 1
51
+ raise Violation.new("the setting 0x#{identifier.to_s(16)} is #{value}, not 0 or 1", code: ErrorCode::SETTINGS_ERROR)
52
+ end
53
+ values[identifier] = value
54
+ end
55
+ new(
56
+ qpack_max_table_capacity: values.fetch(QPACK_MAX_TABLE_CAPACITY, 0), qpack_blocked_streams: values.fetch(QPACK_BLOCKED_STREAMS, 0),
57
+ max_field_section_size: values[MAX_FIELD_SECTION_SIZE], h3_datagram: values[H3_DATAGRAM] == 1,
58
+ enable_connect_protocol: values[ENABLE_CONNECT_PROTOCOL] == 1
59
+ )
60
+ end
61
+
62
+ # What a server's 0-RTT is taken only if the same of (RFC 9114
63
+ # "Initialization"): each setting its frame tells, in the order of its
64
+ # identifier, as a varint identifier and a varint value. The frame has
65
+ # no GREASE, which could change from one connection to the next.
66
+ def early_data_context
67
+ to_frame.settings.sort_by(&:first).map { |identifier, value| Varint.encode(identifier) + Varint.encode(value) }.join.b
68
+ end
69
+
70
+ # The SETTINGS frame that tells these.
71
+ def to_frame
72
+ settings = [] #: Array[[Integer, Integer]]
73
+ settings << [QPACK_MAX_TABLE_CAPACITY, qpack_max_table_capacity] unless qpack_max_table_capacity.zero?
74
+ settings << [MAX_FIELD_SECTION_SIZE, max_field_section_size] if max_field_section_size
75
+ settings << [QPACK_BLOCKED_STREAMS, qpack_blocked_streams] unless qpack_blocked_streams.zero?
76
+ settings << [ENABLE_CONNECT_PROTOCOL, 1] if enable_connect_protocol
77
+ settings << [H3_DATAGRAM, 1] if h3_datagram
78
+ Frame::Settings.new(settings:)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strscan"
4
+
5
+ module Raiha::HTTP3
6
+ # Reads a Dictionary of Structured Field Values (RFC 8941), the form of
7
+ # the Priority header field and of PRIORITY_UPDATE (RFC 9218 "Priority
8
+ # Parameters"), and an Item, the form of Capsule-Protocol (RFC 9297).
9
+ # Every type of item is read, so that a field is known to be of its form
10
+ # or not, but only the values come back:
11
+ # Integers, Floats for Decimals, Strings for Strings and Byte Sequences,
12
+ # Symbols for Tokens, true and false, and Arrays for Inner Lists. The
13
+ # parameters of members are read and dropped.
14
+ module StructuredField
15
+ KEY = /[a-z*][a-z0-9_\-.*]*/
16
+ TOKEN = %r{[A-Za-z*][!#$%&'*+\-.^_`|~0-9A-Za-z:/]*}
17
+ INTEGER = /-?[0-9]{1,15}(?![0-9.])/
18
+ DECIMAL = /-?[0-9]{1,12}\.[0-9]{1,3}(?![0-9.])/
19
+ BYTE_SEQUENCE = %r{:([A-Za-z0-9+/]*={0,2}):}
20
+ STRING_RUN = /[\x20\x21\x23-\x5b\x5d-\x7e]+/n
21
+ OWS = /[ \t]*/
22
+ SP = / */
23
+
24
+ # The members of the Dictionary `text` is, by key, the last of a key
25
+ # that repeats; nil when it is not one.
26
+ def self.dictionary(text)
27
+ scanner = StringScanner.new(text.b)
28
+ scanner.skip(SP)
29
+ members = {} #: Hash[String, value]
30
+ until scanner.eos?
31
+ key = scanner.scan(KEY) or return nil
32
+ if scanner.skip("=")
33
+ value = member_value(scanner)
34
+ return nil if value.nil?
35
+ else
36
+ value = true
37
+ parameters(scanner) or return nil
38
+ end
39
+ members[key] = value
40
+ scanner.skip(OWS)
41
+ break if scanner.eos?
42
+ return nil unless scanner.skip(",")
43
+ scanner.skip(OWS)
44
+ return nil if scanner.eos?
45
+ end
46
+ members
47
+ end
48
+
49
+ # The bare item of the Item `text` is, its parameters dropped; nil when
50
+ # it is not one (RFC 8941 "Parsing an Item").
51
+ def self.item(text)
52
+ scanner = StringScanner.new(text.b)
53
+ scanner.skip(SP)
54
+ value = bare_item(scanner)
55
+ return nil if value.nil? || !parameters(scanner)
56
+ scanner.skip(SP)
57
+ scanner.eos? ? value : nil
58
+ end
59
+
60
+ # An Item or an Inner List, with its parameters; nil when there is
61
+ # neither.
62
+ def self.member_value(scanner)
63
+ value = scanner.match?("(") ? inner_list(scanner) : bare_item(scanner)
64
+ return nil if value.nil?
65
+ parameters(scanner) or return nil
66
+ value
67
+ end
68
+
69
+ def self.inner_list(scanner)
70
+ scanner.skip("(")
71
+ items = [] #: Array[value]
72
+ loop do
73
+ scanner.skip(SP)
74
+ return items if scanner.skip(")")
75
+ item = bare_item(scanner)
76
+ return nil if item.nil?
77
+ parameters(scanner) or return nil
78
+ items << item
79
+ return nil unless scanner.match?(/[ )]/)
80
+ end
81
+ end
82
+
83
+ # Each parameter after a ";"; false when one does not parse.
84
+ def self.parameters(scanner)
85
+ while scanner.skip(";")
86
+ scanner.skip(SP)
87
+ scanner.skip(KEY) or return false
88
+ return false if scanner.skip("=") && bare_item(scanner).nil?
89
+ end
90
+ true
91
+ end
92
+
93
+ def self.bare_item(scanner)
94
+ if (decimal = scanner.scan(DECIMAL)) then Float(decimal)
95
+ elsif (integer = scanner.scan(INTEGER)) then Integer(integer, 10)
96
+ elsif scanner.match?('"') then string(scanner)
97
+ elsif scanner.scan(BYTE_SEQUENCE) then scanner[1].unpack1("m") # steep:ignore
98
+ elsif scanner.skip("?1") then true
99
+ elsif scanner.skip("?0") then false
100
+ elsif (token = scanner.scan(TOKEN)) then token.to_sym
101
+ end
102
+ end
103
+
104
+ def self.string(scanner)
105
+ scanner.skip('"')
106
+ string = +""
107
+ loop do
108
+ if scanner.skip('"') then return string
109
+ elsif scanner.skip("\\")
110
+ escaped = scanner.scan(/["\\]/) or return nil
111
+ string << escaped
112
+ elsif (run = scanner.scan(STRING_RUN)) then string << run
113
+ else return nil
114
+ end
115
+ end
116
+ end
117
+
118
+ private_class_method :member_value, :inner_list, :parameters, :bare_item, :string
119
+ end
120
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # What an HTTP/3 connection tells of itself as it goes, for a qlog or
5
+ # anything else: a tracer includes this and overrides what it wants;
6
+ # what it does not is ignored.
7
+ #
8
+ # `initiator` is :local or :remote. A frame comes with its `type` and
9
+ # the `length` of its payload, nil where the frame is written whole;
10
+ # with `frame`, the frame itself, for those of the control stream and
11
+ # PRIORITY_UPDATE; and with `headers`, a field section as [name, value]
12
+ # pairs, for HEADERS. Nothing here is copied for the tracer: what it
13
+ # keeps, it copies.
14
+ module Tracer
15
+ def parameters_set(initiator:, settings:) = nil
16
+
17
+ # `type` is a stream type, or :request for a request stream.
18
+ def stream_type_set(initiator:, stream_id:, type:) = nil
19
+
20
+ def frame_created(stream_id:, type:, length: nil, frame: nil, headers: nil) = nil
21
+
22
+ def frame_parsed(stream_id:, type:, length: nil, frame: nil, headers: nil) = nil
23
+
24
+ def priority_updated(stream_id:, priority:) = nil
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::HTTP3
4
+ # The variable-length integers of RFC 9000 "Variable-Length Integer
5
+ # Encoding", which HTTP/3 takes for frame types, lengths and the fields
6
+ # of frames: the two high bits of the first byte give the length, 1, 2,
7
+ # 4 or 8 bytes. HTTP/3 has its own, as it does not know QUIC.
8
+ module Varint
9
+ MAX = (1 << 62) - 1
10
+
11
+ def self.size(value)
12
+ raise ArgumentError, "a varint is 0..2^62-1, not #{value.inspect}" unless value.is_a?(Integer) && value.between?(0, MAX)
13
+ if value < (1 << 6) then 1
14
+ elsif value < (1 << 14) then 2
15
+ elsif value < (1 << 30) then 4
16
+ else 8
17
+ end
18
+ end
19
+
20
+ # The shortest encoding of `value`.
21
+ def self.encode(value)
22
+ case size(value)
23
+ when 1 then [value].pack("C")
24
+ when 2 then [0x4000 | value].pack("n")
25
+ when 4 then [0x8000_0000 | value].pack("N")
26
+ else [0xc000_0000_0000_0000 | value].pack("Q>")
27
+ end
28
+ end
29
+
30
+ # The value at `offset` of `bytes` and the offset past it; nil when the
31
+ # bytes end before it does.
32
+ def self.decode(bytes, offset = 0)
33
+ first = bytes.getbyte(offset) or return nil
34
+ size = 1 << (first >> 6)
35
+ return nil if offset + size > bytes.bytesize
36
+ value = case size
37
+ when 1 then first
38
+ when 2 then bytes.unpack1("n", offset:) & 0x3fff
39
+ when 4 then bytes.unpack1("N", offset:) & 0x3fff_ffff
40
+ else bytes.unpack1("Q>", offset:) & MAX
41
+ end
42
+ [value, offset + size]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "namespace"
4
+
5
+ # HTTP/3. Does not know the TLS or QUIC layers.
6
+ module Raiha::HTTP3
7
+ end
8
+
9
+ require_relative "http3/error_code"
10
+ require_relative "http3/error"
11
+ require_relative "http3/varint"
12
+ require_relative "http3/frame"
13
+ require_relative "http3/frame_reader"
14
+ require_relative "http3/settings"
15
+ require_relative "http3/priority"
16
+ require_relative "http3/action"
17
+ require_relative "http3/output"
18
+ require_relative "http3/connection"
19
+ require_relative "http3/qlog"
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "quic"
4
+ require_relative "http3"
5
+
6
+ module Raiha
7
+ # Joins one QUIC connection and the HTTP/3 connection over it, neither of
8
+ # which knows the other: what QUIC tells of its streams goes to HTTP/3,
9
+ # and what HTTP/3 writes and asks of the transport goes to QUIC, the
10
+ # size of a DATAGRAM, which changes with the path, included. It decides
11
+ # nothing of its own beyond opening HTTP/3's three streams once QUIC has
12
+ # connected, and which streams to read. A client's connection
13
+ # that sends 0-RTT carries HTTP/3 from the start: the streams open at the
14
+ # first `flush`, and the requests go with them before the handshake
15
+ # completes.
16
+ #
17
+ # The application gives it each event of the QUIC connection with
18
+ # `receive`, then calls `flush`; HTTP/3's events are then the HTTP/3
19
+ # connection's to take. The streams are read at `flush`, after the
20
+ # events: a stream the peer reset reads as ended, and HTTP/3 has heard of
21
+ # the reset by then, so the end is not taken for its FIN.
22
+ class HTTP3Binding
23
+ # The events of the QUIC connection that are HTTP/3's alone.
24
+ HTTP3_EVENTS = [
25
+ QUIC::Event::StreamReadable, QUIC::Event::StreamWritable, QUIC::Event::StreamReset, QUIC::Event::StopSending,
26
+ QUIC::Event::DatagramReceived
27
+ ].freeze
28
+ # The most bytes one read of a stream asks for.
29
+ READ_SIZE = 64 * 1024
30
+
31
+ def initialize(connection, http3)
32
+ @connection = connection
33
+ @http3 = http3
34
+ ask_datagram_size(http3)
35
+ @paused = {} #: Hash[Integer, true]
36
+ @closed = false
37
+ @started = false
38
+ @start_due = connection.zero_rtt_state == :pending
39
+ end
40
+
41
+ # Takes `event` of the QUIC connection: whether it was HTTP/3's alone.
42
+ # Event::Connected opens HTTP/3's streams, and Event::Closed stops
43
+ # `flush`; both are for the application too.
44
+ def receive(event, now:)
45
+ case event
46
+ when QUIC::Event::Connected then connected(now)
47
+ when QUIC::Event::Closed then @closed = true
48
+ when QUIC::Event::StreamWritable then @http3.stream_writable(event.stream_id)
49
+ when QUIC::Event::StreamReset then @http3.stream_reset(event.stream_id, error_code: event.error_code)
50
+ when QUIC::Event::StopSending then @http3.stream_stopped(event.stream_id, error_code: event.error_code)
51
+ when QUIC::Event::DatagramReceived then @http3.datagram_received(event.data)
52
+ end
53
+ HTTP3_EVENTS.any? { |type| event.is_a?(type) }
54
+ end
55
+
56
+ # Reads the streams with something to read into HTTP/3, all but those
57
+ # paused, and gives QUIC what HTTP/3 asks of it, writes to its streams
58
+ # and sends as DATAGRAMs, in that order. Once QUIC has closed, what came
59
+ # before the close is still read, a response that came with the
60
+ # CONNECTION_CLOSE say, and nothing is written.
61
+ def flush(now:)
62
+ @connection.readable_streams.each { |stream_id| read(stream_id) unless @paused.key?(stream_id) }
63
+ return if @closed
64
+ start(now) if @start_due
65
+ @http3.each_action { |action| perform(action, now) }
66
+ @http3.each_send { |stream_id, data, fin| @connection.stream_write(stream_id, data, fin:) }
67
+ @http3.each_datagram_send { |data, urgency| @connection.datagram_send(data, urgency:) }
68
+ end
69
+
70
+ # The server rejected the client's 0-RTT: the streams the client opened
71
+ # are gone, and with them HTTP/3's, and `http3` takes the place of the
72
+ # HTTP/3 they were of. Its streams open once the handshake has
73
+ # completed. The server's streams carry nothing until then, so `http3`
74
+ # reads them from their start.
75
+ def restart(http3)
76
+ @http3 = http3
77
+ ask_datagram_size(http3)
78
+ @paused.clear
79
+ @started = false
80
+ @start_due = @connection.handshake_complete?
81
+ end
82
+
83
+ # Leaves `stream_id` unread until `resume`, so that QUIC's flow control
84
+ # holds the peer back.
85
+ def pause(stream_id)
86
+ @paused[stream_id] = true
87
+ end
88
+
89
+ # Reads `stream_id` again from the next `flush`.
90
+ def resume(stream_id)
91
+ @paused.delete(stream_id)
92
+ end
93
+
94
+ # `http3` asks QUIC the size of a DATAGRAM each time one is to go: the
95
+ # peer's own limit once the handshake has completed, not the one 0-RTT
96
+ # remembered, and the path's as it changes.
97
+ private def ask_datagram_size(http3)
98
+ http3.datagram_max_size = -> { @connection.datagram_max_size }
99
+ end
100
+
101
+ # The handshake has completed: HTTP/3's streams open unless 0-RTT opened
102
+ # them.
103
+ private def connected(now)
104
+ start(now) unless @started
105
+ end
106
+
107
+ # The control and QPACK streams, three of this end's going one way,
108
+ # which RFC 9114 "Unidirectional Streams" has the peer allow; without
109
+ # them the connection closes with H3_GENERAL_PROTOCOL_ERROR.
110
+ private def start(now)
111
+ @started = true
112
+ @start_due = false
113
+ control, encoder, decoder = Array.new(3) { @connection.open_stream(direction: :uni) }
114
+ if control && encoder && decoder
115
+ @http3.start(control:, encoder:, decoder:)
116
+ else
117
+ @connection.close(error_code: HTTP3::ErrorCode::GENERAL_PROTOCOL_ERROR, reason: "fewer than three streams going one way", now:)
118
+ end
119
+ end
120
+
121
+ # What `stream_id` has, read at once, and its end if it has ended.
122
+ private def read(stream_id)
123
+ data = "".b
124
+ while (chunk = @connection.stream_read(stream_id, READ_SIZE)) && !chunk.empty?
125
+ data << chunk
126
+ end
127
+ @http3.stream_data(stream_id, data, fin: chunk.nil?) unless data.empty? && chunk
128
+ end
129
+
130
+ private def perform(action, now)
131
+ case action
132
+ when HTTP3::Action::Reset then @connection.stream_reset(action.stream_id, error_code: action.error_code)
133
+ when HTTP3::Action::StopSending then @connection.stream_stop_sending(action.stream_id, error_code: action.error_code)
134
+ when HTTP3::Action::Priority then @connection.stream_priority(action.stream_id, urgency: action.urgency, incremental: action.incremental)
135
+ when HTTP3::Action::Close then @connection.close(error_code: action.error_code, reason: action.reason, now:)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The namespace skeleton of raiha. This is the only file written in the
4
+ # nested form; every other file opens its module in the compact form
5
+ # (`module Raiha::TLS`), so a bare `TLS` or `QUIC` raises NameError instead
6
+ # of resolving to a neighbouring layer.
7
+ #
8
+ # Only `Raiha` is defined here. Defining the layer modules too would make
9
+ # `Raiha::QUIC` visible after `require "raiha/tls"`.
10
+ module Raiha
11
+ end