Class: Async::HTTP::URLEndpoint

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

Constant Summary collapse

DEFAULT_ALPN_PROTOCOLS =
['h2', 'http/1.1'].freeze
LOCALHOST =
'localhost'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of URLEndpoint.

Parameters:

  • scheme (Hash)

    a customizable set of options

  • port (Hash)

    a customizable set of options

  • hostname (Hash)

    a customizable set of options

  • ssl_context (Hash)

    a customizable set of options

  • alpn_protocols (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/async/http/url_endpoint.rb', line 42

def initialize(url, endpoint = nil, **options)
	super(**options)
	
	raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute?
	
	@url = url
	@endpoint = endpoint
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



70
71
72
# File 'lib/async/http/url_endpoint.rb', line 70

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



69
70
71
# File 'lib/async/http/url_endpoint.rb', line 69

def url
  @url
end

Class Method Details

.parse(string, **options) ⇒ Object



31
32
33
34
35
# File 'lib/async/http/url_endpoint.rb', line 31

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

Instance Method Details

#addressObject



72
73
74
# File 'lib/async/http/url_endpoint.rb', line 72

def address
	endpoint.address
end

#alpn_protocolsObject



122
123
124
# File 'lib/async/http/url_endpoint.rb', line 122

def alpn_protocols
	@options[:alpn_protocols] || DEFAULT_ALPN_PROTOCOLS
end

#authorityObject



108
109
110
111
112
113
114
# File 'lib/async/http/url_endpoint.rb', line 108

def authority
	if default_port?
		hostname
	else
		"#{hostname}:#{port}"
	end
end

#bind(*args, &block) ⇒ Object



181
182
183
# File 'lib/async/http/url_endpoint.rb', line 181

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

#build_endpoint(endpoint = nil) ⇒ Object



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

def build_endpoint(endpoint = nil)
	endpoint ||= Async::IO::Endpoint.tcp(hostname, port, tcp_options)
	
	if secure?
		# Wrap it in SSL:
		return Async::IO::SSLEndpoint.new(endpoint,
			ssl_context: self.ssl_context,
			hostname: self.hostname,
			timeout: self.timeout,
		)
	end
	
	return endpoint
end

#connect(&block) ⇒ Object



185
186
187
# File 'lib/async/http/url_endpoint.rb', line 185

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

#default_portObject



88
89
90
# File 'lib/async/http/url_endpoint.rb', line 88

def default_port
	secure? ? 443 : 80
end

#default_port?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/async/http/url_endpoint.rb', line 92

def default_port?
	port == default_port
end

#eachObject



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

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

#endpointObject



177
178
179
# File 'lib/async/http/url_endpoint.rb', line 177

def endpoint
	@endpoint ||= build_endpoint
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/async/http/url_endpoint.rb', line 201

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

#hashObject



205
206
207
# File 'lib/async/http/url_endpoint.rb', line 205

def hash
	self.key.hash
end

#hostnameObject



100
101
102
# File 'lib/async/http/url_endpoint.rb', line 100

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

#inspectObject



65
66
67
# File 'lib/async/http/url_endpoint.rb', line 65

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

#keyObject



197
198
199
# File 'lib/async/http/url_endpoint.rb', line 197

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

#pathObject



116
117
118
# File 'lib/async/http/url_endpoint.rb', line 116

def path
	@url.request_uri
end

#portObject



96
97
98
# File 'lib/async/http/url_endpoint.rb', line 96

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

#protocolObject



80
81
82
83
84
85
86
# File 'lib/async/http/url_endpoint.rb', line 80

def protocol
	if secure?
		Protocol::HTTPS
	else
		Protocol::HTTP1
	end
end

#schemeObject



104
105
106
# File 'lib/async/http/url_endpoint.rb', line 104

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

#secure?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/async/http/url_endpoint.rb', line 76

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

#ssl_contextObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/async/http/url_endpoint.rb', line 138

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.



129
130
131
132
133
134
135
136
# File 'lib/async/http/url_endpoint.rb', line 129

def ssl_verify_mode
	case self.hostname
	when LOCALHOST
		OpenSSL::SSL::VERIFY_NONE
	else
		OpenSSL::SSL::VERIFY_PEER
	end
end

#tcp_optionsObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/async/http/url_endpoint.rb', line 150

def tcp_options
	options = @options.dup
	
	options.delete(:scheme)
	options.delete(:port)
	options.delete(:hostname)
	options.delete(:ssl_context)
	options.delete(:alpn_protocols)
	
	return options
end

#to_sObject



61
62
63
# File 'lib/async/http/url_endpoint.rb', line 61

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

#to_urlObject



51
52
53
54
55
56
57
58
59
# File 'lib/async/http/url_endpoint.rb', line 51

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