Namespace
Methods
D
L
M
N
P
Q
S
Constants
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", "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", "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", }
 
Class Public methods
dequote(str)
# File ../ruby/lib/webrick/httputils.rb, line 190
def dequote(str)
  ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
  ret.gsub!(/\(.)/, "\\1")
  ret
end
load_mime_types(file)

Load Apache compatible mime.types file.

# File ../ruby/lib/webrick/httputils.rb, line 94
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)
      next unless ext0
      next if ext0.empty?
      ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype }
    }
    hash
  }
end
mime_type(filename, mime_tab)
# File ../ruby/lib/webrick/httputils.rb, line 110
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)
# File ../ruby/lib/webrick/httputils.rb, line 21
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_header(raw)
# File ../ruby/lib/webrick/httputils.rb, line 119
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_qvalues(value)
# File ../ruby/lib/webrick/httputils.rb, line 170
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)
# File ../ruby/lib/webrick/httputils.rb, line 155
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)
# File ../ruby/lib/webrick/httputils.rb, line 197
def quote(str)
  '"' << str.gsub(/[\\"]/o, "\\\1") << '"'
end
split_header_value(str)
# File ../ruby/lib/webrick/httputils.rb, line 149
def split_header_value(str)
  str.scan(%r\G((?:"(?:\.|[^"])+?"|[^",]+)+)
                (?:,\s*|\Z)'xn).flatten
end
Instance Private methods
dequote(str)
# File ../ruby/lib/webrick/httputils.rb, line 190
def dequote(str)
  ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
  ret.gsub!(/\(.)/, "\\1")
  ret
end
load_mime_types(file)

Load Apache compatible mime.types file.

# File ../ruby/lib/webrick/httputils.rb, line 94
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)
      next unless ext0
      next if ext0.empty?
      ext0.split(/\s+/).each{ |ext| hash[ext] = mimetype }
    }
    hash
  }
end
mime_type(filename, mime_tab)
# File ../ruby/lib/webrick/httputils.rb, line 110
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)
# File ../ruby/lib/webrick/httputils.rb, line 21
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_header(raw)
# File ../ruby/lib/webrick/httputils.rb, line 119
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_qvalues(value)
# File ../ruby/lib/webrick/httputils.rb, line 170
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)
# File ../ruby/lib/webrick/httputils.rb, line 155
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)
# File ../ruby/lib/webrick/httputils.rb, line 197
def quote(str)
  '"' << str.gsub(/[\\"]/o, "\\\1") << '"'
end
split_header_value(str)
# File ../ruby/lib/webrick/httputils.rb, line 149
def split_header_value(str)
  str.scan(%r\G((?:"(?:\.|[^"])+?"|[^",]+)+)
                (?:,\s*|\Z)'xn).flatten
end