Class: Async::HTTP::Endpoint

Inherits:
IO::Endpoint::Generic
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, endpoint = nil, **options) ⇒ Endpoint

Returns a new instance of Endpoint.

Parameters:

  • scheme (Hash)

    a customizable set of options

  • hostname (Hash)

    a customizable set of options

  • port (Hash)

    a customizable set of options

  • ssl_context (Hash)

    a customizable set of options

  • tls_configuration (Hash)

    a customizable set of options

  • alpn_protocols (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/async/http/endpoint.rb', line 75

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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



110
111
112
# File 'lib/async/http/endpoint.rb', line 110

def url
  @url
end

Class Method Details

.[](url) ⇒ Object

Coerce the given object into an endpoint.



61
62
63
64
65
66
67
# File 'lib/async/http/endpoint.rb', line 61

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.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/async/http/endpoint.rb', line 47

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

.parse(string, endpoint = nil, **options) ⇒ Object

Parse a URL string into an endpoint.



36
37
38
39
40
# File 'lib/async/http/endpoint.rb', line 36

def self.parse(string, endpoint = nil, **options)
	url = URI.parse(string).normalize
	
	return self.new(url, endpoint, **options)
end

Instance Method Details

#addressObject



113
114
115
# File 'lib/async/http/endpoint.rb', line 113

def address
	endpoint.address
end

#alpn_protocolsObject



179
180
181
# File 'lib/async/http/endpoint.rb', line 179

def alpn_protocols
	@options.fetch(:alpn_protocols){self.protocol.names}
end

#authority(ignore_default_port = true) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/async/http/endpoint.rb', line 159

def authority(ignore_default_port = true)
	if ignore_default_port and default_port?
		@url.hostname
	else
		"#{@url.hostname}:#{port}"
	end
end

#bind(*arguments, &block) ⇒ Object

Bind to the endpoint.



246
247
248
# File 'lib/async/http/endpoint.rb', line 246

def bind(*arguments, &block)
	endpoint.bind(*arguments, &block)
end

#build_endpoint(endpoint = nil) ⇒ Object

Build a suitable endpoint, optionally wrapping in TLS for secure connections.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/async/http/endpoint.rb', line 218

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,
			tls_configuration: self.tls_configuration,
			hostname: @url.hostname,
			timeout: self.timeout,
		)
	end
	
	return endpoint
end

#connect(&block) ⇒ Object

Connect to the endpoint.



251
252
253
# File 'lib/async/http/endpoint.rb', line 251

def connect(&block)
	endpoint.connect(&block)
end

#default_portObject



134
135
136
# File 'lib/async/http/endpoint.rb', line 134

def default_port
	secure? ? 443 : 80
end

#default_port?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/async/http/endpoint.rb', line 139

def default_port?
	port == default_port
end

#eachObject

Enumerate all resolved endpoints.



258
259
260
261
262
263
264
# File 'lib/async/http/endpoint.rb', line 258

def each
	return to_enum unless block_given?
	
	self.tcp_endpoint.each do |endpoint|
		yield self.class.new(@url, endpoint, **@options)
	end
end

#endpointObject



235
236
237
# File 'lib/async/http/endpoint.rb', line 235

def endpoint
	@endpoint ||= build_endpoint
end

#endpoint=(endpoint) ⇒ Object

Set the underlying endpoint, wrapping it as needed.



241
242
243
# File 'lib/async/http/endpoint.rb', line 241

def endpoint=(endpoint)
	@endpoint = build_endpoint(endpoint)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/async/http/endpoint.rb', line 272

def eql? other
	self.key.eql? other.key
end

#hashObject



277
278
279
# File 'lib/async/http/endpoint.rb', line 277

def hash
	self.key.hash
end

#hostnameObject

The hostname is the server we are connecting to:



149
150
151
# File 'lib/async/http/endpoint.rb', line 149

def hostname
	@options[:hostname] || @url.hostname
end

#inspectObject



106
107
108
# File 'lib/async/http/endpoint.rb', line 106

def inspect
	"\#<#{self.class} #{self.to_url} #{@options.inspect}>"
end

#keyObject



267
268
269
# File 'lib/async/http/endpoint.rb', line 267

def key
	[@url, @options]
end

#localhost?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/async/http/endpoint.rb', line 184

def localhost?
	@url.hostname =~ /^(.*?\.)?localhost\.?$/
end

#pathObject

Return the path and query components of the given URL.



168
169
170
171
172
173
174
175
176
# File 'lib/async/http/endpoint.rb', line 168

def path
	buffer = @url.path || "/"
	
	if query = @url.query
		buffer = "#{buffer}?#{query}"
	end
	
	return buffer
end

#portObject



144
145
146
# File 'lib/async/http/endpoint.rb', line 144

def port
	@options[:port] || @url.port || default_port
end

#protocolObject



123
124
125
126
127
128
129
130
131
# File 'lib/async/http/endpoint.rb', line 123

def protocol
	@options.fetch(:protocol) do
		if secure?
			Protocol::HTTPS
		else
			Protocol::HTTP
		end
	end
end

#schemeObject



154
155
156
# File 'lib/async/http/endpoint.rb', line 154

def scheme
	@options[:scheme] || @url.scheme
end

#secure?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/async/http/endpoint.rb', line 118

def secure?
	["https", "wss"].include?(self.scheme)
end

#ssl_contextObject



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/async/http/endpoint.rb', line 198

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_modeObject

We don't try to validate peer certificates when talking to localhost because they would always be self-signed.



189
190
191
192
193
194
195
# File 'lib/async/http/endpoint.rb', line 189

def ssl_verify_mode
	if self.localhost?
		OpenSSL::SSL::VERIFY_NONE
	else
		OpenSSL::SSL::VERIFY_PEER
	end
end

#tls_configurationObject



211
212
213
# File 'lib/async/http/endpoint.rb', line 211

def tls_configuration
	@options[:tls_configuration]
end

#to_sObject



101
102
103
# File 'lib/async/http/endpoint.rb', line 101

def to_s
	"\#<#{self.class} #{self.to_url} #{@options}>"
end

#to_urlObject



90
91
92
93
94
95
96
97
98
# File 'lib/async/http/endpoint.rb', line 90

def to_url
	url = @url.dup
	
	unless default_port?
		url.port = self.port
	end
	
	return url
end