Class: Async::HTTP::Endpoint
- Inherits:
-
IO::Endpoint::Generic
- Object
- IO::Endpoint::Generic
- Async::HTTP::Endpoint
- Defined in:
- lib/async/http/endpoint.rb
Overview
Represents a way to connect to a remote HTTP server.
Constant Summary collapse
- SCHEMES =
{ "http" => URI::HTTP, "https" => URI::HTTPS, "ws" => URI::WS, "wss" => URI::WSS, }
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.[](url) ⇒ Object
Coerce the given object into an endpoint.
-
.for(scheme, hostname, path = "/", **options) ⇒ Object
Construct an endpoint with a specified scheme, hostname, optional path, and options.
- .parse(string, endpoint = nil, **options) ⇒ Object
Instance Method Summary collapse
- #address ⇒ Object
- #alpn_protocols ⇒ Object
- #authority(ignore_default_port = true) ⇒ Object
- #bind(*arguments, &block) ⇒ Object
- #build_endpoint(endpoint = nil) ⇒ Object
- #connect(&block) ⇒ Object
- #default_port ⇒ Object
- #default_port? ⇒ Boolean
- #each ⇒ Object
- #endpoint ⇒ Object
- #endpoint=(endpoint) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#hostname ⇒ Object
The hostname is the server we are connecting to:.
-
#initialize(url, endpoint = nil, **options) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #inspect ⇒ Object
- #key ⇒ Object
- #localhost? ⇒ Boolean
-
#path ⇒ Object
Return the path and query components of the given URL.
- #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.
- #to_s ⇒ Object
- #to_url ⇒ Object
Constructor Details
#initialize(url, endpoint = nil, **options) ⇒ Endpoint
Returns a new instance of Endpoint.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/async/http/endpoint.rb', line 66 def initialize(url, endpoint = nil, **) super(**) raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute? @url = url if endpoint @endpoint = self.build_endpoint(endpoint) else @endpoint = nil end end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
98 99 100 |
# File 'lib/async/http/endpoint.rb', line 98 def url @url end |
Class Method Details
.[](url) ⇒ Object
Coerce the given object into an endpoint.
53 54 55 56 57 58 59 |
# File 'lib/async/http/endpoint.rb', line 53 def self.[](url) if url.is_a?(Endpoint) return url else Endpoint.parse(url.to_s) end end |
.for(scheme, hostname, path = "/", **options) ⇒ Object
Construct an endpoint with a specified scheme, hostname, optional path, and options.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/async/http/endpoint.rb', line 39 def self.for(scheme, hostname, path = "/", **) # TODO: Consider using URI.for once it becomes available: uri_klass = SCHEMES.fetch(scheme.downcase) do raise ArgumentError, "Unsupported scheme: #{scheme.inspect}" end self.new( uri_klass.new(scheme, nil, hostname, nil, nil, path, nil, nil, nil).normalize, ** ) end |
.parse(string, endpoint = nil, **options) ⇒ Object
28 29 30 31 32 |
# File 'lib/async/http/endpoint.rb', line 28 def self.parse(string, endpoint = nil, **) url = URI.parse(string).normalize return self.new(url, endpoint, **) end |
Instance Method Details
#address ⇒ Object
100 101 102 |
# File 'lib/async/http/endpoint.rb', line 100 def address endpoint.address end |
#alpn_protocols ⇒ Object
158 159 160 |
# File 'lib/async/http/endpoint.rb', line 158 def alpn_protocols @options.fetch(:alpn_protocols) {self.protocol.names} end |
#authority(ignore_default_port = true) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/async/http/endpoint.rb', line 139 def (ignore_default_port = true) if ignore_default_port and default_port? @url.hostname else "#{@url.hostname}:#{port}" end end |
#bind(*arguments, &block) ⇒ Object
210 211 212 |
# File 'lib/async/http/endpoint.rb', line 210 def bind(*arguments, &block) endpoint.bind(*arguments, &block) end |
#build_endpoint(endpoint = nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/async/http/endpoint.rb', line 187 def build_endpoint(endpoint = nil) endpoint ||= tcp_endpoint if secure? # Wrap it in SSL: return ::IO::Endpoint::SSLEndpoint.new(endpoint, ssl_context: self.ssl_context, hostname: @url.hostname, timeout: self.timeout, ) end return endpoint end |
#connect(&block) ⇒ Object
214 215 216 |
# File 'lib/async/http/endpoint.rb', line 214 def connect(&block) endpoint.connect(&block) end |
#default_port ⇒ Object
118 119 120 |
# File 'lib/async/http/endpoint.rb', line 118 def default_port secure? ? 443 : 80 end |
#default_port? ⇒ Boolean
122 123 124 |
# File 'lib/async/http/endpoint.rb', line 122 def default_port? port == default_port end |
#each ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/async/http/endpoint.rb', line 218 def each return to_enum unless block_given? self.tcp_endpoint.each do |endpoint| yield self.class.new(@url, endpoint, **@options) end end |
#endpoint ⇒ Object
202 203 204 |
# File 'lib/async/http/endpoint.rb', line 202 def endpoint @endpoint ||= build_endpoint end |
#endpoint=(endpoint) ⇒ Object
206 207 208 |
# File 'lib/async/http/endpoint.rb', line 206 def endpoint=(endpoint) @endpoint = build_endpoint(endpoint) end |
#eql?(other) ⇒ Boolean
230 231 232 |
# File 'lib/async/http/endpoint.rb', line 230 def eql? other self.key.eql? other.key end |
#hash ⇒ Object
234 235 236 |
# File 'lib/async/http/endpoint.rb', line 234 def hash self.key.hash end |
#hostname ⇒ Object
The hostname is the server we are connecting to:
131 132 133 |
# File 'lib/async/http/endpoint.rb', line 131 def hostname @options[:hostname] || @url.hostname end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/async/http/endpoint.rb', line 94 def inspect "\#<#{self.class} #{self.to_url} #{@options.inspect}>" end |
#key ⇒ Object
226 227 228 |
# File 'lib/async/http/endpoint.rb', line 226 def key [@url, @options] end |
#localhost? ⇒ Boolean
162 163 164 |
# File 'lib/async/http/endpoint.rb', line 162 def localhost? @url.hostname =~ /^(.*?\.)?localhost\.?$/ end |
#path ⇒ Object
Return the path and query components of the given URL.
148 149 150 151 152 153 154 155 156 |
# File 'lib/async/http/endpoint.rb', line 148 def path buffer = @url.path || "/" if query = @url.query buffer = "#{buffer}?#{query}" end return buffer end |
#port ⇒ Object
126 127 128 |
# File 'lib/async/http/endpoint.rb', line 126 def port @options[:port] || @url.port || default_port end |
#protocol ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/async/http/endpoint.rb', line 108 def protocol @options.fetch(:protocol) do if secure? Protocol::HTTPS else Protocol::HTTP end end end |
#scheme ⇒ Object
135 136 137 |
# File 'lib/async/http/endpoint.rb', line 135 def scheme @options[:scheme] || @url.scheme end |
#secure? ⇒ Boolean
104 105 106 |
# File 'lib/async/http/endpoint.rb', line 104 def secure? ["https", "wss"].include?(self.scheme) end |
#ssl_context ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/async/http/endpoint.rb', line 175 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.
167 168 169 170 171 172 173 |
# File 'lib/async/http/endpoint.rb', line 167 def ssl_verify_mode if self.localhost? OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/async/http/endpoint.rb', line 90 def to_s "\#<#{self.class} #{self.to_url} #{@options}>" end |
#to_url ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/async/http/endpoint.rb', line 80 def to_url url = @url.dup unless default_port? url.port = self.port end return url end |