module HTTPX::AltSvc

  1. lib/httpx/altsvc.rb

Public Instance methods

cached_altsvc (origin)
[show source]
   # File lib/httpx/altsvc.rb
12 def cached_altsvc(origin)
13   now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
14   @altsvc_mutex.synchronize do
15     lookup(origin, now)
16   end
17 end
cached_altsvc_set (origin, entry)
[show source]
   # File lib/httpx/altsvc.rb
19 def cached_altsvc_set(origin, entry)
20   now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
21   @altsvc_mutex.synchronize do
22     return if @altsvcs[origin].any? { |altsvc| altsvc["origin"] == entry["origin"] }
23 
24     entry["TTL"] = Integer(entry["ma"]) + now if entry.key?("ma")
25     @altsvcs[origin] << entry
26     entry
27   end
28 end
emit (request, response)
[show source]
   # File lib/httpx/altsvc.rb
39 def emit(request, response)
40   # Alt-Svc
41   return unless response.headers.key?("alt-svc")
42 
43   origin = request.origin
44   host = request.uri.host
45   parse(response.headers["alt-svc"]) do |alt_origin, alt_params|
46     alt_origin.host ||= host
47     yield(alt_origin, origin, alt_params)
48   end
49 end
lookup (origin, ttl)
[show source]
   # File lib/httpx/altsvc.rb
30 def lookup(origin, ttl)
31   return [] unless @altsvcs.key?(origin)
32 
33   @altsvcs[origin] = @altsvcs[origin].select do |entry|
34     !entry.key?("TTL") || entry["TTL"] > ttl
35   end
36   @altsvcs[origin].reject { |entry| entry["noop"] }
37 end
parse (altsvc)
[show source]
   # File lib/httpx/altsvc.rb
51 def parse(altsvc)
52   return enum_for(__method__, altsvc) unless block_given?
53 
54   scanner = StringScanner.new(altsvc)
55   until scanner.eos?
56     alt_origin = scanner.scan(/[^=]+=("[^"]+"|[^;,]+)/)
57 
58     alt_params = []
59     loop do
60       alt_param = scanner.scan(/[^=]+=("[^"]+"|[^;,]+)/)
61       alt_params << alt_param.strip if alt_param
62       scanner.skip(/;/)
63       break if scanner.eos? || scanner.scan(/ *, */)
64     end
65     alt_params = Hash[alt_params.map { |field| field.split("=") }]
66     yield(parse_altsvc_origin(alt_origin), alt_params)
67   end
68 end
parse_altsvc_origin (alt_origin)
[show source]
   # File lib/httpx/altsvc.rb
71 def parse_altsvc_origin(alt_origin)
72   alt_proto, alt_origin = alt_origin.split("=")
73   alt_origin = alt_origin[1..-2] if alt_origin.start_with?("\"") && alt_origin.end_with?("\"")
74   if alt_origin.start_with?(":")
75     alt_origin = "dummy#{alt_origin}"
76     uri = URI.parse(alt_origin)
77     uri.host = nil
78     uri
79   else
80     URI.parse("#{alt_proto}://#{alt_origin}")
81   end
82 end