Class Hobix::WebApp
In: lib/hobix/webapp.rb
Parent: Object

Methods

Classes and Modules

Class Hobix::WebApp::QueryString
Class Hobix::WebApp::QueryValidationFailure

Constants

NameChar = /[-A-Za-z0-9._:]/
NameExp = /[A-Za-z_:]#{NameChar}*/
XmlVersionNum = /[a-zA-Z0-9_.:-]+/
XmlVersionInfo_C = /\s+version\s*=\s*(?:'(#{XmlVersionNum})'|"(#{XmlVersionNum})")/
XmlVersionInfo = XmlVersionInfo_C.disable_capture
XmlEncName = /[A-Za-z][A-Za-z0-9._-]*/
XmlEncodingDecl_C = /\s+encoding\s*=\s*(?:"(#{XmlEncName})"|'(#{XmlEncName})')/
XmlEncodingDecl = XmlEncodingDecl_C.disable_capture
XmlSDDecl_C = /\s+standalone\s*=\s*(?:'(yes|no)'|"(yes|no)")/
XmlSDDecl = XmlSDDecl_C.disable_capture
XmlDecl_C = /<\?xml#{XmlVersionInfo_C}#{XmlEncodingDecl_C}?#{XmlSDDecl_C}?\s*\?>/
XmlDecl = /<\?xml#{XmlVersionInfo}#{XmlEncodingDecl}?#{XmlSDDecl}?\s*\?>/
SystemLiteral_C = /"([^"]*)"|'([^']*)'/
PubidLiteral_C = %r{"([\sa-zA-Z0-9\-'()+,./:=?;!*\#@$_%]*)"|'([\sa-zA-Z0-9\-()+,./:=?;!*\#@$_%]*)'}
ExternalID_C = /(?:SYSTEM|PUBLIC\s+#{PubidLiteral_C})(?:\s+#{SystemLiteral_C})?/
DocType_C = /<!DOCTYPE\s+(#{NameExp})(?:\s+#{ExternalID_C})?\s*(?:\[.*?\]\s*)?>/m
DocType = DocType_C.disable_capture
WebAPPDevelopHost = ENV['WEBAPP_DEVELOP_HOST']

Public Instance methods

[Source]

    # File lib/hobix/webapp.rb, line 69
69:   def <<(str) @response_body << str end

[Source]

     # File lib/hobix/webapp.rb, line 94
 94:   def _GET() 
 95:     unless @_get_vars
 96:       @_get_vars = {}
 97:       query_html_get_application_x_www_form_urlencoded.each do |k, v|
 98:         v.gsub!( /\r\n/, "\n" ) if defined? v.gsub!
 99:         @_get_vars[k] = v
100:       end
101:     end
102:     @_get_vars
103:   end

[Source]

     # File lib/hobix/webapp.rb, line 105
105:   def _POST()
106:     unless @_post_vars
107:       @_post_vars = {}
108:       query_html_post_application_x_www_form_urlencoded.each do |k, v|
109:         v.gsub!( /\r\n/, "\n" ) if defined? v.gsub!
110:         @_post_vars[k] = v
111:       end
112:     end
113:     @_post_vars
114:   end
absuri(hash={})

Alias for make_absolute_uri

[Source]

    # File lib/hobix/webapp.rb, line 92
92:   def action_uri() @request.action_uri end

[Source]

     # File lib/hobix/webapp.rb, line 117
117:   def add_header(field_name, field_body) @response_header.add(field_name, field_body) end

[Source]

     # File lib/hobix/webapp.rb, line 211
211:   def check_last_modified(last_modified)
212:     if ims = @request_header['If-Modified-Since'] and
213:        ((ims = Time.httpdate(ims)) rescue nil) and
214:        last_modified <= ims
215:       @response.status_line = '304 Not Modified'
216:       return
217:     end
218:     @response_header.set 'Last-Modified', last_modified.httpdate
219:     yield
220:   end

[Source]

     # File lib/hobix/webapp.rb, line 119
119:   def clear_header() @response_header.clear end

[Source]

     # File lib/hobix/webapp.rb, line 129
129:   def content_type
130:     @response_header['Content-Type']
131:   end

[Source]

     # File lib/hobix/webapp.rb, line 126
126:   def content_type=(media_type)
127:     @response_header.set 'Content-Type', media_type
128:   end

[Source]

     # File lib/hobix/webapp.rb, line 122
122:   def each_header(&block) # :yields: field_name, field_body
123:     @response_header.each(&block)
124:   end

[Source]

    # File lib/hobix/webapp.rb, line 76
76:   def each_request_header(&block) # :yields: field_name, field_body
77:     @request_header.each(&block)
78:   end

[Source]

     # File lib/hobix/webapp.rb, line 121
121:   def get_header(field_name) @response_header[field_name] end

[Source]

    # File lib/hobix/webapp.rb, line 79
79:   def get_request_header(field_name) @request_header[field_name] end

[Source]

     # File lib/hobix/webapp.rb, line 120
120:   def has_header?(field_name) @response_header.has?(field_name) end

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.

[Source]

     # File lib/hobix/webapp.rb, line 274
274:   def make_absolute_uri(hash={})
275:     @urigen.make_absolute_uri(hash)
276:   end
 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")

[Source]

     # File lib/hobix/webapp.rb, line 262
262:   def make_relative_uri(hash={})
263:     @urigen.make_relative_uri(hash)
264:   end

opens path as relative from a web application directory.

[Source]

     # File lib/hobix/webapp.rb, line 157
157:   def open_resource(path, &block) 
158:     resource_path(path).open(&block)
159:   end

[Source]

    # File lib/hobix/webapp.rb, line 86
86:   def path_info() @request.path_info end

[Source]

    # File lib/hobix/webapp.rb, line 70
70:   def print(*strs) @response_body.print(*strs) end

[Source]

    # File lib/hobix/webapp.rb, line 71
71:   def printf(fmt, *args) @response_body.printf(fmt, *args) end

[Source]

    # File lib/hobix/webapp.rb, line 72
72:   def putc(ch) @response_body.putc ch end

[Source]

    # File lib/hobix/webapp.rb, line 73
73:   def puts(*strs) @response_body.puts(*strs) end

[Source]

     # File lib/hobix/webapp.rb, line 366
366:   def query_html_get_application_x_www_form_urlencoded
367:     @request.query_string.decode_as_application_x_www_form_urlencoded
368:   end

[Source]

     # File lib/hobix/webapp.rb, line 370
370:   def query_html_post_application_x_www_form_urlencoded
371:     if /\Apost\z/i =~ @request.request_method # xxx: should not check?
372:       q = QueryString.primitive_new_for_raw_query_string(@request.body_object.read)
373:       if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(request_content_type)
374:         boundary = $1.dup 
375:         q.decode_as_multipart_form_data boundary
376:       else
377:         q.decode_as_application_x_www_form_urlencoded
378:       end
379:     else
380:       # xxx: warning?
381:       HTMLFormQuery.new
382:     end
383:   end

[Source]

    # File lib/hobix/webapp.rb, line 87
87:   def query_string() @request.query_string end
reluri(hash={})

Alias for make_relative_uri

[Source]

    # File lib/hobix/webapp.rb, line 89
89:   def remote_addr() @request.remote_addr end

[Source]

     # File lib/hobix/webapp.rb, line 118
118:   def remove_header(field_name) @response_header.remove(field_name) end

[Source]

    # File lib/hobix/webapp.rb, line 82
82:   def request_body() @request_body.string end

[Source]

    # File lib/hobix/webapp.rb, line 90
90:   def request_content_type() @request.content_type end

[Source]

    # File lib/hobix/webapp.rb, line 81
81:   def request_method() @request.request_method end

[Source]

    # File lib/hobix/webapp.rb, line 91
91:   def request_uri() @request.request_uri end

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.

[Source]

     # File lib/hobix/webapp.rb, line 143
143:   def resource_path(arg)
144:     path = Pathname.new(arg)
145:     raise ArgumentError, "absolute path: #{arg.inspect}" if !path.relative?
146:     path.each_filename {|f|
147:       raise ArgumentError, "path contains .. : #{arg.inspect}" if f == '..'
148:     }
149:     @manager.resource_basedir + path
150:   end

[Source]

    # File lib/hobix/webapp.rb, line 85
85:   def script_name() @request.script_name end

[Source]

     # File lib/hobix/webapp.rb, line 181
181:   def send_not_found(msg)
182:     @response.status_line = '404 Not Found'
183:     @response_body << "<html>\n  <head><title>404 Not Found</title></head>\n  <body>\n    <h1>404 Not Found</h1>\n    <p>\#{msg}</p>\n    <hr />\n    <small><a href=\"http://hobix.com/\">hobix</a> \#{ Hobix::VERSION } / <a href=\"http://docs.hobix.com\">docs</a> / <a href=\"http://let.us.all.hobix.com\">wiki</a> / <a href=\"http://google.com/search?q=hobix+\#{ URI.escape action_uri }\">search google for this action</a></small>\n  </body>\n</html>\n"
184:   end

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

[Source]

     # File lib/hobix/webapp.rb, line 166
166:   def send_resource(path)
167:     path = resource_path(path)
168:     begin
169:       mtime = path.mtime
170:     rescue Errno::ENOENT
171:       send_not_found "Resource not found: #{path}"
172:       return
173:     end
174:     check_last_modified(path.mtime) {
175:       path.open {|f|
176:         @response_body << f.read
177:       }
178:     }
179:   end

[Source]

     # File lib/hobix/webapp.rb, line 197
197:   def send_unauthorized
198:     @response.status_line = '401 Unauthorized'
199:     @response_body << "<html>\n  <head><title>401 Unauthorized</title></head>\n  <body>\n    <h1>401 Authorized</h1>\n    <p>You lack decent credentials to enter herein.</p>\n  </body>\n</html>\n"
200:   end

[Source]

    # File lib/hobix/webapp.rb, line 83
83:   def server_name() @request.server_name end

[Source]

    # File lib/hobix/webapp.rb, line 84
84:   def server_port() @request.server_port end

[Source]

    # File lib/hobix/webapp.rb, line 88
88:   def server_protocol() @request.server_protocol end

[Source]

     # File lib/hobix/webapp.rb, line 116
116:   def set_header(field_name, field_body) @response_header.set(field_name, field_body) end

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.

[Source]

     # File lib/hobix/webapp.rb, line 334
334:   def setup_redirection(status, uri)
335:     case status
336:     when Fixnum
337:       if status < 300 || 400 <= status
338:         raise ArgumentError, "unexpected status: #{status.inspect}"
339:       end
340:       status = "#{status} #{StatusMessage[status]}"
341:     when String
342:       unless /\A3\d\d(\z| )/ =~ status
343:         raise ArgumentError, "unexpected status: #{status.inspect}"
344:       end
345:       if status.length == 3
346:         status = "#{status} #{StatusMessage[status.to_i]}"
347:       end
348:     else
349:       raise ArgumentError, "unexpected status: #{status.inspect}"
350:     end
351:     case uri
352:     when URI
353:       uri = @urigen.base_uri + uri if uri.relative?
354:     when String
355:       uri = URI.parse(uri)
356:       uri = @urigen.base_uri + uri if uri.relative?
357:     when Hash
358:       uri = make_absolute_uri(uri)
359:     else
360:       raise ArgumentError, "unexpected uri: #{uri.inspect}"
361:     end
362:     @response.status_line = status
363:     @response_header.set 'Location', uri.to_s
364:   end

[Source]

    # File lib/hobix/webapp.rb, line 74
74:   def write(str) @response_body.write str end

[Validate]