Skip to content

Commit

Permalink
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions spec/std/http/client/response_spec.cr
Original file line number Diff line number Diff line change
@@ -113,6 +113,16 @@ class HTTP::Client
response.body.should eq("")
end

it "parses 204 response without body but Content-Length == 0 (#2512)" do
response = Response.from_io(MemoryIO.new("HTTP/1.1 204 OK\r\nContent-Type: text/plain\r\nContent-Length: 0\r\n\r\n"))
response.version.should eq("HTTP/1.1")
response.status_code.should eq(204)
response.status_message.should eq("OK")
response.headers["content-type"].should eq("text/plain")
response.headers["content-length"].should eq("0")
response.body.should eq("")
end

it "doesn't sets content length for 1xx, 204 or 304" do
[100, 101, 204, 304].each do |status|
response = Response.new(status)
@@ -211,6 +221,12 @@ class HTTP::Client
response.charset.should eq("UTF-8")
end

it "creates Response with status code 204, no body and Content-Length == 0 (#2512)" do
response = Response.new(204, version: "HTTP/1.0", body: "", headers: HTTP::Headers{"Content-Length": "0"})
response.status_code.should eq(204)
response.body.should eq("")
end

describe "success?" do
it "returns true for the 2xx" do
response = Response.new(200)
2 changes: 1 addition & 1 deletion src/http/client/response.cr
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ class HTTP::Client::Response
if Response.mandatory_body?(@status_code)
@body = "" unless @body || @body_io
else
if @body || @body_io
if (@body || @body_io) && (headers["Content-Length"]? != "0")
raise ArgumentError.new("status #{status_code} should not have a body")
end
end
6 changes: 5 additions & 1 deletion src/http/common.cr
Original file line number Diff line number Diff line change
@@ -21,7 +21,11 @@ module HTTP
if body_type.prohibited?
body = nil
elsif content_length = headers["Content-Length"]?
body = FixedLengthContent.new(io, content_length.to_u64)
content_length = content_length.to_u64
if content_length != 0
# Don't create IO for Content-Length == 0
body = FixedLengthContent.new(io, content_length)
end
elsif headers["Transfer-Encoding"]? == "chunked"
body = ChunkedContent.new(io)
elsif body_type.mandatory?

0 comments on commit c1affec

Please sign in to comment.