Skip to content

Commit

Permalink
Better handling of headers/trailers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 7, 2020
1 parent 59e7bea commit c112bff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/protocol/http1/body/chunked.rb
Expand Up @@ -90,8 +90,6 @@ def read_line
end

def read_trailers
@headers.trailers!

while line = read_line
# Empty line indicates end of headers:
break if line.empty?
Expand Down
12 changes: 10 additions & 2 deletions lib/protocol/http1/connection.rb
Expand Up @@ -140,13 +140,17 @@ def write_request(authority, method, path, version, headers)
@stream.write("host: #{authority}\r\n")

write_headers(headers)

headers.trailers!
end

def write_response(version, status, headers, reason = Reason::DESCRIPTIONS[status])
# Safari WebSockets break if no reason is given:
@stream.write("#{version} #{status} #{reason}\r\n")

write_headers(headers)

headers.trailers!
end

def write_headers(headers)
Expand Down Expand Up @@ -357,12 +361,16 @@ def write_body_and_close(body, head)
end

def write_body(version, body, head = false, trailers = nil)
if body.nil? or body.empty?
if body.nil?
write_connection_header(version)
write_empty_body(body)
elsif length = body.length
elsif length = body.length and (VERSION != HTTP11 && trailers.nil?)
write_connection_header(version)
write_fixed_length_body(body, length, head)
elsif body.empty?
# Even thought this code is the same as the first clause `body.nil?`, HEAD responses have an empty body but still carry a content length. `write_fixed_length_body` takes care of this appropriately.
write_connection_header(version)
write_empty_body(body)
elsif @persistent and version == HTTP11
write_connection_header(version)
# We specifically ensure that non-persistent connections do not use chunked response, so that hijacking works as expected.
Expand Down

0 comments on commit c112bff

Please sign in to comment.