/
utils.rb
165 lines (135 loc) · 4.39 KB
/
utils.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'tempfile'
module Rack
module Utils
# Performs URI escaping so that you can construct proper
# query strings faster. Use this rather than the cgi.rb
# version since it's faster. (Stolen from Camping).
def escape(s)
s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
'%'+$1.unpack('H2'*$1.size).join('%').upcase
}.tr(' ', '+')
end
module_function :escape
# Unescapes a URI escaped string. (Stolen from Camping).
def unescape(s)
s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
[$1.delete('%')].pack('H*')
}
end
module_function :unescape
# Stolen from Mongrel:
# Parses a query string by breaking it up at the '&'
# and ';' characters. You can also use this to parse
# cookies by changing the characters used in the second
# parameter (which defaults to '&;'.
def parse_query(qs, d = '&;')
params = {}
(qs||'').split(/[#{d}] */n).inject(params) { |h,p|
k, v=unescape(p).split('=',2)
if cur = params[k]
if cur.class == Array
params[k] << v
else
params[k] = [cur, v]
end
else
params[k] = v
end
}
return params
end
module_function :parse_query
def escape_html(string)
string.to_s.gsub("&", "&").
gsub("<", "<").
gsub(">", ">").
gsub("'", "'").
gsub('"', """)
end
module_function :escape_html
class HeaderHash < Hash
def initialize(hash={})
hash.each { |k, v| self[k] = v }
end
def to_hash
{}.replace(self)
end
def [](k)
super capitalize(k)
end
def []=(k, v)
super capitalize(k), v
end
def capitalize(k)
k.to_s.downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }
end
end
# Adapted from IOWA.
module Multipart
EOL = "\r\n"
def self.parse_multipart(env)
unless env['CONTENT_TYPE'] =~ %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
nil
else
boundary = "--#{$1}"
params = {}
buf = ""
content_length = env['CONTENT_LENGTH'].to_i
input = env['rack.input']
boundary_size = boundary.size + EOL.size
bufsize = 16384
content_length -= boundary_size
status = input.read(boundary_size)
raise EOFError, "bad content body" unless status == boundary + EOL
loop {
head = nil
body = ''
filename = content_type = name = nil
until head && buf =~ /#{boundary}(?:#{EOL}|--)/ # /
if !head && i = buf.index("\r\n\r\n")
head = buf.slice!(0, i+2) # First \r\n
buf.slice!(0, 2) # Second \r\n
filename = head[/Content-Disposition:.* filename="?([^\";]*)"?/ni, 1]
content_type = head[/Content-Type: (.*)\r\n/ni, 1]
name = head[/Content-Disposition:.* name="?([^\";]*)"?/ni, 1]
body = Tempfile.new("RackMultipart") if filename
next
end
# Save the read body part.
if head && (boundary.size+4 < buf.size)
body << buf.slice!(0, buf.size - (boundary.size+4))
end
c = input.read(bufsize < content_length ? bufsize : content_length)
raise EOFError, "bad content body" if c.nil? || c.empty?
buf << c
content_length -= c.size
end
# Save the rest.
if i = buf.index(/(?:#{EOL})?#{boundary}(#{EOL}|--)/n)
body << buf.slice!(0, i)
buf.slice!(0, boundary_size+2)
content_length = -1 if $1 == "--"
end
if filename
body.rewind
data = {:filename => filename, :type => content_type,
:name => name, :tempfile => body, :head => head}
else
data = body
end
if name
if name =~ /\[\]\z/
params[name] ||= []
params[name] << data
else
params[name] = data
end
end
break if buf.empty? || content_length == -1
}
params
end
end
end
end
end