esod-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/EXAMPLES +140 -0
- data/GEM_RELEASE +19 -0
- data/README +10 -0
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/esod-client.gemspec +118 -0
- data/esod-client.rb +35 -0
- data/lib/activesupport-2.2.2/CHANGELOG +1257 -0
- data/lib/activesupport-2.2.2/README +43 -0
- data/lib/activesupport-2.2.2/README.CFT +2 -0
- data/lib/activesupport-2.2.2/lib/active_support.rb +26 -0
- data/lib/activesupport-2.2.2/lib/active_support/base64.rb +33 -0
- data/lib/activesupport-2.2.2/lib/active_support/basic_object.rb +24 -0
- data/lib/activesupport-2.2.2/lib/active_support/buffered_logger.rb +122 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache.rb +223 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/compressed_mem_cache_store.rb +20 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/drb_store.rb +15 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/file_store.rb +72 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/mem_cache_store.rb +127 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/memory_store.rb +52 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/synchronized_memory_store.rb +47 -0
- data/lib/activesupport-2.2.2/lib/active_support/callbacks.rb +280 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext.rb +6 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash.rb +14 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/conversions.rb +259 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/deep_merge.rb +23 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/diff.rb +19 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/except.rb +25 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/indifferent_access.rb +137 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/keys.rb +52 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/reverse_merge.rb +35 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/slice.rb +33 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor.rb +14 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder.rb +13 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xchar.rb +115 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb +139 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +63 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlmarkup.rb +328 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +1021 -0
- data/lib/activesupport-2.2.2/lib/activesupport.rb +1 -0
- data/lib/esodclient/esodclient.rb +22 -0
- data/lib/hash.rb +22 -0
- data/lib/mime-types-1.16/History.txt +107 -0
- data/lib/mime-types-1.16/Install.txt +17 -0
- data/lib/mime-types-1.16/Licence.txt +15 -0
- data/lib/mime-types-1.16/Manifest.txt +12 -0
- data/lib/mime-types-1.16/README.txt +28 -0
- data/lib/mime-types-1.16/Rakefile +316 -0
- data/lib/mime-types-1.16/lib/mime/types.rb +751 -0
- data/lib/mime-types-1.16/lib/mime/types.rb.data +1324 -0
- data/lib/mime-types-1.16/mime-types.gemspec +43 -0
- data/lib/mime-types-1.16/setup.rb +1585 -0
- data/lib/mime-types-1.16/test/test_mime_type.rb +356 -0
- data/lib/mime-types-1.16/test/test_mime_types.rb +122 -0
- data/lib/rest-client-1.2.0/README.rdoc +102 -0
- data/lib/rest-client-1.2.0/Rakefile +57 -0
- data/lib/rest-client-1.2.0/VERSION +1 -0
- data/lib/rest-client-1.2.0/bin/restclient +87 -0
- data/lib/rest-client-1.2.0/lib/rest_client.rb +2 -0
- data/lib/rest-client-1.2.0/lib/restclient.rb +108 -0
- data/lib/rest-client-1.2.0/lib/restclient/exceptions.rb +89 -0
- data/lib/rest-client-1.2.0/lib/restclient/mixin/response.rb +48 -0
- data/lib/rest-client-1.2.0/lib/restclient/net_http_ext.rb +21 -0
- data/lib/rest-client-1.2.0/lib/restclient/payload.rb +178 -0
- data/lib/rest-client-1.2.0/lib/restclient/raw_response.rb +30 -0
- data/lib/rest-client-1.2.0/lib/restclient/request.rb +287 -0
- data/lib/rest-client-1.2.0/lib/restclient/resource.rb +146 -0
- data/lib/rest-client-1.2.0/lib/restclient/response.rb +20 -0
- data/lib/rest-client-1.2.0/spec/base.rb +10 -0
- data/lib/rest-client-1.2.0/spec/exceptions_spec.rb +65 -0
- data/lib/rest-client-1.2.0/spec/master_shake.jpg +0 -0
- data/lib/rest-client-1.2.0/spec/mixin/response_spec.rb +46 -0
- data/lib/rest-client-1.2.0/spec/payload_spec.rb +131 -0
- data/lib/rest-client-1.2.0/spec/raw_response_spec.rb +17 -0
- data/lib/rest-client-1.2.0/spec/request_spec.rb +521 -0
- data/lib/rest-client-1.2.0/spec/resource_spec.rb +75 -0
- data/lib/rest-client-1.2.0/spec/response_spec.rb +21 -0
- data/lib/rest-client-1.2.0/spec/restclient_spec.rb +53 -0
- data/lib/trollop/trollop.rb +735 -0
- metadata +137 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
module RestClient
|
2
|
+
module Mixin
|
3
|
+
module Response
|
4
|
+
attr_reader :net_http_res
|
5
|
+
|
6
|
+
# HTTP status code, always 200 since RestClient throws exceptions for
|
7
|
+
# other codes.
|
8
|
+
def code
|
9
|
+
@code ||= @net_http_res.code.to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
# A hash of the headers, beautified with symbols and underscores.
|
13
|
+
# e.g. "Content-type" will become :content_type.
|
14
|
+
def headers
|
15
|
+
@headers ||= self.class.beautify_headers(@net_http_res.to_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
# The raw headers.
|
19
|
+
def raw_headers
|
20
|
+
@raw_headers ||= @net_http_res.to_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
# Hash of cookies extracted from response headers
|
24
|
+
def cookies
|
25
|
+
@cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
|
26
|
+
key, val = raw_c.split('=')
|
27
|
+
unless %w(expires domain path secure).member?(key)
|
28
|
+
out[key] = val
|
29
|
+
end
|
30
|
+
out
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.included(receiver)
|
35
|
+
receiver.extend(RestClient::Mixin::Response::ClassMethods)
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def beautify_headers(headers)
|
40
|
+
headers.inject({}) do |out, (key, value)|
|
41
|
+
out[key.gsub(/-/, '_').downcase.to_sym] = value.first
|
42
|
+
out
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#
|
2
|
+
# Replace the request method in Net::HTTP to sniff the body type
|
3
|
+
# and set the stream if appropriate
|
4
|
+
#
|
5
|
+
# Taken from:
|
6
|
+
# http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/
|
7
|
+
|
8
|
+
module Net
|
9
|
+
class HTTP
|
10
|
+
alias __request__ request
|
11
|
+
|
12
|
+
def request(req, body=nil, &block)
|
13
|
+
if body != nil && body.respond_to?(:read)
|
14
|
+
req.body_stream = body
|
15
|
+
return __request__(req, nil, &block)
|
16
|
+
else
|
17
|
+
return __request__(req, body, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'stringio'
|
3
|
+
require 'mime/types'
|
4
|
+
|
5
|
+
module RestClient
|
6
|
+
module Payload
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def generate(params)
|
10
|
+
if params.is_a?(String)
|
11
|
+
Base.new(params)
|
12
|
+
elsif params
|
13
|
+
if params.delete(:multipart) == true || has_file?(params)
|
14
|
+
Multipart.new(params)
|
15
|
+
else
|
16
|
+
UrlEncoded.new(params)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_file?(params)
|
24
|
+
params.any? do |_, v|
|
25
|
+
case v
|
26
|
+
when Hash
|
27
|
+
has_file?(v)
|
28
|
+
else
|
29
|
+
v.respond_to?(:path) && v.respond_to?(:read)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Base
|
35
|
+
def initialize(params)
|
36
|
+
build_stream(params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def build_stream(params)
|
40
|
+
@stream = StringIO.new(params)
|
41
|
+
@stream.seek(0)
|
42
|
+
end
|
43
|
+
|
44
|
+
def read(bytes=nil)
|
45
|
+
@stream.read(bytes)
|
46
|
+
end
|
47
|
+
|
48
|
+
alias :to_s :read
|
49
|
+
|
50
|
+
def escape(v)
|
51
|
+
URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Flatten parameters by converting hashes of hashes to flat hashes
|
55
|
+
# {keys1 => {keys2 => value}} will be transformed into {keys1[key2] => value}
|
56
|
+
def flatten_params(params, parent_key = nil)
|
57
|
+
result = {}
|
58
|
+
params.keys.map do |key|
|
59
|
+
calculated_key = parent_key ? "#{parent_key}[#{escape key}]" : escape(key)
|
60
|
+
value = params[key]
|
61
|
+
if value.is_a? Hash
|
62
|
+
result.merge!(flatten_params(value, calculated_key))
|
63
|
+
else
|
64
|
+
result[calculated_key] = value
|
65
|
+
end
|
66
|
+
end
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
70
|
+
def headers
|
71
|
+
{ 'Content-Length' => size.to_s }
|
72
|
+
end
|
73
|
+
|
74
|
+
def size
|
75
|
+
@stream.size
|
76
|
+
end
|
77
|
+
|
78
|
+
alias :length :size
|
79
|
+
|
80
|
+
def close
|
81
|
+
@stream.close
|
82
|
+
end
|
83
|
+
|
84
|
+
def inspect
|
85
|
+
result = to_s.inspect
|
86
|
+
@stream.seek(0)
|
87
|
+
result
|
88
|
+
end
|
89
|
+
|
90
|
+
def short_inspect
|
91
|
+
(size > 100 ? "#{size} byte length" : inspect)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
class UrlEncoded < Base
|
97
|
+
def build_stream(params = nil)
|
98
|
+
@stream = StringIO.new(flatten_params(params).map do |k, v|
|
99
|
+
"#{k}=#{escape(v)}"
|
100
|
+
end.join("&"))
|
101
|
+
@stream.seek(0)
|
102
|
+
end
|
103
|
+
|
104
|
+
def headers
|
105
|
+
super.merge({ 'Content-Type' => 'application/x-www-form-urlencoded' })
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
class Multipart < Base
|
110
|
+
EOL = "\r\n"
|
111
|
+
|
112
|
+
def build_stream(params)
|
113
|
+
b = "--#{boundary}"
|
114
|
+
|
115
|
+
@stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
|
116
|
+
@stream.write(b + EOL)
|
117
|
+
|
118
|
+
if params.is_a? Hash
|
119
|
+
x = flatten_params(params).to_a
|
120
|
+
else
|
121
|
+
x = params.to_a
|
122
|
+
end
|
123
|
+
|
124
|
+
last_index = x.length - 1
|
125
|
+
x.each_with_index do |a, index|
|
126
|
+
k, v = *a
|
127
|
+
if v.respond_to?(:read) && v.respond_to?(:path)
|
128
|
+
create_file_field(@stream, k, v)
|
129
|
+
else
|
130
|
+
create_regular_field(@stream, k, v)
|
131
|
+
end
|
132
|
+
@stream.write(EOL + b)
|
133
|
+
@stream.write(EOL) unless last_index == index
|
134
|
+
end
|
135
|
+
@stream.write('--')
|
136
|
+
@stream.write(EOL)
|
137
|
+
@stream.seek(0)
|
138
|
+
end
|
139
|
+
|
140
|
+
def create_regular_field(s, k, v)
|
141
|
+
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"")
|
142
|
+
s.write(EOL)
|
143
|
+
s.write(EOL)
|
144
|
+
s.write(v)
|
145
|
+
end
|
146
|
+
|
147
|
+
def create_file_field(s, k, v)
|
148
|
+
begin
|
149
|
+
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.respond_to?(:original_filename) ? v.original_filename : File.basename(v.path)}\"#{EOL}")
|
150
|
+
s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{EOL}")
|
151
|
+
s.write(EOL)
|
152
|
+
while data = v.read(8124)
|
153
|
+
s.write(data)
|
154
|
+
end
|
155
|
+
ensure
|
156
|
+
v.close
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def mime_for(path)
|
161
|
+
mime = MIME::Types.type_for path
|
162
|
+
mime.empty? ? 'text/plain' : mime[0].content_type
|
163
|
+
end
|
164
|
+
|
165
|
+
def boundary
|
166
|
+
@boundary ||= rand(1_000_000).to_s
|
167
|
+
end
|
168
|
+
|
169
|
+
def headers
|
170
|
+
super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}})
|
171
|
+
end
|
172
|
+
|
173
|
+
def close
|
174
|
+
@stream.close
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/mixin/response'
|
2
|
+
|
3
|
+
module RestClient
|
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
|
+
|
17
|
+
attr_reader :file
|
18
|
+
|
19
|
+
def initialize(tempfile, net_http_res)
|
20
|
+
@net_http_res = net_http_res
|
21
|
+
@file = tempfile
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
@file.open
|
26
|
+
@file.read
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,287 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'mime/types'
|
3
|
+
|
4
|
+
module RestClient
|
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
|
287
|
+
end
|