Module: HTTPX::AltSvc

Defined in:
lib/httpx/altsvc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cached_altsvc(origin) ⇒ Object



10
11
12
13
14
15
# File 'lib/httpx/altsvc.rb', line 10

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



17
18
19
20
21
22
23
24
25
26
# File 'lib/httpx/altsvc.rb', line 17

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpx/altsvc.rb', line 37

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



28
29
30
31
32
33
34
35
# File 'lib/httpx/altsvc.rb', line 28

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



49
50
51
52
53
54
55
56
57
# File 'lib/httpx/altsvc.rb', line 49

def parse(altsvc)
  return enum_for(__method__, altsvc) unless block_given?

  alt_origins, *alt_params = altsvc.split(/ *; */)
  alt_params = Hash[alt_params.map { |field| field.split("=") }]
  alt_origins.split(/ *, */).each do |alt_origin|
    yield(parse_altsvc_origin(alt_origin), alt_params)
  end
end

Instance Method Details

#parse_altsvc_origin(alt_origin) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/httpx/altsvc.rb', line 60

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