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.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
# File 'lib/async/http/url_endpoint.rb', line 37

def initialize(url, endpoint = nil, **options)
  super(**options)
  
  raise ArgumentError.new("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.



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

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



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

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



53
54
55
# File 'lib/async/http/url_endpoint.rb', line 53

def address
  endpoint.address
end

#alpn_protocolsObject



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

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

#authorityObject



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

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

#bind(*args, &block) ⇒ Object



153
154
155
# File 'lib/async/http/url_endpoint.rb', line 153

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

#build_endpoint(endpoint = nil) ⇒ Object



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

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
    )
  end
  
  return endpoint
end

#connect(*args, &block) ⇒ Object



157
158
159
# File 'lib/async/http/url_endpoint.rb', line 157

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

#default_portObject



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

def default_port
  secure? ? 443 : 80
end

#default_port?Boolean

Returns:

  • (Boolean)


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

def default_port?
  port == default_port
end

#eachObject



161
162
163
164
165
166
167
# File 'lib/async/http/url_endpoint.rb', line 161

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

#endpointObject



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

def endpoint
  @endpoint ||= build_endpoint
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/async/http/url_endpoint.rb', line 173

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

#hashObject



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

def hash
  self.key.hash
end

#hostnameObject



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

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

#keyObject



169
170
171
# File 'lib/async/http/url_endpoint.rb', line 169

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

#pathObject



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

def path
  @url.request_uri
end

#portObject



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

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

#protocolObject



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

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

#schemeObject



85
86
87
# File 'lib/async/http/url_endpoint.rb', line 85

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

#secure?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/async/http/url_endpoint.rb', line 57

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

#ssl_contextObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/async/http/url_endpoint.rb', line 119

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.



110
111
112
113
114
115
116
117
# File 'lib/async/http/url_endpoint.rb', line 110

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

#tcp_optionsObject



131
132
133
# File 'lib/async/http/url_endpoint.rb', line 131

def tcp_options
  {reuse_port: @options[:reuse_port] ? true : false}
end

#to_sObject



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

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