Class: Async::HTTP::Endpoint
- Inherits:
-
IO::Endpoint
- Object
- IO::Endpoint
- Async::HTTP::Endpoint
- Defined in:
- lib/async/http/endpoint.rb
Overview
Represents a way to connect to a remote HTTP server.
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #address ⇒ Object
- #alpn_protocols ⇒ Object
- #authority ⇒ Object
- #bind(*args, &block) ⇒ Object
- #build_endpoint(endpoint = nil) ⇒ Object
- #connect(&block) ⇒ Object
- #default_port ⇒ Object
- #default_port? ⇒ Boolean
- #each ⇒ Object
- #endpoint ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#hostname ⇒ Object
The hostname is the server we are connecting to:.
-
#initialize(url, endpoint = nil, **options) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #inspect ⇒ Object
- #key ⇒ Object
- #localhost? ⇒ Boolean
-
#path ⇒ Object
Return the path and query components of the given URL.
- #port ⇒ Object
- #protocol ⇒ Object
- #scheme ⇒ Object
- #secure? ⇒ Boolean
- #ssl_context ⇒ Object
-
#ssl_verify_mode ⇒ Object
We don’t try to validate peer certificates when talking to localhost because they would always be self-signed.
- #tcp_options ⇒ Object
- #to_s ⇒ Object
- #to_url ⇒ Object
Constructor Details
#initialize(url, endpoint = nil, **options) ⇒ Endpoint
Returns a new instance of Endpoint.
43 44 45 46 47 48 49 50 |
# File 'lib/async/http/endpoint.rb', line 43 def initialize(url, endpoint = nil, **) super(**) raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute? @url = url @endpoint = endpoint end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
70 71 72 |
# File 'lib/async/http/endpoint.rb', line 70 def url @url end |
Class Method Details
.parse(string, **options) ⇒ Object
32 33 34 35 36 |
# File 'lib/async/http/endpoint.rb', line 32 def self.parse(string, **) url = URI.parse(string).normalize return self.new(url, nil, **) end |
Instance Method Details
#address ⇒ Object
72 73 74 |
# File 'lib/async/http/endpoint.rb', line 72 def address endpoint.address end |
#alpn_protocols ⇒ Object
130 131 132 |
# File 'lib/async/http/endpoint.rb', line 130 def alpn_protocols @options.fetch(:alpn_protocols) {self.protocol.names} end |
#authority ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/async/http/endpoint.rb', line 111 def if default_port? @url.hostname else "#{@url.hostname}:#{port}" end end |
#bind(*args, &block) ⇒ Object
191 192 193 |
# File 'lib/async/http/endpoint.rb', line 191 def bind(*args, &block) endpoint.bind(*args, &block) end |
#build_endpoint(endpoint = nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/async/http/endpoint.rb', line 172 def build_endpoint(endpoint = nil) endpoint ||= Async::IO::Endpoint.tcp(self.hostname, port, ) 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
195 196 197 |
# File 'lib/async/http/endpoint.rb', line 195 def connect(&block) endpoint.connect(&block) end |
#default_port ⇒ Object
90 91 92 |
# File 'lib/async/http/endpoint.rb', line 90 def default_port secure? ? 443 : 80 end |
#default_port? ⇒ Boolean
94 95 96 |
# File 'lib/async/http/endpoint.rb', line 94 def default_port? port == default_port end |
#each ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/async/http/endpoint.rb', line 199 def each return to_enum unless block_given? self.endpoint.each do |endpoint| yield self.class.new(@url, endpoint, @options) end end |
#endpoint ⇒ Object
187 188 189 |
# File 'lib/async/http/endpoint.rb', line 187 def endpoint @endpoint ||= build_endpoint end |
#eql?(other) ⇒ Boolean
211 212 213 |
# File 'lib/async/http/endpoint.rb', line 211 def eql? other self.key.eql? other.key end |
#hash ⇒ Object
215 216 217 |
# File 'lib/async/http/endpoint.rb', line 215 def hash self.key.hash end |
#hostname ⇒ Object
The hostname is the server we are connecting to:
103 104 105 |
# File 'lib/async/http/endpoint.rb', line 103 def hostname @options[:hostname] || @url.hostname end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/async/http/endpoint.rb', line 66 def inspect "\#<#{self.class} #{self.to_url} #{@options.inspect}>" end |
#key ⇒ Object
207 208 209 |
# File 'lib/async/http/endpoint.rb', line 207 def key [@url.scheme, @url.userinfo, @url.host, @url.port, @options] end |
#localhost? ⇒ Boolean
134 135 136 |
# File 'lib/async/http/endpoint.rb', line 134 def localhost? @url.hostname =~ /^(.*?\.)?localhost\.?$/ end |
#path ⇒ Object
Return the path and query components of the given URL.
120 121 122 123 124 125 126 127 128 |
# File 'lib/async/http/endpoint.rb', line 120 def path buffer = @url.path || "/" if query = @url.query buffer << "?#{query}" end return buffer end |
#port ⇒ Object
98 99 100 |
# File 'lib/async/http/endpoint.rb', line 98 def port @options[:port] || @url.port || default_port end |
#protocol ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/async/http/endpoint.rb', line 80 def protocol @options.fetch(:protocol) do if secure? Protocol::HTTPS else Protocol::HTTP1 end end end |
#scheme ⇒ Object
107 108 109 |
# File 'lib/async/http/endpoint.rb', line 107 def scheme @options[:scheme] || @url.scheme end |
#secure? ⇒ Boolean
76 77 78 |
# File 'lib/async/http/endpoint.rb', line 76 def secure? ['https', 'wss'].include?(@url.scheme) end |
#ssl_context ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/async/http/endpoint.rb', line 147 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.
139 140 141 142 143 144 145 |
# File 'lib/async/http/endpoint.rb', line 139 def ssl_verify_mode if self.localhost? OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end end |
#tcp_options ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/async/http/endpoint.rb', line 159 def = @options.dup .delete(:scheme) .delete(:port) .delete(:hostname) .delete(:ssl_context) .delete(:alpn_protocols) .delete(:protocol) return end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/async/http/endpoint.rb', line 62 def to_s "\#<#{self.class} #{self.to_url} #{@options}>" end |
#to_url ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/async/http/endpoint.rb', line 52 def to_url url = @url.dup unless default_port? url.port = self.port end return url end |