typhoeus 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
data/lib/typhoeus.rb CHANGED
@@ -14,7 +14,7 @@ require 'typhoeus/request'
14
14
  require 'typhoeus/hydra'
15
15
 
16
16
  module Typhoeus
17
- VERSION = "0.1.16"
17
+ VERSION = "0.1.18"
18
18
 
19
19
  def self.easy_object_pool
20
20
  @easy_objects ||= []
data/lib/typhoeus/easy.rb CHANGED
@@ -21,7 +21,8 @@ module Typhoeus
21
21
  :CURLOPT_HTTPAUTH => 107,
22
22
  :CURLOPT_USERPWD => 10000 + 5,
23
23
  :CURLOPT_VERBOSE => 41,
24
- :CURLOPT_PROXY => 10004
24
+ :CURLOPT_PROXY => 10004,
25
+ :CURLOPT_VERIFYPEER => 64
25
26
  }
26
27
  INFO_VALUES = {
27
28
  :CURLINFO_RESPONSE_CODE => 2097154,
@@ -35,42 +36,42 @@ module Typhoeus
35
36
  :CURLAUTH_NTLM => 8,
36
37
  :CURLAUTH_DIGEST_IE => 16
37
38
  }
38
-
39
+
39
40
  def initialize
40
41
  @method = :get
41
42
  @post_dat_set = nil
42
43
  @headers = {}
43
44
  end
44
-
45
+
45
46
  def headers=(hash)
46
47
  @headers = hash
47
48
  end
48
-
49
+
49
50
  def proxy=(proxy)
50
51
  set_option(OPTION_VALUES[:CURLOPT_PROXY], proxy)
51
52
  end
52
-
53
+
53
54
  def auth=(authinfo)
54
55
  set_option(OPTION_VALUES[:CURLOPT_USERPWD], "#{authinfo[:username]}:#{authinfo[:password]}")
55
56
  set_option(OPTION_VALUES[:CURLOPT_HTTPAUTH], authinfo[:method]) if authinfo[:method]
56
57
  end
57
-
58
+
58
59
  def auth_methods
59
60
  get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL])
60
61
  end
61
-
62
+
62
63
  def verbose=(boolean)
63
64
  set_option(OPTION_VALUES[:CURLOPT_VERBOSE], !!boolean ? 1 : 0)
64
65
  end
65
-
66
+
66
67
  def total_time_taken
67
68
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
68
69
  end
69
-
70
+
70
71
  def response_code
71
72
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
72
73
  end
73
-
74
+
74
75
  def follow_location=(boolean)
75
76
  if boolean
76
77
  set_option(OPTION_VALUES[:CURLOPT_FOLLOWLOCATION], 1)
@@ -82,17 +83,17 @@ module Typhoeus
82
83
  def max_redirects=(redirects)
83
84
  set_option(OPTION_VALUES[:CURLOPT_MAXREDIRS], redirects)
84
85
  end
85
-
86
+
86
87
  def timeout=(milliseconds)
87
88
  @timeout = milliseconds
88
89
  set_option(OPTION_VALUES[:CURLOPT_NOSIGNAL], 1)
89
90
  set_option(OPTION_VALUES[:CURLOPT_TIMEOUT_MS], milliseconds)
90
91
  end
91
-
92
+
92
93
  def timed_out?
93
94
  @timeout && total_time_taken > @timeout && response_code == 0
94
95
  end
95
-
96
+
96
97
  def request_body=(request_body)
97
98
  @request_body = request_body
98
99
  if @method == :put
@@ -103,16 +104,20 @@ module Typhoeus
103
104
  self.post_data = request_body
104
105
  end
105
106
  end
106
-
107
+
107
108
  def user_agent=(user_agent)
108
109
  set_option(OPTION_VALUES[:CURLOPT_USERAGENT], user_agent)
109
110
  end
110
-
111
+
111
112
  def url=(url)
112
113
  @url = url
113
114
  set_option(OPTION_VALUES[:CURLOPT_URL], url)
114
115
  end
115
-
116
+
117
+ def disable_ssl_peer_verification
118
+ set_option(OPTION_VALUES[:CURLOPT_VERIFYPEER], 0)
119
+ end
120
+
116
121
  def method=(method)
117
122
  @method = method
118
123
  if method == :get
@@ -127,13 +132,13 @@ module Typhoeus
127
132
  set_option(OPTION_VALUES[:CURLOPT_CUSTOMREQUEST], "DELETE")
128
133
  end
129
134
  end
130
-
135
+
131
136
  def post_data=(data)
132
137
  @post_data_set = true
133
138
  set_option(OPTION_VALUES[:CURLOPT_POSTFIELDS], data)
134
139
  set_option(OPTION_VALUES[:CURLOPT_POSTFIELDSIZE], data.length)
135
140
  end
136
-
141
+
137
142
  def params=(params)
138
143
  params_string = params.keys.collect do |k|
139
144
  value = params[k]
@@ -146,14 +151,14 @@ module Typhoeus
146
151
  "#{Rack::Utils.escape(k.to_s)}=#{Rack::Utils.escape(params[k].to_s)}"
147
152
  end
148
153
  end.flatten.join("&")
149
-
154
+
150
155
  if method == :post
151
156
  self.post_data = params_string
152
157
  else
153
158
  self.url = "#{url}?#{params_string}"
154
159
  end
155
160
  end
156
-
161
+
157
162
  def set_option(option, value)
158
163
  if value.class == String
159
164
  easy_setopt_string(option, value)
@@ -161,42 +166,42 @@ module Typhoeus
161
166
  easy_setopt_long(option, value)
162
167
  end
163
168
  end
164
-
169
+
165
170
  def perform
166
171
  set_headers()
167
172
  easy_perform()
168
173
  response_code()
169
174
  end
170
-
175
+
171
176
  def set_headers
172
177
  headers.each_pair do |key, value|
173
178
  easy_add_header("#{key}: #{value}")
174
179
  end
175
180
  easy_set_headers() unless headers.empty?
176
181
  end
177
-
182
+
178
183
  # gets called when finished and response code is 200-299
179
184
  def success
180
185
  @success.call(self) if @success
181
186
  end
182
-
187
+
183
188
  def on_success(&block)
184
189
  @success = block
185
190
  end
186
-
191
+
187
192
  def on_success=(block)
188
193
  @success = block
189
194
  end
190
-
195
+
191
196
  # gets called when finished and response code is 300-599
192
197
  def failure
193
198
  @failure.call(self) if @failure
194
199
  end
195
-
200
+
196
201
  def on_failure(&block)
197
202
  @failure = block
198
203
  end
199
-
204
+
200
205
  def on_failure=(block)
201
206
  @failure = block
202
207
  end
@@ -204,20 +209,20 @@ module Typhoeus
204
209
  def retries
205
210
  @retries ||= 0
206
211
  end
207
-
212
+
208
213
  def increment_retries
209
214
  @retries ||= 0
210
215
  @retries += 1
211
216
  end
212
-
217
+
213
218
  def max_retries
214
219
  @max_retries ||= 40
215
220
  end
216
-
221
+
217
222
  def max_retries?
218
223
  retries >= max_retries
219
224
  end
220
-
225
+
221
226
  def reset
222
227
  @retries = 0
223
228
  @response_code = 0
@@ -225,19 +230,19 @@ module Typhoeus
225
230
  @response_body = ""
226
231
  easy_reset()
227
232
  end
228
-
233
+
229
234
  def get_info_string(option)
230
235
  easy_getinfo_string(option)
231
236
  end
232
-
237
+
233
238
  def get_info_long(option)
234
239
  easy_getinfo_long(option)
235
240
  end
236
-
241
+
237
242
  def get_info_double(option)
238
243
  easy_getinfo_double(option)
239
244
  end
240
-
245
+
241
246
  def curl_version
242
247
  version
243
248
  end
@@ -125,7 +125,8 @@ module Typhoeus
125
125
  easy.follow_location = request.follow_location if request.follow_location
126
126
  easy.max_redirects = request.max_redirects if request.max_redirects
127
127
  easy.proxy = request.proxy if request.proxy
128
-
128
+ easy.disable_ssl_peer_verification if request.disable_ssl_peer_verification
129
+
129
130
  easy.on_success do |easy|
130
131
  queue_next
131
132
  handle_request(request, response_from_easy(easy, request))
@@ -1,8 +1,8 @@
1
1
  module Typhoeus
2
2
  class Request
3
- attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy
3
+ attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :disable_ssl_peer_verification
4
4
  attr_reader :url
5
-
5
+
6
6
  def initialize(url, options = {})
7
7
  @method = options[:method] || :get
8
8
  @params = options[:params]
@@ -14,6 +14,7 @@ module Typhoeus
14
14
  @follow_location = options[:follow_location]
15
15
  @max_redirects = options[:max_redirects]
16
16
  @proxy = options[:proxy]
17
+ @disable_ssl_peer_verification = options[:disable_ssl_peer_verification]
17
18
 
18
19
  if @method == :post
19
20
  @url = url
@@ -24,7 +25,7 @@ module Typhoeus
24
25
  @after_complete = nil
25
26
  @handled_response = nil
26
27
  end
27
-
28
+
28
29
  def host
29
30
  slash_location = @url.index('/', 8)
30
31
  if slash_location
@@ -34,12 +35,12 @@ module Typhoeus
34
35
  return query_string_location ? @url.slice(0, query_string_location) : @url
35
36
  end
36
37
  end
37
-
38
+
38
39
  def headers
39
40
  @headers["User-Agent"] = @user_agent
40
41
  @headers
41
42
  end
42
-
43
+
43
44
  def params_string
44
45
  params.keys.sort.collect do |k|
45
46
  value = params[k]
@@ -53,65 +54,65 @@ module Typhoeus
53
54
  end
54
55
  end.flatten.join("&")
55
56
  end
56
-
57
+
57
58
  def on_complete(&block)
58
59
  @on_complete = block
59
60
  end
60
-
61
+
61
62
  def on_complete=(proc)
62
63
  @on_complete = proc
63
64
  end
64
-
65
+
65
66
  def after_complete(&block)
66
67
  @after_complete = block
67
68
  end
68
-
69
+
69
70
  def after_complete=(proc)
70
71
  @after_complete = proc
71
72
  end
72
-
73
+
73
74
  def call_handlers
74
75
  if @on_complete
75
76
  @handled_response = @on_complete.call(response)
76
77
  call_after_complete
77
78
  end
78
79
  end
79
-
80
+
80
81
  def call_after_complete
81
82
  @after_complete.call(@handled_response) if @after_complete
82
83
  end
83
-
84
+
84
85
  def handled_response=(val)
85
86
  @handled_response = val
86
87
  end
87
-
88
+
88
89
  def handled_response
89
90
  @handled_response || response
90
91
  end
91
-
92
+
92
93
  def cache_key
93
94
  Digest::SHA1.hexdigest(url)
94
95
  end
95
-
96
+
96
97
  def self.run(url, params)
97
98
  r = new(url, params)
98
99
  Typhoeus::Hydra.hydra.queue r
99
100
  Typhoeus::Hydra.hydra.run
100
101
  r.response
101
102
  end
102
-
103
+
103
104
  def self.get(url, params = {})
104
105
  run(url, params.merge(:method => :get))
105
106
  end
106
-
107
+
107
108
  def self.post(url, params = {})
108
109
  run(url, params.merge(:method => :post))
109
110
  end
110
-
111
+
111
112
  def self.put(url, params = {})
112
113
  run(url, params.merge(:method => :put))
113
114
  end
114
-
115
+
115
116
  def self.delete(url, params = {})
116
117
  run(url, params.merge(:method => :delete))
117
118
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix