xenda-typhoeus 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.markdown +70 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +38 -0
- data/LICENSE +20 -0
- data/README.textile +397 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/benchmarks/profile.rb +25 -0
- data/benchmarks/vs_nethttp.rb +35 -0
- data/examples/file.rb +12 -0
- data/examples/times.rb +40 -0
- data/examples/twitter.rb +21 -0
- data/ext/typhoeus/.gitignore +7 -0
- data/ext/typhoeus/extconf.rb +65 -0
- data/ext/typhoeus/native.c +12 -0
- data/ext/typhoeus/native.h +22 -0
- data/ext/typhoeus/typhoeus_easy.c +232 -0
- data/ext/typhoeus/typhoeus_easy.h +20 -0
- data/ext/typhoeus/typhoeus_form.c +59 -0
- data/ext/typhoeus/typhoeus_form.h +13 -0
- data/ext/typhoeus/typhoeus_multi.c +217 -0
- data/ext/typhoeus/typhoeus_multi.h +16 -0
- data/lib/typhoeus.rb +58 -0
- data/lib/typhoeus/.gitignore +1 -0
- data/lib/typhoeus/easy.rb +379 -0
- data/lib/typhoeus/filter.rb +28 -0
- data/lib/typhoeus/form.rb +32 -0
- data/lib/typhoeus/hydra.rb +247 -0
- data/lib/typhoeus/hydra/callbacks.rb +24 -0
- data/lib/typhoeus/hydra/connect_options.rb +61 -0
- data/lib/typhoeus/hydra/stubbing.rb +52 -0
- data/lib/typhoeus/hydra_mock.rb +131 -0
- data/lib/typhoeus/multi.rb +37 -0
- data/lib/typhoeus/normalized_header_hash.rb +58 -0
- data/lib/typhoeus/remote.rb +306 -0
- data/lib/typhoeus/remote_method.rb +108 -0
- data/lib/typhoeus/remote_proxy_object.rb +50 -0
- data/lib/typhoeus/request.rb +203 -0
- data/lib/typhoeus/response.rb +91 -0
- data/lib/typhoeus/service.rb +20 -0
- data/lib/typhoeus/utils.rb +63 -0
- data/profilers/valgrind.rb +24 -0
- data/spec/fixtures/placeholder.gif +0 -0
- data/spec/fixtures/placeholder.txt +1 -0
- data/spec/fixtures/placeholder.ukn +0 -0
- data/spec/fixtures/result_set.xml +60 -0
- data/spec/servers/app.rb +94 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/typhoeus/easy_spec.rb +343 -0
- data/spec/typhoeus/filter_spec.rb +35 -0
- data/spec/typhoeus/form_spec.rb +117 -0
- data/spec/typhoeus/hydra_mock_spec.rb +300 -0
- data/spec/typhoeus/hydra_spec.rb +574 -0
- data/spec/typhoeus/multi_spec.rb +74 -0
- data/spec/typhoeus/normalized_header_hash_spec.rb +41 -0
- data/spec/typhoeus/remote_method_spec.rb +141 -0
- data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
- data/spec/typhoeus/remote_spec.rb +695 -0
- data/spec/typhoeus/request_spec.rb +300 -0
- data/spec/typhoeus/response_spec.rb +151 -0
- data/spec/typhoeus/utils_spec.rb +22 -0
- data/xenda-typhoeus.gemspec +139 -0
- metadata +203 -0
data/.gitignore
ADDED
data/CHANGELOG.markdown
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
Current master
|
2
|
+
---------------------
|
3
|
+
* Fix array params to be consistent with HTTP spec [gridaphobe]
|
4
|
+
* traversal_to_params_hash should use the escape option [itsmeduncan]
|
5
|
+
* Fix > 1024 open file descriptors [mschulkind]
|
6
|
+
* Fixed a bug with internally queued requests being dropped [mschulkind]
|
7
|
+
* Use gemspec in bundler to avoid duplication [mschulkind]
|
8
|
+
* Run internally queued requests in FIFO order [mschulkind]
|
9
|
+
* Moved Typhoeus::VERSION to a separate file, to fix rake build_native [mschulkind]
|
10
|
+
* Fixed problems related to put requests with empty bodies [skaes, GH-84]
|
11
|
+
* Added CURLOPT_INTERFACE option via Request#interface=. [spiegela]
|
12
|
+
* Added Tempfile support to Form#process! [richievos]
|
13
|
+
* Hydra won't forget to accept gzip/deflate encoding [codesnik]
|
14
|
+
|
15
|
+
0.2.4
|
16
|
+
-----
|
17
|
+
* Fix form POSTs to only use multipart for file uploads, otherwise use application/x-www-form-urlencoded [dbalatero]
|
18
|
+
|
19
|
+
0.2.3
|
20
|
+
-----
|
21
|
+
* Code duplication in Typhoeus::Form led to nested URL param errors on POST only. Fixed [dbalatero]
|
22
|
+
|
23
|
+
0.2.2
|
24
|
+
-----
|
25
|
+
* Fixed a problem with nested URL params encoding incorrectly [dbalatero]
|
26
|
+
|
27
|
+
0.2.1
|
28
|
+
-----
|
29
|
+
* Added extended proxy support [Zapotek, GH-46]
|
30
|
+
* eliminated compile time warnings by using proper type declarations [skaes, GH-54]
|
31
|
+
* fixed broken calls to rb_raise [skaes, GH-54]
|
32
|
+
* prevent leaking of curl easy handles when exceptions are raised (either from typhoeus itself or user callbacks) [skaes, GH-54]
|
33
|
+
* fixed Easy#timed_out? using curl return codes [skaes, GH-54]
|
34
|
+
* provide curl return codes and corresponding curl error messages on classes Easy and Request [skaes, GH-54]
|
35
|
+
* allow VCR to whitelist hosts in Typhoeus stubbing/mocking [myronmarston, GH-57]
|
36
|
+
* added timed_out? documentation, method to Response [dbalatero, GH-34]
|
37
|
+
* added abort to Hydra to prematurely stop a hydra.run [Zapotek]
|
38
|
+
* added file upload support for POST requests [jtarchie, GH-59]
|
39
|
+
|
40
|
+
0.2.0
|
41
|
+
------
|
42
|
+
* Fix warning in Request#headers from attr_accessor
|
43
|
+
* Params with array values were not parsing into the format that rack expects
|
44
|
+
[GH-39, smartocci]
|
45
|
+
* Removed Rack as a dependency [GH-45]
|
46
|
+
* Added integration hooks for VCR!
|
47
|
+
|
48
|
+
0.1.31
|
49
|
+
------
|
50
|
+
* Fixed bug in setting compression encoding [morhekil]
|
51
|
+
* Exposed authentication control methods through Request interface [morhekil]
|
52
|
+
|
53
|
+
0.1.30
|
54
|
+
-----------
|
55
|
+
* Exposed CURLOPT_CONNECTTIMEOUT_MS to Requests [balexis]
|
56
|
+
|
57
|
+
0.1.29
|
58
|
+
------
|
59
|
+
* Fixed a memory corruption with using CURLOPT_POSTFIELDS [gravis,
|
60
|
+
32531d0821aecc4]
|
61
|
+
|
62
|
+
0.1.28
|
63
|
+
----------------
|
64
|
+
* Added SSL cert options for Typhoeus::Easy [GH-25, gravis]
|
65
|
+
* Ported SSL cert options to Typhoeus::Request interface [gravis]
|
66
|
+
* Added support for any HTTP method (purge for Varnish) [ryana]
|
67
|
+
|
68
|
+
0.1.27
|
69
|
+
------
|
70
|
+
* Added rack as dependency, added dev dependencies to Rakefile [GH-21]
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
typhoeus (0.2.4)
|
5
|
+
mime-types
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
gemcutter (0.6.1)
|
12
|
+
git (1.2.5)
|
13
|
+
jeweler (1.4.0)
|
14
|
+
gemcutter (>= 0.1.0)
|
15
|
+
git (>= 1.2.5)
|
16
|
+
rubyforge (>= 2.0.0)
|
17
|
+
json (1.4.6)
|
18
|
+
json_pure (1.4.6)
|
19
|
+
mime-types (1.16)
|
20
|
+
rack (1.2.1)
|
21
|
+
rspec (1.3.1)
|
22
|
+
rubyforge (2.0.4)
|
23
|
+
json_pure (>= 1.1.7)
|
24
|
+
sinatra (1.1.0)
|
25
|
+
rack (~> 1.1)
|
26
|
+
tilt (~> 1.1)
|
27
|
+
tilt (1.1)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
diff-lcs
|
34
|
+
jeweler
|
35
|
+
json
|
36
|
+
rspec
|
37
|
+
sinatra
|
38
|
+
typhoeus!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 David Balatero
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,397 @@
|
|
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
|
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.- The current version of Mac Ports (7.21.2) works just fine.
|
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 => {:Accept => "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
|
+
# Run the request via Hydra.
|
63
|
+
hydra = Typhoeus::Hydra.new
|
64
|
+
hydra.queue(request)
|
65
|
+
hydra.run
|
66
|
+
|
67
|
+
# the response object will be set after the request is run
|
68
|
+
response = request.response
|
69
|
+
response.code # http status code
|
70
|
+
response.time # time in seconds the request took
|
71
|
+
response.headers # the http headers
|
72
|
+
response.headers_hash # http headers put into a hash
|
73
|
+
response.body # the response body
|
74
|
+
</pre>
|
75
|
+
|
76
|
+
*Making Quick Requests*
|
77
|
+
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.
|
78
|
+
|
79
|
+
<pre>
|
80
|
+
response = Typhoeus::Request.get("http://www.pauldix.net")
|
81
|
+
response = Typhoeus::Request.head("http://www.pauldix.net")
|
82
|
+
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
|
83
|
+
response = Typhoeus::Request.post("http://localhost:3000/posts", :params => {:title => "test post", :content => "this is my test"})
|
84
|
+
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")
|
85
|
+
</pre>
|
86
|
+
|
87
|
+
*Handling HTTP errors*
|
88
|
+
You can query the response object to figure out if you had a successful request or not. Here's some example code that you might use to handle errors.
|
89
|
+
|
90
|
+
<pre>
|
91
|
+
request.on_complete do |response|
|
92
|
+
if response.success?
|
93
|
+
# hell yeah
|
94
|
+
elsif response.timed_out?
|
95
|
+
# aw hell no
|
96
|
+
log("got a time out")
|
97
|
+
elsif response.code == 0
|
98
|
+
# Could not get an http response, something's wrong.
|
99
|
+
log(response.curl_error_message)
|
100
|
+
else
|
101
|
+
# Received a non-successful http response.
|
102
|
+
log("HTTP request failed: " + response.code.to_s)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
</pre>
|
106
|
+
|
107
|
+
This also works with serial (blocking) requests in the same fashion. Both serial and parallel requests return a Response object.
|
108
|
+
|
109
|
+
*Handling file uploads*
|
110
|
+
A File object can be passed as a param for a POST request to handle uploading files to the server. Typhoeus will upload the file as the original file name and use Mime::Types to set the content type.
|
111
|
+
|
112
|
+
<pre>
|
113
|
+
response = Typhoeus::Request.post("http://localhost:3000/posts",
|
114
|
+
:params => {
|
115
|
+
:title => "test post", :content => "this is my test",
|
116
|
+
:file => File.open("thesis.txt","r")
|
117
|
+
}
|
118
|
+
)
|
119
|
+
</pre>
|
120
|
+
|
121
|
+
*Making Parallel Requests*
|
122
|
+
|
123
|
+
<pre>
|
124
|
+
# Generally, you should be running requests through hydra. Here is how that looks
|
125
|
+
hydra = Typhoeus::Hydra.new
|
126
|
+
|
127
|
+
first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
|
128
|
+
first_request.on_complete do |response|
|
129
|
+
post = JSON.parse(response.body)
|
130
|
+
third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
|
131
|
+
third_request.on_complete do |response|
|
132
|
+
# do something with that
|
133
|
+
end
|
134
|
+
hydra.queue third_request
|
135
|
+
return post
|
136
|
+
end
|
137
|
+
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
|
138
|
+
second_request.on_complete do |response|
|
139
|
+
JSON.parse(response.body)
|
140
|
+
end
|
141
|
+
hydra.queue first_request
|
142
|
+
hydra.queue second_request
|
143
|
+
hydra.run # this is a blocking call that returns once all requests are complete
|
144
|
+
|
145
|
+
first_request.handled_response # the value returned from the on_complete block
|
146
|
+
second_request.handled_response # the value returned from the on_complete block (parsed JSON)
|
147
|
+
</pre>
|
148
|
+
|
149
|
+
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.
|
150
|
+
|
151
|
+
*Specifying Max Concurrency*
|
152
|
+
|
153
|
+
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.
|
154
|
+
|
155
|
+
<pre>
|
156
|
+
hydra = Typhoeus::Hydra.new(:max_concurrency => 20) # keep from killing some servers
|
157
|
+
</pre>
|
158
|
+
|
159
|
+
*Memoization*
|
160
|
+
Hydra memoizes requests within a single run call. You can also disable memoization.
|
161
|
+
|
162
|
+
<pre>
|
163
|
+
hydra = Typhoeus::Hydra.new
|
164
|
+
2.times do
|
165
|
+
r = Typhoeus::Request.new("http://localhost/3000/users/1")
|
166
|
+
hydra.queue r
|
167
|
+
end
|
168
|
+
hydra.run # this will result in a single request being issued. However, the on_complete handlers of both will be called.
|
169
|
+
hydra.disable_memoization
|
170
|
+
2.times do
|
171
|
+
r = Typhoeus::Request.new("http://localhost/3000/users/1")
|
172
|
+
hydra.queue r
|
173
|
+
end
|
174
|
+
hydra.run # this will result in a two requests.
|
175
|
+
</pre>
|
176
|
+
|
177
|
+
*Caching*
|
178
|
+
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.
|
179
|
+
|
180
|
+
<pre>
|
181
|
+
hydra = Typhoeus::Hydra.new
|
182
|
+
hydra.cache_setter do |request|
|
183
|
+
@cache.set(request.cache_key, request.response, request.cache_timeout)
|
184
|
+
end
|
185
|
+
|
186
|
+
hydra.cache_getter do |request|
|
187
|
+
@cache.get(request.cache_key) rescue nil
|
188
|
+
end
|
189
|
+
</pre>
|
190
|
+
|
191
|
+
*Direct Stubbing*
|
192
|
+
Hydra allows you to stub out specific urls and patters to avoid hitting remote servers while testing.
|
193
|
+
|
194
|
+
<pre>
|
195
|
+
hydra = Typhoeus::Hydra.new
|
196
|
+
response = Response.new(:code => 200, :headers => "", :body => "{'name' : 'paul'}", :time => 0.3)
|
197
|
+
hydra.stub(:get, "http://localhost:3000/users/1").and_return(response)
|
198
|
+
|
199
|
+
request = Typhoeus::Request.new("http://localhost:3000/users/1")
|
200
|
+
request.on_complete do |response|
|
201
|
+
JSON.parse(response.body)
|
202
|
+
end
|
203
|
+
hydra.queue request
|
204
|
+
hydra.run
|
205
|
+
</pre>
|
206
|
+
|
207
|
+
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.
|
208
|
+
|
209
|
+
<pre>
|
210
|
+
hydra.stub(:get, /http\:\/\/localhost\:3000\/users\/.*/).and_return(response)
|
211
|
+
# any requests for a user will be stubbed out with the pre built response.
|
212
|
+
</pre>
|
213
|
+
|
214
|
+
*The Singleton*
|
215
|
+
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.
|
216
|
+
|
217
|
+
<pre>
|
218
|
+
hydra = Typhoeus::Hydra.hydra
|
219
|
+
hydra.stub(:get, "http://localhost:3000/users")
|
220
|
+
</pre>
|
221
|
+
|
222
|
+
*Timeouts*
|
223
|
+
|
224
|
+
No exceptions are raised on HTTP timeouts. You can check whether a request timed out with the following methods:
|
225
|
+
|
226
|
+
<pre>
|
227
|
+
easy.timed_out? # for a raw Easy handle
|
228
|
+
response.timed_out? # for a Response handle
|
229
|
+
</pre>
|
230
|
+
|
231
|
+
*Basic Authentication*
|
232
|
+
|
233
|
+
<pre>
|
234
|
+
response = Typhoeus::Request.get("http://twitter.com/statuses/followers.json",
|
235
|
+
:username => username, :password => password)
|
236
|
+
</pre>
|
237
|
+
|
238
|
+
*SSL*
|
239
|
+
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:
|
240
|
+
|
241
|
+
<pre>
|
242
|
+
Typhoeus::Easy.new.curl_version # output should include OpenSSL/...
|
243
|
+
</pre>
|
244
|
+
|
245
|
+
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:
|
246
|
+
|
247
|
+
<pre>
|
248
|
+
Typhoeus::Request.get("https://mail.google.com/mail", :disable_ssl_peer_verification => true)
|
249
|
+
</pre>
|
250
|
+
|
251
|
+
*LibCurl*
|
252
|
+
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.
|
253
|
+
|
254
|
+
However, by using this raw interface, you do not get access to Hydra-specific features, such as stubbing/mocking.
|
255
|
+
|
256
|
+
SSL Certs can be provided to the Easy interface:
|
257
|
+
|
258
|
+
<pre>
|
259
|
+
e = Typhoeus::Easy.new
|
260
|
+
e.url = "https://example.com/action"
|
261
|
+
s.ssl_cacert = "ca_file.cer"
|
262
|
+
e.ssl_cert = "acert.crt"
|
263
|
+
e.ssl_key = "akey.key"
|
264
|
+
[...]
|
265
|
+
e.perform
|
266
|
+
</pre>
|
267
|
+
|
268
|
+
or directly to a Typhoeus::Request :
|
269
|
+
|
270
|
+
<pre>
|
271
|
+
e = Typhoeus::Request.get("https://example.com/action",
|
272
|
+
:ssl_cacert => "ca_file.cer",
|
273
|
+
:ssl_cert => "acert.crt",
|
274
|
+
:ssl_key => "akey.key",
|
275
|
+
[...]
|
276
|
+
end
|
277
|
+
</pre>
|
278
|
+
|
279
|
+
h2. Advanced authentication
|
280
|
+
|
281
|
+
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.
|
282
|
+
|
283
|
+
<pre>
|
284
|
+
e = Typhoeus::Easy.new
|
285
|
+
e.auth = {
|
286
|
+
:username => 'username',
|
287
|
+
:password => 'password',
|
288
|
+
:method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM]
|
289
|
+
}
|
290
|
+
e.url = "http://example.com/auth_ntlm"
|
291
|
+
e.method = :get
|
292
|
+
e.perform
|
293
|
+
</pre>
|
294
|
+
|
295
|
+
*Other authentication types*
|
296
|
+
|
297
|
+
The following authentication types are available:
|
298
|
+
* CURLAUTH_BASIC
|
299
|
+
* CURLAUTH_DIGEST
|
300
|
+
* CURLAUTH_GSSNEGOTIATE
|
301
|
+
* CURLAUTH_NTLM
|
302
|
+
* CURLAUTH_DIGEST_IE
|
303
|
+
* CURLAUTH_AUTO
|
304
|
+
|
305
|
+
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.
|
306
|
+
|
307
|
+
*Authentication via the quick request interface*
|
308
|
+
|
309
|
+
There's also an easy way to perform any kind of authentication via the quick request interface:
|
310
|
+
|
311
|
+
<pre>
|
312
|
+
e = Typhoeus::Request.get("http://example.com",
|
313
|
+
:username => 'username',
|
314
|
+
:password => 'password',
|
315
|
+
:auth_method => :ntlm)
|
316
|
+
</pre>
|
317
|
+
|
318
|
+
All methods listed above is available in a shorter form - :basic, :digest, :gssnegotiate, :ntlm, :digest_ie, :auto.
|
319
|
+
|
320
|
+
*Query of available auth types*
|
321
|
+
|
322
|
+
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
|
323
|
+
that you'll need to decode yourself, please refer to easy.rb source code to see the numeric values of different auth types.
|
324
|
+
|
325
|
+
h2. Verbose debug output
|
326
|
+
|
327
|
+
Sometime it's useful to see verbose output from curl. You may now enable it:
|
328
|
+
|
329
|
+
<pre>
|
330
|
+
e = Typhoeus::Easy.new
|
331
|
+
e.verbose = 1
|
332
|
+
</pre>
|
333
|
+
|
334
|
+
or using the quick request:
|
335
|
+
|
336
|
+
<pre>
|
337
|
+
e = Typhoeus::Request.get("http://example.com", :verbose => true)
|
338
|
+
</pre>
|
339
|
+
|
340
|
+
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.
|
341
|
+
|
342
|
+
h2. Benchmarks
|
343
|
+
|
344
|
+
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:
|
345
|
+
|
346
|
+
<pre>
|
347
|
+
net::http 0.030000 0.010000 0.040000 ( 10.054327)
|
348
|
+
typhoeus 0.020000 0.070000 0.090000 ( 0.508817)
|
349
|
+
</pre>
|
350
|
+
|
351
|
+
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.
|
352
|
+
|
353
|
+
h2. Running the specs
|
354
|
+
|
355
|
+
Running the specs requires the native extensions to be built and a couple of Sinatra servers to be booted. Do this:
|
356
|
+
|
357
|
+
<pre>
|
358
|
+
# Make sure to build the native extensions
|
359
|
+
rake build_native
|
360
|
+
# Start up the test servers (in another terminal)
|
361
|
+
rake start_test_servers
|
362
|
+
|
363
|
+
# Run the specs
|
364
|
+
rake spec
|
365
|
+
</pre>
|
366
|
+
|
367
|
+
h2. Next Steps
|
368
|
+
|
369
|
+
* Add in ability to keep-alive requests and reuse them within hydra.
|
370
|
+
* Add support for automatic retry, exponential back-off, and queuing for later.
|
371
|
+
|
372
|
+
h2. LICENSE
|
373
|
+
|
374
|
+
(The MIT License)
|
375
|
+
|
376
|
+
Copyright (c) 2009:
|
377
|
+
|
378
|
+
"Paul Dix":http://pauldix.net
|
379
|
+
|
380
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
381
|
+
a copy of this software and associated documentation files (the
|
382
|
+
'Software'), to deal in the Software without restriction, including
|
383
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
384
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
385
|
+
permit persons to whom the Software is furnished to do so, subject to
|
386
|
+
the following conditions:
|
387
|
+
|
388
|
+
The above copyright notice and this permission notice shall be
|
389
|
+
included in all copies or substantial portions of the Software.
|
390
|
+
|
391
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
392
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
393
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
394
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
395
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
396
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
397
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|