Module: HTTPX::AltSvc

Defined in:
lib/httpx/altsvc.rb,
sig/altsvc.rbs

Defined Under Namespace

Modules: ConnectionMixin

Class Method Summary collapse

Class Method Details

.cached_altsvc(origin) ⇒ Array[altsvc_params]

Parameters:

  • origin (String)

Returns:

  • (Array[altsvc_params])


68
69
70
71
72
73
# File 'lib/httpx/altsvc.rb', line 68

def cached_altsvc(origin)
  now = Utils.now
  @altsvc_mutex.synchronize do
    lookup(origin, now)
  end
end

.cached_altsvc_set(origin, entry) ⇒ void

This method returns an undefined value.

Parameters:

  • origin (String)
  • (altsvc_params)


75
76
77
78
79
80
81
82
83
84
# File 'lib/httpx/altsvc.rb', line 75

def cached_altsvc_set(origin, entry)
  now = Utils.now
  @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) {|alt_origin, origin, alt_params| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • request (Request)
  • response (response)

Yields:

Yield Parameters:

  • alt_origin (http_uri)
  • origin (String)
  • alt_params (altsvc_params)

Yield Returns:

  • (void)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/httpx/altsvc.rb', line 95

def emit(request, response)
  return unless response.respond_to?(:headers)
  # Alt-Svc
  return unless response.headers.key?("alt-svc")

  origin = request.origin
  host = request.uri.host

  altsvc = response.headers["alt-svc"]

  # https://datatracker.ietf.org/doc/html/rfc7838#section-3
  # A field value containing the special value "clear" indicates that the
  # origin requests all alternatives for that origin to be invalidated
  # (including those specified in the same response, in case of an
  # invalid reply containing both "clear" and alternative services).
  if altsvc == "clear"
    @altsvc_mutex.synchronize do
      @altsvcs[origin].clear
    end

    return
  end

  parse(altsvc, host: host) do |alt_origin, alt_params|
    alt_origin.host ||= host
    yield(alt_origin, origin, alt_params)
  end
end

.lookup(origin, ttl) ⇒ Array[altsvc_params]

Parameters:

  • origin (String)
  • ttl (Integer, Float)

Returns:

  • (Array[altsvc_params])


86
87
88
89
90
91
92
93
# File 'lib/httpx/altsvc.rb', line 86

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, host:) ⇒ void .parse(altsvc, host:) ⇒ Enumerable[[http_uri, altsvc_params]]

Overloads:

  • .parse(altsvc, host:) ⇒ void

    This method returns an undefined value.

    Parameters:

    • altsvc (String)
    • host: (String, nil)
  • .parse(altsvc, host:) ⇒ Enumerable[[http_uri, altsvc_params]]

    Parameters:

    • altsvc (String)
    • host: (String, nil)

    Returns:

    • (Enumerable[[http_uri, altsvc_params]])

Yields:

Yield Parameters:

  • alt_origin (http_uri)
  • alt_params (altsvc_params)

Yield Returns:

  • (void)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/httpx/altsvc.rb', line 124

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

  scanner = StringScanner.new(altsvc)
  until scanner.eos?
    alt_services = [] #: Array[String]

    while (alt_service = scanner.scan(/[^=]+=("[^"]+")/))
      alt_services << alt_service
      scanner.skip(/[, ]+/)
    end

    scanner.skip(/;/)

    alt_params = [] #: Array[String]
    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 do |field|
      field.split("=", 2)
    end]

    alt_services.each do |alt_service|
      alt_proto, alt_authority = alt_service.split("=", 2)
      alt_origin = parse_altsvc_origin(alt_proto, alt_authority, host: host)

      next unless alt_origin

      yield(alt_origin, alt_params.merge("proto" => alt_proto))
    end
  end
end

.parse_altsvc_origin(alt_proto, alt_origin, host: nil) ⇒ http_uri?

Parameters:

  • alt_proto (string)
  • alt_origin (String)
  • host: (String, nil) (defaults to: nil)

Returns:

  • (http_uri, nil)


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/httpx/altsvc.rb', line 169

def parse_altsvc_origin(alt_proto, alt_origin, host: nil)
  alt_scheme = parse_altsvc_scheme(alt_proto)

  return unless alt_scheme

  alt_origin = alt_origin[1..-2] if alt_origin.start_with?("\"") && alt_origin.end_with?("\"")

  uri = URI.parse("#{alt_scheme}://#{alt_origin}")
  uri.host = host if uri.host.nil? || uri.host.empty?
  uri
end

.parse_altsvc_scheme(alt_proto) ⇒ String?

Parameters:

  • alt_proto (String)

Returns:

  • (String, nil)


160
161
162
163
164
165
166
167
# File 'lib/httpx/altsvc.rb', line 160

def parse_altsvc_scheme(alt_proto)
  case alt_proto
  when "h2c"
    "http"
  when "h2", "quic", /^h3/
    "https"
  end
end