# WEBrick::HTTPUtils (Module)

In: | webrick/httputils.rb

## Methods

_escape _make_regex _unescape dequote escape escape8bit escape_form load_mime_types mime_type normalize_path parse_form_data parse_header parse_query parse_range_header quote split_header_value unescape unescape_form

## 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", "xls" => "application/vnd.ms-excel", "xml" => "text/xml", "xpm" => "image/x-xpixmap", "xwd" => "image/x-xwindowdump", "zip" => "application/zip", }
UNESCAPED | = | _make_regex(control+delims+unwise+nonascii)
UNESCAPED_FORM | = | _make_regex(reserved+control+delims+unwise+nonascii)
NONASCII | = | _make_regex(nonascii)
ESCAPED | = | /%([0-9a-fA-F]{2})/

## Classes and Modules

Class WEBrick::HTTPUtils::FormData

## Public Instance methods

normalize_path(path)

[Source]

    # File webrick/httputils.rb, line 21
        def normalize_path(path)
          raise "abnormal path `#{path}'" if path[0] != ?/
          ret = path.dup

          ret.gsub!(%{/+}o, '/')                    # //      => /

          while ret.sub!(%{/\.(/|\Z)}o, '/'); end   # /.      => /

          begin                                      # /foo/.. => /foo

            match = ret.sub!(%{/([^/]+)/\.\.(/|\Z)}o){
              if $1 == ".."
                raise "abnormal path `#{path}'"
              else
                "/"
              end
            }
          end while match

          raise "abnormal path `#{path}'" if %{/\.\.(/|\Z)} =~ ret
          ret
        end

load_mime_types(file)

Load Apache compatible mime.types file.

[Source]

    # File webrick/httputils.rb, line 101
        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)

[Source]

    # File webrick/httputils.rb, line 117
        def mime_type(filename, mime_tab)
          if suffix = (/\.(\w+)$/ =~ filename && $1)
            mtype = mime_tab[suffix.downcase]
          end
          mtype || "application/octet-stream"
        end

parse_header(raw)

[Source]

    # File webrick/httputils.rb, line 127
        def parse_header(raw)
          header = Hash.new([].freeze)
          field = nil
          raw.each{|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 "bad header '#{line.inspect}'."
              end
              header[field][-1] << " " << value
            else
              raise "bad header '#{line.inspect}'."
            end
          }
          header.each{|key, values|
            values.each{|value|
              value.strip!
              value.gsub!(/\s+/, " ")
            }
          }
          header
        end

split_header_value(str)

[Source]

    # File webrick/httputils.rb, line 157
        def split_header_value(str)
          str.scan(/((?:"(?:\\.|[^"])+?"|[^",]+)+)
                    (?:,\s*|\Z)/xn).collect{|v| v[0] }
        end

parse_range_header(ranges_specifier)

[Source]

    # File webrick/httputils.rb, line 163
        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

dequote(str)

[Source]

    # File webrick/httputils.rb, line 180
        def dequote(str)
          ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
          ret.gsub!(/\\(.)/, "\\1")
          ret
        end

quote(str)

[Source]

    # File webrick/httputils.rb, line 187
        def quote(str)
          '"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
        end

parse_query(str)

[Source]

    # File webrick/httputils.rb, line 276
        def parse_query(str)
          query = Hash.new
          if str
            str.split(/[&;]/).each{|x|
              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_form_data(io, boundary)

[Source]

    # File webrick/httputils.rb, line 296
        def parse_form_data(io, boundary)
          boundary_regexp = /\A--#{boundary}(--)?#{CRLF}\z/
          form_data = Hash.new
          data = nil
          io.each{|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

_make_regex(str)

[Source]

    # File webrick/httputils.rb, line 337
        def _make_regex(str) /([#{Regexp.escape(str)}])/n end

        def _escape(str, regex) str.gsub(regex){ "%%%02X" % $1[0] } end
        def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end
        module_function :_make_regex, :_escape, :_unescape

        UNESCAPED = _make_regex(control+delims+unwise+nonascii)
        UNESCAPED_FORM = _make_regex(reserved+control+delims+unwise+nonascii)
        NONASCII  = _make_regex(nonascii)
        ESCAPED   = /%([0-9a-fA-F]{2})/

        def escape(str)
          _escape(str, UNESCAPED)
        end

        def unescape(str)
          _unescape(str, ESCAPED)
        end

        def escape_form(str)
          ret = _escape(str, UNESCAPED_FORM)
          ret.gsub!(/ /, "+")
          ret
        end

        def unescape_form(str)
          _unescape(str.gsub(/\+/, " "), ESCAPED)
        end

        def escape8bit(str)
          _escape(str, NONASCII)
        end

        module_function :escape, :unescape, :escape_form, :unescape_form,
                        :escape8bit

      end

_escape(str, regex)

[Source]

    # File webrick/httputils.rb, line 338
        def _escape(str, regex) str.gsub(regex){ "%%%02X" % $1[0] } end

_unescape(str, regex)

[Source]

    # File webrick/httputils.rb, line 339
        def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end

escape(str)

[Source]

    # File webrick/httputils.rb, line 347
        def escape(str)
          _escape(str, UNESCAPED)
        end

unescape(str)

[Source]

    # File webrick/httputils.rb, line 351
        def unescape(str)
          _unescape(str, ESCAPED)
        end

escape_form(str)

[Source]

    # File webrick/httputils.rb, line 355
        def escape_form(str)
          ret = _escape(str, UNESCAPED_FORM)
          ret.gsub!(/ /, "+")
          ret
        end

unescape_form(str)

[Source]

    # File webrick/httputils.rb, line 361
        def unescape_form(str)
          _unescape(str.gsub(/\+/, " "), ESCAPED)
        end

escape8bit(str)

[Source]

    # File webrick/httputils.rb, line 365
        def escape8bit(str)
          _escape(str, NONASCII)
        end

[Validate]