Class: Async::HTTP::URLEndpoint
- Inherits:
-
IO::Endpoint
- Object
- IO::Endpoint
- Async::HTTP::URLEndpoint
- Defined in:
- lib/async/http/url_endpoint.rb
Constant Summary collapse
- DEFAULT_ALPN_PROTOCOLS =
['h2', 'http/1.1'].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #address ⇒ Object
- #alpn_protocols ⇒ Object
- #bind(*args, &block) ⇒ Object
- #connect(*args, &block) ⇒ Object
- #default_port ⇒ Object
- #each ⇒ Object
- #endpoint ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #hostname ⇒ Object
-
#initialize(url, endpoint = nil, **options) ⇒ URLEndpoint
constructor
A new instance of URLEndpoint.
- #key ⇒ Object
- #port ⇒ Object
- #protocol ⇒ Object
- #secure? ⇒ Boolean
- #ssl_context ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(url, endpoint = nil, **options) ⇒ URLEndpoint
Returns a new instance of URLEndpoint.
36 37 38 39 40 41 42 43 |
# File 'lib/async/http/url_endpoint.rb', line 36 def initialize(url, endpoint = nil, **) super(**) raise ArgumentError.new("URL must be absolute (include scheme, host): #{url}") unless url.absolute? @url = url @endpoint = endpoint end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
50 51 52 |
# File 'lib/async/http/url_endpoint.rb', line 50 def @options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
49 50 51 |
# File 'lib/async/http/url_endpoint.rb', line 49 def url @url end |
Class Method Details
.parse(string, **options) ⇒ Object
30 31 32 33 34 |
# File 'lib/async/http/url_endpoint.rb', line 30 def self.parse(string, **) url = URI.parse(string).normalize self.new(url, **) end |
Instance Method Details
#address ⇒ Object
52 53 54 |
# File 'lib/async/http/url_endpoint.rb', line 52 def address endpoint.address end |
#alpn_protocols ⇒ Object
82 83 84 |
# File 'lib/async/http/url_endpoint.rb', line 82 def alpn_protocols @options.fetch(:alpn_protocols, DEFAULT_ALPN_PROTOCOLS) end |
#bind(*args, &block) ⇒ Object
112 113 114 |
# File 'lib/async/http/url_endpoint.rb', line 112 def bind(*args, &block) endpoint.bind(*args, &block) end |
#connect(*args, &block) ⇒ Object
116 117 118 |
# File 'lib/async/http/url_endpoint.rb', line 116 def connect(*args, &block) endpoint.connect(*args, &block) end |
#default_port ⇒ Object
68 69 70 |
# File 'lib/async/http/url_endpoint.rb', line 68 def default_port secure? ? 443 : 80 end |
#each ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/async/http/url_endpoint.rb', line 120 def each return to_enum unless block_given? self.endpoint.each do |endpoint| yield self.class.new(@url, endpoint, @options) end end |
#endpoint ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/async/http/url_endpoint.rb', line 96 def endpoint unless @endpoint @endpoint = Async::IO::Endpoint.tcp(hostname, port) if secure? # Wrap it in SSL: @endpoint = Async::IO::SecureEndpoint.new(@endpoint, ssl_context: ssl_context, hostname: self.hostname ) end end return @endpoint end |
#eql?(other) ⇒ Boolean
132 133 134 |
# File 'lib/async/http/url_endpoint.rb', line 132 def eql? other self.key.eql? other.key end |
#hash ⇒ Object
136 137 138 |
# File 'lib/async/http/url_endpoint.rb', line 136 def hash self.key.hash end |
#hostname ⇒ Object
76 77 78 |
# File 'lib/async/http/url_endpoint.rb', line 76 def hostname @options.fetch(:hostname, @url.hostname) end |
#key ⇒ Object
128 129 130 |
# File 'lib/async/http/url_endpoint.rb', line 128 def key [@url.scheme, @url.userinfo, @url.host, @url.port, @options] end |
#port ⇒ Object
72 73 74 |
# File 'lib/async/http/url_endpoint.rb', line 72 def port @url.port || default_port end |
#protocol ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/async/http/url_endpoint.rb', line 60 def protocol if secure? Protocol::HTTPS else Protocol::HTTP1 end end |
#secure? ⇒ Boolean
56 57 58 |
# File 'lib/async/http/url_endpoint.rb', line 56 def secure? ['https', 'wss'].include?(@url.scheme) end |
#ssl_context ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/async/http/url_endpoint.rb', line 86 def ssl_context @options[:ssl_context] || ::OpenSSL::SSL::SSLContext.new.tap do |context| if alpn_protocols = self.alpn_protocols context.alpn_protocols = alpn_protocols end context.set_params end end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/async/http/url_endpoint.rb', line 45 def to_s "\#<#{self.class} #{@url} #{@options.inspect}>" end |