Class: Async::HTTP::Endpoint

Inherits:
IO::Endpoint
  • Object
show all
Defined in:
lib/async/http/endpoint.rb

Overview

Represents a way to connect to a remote HTTP server.

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

  • alpn_protocols (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/http/endpoint.rb', line 43

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.



75
76
77
# File 'lib/async/http/endpoint.rb', line 75

def url
  @url
end

Class Method Details

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



32
33
34
35
36
# File 'lib/async/http/endpoint.rb', line 32

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

Instance Method Details

#addressObject



77
78
79
# File 'lib/async/http/endpoint.rb', line 77

def address
	endpoint.address
end

#alpn_protocolsObject



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

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

#authority(ignore_default_port = true) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/async/http/endpoint.rb', line 116

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

#bind(*args, &block) ⇒ Object



196
197
198
# File 'lib/async/http/endpoint.rb', line 196

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

#build_endpoint(endpoint = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/async/http/endpoint.rb', line 177

def build_endpoint(endpoint = nil)
	endpoint ||= Async::IO::Endpoint.tcp(self.hostname, port, tcp_options)
	
	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) ⇒ Object



200
201
202
# File 'lib/async/http/endpoint.rb', line 200

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

#default_portObject



95
96
97
# File 'lib/async/http/endpoint.rb', line 95

def default_port
	secure? ? 443 : 80
end

#default_port?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/async/http/endpoint.rb', line 99

def default_port?
	port == default_port
end

#eachObject



204
205
206
207
208
209
210
# File 'lib/async/http/endpoint.rb', line 204

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

#endpointObject



192
193
194
# File 'lib/async/http/endpoint.rb', line 192

def endpoint
	@endpoint ||= build_endpoint
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/async/http/endpoint.rb', line 216

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

#hashObject



220
221
222
# File 'lib/async/http/endpoint.rb', line 220

def hash
	self.key.hash
end

#hostnameObject

The hostname is the server we are connecting to:



108
109
110
# File 'lib/async/http/endpoint.rb', line 108

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

#inspectObject



71
72
73
# File 'lib/async/http/endpoint.rb', line 71

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

#keyObject



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

def key
	[@url.scheme, @url.userinfo, @url.host, @url.port, @options]
end

#localhost?Boolean

Returns:

  • (Boolean)


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

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

#pathObject

Return the path and query components of the given URL.



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

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

#portObject



103
104
105
# File 'lib/async/http/endpoint.rb', line 103

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

#protocolObject



85
86
87
88
89
90
91
92
93
# File 'lib/async/http/endpoint.rb', line 85

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

#schemeObject



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

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

#secure?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/async/http/endpoint.rb', line 81

def secure?
	['https', 'wss'].include?(@url.scheme)
end

#ssl_contextObject



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/async/http/endpoint.rb', line 152

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.



144
145
146
147
148
149
150
# File 'lib/async/http/endpoint.rb', line 144

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

#tcp_optionsObject



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

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

#to_sObject



67
68
69
# File 'lib/async/http/endpoint.rb', line 67

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

#to_urlObject



57
58
59
60
61
62
63
64
65
# File 'lib/async/http/endpoint.rb', line 57

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