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,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "connection"
|
|
4
|
+
require_relative "frame"
|
|
5
|
+
require_relative "settings"
|
|
6
|
+
require_relative "tracer"
|
|
7
|
+
|
|
8
|
+
module Raiha::HTTP3
|
|
9
|
+
# qlog for HTTP/3 (draft-ietf-quic-qlog-h3-events-12).
|
|
10
|
+
module Qlog
|
|
11
|
+
# The schema of these events, under the draft's number, as the draft
|
|
12
|
+
# asks until it is an RFC.
|
|
13
|
+
EVENT_SCHEMA = "urn:ietf:params:qlog:events:http3-12"
|
|
14
|
+
|
|
15
|
+
# A Tracer that writes the events of an HTTP/3 connection to `writer`,
|
|
16
|
+
# which takes them with `record(name, data)` in qlog's form: QUIC's
|
|
17
|
+
# Qlog::Tracer for the same connection, which bounds how much is
|
|
18
|
+
# written. What the peer sent is cut short here: a name or a value of a
|
|
19
|
+
# field to MAX_FIELD_BYTES, a field section to MAX_FIELDS lines, the
|
|
20
|
+
# rest counted, so that one event stays small however large what it
|
|
21
|
+
# tells of. A name or value that is not text goes as hex.
|
|
22
|
+
class Tracer
|
|
23
|
+
include Raiha::HTTP3::Tracer
|
|
24
|
+
|
|
25
|
+
MAX_FIELD_BYTES = 256
|
|
26
|
+
MAX_FIELDS = 64
|
|
27
|
+
# The fields that carry credentials: their values go as their length
|
|
28
|
+
# alone, so that a trace given to someone to read gives them no one's
|
|
29
|
+
# session or password.
|
|
30
|
+
SECRET_FIELDS = %w[authorization proxy-authorization cookie set-cookie].freeze
|
|
31
|
+
TEXT = /\A[\x20-\x7e\t]*\z/n
|
|
32
|
+
STREAM_TYPES = {
|
|
33
|
+
Connection::CONTROL_STREAM => "control", Connection::PUSH_STREAM => "push",
|
|
34
|
+
Connection::ENCODER_STREAM => "qpack_encode", Connection::DECODER_STREAM => "qpack_decode"
|
|
35
|
+
}.freeze
|
|
36
|
+
SETTING_NAMES = {
|
|
37
|
+
Settings::QPACK_MAX_TABLE_CAPACITY => "settings_qpack_max_table_capacity",
|
|
38
|
+
Settings::MAX_FIELD_SECTION_SIZE => "settings_max_field_section_size",
|
|
39
|
+
Settings::QPACK_BLOCKED_STREAMS => "settings_qpack_blocked_streams",
|
|
40
|
+
Settings::ENABLE_CONNECT_PROTOCOL => "settings_enable_connect_protocol",
|
|
41
|
+
Settings::H3_DATAGRAM => "settings_h3_datagram"
|
|
42
|
+
}.freeze
|
|
43
|
+
FRAME_TYPES = {
|
|
44
|
+
Frame::DATA => "data", Frame::HEADERS => "headers", Frame::CANCEL_PUSH => "cancel_push", Frame::SETTINGS => "settings",
|
|
45
|
+
Frame::PUSH_PROMISE => "push_promise", Frame::GOAWAY => "goaway", Frame::MAX_PUSH_ID => "max_push_id",
|
|
46
|
+
Frame::PRIORITY_UPDATE => "priority_update", Frame::PRIORITY_UPDATE_PUSH => "priority_update"
|
|
47
|
+
}.freeze
|
|
48
|
+
|
|
49
|
+
def initialize(writer)
|
|
50
|
+
@writer = writer
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def parameters_set(initiator:, settings:)
|
|
54
|
+
data = {
|
|
55
|
+
initiator: initiator.to_s, max_field_section_size: settings.max_field_section_size, max_table_capacity: settings.qpack_max_table_capacity,
|
|
56
|
+
blocked_streams_count: settings.qpack_blocked_streams, extended_connect: settings.enable_connect_protocol ? 1 : 0,
|
|
57
|
+
h3_datagram: settings.h3_datagram ? 1 : 0
|
|
58
|
+
}.compact #: Hash[Symbol, json]
|
|
59
|
+
@writer.record("http3:parameters_set", data)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def stream_type_set(initiator:, stream_id:, type:)
|
|
63
|
+
data = { initiator: initiator.to_s, stream_id: } #: Hash[Symbol, json]
|
|
64
|
+
if type == :request
|
|
65
|
+
data[:stream_type] = "request"
|
|
66
|
+
elsif (name = STREAM_TYPES[type])
|
|
67
|
+
data[:stream_type] = name
|
|
68
|
+
else
|
|
69
|
+
data[:stream_type] = reserved?(type) ? "reserved" : "unknown"
|
|
70
|
+
data[:stream_type_bytes] = type
|
|
71
|
+
end
|
|
72
|
+
@writer.record("http3:stream_type_set", data)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def frame_created(stream_id:, type:, length: nil, frame: nil, headers: nil)
|
|
76
|
+
@writer.record("http3:frame_created", { stream_id:, frame: frame_data(type, length, frame, headers) })
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def frame_parsed(stream_id:, type:, length: nil, frame: nil, headers: nil)
|
|
80
|
+
@writer.record("http3:frame_parsed", { stream_id:, frame: frame_data(type, length, frame, headers) })
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def priority_updated(stream_id:, priority:)
|
|
84
|
+
@writer.record("http3:priority_updated", { stream_id:, new: priority.to_field })
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private def frame_data(type, length, frame, headers)
|
|
88
|
+
data = {} #: Hash[Symbol, json]
|
|
89
|
+
if (name = FRAME_TYPES[type])
|
|
90
|
+
data[:frame_type] = name
|
|
91
|
+
else
|
|
92
|
+
data[:frame_type] = reserved?(type) || Frame::HTTP2_TYPES.include?(type) ? "reserved" : "unknown"
|
|
93
|
+
data[:frame_type_bytes] = type
|
|
94
|
+
end
|
|
95
|
+
data.merge!(fields(headers)) if headers
|
|
96
|
+
case frame
|
|
97
|
+
when Frame::Settings then data[:settings] = frame.settings.first(MAX_FIELDS).map { |identifier, value| setting(identifier, value) }
|
|
98
|
+
when Frame::GoAway then data[:id] = frame.id
|
|
99
|
+
when Frame::CancelPush, Frame::MaxPushId, Frame::PushPromise then data[:push_id] = frame.push_id
|
|
100
|
+
when Frame::PriorityUpdate
|
|
101
|
+
data[frame.push ? :push_id : :stream_id] = frame.element_id
|
|
102
|
+
data.merge!(field("priority_field_value", frame.priority))
|
|
103
|
+
end
|
|
104
|
+
data[:raw] = { payload_length: length } if length
|
|
105
|
+
data
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The lines of a field section, MAX_FIELDS at most, the rest counted.
|
|
109
|
+
private def fields(headers)
|
|
110
|
+
lines = headers.first(MAX_FIELDS).map do |name, value|
|
|
111
|
+
next field("name", name).merge("raiha:value_length": value.bytesize) if SECRET_FIELDS.include?(name.downcase)
|
|
112
|
+
field("name", name).merge(field("value", value))
|
|
113
|
+
end
|
|
114
|
+
result = { headers: lines } #: Hash[Symbol, json]
|
|
115
|
+
result[:"raiha:headers_omitted"] = headers.size - MAX_FIELDS if headers.size > MAX_FIELDS
|
|
116
|
+
result
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# A name, a value or a Priority Field Value as text, or hex when it is
|
|
120
|
+
# not, cut to MAX_FIELD_BYTES; its whole length when cut.
|
|
121
|
+
private def field(key, bytes)
|
|
122
|
+
cut = bytes.byteslice(0, MAX_FIELD_BYTES) || ""
|
|
123
|
+
result = TEXT.match?(cut) ? { key.to_sym => text(cut) } : { :"#{key}_bytes" => cut.unpack1("H*") } #: Hash[Symbol, json]
|
|
124
|
+
result[:"raiha:#{key}_length"] = bytes.bytesize if cut.bytesize < bytes.bytesize
|
|
125
|
+
result
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private def setting(identifier, value)
|
|
129
|
+
if (name = SETTING_NAMES[identifier])
|
|
130
|
+
{ name:, value: }
|
|
131
|
+
else
|
|
132
|
+
{ name: reserved?(identifier) ? "reserved" : "unknown", name_bytes: identifier, value: }
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The identifiers 0x1f * N + 0x21, which HTTP/3 reserves to be ignored
|
|
137
|
+
# (RFC 9114 "Reserved Stream Types", "Reserved Frame Types",
|
|
138
|
+
# "Defined SETTINGS Parameters").
|
|
139
|
+
private def reserved?(value) = value >= 0x21 && (value - 0x21) % 0x1f == 0
|
|
140
|
+
|
|
141
|
+
# Bytes TEXT matched, as a String JSON takes.
|
|
142
|
+
private def text(bytes) = bytes.dup.force_encoding(Encoding::UTF_8)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "dynamic_table"
|
|
4
|
+
require_relative "error_code"
|
|
5
|
+
require_relative "field"
|
|
6
|
+
require_relative "reader"
|
|
7
|
+
require_relative "result"
|
|
8
|
+
require_relative "static_table"
|
|
9
|
+
require_relative "writer"
|
|
10
|
+
|
|
11
|
+
module Raiha::HTTP3::QPACK
|
|
12
|
+
# The decoder of one connection: it follows the encoder stream into its
|
|
13
|
+
# dynamic table and decodes field sections against it. What the peer
|
|
14
|
+
# sends never raises; it comes back as a Failure.
|
|
15
|
+
class Decoder
|
|
16
|
+
DEFAULT_MAX_FIELD_SECTION_SIZE = 65_536
|
|
17
|
+
|
|
18
|
+
# The decoder instructions of RFC 9204 "Decoder Instructions", as the
|
|
19
|
+
# bits above their prefixes.
|
|
20
|
+
SECTION_ACKNOWLEDGMENT = 0b1000_0000
|
|
21
|
+
STREAM_CANCELLATION = 0b0100_0000
|
|
22
|
+
INSERT_COUNT_INCREMENT = 0b0000_0000
|
|
23
|
+
|
|
24
|
+
# How many times its size against the budget a field line can take on
|
|
25
|
+
# the wire, at most.
|
|
26
|
+
EXPANSION_BOUND = 4
|
|
27
|
+
|
|
28
|
+
# `max_table_capacity` and `max_blocked_streams` are what this end
|
|
29
|
+
# sends as SETTINGS_QPACK_MAX_TABLE_CAPACITY and
|
|
30
|
+
# SETTINGS_QPACK_BLOCKED_STREAMS; both default to 0, as in RFC 9204
|
|
31
|
+
# "Configuration".
|
|
32
|
+
def initialize(max_table_capacity: 0, max_blocked_streams: 0, max_field_section_size: DEFAULT_MAX_FIELD_SECTION_SIZE)
|
|
33
|
+
@table = DynamicTable.new(max_table_capacity)
|
|
34
|
+
@max_blocked_streams = max_blocked_streams
|
|
35
|
+
@max_field_section_size = max_field_section_size
|
|
36
|
+
# The encoder stream's bytes of an instruction yet to arrive whole, and
|
|
37
|
+
# how many of them it needs at least before it is worth reading again.
|
|
38
|
+
@pending = String.new(encoding: Encoding::BINARY)
|
|
39
|
+
@needed = 0
|
|
40
|
+
# [Required Insert Count, Base, the section read past its prefix,
|
|
41
|
+
# limit] of each stream blocked, by stream ID in the order they
|
|
42
|
+
# blocked.
|
|
43
|
+
@blocked = {} #: Hash[Integer, [Integer, Integer, Reader, Integer]]
|
|
44
|
+
# The decoder stream's instructions yet to be taken, and the Known
|
|
45
|
+
# Received Count as the encoder will have it once they reach it (RFC
|
|
46
|
+
# 9204 "Known Received Count").
|
|
47
|
+
@instructions = Writer.new
|
|
48
|
+
@known_received_count = 0
|
|
49
|
+
@failure = nil
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Follows the instructions in `bytes` from the encoder stream (RFC 9204
|
|
53
|
+
# "Encoder Instructions"), keeping an instruction cut short for the
|
|
54
|
+
# bytes after it. A string that would not fit the dynamic table is
|
|
55
|
+
# refused as soon as its length shows it, before its bytes come.
|
|
56
|
+
#
|
|
57
|
+
# Gives the field sections the new entries unblock, in the order they
|
|
58
|
+
# blocked: decoded, or failed when only their stream does. A section
|
|
59
|
+
# that does not decode fails the connection with
|
|
60
|
+
# QPACK_DECOMPRESSION_FAILED, the encoder stream's own errors with
|
|
61
|
+
# QPACK_ENCODER_STREAM_ERROR.
|
|
62
|
+
def receive_encoder_stream(bytes)
|
|
63
|
+
return @failure if @failure # steep:ignore
|
|
64
|
+
begin
|
|
65
|
+
follow_all(bytes)
|
|
66
|
+
rescue Malformed, TooLong => error
|
|
67
|
+
return @failure = Failure.new(code: ErrorCode::ENCODER_STREAM_ERROR, reason: error.message)
|
|
68
|
+
end
|
|
69
|
+
unblock
|
|
70
|
+
rescue Malformed => error
|
|
71
|
+
@failure = Failure.new(code: ErrorCode::DECOMPRESSION_FAILED, reason: error.message)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The field section `bytes` from the stream `stream_id` (RFC 9204
|
|
75
|
+
# "Field Line Representations"). Bytes that do not decode are a
|
|
76
|
+
# Failure of the connection, but an integer past 62 bits only of the
|
|
77
|
+
# stream (RFC 9204 "Implementation Limits").
|
|
78
|
+
#
|
|
79
|
+
# The fields are counted against `limit` as they come out, each its
|
|
80
|
+
# name and value and 32 more (RFC 9114 "Header Size Constraints"): the
|
|
81
|
+
# decoder stops at the line that passes it, or at a string sure to, and
|
|
82
|
+
# gives Oversized, not expanding the rest. The limit is the budget of
|
|
83
|
+
# the whole message, so the HTTP/3 layer passes what is left of it for
|
|
84
|
+
# trailers.
|
|
85
|
+
#
|
|
86
|
+
# A section that refers to entries yet to come is kept, Blocked, until
|
|
87
|
+
# the encoder stream brings them (RFC 9204 "Blocked Decoding"): more
|
|
88
|
+
# blocked streams than `max_blocked_streams` fail the connection. A
|
|
89
|
+
# stream holds one section at a time, so another for a stream still
|
|
90
|
+
# blocked raises Error::Usage.
|
|
91
|
+
def decode(stream_id, bytes, limit: @max_field_section_size)
|
|
92
|
+
return @failure if @failure # steep:ignore
|
|
93
|
+
raise Error::Usage, "stream #{stream_id} has a field section blocked" if @blocked.key?(stream_id)
|
|
94
|
+
raise ArgumentError, "a limit is not negative, not #{limit}" if limit.negative?
|
|
95
|
+
reader = Reader.new(bytes)
|
|
96
|
+
required = required_insert_count(reader.integer(8))
|
|
97
|
+
negative = reader.peek[7] == 1
|
|
98
|
+
delta = reader.integer(7)
|
|
99
|
+
raise Malformed, "a Base below 0" if negative && delta >= required
|
|
100
|
+
base = negative ? required - delta - 1 : required + delta
|
|
101
|
+
return block(stream_id, required, base, bytes, reader.offset, limit) if required > @table.insert_count
|
|
102
|
+
fields_of(stream_id, reader, required, base, limit)
|
|
103
|
+
rescue IntegerOverflow => error
|
|
104
|
+
Failure.new(code: ErrorCode::DECOMPRESSION_FAILED, reason: error.message, stream_id:)
|
|
105
|
+
rescue Malformed => error
|
|
106
|
+
@failure = Failure.new(code: ErrorCode::DECOMPRESSION_FAILED, reason: error.message)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Forgets the stream `stream_id`, reset or no longer read, with the
|
|
110
|
+
# section it has blocked, and tells the encoder with a Stream
|
|
111
|
+
# Cancellation (RFC 9204 "Abandonment of a Stream"). A table too small
|
|
112
|
+
# for any entry has no reference to cancel, and sends none.
|
|
113
|
+
def cancel(stream_id)
|
|
114
|
+
@blocked.delete(stream_id)
|
|
115
|
+
@instructions.integer(stream_id, 6, STREAM_CANCELLATION) if @table.max_entries.positive?
|
|
116
|
+
nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# The bytes to write on the decoder stream: a Section Acknowledgment
|
|
120
|
+
# for each section decoded that refers to the dynamic table, the Stream
|
|
121
|
+
# Cancellations, and one Insert Count Increment for the entries
|
|
122
|
+
# inserted since the encoder last learnt of them.
|
|
123
|
+
def take_instructions
|
|
124
|
+
if @table.insert_count > @known_received_count
|
|
125
|
+
@instructions.integer(@table.insert_count - @known_received_count, 6, INSERT_COUNT_INCREMENT)
|
|
126
|
+
@known_received_count = @table.insert_count
|
|
127
|
+
end
|
|
128
|
+
bytes = @instructions.to_s
|
|
129
|
+
@instructions = Writer.new
|
|
130
|
+
bytes
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
private def follow_all(bytes)
|
|
134
|
+
@pending.append_as_bytes(bytes)
|
|
135
|
+
return if @pending.bytesize < @needed
|
|
136
|
+
reader = Reader.new(@pending)
|
|
137
|
+
until reader.eof?
|
|
138
|
+
start = reader.offset
|
|
139
|
+
begin
|
|
140
|
+
follow(reader)
|
|
141
|
+
rescue Truncated => truncated
|
|
142
|
+
if start.positive?
|
|
143
|
+
@pending = @pending.byteslice(start..) #: String
|
|
144
|
+
end
|
|
145
|
+
@needed = truncated.needed - start
|
|
146
|
+
return
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
@pending = String.new(encoding: Encoding::BINARY)
|
|
150
|
+
@needed = 0
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Keeps the section, its bytes from `offset` on copied unless frozen,
|
|
154
|
+
# since the caller's buffer may change before they are read. A line
|
|
155
|
+
# takes at most four times as many octets on the wire as it counts
|
|
156
|
+
# against the budget (32 and a string decoded from 30-bit codes at
|
|
157
|
+
# worst, against its integers and the string's octets), so a section
|
|
158
|
+
# longer than four times the limit cannot fit it, whatever the entries
|
|
159
|
+
# it waits for: it is Oversized at once, which bounds what the blocked
|
|
160
|
+
# sections hold to the blocked streams allowed times that.
|
|
161
|
+
private def block(stream_id, required, base, bytes, offset, limit)
|
|
162
|
+
return oversize(stream_id, required) if bytes.bytesize - offset > EXPANSION_BOUND * limit
|
|
163
|
+
if @blocked.size >= @max_blocked_streams
|
|
164
|
+
raise Malformed, "a field section blocks a stream past the #{@max_blocked_streams} allowed"
|
|
165
|
+
end
|
|
166
|
+
@blocked[stream_id] = [required, base, Reader.new(bytes.frozen? ? bytes : bytes.dup.freeze, offset), limit]
|
|
167
|
+
Blocked.new(stream_id:)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
private def unblock
|
|
171
|
+
results = [] #: Array[Decoded | Oversized | Failure]
|
|
172
|
+
return results if @blocked.empty?
|
|
173
|
+
@blocked.each do |stream_id, (required, base, reader, limit)|
|
|
174
|
+
next if required > @table.insert_count
|
|
175
|
+
@blocked.delete(stream_id)
|
|
176
|
+
results << begin
|
|
177
|
+
fields_of(stream_id, reader, required, base, limit)
|
|
178
|
+
rescue IntegerOverflow => error
|
|
179
|
+
Failure.new(code: ErrorCode::DECOMPRESSION_FAILED, reason: error.message, stream_id:)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
results
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The field lines from `reader` on, below the Required Insert Count
|
|
186
|
+
# `required` and against the Base `base`, until they pass `limit`.
|
|
187
|
+
private def fields_of(stream_id, reader, required, base, limit)
|
|
188
|
+
fields = [] #: Array[Field]
|
|
189
|
+
size = 0
|
|
190
|
+
until reader.eof?
|
|
191
|
+
field = represent(reader, required, base, limit - size - DynamicTable::ENTRY_OVERHEAD)
|
|
192
|
+
size += field.name.bytesize + field.value.bytesize + DynamicTable::ENTRY_OVERHEAD
|
|
193
|
+
return oversize(stream_id, required) if size > limit
|
|
194
|
+
fields << field
|
|
195
|
+
end
|
|
196
|
+
acknowledge(stream_id, required) if required.positive?
|
|
197
|
+
Decoded.new(stream_id:, fields: fields.freeze, size:)
|
|
198
|
+
rescue TooLong
|
|
199
|
+
oversize(stream_id, required)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# A section left undecoded keeps its references, so one that refers to
|
|
203
|
+
# the dynamic table is cancelled (RFC 9204 "Abandonment of a Stream").
|
|
204
|
+
private def oversize(stream_id, required)
|
|
205
|
+
@instructions.integer(stream_id, 6, STREAM_CANCELLATION) if required.positive?
|
|
206
|
+
Oversized.new(stream_id:)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# RFC 9204 "Section Acknowledgment", which tells the encoder of every
|
|
210
|
+
# entry below the section's Required Insert Count.
|
|
211
|
+
private def acknowledge(stream_id, required)
|
|
212
|
+
@instructions.integer(stream_id, 7, SECTION_ACKNOWLEDGMENT)
|
|
213
|
+
@known_received_count = required if required > @known_received_count
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# RFC 9204 "Required Insert Count": the encoded value is the count
|
|
217
|
+
# modulo twice the most entries the table can hold, plus 1.
|
|
218
|
+
private def required_insert_count(encoded)
|
|
219
|
+
return 0 if encoded.zero?
|
|
220
|
+
max_entries = @table.max_entries
|
|
221
|
+
full_range = 2 * max_entries
|
|
222
|
+
raise Malformed, "an encoded Required Insert Count of #{encoded} past #{full_range}" if encoded > full_range
|
|
223
|
+
max_value = @table.insert_count + max_entries
|
|
224
|
+
required = (max_value / full_range * full_range) + encoded - 1
|
|
225
|
+
if required > max_value
|
|
226
|
+
raise Malformed, "an encoded Required Insert Count of #{encoded} no encoder could send" if required <= full_range
|
|
227
|
+
required -= full_range
|
|
228
|
+
end
|
|
229
|
+
raise Malformed, "a Required Insert Count of 0 encoded as #{encoded}" if required.zero?
|
|
230
|
+
required
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# One field line, its representation told by the first byte, whose
|
|
234
|
+
# strings take `room` octets at most or raise TooLong. A reference to
|
|
235
|
+
# the dynamic table must be below the Required Insert Count (RFC 9204
|
|
236
|
+
# "Invalid References").
|
|
237
|
+
private def represent(reader, required, base, room)
|
|
238
|
+
first = reader.peek
|
|
239
|
+
if first >= 0b1000_0000 # Indexed Field Line
|
|
240
|
+
static = first[6] == 1
|
|
241
|
+
index = reader.integer(6)
|
|
242
|
+
name, value = static ? static_entry(index) : field_entry(base - 1 - index, required)
|
|
243
|
+
Field.new(name:, value:)
|
|
244
|
+
elsif first >= 0b0100_0000 # Literal Field Line with Name Reference
|
|
245
|
+
never_indexed = first[5] == 1
|
|
246
|
+
static = first[4] == 1
|
|
247
|
+
index = reader.integer(4)
|
|
248
|
+
name = (static ? static_entry(index) : field_entry(base - 1 - index, required)).first
|
|
249
|
+
Field.new(name:, value: reader.string(8, room - name.bytesize), never_indexed:)
|
|
250
|
+
elsif first >= 0b0010_0000 # Literal Field Line with Literal Name
|
|
251
|
+
never_indexed = first[4] == 1
|
|
252
|
+
name = reader.string(4, room)
|
|
253
|
+
Field.new(name:, value: reader.string(8, room - name.bytesize), never_indexed:)
|
|
254
|
+
elsif first >= 0b0001_0000 # Indexed Field Line with Post-Base Index
|
|
255
|
+
name, value = field_entry(base + reader.integer(4), required)
|
|
256
|
+
Field.new(name:, value:)
|
|
257
|
+
else # Literal Field Line with Post-Base Name Reference
|
|
258
|
+
never_indexed = first[3] == 1
|
|
259
|
+
name = field_entry(base + reader.integer(3), required).first
|
|
260
|
+
Field.new(name:, value: reader.string(8, room - name.bytesize), never_indexed:)
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
private def field_entry(absolute, required)
|
|
265
|
+
if absolute.negative? || absolute >= required
|
|
266
|
+
raise Malformed, "a reference to dynamic entry #{absolute}, not below the Required Insert Count of #{required}"
|
|
267
|
+
end
|
|
268
|
+
@table.entry(absolute) or raise Malformed, "a reference to dynamic entry #{absolute}, which was evicted"
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
private def follow(reader)
|
|
272
|
+
first = reader.peek
|
|
273
|
+
if first >= 0b1000_0000 # Insert with Name Reference
|
|
274
|
+
static = first[6] == 1
|
|
275
|
+
name = (static ? static_entry(reader.integer(6)) : relative_entry(reader.integer(6))).first
|
|
276
|
+
@table.insert(name, reader.string(8, room - name.bytesize))
|
|
277
|
+
elsif first >= 0b0100_0000 # Insert with Literal Name
|
|
278
|
+
name = reader.string(6, room)
|
|
279
|
+
@table.insert(name, reader.string(8, room - name.bytesize))
|
|
280
|
+
elsif first >= 0b0010_0000 # Set Dynamic Table Capacity
|
|
281
|
+
@table.resize(reader.integer(5))
|
|
282
|
+
else # Duplicate
|
|
283
|
+
name, value = relative_entry(reader.integer(5))
|
|
284
|
+
@table.insert(name, value)
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# The octets of name and value an entry may have and still fit.
|
|
289
|
+
private def room = @table.capacity - DynamicTable::ENTRY_OVERHEAD
|
|
290
|
+
|
|
291
|
+
private def static_entry(index)
|
|
292
|
+
StaticTable.entry(index) or raise Malformed, "no static entry #{index}"
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# RFC 9204 "Relative Indexing": on the encoder stream, 0 is the entry
|
|
296
|
+
# inserted last.
|
|
297
|
+
private def relative_entry(index)
|
|
298
|
+
absolute = @table.insert_count - 1 - index
|
|
299
|
+
@table.entry(absolute) or raise Malformed, "no dynamic entry at relative index #{index}"
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "error"
|
|
4
|
+
|
|
5
|
+
module Raiha::HTTP3::QPACK
|
|
6
|
+
# The dynamic table as the decoder keeps it (RFC 9204 "Dynamic Table"):
|
|
7
|
+
# entries by absolute index, the oldest evicted first. The entries sit in
|
|
8
|
+
# a ring, so inserting and evicting move none of the others; the ring
|
|
9
|
+
# doubles when it fills, which the capacity bounds.
|
|
10
|
+
#
|
|
11
|
+
# The table is looked up by index only. The encoder, which would look up
|
|
12
|
+
# names and values, inserts nothing yet.
|
|
13
|
+
class DynamicTable
|
|
14
|
+
# RFC 9204 "Dynamic Table Size": an entry's size is its name's and
|
|
15
|
+
# value's octets and 32 more.
|
|
16
|
+
ENTRY_OVERHEAD = 32
|
|
17
|
+
INITIAL_SLOTS = 16
|
|
18
|
+
|
|
19
|
+
# The capacity the encoder set, the size of the entries held, and how
|
|
20
|
+
# many entries were ever inserted, which is the absolute index of the
|
|
21
|
+
# next one.
|
|
22
|
+
attr_reader :capacity, :size, :insert_count
|
|
23
|
+
|
|
24
|
+
def initialize(max_capacity)
|
|
25
|
+
@max_capacity = max_capacity
|
|
26
|
+
@capacity = 0
|
|
27
|
+
@size = 0
|
|
28
|
+
@insert_count = 0
|
|
29
|
+
# The absolute index of the oldest entry held.
|
|
30
|
+
@evicted = 0
|
|
31
|
+
@ring = Array.new(INITIAL_SLOTS) #: Array[[String, String]?]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# RFC 9204 "Required Insert Count": the most entries the table can
|
|
35
|
+
# hold, by the maximum capacity rather than the one set.
|
|
36
|
+
def max_entries = @max_capacity / ENTRY_OVERHEAD
|
|
37
|
+
|
|
38
|
+
# Set Dynamic Table Capacity: evicts down to `capacity`, which may not
|
|
39
|
+
# pass the maximum.
|
|
40
|
+
def resize(capacity)
|
|
41
|
+
raise Malformed, "a capacity of #{capacity} past the maximum of #{@max_capacity}" if capacity > @max_capacity
|
|
42
|
+
@capacity = capacity
|
|
43
|
+
evict_down_to(capacity)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Inserts [name, value] after evicting what it needs room for. An entry
|
|
47
|
+
# larger than the capacity raises Malformed. `name` and `value` are
|
|
48
|
+
# frozen, so one taken from an entry the insertion evicts stays whole.
|
|
49
|
+
def insert(name, value)
|
|
50
|
+
entry_size = name.bytesize + value.bytesize + ENTRY_OVERHEAD
|
|
51
|
+
raise Malformed, "an entry of #{entry_size} octets in a table of #{@capacity}" if entry_size > @capacity
|
|
52
|
+
evict_down_to(@capacity - entry_size)
|
|
53
|
+
grow if @insert_count - @evicted == @ring.size
|
|
54
|
+
@ring[@insert_count & (@ring.size - 1)] = [name, value]
|
|
55
|
+
@insert_count += 1
|
|
56
|
+
@size += entry_size
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The entry at the absolute index `index`, or nil for one evicted or
|
|
60
|
+
# yet to be inserted.
|
|
61
|
+
def entry(index)
|
|
62
|
+
return nil unless index >= @evicted && index < @insert_count
|
|
63
|
+
@ring[index & (@ring.size - 1)]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private def evict_down_to(limit)
|
|
67
|
+
while @size > limit
|
|
68
|
+
slot = @evicted & (@ring.size - 1)
|
|
69
|
+
name, value = @ring[slot] #: [String, String]
|
|
70
|
+
@size -= name.bytesize + value.bytesize + ENTRY_OVERHEAD
|
|
71
|
+
@ring[slot] = nil
|
|
72
|
+
@evicted += 1
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Doubles the ring, laying each entry at its absolute index anew.
|
|
77
|
+
private def grow
|
|
78
|
+
ring = Array.new(@ring.size * 2) #: Array[[String, String]?]
|
|
79
|
+
(@evicted...@insert_count).each { |index| ring[index & (ring.size - 1)] = @ring[index & (@ring.size - 1)] }
|
|
80
|
+
@ring = ring
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "field"
|
|
4
|
+
require_relative "result"
|
|
5
|
+
require_relative "static_table"
|
|
6
|
+
require_relative "reader"
|
|
7
|
+
require_relative "writer"
|
|
8
|
+
|
|
9
|
+
module Raiha::HTTP3::QPACK
|
|
10
|
+
# The encoder of one connection. It refers to the static table and sends
|
|
11
|
+
# the rest as literals: it inserts nothing into the dynamic table, so a
|
|
12
|
+
# field section never waits on the encoder stream and no entry is held
|
|
13
|
+
# by a reference the decoder has yet to acknowledge.
|
|
14
|
+
class Encoder
|
|
15
|
+
# The representations of RFC 9204 "Field Line Representations", as the
|
|
16
|
+
# bits above their prefixes.
|
|
17
|
+
INDEXED_STATIC = 0b1100_0000 # Indexed Field Line, T = 1
|
|
18
|
+
NAME_REFERENCE_STATIC = 0b0101_0000 # Literal Field Line with Name Reference, T = 1
|
|
19
|
+
NAME_REFERENCE_NEVER_INDEXED = 0b0010_0000
|
|
20
|
+
LITERAL_NAME = 0b0010_0000 # Literal Field Line with Literal Name
|
|
21
|
+
LITERAL_NAME_NEVER_INDEXED = 0b0001_0000
|
|
22
|
+
|
|
23
|
+
def initialize
|
|
24
|
+
# The decoder stream's bytes of an instruction yet to arrive whole: at
|
|
25
|
+
# most an integer's ten bytes, as a longer one fails to decode.
|
|
26
|
+
@pending = String.new(encoding: Encoding::BINARY)
|
|
27
|
+
@failure = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The field section of `fields` for the stream `stream_id`. Its
|
|
31
|
+
# Required Insert Count and Base are both 0. A field in the static
|
|
32
|
+
# table goes as its index, one whose name is there as a reference to
|
|
33
|
+
# the name, and the rest as literals; a never-indexed one is always a
|
|
34
|
+
# literal.
|
|
35
|
+
def encode(stream_id, fields)
|
|
36
|
+
writer = Writer.new.integer(0, 8).integer(0, 7)
|
|
37
|
+
fields.each { |field| represent(writer, field) }
|
|
38
|
+
writer.to_s
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Follows the instructions in `bytes` from the decoder stream (RFC 9204
|
|
42
|
+
# "Decoder Instructions"), keeping an instruction cut short for the
|
|
43
|
+
# bytes after it. With no section that refers to the dynamic table, a
|
|
44
|
+
# Section Acknowledgment has nothing to acknowledge and an Insert Count
|
|
45
|
+
# Increment nothing to count, so either is a Failure, as is anything
|
|
46
|
+
# after one; a Stream Cancellation has nothing to cancel.
|
|
47
|
+
def receive_decoder_stream(bytes)
|
|
48
|
+
return @failure if @failure
|
|
49
|
+
@pending.append_as_bytes(bytes)
|
|
50
|
+
reader = Reader.new(@pending)
|
|
51
|
+
until reader.eof?
|
|
52
|
+
start = reader.offset
|
|
53
|
+
begin
|
|
54
|
+
follow(reader)
|
|
55
|
+
rescue Truncated
|
|
56
|
+
@pending = @pending.byteslice(start..) #: String
|
|
57
|
+
return
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
@pending = String.new(encoding: Encoding::BINARY)
|
|
61
|
+
nil
|
|
62
|
+
rescue Malformed => error
|
|
63
|
+
@failure = Failure.new(code: ErrorCode::DECODER_STREAM_ERROR, reason: error.message)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The bytes to write on the encoder stream: none, with nothing inserted
|
|
67
|
+
# into the dynamic table.
|
|
68
|
+
def take_instructions = String.new(encoding: Encoding::BINARY)
|
|
69
|
+
|
|
70
|
+
private def follow(reader)
|
|
71
|
+
first = reader.peek
|
|
72
|
+
if first >= 0b1000_0000
|
|
73
|
+
stream_id = reader.integer(7)
|
|
74
|
+
raise Malformed, "a Section Acknowledgment for stream #{stream_id}, which has no section to acknowledge"
|
|
75
|
+
elsif first >= 0b0100_0000
|
|
76
|
+
reader.integer(6)
|
|
77
|
+
else
|
|
78
|
+
increment = reader.integer(6)
|
|
79
|
+
raise Malformed, "an Insert Count Increment of #{increment}, with no entry inserted"
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private def represent(writer, field)
|
|
84
|
+
never_indexed = field.never_indexed
|
|
85
|
+
if !never_indexed && (index = StaticTable.find(field.name, field.value))
|
|
86
|
+
writer.integer(index, 6, INDEXED_STATIC)
|
|
87
|
+
elsif (index = StaticTable.find_name(field.name))
|
|
88
|
+
writer.integer(index, 4, NAME_REFERENCE_STATIC | (never_indexed ? NAME_REFERENCE_NEVER_INDEXED : 0)).string(field.value, 8)
|
|
89
|
+
else
|
|
90
|
+
writer.string(field.name, 4, LITERAL_NAME | (never_indexed ? LITERAL_NAME_NEVER_INDEXED : 0)).string(field.value, 8)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::HTTP3::QPACK
|
|
4
|
+
# Exceptions are for misuse of the API only. What the peer sends never
|
|
5
|
+
# raises; it comes back as a Failure.
|
|
6
|
+
module Error
|
|
7
|
+
# A call the API does not allow, as a field section for a stream that
|
|
8
|
+
# has one blocked.
|
|
9
|
+
class Usage < StandardError; end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Bytes from the peer that do not decode. Raised inside QPACK and turned
|
|
13
|
+
# into a Failure where the bytes came in, whose error code depends on
|
|
14
|
+
# the stream they came on; the caller never sees it.
|
|
15
|
+
class Malformed < StandardError; end
|
|
16
|
+
|
|
17
|
+
# The bytes ran out before a field did. On the encoder and decoder
|
|
18
|
+
# streams the rest is yet to come; a field section is always whole.
|
|
19
|
+
# `needed` is how far the bytes must reach, at least, for the field to
|
|
20
|
+
# be read, so that a long instruction arriving a byte at a time is not
|
|
21
|
+
# read over again for each byte.
|
|
22
|
+
class Truncated < Malformed
|
|
23
|
+
attr_reader :needed
|
|
24
|
+
|
|
25
|
+
def initialize(message = nil, needed: 0)
|
|
26
|
+
super(message)
|
|
27
|
+
@needed = needed
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# An integer larger than QPACK has to decode (RFC 9204 "Implementation
|
|
32
|
+
# Limits"): a stream error on a request stream, unlike the rest.
|
|
33
|
+
class IntegerOverflow < Malformed; end
|
|
34
|
+
|
|
35
|
+
# A string longer than the caller allows, found out before its bytes are
|
|
36
|
+
# taken: past the capacity of the dynamic table on the encoder stream,
|
|
37
|
+
# past the budget in a field section. Not malformed in itself.
|
|
38
|
+
class TooLong < StandardError; end
|
|
39
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::HTTP3::QPACK
|
|
4
|
+
# The HTTP/3 error codes QPACK defines (RFC 9204 "Error Handling").
|
|
5
|
+
module ErrorCode
|
|
6
|
+
# A field section could not be decoded.
|
|
7
|
+
DECOMPRESSION_FAILED = 0x0200
|
|
8
|
+
# An instruction on the encoder stream could not be followed.
|
|
9
|
+
ENCODER_STREAM_ERROR = 0x0201
|
|
10
|
+
# An instruction on the decoder stream could not be followed.
|
|
11
|
+
DECODER_STREAM_ERROR = 0x0202
|
|
12
|
+
end
|
|
13
|
+
end
|