2008-09-30 08:39:57 +02:00
|
|
|
# This file must be loaded after the JSON gem and any other library that beats up the Time class.
|
2008-09-12 06:09:39 +02:00
|
|
|
class Time
|
2008-09-26 23:31:29 +02:00
|
|
|
# This date format sorts lexicographically
|
2008-09-30 08:39:57 +02:00
|
|
|
# and is compatible with Javascript's <tt>new Date(time_string)</tt> constructor.
|
2008-09-26 23:31:29 +02:00
|
|
|
# Note this this format stores all dates in UTC so that collation
|
2008-09-30 08:39:57 +02:00
|
|
|
# order is preserved. (There's no longer a need to set <tt>ENV['TZ'] = 'UTC'</tt>
|
2008-09-26 23:31:29 +02:00
|
|
|
# in your application.)
|
2008-09-12 06:09:39 +02:00
|
|
|
|
|
|
|
def to_json(options = nil)
|
2008-09-26 23:31:29 +02:00
|
|
|
u = self.utc
|
2009-01-14 05:08:58 +01:00
|
|
|
%("#{u.strftime("%Y/%m/%d %H:%M:%S +0000")}")
|
2008-09-12 06:09:39 +02:00
|
|
|
end
|
|
|
|
|
2008-09-26 23:31:29 +02:00
|
|
|
# Decodes the JSON time format to a UTC time.
|
|
|
|
# Based on Time.parse from ActiveSupport. ActiveSupport's version
|
|
|
|
# is more complete, returning a time in your current timezone,
|
|
|
|
# rather than keeping the time in UTC. YMMV.
|
2008-09-12 06:09:39 +02:00
|
|
|
# def self.parse string, fallback=nil
|
|
|
|
# d = DateTime.parse(string).new_offset
|
|
|
|
# self.utc(d.year, d.month, d.day, d.hour, d.min, d.sec)
|
|
|
|
# rescue
|
|
|
|
# fallback
|
|
|
|
# end
|
2009-01-16 09:37:27 +01:00
|
|
|
end
|
|
|
|
|
2009-01-29 03:37:45 +01:00
|
|
|
# Monkey patch for faster net/http io
|
|
|
|
if RUBY_VERSION.to_f < 1.9
|
|
|
|
class Net::BufferedIO #:nodoc:
|
|
|
|
alias :old_rbuf_fill :rbuf_fill
|
|
|
|
def rbuf_fill
|
|
|
|
begin
|
|
|
|
@rbuf << @io.read_nonblock(65536)
|
|
|
|
rescue Errno::EWOULDBLOCK
|
|
|
|
if IO.select([@io], nil, nil, @read_timeout)
|
|
|
|
@rbuf << @io.read_nonblock(65536)
|
|
|
|
else
|
|
|
|
raise Timeout::TimeoutError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-16 09:37:27 +01:00
|
|
|
module RestClient
|
|
|
|
def self.copy(url, headers={})
|
|
|
|
Request.execute(:method => :copy,
|
|
|
|
:url => url,
|
|
|
|
:headers => headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.move(url, headers={})
|
|
|
|
Request.execute(:method => :move,
|
|
|
|
:url => url,
|
|
|
|
:headers => headers)
|
|
|
|
end
|
2009-01-29 03:37:45 +01:00
|
|
|
|
|
|
|
class Request
|
|
|
|
def transmit(uri, req, payload)
|
|
|
|
setup_credentials(req)
|
|
|
|
|
|
|
|
Thread.current[:host] ||= uri.host
|
|
|
|
Thread.current[:port] ||= uri.port
|
|
|
|
|
|
|
|
net = net_http_class.new(uri.host, uri.port)
|
|
|
|
|
|
|
|
if Thread.current[:connection].nil? || Thread.current[:host] != uri.host
|
|
|
|
Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
|
|
|
|
net.use_ssl = uri.is_a?(URI::HTTPS)
|
|
|
|
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
Thread.current[:connection] = net
|
|
|
|
Thread.current[:connection].start
|
|
|
|
end
|
|
|
|
|
|
|
|
display_log request_log
|
|
|
|
http = Thread.current[:connection]
|
|
|
|
|
|
|
|
http.read_timeout = @timeout if @timeout
|
|
|
|
begin
|
|
|
|
res = http.request(req, payload)
|
|
|
|
rescue
|
|
|
|
# p "Net::HTTP connection failed, reconnecting"
|
|
|
|
Thread.current[:connection].finish
|
|
|
|
http = Thread.current[:connection] = net
|
|
|
|
Thread.current[:connection].start
|
|
|
|
res = http.request(req, payload)
|
|
|
|
display_log response_log(res)
|
|
|
|
process_result res
|
|
|
|
else
|
|
|
|
display_log response_log(res)
|
|
|
|
process_result res
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue EOFError
|
|
|
|
raise RestClient::ServerBrokeConnection
|
|
|
|
rescue Timeout::Error
|
|
|
|
raise RestClient::RequestTimeout
|
|
|
|
end
|
|
|
|
end
|
2008-09-12 06:09:39 +02:00
|
|
|
end
|