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,983 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "action"
|
|
4
|
+
require_relative "capsule"
|
|
5
|
+
require_relative "error"
|
|
6
|
+
require_relative "event"
|
|
7
|
+
require_relative "frame"
|
|
8
|
+
require_relative "frame_reader"
|
|
9
|
+
require_relative "message"
|
|
10
|
+
require_relative "output"
|
|
11
|
+
require_relative "priority"
|
|
12
|
+
require_relative "qpack"
|
|
13
|
+
require_relative "receiving"
|
|
14
|
+
require_relative "sending"
|
|
15
|
+
require_relative "settings"
|
|
16
|
+
require_relative "tracer"
|
|
17
|
+
require_relative "varint"
|
|
18
|
+
|
|
19
|
+
module Raiha::HTTP3
|
|
20
|
+
# One HTTP/3 connection (RFC 9114), sans-IO and over no transport in
|
|
21
|
+
# particular: it takes the bytes of streams and gives back what to write
|
|
22
|
+
# to them, what else to ask of the transport (Action), and what happened
|
|
23
|
+
# (Event). The stream IDs are the transport's: this layer is given the
|
|
24
|
+
# streams it is to use, never opens one.
|
|
25
|
+
#
|
|
26
|
+
# The bytes for a stream wait here until the transport takes them:
|
|
27
|
+
# `each_send` gives them, a stream at a time and the most urgent first,
|
|
28
|
+
# so that what is urgent finds room in the transport's send buffer, and
|
|
29
|
+
# a stream the transport takes less than given from waits until
|
|
30
|
+
# `stream_writable`.
|
|
31
|
+
class Connection
|
|
32
|
+
# The types of unidirectional streams (RFC 9114 "Unidirectional
|
|
33
|
+
# Streams", RFC 9204 "Encoder and Decoder Streams").
|
|
34
|
+
CONTROL_STREAM = 0x00
|
|
35
|
+
PUSH_STREAM = 0x01
|
|
36
|
+
ENCODER_STREAM = 0x02
|
|
37
|
+
DECODER_STREAM = 0x03
|
|
38
|
+
DEFAULT_MAX_SEND_BUFFER = 64 * 1024
|
|
39
|
+
# The control and QPACK streams: little goes on them, and nothing is to
|
|
40
|
+
# wait behind it.
|
|
41
|
+
CRITICAL = Priority.new(urgency: 0, incremental: true)
|
|
42
|
+
TRANSPORT_ORDERINGS = %i[per_stream global].freeze
|
|
43
|
+
# The types of the streams each end opens one of, and must never close.
|
|
44
|
+
CRITICAL_STREAMS = { CONTROL_STREAM => "control", ENCODER_STREAM => "QPACK encoder", DECODER_STREAM => "QPACK decoder" }.freeze
|
|
45
|
+
# The longest a frame on the control stream is held, its payload; a
|
|
46
|
+
# longer one is H3_EXCESSIVE_LOAD.
|
|
47
|
+
MAX_CONTROL_FRAME = 16 * 1024
|
|
48
|
+
# The frames that never go on a control stream (RFC 9114 "HTTP Framing
|
|
49
|
+
# Layer"), and those only a client sends on one.
|
|
50
|
+
NOT_ON_CONTROL = [Frame::DATA, Frame::HEADERS, Frame::PUSH_PROMISE, *Frame::HTTP2_TYPES].freeze
|
|
51
|
+
FROM_CLIENTS = [Frame::MAX_PUSH_ID, Frame::PRIORITY_UPDATE, Frame::PRIORITY_UPDATE_PUSH].freeze
|
|
52
|
+
# The most frames, and pieces of DATA, a request stream holds while its
|
|
53
|
+
# field section waits on QPACK; the bytes of DATA and field sections are
|
|
54
|
+
# held to the longest a field section may be. The transport's flow control does not
|
|
55
|
+
# bound them, as what is read here gives the peer credit.
|
|
56
|
+
MAX_WAITING = 1024
|
|
57
|
+
# How many requests not opened yet a server keeps a PRIORITY_UPDATE for
|
|
58
|
+
# (RFC 9218 "The PRIORITY_UPDATE Frame" leaves the bound to it).
|
|
59
|
+
MAX_PENDING_PRIORITIES = 128
|
|
60
|
+
# The HTTP Datagrams that wait for the transport at most, as QUIC's
|
|
61
|
+
# DATAGRAM frames do; the largest Quarter Stream ID (RFC 9297 "HTTP/3
|
|
62
|
+
# Datagrams").
|
|
63
|
+
MAX_DATAGRAMS = 32
|
|
64
|
+
MAX_QUARTER_STREAM_ID = (1 << 60) - 1
|
|
65
|
+
# The longest Capsule value held; a longer one is passed over unread.
|
|
66
|
+
MAX_CAPSULE = 64 * 1024
|
|
67
|
+
# The most events the application may leave untaken; past them the
|
|
68
|
+
# connection closes with H3_INTERNAL_ERROR, as QUIC's does.
|
|
69
|
+
MAX_EVENTS = 4096
|
|
70
|
+
# The frames this layer knows that never go on a request stream (RFC
|
|
71
|
+
# 9114 "HTTP Framing Layer"). PUSH_PROMISE goes on one only from a
|
|
72
|
+
# server.
|
|
73
|
+
NOT_ON_REQUEST = [
|
|
74
|
+
Frame::CANCEL_PUSH, Frame::SETTINGS, Frame::GOAWAY, Frame::MAX_PUSH_ID, Frame::PRIORITY_UPDATE, Frame::PRIORITY_UPDATE_PUSH,
|
|
75
|
+
*Frame::HTTP2_TYPES
|
|
76
|
+
].freeze
|
|
77
|
+
|
|
78
|
+
attr_reader :role, :settings
|
|
79
|
+
|
|
80
|
+
# A client's: `settings` are this end's to tell, `transport_ordering`
|
|
81
|
+
# whether the transport orders the bytes of different streams, :global,
|
|
82
|
+
# or not, :per_stream as QUIC does, and `max_send_buffer` the bytes of
|
|
83
|
+
# data a stream holds for the transport at most.
|
|
84
|
+
# A client's. `remembered` are the server's SETTINGS of an HTTP3::Session
|
|
85
|
+
# whose 0-RTT this layer is sent in: they stand for the server's until
|
|
86
|
+
# those come, which must then be compatible with them.
|
|
87
|
+
def self.client(settings: Settings.new, transport_ordering: :per_stream, max_send_buffer: DEFAULT_MAX_SEND_BUFFER, tracer: nil,
|
|
88
|
+
remembered: nil)
|
|
89
|
+
raise ArgumentError, "remembered must be Settings" unless remembered.nil? || remembered.is_a?(Settings)
|
|
90
|
+
new(role: :client, settings:, transport_ordering:, max_send_buffer:, tracer:, remembered:)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# A server's, which takes Extended CONNECT by default (RFC 9220).
|
|
94
|
+
def self.server(settings: Settings.new(enable_connect_protocol: true), transport_ordering: :per_stream, max_send_buffer: DEFAULT_MAX_SEND_BUFFER, tracer: nil)
|
|
95
|
+
new(role: :server, settings:, transport_ordering:, max_send_buffer:, tracer:)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def initialize(role:, settings:, transport_ordering:, max_send_buffer:, tracer: nil, remembered: nil)
|
|
99
|
+
limit = settings.max_field_section_size or raise ArgumentError, "a field section needs a limit, not nil"
|
|
100
|
+
raise ArgumentError, "transport_ordering is :per_stream or :global, not #{transport_ordering.inspect}" unless TRANSPORT_ORDERINGS.include?(transport_ordering)
|
|
101
|
+
raise ArgumentError, "max_send_buffer is a positive Integer, not #{max_send_buffer.inspect}" unless max_send_buffer.is_a?(Integer) && max_send_buffer.positive?
|
|
102
|
+
@role = role
|
|
103
|
+
@tracer = tracer
|
|
104
|
+
@settings = settings
|
|
105
|
+
@transport_ordering = transport_ordering
|
|
106
|
+
@max_send_buffer = max_send_buffer
|
|
107
|
+
@outputs = {} #: Hash[Integer, Output]
|
|
108
|
+
@priorities = {} #: Hash[Integer, Priority]
|
|
109
|
+
@actions = [] #: Array[Action::t]
|
|
110
|
+
@started = false
|
|
111
|
+
# This end's control and QPACK streams; the peer's going one way, by
|
|
112
|
+
# ID, with their type or the bytes of it so far; and the peer's
|
|
113
|
+
# control and QPACK streams, by type.
|
|
114
|
+
@own_critical = [] #: Array[Integer]
|
|
115
|
+
@own_qpack_streams = nil #: [Integer, Integer]?
|
|
116
|
+
@incoming = {} #: Hash[Integer, Integer | String]
|
|
117
|
+
@critical = {} #: Hash[Integer, Integer]
|
|
118
|
+
@closed = false
|
|
119
|
+
# The frames of the peer's control stream, and the settings they gave.
|
|
120
|
+
@control_reader = FrameReader.new(max_payload: MAX_CONTROL_FRAME)
|
|
121
|
+
@peer_settings = nil #: Settings?
|
|
122
|
+
@remembered = remembered
|
|
123
|
+
@events = [] #: Array[Event::t]
|
|
124
|
+
# QPACK: this end's decoder takes the peer's encoder stream and field
|
|
125
|
+
# sections, by this end's settings; its encoder writes field sections
|
|
126
|
+
# and takes the peer's decoder stream.
|
|
127
|
+
@decoder = QPACK::Decoder.new(
|
|
128
|
+
max_table_capacity: settings.qpack_max_table_capacity, max_blocked_streams: settings.qpack_blocked_streams,
|
|
129
|
+
max_field_section_size: limit
|
|
130
|
+
)
|
|
131
|
+
@encoder = QPACK::Encoder.new
|
|
132
|
+
# The request streams being received, and the longest field section a
|
|
133
|
+
# HEADERS frame may carry: one line takes no more on the wire than
|
|
134
|
+
# four times what it counts for (RFC 9204's decoder holds no longer).
|
|
135
|
+
@receiving = {} #: Hash[Integer, Receiving]
|
|
136
|
+
@sending = {} #: Hash[Integer, Sending]
|
|
137
|
+
# The lowest ID of the peer's request streams not seen yet, and those
|
|
138
|
+
# below it skipped, which may still come.
|
|
139
|
+
@next_request = 0
|
|
140
|
+
@skipped = {} #: Hash[Integer, true]
|
|
141
|
+
# The GOAWAY this end sent, the peer's, and the most push IDs a client
|
|
142
|
+
# allowed.
|
|
143
|
+
@goaway = nil #: Integer?
|
|
144
|
+
@peer_goaway = nil #: Integer?
|
|
145
|
+
@max_push_id = nil #: Integer?
|
|
146
|
+
# The PRIORITY_UPDATEs for requests not opened yet.
|
|
147
|
+
@pending_priorities = {} #: Hash[Integer, Priority]
|
|
148
|
+
# HTTP Datagrams: where to ask the most the transport carries in one,
|
|
149
|
+
# and those waiting to go with their urgency.
|
|
150
|
+
@datagram_max_size = nil #: ^() -> Integer? | nil
|
|
151
|
+
@datagrams = [] #: Array[[String, Integer]]
|
|
152
|
+
@field_section_limit = limit
|
|
153
|
+
@max_section = QPACK::Decoder::EXPANSION_BOUND * limit
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# The peer's settings; nil until its SETTINGS frame has come.
|
|
157
|
+
attr_reader :peer_settings
|
|
158
|
+
|
|
159
|
+
# Whether this layer has closed the connection, after which it takes
|
|
160
|
+
# nothing more.
|
|
161
|
+
def closed? = @closed
|
|
162
|
+
|
|
163
|
+
# The transport opened `control`, `encoder` and `decoder`, streams of
|
|
164
|
+
# this end going one way, for the control stream and the QPACK
|
|
165
|
+
# streams: their types go on them, and this end's SETTINGS.
|
|
166
|
+
def start(control:, encoder:, decoder:)
|
|
167
|
+
raise Error::Usage, "the connection has its streams already" if @started
|
|
168
|
+
streams = [control, encoder, decoder]
|
|
169
|
+
streams.each do |stream_id|
|
|
170
|
+
raise ArgumentError, "stream #{stream_id.inspect} is not one of this end going one way" unless local_unidirectional?(stream_id)
|
|
171
|
+
end
|
|
172
|
+
raise ArgumentError, "three streams, not one twice" unless streams.uniq.size == 3
|
|
173
|
+
@started = true
|
|
174
|
+
@own_critical = streams
|
|
175
|
+
@own_qpack_streams = [encoder, decoder]
|
|
176
|
+
settings = @settings.to_frame
|
|
177
|
+
output(control) << Varint.encode(CONTROL_STREAM) << Frame.encode(settings)
|
|
178
|
+
output(encoder) << Varint.encode(ENCODER_STREAM)
|
|
179
|
+
output(decoder) << Varint.encode(DECODER_STREAM)
|
|
180
|
+
streams.each { |stream_id| prioritize(stream_id, CRITICAL) }
|
|
181
|
+
if (tracer = @tracer)
|
|
182
|
+
tracer.parameters_set(initiator: :local, settings: @settings)
|
|
183
|
+
{ control => CONTROL_STREAM, encoder => ENCODER_STREAM, decoder => DECODER_STREAM }.each do |stream_id, type|
|
|
184
|
+
tracer.stream_type_set(initiator: :local, stream_id:, type:)
|
|
185
|
+
end
|
|
186
|
+
tracer.frame_created(stream_id: control, type: Frame::SETTINGS, frame: settings)
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Gives the bytes waiting for each stream, the most urgent stream first:
|
|
191
|
+
# the block writes `data` to `stream_id`, ending it with `fin`, and
|
|
192
|
+
# returns how many bytes it took. What it does not take waits, and so
|
|
193
|
+
# does the stream, until `stream_writable`.
|
|
194
|
+
def each_send(&)
|
|
195
|
+
return if @closed
|
|
196
|
+
take_qpack_instructions
|
|
197
|
+
ready = @outputs.select { |_, output| output.ready? }.keys
|
|
198
|
+
ready.sort_by! { |stream_id| [priority_of(stream_id).urgency, stream_id] }
|
|
199
|
+
ready.each do |stream_id|
|
|
200
|
+
output = @outputs.fetch(stream_id)
|
|
201
|
+
ended = output.drain { |data, fin| yield(stream_id, data, fin) }
|
|
202
|
+
forget_output(stream_id) if ended
|
|
203
|
+
writable_again(stream_id, output)
|
|
204
|
+
end
|
|
205
|
+
events_bounded
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Sends a request on `stream_id`, a stream going both ways the transport
|
|
209
|
+
# opened for it, ending the stream with `fin`; its content follows with
|
|
210
|
+
# `send_data`, and its response comes as events. One that would be
|
|
211
|
+
# malformed (RFC 9114 "Malformed Requests and Responses") is an
|
|
212
|
+
# ArgumentError; :protocol is for a server that took Extended CONNECT.
|
|
213
|
+
def send_request(stream_id, headers, fin: false)
|
|
214
|
+
raise Error::Usage, "a server sends no request" unless @role == :client
|
|
215
|
+
raise Error::Usage, "the server goes away; no more requests" if @peer_goaway
|
|
216
|
+
unless stream_id.is_a?(Integer) && stream_id.between?(0, Varint::MAX) && stream_id & 0x3 == 0
|
|
217
|
+
raise ArgumentError, "stream #{stream_id.inspect} is not a client's going both ways"
|
|
218
|
+
end
|
|
219
|
+
raise Error::Usage, "stream #{stream_id} has a request already" if @receiving.key?(stream_id) || @sending.key?(stream_id)
|
|
220
|
+
checked { Message.request(headers, extended_connect: peer_limits&.enable_connect_protocol || false) }
|
|
221
|
+
receiving = @receiving[stream_id] = Receiving.new(max_payload: @max_section)
|
|
222
|
+
receiving.surfaced = true
|
|
223
|
+
receiving.no_content = headers.include?([":method", "HEAD"])
|
|
224
|
+
sending = @sending[stream_id] = Sending.new
|
|
225
|
+
sending.phase = :body
|
|
226
|
+
sending.datagrams = receiving.datagrams = headers.any? { |name, _| name == ":protocol" }
|
|
227
|
+
sending.capsules = receiving.capsules_asked = sending.datagrams && checked { Message.capsule_protocol?(headers) }
|
|
228
|
+
@tracer&.stream_type_set(initiator: :local, stream_id:, type: :request)
|
|
229
|
+
prioritize(stream_id, priority_in(headers))
|
|
230
|
+
write_headers(stream_id, headers)
|
|
231
|
+
finish_sending(stream_id) if fin
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Sends a field section on `stream_id`, ending the stream with `fin`:
|
|
235
|
+
# a response, or an interim response before it, on a request; and once
|
|
236
|
+
# the content has begun, the trailer section, which always ends it.
|
|
237
|
+
# One that would be malformed (RFC 9114 "Malformed Requests and
|
|
238
|
+
# Responses") is an ArgumentError.
|
|
239
|
+
def send_headers(stream_id, headers, fin: false)
|
|
240
|
+
sending = sending_for(stream_id)
|
|
241
|
+
if sending.phase == :headers
|
|
242
|
+
status = checked { Message.response(headers) }.first
|
|
243
|
+
interim = status < 200
|
|
244
|
+
raise ArgumentError, "an interim response does not end the stream" if interim && fin
|
|
245
|
+
sending.phase = :body unless interim
|
|
246
|
+
merge_priority(stream_id, headers) unless interim
|
|
247
|
+
else
|
|
248
|
+
checked { Message.trailers(headers) }
|
|
249
|
+
fin = true
|
|
250
|
+
end
|
|
251
|
+
write_headers(stream_id, headers)
|
|
252
|
+
finish_sending(stream_id) if fin
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Sends `data` as content on `stream_id`, as much of it as the stream
|
|
256
|
+
# holds room for, max_send_buffer bytes waiting at most, and ends the
|
|
257
|
+
# stream with `fin` once all of it is taken: how many bytes were taken.
|
|
258
|
+
# Less than all of it, and Event::Writable tells when the stream takes
|
|
259
|
+
# more.
|
|
260
|
+
def send_data(stream_id, data, fin: false)
|
|
261
|
+
sending = sending_for(stream_id)
|
|
262
|
+
raise Error::Usage, "no content before the header section on stream #{stream_id}" unless sending.phase == :body
|
|
263
|
+
output = output(stream_id)
|
|
264
|
+
taken = [data.bytesize, [@max_send_buffer - output.buffered, 0].max].min
|
|
265
|
+
if taken.positive?
|
|
266
|
+
output << Frame.header(Frame::DATA, taken) << data.byteslice(0, taken).b # steep:ignore
|
|
267
|
+
@tracer&.frame_created(stream_id:, type: Frame::DATA, length: taken)
|
|
268
|
+
end
|
|
269
|
+
if taken < data.bytesize
|
|
270
|
+
sending.wants_writable = true
|
|
271
|
+
elsif fin
|
|
272
|
+
finish_sending(stream_id)
|
|
273
|
+
end
|
|
274
|
+
taken
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# What gives the most a DATAGRAM frame of the transport carries at the
|
|
278
|
+
# time, as that changes with the path, or nil while it carries none,
|
|
279
|
+
# which may be for good; asked each time a datagram is to go.
|
|
280
|
+
attr_writer :datagram_max_size
|
|
281
|
+
|
|
282
|
+
# The transport received a DATAGRAM, `data`, an HTTP Datagram (RFC 9297
|
|
283
|
+
# "HTTP/3 Datagrams"): a Quarter Stream ID, then the payload. One for a
|
|
284
|
+
# request not opened yet, or whose sending side has ended, is dropped,
|
|
285
|
+
# as are all when this end did not take them; one for a request that
|
|
286
|
+
# has no use for them stops it with H3_DATAGRAM_ERROR.
|
|
287
|
+
def datagram_received(data)
|
|
288
|
+
return if @closed || !@settings.h3_datagram
|
|
289
|
+
quarter, offset = Varint.decode(data)
|
|
290
|
+
raise Violation.new("a datagram with no Quarter Stream ID", code: ErrorCode::DATAGRAM_ERROR) unless quarter && offset
|
|
291
|
+
raise Violation.new("the Quarter Stream ID #{quarter}", code: ErrorCode::DATAGRAM_ERROR) if quarter > MAX_QUARTER_STREAM_ID
|
|
292
|
+
stream_id = quarter * 4
|
|
293
|
+
receiving = @receiving[stream_id] or return
|
|
294
|
+
return unless receiving.surfaced
|
|
295
|
+
return stream_error(stream_id, ErrorCode::DATAGRAM_ERROR) unless receiving.datagrams
|
|
296
|
+
@events << Event::Datagram.new(stream_id:, payload: data.byteslice(offset..).freeze) # steep:ignore
|
|
297
|
+
events_bounded
|
|
298
|
+
rescue Violation => violation
|
|
299
|
+
close_with(violation.code, violation.message)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Sends `payload` as an HTTP Datagram for the request on `stream_id`:
|
|
303
|
+
# whether it was taken. Only when both ends took them (RFC 9297 "The
|
|
304
|
+
# SETTINGS_H3_DATAGRAM HTTP/3 Setting"), on a request that has a use for
|
|
305
|
+
# them, Extended CONNECT, while this end still sends on it, and when it
|
|
306
|
+
# fits in the transport's DATAGRAM frame; the stream's urgency goes
|
|
307
|
+
# with it for the transport to order it by.
|
|
308
|
+
def datagram_send(stream_id, payload)
|
|
309
|
+
return false if @closed || !@settings.h3_datagram || !peer_limits&.h3_datagram
|
|
310
|
+
return false unless (max_size = @datagram_max_size&.call) && @sending[stream_id]&.datagrams && @datagrams.size < MAX_DATAGRAMS
|
|
311
|
+
data = Varint.encode(stream_id / 4) << payload.b
|
|
312
|
+
return false if data.bytesize > max_size
|
|
313
|
+
@datagrams << [data, priority_of(stream_id).urgency]
|
|
314
|
+
true
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Sends `capsule` whole on the data stream of the request on
|
|
318
|
+
# `stream_id`, which must carry Capsules: whether there was room for
|
|
319
|
+
# it. If not, Event::Writable tells when the stream takes more.
|
|
320
|
+
def send_capsule(stream_id, capsule)
|
|
321
|
+
sending = sending_for(stream_id)
|
|
322
|
+
raise Error::Usage, "stream #{stream_id} carries no Capsules" unless sending.capsules && sending.phase == :body
|
|
323
|
+
bytes = Capsule.encode(capsule)
|
|
324
|
+
output = output(stream_id)
|
|
325
|
+
if output.buffered + bytes.bytesize > @max_send_buffer
|
|
326
|
+
sending.wants_writable = true
|
|
327
|
+
return false
|
|
328
|
+
end
|
|
329
|
+
output << Frame.header(Frame::DATA, bytes.bytesize) << bytes
|
|
330
|
+
@tracer&.frame_created(stream_id:, type: Frame::DATA, length: bytes.bytesize)
|
|
331
|
+
true
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Gives the HTTP Datagrams waiting, each with its urgency, to go in the
|
|
335
|
+
# transport's DATAGRAM frames.
|
|
336
|
+
def each_datagram_send
|
|
337
|
+
return @datagrams.clear if @closed
|
|
338
|
+
until @datagrams.empty?
|
|
339
|
+
data, urgency = @datagrams.shift
|
|
340
|
+
yield data, urgency
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# The transport can take more of `stream_id`.
|
|
345
|
+
def stream_writable(stream_id)
|
|
346
|
+
@outputs[stream_id]&.unblock
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# The transport received `data` on `stream_id`, and the end of the
|
|
350
|
+
# stream with `fin`.
|
|
351
|
+
def stream_data(stream_id, data, fin: false)
|
|
352
|
+
raise ArgumentError, "stream #{stream_id} is this end's and goes one way" if local_unidirectional?(stream_id)
|
|
353
|
+
return if @closed
|
|
354
|
+
if stream_id & 0x2 != 0
|
|
355
|
+
unidirectional_data(stream_id, data, fin)
|
|
356
|
+
else
|
|
357
|
+
bidirectional_data(stream_id, data, fin)
|
|
358
|
+
end
|
|
359
|
+
events_bounded
|
|
360
|
+
rescue Violation => violation
|
|
361
|
+
close_with(violation.code, violation.message)
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# The peer reset `stream_id`, with `error_code`: on a request stream,
|
|
365
|
+
# what it sends ends there, and the application hears so with
|
|
366
|
+
# Event::Reset if it knows of the request; what this end sends on it
|
|
367
|
+
# may still go.
|
|
368
|
+
def stream_reset(stream_id, error_code:)
|
|
369
|
+
return if @closed
|
|
370
|
+
if stream_id & 0x2 == 0
|
|
371
|
+
@skipped.delete(stream_id)
|
|
372
|
+
receiving = @receiving.delete(stream_id) or return
|
|
373
|
+
@decoder.cancel(stream_id)
|
|
374
|
+
@events << Event::Reset.new(stream_id:, error_code:) if receiving.surfaced
|
|
375
|
+
return events_bounded
|
|
376
|
+
end
|
|
377
|
+
type = @incoming.delete(stream_id)
|
|
378
|
+
return unless type.is_a?(Integer) && CRITICAL_STREAMS.key?(type)
|
|
379
|
+
close_with(ErrorCode::CLOSED_CRITICAL_STREAM, "the peer reset its #{CRITICAL_STREAMS.fetch(type)} stream")
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# The peer asked this end to stop sending on `stream_id`, with
|
|
383
|
+
# `error_code`, and the transport reset it: on a request stream,
|
|
384
|
+
# nothing more goes, and the application, if it was writing, hears so
|
|
385
|
+
# with Event::Stopped; what the peer sends on it may still come.
|
|
386
|
+
def stream_stopped(stream_id, error_code:)
|
|
387
|
+
return if @closed
|
|
388
|
+
if @own_critical.include?(stream_id)
|
|
389
|
+
return close_with(ErrorCode::CLOSED_CRITICAL_STREAM, "the peer stopped this end's stream #{stream_id}")
|
|
390
|
+
end
|
|
391
|
+
forget_output(stream_id)
|
|
392
|
+
@events << Event::Stopped.new(stream_id:, error_code:) if @sending.delete(stream_id)
|
|
393
|
+
events_bounded
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
# Gives the request on `stream_id` `urgency` and `incremental` (RFC
|
|
397
|
+
# 9218), each left as it is when not given ("Merging Client- and
|
|
398
|
+
# Server-Driven Priority Parameters"): a client tells the server with
|
|
399
|
+
# PRIORITY_UPDATE, and both send by it what they send on the stream.
|
|
400
|
+
def update_priority(stream_id, urgency: nil, incremental: nil)
|
|
401
|
+
raise Error::Usage, "stream #{stream_id} has no request" unless @receiving.key?(stream_id) || @sending.key?(stream_id) || @outputs.key?(stream_id)
|
|
402
|
+
priority = priority_of(stream_id).merge(urgency, incremental)
|
|
403
|
+
if @role == :client
|
|
404
|
+
control = @own_critical.first or raise Error::Usage, "no control stream to send PRIORITY_UPDATE on yet"
|
|
405
|
+
frame = Frame::PriorityUpdate.new(element_id: stream_id, priority: priority.to_field)
|
|
406
|
+
output(control) << Frame.encode(frame)
|
|
407
|
+
@tracer&.frame_created(stream_id: control, type: Frame::PRIORITY_UPDATE, frame:)
|
|
408
|
+
end
|
|
409
|
+
prioritize(stream_id, priority)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# Tells the peer this end goes away (RFC 9114 "Connection Shutdown"): a
|
|
413
|
+
# server's GOAWAY gives the lowest ID of a request it has not seen, by
|
|
414
|
+
# default, from which it rejects requests with H3_REQUEST_REJECTED; a
|
|
415
|
+
# client's gives a push ID, 0 by default, as it allows no push. A later
|
|
416
|
+
# GOAWAY may only give a lower one.
|
|
417
|
+
def goaway(id = nil)
|
|
418
|
+
raise Error::Usage, "no control stream to send GOAWAY on yet" unless (control = @own_critical.first)
|
|
419
|
+
id ||= @role == :server ? @next_request : 0
|
|
420
|
+
raise ArgumentError, "GOAWAY gives 0..2^62-1, not #{id.inspect}" unless id.is_a?(Integer) && id.between?(0, Varint::MAX)
|
|
421
|
+
raise ArgumentError, "a server's GOAWAY gives a request's stream ID, not #{id}" if @role == :server && id & 0x3 != 0
|
|
422
|
+
raise ArgumentError, "GOAWAY gave #{@goaway} already, not more" if (sent = @goaway) && id > sent
|
|
423
|
+
return if @closed
|
|
424
|
+
@goaway = id
|
|
425
|
+
frame = Frame::GoAway.new(id:)
|
|
426
|
+
output(control) << Frame.encode(frame)
|
|
427
|
+
@tracer&.frame_created(stream_id: control, type: Frame::GOAWAY, frame:)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Stops a request on `stream_id` both ways, with `error_code`
|
|
431
|
+
# (RFC 9114 "Request Cancellation and Rejection"): nothing more of it
|
|
432
|
+
# is sent or taken. A stream with no request, or one over, is left as
|
|
433
|
+
# it is.
|
|
434
|
+
def cancel(stream_id, error_code: ErrorCode::REQUEST_CANCELLED)
|
|
435
|
+
raise ArgumentError, "an error code is 0..2^62-1, not #{error_code.inspect}" unless error_code.is_a?(Integer) && error_code.between?(0, Varint::MAX)
|
|
436
|
+
raise ArgumentError, "stream #{stream_id} is not a request stream" unless stream_id.is_a?(Integer) && stream_id & 0x2 == 0
|
|
437
|
+
return if @closed
|
|
438
|
+
return unless @receiving.key?(stream_id) || @sending.key?(stream_id) || @outputs.key?(stream_id)
|
|
439
|
+
@receiving.delete(stream_id)
|
|
440
|
+
@sending.delete(stream_id)
|
|
441
|
+
forget_output(stream_id)
|
|
442
|
+
@decoder.cancel(stream_id)
|
|
443
|
+
@actions << Action::StopSending.new(stream_id:, error_code:) << Action::Reset.new(stream_id:, error_code:)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Takes no more of the request on `stream_id`, which this server
|
|
447
|
+
# answers without the rest of it: the client is asked to stop sending
|
|
448
|
+
# with H3_NO_ERROR (RFC 9114 "HTTP Message Exchanges"), what of it
|
|
449
|
+
# still comes is dropped, and the response goes on. A stream whose
|
|
450
|
+
# request has ended, or has none, is left as it is.
|
|
451
|
+
def stop_reading(stream_id)
|
|
452
|
+
raise Error::Usage, "a client stops reading a response by cancelling its request" unless @role == :server
|
|
453
|
+
raise ArgumentError, "stream #{stream_id} is not a request stream" unless stream_id.is_a?(Integer) && stream_id & 0x2 == 0
|
|
454
|
+
return if @closed
|
|
455
|
+
@receiving.delete(stream_id) or return
|
|
456
|
+
@decoder.cancel(stream_id)
|
|
457
|
+
@actions << Action::StopSending.new(stream_id:, error_code: ErrorCode::NO_ERROR)
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# Gives what this layer asks of the transport, Action values, in the
|
|
461
|
+
# order asked.
|
|
462
|
+
def each_action
|
|
463
|
+
yield @actions.shift until @actions.empty?
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# Gives what happened, Event values, in the order it happened.
|
|
467
|
+
def each_event
|
|
468
|
+
yield @events.shift until @events.empty?
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# Bytes on a stream of the peer going one way: first its type (RFC 9114
|
|
472
|
+
# "Unidirectional Streams"), then what the type says. A stream may end
|
|
473
|
+
# before its type comes.
|
|
474
|
+
private def unidirectional_data(stream_id, data, fin)
|
|
475
|
+
type = @incoming[stream_id]
|
|
476
|
+
unless type.is_a?(Integer)
|
|
477
|
+
bytes = type ? type + data : data
|
|
478
|
+
type, offset = Varint.decode(bytes)
|
|
479
|
+
unless type && offset
|
|
480
|
+
fin ? @incoming.delete(stream_id) : @incoming[stream_id] = bytes
|
|
481
|
+
return
|
|
482
|
+
end
|
|
483
|
+
open_unidirectional(stream_id, type)
|
|
484
|
+
@tracer&.stream_type_set(initiator: :remote, stream_id:, type:)
|
|
485
|
+
@incoming[stream_id] = type
|
|
486
|
+
data = bytes.byteslice(offset..) #: String
|
|
487
|
+
end
|
|
488
|
+
unless CRITICAL_STREAMS.key?(type)
|
|
489
|
+
@incoming.delete(stream_id) if fin
|
|
490
|
+
return
|
|
491
|
+
end
|
|
492
|
+
case type
|
|
493
|
+
when CONTROL_STREAM then @control_reader.feed(data) { |item| control_item(item) }
|
|
494
|
+
when ENCODER_STREAM
|
|
495
|
+
results = @decoder.receive_encoder_stream(data)
|
|
496
|
+
qpack_failed(results)
|
|
497
|
+
results.each { |result| unblocked(result) } if results.is_a?(Array)
|
|
498
|
+
when DECODER_STREAM then qpack_failed(@encoder.receive_decoder_stream(data))
|
|
499
|
+
end
|
|
500
|
+
raise Violation.new("the peer closed its #{CRITICAL_STREAMS.fetch(type)} stream", code: ErrorCode::CLOSED_CRITICAL_STREAM) if fin
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
# What the peer's control stream holds: its SETTINGS first and once,
|
|
504
|
+
# and only the frames a control stream carries (RFC 9114 "Control
|
|
505
|
+
# Streams", "HTTP Framing Layer").
|
|
506
|
+
# The peer's SETTINGS, or until they come, those remembered for 0-RTT.
|
|
507
|
+
private def peer_limits = @peer_settings || @remembered
|
|
508
|
+
|
|
509
|
+
# The server's SETTINGS after 0-RTT sent by remembered ones: a QPACK
|
|
510
|
+
# capacity remembered, not 0, must come again as it was, which the
|
|
511
|
+
# encoder takes as QPACK_DECODER_STREAM_ERROR otherwise (RFC 9204
|
|
512
|
+
# "Maximum Dynamic Table Capacity"), and H3_DATAGRAM remembered must stay
|
|
513
|
+
# (RFC 9297 "The SETTINGS_H3_DATAGRAM HTTP/3 Setting").
|
|
514
|
+
private def check_remembered(settings, remembered)
|
|
515
|
+
capacity = remembered.qpack_max_table_capacity
|
|
516
|
+
if !capacity.zero? && settings.qpack_max_table_capacity != capacity
|
|
517
|
+
raise Violation.new("a QPACK capacity of #{settings.qpack_max_table_capacity} where 0-RTT remembered #{capacity}",
|
|
518
|
+
code: QPACK::ErrorCode::DECODER_STREAM_ERROR)
|
|
519
|
+
end
|
|
520
|
+
return unless remembered.h3_datagram && !settings.h3_datagram
|
|
521
|
+
raise Violation.new("no H3_DATAGRAM where 0-RTT remembered it", code: ErrorCode::SETTINGS_ERROR)
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
private def control_item(item)
|
|
525
|
+
trace_parsed(@critical.fetch(CONTROL_STREAM), item)
|
|
526
|
+
case item
|
|
527
|
+
when FrameReader::Started
|
|
528
|
+
type = item.type
|
|
529
|
+
if @peer_settings.nil? && type != Frame::SETTINGS
|
|
530
|
+
raise Violation.new("a frame of type 0x#{type.to_s(16)} before SETTINGS", code: ErrorCode::MISSING_SETTINGS)
|
|
531
|
+
end
|
|
532
|
+
if (type == Frame::SETTINGS && @peer_settings) || NOT_ON_CONTROL.include?(type) || (@role == :client && FROM_CLIENTS.include?(type))
|
|
533
|
+
raise Violation.new("a frame of type 0x#{type.to_s(16)} on the control stream", code: ErrorCode::FRAME_UNEXPECTED)
|
|
534
|
+
end
|
|
535
|
+
when FrameReader::TooLong
|
|
536
|
+
raise Violation.new("a frame of #{item.length} bytes on the control stream", code: ErrorCode::EXCESSIVE_LOAD)
|
|
537
|
+
when Frame::Settings
|
|
538
|
+
settings = Settings.from_frame(item)
|
|
539
|
+
@tracer&.parameters_set(initiator: :remote, settings:)
|
|
540
|
+
remembered = @remembered
|
|
541
|
+
check_remembered(settings, remembered) if remembered
|
|
542
|
+
@remembered = nil
|
|
543
|
+
@peer_settings = settings
|
|
544
|
+
@events << Event::Settings.new(settings:)
|
|
545
|
+
when Frame::GoAway then goaway_received(item.id)
|
|
546
|
+
when Frame::CancelPush
|
|
547
|
+
raise Violation.new("CANCEL_PUSH for push #{item.push_id}, with no push allowed or promised", code: ErrorCode::ID_ERROR)
|
|
548
|
+
when Frame::PriorityUpdate then priority_update_received(item)
|
|
549
|
+
when Frame::MaxPushId
|
|
550
|
+
if (allowed = @max_push_id) && item.push_id < allowed
|
|
551
|
+
raise Violation.new("MAX_PUSH_ID down from #{allowed} to #{item.push_id}", code: ErrorCode::ID_ERROR)
|
|
552
|
+
end
|
|
553
|
+
@max_push_id = item.push_id
|
|
554
|
+
end
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
# A client's PRIORITY_UPDATE (RFC 9218): for a request, the latest
|
|
558
|
+
# signal, which a request not opened yet takes when it opens. A value
|
|
559
|
+
# that is not a Dictionary drops the frame, as a connection error over
|
|
560
|
+
# it is only a MAY.
|
|
561
|
+
private def priority_update_received(frame)
|
|
562
|
+
stream_id = frame.element_id
|
|
563
|
+
raise Violation.new("PRIORITY_UPDATE for push #{stream_id}, never promised", code: ErrorCode::ID_ERROR) if frame.push
|
|
564
|
+
raise Violation.new("PRIORITY_UPDATE for stream #{stream_id}, not a request's", code: ErrorCode::ID_ERROR) if stream_id & 0x3 != 0
|
|
565
|
+
urgency, incremental = Priority.parameters(frame.priority) || return
|
|
566
|
+
priority = Priority.new(urgency: urgency || Priority::DEFAULT_URGENCY, incremental: incremental || false)
|
|
567
|
+
if @receiving.key?(stream_id) || @sending.key?(stream_id)
|
|
568
|
+
prioritize(stream_id, priority)
|
|
569
|
+
elsif stream_id >= @next_request || @skipped.key?(stream_id)
|
|
570
|
+
return unless @pending_priorities.key?(stream_id) || @pending_priorities.size < MAX_PENDING_PRIORITIES
|
|
571
|
+
@pending_priorities[stream_id] = priority
|
|
572
|
+
end
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
# The peer goes away (RFC 9114 "Connection Shutdown"): a server's `id`,
|
|
576
|
+
# a stream ID of a client's request, says the requests from it on were
|
|
577
|
+
# not processed, and a client's, a push ID, the pushes; neither may
|
|
578
|
+
# grow. A client makes no more requests, and those it made from `id`
|
|
579
|
+
# on are let go, each told with Event::Reset as H3_REQUEST_REJECTED.
|
|
580
|
+
private def goaway_received(id)
|
|
581
|
+
if @role == :client && id & 0x3 != 0
|
|
582
|
+
raise Violation.new("GOAWAY for stream #{id}, not a request's", code: ErrorCode::ID_ERROR)
|
|
583
|
+
end
|
|
584
|
+
previous = @peer_goaway
|
|
585
|
+
raise Violation.new("GOAWAY up from #{previous} to #{id}", code: ErrorCode::ID_ERROR) if previous && id > previous
|
|
586
|
+
@peer_goaway = id
|
|
587
|
+
@events << Event::GoAway.new(stream_id: id)
|
|
588
|
+
return unless @role == :client
|
|
589
|
+
(@receiving.keys | @sending.keys).select { |stream_id| stream_id >= id }.sort.each do |stream_id|
|
|
590
|
+
cancel(stream_id)
|
|
591
|
+
@events << Event::Reset.new(stream_id:, error_code: ErrorCode::REQUEST_REJECTED)
|
|
592
|
+
end
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# The peer opened `stream_id`, of `type`: one each of the control and
|
|
596
|
+
# QPACK streams, no push stream, as this end allows no push, and a type
|
|
597
|
+
# not known passed over (RFC 9114 "Unidirectional Streams").
|
|
598
|
+
private def open_unidirectional(stream_id, type)
|
|
599
|
+
if CRITICAL_STREAMS.key?(type)
|
|
600
|
+
raise Violation.new("a second #{CRITICAL_STREAMS.fetch(type)} stream", code: ErrorCode::STREAM_CREATION_ERROR) if @critical.key?(type)
|
|
601
|
+
@critical[type] = stream_id
|
|
602
|
+
elsif type == PUSH_STREAM
|
|
603
|
+
raise Violation.new("a push stream from a client", code: ErrorCode::STREAM_CREATION_ERROR) if @role == :server
|
|
604
|
+
raise Violation.new("a push stream, with no push allowed", code: ErrorCode::ID_ERROR)
|
|
605
|
+
else
|
|
606
|
+
@actions << Action::StopSending.new(stream_id:, error_code: ErrorCode::STREAM_CREATION_ERROR)
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# Bytes on a stream going both ways: a server's are requests; a client
|
|
611
|
+
# takes none the server opens (RFC 9114 "Bidirectional Streams").
|
|
612
|
+
private def bidirectional_data(stream_id, data, fin)
|
|
613
|
+
if @role == :client
|
|
614
|
+
raise Violation.new("a stream going both ways from a server", code: ErrorCode::STREAM_CREATION_ERROR) if stream_id & 0x1 != 0
|
|
615
|
+
receiving = @receiving[stream_id] or return
|
|
616
|
+
else
|
|
617
|
+
receiving = @receiving[stream_id] || open_request(stream_id) or return
|
|
618
|
+
end
|
|
619
|
+
receiving.reader.feed(data) { |item| request_item(stream_id, receiving, item) }
|
|
620
|
+
request_ended(stream_id, receiving) if fin && @receiving[stream_id].equal?(receiving)
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
# A request stream the peer opened: one past those seen so far, or one
|
|
624
|
+
# below them it skipped, as the IDs of a peer's streams come in order
|
|
625
|
+
# but their bytes need not; nil for one that ended or was stopped, whose
|
|
626
|
+
# late bytes are dropped. How many are skipped is bounded by the
|
|
627
|
+
# transport's limit on the peer's streams.
|
|
628
|
+
private def open_request(stream_id)
|
|
629
|
+
if stream_id >= @next_request
|
|
630
|
+
skipped = @next_request
|
|
631
|
+
while skipped < stream_id
|
|
632
|
+
@skipped[skipped] = true
|
|
633
|
+
skipped += 4
|
|
634
|
+
end
|
|
635
|
+
@next_request = stream_id + 4
|
|
636
|
+
else
|
|
637
|
+
@skipped.delete(stream_id) or return nil
|
|
638
|
+
end
|
|
639
|
+
if (goaway = @goaway) && stream_id >= goaway
|
|
640
|
+
stream_error(stream_id, ErrorCode::REQUEST_REJECTED)
|
|
641
|
+
return nil
|
|
642
|
+
end
|
|
643
|
+
@tracer&.stream_type_set(initiator: :remote, stream_id:, type: :request)
|
|
644
|
+
@receiving[stream_id] = Receiving.new(max_payload: @max_section)
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
# What a request stream holds (RFC 9114 "HTTP Message Framing"): a
|
|
648
|
+
# header section, DATA, and a trailer section at most, and none of the
|
|
649
|
+
# frames that go elsewhere. While a field section waits on QPACK, what
|
|
650
|
+
# follows it waits too; a frame the stream does not carry is refused
|
|
651
|
+
# all the same.
|
|
652
|
+
private def request_item(stream_id, receiving, item)
|
|
653
|
+
return unless @receiving[stream_id].equal?(receiving)
|
|
654
|
+
if item.is_a?(FrameReader::Started)
|
|
655
|
+
check_request_frame(receiving, item.type)
|
|
656
|
+
trace_parsed(stream_id, item) unless item.type == Frame::HEADERS
|
|
657
|
+
end
|
|
658
|
+
return wait(stream_id, receiving, item) if receiving.blocked
|
|
659
|
+
case item
|
|
660
|
+
when FrameReader::TooLong
|
|
661
|
+
@decoder.cancel(stream_id)
|
|
662
|
+
receiving.surfaced ? stream_error(stream_id, ErrorCode::EXCESSIVE_LOAD) : too_large(stream_id)
|
|
663
|
+
when Frame::Headers
|
|
664
|
+
limit = receiving.phase == :body ? receiving.budget : @field_section_limit
|
|
665
|
+
section_decoded(stream_id, receiving, @decoder.decode(stream_id, item.field_section, limit:))
|
|
666
|
+
when Frame::Data
|
|
667
|
+
request_data(stream_id, receiving, item.data)
|
|
668
|
+
end
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
# A frame of `type` that begins on a request stream: H3_FRAME_UNEXPECTED
|
|
672
|
+
# for one that goes elsewhere, DATA before the header section, and
|
|
673
|
+
# anything but frames not known after the trailer section; these last
|
|
674
|
+
# two are checked once the frames before have been decoded, if one
|
|
675
|
+
# waits on QPACK. A PUSH_PROMISE goes only to a client, which allows
|
|
676
|
+
# no push (RFC 9114 "PUSH_PROMISE").
|
|
677
|
+
private def check_request_frame(receiving, type)
|
|
678
|
+
if type == Frame::PUSH_PROMISE && @role == :client
|
|
679
|
+
raise Violation.new("a push promised, with no push allowed", code: ErrorCode::ID_ERROR)
|
|
680
|
+
end
|
|
681
|
+
out_of_order = !receiving.blocked && ((type == Frame::DATA && receiving.phase != :body) || (type == Frame::HEADERS && receiving.phase == :trailers))
|
|
682
|
+
return unless NOT_ON_REQUEST.include?(type) || type == Frame::PUSH_PROMISE || out_of_order
|
|
683
|
+
raise Violation.new("a frame of type 0x#{type.to_s(16)} on a request stream, after the #{receiving.phase}", code: ErrorCode::FRAME_UNEXPECTED)
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
# Content of a request, counted against its Content-Length (RFC 9114
|
|
687
|
+
# "Malformed Requests and Responses").
|
|
688
|
+
private def request_data(stream_id, receiving, data)
|
|
689
|
+
if (capsules = receiving.capsule_reader)
|
|
690
|
+
return capsules.feed(data) { |capsule| capsule_received(stream_id, capsule) }
|
|
691
|
+
end
|
|
692
|
+
receiving.received += data.bytesize
|
|
693
|
+
length = receiving.content_length
|
|
694
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR) if length && receiving.received > length
|
|
695
|
+
@events << Event::Data.new(stream_id:, data:, fin: false) unless data.empty?
|
|
696
|
+
end
|
|
697
|
+
|
|
698
|
+
# A DATAGRAM Capsule is an HTTP Datagram; any other goes as it is, for
|
|
699
|
+
# the layers above to read or drop (RFC 9297 "The Capsule Protocol").
|
|
700
|
+
private def capsule_received(stream_id, capsule)
|
|
701
|
+
if capsule.type == Capsule::DATAGRAM
|
|
702
|
+
@events << Event::Datagram.new(stream_id:, payload: capsule.value)
|
|
703
|
+
else
|
|
704
|
+
@events << Event::Capsule.new(stream_id:, capsule:)
|
|
705
|
+
end
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
# Holds `item` until the field section of `stream_id` is decoded, no more
|
|
709
|
+
# than MAX_WAITING of them and no more bytes of DATA and field sections
|
|
710
|
+
# than a field section may be long; past either, the stream stops with
|
|
711
|
+
# H3_EXCESSIVE_LOAD.
|
|
712
|
+
private def wait(stream_id, receiving, item)
|
|
713
|
+
receiving.waiting_bytes += case item
|
|
714
|
+
when Frame::Data then item.data.bytesize
|
|
715
|
+
when Frame::Headers then item.field_section.bytesize
|
|
716
|
+
else 0
|
|
717
|
+
end
|
|
718
|
+
return stream_error(stream_id, ErrorCode::EXCESSIVE_LOAD) if receiving.waiting.size >= MAX_WAITING || receiving.waiting_bytes > @max_section
|
|
719
|
+
receiving.waiting << item
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
# The QPACK decoder's `result` for a field section of `stream_id`: the
|
|
723
|
+
# header section until the application has it, the trailer section
|
|
724
|
+
# after. Past the limit, a header section gets a 431; a trailer
|
|
725
|
+
# section, which takes what the header section left of the limit,
|
|
726
|
+
# stops the stream with H3_EXCESSIVE_LOAD.
|
|
727
|
+
private def section_decoded(stream_id, receiving, result)
|
|
728
|
+
case result
|
|
729
|
+
when QPACK::Decoded
|
|
730
|
+
headers = result.fields.map { |field| [field.name, field.value] } #: Array[[String, String]]
|
|
731
|
+
@tracer&.frame_parsed(stream_id:, type: Frame::HEADERS, headers:)
|
|
732
|
+
if receiving.phase == :headers
|
|
733
|
+
headers_decoded(stream_id, receiving, headers, result.size)
|
|
734
|
+
else
|
|
735
|
+
trailers_decoded(stream_id, receiving, headers)
|
|
736
|
+
end
|
|
737
|
+
when QPACK::Blocked
|
|
738
|
+
receiving.blocked = true
|
|
739
|
+
@events << Event::Blocked.new(stream_id:)
|
|
740
|
+
when QPACK::Oversized
|
|
741
|
+
receiving.surfaced ? stream_error(stream_id, ErrorCode::EXCESSIVE_LOAD) : too_large(stream_id)
|
|
742
|
+
when QPACK::Failure
|
|
743
|
+
raise Violation.new(result.reason, code: result.code) unless result.stream_id
|
|
744
|
+
stream_error(stream_id, result.code)
|
|
745
|
+
end
|
|
746
|
+
end
|
|
747
|
+
|
|
748
|
+
# A header section: a request's at a server; at a client, a response's,
|
|
749
|
+
# or an interim response's, which the response follows.
|
|
750
|
+
private def headers_decoded(stream_id, receiving, headers, size)
|
|
751
|
+
begin
|
|
752
|
+
if @role == :server
|
|
753
|
+
content_length = Message.request(headers, extended_connect: @settings.enable_connect_protocol)
|
|
754
|
+
datagrams = headers.any? { |name, _| name == ":protocol" }
|
|
755
|
+
capsules = datagrams && Message.capsule_protocol?(headers)
|
|
756
|
+
else
|
|
757
|
+
status, content_length = Message.response(headers)
|
|
758
|
+
capsules = receiving.capsules_asked && status.between?(200, 299)
|
|
759
|
+
Message.capsule_protocol?(headers, status:) if capsules
|
|
760
|
+
end
|
|
761
|
+
rescue Message::Malformed
|
|
762
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR)
|
|
763
|
+
end
|
|
764
|
+
if status && status < 200
|
|
765
|
+
return @events << Event::Headers.new(stream_id:, headers:, fin: false)
|
|
766
|
+
end
|
|
767
|
+
# A response that never has content (RFC 9114 "Malformed Requests and
|
|
768
|
+
# Responses") may say a Content-Length all the same.
|
|
769
|
+
content_length = nil if receiving.no_content || status == 204 || status == 304
|
|
770
|
+
unless receiving.surfaced
|
|
771
|
+
sending = @sending[stream_id] = Sending.new
|
|
772
|
+
sending.datagrams = receiving.datagrams = datagrams || false
|
|
773
|
+
sending.capsules = capsules || false
|
|
774
|
+
end
|
|
775
|
+
receiving.capsule_reader = CapsuleReader.new(max_value: MAX_CAPSULE) if capsules
|
|
776
|
+
prioritize(stream_id, @pending_priorities.delete(stream_id) || priority_in(headers)) if @role == :server
|
|
777
|
+
receiving.surfaced = true
|
|
778
|
+
receiving.phase = :body
|
|
779
|
+
receiving.content_length = content_length
|
|
780
|
+
receiving.budget = @field_section_limit - size
|
|
781
|
+
@events << Event::Headers.new(stream_id:, headers:, fin: false)
|
|
782
|
+
end
|
|
783
|
+
|
|
784
|
+
# The trailer section ends the message: its content must have come
|
|
785
|
+
# whole by then.
|
|
786
|
+
private def trailers_decoded(stream_id, receiving, headers)
|
|
787
|
+
begin
|
|
788
|
+
Message.trailers(headers)
|
|
789
|
+
rescue Message::Malformed
|
|
790
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR)
|
|
791
|
+
end
|
|
792
|
+
length = receiving.content_length
|
|
793
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR) if length && receiving.received != length
|
|
794
|
+
receiving.phase = :trailers
|
|
795
|
+
@events << Event::Trailers.new(stream_id:, headers:)
|
|
796
|
+
end
|
|
797
|
+
|
|
798
|
+
# A field section that waited on QPACK is decoded: what waited behind
|
|
799
|
+
# it follows.
|
|
800
|
+
private def unblocked(result)
|
|
801
|
+
stream_id = result.stream_id or return
|
|
802
|
+
receiving = @receiving[stream_id] or return
|
|
803
|
+
receiving.blocked = false
|
|
804
|
+
section_decoded(stream_id, receiving, result)
|
|
805
|
+
until receiving.waiting.empty? || receiving.blocked || !@receiving[stream_id].equal?(receiving)
|
|
806
|
+
item = receiving.waiting.shift or break
|
|
807
|
+
request_item(stream_id, receiving, item)
|
|
808
|
+
end
|
|
809
|
+
request_ended(stream_id, receiving) if receiving.fin_waiting && !receiving.blocked && @receiving[stream_id].equal?(receiving)
|
|
810
|
+
end
|
|
811
|
+
|
|
812
|
+
# The request on `stream_id` ended: within a frame, H3_FRAME_ERROR;
|
|
813
|
+
# before its header section, H3_REQUEST_INCOMPLETE; short of its
|
|
814
|
+
# Content-Length, H3_MESSAGE_ERROR (RFC 9114 "Frame Layout", "HTTP
|
|
815
|
+
# Message Framing", "Malformed Requests and Responses"). Else its last
|
|
816
|
+
# event, or an empty Event::Data, tells so, unless a trailer section
|
|
817
|
+
# did already.
|
|
818
|
+
private def request_ended(stream_id, receiving)
|
|
819
|
+
return receiving.fin_waiting = true if receiving.blocked
|
|
820
|
+
raise Violation.new("the stream ended within a frame", code: ErrorCode::FRAME_ERROR) if receiving.reader.mid_frame?
|
|
821
|
+
if receiving.phase == :headers
|
|
822
|
+
return stream_error(stream_id, @role == :server ? ErrorCode::REQUEST_INCOMPLETE : ErrorCode::MESSAGE_ERROR)
|
|
823
|
+
end
|
|
824
|
+
length = receiving.content_length
|
|
825
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR) if receiving.phase == :body && length && receiving.received != length
|
|
826
|
+
return stream_error(stream_id, ErrorCode::MESSAGE_ERROR) if receiving.capsule_reader&.mid_capsule?
|
|
827
|
+
@receiving.delete(stream_id)
|
|
828
|
+
return if receiving.phase == :trailers
|
|
829
|
+
last = @events.last
|
|
830
|
+
if (last.is_a?(Event::Headers) || last.is_a?(Event::Data)) && last.stream_id == stream_id
|
|
831
|
+
@events[-1] = last.with(fin: true)
|
|
832
|
+
else
|
|
833
|
+
@events << Event::Data.new(stream_id:, data: "", fin: true)
|
|
834
|
+
end
|
|
835
|
+
end
|
|
836
|
+
|
|
837
|
+
# A request whose field section is past this end's limit, which the
|
|
838
|
+
# application never sees: a 431 answers it, and the rest of it is not
|
|
839
|
+
# read (RFC 9114 "Header Size Constraints", "Request Cancellation and
|
|
840
|
+
# Rejection").
|
|
841
|
+
private def too_large(stream_id)
|
|
842
|
+
@receiving.delete(stream_id)
|
|
843
|
+
write_headers(stream_id, [[":status", "431"]])
|
|
844
|
+
output(stream_id).finish
|
|
845
|
+
@actions << Action::StopSending.new(stream_id:, error_code: ErrorCode::NO_ERROR)
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
# Stops `stream_id` both ways with `code` (RFC 9114 "Request
|
|
849
|
+
# Cancellation and Rejection"); the application hears of it if it has
|
|
850
|
+
# the request.
|
|
851
|
+
private def stream_error(stream_id, code)
|
|
852
|
+
receiving = @receiving.delete(stream_id)
|
|
853
|
+
@decoder.cancel(stream_id)
|
|
854
|
+
forget_output(stream_id)
|
|
855
|
+
@sending.delete(stream_id)
|
|
856
|
+
@actions << Action::StopSending.new(stream_id:, error_code: code) << Action::Reset.new(stream_id:, error_code: code)
|
|
857
|
+
@events << Event::Reset.new(stream_id:, error_code: code) if receiving&.surfaced
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
# The sending side of `stream_id`, for the application to write to.
|
|
861
|
+
private def sending_for(stream_id)
|
|
862
|
+
raise Error::Usage, "stream #{stream_id} has nothing to send on, or has ended" unless (sending = @sending[stream_id])
|
|
863
|
+
sending
|
|
864
|
+
end
|
|
865
|
+
|
|
866
|
+
# `stream_id` ends after what waits for it.
|
|
867
|
+
private def finish_sending(stream_id)
|
|
868
|
+
@sending.delete(stream_id)
|
|
869
|
+
output(stream_id).finish
|
|
870
|
+
end
|
|
871
|
+
|
|
872
|
+
# A stream written less than asked hears, once, that it takes more.
|
|
873
|
+
private def writable_again(stream_id, output)
|
|
874
|
+
sending = @sending[stream_id]
|
|
875
|
+
return unless sending&.wants_writable && output.buffered < @max_send_buffer
|
|
876
|
+
sending.wants_writable = false
|
|
877
|
+
@events << Event::Writable.new(stream_id:)
|
|
878
|
+
end
|
|
879
|
+
|
|
880
|
+
# What `block` checks of a field section this end would send, misuse
|
|
881
|
+
# if it fails.
|
|
882
|
+
private def checked
|
|
883
|
+
yield
|
|
884
|
+
rescue Message::Malformed => malformed
|
|
885
|
+
raise ArgumentError, "a malformed field section: #{malformed.message}"
|
|
886
|
+
end
|
|
887
|
+
|
|
888
|
+
# QPACK's connection error, if `result` is one.
|
|
889
|
+
private def qpack_failed(result)
|
|
890
|
+
raise Violation.new(result.reason, code: result.code) if result.is_a?(QPACK::Failure)
|
|
891
|
+
end
|
|
892
|
+
|
|
893
|
+
# What this end's QPACK encoder and decoder have to write go on their
|
|
894
|
+
# streams, once the transport has given them.
|
|
895
|
+
private def take_qpack_instructions
|
|
896
|
+
encoder, decoder = @own_qpack_streams || return
|
|
897
|
+
{ encoder => @encoder.take_instructions, decoder => @decoder.take_instructions }.each do |stream_id, instructions|
|
|
898
|
+
output(stream_id) << instructions unless instructions.empty?
|
|
899
|
+
end
|
|
900
|
+
end
|
|
901
|
+
|
|
902
|
+
# Closes the connection with H3_INTERNAL_ERROR when the application has
|
|
903
|
+
# left more than MAX_EVENTS events untaken.
|
|
904
|
+
private def events_bounded
|
|
905
|
+
close_with(ErrorCode::INTERNAL_ERROR, "#{@events.size} events not taken") if !@closed && @events.size > MAX_EVENTS
|
|
906
|
+
end
|
|
907
|
+
|
|
908
|
+
# Closes the connection with `code` for `reason`: nothing waits to be
|
|
909
|
+
# sent any more, and nothing is taken.
|
|
910
|
+
private def close_with(code, reason)
|
|
911
|
+
@closed = true
|
|
912
|
+
@outputs.clear
|
|
913
|
+
@sending.clear
|
|
914
|
+
@receiving.clear
|
|
915
|
+
@actions << Action::Close.new(error_code: code, reason:)
|
|
916
|
+
end
|
|
917
|
+
|
|
918
|
+
private def output(stream_id) = @outputs[stream_id] ||= Output.new
|
|
919
|
+
|
|
920
|
+
# What waits for `stream_id` is gone, and its priority with it.
|
|
921
|
+
private def forget_output(stream_id)
|
|
922
|
+
@outputs.delete(stream_id)
|
|
923
|
+
@priorities.delete(stream_id)
|
|
924
|
+
end
|
|
925
|
+
|
|
926
|
+
# The priority the Priority header fields of `headers` ask for, the
|
|
927
|
+
# defaults if none (RFC 9218 "Priority Parameters").
|
|
928
|
+
private def priority_in(headers)
|
|
929
|
+
Priority.parse(headers.filter_map { |name, value| value if name == "priority" }.join(", "))
|
|
930
|
+
end
|
|
931
|
+
|
|
932
|
+
# A response's Priority overrides what it gives of the request's
|
|
933
|
+
# (RFC 9218 "Merging Client- and Server-Driven Priority Parameters").
|
|
934
|
+
private def merge_priority(stream_id, headers)
|
|
935
|
+
values = headers.filter_map { |name, value| value if name == "priority" }
|
|
936
|
+
return if values.empty?
|
|
937
|
+
urgency, incremental = Priority.parameters(values.join(", ")) || return
|
|
938
|
+
prioritize(stream_id, priority_of(stream_id).merge(urgency, incremental))
|
|
939
|
+
end
|
|
940
|
+
|
|
941
|
+
# Sends `stream_id` by `priority`, if this end still has something to
|
|
942
|
+
# send on it.
|
|
943
|
+
private def prioritize(stream_id, priority)
|
|
944
|
+
return unless @outputs.key?(stream_id) || @sending.key?(stream_id)
|
|
945
|
+
@priorities[stream_id] = priority
|
|
946
|
+
@actions << Action::Priority.new(stream_id:, urgency: priority.urgency, incremental: priority.incremental)
|
|
947
|
+
@tracer&.priority_updated(stream_id:, priority:) unless @own_critical.include?(stream_id)
|
|
948
|
+
end
|
|
949
|
+
|
|
950
|
+
# Encodes `headers` with QPACK and sends them in a HEADERS frame on
|
|
951
|
+
# `stream_id`.
|
|
952
|
+
private def write_headers(stream_id, headers)
|
|
953
|
+
fields = headers.map { |name, value| QPACK::Field.new(name:, value:) }
|
|
954
|
+
output(stream_id) << Frame.encode(Frame::Headers.new(field_section: @encoder.encode(stream_id, fields)))
|
|
955
|
+
@tracer&.frame_created(stream_id:, type: Frame::HEADERS, headers:)
|
|
956
|
+
end
|
|
957
|
+
|
|
958
|
+
# Tells the tracer of a frame read on `stream_id`: one this layer holds
|
|
959
|
+
# whole, when it is; any other as it begins. A HEADERS frame is told
|
|
960
|
+
# once its field section is decoded.
|
|
961
|
+
private def trace_parsed(stream_id, item)
|
|
962
|
+
return unless (tracer = @tracer)
|
|
963
|
+
case item
|
|
964
|
+
when FrameReader::Started
|
|
965
|
+
tracer.frame_parsed(stream_id:, type: item.type, length: item.length) unless FrameReader::HELD.include?(item.type)
|
|
966
|
+
when Frame::Settings then tracer.frame_parsed(stream_id:, type: Frame::SETTINGS, frame: item)
|
|
967
|
+
when Frame::GoAway then tracer.frame_parsed(stream_id:, type: Frame::GOAWAY, frame: item)
|
|
968
|
+
when Frame::CancelPush then tracer.frame_parsed(stream_id:, type: Frame::CANCEL_PUSH, frame: item)
|
|
969
|
+
when Frame::MaxPushId then tracer.frame_parsed(stream_id:, type: Frame::MAX_PUSH_ID, frame: item)
|
|
970
|
+
when Frame::PriorityUpdate
|
|
971
|
+
tracer.frame_parsed(stream_id:, type: item.push ? Frame::PRIORITY_UPDATE_PUSH : Frame::PRIORITY_UPDATE, frame: item)
|
|
972
|
+
end
|
|
973
|
+
end
|
|
974
|
+
|
|
975
|
+
private def priority_of(stream_id) = @priorities.fetch(stream_id) { Priority.new }
|
|
976
|
+
|
|
977
|
+
# Whether `stream_id` is of this end and goes one way (RFC 9000 "Stream
|
|
978
|
+
# Types and Identifiers").
|
|
979
|
+
private def local_unidirectional?(stream_id)
|
|
980
|
+
stream_id.is_a?(Integer) && stream_id.between?(0, Varint::MAX) && stream_id & 0x3 == (@role == :client ? 0x2 : 0x3)
|
|
981
|
+
end
|
|
982
|
+
end
|
|
983
|
+
end
|