Browser::Actions (no specs)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
require 'common/typhoeus_cache'
|
||||
require 'common/browser/actions'
|
||||
|
||||
class Browser
|
||||
extend Browser::Actions
|
||||
|
||||
@@instance = nil
|
||||
USER_AGENT_MODES = %w{ static semi-static random }
|
||||
|
||||
@@ -122,26 +125,6 @@ class Browser
|
||||
end
|
||||
end
|
||||
|
||||
def get(url, params = {})
|
||||
run_request(
|
||||
forge_request(url, params.merge(method: :get))
|
||||
)
|
||||
end
|
||||
|
||||
def post(url, params = {})
|
||||
run_request(
|
||||
forge_request(url, params.merge(method: :post))
|
||||
)
|
||||
end
|
||||
|
||||
def get_and_follow_location(url, params = {})
|
||||
params[:maxredirs] ||= 2
|
||||
|
||||
run_request(
|
||||
forge_request(url, params.merge(method: :get, followlocation: true))
|
||||
)
|
||||
end
|
||||
|
||||
def forge_request(url, params = {})
|
||||
Typhoeus::Request.new(
|
||||
url.to_s,
|
||||
@@ -181,12 +164,19 @@ class Browser
|
||||
params.merge!(ssl_verifypeer: false)
|
||||
params.merge!(ssl_verifyhost: 0)
|
||||
|
||||
params.merge!(cookie_jar: @cache_dir + '/cookie-jar')
|
||||
params.merge!(cookie_file: @cache_dir + '/cookie-jar')
|
||||
params.merge!(cookiejar: @cache_dir + '/cookie-jar')
|
||||
params.merge!(cookiefile: @cache_dir + '/cookie-jar')
|
||||
|
||||
params
|
||||
end
|
||||
|
||||
# return the response
|
||||
def run_request(request)
|
||||
@hydra.queue request
|
||||
@hydra.run
|
||||
request.response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# return Array
|
||||
@@ -199,13 +189,6 @@ class Browser
|
||||
params
|
||||
end
|
||||
|
||||
# return the response
|
||||
def run_request(request)
|
||||
@hydra.queue request
|
||||
@hydra.run
|
||||
request.response
|
||||
end
|
||||
|
||||
# Override with the options if they are set
|
||||
def override_config_with_options(options)
|
||||
options.each do |option, value|
|
||||
@@ -214,4 +197,5 @@ class Browser
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
49
lib/common/browser/actions.rb
Normal file
49
lib/common/browser/actions.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
class Browser
|
||||
module Actions
|
||||
|
||||
# @param [ String ] url
|
||||
# @param [ Hash ] params
|
||||
#
|
||||
# @return [ Typhoeus::Response ]
|
||||
def get(url, params = {})
|
||||
#Typhoeus.get(url, Browser.instance.merge_request_params(params))
|
||||
process(url, params.merge(method: :get))
|
||||
end
|
||||
|
||||
# @param [ String ] url
|
||||
# @param [ Hash ] params
|
||||
#
|
||||
# @return [ Typhoeus::Response ]
|
||||
def post(url, params = {})
|
||||
#Typhoeus.post(url, Browser.instance.merge_request_params(params))
|
||||
process(url, params.merge(method: :post))
|
||||
end
|
||||
|
||||
# @param [ String ] url
|
||||
# @param [ Hash ] params
|
||||
#
|
||||
# @return [ Typhoeus::Response ]
|
||||
def get_and_follow_location(url, params = {})
|
||||
params[:maxredirs] ||= 2
|
||||
|
||||
get(url, params.merge(followlocation: true))
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# @param [ String ] url
|
||||
# @param [ Hash ] params
|
||||
#
|
||||
# @return [ Typhoeus::Response ]
|
||||
def process(url, params)
|
||||
browser = Browser.instance
|
||||
|
||||
browser.run_request(
|
||||
browser.forge_request(url, params)
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
7
lib/common/browser/options.rb
Normal file
7
lib/common/browser/options.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
class Browser
|
||||
module Options
|
||||
|
||||
end
|
||||
end
|
||||
@@ -67,7 +67,7 @@ class WpItems < Array
|
||||
results = new
|
||||
item_class = self.item_class
|
||||
type = self.to_s.gsub(/Wp/, '').downcase
|
||||
response = Browser.instance.get(wp_target.url)
|
||||
response = Browser.get(wp_target.url)
|
||||
item_options = {
|
||||
wp_content_dir: wp_target.wp_content_dir,
|
||||
wp_plugins_dir: wp_target.wp_plugins_dir,
|
||||
|
||||
@@ -47,20 +47,6 @@ module Typhoeus
|
||||
end
|
||||
end
|
||||
|
||||
module Ethon
|
||||
class Easy
|
||||
module Options
|
||||
def cookie_jar=(value)
|
||||
Curl.set_option(:cookiejar, value_for(value, :string), handle)
|
||||
end
|
||||
|
||||
def cookie_file=(value)
|
||||
Curl.set_option(:cookiefile, value_for(value, :string), handle)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Override for puts to enable logging
|
||||
def puts(o = '')
|
||||
# remove color for logging
|
||||
|
||||
@@ -13,7 +13,7 @@ class WpItem
|
||||
# @return [ Boolean ]
|
||||
def exists?(options = {}, response = nil)
|
||||
unless response
|
||||
response = Browser.instance.get(url)
|
||||
response = Browser.get(url)
|
||||
end
|
||||
exists_from_response?(response, options)
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class WpItem
|
||||
|
||||
# @return [ Boolean ]
|
||||
def has_readme?
|
||||
Browser.instance.get(readme_url).code == 200 ? true : false
|
||||
Browser.get(readme_url).code == 200 ? true : false
|
||||
end
|
||||
|
||||
# @return [ String ] The url to the readme file
|
||||
@@ -17,7 +17,7 @@ class WpItem
|
||||
|
||||
# @return [ Boolean ]
|
||||
def has_changelog?
|
||||
Browser.instance.get(changelog_url).code == 200 ? true : false
|
||||
Browser.get(changelog_url).code == 200 ? true : false
|
||||
end
|
||||
|
||||
# @return [ String ] The url to the changelog file
|
||||
@@ -27,7 +27,7 @@ class WpItem
|
||||
|
||||
# @return [ Boolean ]
|
||||
def has_directory_listing?
|
||||
Browser.instance.get(@uri.to_s).body[%r{<title>Index of}] ? true : false
|
||||
Browser.get(@uri.to_s).body[%r{<title>Index of}] ? true : false
|
||||
end
|
||||
|
||||
# Discover any error_log files created by WordPress
|
||||
@@ -41,7 +41,7 @@ class WpItem
|
||||
#
|
||||
# @return [ Boolean ]
|
||||
def has_error_log?
|
||||
response_body = Browser.instance.get(error_log_url, headers: {'range' => 'bytes=0-700'}).body
|
||||
response_body = Browser.get(error_log_url, headers: {'range' => 'bytes=0-700'}).body
|
||||
response_body[%r{PHP Fatal error}i] ? true : false
|
||||
end
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class WpItem
|
||||
# @return [ String ] The version number
|
||||
def version
|
||||
unless @version
|
||||
response = Browser.instance.get(readme_url)
|
||||
response = Browser.get(readme_url)
|
||||
@version = response.body[%r{stable tag: #{WpVersion.version_pattern}}i, 1]
|
||||
end
|
||||
@version
|
||||
|
||||
@@ -27,7 +27,7 @@ class WpTheme < WpItem
|
||||
#
|
||||
# @return [ WpTheme ]
|
||||
def find_from_css_link(target_uri)
|
||||
response = Browser.instance.get_and_follow_location(target_uri.to_s)
|
||||
response = Browser.get_and_follow_location(target_uri.to_s)
|
||||
|
||||
# https + domain is optional because of relative links
|
||||
matches = %r{(?:https?://[^"']+)?/([^/]+)/themes/([^"']+)/style.css}i.match(response.body)
|
||||
@@ -49,7 +49,7 @@ class WpTheme < WpItem
|
||||
#
|
||||
# @return [ WpTheme ]
|
||||
def find_from_wooframework(target_uri)
|
||||
body = Browser.instance.get(target_uri.to_s).body
|
||||
body = Browser.get(target_uri.to_s).body
|
||||
regexp = %r{<meta name="generator" content="([^\s"]+)\s?([^"]+)?" />\s+<meta name="generator" content="WooFramework\s?([^"]+)?" />}
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ class WpTheme < WpItem
|
||||
|
||||
def version
|
||||
unless @version
|
||||
@version = Browser.instance.get(style_url).body[%r{Version:\s([^\s]+)}i, 1]
|
||||
@version = Browser.get(style_url).body[%r{Version:\s([^\s]+)}i, 1]
|
||||
|
||||
# Get Version from readme.txt
|
||||
@version ||= super
|
||||
|
||||
@@ -9,7 +9,7 @@ class WpTimthumb < WpItem
|
||||
# @return [ String ] The version
|
||||
def version
|
||||
unless @version
|
||||
response = Browser.instance.get(url)
|
||||
response = Browser.get(url)
|
||||
@version = response.body[%r{TimThumb version\s*: ([^<]+)} , 1]
|
||||
end
|
||||
@version
|
||||
|
||||
@@ -24,7 +24,7 @@ class WpUser < WpItem
|
||||
|
||||
@login = Existable.login_from_author_pattern(location)
|
||||
@display_name = Existable.display_name_from_body(
|
||||
Browser.instance.get(location).body
|
||||
Browser.get(location).body
|
||||
)
|
||||
elsif response.code == 200 # login in body?
|
||||
@login = Existable.login_from_body(response.body)
|
||||
|
||||
@@ -45,7 +45,7 @@ class WpVersion < WpItem
|
||||
# @return [ String ]
|
||||
def scan_url(target_uri, pattern, path = nil)
|
||||
url = path ? target_uri.merge(path).to_s : target_uri.to_s
|
||||
response = Browser.instance.get_and_follow_location(url)
|
||||
response = Browser.get_and_follow_location(url)
|
||||
|
||||
response.body[pattern, 1]
|
||||
end
|
||||
@@ -163,7 +163,7 @@ class WpVersion < WpItem
|
||||
xml.xpath('//file').each do |node|
|
||||
wp_item.path = node.attribute('src').text
|
||||
|
||||
response = Browser.instance.get(wp_item.url)
|
||||
response = Browser.get(wp_item.url)
|
||||
md5sum = Digest::MD5.hexdigest(response.body)
|
||||
|
||||
node.search('hash').each do |hash|
|
||||
|
||||
Reference in New Issue
Block a user