-
Notifications
You must be signed in to change notification settings - Fork 35
/
S3encoder.rb
50 lines (46 loc) · 1.87 KB
/
S3encoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# This software code is made available "AS IS" without warranties of any
# kind. You may copy, display, modify and redistribute the software
# code either by itself or as incorporated into your code; provided that
# you do not remove any proprietary notices. Your use of this software
# code is at your own risk and you waive any claim against the author
# with respect to your use of this software code.
# (c) 2007 s3sync.net
#
# The purpose of this file is to overlay the cgi class
# to add some functionality
# (without changing the file itself or requiring a specific version)
# It still isn't perfectly robust, i.e. if radical changes are made
# to the underlying lib this stuff will need updating.
require 'cgi'
require 'iconv' # for UTF-8 conversion
# thanks to http://www.redhillconsulting.com.au/blogs/simon/archives/000326.html
module S3ExtendCGI
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
class << self
alias_method :S3Extend_escape_orig, :escape unless method_defined?(:S3Extend_escape_orig)
alias_method :escape, :S3Extend_escape
end
end
end
module ClassMethods
@@exemptSlashesInEscape = false
attr_writer :exemptSlashesInEscape
@@usePercent20InEscape = false
attr_writer :usePercent20InEscape
@@nativeCharacterEncoding = "ISO-8859-1"
attr_writer :nativeCharacterEncoding
@@useUTF8InEscape = false
attr_writer :useUTF8InEscape
def S3Extend_escape(string)
result = string
result = Iconv.iconv("UTF-8", @nativeCharacterEncoding, string).join if @useUTF8InEscape
result = S3Extend_escape_orig(result)
result.gsub!(/%2f/i, "/") if @exemptSlashesInEscape
result.gsub!("+", "%20") if @usePercent20InEscape
result
end
end
end
CGI.send(:include, S3ExtendCGI)