Module: HTTPX::AltSvc
- Defined in:
- lib/httpx/altsvc.rb
Defined Under Namespace
Modules: ConnectionMixin
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
- .parse_altsvc_origin(alt_proto, alt_origin) ⇒ Object
- .parse_altsvc_scheme(alt_proto) ⇒ Object
Class Method Details
.cached_altsvc(origin) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/httpx/altsvc.rb', line 64 def cached_altsvc(origin) now = Utils.now @altsvc_mutex.synchronize do lookup(origin, now) end end |
.cached_altsvc_set(origin, entry) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/httpx/altsvc.rb', line 71 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) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/httpx/altsvc.rb', line 91 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) do |alt_origin, alt_params| alt_origin.host ||= host yield(alt_origin, origin, alt_params) end end |
.lookup(origin, ttl) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/httpx/altsvc.rb', line 82 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/httpx/altsvc.rb', line 120 def parse(altsvc) return enum_for(__method__, altsvc) unless block_given? scanner = StringScanner.new(altsvc) until scanner.eos? 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("=", 2) }] alt_proto, = alt_service.split("=", 2) alt_origin = parse_altsvc_origin(alt_proto, ) return unless alt_origin yield(alt_origin, alt_params.merge("proto" => alt_proto)) end end |
.parse_altsvc_origin(alt_proto, alt_origin) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/httpx/altsvc.rb', line 153 def parse_altsvc_origin(alt_proto, alt_origin) 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.parse("#{alt_scheme}://#{alt_origin}") end |
.parse_altsvc_scheme(alt_proto) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/httpx/altsvc.rb', line 144 def parse_altsvc_scheme(alt_proto) case alt_proto when "h2c" "http" when "h2" "https" end end |