class CMSScanner::WebSite
WebSite
Implementation
Attributes
Public Class Methods
Source
# File lib/cms_scanner/web_site.rb, line 10 def initialize(site_url, opts = {}) self.url = site_url @opts = opts end
@param [ String ] site_url @param [ Hash ] opts
Public Instance Methods
Source
# File lib/cms_scanner/web_site.rb, line 88 def access_forbidden?(path = nil) NS::Browser.get(url(path)).code == 403 end
@param [ String ] path
@return [ Boolean ]
Source
# File lib/cms_scanner/web_site.rb, line 60 def error_404_res @error_404_res ||= NS::Browser.get_and_follow_location(error_404_url) end
@return [ Typhoeus::Response
]
Source
# File lib/cms_scanner/web_site.rb, line 65 def error_404_url @error_404_url ||= uri.join("#{Digest::MD5.hexdigest(rand(999_999).to_s)[0..6]}.html").to_s end
@return [ String ] The URL of an unlikely existant page
Source
# File lib/cms_scanner/web_site.rb, line 136 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
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.
@param [ String ] path @param [ Array<String> ] codes @param [ Hash ] params The requests params @option params [ Hash ] :head Request params for the HEAD @option params [ hash ] :get Request params for the GET
@return [ Typhoeus::Response
]
Source
# File lib/cms_scanner/web_site.rb, line 117 def head_or_get_params @head_or_get_params ||= if [0, 405, 501].include?(NS::Browser.head(homepage_url).code) { method: :get, maxfilesize: 1 } else { method: :head } end end
@return [ Hash ] The Typhoeus
params to use to perform head requests
Source
# File lib/cms_scanner/web_site.rb, line 49 def homepage_res @homepage_res ||= NS::Browser.get_and_follow_location(url) end
@return [ Typhoeus::Response
]
As webmock does not support redirects mocking, coverage is ignored :nocov:
Source
# File lib/cms_scanner/web_site.rb, line 55 def homepage_url @homepage_url ||= homepage_res.effective_url end
@return [ String ]
Source
# File lib/cms_scanner/web_site.rb, line 81 def http_auth?(path = nil) NS::Browser.get(url(path)).code == 401 end
@param [ String ] path
@return [ Boolean ]
Source
# File lib/cms_scanner/web_site.rb, line 37 def ip @ip ||= IPSocket.getaddress(uri.host) rescue SocketError 'Unknown' end
@return [ String ] The IP address of the target
Source
# File lib/cms_scanner/web_site.rb, line 74 def online?(path = nil) NS::Browser.get(url(path)).code.nonzero? ? true : false end
Checks if the remote website is up.
@param [ String ] path
@return [ Boolean ]
Source
# File lib/cms_scanner/web_site.rb, line 95 def proxy_auth?(path = nil) NS::Browser.get(url(path)).code == 407 end
@param [ String ] path
@return [ Boolean ]
Source
# File lib/cms_scanner/web_site.rb, line 105 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, maxredirs: 10) res.effective_url == url ? nil : res.effective_url end
@param [ String ] url
@return [ String ] The redirection url or nil
As webmock does not support redirects mocking, coverage is ignored :nocov:
Source
# File lib/cms_scanner/web_site.rb, line 30 def url(path = nil) return @uri.to_s unless path @uri.join(Addressable::URI.encode(path).gsub('#', '%23')).to_s end
@param [ String ] path Optional path to merge with the uri
@return [ String ]
Source
# File lib/cms_scanner/web_site.rb, line 15 def url=(site_url) new_url = site_url.dup # Add a trailing slash to the URL new_url << '/' if new_url[-1, 1] != '/' # Use the validator to ensure the URL has a correct format OptParseValidator::OptURL.new([]).validate(new_url) @uri = Addressable::URI.parse(new_url).normalize end