lib/httpx/parser/http1.rb in httpx-0.9.0 vs lib/httpx/parser/http1.rb in httpx-0.10.0
- old
+ new
@@ -7,14 +7,13 @@
class HTTP1
VERSIONS = %w[1.0 1.1].freeze
attr_reader :status_code, :http_version, :headers
- def initialize(observer, header_separator: ":")
+ def initialize(observer)
@observer = observer
@state = :idle
- @header_separator = header_separator
@buffer = "".b
@headers = {}
end
def <<(chunk)
@@ -38,29 +37,29 @@
end
private
def parse
- state = @state
- case @state
- when :idle
- parse_headline
- when :headers
- parse_headers
- when :trailers
- parse_headers
- when :data
- parse_data
+ loop do
+ state = @state
+ case @state
+ when :idle
+ parse_headline
+ when :headers, :trailers
+ parse_headers
+ when :data
+ parse_data
+ end
+ return if @buffer.empty? || state == @state
end
- parse if !@buffer.empty? && state != @state
end
def parse_headline
idx = @buffer.index("\n")
return unless idx
- (m = %r{\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)(?:\s+(.*))?}in.match(@buffer)) ||
+ (m = %r{\AHTTP(?:/(\d+\.\d+))?\s+(\d\d\d)(?:\s+(.*))?}in.match(@buffer)) ||
raise(Error, "wrong head line format")
version, code, _ = m.captures
raise(Error, "unsupported HTTP version (HTTP/#{version})") unless VERSIONS.include?(version)
@http_version = version.split(".").map(&:to_i)
@@ -89,15 +88,13 @@
headers.clear
when :trailers
@observer.on_trailers(headers)
headers.clear
nextstate(:complete)
- else
- raise Error, "wrong header format"
end
return
end
- separator_index = line.index(@header_separator)
+ separator_index = line.index(":")
raise Error, "wrong header format" unless separator_index
key = line[0..separator_index - 1]
raise Error, "wrong header format" if key.start_with?("\s", "\t")