Module: Blinkr::HttpUtils

Included in:
Engine, Extensions::Links, PhantomJSWrapper, TyphoeusWrapper
Defined in:
lib/blinkr/http_utils.rb

Instance Method Summary collapse

Instance Method Details

#retry?(resp) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/blinkr/http_utils.rb', line 48

def retry? resp
  resp.timed_out? || (resp.code == 0 && [ "Server returned nothing (no headers, no data)", "SSL connect error", "Failure when receiving data from the peer" ].include?(resp.return_message) )
end

#sanitize(dest, src) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/blinkr/http_utils.rb', line 6

def sanitize(dest, src)
  return nil if (dest.nil? || src.nil?)

  # src is the page that tried to load the URL
  # URI fails to handle #! style fragments, so we chomp them
  src = src[0, src.rindex('#!')] unless src.rindex('#!').nil?
  src_uri = URI(src)

  # Remove the query and fragment from the SRC URI, as we are going to use it to resolve the relative dest URIs
  src_uri.query = nil
  src_uri.fragment = nil

  # base is the web root of the site
  base_uri = URI(@config.base_url)

  begin

    # dest is the URL we are trying to load
    dest_uri = URI(dest)
    dest_uri.fragment = nil if @config.ignore_fragments

    # If we have a relative URI, or just a fragment, join what we have to our base URL
    dest_uri = URI.join(src_uri, dest) if (empty?(dest_uri.path) && !empty?(dest_uri.fragment)) || dest_uri.relative?

    # If we have an absolute path URI, join it to the base URL
    dest_uri = URI.join(base_uri.scheme, base_uri.hostname, base_uri.port, dest_uri) if empty?(dest_uri.scheme) && empty?(dest_uri.hostname)

    # switch multiple '/' to just one. Those types of URIs don't affect the browser,
    # but they do affect our checking
    dest_uri.path = dest_uri.path.gsub(/\/+/, '/') if dest_uri.path
    dest_uri.query = dest_uri.query.gsub(/\/+/, '/') if dest_uri.query
    dest_uri.fragment = dest_uri.query.gsub(/\/+/, '/') if dest_uri.fragment

    dest = dest_uri.to_s
  rescue URI::InvalidURIError, URI::InvalidComponentError, URI::BadURIError
    # ignored
  rescue StandardError
    return nil
  end
  dest.chomp('#').chomp('index.html')
end