typhoeus 0.1.24 → 0.1.31

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.
@@ -0,0 +1,23 @@
1
+ 0.1.31
2
+ ------
3
+ * Fixed bug in setting compression encoding [morhekil]
4
+ * Exposed authentication control methods through Request interface [morhekil]
5
+
6
+ 0.1.30
7
+ -----------
8
+ * Exposed CURLOPT_CONNECTTIMEOUT_MS to Requests [balexis]
9
+
10
+ 0.1.29
11
+ ------
12
+ * Fixed a memory corruption with using CURLOPT_POSTFIELDS [gravis,
13
+ 32531d0821aecc4]
14
+
15
+ 0.1.28
16
+ ----------------
17
+ * Added SSL cert options for Typhoeus::Easy [GH-25, gravis]
18
+ * Ported SSL cert options to Typhoeus::Request interface [gravis]
19
+ * Added support for any HTTP method (purge for Varnish) [ryana]
20
+
21
+ 0.1.27
22
+ ------
23
+ * Added rack as dependency, added dev dependencies to Rakefile [GH-21]
data/README.textile ADDED
@@ -0,0 +1,333 @@
1
+ h1. Typhoeus
2
+
3
+ "http://github.com/pauldix/typhoeus/tree/master":http://github.com/pauldix/typhoeus/tree/master
4
+
5
+ "the mailing list":http://groups.google.com/group/typhoeus
6
+
7
+ Thanks to my employer "kgbweb":http://kgbweb.com for allowing me to release this as open source. Btw, we're hiring and we work on cool stuff like this every day. Get a hold of me if you rock at rails/js/html/css or if you have experience in search, information retrieval, and machine learning.
8
+
9
+ I also wanted to thank Todd A. Fisher. I ripped a good chunk of the c libcurl-multi code from his update to Curb. Awesome stuff Todd!
10
+
11
+ h2. Summary
12
+
13
+ Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic. To be a little more specific, it's a library for accessing web services in Ruby. It's specifically designed for building RESTful service oriented architectures in Ruby that need to be fast enough to process calls to multiple services within the client's HTTP request/response life cycle.
14
+
15
+ Some of the awesome features are parallel request execution, memoization of request responses (so you don't make the same request multiple times in a single group), built in support for caching responses to memcached (or whatever), and mocking capability baked in. It uses libcurl and libcurl-multi to work this speedy magic. I wrote the c bindings myself so it's yet another Ruby libcurl library, but with some extra awesomeness added in.
16
+
17
+ h2. Installation
18
+
19
+ Typhoeus requires you to have a current version of libcurl installed. I've tested this with 7.19.4 and higher.
20
+ <pre>
21
+ gem install typhoeus --source http://gemcutter.org
22
+ </pre>
23
+ If you're on Debian or Ubuntu and getting errors while trying to install, it could be because you don't have the latest version of libcurl installed. Do this to fix:
24
+ <pre>
25
+ sudo apt-get install libcurl4-gnutls-dev
26
+ </pre>
27
+ There's also something built in so that if you have a super old version of curl that you can't get rid of for some reason, you can install in a user directory and specify that during installation like so:
28
+ <pre>
29
+ gem install typhoeus --source http://gemcutter.org -- --with-curl=/usr/local/curl/7.19.7/
30
+ </pre>
31
+
32
+ Another problem could be if you are running Mac Ports and you have libcurl installed through there. You need to uninstall it for Typhoeus to work! The version in Mac Ports is old and doesn't play nice. You should "download curl":http://curl.haxx.se/download.html and build from source. Then you'll have to install the gem again.
33
+
34
+ If you're still having issues, please let me know on "the mailing list":http://groups.google.com/group/typhoeus.
35
+
36
+ There's one other thing you should know. The Easy object (which is just a libcurl thing) allows you to set timeout values in milliseconds. However, for this to work you need to build libcurl with c-ares support built in.
37
+
38
+ h2. Usage
39
+
40
+ *Deprecation Warning!*
41
+ The old version of Typhoeus used a module that you included in your class to get functionality. That interface has been deprecated. Here is the new interface.
42
+
43
+ The primary interface for Typhoeus is comprised of three classes: Request, Response, and Hydra. Request represents an HTTP request object, response represents an HTTP response, and Hydra manages making parallel HTTP connections.
44
+
45
+ <pre>
46
+ require 'rubygems'
47
+ require 'typhoeus'
48
+ require 'json'
49
+
50
+ # the request object
51
+ request = Typhoeus::Request.new("http://www.pauldix.net",
52
+ :body => "this is a request body",
53
+ :method => :post,
54
+ :headers => {:Accepts => "text/html"},
55
+ :timeout => 100, # milliseconds
56
+ :cache_timeout => 60, # seconds
57
+ :params => {:field1 => "a field"})
58
+ # we can see from this that the first argument is the url. the second is a set of options.
59
+ # the options are all optional. The default for :method is :get. Timeout is measured in milliseconds.
60
+ # cache_timeout is measured in seconds.
61
+
62
+ # the response object will be set after the request is run
63
+ response = request.response
64
+ response.code # http status code
65
+ response.time # time in seconds the request took
66
+ response.headers # the http headers
67
+ response.headers_hash # http headers put into a hash
68
+ response.body # the response body
69
+ </pre>
70
+
71
+ *Making Quick Requests*
72
+ The request object has some convenience methods for performing single HTTP requests. The arguments are the same as those you pass into the request constructor.
73
+
74
+ <pre>
75
+ response = Typhoeus::Request.get("http://www.pauldix.net")
76
+ response = Typhoeus::Request.head("http://www.pauldix.net")
77
+ response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
78
+ response = Typhoeus::Request.post("http://localhost:3000/posts", :params => {:title => "test post", :content => "this is my test"})
79
+ response = Typhoeus::Request.delete("http://localhost:3000/posts/1")
80
+ </pre>
81
+
82
+ *Making Parallel Requests*
83
+
84
+ <pre>
85
+ # Generally, you should be running requests through hydra. Here is how that looks
86
+ hydra = Typhoeus::Hydra.new
87
+
88
+ first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
89
+ first_request.on_complete do |response|
90
+ post = JSON.parse(response.body)
91
+ third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
92
+ third_request.on_complete do |response|
93
+ # do something with that
94
+ end
95
+ hydra.queue third_request
96
+ return post
97
+ end
98
+ second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
99
+ second_request.on_complete do |response|
100
+ JSON.parse(response.body)
101
+ end
102
+ hydra.queue first_request
103
+ hydra.queue second_request
104
+ hydra.run # this is a blocking call that returns once all requests are complete
105
+
106
+ first_request.handled_response # the value returned from the on_complete block
107
+ second_request.handled_response # the value returned from the on_complete block (parsed JSON)
108
+ </pre>
109
+
110
+ The execution of that code goes something like this. The first and second requests are built and queued. When hydra is run the first and second requests run in parallel. When the first request completes, the third request is then built and queued up. The moment it is queued Hydra starts executing it. Meanwhile the second request would continue to run (or it could have completed before the first). Once the third request is done, hydra.run returns.
111
+
112
+ *Specifying Max Concurrency*
113
+
114
+ Hydra will also handle how many requests you can make in parallel. Things will get flakey if you try to make too many requests at the same time. The built in limit is 200. When more requests than that are queued up, hydra will save them for later and start the requests as others are finished. You can raise or lower the concurrency limit through the Hydra constructor.
115
+
116
+ <pre>
117
+ hydra = Typhoeus::Hydra.new(:max_concurrency => 20) # keep from killing some servers
118
+ </pre>
119
+
120
+ *Memoization*
121
+ Hydra memoizes requests within a single run call. You can also disable memoization.
122
+
123
+ <pre>
124
+ hydra = Typhoeus::Hydra.new
125
+ 2.times do
126
+ r = Typhoeus::Request.new("http://localhost/3000/users/1")
127
+ hydra.queue r
128
+ end
129
+ hydra.run # this will result in a single request being issued. However, the on_complete handlers of both will be called.
130
+ hydra.disable_memoization
131
+ 2.times do
132
+ r = Typhoeus::Request.new("http://localhost/3000/users/1")
133
+ hydra.queue r
134
+ end
135
+ hydra.run # this will result in a two requests.
136
+ </pre>
137
+
138
+ *Caching*
139
+ Hydra includes built in support for creating cache getters and setters. In the following example, if there is a cache hit, the cached object is passed to the on_complete handler of the request object.
140
+
141
+ <pre>
142
+ hydra = Typhoeus::Hydra.new
143
+ hydra.cache_setter do |request|
144
+ @cache.set(request.cache_key, request.response, request.cache_timeout)
145
+ end
146
+
147
+ hydra.cache_getter do |request|
148
+ @cache.get(request.cache_key) rescue nil
149
+ end
150
+ </pre>
151
+
152
+ *Stubbing*
153
+ Hydra allows you to stub out specific urls and patters to avoid hitting remote servers while testing.
154
+
155
+ <pre>
156
+ hydra = Typhoeus::Hydra.new
157
+ response = Response.new(:code => 200, :headers => "", :body => "{'name' : 'paul'}", :time => 0.3)
158
+ hydra.stub(:get, "http://localhost:3000/users/1").and_return(response)
159
+
160
+ request = Typhoeus::Request.new("http://localhost:3000/users/1")
161
+ request.on_complete do |response|
162
+ JSON.parse(response.body)
163
+ end
164
+ hydra.queue request
165
+ hydra.run
166
+ </pre>
167
+
168
+ The queued request will hit the stub. The on_complete handler will be called and will be passed the response object. You can also specify a regex to match urls.
169
+
170
+ <pre>
171
+ hydra.stub(:get, /http\:\/\/localhost\:3000\/users\/.*/).and_return(response)
172
+ # any requests for a user will be stubbed out with the pre built response.
173
+ </pre>
174
+
175
+ *The Singleton*
176
+ All of the quick requests are done using the singleton hydra object. If you want to enable caching or stubbing on the quick requests, set those options on the singleton.
177
+
178
+ <pre>
179
+ hydra = Typhoeus::Hydra.hydra
180
+ hydra.stub(:get, "http://localhost:3000/users")
181
+ </pre>
182
+
183
+ *Basic Authentication*
184
+
185
+ <pre>
186
+ response = Typhoeus::Request.get("http://twitter.com/statuses/followers.json",
187
+ :username => username, :password => password)
188
+ </pre>
189
+
190
+ *SSL*
191
+ SSL comes built in to libcurl so it's in Typhoeus as well. If you pass in a url with "https" it should just work assuming that you have your "cert bundle":http://curl.haxx.se/docs/caextract.html in order and the server is verifiable. You must also have libcurl built with SSL support enabled. You can check that by doing this:
192
+
193
+ <pre>
194
+ Typhoeus::Easy.new.curl_version # output should include OpenSSL/...
195
+ </pre>
196
+
197
+ Now, even if you have libcurl built with OpenSSL you may still have a messed up cert bundle or if you're hitting a non-verifiable SSL server then you'll have to disable peer verification to make SSL work. Like this:
198
+
199
+ <pre>
200
+ Typhoeus::Request.get("https://mail.google.com/mail", :disable_ssl_peer_verification => true)
201
+ </pre>
202
+
203
+ *LibCurl*
204
+ Typhoeus also has a more raw libcurl interface. These are the Easy and Multi objects. If you're into accessing just the raw libcurl style, those are your best bet.
205
+
206
+ SSL Certs can be provided to the Easy interface :
207
+
208
+ <pre>
209
+ e = Typhoeus::Easy.new
210
+ e.url = "https://example.com/action"
211
+ s.ssl_cacert = "ca_file.cer"
212
+ e.ssl_cert = "acert.crt"
213
+ e.ssl_key = "akey.key"
214
+ [...]
215
+ e.perform
216
+ </pre>
217
+
218
+ or directly to a Typhoeus::Request :
219
+
220
+ <pre>
221
+ e = Typhoeus::Request.get("https://example.com/action",
222
+ :ssl_cacert => "ca_file.cer",
223
+ :ssl_cert => "acert.crt",
224
+ :ssl_key => "akey.key",
225
+ [...]
226
+ end
227
+ </pre>
228
+
229
+ h2. Advanced authentication
230
+
231
+ Thanks for the authentication piece and this description go to Oleg Ivanov (morhekil). The major reason to start this fork was the need to perform NTLM authentication in Ruby, and other libcurl's authentications method were made possible as a result. Now you can do it via Typhoeus::Easy interface using the following API.
232
+
233
+ <pre>
234
+ e = Typhoeus::Easy.new
235
+ e.auth = {
236
+ :username => 'username',
237
+ :password => 'password',
238
+ :method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM]
239
+ }
240
+ e.url = "http://example.com/auth_ntlm"
241
+ e.method = :get
242
+ e.perform
243
+ </pre>
244
+
245
+ *Other authentication types*
246
+
247
+ The following authentication types are available:
248
+ * CURLAUTH_BASIC
249
+ * CURLAUTH_DIGEST
250
+ * CURLAUTH_GSSNEGOTIATE
251
+ * CURLAUTH_NTLM
252
+ * CURLAUTH_DIGEST_IE
253
+ * CURLAUTH_AUTO
254
+
255
+ The last one (CURLAUTH_AUTO) is really a combination of all previous methods and is provided by Typhoeus for convenience. When you set authentication to auto, Typhoeus will retrieve the given URL first and examine it's headers to confirm what auth types are supported by the server. The it will select the strongest of available auth methods and will send the second request using the selected authentication method.
256
+
257
+ *Authentication via the quick request interface*
258
+
259
+ There's also an easy way to perform any kind of authentication via the quick request interface:
260
+
261
+ <pre>
262
+ e = Typhoeus::Request.get("http://example.com",
263
+ :username => 'username',
264
+ :password => 'password',
265
+ :method => :ntlm)
266
+ </pre>
267
+
268
+ All methods listed above is available in a shorter form - :basic, :digest, :gssnegotiate, :ntlm, :digest_ie, :auto.
269
+
270
+ *Query of available auth types*
271
+
272
+ After the initial request you can get the authentication types available on the server via Typhoues::Easy#auth_methods call. It will return a number
273
+ that you'll need to decode yourself, please refer to easy.rb source code to see the numeric values of different auth types.
274
+
275
+ h2. Verbose debug output
276
+
277
+ Sometime it's useful to see verbose output from curl. You may now enable it:
278
+
279
+ <pre>
280
+ e = Typhoeus::Easy.new
281
+ e.verbose = 1
282
+ </pre>
283
+
284
+ or using the quick request:
285
+
286
+ <pre>
287
+ e = Typhoeus::Request.get("http://example.com", :verbose => true)
288
+ </pre>
289
+
290
+ Just remember that libcurl prints it's debug output to the console (to STDERR), so you'll need to run your scripts from the console to see it.
291
+
292
+ h2. Benchmarks
293
+
294
+ I set up a benchmark to test how the parallel performance works vs Ruby's built in NET::HTTP. The setup was a local evented HTTP server that would take a request, sleep for 500 milliseconds and then issued a blank response. I set up the client to call this 20 times. Here are the results:
295
+
296
+ <pre>
297
+ net::http 0.030000 0.010000 0.040000 ( 10.054327)
298
+ typhoeus 0.020000 0.070000 0.090000 ( 0.508817)
299
+ </pre>
300
+
301
+ We can see from this that NET::HTTP performs as expected, taking 10 seconds to run 20 500ms requests. Typhoeus only takes 500ms (the time of the response that took the longest.) One other thing to note is that Typhoeus keeps a pool of libcurl Easy handles to use. For this benchmark I warmed the pool first. So if you test this out it may be a bit slower until the Easy handle pool has enough in it to run all the simultaneous requests. For some reason the easy handles can take quite some time to allocate.
302
+
303
+ h2. Next Steps
304
+
305
+ * Add in ability to keep-alive requests and reuse them within hydra.
306
+ * Add support for automatic retry, exponential back-off, and queuing for later.
307
+
308
+ h2. LICENSE
309
+
310
+ (The MIT License)
311
+
312
+ Copyright (c) 2009:
313
+
314
+ "Paul Dix":http://pauldix.net
315
+
316
+ Permission is hereby granted, free of charge, to any person obtaining
317
+ a copy of this software and associated documentation files (the
318
+ 'Software'), to deal in the Software without restriction, including
319
+ without limitation the rights to use, copy, modify, merge, publish,
320
+ distribute, sublicense, and/or sell copies of the Software, and to
321
+ permit persons to whom the Software is furnished to do so, subject to
322
+ the following conditions:
323
+
324
+ The above copyright notice and this permission notice shall be
325
+ included in all copies or substantial portions of the Software.
326
+
327
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
328
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
329
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
330
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
331
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
332
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
333
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require "spec"
2
+ require "spec/rake/spectask"
3
+ require 'lib/typhoeus.rb'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "typhoeus"
9
+ gemspec.summary = "A library for interacting with web services (and building SOAs) at blinding speed."
10
+ gemspec.description = "Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic."
11
+ gemspec.email = "paul@pauldix.net"
12
+ gemspec.homepage = "http://github.com/pauldix/typhoeus"
13
+ gemspec.authors = ["Paul Dix"]
14
+ gemspec.add_dependency "rack"
15
+ gemspec.add_development_dependency "rspec"
16
+ gemspec.add_development_dependency "jeweler"
17
+ gemspec.add_development_dependency "diff-lcs"
18
+ gemspec.add_development_dependency "sinatra"
19
+ gemspec.add_development_dependency "json"
20
+ end
21
+
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler not available. Install it with: gem install jeweler"
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new do |t|
28
+ t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
29
+ t.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ task :install do
33
+ rm_rf "*.gem"
34
+ puts `gem build typhoeus.gemspec`
35
+ puts `sudo gem install typhoeus-#{Typhoeus::VERSION}.gem`
36
+ end
37
+
38
+ desc "Run all the tests"
39
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.31
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../lib/typhoeus.rb'
2
+ require 'rubygems'
3
+ require 'ruby-prof'
4
+
5
+ calls = 20
6
+ @klass = Class.new do
7
+ include Typhoeus
8
+ end
9
+
10
+ Typhoeus.init_easy_objects
11
+
12
+ RubyProf.start
13
+
14
+ responses = []
15
+ calls.times do |i|
16
+ responses << @klass.get("http://127.0.0.1:3000/#{i}")
17
+ end
18
+
19
+ responses.each {|r| }#raise unless r.response_body == "whatever"}
20
+
21
+ result = RubyProf.stop
22
+
23
+ # Print a flat profile to text
24
+ printer = RubyProf::FlatPrinter.new(result)
25
+ printer.print(STDOUT, 0)
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + '/../lib/typhoeus.rb'
3
+ require 'open-uri'
4
+ require 'benchmark'
5
+ include Benchmark
6
+
7
+
8
+ calls = 20
9
+ @klass = Class.new do
10
+ include Typhoeus
11
+ end
12
+
13
+ Typhoeus.init_easy_object_pool
14
+
15
+ benchmark do |t|
16
+ t.report("net::http") do
17
+ responses = []
18
+
19
+ calls.times do |i|
20
+ responses << open("http://127.0.0.1:3000/#{i}").read
21
+ end
22
+
23
+ responses.each {|r| raise unless r == "whatever"}
24
+ end
25
+
26
+ t.report("typhoeus") do
27
+ responses = []
28
+
29
+ calls.times do |i|
30
+ responses << @klass.get("http://127.0.0.1:3000/#{i}")
31
+ end
32
+
33
+ responses.each {|r| raise unless r.body == "whatever"}
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + '/../lib/typhoeus.rb'
3
+ require 'json'
4
+
5
+ class Twitter
6
+ include Typhoeus
7
+ remote_defaults :on_success => lambda {|response| JSON.parse(response.body)},
8
+ :on_failure => lambda {|response| puts "error code: #{response.code}"},
9
+ :base_uri => "http://search.twitter.com"
10
+
11
+ define_remote_method :search, :path => '/search.json'
12
+ define_remote_method :trends, :path => '/trends/:time_frame.json'
13
+ end
14
+
15
+ tweets = Twitter.search(:params => {:q => "railsconf"})
16
+ trends = Twitter.trends(:time_frame => :current)
17
+
18
+ # and then the calls don't actually happen until the first time you
19
+ # call a method on one of the objects returned from the remote_method
20
+
21
+ puts tweets.keys # it's a hash from parsed JSON
data/lib/typhoeus/easy.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Typhoeus
2
2
  class Easy
3
- attr_reader :response_body, :response_header, :method, :headers, :url
3
+ attr_reader :response_body, :response_header, :method, :headers, :url, :params
4
4
  attr_accessor :start_time
5
5
 
6
+ # These integer codes are available in curl/curl.h
6
7
  CURLINFO_STRING = 1048576
7
8
  OPTION_VALUES = {
8
9
  :CURLOPT_URL => 10002,
@@ -11,9 +12,14 @@ module Typhoeus
11
12
  :CURLOPT_UPLOAD => 46,
12
13
  :CURLOPT_CUSTOMREQUEST => 10036,
13
14
  :CURLOPT_POSTFIELDS => 10015,
15
+ :CURLOPT_COPYPOSTFIELDS => 10165,
14
16
  :CURLOPT_POSTFIELDSIZE => 60,
15
17
  :CURLOPT_USERAGENT => 10018,
16
18
  :CURLOPT_TIMEOUT_MS => 155,
19
+ # Time-out connect operations after this amount of milliseconds.
20
+ # [Only works on unix-style/SIGALRM operating systems. IOW, does
21
+ # not work on Windows.
22
+ :CURLOPT_CONNECTTIMEOUT_MS => 156,
17
23
  :CURLOPT_NOSIGNAL => 99,
18
24
  :CURLOPT_HTTPHEADER => 10023,
19
25
  :CURLOPT_FOLLOWLOCATION => 52,
@@ -23,25 +29,37 @@ module Typhoeus
23
29
  :CURLOPT_VERBOSE => 41,
24
30
  :CURLOPT_PROXY => 10004,
25
31
  :CURLOPT_VERIFYPEER => 64,
26
- :CURLOPT_NOBODY => 44
32
+ :CURLOPT_NOBODY => 44,
33
+ :CURLOPT_ENCODING => 10000 + 102,
34
+ :CURLOPT_SSLCERT => 10025,
35
+ :CURLOPT_SSLCERTTYPE => 10086,
36
+ :CURLOPT_SSLKEY => 10087,
37
+ :CURLOPT_SSLKEYTYPE => 10088,
38
+ :CURLOPT_KEYPASSWD => 10026,
39
+ :CURLOPT_CAINFO => 10065,
40
+ :CURLOPT_CAPATH => 10097
27
41
  }
28
42
  INFO_VALUES = {
29
43
  :CURLINFO_RESPONSE_CODE => 2097154,
30
44
  :CURLINFO_TOTAL_TIME => 3145731,
31
- :CURLINFO_HTTPAUTH_AVAIL => 0x200000 + 23
45
+ :CURLINFO_HTTPAUTH_AVAIL => 0x200000 + 23,
46
+ :CURLINFO_EFFECTIVE_URL => 0x100000 + 1
32
47
  }
33
48
  AUTH_TYPES = {
34
49
  :CURLAUTH_BASIC => 1,
35
50
  :CURLAUTH_DIGEST => 2,
36
51
  :CURLAUTH_GSSNEGOTIATE => 4,
37
52
  :CURLAUTH_NTLM => 8,
38
- :CURLAUTH_DIGEST_IE => 16
53
+ :CURLAUTH_DIGEST_IE => 16,
54
+ :CURLAUTH_AUTO => 16 | 8 | 4 | 2 | 1
39
55
  }
40
56
 
41
57
  def initialize
42
58
  @method = :get
43
- @post_dat_set = nil
44
59
  @headers = {}
60
+
61
+ # Enable encoding/compression support
62
+ set_option(OPTION_VALUES[:CURLOPT_ENCODING], '')
45
63
  end
46
64
 
47
65
  def headers=(hash)
@@ -69,6 +87,10 @@ module Typhoeus
69
87
  get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
70
88
  end
71
89
 
90
+ def effective_url
91
+ get_info_string(INFO_VALUES[:CURLINFO_EFFECTIVE_URL])
92
+ end
93
+
72
94
  def response_code
73
95
  get_info_long(INFO_VALUES[:CURLINFO_RESPONSE_CODE])
74
96
  end
@@ -84,6 +106,12 @@ module Typhoeus
84
106
  def max_redirects=(redirects)
85
107
  set_option(OPTION_VALUES[:CURLOPT_MAXREDIRS], redirects)
86
108
  end
109
+
110
+ def connect_timeout=(milliseconds)
111
+ @connect_timeout = milliseconds
112
+ set_option(OPTION_VALUES[:CURLOPT_NOSIGNAL], 1)
113
+ set_option(OPTION_VALUES[:CURLOPT_CONNECTTIMEOUT_MS], milliseconds)
114
+ end
87
115
 
88
116
  def timeout=(milliseconds)
89
117
  @timeout = milliseconds
@@ -95,6 +123,10 @@ module Typhoeus
95
123
  @timeout && total_time_taken > @timeout && response_code == 0
96
124
  end
97
125
 
126
+ def supports_zlib?
127
+ !!(curl_version.match(/zlib/))
128
+ end
129
+
98
130
  def request_body=(request_body)
99
131
  @request_body = request_body
100
132
  if @method == :put
@@ -132,17 +164,18 @@ module Typhoeus
132
164
  elsif method == :head
133
165
  set_option(OPTION_VALUES[:CURLOPT_NOBODY], 1)
134
166
  else
135
- set_option(OPTION_VALUES[:CURLOPT_CUSTOMREQUEST], "DELETE")
167
+ set_option(OPTION_VALUES[:CURLOPT_CUSTOMREQUEST], method.to_s.upcase)
136
168
  end
137
169
  end
138
170
 
139
171
  def post_data=(data)
140
172
  @post_data_set = true
141
- set_option(OPTION_VALUES[:CURLOPT_POSTFIELDS], data)
142
173
  set_option(OPTION_VALUES[:CURLOPT_POSTFIELDSIZE], data.length)
174
+ set_option(OPTION_VALUES[:CURLOPT_COPYPOSTFIELDS], data)
143
175
  end
144
176
 
145
177
  def params=(params)
178
+ @params = params
146
179
  params_string = params.keys.collect do |k|
147
180
  value = params[k]
148
181
  if value.is_a? Hash
@@ -162,10 +195,58 @@ module Typhoeus
162
195
  end
163
196
  end
164
197
 
198
+ # Set SSL certificate
199
+ # " The string should be the file name of your certificate. "
200
+ # The default format is "PEM" and can be changed with ssl_cert_type=
201
+ def ssl_cert=(cert)
202
+ set_option(OPTION_VALUES[:CURLOPT_SSLCERT], cert)
203
+ end
204
+
205
+ # Set SSL certificate type
206
+ # " The string should be the format of your certificate. Supported formats are "PEM" and "DER" "
207
+ def ssl_cert_type=(cert_type)
208
+ raise "Invalid ssl cert type : '#{cert_type}'..." if cert_type and !%w(PEM DER).include?(cert_type)
209
+ set_option(OPTION_VALUES[:CURLOPT_SSLCERTTYPE], cert_type)
210
+ end
211
+
212
+ # Set SSL Key file
213
+ # " The string should be the file name of your private key. "
214
+ # The default format is "PEM" and can be changed with ssl_key_type=
215
+ #
216
+ def ssl_key=(key)
217
+ set_option(OPTION_VALUES[:CURLOPT_SSLKEY], key)
218
+ end
219
+
220
+ # Set SSL Key type
221
+ # " The string should be the format of your private key. Supported formats are "PEM", "DER" and "ENG". "
222
+ #
223
+ def ssl_key_type=(key_type)
224
+ raise "Invalid ssl key type : '#{key_type}'..." if key_type and !%w(PEM DER ENG).include?(key_type)
225
+ set_option(OPTION_VALUES[:CURLOPT_SSLKEYTYPE], key_type)
226
+ end
227
+
228
+ def ssl_key_password=(key_password)
229
+ set_option(OPTION_VALUES[:CURLOPT_KEYPASSWD], key_password)
230
+ end
231
+
232
+ # Set SSL CACERT
233
+ # " File holding one or more certificates to verify the peer with. "
234
+ #
235
+ def ssl_cacert=(cacert)
236
+ set_option(OPTION_VALUES[:CURLOPT_CAINFO], cacert)
237
+ end
238
+
239
+ # Set CAPATH
240
+ # " directory holding multiple CA certificates to verify the peer with. The certificate directory must be prepared using the openssl c_rehash utility. "
241
+ #
242
+ def ssl_capath=(capath)
243
+ set_option(OPTION_VALUES[:CURLOPT_CAPATH], capath)
244
+ end
245
+
165
246
  def set_option(option, value)
166
247
  if value.class == String
167
248
  easy_setopt_string(option, value)
168
- else
249
+ elsif value
169
250
  easy_setopt_long(option, value)
170
251
  end
171
252
  end
@@ -173,7 +254,13 @@ module Typhoeus
173
254
  def perform
174
255
  set_headers()
175
256
  easy_perform()
176
- response_code()
257
+ resp_code = response_code()
258
+ if resp_code >= 200 && resp_code <= 299
259
+ success
260
+ else
261
+ failure
262
+ end
263
+ resp_code
177
264
  end
178
265
 
179
266
  def set_headers
@@ -249,5 +336,6 @@ module Typhoeus
249
336
  def curl_version
250
337
  version
251
338
  end
339
+
252
340
  end
253
341
  end