# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require'async/io/endpoint'require'async/io/ssl_socket'moduleAsyncmoduleHTTPclassURLEndpoint<Async::IO::EndpointDEFAULT_ALPH_PROTOCOLS=['h2','http/1.1'].freezedefself.parse(string,**options)self.new(URI.parse(string),**options)enddefinitialize(url,endpoint=nil,**options)super(**options)@url=url@endpoint=endpointenddefto_s"\#<#{self.class}#{@url}#{@options.inspect}>"endattr:urlattr:optionsdefaddressendpoint.addressenddefsecure?['https','wss'].include?(@url.scheme)enddefprotocolifsecure?Protocol::HTTPSelseProtocol::HTTP1endenddefdefault_portsecure??443:80enddefport@url.port||default_portenddefhostname@options.fetch(:hostname,@url.hostname)enddefssl_context@options[:ssl_context]||::OpenSSL::SSL::SSLContext.new.tapdo|context|context.alpn_protocols=@options.fetch(:alpn_protocols,DEFAULT_ALPH_PROTOCOLS)context.set_paramsendenddefendpointunless@endpoint@endpoint=Async::IO::Endpoint.tcp(hostname,port)ifsecure?# Wrap it in SSL:@endpoint=Async::IO::SecureEndpoint.new(@endpoint,ssl_context: ssl_context,hostname: self.hostname)endendreturn@endpointenddefbind(*args,&block)endpoint.bind(*args,&block)enddefconnect(*args,&block)endpoint.connect(*args,&block)enddefeachreturnto_enumunlessblock_given?self.endpoint.eachdo|endpoint|yieldself.class.new(@url,endpoint,@options)endendendendend