Skip to content

Commit

Permalink
Better handle explicit chunked encoding responses (#15092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Oct 20, 2024
1 parent 57017f6 commit 7cd5f73
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions spec/std/http/server/response_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ describe HTTP::Server::Response do
io.to_s.should eq("HTTP/1.1 304 Not Modified\r\nContent-Length: 5\r\n\r\n")
end

it "allow explicitly configuring a `Transfer-Encoding` response" do
io = IO::Memory.new
response = Response.new(io)
response.headers["Transfer-Encoding"] = "chunked"
response.print "Hello"
response.close
io.to_s.should eq("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n0\r\n\r\n")
end

it "prints less then buffer's size" do
io = IO::Memory.new
response = Response.new(io)
Expand Down
6 changes: 4 additions & 2 deletions src/http/server/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ class HTTP::Server
private def unbuffered_write(slice : Bytes) : Nil
return if slice.empty?

unless response.wrote_headers?
if response.headers["Transfer-Encoding"]? == "chunked"
@chunked = true
elsif !response.wrote_headers?
if response.version != "HTTP/1.0" && !response.headers.has_key?("Content-Length")
response.headers["Transfer-Encoding"] = "chunked"
@chunked = true
Expand Down Expand Up @@ -289,7 +291,7 @@ class HTTP::Server
status = response.status
set_content_length = !(status.not_modified? || status.no_content? || status.informational?)

if !response.wrote_headers? && !response.headers.has_key?("Content-Length") && set_content_length
if !response.wrote_headers? && !response.headers.has_key?("Transfer-Encoding") && !response.headers.has_key?("Content-Length") && set_content_length
response.content_length = @out_count
end

Expand Down

0 comments on commit 7cd5f73

Please sign in to comment.