class WebApp

Copyright © 2004, 2005, 2006 Tanaka Akira. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copyright © 2006 Tanaka Akira. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Public Class Methods

reluri(hash={}) click to toggle source

calls #reluri using the webapp object currently processing.

# File webapp.rb, line 290
def WebApp.reluri(hash={})
  WebApp.get_thread_webapp_object.reluri(hash)
end

Public Instance Methods

<<(str) click to toggle source
# File webapp.rb, line 135
def <<(str) @response_body << str end
add_header(field_name, field_body) click to toggle source
# File webapp.rb, line 160
def add_header(field_name, field_body) @response_header.add(field_name, field_body) end
charset() click to toggle source
# File webapp.rb, line 142
def charset() @response_charset end
charset=(cs) click to toggle source
# File webapp.rb, line 141
def charset=(cs) @response_charset = cs end
check_last_modified(last_modified) { || ... } click to toggle source
# File webapp.rb, line 233
def check_last_modified(last_modified)
  if ims = @request_header['If-Modified-Since'] and
     ((ims = Time.httpdate(ims)) rescue nil) and
     last_modified <= ims
    @response.status_line = '304 Not Modified'
    return
  end
  @response_header.set 'Last-Modified', last_modified.httpdate
  yield
end
clear_header() click to toggle source
# File webapp.rb, line 162
def clear_header() @response_header.clear end
content_type() click to toggle source
# File webapp.rb, line 172
def content_type
  @response_header['Content-Type']
end
content_type=(media_type) click to toggle source
# File webapp.rb, line 169
def content_type=(media_type)
  @response_header.set 'Content-Type', media_type
end
each_header() { |field_name, field_body| ... } click to toggle source
# File webapp.rb, line 165
def each_header(&block) # :yields: field_name, field_body
  @response_header.each(&block)
end
each_request_header() { |field_name, field_body| ... } click to toggle source
# File webapp.rb, line 144
def each_request_header(&block) # :yields: field_name, field_body
  @request_header.each(&block)
end
get_header(field_name) click to toggle source
# File webapp.rb, line 164
def get_header(field_name) @response_header[field_name] end
get_request_header(field_name) click to toggle source
# File webapp.rb, line 147
def get_request_header(field_name) @request_header[field_name] end
has_header?(field_name) click to toggle source
# File webapp.rb, line 163
def has_header?(field_name) @response_header.has?(field_name) end
make_absolute_uri(:script=>string, :path_info=>string, :query=>query, :fragment=>string) → URI click to toggle source

#make_absolute_uri returns a absolute URI which base URI is the URI of the web application is invoked.

The argument is same as make_relative_uri.

# File webapp.rb, line 301
def make_absolute_uri(hash={})
  @urigen.make_absolute_uri(hash)
end
reluri(:script=>string, :path_info=>string, :query=>query, :fragment=>string) → URI click to toggle source
make_relative_uri(:script=>string, :path_info=>string, :query=>query, :fragment=>string) → URI
make_relative_uri returns a relative URI which base URI is the URI the
web application is invoked.

The argument should be a hash which may have following components.
- :script specifies script_name relative from the directory containing
  the web application script.
  If it is not specified, the web application itself is assumed.
- :path_info specifies path_info component for calling web application.
  It should begin with a slash.
  If it is not specified, "" is assumed.
- :query specifies query a component.
  It should be a Hash or a WebApp::QueryString.
- :fragment specifies a fragment identifier.
  If it is not specified, a fragment identifier is not appended to
  the result URL.

Since the method escapes the components properly,
you should specify them in unescaped form.

In the example follow, assume that the web application bar.cgi is invoked
as http://host/foo/bar.cgi/baz/qux.

  webapp.reluri(:path_info=>"/hoge") => URI("../hoge")
  webapp.reluri(:path_info=>"/baz/fuga") => URI("fuga")
  webapp.reluri(:path_info=>"/baz/") => URI("./")
  webapp.reluri(:path_info=>"/") => URI("../")
  webapp.reluri() => URI("../../bar.cgi")
  webapp.reluri(:script=>"funyo.cgi") => URI("../../funyo.cgi")
  webapp.reluri(:script=>"punyo/gunyo.cgi") => URI("../../punyo/gunyo.cgi")
  webapp.reluri(:script=>"../genyo.cgi") => URI("../../../genyo.cgi")
  webapp.reluri(:fragment=>"sec1") => URI("../../bar.cgi#sec1")

)

webapp.reluri(:path_info=>"/h?#o/x y") => URI("../h%3F%23o/x%20y")
webapp.reluri(:script=>"ho%o.cgi") => URI("../../ho%25o.cgi")
webapp.reluri(:fragment=>"sp ce") => URI("../../bar.cgi#sp%20ce")
# File webapp.rb, line 284
def make_relative_uri(hash={})
  @urigen.make_relative_uri(hash)
end
Also aliased as: reluri
open_resource(path) click to toggle source
open_resource(path) {|io| ... }

opens path as relative from a web application directory.

# File webapp.rb, line 200
def open_resource(path, &block) 
  resource_path(path).open(&block)
end
path_info() click to toggle source
# File webapp.rb, line 153
def path_info() @request.path_info end
print(*strs) click to toggle source
printf(fmt, *args) click to toggle source
# File webapp.rb, line 137
def printf(fmt, *args) @response_body.printf(fmt, *args) end
putc(ch) click to toggle source
# File webapp.rb, line 138
def putc(ch) @response_body.putc ch end
puts(*strs) click to toggle source
# File webapp.rb, line 139
def puts(*strs) @response_body.puts(*strs) end
query_html_get_application_x_www_form_urlencoded() click to toggle source
# File webapp.rb, line 392
def query_html_get_application_x_www_form_urlencoded
  @request.query_string.decode_as_application_x_www_form_urlencoded
end
query_html_post_application_x_www_form_urlencoded() click to toggle source
# File webapp.rb, line 396
def query_html_post_application_x_www_form_urlencoded
  if /\Apost\z/i =~ @request.request_method # xxx: should not check?
    q = QueryString.primitive_new_for_raw_query_string(@request.body_object.read)
    q.decode_as_application_x_www_form_urlencoded
  else
    # xxx: warning?
    HTMLFormQuery.new
  end
end
query_string() click to toggle source
# File webapp.rb, line 154
def query_string() @request.query_string end
reluri(hash={})
Alias for: make_relative_uri
remote_addr() click to toggle source
# File webapp.rb, line 156
def remote_addr() @request.remote_addr end
remove_header(field_name) click to toggle source
# File webapp.rb, line 161
def remove_header(field_name) @response_header.remove(field_name) end
request_content_type() click to toggle source
# File webapp.rb, line 157
def request_content_type() @request.content_type end
request_method() click to toggle source
# File webapp.rb, line 149
def request_method() @request.request_method end
resource_path(arg) click to toggle source

returns a Pathname object. path is interpreted as a relative path from the directory which a web application exists.

If /home/user/public_html/foo/bar.cgi is a web application which WebApp {} calls, webapp.resource_path(“baz”) returns a pathname points to /home/user/public_html/foo/baz.

path must not have “..” component and must not be absolute. Otherwise ArgumentError is raised.

# File webapp.rb, line 186
def resource_path(arg)
  path = Pathname.new(arg)
  raise ArgumentError, "absolute path: #{arg.inspect}" if !path.relative?
  path.each_filename {|f|
    raise ArgumentError, "path contains .. : #{arg.inspect}" if f == '..'
  }
  @manager.resource_basedir + path
end
script_name() click to toggle source
# File webapp.rb, line 152
def script_name() @request.script_name end
send_resource(path) click to toggle source

send the resource indicated by path. Last-Modified: and If-Modified-Since: header is supported.

# File webapp.rb, line 209
  def send_resource(path)
    path = resource_path(path)
    begin
      mtime = path.mtime
    rescue Errno::ENOENT
      @response.status_line = '404 Not Found'
      HTree.expand_template(@response_body) {<<'End'}
<html>
  <head><title>404 Not Found</title></head>
  <body>
    <h1>404 Not Found</h1>
    <p>Resource not found: <span _text="path"/></p>
  </body>
</html>
End
      return
    end
    check_last_modified(path.mtime) {
      path.open {|f|
        @response_body << f.read
      }
    }
  end
server_name() click to toggle source
# File webapp.rb, line 150
def server_name() @request.server_name end
server_port() click to toggle source
# File webapp.rb, line 151
def server_port() @request.server_port end
server_protocol() click to toggle source
# File webapp.rb, line 155
def server_protocol() @request.server_protocol end
set_header(field_name, field_body) click to toggle source
# File webapp.rb, line 159
def set_header(field_name, field_body) @response_header.set(field_name, field_body) end
setup_redirection(status, uri) click to toggle source

setup_redirect makes a status line and a Location header appropriate as redirection.

status specifies the status line. It should be a Fixnum 3xx or String '3xx …'.

uri specifies the Location header body. It should be a URI, String or Hash. If a Hash is given, #make_absolute_uri is called to convert to URI. If given URI is relative, it is converted as absolute URI.

# File webapp.rb, line 360
def setup_redirection(status, uri)
  case status
  when Fixnum
    if status < 300 || 400 <= status
      raise ArgumentError, "unexpected status: #{status.inspect}"
    end
    status = "#{status} #{StatusMessage[status]}"
  when String
    unless /\A3\d\d(\z| )/ =~ status
      raise ArgumentError, "unexpected status: #{status.inspect}"
    end
    if status.length == 3
      status = "#{status} #{StatusMessage[status.to_i]}"
    end
  else
    raise ArgumentError, "unexpected status: #{status.inspect}"
  end
  case uri
  when URI
    uri = @urigen.base_uri + uri if uri.relative?
  when String
    uri = URI.parse(uri)
    uri = @urigen.base_uri + uri if uri.relative?
  when Hash
    uri = make_absolute_uri(uri)
  else
    raise ArgumentError, "unexpected uri: #{uri.inspect}"
  end
  @response.status_line = status
  @response_header.set 'Location', uri.to_s
end
validate_html_query(form, form_id=nil) click to toggle source
# File webapp.rb, line 408
def validate_html_query(form, form_id=nil)
  HTMLFormValidator.new(form, form_id).validate(self)
end
write(str) click to toggle source
# File webapp.rb, line 140
def write(str) @response_body.write str end