Skip to content

Commit d1dd994

Browse files
committed
Initial (working?) support for request/response trailers.
1 parent 027186e commit d1dd994

File tree

13 files changed

+123
-20
lines changed

13 files changed

+123
-20
lines changed

async-http.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
2222
spec.add_dependency("async-pool", "~> 0.2")
2323

2424
spec.add_dependency("protocol-http", "~> 0.16.0")
25-
spec.add_dependency("protocol-http1", "~> 0.10.0")
25+
spec.add_dependency("protocol-http1", "~> 0.11.0")
2626
spec.add_dependency("protocol-http2", "~> 0.13.0")
2727

2828
# spec.add_dependency("openssl")

bake/async/http.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ def fetch(url, method:)
6767
end
6868

6969
response.finish
70+
71+
if trailers = response.headers.trailers
72+
trailers.each do |key, value|
73+
terminal.print_line(
74+
:key, key.rjust(align), :reset, ": ", :value, value.inspect
75+
)
76+
end
77+
end
78+
7079
internet.close
7180
end
7281
end

lib/async/http/protocol/http1.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def self.bidirectional?
3333
true
3434
end
3535

36+
def self.trailers?
37+
true
38+
end
39+
3640
def self.client(peer)
3741
stream = IO::Stream.new(peer, sync: false)
3842

lib/async/http/protocol/http1/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def call(request, task: Task.current)
6161
subtask.annotate("Streaming body.")
6262

6363
# Once we start writing the body, we can't recover if the request fails. That's because the body might be generated dynamically, streaming, etc.
64-
write_body(@version, body)
64+
write_body(@version, body, false, request.trailers)
6565
end
6666
end
6767
elsif protocol = request.protocol

lib/async/http/protocol/http1/server.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ def each(task: Task.current)
8484
body.call(stream)
8585
else
8686
head = request.head?
87+
trailers = response.trailers
8788

8889
request = nil unless body
8990
response = nil
9091

91-
write_body(@version, body, head)
92+
write_body(@version, body, head, trailers)
9293
end
9394
else
9495
# If the request failed to generate a response, it was an internal server error:

lib/async/http/protocol/http10.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def self.bidirectional?
3232
false
3333
end
3434

35+
def self.trailers?
36+
false
37+
end
38+
3539
def self.client(peer)
3640
stream = IO::Stream.new(peer, sync: false)
3741

lib/async/http/protocol/http11.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def self.bidirectional?
3232
true
3333
end
3434

35+
def self.trailers?
36+
true
37+
end
38+
3539
def self.client(peer)
3640
stream = IO::Stream.new(peer, sync: false)
3741

lib/async/http/protocol/http2.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def self.bidirectional?
3333
true
3434
end
3535

36+
def self.trailers?
37+
true
38+
end
39+
3640
CLIENT_SETTINGS = {
3741
::Protocol::HTTP2::Settings::ENABLE_PUSH => 0,
3842
::Protocol::HTTP2::Settings::MAXIMUM_FRAME_SIZE => 0x100000,

lib/async/http/protocol/http2/output.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,26 @@ module HTTP
2727
module Protocol
2828
module HTTP2
2929
class Output
30-
def self.for(stream, body)
31-
output = self.new(stream, body)
30+
def self.for(stream, body, trailers)
31+
output = self.new(stream, body, trailers)
3232

3333
output.start
3434

3535
return output
3636
end
3737

38-
def initialize(stream, body)
38+
def initialize(stream, body, trailers = nil)
3939
@stream = stream
4040
@body = body
41+
@trailers = trailers
42+
4143
@task = nil
4244

4345
@window_updated = Async::Condition.new
4446
end
4547

48+
attr :trailers
49+
4650
def start(parent: Task.current)
4751
raise "Task already started!" if @task
4852

lib/async/http/protocol/http2/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def send_response(response)
184184

185185
if body = response.body and !self.head?
186186
@stream.send_headers(nil, headers)
187-
@stream.send_body(body)
187+
@stream.send_body(body, response.trailers)
188188
else
189189
# Ensure the response body is closed if we are ending the stream:
190190
response.close

0 commit comments

Comments
 (0)