Qore WebUtil Module Reference  1.2
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 (C) 2013 - 2014 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.11 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 
65 namespace WebUtil {
69 
70 public:
71  private :
73  Program p;
74 
75 public:
76 
77  public :
80 
81 public:
82 
85 
86 
88 
114  static string add(Program p, string fn, string name, string src);
115 
117 
118 private:
119  static string getCode(bool bare_refs, string fmt);
120 public:
121 
122 
124 
125 private:
126  static bool doBlock(string end, reference src, string type, reference i, reference l);
127 public:
128 
129 
131  static string getContentType(string name);
132  };
133 
136 
137 public:
138  private :
140  int po;
141 
143  string path;
144 
146  string ct;
147 
149  const TemplateFunc = "t";
150 
152  *code psetup;
153 
155  RWLock rwl();
156 
159 
160 public:
161 
162  public :
163 
164 public:
165 
167 
171  constructor(string path, int po = DefaultProgramOptions, *code psetup);
172 
173 
175 
190  hash render(date mtime, hash ctx, int code = 200, *hash hdr);
191 
192 
193  private setupTemplateIntern(date mtime);
194 
195  };
196 
198 
202 
203 public:
204  private :
206  RWLock rwl();
207 
210 
212  int po;
213 
215  *code psetup;
216 
217 public:
218 
220 
225 
226 
228 
245  hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
246 
247 
249  purge(string tname);
250 
251  };
252 
254 
260 
261 public:
262  private :
265 
267  Sequence seq();
268 
269 public:
270 
273 
274 
276 
301  string add(string name, string src, *string ct);
302 
303 
305  bool hasTemplate(string tname);
306 
307 
309 
326  hash render(string tname, hash ctx, int code = 200, *hash hdr);
327 
328 
330 
345  *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
346 
347 
350 
351 
354 
355  };
356 
359 
360 public:
361  public :
363  string file_root;
364 
366  softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
367 
369  hash template_extensions = Defaults.TemplateExtensions;
370 
372  *string default_target;
373 
375 
380  softint error_level = 0;
381 
383  const Unix = (PlatformOS != "Windows");
384 
386  const DirSep = (Unix ? "/" : "\\");
387 
389  const Defaults = (
390  // default index file
391  "IndexFile": "index.html",
392  "IndexTemplate": "index.qhtml",
393  "TemplateExtensions": (
394  "qhtml": True,
395  ),
396  );
397 
399  const Dirlisting = "<html lang=\"en\" >"
400 "{%"
401 " Dir $d();"
402 " $d.chdir($ctx.path);"
403 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
404 "%}"
405 " <head>"
406 " <meta charset=\"utf-8\" />"
407 " <title>{{ $title }}</title>"
408 " </head>"
409 " <body>"
410 " <header>"
411 " <h1>{{ $title }}</h1>"
412 " </header>"
413 " <div class=\"container\">"
414 " <table>"
415 " <tr>"
416 " <th>Name</th>"
417 " <th width=\"40\">Type</th>"
418 " <th width=\"80\">Size</th>"
419 " </tr>"
420 " {% if ($ctx.parent_url) { %}"
421 " <tr>"
422 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
423 " </tr>"
424 " {% } %}"
425 " {% foreach string $dir in (sort($d.listDirs())) { %}"
426 " {% if ($dir[0]!=\".\") {"
427 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
428 " %}"
429 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
430 " {% } %}"
431 " {% } %}"
432 " {% foreach string $file in (sort($d.listFiles())) { %}"
433 " {% if ($file[0]!=\".\") {"
434 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
435 " %}"
436 " <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>"
437 " {% } %}"
438 " {% } %}"
439 " </table>"
440 " </div>"
441 " </body>"
442 "</html>";
443 
444 public:
445 
446  private :
449 
450 public:
451 
453 
462  constructor(string file_root, string url_root = "/", *hash opt);
463 
464 
465  private string getDirlistingTemplate();
466 
467 
469 
498  hash handleRequest(HttpListener listener, Socket s, hash cx, hash hdr, *data body);
499 
500 
502  logInfo(string fmt);
503 
504 
506  logError(string fmt);
507 
508 
510  logDebug(string fmt);
511 
512 
514 
516  private hash unhandledRequest(hash cx, hash hdr, *data body);
517 
518 
520 
522  private hash fileError(hash cx, hash sh);
523 
524 
526 
528  private hash serverError(hash cx, hash ex);
529 
530 
531  private *hash tryServeRequest(hash cx);
532 
533 
535 
538  *hash renderDirectory(hash cx, string path);
539 
540 
542 
572  private *hash handleRequestImpl(reference cx, hash hdr, *data body);
573 
574  };
575 };
const PO_NO_USER_CLASSES
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:399
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:380
*date mtime
file's modification time
Definition: WebUtil.qm.dox.h:158
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:135
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:209
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:152
private hash unhandledRequest(hash cx, hash hdr, *data body)
this method returns a 404 "Not Found" error code to GET requests and a 501 "Not Implemented" error co...
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:146
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 "Internal Server Error" error code when an exception occurs ...
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:140
const True
private *hash handleRequestImpl(reference cx, hash hdr, *data body)
this method is called by this class'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:68
this class manages templates based on files
Definition: WebUtil.qm.dox.h:201
*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 "q" in the extensions, assuming a template
const TemplateFunc
the name of the template function
Definition: WebUtil.qm.dox.h:149
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:389
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 "Bad Request" error code when a file should be served that's not a regular ...
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:372
const PO_REQUIRE_OUR
list list(...)
hash handleRequest(HttpListener listener, Socket s, hash cx, hash hdr, *data body)
this method calls handleRequestImpl() to service the request, if handleRequestImpl() returns NOTHING...
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:358
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:215
static string getCode(bool bare_refs, string fmt)
a helper method that strips dollar signs from code when PO_ALLOW_BARE_REFS is set in the template pro...
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:369
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:212
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:366
A container for holding static text templates (ie that do not change once created in the template man...
Definition: WebUtil.qm.dox.h:259
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:143
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:386
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:363
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:383
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:73
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:79
const PO_NO_TOP_LEVEL_STATEMENTS
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:264
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
the WebUtil namespace contains all the objects in the WebUtil module
Definition: WebUtil.qm.dox.h:66