class Async::HTTP::Endpoint
Represents a way to connect to a remote HTTP
server.
Attributes
url[R]
Public Class Methods
for(scheme, hostname, **options)
click to toggle source
Construct an endpoint with a specified scheme, hostname, and options.
# File lib/async/http/endpoint.rb, line 41 def self.for(scheme, hostname, **options) # TODO: Consider using URI.for once it becomes available: uri_klass = URI.scheme_list[scheme.upcase] || URI::HTTP self.new( uri_klass.new(scheme, nil, hostname, nil, nil, nil, nil, nil, nil), **options ) end
new(url, endpoint = nil, **options)
click to toggle source
@option scheme [String] the scheme to use, overrides the URL scheme. @option hostname [String] the hostname to connect to (or bind to), overrides the URL hostname (used for SNI). @option port [Integer] the port to bind to, overrides the URL port. @option ssl_context
[OpenSSL::SSL::SSLContext] the context to use for TLS. @option alpn_protocols
[Array<String>] the alpn protocols to negotiate.
Calls superclass method
# File lib/async/http/endpoint.rb, line 56 def initialize(url, endpoint = nil, **options) super(**options) 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
parse(string, endpoint = nil, **options)
click to toggle source
# File lib/async/http/endpoint.rb, line 34 def self.parse(string, endpoint = nil, **options) url = URI.parse(string).normalize return self.new(url, endpoint, **options) end
Public Instance Methods
address()
click to toggle source
# File lib/async/http/endpoint.rb, line 90 def address endpoint.address end
alpn_protocols()
click to toggle source
# File lib/async/http/endpoint.rb, line 148 def alpn_protocols @options.fetch(:alpn_protocols) {self.protocol.names} end
bind(*arguments, &block)
click to toggle source
# File lib/async/http/endpoint.rb, line 196 def bind(*arguments, &block) endpoint.bind(*arguments, &block) end
build_endpoint(endpoint = nil)
click to toggle source
# File lib/async/http/endpoint.rb, line 177 def build_endpoint(endpoint = nil) endpoint ||= tcp_endpoint if secure? # Wrap it in SSL: return Async::IO::SSLEndpoint.new(endpoint, ssl_context: self.ssl_context, hostname: @url.hostname, timeout: self.timeout, ) end return endpoint end
connect(&block)
click to toggle source
# File lib/async/http/endpoint.rb, line 200 def connect(&block) endpoint.connect(&block) end
default_port()
click to toggle source
# File lib/async/http/endpoint.rb, line 108 def default_port secure? ? 443 : 80 end
default_port?()
click to toggle source
# File lib/async/http/endpoint.rb, line 112 def default_port? port == default_port end
each() { |class.new(url, endpoint, **options)| ... }
click to toggle source
# File lib/async/http/endpoint.rb, line 204 def each return to_enum unless block_given? self.tcp_endpoint.each do |endpoint| yield self.class.new(@url, endpoint, **@options) end end
endpoint()
click to toggle source
# File lib/async/http/endpoint.rb, line 192 def endpoint @endpoint ||= build_endpoint end
eql?(other)
click to toggle source
# File lib/async/http/endpoint.rb, line 216 def eql? other self.key.eql? other.key end
hash()
click to toggle source
# File lib/async/http/endpoint.rb, line 220 def hash self.key.hash end
hostname()
click to toggle source
The hostname is the server we are connecting to:
# File lib/async/http/endpoint.rb, line 121 def hostname @options[:hostname] || @url.hostname end
inspect()
click to toggle source
# File lib/async/http/endpoint.rb, line 84 def inspect "\#<#{self.class} #{self.to_url} #{@options.inspect}>" end
key()
click to toggle source
# File lib/async/http/endpoint.rb, line 212 def key [@url, @options] end
localhost?()
click to toggle source
# File lib/async/http/endpoint.rb, line 152 def localhost? @url.hostname =~ /^(.*?\.)?localhost\.?$/ end
path()
click to toggle source
Return the path and query components of the given URL.
# File lib/async/http/endpoint.rb, line 138 def path buffer = @url.path || "/" if query = @url.query buffer = "#{buffer}?#{query}" end return buffer end
port()
click to toggle source
# File lib/async/http/endpoint.rb, line 116 def port @options[:port] || @url.port || default_port end
protocol()
click to toggle source
# File lib/async/http/endpoint.rb, line 98 def protocol @options.fetch(:protocol) do if secure? Protocol::HTTPS else Protocol::HTTP1 end end end
scheme()
click to toggle source
# File lib/async/http/endpoint.rb, line 125 def scheme @options[:scheme] || @url.scheme end
secure?()
click to toggle source
# File lib/async/http/endpoint.rb, line 94 def secure? ['https', 'wss'].include?(self.scheme) end
ssl_context()
click to toggle source
# File lib/async/http/endpoint.rb, line 165 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()
click to toggle source
We don't try to validate peer certificates when talking to localhost because they would always be self-signed.
# File lib/async/http/endpoint.rb, line 157 def ssl_verify_mode if self.localhost? OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end end
to_s()
click to toggle source
# File lib/async/http/endpoint.rb, line 80 def to_s "\#<#{self.class} #{self.to_url} #{@options}>" end
to_url()
click to toggle source
# File lib/async/http/endpoint.rb, line 70 def to_url url = @url.dup unless default_port? url.port = self.port end return url end
Protected Instance Methods
tcp_endpoint()
click to toggle source
# File lib/async/http/endpoint.rb, line 239 def tcp_endpoint Async::IO::Endpoint.tcp(self.hostname, port, **tcp_options) end
tcp_options()
click to toggle source
# File lib/async/http/endpoint.rb, line 226 def tcp_options options = @options.dup options.delete(:scheme) options.delete(:port) options.delete(:hostname) options.delete(:ssl_context) options.delete(:alpn_protocols) options.delete(:protocol) return options end