arachni-typhoeus 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG.markdown +43 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +30 -0
  5. data/README.textile +6 -0
  6. data/Rakefile +40 -0
  7. data/VERSION +1 -0
  8. data/benchmarks/profile.rb +25 -0
  9. data/benchmarks/vs_nethttp.rb +35 -0
  10. data/examples/twitter.rb +21 -0
  11. data/ext/typhoeus/.gitignore +7 -0
  12. data/ext/typhoeus/extconf.rb +65 -0
  13. data/ext/typhoeus/native.c +11 -0
  14. data/ext/typhoeus/native.h +21 -0
  15. data/ext/typhoeus/typhoeus_easy.c +220 -0
  16. data/ext/typhoeus/typhoeus_easy.h +19 -0
  17. data/ext/typhoeus/typhoeus_multi.c +211 -0
  18. data/ext/typhoeus/typhoeus_multi.h +16 -0
  19. data/lib/typhoeus.rb +58 -0
  20. data/lib/typhoeus/.gitignore +1 -0
  21. data/lib/typhoeus/easy.rb +366 -0
  22. data/lib/typhoeus/filter.rb +28 -0
  23. data/lib/typhoeus/hydra.rb +245 -0
  24. data/lib/typhoeus/hydra/callbacks.rb +24 -0
  25. data/lib/typhoeus/hydra/connect_options.rb +61 -0
  26. data/lib/typhoeus/hydra/stubbing.rb +52 -0
  27. data/lib/typhoeus/hydra_mock.rb +131 -0
  28. data/lib/typhoeus/multi.rb +37 -0
  29. data/lib/typhoeus/normalized_header_hash.rb +58 -0
  30. data/lib/typhoeus/remote.rb +306 -0
  31. data/lib/typhoeus/remote_method.rb +108 -0
  32. data/lib/typhoeus/remote_proxy_object.rb +50 -0
  33. data/lib/typhoeus/request.rb +210 -0
  34. data/lib/typhoeus/response.rb +91 -0
  35. data/lib/typhoeus/service.rb +20 -0
  36. data/lib/typhoeus/utils.rb +24 -0
  37. data/profilers/valgrind.rb +24 -0
  38. data/spec/fixtures/result_set.xml +60 -0
  39. data/spec/servers/app.rb +84 -0
  40. data/spec/spec.opts +2 -0
  41. data/spec/spec_helper.rb +11 -0
  42. data/spec/typhoeus/easy_spec.rb +284 -0
  43. data/spec/typhoeus/filter_spec.rb +35 -0
  44. data/spec/typhoeus/hydra_mock_spec.rb +300 -0
  45. data/spec/typhoeus/hydra_spec.rb +526 -0
  46. data/spec/typhoeus/multi_spec.rb +74 -0
  47. data/spec/typhoeus/normalized_header_hash_spec.rb +41 -0
  48. data/spec/typhoeus/remote_method_spec.rb +141 -0
  49. data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
  50. data/spec/typhoeus/remote_spec.rb +695 -0
  51. data/spec/typhoeus/request_spec.rb +276 -0
  52. data/spec/typhoeus/response_spec.rb +151 -0
  53. data/spec/typhoeus/utils_spec.rb +22 -0
  54. data/typhoeus.gemspec +123 -0
  55. metadata +196 -0
@@ -0,0 +1,28 @@
1
+ module Typhoeus
2
+ class Filter
3
+ attr_reader :method_name
4
+
5
+ def initialize(method_name, options = {})
6
+ @method_name = method_name
7
+ @options = options
8
+ end
9
+
10
+ def apply_filter?(method_name)
11
+ if @options[:only]
12
+ if @options[:only].instance_of? Symbol
13
+ @options[:only] == method_name
14
+ else
15
+ @options[:only].include?(method_name)
16
+ end
17
+ elsif @options[:except]
18
+ if @options[:except].instance_of? Symbol
19
+ @options[:except] != method_name
20
+ else
21
+ !@options[:except].include?(method_name)
22
+ end
23
+ else
24
+ true
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,245 @@
1
+ require 'typhoeus/hydra/callbacks'
2
+ require 'typhoeus/hydra/connect_options'
3
+ require 'typhoeus/hydra/stubbing'
4
+
5
+ module Typhoeus
6
+ class Hydra
7
+ include ConnectOptions
8
+ include Stubbing
9
+ extend Callbacks
10
+
11
+ def initialize(options = {})
12
+ @memoize_requests = true
13
+ @multi = Multi.new
14
+ @easy_pool = []
15
+ initial_pool_size = options[:initial_pool_size] || 10
16
+ @max_concurrency = options[:max_concurrency] || 200
17
+ initial_pool_size.times { @easy_pool << Easy.new }
18
+ @memoized_requests = {}
19
+ @retrieved_from_cache = {}
20
+ @queued_requests = []
21
+ @running_requests = 0
22
+
23
+ self.stubs = []
24
+ @active_stubs = []
25
+ end
26
+
27
+ def self.hydra
28
+ @hydra ||= new
29
+ end
30
+
31
+ def self.hydra=(val)
32
+ @hydra = val
33
+ end
34
+
35
+ #
36
+ # Abort the run on a best-effort basis.
37
+ #
38
+ # It won't abort the current burst of @max_concurrency requests,
39
+ # however it won't fire the rest of the queued requests so the run
40
+ # will be aborted as soon as possible...
41
+ #
42
+ def abort
43
+ @queued_requests.clear
44
+ end
45
+
46
+ def clear_cache_callbacks
47
+ @cache_setter = nil
48
+ @cache_getter = nil
49
+ end
50
+
51
+ def fire_and_forget
52
+ @queued_requests.each {|r| queue(r, false)}
53
+ @multi.fire_and_forget
54
+ end
55
+
56
+ def queue(request, obey_concurrency_limit = true)
57
+ return if assign_to_stub(request)
58
+
59
+ # At this point, we are running over live HTTP. Make sure we haven't
60
+ # disabled live requests.
61
+ check_allow_net_connect!(request)
62
+
63
+ if @running_requests >= @max_concurrency && obey_concurrency_limit
64
+ @queued_requests << request
65
+ else
66
+ if request.method == :get
67
+ if @memoize_requests && @memoized_requests.has_key?(request.url)
68
+ if response = @retrieved_from_cache[request.url]
69
+ request.response = response
70
+ request.call_handlers
71
+ else
72
+ @memoized_requests[request.url] << request
73
+ end
74
+ else
75
+ @memoized_requests[request.url] = [] if @memoize_requests
76
+ get_from_cache_or_queue(request)
77
+ end
78
+ else
79
+ get_from_cache_or_queue(request)
80
+ end
81
+ end
82
+ end
83
+
84
+ def run
85
+ while !@active_stubs.empty?
86
+ m = @active_stubs.first
87
+ while request = m.requests.shift
88
+ response = m.response
89
+ response.request = request
90
+ handle_request(request, response)
91
+ end
92
+ @active_stubs.delete(m)
93
+ end
94
+
95
+ @multi.perform
96
+ ensure
97
+ @multi.reset_easy_handles{|easy| release_easy_object(easy)}
98
+ @memoized_requests = {}
99
+ @retrieved_from_cache = {}
100
+ @running_requests = 0
101
+ end
102
+
103
+ def disable_memoization
104
+ @memoize_requests = false
105
+ end
106
+
107
+ def cache_getter(&block)
108
+ @cache_getter = block
109
+ end
110
+
111
+ def cache_setter(&block)
112
+ @cache_setter = block
113
+ end
114
+
115
+ def on_complete(&block)
116
+ @on_complete = block
117
+ end
118
+
119
+ def on_complete=(proc)
120
+ @on_complete = proc
121
+ end
122
+
123
+ def get_from_cache_or_queue(request)
124
+ if @cache_getter
125
+ val = @cache_getter.call(request)
126
+ if val
127
+ @retrieved_from_cache[request.url] = val
128
+ handle_request(request, val, false)
129
+ else
130
+ @multi.add(get_easy_object(request))
131
+ end
132
+ else
133
+ @multi.add(get_easy_object(request))
134
+ end
135
+ end
136
+ private :get_from_cache_or_queue
137
+
138
+ def get_easy_object(request)
139
+ @running_requests += 1
140
+
141
+ easy = @easy_pool.pop || Easy.new
142
+ easy.verbose = request.verbose
143
+ if request.username || request.password
144
+ auth = { :username => request.username, :password => request.password }
145
+ auth[:method] = Typhoeus::Easy::AUTH_TYPES["CURLAUTH_#{request.auth_method.to_s.upcase}".to_sym] if request.auth_method
146
+ easy.auth = auth
147
+ end
148
+
149
+ if request.proxy
150
+ proxy = { :server => request.proxy }
151
+ proxy[:type] = Typhoeus::Easy::PROXY_TYPES["CURLPROXY_#{request.proxy_type.to_s.upcase}".to_sym] if request.proxy_type
152
+ easy.proxy = proxy if request.proxy
153
+ end
154
+
155
+ if request.proxy_username || request.proxy_password
156
+ auth = { :username => request.proxy_username, :password => request.proxy_password }
157
+ auth[:method] = Typhoeus::Easy::AUTH_TYPES["CURLAUTH_#{request.proxy_auth_method.to_s.upcase}".to_sym] if request.proxy_auth_method
158
+ easy.proxy_auth = auth
159
+ end
160
+
161
+ easy.url = request.url
162
+ easy.method = request.method
163
+ easy.params = request.params if request.method == :post && !request.params.nil?
164
+ easy.headers = request.headers if request.headers
165
+ easy.request_body = request.body if request.body
166
+ easy.timeout = request.timeout if request.timeout
167
+ easy.connect_timeout = request.connect_timeout if request.connect_timeout
168
+ easy.follow_location = request.follow_location if request.follow_location
169
+ easy.max_redirects = request.max_redirects if request.max_redirects
170
+ easy.disable_ssl_peer_verification if request.disable_ssl_peer_verification
171
+ easy.ssl_cert = request.ssl_cert
172
+ easy.ssl_cert_type = request.ssl_cert_type
173
+ easy.ssl_key = request.ssl_key
174
+ easy.ssl_key_type = request.ssl_key_type
175
+ easy.ssl_key_password = request.ssl_key_password
176
+ easy.ssl_cacert = request.ssl_cacert
177
+ easy.ssl_capath = request.ssl_capath
178
+ easy.verbose = request.verbose
179
+
180
+ easy.on_success do |easy|
181
+ queue_next
182
+ handle_request(request, response_from_easy(easy, request))
183
+ release_easy_object(easy)
184
+ end
185
+ easy.on_failure do |easy|
186
+ queue_next
187
+ handle_request(request, response_from_easy(easy, request))
188
+ release_easy_object(easy)
189
+ end
190
+ easy.set_headers
191
+ easy
192
+ end
193
+ private :get_easy_object
194
+
195
+ def queue_next
196
+ @running_requests -= 1
197
+ queue(@queued_requests.pop) unless @queued_requests.empty?
198
+ end
199
+ private :queue_next
200
+
201
+ def release_easy_object(easy)
202
+ easy.reset
203
+ @easy_pool.push easy
204
+ end
205
+ private :release_easy_object
206
+
207
+ def handle_request(request, response, live_request = true)
208
+ request.response = response
209
+
210
+ self.class.run_global_hooks_for(:after_request_before_on_complete,
211
+ request)
212
+
213
+ if live_request && request.cache_timeout && @cache_setter
214
+ @cache_setter.call(request)
215
+ end
216
+ @on_complete.call(response) if @on_complete
217
+
218
+ request.call_handlers
219
+ if requests = @memoized_requests[request.url]
220
+ requests.each do |r|
221
+ r.response = response
222
+ r.call_handlers
223
+ end
224
+ end
225
+ end
226
+ private :handle_request
227
+
228
+ def response_from_easy(easy, request)
229
+ Response.new(:code => easy.response_code,
230
+ :headers => easy.response_header,
231
+ :body => easy.response_body,
232
+ :time => easy.total_time_taken,
233
+ :start_transfer_time => easy.start_transfer_time,
234
+ :app_connect_time => easy.app_connect_time,
235
+ :pretransfer_time => easy.pretransfer_time,
236
+ :connect_time => easy.connect_time,
237
+ :name_lookup_time => easy.name_lookup_time,
238
+ :effective_url => easy.effective_url,
239
+ :curl_return_code => easy.curl_return_code,
240
+ :curl_error_message => easy.curl_error_message,
241
+ :request => request)
242
+ end
243
+ private :response_from_easy
244
+ end
245
+ end
@@ -0,0 +1,24 @@
1
+ module Typhoeus
2
+ class Hydra
3
+ module Callbacks
4
+ def self.extended(base)
5
+ class << base
6
+ attr_accessor :global_hooks
7
+ end
8
+ base.global_hooks = Hash.new { |h, k| h[k] = [] }
9
+ end
10
+
11
+ def after_request_before_on_complete(&block)
12
+ global_hooks[:after_request_before_on_complete] << block
13
+ end
14
+
15
+ def run_global_hooks_for(name, request)
16
+ global_hooks[name].each { |hook| hook.call(request) }
17
+ end
18
+
19
+ def clear_global_hooks
20
+ global_hooks.clear
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,61 @@
1
+ module Typhoeus
2
+ class Hydra
3
+ class NetConnectNotAllowedError < StandardError; end
4
+
5
+ module ConnectOptions
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ # This method checks to see if we should raise an error on
11
+ # a request.
12
+ #
13
+ # @raises NetConnectNotAllowedError
14
+ def check_allow_net_connect!(request)
15
+ return if Typhoeus::Hydra.allow_net_connect?
16
+ return if Typhoeus::Hydra.ignore_hosts.include?(request.host_domain)
17
+
18
+ raise NetConnectNotAllowedError, "Real HTTP requests are not allowed. Unregistered request: #{request.inspect}"
19
+ end
20
+ private :check_allow_net_connect!
21
+
22
+ module ClassMethods
23
+ def self.extended(base)
24
+ class << base
25
+ attr_accessor :allow_net_connect
26
+ attr_accessor :ignore_localhost
27
+ end
28
+ base.allow_net_connect = true
29
+ base.ignore_localhost = false
30
+ end
31
+
32
+ # Returns whether we allow external HTTP connections.
33
+ # Useful for mocking/tests.
34
+ #
35
+ # @return [boolean] true/false
36
+ def allow_net_connect?
37
+ allow_net_connect
38
+ end
39
+
40
+ def ignore_localhost?
41
+ ignore_localhost
42
+ end
43
+
44
+ def ignore_hosts
45
+ @ignore_hosts ||= []
46
+
47
+ if ignore_localhost?
48
+ @ignore_hosts + Typhoeus::Request::LOCALHOST_ALIASES
49
+ else
50
+ @ignore_hosts
51
+ end
52
+ end
53
+
54
+ def ignore_hosts=(hosts)
55
+ @ignore_hosts = hosts
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,52 @@
1
+ module Typhoeus
2
+ class Hydra
3
+ module Stubbing
4
+ module SharedMethods
5
+ def stub(method, url, options = {})
6
+ stubs << HydraMock.new(url, method, options)
7
+ stubs.last
8
+ end
9
+
10
+ def clear_stubs
11
+ self.stubs = []
12
+ end
13
+
14
+ def find_stub_from_request(request)
15
+ stubs.detect { |stub| stub.matches?(request) }
16
+ end
17
+
18
+ def self.extended(base)
19
+ class << base
20
+ attr_accessor :stubs
21
+ end
22
+ base.stubs = []
23
+ end
24
+ end
25
+
26
+ def self.included(base)
27
+ base.extend(SharedMethods)
28
+ base.class_eval do
29
+ attr_accessor :stubs
30
+ end
31
+ end
32
+
33
+ def assign_to_stub(request)
34
+ m = find_stub_from_request(request)
35
+
36
+ # Fallback to global stubs.
37
+ m ||= self.class.find_stub_from_request(request)
38
+
39
+ if m
40
+ m.add_request(request)
41
+ @active_stubs << m
42
+ m
43
+ else
44
+ nil
45
+ end
46
+ end
47
+ private :assign_to_stub
48
+
49
+ include SharedMethods
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,131 @@
1
+ module Typhoeus
2
+ class HydraMock
3
+ attr_reader :url, :method, :requests, :uri
4
+
5
+ def initialize(url, method, options = {})
6
+ @url = url
7
+ @uri = URI.parse(url) if url.kind_of?(String)
8
+ @method = method
9
+ @requests = []
10
+ @options = options
11
+ if @options[:headers]
12
+ @options[:headers] = Typhoeus::NormalizedHeaderHash.new(@options[:headers])
13
+ end
14
+
15
+ @current_response_index = 0
16
+ end
17
+
18
+ def body
19
+ @options[:body]
20
+ end
21
+
22
+ def body?
23
+ @options.has_key?(:body)
24
+ end
25
+
26
+ def headers
27
+ @options[:headers]
28
+ end
29
+
30
+ def headers?
31
+ @options.has_key?(:headers)
32
+ end
33
+
34
+ def add_request(request)
35
+ @requests << request
36
+ end
37
+
38
+ def and_return(val)
39
+ if val.respond_to?(:each)
40
+ @responses = val
41
+ else
42
+ @responses = [val]
43
+ end
44
+
45
+ # make sure to mark them as a mock.
46
+ @responses.each { |r| r.mock = true }
47
+
48
+ val
49
+ end
50
+
51
+ def response
52
+ if @current_response_index == (@responses.length - 1)
53
+ @responses.last
54
+ else
55
+ value = @responses[@current_response_index]
56
+ @current_response_index += 1
57
+ value
58
+ end
59
+ end
60
+
61
+ def matches?(request)
62
+ if !method_matches?(request) or !url_matches?(request)
63
+ return false
64
+ end
65
+
66
+ if body?
67
+ return false unless body_matches?(request)
68
+ end
69
+
70
+ if headers?
71
+ return false unless headers_match?(request)
72
+ end
73
+
74
+ true
75
+ end
76
+
77
+ private
78
+ def method_matches?(request)
79
+ self.method == :any or self.method == request.method
80
+ end
81
+
82
+ def url_matches?(request)
83
+ if url.kind_of?(String)
84
+ request_uri = URI.parse(request.url)
85
+ request_uri == self.uri
86
+ else
87
+ self.url =~ request.url
88
+ end
89
+ end
90
+
91
+ def body_matches?(request)
92
+ !request.body.nil? && !request.body.empty? && request.body == self.body
93
+ end
94
+
95
+ def headers_match?(request)
96
+ request_headers = NormalizedHeaderHash.new(request.headers)
97
+
98
+ if empty_headers?(self.headers)
99
+ empty_headers?(request_headers)
100
+ else
101
+ return false if empty_headers?(request_headers)
102
+
103
+ headers.each do |key, value|
104
+ return false unless header_value_matches?(value, request_headers[key])
105
+ end
106
+
107
+ true
108
+ end
109
+ end
110
+
111
+ def header_value_matches?(mock_value, request_value)
112
+ mock_arr = mock_value.is_a?(Array) ? mock_value : [mock_value]
113
+ request_arr = request_value.is_a?(Array) ? request_value : [request_value]
114
+
115
+ return false unless mock_arr.size == request_arr.size
116
+ mock_arr.all? do |value|
117
+ request_arr.any? { |a| value === a }
118
+ end
119
+ end
120
+
121
+ def empty_headers?(headers)
122
+ # We consider the default User-Agent header to be empty since
123
+ # Typhoeus always adds that.
124
+ headers.nil? || headers.empty? || default_typhoeus_headers?(headers)
125
+ end
126
+
127
+ def default_typhoeus_headers?(headers)
128
+ headers.size == 1 && headers['User-Agent'] == Typhoeus::USER_AGENT
129
+ end
130
+ end
131
+ end