Class: CMSScanner::WebSite

Inherits:
Object
  • Object
show all
Defined in:
lib/cms_scanner/web_site.rb

Overview

WebSite Implementation

Direct Known Subclasses

Target

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_url, opts = {}) ⇒ WebSite

Returns a new instance of WebSite.

Parameters:

  • site_url (String)
  • opts (Hash) (defaults to: {})


10
11
12
13
# File 'lib/cms_scanner/web_site.rb', line 10

def initialize(site_url, opts = {})
  self.url = +site_url
  @opts    = opts
end

Instance Attribute Details

#homepage_res ⇒ Typhoeus::Response

As webmock does not support redirects mocking, coverage is ignored :nocov:

Returns:



48
49
50
# File 'lib/cms_scanner/web_site.rb', line 48

def homepage_res
  @homepage_res ||= NS::Browser.get_and_follow_location(url)
end

#opts ⇒ Object (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/cms_scanner/web_site.rb', line 6

def opts
  @opts
end

#uri ⇒ Object (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/cms_scanner/web_site.rb', line 6

def uri
  @uri
end

Instance Method Details

#access_forbidden?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


93
94
95
# File 'lib/cms_scanner/web_site.rb', line 93

def access_forbidden?(path = nil)
  NS::Browser.get(url(path)).code == 403
end

#error_404_res ⇒ Typhoeus::Response

Returns:



59
60
61
# File 'lib/cms_scanner/web_site.rb', line 59

def error_404_res
  @error_404_res ||= NS::Browser.get(error_404_url)
end

#error_404_url ⇒ String

Returns The URL of an unlikely existant page.

Returns:

  • (String) —

    The URL of an unlikely existant page



64
65
66
# File 'lib/cms_scanner/web_site.rb', line 64

def error_404_url
  non_existant_page_url
end

#head_and_get(path, codes = [200], params = {}) ⇒ Typhoeus::Response

Perform a HEAD request to the path provided, then if its response code is in the array of codes given, a GET is done and the response returned. Otherwise the HEAD response is returned.

Parameters:

  • path (String)
  • codes (Array<String>) (defaults to: [200])
  • params (Hash) (defaults to: {}) —

    The requests params

Options Hash (params):

  • :head (Hash) —

    Request params for the HEAD

  • :get (hash) —

    Request params for the GET

Returns:



141
142
143
144
145
146
147
148
# File 'lib/cms_scanner/web_site.rb', line 141

def head_and_get(path, codes = [200], params = {})
  url_to_get  = url(path)
  head_params = (params[:head] || {}).merge(head_or_get_params)

  head_res = NS::Browser.forge_request(url_to_get, head_params).run

  codes.include?(head_res.code) ? NS::Browser.get(url_to_get, params[:get] || {}) : head_res
end

#head_or_get_params ⇒ Hash

Returns The Typhoeus params to use to perform head requests.

Returns:

  • (Hash) —

    The Typhoeus params to use to perform head requests



122
123
124
125
126
127
128
# File 'lib/cms_scanner/web_site.rb', line 122

def head_or_get_params
  @head_or_get_params ||= if NS::Browser.head(homepage_url).code == 405
                            { method: :get, maxfilesize: 1 }
                          else
                            { method: :head }
                          end
end

#homepage_url ⇒ String

Returns:

  • (String)


54
55
56
# File 'lib/cms_scanner/web_site.rb', line 54

def homepage_url
  @homepage_url ||= homepage_res.effective_url
end

#http_auth?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


86
87
88
# File 'lib/cms_scanner/web_site.rb', line 86

def http_auth?(path = nil)
  NS::Browser.get(url(path)).code == 401
end

#non_existant_page_url ⇒ String

TODO: This will be removed in the next major version (0.7)

Returns:

  • (String) —

    The URL of an unlikely existant page



70
71
72
# File 'lib/cms_scanner/web_site.rb', line 70

def non_existant_page_url
  uri.join(Digest::MD5.hexdigest(rand(999_999).to_s) + '.html').to_s
end

#online?(path = nil) ⇒ Boolean

Checks if the remote website is up.

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


79
80
81
# File 'lib/cms_scanner/web_site.rb', line 79

def online?(path = nil)
  NS::Browser.get(url(path)).code.nonzero? ? true : false
end

#proxy_auth?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


100
101
102
# File 'lib/cms_scanner/web_site.rb', line 100

def proxy_auth?(path = nil)
  NS::Browser.get(url(path)).code == 407
end

#redirection(url = nil) ⇒ String

As webmock does not support redirects mocking, coverage is ignored :nocov:

Parameters:

  • url (String) (defaults to: nil)

Returns:

  • (String) —

    The redirection url or nil



110
111
112
113
114
115
116
117
118
# File 'lib/cms_scanner/web_site.rb', line 110

def redirection(url = nil)
  url ||= @uri.to_s

  return unless [301, 302].include?(NS::Browser.get(url).code)

  res = NS::Browser.get(url, followlocation: true)

  res.effective_url == url ? nil : res.effective_url
end

#url(path = nil) ⇒ String

Used for convenience

URI.encode is preferered over Addressable::URI.encode as it will encode leading # character: URI.encode('#t#') => %23t%23 Addressable::URI.encode('#t#') => #t%23

Parameters:

  • path (String) (defaults to: nil) —

    Optional path to merge with the uri

Returns:

  • (String)


36
37
38
39
40
# File 'lib/cms_scanner/web_site.rb', line 36

def url(path = nil)
  return @uri.to_s unless path

  @uri.join(URI.encode(path)).to_s
end

#url=(site_url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/cms_scanner/web_site.rb', line 15

def url=(site_url)
  # Add a trailing slash to the site url
  # Making also sure the site_url is unfrozen
  +site_url << '/' if site_url[-1, 1] != '/'

  # Use the validator to ensure the site_url has a correct format
  OptParseValidator::OptURL.new([]).validate(site_url)

  @uri = Addressable::URI.parse(site_url).normalize
end