Skip to content

Commit c978d25

Browse files
committed
Don't set Content-Length for GET requests without a BODY
The HTTP spec says (https://tools.ietf.org/html/rfc7230#section-3.3.2): > A user agent SHOULD NOT send a Content-Length header field when the request message does not contain a payload body and the method semantics do not anticipate such a body
1 parent 2df5e46 commit c978d25

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

lib/http/request/writer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def connect_through_proxy
4747
# Adds the headers to the header array for the given request body we are working
4848
# with
4949
def add_body_type_headers
50-
return if @headers[Headers::CONTENT_LENGTH] || chunked?
50+
return if @headers[Headers::CONTENT_LENGTH] || chunked? || (
51+
@request_header[0].start_with?("GET ") && @body.source.nil?
52+
)
5153

5254
@request_header << "#{Headers::CONTENT_LENGTH}: #{@body.size}"
5355
end

spec/lib/http/request/writer_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@
5050
context "when body is empty" do
5151
let(:body) { HTTP::Request::Body.new(nil) }
5252

53-
it "doesn't write anything to the socket and sets Content-Length" do
53+
it "doesn't write anything to the socket and doesn't set Content-Length" do
5454
writer.stream
5555
expect(io.string).to eq [
56-
"#{headerstart}\r\n",
57-
"Content-Length: 0\r\n\r\n"
56+
"#{headerstart}\r\n\r\n"
5857
].join
5958
end
6059
end

0 commit comments

Comments
 (0)