lib/httpx/parser/http1.rb in httpx-0.16.1 vs lib/httpx/parser/http1.rb in httpx-0.17.0
- old
+ new
@@ -58,11 +58,11 @@
return unless idx
(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)
+ raise(Error, "unsupported HTTP version (HTTP/#{version})") unless version && VERSIONS.include?(version)
@http_version = version.split(".").map(&:to_i)
@status_code = code.to_i
raise(Error, "wrong status code (#{@status_code})") unless (100..599).cover?(@status_code)
@@ -70,13 +70,18 @@
nextstate(:headers)
end
def parse_headers
headers = @headers
- while (idx = @buffer.index("\n"))
- line = @buffer.byteslice(0..idx).sub(/\s+\z/, "")
- @buffer = @buffer.byteslice((idx + 1)..-1)
+ buffer = @buffer
+
+ while (idx = buffer.index("\n"))
+ line = buffer.byteslice(0..idx)
+ raise Error, "wrong header format" if line.start_with?("\s", "\t")
+
+ line.lstrip!
+ buffer = @buffer = buffer.byteslice((idx + 1)..-1)
if line.empty?
case @state
when :headers
prepare_data(headers)
@observer.on_headers(headers)
@@ -95,12 +100,11 @@
end
separator_index = line.index(":")
raise Error, "wrong header format" unless separator_index
key = line.byteslice(0..(separator_index - 1))
- raise Error, "wrong header format" if key.start_with?("\s", "\t")
- key.strip!
+ key.rstrip! # was lstripped previously!
value = line.byteslice((separator_index + 1)..-1)
value.strip!
raise Error, "wrong header format" if value.nil?
(headers[key.downcase] ||= []) << value