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
- LOCALHOST =
'localhost'.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
- #authority ⇒ Object
- #bind(*args, &block) ⇒ Object
- #build_endpoint(endpoint = nil) ⇒ Object
- #connect(*args, &block) ⇒ Object
- #default_port ⇒ Object
- #default_port? ⇒ Boolean
- #each ⇒ Object
- #endpoint ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #hostname ⇒ Object
-
#initialize(url, endpoint = nil, **options) ⇒ URLEndpoint
constructor
A new instance of URLEndpoint.
- #inspect ⇒ Object
- #key ⇒ Object
- #path ⇒ Object
- #port ⇒ Object
- #protocol ⇒ Object
- #scheme ⇒ Object
- #secure? ⇒ Boolean
- #ssl_context ⇒ Object
-
#ssl_verify_mode ⇒ Object
We don’t try to validate peer certificates when talking to localhost because they would always be self-signed.
- #tcp_options ⇒ Object
- #to_s ⇒ Object
- #to_url ⇒ Object
Constructor Details
#initialize(url, endpoint = nil, **options) ⇒ URLEndpoint
Returns a new instance of URLEndpoint.
37 38 39 40 41 42 43 44 |
# File 'lib/async/http/url_endpoint.rb', line 37 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.
65 66 67 |
# File 'lib/async/http/url_endpoint.rb', line 65 def @options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
64 65 66 |
# File 'lib/async/http/url_endpoint.rb', line 64 def url @url end |
Class Method Details
.parse(string, **options) ⇒ Object
31 32 33 34 35 |
# File 'lib/async/http/url_endpoint.rb', line 31 def self.parse(string, **) url = URI.parse(string).normalize self.new(url, **) end |
Instance Method Details
#address ⇒ Object
67 68 69 |
# File 'lib/async/http/url_endpoint.rb', line 67 def address endpoint.address end |
#alpn_protocols ⇒ Object
117 118 119 |
# File 'lib/async/http/url_endpoint.rb', line 117 def alpn_protocols @options[:alpn_protocols] || DEFAULT_ALPN_PROTOCOLS end |
#authority ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/async/http/url_endpoint.rb', line 103 def if default_port? hostname else "#{hostname}:#{port}" end end |
#bind(*args, &block) ⇒ Object
171 172 173 |
# File 'lib/async/http/url_endpoint.rb', line 171 def bind(*args, &block) endpoint.bind(*args, &block) end |
#build_endpoint(endpoint = nil) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/async/http/url_endpoint.rb', line 152 def build_endpoint(endpoint = nil) endpoint ||= Async::IO::Endpoint.tcp(hostname, port, ) if secure? # Wrap it in SSL: return Async::IO::SSLEndpoint.new(endpoint, ssl_context: self.ssl_context, hostname: self.hostname, timeout: self.timeout, ) end return endpoint end |
#connect(*args, &block) ⇒ Object
175 176 177 |
# File 'lib/async/http/url_endpoint.rb', line 175 def connect(*args, &block) endpoint.connect(*args, &block) end |
#default_port ⇒ Object
83 84 85 |
# File 'lib/async/http/url_endpoint.rb', line 83 def default_port secure? ? 443 : 80 end |
#default_port? ⇒ Boolean
87 88 89 |
# File 'lib/async/http/url_endpoint.rb', line 87 def default_port? port == default_port end |
#each ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/async/http/url_endpoint.rb', line 179 def each return to_enum unless block_given? self.endpoint.each do |endpoint| yield self.class.new(@url, endpoint, @options) end end |
#endpoint ⇒ Object
167 168 169 |
# File 'lib/async/http/url_endpoint.rb', line 167 def endpoint @endpoint ||= build_endpoint end |
#eql?(other) ⇒ Boolean
191 192 193 |
# File 'lib/async/http/url_endpoint.rb', line 191 def eql? other self.key.eql? other.key end |
#hash ⇒ Object
195 196 197 |
# File 'lib/async/http/url_endpoint.rb', line 195 def hash self.key.hash end |
#hostname ⇒ Object
95 96 97 |
# File 'lib/async/http/url_endpoint.rb', line 95 def hostname @options[:hostname] || @url.hostname end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/async/http/url_endpoint.rb', line 60 def inspect "\#<#{self.class} #{self.to_url} #{@options.inspect}>" end |
#key ⇒ Object
187 188 189 |
# File 'lib/async/http/url_endpoint.rb', line 187 def key [@url.scheme, @url.userinfo, @url.host, @url.port, @options] end |
#path ⇒ Object
111 112 113 |
# File 'lib/async/http/url_endpoint.rb', line 111 def path @url.request_uri end |
#port ⇒ Object
91 92 93 |
# File 'lib/async/http/url_endpoint.rb', line 91 def port @options[:port] || @url.port || default_port end |
#protocol ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/async/http/url_endpoint.rb', line 75 def protocol if secure? Protocol::HTTPS else Protocol::HTTP1 end end |
#scheme ⇒ Object
99 100 101 |
# File 'lib/async/http/url_endpoint.rb', line 99 def scheme @options[:scheme] || @url.scheme end |
#secure? ⇒ Boolean
71 72 73 |
# File 'lib/async/http/url_endpoint.rb', line 71 def secure? ['https', 'wss'].include?(@url.scheme) end |
#ssl_context ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/async/http/url_endpoint.rb', line 133 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( verify_mode: self.ssl_verify_mode ) end end |
#ssl_verify_mode ⇒ Object
We don’t try to validate peer certificates when talking to localhost because they would always be self-signed.
124 125 126 127 128 129 130 131 |
# File 'lib/async/http/url_endpoint.rb', line 124 def ssl_verify_mode case self.hostname when LOCALHOST OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end end |
#tcp_options ⇒ Object
145 146 147 148 149 150 |
# File 'lib/async/http/url_endpoint.rb', line 145 def { reuse_port: self.reuse_port, timeout: self.timeout, } end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/async/http/url_endpoint.rb', line 56 def to_s "\#<#{self.class} #{self.to_url}>" end |
#to_url ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/async/http/url_endpoint.rb', line 46 def to_url url = @url.dup unless default_port? url.port = self.port end return url end |