Qore WebUtil Module Reference  1.0
 All Classes Namespaces Functions Variables Groups Pages
WebUtil.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file WebUtil.qm Qore user module supprting web server operations
3 
4 /* WebUtil.qm Copyright 2013 Qore Technologies, sro
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.8 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 
33 /* Version History
34  * 2013-10-04 v1.0: David Nichols <david@qore.org>
35  + the initial version of the WebUtil module
36 */
37 
54 namespace WebUtil {
58 
59 public:
60  private :
62  Program p;
63 
64 public:
65 
66  public :
69 
70 public:
71 
74 
75 
77 
103  static string add(Program p, string fn, string name, string src);
104 
106 
107 private:
108  static bool doBlock(string end, reference src, string type, reference i, reference l);
109 public:
110 
111 
113  static string getContentType(string name);
114  };
115 
118 
119 public:
120  private :
122  int po;
123 
125  string path;
126 
128  string ct;
129 
131  const TemplateFunc = "t";
132 
134  *code psetup;
135 
137  RWLock rwl();
138 
141 
142 public:
143 
144  public :
145 
146 public:
147 
149 
153  constructor(string path, int po = DefaultProgramOptions, *code psetup);
154 
155 
157 
172  hash render(date mtime, hash ctx, int code = 200, *hash hdr);
173 
174 
175  private setupTemplateIntern(date mtime);
176 
177  };
178 
180 
184 
185 public:
186  private :
188  RWLock rwl();
189 
192 
194  int po;
195 
197  *code psetup;
198 
199 public:
200 
202 
207 
208 
210 
227  hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
228 
229 
231  purge(string tname);
232 
233  };
234 
236 
242 
243 public:
244  private :
247 
249  Sequence seq();
250 
251 public:
252 
255 
256 
258 
283  string add(string name, string src, *string ct);
284 
285 
287  bool hasTemplate(string tname);
288 
289 
291 
308  hash render(string tname, hash ctx, int code = 200, *hash hdr);
309 
310 
312 
327  *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
328 
329 
332 
333 
336 
337  };
338 
341 
342 public:
343  public :
345  string file_root;
346 
348  softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
349 
351  hash template_extensions = Defaults.TemplateExtensions;
352 
354  *string default_target;
355 
357 
362  softint error_level = 0;
363 
365  const Unix = (PlatformOS != "Windows");
366 
368  const DirSep = (Unix ? "/" : "\\");
369 
371  const Defaults = (
372  // default index file
373  "IndexFile": "index.html",
374  "IndexTemplate": "index.qhtml",
375  "TemplateExtensions": (
376  "qhtml": True,
377  ),
378  );
379 
381  const Dirlisting = "<html lang=\"en\" >"
382 "{%"
383 " Dir $d();"
384 " $d.chdir($ctx.path);"
385 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
386 "%}"
387 " <head>"
388 " <meta charset=\"utf-8\" />"
389 " <title>{{ $title }}</title>"
390 " </head>"
391 " <body>"
392 " <header>"
393 " <h1>{{ $title }}</h1>"
394 " </header>"
395 " <div class=\"container\">"
396 " <table>"
397 " <tr>"
398 " <th>Name</th>"
399 " <th width=\"40\">Type</th>"
400 " <th width=\"80\">Size</th>"
401 " </tr>"
402 " {% if ($ctx.parent_url) { %}"
403 " <tr>"
404 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
405 " </tr>"
406 " {% } %}"
407 " {% foreach string $dir in (sort($d.listDirs())) { %}"
408 " {% if ($dir[0]!=\".\") {"
409 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
410 " %}"
411 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
412 " {% } %}"
413 " {% } %}"
414 " {% foreach string $file in (sort($d.listFiles())) { %}"
415 " {% if ($file[0]!=\".\") {"
416 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
417 " %}"
418 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $file }}\">{{ $file }}</a></td><td width=\"40\">File</td><td align=\"right\">{{hstat($ctx.path + \"/\" + $file).size }}</td></tr>"
419 " {% } %}"
420 " {% } %}"
421 " </table>"
422 " </div>"
423 " </body>"
424 "</html>";
425 
426 public:
427 
428  private :
431 
432 public:
433 
435 
444  constructor(string file_root, string url_root = "/", *hash opt);
445 
446 
447  private string getDirlistingTemplate();
448 
449 
451 
480  hash handleRequest(hash cx, hash hdr, *data body);
481 
482 
484  logInfo(string fmt);
485 
486 
488  logError(string fmt);
489 
490 
492  logDebug(string fmt);
493 
494 
496 
498  private hash unhandledRequest(hash cx, hash hdr, *data body);
499 
500 
502 
504  private hash fileError(hash cx, hash sh);
505 
506 
508 
510  private hash serverError(hash cx, hash ex);
511 
512 
513  private *hash tryServeRequest(hash cx);
514 
515 
517 
520  *hash renderDirectory(hash cx, string path);
521 
522 
524 
554  private *hash handleRequestImpl(reference cx, hash hdr, *data body);
555 
556  };
557 };
const PO_NO_USER_CLASSES
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:381
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...
bool hasTemplate(string tname)
returns True if the given template exists, False if not
date date(date dt)
softint error_level
set for error info level
Definition: WebUtil.qm.dox.h:362
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:140
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:117
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:191
constructor(string path, int po=DefaultProgramOptions, *code psetup)
creates the object based on the pathname and Program options
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:134
private hash unhandledRequest(hash cx, hash hdr, *data body)
this method returns a 404 &quot;Not Found&quot; error code to GET requests and a 501 &quot;Not Implemented&quot; error co...
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:128
string add(string name, string src, *string ct)
adds a template to the object
private hash serverError(hash cx, hash ex)
this method returns a 500 &quot;Internal Server Error&quot; error code when an exception occurs ...
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:122
const True
private *hash handleRequestImpl(reference cx, hash hdr, *data body)
this method is called by this class&#39;s handleRequest() before trying to service the request automatica...
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:57
this class manages templates based on files
Definition: WebUtil.qm.dox.h:183
*hash tryRender(string tname, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument context hash if the template exists; if...
static string getContentType(string name)
returns the content type from the file name, ignores any leading &quot;q&quot; in the extensions, assuming a template
const TemplateFunc
the name of the template function
Definition: WebUtil.qm.dox.h:131
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:371
static string add(Program p, string fn, string name, string src)
adds a template function to a template Program object
private hash fileError(hash cx, hash sh)
this method returns a 400 &quot;Bad Request&quot; error code when a file should be served that&#39;s not a regula...
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:354
const PO_REQUIRE_OUR
list list(...)
const PO_NO_INHERIT_GLOBAL_VARS
constructor(int po=DefaultProgramOptions)
sets up the object
hash render(string tname, string path, date mtime, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given path and context argument
RWLock rwl()
to ensure atomicity regarding contention between setup/recompiles and rendering requests ...
this class serves files from the file system based on a root location
Definition: WebUtil.qm.dox.h:340
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:197
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:351
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:194
list getTemplateList()
returns a list of template names, an empty list is returned if there are no templates ...
softlist indexes
indexes for directories; handled in order of appearance
Definition: WebUtil.qm.dox.h:348
A container for holding static text templates (ie that do not change once created in the template man...
Definition: WebUtil.qm.dox.h:241
constructor(int po=DefaultProgramOptions)
sets up the object
string type(any arg)
const PO_NO_INHERIT_USER_FUNC_VARIANTS
*hash getTemplateHash()
returns a hash of template names, values are True or NOTHING if no templates are currently cached ...
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
*hash renderDirectory(hash cx, string path)
this method is called when a directory should be rendered
string path
the path to the template
Definition: WebUtil.qm.dox.h:125
purge(string tname)
this method can be called when a resource is requested that no longer exists in case a template is st...
Sequence seq()
Sequence for template function names.
StaticTemplateManager stm()
static templates
const DirSep
directory separator character
Definition: WebUtil.qm.dox.h:368
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:345
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:365
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:62
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:68
const PO_NO_TOP_LEVEL_STATEMENTS
hash handleRequest(hash cx, hash hdr, *data body)
this method calls handleRequestImpl() to service the request, if handleRequestImpl() returns NOTHING...
constructor(int po=TextTemplateBase::DefaultProgramOptions, *code psetup)
creates the object with optional Program options
hash render(date mtime, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
const PlatformOS
static bool doBlock(string end, reference src, string type, reference i, reference l)
a helper method used in parsing
hash render(string tname, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:246
hash hash(object obj)
RWLock rwl()
read-write lock for managing template access
const PO_NO_TERMINAL_IO
constructor(string file_root, string url_root="/", *hash opt)
create the object optionally with the given AbstractAuthenticator