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,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::Runtime
4
+ # Runs an Endpoint on a UDP socket, and on others besides, as a server's
5
+ # at its preferred addresses, with fibers of its own under the Fiber
6
+ # scheduler: a reader for each socket waits for datagrams and gives them
7
+ # to the Endpoint; the runner runs the timers, lets the owner take its
8
+ # turn and sends what there is to send, then waits until a timer is due
9
+ # or something has changed. A Transmit goes from the socket its source
10
+ # is on (UDP#sends_from?), the first when none is.
11
+ #
12
+ # A call into the core never waits: the runtime gives it no I/O that could
13
+ # yield to another fiber (a qlog goes to memory, and to its file after the
14
+ # call), so each call runs to its end before any other fiber runs, and no
15
+ # lock is held around it. The datagrams go out after it. The owner, a
16
+ # client or a server of the runtime, is told of each Route but a response
17
+ # to send, takes its turn each time round with a timer of its own, and is
18
+ # told when the driver stops, with what failed when something did.
19
+ #
20
+ # The driver of one of a server's workers hands on to its `worker` what
21
+ # the Endpoint routes to another (Route::Foreign), with where it came from
22
+ # and when, and in each turn gives the Endpoint what other workers handed
23
+ # on to this one, as it came then.
24
+ class Driver
25
+ attr_reader :endpoint, :failure
26
+
27
+ def self.clock = Process.clock_gettime(Process::CLOCK_MONOTONIC)
28
+
29
+ def initialize(endpoint, udp, owner, others: [], worker: nil)
30
+ @endpoint = endpoint
31
+ @worker = worker
32
+ @udp = udp
33
+ @udps = [udp, *others]
34
+ @owner = owner
35
+ # Only for the runner to wait on.
36
+ @lock = Thread::Mutex.new
37
+ @changed = Thread::ConditionVariable.new
38
+ @dirty = false
39
+ @stopped = false
40
+ @failure = nil #: StandardError?
41
+ end
42
+
43
+ # Starts the reader and the runner, in fibers of the scheduler.
44
+ def start
45
+ raise Error::Usage, "the runtime waits through a Fiber scheduler; this thread has none" unless Fiber.scheduler
46
+ @udps.each { |udp| Fiber.schedule { read(udp) } }
47
+ Fiber.schedule { run }
48
+ self
49
+ end
50
+
51
+ # Has the runner go round again, as after a call into the core.
52
+ def changed
53
+ @lock.synchronize do
54
+ @dirty = true
55
+ @changed.signal
56
+ end
57
+ end
58
+
59
+ def stopped? = @stopped
60
+
61
+ # Sends what there is to send now from the calling fiber, as the last
62
+ # datagrams of a connection closed before the driver stops.
63
+ def flush
64
+ transmits = [] #: Array[Raiha::QUIC::Transmit]
65
+ @endpoint.each_transmit(now: Driver.clock) { |transmit| transmits << transmit }
66
+ transmits.each { |transmit| emit(transmit) }
67
+ end
68
+
69
+ # Stops the readers and the runner and closes the sockets; the owner is
70
+ # told. The sockets close even when telling the owner fails.
71
+ def stop
72
+ return if @stopped
73
+ @stopped = true
74
+ changed
75
+ @owner.stopped(@failure)
76
+ ensure
77
+ @udps.each { |udp| udp.close unless udp.closed? }
78
+ end
79
+
80
+ private def read(udp)
81
+ until @stopped
82
+ udp.wait_readable
83
+ responses = take(udp.receive, Driver.clock)
84
+ changed
85
+ responses.each { |transmit| emit(transmit) }
86
+ end
87
+ rescue IOError, Errno::EBADF
88
+ raise unless @stopped
89
+ rescue StandardError => error
90
+ fail(error)
91
+ end
92
+
93
+ # Gives each datagram to the Endpoint, and each Route to the owner; the
94
+ # responses the Endpoint sends itself.
95
+ private def take(datagrams, now)
96
+ responses = [] #: Array[Raiha::QUIC::Transmit]
97
+ datagrams.each do |data, from, to, ecn|
98
+ case (route = @endpoint.receive(data, from:, to:, ecn:, now:))
99
+ when Raiha::QUIC::Route::Respond then responses << route.transmit
100
+ when Raiha::QUIC::Route::Foreign then @worker&.forward(route.worker, [data, from, to, ecn, now])
101
+ when nil then nil
102
+ else @owner.route(route, now)
103
+ end
104
+ end
105
+ responses
106
+ end
107
+
108
+ # What other workers handed on to this one, given to the Endpoint as
109
+ # it came then, never to be handed on again; the responses it sends.
110
+ private def take_forwarded
111
+ responses = [] #: Array[Raiha::QUIC::Transmit]
112
+ @worker&.take&.each do |data, from, to, ecn, received_at|
113
+ case (route = @endpoint.receive(data, from:, to:, ecn:, now: received_at, forwarded: true))
114
+ when Raiha::QUIC::Route::Respond then responses << route.transmit
115
+ when nil then nil
116
+ else @owner.route(route, received_at)
117
+ end
118
+ end
119
+ responses
120
+ end
121
+
122
+ private def run
123
+ until @stopped
124
+ turn(Driver.clock).each { |transmit| emit(transmit) }
125
+ @lock.synchronize do
126
+ @changed.wait(@lock, timeout) unless @dirty || @stopped
127
+ @dirty = false
128
+ end
129
+ end
130
+ rescue IOError, Errno::EBADF
131
+ raise unless @stopped
132
+ rescue StandardError => error
133
+ fail(error)
134
+ end
135
+
136
+ # The timers of the Endpoint, the owner's turn, and what there is to
137
+ # send after.
138
+ private def turn(now)
139
+ take_forwarded.each { |transmit| emit(transmit) }
140
+ deadline = @endpoint.next_timeout
141
+ @endpoint.handle_timeout(now:) if deadline && deadline <= now
142
+ @owner.turn(now)
143
+ transmits = [] #: Array[Raiha::QUIC::Transmit]
144
+ @endpoint.each_transmit(now:) { |transmit| transmits << transmit }
145
+ transmits
146
+ end
147
+
148
+ # Seconds until the next timer, nil for none.
149
+ private def timeout
150
+ deadline = [@endpoint.next_timeout, @owner.next_timeout].compact.min or return nil
151
+ [deadline - Driver.clock, 0.0].max
152
+ end
153
+
154
+ # Sends `transmit` from the socket its source is on.
155
+ private def emit(transmit)
156
+ source = transmit.source
157
+ (source && @udps.find { |udp| udp.sends_from?(source) } || @udp).send(transmit)
158
+ end
159
+
160
+ private def fail(error)
161
+ @failure ||= error
162
+ stop
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::Runtime
4
+ # Misuse of the runtime. What the peer or the network does to a
5
+ # connection comes as QUIC::ClosedError instead.
6
+ module Error
7
+ class Base < StandardError; end
8
+
9
+ # A call the runtime does not allow, as one with no Fiber scheduler.
10
+ class Usage < Base; end
11
+
12
+ # A part of the runtime that runs on its own, as the record of 0-RTT
13
+ # the workers of Workers share, stopped before it was told to.
14
+ class Stopped < Base; end
15
+ end
16
+ end
@@ -0,0 +1,363 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stringio"
4
+ require_relative "../http3_binding"
5
+ require_relative "../http3/session"
6
+ require_relative "http3_fields"
7
+
8
+ module Raiha::Runtime
9
+ # The HTTP/3 the applications of the runtime use: a client over a Session
10
+ # of Client.connect, and a server. HTTP/3 itself is the sans-IO
11
+ # Raiha::HTTP3, joined to the QUIC connection by HTTP3Binding.
12
+ module HTTP3
13
+ # The peer reset the request or the response a read or a write waits
14
+ # on, with the HTTP/3 `error_code`.
15
+ class StreamReset < Raiha::Runtime::StreamReset; end
16
+
17
+ # A response read whole: its status, its header fields without the
18
+ # pseudo-header ones, its body and its trailer fields ([] for none).
19
+ class Response < Data.define(:status, :headers, :body, :trailers); end
20
+
21
+ # What the client keeps of a request until its response is read: the
22
+ # response as it comes, its body as far as the application has not read
23
+ # it, how the peer reset the response or stopped the request, and
24
+ # whether the server rejected the 0-RTT it went in. The server keeps the
25
+ # same of a request's body until the request is over, and whether its
26
+ # response has gone whole.
27
+ class Exchange
28
+ # The most of a body kept unread before its stream is read no more,
29
+ # so that the peer's flow control holds the rest back. What one read
30
+ # of the stream takes comes on top, as much as the stream's flow
31
+ # control window lets the peer send. What is unread is kept in one
32
+ # String, however small the DATA frames it came in.
33
+ MAX_UNREAD = 256 * 1024
34
+
35
+ attr_accessor :status, :headers, :trailers, :reset, :stopped, :paused, :answered, :rejected
36
+ attr_reader :unread
37
+
38
+ def initialize
39
+ @status = nil #: Integer?
40
+ @headers = [] #: Array[[String, String]]
41
+ @data = "".b
42
+ @unread = 0
43
+ @trailers = [] #: Array[[String, String]]
44
+ @reset = nil #: Integer?
45
+ @stopped = nil #: Integer?
46
+ @paused = false
47
+ @answered = false
48
+ @rejected = false
49
+ @ended = false
50
+ end
51
+
52
+ def <<(chunk)
53
+ @data << chunk
54
+ @unread += chunk.bytesize
55
+ self
56
+ end
57
+
58
+ # What of the body has come and is not read yet, nil when nothing has.
59
+ def shift
60
+ return nil if @data.empty?
61
+ data = @data
62
+ @data = "".b
63
+ @unread = 0
64
+ data
65
+ end
66
+
67
+ def full? = @unread >= MAX_UNREAD
68
+
69
+ def ended? = @ended
70
+
71
+ # The response has ended; with `dropped`, what was not read goes.
72
+ def end!(dropped: false)
73
+ @ended = true
74
+ return unless dropped
75
+ @data = "".b
76
+ @unread = 0
77
+ end
78
+ end
79
+
80
+ # A response as it comes, given to the block of Client#request once
81
+ # its header section has.
82
+ class ResponseStream
83
+ attr_reader :status, :headers, :body
84
+
85
+ def initialize(client, stream_id, exchange, status)
86
+ @client = client
87
+ @stream_id = stream_id
88
+ @exchange = exchange
89
+ @status = status
90
+ @headers = exchange.headers
91
+ @body = Body.new(client, stream_id)
92
+ end
93
+
94
+ # The trailer fields, once the body has been read to its end.
95
+ def trailers = @exchange.trailers
96
+
97
+ # Stops the request and its response with H3_REQUEST_CANCELLED.
98
+ def cancel
99
+ @client.cancel(@stream_id)
100
+ end
101
+ end
102
+
103
+ # The body of a response, read as it comes.
104
+ class Body
105
+ def initialize(client, stream_id)
106
+ @client = client
107
+ @stream_id = stream_id
108
+ end
109
+
110
+ # The next chunk once there is one, nil at the end.
111
+ def read_chunk = @client.read_chunk(@stream_id)
112
+
113
+ # Each chunk until the end, waiting for each.
114
+ def each
115
+ while (chunk = read_chunk)
116
+ yield chunk
117
+ end
118
+ end
119
+
120
+ # All of what is left.
121
+ def read
122
+ data = "".b
123
+ each { |chunk| data << chunk }
124
+ data
125
+ end
126
+ end
127
+
128
+ # The client of HTTP/3 over a Session, which it attaches itself to. Each
129
+ # request goes on a stream of its own, and the fiber that made it writes
130
+ # its body and waits for its response. A body is a String or what reads
131
+ # as an IO does, and goes as the peer's flow control lets it; the body
132
+ # of a response is read no further than MAX_UNREAD bytes ahead of the
133
+ # application.
134
+ #
135
+ # When the server rejects 0-RTT, each request that went in it raises
136
+ # QUIC::Error::ZeroRttRejected, and HTTP/3 starts over with no SETTINGS
137
+ # remembered. Sending one again is the application's (RFC 9846 "Replay
138
+ # Attacks on 0-RTT"). A connection whose handshake chose another ALPN
139
+ # than "h3", which a client offering more may be given once its 0-RTT
140
+ # of "h3" was rejected, closes with H3_GENERAL_PROTOCOL_ERROR.
141
+ class Client
142
+ # The most of a request's body read from it at a time.
143
+ PIECE = 64 * 1024
144
+ ALPN = "h3"
145
+
146
+ def initialize(session, authority: nil, settings: Raiha::HTTP3::Settings.new)
147
+ # One of threads would be called into with no lock taken.
148
+ raise Error::Usage, "a Session of the runtime on fibers, not #{session.class}" if session.is_a?(Threaded::Session)
149
+ @session = session
150
+ @settings = settings
151
+ @tracer = (quic = session.tracer) && Raiha::HTTP3::Qlog::Tracer.new(quic)
152
+ @http3 = Raiha::HTTP3::Connection.client(settings:, tracer: @tracer, remembered:)
153
+ @binding = Raiha::HTTP3Binding.new(session.connection, @http3)
154
+ @exchanges = {} #: Hash[Integer, Exchange]
155
+ session.attach(self, close_code: Raiha::HTTP3::ErrorCode::NO_ERROR)
156
+ @authority = authority || session.server_name || raise(Error::Usage, "no authority to request from")
157
+ end
158
+
159
+ # The tickets the server gave, each an HTTP3::Session with the server's
160
+ # SETTINGS, for Client.connect(session:); none until the SETTINGS have
161
+ # come, which may be after the tickets (RFC 9114 "Initialization"). As
162
+ # Session#tickets, the newest last.
163
+ def tickets
164
+ settings = @http3.peer_settings or return []
165
+ @session.tickets.map { |quic| Raiha::HTTP3::Session.new(quic:, settings:) }
166
+ end
167
+
168
+ # The response to a GET of `path`, its body read whole.
169
+ def get(path, headers: [])
170
+ request("GET", path, headers:)
171
+ end
172
+
173
+ # Sends a request, with `body` when there is one, and its priority
174
+ # (RFC 9218) as the `priority` field when `urgency` (0 to 7) or
175
+ # `incremental` is given. The response comes to the block as a
176
+ # ResponseStream, and the block's value is returned; with no block, the
177
+ # response is read whole into a Response. A response the block left
178
+ # unread is cancelled.
179
+ def request(method, path, headers: [], body: nil, urgency: nil, incremental: nil)
180
+ stream_id, exchange = start(method, path, headers + Fields.priority(urgency, incremental), fin: body.nil?)
181
+ upload(stream_id, exchange, body) if body
182
+ status = @session.wait_for(drain: true) do
183
+ check(stream_id, exchange)
184
+ exchange.status
185
+ end
186
+ return yield ResponseStream.new(self, stream_id, exchange, status) if block_given?
187
+ Response.new(status:, headers: exchange.headers, body: Body.new(self, stream_id).read, trailers: exchange.trailers)
188
+ ensure
189
+ finish(stream_id, exchange) if stream_id && exchange
190
+ end
191
+
192
+ # The next chunk of the response on `stream_id` once there is one, nil
193
+ # at its end; for Body.
194
+ def read_chunk(stream_id)
195
+ exchange = @exchanges[stream_id] or return nil
196
+ chunk = @session.wait_for(drain: true) do
197
+ check(stream_id, exchange)
198
+ exchange.shift || (:end if exchange.ended?)
199
+ end
200
+ resume(stream_id, exchange) if exchange.paused && !exchange.full?
201
+ chunk if chunk.is_a?(String)
202
+ end
203
+
204
+ # Stops the request on `stream_id` and its response; for
205
+ # ResponseStream.
206
+ def cancel(stream_id)
207
+ exchange = @exchanges[stream_id] or return
208
+ return if exchange.ended? || exchange.reset
209
+ @http3.cancel(stream_id)
210
+ exchange.end!(dropped: true)
211
+ @binding.resume(stream_id)
212
+ flush
213
+ end
214
+
215
+ # The Session's protocol: the events of QUIC that are HTTP/3's.
216
+ def take(event, now)
217
+ case event
218
+ when Raiha::QUIC::Event::ZeroRttRejected then zero_rtt_rejected
219
+ when Raiha::QUIC::Event::Connected
220
+ alpn = event.handshake.alpn
221
+ unless alpn == ALPN
222
+ @session.connection.close(error_code: Raiha::HTTP3::ErrorCode::GENERAL_PROTOCOL_ERROR, reason: "ALPN #{alpn.inspect}, not h3",
223
+ now:)
224
+ return false
225
+ end
226
+ end
227
+ @binding.receive(event, now:)
228
+ end
229
+
230
+ # The Session's protocol: a client's session runs out of no server's
231
+ # time.
232
+ def expire = nil
233
+
234
+ # The Session's protocol: HTTP/3 reads what came and has what it wrote
235
+ # go out, and its events go to the exchanges they are of.
236
+ def turn(now)
237
+ @binding.flush(now:)
238
+ @http3.each_event { |event| take_http3(event) }
239
+ @binding.flush(now:)
240
+ end
241
+
242
+ # Sends the request on a new stream: its ID and the Exchange kept of it,
243
+ # which a rejected 0-RTT may have let go of by the time it is looked
244
+ # for. A request HTTP/3 does not take, after the server's GOAWAY or
245
+ # with header fields it finds malformed, gives its stream up both
246
+ # ways.
247
+ private def start(method, path, headers, fin:)
248
+ stream_id = @session.open_stream_id
249
+ exchange = @exchanges[stream_id] = Exchange.new
250
+ begin
251
+ @http3.send_request(stream_id, Fields.request(method, @authority, path) + headers, fin:)
252
+ rescue StandardError
253
+ @exchanges.delete(stream_id)
254
+ @session.connection.stream_reset(stream_id, error_code: Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED)
255
+ @session.connection.stream_stop_sending(stream_id, error_code: Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED)
256
+ @session.changed
257
+ raise
258
+ end
259
+ flush
260
+ [stream_id, exchange] #: [Integer, Exchange]
261
+ end
262
+
263
+ # While 0-RTT goes, the SETTINGS of the HTTP3::Session the connection
264
+ # resumes, which the requests keep to until the server's come.
265
+ private def remembered
266
+ resumed = @session.resumed
267
+ resumed.settings if resumed.is_a?(Raiha::HTTP3::Session) && @session.connection.zero_rtt_state == :pending
268
+ end
269
+
270
+ # Writes `body` as the request's content, as the stream takes it, and
271
+ # ends it. A peer that stops the request with H3_NO_ERROR wants no
272
+ # more of it and answers all the same (RFC 9114 "Request
273
+ # Cancellation and Rejection").
274
+ private def upload(stream_id, exchange, body)
275
+ source = body.is_a?(String) ? StringIO.new(body) : body
276
+ while (piece = source.read(PIECE))
277
+ written = 0
278
+ until written == piece.bytesize
279
+ written += @session.wait_for do
280
+ check(stream_id, exchange)
281
+ stopped = exchange.stopped and return check_stopped(stream_id, stopped)
282
+ taken = @http3.send_data(stream_id, piece.byteslice(written..) || "".b)
283
+ # Nothing taken, nothing to send: waking the driver would wake
284
+ # this fiber again at once.
285
+ next nil unless taken.positive?
286
+ flush
287
+ taken
288
+ end
289
+ end
290
+ end
291
+ @http3.send_data(stream_id, "".b, fin: true)
292
+ flush
293
+ end
294
+
295
+ private def check_stopped(stream_id, error_code)
296
+ raise StreamReset.new(stream_id, error_code) unless error_code == Raiha::HTTP3::ErrorCode::NO_ERROR
297
+ end
298
+
299
+ # What the server did to the request, raised where the fiber that made
300
+ # it waits.
301
+ private def check(stream_id, exchange)
302
+ raise Raiha::QUIC::Error::ZeroRttRejected, "the request on stream #{stream_id} went in rejected 0-RTT" if exchange.rejected
303
+ reset = exchange.reset and raise StreamReset.new(stream_id, reset)
304
+ end
305
+
306
+ # Lets go of what was kept of the request; a response not read to its
307
+ # end is cancelled. A request of rejected 0-RTT has nothing kept, and
308
+ # its stream's ID may be another's now.
309
+ private def finish(stream_id, exchange)
310
+ return unless @exchanges[stream_id].equal?(exchange)
311
+ cancel(stream_id)
312
+ @exchanges.delete(stream_id)
313
+ end
314
+
315
+ # The server rejected 0-RTT: the streams of the requests are gone, and
316
+ # HTTP/3's, and it starts over with a layer that remembers nothing.
317
+ private def zero_rtt_rejected
318
+ @exchanges.each_value { |exchange| exchange.rejected = true }
319
+ @exchanges.clear
320
+ @http3 = Raiha::HTTP3::Connection.client(settings: @settings, tracer: @tracer)
321
+ @binding.restart(@http3)
322
+ end
323
+
324
+ private def resume(stream_id, exchange)
325
+ exchange.paused = false
326
+ @binding.resume(stream_id)
327
+ flush
328
+ end
329
+
330
+ private def flush
331
+ @binding.flush(now: Driver.clock)
332
+ @session.changed
333
+ end
334
+
335
+ private def take_http3(event)
336
+ case event
337
+ when Raiha::HTTP3::Event::Headers
338
+ exchange = @exchanges[event.stream_id] or return
339
+ status = Fields.status(event.headers) or return
340
+ exchange.status = status
341
+ exchange.headers = Fields.split(event.headers).last
342
+ exchange.end! if event.fin
343
+ when Raiha::HTTP3::Event::Data
344
+ exchange = @exchanges[event.stream_id] or return
345
+ exchange << event.data unless event.data.empty?
346
+ exchange.end! if event.fin
347
+ pause(event.stream_id, exchange) if exchange.full? && !exchange.paused
348
+ when Raiha::HTTP3::Event::Trailers
349
+ exchange = @exchanges[event.stream_id] or return
350
+ exchange.trailers = event.headers
351
+ exchange.end!
352
+ when Raiha::HTTP3::Event::Reset then @exchanges[event.stream_id]&.reset = event.error_code
353
+ when Raiha::HTTP3::Event::Stopped then @exchanges[event.stream_id]&.stopped = event.error_code
354
+ end
355
+ end
356
+
357
+ private def pause(stream_id, exchange)
358
+ exchange.paused = true
359
+ @binding.pause(stream_id)
360
+ end
361
+ end
362
+ end
363
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::Runtime
4
+ module HTTP3
5
+ # How the HTTP/3 of the runtime puts the fields of a request and a
6
+ # response together and takes them apart, apart from how it waits. The
7
+ # HTTP/3 of fibers and that of threads do it alike.
8
+ module Fields
9
+ # The pseudo-header fields of `headers` by name, and the other fields
10
+ # in their order.
11
+ def self.split(headers)
12
+ pseudo, fields = headers.partition { |name, _| name.start_with?(":") }
13
+ [pseudo.to_h, fields] #: [Hash[String, String], Array[[String, String]]]
14
+ end
15
+
16
+ # The pseudo-header fields of a request of `method` for `path` at
17
+ # `authority`, over https.
18
+ def self.request(method, authority, path)
19
+ [[":method", method], [":scheme", "https"], [":authority", authority], [":path", path]] #: Array[[String, String]]
20
+ end
21
+
22
+ # The `priority` field (RFC 9218) of `urgency` (0 to 7) and
23
+ # `incremental`; none when neither is given.
24
+ def self.priority(urgency, incremental)
25
+ raise ArgumentError, "an urgency is 0 to 7, not #{urgency.inspect}" unless urgency.nil? || (urgency.is_a?(Integer) && urgency.between?(0, 7))
26
+ parameters = [("u=#{urgency}" if urgency), ("i" if incremental)].compact
27
+ parameters.empty? ? [] : [["priority", parameters.join(", ")]]
28
+ end
29
+
30
+ # The status of a response's header section `headers`, 0 when it has
31
+ # none; nil for an interim response (1xx), which is let go.
32
+ def self.status(headers)
33
+ status = headers.find { |name, _| name == ":status" }&.last.to_i
34
+ status unless status.between?(100, 199)
35
+ end
36
+ end
37
+ end
38
+ end