Module: WEBrick::HTTPUtils
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Defined in: | lib/webrick/httputils.rb |
Overview
HTTPUtils
provides utility methods for working with the HTTP protocol.
This module is generally used internally by ::WEBrick
Constant Summary
-
DefaultMimeTypes =
Default mime types
{ "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", "js" => "application/javascript", "json" => "application/json", "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", "svg" => "image/svg+xml", "tif" => "image/tiff", "tiff" => "image/tiff", "txt" => "text/plain", "xbm" => "image/x-xbitmap", "xhtml" => "text/html", "xls" => "application/vnd.ms-excel", "xml" => "text/xml", "xpm" => "image/x-xpixmap", "xwd" => "image/x-xwindowdump", "zip" => "application/zip", }
-
ESCAPED =
Internal use only
# File 'lib/webrick/httputils.rb', line 460/%([0-9a-fA-F]{2})/
-
NONASCII =
Internal use only
# File 'lib/webrick/httputils.rb', line 459_make_regex(nonascii)
-
UNESCAPED =
Internal use only
# File 'lib/webrick/httputils.rb', line 457_make_regex(controlspacedelimsunwisenonascii)
-
UNESCAPED_FORM =
Internal use only
# File 'lib/webrick/httputils.rb', line 458_make_regex(reservedcontroldelimsunwisenonascii)
-
UNESCAPED_PCHAR =
Internal use only
# File 'lib/webrick/httputils.rb', line 461_make_regex!(unreserved":@&=$,")
Class Method Summary
-
.dequote(str)
mod_func
Removes quotes and escapes from
str
-
.escape(str)
mod_func
Escapes HTTP reserved and unwise characters in
str
-
.escape8bit(str)
mod_func
Escapes 8 bit characters in
str
-
.escape_form(str)
mod_func
Escapes form reserved characters in
str
-
.escape_path(str)
mod_func
Escapes path
str
-
.load_mime_types(file)
mod_func
Loads Apache-compatible mime.types in
file
. -
.mime_type(filename, mime_tab)
mod_func
Returns the mime type of
filename
from the list inmime_tab
. -
.normalize_path(path)
mod_func
Normalizes a request path.
-
.parse_form_data(io, boundary)
mod_func
Parses form data in
io
with the givenboundary
-
.parse_header(raw)
mod_func
Parses an HTTP header
raw
into a hash of header fields with an Array of values. -
.parse_query(str)
mod_func
Parses the query component of a URI in
str
-
.parse_qvalues(value)
mod_func
Parses q values in
value
as used in Accept headers. -
.parse_range_header(ranges_specifier)
mod_func
Parses a Range header value
ranges_specifier
-
.quote(str)
mod_func
Quotes and escapes quotes in
str
-
.split_header_value(str)
mod_func
Splits a header value
str
according to HTTP specification. -
.unescape(str)
mod_func
Unescapes HTTP reserved and unwise characters in
str
-
.unescape_form(str)
mod_func
Unescapes form reserved characters in
str
- ._escape(str, regex) mod_func Internal use only
- ._make_regex(str) mod_func Internal use only
- ._make_regex!(str) mod_func Internal use only
- ._unescape(str, regex) mod_func Internal use only
Class Method Details
._escape(str, regex) (mod_func)
# File 'lib/webrick/httputils.rb', line 444
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
._make_regex(str) (mod_func)
# File 'lib/webrick/httputils.rb', line 442
def _make_regex(str) /([#{Regexp.escape(str)}])/n end
._make_regex!(str) (mod_func)
# File 'lib/webrick/httputils.rb', line 443
def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end
._unescape(str, regex) (mod_func)
# File 'lib/webrick/httputils.rb', line 450
def _unescape(str, regex) str = str.b str.gsub!(regex) {$1.hex.chr} # encoding of %-unescaped string is unknown str end
.dequote(str) (mod_func)
Removes quotes and escapes from str
# File 'lib/webrick/httputils.rb', line 224
def dequote(str) ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup ret.gsub!(/\\(.)/, "\\1") ret end
.escape(str) (mod_func)
Escapes HTTP reserved and unwise characters in str
.escape8bit(str) (mod_func)
Escapes 8 bit characters in str
.escape_form(str) (mod_func)
Escapes form reserved characters in str
# File 'lib/webrick/httputils.rb', line 482
def escape_form(str) ret = _escape(str, UNESCAPED_FORM) ret.gsub!(/ /, "+") ret end
.escape_path(str) (mod_func)
Escapes path str
# File 'lib/webrick/httputils.rb', line 498
def escape_path(str) result = "" str.scan(%r{/([^/]*)}).each{|i| result << "/" << _escape(i[0], UNESCAPED_PCHAR) } return result end
.load_mime_types(file) (mod_func)
Loads Apache-compatible mime.types in file
.
# File 'lib/webrick/httputils.rb', line 110
def load_mime_types(file) # note: file may be a "| command" for now; some people may # rely on this, but currently we do not use this method by default. open(file){ |io| hash = Hash.new io.each{ |line| next if /^#/ =~ line line.chomp! mimetype, ext0 = line.split(/\s+/, 2) next unless ext0 next if ext0.empty? ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype } } hash } end
.mime_type(filename, mime_tab) (mod_func)
Returns the mime type of filename
from the list in mime_tab
. If no mime type was found application/octet-stream is returned.
# File 'lib/webrick/httputils.rb', line 132
def mime_type(filename, mime_tab) suffix1 = (/\.(\w+)$/ =~ filename && $1.downcase) suffix2 = (/\.(\w)\.[\w\-]$/ =~ filename && $1.downcase) mime_tab[suffix1] || mime_tab[suffix2] || "application/octet-stream" end
.normalize_path(path) (mod_func)
Normalizes a request path. Raises an exception if the path cannot be normalized.
# File 'lib/webrick/httputils.rb', line 31
def normalize_path(path) raise "abnormal path `#{path}'" if path[0] != ?/ ret = path.dup ret.gsub!(%r{/+}o, '/') # // => / while ret.sub!(%r'/\.(?:/|\Z)', '/'); end # /. => / while ret.sub!(%r'/(?!\.\./)[^/]+/\.\.(?:/|\Z)', '/'); end # /foo/.. => /foo raise "abnormal path `#{path}'" if %r{/\.\.(/|\Z)} =~ ret ret end
.parse_form_data(io, boundary) (mod_func)
Parses form data in io
with the given boundary
# File 'lib/webrick/httputils.rb', line 396
def parse_form_data(io, boundary) boundary_regexp = /\A--#{Regexp.quote(boundary)}(--)?#{CRLF}\z/ form_data = Hash.new return form_data unless io data = nil io.each_line{|line| if boundary_regexp =~ line if data data.chop! key = data.name if form_data.has_key?(key) form_data[key].append_data(data) else form_data[key] = data end end data = FormData.new next else if data data << line end end } return form_data end
.parse_header(raw) (mod_func)
Parses an HTTP header raw
into a hash of header fields with an Array of values.
# File 'lib/webrick/httputils.rb', line 143
def parse_header(raw) header = Hash.new([].freeze) field = nil raw.each_line{|line| case line when /^([A-Za-z0-9!\#$%&'*\-.^_`|~]):\s*(.*?)\s*\z/om 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 raise HTTPStatus::BadRequest, "bad header '#{line}'." end header[field][-1] << " " << value else raise HTTPStatus::BadRequest, "bad header '#{line}'." end } header.each{|key, values| values.each{|value| value.strip! value.gsub!(/\s+/, " ") } } header end
.parse_query(str) (mod_func)
Parses the query component of a URI in str
# File 'lib/webrick/httputils.rb', line 372
def parse_query(str) query = Hash.new if str str.split(/[&;]/).each{|x| next if x.empty? 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
.parse_qvalues(value) (mod_func)
Parses q values in value
as used in Accept headers.
# File 'lib/webrick/httputils.rb', line 203
def parse_qvalues(value) tmp = [] if value parts = value.split(/,\s*/) parts.each {|part| if m = %r{^([^\s,]?)(?:;\s*q=(\d(?:\.\d+)?))?$}.match(part) val = m[1] q = (m[2] or 1).to_f tmp.push([val, q]) end } tmp = tmp.sort_by{|val, q| -q} tmp.collect!{|val, q| val} end return tmp end
.parse_range_header(ranges_specifier) (mod_func)
Parses a Range header value ranges_specifier
# File 'lib/webrick/httputils.rb', line 185
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 when /^-(\d+)/ then -($1.to_i) .. -1 else return nil end } end end
.quote(str) (mod_func)
Quotes and escapes quotes in str
# File 'lib/webrick/httputils.rb', line 234
def quote(str) '"' << str.gsub(/[\\\"]/o, "\\\1") << '"' end
.split_header_value(str) (mod_func)
Splits a header value str
according to HTTP specification.
# File 'lib/webrick/httputils.rb', line 176
def split_header_value(str) str.scan(%r'\G((?:"(?:\\.|[^"])?"|[^",])+) (?:,\s*|\Z)'xn).flatten end
.unescape(str) (mod_func)
Unescapes HTTP reserved and unwise characters in str
.unescape_form(str) (mod_func)
Unescapes form reserved characters in str