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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Getting started
|
|
2
|
+
|
|
3
|
+
This guide runs an HTTP/3 server on the loopback interface and fetches a page from it with Raiha's own client. Both are plain Ruby scripts that block on their UDP sockets; neither needs a Fiber scheduler.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
Add Raiha to your Gemfile and run `bundle install`:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "raiha"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
QUIC always runs over TLS, so the server needs a certificate. For trying things out, make a self-signed one for `localhost`:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -keyout key.pem -out cert.pem -days 30 -subj /CN=localhost -addext subjectAltName=DNS:localhost
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## The server
|
|
20
|
+
|
|
21
|
+
Save this as `server.rb`:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require "raiha"
|
|
25
|
+
require "socket"
|
|
26
|
+
|
|
27
|
+
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
28
|
+
|
|
29
|
+
chain = OpenSSL::X509::Certificate.load(File.read("cert.pem")).map(&:to_der)
|
|
30
|
+
key = OpenSSL::PKey.read(File.read("key.pem")).private_to_der
|
|
31
|
+
identity = Raiha::TLS::Keyring::Identity.new(cert_der: chain, key_der: key)
|
|
32
|
+
|
|
33
|
+
tls_config = Raiha::TLS::Config.server(
|
|
34
|
+
profile: Raiha::TLS::Profile::QUIC,
|
|
35
|
+
alpn: ["h3"],
|
|
36
|
+
registry: Raiha::TLS::Extension::Registry::BUILTIN.with(Raiha::QUIC::TransportParameters::Extension)
|
|
37
|
+
)
|
|
38
|
+
keyring = Raiha::TLS::Keyring.server(config: tls_config, identities: [identity])
|
|
39
|
+
|
|
40
|
+
socket = UDPSocket.new
|
|
41
|
+
socket.bind("127.0.0.1", 4433)
|
|
42
|
+
endpoint = Raiha::QUIC::Endpoint.server(config: Raiha::QUIC::Config.new(ecn_address_families: [], enable_pmtud: false), random: SecureRandom, time: Time.now, now:)
|
|
43
|
+
# Each QUIC connection with its HTTP/3 connection and the binding between them.
|
|
44
|
+
connections = {}
|
|
45
|
+
|
|
46
|
+
def respond(http3, event)
|
|
47
|
+
return unless event.is_a?(Raiha::HTTP3::Event::Headers)
|
|
48
|
+
path = event.headers.to_h[":path"]
|
|
49
|
+
http3.send_headers(event.stream_id, [[":status", "200"], ["content-type", "text/plain"]])
|
|
50
|
+
http3.send_data(event.stream_id, "hello from #{path}\n", fin: true)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
begin
|
|
54
|
+
loop do
|
|
55
|
+
endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
56
|
+
|
|
57
|
+
deadline = endpoint.next_timeout
|
|
58
|
+
if IO.select([socket], nil, nil, deadline && [deadline - now, 0].max)
|
|
59
|
+
data, address = socket.recvfrom(65535)
|
|
60
|
+
route = endpoint.receive(data, from: [address[3], address[1]], now:)
|
|
61
|
+
route = endpoint.admit(route.handle, now:) if route.is_a?(Raiha::QUIC::Route::Incoming)
|
|
62
|
+
case route
|
|
63
|
+
when Raiha::QUIC::Route::ClientHello
|
|
64
|
+
quic = endpoint.accept(route.handle, tls_config:, keyring:, now:)
|
|
65
|
+
http3 = Raiha::HTTP3::Connection.server
|
|
66
|
+
connections[quic] = [http3, Raiha::HTTP3Binding.new(quic, http3)]
|
|
67
|
+
when Raiha::QUIC::Route::Respond
|
|
68
|
+
socket.send(route.transmit.payload, 0, *route.transmit.destination)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
deadline = endpoint.next_timeout
|
|
72
|
+
endpoint.handle_timeout(now:) if deadline && now >= deadline
|
|
73
|
+
|
|
74
|
+
connections.each do |quic, (http3, binding)|
|
|
75
|
+
quic.each_event { |event| binding.receive(event, now:) }
|
|
76
|
+
binding.flush(now:)
|
|
77
|
+
http3.each_event { |event| respond(http3, event) }
|
|
78
|
+
binding.flush(now:)
|
|
79
|
+
end
|
|
80
|
+
connections.delete_if { |quic, _| quic.closed? }
|
|
81
|
+
end
|
|
82
|
+
rescue Interrupt
|
|
83
|
+
connections.each_key { |quic| quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:) }
|
|
84
|
+
endpoint.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
85
|
+
end
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
What each part does:
|
|
89
|
+
|
|
90
|
+
- `Raiha::TLS::Keyring::Identity` is a certificate chain and its private key, both DER-encoded. `cert.pem` may hold the whole chain, leaf first.
|
|
91
|
+
- `Raiha::TLS::Config.server` is the TLS side. `Raiha::TLS::Profile::QUIC` sets TLS up as QUIC needs it, `alpn` offers HTTP/3 (`"h3"`), and the registry adds the QUIC transport parameters extension to the TLS extensions Raiha knows. `Raiha::TLS::Keyring.server` holds the identities the server can present.
|
|
92
|
+
- `Raiha::QUIC::Endpoint.server` is the QUIC side of the socket. It does no I/O: the loop gives it each datagram with `receive` and sends what `each_transmit` gives. `now` is a monotonic clock in seconds, passed to every call, and `Raiha::QUIC::Config.new` takes the transport settings ([Configuration](configuration.md) lists them). A plain `UDPSocket` can neither mark ECN nor keep IP from fragmenting path MTU probes, so the example turns both off with `ecn_address_families: []` and `enable_pmtud: false`.
|
|
93
|
+
- `receive` returns a route. A new client's first datagrams come as `Raiha::QUIC::Route::Incoming`; `admit` turns it into `Raiha::QUIC::Route::ClientHello` once the ClientHello is whole, and `accept` makes it a `Raiha::QUIC::Connection`.
|
|
94
|
+
- `Raiha::HTTP3::Connection.server` is the HTTP/3 of that connection, and `Raiha::HTTP3Binding` carries stream data between the two. Each pass gives the QUIC events to the binding, flushes it, takes the HTTP/3 events, and flushes again so that the response goes to QUIC.
|
|
95
|
+
- `respond` answers each request's header section with `send_headers` and `send_data`; headers are an array of `[name, value]` pairs with lowercase names.
|
|
96
|
+
- Once `endpoint.next_timeout` has come, `handle_timeout` runs the timers (retransmission, the idle timeout and so on), whether or not a datagram came.
|
|
97
|
+
- Ctrl-C closes every connection with `H3_NO_ERROR` and sends the CONNECTION_CLOSE frames.
|
|
98
|
+
|
|
99
|
+
Run it:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
bundle exec ruby server.rb
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
[HTTP/3 server](http3-server.md) goes through the loop in detail.
|
|
106
|
+
|
|
107
|
+
## The client
|
|
108
|
+
|
|
109
|
+
Save this as `client.rb` and run it with `bundle exec ruby client.rb` while the server runs:
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
require "raiha"
|
|
113
|
+
require "socket"
|
|
114
|
+
|
|
115
|
+
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
116
|
+
|
|
117
|
+
tls_config = Raiha::TLS::Config.client(
|
|
118
|
+
trust_anchors: [OpenSSL::X509::Certificate.new(File.read("cert.pem")).to_der],
|
|
119
|
+
profile: Raiha::TLS::Profile::QUIC,
|
|
120
|
+
alpn: ["h3"],
|
|
121
|
+
registry: Raiha::TLS::Extension::Registry::BUILTIN.with(Raiha::QUIC::TransportParameters::Extension)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
socket = UDPSocket.new
|
|
125
|
+
socket.bind("127.0.0.1", 0)
|
|
126
|
+
quic = Raiha::QUIC::Connection.client(config: Raiha::QUIC::Config.new(ecn_address_families: [], enable_pmtud: false), tls_config:, keyring: Raiha::TLS::Keyring.client(config: tls_config),
|
|
127
|
+
server_name: "localhost", remote: ["127.0.0.1", 4433], time: Time.now, now:)
|
|
128
|
+
http3 = Raiha::HTTP3::Connection.client
|
|
129
|
+
binding = Raiha::HTTP3Binding.new(quic, http3)
|
|
130
|
+
|
|
131
|
+
status = nil
|
|
132
|
+
body = +""
|
|
133
|
+
done = false
|
|
134
|
+
|
|
135
|
+
until done || quic.closed?
|
|
136
|
+
quic.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
137
|
+
deadline = quic.next_timeout
|
|
138
|
+
if IO.select([socket], nil, nil, deadline && [deadline - now, 0].max)
|
|
139
|
+
data, address = socket.recvfrom(65535)
|
|
140
|
+
quic.receive(data, from: [address[3], address[1]], now:)
|
|
141
|
+
end
|
|
142
|
+
deadline = quic.next_timeout
|
|
143
|
+
quic.handle_timeout(now:) if deadline && now >= deadline
|
|
144
|
+
|
|
145
|
+
quic.each_event do |event|
|
|
146
|
+
binding.receive(event, now:)
|
|
147
|
+
next unless event.is_a?(Raiha::QUIC::Event::Connected)
|
|
148
|
+
request = [[":method", "GET"], [":scheme", "https"], [":authority", "localhost"], [":path", "/hello"]]
|
|
149
|
+
http3.send_request(quic.open_stream, request, fin: true)
|
|
150
|
+
end
|
|
151
|
+
binding.flush(now:)
|
|
152
|
+
http3.each_event do |event|
|
|
153
|
+
case event
|
|
154
|
+
when Raiha::HTTP3::Event::Headers
|
|
155
|
+
status = event.headers.to_h[":status"]
|
|
156
|
+
done = event.fin
|
|
157
|
+
when Raiha::HTTP3::Event::Data
|
|
158
|
+
body << event.data
|
|
159
|
+
done = event.fin
|
|
160
|
+
when Raiha::HTTP3::Event::Trailers, Raiha::HTTP3::Event::Reset
|
|
161
|
+
done = true
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
binding.flush(now:)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:)
|
|
168
|
+
quic.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
169
|
+
puts status, body
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
It prints:
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
200
|
|
176
|
+
hello from /hello
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The client trusts only the certificates in `trust_anchors`, here the self-signed one, and checks the server's certificate for `server_name`. Its loop is the server's for one connection: once the handshake completes (`Raiha::QUIC::Event::Connected`), it opens a stream and sends the request on it, then gathers the response's events until the response ends. [HTTP/3 client](http3-client.md) explains why it checks for the end right after taking the events.
|
|
180
|
+
|
|
181
|
+
A curl built with HTTP/3 support can ask the server too: `curl -k --http3-only https://127.0.0.1:4433/`.
|
|
182
|
+
|
|
183
|
+
## Next steps
|
|
184
|
+
|
|
185
|
+
- [HTTP/3 server](http3-server.md): the loop, request bodies, large responses, trailers, stopping and 0-RTT
|
|
186
|
+
- [HTTP/3 client](http3-client.md): request bodies, several requests on one connection, resumption and 0-RTT
|
|
187
|
+
- [QUIC](quic.md): the sans-I/O QUIC core without HTTP/3
|
|
188
|
+
- [Fiber scheduler](fiber-scheduler.md): the runtime, which runs these loops for you under a Fiber scheduler such as the `async` gem, with Rack and Ractor workers
|
|
189
|
+
- [Configuration](configuration.md): transport settings, certificates, resumption and 0-RTT
|
|
190
|
+
- [qlog](qlog.md): tracing connections
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# HTTP/3 client
|
|
2
|
+
|
|
3
|
+
A client is a `Raiha::QUIC::Connection.client` with a `Raiha::HTTP3::Connection.client` over it, joined by `Raiha::HTTP3Binding`, and a loop that you write around a UDP socket. None of them does I/O. The loop below blocks its thread on `IO.select` and needs no Fiber scheduler; under one, `Raiha::Runtime::Client.connect` and `Raiha::Runtime::HTTP3::Client` do the same in a few lines (see [fiber-scheduler.md](fiber-scheduler.md)).
|
|
4
|
+
|
|
5
|
+
The QUIC side of the loop is described in [quic.md](quic.md), the TLS and QUIC settings in [configuration.md](configuration.md), and the server these examples talk to in [http3-server.md](http3-server.md).
|
|
6
|
+
|
|
7
|
+
## A complete client
|
|
8
|
+
|
|
9
|
+
This client GETs `/hello` from the server at 127.0.0.1:4433, which presents a self-signed certificate for `localhost` from `cert.pem`, and prints the status and the body.
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
require "raiha"
|
|
13
|
+
require "socket"
|
|
14
|
+
|
|
15
|
+
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
16
|
+
|
|
17
|
+
tls_config = Raiha::TLS::Config.client(
|
|
18
|
+
trust_anchors: [OpenSSL::X509::Certificate.new(File.read("cert.pem")).to_der],
|
|
19
|
+
profile: Raiha::TLS::Profile::QUIC,
|
|
20
|
+
alpn: ["h3"],
|
|
21
|
+
registry: Raiha::TLS::Extension::Registry::BUILTIN.with(Raiha::QUIC::TransportParameters::Extension)
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
socket = UDPSocket.new
|
|
25
|
+
socket.bind("127.0.0.1", 0)
|
|
26
|
+
quic = Raiha::QUIC::Connection.client(config: Raiha::QUIC::Config.new(ecn_address_families: [], enable_pmtud: false), tls_config:, keyring: Raiha::TLS::Keyring.client(config: tls_config),
|
|
27
|
+
server_name: "localhost", remote: ["127.0.0.1", 4433], time: Time.now, now:)
|
|
28
|
+
http3 = Raiha::HTTP3::Connection.client
|
|
29
|
+
binding = Raiha::HTTP3Binding.new(quic, http3)
|
|
30
|
+
|
|
31
|
+
status = nil
|
|
32
|
+
body = +""
|
|
33
|
+
done = false
|
|
34
|
+
|
|
35
|
+
until done || quic.closed?
|
|
36
|
+
quic.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
37
|
+
deadline = quic.next_timeout
|
|
38
|
+
if IO.select([socket], nil, nil, deadline && [deadline - now, 0].max)
|
|
39
|
+
data, address = socket.recvfrom(65535)
|
|
40
|
+
quic.receive(data, from: [address[3], address[1]], now:)
|
|
41
|
+
end
|
|
42
|
+
deadline = quic.next_timeout
|
|
43
|
+
quic.handle_timeout(now:) if deadline && now >= deadline
|
|
44
|
+
|
|
45
|
+
quic.each_event do |event|
|
|
46
|
+
binding.receive(event, now:)
|
|
47
|
+
next unless event.is_a?(Raiha::QUIC::Event::Connected)
|
|
48
|
+
request = [[":method", "GET"], [":scheme", "https"], [":authority", "localhost"], [":path", "/hello"]]
|
|
49
|
+
http3.send_request(quic.open_stream, request, fin: true)
|
|
50
|
+
end
|
|
51
|
+
binding.flush(now:)
|
|
52
|
+
http3.each_event do |event|
|
|
53
|
+
case event
|
|
54
|
+
when Raiha::HTTP3::Event::Headers
|
|
55
|
+
status = event.headers.to_h[":status"]
|
|
56
|
+
done = event.fin
|
|
57
|
+
when Raiha::HTTP3::Event::Data
|
|
58
|
+
body << event.data
|
|
59
|
+
done = event.fin
|
|
60
|
+
when Raiha::HTTP3::Event::Trailers, Raiha::HTTP3::Event::Reset
|
|
61
|
+
done = true
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
binding.flush(now:)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:)
|
|
68
|
+
quic.each_transmit(now:) { |transmit| socket.send(transmit.payload, 0, *transmit.destination) }
|
|
69
|
+
puts status, body
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`trust_anchors` is what the server's certificate is verified against, and `server_name` the name it is checked for and sent in SNI. To trust the system's CAs instead, see [configuration.md](configuration.md). `ecn_address_families: []` and `enable_pmtud: false` are for the plain `UDPSocket`, which neither marks ECN nor keeps IP from fragmenting path MTU probes; see [quic.md](quic.md#a-plain-socket).
|
|
73
|
+
|
|
74
|
+
## The loop
|
|
75
|
+
|
|
76
|
+
Each pass sends what is due, waits for a datagram or `quic.next_timeout`, hands any datagram to `quic.receive`, runs the timers with `handle_timeout` once `next_timeout` has come (whether or not a datagram came), and then takes what that brought: the QUIC events to `binding.receive`, `binding.flush`, the HTTP/3 events, and `binding.flush` again so that what you wrote goes to QUIC.
|
|
77
|
+
|
|
78
|
+
Keep that order, and check whether you are done right after taking the events, before the next wait. A loop that waits first and checks after can sleep through the end of the response: once the server has sent the last of it, no datagram may come until the idle timeout.
|
|
79
|
+
|
|
80
|
+
As at a server, a flush can bring HTTP/3 events of its own (`Raiha::HTTP3::Event::Writable`), so a client that sends large bodies goes round `flush` and `each_event` until a pass brings no event. The class at the end of this page does.
|
|
81
|
+
|
|
82
|
+
## Requests
|
|
83
|
+
|
|
84
|
+
A request goes on a bidirectional stream that QUIC opens: `quic.open_stream` returns its ID (0, 4, 8, ...), or `nil` while the server allows no more streams, in which case try again on a later pass; there is no event for it. Before the handshake has completed (`Raiha::QUIC::Event::Connected`) and outside 0-RTT, QUIC knows no limit yet and opens nothing.
|
|
85
|
+
|
|
86
|
+
`http3.send_request(stream_id, headers, fin: false)` sends the header section. `headers` is an Array of `[name, value]` Strings, the pseudo-header fields first: `:method`, `:scheme` (`https`), `:authority` and `:path`. Names must be lowercase. A malformed request raises `ArgumentError`; one after the server's GOAWAY raises `Raiha::HTTP3::Error::Usage`.
|
|
87
|
+
|
|
88
|
+
With `fin: true` the request has no body. Otherwise send it with `http3.send_data(stream_id, data, fin: true)`, which returns how many bytes it took. It holds at most 64 KiB for the stream (`max_send_buffer:` of `Connection.client`); with less taken than given, `Raiha::HTTP3::Event::Writable` tells when to give the rest, and `fin` applies only once all of it is taken. Trailers end the request: `send_headers` after the body.
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
http3.send_request(stream_id, [[":method", "POST"], [":scheme", "https"], [":authority", "localhost"], [":path", "/upload"]])
|
|
92
|
+
http3.send_data(stream_id, "abc")
|
|
93
|
+
http3.send_headers(stream_id, [["x-sum", "3"]])
|
|
94
|
+
binding.flush(now:)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Responses
|
|
98
|
+
|
|
99
|
+
The response comes as events of its stream:
|
|
100
|
+
|
|
101
|
+
| Event | What it means |
|
|
102
|
+
| --- | --- |
|
|
103
|
+
| `Raiha::HTTP3::Event::Headers` | A header section, `[":status", "200"]` first. A 1xx status is an interim response; the final one follows. `fin` when the response has no body. |
|
|
104
|
+
| `Raiha::HTTP3::Event::Data` | Some of the body, as it came. `fin` when it ends here, which may come with empty `data`. |
|
|
105
|
+
| `Raiha::HTTP3::Event::Trailers` | The trailer section, which ends the response. |
|
|
106
|
+
| `Raiha::HTTP3::Event::Reset` | The server reset the response, with the HTTP/3 `error_code` (for example `H3_REQUEST_REJECTED` after its GOAWAY). |
|
|
107
|
+
| `Raiha::HTTP3::Event::Stopped` | The server does not want the rest of the request's body. With `H3_NO_ERROR` it answers all the same. |
|
|
108
|
+
| `Raiha::HTTP3::Event::GoAway` | The server goes away: requests on streams from `stream_id` on were not processed, and no new request can be sent. |
|
|
109
|
+
| `Raiha::HTTP3::Event::Blocked` | A header section waits for the QPACK encoder stream, which can happen only when the client's `qpack_max_table_capacity` is above 0. Its `Headers` or `Trailers` follow; there is nothing to do. |
|
|
110
|
+
| `Raiha::HTTP3::Event::Datagram`, `Raiha::HTTP3::Event::Capsule` | An HTTP Datagram, or another capsule, for an Extended CONNECT request (RFC 9297). |
|
|
111
|
+
|
|
112
|
+
A malformed response never reaches you: HTTP/3 resets its stream with `H3_MESSAGE_ERROR` and gives `Reset`.
|
|
113
|
+
|
|
114
|
+
`http3.cancel(stream_id)` drops a request and its response both ways with `H3_REQUEST_CANCELLED`; call `binding.flush` after it. Events already queued for the stream may still come.
|
|
115
|
+
|
|
116
|
+
### Priority
|
|
117
|
+
|
|
118
|
+
A `priority` header field in the request (`[["priority", "u=5, i"]]`, RFC 9218) asks for the urgency and incrementality of the response. `http3.update_priority(stream_id, urgency: 1)` changes it while the request is open, sending a PRIORITY_UPDATE frame; it raises `Raiha::HTTP3::Error::Usage` once the request is over, and before the handshake has completed, while HTTP/3 has no control stream to send the frame on.
|
|
119
|
+
|
|
120
|
+
## Closing
|
|
121
|
+
|
|
122
|
+
`quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:)` closes with `H3_NO_ERROR`, HTTP/3's code for a clean close, then `each_transmit` sends the CONNECTION_CLOSE. Whichever end closes, and for whatever reason (an HTTP/3 error included, which the binding closes the connection for), `Raiha::QUIC::Event::Closed` comes with the `error` and whether it was `by_peer`, and `quic.closed?` turns true once the closing period is over.
|
|
123
|
+
|
|
124
|
+
## Several requests, resumption and 0-RTT
|
|
125
|
+
|
|
126
|
+
The class below keeps the responses of many requests on one connection by stream ID, and the session tickets and tokens the server gives for later connections:
|
|
127
|
+
|
|
128
|
+
- `Raiha::QUIC::Event::SessionTicket` gives a ticket as a `Raiha::QUIC::Session`. For HTTP/3 it goes with the server's SETTINGS, as `Raiha::HTTP3::Session.new(quic:, settings: http3.peer_settings)`, since requests in 0-RTT keep to the SETTINGS the ticket remembers. A server without ticket keys sends none.
|
|
129
|
+
- `Raiha::QUIC::Event::NewToken` gives a `Raiha::QUIC::Token`, which spares a later connection the server's address validation.
|
|
130
|
+
- `Raiha::HTTP3::Session#to_h` and `Raiha::HTTP3::Session.from_h` turn a ticket into plain values and back, to keep it where you like, as `Raiha::QUIC::Token#to_h` and `Raiha::QUIC::Token.from_h` do a token. Use each ticket and token for one connection only, since using one again links the connections for anyone watching.
|
|
131
|
+
|
|
132
|
+
A later connection gives them to `Raiha::QUIC::Connection.client(session: ticket.quic, token:, early_data: true)` and to `Raiha::HTTP3::Connection.client(remembered: ticket.settings)` when `quic.zero_rtt_state` is `:pending`. It may then open streams and send requests at once, in 0-RTT, which an attacker can replay: send only requests that are safe to repeat.
|
|
133
|
+
|
|
134
|
+
If the server rejects the 0-RTT, `Raiha::QUIC::Event::ZeroRttRejected` comes, the streams opened in it are gone, and HTTP/3 starts over. Acknowledge it with `quic.acknowledge_zero_rtt_rejection`, give the binding a new HTTP/3 connection with `binding.restart`, and decide which requests to send again. `quic.zero_rtt_state` ends as `:accepted` or `:rejected`.
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
# One HTTP/3 connection on its own UDP socket, and the responses to its
|
|
138
|
+
# requests by stream ID.
|
|
139
|
+
class Client
|
|
140
|
+
attr_reader :quic, :http3, :responses, :tickets, :tokens, :rejected
|
|
141
|
+
|
|
142
|
+
def initialize(host, port, server_name:, tls_config:, session: nil, token: nil, early_data: false)
|
|
143
|
+
@socket = UDPSocket.new
|
|
144
|
+
@socket.bind("127.0.0.1", 0)
|
|
145
|
+
@quic = Raiha::QUIC::Connection.client(config: Raiha::QUIC::Config.new(ecn_address_families: [], enable_pmtud: false), tls_config:, keyring: Raiha::TLS::Keyring.client(config: tls_config),
|
|
146
|
+
server_name:, remote: [host, port], time: Time.now, now:, session: session&.quic, token:, early_data:)
|
|
147
|
+
# In 0-RTT, HTTP/3 keeps to the server's SETTINGS the ticket remembers.
|
|
148
|
+
remembered = session.settings if @quic.zero_rtt_state == :pending
|
|
149
|
+
@http3 = Raiha::HTTP3::Connection.client(remembered:)
|
|
150
|
+
@binding = Raiha::HTTP3Binding.new(@quic, @http3)
|
|
151
|
+
@authority = server_name
|
|
152
|
+
@connected = false
|
|
153
|
+
@responses = {}
|
|
154
|
+
@unsent = {}
|
|
155
|
+
@writable = []
|
|
156
|
+
@tickets = []
|
|
157
|
+
@tokens = []
|
|
158
|
+
@rejected = []
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Whether requests can go: the handshake has completed, or 0-RTT goes.
|
|
162
|
+
def ready? = @connected || @quic.zero_rtt_state == :pending
|
|
163
|
+
|
|
164
|
+
# Sends a request, with `body` when given, on a new stream: its ID, or
|
|
165
|
+
# nil when the server allows no more streams yet.
|
|
166
|
+
def request(method, path, headers: [], body: nil)
|
|
167
|
+
stream_id = @quic.open_stream or return nil
|
|
168
|
+
pseudo = [[":method", method], [":scheme", "https"], [":authority", @authority], [":path", path]]
|
|
169
|
+
@http3.send_request(stream_id, pseudo + headers, fin: body.nil?)
|
|
170
|
+
@responses[stream_id] = { status: nil, headers: [], body: +"", trailers: [], done: false, request: [method, path] }
|
|
171
|
+
write(stream_id, body) if body
|
|
172
|
+
@binding.flush(now:)
|
|
173
|
+
stream_id
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def cancel(stream_id)
|
|
177
|
+
@http3.cancel(stream_id)
|
|
178
|
+
@responses.delete(stream_id)
|
|
179
|
+
@binding.flush(now:)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def done?(stream_id) = @responses.fetch(stream_id)[:done]
|
|
183
|
+
|
|
184
|
+
# The tickets to resume with, each with the server's SETTINGS.
|
|
185
|
+
def http3_tickets
|
|
186
|
+
settings = @http3.peer_settings or return []
|
|
187
|
+
@tickets.map { |quic| Raiha::HTTP3::Session.new(quic:, settings:) }
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Runs passes until the block is true or the connection has closed.
|
|
191
|
+
def run_until
|
|
192
|
+
turn until yield || @quic.closed?
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def close
|
|
196
|
+
@quic.close(error_code: Raiha::HTTP3::ErrorCode::NO_ERROR, now:)
|
|
197
|
+
@quic.each_transmit(now:) { |transmit| @socket.send(transmit.payload, 0, *transmit.destination) }
|
|
198
|
+
@socket.close
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# One pass: sends what is due, waits for a datagram or the next timer,
|
|
202
|
+
# and takes what came.
|
|
203
|
+
private def turn
|
|
204
|
+
@quic.each_transmit(now:) { |transmit| @socket.send(transmit.payload, 0, *transmit.destination) }
|
|
205
|
+
deadline = @quic.next_timeout
|
|
206
|
+
if IO.select([@socket], nil, nil, deadline && [deadline - now, 0].max)
|
|
207
|
+
data, address = @socket.recvfrom(65535)
|
|
208
|
+
@quic.receive(data, from: [address[3], address[1]], now:)
|
|
209
|
+
end
|
|
210
|
+
deadline = @quic.next_timeout
|
|
211
|
+
@quic.handle_timeout(now:) if deadline && now >= deadline
|
|
212
|
+
@quic.each_event do |event|
|
|
213
|
+
@binding.receive(event, now:)
|
|
214
|
+
case event
|
|
215
|
+
when Raiha::QUIC::Event::Connected then @connected = true
|
|
216
|
+
when Raiha::QUIC::Event::SessionTicket then @tickets << event.session
|
|
217
|
+
when Raiha::QUIC::Event::NewToken then @tokens << event.token
|
|
218
|
+
when Raiha::QUIC::Event::ZeroRttRejected then restart
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
loop do
|
|
222
|
+
@binding.flush(now:)
|
|
223
|
+
taken = 0
|
|
224
|
+
@http3.each_event { |event| take(event); taken += 1 }
|
|
225
|
+
@writable.each { |stream_id| (data = @unsent.delete(stream_id)) && write(stream_id, data) }
|
|
226
|
+
@writable.clear
|
|
227
|
+
break if taken.zero?
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
private def take(event)
|
|
232
|
+
case event
|
|
233
|
+
when Raiha::HTTP3::Event::Writable then @writable << event.stream_id
|
|
234
|
+
when Raiha::HTTP3::Event::Stopped then @unsent.delete(event.stream_id)
|
|
235
|
+
when Raiha::HTTP3::Event::Headers
|
|
236
|
+
response = @responses[event.stream_id] or return
|
|
237
|
+
status = event.headers.to_h[":status"]
|
|
238
|
+
return if status.start_with?("1") # an interim response
|
|
239
|
+
response[:status] = Integer(status)
|
|
240
|
+
response[:headers] = event.headers.reject { |name, _| name.start_with?(":") }
|
|
241
|
+
response[:done] = event.fin
|
|
242
|
+
when Raiha::HTTP3::Event::Data
|
|
243
|
+
response = @responses[event.stream_id] or return
|
|
244
|
+
response[:body] << event.data
|
|
245
|
+
response[:done] = event.fin
|
|
246
|
+
when Raiha::HTTP3::Event::Trailers
|
|
247
|
+
response = @responses[event.stream_id] or return
|
|
248
|
+
response[:trailers] = event.headers
|
|
249
|
+
response[:done] = true
|
|
250
|
+
when Raiha::HTTP3::Event::Reset
|
|
251
|
+
@unsent.delete(event.stream_id)
|
|
252
|
+
response = @responses[event.stream_id] or return
|
|
253
|
+
response[:reset] = event.error_code
|
|
254
|
+
response[:done] = true
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Writes `data` and ends the request; what the stream does not take now
|
|
259
|
+
# waits for Raiha::HTTP3::Event::Writable.
|
|
260
|
+
private def write(stream_id, data)
|
|
261
|
+
taken = @http3.send_data(stream_id, data, fin: true)
|
|
262
|
+
@unsent[stream_id] = data.byteslice(taken..) if taken < data.bytesize
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# The server rejected 0-RTT: the requests in it are gone, and HTTP/3
|
|
266
|
+
# starts over. Which to send again is the application's to decide.
|
|
267
|
+
private def restart
|
|
268
|
+
@quic.acknowledge_zero_rtt_rejection
|
|
269
|
+
@http3 = Raiha::HTTP3::Connection.client
|
|
270
|
+
@binding.restart(@http3)
|
|
271
|
+
@rejected = @responses.values.map { |response| response[:request] }
|
|
272
|
+
@responses.clear
|
|
273
|
+
@unsent.clear
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Used with `tls_config` as in the complete client, against a server that answers `/big` with a large body, as the `Handler` in [http3-server.md](http3-server.md#requests) does, and issues tickets and takes 0-RTT as in [its 0-RTT section](http3-server.md#settings-and-0-rtt). A server without ticket keys sends no ticket, and the wait for one below lasts until the idle timeout closes the connection.
|
|
279
|
+
|
|
280
|
+
```ruby
|
|
281
|
+
client = Client.new("127.0.0.1", 4433, server_name: "localhost", tls_config:)
|
|
282
|
+
client.run_until { client.ready? }
|
|
283
|
+
ids = [
|
|
284
|
+
client.request("GET", "/a"),
|
|
285
|
+
client.request("POST", "/upload", body: "y" * 300_000),
|
|
286
|
+
client.request("GET", "/big")
|
|
287
|
+
]
|
|
288
|
+
client.run_until { ids.all? { |stream_id| client.done?(stream_id) } }
|
|
289
|
+
client.run_until { !client.http3_tickets.empty? && !client.tokens.empty? }
|
|
290
|
+
ticket = client.http3_tickets.last
|
|
291
|
+
token = client.tokens.last
|
|
292
|
+
client.close
|
|
293
|
+
|
|
294
|
+
again = Client.new("127.0.0.1", 4433, server_name: "localhost", tls_config:, session: ticket, token:, early_data: true)
|
|
295
|
+
# At once when 0-RTT goes; otherwise once the handshake completes.
|
|
296
|
+
again.run_until { again.ready? }
|
|
297
|
+
early = again.request("GET", "/early")
|
|
298
|
+
again.run_until { again.responses[early]&.fetch(:done) || !again.rejected.empty? }
|
|
299
|
+
p again.quic.zero_rtt_state # => :accepted, or :rejected with the request in again.rejected
|
|
300
|
+
again.close
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
The first connection's three requests go at once and their responses interleave. The 0-RTT request of the second goes before its handshake completes; whether the server takes it depends on its ticket keys and replay record (see [http3-server.md](http3-server.md#settings-and-0-rtt)).
|