Skip to content

Commit

Permalink
[fog|time] add date header formatting that is non-localized
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Jan 28, 2011
1 parent 242595e commit d055ac1
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/fog/aws/ses.rb
Expand Up @@ -70,7 +70,7 @@ def request(params)

headers = {
'Content-Type' => 'application/x-www-form-urlencoded',
'Date' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
'Date' => Fog::Time.now.to_date_header
}

#AWS3-HTTPS AWSAccessKeyId=<Your AWS Access Key ID>, Algorithm=HmacSHA256, Signature=<Signature>
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/cdn/aws.rb
Expand Up @@ -98,7 +98,7 @@ def reload

def request(params, &block)
params[:headers] ||= {}
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
params[:path] = "/#{@version}/#{params[:path]}"
@connection.request(params, &block)
Expand Down
32 changes: 19 additions & 13 deletions lib/fog/core/time.rb
@@ -1,21 +1,27 @@
module Fog
class Time < ::Time
class << self

def now
::Time.now - offset
end
DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

def now=(new_now)
old_now = ::Time.now
@offset = old_now - new_now
new_now
end
def self.now
at((::Time.now - offset).to_i)
end

def offset
@offset ||= 0
end
def self.now=(new_now)
old_now = ::Time.now
@offset = old_now - new_now
new_now
end

def self.offset
@offset ||= 0
end

def to_date_header
now = self.class.now.utc
now.strftime("#{DAYS[now.wday]}, %d #{MONTHS[now.month - 1]} %Y %H:%M:%S +0000")
end

end
end
end
2 changes: 1 addition & 1 deletion lib/fog/dns/aws.rb
Expand Up @@ -103,7 +103,7 @@ def reload

def request(params, &block)
params[:headers] ||= {}
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['X-Amzn-Authorization'] = "AWS3-HTTPS AWSAccessKeyId=#{@aws_access_key_id},Algorithm=HmacSHA1,Signature=#{signature(params)}"
params[:path] = "/#{@version}/#{params[:path]}"
@connection.request(params, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/storage/aws.rb
Expand Up @@ -289,7 +289,7 @@ def reload
private

def request(params, &block)
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
params[:expects] = [307, *params[:expects]].flatten
# FIXME: ToHashParser should make this not needed
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/storage/google.rb
Expand Up @@ -212,7 +212,7 @@ def reload
private

def request(params, &block)
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Date'] = Fog::Time.now.to_date_header
params[:headers]['Authorization'] = "GOOG1 #{@google_storage_access_key_id}:#{signature(params)}"

response = @connection.request(params, &block)
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/storage/requests/aws/get_object.rb
Expand Up @@ -39,8 +39,8 @@ def get_object(bucket_name, object_name, options = {}, &block)
query = {'versionId' => version_id}
end
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/storage/requests/aws/head_object.rb
Expand Up @@ -39,8 +39,8 @@ def head_object(bucket_name, object_name, options={})
query = {'versionId' => version_id}
end
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/storage/requests/aws/put_object.rb
Expand Up @@ -67,7 +67,7 @@ def put_object(bucket_name, object_name, data, options = {})
'Content-Type' => data[:headers]['Content-Type'],
'ETag' => Fog::AWS::Mock.etag,
'Key' => object_name,
'LastModified' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000"),
'LastModified' => Fog::Time.now.to_date_header,
'Size' => data[:headers]['Content-Length'],
'StorageClass' => 'STANDARD'
}
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/storage/requests/google/get_object.rb
Expand Up @@ -36,8 +36,8 @@ def get_object(bucket_name, object_name, options = {}, &block)
query = {'versionId' => version_id}
end
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/storage/requests/google/head_object.rb
Expand Up @@ -35,8 +35,8 @@ def head_object(bucket_name, object_name, options={})
query = {'versionId' => version_id}
end
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/storage/requests/google/put_object.rb
Expand Up @@ -62,7 +62,7 @@ def put_object(bucket_name, object_name, data, options = {})
'Content-Type' => data[:headers]['Content-Type'],
'ETag' => Fog::Google::Mock.etag,
'Key' => object_name,
'LastModified' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000"),
'LastModified' => Fog::Time.now.to_date_header,
'Size' => data[:headers]['Content-Length'],
'StorageClass' => 'STANDARD'
}
Expand Down

0 comments on commit d055ac1

Please sign in to comment.