class Endpoint
Represents a way to connect to a remote HTTP server.
Definitions
def self.for(scheme, hostname, path = "/", **options)
Construct an endpoint with a specified scheme, hostname, optional path, and options.
Signature
-
parameter
scheme
String
The scheme to use, e.g. "http" or "https".
-
parameter
hostname
String
The hostname to connect to (or bind to).
-
parameter
*options
Hash
Additional options, passed to
#initialize
.
Implementation
def self.for(scheme, hostname, path = "/", **options)
# 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,
**options
)
end
def self.[](url)
Coerce the given object into an endpoint.
Signature
-
parameter
url
String | Endpoint
The URL or endpoint to convert.
Implementation
def self.[](url)
if url.is_a?(Endpoint)
return url
else
Endpoint.parse(url.to_s)
end
end
def initialize(url, endpoint = nil, **options)
Signature
-
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.
Implementation
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
def hostname
The hostname is the server we are connecting to:
Implementation
def hostname
@options[:hostname] || @url.hostname
end
def path
Return the path and query components of the given URL.
Implementation
def path
buffer = @url.path || "/"
if query = @url.query
buffer = "#{buffer}?#{query}"
end
return buffer
end
def ssl_verify_mode
We don't try to validate peer certificates when talking to localhost because they would always be self-signed.
Implementation
def ssl_verify_mode
if self.localhost?
OpenSSL::SSL::VERIFY_NONE
else
OpenSSL::SSL::VERIFY_PEER
end
end