typhoeus 0.1.30 → 0.1.31

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,8 @@
1
+ 0.1.31
2
+ ------
3
+ * Fixed bug in setting compression encoding [morhekil]
4
+ * Exposed authentication control methods through Request interface [morhekil]
5
+
1
6
  0.1.30
2
7
  -----------
3
8
  * Exposed CURLOPT_CONNECTTIMEOUT_MS to Requests [balexis]
data/README.textile CHANGED
@@ -183,9 +183,8 @@ hydra.stub(:get, "http://localhost:3000/users")
183
183
  *Basic Authentication*
184
184
 
185
185
  <pre>
186
- require 'base64'
187
186
  response = Typhoeus::Request.get("http://twitter.com/statuses/followers.json",
188
- :headers => {"Authorization" => "Basic #{Base64.b64encode("#{username}:#{password}")}"})
187
+ :username => username, :password => password)
189
188
  </pre>
190
189
 
191
190
  *SSL*
@@ -227,9 +226,9 @@ e = Typhoeus::Request.get("https://example.com/action",
227
226
  end
228
227
  </pre>
229
228
 
230
- h2. NTLM authentication
229
+ h2. Advanced authentication
231
230
 
232
- 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. Now you can do it via Typhoeus::Easy interface using the following API.
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.
233
232
 
234
233
  <pre>
235
234
  e = Typhoeus::Easy.new
@@ -251,6 +250,22 @@ The following authentication types are available:
251
250
  * CURLAUTH_GSSNEGOTIATE
252
251
  * CURLAUTH_NTLM
253
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.
254
269
 
255
270
  *Query of available auth types*
256
271
 
@@ -266,7 +281,13 @@ e = Typhoeus::Easy.new
266
281
  e.verbose = 1
267
282
  </pre>
268
283
 
269
- Please note that libcurl prints it's output to the console, so you'll need to run your scripts from the console to see the debug info.
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.
270
291
 
271
292
  h2. Benchmarks
272
293
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.30
1
+ 0.1.31
data/lib/typhoeus/easy.rb CHANGED
@@ -30,7 +30,7 @@ module Typhoeus
30
30
  :CURLOPT_PROXY => 10004,
31
31
  :CURLOPT_VERIFYPEER => 64,
32
32
  :CURLOPT_NOBODY => 44,
33
- :CURLOPT_ENCODING => 102,
33
+ :CURLOPT_ENCODING => 10000 + 102,
34
34
  :CURLOPT_SSLCERT => 10025,
35
35
  :CURLOPT_SSLCERTTYPE => 10086,
36
36
  :CURLOPT_SSLKEY => 10087,
@@ -50,14 +50,16 @@ module Typhoeus
50
50
  :CURLAUTH_DIGEST => 2,
51
51
  :CURLAUTH_GSSNEGOTIATE => 4,
52
52
  :CURLAUTH_NTLM => 8,
53
- :CURLAUTH_DIGEST_IE => 16
53
+ :CURLAUTH_DIGEST_IE => 16,
54
+ :CURLAUTH_AUTO => 16 | 8 | 4 | 2 | 1
54
55
  }
55
56
 
56
57
  def initialize
57
58
  @method = :get
58
59
  @headers = {}
59
60
 
60
- set_option(OPTION_VALUES[:CURLOPT_ENCODING], 'zlib') if supports_zlib?
61
+ # Enable encoding/compression support
62
+ set_option(OPTION_VALUES[:CURLOPT_ENCODING], '')
61
63
  end
62
64
 
63
65
  def headers=(hash)
@@ -132,6 +132,12 @@ module Typhoeus
132
132
  @running_requests += 1
133
133
 
134
134
  easy = @easy_pool.pop || Easy.new
135
+ easy.verbose = request.verbose
136
+ if request.username || request.password
137
+ auth = { :username => request.username, :password => request.password }
138
+ auth[:method] = Typhoeus::Easy::AUTH_TYPES["CURLAUTH_#{request.auth_method.to_s.upcase}".to_sym] if request.auth_method
139
+ easy.auth = auth
140
+ end
135
141
  easy.url = request.url
136
142
  easy.method = request.method
137
143
  easy.params = request.params if request.method == :post && !request.params.nil?
@@ -1,6 +1,7 @@
1
1
  module Typhoeus
2
2
  class Request
3
- attr_accessor :method, :params, :body, :headers, :connect_timeout, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :disable_ssl_peer_verification, :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type, :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose
3
+ attr_accessor :method, :params, :body, :headers, :connect_timeout, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects, :proxy, :disable_ssl_peer_verification, :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type, :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose, :username, :password,
4
+ :auth_method
4
5
 
5
6
  attr_reader :url
6
7
 
@@ -29,6 +30,9 @@ module Typhoeus
29
30
  # ** +:ssl_cacert
30
31
  # ** +:ssl_capath
31
32
  # ** +:verbose
33
+ # ** +:username
34
+ # ** +:password
35
+ # ** +:auth_method
32
36
  #
33
37
  def initialize(url, options = {})
34
38
  @method = options[:method] || :get
@@ -51,6 +55,9 @@ module Typhoeus
51
55
  @ssl_cacert = options[:ssl_cacert]
52
56
  @ssl_capath = options[:ssl_capath]
53
57
  @verbose = options[:verbose]
58
+ @username = options[:username]
59
+ @password = options[:password]
60
+ @auth_method = options[:auth_method]
54
61
 
55
62
  if @method == :post
56
63
  @url = url
data/spec/servers/app.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  require 'rubygems'
3
3
  require 'sinatra'
4
4
  require 'json'
5
+ require 'zlib'
5
6
 
6
7
  @@fail_count = 0
7
8
  get '/fail/:number' do
@@ -43,11 +44,21 @@ get '/auth_ntlm' do
43
44
  # we're just checking for the existence if NTLM auth header here. It's validation
44
45
  # is too troublesome and really doesn't bother is much, it's up to libcurl to make
45
46
  # it valid
47
+ response['WWW-Authenticate'] = 'NTLM'
46
48
  is_ntlm_auth = /^NTLM/ =~ request.env['HTTP_AUTHORIZATION']
47
49
  true if is_ntlm_auth
48
50
  throw(:halt, [401, "Not authorized\n"]) if !is_ntlm_auth
49
51
  end
50
52
 
53
+ get '/gzipped' do
54
+ req_env = request.env.to_json
55
+ z = Zlib::Deflate.new
56
+ gzipped_env = z.deflate(req_env, Zlib::FINISH)
57
+ z.close
58
+ response['Content-Encoding'] = 'gzip'
59
+ gzipped_env
60
+ end
61
+
51
62
  get '/**' do
52
63
  sleep params["delay"].to_i if params.has_key?("delay")
53
64
  request.env.merge!(:body => request.body.read).to_json
@@ -232,5 +232,18 @@ describe Typhoeus::Easy do
232
232
  easy.response_code.should == 200
233
233
  easy.response_body.should include("this is a body!")
234
234
  end
235
- end
235
+ end
236
+
237
+ describe "encoding/compression support" do
238
+
239
+ it "should send valid encoding headers and decode the response" do
240
+ easy = Typhoeus::Easy.new
241
+ easy.url = "http://localhost:3002/gzipped"
242
+ easy.method = :get
243
+ easy.perform
244
+ easy.response_code.should == 200
245
+ JSON.parse(easy.response_body)["HTTP_ACCEPT_ENCODING"].should == "deflate, gzip"
246
+ end
247
+
248
+ end
236
249
  end
@@ -160,6 +160,32 @@ describe Typhoeus::Request do
160
160
  request.call_handlers
161
161
  good.should be_true
162
162
  end
163
+
164
+ describe "authentication" do
165
+
166
+ it "should allow to set username and password" do
167
+ auth = { :username => 'foo', :password => 'bar' }
168
+ e = Typhoeus::Request.get(
169
+ "http://localhost:3001/auth_basic/#{auth[:username]}/#{auth[:password]}",
170
+ auth
171
+ )
172
+ e.code.should == 200
173
+ end
174
+
175
+ it "should allow to set authentication method" do
176
+ auth = {
177
+ :username => 'username',
178
+ :password => 'password',
179
+ :auth_method => :ntlm
180
+ }
181
+ e = Typhoeus::Request.get(
182
+ "http://localhost:3001/auth_ntlm",
183
+ auth
184
+ )
185
+ e.code.should == 200
186
+ end
187
+
188
+ end
163
189
 
164
190
  describe "retry" do
165
191
  it "should take a retry option"
data/typhoeus.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{typhoeus}
8
- s.version = "0.1.30"
8
+ s.version = "0.1.31"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Paul Dix"]
12
- s.date = %q{2010-07-15}
12
+ s.date = %q{2010-07-16}
13
13
  s.description = %q{Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic.}
14
14
  s.email = %q{paul@pauldix.net}
15
15
  s.extensions = ["ext/typhoeus/extconf.rb"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 30
9
- version: 0.1.30
8
+ - 31
9
+ version: 0.1.31
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paul Dix
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-15 00:00:00 -07:00
17
+ date: 2010-07-16 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency