Class: RDF::Util::File::NetHttpAdapter

Inherits:
HttpAdapter show all
Defined in:
lib/rdf/util/file.rb

Overview

Net::HTTP adapter to retrieve resources without additional dependencies

Since:

  • 1.2

Class Method Summary collapse

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.

Parameters:

  • base_uri (String)

    to open

  • proxy (String) (defaults to: nil)

    HTTP Proxy to use for requests.

  • headers (Array, String) (defaults to: {})

    ({}) HTTP Request headers

    Defaults Accept header based on available reader content types to allow for content negotiation based on available readers.

    Defaults User-Agent header, unless one is specified.

  • verify_none (Boolean) (defaults to: false)

    (false) Don't verify SSL certificates

  • options (Hash{Symbol => Object})

    options are ignored in this implementation. Applications are encouraged to override this implementation to provide more control over HTTP headers and redirect following.

Returns:

Raises:

  • (IOError)

    if not found

Since:

  • 1.2



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, **options)
  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
          document_options = {
            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)
          document_options[:last_modified] = DateTime.parse(response["Last-Modified"]) if response["Last-Modified"]

          remote_document = RemoteDocument.new(response.body, document_options)
        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.message}(#{response.code})"
        end
      end
    end
  end
  remote_document
end