rest-client 1.1.0 → 1.6.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rest-client might be problematic. Click here for more details.
- data/README.rdoc +190 -13
- data/Rakefile +35 -26
- data/VERSION +1 -1
- data/bin/restclient +43 -38
- data/history.md +112 -0
- data/lib/rest-client.rb +2 -0
- data/lib/restclient.rb +107 -40
- data/lib/restclient/abstract_response.rb +106 -0
- data/lib/restclient/exceptions.rb +187 -82
- data/lib/restclient/net_http_ext.rb +11 -11
- data/lib/restclient/payload.rb +217 -168
- data/lib/restclient/raw_response.rb +28 -24
- data/lib/restclient/request.rb +310 -248
- data/lib/restclient/resource.rb +155 -132
- data/lib/restclient/response.rb +19 -15
- data/spec/abstract_response_spec.rb +85 -0
- data/spec/base.rb +6 -0
- data/spec/exceptions_spec.rb +79 -46
- data/spec/integration/certs/equifax.crt +19 -0
- data/spec/integration/certs/verisign.crt +14 -0
- data/spec/integration/request_spec.rb +25 -0
- data/spec/integration_spec.rb +38 -0
- data/spec/payload_spec.rb +187 -99
- data/spec/raw_response_spec.rb +12 -12
- data/spec/request2_spec.rb +27 -0
- data/spec/request_spec.rb +526 -494
- data/spec/resource_spec.rb +131 -72
- data/spec/response_spec.rb +166 -18
- data/spec/restclient_spec.rb +70 -50
- metadata +79 -20
- data/lib/restclient/mixin/response.rb +0 -48
- data/spec/mixin/response_spec.rb +0 -46
data/lib/restclient/request.rb
CHANGED
@@ -1,252 +1,314 @@
|
|
1
1
|
require 'tempfile'
|
2
|
+
require 'mime/types'
|
3
|
+
require 'cgi'
|
2
4
|
|
3
5
|
module RestClient
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
6
|
+
# This class is used internally by RestClient to send the request, but you can also
|
7
|
+
# call it directly if you'd like to use a method not supported by the
|
8
|
+
# main API. For example:
|
9
|
+
#
|
10
|
+
# RestClient::Request.execute(:method => :head, :url => 'http://example.com')
|
11
|
+
#
|
12
|
+
# Mandatory parameters:
|
13
|
+
# * :method
|
14
|
+
# * :url
|
15
|
+
# Optional parameters (have a look at ssl and/or uri for some explanations):
|
16
|
+
# * :headers a hash containing the request headers
|
17
|
+
# * :cookies will replace possible cookies in the :headers
|
18
|
+
# * :user and :password for basic auth, will be replaced by a user/password available in the :url
|
19
|
+
# * :block_response call the provided block with the HTTPResponse as parameter
|
20
|
+
# * :raw_response return a low-level RawResponse instead of a Response
|
21
|
+
# * :max_redirects maximum number of redirections (default to 10)
|
22
|
+
# * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
|
23
|
+
# * :timeout and :open_timeout
|
24
|
+
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
|
25
|
+
class Request
|
26
|
+
|
27
|
+
attr_reader :method, :url, :headers, :cookies,
|
28
|
+
:payload, :user, :password, :timeout, :max_redirects,
|
29
|
+
:open_timeout, :raw_response, :verify_ssl, :ssl_client_cert,
|
30
|
+
:ssl_client_key, :ssl_ca_file, :processed_headers, :args
|
31
|
+
|
32
|
+
def self.execute(args, & block)
|
33
|
+
new(args).execute(& block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize args
|
37
|
+
@method = args[:method] or raise ArgumentError, "must pass :method"
|
38
|
+
@headers = args[:headers] || {}
|
39
|
+
if args[:url]
|
40
|
+
@url = process_get_params(args[:url], headers)
|
41
|
+
else
|
42
|
+
raise ArgumentError, "must pass :url"
|
43
|
+
end
|
44
|
+
@cookies = @headers.delete(:cookies) || args[:cookies] || {}
|
45
|
+
@payload = Payload.generate(args[:payload])
|
46
|
+
@user = args[:user]
|
47
|
+
@password = args[:password]
|
48
|
+
@timeout = args[:timeout]
|
49
|
+
@open_timeout = args[:open_timeout]
|
50
|
+
@block_response = args[:block_response]
|
51
|
+
@raw_response = args[:raw_response] || false
|
52
|
+
@verify_ssl = args[:verify_ssl] || false
|
53
|
+
@ssl_client_cert = args[:ssl_client_cert] || nil
|
54
|
+
@ssl_client_key = args[:ssl_client_key] || nil
|
55
|
+
@ssl_ca_file = args[:ssl_ca_file] || nil
|
56
|
+
@tf = nil # If you are a raw request, this is your tempfile
|
57
|
+
@max_redirects = args[:max_redirects] || 10
|
58
|
+
@processed_headers = make_headers headers
|
59
|
+
@args = args
|
60
|
+
end
|
61
|
+
|
62
|
+
def execute & block
|
63
|
+
uri = parse_url_with_auth(url)
|
64
|
+
transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload, & block
|
65
|
+
end
|
66
|
+
|
67
|
+
# Extract the query parameters for get request and append them to the url
|
68
|
+
def process_get_params url, headers
|
69
|
+
if [:get, :head, :delete].include? method
|
70
|
+
get_params = {}
|
71
|
+
headers.delete_if do |key, value|
|
72
|
+
if 'params' == key.to_s.downcase && value.is_a?(Hash)
|
73
|
+
get_params.merge! value
|
74
|
+
true
|
75
|
+
else
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
unless get_params.empty?
|
80
|
+
query_string = get_params.collect { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
|
81
|
+
url + "?#{query_string}"
|
82
|
+
else
|
83
|
+
url
|
84
|
+
end
|
85
|
+
else
|
86
|
+
url
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def make_headers user_headers
|
91
|
+
unless @cookies.empty?
|
92
|
+
user_headers[:cookie] = @cookies.map { |(key, val)| "#{key.to_s}=#{CGI::unescape(val)}" }.sort.join(';')
|
93
|
+
end
|
94
|
+
headers = stringify_headers(default_headers).merge(stringify_headers(user_headers))
|
95
|
+
headers.merge!(@payload.headers) if @payload
|
96
|
+
headers
|
97
|
+
end
|
98
|
+
|
99
|
+
def net_http_class
|
100
|
+
if RestClient.proxy
|
101
|
+
proxy_uri = URI.parse(RestClient.proxy)
|
102
|
+
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
|
103
|
+
else
|
104
|
+
Net::HTTP
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def net_http_request_class(method)
|
109
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
110
|
+
end
|
111
|
+
|
112
|
+
def parse_url(url)
|
113
|
+
url = "http://#{url}" unless url.match(/^http/)
|
114
|
+
URI.parse(url)
|
115
|
+
end
|
116
|
+
|
117
|
+
def parse_url_with_auth(url)
|
118
|
+
uri = parse_url(url)
|
119
|
+
@user = CGI.unescape(uri.user) if uri.user
|
120
|
+
@password = CGI.unescape(uri.password) if uri.password
|
121
|
+
uri
|
122
|
+
end
|
123
|
+
|
124
|
+
def process_payload(p=nil, parent_key=nil)
|
125
|
+
unless p.is_a?(Hash)
|
126
|
+
p
|
127
|
+
else
|
128
|
+
@headers[:content_type] ||= 'application/x-www-form-urlencoded'
|
129
|
+
p.keys.map do |k|
|
130
|
+
key = parent_key ? "#{parent_key}[#{k}]" : k
|
131
|
+
if p[k].is_a? Hash
|
132
|
+
process_payload(p[k], key)
|
133
|
+
else
|
134
|
+
value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
135
|
+
"#{key}=#{value}"
|
136
|
+
end
|
137
|
+
end.join("&")
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def transmit uri, req, payload, & block
|
142
|
+
setup_credentials req
|
143
|
+
|
144
|
+
net = net_http_class.new(uri.host, uri.port)
|
145
|
+
net.use_ssl = uri.is_a?(URI::HTTPS)
|
146
|
+
if (@verify_ssl == false) || (@verify_ssl == OpenSSL::SSL::VERIFY_NONE)
|
147
|
+
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
148
|
+
elsif @verify_ssl.is_a? Integer
|
149
|
+
net.verify_mode = @verify_ssl
|
150
|
+
net.verify_callback = lambda do |preverify_ok, ssl_context|
|
151
|
+
if (!preverify_ok) || ssl_context.error != 0
|
152
|
+
err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
|
153
|
+
raise SSLCertificateNotVerified.new(err_msg)
|
154
|
+
end
|
155
|
+
true
|
156
|
+
end
|
157
|
+
end
|
158
|
+
net.cert = @ssl_client_cert if @ssl_client_cert
|
159
|
+
net.key = @ssl_client_key if @ssl_client_key
|
160
|
+
net.ca_file = @ssl_ca_file if @ssl_ca_file
|
161
|
+
net.read_timeout = @timeout if @timeout
|
162
|
+
net.open_timeout = @open_timeout if @open_timeout
|
163
|
+
|
164
|
+
RestClient.before_execution_procs.each do |before_proc|
|
165
|
+
before_proc.call(req, args)
|
166
|
+
end
|
167
|
+
|
168
|
+
log_request
|
169
|
+
|
170
|
+
net.start do |http|
|
171
|
+
if @block_response
|
172
|
+
http.request(req, payload ? payload.to_s : nil, & @block_response)
|
173
|
+
else
|
174
|
+
res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
|
175
|
+
log_response res
|
176
|
+
process_result res, & block
|
177
|
+
end
|
178
|
+
end
|
179
|
+
rescue EOFError
|
180
|
+
raise RestClient::ServerBrokeConnection
|
181
|
+
rescue Timeout::Error
|
182
|
+
raise RestClient::RequestTimeout
|
183
|
+
end
|
184
|
+
|
185
|
+
def setup_credentials(req)
|
186
|
+
req.basic_auth(user, password) if user
|
187
|
+
end
|
188
|
+
|
189
|
+
def fetch_body(http_response)
|
190
|
+
if @raw_response
|
191
|
+
# Taken from Chef, which as in turn...
|
192
|
+
# Stolen from http://www.ruby-forum.com/topic/166423
|
193
|
+
# Kudos to _why!
|
194
|
+
@tf = Tempfile.new("rest-client")
|
195
|
+
size, total = 0, http_response.header['Content-Length'].to_i
|
196
|
+
http_response.read_body do |chunk|
|
197
|
+
@tf.write chunk
|
198
|
+
size += chunk.size
|
199
|
+
if RestClient.log
|
200
|
+
if size == 0
|
201
|
+
RestClient.log << "#{@method} #{@url} done (0 length file\n)"
|
202
|
+
elsif total == 0
|
203
|
+
RestClient.log << "#{@method} #{@url} (zero content length)\n"
|
204
|
+
else
|
205
|
+
RestClient.log << "#{@method} #{@url} %d%% done (%d of %d)\n" % [(size * 100) / total, size, total]
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
@tf.close
|
210
|
+
@tf
|
211
|
+
else
|
212
|
+
http_response.read_body
|
213
|
+
end
|
214
|
+
http_response
|
215
|
+
end
|
216
|
+
|
217
|
+
def process_result res, & block
|
218
|
+
if @raw_response
|
219
|
+
# We don't decode raw requests
|
220
|
+
response = RawResponse.new(@tf, res, args)
|
221
|
+
else
|
222
|
+
response = Response.create(Request.decode(res['content-encoding'], res.body), res, args)
|
223
|
+
end
|
224
|
+
|
225
|
+
if block_given?
|
226
|
+
block.call(response, self, res, & block)
|
227
|
+
else
|
228
|
+
response.return!(self, res, & block)
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
def self.decode content_encoding, body
|
234
|
+
if (!body) || body.empty?
|
235
|
+
body
|
236
|
+
elsif content_encoding == 'gzip'
|
237
|
+
Zlib::GzipReader.new(StringIO.new(body)).read
|
238
|
+
elsif content_encoding == 'deflate'
|
239
|
+
begin
|
240
|
+
Zlib::Inflate.new.inflate body
|
241
|
+
rescue Zlib::DataError
|
242
|
+
# No luck with Zlib decompression. Let's try with raw deflate,
|
243
|
+
# like some broken web servers do.
|
244
|
+
Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate body
|
245
|
+
end
|
246
|
+
else
|
247
|
+
body
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def log_request
|
252
|
+
if RestClient.log
|
253
|
+
out = []
|
254
|
+
out << "RestClient.#{method} #{url.inspect}"
|
255
|
+
out << payload.short_inspect if payload
|
256
|
+
out << processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ")
|
257
|
+
RestClient.log << out.join(', ') + "\n"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def log_response res
|
262
|
+
if RestClient.log
|
263
|
+
size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size)
|
264
|
+
RestClient.log << "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes\n"
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
# Return a hash of headers whose keys are capitalized strings
|
269
|
+
def stringify_headers headers
|
270
|
+
headers.inject({}) do |result, (key, value)|
|
271
|
+
if key.is_a? Symbol
|
272
|
+
key = key.to_s.split(/_/).map { |w| w.capitalize }.join('-')
|
273
|
+
end
|
274
|
+
if 'CONTENT-TYPE' == key.upcase
|
275
|
+
target_value = value.to_s
|
276
|
+
result[key] = MIME::Types.type_for_extension target_value
|
277
|
+
elsif 'ACCEPT' == key.upcase
|
278
|
+
# Accept can be composed of several comma-separated values
|
279
|
+
if value.is_a? Array
|
280
|
+
target_values = value
|
281
|
+
else
|
282
|
+
target_values = value.to_s.split ','
|
283
|
+
end
|
284
|
+
result[key] = target_values.map { |ext| MIME::Types.type_for_extension(ext.to_s.strip) }.join(', ')
|
285
|
+
else
|
286
|
+
result[key] = value.to_s
|
287
|
+
end
|
288
|
+
result
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def default_headers
|
293
|
+
{:accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate'}
|
294
|
+
end
|
295
|
+
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
module MIME
|
300
|
+
class Types
|
301
|
+
|
302
|
+
# Return the first found content-type for a value considered as an extension or the value itself
|
303
|
+
def type_for_extension ext
|
304
|
+
candidates = @extension_index[ext]
|
305
|
+
candidates.empty? ? ext : candidates[0].content_type
|
306
|
+
end
|
307
|
+
|
308
|
+
class << self
|
309
|
+
def type_for_extension ext
|
310
|
+
@__types__.type_for_extension ext
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
252
314
|
end
|