typhoeus 0.4.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +4 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +341 -28
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +31 -2
- data/Guardfile +9 -0
- data/LICENSE +1 -1
- data/README.md +486 -357
- data/Rakefile +21 -12
- data/UPGRADE.md +55 -0
- data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
- data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
- data/lib/rack/typhoeus.rb +1 -0
- data/lib/typhoeus/adapters/faraday.rb +180 -0
- data/lib/typhoeus/cache/dalli.rb +28 -0
- data/lib/typhoeus/cache/rails.rb +28 -0
- data/lib/typhoeus/cache/redis.rb +35 -0
- data/lib/typhoeus/config.rb +69 -0
- data/lib/typhoeus/easy_factory.rb +180 -0
- data/lib/typhoeus/errors/no_stub.rb +12 -0
- data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
- data/lib/typhoeus/errors.rb +9 -0
- data/lib/typhoeus/expectation.rb +217 -0
- data/lib/typhoeus/hydra/addable.rb +23 -0
- data/lib/typhoeus/hydra/before.rb +31 -0
- data/lib/typhoeus/hydra/block_connection.rb +35 -0
- data/lib/typhoeus/hydra/cacheable.rb +15 -0
- data/lib/typhoeus/hydra/memoizable.rb +56 -0
- data/lib/typhoeus/hydra/queueable.rb +83 -0
- data/lib/typhoeus/hydra/runnable.rb +19 -0
- data/lib/typhoeus/hydra/stubbable.rb +28 -0
- data/lib/typhoeus/hydra.rb +84 -236
- data/lib/typhoeus/pool.rb +70 -0
- data/lib/typhoeus/railtie.rb +12 -0
- data/lib/typhoeus/request/actions.rb +125 -0
- data/lib/typhoeus/request/before.rb +30 -0
- data/lib/typhoeus/request/block_connection.rb +52 -0
- data/lib/typhoeus/request/cacheable.rb +38 -0
- data/lib/typhoeus/request/callbacks.rb +151 -0
- data/lib/typhoeus/request/marshal.rb +22 -0
- data/lib/typhoeus/request/memoizable.rb +38 -0
- data/lib/typhoeus/request/operations.rb +40 -0
- data/lib/typhoeus/request/responseable.rb +29 -0
- data/lib/typhoeus/request/streamable.rb +34 -0
- data/lib/typhoeus/request/stubbable.rb +30 -0
- data/lib/typhoeus/request.rb +186 -231
- data/lib/typhoeus/response/cacheable.rb +14 -0
- data/lib/typhoeus/response/header.rb +105 -0
- data/lib/typhoeus/response/informations.rb +248 -0
- data/lib/typhoeus/response/status.rb +106 -0
- data/lib/typhoeus/response.rb +60 -115
- data/lib/typhoeus/version.rb +3 -1
- data/lib/typhoeus.rb +126 -39
- data/perf/profile.rb +14 -0
- data/perf/vs_nethttp.rb +64 -0
- data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
- data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/localhost_server.rb +94 -0
- data/spec/support/memory_cache.rb +15 -0
- data/spec/support/server.rb +116 -0
- data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
- data/spec/typhoeus/cache/dalli_spec.rb +41 -0
- data/spec/typhoeus/cache/redis_spec.rb +41 -0
- data/spec/typhoeus/config_spec.rb +15 -0
- data/spec/typhoeus/easy_factory_spec.rb +143 -0
- data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
- data/spec/typhoeus/expectation_spec.rb +280 -0
- data/spec/typhoeus/hydra/addable_spec.rb +22 -0
- data/spec/typhoeus/hydra/before_spec.rb +98 -0
- data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
- data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
- data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
- data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
- data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
- data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
- data/spec/typhoeus/hydra_spec.rb +22 -0
- data/spec/typhoeus/pool_spec.rb +137 -0
- data/spec/typhoeus/request/actions_spec.rb +19 -0
- data/spec/typhoeus/request/before_spec.rb +93 -0
- data/spec/typhoeus/request/block_connection_spec.rb +75 -0
- data/spec/typhoeus/request/cacheable_spec.rb +94 -0
- data/spec/typhoeus/request/callbacks_spec.rb +91 -0
- data/spec/typhoeus/request/marshal_spec.rb +60 -0
- data/spec/typhoeus/request/memoizable_spec.rb +34 -0
- data/spec/typhoeus/request/operations_spec.rb +101 -0
- data/spec/typhoeus/request/responseable_spec.rb +13 -0
- data/spec/typhoeus/request/stubbable_spec.rb +45 -0
- data/spec/typhoeus/request_spec.rb +232 -0
- data/spec/typhoeus/response/header_spec.rb +147 -0
- data/spec/typhoeus/response/informations_spec.rb +283 -0
- data/spec/typhoeus/response/status_spec.rb +256 -0
- data/spec/typhoeus/response_spec.rb +100 -0
- data/spec/typhoeus_spec.rb +105 -0
- data/typhoeus.gemspec +25 -0
- metadata +146 -158
- data/lib/typhoeus/curl.rb +0 -453
- data/lib/typhoeus/easy/auth.rb +0 -14
- data/lib/typhoeus/easy/callbacks.rb +0 -33
- data/lib/typhoeus/easy/ffi_helper.rb +0 -61
- data/lib/typhoeus/easy/infos.rb +0 -90
- data/lib/typhoeus/easy/options.rb +0 -115
- data/lib/typhoeus/easy/proxy.rb +0 -20
- data/lib/typhoeus/easy/ssl.rb +0 -82
- data/lib/typhoeus/easy.rb +0 -115
- data/lib/typhoeus/filter.rb +0 -28
- data/lib/typhoeus/form.rb +0 -61
- data/lib/typhoeus/header.rb +0 -54
- data/lib/typhoeus/hydra/callbacks.rb +0 -24
- data/lib/typhoeus/hydra/connect_options.rb +0 -61
- data/lib/typhoeus/hydra/stubbing.rb +0 -68
- data/lib/typhoeus/hydra_mock.rb +0 -131
- data/lib/typhoeus/multi.rb +0 -146
- data/lib/typhoeus/param_processor.rb +0 -43
- data/lib/typhoeus/remote.rb +0 -306
- data/lib/typhoeus/remote_method.rb +0 -108
- data/lib/typhoeus/remote_proxy_object.rb +0 -50
- data/lib/typhoeus/utils.rb +0 -50
@@ -0,0 +1,248 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Response
|
3
|
+
|
4
|
+
# This module contains logic about informations
|
5
|
+
# on a response.
|
6
|
+
module Informations
|
7
|
+
|
8
|
+
# Return libcurls return value.
|
9
|
+
#
|
10
|
+
# @example Get return_code.
|
11
|
+
# response.return_code
|
12
|
+
#
|
13
|
+
# @return [ Symbol ] The return_code.
|
14
|
+
def return_code
|
15
|
+
options[:return_code]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a string describing the return.
|
19
|
+
#
|
20
|
+
# @example Get return_message.
|
21
|
+
# response.return_message
|
22
|
+
#
|
23
|
+
# @return [ String ] The return_message.
|
24
|
+
#
|
25
|
+
# @since 0.6.2
|
26
|
+
def return_message
|
27
|
+
Ethon::Curl.easy_strerror(return_code) if return_code
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return the http response body.
|
31
|
+
#
|
32
|
+
# @example Get response_body.
|
33
|
+
# response.response_body
|
34
|
+
#
|
35
|
+
# @return [ String ] The response_body.
|
36
|
+
def response_body
|
37
|
+
options[:response_body] || options[:body]
|
38
|
+
end
|
39
|
+
alias :body :response_body
|
40
|
+
|
41
|
+
# Return the http response headers.
|
42
|
+
#
|
43
|
+
# @example Get response_headers.
|
44
|
+
# response.response_headers
|
45
|
+
#
|
46
|
+
# @return [ String ] The response_headers.
|
47
|
+
def response_headers
|
48
|
+
return options[:response_headers] if options[:response_headers]
|
49
|
+
if mock? && h = options[:headers]
|
50
|
+
status_code = return_code || "200"
|
51
|
+
reason_phrase = status_code == "200" ? "OK" : "Mock Reason Phrase"
|
52
|
+
status_line = "HTTP/1.1 #{status_code} #{reason_phrase}"
|
53
|
+
actual_headers = h.map{ |k,v| [k, v.respond_to?(:join) ? v.join(',') : v] }.
|
54
|
+
map{ |e| "#{e.first}: #{e.last}" }
|
55
|
+
|
56
|
+
[status_line, *actual_headers].join("\r\n")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Return the last received HTTP, FTP or SMTP response code.
|
61
|
+
# The value will be zero if no server response code has
|
62
|
+
# been received. Note that a proxy's CONNECT response should
|
63
|
+
# be read with http_connect_code and not this.
|
64
|
+
#
|
65
|
+
# @example Get response_code.
|
66
|
+
# response.response_code
|
67
|
+
#
|
68
|
+
# @return [ Integer ] The response_code.
|
69
|
+
def response_code
|
70
|
+
(options[:response_code] || options[:code]).to_i
|
71
|
+
end
|
72
|
+
alias :code :response_code
|
73
|
+
|
74
|
+
# Return the available http auth methods.
|
75
|
+
# Bitmask indicating the authentication method(s)
|
76
|
+
# available.
|
77
|
+
#
|
78
|
+
# @example Get httpauth_avail.
|
79
|
+
# response.httpauth_avail
|
80
|
+
#
|
81
|
+
# @return [ Integer ] The bitmask.
|
82
|
+
def httpauth_avail
|
83
|
+
options[:httpauth_avail]
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
# Return the total time in seconds for the previous
|
88
|
+
# transfer, including name resolving, TCP connect etc.
|
89
|
+
#
|
90
|
+
# @example Get total_time.
|
91
|
+
# response.total_time
|
92
|
+
#
|
93
|
+
# @return [ Float ] The total_time.
|
94
|
+
def total_time
|
95
|
+
options[:total_time] || options[:time]
|
96
|
+
end
|
97
|
+
alias :time :total_time
|
98
|
+
|
99
|
+
# Return the time, in seconds, it took from the start
|
100
|
+
# until the first byte is received by libcurl. This
|
101
|
+
# includes pretransfer time and also the time the
|
102
|
+
# server needs to calculate the result.
|
103
|
+
#
|
104
|
+
# @example Get starttransfer_time.
|
105
|
+
# response.starttransfer_time
|
106
|
+
#
|
107
|
+
# @return [ Float ] The starttransfer_time.
|
108
|
+
def starttransfer_time
|
109
|
+
options[:starttransfer_time] || options[:start_transfer_time]
|
110
|
+
end
|
111
|
+
alias :start_transfer_time :starttransfer_time
|
112
|
+
|
113
|
+
# Return the time, in seconds, it took from the start
|
114
|
+
# until the SSL/SSH connect/handshake to the remote
|
115
|
+
# host was completed. This time is most often very near
|
116
|
+
# to the pre transfer time, except for cases such as HTTP
|
117
|
+
# pipelining where the pretransfer time can be delayed
|
118
|
+
# due to waits in line for the pipeline and more.
|
119
|
+
#
|
120
|
+
# @example Get appconnect_time.
|
121
|
+
# response.appconnect_time
|
122
|
+
#
|
123
|
+
# @return [ Float ] The appconnect_time.
|
124
|
+
def appconnect_time
|
125
|
+
options[:appconnect_time] || options[:app_connect_time]
|
126
|
+
end
|
127
|
+
alias :app_connect_time :appconnect_time
|
128
|
+
|
129
|
+
# Return the time, in seconds, it took from the start
|
130
|
+
# until the file transfer is just about to begin. This
|
131
|
+
# includes all pre-transfer commands and negotiations
|
132
|
+
# that are specific to the particular protocol(s) involved.
|
133
|
+
# It does not involve the sending of the protocol-
|
134
|
+
# specific request that triggers a transfer.
|
135
|
+
#
|
136
|
+
# @example Get pretransfer_time.
|
137
|
+
# response.pretransfer_time
|
138
|
+
#
|
139
|
+
# @return [ Float ] The pretransfer_time.
|
140
|
+
def pretransfer_time
|
141
|
+
options[:pretransfer_time]
|
142
|
+
end
|
143
|
+
|
144
|
+
# Return the time, in seconds, it took from the start
|
145
|
+
# until the connect to the remote host (or proxy) was completed.
|
146
|
+
#
|
147
|
+
# @example Get connect_time.
|
148
|
+
# response.connect_time
|
149
|
+
#
|
150
|
+
# @return [ Float ] The connect_time.
|
151
|
+
def connect_time
|
152
|
+
options[:connect_time]
|
153
|
+
end
|
154
|
+
|
155
|
+
# Return the time, in seconds, it took from the
|
156
|
+
# start until the name resolving was completed.
|
157
|
+
#
|
158
|
+
# @example Get namelookup_time.
|
159
|
+
# response.namelookup_time
|
160
|
+
#
|
161
|
+
# @return [ Float ] The namelookup_time.
|
162
|
+
def namelookup_time
|
163
|
+
options[:namelookup_time] || options[:name_lookup_time]
|
164
|
+
end
|
165
|
+
alias :name_lookup_time :namelookup_time
|
166
|
+
|
167
|
+
# Return the time, in seconds, it took for all redirection steps
|
168
|
+
# include name lookup, connect, pretransfer and transfer before the
|
169
|
+
# final transaction was started. time_redirect shows the complete
|
170
|
+
# execution time for multiple redirections.
|
171
|
+
#
|
172
|
+
# @example Get redirect_time.
|
173
|
+
# response.redirect_time
|
174
|
+
#
|
175
|
+
# @return [ Float ] The redirect_time.
|
176
|
+
def redirect_time
|
177
|
+
options[:redirect_time]
|
178
|
+
end
|
179
|
+
|
180
|
+
# Return the last used effective url.
|
181
|
+
#
|
182
|
+
# @example Get effective_url.
|
183
|
+
# response.effective_url
|
184
|
+
#
|
185
|
+
# @return [ String ] The effective_url.
|
186
|
+
def effective_url
|
187
|
+
options[:effective_url]
|
188
|
+
end
|
189
|
+
|
190
|
+
# Return the string holding the IP address of the most recent
|
191
|
+
# connection done with this curl handle. This string
|
192
|
+
# may be IPv6 if that's enabled.
|
193
|
+
#
|
194
|
+
# @example Get primary_ip.
|
195
|
+
# response.primary_ip
|
196
|
+
#
|
197
|
+
# @return [ String ] The primary_ip.
|
198
|
+
def primary_ip
|
199
|
+
options[:primary_ip]
|
200
|
+
end
|
201
|
+
|
202
|
+
# Return the total number of redirections that were
|
203
|
+
# actually followed
|
204
|
+
#
|
205
|
+
# @example Get redirect_count.
|
206
|
+
# response.redirect_count
|
207
|
+
#
|
208
|
+
# @return [ Integer ] The redirect_count.
|
209
|
+
def redirect_count
|
210
|
+
options[:redirect_count]
|
211
|
+
end
|
212
|
+
|
213
|
+
def request_size
|
214
|
+
options[:request_size]
|
215
|
+
end
|
216
|
+
|
217
|
+
def debug_info
|
218
|
+
options[:debug_info]
|
219
|
+
end
|
220
|
+
|
221
|
+
# Returns the response header.
|
222
|
+
#
|
223
|
+
# @example Return headers.
|
224
|
+
# response.headers
|
225
|
+
#
|
226
|
+
# @return [ Typhoeus::Header ] The response header.
|
227
|
+
def headers
|
228
|
+
return Header.new(options[:headers]) if mock? && options[:headers]
|
229
|
+
return nil if response_headers.nil? && !defined?(@headers)
|
230
|
+
@headers ||= Header.new(response_headers.split("\r\n\r\n").last)
|
231
|
+
end
|
232
|
+
alias :headers_hash :headers
|
233
|
+
|
234
|
+
# Return all redirections in between as multiple
|
235
|
+
# responses with header.
|
236
|
+
#
|
237
|
+
# @example Return redirections.
|
238
|
+
# response.redirections
|
239
|
+
#
|
240
|
+
# @return [ Array<Typhoeus::Response> ] The redirections
|
241
|
+
def redirections
|
242
|
+
return [] unless response_headers
|
243
|
+
response_headers.split("\r\n\r\n")[0..-2].map{ |h| Response.new(:response_headers => h) }
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Response
|
3
|
+
|
4
|
+
# This module contains logic about the http
|
5
|
+
# status.
|
6
|
+
module Status
|
7
|
+
|
8
|
+
# Return the status message if present.
|
9
|
+
#
|
10
|
+
# @example Return status message.
|
11
|
+
# reesponse.status_message
|
12
|
+
#
|
13
|
+
# @return [ String ] The message.
|
14
|
+
def status_message
|
15
|
+
return @status_message if defined?(@status_message) && @status_message
|
16
|
+
return options[:status_message] unless options[:status_message].nil?
|
17
|
+
|
18
|
+
# HTTP servers can choose not to include the explanation to HTTP codes. The RFC
|
19
|
+
# states this (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4):
|
20
|
+
# Except when responding to a HEAD request, the server SHOULD include an entity containing
|
21
|
+
# an explanation of the error situation [...]
|
22
|
+
# This means 'HTTP/1.1 404' is as valid as 'HTTP/1.1 404 Not Found' and we have to handle it.
|
23
|
+
#
|
24
|
+
# Regexp doc: http://rubular.com/r/eAr1oVYsVa
|
25
|
+
if first_header_line != nil and first_header_line[/\d{3} (.*)$/, 1] != nil
|
26
|
+
@status_message = first_header_line[/\d{3} (.*)$/, 1].chomp
|
27
|
+
else
|
28
|
+
@status_message = nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the http version.
|
33
|
+
#
|
34
|
+
# @example Return http version.
|
35
|
+
# response.http_version
|
36
|
+
#
|
37
|
+
# @return [ String ] The http version.
|
38
|
+
def http_version
|
39
|
+
@http_version ||= first_header_line ? first_header_line[/HTTP\/(\S+)/, 1] : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
# Return whether the response is a success.
|
43
|
+
#
|
44
|
+
# @example Return if the response was successful.
|
45
|
+
# response.success?
|
46
|
+
#
|
47
|
+
# @return [ Boolean ] Return true if successful, false else.
|
48
|
+
def success?
|
49
|
+
(mock || return_code == :ok) && response_code && has_good_response_code?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return whether the response is a failure.
|
53
|
+
#
|
54
|
+
# @example Return if the response was failed.
|
55
|
+
# response.failure?
|
56
|
+
#
|
57
|
+
# @return [ Boolean ] Return true if failure, false else.
|
58
|
+
def failure?
|
59
|
+
(mock || return_code == :internal_server_error) && response_code && has_bad_response_code?
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return wether the response is modified.
|
63
|
+
#
|
64
|
+
# @example Return if the response was modified.
|
65
|
+
# response.modified?
|
66
|
+
#
|
67
|
+
# @return [ Boolean ] Return true if modified, false else.
|
68
|
+
def modified?
|
69
|
+
(mock || return_code == :ok) && response_code && response_code != 304
|
70
|
+
end
|
71
|
+
|
72
|
+
# Return whether the response is timed out.
|
73
|
+
#
|
74
|
+
# @example Return if the response timed out.
|
75
|
+
# response.timed_out?
|
76
|
+
#
|
77
|
+
# @return [ Boolean ] Return true if timed out, false else.
|
78
|
+
def timed_out?
|
79
|
+
return_code == :operation_timedout
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
# :nodoc:
|
85
|
+
def first_header_line
|
86
|
+
@first_header_line ||= begin
|
87
|
+
if response_headers.to_s.include?("\r\n\r\n")
|
88
|
+
response_headers.to_s.split("\r\n\r\n").last.split("\r\n").first
|
89
|
+
else
|
90
|
+
response_headers.to_s.split("\r\n").first
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# :nodoc:
|
96
|
+
def has_good_response_code?
|
97
|
+
response_code >= 200 && response_code < 300
|
98
|
+
end
|
99
|
+
|
100
|
+
# :nodoc:
|
101
|
+
def has_bad_response_code?
|
102
|
+
!has_good_response_code?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/lib/typhoeus/response.rb
CHANGED
@@ -1,123 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
:requested_url, :requested_remote_method,
|
6
|
-
:requested_http_method, :start_time,
|
7
|
-
:effective_url, :start_transfer_time,
|
8
|
-
:app_connect_time, :pretransfer_time,
|
9
|
-
:connect_time, :name_lookup_time,
|
10
|
-
:curl_return_code, :curl_error_message,
|
11
|
-
:primary_ip, :redirect_count
|
12
|
-
|
13
|
-
attr_writer :headers_hash
|
14
|
-
|
15
|
-
def initialize(params = {})
|
16
|
-
@code = params[:code]
|
17
|
-
@curl_return_code = params[:curl_return_code]
|
18
|
-
@curl_error_message = params[:curl_error_message]
|
19
|
-
@status_message = params[:status_message]
|
20
|
-
@http_version = params[:http_version]
|
21
|
-
@headers = params[:headers]
|
22
|
-
@body = params[:body]
|
23
|
-
@time = params[:time]
|
24
|
-
@requested_url = params[:requested_url]
|
25
|
-
@requested_http_method = params[:requested_http_method]
|
26
|
-
@start_time = params[:start_time]
|
27
|
-
@start_transfer_time = params[:start_transfer_time]
|
28
|
-
@app_connect_time = params[:app_connect_time]
|
29
|
-
@pretransfer_time = params[:pretransfer_time]
|
30
|
-
@connect_time = params[:connect_time]
|
31
|
-
@name_lookup_time = params[:name_lookup_time]
|
32
|
-
@request = params[:request]
|
33
|
-
@effective_url = params[:effective_url]
|
34
|
-
@primary_ip = params[:primary_ip]
|
35
|
-
@redirect_count = params[:redirect_count]
|
36
|
-
@mock = params[:mock] || false # default
|
37
|
-
@headers_hash = Header.new(params[:headers_hash]) if params[:headers_hash]
|
38
|
-
end
|
39
|
-
|
40
|
-
# Returns true if this is a mock response.
|
41
|
-
def mock?
|
42
|
-
@mock
|
43
|
-
end
|
44
|
-
|
45
|
-
def headers
|
46
|
-
@headers ||= @headers_hash ? construct_header_string : ''
|
47
|
-
end
|
48
|
-
|
49
|
-
def headers_hash
|
50
|
-
@headers_hash ||= begin
|
51
|
-
headers.split("\n").map {|o| o.strip}.inject(Typhoeus::Header.new) do |hash, o|
|
52
|
-
if o.empty? || o =~ /^HTTP\/[\d\.]+/
|
53
|
-
hash
|
54
|
-
else
|
55
|
-
i = o.index(":") || o.size
|
56
|
-
key = o.slice(0, i)
|
57
|
-
value = o.slice(i + 1, o.size)
|
58
|
-
value = value.strip unless value.nil?
|
59
|
-
if hash.key? key
|
60
|
-
hash[key] = [hash[key], value].flatten
|
61
|
-
else
|
62
|
-
hash[key] = value
|
63
|
-
end
|
64
|
-
|
65
|
-
hash
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def status_message
|
72
|
-
return @status_message if @status_message != nil
|
1
|
+
require 'typhoeus/response/header'
|
2
|
+
require 'typhoeus/response/informations'
|
3
|
+
require 'typhoeus/response/status'
|
4
|
+
require 'typhoeus/response/cacheable'
|
73
5
|
|
74
|
-
|
75
|
-
# states this (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4):
|
76
|
-
# Except when responding to a HEAD request, the server SHOULD include an entity containing
|
77
|
-
# an explanation of the error situation [...]
|
78
|
-
# This means 'HTTP/1.1 404' is as valid as 'HTTP/1.1 404 Not Found' and we have to handle it.
|
79
|
-
|
80
|
-
# Regexp doc: http://rubular.com/r/eAr1oVYsVa
|
81
|
-
if first_header_line != nil and first_header_line[/\d{3} (.*)$/, 1] != nil
|
82
|
-
@status_message = first_header_line[/\d{3} (.*)$/, 1].chomp
|
83
|
-
else
|
84
|
-
@status_message = nil
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def http_version
|
89
|
-
@http_version ||= first_header_line ? first_header_line[/HTTP\/(\S+)/, 1] : nil
|
90
|
-
end
|
6
|
+
module Typhoeus
|
91
7
|
|
92
|
-
|
93
|
-
|
8
|
+
# This class represents the response.
|
9
|
+
class Response
|
10
|
+
include Response::Informations
|
11
|
+
include Response::Status
|
12
|
+
include Response::Cacheable
|
13
|
+
|
14
|
+
# Remembers the corresponding request.
|
15
|
+
#
|
16
|
+
# @example Get request.
|
17
|
+
# request = Typhoeus::Request.new("www.example.com")
|
18
|
+
# response = request.run
|
19
|
+
# request == response.request
|
20
|
+
# #=> true
|
21
|
+
#
|
22
|
+
# @return [ Typhoeus::Request ]
|
23
|
+
attr_accessor :request
|
24
|
+
|
25
|
+
# The provided options, which contain all the
|
26
|
+
# informations about the request.
|
27
|
+
#
|
28
|
+
# @return [ Hash ]
|
29
|
+
attr_accessor :options
|
30
|
+
|
31
|
+
# Set the handled response.
|
32
|
+
attr_writer :handled_response
|
33
|
+
|
34
|
+
# @api private
|
35
|
+
attr_writer :mock
|
36
|
+
|
37
|
+
# Create a new response.
|
38
|
+
#
|
39
|
+
# @example Create a response.
|
40
|
+
# Response.new
|
41
|
+
#
|
42
|
+
# @param [ Hash ] options The options hash.
|
43
|
+
#
|
44
|
+
# @return [ Response ] The new response.
|
45
|
+
def initialize(options = {})
|
46
|
+
@options = options
|
47
|
+
@headers = Header.new(options[:headers]) if options[:headers]
|
94
48
|
end
|
95
49
|
|
96
|
-
|
97
|
-
|
50
|
+
# Returns whether this request is mocked
|
51
|
+
# or not.
|
52
|
+
#
|
53
|
+
# @api private
|
54
|
+
def mock
|
55
|
+
defined?(@mock) ? @mock : options[:mock]
|
98
56
|
end
|
99
|
-
|
100
|
-
|
101
|
-
|
57
|
+
alias :mock? :mock
|
58
|
+
|
59
|
+
# Returns the handled_response if it has
|
60
|
+
# been defined; otherwise, returns the response
|
61
|
+
#
|
62
|
+
# @return [ Object ] The result of callbacks
|
63
|
+
# done on the response or the original response.
|
64
|
+
def handled_response
|
65
|
+
@handled_response || self
|
102
66
|
end
|
103
|
-
|
104
|
-
private
|
105
|
-
|
106
|
-
def first_header_line
|
107
|
-
@first_header_line ||= @headers.to_s.split("\n").first
|
108
|
-
end
|
109
|
-
|
110
|
-
def construct_header_string
|
111
|
-
lines = ["HTTP/#{http_version} #{code} #{status_message}"]
|
112
|
-
|
113
|
-
@headers_hash.each do |key, values|
|
114
|
-
[values].flatten.each do |value|
|
115
|
-
lines << "#{key}: #{value}"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
lines << '' << ''
|
120
|
-
lines.join("\r\n")
|
121
|
-
end
|
122
67
|
end
|
123
68
|
end
|
data/lib/typhoeus/version.rb
CHANGED