Module: WEBrick::HTTPUtils

Defined in:
lib/webrick/httputils.rb

Defined Under Namespace

Classes: FormData

Constant Summary collapse

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+space+delims+unwise+nonascii)
UNESCAPED_FORM =
_make_regex(reserved+control+delims+unwise+nonascii)
NONASCII =
_make_regex(nonascii)
ESCAPED =
/%([0-9a-fA-F]{2})/
UNESCAPED_PCHAR =
_make_regex!(unreserved+":@&=+$,")

Instance Method Summary collapse

Instance Method Details

#_escape(str, regex) ⇒ Object



352
# File 'lib/webrick/httputils.rb', line 352

def _escape(str, regex) str.gsub(regex){ "%%%02X" % $1[0] } end

#_make_regex(str) ⇒ Object



350
# File 'lib/webrick/httputils.rb', line 350

def _make_regex(str) /([#{Regexp.escape(str)}])/n end

#_make_regex!(str) ⇒ Object



351
# File 'lib/webrick/httputils.rb', line 351

def _make_regex!(str) /([^#{Regexp.escape(str)}])/n end

#_unescape(str, regex) ⇒ Object



353
# File 'lib/webrick/httputils.rb', line 353

def _unescape(str, regex) str.gsub(regex){ $1.hex.chr } end

#dequote(str) ⇒ Object

###



189
190
191
192
193
# File 'lib/webrick/httputils.rb', line 189

def dequote(str)
  ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
  ret.gsub!(/\\(.)/, "\\1")
  ret
end

#escape(str) ⇒ Object



361
362
363
# File 'lib/webrick/httputils.rb', line 361

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

#escape8bit(str) ⇒ Object



387
388
389
# File 'lib/webrick/httputils.rb', line 387

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

#escape_form(str) ⇒ Object



369
370
371
372
373
# File 'lib/webrick/httputils.rb', line 369

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

#escape_path(str) ⇒ Object



379
380
381
382
383
384
385
# File 'lib/webrick/httputils.rb', line 379

def escape_path(str)
  result = ""
  str.scan(%r{/([^/]*)}).each{|i|
    result << "/" << _escape(i[0], UNESCAPED_PCHAR)
  }
  return result
end

#load_mime_types(file) ⇒ Object

Load Apache compatible mime.types file.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/webrick/httputils.rb', line 93

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) ⇒ Object



109
110
111
112
113
# File 'lib/webrick/httputils.rb', line 109

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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File '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_form_data(io, boundary) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/webrick/httputils.rb', line 306

def parse_form_data(io, boundary)
  boundary_regexp = /\A--#{boundary}(--)?#{CRLF}\z/
  form_data = Hash.new
  return form_data unless io
  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

#parse_header(raw) ⇒ Object

###



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

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 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) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/webrick/httputils.rb', line 285

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) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/webrick/httputils.rb', line 169

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) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/webrick/httputils.rb', line 154

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) ⇒ Object



196
197
198
# File 'lib/webrick/httputils.rb', line 196

def quote(str)
  '"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
end

#split_header_value(str) ⇒ Object



148
149
150
151
# File 'lib/webrick/httputils.rb', line 148

def split_header_value(str)
  str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
                (?:,\s*|\Z)'xn).flatten
end

#unescape(str) ⇒ Object



365
366
367
# File 'lib/webrick/httputils.rb', line 365

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

#unescape_form(str) ⇒ Object



375
376
377
# File 'lib/webrick/httputils.rb', line 375

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