biryani 0.0.15 → 0.0.17
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/.ruby-version +1 -1
- data/conformance/server_spec.rb +3 -2
- data/example/sqlite3.rb +4 -3
- data/lib/biryani/connection.rb +70 -33
- data/lib/biryani/frame.rb +4 -1
- data/lib/biryani/hpack/decoder.rb +8 -6
- data/lib/biryani/hpack/dynamic_table.rb +24 -15
- data/lib/biryani/hpack/encoder.rb +26 -7
- data/lib/biryani/hpack/field.rb +4 -3
- data/lib/biryani/hpack/huffman.rb +2 -2
- data/lib/biryani/hpack/integer.rb +11 -0
- data/lib/biryani/hpack/string.rb +7 -0
- data/lib/biryani/server.rb +7 -3
- data/lib/biryani/stream.rb +12 -9
- data/lib/biryani/streams_context.rb +9 -2
- data/lib/biryani/utils.rb +9 -0
- data/lib/biryani/version.rb +1 -1
- data/lib/biryani.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '029eb52bcddc4958bfe8e9a6b003f02d5b9ec3fe41a67591cb985b4662faadd8'
|
|
4
|
+
data.tar.gz: c354991b18f88477b1ce943d094664a3e4f7eb8a0e12d3cdc0b9eb8c8b1140d5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81ccd5878932f0c0f442d397b2e8e6948dd5c682bef4b4d3db675f174e2a9ac27828c5fa8a18e10acdbf52ccc03c7853c31aabb21861889f82675cd6460ba6ca
|
|
7
|
+
data.tar.gz: 35f7193bf001c304ef76ebf1d4544f59907c783cc558bca5aec597299f45182840b425a026e50675c02e11cd120c3fb053c3037c68553792cc4dbb1f65d2012e
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
4.
|
|
1
|
+
4.1-dev
|
data/conformance/server_spec.rb
CHANGED
|
@@ -4,7 +4,7 @@ RSpec.describe Server do
|
|
|
4
4
|
before do
|
|
5
5
|
@tcpserver = TCPServer.open(PORT)
|
|
6
6
|
|
|
7
|
-
Ractor.new
|
|
7
|
+
r = Ractor.new do
|
|
8
8
|
server = Server.new(
|
|
9
9
|
Ractor.shareable_proc do |_, res|
|
|
10
10
|
res.status = 200
|
|
@@ -12,8 +12,9 @@ RSpec.describe Server do
|
|
|
12
12
|
end
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
-
server.run(
|
|
15
|
+
server.run(Ractor.receive)
|
|
16
16
|
end
|
|
17
|
+
r.send(@tcpserver.dup, move: true)
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
let(:junit_report_file_path) do
|
data/example/sqlite3.rb
CHANGED
|
@@ -20,13 +20,13 @@ require 'biryani'
|
|
|
20
20
|
# | DB_PORT.receive <---------------+-----------+--+---- DB_PORT.send | |
|
|
21
21
|
# | reply.send ---------------------+-----------+--+---> reply.receive | |
|
|
22
22
|
# | | | +------------------------------------------+ |
|
|
23
|
-
# |
|
|
23
|
+
# | r.send(socket, move: true) | | |
|
|
24
24
|
# +----+-------------------------------+ +-------------------------------------------------+
|
|
25
25
|
# | ^
|
|
26
26
|
# v |
|
|
27
27
|
# +-- server Ractor ----------------------+ +-- connection Ractor (1 Ractor / connection) --+--+
|
|
28
28
|
# | server.run(socket) | | Stream.new(tx, stream_id, proc) | |
|
|
29
|
-
# |
|
|
29
|
+
# | r.send(io, move: true) ------------+------>| Ractor.new(tx, stream_id, proc) -----------+ |
|
|
30
30
|
# +---------------------------------------+ +--------------------------------------------------+
|
|
31
31
|
DB_PORT = Ractor::Port.new
|
|
32
32
|
|
|
@@ -159,7 +159,8 @@ server = Biryani::Server.new(
|
|
|
159
159
|
App.call(req, res)
|
|
160
160
|
end
|
|
161
161
|
)
|
|
162
|
-
Ractor.new(server
|
|
162
|
+
r = Ractor.new(server) { |s| s.run(Ractor.receive) }
|
|
163
|
+
r.send(socket, move: true)
|
|
163
164
|
DB.serve(path)
|
|
164
165
|
|
|
165
166
|
# $ bundle exec ruby sqlite3.rb
|
data/lib/biryani/connection.rb
CHANGED
|
@@ -14,8 +14,10 @@ module Biryani
|
|
|
14
14
|
CONNECTION_PREFACE_LENGTH = CONNECTION_PREFACE.length
|
|
15
15
|
DEFAULT_HEADER_TABLE_SIZE = 4_096
|
|
16
16
|
INITIAL_CONNECTION_WINDOW_SIZE = 65_535
|
|
17
|
+
LINGER_TIMEOUT = 5
|
|
17
18
|
|
|
18
|
-
private_constant :CONNECTION_PREFACE, :CONNECTION_PREFACE_LENGTH, :DEFAULT_HEADER_TABLE_SIZE, :INITIAL_CONNECTION_WINDOW_SIZE
|
|
19
|
+
private_constant :CONNECTION_PREFACE, :CONNECTION_PREFACE_LENGTH, :DEFAULT_HEADER_TABLE_SIZE, :INITIAL_CONNECTION_WINDOW_SIZE,
|
|
20
|
+
:LINGER_TIMEOUT
|
|
19
21
|
Ractor.make_shareable(CONNECTION_PREFACE)
|
|
20
22
|
Ractor.make_shareable(CONNECTION_PREFACE_LENGTH)
|
|
21
23
|
|
|
@@ -47,32 +49,33 @@ module Biryani
|
|
|
47
49
|
|
|
48
50
|
recv_loop(io.clone)
|
|
49
51
|
select_loop(io)
|
|
52
|
+
rescue *PEER_CLOSED_ERRORS
|
|
53
|
+
# peer has already closed the connection
|
|
50
54
|
rescue StandardError => e
|
|
51
55
|
puts e.backtrace
|
|
52
|
-
|
|
56
|
+
Biryani.ignore_peer_closed do
|
|
57
|
+
self.class.do_send(io, Frame::Goaway.new(@streams_ctx.last_stream_id, ErrorCode::INTERNAL_ERROR, 'internal error'), true)
|
|
58
|
+
end
|
|
53
59
|
ensure
|
|
54
|
-
io.close_write
|
|
60
|
+
Biryani.ignore_peer_closed { io.close_write }
|
|
55
61
|
end
|
|
56
62
|
|
|
57
63
|
# @param io [IO]
|
|
58
64
|
def recv_loop(io)
|
|
59
|
-
Ractor.new(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
break
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
port_.send([:frame, obj], move: true)
|
|
68
|
-
end
|
|
65
|
+
r = Ractor.new(@port) do |port_|
|
|
66
|
+
io_ = Ractor.receive
|
|
67
|
+
Connection.recv_frames(io_, port_)
|
|
68
|
+
ensure
|
|
69
|
+
Biryani.ignore_peer_closed { io_&.close }
|
|
69
70
|
end
|
|
71
|
+
r.send(io, move: true)
|
|
70
72
|
end
|
|
71
73
|
|
|
72
74
|
# @param io [IO]
|
|
73
75
|
def select_loop(io)
|
|
74
76
|
loop do
|
|
75
|
-
|
|
77
|
+
# After EOF, no frames arrive from the peer, so only half_closed_remote streams can still progress: by the handler's response.
|
|
78
|
+
break if closed? || (@recv_done && @streams_ctx.none_half_closed_remote?)
|
|
76
79
|
|
|
77
80
|
case @port.receive
|
|
78
81
|
in :eof
|
|
@@ -82,8 +85,6 @@ module Biryani
|
|
|
82
85
|
in [:response, res, stream_id]
|
|
83
86
|
handle_response(io, res, stream_id)
|
|
84
87
|
end
|
|
85
|
-
|
|
86
|
-
break if closed?
|
|
87
88
|
end
|
|
88
89
|
end
|
|
89
90
|
|
|
@@ -144,7 +145,7 @@ module Biryani
|
|
|
144
145
|
when FrameType::DATA, FrameType::HEADERS, FrameType::PRIORITY, FrameType::RST_STREAM, FrameType::PUSH_PROMISE, FrameType::CONTINUATION
|
|
145
146
|
[ConnectionError.new(ErrorCode::PROTOCOL_ERROR, "invalid frame type #{format('0x%02x', frame.f_type)} for stream identifier 0x00")]
|
|
146
147
|
when FrameType::SETTINGS
|
|
147
|
-
obj = self.class.handle_settings(frame, @peer_settings, @
|
|
148
|
+
obj = self.class.handle_settings(frame, @peer_settings, @encoder, @streams_ctx)
|
|
148
149
|
return [] if obj.nil?
|
|
149
150
|
|
|
150
151
|
settings_ack = obj
|
|
@@ -236,6 +237,14 @@ module Biryani
|
|
|
236
237
|
# @param res [HTTP::Response]
|
|
237
238
|
# @param stream_id [Integer]
|
|
238
239
|
def handle_response(io, res, stream_id)
|
|
240
|
+
ctx = @streams_ctx[stream_id]
|
|
241
|
+
# The RST_STREAM frame fully terminates the referenced stream and
|
|
242
|
+
# causes it to enter the "closed" state. After receiving a RST_STREAM
|
|
243
|
+
# on a stream, the receiver MUST NOT send additional frames for that
|
|
244
|
+
# stream, except for PRIORITY.
|
|
245
|
+
# https://datatracker.ietf.org/doc/html/rfc9113#section-6.4-5
|
|
246
|
+
return if ctx.nil? || ctx.closed?
|
|
247
|
+
|
|
239
248
|
fragment, data = self.class.http_response(res, @encoder)
|
|
240
249
|
max_frame_size = @peer_settings[SettingsID::SETTINGS_MAX_FRAME_SIZE]
|
|
241
250
|
self.class.send_headers(io, stream_id, fragment, data.empty?, max_frame_size, @streams_ctx)
|
|
@@ -360,6 +369,35 @@ module Biryani
|
|
|
360
369
|
ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid connection preface') if s != CONNECTION_PREFACE
|
|
361
370
|
end
|
|
362
371
|
|
|
372
|
+
# @param io [IO]
|
|
373
|
+
# @param port [Ractor::Port]
|
|
374
|
+
def self.recv_frames(io, port)
|
|
375
|
+
loop do
|
|
376
|
+
obj = Frame.read(io)
|
|
377
|
+
break if obj.nil?
|
|
378
|
+
|
|
379
|
+
port.send([:frame, obj], move: true)
|
|
380
|
+
rescue Ractor::ClosedError
|
|
381
|
+
# the connection has finished before the peer closes
|
|
382
|
+
return linger(io)
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
port.send(:eof, move: true)
|
|
386
|
+
rescue Ractor::ClosedError
|
|
387
|
+
# the connection has finished after the peer closes
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# Lingering close: discard incoming data until EOF so that close does not send RST.
|
|
391
|
+
#
|
|
392
|
+
# @param io [IO]
|
|
393
|
+
def self.linger(io)
|
|
394
|
+
Timeout.timeout(LINGER_TIMEOUT) do
|
|
395
|
+
loop { io.readpartial(16_384) }
|
|
396
|
+
end
|
|
397
|
+
rescue *PEER_CLOSED_ERRORS, Timeout::Error
|
|
398
|
+
# EOF, timeout, connection reset, etc.
|
|
399
|
+
end
|
|
400
|
+
|
|
363
401
|
# @param stream_id [Integer]
|
|
364
402
|
# @param data [String]
|
|
365
403
|
# @param recv_window [Window]
|
|
@@ -368,7 +406,6 @@ module Biryani
|
|
|
368
406
|
#
|
|
369
407
|
# @return [Array<WindowUpdate>, ConnectionError, StreamError]
|
|
370
408
|
# rubocop: disable Metrics/AbcSize
|
|
371
|
-
# rubocop: disable Metrics/CyclomaticComplexity
|
|
372
409
|
def self.handle_data(stream_id, data, recv_window, streams_ctx, decoder)
|
|
373
410
|
ctx = streams_ctx[stream_id]
|
|
374
411
|
return ConnectionError.new(ErrorCode::FLOW_CONTROL_ERROR, 'DATA Frame length exceeds flow-control window size') \
|
|
@@ -377,7 +414,6 @@ module Biryani
|
|
|
377
414
|
ctx.content << data
|
|
378
415
|
if ctx.half_closed_remote?
|
|
379
416
|
obj = http_request(stream_id, ctx.fragment, ctx.content, decoder)
|
|
380
|
-
ctx << nil if obj.is_a?(StreamError) # terminate the Stream Ractor waiting for a request
|
|
381
417
|
return obj if Biryani.err?(obj)
|
|
382
418
|
|
|
383
419
|
ctx << obj
|
|
@@ -389,7 +425,6 @@ module Biryani
|
|
|
389
425
|
window_updates
|
|
390
426
|
end
|
|
391
427
|
# rubocop: enable Metrics/AbcSize
|
|
392
|
-
# rubocop: enable Metrics/CyclomaticComplexity
|
|
393
428
|
|
|
394
429
|
# @param headers [Headers]
|
|
395
430
|
# @param ctx [StreamContext]
|
|
@@ -400,7 +435,6 @@ module Biryani
|
|
|
400
435
|
ctx.fragment << headers.fragment
|
|
401
436
|
if ctx.half_closed_remote?
|
|
402
437
|
obj = http_request(headers.stream_id, ctx.fragment, ctx.content, decoder)
|
|
403
|
-
ctx << nil if obj.is_a?(StreamError) # terminate the Stream Ractor waiting for a request
|
|
404
438
|
return obj if Biryani.err?(obj)
|
|
405
439
|
|
|
406
440
|
ctx << obj
|
|
@@ -415,18 +449,21 @@ module Biryani
|
|
|
415
449
|
ctx.close
|
|
416
450
|
end
|
|
417
451
|
|
|
418
|
-
# @param
|
|
452
|
+
# @param recv_settings [Settings]
|
|
419
453
|
# @param peer_settings [Hash<Integer, Integer>]
|
|
420
|
-
# @param
|
|
454
|
+
# @param encoder [Encoder]
|
|
421
455
|
# @param streams_ctx [StreamsContext]
|
|
422
456
|
#
|
|
423
457
|
# @return [Settings]
|
|
424
|
-
def self.handle_settings(
|
|
425
|
-
return nil if
|
|
426
|
-
|
|
427
|
-
peer_settings.merge!(
|
|
428
|
-
|
|
429
|
-
|
|
458
|
+
def self.handle_settings(recv_settings, peer_settings, encoder, streams_ctx)
|
|
459
|
+
return nil if recv_settings.ack?
|
|
460
|
+
|
|
461
|
+
peer_settings.merge!(recv_settings.setting)
|
|
462
|
+
if recv_settings.setting.key?(SettingsID::SETTINGS_HEADER_TABLE_SIZE)
|
|
463
|
+
max_size_limit = [recv_settings.setting[SettingsID::SETTINGS_HEADER_TABLE_SIZE], DEFAULT_HEADER_TABLE_SIZE].min
|
|
464
|
+
peer_settings[SettingsID::SETTINGS_HEADER_TABLE_SIZE] = max_size_limit
|
|
465
|
+
encoder.max_size_limit!(max_size_limit)
|
|
466
|
+
end
|
|
430
467
|
send_initial_window_size = peer_settings[SettingsID::SETTINGS_INITIAL_WINDOW_SIZE]
|
|
431
468
|
streams_ctx.each do |ctx|
|
|
432
469
|
ctx.send_window.update!(send_initial_window_size)
|
|
@@ -475,13 +512,13 @@ module Biryani
|
|
|
475
512
|
#
|
|
476
513
|
# @return [HTTP::Request, ConnectionError, StreamError]
|
|
477
514
|
def self.http_request(stream_id, fragment, content, decoder)
|
|
478
|
-
|
|
479
|
-
return obj if Biryani.err?(obj)
|
|
480
|
-
|
|
481
|
-
fields = obj
|
|
515
|
+
fields = decoder.decode(fragment)
|
|
482
516
|
builder = HTTP::RequestBuilder.new
|
|
483
517
|
builder.fields(fields)
|
|
484
518
|
builder.build(content)
|
|
519
|
+
rescue HPACK::Error::Error => e
|
|
520
|
+
# https://datatracker.ietf.org/doc/html/rfc9113#section-4.3-3
|
|
521
|
+
ConnectionError.new(ErrorCode::COMPRESSION_ERROR, e.message)
|
|
485
522
|
rescue HTTP::Error::MalformedRequestError => e
|
|
486
523
|
# https://datatracker.ietf.org/doc/html/rfc9113#section-8.1.1-3
|
|
487
524
|
StreamError.new(ErrorCode::PROTOCOL_ERROR, stream_id, e.message)
|
data/lib/biryani/frame.rb
CHANGED
|
@@ -157,10 +157,13 @@ module Biryani
|
|
|
157
157
|
|
|
158
158
|
payload_length, f_type, flags, stream_id = read_header(s)
|
|
159
159
|
payload = io.read(payload_length)
|
|
160
|
-
return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if payload.bytesize != payload_length
|
|
160
|
+
return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if payload.nil? || payload.bytesize != payload_length
|
|
161
161
|
return Frame::Unknown.new(f_type, flags, stream_id, payload) unless FRAME_MAP.key?(f_type)
|
|
162
162
|
|
|
163
163
|
FRAME_MAP[f_type].read(payload, flags, stream_id)
|
|
164
|
+
rescue *PEER_CLOSED_ERRORS
|
|
165
|
+
# the peer has closed the connection (e.g. ECONNRESET): treat it as EOF
|
|
166
|
+
nil
|
|
164
167
|
rescue StandardError
|
|
165
168
|
ConnectionError.new(ErrorCode::FRAME_SIZE_ERROR, 'invalid frame')
|
|
166
169
|
end
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
module Biryani
|
|
2
2
|
module HPACK
|
|
3
3
|
class Decoder
|
|
4
|
-
# @param
|
|
5
|
-
def initialize(
|
|
6
|
-
@dynamic_table = DynamicTable.new(
|
|
4
|
+
# @param max_size_limit [Integer]
|
|
5
|
+
def initialize(max_size_limit)
|
|
6
|
+
@dynamic_table = DynamicTable.new(max_size_limit)
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
# @param s [String]
|
|
10
10
|
#
|
|
11
|
+
# @raise [HPACK::Error::Error]
|
|
12
|
+
#
|
|
11
13
|
# @return [Array]
|
|
12
14
|
def decode(s)
|
|
13
15
|
Fields.decode(s.force_encoding(Encoding::ASCII_8BIT), @dynamic_table)
|
|
14
16
|
end
|
|
15
17
|
|
|
16
|
-
# @param
|
|
17
|
-
def
|
|
18
|
-
@dynamic_table.
|
|
18
|
+
# @param new_max_size_limit [Integer]
|
|
19
|
+
def max_size_limit!(new_max_size_limit)
|
|
20
|
+
@dynamic_table.max_size_limit!(new_max_size_limit)
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
module Biryani
|
|
2
2
|
module HPACK
|
|
3
3
|
class DynamicTable
|
|
4
|
-
attr_reader :size, :
|
|
4
|
+
attr_reader :size, :max_size, :max_size_limit
|
|
5
5
|
|
|
6
|
-
# @param
|
|
7
|
-
def initialize(
|
|
6
|
+
# @param max_size_limit [Integer]
|
|
7
|
+
def initialize(max_size_limit)
|
|
8
8
|
@table = []
|
|
9
9
|
@size = 0
|
|
10
|
-
@
|
|
10
|
+
@max_size = max_size_limit
|
|
11
|
+
@max_size_limit = max_size_limit
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
# @return [Integer]
|
|
@@ -20,7 +21,7 @@ module Biryani
|
|
|
20
21
|
def store(name, value)
|
|
21
22
|
@table.unshift([name, value])
|
|
22
23
|
@size += name.bytesize + value.bytesize + 32
|
|
23
|
-
chomp!(@
|
|
24
|
+
chomp!(@max_size)
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# @param name [String]
|
|
@@ -51,7 +52,7 @@ module Biryani
|
|
|
51
52
|
#
|
|
52
53
|
# @return [Array] [name, value]
|
|
53
54
|
def entry_at(index)
|
|
54
|
-
raise Error::HPACKDecodeError if index.zero? || index > STATIC_TABLE_SIZE + count_entries
|
|
55
|
+
raise Error::HPACKDecodeError, "invalid index #{index}" if index.zero? || index > STATIC_TABLE_SIZE + count_entries
|
|
55
56
|
|
|
56
57
|
if index <= STATIC_TABLE_SIZE
|
|
57
58
|
STATIC_TABLE[index - 1]
|
|
@@ -67,19 +68,27 @@ module Biryani
|
|
|
67
68
|
entry_at(index)[0]
|
|
68
69
|
end
|
|
69
70
|
|
|
70
|
-
# @param
|
|
71
|
-
def
|
|
72
|
-
|
|
71
|
+
# @param new_max_size_limit [Integer]
|
|
72
|
+
def max_size_limit!(new_max_size_limit)
|
|
73
|
+
max_size!(new_max_size_limit)
|
|
74
|
+
@max_size_limit = new_max_size_limit
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @param new_max_size [Integer]
|
|
78
|
+
def max_size!(new_max_size)
|
|
79
|
+
chomp!(new_max_size)
|
|
80
|
+
@max_size = new_max_size
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
# @param max_size [Integer]
|
|
86
|
+
def chomp!(max_size)
|
|
87
|
+
while @size > max_size
|
|
73
88
|
n, v = @table.pop
|
|
74
89
|
@size -= n.bytesize + v.bytesize + 32
|
|
75
90
|
end
|
|
76
91
|
end
|
|
77
|
-
|
|
78
|
-
# @param new_limit [Integer]
|
|
79
|
-
def limit!(new_limit)
|
|
80
|
-
chomp!(new_limit)
|
|
81
|
-
@limit = new_limit
|
|
82
|
-
end
|
|
83
92
|
end
|
|
84
93
|
end
|
|
85
94
|
end
|
|
@@ -1,21 +1,40 @@
|
|
|
1
1
|
module Biryani
|
|
2
2
|
module HPACK
|
|
3
3
|
class Encoder
|
|
4
|
-
# @param
|
|
5
|
-
def initialize(
|
|
6
|
-
@dynamic_table = DynamicTable.new(
|
|
4
|
+
# @param max_size_limit [Integer]
|
|
5
|
+
def initialize(max_size_limit)
|
|
6
|
+
@dynamic_table = DynamicTable.new(max_size_limit)
|
|
7
|
+
@unsignaled_min_max_size = nil
|
|
7
8
|
end
|
|
8
9
|
|
|
9
10
|
# @param fields [Array]
|
|
10
11
|
#
|
|
11
12
|
# @return [String]
|
|
12
13
|
def encode(fields)
|
|
13
|
-
Fields.encode(fields, @dynamic_table).force_encoding(Encoding::ASCII_8BIT)
|
|
14
|
+
(dynamic_table_size_update + Fields.encode(fields, @dynamic_table)).force_encoding(Encoding::ASCII_8BIT)
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
# @param
|
|
17
|
-
def
|
|
18
|
-
@dynamic_table.
|
|
17
|
+
# @param new_max_size_limit [Integer]
|
|
18
|
+
def max_size_limit!(new_max_size_limit)
|
|
19
|
+
@dynamic_table.max_size_limit!(new_max_size_limit)
|
|
20
|
+
@unsignaled_min_max_size = [@unsignaled_min_max_size || new_max_size_limit, new_max_size_limit].min
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
# signal @unsignaled_min_max_size, the smallest max_size not yet signaled since the last field block,
|
|
26
|
+
# before the final max_size so that the decoder evicts the same entries.
|
|
27
|
+
# https://datatracker.ietf.org/doc/html/rfc7541#section-4.2
|
|
28
|
+
# https://datatracker.ietf.org/doc/html/rfc7541#section-6.3
|
|
29
|
+
#
|
|
30
|
+
# @return [String]
|
|
31
|
+
def dynamic_table_size_update
|
|
32
|
+
return ''.b if @unsignaled_min_max_size.nil?
|
|
33
|
+
|
|
34
|
+
updates = [@unsignaled_min_max_size, @dynamic_table.max_size].uniq
|
|
35
|
+
@unsignaled_min_max_size = nil
|
|
36
|
+
|
|
37
|
+
updates.map { |i| Integer.encode(i, 5, 0b00100000) }.join.b
|
|
19
38
|
end
|
|
20
39
|
end
|
|
21
40
|
end
|
data/lib/biryani/hpack/field.rb
CHANGED
|
@@ -215,12 +215,13 @@ module Biryani
|
|
|
215
215
|
# @return [nil]
|
|
216
216
|
# @return [Integer]
|
|
217
217
|
def self.decode_dynamic_table_size_update(io, cursor, dynamic_table)
|
|
218
|
-
raise Error::HPACKDecodeError
|
|
218
|
+
raise Error::HPACKDecodeError, 'misplaced dynamic table size update' \
|
|
219
|
+
unless cursor.zero? || (io.get_value(:U8, 0) & 0b00100000).positive? && Integer.decode(io, 5, 0)[1] == cursor
|
|
219
220
|
|
|
220
221
|
max_size, c = Integer.decode(io, 5, cursor)
|
|
221
|
-
raise Error::HPACKDecodeError if max_size > dynamic_table.
|
|
222
|
+
raise Error::HPACKDecodeError, 'dynamic table size update exceeds the limit' if max_size > dynamic_table.max_size_limit
|
|
222
223
|
|
|
223
|
-
dynamic_table.
|
|
224
|
+
dynamic_table.max_size!(max_size)
|
|
224
225
|
[nil, c]
|
|
225
226
|
end
|
|
226
227
|
|
|
@@ -550,7 +550,7 @@ module Biryani
|
|
|
550
550
|
(length * 8).times do |i|
|
|
551
551
|
bits_len += 1
|
|
552
552
|
bits += 1 if (bytes[i / 8] & (1 << 7 - (i % 8))).positive?
|
|
553
|
-
raise Error::HuffmanDecodeError if bits == EOS
|
|
553
|
+
raise Error::HuffmanDecodeError, 'EOS in huffman-encoded string' if bits == EOS
|
|
554
554
|
|
|
555
555
|
if (chr = DECODE_TABLE[[bits, bits_len]])
|
|
556
556
|
res << chr
|
|
@@ -560,7 +560,7 @@ module Biryani
|
|
|
560
560
|
bits <<= 1
|
|
561
561
|
end
|
|
562
562
|
end
|
|
563
|
-
raise Error::HuffmanDecodeError if bits_len.positive? && bits != (2 << bits_len) - 2 || bits_len >= 8
|
|
563
|
+
raise Error::HuffmanDecodeError, 'invalid huffman padding' if bits_len.positive? && bits != (2 << bits_len) - 2 || bits_len >= 8
|
|
564
564
|
|
|
565
565
|
res
|
|
566
566
|
end
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
module Biryani
|
|
2
2
|
module HPACK
|
|
3
3
|
module Integer
|
|
4
|
+
# https://datatracker.ietf.org/doc/html/rfc7541#section-7.4
|
|
5
|
+
MAX_DECODED_INTEGER = (1 << 32) - 1
|
|
6
|
+
private_constant :MAX_DECODED_INTEGER
|
|
7
|
+
|
|
4
8
|
# if I < 2^N - 1, encode I on N bits
|
|
5
9
|
# else
|
|
6
10
|
# encode (2^N - 1) on N bits
|
|
@@ -44,10 +48,14 @@ module Biryani
|
|
|
44
48
|
# @param cursor [Integer]
|
|
45
49
|
# @param n [Integer]
|
|
46
50
|
#
|
|
51
|
+
# @raise [HPACKDecodeError]
|
|
52
|
+
#
|
|
47
53
|
# @return [Integer]
|
|
48
54
|
# @return [Integer]
|
|
49
55
|
def self.decode(io, n, cursor)
|
|
50
56
|
limit = (1 << n) - 1
|
|
57
|
+
raise Error::HPACKDecodeError, 'integer exceeds field block' if cursor >= io.size
|
|
58
|
+
|
|
51
59
|
h = io.get_value(:U8, cursor)
|
|
52
60
|
return [h & limit, cursor + 1] if (h & limit) != limit
|
|
53
61
|
|
|
@@ -55,8 +63,11 @@ module Biryani
|
|
|
55
63
|
c = cursor + 1
|
|
56
64
|
i = 0
|
|
57
65
|
loop do
|
|
66
|
+
raise Error::HPACKDecodeError, 'integer exceeds field block' if c + i >= io.size
|
|
67
|
+
|
|
58
68
|
byte = io.get_value(:U8, c + i)
|
|
59
69
|
res += (byte & 127) << (i * 7)
|
|
70
|
+
raise Error::HPACKDecodeError, 'integer overflow' if res > MAX_DECODED_INTEGER
|
|
60
71
|
|
|
61
72
|
break if (byte & 128).zero?
|
|
62
73
|
|
data/lib/biryani/hpack/string.rb
CHANGED
|
@@ -26,11 +26,18 @@ module Biryani
|
|
|
26
26
|
# @param io [IO::Buffer]
|
|
27
27
|
# @param cursor [Integer]
|
|
28
28
|
#
|
|
29
|
+
# @raise [HPACKDecodeError]
|
|
30
|
+
# @raise [HuffmanDecodeError]
|
|
31
|
+
#
|
|
29
32
|
# @return [String]
|
|
30
33
|
# @return [Integer]
|
|
31
34
|
def self.decode(io, cursor)
|
|
35
|
+
raise Error::HPACKDecodeError, 'string exceeds field block' if cursor >= io.size
|
|
36
|
+
|
|
32
37
|
h = (io.get_value(:U8, cursor) & 0b10000000).positive?
|
|
33
38
|
len, c = Integer.decode(io, 7, cursor)
|
|
39
|
+
raise Error::HPACKDecodeError, 'string exceeds field block' if c + len > io.size
|
|
40
|
+
|
|
34
41
|
return [Huffman.decode(io, c, len), c + len] if h
|
|
35
42
|
|
|
36
43
|
[io.get_string(c, len), c + len]
|
data/lib/biryani/server.rb
CHANGED
|
@@ -8,11 +8,15 @@ module Biryani
|
|
|
8
8
|
# @param socket [Socket]
|
|
9
9
|
def run(socket)
|
|
10
10
|
loop do
|
|
11
|
-
|
|
11
|
+
io = socket.accept
|
|
12
|
+
r = Ractor.new(@proc) do |proc|
|
|
13
|
+
io_ = Ractor.receive
|
|
12
14
|
conn = Connection.new(proc)
|
|
13
|
-
conn.serve(
|
|
14
|
-
|
|
15
|
+
conn.serve(io_)
|
|
16
|
+
ensure
|
|
17
|
+
Biryani.ignore_peer_closed { io_&.close }
|
|
15
18
|
end
|
|
19
|
+
r.send(io, move: true)
|
|
16
20
|
end
|
|
17
21
|
end
|
|
18
22
|
end
|
data/lib/biryani/stream.rb
CHANGED
|
@@ -7,18 +7,21 @@ module Biryani
|
|
|
7
7
|
# @param proc [Proc]
|
|
8
8
|
def initialize(tx, stream_id, proc)
|
|
9
9
|
@rx = Ractor.new(tx, stream_id, proc) do |tx, stream_id, proc|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
req = Ractor.recv
|
|
11
|
+
res = HTTP::Response.default
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
begin
|
|
14
|
+
proc.call(req, res)
|
|
15
|
+
res.validate
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
puts e.backtrace
|
|
18
|
+
res = HTTP::Response.internal_server_error
|
|
19
|
+
end
|
|
20
20
|
|
|
21
|
+
begin
|
|
21
22
|
tx.send([:response, res, stream_id], move: true)
|
|
23
|
+
rescue Ractor::ClosedError
|
|
24
|
+
# the Connection has already finished, so discard the response
|
|
22
25
|
end
|
|
23
26
|
end
|
|
24
27
|
end
|
|
@@ -50,6 +50,11 @@ module Biryani
|
|
|
50
50
|
@h.values.filter(&:active?).length
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
+
# @return [Boolean]
|
|
54
|
+
def none_half_closed_remote?
|
|
55
|
+
@h.each_value.none?(&:half_closed_remote?)
|
|
56
|
+
end
|
|
57
|
+
|
|
53
58
|
# @return [Integer, nil]
|
|
54
59
|
def receiving_continuation_stream_id
|
|
55
60
|
@h.find { |_, ctx| ctx.receiving_continuation? }&.first
|
|
@@ -109,7 +114,9 @@ module Biryani
|
|
|
109
114
|
# @param proc [Proc]
|
|
110
115
|
# @param tx [Ractor::Port]
|
|
111
116
|
def initialize(stream_id, send_initial_window_size, recv_initial_window_size, proc, tx)
|
|
112
|
-
@
|
|
117
|
+
@stream_id = stream_id
|
|
118
|
+
@proc = proc
|
|
119
|
+
@tx = tx
|
|
113
120
|
@send_window = Window.new(send_initial_window_size)
|
|
114
121
|
@recv_window = Window.new(recv_initial_window_size)
|
|
115
122
|
@fragment = ''.b
|
|
@@ -119,7 +126,7 @@ module Biryani
|
|
|
119
126
|
|
|
120
127
|
# @param req [HTTP::Request]
|
|
121
128
|
def <<(req)
|
|
122
|
-
@
|
|
129
|
+
Stream.new(@tx, @stream_id, @proc).rx.send(req, move: true)
|
|
123
130
|
end
|
|
124
131
|
|
|
125
132
|
def close
|
data/lib/biryani/utils.rb
CHANGED
|
@@ -20,4 +20,13 @@ module Biryani
|
|
|
20
20
|
obj
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
# Errors raised when the peer has already closed the connection (EOF, EPIPE, ECONNRESET, etc.)
|
|
25
|
+
PEER_CLOSED_ERRORS = Ractor.make_shareable([IOError, SystemCallError])
|
|
26
|
+
|
|
27
|
+
def self.ignore_peer_closed
|
|
28
|
+
yield
|
|
29
|
+
rescue *PEER_CLOSED_ERRORS
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
23
32
|
end
|
data/lib/biryani/version.rb
CHANGED
data/lib/biryani.rb
CHANGED