lib/httpx/altsvc.rb in httpx-0.18.3 vs lib/httpx/altsvc.rb in httpx-0.18.4
- old
+ new
@@ -68,42 +68,58 @@
def parse(altsvc)
return enum_for(__method__, altsvc) unless block_given?
scanner = StringScanner.new(altsvc)
until scanner.eos?
- alt_origin = scanner.scan(/[^=]+=("[^"]+"|[^;,]+)/)
+ alt_service = scanner.scan(/[^=]+=("[^"]+"|[^;,]+)/)
alt_params = []
loop do
alt_param = scanner.scan(/[^=]+=("[^"]+"|[^;,]+)/)
alt_params << alt_param.strip if alt_param
scanner.skip(/;/)
break if scanner.eos? || scanner.scan(/ *, */)
end
alt_params = Hash[alt_params.map { |field| field.split("=") }]
- yield(parse_altsvc_origin(alt_origin), alt_params)
+
+ alt_proto, alt_authority = alt_service.split("=")
+ alt_origin = parse_altsvc_origin(alt_proto, alt_authority)
+ return unless alt_origin
+
+ yield(alt_origin, alt_params.merge("proto" => alt_proto))
end
end
+ def parse_altsvc_scheme(alt_proto)
+ case alt_proto
+ when "h2c"
+ "http"
+ when "h2"
+ "https"
+ end
+ end
+
# :nocov:
if RUBY_VERSION < "2.2"
- def parse_altsvc_origin(alt_origin)
- alt_proto, alt_origin = alt_origin.split("=")
+ def parse_altsvc_origin(alt_proto, alt_origin)
+ alt_scheme = parse_altsvc_scheme(alt_proto) or return
+
alt_origin = alt_origin[1..-2] if alt_origin.start_with?("\"") && alt_origin.end_with?("\"")
if alt_origin.start_with?(":")
- alt_origin = "#{alt_proto}://dummy#{alt_origin}"
+ alt_origin = "#{alt_scheme}://dummy#{alt_origin}"
uri = URI.parse(alt_origin)
uri.host = nil
uri
else
- URI.parse("#{alt_proto}://#{alt_origin}")
+ URI.parse("#{alt_scheme}://#{alt_origin}")
end
end
else
- def parse_altsvc_origin(alt_origin)
- alt_proto, alt_origin = alt_origin.split("=")
+ def parse_altsvc_origin(alt_proto, alt_origin)
+ alt_scheme = parse_altsvc_scheme(alt_proto) or return
alt_origin = alt_origin[1..-2] if alt_origin.start_with?("\"") && alt_origin.end_with?("\"")
- URI.parse("#{alt_proto}://#{alt_origin}")
+
+ URI.parse("#{alt_scheme}://#{alt_origin}")
end
end
# :nocov:
end
end