2009-03-16 18:28:20 -04:00
|
|
|
require 'tempfile'
|
2009-12-29 14:30:38 -05:00
|
|
|
require 'mime/types'
|
2009-03-16 18:28:20 -04:00
|
|
|
|
2009-01-24 17:49:01 -05:00
|
|
|
module RestClient
|
2009-12-29 12:27:39 -05:00
|
|
|
# This class is used internally by RestClient to send the request, but you can also
|
|
|
|
# call it directly if you'd like to use a method not supported by the
|
|
|
|
# main API. For example:
|
|
|
|
#
|
|
|
|
# RestClient::Request.execute(:method => :head, :url => 'http://example.com')
|
|
|
|
#
|
|
|
|
# Mandatory parameters:
|
|
|
|
# * :method
|
|
|
|
# * :url
|
|
|
|
# Optional parameters (have a look at ssl and/or uri for some explanations):
|
|
|
|
# * :headers a hash containing the request headers
|
|
|
|
# * :cookies will replace possible cookies in the :headers
|
|
|
|
# * :user and :password for basic auth, will be replaced by a user/password available in the :url
|
|
|
|
# * :raw_response return a low-level RawResponse instead of a Response
|
|
|
|
# * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
|
|
|
|
# * :timeout and :open_timeout
|
|
|
|
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
|
|
|
|
class Request
|
2010-01-18 16:15:15 -05:00
|
|
|
|
2010-01-31 07:31:30 -05:00
|
|
|
attr_reader :method, :url, :headers, :cookies,
|
|
|
|
:payload, :user, :password, :timeout,
|
|
|
|
:open_timeout, :raw_response, :verify_ssl, :ssl_client_cert,
|
|
|
|
:ssl_client_key, :ssl_ca_file, :processed_headers, :args
|
2010-01-18 16:15:15 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.execute(args, &block)
|
|
|
|
new(args).execute &block
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def initialize args
|
2009-12-29 12:27:39 -05:00
|
|
|
@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 = Payload.generate(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
|
2009-12-29 14:30:38 -05:00
|
|
|
@processed_headers = make_headers headers
|
2010-01-31 07:31:30 -05:00
|
|
|
@args = args
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def execute &block
|
2009-12-29 12:27:39 -05:00
|
|
|
uri = parse_url_with_auth(url)
|
2010-01-22 15:04:49 -05:00
|
|
|
transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload, &block
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2009-12-29 14:30:38 -05:00
|
|
|
def make_headers user_headers
|
2009-12-29 12:27:39 -05:00
|
|
|
unless @cookies.empty?
|
2010-04-15 12:23:34 -04:00
|
|
|
user_headers[:cookie] = @cookies.map {|(key, val)| "#{key.to_s}=#{val}" }.sort.join(';')
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2010-03-28 04:58:21 -04:00
|
|
|
headers = stringify_headers(default_headers).merge(stringify_headers(user_headers))
|
2009-12-29 12:27:39 -05:00
|
|
|
headers.merge!(@payload.headers) if @payload
|
|
|
|
headers
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def net_http_request_class(method)
|
|
|
|
Net::HTTP.const_get(method.to_s.capitalize)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_url(url)
|
|
|
|
url = "http://#{url}" unless url.match(/^http/)
|
|
|
|
URI.parse(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_url_with_auth(url)
|
|
|
|
uri = parse_url(url)
|
|
|
|
@user = uri.user if uri.user
|
|
|
|
@password = uri.password if uri.password
|
|
|
|
uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_payload(p=nil, parent_key=nil)
|
|
|
|
unless p.is_a?(Hash)
|
|
|
|
p
|
|
|
|
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
|
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def transmit uri, req, payload, &block
|
|
|
|
setup_credentials req
|
2009-12-29 12:27:39 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-03-05 19:55:56 -05:00
|
|
|
RestClient.before_execution_procs.each do |before_proc|
|
|
|
|
before_proc.call(req, args)
|
2010-01-31 07:31:30 -05:00
|
|
|
end
|
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
log_request
|
2009-12-29 12:27:39 -05:00
|
|
|
|
|
|
|
net.start do |http|
|
|
|
|
res = http.request(req, payload) { |http_response| fetch_body(http_response) }
|
2010-01-21 13:32:01 -05:00
|
|
|
log_response res
|
2010-01-22 15:04:49 -05:00
|
|
|
process_result res, &block
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
raise RestClient::ServerBrokeConnection
|
|
|
|
rescue Timeout::Error
|
|
|
|
raise RestClient::RequestTimeout
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_credentials(req)
|
|
|
|
req.basic_auth(user, password) if user
|
|
|
|
end
|
|
|
|
|
|
|
|
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("rest-client")
|
|
|
|
size, total = 0, http_response.header['Content-Length'].to_i
|
|
|
|
http_response.read_body do |chunk|
|
2010-01-22 15:04:49 -05:00
|
|
|
@tf.write chunk
|
2009-12-29 12:27:39 -05:00
|
|
|
size += chunk.size
|
2010-01-05 15:03:27 -05:00
|
|
|
if RestClient.log
|
|
|
|
if size == 0
|
2010-01-24 11:22:48 -05:00
|
|
|
RestClient.log << "#{@method} #{@url} done (0 length file\n)"
|
2010-01-05 15:03:27 -05:00
|
|
|
elsif total == 0
|
2010-01-24 11:22:48 -05:00
|
|
|
RestClient.log << "#{@method} #{@url} (zero content length)\n"
|
2010-01-05 15:03:27 -05:00
|
|
|
else
|
2010-01-24 11:22:48 -05:00
|
|
|
RestClient.log << "#{@method} #{@url} %d%% done (%d of %d)\n" % [(size * 100) / total, size, total]
|
2010-01-05 15:03:27 -05:00
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@tf.close
|
|
|
|
@tf
|
|
|
|
else
|
|
|
|
http_response.read_body
|
|
|
|
end
|
|
|
|
http_response
|
|
|
|
end
|
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
def process_result res, &block
|
2010-01-18 16:15:15 -05:00
|
|
|
if @raw_response
|
2009-12-29 12:27:39 -05:00
|
|
|
# We don't decode raw requests
|
2010-02-10 15:31:59 -05:00
|
|
|
response = RawResponse.new(@tf, res, args)
|
2010-01-18 16:15:15 -05:00
|
|
|
else
|
2010-03-29 13:38:23 -04:00
|
|
|
response = Response.create(Request.decode(res['content-encoding'], res.body), res, args)
|
2010-01-18 16:15:15 -05:00
|
|
|
end
|
|
|
|
|
2010-02-10 15:31:59 -05:00
|
|
|
if block_given?
|
|
|
|
block.call response, &block
|
2009-12-29 12:27:39 -05:00
|
|
|
else
|
2010-02-10 15:31:59 -05:00
|
|
|
response.return! &block
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2010-02-10 15:31:59 -05:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-01-18 16:15:15 -05:00
|
|
|
def self.decode content_encoding, body
|
2010-02-24 12:28:29 -05:00
|
|
|
if (!body) || body.empty?
|
|
|
|
body
|
|
|
|
elsif content_encoding == 'gzip'
|
2009-12-29 12:27:39 -05:00
|
|
|
Zlib::GzipReader.new(StringIO.new(body)).read
|
|
|
|
elsif content_encoding == 'deflate'
|
2010-01-18 16:15:15 -05:00
|
|
|
Zlib::Inflate.new.inflate body
|
2009-12-29 12:27:39 -05:00
|
|
|
else
|
|
|
|
body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
def log_request
|
2009-12-29 14:30:38 -05:00
|
|
|
if RestClient.log
|
|
|
|
out = []
|
|
|
|
out << "RestClient.#{method} #{url.inspect}"
|
2010-01-05 15:03:27 -05:00
|
|
|
out << payload.short_inspect if payload
|
2010-03-28 04:58:21 -04:00
|
|
|
out << processed_headers.to_a.sort.map{|(k,v)| [k.inspect, v.inspect].join("=>")}.join(", ")
|
2010-01-24 11:22:48 -05:00
|
|
|
RestClient.log << out.join(', ') + "\n"
|
2009-12-29 14:30:38 -05:00
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
def log_response res
|
|
|
|
if RestClient.log
|
|
|
|
size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size)
|
2010-01-24 11:22:48 -05:00
|
|
|
RestClient.log << "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes\n"
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-28 04:58:21 -04:00
|
|
|
# Return a hash of headers whose keys are capitalized strings
|
|
|
|
def stringify_headers headers
|
|
|
|
headers.inject({}) do |result, (key, value)|
|
|
|
|
target_key = key.to_s.split(/_/).map{|w| w.capitalize}.join('-')
|
|
|
|
if 'CONTENT-TYPE' == target_key.upcase
|
|
|
|
target_value = value.to_s
|
|
|
|
result[target_key] = MIME::Types.type_for_extension target_value
|
|
|
|
elsif 'ACCEPT' == target_key.upcase
|
|
|
|
# Accept can be composed of several comma-separated values
|
|
|
|
if value.is_a? Array
|
|
|
|
target_values = value
|
|
|
|
else
|
|
|
|
target_values = value.to_s.split ','
|
|
|
|
end
|
|
|
|
result[target_key] = target_values.map{ |ext| MIME::Types.type_for_extension(ext.to_s.strip)}.join(', ')
|
|
|
|
else
|
|
|
|
result[target_key] = value.to_s
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
def default_headers
|
|
|
|
{ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' }
|
|
|
|
end
|
2010-03-28 04:58:21 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:49:01 -05:00
|
|
|
end
|
2009-12-29 14:30:38 -05:00
|
|
|
|
|
|
|
module MIME
|
|
|
|
class Types
|
|
|
|
|
|
|
|
# Return the first found content-type for a value considered as an extension or the value itself
|
|
|
|
def type_for_extension ext
|
2010-01-05 15:03:27 -05:00
|
|
|
candidates = @extension_index[ext]
|
2009-12-29 14:30:38 -05:00
|
|
|
candidates.empty? ? ext : candidates[0].content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def type_for_extension ext
|
|
|
|
@__types__.type_for_extension ext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|