def parse_response_header
return false unless parse_header(@response_header)
unless @response_header.http_status and @response_header.http_reason
@state = :invalid
on_error "no HTTP response"
return false
end
if @response_header.location
begin
location = Addressable::URI.parse @response_header.location
if location.relative?
location = (@uri.join location).to_s
@response_header[LOCATION] = location
end
rescue
on_error "Location header format error"
return false
end
end
if @method == "HEAD"
@state = :finished
on_request_complete
end
if websocket?
if @response_header.status == 101
@state = :websocket
succeed
else
fail "websocket handshake failed"
end
elsif @response_header.chunked_encoding?
@state = :chunk_header
elsif @response_header.content_length
@state = :body
@bytes_remaining = @response_header.content_length
else
@state = :body
@bytes_remaining = nil
end
if 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
true
end