typhoeus 0.5.0.rc → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +11 -13
- data/Gemfile +2 -0
- data/README.md +12 -3
- data/lib/typhoeus.rb +11 -1
- data/lib/typhoeus/adapters/faraday.rb +136 -0
- data/lib/typhoeus/hydra.rb +5 -2
- data/lib/typhoeus/request.rb +6 -6
- data/lib/typhoeus/request/actions.rb +7 -7
- data/lib/typhoeus/request/block_connection.rb +2 -2
- data/lib/typhoeus/request/callbacks.rb +4 -0
- data/lib/typhoeus/request/operations.rb +43 -28
- data/lib/typhoeus/response.rb +4 -1
- data/lib/typhoeus/response/header.rb +15 -12
- data/lib/typhoeus/response/status.rb +8 -2
- data/lib/typhoeus/version.rb +1 -1
- metadata +7 -6
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
## 0.5.0
|
3
|
+
## 0.5.0
|
4
4
|
|
5
5
|
[Full Changelog](http://github.com/typhoeus/typhoeus/compare/v0.4.2...master)
|
6
6
|
|
@@ -8,18 +8,13 @@ Major Changes:
|
|
8
8
|
|
9
9
|
* Ethon integration
|
10
10
|
* Params are url params and a body is always a body for every request type
|
11
|
-
* Request parameter and body are properly encoded (only POST multiform body is not)
|
12
|
-
* No more header sanitizing
|
13
|
-
|
14
|
-
Before: `:headers => { 'user_agent' => 'Custom' }` was modified to
|
15
|
-
`:headers => { 'User-Agent' => 'Custom' }`
|
16
|
-
|
17
11
|
* The options you can set might have a slightly other names, as Ethon sticks to
|
18
12
|
libcurl names. See
|
19
13
|
[Easy.new](http://rubydoc.info/github/typhoeus/ethon/Ethon/Easy#initialize-instance_method)
|
20
14
|
for a description.
|
21
|
-
*
|
22
|
-
|
15
|
+
* Request parameter and body are properly encoded (only POST multiform body is not)
|
16
|
+
* No more header sanitizing. Before: `:headers => { 'user_agent' => 'Custom' }` was modified to
|
17
|
+
`:headers => { 'User-Agent' => 'Custom' }`
|
23
18
|
* `Typhoeus::Easy` and `Typhoeus::Multi` are now `Ethon::Easy` and `Ethon::Multi`
|
24
19
|
|
25
20
|
* Request shortcuts: `Typhoeus.get("www.google.de")`
|
@@ -30,20 +25,23 @@ Typhoeus.configure do |config|
|
|
30
25
|
config.memoize = true
|
31
26
|
end
|
32
27
|
```
|
33
|
-
* No more Response#headers_hash
|
28
|
+
* No more `Response#headers_hash`, instead `Response#headers` returning the last
|
34
29
|
header and response#redirections returning the responses with headers
|
35
30
|
generated through redirections
|
36
31
|
* Instead of defining the same callbacks on every request, you can define global callbacks:
|
37
|
-
|
38
32
|
```ruby
|
39
33
|
Typhoeus.on_complete { p "yay" }
|
40
34
|
```
|
41
|
-
|
42
35
|
* The stubbing interface changed slightly. You now have the same syntax as for requests:
|
43
|
-
|
44
36
|
```ruby
|
45
37
|
Typhoeus.stub(url, options).and_return(response)
|
46
38
|
```
|
39
|
+
* The following things were removed because they do not seemed to be used at all. Ping me if you disagree!
|
40
|
+
* `Typhoeus::Filter`
|
41
|
+
* `Typhoeus::Remote`
|
42
|
+
* `Typhoeus::RemoteMethod`
|
43
|
+
* `Typhoeus::RemoteProxyObject`
|
44
|
+
* build in cache interface
|
47
45
|
|
48
46
|
Enhancements:
|
49
47
|
|
data/Gemfile
CHANGED
@@ -8,6 +8,7 @@ group :development, :test do
|
|
8
8
|
|
9
9
|
gem "sinatra", "~> 1.3"
|
10
10
|
gem "json"
|
11
|
+
gem "faraday", "~> 0.8.4"
|
11
12
|
|
12
13
|
if RUBY_PLATFORM == "java"
|
13
14
|
gem "spoon"
|
@@ -15,5 +16,6 @@ group :development, :test do
|
|
15
16
|
|
16
17
|
unless ENV["CI"]
|
17
18
|
gem "guard-rspec", "~> 0.7"
|
19
|
+
gem 'rb-fsevent', '~> 0.9.1'
|
18
20
|
end
|
19
21
|
end
|
data/README.md
CHANGED
@@ -7,20 +7,29 @@ Like a modern code version of the mythical beast with 100 serpent heads, Typhoeu
|
|
7
7
|
Single request:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
Typhoeus.get("www.example.com")
|
10
|
+
Typhoeus.get("www.example.com", followlocation: true)
|
11
11
|
```
|
12
12
|
|
13
13
|
Parallel requests:
|
14
14
|
|
15
15
|
```ruby
|
16
16
|
hydra = Typhoeus::Hydra.new
|
17
|
-
10.times.map{ hydra.queue(Typhoeus::Request.new("www.example.com")) }
|
17
|
+
10.times.map{ hydra.queue(Typhoeus::Request.new("www.example.com", followlocation: true)) }
|
18
18
|
hydra.run
|
19
19
|
```
|
20
20
|
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
```
|
24
|
+
gem install typhoeus --pre
|
25
|
+
```
|
26
|
+
```
|
27
|
+
gem "typhoeus", git: "git://github.com/typhoeus/typhoeus.git"
|
28
|
+
```
|
29
|
+
|
21
30
|
## Project Tracking
|
22
31
|
|
23
|
-
* [Documentation](http://rubydoc.info/github/typhoeus/typhoeus) (github master)
|
32
|
+
* [Documentation](http://rubydoc.info/github/typhoeus/typhoeus/frames/Typhoeus) (github master)
|
24
33
|
* [Website](http://typhoeus.github.com/) (v0.4.2)
|
25
34
|
* [Mailinglist](http://groups.google.com/group/typhoeus)
|
26
35
|
|
data/lib/typhoeus.rb
CHANGED
@@ -10,13 +10,23 @@ require 'typhoeus/response'
|
|
10
10
|
require 'typhoeus/version'
|
11
11
|
|
12
12
|
# Typhoeus is a http client library based on Ethon which
|
13
|
-
# wraps libcurl.
|
13
|
+
# wraps libcurl. Sitting on top of libcurl make Typhoeus
|
14
|
+
# very reliable and fast.
|
15
|
+
#
|
16
|
+
# There are some gems using Typhoeus like
|
17
|
+
# {https://github.com/myronmarston/vcr VCR},
|
18
|
+
# {https://github.com/bblimke/webmock Webmock} or
|
19
|
+
# {https://github.com/technoweenie/faraday Faraday}. VCR
|
20
|
+
# and Webmock are providing their own adapter
|
21
|
+
# whereas Faraday relies on {Faraday::Adapter::Typhoeus}
|
22
|
+
# since Typhoeus version 0.5.
|
14
23
|
#
|
15
24
|
# @example (see Typhoeus::Request)
|
16
25
|
# @example (see Typhoeus::Hydra)
|
17
26
|
#
|
18
27
|
# @see Typhoeus::Request
|
19
28
|
# @see Typhoeus::Hydra
|
29
|
+
# @see Faraday::Adapter::Typhoeus
|
20
30
|
#
|
21
31
|
# @since 0.5.0
|
22
32
|
module Typhoeus
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday # :nodoc:
|
4
|
+
class Adapter # :nodoc:
|
5
|
+
|
6
|
+
# Adapter to use Faraday with Typhoeus.
|
7
|
+
#
|
8
|
+
# @example Use Typhoeus.
|
9
|
+
# require 'faraday'
|
10
|
+
# require 'typhoeus'
|
11
|
+
# require 'typhoeus/adapters/faraday'
|
12
|
+
#
|
13
|
+
# conn = Faraday.new(url: "www.example.com") do |faraday|
|
14
|
+
# faraday.adapter :typhoeus
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# response = conn.get("/")
|
18
|
+
class Typhoeus < Faraday::Adapter
|
19
|
+
self.supports_parallel = true
|
20
|
+
|
21
|
+
# Setup Hydra with provided options.
|
22
|
+
#
|
23
|
+
# @example Setup Hydra.
|
24
|
+
# Faraday::Adapter::Typhoeus.setup_parallel_manager
|
25
|
+
# #=> #<Typhoeus::Hydra ... >
|
26
|
+
#
|
27
|
+
# @param (see Typhoeus::Hydra#initialize)
|
28
|
+
# @option (see Typhoeus::Hydra#initialize)
|
29
|
+
#
|
30
|
+
# @return [ Typhoeus::Hydra ] The hydra.
|
31
|
+
def self.setup_parallel_manager(options = {})
|
32
|
+
::Typhoeus::Hydra.new(options)
|
33
|
+
end
|
34
|
+
|
35
|
+
dependency 'typhoeus'
|
36
|
+
|
37
|
+
# Hook into Faraday and perform the request with Typhoeus.
|
38
|
+
#
|
39
|
+
# @param [ Hash ] env The environment.
|
40
|
+
#
|
41
|
+
# @return [ void ]
|
42
|
+
def call(env)
|
43
|
+
super
|
44
|
+
perform_request env
|
45
|
+
@app.call env
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def perform_request(env)
|
51
|
+
if parallel?(env)
|
52
|
+
env[:parallel_manager].queue request(env)
|
53
|
+
else
|
54
|
+
request(env).run
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def request(env)
|
59
|
+
read_body env
|
60
|
+
|
61
|
+
req = ::Typhoeus::Request.new(
|
62
|
+
env[:url].to_s,
|
63
|
+
:method => env[:method],
|
64
|
+
:body => env[:body],
|
65
|
+
:headers => env[:request_headers]
|
66
|
+
)
|
67
|
+
|
68
|
+
configure_ssl req, env
|
69
|
+
configure_proxy req, env
|
70
|
+
configure_timeout req, env
|
71
|
+
configure_socket req, env
|
72
|
+
|
73
|
+
req.on_complete do |resp|
|
74
|
+
if resp.timed_out?
|
75
|
+
if parallel?(env)
|
76
|
+
# TODO: error callback in async mode
|
77
|
+
else
|
78
|
+
raise Faraday::Error::TimeoutError, "request timed out"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
save_response(env, resp.code, resp.body) do |response_headers|
|
83
|
+
response_headers.parse resp.response_headers
|
84
|
+
end
|
85
|
+
# in async mode, :response is initialized at this point
|
86
|
+
env[:response].finish(env) if parallel?(env)
|
87
|
+
end
|
88
|
+
|
89
|
+
req
|
90
|
+
end
|
91
|
+
|
92
|
+
def read_body(env)
|
93
|
+
env[:body] = env[:body].read if env[:body].respond_to? :read
|
94
|
+
end
|
95
|
+
|
96
|
+
def configure_ssl(req, env)
|
97
|
+
ssl = env[:ssl]
|
98
|
+
|
99
|
+
ssl_verifyhost = (ssl && ssl.fetch(:verify, true)) ? 2 : 0
|
100
|
+
req.options[:ssl_verifyhost] = ssl_verifyhost
|
101
|
+
req.options[:sslversion] = ssl[:version] if ssl[:version]
|
102
|
+
req.options[:sslcert] = ssl[:client_cert_file] if ssl[:client_cert_file]
|
103
|
+
req.options[:sslkey] = ssl[:client_key_file] if ssl[:client_key_file]
|
104
|
+
req.options[:cainfo] = ssl[:ca_file] if ssl[:ca_file]
|
105
|
+
req.options[:capath] = ssl[:ca_path] if ssl[:ca_path]
|
106
|
+
end
|
107
|
+
|
108
|
+
def configure_proxy(req, env)
|
109
|
+
proxy = env[:request][:proxy]
|
110
|
+
return unless proxy
|
111
|
+
|
112
|
+
req.options[:proxy] = "#{proxy[:uri].host}:#{proxy[:uri].port}"
|
113
|
+
|
114
|
+
if proxy[:username] && proxy[:password]
|
115
|
+
req.options[:proxyuserpwd] = "#{proxy[:username]}:#{proxy[:password]}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def configure_timeout(req, env)
|
120
|
+
env_req = env[:request]
|
121
|
+
req.options[:timeout_ms] = (env_req[:timeout] * 1000) if env_req[:timeout]
|
122
|
+
req.options[:connecttimeout_ms] = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
|
123
|
+
end
|
124
|
+
|
125
|
+
def configure_socket(req, env)
|
126
|
+
if bind = env[:request][:bind]
|
127
|
+
req.options[:interface] = bind[:host]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def parallel?(env)
|
132
|
+
!!env[:parallel_manager]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/lib/typhoeus/hydra.rb
CHANGED
@@ -35,6 +35,9 @@ module Typhoeus
|
|
35
35
|
# requests = (0..9).map{ Typhoeus::Request.new("www.example.com") }
|
36
36
|
# requests.each{ |request| hydra.queue(request) }
|
37
37
|
# hydra.run
|
38
|
+
#
|
39
|
+
# @note Callbacks are going to delay the request
|
40
|
+
# execution.
|
38
41
|
class Hydra
|
39
42
|
include Hydra::EasyPool
|
40
43
|
include Hydra::Queueable
|
@@ -45,7 +48,7 @@ module Typhoeus
|
|
45
48
|
include Hydra::Before
|
46
49
|
|
47
50
|
# @example Set max_concurrency.
|
48
|
-
# Typhoeus::Hydra.new(:
|
51
|
+
# Typhoeus::Hydra.new(max_concurrency: 20)
|
49
52
|
attr_reader :max_concurrency
|
50
53
|
|
51
54
|
# @api private
|
@@ -75,7 +78,7 @@ module Typhoeus
|
|
75
78
|
# Typhoeus::Hydra.new
|
76
79
|
#
|
77
80
|
# @example Create a hydra with max_concurrency.
|
78
|
-
# Typhoeus::Hydra.new(:
|
81
|
+
# Typhoeus::Hydra.new(max_concurrency: 20)
|
79
82
|
#
|
80
83
|
# @param [ Hash ] options The options hash.
|
81
84
|
#
|
data/lib/typhoeus/request.rb
CHANGED
@@ -67,26 +67,26 @@ module Typhoeus
|
|
67
67
|
# @example Request with url parameters.
|
68
68
|
# response = Typhoeus::Request.new(
|
69
69
|
# "www.example.com",
|
70
|
-
# :
|
70
|
+
# params: {a: 1}
|
71
71
|
# ).run
|
72
72
|
#
|
73
73
|
# @example Request with a body.
|
74
74
|
# response = Typhoeus::Request.new(
|
75
75
|
# "www.example.com",
|
76
|
-
# :
|
76
|
+
# body: {b: 2}
|
77
77
|
# ).run
|
78
78
|
#
|
79
79
|
# @example Request with parameters and body.
|
80
80
|
# response = Typhoeus::Request.new(
|
81
81
|
# "www.example.com",
|
82
|
-
# :
|
83
|
-
# :
|
82
|
+
# params: {a: 1},
|
83
|
+
# body: {b: 2}
|
84
84
|
# ).run
|
85
85
|
#
|
86
86
|
# @example Create a request and allow follow redirections.
|
87
87
|
# response = Typhoeus::Request.new(
|
88
88
|
# "www.example.com",
|
89
|
-
# :
|
89
|
+
# followlocation: true
|
90
90
|
# ).run
|
91
91
|
#
|
92
92
|
# @param [ String ] url The url to request.
|
@@ -99,7 +99,7 @@ module Typhoeus
|
|
99
99
|
#
|
100
100
|
# @return [ Typhoeus::Request ] The request.
|
101
101
|
#
|
102
|
-
# @note See {Ethon::Easy#initialize} for more options.
|
102
|
+
# @note See {http://rubydoc.info/github/typhoeus/ethon/Ethon/Easy#initialize-instance_method Ethon::Easy#initialize} for more options.
|
103
103
|
#
|
104
104
|
# @see Typhoeus::Hydra
|
105
105
|
# @see Typhoeus::Response
|
@@ -19,7 +19,7 @@ module Typhoeus
|
|
19
19
|
#
|
20
20
|
# @note (see Typhoeus::Request#initialize)
|
21
21
|
def get(url, options = {})
|
22
|
-
Request.
|
22
|
+
Request.new(url, options.merge(:method => :get)).run
|
23
23
|
end
|
24
24
|
|
25
25
|
# Make a post request.
|
@@ -35,7 +35,7 @@ module Typhoeus
|
|
35
35
|
#
|
36
36
|
# @note (see Typhoeus::Request#initialize)
|
37
37
|
def post(url, options = {})
|
38
|
-
Request.
|
38
|
+
Request.new(url, options.merge(:method => :post)).run
|
39
39
|
end
|
40
40
|
|
41
41
|
# Make a put request.
|
@@ -54,7 +54,7 @@ module Typhoeus
|
|
54
54
|
#
|
55
55
|
# @note (see Typhoeus::Request#initialize)
|
56
56
|
def put(url, options = {})
|
57
|
-
Request.
|
57
|
+
Request.new(url, options.merge(:method => :put)).run
|
58
58
|
end
|
59
59
|
|
60
60
|
# Make a delete request.
|
@@ -70,7 +70,7 @@ module Typhoeus
|
|
70
70
|
#
|
71
71
|
# @note (see Typhoeus::Request#initialize)
|
72
72
|
def delete(url, options = {})
|
73
|
-
Request.
|
73
|
+
Request.new(url, options.merge(:method => :delete)).run
|
74
74
|
end
|
75
75
|
|
76
76
|
# Make a head request.
|
@@ -86,7 +86,7 @@ module Typhoeus
|
|
86
86
|
#
|
87
87
|
# @note (see Typhoeus::Request#initialize)
|
88
88
|
def head(url, options = {})
|
89
|
-
Request.
|
89
|
+
Request.new(url, options.merge(:method => :head)).run
|
90
90
|
end
|
91
91
|
|
92
92
|
# Make a patch request.
|
@@ -102,7 +102,7 @@ module Typhoeus
|
|
102
102
|
#
|
103
103
|
# @note (see Typhoeus::Request#initialize)
|
104
104
|
def patch(url, options = {})
|
105
|
-
Request.
|
105
|
+
Request.new(url, options.merge(:method => :patch)).run
|
106
106
|
end
|
107
107
|
|
108
108
|
# Make a options request.
|
@@ -118,7 +118,7 @@ module Typhoeus
|
|
118
118
|
#
|
119
119
|
# @note (see Typhoeus::Request#initialize)
|
120
120
|
def options(url, options = {})
|
121
|
-
Request.
|
121
|
+
Request.new(url, options.merge(:method => :options)).run
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
@@ -42,9 +42,9 @@ module Typhoeus
|
|
42
42
|
# @return [ Boolean ] True if blocked, false else.
|
43
43
|
def blocked?
|
44
44
|
if block_connection.nil?
|
45
|
-
|
45
|
+
Typhoeus::Config.block_connection
|
46
46
|
else
|
47
|
-
|
47
|
+
block_connection
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -20,6 +20,10 @@ module Typhoeus
|
|
20
20
|
# request.on_complete.clear
|
21
21
|
# request.execute_callbacks
|
22
22
|
# #=> []
|
23
|
+
#
|
24
|
+
# @note If you're using the Hydra to execute multiple
|
25
|
+
# requests, then callbacks are delaying the
|
26
|
+
# request execution.
|
23
27
|
module Callbacks
|
24
28
|
|
25
29
|
module Types # :nodoc:
|
@@ -5,29 +5,6 @@ module Typhoeus
|
|
5
5
|
# to make a single request.
|
6
6
|
module Operations
|
7
7
|
|
8
|
-
# :nodoc:
|
9
|
-
def self.included(base)
|
10
|
-
base.extend ClassMethods
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods # :nodoc:
|
14
|
-
|
15
|
-
# Shortcut to perform a single request.
|
16
|
-
#
|
17
|
-
# @example Perform request.
|
18
|
-
# Request.run("www.example.com")
|
19
|
-
#
|
20
|
-
# @param [ String ] url The url to request.
|
21
|
-
# @param [ Hash ] options The options hash.
|
22
|
-
#
|
23
|
-
# @return [ Response ] The response.
|
24
|
-
#
|
25
|
-
# @deprecated
|
26
|
-
def run(url, options = {})
|
27
|
-
new(url, options).run
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
8
|
# Run a request.
|
32
9
|
#
|
33
10
|
# @example Run a request.
|
@@ -36,11 +13,16 @@ module Typhoeus
|
|
36
13
|
# @return [ Response ] The response.
|
37
14
|
def run
|
38
15
|
easy = Typhoeus.get_easy
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
16
|
+
begin
|
17
|
+
easy.http_request(
|
18
|
+
url,
|
19
|
+
options.fetch(:method, :get),
|
20
|
+
options.reject{|k,_| k==:method}
|
21
|
+
)
|
22
|
+
rescue Ethon::Errors::InvalidOption => e
|
23
|
+
help = provide_help(e.message.match(/:\s(\w+)/)[1])
|
24
|
+
raise $!, "#{$!}#{help}", $!.backtrace
|
25
|
+
end
|
44
26
|
easy.prepare
|
45
27
|
easy.perform
|
46
28
|
finish(Response.new(easy.to_hash))
|
@@ -66,6 +48,39 @@ module Typhoeus
|
|
66
48
|
execute_callbacks
|
67
49
|
response
|
68
50
|
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def provide_help(option)
|
55
|
+
renamed = {
|
56
|
+
:connect_timeout => :connecttimeout,
|
57
|
+
:follow_location => :followlocation,
|
58
|
+
:max_redirects => :maxredirs,
|
59
|
+
:proxy_username => :proxyuserpwd,
|
60
|
+
:proxy_password => :proxyuserpwd,
|
61
|
+
:disable_ssl_peer_verification => :ssl_verifypeer,
|
62
|
+
:disable_ssl_host_verification => :ssl_verifyhost,
|
63
|
+
:ssl_cert => :sslcert,
|
64
|
+
:ssl_cert_type => :sslcerttype,
|
65
|
+
:ssl_key => :sslkey,
|
66
|
+
:ssl_key_type => :sslkeytype,
|
67
|
+
:ssl_key_password => :keypasswd,
|
68
|
+
:ssl_cacert => :cainfo,
|
69
|
+
:ssl_capath => :capath,
|
70
|
+
:ssl_version => :sslversion,
|
71
|
+
:username => :userpwd,
|
72
|
+
:password => :userpwd,
|
73
|
+
:auth_method => :httpauth,
|
74
|
+
:proxy_auth_method => :proxyauth,
|
75
|
+
:proxy_type => :proxytype
|
76
|
+
}
|
77
|
+
removed = [:cache_key_basis, :cache_timout, :user_agent]
|
78
|
+
if new_option = renamed[option.to_sym]
|
79
|
+
"\nPlease try #{new_option} instead of #{option}." if new_option
|
80
|
+
elsif removed.include?(option.to_sym)
|
81
|
+
"\nThe option #{option} was removed."
|
82
|
+
end
|
83
|
+
end
|
69
84
|
end
|
70
85
|
end
|
71
86
|
end
|
data/lib/typhoeus/response.rb
CHANGED
@@ -14,7 +14,7 @@ module Typhoeus
|
|
14
14
|
# @example Get request.
|
15
15
|
# request = Typhoeus::Request.get("www.example.com")
|
16
16
|
# response = request.run
|
17
|
-
#
|
17
|
+
# request == response.request
|
18
18
|
# #=> true
|
19
19
|
#
|
20
20
|
# @return [ Typhoeus::Request ]
|
@@ -42,6 +42,9 @@ module Typhoeus
|
|
42
42
|
@headers = options[:headers]
|
43
43
|
end
|
44
44
|
|
45
|
+
# Returns wether this request is mocked
|
46
|
+
# or not.
|
47
|
+
#
|
45
48
|
# @api private
|
46
49
|
def mock
|
47
50
|
defined?(@mock) ? @mock : options[:mock]
|
@@ -23,23 +23,26 @@ module Typhoeus
|
|
23
23
|
# header.parse
|
24
24
|
def parse
|
25
25
|
raw.lines.each do |header|
|
26
|
-
|
27
|
-
|
28
|
-
unless parts.empty?
|
29
|
-
parts.map(&:strip!)
|
30
|
-
if self.has_key?(parts[0])
|
31
|
-
self[parts[0]] = [self[parts[0]]] unless self[parts[0]].kind_of? Array
|
32
|
-
self[parts[0]] << parts[1]
|
33
|
-
else
|
34
|
-
self[parts[0]] = parts[1]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
26
|
+
next if header.empty? || header =~ /^HTTP\/1.[01]/
|
27
|
+
process_line(header)
|
38
28
|
end
|
39
29
|
end
|
40
30
|
|
41
31
|
private
|
42
32
|
|
33
|
+
# Processes line and saves the result.
|
34
|
+
#
|
35
|
+
# @return [ void ]
|
36
|
+
def process_line(header)
|
37
|
+
key, value = header.split(':', 2).map(&:strip)
|
38
|
+
if self.has_key?(key)
|
39
|
+
self[key] = [self[key]] unless self[key].is_a? Array
|
40
|
+
self[key] << value
|
41
|
+
else
|
42
|
+
self[key] = value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
43
46
|
# Returns the raw header or empty string.
|
44
47
|
#
|
45
48
|
# @example Return raw header.
|
@@ -20,7 +20,7 @@ module Typhoeus
|
|
20
20
|
# Except when responding to a HEAD request, the server SHOULD include an entity containing
|
21
21
|
# an explanation of the error situation [...]
|
22
22
|
# This means 'HTTP/1.1 404' is as valid as 'HTTP/1.1 404 Not Found' and we have to handle it.
|
23
|
-
|
23
|
+
#
|
24
24
|
# Regexp doc: http://rubular.com/r/eAr1oVYsVa
|
25
25
|
if first_header_line != nil and first_header_line[/\d{3} (.*)$/, 1] != nil
|
26
26
|
@status_message = first_header_line[/\d{3} (.*)$/, 1].chomp
|
@@ -73,7 +73,13 @@ module Typhoeus
|
|
73
73
|
|
74
74
|
# :nodoc:
|
75
75
|
def first_header_line
|
76
|
-
@first_header_line ||=
|
76
|
+
@first_header_line ||= begin
|
77
|
+
if response_headers.to_s.include?("\r\n\r\n")
|
78
|
+
response_headers.to_s.split("\r\n\r\n").last.split("\r\n").first
|
79
|
+
else
|
80
|
+
response_headers.to_s.split("\r\n").first
|
81
|
+
end
|
82
|
+
end
|
77
83
|
end
|
78
84
|
end
|
79
85
|
end
|
data/lib/typhoeus/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typhoeus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- David Balatero
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-10-
|
14
|
+
date: 2012-10-30 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: ethon
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - '='
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.5.
|
23
|
+
version: 0.5.2
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - '='
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version: 0.5.
|
31
|
+
version: 0.5.2
|
32
32
|
description: Like a modern code version of the mythical beast with 100 serpent heads,
|
33
33
|
Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic.
|
34
34
|
email:
|
@@ -37,6 +37,7 @@ executables: []
|
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
|
+
- lib/typhoeus/adapters/faraday.rb
|
40
41
|
- lib/typhoeus/config.rb
|
41
42
|
- lib/typhoeus/errors/no_stub.rb
|
42
43
|
- lib/typhoeus/errors/typhoeus_error.rb
|
@@ -86,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
87
|
version: '0'
|
87
88
|
segments:
|
88
89
|
- 0
|
89
|
-
hash: -
|
90
|
+
hash: -2522351330136258713
|
90
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
92
|
none: false
|
92
93
|
requirements:
|