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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
=begin
= apache/query.rb
Copyright (C) 2003 Shugo Maeda <shugo@modruby.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
=end
module Apache
class Query
attr_reader :params, :cookies
def initialize(req)
@req = req
@files = []
@closed = false
if @req.method_number == Apache::M_POST &&
%r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n =~
@req.headers_in["Content-Type"]
@multipart = true
boundary = $1
@params = get_multipart(boundary)
else
@multipart = false
str = get_query_string
@params = parse_query_string(str)
end
@cookies = get_cookies
end
def multipart?
return @multipart
end
def param(name)
return @params[name][0]
end
alias [] param
def close
return if @closed
for file in @files
file.close!
end
@closed = true
end
private
def get_query_string
if @req.method_number == Apache::M_POST
return @req.read(@req.headers_in["Content-Length"].to_i) || ""
else
return @req.args || ""
end
end
def parse_query_string(str)
params = Hash.new([])
str.split(/[&;]/n).each do |pairs|
key, value = pairs.split("=",2).collect{ |v| urldecode(v) }
if params.key?(key)
params[key].push(value)
else
params[key] = [value]
end
end
return params
end
def get_multipart(boundary)
params = Hash.new([])
reader = MultipartReader.new(@req, boundary)
while !reader.eof?
header = reader.read_header
if / name="?([^\";]*)"?/n =~ header["Content-Disposition"]
name = $1
else
name = ""
end
if / filename="?([^\";]*)"?/n =~ header["Content-Disposition"]
filename = $1
else
value = reader.read_body
if params.key?(name)
params[name].push(value)
else
params[name] = [value]
end
next
end
if /Mac/ni =~ @req.headers_in["User-Agent"] &&
/Mozilla/ni =~ @req.headers_in["User-Agent"] &&
/MSIE/ni !~ @req.headers_in["User-Agent"]
filename = urldecode(filename)
end
file = MultipartFile.new("query.rb")
@files.push(file)
file.binmode
while data = reader.read
file.print(data)
end
file.rewind
file.original_filename = filename
file.content_type = header["Content-Type"]
if params.key?(name)
params[name].push(file)
else
params[name] = [file]
end
end
return params
end
def get_cookies
cookies = {}
str = @req.headers_in["Cookie"]
return cookies unless str
str.split(/; /n).each do |pair|
name, value = pair.split("=", 2)
name = urldecode(name)
value = urldecode(value || "")
cookies[name] = value
end
return cookies
end
def urldecode(str)
return str.tr("+", " ").gsub(/((?:%[0-9a-fA-F]{2})+)/n) {
[$1.delete("%")].pack("H*")
}
end
end
class MultipartReader
INITIAL_SIZE = 4 * 1024
EOL = "\r\n"
HEADER_VALID_TOKEN = '[-\w!\#$%&\'*+.^_`|{}~]'
HEADER_REGEXP = /(#{HEADER_VALID_TOKEN}+):\s+([^#{EOL}]*)/n
def initialize(req, boundary)
@req = req
@boundary = "--" + boundary
@length = @req.headers_in["Content-Length"].to_i
@buffer = ""
@chunk_size = @boundary.size > INITIAL_SIZE ?
@boundary.size : INITIAL_SIZE
end
def read_header
while true
fill_buffer(@chunk_size)
endi = @buffer.index(EOL + EOL)
break if endi || @buffer.empty?
return {} unless @length > 0
end
header = @buffer[0 ... endi + 2]
@buffer[0 ... endi + 4] = ""
result = {}
header.gsub!(/EOL\s+/n, "")
header.scan(HEADER_REGEXP) do |name, value|
name.gsub!(/\b\w/n) { |m| m.upcase }
result[name] = value
end
return result
end
def read_body
result = ""
while data = read
result.concat(data)
end
result
end
def read(bytes = INITIAL_SIZE)
fill_buffer(bytes)
start = @buffer.index(@boundary)
case start
when 0
if @buffer.index(@boundary + "--") == 0
@buffer = ""
@length = 0
return nil
end
@buffer[0 ... @boundary.size + 2] = ""
return nil
when nil
bytes_to_return = bytes - @boundary.size - 1
else
bytes_to_return = start > bytes ? bytes : start
end
chunk = @buffer[0 ... bytes_to_return]
@buffer[0 ... bytes_to_return] = ""
return start ? chunk[0 ... -2] : chunk
end
def fill_buffer(bytes)
return if @length == 0
bytes_to_read = bytes - @buffer.size + @boundary.size + 2
bytes_to_read = @length if bytes_to_read > @length
chunk = @req.read(bytes_to_read) || ""
@buffer.concat(chunk)
@length -= chunk.size
end
def eof?
@buffer.empty? && @length <= 0
end
end
class MultipartFile < Tempfile
attr_accessor :original_filename, :content_type
end
end
|