mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
extending URI API with #origin and #authority, using it everywhere possible
This commit is contained in:
parent
a8ed199e01
commit
4fb10d9eb8
@ -35,6 +35,8 @@ module HTTPX
|
||||
include Loggable
|
||||
include Callbacks
|
||||
|
||||
using URIExtensions
|
||||
|
||||
require "httpx/channel/http2"
|
||||
require "httpx/channel/http1"
|
||||
|
||||
@ -68,7 +70,7 @@ module HTTPX
|
||||
def initialize(type, uri, options)
|
||||
@type = type
|
||||
@uri = uri
|
||||
@hostnames = [@uri.host]
|
||||
@origins = [@uri.origin]
|
||||
@options = Options.new(options)
|
||||
@window_size = @options.window_size
|
||||
@read_buffer = Buffer.new(BUFFER_SIZE)
|
||||
@ -93,14 +95,12 @@ module HTTPX
|
||||
if @io.protocol == "h2" && @uri.scheme == "https"
|
||||
@io.verify_hostname(channel.uri.host)
|
||||
else
|
||||
@uri.host == channel.uri.host &&
|
||||
@uri.port == channel.uri.port &&
|
||||
@uri.scheme == channel.uri.scheme
|
||||
@uri.origin == channel.uri.origin
|
||||
end
|
||||
end
|
||||
|
||||
def merge(channel)
|
||||
@hostnames += channel.instance_variable_get(:@hostnames)
|
||||
@origins += channel.instance_variable_get(:@origins)
|
||||
pending = channel.instance_variable_get(:@pending)
|
||||
pending.each do |req, args|
|
||||
send(req, args)
|
||||
@ -108,7 +108,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def unmerge(channel)
|
||||
@hostnames -= channel.instance_variable_get(:@hostnames)
|
||||
@origins -= channel.instance_variable_get(:@origins)
|
||||
[@parser.pending, @pending].each do |pending|
|
||||
pending.reject! do |request|
|
||||
request.uri == channel.uri && begin
|
||||
@ -123,17 +123,13 @@ module HTTPX
|
||||
def match?(uri)
|
||||
return false if @state == :closing
|
||||
|
||||
(@hostnames.include?(uri.host) &&
|
||||
uri.port == @uri.port &&
|
||||
uri.scheme == @uri.scheme) || match_alternative_service?(uri)
|
||||
@origins.include?(uri.origin) || match_alternative_service?(uri)
|
||||
end
|
||||
|
||||
def match_alternative_service?(uri)
|
||||
AltSvc.cached_lookup(@uri.host).any? do |altsvc|
|
||||
origin = altsvc["origin"]
|
||||
uri.host == origin.host &&
|
||||
uri.port == origin.port &&
|
||||
uri.scheme == origin.scheme
|
||||
uri.origin == origin.to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1,52 +1,69 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
unless Method.method_defined?(:curry)
|
||||
require "uri"
|
||||
|
||||
# Backport
|
||||
#
|
||||
# Ruby 2.1 and lower implement curry only for Procs.
|
||||
#
|
||||
# Why not using Refinements? Because they don't work for Method (tested with ruby 2.1.9).
|
||||
#
|
||||
module CurryMethods # :nodoc:
|
||||
# Backport for the Method#curry method, which is part of ruby core since 2.2 .
|
||||
module HTTPX
|
||||
unless Method.method_defined?(:curry)
|
||||
|
||||
# Backport
|
||||
#
|
||||
def curry(*args)
|
||||
to_proc.curry(*args)
|
||||
# Ruby 2.1 and lower implement curry only for Procs.
|
||||
#
|
||||
# Why not using Refinements? Because they don't work for Method (tested with ruby 2.1.9).
|
||||
#
|
||||
module CurryMethods # :nodoc:
|
||||
# Backport for the Method#curry method, which is part of ruby core since 2.2 .
|
||||
#
|
||||
def curry(*args)
|
||||
to_proc.curry(*args)
|
||||
end
|
||||
end
|
||||
Method.__send__(:include, CurryMethods)
|
||||
end
|
||||
Method.__send__(:include, CurryMethods)
|
||||
end
|
||||
|
||||
unless String.method_defined?(:+@)
|
||||
# Backport for +"", to initialize unfrozen strings from the string literal.
|
||||
#
|
||||
module LiteralStringExtensions
|
||||
def +@
|
||||
frozen? ? dup : self
|
||||
unless String.method_defined?(:+@)
|
||||
# Backport for +"", to initialize unfrozen strings from the string literal.
|
||||
#
|
||||
module LiteralStringExtensions
|
||||
def +@
|
||||
frozen? ? dup : self
|
||||
end
|
||||
end
|
||||
String.__send__(:include, LiteralStringExtensions)
|
||||
end
|
||||
String.__send__(:include, LiteralStringExtensions)
|
||||
end
|
||||
|
||||
unless Numeric.method_defined?(:positive?)
|
||||
# Ruby 2.3 Backport (Numeric#positive?)
|
||||
#
|
||||
module PosMethods
|
||||
def positive?
|
||||
self > 0
|
||||
unless Numeric.method_defined?(:positive?)
|
||||
# Ruby 2.3 Backport (Numeric#positive?)
|
||||
#
|
||||
module PosMethods
|
||||
def positive?
|
||||
self > 0
|
||||
end
|
||||
end
|
||||
Numeric.__send__(:include, PosMethods)
|
||||
end
|
||||
Numeric.__send__(:include, PosMethods)
|
||||
end
|
||||
|
||||
unless Numeric.method_defined?(:negative?)
|
||||
# Ruby 2.3 Backport (Numeric#negative?)
|
||||
#
|
||||
module NegMethods
|
||||
def negative?
|
||||
self < 0
|
||||
unless Numeric.method_defined?(:negative?)
|
||||
# Ruby 2.3 Backport (Numeric#negative?)
|
||||
#
|
||||
module NegMethods
|
||||
def negative?
|
||||
self < 0
|
||||
end
|
||||
end
|
||||
Numeric.__send__(:include, NegMethods)
|
||||
end
|
||||
|
||||
module URIExtensions
|
||||
refine URI::Generic do
|
||||
def authority
|
||||
port_string = port == default_port ? nil : ":#{port}"
|
||||
"#{host}#{port_string}"
|
||||
end
|
||||
|
||||
def origin
|
||||
"#{scheme}://#{authority}"
|
||||
end
|
||||
end
|
||||
end
|
||||
Numeric.__send__(:include, NegMethods)
|
||||
end
|
||||
end
|
@ -5,6 +5,7 @@ require "forwardable"
|
||||
module HTTPX
|
||||
class Request
|
||||
extend Forwardable
|
||||
using URIExtensions
|
||||
|
||||
METHODS = [
|
||||
# RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
|
||||
@ -71,14 +72,14 @@ module HTTPX
|
||||
path
|
||||
end
|
||||
|
||||
# https://bugs.ruby-lang.org/issues/15278
|
||||
def authority
|
||||
host = @uri.host
|
||||
port_string = @uri.port == @uri.default_port ? nil : ":#{@uri.port}"
|
||||
"#{host}#{port_string}"
|
||||
@uri.authority
|
||||
end
|
||||
|
||||
# https://bugs.ruby-lang.org/issues/15278
|
||||
def origin
|
||||
"#{scheme}://#{authority}"
|
||||
@uri.origin
|
||||
end
|
||||
|
||||
def query
|
||||
|
Loading…
x
Reference in New Issue
Block a user