Class: RestClient::Request
- Inherits:
-
Object
- Object
- RestClient::Request
- Defined in:
- lib/restclient/request.rb,
lib/restclient/exceptions.rb
Overview
backwards compatibility
Constant Summary collapse
- EOL =
"\r\n"- Redirect =
RestClient::Redirect
RestClient::Unauthorized
- RequestFailed =
RestClient::RequestFailed
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#ssl_ca_file ⇒ Object
readonly
Returns the value of attribute ssl_ca_file.
-
#ssl_client_cert ⇒ Object
readonly
Returns the value of attribute ssl_client_cert.
-
#ssl_client_key ⇒ Object
readonly
Returns the value of attribute ssl_client_key.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Class Method Summary collapse
Instance Method Summary collapse
- #boundary ⇒ Object
- #build_multipart(params) ⇒ Object
- #create_file_field(s, k, v, content_type = nil) ⇒ Object
- #create_regular_field(s, k, v) ⇒ Object
- #decode(content_encoding, body) ⇒ Object
- #default_headers ⇒ Object
- #display_log(msg) ⇒ Object
- #execute ⇒ Object
- #execute_inner ⇒ Object
- #fetch_body(http_response) ⇒ Object
-
#initialize(args) ⇒ Request
constructor
A new instance of Request.
- #make_headers(user_headers) ⇒ Object
- #net_http_class ⇒ Object
- #net_http_request_class(method) ⇒ Object
- #parse_url(url) ⇒ Object
- #parse_url_with_auth(url) ⇒ Object
- #process_payload(p = nil, parent_key = nil) ⇒ Object
- #process_result(res) ⇒ Object
- #request_log ⇒ Object
- #response_log(res) ⇒ Object
- #setup_credentials(req) ⇒ Object
- #transmit(uri, req, payload) ⇒ Object
Constructor Details
#initialize(args) ⇒ Request
Returns a new instance of Request.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/restclient/request.rb', line 21 def initialize(args) @method = args[:method] or raise ArgumentError, "must pass :method" @url = args[:url] or raise ArgumentError, "must pass :url" @headers = args[:headers] || {} @cookies = @headers.delete(:cookies) || args[:cookies] || {} @payload = process_payload(args[:payload]) @user = args[:user] @password = args[:password] @timeout = args[:timeout] @open_timeout = args[:open_timeout] @raw_response = args[:raw_response] || false @verify_ssl = args[:verify_ssl] || false @ssl_client_cert = args[:ssl_client_cert] || nil @ssl_client_key = args[:ssl_client_key] || nil @ssl_ca_file = args[:ssl_ca_file] || nil @tf = nil # If you are a raw request, this is your tempfile end |
Instance Attribute Details
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def @cookies end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def method @method end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def open_timeout @open_timeout end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def password @password end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def payload @payload end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def raw_response @raw_response end |
#ssl_ca_file ⇒ Object (readonly)
Returns the value of attribute ssl_ca_file.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def ssl_ca_file @ssl_ca_file end |
#ssl_client_cert ⇒ Object (readonly)
Returns the value of attribute ssl_client_cert.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def ssl_client_cert @ssl_client_cert end |
#ssl_client_key ⇒ Object (readonly)
Returns the value of attribute ssl_client_key.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def ssl_client_key @ssl_client_key end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def timeout @timeout end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def user @user end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
12 13 14 |
# File 'lib/restclient/request.rb', line 12 def verify_ssl @verify_ssl end |
Class Method Details
.execute(args) ⇒ Object
17 18 19 |
# File 'lib/restclient/request.rb', line 17 def self.execute(args) new(args).execute end |
Instance Method Details
#boundary ⇒ Object
150 151 152 |
# File 'lib/restclient/request.rb', line 150 def boundary @boundary ||= rand(1_000_000).to_s end |
#build_multipart(params) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/restclient/request.rb', line 110 def build_multipart(params) b = "--#{boundary}" @stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}") @stream.write(b) params.each do |k,v| @stream.write(EOL) if v.respond_to?(:read) && v.respond_to?(:path) create_file_field(@stream, k,v) else create_regular_field(@stream, k,v) end @stream.write(EOL + b) end @stream.write('--') @stream.write(EOL) @stream.seek(0) end |
#create_file_field(s, k, v, content_type = nil) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/restclient/request.rb', line 136 def create_file_field(s, k, v, content_type = nil) content_type ||= `file -b --mime #{v.path}`.gsub(/\n/, '') begin s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}") s.write("Content-Type: #{content_type}#{EOL}") s.write(EOL) while data = v.read(8124) s.write(data) end ensure v.close end end |
#create_regular_field(s, k, v) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/restclient/request.rb', line 129 def create_regular_field(s, k, v) s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"") s.write(EOL) s.write(EOL) s.write(v) end |
#decode(content_encoding, body) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'lib/restclient/request.rb', line 250 def decode(content_encoding, body) if content_encoding == 'gzip' and not body.empty? Zlib::GzipReader.new(StringIO.new(body)).read elsif content_encoding == 'deflate' Zlib::Inflate.new.inflate(body) else body end end |
#default_headers ⇒ Object
285 286 287 |
# File 'lib/restclient/request.rb', line 285 def default_headers { :accept => 'application/xml', :accept_encoding => 'gzip, deflate' } end |
#display_log(msg) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/restclient/request.rb', line 273 def display_log(msg) return unless log_to = RestClient.log if log_to == 'stdout' STDOUT.puts msg elsif log_to == 'stderr' STDERR.puts msg else File.open(log_to, 'a') { |f| f.puts msg } end end |
#execute ⇒ Object
39 40 41 42 43 44 |
# File 'lib/restclient/request.rb', line 39 def execute execute_inner rescue Redirect => e @url = e.url execute end |
#execute_inner ⇒ Object
46 47 48 49 |
# File 'lib/restclient/request.rb', line 46 def execute_inner uri = parse_url_with_auth(url) transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload end |
#fetch_body(http_response) ⇒ Object
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 |
# File 'lib/restclient/request.rb', line 197 def fetch_body(http_response) if @raw_response # Taken from Chef, which as in turn... # Stolen from http://www.ruby-forum.com/topic/166423 # Kudos to _why! @tf = Tempfile.new("restclient") size, total = 0, http_response.header['Content-Length'].to_i http_response.read_body do |chunk| @tf.write(chunk) size += chunk.size if size == 0 display_log("#{@method} #{@url} done (0 length file)") elsif total == 0 display_log("#{@method} #{@url} (zero content length)") else display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total]) end end @tf.close @tf else http_response.read_body end http_response end |
#make_headers(user_headers) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/restclient/request.rb', line 51 def make_headers(user_headers) unless @cookies.empty? user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ') end default_headers.merge(user_headers).inject({}) do |final, (key, value)| final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s final end end |
#net_http_class ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/restclient/request.rb', line 62 def net_http_class if RestClient.proxy proxy_uri = URI.parse(RestClient.proxy) Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) else Net::HTTP end end |
#net_http_request_class(method) ⇒ Object
71 72 73 |
# File 'lib/restclient/request.rb', line 71 def net_http_request_class(method) Net::HTTP.const_get(method.to_s.capitalize) end |
#parse_url(url) ⇒ Object
75 76 77 78 |
# File 'lib/restclient/request.rb', line 75 def parse_url(url) url = "http://#{url}" unless url.match(/^http/) URI.parse(url) end |
#parse_url_with_auth(url) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/restclient/request.rb', line 80 def parse_url_with_auth(url) uri = parse_url(url) @user = uri.user if uri.user @password = uri.password if uri.password uri end |
#process_payload(p = nil, parent_key = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/restclient/request.rb', line 87 def process_payload(p=nil, parent_key=nil) unless p.is_a?(Hash) p else if p.delete(:multipart) == true multipart = Multipart.new(p) @headers[:content_type] = %Q{multipart/form-data; boundary="#{multipart.boundary}"} return multipart else @headers[:content_type] ||= 'application/x-www-form-urlencoded' p.keys.map do |k| key = parent_key ? "#{parent_key}[#{k}]" : k if p[k].is_a? Hash process_payload(p[k], key) else value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) "#{key}=#{value}" end end.join("&") end end end |
#process_result(res) ⇒ Object
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 |
# File 'lib/restclient/request.rb', line 223 def process_result(res) if res.code =~ /\A2\d{2}\z/ # We don't decode raw requests unless @raw_response decode res['content-encoding'], res.body if res.body end elsif %w(301 302 303).include? res.code url = res.header['Location'] if url !~ /^http/ uri = URI.parse(@url) uri.path = "/#{url}".squeeze('/') url = uri.to_s end raise Redirect, url elsif res.code == "304" raise NotModified, res elsif res.code == "401" raise Unauthorized, res elsif res.code == "404" raise ResourceNotFound, res else raise RequestFailed, res end end |
#request_log ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/restclient/request.rb', line 260 def request_log out = [] out << "RestClient.#{method} #{url.inspect}" out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty? out.join(', ') end |
#response_log(res) ⇒ Object
268 269 270 271 |
# File 'lib/restclient/request.rb', line 268 def response_log(res) size = @raw_response ? File.size(@tf.path) : res.body.size "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes" end |
#setup_credentials(req) ⇒ Object
193 194 195 |
# File 'lib/restclient/request.rb', line 193 def setup_credentials(req) req.basic_auth(user, password) if user end |
#transmit(uri, req, payload) ⇒ Object
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 |
# File 'lib/restclient/request.rb', line 154 def transmit(uri, req, payload) setup_credentials(req) net = net_http_class.new(uri.host, uri.port) net.use_ssl = uri.is_a?(URI::HTTPS) if @verify_ssl == false net.verify_mode = OpenSSL::SSL::VERIFY_NONE elsif @verify_ssl.is_a? Integer net.verify_mode = @verify_ssl end net.cert = @ssl_client_cert if @ssl_client_cert net.key = @ssl_client_key if @ssl_client_key net.ca_file = @ssl_ca_file if @ssl_ca_file net.read_timeout = @timeout if @timeout net.open_timeout = @open_timeout if @open_timeout display_log request_log net.start do |http| res = http.request(req, payload) { |http_response| fetch_body(http_response) } result = process_result(res) display_log response_log(res) if result.kind_of?(String) or @method == :head Response.new(result, res) elsif @raw_response RawResponse.new(@tf, res) else nil end end rescue EOFError raise RestClient::ServerBrokeConnection rescue Timeout::Error raise RestClient::RequestTimeout ensure @payload.close end |