lib/curb-fu/response.rb in curb-fu-0.4.4 vs lib/curb-fu/response.rb in curb-fu-0.6.0
- old
+ new
@@ -33,18 +33,45 @@
def parse_headers(header_string)
header_lines = header_string.split($/)
header_lines.shift
header_lines.inject({}) do |hsh, line|
whole_enchillada, key, value = /^(.*?):\s*(.*)$/.match(line.chomp).to_a
- hsh[key] = value unless whole_enchillada.nil?
+ unless whole_enchillada.nil?
+ # note: headers with multiple instances should have multiple values in the headers hash
+ hsh[key] = hsh[key] ? [hsh[key]].flatten << value : value
+ end
hsh
end
end
def to_hash
{ :status => status, :body => body, :headers => headers }
end
-
+
+ def content_length
+ if ( header_value = self['Content-Length'] )
+ header_value.to_i
+ end
+ end
+
+ def content_type
+ if ( header_value = self['Content-Type'] )
+ header_value.split(';').first
+ end
+ end
+
+ def get_fields(key)
+ if ( match = @headers.find{|k,v| k.downcase == key.downcase} )
+ [match.last].flatten
+ else
+ []
+ end
+ end
+
+ def [](key)
+ get_fields(key).last
+ end
+
def set_response_type(status)
case status
when 100..199 then
self.extend CurbFu::Response::Information
case self.status