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