File tree 2 files changed +36
-3
lines changed
2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -347,5 +347,33 @@ module HTTP
347
347
request.host_with_port.should eq(" host.example.org:3000" )
348
348
end
349
349
end
350
+
351
+ it " doesn't raise on request with multiple Content_length headers" do
352
+ io = IO ::Memory .new <<-REQ
353
+ GET / HTTP/1.1
354
+ Host: host
355
+ Content-Length: 5
356
+ Content-Length: 5
357
+ Content-Type: text/plain
358
+
359
+ abcde
360
+ REQ
361
+ HTTP ::Request .from_io(io)
362
+ end
363
+
364
+ it " raises if request has multiple and differing content-length headers" do
365
+ io = IO ::Memory .new <<-REQ
366
+ GET / HTTP/1.1
367
+ Host: host
368
+ Content-Length: 5
369
+ Content-Length: 6
370
+ Content-Type: text/plain
371
+
372
+ abcde
373
+ REQ
374
+ expect_raises(ArgumentError ) do
375
+ HTTP ::Request .from_io(io)
376
+ end
377
+ end
350
378
end
351
379
end
Original file line number Diff line number Diff line change @@ -26,8 +26,7 @@ module HTTP
26
26
body = nil
27
27
if body_type.prohibited?
28
28
body = nil
29
- elsif content_length = headers[" Content-Length" ]?
30
- content_length = content_length.to_u64
29
+ elsif content_length = content_length(headers)
31
30
if content_length != 0
32
31
# Don't create IO for Content-Length == 0
33
32
body = FixedLengthContent .new(io, content_length)
@@ -166,7 +165,13 @@ module HTTP
166
165
167
166
# :nodoc:
168
167
def self.content_length (headers )
169
- headers[" Content-Length" ]?.try & .to_u64?
168
+ length_headers = headers.get? " Content-Length"
169
+ return nil unless length_headers
170
+ first_header = length_headers[0 ]
171
+ if length_headers.size > 1 && length_headers.any? { |header | header != first_header }
172
+ raise ArgumentError .new(" Multiple Content-Length headers received did not match: #{ length_headers } " )
173
+ end
174
+ first_header.to_u64
170
175
end
171
176
172
177
# :nodoc:
You can’t perform that action at this time.
0 commit comments