Module: Web

Defined in:
lib/web.rb,
lib/web/info.rb,
lib/web/load.rb,
lib/web/mime.rb,
lib/web/unit.rb,
lib/web/wiki.rb,
lib/web/config.rb,
lib/web/upload.rb,
lib/web/session.rb,
lib/web/testing.rb,
lib/web/stringio.rb,
lib/web/validate.rb,
lib/web/wiki/page.rb,
lib/web/connection.rb,
lib/web/convenience.rb,
lib/web/sapi/apache.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/install/iis.rb,
lib/web/sapi/install/apache.rb

Defined Under Namespace

Modules: Install, Testing, Unit, WritableIO Classes: Config, Connection, Error, FastCGIConnection, FatalValidationError, Mime, ModRubyConnection, RubyRun, RubyWebHandler, Rule, Session, Template, TemplateError, Upload, WebrickConnection, Wiki

Constant Summary collapse

SHEBANG_PATTERN =
/^#!.*?[\r\n\f]/
APPLICATION =
"application.rb"
VERSION =
"1.0.0"
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 ]
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
@@message_template =
nil
@@config =
Config.new
@@display_trace =

Web::display_trace controls the display of trace variables

false

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.display_traceObject

Returns the value of attribute display_trace.



13
14
15
# File 'lib/web/traceoutput.rb', line 13

def display_trace
  @display_trace
end

Class Method Details

.argv_switches(args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/web/load.rb', line 191

def Web::argv_switches( args )
  args.shift
  switches = []
  found_doubledash = false
  args.delete_if do |e|
    unless found_doubledash
      if e =~ /(^--$)/
        found_doubledash = true
      elsif e =~ /^[^-]/
        found_doubledash = true
        false
      else
        switches.push e
      end
    end
  end
  switches
end

.configObject



46
47
48
# File 'lib/web/config.rb', line 46

def Web.config
  @@config
end

.connection(options = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/web/connection.rb', line 75

def connection( options={} )
  unless Thread.current[:web_connection]
    self.connection = Web::Connection::create(options)
  end
  
  Thread.current[:web_connection]
end

.connection=(connection) ⇒ Object



71
72
73
# File 'lib/web/connection.rb', line 71

def connection= connection
  Thread.current[:web_connection] = connection
end

.docrootObject



8
9
10
# File 'lib/web/config.rb', line 8

def Web::docroot
  Web::config[:docroot]
end

.docroot=(docroot) ⇒ Object



4
5
6
# File 'lib/web/config.rb', line 4

def Web::docroot= docroot
  Web::config[:docroot] = docroot
end

.escape(string) ⇒ Object



19
20
21
22
23
24
# File 'lib/web/convenience.rb', line 19

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/web/convenience.rb', line 107

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



35
36
37
38
# File 'lib/web/convenience.rb', line 35

def Web::escape_html(string)
  return nil unless string
  string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
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

.infoObject



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
# File 'lib/web/info.rb', line 22

def Web::info
    Web::clear()
    if ( [
           "multipart.html",
           "multipart.rb",
           "cookie.rb",
           "response.html",
           "redirect.rb",
           "counter.rb"
         ].include? Web["test"] )
      Web::load( File.dirname(__FILE__) + "/resources/test/" + Web["test"] )
  else

    
    msg = ""
 
    [:request, :get, :post, :cookie, :env].each do |namespace|
      variables = Web::send(namespace)
      unless variables.empty?    
        msg += "<h2>Web::#{namespace}</h2>"
        msg += "<table onMouseover=\"changeto(event, '#F8FF80')\" onMouseout=\"changeback(event, '#eeeeff')\">"
        variables.keys.sort.each do |key|
          msg += "<tr><td><b>#{key}</b></td><td>"
          value = variables[key]
          value = [value] unless value.kind_of? Array
          msg += value.collect do |e|
                   Web::html_encode( if e.kind_of? String
                                       e
                                     else
                                       e.inspect
                                     end )
                 end.join("<br />")
        end
        msg += "</table>"
      end
    end
    
    Web::print_message( "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



9
10
11
12
13
# File 'lib/web/convenience.rb', line 9

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



2
3
4
5
6
7
# File 'lib/web/convenience.rb', line 2

def Web::lib_filename( resource, dirname="missing" )
  if dirname == "missing"
    dirname = File.dirname( Web::parse_trace( caller[1] ).shift )
  end
  File.join( dirname, resource )
end

.load(scriptname) ⇒ Object



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
# File 'lib/web/load.rb', line 96

def Web::load( scriptname )
  Web::connection.templates ||= [ ]
  Web::connection.templates.push scriptname
  
  scriptname = Web::template_file( scriptname )
  
  unless (File.exists? scriptname)
    raise LoadError.new("Template not found: #{scriptname}")
  end

  Web::local.merge! Web::options[:local] || {}

  unless Web::application_loaded
    Web::application_loaded = true
    Web::connection.run_caller = caller
    Web::load_application(scriptname)
  end

  begin
    Web::config[:load_suffix].each do |suffix, block|
      if scriptname =~ /\.#{suffix}$/i
        return block.call( scriptname )
      end
    end
    return Web::config[:load_suffix].default.call( scriptname )
  rescue TemplateError => e
    raise e.trimmed_original
  end
end

.load_application(scriptname) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/web/load.rb', line 138

def Web::load_application(scriptname)
  search_path = File.expand_path(scriptname)
  while( search_path &&                    \
         !(search_path =~ /^([a-z]:)?\/$/i ) &&             \
         search_path != '.' )
    app = File.join( File::dirname(search_path), APPLICATION )
    
    if File.exists? app
      Web::load app
      search_path = nil
    else
      search_path = File::dirname(search_path)
    end
  end
end

.load_configObject



55
56
57
58
59
60
# File 'lib/web.rb', line 55

def load_config
  begin
    Kernel::load File.dirname(__FILE__) + "/web/site-config.rb"
  rescue LoadError
  end
end

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



67
68
69
# File 'lib/web/connection.rb', line 67

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

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



9
10
11
12
13
14
15
16
17
18
# File 'lib/web.rb', line 9

def open( options={}, &block )
  if Web::Connection::server_sniff == :fcgi
    FCGI::each_request{ |fcgi|
      options[:fcgi] = fcgi
      run( options, &block )
    }
  else
    run(options, &block )
  end
end

.parse_trace(line) ⇒ Object



149
150
151
152
# File 'lib/web/convenience.rb', line 149

def Web::parse_trace(line)
  line =~ /^((\w:){0,1}.*?):(\d+)(:?(.*))$/
  [ $1, $3, $5 ]
end


4
5
6
7
8
9
10
# File 'lib/web/info.rb', line 4

def Web::print_message( title, content )
  @@message_template ||=
           lib_file_contents('resources/message_template.html' )
  puts( @@message_template.gsub( /\$title\$/,
                                      title ).gsub( /\$content\$/,
                                                    content ) )
end

.rfc1123_date(time) ⇒ Object



142
143
144
145
146
147
# File 'lib/web/convenience.rb', line 142

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

.ruby_webObject



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
# File 'lib/web/load.rb', line 154

def Web::ruby_web()
  # remnants of narflets.. do we still want to use this?
  #Web::docroot = ENV["DOCUMENT_ROOT"]
  #$: << "#{Web::docroot}/../src"
  #$: << "#{Web::docroot}/../config"
  
  options = {}
  switches = Web::argv_switches(ARGV)
  switches.each do |arg|
    case arg
    when /^-I/
      $:.unshift arg.sub( /^-I/, '' )
    when /^--cli$/
      options[:noheader] = true
    when /^--ruby$/
      options[:style] = :ruby
    when /^--xml$/
      options[:xml] = true
    else
      #Web::print_usage
      #exit
    end
  end
  
  script = if (File.exists? ENV['PATH_TRANSLATED'].to_s)
             ENV['PATH_TRANSLATED']
           elsif(File.exists?ENV['SCRIPT_FILENAME'].to_s)
             ENV['SCRIPT_FILENAME']
           else
             ARGV[0]
           end
  
  ARGV.shift if (script == ARGV.first )

  Web::open(options) { Web::load( script ) }
end

.rule(params = {}) ⇒ Object



2
3
4
5
# File 'lib/web/validate.rb', line 2

def Web::rule( params = {} )
  Web::ruleset ||= Array.new
  Web::ruleset.push Rule.new( params )
end

.run(options = {}) ⇒ Object

:nodoc:



20
21
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
# File 'lib/web.rb', line 20

def run( options={} ) #:nodoc:
  Web::load_config
  
  Web.connection = Web::Connection.create( options )

  old_stdin, old_stdout = $stdin, $stdout
  
  begin
    $stdin, $stdout = Web::raw_post_data, Web::connection
    $> = Web::connection
    
    case Web["ruby-web_resource"]
    when "ruby-web-logo.gif"
      Web::send_lib_file('web/resources/ruby-web-logo.gif')
    when "ruby-logo.gif"
      Web::send_lib_file('web/resources/ruby-logo.gif')
    when "ruby-web-styles.css"
      Web::send_lib_file('web/resources/ruby-web-styles.css')
    when "highlight_table.js"
      Web::send_lib_file('web/resources/highlight_table.js')
    else
      yield Web.connection(options)
    end

  rescue Exception => error
    Web::report_error( error )      
  ensure
    $stdin, $stdout = old_stdin, old_stdout
    $> = old_stdout
  end

  Web.connection(options).close
  Web.connection(options)      
end

.send_lib_file(filename, dirname = "missing") ⇒ Object



15
16
17
# File 'lib/web/convenience.rb', line 15

def Web::send_lib_file( filename, dirname = "missing" )
  Web::connection::send_file( Web::lib_filename( filename, dirname ) )
end

.setup(options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/web/testing.rb', line 27

def Web::setup( options = {} )
  options[:out] ||= ''
  Web::config[:error_style] = :testing
  Web.connection = Web::Connection.new(options)
end

.teardownObject



33
34
35
# File 'lib/web/testing.rb', line 33

def Web::teardown
  Web::config[:error_style] = :default
end

.template_file(template) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/web/load.rb', line 127

def Web::template_file( template )
  template_file = template
  Web::config[:load_path].reverse.each{ |dir|
    full_template_path = File.join( dir, template )
    if (File.exists? full_template_path)
      template_file = full_template_path
    end
  }
  template_file
end

.unescape(string) ⇒ Object



27
28
29
30
31
32
# File 'lib/web/convenience.rb', line 27

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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/web/convenience.rb', line 120

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



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
# File 'lib/web/convenience.rb', line 71

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

.validateObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/web/validate.rb', line 7

def Web::validate
  problems = Array.new
  Web::ruleset.each do |r|
    unless Web[r.field] =~ r.regexp
      problems.push r
      raise Web::FatalValidationError.new(r) if (r.malformed == :fatal)
    end
  end
  problems
end

Instance Method Details

#escape_element(string, *elements) ⇒ Object



56
57
58
# File 'lib/web/convenience.rb', line 56

def escape_element(string, *elements)
  Web::escape_element(string, *elements)
end

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



50
51
52
# File 'lib/web/convenience.rb', line 50

def escape_html(string)
  Web::escape_html(string)
end

#escapeElementObject



65
66
67
# File 'lib/web/convenience.rb', line 65

def escape_element(string, *elements)
  Web::escape_element(string, *elements)
end


44
45
46
# File 'lib/web/convenience.rb', line 44

def print(*args)
  Web::connection.print(*args)
end

#puts(*args) ⇒ Object

include these methods with Web



41
42
43
# File 'lib/web/convenience.rb', line 41

def puts(*args)
  Web::connection.puts(args)
end

#rfc1123_date(time) ⇒ Object



67
68
69
# File 'lib/web/convenience.rb', line 67

def rfc1123_date(time)
  Web::rfc1123_date(time)
end

#unescape_element(string, *elements) ⇒ Object



59
60
61
# File 'lib/web/convenience.rb', line 59

def unescape_element(string, *elements)
  Web::unescape_element(string, *elements)
end

#unescape_html(string) ⇒ Object



53
54
55
# File 'lib/web/convenience.rb', line 53

def unescape_html(string)
  Web::unescape_html(string)
end

#unescapeElementObject



66
67
68
# File 'lib/web/convenience.rb', line 66

def unescape_element(string, *elements)
  Web::unescape_element(string, *elements)
end

#unescapeHTMLObject



64
65
66
# File 'lib/web/convenience.rb', line 64

def unescape_html(string)
  Web::unescape_html(string)
end

#write(*args) ⇒ Object



47
48
49
# File 'lib/web/convenience.rb', line 47

def write(*args)
  Web::connection.write(*args)
end