Module: Web
- Defined in:
- lib/web.rb,
lib/web/cgi.rb,
lib/web/info.rb,
lib/web/mime.rb,
lib/web/wiki.rb,
lib/web/forms.rb,
lib/web/action.rb,
lib/web/parser.rb,
lib/web/runner.rb,
lib/web/upload.rb,
lib/web/session.rb,
lib/web/testing.rb,
lib/web/stringio.rb,
lib/web/template.rb,
lib/web/wiki/page.rb,
lib/web/sapi/apache.rb,
lib/web/traceoutput.rb,
lib/web/wiki/linker.rb,
lib/web/sapi/fastcgi.rb,
lib/web/sapi/webrick.rb,
lib/web/sapi/mod_ruby.rb,
lib/web/simpledispatcher.rb
Overview
Purpose
Web provides an infrastructure for building and testing web applications.
Usage
Use the Web module to interact with the client. The module delegates to a Web::CGI object, which does the actual work.
#!/usr/bin/ruby
require 'web'
Web::process do
Web["field"] # access parameters
Web.["muppet"] # access cookies
Web.session["key"] ||= "value" # get and set session values
Web.( "muppet", "cookie monster" ) # set cookies
Web.puts "something" # print 'something' out
end
If the request contained multipart/form-data, uploaded files are returned as Web::Upload objects. Other parameters are treated as strings.
Output
You write output as if it were an IO:
Web << "<h1>Hello World!</h1>"
Web.print "<p>We have lots of tricks for you.<br>"
Web.write "From kungfu to jiujitsu<br>"
Web.puts "anything and everything works</p>."
or even:
puts "hello world"
Narf buffers output by default. Buffered output allows you to set headers (cookies, redirects, status, etc.) at any point before the cgi is flushed. You can force this by calling:
Web.flush
Once the headers are sent to the client, you cannot set any more headers.
Error Handling
For cgi-scripts under Apache, one can use the ‘bin/narf’ script instead of using the ruby executable. This little c program doesn’t do much fancy, but it can trap syntax errors and send them back to the browser. I would especially appreciate security-related feedback; narf.c is contributed and I am not a c programmer.
Sessions:
Sessions provide a way of storing data between Web requests. Anything that can be marshalled can be stored in the session.
Getting and Setting values in a session
Web.session["key"] = "value"
Web.session["key"] # => "value"
Deleting session values
Web.session["key"] = "value"
Web.session["key"].delete # => "value"
Web.session["key"] # => "nil"
Iterating through session keys
Web.session.each do |key, val|
Web.puts "#{key} => #{val}<br>"
end
Resetting the Session
Reseting the session to an empty hash is simple:
Web.session.reset
Testing
Narf takes testing very seriously. I consider supporting testing to be part of the contract when writing a IO library. See Web::Testing for more details.
Defined Under Namespace
Modules: Parser, Request, SimpleDispatcher, TemplatePrinter, Testing, WritableIO Classes: Action, CGD, CGI, CaseInsensitiveHash, Error, Mime, ModNarf, 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
- SHEBANG_PATTERN =
/^#!.*?[\r\n\f]/
- NODE =
/^([\w?_]+)/
- SEPARATOR =
/^\./
- ARRAY_VAR =
/^\[(\d+)\]$/
- ARRAY =
/^\[(\d+)\]/
- VAR =
/^([\w?_]+)$/
- @@message_template =
nil
- @@docroot =
nil
Class Method Summary collapse
- .encode_objects(hash) ⇒ Object
-
.escape(string) ⇒ Object
URL-encode a string.
-
.escape_element(string, *elements) ⇒ Object
(also: escapeElement)
Escape only the tags of certain HTML elements in
string
. -
.escape_html(string) ⇒ Object
(also: escapeHTML, html_encode)
Escape special characters in HTML, namely &"<> Web::escape_html(‘Usage: foo “bar” <baz>’) # => “Usage: foo "bar" <baz>” (from cgi.rb).
- .get_cgi ⇒ Object
- .get_docroot ⇒ Object
- .get_mime_type(filename) ⇒ Object (also: get_mimetype)
- .info ⇒ Object
- .info_fileinfo(file) ⇒ Object
-
.lib_file_contents(filename, dirname = "missing") ⇒ Object
Get the contents of a lib file, aka template = Web::lib_file_contents(‘resources/template.html’ ) Uses Kernel::caller to determine base directory, use optional dirname parameter to change base directory.
-
.lib_filename(resource, dirname = "missing") ⇒ Object
this function should be useful for web apps that want to store resources with the source files in the ruby lib directories.
- .load(scriptname, options = { }) ⇒ Object
- .load_narf(scriptname, options = {}) ⇒ Object
- .load_ruby(scriptname, options = {}) ⇒ Object
-
.method_missing(method, *args, &block) ⇒ Object
Web delegates to the current cgi object.
- .print_message(title, content) ⇒ Object
- .process(options = {}, &block) ⇒ Object
-
.rfc1123_date(time) ⇒ Object
Make RFC1123 date string Web::rfc1123_date(Time.now) # => Sat, 01 Jan 2000 00:00:00 GMT.
-
.send_lib_file(filename, dirname = "missing") ⇒ Object
To send a resource gif to the client: Web::send_lib_file( ‘resources/logo.gif’ ) Uses Kernel::caller to determine base directory, use optional dirname parameter to change base directory.
- .set_cgi(cgi) ⇒ Object
-
.set_docroot(docroot) ⇒ Object
When testing, this is a useful method to tell NARF where to find your scripts.
- .typed_params ⇒ Object
-
.unescape(string) ⇒ Object
URL-decode a string.
-
.unescape_element(string, *elements) ⇒ Object
(also: unescapeElement)
Undo escaping such as that done by Web::escape_element().
-
.unescape_html(string) ⇒ Object
(also: unescapeHTML)
Unescape a string that has been HTML-escaped Web::unescape_html(“Usage: foo "bar" <baz>”) # => “Usage: foo "bar" <baz>” (from cgi.rb).
Instance Method Summary collapse
- #escape_element(string, *elements) ⇒ Object
- #escape_html(string) ⇒ Object (also: #escapeHTML, #html_encode)
- #escapeElement ⇒ Object
- #print(*args) ⇒ Object
-
#puts(*args) ⇒ Object
Append output to client with line endings.
- #rfc1123_date(time) ⇒ Object
- #unescape_element(string, *elements) ⇒ Object
- #unescape_html(string) ⇒ Object
- #unescapeElement ⇒ Object
- #unescapeHTML ⇒ Object
- #write(*args) ⇒ Object
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)
178 179 180 181 182 183 |
# File 'lib/web.rb', line 178 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><A HREF="url"></A>"
print Web::escape_element('<BR><A HREF="url"></A>', ["A", "IMG"])
# "<BR><A HREF="url"></A>"
(from cgi.rb)
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/web.rb', line 296 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 "bar" <baz>"
(from cgi.rb)
202 203 204 205 |
# File 'lib/web.rb', line 202 def Web::escape_html(string) return nil unless string string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(/</n, '<') end |
.get_docroot ⇒ Object
10 11 12 |
# File 'lib/web/testing.rb', line 10 def Web.get_docroot @@docroot end |
.get_mime_type(filename) ⇒ Object Also known as: get_mimetype
81 82 83 |
# File 'lib/web/mime.rb', line 81 def get_mime_type(filename) Mime.get_mime_type(filename) end |
.info ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/web/info.rb', line 22 def Web::info Web::clear() case Web["test"] when "multipart.html" Web::puts <<-EOF <html> <head> <title>test for multipart/formdata</title> </head> <body> <form method="post" enctype="multipart/form-data" action="test_install.cgi"> <input type="hidden" name="test" value="multipart"> <ul> <li>files <ul> <li><input type="file" name="file1"></li> <li><input type="file" name="file2"></li> </ul> </li> <li>textinput <ul> <li><input type="text" name="text1" value="Text 1"></li> <li><input type="text" name="text2" value="Text 2"></li> </ul> </li> <li>checkbox <ul> <li><input type="checkbox" name="checkbox1" value="Checkbox 1">1</li> <li><input type="checkbox" name="checkbox1" value="Checkbox 2">2</li> <li><input type="checkbox" name="checkbox1" value="Checkbox 3" checked>3</li> </ul> </li> <li>radio button <ul> <li><input type="radio" name="radio1" value="Radio 1-1">1-1</li> <li><input type="radio" name="radio1" value="Radio 1-2" checked>1-2</li> <li><input type="radio" name="radio1" value="Radio 1-3">1-3</li> <li><input type="radio" name="radio2" value="Radio 2-1">2-1</li> <li><input type="radio" name="radio2" value="Radio 2-2">2-2</li> <li><input type="radio" name="radio2" value="Radio 2-3" checked>2-3</li> </ul> </li> <li>text area <ul> <li><textarea name="textarea1">Text Area 1</textarea></li> <li><textarea name="textarea2">Text Area 2</textarea></li> </ul> <li>selections <ul> <li> <select name="select1"> <option value="a">A</option> <option>B</option> </select> </li> <li> <select name="select2"> <option value="aa">AA</option> <option selected>BB</option> </select> </li> </ul> </li> </ul> <div> <input type="submit" name="submit" value="submit"> </div> </form> </body> </html> EOF when "multipart" begin Web << '<h1>result</h1>' Web << Web::info_fileinfo( Web['file1']) + '<hr>' Web << Web::info_fileinfo( Web['file2']) + '<hr>' Web << '<table border>' %w!text1 text2 checkbox1 radio1 radio2 textarea1 textarea2 select1 select2!.each { |k| Web << "<tr><td>#{k}</td><td>#{Web[k]}</td></tr>" } Web << '</table>' rescue Exception Web << 'error:' Web << $!.to_s end when "cookie" if Web.key?( 'key' ) && Web['key'] != '' && Web.key?( 'value' ) Web.( Web['key'], Web['value'] ) end Web << '<html><head><title>WebUnit::Cookies</title></head>' Web << '<body><h1>WebUnit::Cookies</h1><table border=1>' Web << Web..keys.collect{|key| if key == '' nil else "<tr><td>#{key}</td><td>#{Web.[key]}</td></tr>" end }.compact.to_s Web << '</table><br>' Web << 'Params<table border=1>' Web << Web.keys.collect{|key| "<tr><td>#{key}</td><td>#{Web[key]}</td></tr>" }.to_s Web << '</table>' Web << "<form>" Web << '<input type="hidden" name="test" value="cookie">' Web << 'key = <input type="text" name="key" value="" size="20"><br>' Web << 'value = <input type="text" name="value" value="" size="20"><br>' Web << '<input type="submit">' Web << '</body></html>' when "response.html" Web::puts <<-EOF <html> <head> <title>WebUnit::response</title> </head> <body> <h1>WebUnit::response</h1> <hr> <form action='simple.cgi'> <table border=1> <tr><td>link</td> <td><a href=index-en.html>test</a></td></tr> <tr><td>hidden</td> <td>HIDDEN<input type='hidden' name='nhidden' value='HIDDEN'></td></tr> <tr><td>text</td> <td><input type='text' name='ntext' value='TEXT'></td></tr> <tr><td>password</td> <td><input type='password' name='npassword' value='PASSWORD'></td></tr> <tr><td>textarea</td> <td><textarea name='ntextarea' rows=4 cols=40>TEXTAREA TEXTAREA</textarea></td></tr> <tr><td>checkbox</td> <td><input type='checkbox' name='ncheckbox'> <input type='checkbox' name='ncheckbox1' value=1 checked></td> </tr> <tr><td>radio</td> <td> <input type='radio' name='nradio' value='a'>a, <input type='radio' name='nradio' value='b'>b, <input type='radio' name='nradio' value='c'>c </td> </tr> <tr><td>combo</td> <td> <select name=nselect> <option value=aaa>a <option>b </select> </td> </tr> <tr><td>list</td> <td> <select name=nselect1 size=5> <option value=aaa>a <option>b <option>c <option>d </select> </td> </tr> <tr> <td></td> <td> <!--<input type='submit' value='SUBMIT'>--> <input type='submit' value='SUBMIT'> <input type='reset' value='RESET'> </td> </tr> </table> </form> </body> </html> EOF when "redirect" if Web.key?( 'redirect' ) Web.set_redirect Web['redirect'] else puts( "<html><body>", "Use test=redirect&redirect=http://... <br>", "<b>" + Web::html_encode( ENV['QUERY_STRING'] || '' ) + "</b>", "</body></html>" ) end when "stock" stock = '' tempfile = "/tmp/stock.cgi" if Web.key?( 'clear' ) File::delete( tempfile ) if File::exist?( tempfile ) else if Web.key?( 'name' ) && Web.key?( 'title' ) open( tempfile, "a" ){ |f| f.puts "#{ Web.multiple_params['name'][0] },#{ Web.multiple_params['title'][0] }" } end if File::exist?( tempfile ) open( tempfile ).each do |line| line.chop! stock << "<input type=\"text\" name=\"name\" value=\"#{ line.split( ',' )[0] }\">" stock << "<input type=\"text\" name=\"title\" value=\"#{ line.split( ',' )[1] }\">" stock << "<br>\n" end end end Web << "<html><head><title>stock.cgi</title></head><body>" Web << "<form method='POST'>" Web << "<input type=\"hidden\" name=\"test\" value=\"stock\">" Web << "<input type=\"text\" name=\"name\">" Web << "<input type=\"text\" name=\"title\">" Web << "<input type=\"submit\" value=\"add\">" Web << "<br>\n" Web << stock Web << "</form></body></html>" else msg = "" msg += "<b>" + Web.html_encode( Web.env['query_string'] || '' ) + "</b><br>" unless Web::params.empty? msg += "<h2>Web::params</h2>" msg += "<table onMouseover=\"changeto(event, '#F8FF80')\" onMouseout=\"changeback(event, '#eeeeff')\">" Web.keys.each do |key| msg += "<tr><td><b>#{k}:</b></td><td>#{Web::html_encode(v)}</td></tr>" end msg += "</table>" end msg += "<h2>Web::env</h2>" msg += "<table onMouseover=\"changeto(event, '#F8FF80')\" onMouseout=\"changeback(event, '#eeeeff')\">" Web.env.each do |k,v| msg += "<tr><td><b>#{k}:</b></td><td>#{Web::html_encode(v)}</td></tr>" end msg +="</table>" Web::( "Web::info", msg ) end end |
.info_fileinfo(file) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/web/info.rb', line 12 def Web::info_fileinfo( file) lines = '<table>' %w!original_filename read content_type!.each { |m| lines << "<tr><td>#{m}</td><td>#{file.send(m)}</td></tr>" } lines << '</table>' end |
.lib_file_contents(filename, dirname = "missing") ⇒ Object
Get the contents of a lib file, aka
template = Web::lib_file_contents('resources/template.html' )
Uses Kernel::caller to determine base directory, use optional dirname parameter to change base directory
160 161 162 163 164 |
# File 'lib/web.rb', line 160 def Web::lib_file_contents( filename, dirname="missing" ) File.open( lib_filename( filename, dirname ), 'r' ) { |f| f.read } end |
.lib_filename(resource, dirname = "missing") ⇒ Object
this function should be useful for web apps that want to store resources with the source files in the ruby lib directories. For example:
Web::lib_filename('resources/logo.gif' )
Uses Kernel::caller to determine base directory, use optional dirname parameter to change base directory
147 148 149 150 151 152 153 154 |
# File 'lib/web.rb', line 147 def Web::lib_filename( resource, dirname="missing" ) if dirname == "missing" dirname = caller[1] dirname =~ Web::CGI::TRACE_PATTERN dirname = File.dirname($1) end File.join( dirname, resource ) end |
.load(scriptname, options = { }) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/web/runner.rb', line 16 def Web::load( scriptname, = { } ) if ARGV.first == '--ruby' [:style] ||= :ruby ARGV.shift end if (scriptname == ARGV.first ) ARGV.shift end if (scriptname =~ /\.rb$/i) [:style] ||= :ruby elsif (scriptname =~ /\.narf$/i) [:style] ||= :narf elsif (scriptname =~ /\.rhtml$/i) [:style] ||= :narf end [:style] ||= :narf case [:style] when :narf Web::load_narf( scriptname, ) when :ruby Web::load_ruby( scriptname, ) end end |
.load_narf(scriptname, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/web/runner.rb', line 53 def Web::load_narf( scriptname, = {} ) Web::process() do begin raise "Web::load -- no scriptname to run (nil)" unless scriptname narf_src = File.open(scriptname,'r'){|f|f.read} ruby_src = narf_src.gsub( SHEBANG_PATTERN, '' ) script = PHPRB.new(ruby_src) script.filename = scriptname Web::puts( script.result ) # print out html on syntax error rescue SyntaxError => err if ([:testing]) raise err else = err. # munging message is hard, and I am afraid Web::( err.class.to_s, "<table><tr><td><pre>#{}</pre></td></tr></table>" ) end end end end |
.load_ruby(scriptname, options = {}) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/web/runner.rb', line 44 def Web::load_ruby( scriptname, = {} ) Web::process() do raise "Web::load -- no scriptname to run (nil)" unless scriptname Kernel::load( scriptname ) end 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.
123 124 125 |
# File 'lib/web.rb', line 123 def method_missing(method, *args, &block) CGI.get_cgi.send(method,*args, &block) end |
.print_message(title, content) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/web/info.rb', line 4 def Web::( title, content ) @@message_template ||= lib_file_contents('resources/message_template.html' ) Web::puts( @@message_template.gsub( /\$title\$/, title ).gsub( /\$content\$/, content ) ) end |
.process(options = {}, &block) ⇒ Object
135 136 137 |
# File 'lib/web.rb', line 135 def process(={}, &block) CGI.process(, &block) end |
.rfc1123_date(time) ⇒ Object
Make RFC1123 date string
Web::rfc1123_date(Time.now) # => Sat, 01 Jan 2000 00:00:00 GMT
343 344 345 346 347 348 |
# File 'lib/web.rb', line 343 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 |
.send_lib_file(filename, dirname = "missing") ⇒ Object
To send a resource gif to the client:
Web::send_lib_file( 'resources/logo.gif' )
Uses Kernel::caller to determine base directory, use optional dirname parameter to change base directory
170 171 172 |
# File 'lib/web.rb', line 170 def Web::send_lib_file( filename, dirname = "missing" ) Web::get_cgi::send_file( Web::lib_filename( filename, dirname ) ) end |
.set_cgi(cgi) ⇒ Object
127 128 129 |
# File 'lib/web.rb', line 127 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_params ⇒ Object
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)
190 191 192 193 194 195 |
# File 'lib/web.rb', line 190 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")
# "<BR><A HREF="url"></A>"
print Web::unescape_element(
Web::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
# "<BR><A HREF="url"></A>"
(from cgi.rb)
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/web.rb', line 319 def Web::unescape_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::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 "bar" <baz>")
# => "Usage: foo \"bar\" <baz>"
(from cgi.rb)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/web.rb', line 245 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 |
Instance Method Details
#escape_element(string, *elements) ⇒ Object
225 226 227 |
# File 'lib/web.rb', line 225 def escape_element(string, *elements) Web::escape_element(string, *elements) end |
#escape_html(string) ⇒ Object Also known as: escapeHTML, html_encode
219 220 221 |
# File 'lib/web.rb', line 219 def escape_html(string) Web::escape_html(string) end |
#escapeElement ⇒ Object
234 235 236 |
# File 'lib/web.rb', line 234 def escape_element(string, *elements) Web::escape_element(string, *elements) end |
#print(*args) ⇒ Object
213 214 215 |
# File 'lib/web.rb', line 213 def print(*args) Web::get_cgi.print(*args) end |
#puts(*args) ⇒ Object
Append output to client with line endings
210 211 212 |
# File 'lib/web.rb', line 210 def puts(*args) Web::get_cgi.puts(*args) end |
#rfc1123_date(time) ⇒ Object
237 238 239 |
# File 'lib/web.rb', line 237 def rfc1123_date(time) Web::rfc1123_date(time) end |
#unescape_element(string, *elements) ⇒ Object
228 229 230 |
# File 'lib/web.rb', line 228 def unescape_element(string, *elements) Web::unescape_element(string, *elements) end |
#unescape_html(string) ⇒ Object
222 223 224 |
# File 'lib/web.rb', line 222 def unescape_html(string) Web::unescape_html(string) end |
#unescapeElement ⇒ Object
235 236 237 |
# File 'lib/web.rb', line 235 def unescape_element(string, *elements) Web::unescape_element(string, *elements) end |
#unescapeHTML ⇒ Object
233 234 235 |
# File 'lib/web.rb', line 233 def unescape_html(string) Web::unescape_html(string) end |