Module: Web

Defined in:
lib/web.rb,
lib/web/cgi.rb,
lib/web/mime.rb,
lib/web/wiki.rb,
lib/web/forms.rb,
lib/web/action.rb,
lib/web/buffer.rb,
lib/web/parser.rb,
lib/web/upload.rb,
lib/web/session.rb,
lib/web/testing.rb,
lib/web/template.rb,
lib/web/unit/form.rb,
lib/web/unit/link.rb,
lib/web/wiki/page.rb,
lib/web/unit/frame.rb,
lib/web/unit/image.rb,
lib/web/unit/table.rb,
lib/web/unit/utils.rb,
lib/web/writableio.rb,
lib/web/traceoutput.rb,
lib/web/unit/assert.rb,
lib/web/unit/cookie.rb,
lib/web/unit/params.rb,
lib/web/unit/parser.rb,
lib/web/wiki/linker.rb,
lib/web/sapi/fastcgi.rb,
lib/web/sapi/webrick.rb,
lib/web/unit/jscript.rb,
lib/web/unit/runtest.rb,
lib/web/sapi/mod_ruby.rb,
lib/web/unit/htmlelem.rb,
lib/web/unit/response.rb,
lib/web/unit/testcase.rb,
lib/web/unit/domwalker.rb,
lib/web/unit/exception.rb,
lib/web/simpledispatcher.rb,
lib/web/unit/html-parser.rb,
lib/web/unit/sgml-parser.rb,
lib/web/unit/ui/cui/testrunner.rb

Overview

WebUnit::exception

Copyright(C) 2001 yuichi TAKAHASHI
$Id: exception.rb,v 1.1.1.1 2003/01/21 11:50:40 yuichi Exp $

Defined Under Namespace

Modules: Parser, Request, SimpleDispatcher, TemplatePrinter, Testing, Unit, WritableIO Classes: Action, Buffer, CGD, CGI, CaseInsensitiveHash, Error, Mime, NarfHandler, Narflates, Node, Scope, Session, TemplateException, Upload, Wiki

Constant Summary collapse

RFC822_DAYS =
%w[ Sun Mon Tue Wed Thu Fri Sat ]
RFC822_MONTHS =
%w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
HTTP_STATUS =
{"200" => "OK",
               "206" => "Partial Content",
               "300" => "Multiple Choices",
               "301" => "Moved Permanently",
               "302" => "Found",
               "304" => "Not Modified",
               "400" => "Bad Request",
               "401" => "Authorization Required",
               "403" => "Forbidden",
               "404" => "Not Found",
               "405" => "Method Not Allowed",
               "406" => "Not Acceptable",
               "411" => "Length Required",
               "412" => "Precondition Failed",
               "500" => "Internal Server Error",
               "501" => "Method Not Implemented",
               "502" => "Bad Gateway",
               "506" => "Variant Also Negotiates"
}
TRACE_STYLESHEET =
<<-EOF
<style type="text/css">
span.tracecontent { background-color:white; color:black;font: 10pt verdana, arial; }
span.tracecontent table { font: 10pt verdana, arial; cellspacing:0; cellpadding:0; margin-bottom:25}
span.tracecontent tr.subhead { background-color:cccccc;}
span.tracecontent th { padding:0,3,0,3 }
span.tracecontent th.alt { background-color:black; color:white; padding:3,3,2,3; }
span.tracecontent td { padding:0,3,0,3 }
span.tracecontent tr.alt { background-color:eeeeee }
span.tracecontent h1 { font: 24pt verdana, arial; margin:0,0,0,0}
span.tracecontent h2 { font: 18pt verdana, arial; margin:0,0,0,0}
span.tracecontent h3 { font: 12pt verdana, arial; margin:0,0,0,0}
span.tracecontent th a { color:darkblue; font: 8pt verdana, arial; }
span.tracecontent a { color:darkblue;text-decoration:none }
span.tracecontent a:hover { color:darkblue;text-decoration:underline; }
span.tracecontent div.outer { width:90%; margin:15,15,15,15}
span.tracecontent table.viewmenu td { background-color:006699; color:white; padding:0,5,0,5; }
span.tracecontent table.viewmenu td.end { padding:0,0,0,0; }
span.tracecontent table.viewmenu a {color:white; font: 8pt verdana, arial; }
span.tracecontent table.viewmenu a:hover {color:white; font: 8pt verdana, arial; }
span.tracecontent a.tinylink {color:darkblue; font: 8pt verdana, arial;text-decoration:underline;}
span.tracecontent a.link {color:darkblue; text-decoration:underline;}
span.tracecontent div.buffer {padding-top:7; padding-bottom:17;}
span.tracecontent .small { font: 8pt verdana, arial }
span.tracecontent table td { padding-right:20 }
span.tracecontent table td.nopad { padding-right:5 }
</style>
EOF
NODE =
/^([\w?_]+)/
SEPARATOR =
/^\./
ARRAY_VAR =
/^\[(\d+)\]$/
ARRAY =
/^\[(\d+)\]/
VAR =
/^([\w?_]+)$/
@@docroot =
nil

Class Method Summary collapse

Class Method Details

.encode_objects(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/web/forms.rb', line 46

def encode_objects hash
    newhash = {}
    hash.each do |k,v|
	v = ([] << v).flatten
	v.each { |v|
	    if v.kind_of?(String)
		newhash[k] = v
	    elsif 
		newhash[k] = Web.escape(v.encode)
		newhash["#{k}-type"] = v.class.name
	    end
	}
    end
    newhash
end

.escape(string) ⇒ Object

URL-encode a string.

url_encoded_string = Web::escape("'Stop!' said Fred")
   # => "%27Stop%21%27+said+Fred"

(from cgi.rb)



186
187
188
189
190
191
# File 'lib/web.rb', line 186

def Web::escape(string)
  return nil unless string
  string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end.tr(' ', '+')
end

.escape_element(string, *elements) ⇒ Object Also known as: escapeElement

Escape only the tags of certain HTML elements in string.

Takes an element or elements or array of elements. Each element is specified by the name of the element, without angle brackets. This matches both the start and the end tag of that element. The attribute list of the open tag will also be escaped (for instance, the double-quotes surrounding attribute values).

print Web::escape_element('<BR><A HREF="url"></A>', "A", "IMG")
  # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"

print Web::escape_element('<BR><A HREF="url"></A>', ["A", "IMG"])
  # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"

(from cgi.rb)



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/web.rb', line 271

def Web::escape_element(string, *elements)
  return nil unless string
  elements = elements[0] if elements[0].kind_of?(Array)
  unless elements.empty?
    string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/ni) do
      Web::escape_html($&)
    end
  else
    string
  end
end

.escape_html(string) ⇒ Object Also known as: escapeHTML, html_encode

Escape special characters in HTML, namely &"<>

Web::escape_html('Usage: foo "bar" <baz>')
   # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"

(from cgi.rb)



210
211
212
213
# File 'lib/web.rb', line 210

def Web::escape_html(string)
  return nil unless string
  string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
end

.get_cgiObject



173
174
175
# File 'lib/web.rb', line 173

def get_cgi
  CGI.get_cgi
end

.get_docrootObject



10
11
12
# File 'lib/web/testing.rb', line 10

def Web.get_docroot
  @@docroot
end

.get_mime_type(filename) ⇒ Object



78
79
80
# File 'lib/web/mime.rb', line 78

def Web.get_mime_type( filename )
  Mime.get_mime_type( filename )
end

.method_missing(method, *args, &block) ⇒ Object

Web delegates to the current cgi object. This is the recommended way to use narf:

Web["param"]
Web << "hello world"
Web.set_redirect( "http://www.narf-lib.org" )
...

The documentation for these methods is on the Web::CGI object.



165
166
167
# File 'lib/web.rb', line 165

def method_missing(method, *args, &block)
  CGI.get_cgi.send(method,*args, &block)
end

.process(options = {}, &block) ⇒ Object



177
178
179
# File 'lib/web.rb', line 177

def process(options={}, &block)
  CGI.process(options, &block)
end

.rfc1123_date(time) ⇒ Object

Make RFC1123 date string

Web::rfc1123_date(Time.now) # => Sat, 01 Jan 2000 00:00:00 GMT


318
319
320
321
322
323
# File 'lib/web.rb', line 318

def Web::rfc1123_date(time)
  t = time.clone.gmtime
  return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
                RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
                t.hour, t.min, t.sec)
end

.set_cgi(cgi) ⇒ Object



169
170
171
# File 'lib/web.rb', line 169

def set_cgi( cgi )
  CGI.set_cgi( cgi )
end

.set_docroot(docroot) ⇒ Object

When testing, this is a useful method to tell NARF where to find your scripts



6
7
8
# File 'lib/web/testing.rb', line 6

def Web.set_docroot docroot
  @@docroot = docroot
end

.typed_paramsObject



62
63
64
# File 'lib/web/forms.rb', line 62

def typed_params
    $__web__cgi.typed_params
end

.unescape(string) ⇒ Object

URL-decode a string.

string = Web::unescape("%27Stop%21%27+said+Fred")
   # => "'Stop!' said Fred"

(from cgi.rb)



198
199
200
201
202
203
# File 'lib/web.rb', line 198

def Web::unescape(string)
  return nil unless string
  string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
    [$1.delete('%')].pack('H*')
  end
end

.unescape_element(string, *elements) ⇒ Object Also known as: unescapeElement

Undo escaping such as that done by Web::escape_element()

print Web::unescape_element(
        Web::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
  # "&lt;BR&gt;<A HREF="url"></A>"

print Web::unescape_element(
        Web::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
  # "&lt;BR&gt;<A HREF="url"></A>"

(from cgi.rb)



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/web.rb', line 294

def Web::unescape_element(string, *elements)
  return nil unless string
  elements = elements[0] if elements[0].kind_of?(Array)
  unless elements.empty?
    string.gsub(/&lt;\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?&gt;/ni) do
      Web::unescape_html($&)
    end
  else
    string
  end
end

.unescape_html(string) ⇒ Object Also known as: unescapeHTML

Unescape a string that has been HTML-escaped

Web::unescape_html("Usage: foo &quot;bar&quot; &lt;baz&gt;")
   # => "Usage: foo \"bar\" <baz>"

(from cgi.rb)



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/web.rb', line 220

def Web::unescape_html(string)
  return nil unless string
  string.gsub(/&(.*?);/n) do
    match = $1.dup
    case match
    when /\Aamp\z/ni           then '&'
    when /\Aquot\z/ni          then '"'
    when /\Agt\z/ni            then '>'
    when /\Alt\z/ni            then '<'
    when /\A#0*(\d+)\z/n       then
      if Integer($1) < 256
        Integer($1).chr
      else
        if Integer($1) < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
          [Integer($1)].pack("U")
        else
          "&##{$1};"
        end
      end
    when /\A#x([0-9a-f]+)\z/ni then
      if $1.hex < 256
        $1.hex.chr
      else
        if $1.hex < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
          [$1.hex].pack("U")
        else
          "&#x#{$1};"
        end
      end
    else
      "&#{match};"
    end
  end
end