Better URL escaping for Rackspace Cloud Files.

URI.escape doesn't encode question marks properly, CGI.escape doesn't encode
spaces properly.  So we create an escape class method for Fog::Rackspace that
does the CGI.escape methods, only encoding spaces as %20.

This makes things work properly with Rackspace Cloud Files.
This commit is contained in:
H. Wade Minter 2011-09-16 23:46:22 -04:00
parent cb1d5919f0
commit 934ff8a993
11 changed files with 19 additions and 9 deletions

View File

@ -70,5 +70,12 @@ module Fog
!['X-Server-Management-Url', 'X-Storage-Url', 'X-CDN-Management-Url', 'X-Auth-Token'].include?(key)
end
end
# CGI.escape, but without special treatment on spaces
def self.escape(str,extra_exclude_chars = '')
str.gsub(/([^a-zA-Z0-9_.-#{extra_exclude_chars}]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end
end
end
end

View File

@ -12,7 +12,7 @@ module Fog
request(
:expects => 204,
:method => 'DELETE',
:path => URI.escape(name)
:path => Fog::Rackspace.escape(name)
)
end

View File

@ -13,7 +13,7 @@ module Fog
request(
:expects => 204,
:method => 'DELETE',
:path => "#{URI.escape(container)}/#{URI.escape(object)}"
:path => "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
)
end

View File

@ -33,7 +33,7 @@ module Fog
request(
:expects => 200,
:method => 'GET',
:path => URI.escape(container),
:path => Fog::Rackspace.escape(container),
:query => {'format' => 'json'}.merge!(options)
)
end

View File

@ -14,7 +14,7 @@ module Fog
:block => block,
:expects => 200,
:method => 'GET',
:path => "#{URI.escape(container)}/#{URI.escape(object)}"
:path => "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
}, false, &block)
end

View File

@ -17,7 +17,7 @@ module Fog
request(
:expects => 204,
:method => 'HEAD',
:path => URI.escape(container),
:path => Fog::Rackspace.escape(container),
:query => {'format' => 'json'}
)
end

View File

@ -13,7 +13,7 @@ module Fog
request({
:expects => 200,
:method => 'HEAD',
:path => "#{URI.escape(container)}/#{URI.escape(object)}"
:path => "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
}, false)
end

View File

@ -12,7 +12,7 @@ module Fog
request(
:expects => [201, 202],
:method => 'PUT',
:path => URI.escape(name)
:path => Fog::Rackspace.escape(name)
)
end

View File

@ -19,7 +19,7 @@ module Fog
:expects => 201,
:headers => headers,
:method => 'PUT',
:path => "#{URI.escape(container)}/#{URI.escape(object)}"
:path => "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
)
end

View File

@ -10,7 +10,7 @@ module Fog
# * object<~String> - Name for object
#
def put_object_manifest(container, object)
path = "#{URI.escape(container)}/#{URI.escape(object)}"
path = "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
request(
:expects => 201,
:headers => {'X-Object-Manifest' => path},

View File

@ -0,0 +1,3 @@
Shindo.tests('Rackspace | url_encoding', ['rackspace']) do
returns( Fog::Rackspace.escape( "is this my file?.jpg" ) ) { "is%20this%20my%20file%3F.jpg" }
end