rest-client 1.1.0 → 1.2.0
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 +3 -0
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/restclient.rb +32 -31
- data/lib/restclient/exceptions.rb +70 -69
- data/lib/restclient/mixin/response.rb +40 -40
- data/lib/restclient/net_http_ext.rb +11 -11
- data/lib/restclient/payload.rb +175 -168
- data/lib/restclient/raw_response.rb +22 -22
- data/lib/restclient/request.rb +283 -248
- data/lib/restclient/resource.rb +132 -132
- data/lib/restclient/response.rb +13 -13
- data/spec/exceptions_spec.rb +45 -45
- data/spec/mixin/response_spec.rb +38 -38
- data/spec/payload_spec.rb +92 -92
- data/spec/raw_response_spec.rb +11 -11
- data/spec/request_spec.rb +517 -493
- data/spec/resource_spec.rb +57 -57
- data/spec/response_spec.rb +15 -15
- data/spec/restclient_spec.rb +49 -49
- metadata +2 -2
@@ -1,30 +1,30 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/mixin/response'
|
2
2
|
|
3
3
|
module RestClient
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
# The response from RestClient on a raw request looks like a string, but is
|
5
|
+
# actually one of these. 99% of the time you're making a rest call all you
|
6
|
+
# care about is the body, but on the occassion you want to fetch the
|
7
|
+
# headers you can:
|
8
|
+
#
|
9
|
+
# RestClient.get('http://example.com').headers[:content_type]
|
10
|
+
#
|
11
|
+
# In addition, if you do not use the response as a string, you can access
|
12
|
+
# a Tempfile object at res.file, which contains the path to the raw
|
13
|
+
# downloaded request body.
|
14
|
+
class RawResponse
|
15
|
+
include RestClient::Mixin::Response
|
16
16
|
|
17
|
-
|
17
|
+
attr_reader :file
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def initialize(tempfile, net_http_res)
|
20
|
+
@net_http_res = net_http_res
|
21
|
+
@file = tempfile
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
def to_s
|
25
|
+
@file.open
|
26
|
+
@file.read
|
27
|
+
end
|
28
28
|
|
29
|
-
|
29
|
+
end
|
30
30
|
end
|
data/lib/restclient/request.rb
CHANGED
@@ -1,252 +1,287 @@
|
|
1
1
|
require 'tempfile'
|
2
|
+
require 'mime/types'
|
2
3
|
|
3
4
|
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
|
-
|
5
|
+
# This class is used internally by RestClient to send the request, but you can also
|
6
|
+
# call it directly if you'd like to use a method not supported by the
|
7
|
+
# main API. For example:
|
8
|
+
#
|
9
|
+
# RestClient::Request.execute(:method => :head, :url => 'http://example.com')
|
10
|
+
#
|
11
|
+
# Mandatory parameters:
|
12
|
+
# * :method
|
13
|
+
# * :url
|
14
|
+
# Optional parameters (have a look at ssl and/or uri for some explanations):
|
15
|
+
# * :headers a hash containing the request headers
|
16
|
+
# * :cookies will replace possible cookies in the :headers
|
17
|
+
# * :user and :password for basic auth, will be replaced by a user/password available in the :url
|
18
|
+
# * :raw_response return a low-level RawResponse instead of a Response
|
19
|
+
# * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
|
20
|
+
# * :timeout and :open_timeout
|
21
|
+
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
|
22
|
+
class Request
|
23
|
+
attr_reader :method, :url, :payload, :headers, :processed_headers,
|
24
|
+
:cookies, :user, :password, :timeout, :open_timeout,
|
25
|
+
:verify_ssl, :ssl_client_cert, :ssl_client_key, :ssl_ca_file,
|
26
|
+
:raw_response
|
27
|
+
|
28
|
+
def self.execute(args)
|
29
|
+
new(args).execute
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(args)
|
33
|
+
@method = args[:method] or raise ArgumentError, "must pass :method"
|
34
|
+
@url = args[:url] or raise ArgumentError, "must pass :url"
|
35
|
+
@headers = args[:headers] || {}
|
36
|
+
@cookies = @headers.delete(:cookies) || args[:cookies] || {}
|
37
|
+
@payload = Payload.generate(args[:payload])
|
38
|
+
@user = args[:user]
|
39
|
+
@password = args[:password]
|
40
|
+
@timeout = args[:timeout]
|
41
|
+
@open_timeout = args[:open_timeout]
|
42
|
+
@raw_response = args[:raw_response] || false
|
43
|
+
@verify_ssl = args[:verify_ssl] || false
|
44
|
+
@ssl_client_cert = args[:ssl_client_cert] || nil
|
45
|
+
@ssl_client_key = args[:ssl_client_key] || nil
|
46
|
+
@ssl_ca_file = args[:ssl_ca_file] || nil
|
47
|
+
@tf = nil # If you are a raw request, this is your tempfile
|
48
|
+
@processed_headers = make_headers headers
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute
|
52
|
+
execute_inner
|
53
|
+
rescue Redirect => e
|
54
|
+
@url = e.url
|
55
|
+
@method = :get
|
56
|
+
@payload = nil
|
57
|
+
execute
|
58
|
+
end
|
59
|
+
|
60
|
+
def execute_inner
|
61
|
+
uri = parse_url_with_auth(url)
|
62
|
+
transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload
|
63
|
+
end
|
64
|
+
|
65
|
+
def make_headers user_headers
|
66
|
+
unless @cookies.empty?
|
67
|
+
user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
|
68
|
+
end
|
69
|
+
|
70
|
+
headers = default_headers.merge(user_headers).inject({}) do |final, (key, value)|
|
71
|
+
target_key = key.to_s.gsub(/_/, '-').capitalize
|
72
|
+
if 'CONTENT-TYPE' == target_key.upcase
|
73
|
+
target_value = value.to_s
|
74
|
+
final[target_key] = MIME::Types.type_for_extension target_value
|
75
|
+
elsif 'ACCEPT' == target_key.upcase
|
76
|
+
# Accept can be composed of several comma-separated values
|
77
|
+
if value.is_a? Array
|
78
|
+
target_values = value
|
79
|
+
else
|
80
|
+
target_values = value.to_s.split ','
|
81
|
+
end
|
82
|
+
final[target_key] = target_values.map{ |ext| MIME::Types.type_for_extension(ext.to_s.strip)}.join(', ')
|
83
|
+
else
|
84
|
+
final[target_key] = value.to_s
|
85
|
+
end
|
86
|
+
final
|
87
|
+
end
|
88
|
+
|
89
|
+
headers.merge!(@payload.headers) if @payload
|
90
|
+
headers
|
91
|
+
end
|
92
|
+
|
93
|
+
def net_http_class
|
94
|
+
if RestClient.proxy
|
95
|
+
proxy_uri = URI.parse(RestClient.proxy)
|
96
|
+
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
|
97
|
+
else
|
98
|
+
Net::HTTP
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def net_http_request_class(method)
|
103
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_url(url)
|
107
|
+
url = "http://#{url}" unless url.match(/^http/)
|
108
|
+
URI.parse(url)
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse_url_with_auth(url)
|
112
|
+
uri = parse_url(url)
|
113
|
+
@user = uri.user if uri.user
|
114
|
+
@password = uri.password if uri.password
|
115
|
+
uri
|
116
|
+
end
|
117
|
+
|
118
|
+
def process_payload(p=nil, parent_key=nil)
|
119
|
+
unless p.is_a?(Hash)
|
120
|
+
p
|
121
|
+
else
|
122
|
+
@headers[:content_type] ||= 'application/x-www-form-urlencoded'
|
123
|
+
p.keys.map do |k|
|
124
|
+
key = parent_key ? "#{parent_key}[#{k}]" : k
|
125
|
+
if p[k].is_a? Hash
|
126
|
+
process_payload(p[k], key)
|
127
|
+
else
|
128
|
+
value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
129
|
+
"#{key}=#{value}"
|
130
|
+
end
|
131
|
+
end.join("&")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def transmit(uri, req, payload)
|
136
|
+
setup_credentials(req)
|
137
|
+
|
138
|
+
net = net_http_class.new(uri.host, uri.port)
|
139
|
+
net.use_ssl = uri.is_a?(URI::HTTPS)
|
140
|
+
if @verify_ssl == false
|
141
|
+
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
142
|
+
elsif @verify_ssl.is_a? Integer
|
143
|
+
net.verify_mode = @verify_ssl
|
144
|
+
end
|
145
|
+
net.cert = @ssl_client_cert if @ssl_client_cert
|
146
|
+
net.key = @ssl_client_key if @ssl_client_key
|
147
|
+
net.ca_file = @ssl_ca_file if @ssl_ca_file
|
148
|
+
net.read_timeout = @timeout if @timeout
|
149
|
+
net.open_timeout = @open_timeout if @open_timeout
|
150
|
+
|
151
|
+
display_log request_log
|
152
|
+
|
153
|
+
net.start do |http|
|
154
|
+
res = http.request(req, payload) { |http_response| fetch_body(http_response) }
|
155
|
+
result = process_result(res)
|
156
|
+
display_log response_log(res)
|
157
|
+
|
158
|
+
if result.kind_of?(String) or @method == :head
|
159
|
+
Response.new(result, res)
|
160
|
+
elsif @raw_response
|
161
|
+
RawResponse.new(@tf, res)
|
162
|
+
else
|
163
|
+
Response.new(nil, res)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
rescue EOFError
|
167
|
+
raise RestClient::ServerBrokeConnection
|
168
|
+
rescue Timeout::Error
|
169
|
+
raise RestClient::RequestTimeout
|
170
|
+
end
|
171
|
+
|
172
|
+
def setup_credentials(req)
|
173
|
+
req.basic_auth(user, password) if user
|
174
|
+
end
|
175
|
+
|
176
|
+
def fetch_body(http_response)
|
177
|
+
if @raw_response
|
178
|
+
# Taken from Chef, which as in turn...
|
179
|
+
# Stolen from http://www.ruby-forum.com/topic/166423
|
180
|
+
# Kudos to _why!
|
181
|
+
@tf = Tempfile.new("rest-client")
|
182
|
+
size, total = 0, http_response.header['Content-Length'].to_i
|
183
|
+
http_response.read_body do |chunk|
|
184
|
+
@tf.write(chunk)
|
185
|
+
size += chunk.size
|
186
|
+
if size == 0
|
187
|
+
display_log("#{@method} #{@url} done (0 length file)")
|
188
|
+
elsif total == 0
|
189
|
+
display_log("#{@method} #{@url} (zero content length)")
|
190
|
+
else
|
191
|
+
display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total])
|
192
|
+
end
|
193
|
+
end
|
194
|
+
@tf.close
|
195
|
+
@tf
|
196
|
+
else
|
197
|
+
http_response.read_body
|
198
|
+
end
|
199
|
+
http_response
|
200
|
+
end
|
201
|
+
|
202
|
+
def process_result(res)
|
203
|
+
if res.code =~ /\A2\d{2}\z/
|
204
|
+
# We don't decode raw requests
|
205
|
+
unless @raw_response
|
206
|
+
self.class.decode res['content-encoding'], res.body if res.body
|
207
|
+
end
|
208
|
+
elsif %w(301 302 303).include? res.code
|
209
|
+
url = res.header['Location']
|
210
|
+
|
211
|
+
if url !~ /^http/
|
212
|
+
uri = URI.parse(@url)
|
213
|
+
uri.path = "/#{url}".squeeze('/')
|
214
|
+
url = uri.to_s
|
215
|
+
end
|
216
|
+
|
217
|
+
raise Redirect, url
|
218
|
+
elsif res.code == "304"
|
219
|
+
raise NotModified, res
|
220
|
+
elsif res.code == "401"
|
221
|
+
raise Unauthorized, res
|
222
|
+
elsif res.code == "404"
|
223
|
+
raise ResourceNotFound, res
|
224
|
+
else
|
225
|
+
raise RequestFailed, res
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def self.decode(content_encoding, body)
|
230
|
+
if content_encoding == 'gzip' and not body.empty?
|
231
|
+
Zlib::GzipReader.new(StringIO.new(body)).read
|
232
|
+
elsif content_encoding == 'deflate'
|
233
|
+
Zlib::Inflate.new.inflate(body)
|
234
|
+
else
|
235
|
+
body
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def request_log
|
240
|
+
if RestClient.log
|
241
|
+
out = []
|
242
|
+
out << "RestClient.#{method} #{url.inspect}"
|
243
|
+
out << "headers: #{processed_headers.inspect}"
|
244
|
+
out << "paylod: #{payload.short_inspect}" if payload
|
245
|
+
out.join(', ')
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
def response_log(res)
|
250
|
+
size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size)
|
251
|
+
"# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes"
|
252
|
+
end
|
253
|
+
|
254
|
+
def display_log(msg)
|
255
|
+
return unless log_to = RestClient.log
|
256
|
+
|
257
|
+
if log_to == 'stdout'
|
258
|
+
STDOUT.puts msg
|
259
|
+
elsif log_to == 'stderr'
|
260
|
+
STDERR.puts msg
|
261
|
+
else
|
262
|
+
File.open(log_to, 'a') { |f| f.puts msg }
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
def default_headers
|
267
|
+
{ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' }
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
module MIME
|
273
|
+
class Types
|
274
|
+
|
275
|
+
# Return the first found content-type for a value considered as an extension or the value itself
|
276
|
+
def type_for_extension ext
|
277
|
+
candidates = @extension_index[ext]
|
278
|
+
candidates.empty? ? ext : candidates[0].content_type
|
279
|
+
end
|
280
|
+
|
281
|
+
class << self
|
282
|
+
def type_for_extension ext
|
283
|
+
@__types__.type_for_extension ext
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
252
287
|
end
|