def parse_response_header(header, version, status)
@response_header.raw = header
header.each do |key, val|
@response_header[key.upcase.gsub('-','_')] = val
end
@response_header.http_version = version.join('.')
@response_header.http_status = status
@response_header.http_reason = CODE[status] || 'unknown'
@headers.call(@response_header) if @headers
unless @response_header.http_status and @response_header.http_reason
@state = :invalid
on_error "no HTTP response"
return
end
if @response_header.cookie && @req.pass_cookies
[@response_header.cookie].flatten.each {|cookie| @cookiejar.set(cookie, @req.uri)}
end
if @response_header.location
begin
location = Addressable::URI.parse(@response_header.location)
if location.relative?
location = @req.uri.join(location)
@response_header[LOCATION] = location.to_s
else
raise if location.host.nil?
end
rescue
on_error "Location header format error"
return
end
end
if @req.method == "HEAD"
@state = :finished
return
end
if @response_header.chunked_encoding?
@state = :chunk_header
elsif @response_header.content_length
@state = :body
else
@state = :body
end
if @req.decoding && decoder_class = HttpDecoders.decoder_for_encoding(response_header[CONTENT_ENCODING])
begin
@content_decoder = decoder_class.new do |s| on_decoded_body_data(s) end
rescue HttpDecoders::DecoderError
on_error "Content-decoder error"
end
end
content_type = [response_header[CONTENT_TYPE]].flatten.first
if String.method_defined?(:force_encoding) && /;\s*charset=\s*(.+?)\s*(;|$)/.match(content_type)
@content_charset = Encoding.find($1.gsub(/^\"|\"$/, '')) rescue Encoding.default_external
end
end