larsburgess-rest-client 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ require 'cgi'
2
+
3
+ module RestClient
4
+
5
+ module AbstractResponse
6
+
7
+ attr_reader :net_http_res, :args
8
+
9
+ # HTTP status code
10
+ def code
11
+ @code ||= @net_http_res.code.to_i
12
+ end
13
+
14
+ # A hash of the headers, beautified with symbols and underscores.
15
+ # e.g. "Content-type" will become :content_type.
16
+ def headers
17
+ @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
18
+ end
19
+
20
+ # The raw headers.
21
+ def raw_headers
22
+ @raw_headers ||= @net_http_res.to_hash
23
+ end
24
+
25
+ # Hash of cookies extracted from response headers
26
+ def cookies
27
+ @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
28
+ out.merge parse_cookie(cookie_content)
29
+ end
30
+ end
31
+
32
+ # Return the default behavior corresponding to the response code:
33
+ # the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
34
+ def return! request = nil, result = nil, & block
35
+ if (200..207).include? code
36
+ self
37
+ elsif [301, 302, 307].include? code
38
+ unless [:get, :head].include? args[:method]
39
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
40
+ else
41
+ follow_redirection(request, result, & block)
42
+ end
43
+ elsif code == 303
44
+ args[:method] = :get
45
+ args.delete :payload
46
+ follow_redirection(request, result, & block)
47
+ elsif Exceptions::EXCEPTIONS_MAP[code]
48
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
49
+ else
50
+ raise RequestFailed, self
51
+ end
52
+ end
53
+
54
+ def to_i
55
+ code
56
+ end
57
+
58
+ def description
59
+ "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
60
+ end
61
+
62
+ # Follow a redirection
63
+ def follow_redirection request = nil, result = nil, & block
64
+ url = headers[:location]
65
+ if url !~ /^http/
66
+ url = URI.parse(args[:url]).merge(url).to_s
67
+ end
68
+ args[:url] = url
69
+ if request
70
+ args[:password] = request.password
71
+ args[:user] = request.user
72
+ args[:headers] = request.headers
73
+ # pass any cookie set in the result
74
+ if result && result['set-cookie']
75
+ args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge(parse_cookie(result['set-cookie']))
76
+ end
77
+ end
78
+ Request.execute args, &block
79
+ end
80
+
81
+ def AbstractResponse.beautify_headers(headers)
82
+ headers.inject({}) do |out, (key, value)|
83
+ out[key.gsub(/-/, '_').downcase.to_sym] = %w{ set-cookie }.include?(key.downcase) ? value : value.first
84
+ out
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ # Parse a cookie value and return its content in an Hash
91
+ def parse_cookie cookie_content
92
+ out = {}
93
+ CGI::Cookie::parse(cookie_content).each do |key, cookie|
94
+ unless ['expires', 'path'].include? key
95
+ out[CGI::escape(key)] = cookie.value[0] ? (CGI::escape(cookie.value[0]) || '') : ''
96
+ end
97
+ end
98
+ out
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,189 @@
1
+ module RestClient
2
+
3
+ STATUSES = {100 => 'Continue',
4
+ 101 => 'Switching Protocols',
5
+ 102 => 'Processing', #WebDAV
6
+
7
+ 200 => 'OK',
8
+ 201 => 'Created',
9
+ 202 => 'Accepted',
10
+ 203 => 'Non-Authoritative Information', # http/1.1
11
+ 204 => 'No Content',
12
+ 205 => 'Reset Content',
13
+ 206 => 'Partial Content',
14
+ 207 => 'Multi-Status', #WebDAV
15
+
16
+ 300 => 'Multiple Choices',
17
+ 301 => 'Moved Permanently',
18
+ 302 => 'Found',
19
+ 303 => 'See Other', # http/1.1
20
+ 304 => 'Not Modified',
21
+ 305 => 'Use Proxy', # http/1.1
22
+ 306 => 'Switch Proxy', # no longer used
23
+ 307 => 'Temporary Redirect', # http/1.1
24
+
25
+ 400 => 'Bad Request',
26
+ 401 => 'Unauthorized',
27
+ 402 => 'Payment Required',
28
+ 403 => 'Forbidden',
29
+ 404 => 'Resource Not Found',
30
+ 405 => 'Method Not Allowed',
31
+ 406 => 'Not Acceptable',
32
+ 407 => 'Proxy Authentication Required',
33
+ 408 => 'Request Timeout',
34
+ 409 => 'Conflict',
35
+ 410 => 'Gone',
36
+ 411 => 'Length Required',
37
+ 412 => 'Precondition Failed',
38
+ 413 => 'Request Entity Too Large',
39
+ 414 => 'Request-URI Too Long',
40
+ 415 => 'Unsupported Media Type',
41
+ 416 => 'Requested Range Not Satisfiable',
42
+ 417 => 'Expectation Failed',
43
+ 418 => 'I\'m A Teapot',
44
+ 421 => 'Too Many Connections From This IP',
45
+ 422 => 'Unprocessable Entity', #WebDAV
46
+ 423 => 'Locked', #WebDAV
47
+ 424 => 'Failed Dependency', #WebDAV
48
+ 425 => 'Unordered Collection', #WebDAV
49
+ 426 => 'Upgrade Required',
50
+ 449 => 'Retry With', #Microsoft
51
+ 450 => 'Blocked By Windows Parental Controls', #Microsoft
52
+
53
+ 500 => 'Internal Server Error',
54
+ 501 => 'Not Implemented',
55
+ 502 => 'Bad Gateway',
56
+ 503 => 'Service Unavailable',
57
+ 504 => 'Gateway Timeout',
58
+ 505 => 'HTTP Version Not Supported',
59
+ 506 => 'Variant Also Negotiates',
60
+ 507 => 'Insufficient Storage', #WebDAV
61
+ 509 => 'Bandwidth Limit Exceeded', #Apache
62
+ 510 => 'Not Extended'}
63
+
64
+ # Compatibility : make the Response act like a Net::HTTPResponse when needed
65
+ module ResponseForException
66
+ def method_missing symbol, *args
67
+ if net_http_res.respond_to? symbol
68
+ warn "[warning] The response contained in an RestClient::Exception is now a RestClient::Response instead of a Net::HTTPResponse, please update your code"
69
+ net_http_res.send symbol, *args
70
+ else
71
+ super
72
+ end
73
+ end
74
+ end
75
+
76
+ # This is the base RestClient exception class. Rescue it if you want to
77
+ # catch any exception that your request might raise
78
+ # You can get the status code by e.http_code, or see anything about the
79
+ # response via e.response.
80
+ # For example, the entire result body (which is
81
+ # probably an HTML error page) is e.response.
82
+ class Exception < RuntimeError
83
+ attr_accessor :response
84
+ attr_writer :message
85
+
86
+ def initialize response = nil, initial_response_code = nil
87
+ @response = response
88
+ @initial_response_code = initial_response_code
89
+
90
+ # compatibility: this make the exception behave like a Net::HTTPResponse
91
+ response.extend ResponseForException if response
92
+ end
93
+
94
+ def http_code
95
+ # return integer for compatibility
96
+ if @response
97
+ @response.code.to_i
98
+ else
99
+ @initial_response_code
100
+ end
101
+ end
102
+
103
+ def http_body
104
+ @response.body if @response
105
+ end
106
+
107
+ def inspect
108
+ "#{message}: #{http_body}"
109
+ end
110
+
111
+ def to_s
112
+ inspect
113
+ end
114
+
115
+ def message
116
+ @message || self.class.name
117
+ end
118
+
119
+ end
120
+
121
+ # Compatibility
122
+ class ExceptionWithResponse < Exception
123
+ end
124
+
125
+ # The request failed with an error code not managed by the code
126
+ class RequestFailed < ExceptionWithResponse
127
+
128
+ def message
129
+ "HTTP status code #{http_code}"
130
+ end
131
+
132
+ def to_s
133
+ message
134
+ end
135
+ end
136
+
137
+ # We will a create an exception for each status code, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
138
+ module Exceptions
139
+ # Map http status codes to the corresponding exception class
140
+ EXCEPTIONS_MAP = {}
141
+ end
142
+
143
+ STATUSES.each_pair do |code, message|
144
+
145
+ # Compatibility
146
+ superclass = ([304, 401, 404].include? code) ? ExceptionWithResponse : RequestFailed
147
+ klass = Class.new(superclass) do
148
+ send(:define_method, :message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
149
+ end
150
+ klass_constant = const_set message.delete(' \-\''), klass
151
+ Exceptions::EXCEPTIONS_MAP[code] = klass_constant
152
+ end
153
+
154
+ # A redirect was encountered; caught by execute to retry with the new url.
155
+ class Redirect < Exception
156
+
157
+ message = 'Redirect'
158
+
159
+ attr_accessor :url
160
+
161
+ def initialize(url)
162
+ @url = url
163
+ end
164
+ end
165
+
166
+ # The server broke the connection prior to the request completing. Usually
167
+ # this means it crashed, or sometimes that your network connection was
168
+ # severed before it could complete.
169
+ class ServerBrokeConnection < Exception
170
+ def initialize(message = 'Server broke connection')
171
+ super nil, nil
172
+ self.message = message
173
+ end
174
+ end
175
+
176
+ class SSLCertificateNotVerified < Exception
177
+ def initialize(message)
178
+ super nil, nil
179
+ self.message = message
180
+ end
181
+ end
182
+ end
183
+
184
+ # backwards compatibility
185
+ class RestClient::Request
186
+ Redirect = RestClient::Redirect
187
+ Unauthorized = RestClient::Unauthorized
188
+ RequestFailed = RestClient::RequestFailed
189
+ 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,220 @@
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.respond_to?(:read)
13
+ Streamed.new(params)
14
+ elsif params
15
+ if params.delete(:multipart) == true || has_file?(params)
16
+ Multipart.new(params)
17
+ else
18
+ UrlEncoded.new(params)
19
+ end
20
+ else
21
+ nil
22
+ end
23
+ end
24
+
25
+ def has_file?(params)
26
+ params.any? do |_, v|
27
+ case v
28
+ when Hash
29
+ has_file?(v)
30
+ else
31
+ v.respond_to?(:path) && v.respond_to?(:read)
32
+ end
33
+ end
34
+ end
35
+
36
+ class Base
37
+ def initialize(params)
38
+ build_stream(params)
39
+ end
40
+
41
+ def build_stream(params)
42
+ @stream = StringIO.new(params)
43
+ @stream.seek(0)
44
+ end
45
+
46
+ def read(bytes=nil)
47
+ @stream.read(bytes)
48
+ end
49
+
50
+ alias :to_s :read
51
+
52
+ # Flatten parameters by converting hashes of hashes to flat hashes
53
+ # {keys1 => {keys2 => value}} will be transformed into [keys1[key2], value]
54
+ def flatten_params(params, parent_key = nil)
55
+ result = []
56
+ params.each do |key, value|
57
+ calculated_key = parent_key ? "#{parent_key}[#{handle_key(key)}]" : handle_key(key)
58
+ if value.is_a? Hash
59
+ result += flatten_params(value, calculated_key)
60
+ elsif value.is_a? Array
61
+ result += flatten_params_array(value, calculated_key)
62
+ else
63
+ result << [calculated_key, value]
64
+ end
65
+ end
66
+ result
67
+ end
68
+
69
+ def flatten_params_array value, calculated_key
70
+ result = []
71
+ value.each do |elem|
72
+ if elem.is_a? Hash
73
+ result += flatten_params(elem, calculated_key)
74
+ elsif elem.is_a? Array
75
+ result += flatten_params_array(elem, calculated_key)
76
+ else
77
+ result << ["#{calculated_key}[]", elem]
78
+ end
79
+ end
80
+ result
81
+ end
82
+
83
+ def headers
84
+ {'Content-Length' => size.to_s}
85
+ end
86
+
87
+ def size
88
+ @stream.size
89
+ end
90
+
91
+ alias :length :size
92
+
93
+ def close
94
+ @stream.close
95
+ end
96
+
97
+ def inspect
98
+ result = to_s.inspect
99
+ @stream.seek(0)
100
+ result
101
+ end
102
+
103
+ def short_inspect
104
+ (size > 500 ? "#{size} byte(s) length" : inspect)
105
+ end
106
+
107
+ end
108
+
109
+ class Streamed < Base
110
+ def build_stream(params = nil)
111
+ @stream = params
112
+ end
113
+
114
+ def size
115
+ if @stream.respond_to?(:size)
116
+ @stream.size
117
+ elsif @stream.is_a?(IO)
118
+ @stream.stat.size
119
+ end
120
+ end
121
+
122
+ alias :length :size
123
+ end
124
+
125
+ class UrlEncoded < Base
126
+ def build_stream(params = nil)
127
+ @stream = StringIO.new(flatten_params(params).collect do |entry|
128
+ "#{entry[0]}=#{handle_key(entry[1])}"
129
+ end.join("&"))
130
+ @stream.seek(0)
131
+ end
132
+
133
+ # for UrlEncoded escape the keys
134
+ def handle_key key
135
+ URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
136
+ end
137
+
138
+ def headers
139
+ super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
140
+ end
141
+ end
142
+
143
+ class Multipart < Base
144
+ EOL = "\r\n"
145
+
146
+ def build_stream(params)
147
+ b = "--#{boundary}"
148
+
149
+ @stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
150
+ @stream.binmode
151
+ @stream.write(b + EOL)
152
+
153
+ if params.is_a? Hash
154
+ x = flatten_params(params)
155
+ else
156
+ x = params
157
+ end
158
+
159
+ last_index = x.length - 1
160
+ x.each_with_index do |a, index|
161
+ k, v = * a
162
+ if v.respond_to?(:read) && v.respond_to?(:path)
163
+ create_file_field(@stream, k, v)
164
+ else
165
+ create_regular_field(@stream, k, v)
166
+ end
167
+ @stream.write(EOL + b)
168
+ @stream.write(EOL) unless last_index == index
169
+ end
170
+ @stream.write('--')
171
+ @stream.write(EOL)
172
+ @stream.seek(0)
173
+ end
174
+
175
+ def create_regular_field(s, k, v)
176
+ s.write("Content-Disposition: form-data; name=\"#{k}\"")
177
+ s.write(EOL)
178
+ s.write(EOL)
179
+ s.write(v)
180
+ end
181
+
182
+ def create_file_field(s, k, v)
183
+ begin
184
+ s.write("Content-Disposition: form-data;")
185
+ s.write(" name=\"#{k}\";") unless (k.nil? || k=='')
186
+ s.write(" filename=\"#{v.respond_to?(:original_filename) ? v.original_filename : File.basename(v.path)}\"#{EOL}")
187
+ s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{EOL}")
188
+ s.write(EOL)
189
+ while data = v.read(8124)
190
+ s.write(data)
191
+ end
192
+ ensure
193
+ v.close
194
+ end
195
+ end
196
+
197
+ def mime_for(path)
198
+ mime = MIME::Types.type_for path
199
+ mime.empty? ? 'text/plain' : mime[0].content_type
200
+ end
201
+
202
+ def boundary
203
+ @boundary ||= rand(1_000_000).to_s
204
+ end
205
+
206
+ # for Multipart do not escape the keys
207
+ def handle_key key
208
+ key
209
+ end
210
+
211
+ def headers
212
+ super.merge({'Content-Type' => %Q{multipart/form-data; boundary=#{boundary}}})
213
+ end
214
+
215
+ def close
216
+ @stream.close
217
+ end
218
+ end
219
+ end
220
+ end