Class: RDF::Util::File::NetHttpAdapter
- Inherits:
-
HttpAdapter
- Object
- HttpAdapter
- RDF::Util::File::NetHttpAdapter
- Defined in:
- lib/rdf/util/file.rb
Overview
Net::HTTP adapter to retrieve resources without additional dependencies
Class Method Summary collapse
- .open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options) ⇒ RemoteDocument, Object abstract
Methods inherited from HttpAdapter
default_accept_header, default_user_agent, headers
Class Method Details
.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options) ⇒ RemoteDocument, Object
This method is abstract.
Returns A RemoteDocument. If a block is given, the result of evaluating the block is returned.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rdf/util/file.rb', line 119 def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **) ssl_verify = verify_none ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER redirect_count = 0 max_redirects = 5 parsed_url = RDF::URI.parse(base_uri) parsed_proxy = RDF::URI.parse(proxy.to_s) base_uri = parsed_url.to_s remote_document = nil until remote_document do Net::HTTP::start(parsed_url.host, parsed_url.port, parsed_proxy.host, parsed_proxy.port, open_timeout: 60 * 1000, use_ssl: parsed_url.scheme == 'https', verify_mode: ssl_verify ) do |http| request = Net::HTTP::Get.new(parsed_url.request_uri, headers(headers: headers)) http.request(request) do |response| case response when Net::HTTPSuccess # found object # Normalize headers using symbols response_headers = response.to_hash.inject({}) do |out, (key, value)| out[key.gsub(/-/, '_').downcase.to_sym] = %w{ set-cookie }.include?(key.downcase) ? value : value.first out end # If a Location is returned, it defines the base resource for this file, not it's actual ending location = { base_uri: RDF::URI(response["Location"] ? response["Location"] : base_uri), code: response.code.to_i, content_type: response.content_type, headers: response_headers }.merge(response.type_params) [:last_modified] = DateTime.parse(response["Last-Modified"]) if response["Last-Modified"] remote_document = RemoteDocument.new(response.body, ) when Net::HTTPRedirection # Follow redirection raise IOError, "Too many redirects" if (redirect_count += 1) > max_redirects # Location may be relative parsed_url = ::URI.join(base_uri, response["Location"]) base_uri = parsed_url.to_s else raise IOError, "<#{parsed_url}>: #{response.}(#{response.code})" end end end end remote_document end |