Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Net::HTTPHeader#content_length
() -> (nil | ::Integer)
content_length
lib/net/http/header.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/lib/net/http/header.rb, line 639
def content_length
values = @header['content-length']
return nil if values.nil?
lengths = []
values.each do |value|
value = value.strip
if value.empty?
raise Net::HTTPHeaderSyntaxError,
"empty Content-Length value"
end
lengths.concat(value.split(/\s*,\s*/, -1))
end
if lengths.uniq.size != 1
raise Net::HTTPHeaderSyntaxError,
"Content-Length has multiple different values: " +
values.join(", ")
end
length = lengths.first
unless /\A\d+\z/.match?(length)
raise Net::HTTPHeaderSyntaxError,
"wrong Content-Length format: #{length}"
end
length.to_i
end
Returns the value of field 'Content-Length' as an integer, or nil if there is no such field; see Content-Length request header:
Example
res = Net::HTTP.get_response(hostname, '/nosuch/1') res.content_length # => 2 res = Net::HTTP.get_response(hostname, '/todos/1') res.content_length # => nil
The value must consist of digits only. Multiple 'Content-Length' values are accepted only when they are all identical; otherwise Net::HTTPHeaderSyntaxError is raised. See RFC 9110 Section 8.6.
See also
#content_length=
#connection_keep_alive?
#connection_close?
#content_range
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0