2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# httputils.rb -- HTTPUtils Module
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: httputils.rb,v 1.34 2003/06/05 21:34:08 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
module WEBrick
|
2013-01-25 20:12:54 -05:00
|
|
|
CR = "\x0d" # :nodoc:
|
|
|
|
LF = "\x0a" # :nodoc:
|
|
|
|
CRLF = "\x0d\x0a" # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTPUtils provides utility methods for working with the HTTP protocol.
|
|
|
|
#
|
|
|
|
# This module is generally used internally by WEBrick
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
module HTTPUtils
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Normalizes a request path. Raises an exception if the path cannot be
|
|
|
|
# normalized.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def normalize_path(path)
|
|
|
|
raise "abnormal path `#{path}'" if path[0] != ?/
|
|
|
|
ret = path.dup
|
|
|
|
|
|
|
|
ret.gsub!(%r{/+}o, '/') # // => /
|
2008-07-25 10:35:21 -04:00
|
|
|
while ret.sub!(%r'/\.(?:/|\Z)', '/'); end # /. => /
|
|
|
|
while ret.sub!(%r'/(?!\.\./)[^/]+/\.\.(?:/|\Z)', '/'); end # /foo/.. => /foo
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
raise "abnormal path `#{path}'" if %r{/\.\.(/|\Z)} =~ ret
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
module_function :normalize_path
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Default mime types
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
DefaultMimeTypes = {
|
|
|
|
"ai" => "application/postscript",
|
|
|
|
"asc" => "text/plain",
|
|
|
|
"avi" => "video/x-msvideo",
|
|
|
|
"bin" => "application/octet-stream",
|
|
|
|
"bmp" => "image/bmp",
|
|
|
|
"class" => "application/octet-stream",
|
|
|
|
"cer" => "application/pkix-cert",
|
|
|
|
"crl" => "application/pkix-crl",
|
|
|
|
"crt" => "application/x-x509-ca-cert",
|
|
|
|
#"crl" => "application/x-pkcs7-crl",
|
|
|
|
"css" => "text/css",
|
|
|
|
"dms" => "application/octet-stream",
|
|
|
|
"doc" => "application/msword",
|
|
|
|
"dvi" => "application/x-dvi",
|
|
|
|
"eps" => "application/postscript",
|
|
|
|
"etx" => "text/x-setext",
|
|
|
|
"exe" => "application/octet-stream",
|
|
|
|
"gif" => "image/gif",
|
|
|
|
"htm" => "text/html",
|
|
|
|
"html" => "text/html",
|
|
|
|
"jpe" => "image/jpeg",
|
|
|
|
"jpeg" => "image/jpeg",
|
|
|
|
"jpg" => "image/jpeg",
|
2011-09-25 22:28:54 -04:00
|
|
|
"js" => "application/javascript",
|
2017-09-18 21:54:28 -04:00
|
|
|
"json" => "application/json",
|
2003-07-23 12:51:36 -04:00
|
|
|
"lha" => "application/octet-stream",
|
|
|
|
"lzh" => "application/octet-stream",
|
|
|
|
"mov" => "video/quicktime",
|
|
|
|
"mpe" => "video/mpeg",
|
|
|
|
"mpeg" => "video/mpeg",
|
|
|
|
"mpg" => "video/mpeg",
|
|
|
|
"pbm" => "image/x-portable-bitmap",
|
|
|
|
"pdf" => "application/pdf",
|
|
|
|
"pgm" => "image/x-portable-graymap",
|
|
|
|
"png" => "image/png",
|
|
|
|
"pnm" => "image/x-portable-anymap",
|
|
|
|
"ppm" => "image/x-portable-pixmap",
|
|
|
|
"ppt" => "application/vnd.ms-powerpoint",
|
|
|
|
"ps" => "application/postscript",
|
|
|
|
"qt" => "video/quicktime",
|
|
|
|
"ras" => "image/x-cmu-raster",
|
|
|
|
"rb" => "text/plain",
|
|
|
|
"rd" => "text/plain",
|
|
|
|
"rtf" => "application/rtf",
|
|
|
|
"sgm" => "text/sgml",
|
|
|
|
"sgml" => "text/sgml",
|
2011-09-25 22:28:54 -04:00
|
|
|
"svg" => "image/svg+xml",
|
2003-07-23 12:51:36 -04:00
|
|
|
"tif" => "image/tiff",
|
|
|
|
"tiff" => "image/tiff",
|
|
|
|
"txt" => "text/plain",
|
|
|
|
"xbm" => "image/x-xbitmap",
|
2005-08-07 16:16:15 -04:00
|
|
|
"xhtml" => "text/html",
|
2003-07-23 12:51:36 -04:00
|
|
|
"xls" => "application/vnd.ms-excel",
|
|
|
|
"xml" => "text/xml",
|
|
|
|
"xpm" => "image/x-xpixmap",
|
|
|
|
"xwd" => "image/x-xwindowdump",
|
|
|
|
"zip" => "application/zip",
|
|
|
|
}
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Loads Apache-compatible mime.types in +file+.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def load_mime_types(file)
|
|
|
|
open(file){ |io|
|
|
|
|
hash = Hash.new
|
|
|
|
io.each{ |line|
|
|
|
|
next if /^#/ =~ line
|
|
|
|
line.chomp!
|
|
|
|
mimetype, ext0 = line.split(/\s+/, 2)
|
2009-03-05 22:56:38 -05:00
|
|
|
next unless ext0
|
2003-07-23 12:51:36 -04:00
|
|
|
next if ext0.empty?
|
|
|
|
ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype }
|
|
|
|
}
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
end
|
|
|
|
module_function :load_mime_types
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Returns the mime type of +filename+ from the list in +mime_tab+. If no
|
|
|
|
# mime type was found application/octet-stream is returned.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def mime_type(filename, mime_tab)
|
2004-10-12 08:26:39 -04:00
|
|
|
suffix1 = (/\.(\w+)$/ =~ filename && $1.downcase)
|
|
|
|
suffix2 = (/\.(\w+)\.[\w\-]+$/ =~ filename && $1.downcase)
|
|
|
|
mime_tab[suffix1] || mime_tab[suffix2] || "application/octet-stream"
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
module_function :mime_type
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses an HTTP header +raw+ into a hash of header fields with an Array
|
|
|
|
# of values.
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
def parse_header(raw)
|
|
|
|
header = Hash.new([].freeze)
|
|
|
|
field = nil
|
2006-12-01 17:38:47 -05:00
|
|
|
raw.each_line{|line|
|
2003-07-23 12:51:36 -04:00
|
|
|
case line
|
2004-01-26 12:31:53 -05:00
|
|
|
when /^([A-Za-z0-9!\#$%&'*+\-.^_`|~]+):\s*(.*?)\s*\z/om
|
2003-07-23 12:51:36 -04:00
|
|
|
field, value = $1, $2
|
|
|
|
field.downcase!
|
|
|
|
header[field] = [] unless header.has_key?(field)
|
|
|
|
header[field] << value
|
|
|
|
when /^\s+(.*?)\s*\z/om
|
|
|
|
value = $1
|
|
|
|
unless field
|
2010-01-10 04:33:47 -05:00
|
|
|
raise HTTPStatus::BadRequest, "bad header '#{line}'."
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
header[field][-1] << " " << value
|
|
|
|
else
|
2010-01-10 04:33:47 -05:00
|
|
|
raise HTTPStatus::BadRequest, "bad header '#{line}'."
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
header.each{|key, values|
|
|
|
|
values.each{|value|
|
|
|
|
value.strip!
|
|
|
|
value.gsub!(/\s+/, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header
|
|
|
|
end
|
|
|
|
module_function :parse_header
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Splits a header value +str+ according to HTTP specification.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def split_header_value(str)
|
2008-07-25 10:35:21 -04:00
|
|
|
str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
|
|
|
|
(?:,\s*|\Z)'xn).flatten
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
module_function :split_header_value
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses a Range header value +ranges_specifier+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def parse_range_header(ranges_specifier)
|
|
|
|
if /^bytes=(.*)/ =~ ranges_specifier
|
|
|
|
byte_range_set = split_header_value($1)
|
|
|
|
byte_range_set.collect{|range_spec|
|
|
|
|
case range_spec
|
|
|
|
when /^(\d+)-(\d+)/ then $1.to_i .. $2.to_i
|
|
|
|
when /^(\d+)-/ then $1.to_i .. -1
|
2004-08-13 00:11:30 -04:00
|
|
|
when /^-(\d+)/ then -($1.to_i) .. -1
|
2003-07-23 12:51:36 -04:00
|
|
|
else return nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :parse_range_header
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses q values in +value+ as used in Accept headers.
|
|
|
|
|
2004-10-12 08:26:39 -04:00
|
|
|
def parse_qvalues(value)
|
|
|
|
tmp = []
|
|
|
|
if value
|
|
|
|
parts = value.split(/,\s*/)
|
|
|
|
parts.each {|part|
|
2005-01-07 06:05:22 -05:00
|
|
|
if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
|
|
|
|
val = m[1]
|
2004-10-12 08:26:39 -04:00
|
|
|
q = (m[2] or 1).to_f
|
2005-01-07 06:05:22 -05:00
|
|
|
tmp.push([val, q])
|
2004-10-12 08:26:39 -04:00
|
|
|
end
|
|
|
|
}
|
2005-01-07 06:05:22 -05:00
|
|
|
tmp = tmp.sort_by{|val, q| -q}
|
|
|
|
tmp.collect!{|val, q| val}
|
2004-10-12 08:26:39 -04:00
|
|
|
end
|
|
|
|
return tmp
|
|
|
|
end
|
|
|
|
module_function :parse_qvalues
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Removes quotes and escapes from +str+
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
def dequote(str)
|
|
|
|
ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
|
|
|
|
ret.gsub!(/\\(.)/, "\\1")
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
module_function :dequote
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Quotes and escapes quotes in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def quote(str)
|
|
|
|
'"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
|
|
|
|
end
|
|
|
|
module_function :quote
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Stores multipart form data. FormData objects are created when
|
|
|
|
# WEBrick::HTTPUtils.parse_form_data is called.
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
class FormData < String
|
2013-01-25 20:12:54 -05:00
|
|
|
EmptyRawHeader = [].freeze # :nodoc:
|
|
|
|
EmptyHeader = {}.freeze # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# The name of the form data part
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
##
|
|
|
|
# The filename of the form data part
|
|
|
|
|
|
|
|
attr_accessor :filename
|
|
|
|
|
|
|
|
attr_accessor :next_data # :nodoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
protected :next_data
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Creates a new FormData object.
|
|
|
|
#
|
|
|
|
# +args+ is an Array of form data entries. One FormData will be created
|
|
|
|
# for each entry.
|
|
|
|
#
|
|
|
|
# This is called by WEBrick::HTTPUtils.parse_form_data for you
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(*args)
|
|
|
|
@name = @filename = @next_data = nil
|
|
|
|
if args.empty?
|
|
|
|
@raw_header = []
|
|
|
|
@header = nil
|
|
|
|
super("")
|
|
|
|
else
|
|
|
|
@raw_header = EmptyRawHeader
|
2009-03-05 22:56:38 -05:00
|
|
|
@header = EmptyHeader
|
2003-07-23 12:51:36 -04:00
|
|
|
super(args.shift)
|
|
|
|
unless args.empty?
|
|
|
|
@next_data = self.class.new(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Retrieves the header at the first entry in +key+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def [](*key)
|
|
|
|
begin
|
|
|
|
@header[key[0].downcase].join(", ")
|
|
|
|
rescue StandardError, NameError
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Adds +str+ to this FormData which may be the body, a header or a
|
|
|
|
# header entry.
|
|
|
|
#
|
|
|
|
# This is called by WEBrick::HTTPUtils.parse_form_data for you
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def <<(str)
|
|
|
|
if @header
|
|
|
|
super
|
|
|
|
elsif str == CRLF
|
2006-12-01 17:38:47 -05:00
|
|
|
@header = HTTPUtils::parse_header(@raw_header.join)
|
2003-07-23 12:51:36 -04:00
|
|
|
if cd = self['content-disposition']
|
|
|
|
if /\s+name="(.*?)"/ =~ cd then @name = $1 end
|
|
|
|
if /\s+filename="(.*?)"/ =~ cd then @filename = $1 end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@raw_header << str
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Adds +data+ at the end of the chain of entries
|
|
|
|
#
|
|
|
|
# This is called by WEBrick::HTTPUtils.parse_form_data for you.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def append_data(data)
|
|
|
|
tmp = self
|
|
|
|
while tmp
|
2009-03-05 22:56:38 -05:00
|
|
|
unless tmp.next_data
|
2003-07-23 12:51:36 -04:00
|
|
|
tmp.next_data = data
|
|
|
|
break
|
|
|
|
end
|
|
|
|
tmp = tmp.next_data
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Yields each entry in this FormData
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def each_data
|
|
|
|
tmp = self
|
|
|
|
while tmp
|
|
|
|
next_data = tmp.next_data
|
|
|
|
yield(tmp)
|
|
|
|
tmp = next_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Returns all the FormData as an Array
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def list
|
|
|
|
ret = []
|
|
|
|
each_data{|data|
|
2003-08-07 14:24:27 -04:00
|
|
|
ret << data.to_s
|
2003-07-23 12:51:36 -04:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# A FormData will behave like an Array
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
alias :to_ary :list
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# This FormData's body
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def to_s
|
|
|
|
String.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses the query component of a URI in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def parse_query(str)
|
|
|
|
query = Hash.new
|
|
|
|
if str
|
|
|
|
str.split(/[&;]/).each{|x|
|
2005-06-25 04:16:16 -04:00
|
|
|
next if x.empty?
|
2003-07-23 12:51:36 -04:00
|
|
|
key, val = x.split(/=/,2)
|
|
|
|
key = unescape_form(key)
|
|
|
|
val = unescape_form(val.to_s)
|
|
|
|
val = FormData.new(val)
|
|
|
|
val.name = key
|
|
|
|
if query.has_key?(key)
|
|
|
|
query[key].append_data(val)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
query[key] = val
|
|
|
|
}
|
|
|
|
end
|
|
|
|
query
|
|
|
|
end
|
|
|
|
module_function :parse_query
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Parses form data in +io+ with the given +boundary+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def parse_form_data(io, boundary)
|
2009-06-16 18:42:13 -04:00
|
|
|
boundary_regexp = /\A--#{Regexp.quote(boundary)}(--)?#{CRLF}\z/
|
2003-07-23 12:51:36 -04:00
|
|
|
form_data = Hash.new
|
2003-12-18 14:08:11 -05:00
|
|
|
return form_data unless io
|
2003-07-23 12:51:36 -04:00
|
|
|
data = nil
|
2006-12-01 17:38:47 -05:00
|
|
|
io.each_line{|line|
|
2003-07-23 12:51:36 -04:00
|
|
|
if boundary_regexp =~ line
|
|
|
|
if data
|
|
|
|
data.chop!
|
|
|
|
key = data.name
|
|
|
|
if form_data.has_key?(key)
|
|
|
|
form_data[key].append_data(data)
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
form_data[key] = data
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
data = FormData.new
|
|
|
|
next
|
|
|
|
else
|
|
|
|
if data
|
|
|
|
data << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return form_data
|
|
|
|
end
|
|
|
|
module_function :parse_form_data
|
|
|
|
|
|
|
|
#####
|
|
|
|
|
|
|
|
reserved = ';/?:@&=+$,'
|
|
|
|
num = '0123456789'
|
|
|
|
lowalpha = 'abcdefghijklmnopqrstuvwxyz'
|
|
|
|
upalpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
mark = '-_.!~*\'()'
|
|
|
|
unreserved = num + lowalpha + upalpha + mark
|
|
|
|
control = (0x0..0x1f).collect{|c| c.chr }.join + "\x7f"
|
|
|
|
space = " "
|
|
|
|
delims = '<>#%"'
|
|
|
|
unwise = '{}|\\^[]`'
|
|
|
|
nonascii = (0x80..0xff).collect{|c| c.chr }.join
|
|
|
|
|
2005-01-10 01:29:58 -05:00
|
|
|
module_function
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def _make_regex(str) /([#{Regexp.escape(str)}])/n end
|
2005-01-10 01:29:58 -05:00
|
|
|
def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end
|
2013-05-19 21:40:30 -04:00
|
|
|
def _escape(str, regex)
|
|
|
|
str = str.b
|
|
|
|
str.gsub!(regex) {"%%%02X" % $1.ord}
|
|
|
|
# %-escaped string should contain US-ASCII only
|
|
|
|
str.force_encoding(Encoding::US_ASCII)
|
|
|
|
end
|
|
|
|
def _unescape(str, regex)
|
|
|
|
str = str.b
|
|
|
|
str.gsub!(regex) {$1.hex.chr}
|
|
|
|
# encoding of %-unescaped string is unknown
|
|
|
|
str
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2004-07-25 22:50:18 -04:00
|
|
|
UNESCAPED = _make_regex(control+space+delims+unwise+nonascii)
|
2003-07-23 12:51:36 -04:00
|
|
|
UNESCAPED_FORM = _make_regex(reserved+control+delims+unwise+nonascii)
|
|
|
|
NONASCII = _make_regex(nonascii)
|
|
|
|
ESCAPED = /%([0-9a-fA-F]{2})/
|
2005-01-10 01:29:58 -05:00
|
|
|
UNESCAPED_PCHAR = _make_regex!(unreserved+":@&=+$,")
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Escapes HTTP reserved and unwise characters in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def escape(str)
|
|
|
|
_escape(str, UNESCAPED)
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Unescapes HTTP reserved and unwise characters in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def unescape(str)
|
|
|
|
_unescape(str, ESCAPED)
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Escapes form reserved characters in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def escape_form(str)
|
|
|
|
ret = _escape(str, UNESCAPED_FORM)
|
|
|
|
ret.gsub!(/ /, "+")
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Unescapes form reserved characters in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def unescape_form(str)
|
|
|
|
_unescape(str.gsub(/\+/, " "), ESCAPED)
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Escapes path +str+
|
|
|
|
|
2005-01-10 01:29:58 -05:00
|
|
|
def escape_path(str)
|
2005-04-29 18:10:56 -04:00
|
|
|
result = ""
|
|
|
|
str.scan(%r{/([^/]*)}).each{|i|
|
|
|
|
result << "/" << _escape(i[0], UNESCAPED_PCHAR)
|
|
|
|
}
|
|
|
|
return result
|
2005-01-10 01:29:58 -05:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Escapes 8 bit characters in +str+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def escape8bit(str)
|
|
|
|
_escape(str, NONASCII)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|