Class: Async::HTTP::URLEndpoint

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

Constant Summary collapse

DEFAULT_ALPH_PROTOCOLS =
['h2', 'http/1.1'].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.



33
34
35
36
37
38
# File 'lib/async/http/url_endpoint.rb', line 33

def initialize(url, endpoint = nil, **options)
	super(**options)
	
	@url = url
	@endpoint = endpoint
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/async/http/url_endpoint.rb', line 45

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



44
45
46
# File 'lib/async/http/url_endpoint.rb', line 44

def url
  @url
end

Class Method Details

.parse(string, **options) ⇒ Object



29
30
31
# File 'lib/async/http/url_endpoint.rb', line 29

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

Instance Method Details

#addressObject



47
48
49
# File 'lib/async/http/url_endpoint.rb', line 47

def address
	endpoint.address
end

#bind(*args, &block) ⇒ Object



98
99
100
# File 'lib/async/http/url_endpoint.rb', line 98

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

#connect(*args, &block) ⇒ Object



102
103
104
# File 'lib/async/http/url_endpoint.rb', line 102

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

#default_portObject



63
64
65
# File 'lib/async/http/url_endpoint.rb', line 63

def default_port
	secure? ? 443 : 80
end

#eachObject



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

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

#endpointObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/async/http/url_endpoint.rb', line 82

def endpoint
	unless @endpoint
		@endpoint = Async::IO::Endpoint.tcp(hostname, port)
		
		if secure?
			# Wrap it in SSL:
			@endpoint = Async::IO::SecureEndpoint.new(@endpoint,
				ssl_context: ssl_context,
				hostname: self.hostname
			)
		end
	end
	
	return @endpoint
end

#hostnameObject



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

def hostname
	@options.fetch(:hostname, @url.hostname)
end

#portObject



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

def port
	@url.port || default_port
end

#protocolObject



55
56
57
58
59
60
61
# File 'lib/async/http/url_endpoint.rb', line 55

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

#secure?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/async/http/url_endpoint.rb', line 51

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

#ssl_contextObject



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

def ssl_context
	@options[:ssl_context] || ::OpenSSL::SSL::SSLContext.new.tap do |context|
		context.alpn_protocols = @options.fetch(:alpn_protocols, DEFAULT_ALPH_PROTOCOLS)
		context.set_params
	end
end

#to_sObject



40
41
42
# File 'lib/async/http/url_endpoint.rb', line 40

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