1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

implements the get_http_url, get_https_url and url functions according to the openstack and aws providers

This commit is contained in:
Julian Weber 2013-10-08 16:48:26 +02:00
parent b5761cc36b
commit b7056383cb
4 changed files with 123 additions and 12 deletions

View file

@ -60,6 +60,18 @@ module Fog
self.collection.get_url(self.key)
end
# Get a url for file.
#
# required attributes: key
#
# @param expires [String] number of seconds (since 1970-01-01 00:00) before url expires
# @param options [Hash]
# @return [String] url
def url(expires, options = {})
requires :key
self.collection.get_https_url(key, expires, options)
end
def cdn_public_url
requires :key
self.collection.get_cdn_url(self.key)

View file

@ -71,7 +71,29 @@ module Fog
"#{self.directory.public_url}/#{Fog::HP.escape(key)}"
end
end
# Get a temporary http url for a file.
#
# required attributes: key
# @param key [String] the key of the file within the directory
# @param expires [String] number of seconds (since 1970-01-01 00:00) before url expires
# @param options [Hash]
# @return [String] url
def get_http_url(key, expires, options = {})
service.get_object_http_url(directory.key, key, expires, options)
end
# Get a temporary https url for a file.
#
# required attributes: key
# @param key [String] the key of the file within the directory
# @param expires [String] number of seconds (since 1970-01-01 00:00) before url expires
# @param options [Hash]
# @return [String] url
def get_https_url(key, expires, options = {})
service.get_object_https_url(directory.key, key, expires, options)
end
def get_cdn_url(key)
requires :directory
if self.directory.cdn_public_url

View file

@ -146,9 +146,69 @@ module Fog
write_h = write_header.split(',') unless write_header.nil?
return read_h, write_h
end
# Get an expiring object https url
#
# ==== Parameters
# * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for
# * expires<~Time> - An expiry time for this url
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~String> - url for object
def get_object_https_url(container, object, expires, options = {})
create_temp_url(container, object, expires, "GET", options.merge(:scheme => "https"))
end
# Get an expiring object http url
#
# ==== Parameters
# * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for
# * expires<~Time> - An expiry time for this url
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~String> - url for object
def get_object_http_url(container, object, expires, options = {})
create_temp_url(container, object, expires, "GET", options.merge(:scheme => "http"))
end
# Get an object http url expiring in the given amount of seconds
#
# ==== Parameters
# * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for
# * expires_secs<~Integer> - the amount of seconds from now until the url expires
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~String> - url for object
def generate_object_temp_url(container, object, expires_secs, method)
return unless (container && object && expires_secs && method)
expiration_time = (Time.now + expires_secs.to_i).to_i
create_temp_url(container, object, expiration_time, method, {})
end
# creates a temporary url
#
# ==== Parameters
# * container<~String> - Name of container containing object
# * object<~String> - Name of object to get expiring url for
# * expires<~Time> - An expiry time for this url
# * method<~String> - The method to use for accessing the object (GET, PUT, HEAD)
# * scheme<~String> - The scheme to use (http, https)
# * options<~Hash> - An optional options hash
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~String> - url for object
#
# ==== See Also
# http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html
def create_temp_url(container, object, expires, method, options = {})
raise ArgumentError, "Insufficient parameters specified." unless (container && object && expires && method)
raise ArgumentError, "Storage must my instantiated with the :os_account_meta_temp_url_key option" if @os_account_meta_temp_url_key.nil?
# POST not allowed
allowed_methods = %w{GET PUT HEAD}
@ -156,23 +216,25 @@ module Fog
raise ArgumentError.new("Invalid method '#{method}' specified. Valid methods are: #{allowed_methods.join(', ')}")
end
expires = (Time.now + expires_secs.to_i).to_i
expires = expires.to_i
# split up the storage uri
uri = URI.parse(@hp_storage_uri)
host = uri.host
path = uri.path
port = uri.port
scheme = uri.scheme
uri_scheme = uri.scheme
scheme = options[:scheme] || uri_scheme
# do not encode before signature generation, encode after
sig_path = "#{path}/#{container}/#{object}"
encoded_path = "#{path}/#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
string_to_sign = "#{method}\n#{expires}\n#{sig_path}"
signature = nil
signature = nil
# HP uses a different strategy to create the signature that is passed to swift than OpenStack.
# As the HP provider is broadly used by OpenStack users the OpenStack strategy is applied when
# the @os_account_meta_temp_url_key is given.
@ -190,11 +252,14 @@ module Fog
signature = @hp_tenant_id.to_s + ":" + @hp_access_key.to_s + ":" + signed_string
signature = Fog::HP.escape(signature)
end
# generate the temp url using the signature and expiry
"#{scheme}://#{host}:#{port}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
if scheme == "http"
"#{scheme}://#{host}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
else
"#{scheme}://#{host}:#{port}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
end
end
end
class Mock

View file

@ -46,6 +46,18 @@ Shindo.tests("Fog::Storage[:hp] | object requests", ['hp', 'storage']) do
tests("#delete_object('#{@dir_name}', 'fog_object')").succeeds do
Fog::Storage[:hp].delete_object(@dir_name, 'fog_object')
end
tests("#get_object_http_url('#{@directory.identity}', 'fog_object', expiration timestamp)").returns(true) do
object_url = Fog::Storage[:hp].get_object_http_url(@dir_name, 'fog_object', (Time.now + 60))
(object_url =~ /http:\/\/\S+\/v1\/AUTH_\S+\/#{@dir_name}\/fog_object\?temp_url_sig=\S+&temp_url_expires=\d+/) != nil
end
tests("#get_object_https_url('#{@directory.identity}', 'fog_object', expiration timestamp)").returns(true) do
object_url = Fog::Storage[:hp].get_object_https_url(@dir_name, 'fog_object', (Time.now + 60))
(object_url =~ /https:\/\/\S+\/v1\/AUTH_\S+\/#{@dir_name}\/fog_object\?temp_url_sig=\S+&temp_url_expires=\d+/) != nil
end
end
tests('failure') do
@ -83,7 +95,7 @@ Shindo.tests("Fog::Storage[:hp] | object requests", ['hp', 'storage']) do
end
end
@directory.destroy
end