Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle explicit chunked encoding responses #15092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading