Module: Wpxf::Net::HttpClient
- Includes:
- HttpOptions, TyphoeusHelper, UserAgent
- Included in:
- Module
- Defined in:
- lib/wpxf/net/http_client.rb
Overview
Provides HTTP client functionality.
Instance Method Summary collapse
-
#base_http_headers ⇒ Hash
The base headers to be used in HTTP requests.
-
#base_uri ⇒ String
Returns the base URI string.
-
#download_file(opts) ⇒ HttpResponse?
Stream a response directly to a file (leaves the body attribute empty).
-
#execute_delete_request(opts) ⇒ HttpResponse?
Execute a HTTP DELETE request.
-
#execute_get_request(opts) ⇒ HttpResponse?
Execute a HTTP GET request.
-
#execute_post_request(opts) ⇒ HttpResponse?
Execute a HTTP POST request.
-
#execute_put_request(opts) ⇒ HttpResponse?
Execute a HTTP PUT request.
-
#execute_queued_requests ⇒ Object
Execute multiple HTTP requests in parallel queued by #queue_request.
-
#execute_request(opts) ⇒ HttpResponse?
Execute a HTTP request.
-
#full_uri ⇒ String
Returns the full URI string including the target path.
-
#initialize ⇒ Object
Initialize a new instance of HttpClient.
-
#initialize_advanced_options ⇒ Object
Initialize the advanced HTTP options for the module.
-
#initialize_options ⇒ Object
Initialize the basic HTTP options for the module.
-
#max_http_concurrency ⇒ Integer
The maximum number of threads to use when using #execute_queued_requests.
-
#normalize_relative_uri(uri) ⇒ String
Normalize a relative URI into an absolute URL.
-
#normalize_uri(*parts) ⇒ String
Normalize a URI to remove duplicated slashes and ensure a slash at the start of the string if it doesn't start with http:// or https://.
-
#queue_request(opts, &callback) ⇒ Object
Queue a HTTP request to be executed later by #execute_queued_requests.
-
#target_host ⇒ String
The target host address.
-
#target_port ⇒ Integer
The target port number that the application is on.
-
#target_uri ⇒ String
The base path to the WordPress application.
Methods included from UserAgent
#clients_by_frequency, #random_browser_and_os, #random_chrome_platform_string, #random_firefox_platform_string, #random_firefox_version_string, #random_iexplorer_platform_string, #random_opera_platform_string, #random_processor_string, #random_safari_platform_string, #random_time_string, #random_user_agent
Methods included from Versioning::OSVersions
#random_nt_version, #random_osx_version
Methods included from Versioning::BrowserVersions
#random_chrome_build_number, #random_chrome_version, #random_ie_version, #random_opera_version, #random_presto_version, #random_presto_version2, #random_safari_build_number, #random_safari_version, #random_trident_version
Instance Method Details
#base_http_headers ⇒ Hash
Returns the base headers to be used in HTTP requests.
93 94 95 96 97 98 99 100 101 |
# File 'lib/wpxf/net/http_client.rb', line 93 def base_http_headers headers = { 'User-Agent' => datastore['user_agent'] } unless datastore['vhost'].nil? || datastore['vhost'].empty? headers['Host'] = datastore['vhost'] end headers end |
#base_uri ⇒ String
Returns the base URI string.
80 81 82 83 84 |
# File 'lib/wpxf/net/http_client.rb', line 80 def base_uri uri_scheme = normalized_option_value('ssl') ? 'https' : 'http' uri_port = target_port == 80 ? '' : ":#{target_port}" "#{uri_scheme}://#{target_host}#{uri_port}" end |
#download_file(opts) ⇒ HttpResponse?
Stream a response directly to a file (leaves the body attribute empty).
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/wpxf/net/http_client.rb', line 138 def download_file(opts) target_file = File.open(opts[:local_filename], 'wb') req = create_typhoeus_request(opts) req.on_headers do |response| return Wpxf::Net::HttpResponse.new(response) if response.code != 200 end req.on_body do |chunk| target_file.write(chunk) end req.on_complete do |response| target_file.close resp = Wpxf::Net::HttpResponse.new(response) resp.body = File.read(opts[:local_filename]) return resp end req.run end |
#execute_delete_request(opts) ⇒ HttpResponse?
Execute a HTTP DELETE request.
184 185 186 |
# File 'lib/wpxf/net/http_client.rb', line 184 def execute_delete_request(opts) execute_request(opts.merge(method: :delete)) end |
#execute_get_request(opts) ⇒ HttpResponse?
Execute a HTTP GET request.
163 164 165 |
# File 'lib/wpxf/net/http_client.rb', line 163 def execute_get_request(opts) execute_request(opts.merge(method: :get)) end |
#execute_post_request(opts) ⇒ HttpResponse?
Execute a HTTP POST request.
170 171 172 |
# File 'lib/wpxf/net/http_client.rb', line 170 def execute_post_request(opts) execute_request(opts.merge(method: :post)) end |
#execute_put_request(opts) ⇒ HttpResponse?
Execute a HTTP PUT request.
177 178 179 |
# File 'lib/wpxf/net/http_client.rb', line 177 def execute_put_request(opts) execute_request(opts.merge(method: :put)) end |
#execute_queued_requests ⇒ Object
Execute multiple HTTP requests in parallel queued by #queue_request.
117 118 119 |
# File 'lib/wpxf/net/http_client.rb', line 117 def execute_queued_requests @hydra.run end |
#execute_request(opts) ⇒ HttpResponse?
Execute a HTTP request.
124 125 126 127 128 129 130 131 132 |
# File 'lib/wpxf/net/http_client.rb', line 124 def execute_request(opts) req = create_typhoeus_request(opts) req.on_complete do |res| return Wpxf::Net::HttpResponse.new(res) end emit_info "Requesting #{req.url}...", true req.run end |
#full_uri ⇒ String
Returns the full URI string including the target path.
88 89 90 |
# File 'lib/wpxf/net/http_client.rb', line 88 def full_uri normalize_uri(base_uri, target_uri) end |
#initialize ⇒ Object
Initialize a new instance of Wpxf::Net::HttpClient.
14 15 16 17 18 19 20 21 |
# File 'lib/wpxf/net/http_client.rb', line 14 def initialize super @hydra = Typhoeus::Hydra.new(max_concurrency: max_http_concurrency) end |
#initialize_advanced_options ⇒ Object
Initialize the advanced HTTP options for the module.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wpxf/net/http_client.rb', line 36 def ([ HTTP_OPTION_BASIC_AUTH_CREDS, HTTP_OPTION_PROXY_AUTH_CREDS, HTTP_OPTION_HOST_VERIFICATION, HTTP_OPTION_MAX_CONCURRENCY, HTTP_OPTION_CLIENT_TIMEOUT, HTTP_OPTION_USER_AGENT, HTTP_OPTION_FOLLOW_REDIRECT, HTTP_OPTION_PEER_VERIFICATION ]) set_option_value('user_agent', random_user_agent) end |
#initialize_options ⇒ Object
Initialize the basic HTTP options for the module.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/wpxf/net/http_client.rb', line 24 def ([ HTTP_OPTION_HOST, HTTP_OPTION_PORT, HTTP_OPTION_SSL, HTTP_OPTION_VHOST, HTTP_OPTION_PROXY, HTTP_OPTION_TARGET_URI ]) end |
#max_http_concurrency ⇒ Integer
Returns the maximum number of threads to use when using #execute_queued_requests.
190 191 192 |
# File 'lib/wpxf/net/http_client.rb', line 190 def max_http_concurrency normalized_option_value('max_http_concurrency') end |
#normalize_relative_uri(uri) ⇒ String
Normalize a relative URI into an absolute URL.
197 198 199 200 201 202 203 |
# File 'lib/wpxf/net/http_client.rb', line 197 def normalize_relative_uri(uri) if uri.start_with?('/') normalize_uri(full_uri, uri) else uri end end |
#normalize_uri(*parts) ⇒ String
Normalize a URI to remove duplicated slashes and ensure a slash at the start of the string if it doesn't start with http:// or https://.
70 71 72 73 74 75 76 |
# File 'lib/wpxf/net/http_client.rb', line 70 def normalize_uri(*parts) path = parts * '/' path = '/' + path unless path.start_with?('/', 'http://', 'https://') url = URI.parse(path) url.path.squeeze!('/') url.to_s end |
#queue_request(opts, &callback) ⇒ Object
Queue a HTTP request to be executed later by #execute_queued_requests.
106 107 108 109 110 111 112 113 114 |
# File 'lib/wpxf/net/http_client.rb', line 106 def queue_request(opts, &callback) req = create_typhoeus_request(opts) req.on_complete do |res| callback.call(Wpxf::Net::HttpResponse.new(res)) if callback end @hydra.queue req @hydra.queued_requests end |
#target_host ⇒ String
Returns the target host address.
62 63 64 |
# File 'lib/wpxf/net/http_client.rb', line 62 def target_host normalized_option_value('host') end |
#target_port ⇒ Integer
Returns the target port number that the application is on.
57 58 59 |
# File 'lib/wpxf/net/http_client.rb', line 57 def target_port normalized_option_value('port') end |
#target_uri ⇒ String
Returns the base path to the WordPress application.
52 53 54 |
# File 'lib/wpxf/net/http_client.rb', line 52 def target_uri datastore['target_uri'] end |