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)


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

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

#url ⇒ Object (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

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



34
35
36
37
38
# 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

Instance Method Details

#address ⇒ Object



79
80
81
# File 'lib/async/http/endpoint.rb', line 79

def address
	endpoint.address
end

#alpn_protocols ⇒ Object



137
138
139
# File 'lib/async/http/endpoint.rb', line 137

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

#authority(ignore_default_port = true) ⇒ Object



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

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



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

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

#build_endpoint(endpoint = nil) ⇒ Object



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

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) ⇒ Object



189
190
191
# File 'lib/async/http/endpoint.rb', line 189

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

#default_port ⇒ Object



97
98
99
# File 'lib/async/http/endpoint.rb', line 97

def default_port
	secure? ? 443 : 80
end

#default_port? ⇒ Boolean

Returns:

  • (Boolean)


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

def default_port?
	port == default_port
end

#each ⇒ Object



193
194
195
196
197
198
199
# File 'lib/async/http/endpoint.rb', line 193

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

#endpoint ⇒ Object



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

def endpoint
	@endpoint ||= build_endpoint
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hash ⇒ Object



209
210
211
# File 'lib/async/http/endpoint.rb', line 209

def hash
	self.key.hash
end

#hostname ⇒ Object

The hostname is the server we are connecting to:



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

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

#inspect ⇒ Object



73
74
75
# File 'lib/async/http/endpoint.rb', line 73

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

#key ⇒ Object



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

def key
	[@url, @options]
end

#localhost? ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/async/http/endpoint.rb', line 141

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

#path ⇒ Object

Return the path and query components of the given URL.



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

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

#port ⇒ Object



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

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

#protocol ⇒ Object



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

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

#scheme ⇒ Object



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

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

#secure? ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/async/http/endpoint.rb', line 83

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

#ssl_context ⇒ Object



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

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 ⇒ Object

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



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

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

#to_s ⇒ Object



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

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

#to_url ⇒ Object



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

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