Qore WebUtil Module Reference 1.0

- Main Page
- Related Pages
- Namespaces
- Classes

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 {
57 class TextTemplateBase {
58
59 public:
60 private :
62 Program p;
63
64 public:
65
66 public :
68 const DefaultProgramOptions = PO_REQUIRE_OUR|PO_NO_TOP_LEVEL_STATEMENTS|PO_NO_INHERIT_USER_FUNC_VARIANTS|PO_NO_INHERIT_GLOBAL_VARS|PO_NO_USER_CLASSES|PO_NO_TERMINAL_IO;
69
70 public:
71
73 constructor(int po = DefaultProgramOptions);
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
117 class FileTemplate : public TextTemplateBase {
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
140 *date mtime;
141
142 public:
143
144 public :
146 Condition c();
147
148 public:
149
151
155 constructor(string path, int po = DefaultProgramOptions, *code psetup);
156
157
159
174 hash render(date mtime, hash ctx, int code = 200, *hash hdr);
175
176
177
178 private:
179 setupTemplateIntern(date mtime);
180 public:
181
182 };
183
185
188 class TemplateFileManager {
189
190 public:
191 private :
193 RWLock rwl();
194
196 hash th;
197
199 int po;
200
202 *code psetup;
203
204 public:
205
207
211 constructor(int po = TextTemplateBase::DefaultProgramOptions, *code psetup);
212
213
215
232 hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
233
234
236 purge(string tname);
237
238 };
239
241
246 class StaticTemplateManager : public WebUtil::TextTemplateBase {
247
248 public:
249 private :
251 hash th;
252
254 Sequence seq();
255
256 public:
257
259 constructor(int po = DefaultProgramOptions);
260
261
263
288 string add(string name, string src, *string ct);
289
290
292 bool hasTemplate(string tname);
293
294
296
313 hash render(string tname, hash ctx, int code = 200, *hash hdr);
314
315
317
332 *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
333
334
336 *hash getTemplateHash();
337
338
340 list getTemplateList();
341
342 };
343
345 class FileHandler : public HttpServer::AbstractUrlHandler, private WebUtil::TemplateFileManager {
346
347 public:
348 public :
350 string file_root;
351
353 softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
354
356 hash template_extensions = Defaults.TemplateExtensions;
357
359 *string default_target;
360
362
366 softint error_level = 0;
367
369 const Unix = (PlatformOS != "Windows");
370
372 const DirSep = (Unix ? "/" : "\\");
373
375 const Defaults = (
376 // default index file
377 "IndexFile": "index.html",
378 "IndexTemplate": "index.qhtml",
379 "TemplateExtensions": (
380 "qhtml": True,
381 ),
382 );
383
385 const Dirlisting = "<html lang=\"en\" >"
386 "{%"
387 " Dir $d();"
388 " $d.chdir($ctx.path);"
389 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
390
391 public:
392 " <head>"
393 " <meta charset=\"utf-8\" />"
394 " <title>{{ $title }}</title>"
395 " </head>"
396 " <body>"
397 " <header>"
398 " <h1>{{ $title }}</h1>"
399 " </header>"
400 " <div class=\"container\">"
401 " <table>"
402 " <tr>"
403 " <th>Name</th>"
404 " <th width=\"40\">Type</th>"
405 " <th width=\"80\">Size</th>"
406 " </tr>"
407 " {% if ($ctx.parent_url) { %}"
408 " <tr>"
409 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
410 " </tr>"
411 " {% } %}"
412 " {% foreach string $dir in (sort($d.listDirs())) { %}"
413 " {% if ($dir[0]!=\".\") {"
414 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
415 " %}"
416 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
417 " {% } %}"
418 " {% } %}"
419 " {% foreach string $file in (sort($d.listFiles())) { %}"
420 " {% if ($file[0]!=\".\") {"
421 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
422 " %}"
423 " <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>"
424 " {% } %}"
425 " {% } %}"
426 " </table>"
427 " </div>"
428 " </body>"
429 "</html>";
430 }
431
432 private :
434 StaticTemplateManager stm();
435
436 public:
437
439
448 constructor(string file_root, string url_root = "/", *hash opt);
449
450
451
452 private:
453 string getDirlistingTemplate();
454 public:
455
456
458
487 hash handleRequest(hash cx, hash hdr, *data body);
488
489
491 logInfo(string fmt);
492
493
495 logError(string fmt);
496
497
499 logDebug(string fmt);
500
501
503
506 private:
507 hash unhandledRequest(hash cx, hash hdr, *data body);
508 public:
509
510
512
515 private:
516 hash fileError(hash cx, hash sh);
517 public:
518
519
521
524 private:
525 hash serverError(hash cx, hash ex);
526 public:
527
528
529
530 private:
531 *hash tryServeRequest(hash cx);
532 public:
533
534
536
539 *hash renderDirectory(hash cx, string path);
540
541
543
574 private:
575 *hash handleRequestImpl(reference cx, hash hdr, *data body);
576 public:
577
578 };
579 };
WebUtil::FileTemplate::c
Condition c()
Condition variable for waiting for a template to be.
PO_NO_USER_CLASSES
const PO_NO_USER_CLASSES
WebUtil::FileHandler::Dirlisting
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:385
WebUtil::StaticTemplateManager::hasTemplate
bool hasTemplate(string tname)
returns True if the given template exists, False if not
date
date date(date dt)
WebUtil::FileHandler::error_level
softint error_level
set for error info level
Definition: WebUtil.qm.dox.h:366
WebUtil::FileTemplate::mtime
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:140
WebUtil::FileTemplate
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:117
WebUtil::TemplateFileManager::th
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:196
WebUtil::FileTemplate::constructor
constructor(string path, int po=DefaultProgramOptions, *code psetup)
creates the object based on the pathname and Program options
WebUtil::FileTemplate::psetup
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:134
WebUtil::FileTemplate::ct
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:128
WebUtil::StaticTemplateManager::add
string add(string name, string src, *string ct)
adds a template to the object
WebUtil::FileTemplate::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:122
WebUtil::logError
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
True
const True
WebUtil::TextTemplateBase
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:57
WebUtil::TemplateFileManager
this class manages templates based on files
Definition: WebUtil.qm.dox.h:188
WebUtil::StaticTemplateManager::tryRender
*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...
WebUtil::TextTemplateBase::getContentType
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
WebUtil::FileTemplate::TemplateFunc
const TemplateFunc
the name of the template function
Definition: WebUtil.qm.dox.h:131
WebUtil::FileHandler::Defaults
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:375
WebUtil::TextTemplateBase::add
static string add(Program p, string fn, string name, string src)
adds a template function to a template Program object
WebUtil::FileHandler::default_target
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:359
WebUtil::unhandledRequest
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...
PO_REQUIRE_OUR
const PO_REQUIRE_OUR
list
list list(...)
PO_NO_INHERIT_GLOBAL_VARS
const PO_NO_INHERIT_GLOBAL_VARS
WebUtil::TextTemplateBase::constructor
constructor(int po=DefaultProgramOptions)
sets up the object
WebUtil::handleRequestImpl
*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...
WebUtil::TemplateFileManager::render
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
WebUtil::FileTemplate::rwl
RWLock rwl()
to ensure atomicity regarding contention between setup/recompiles and rendering requests ...
WebUtil::FileHandler
this class serves files from the file system based on a root location
Definition: WebUtil.qm.dox.h:345
WebUtil::TemplateFileManager::psetup
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:202
WebUtil::FileHandler::template_extensions
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:356
WebUtil::TemplateFileManager::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:199
WebUtil::StaticTemplateManager::getTemplateList
list getTemplateList()
returns a list of template names, an empty list is returned if there are no templates ...
WebUtil::FileHandler::indexes
softlist indexes
indexes for directories; handled in order of appearance
Definition: WebUtil.qm.dox.h:353
WebUtil::StaticTemplateManager
A container for holding static text templates (ie that do not change once created in the template man...
Definition: WebUtil.qm.dox.h:246
WebUtil::StaticTemplateManager::constructor
constructor(int po=DefaultProgramOptions)
sets up the object
WebUtil::stm
WebUtil::FileHandler HttpServer::AbstractUrlHandler, WebUtil::TemplateFileManager stm()
static templates
WebUtil::constructor
constructor(string file_root, string url_root="/", *hash opt)
create the object optionally with the given AbstractAuthenticator
type
string type(any arg)
PO_NO_INHERIT_USER_FUNC_VARIANTS
const PO_NO_INHERIT_USER_FUNC_VARIANTS
WebUtil::StaticTemplateManager::getTemplateHash
*hash getTemplateHash()
returns a hash of template names, values are True or NOTHING if no templates are currently cached ...
WebUtil::FileTemplate::path
string path
the path to the template
Definition: WebUtil.qm.dox.h:125
WebUtil::TemplateFileManager::purge
purge(string tname)
this method can be called when a resource is requested that no longer exists in case a template is st...
WebUtil::StaticTemplateManager::seq
Sequence seq()
Sequence for template function names.
WebUtil::FileHandler::DirSep
const DirSep
directory separator character
Definition: WebUtil.qm.dox.h:372
HttpServer::AbstractUrlHandler
WebUtil::FileHandler::file_root
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:350
WebUtil::FileHandler::Unix
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:369
WebUtil::TextTemplateBase::p
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:62
WebUtil::TextTemplateBase::DefaultProgramOptions
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:68
WebUtil::serverError
hash serverError(hash cx, hash ex)
this method returns a 500 &quot;Internal Server Error&quot; error code when an exception occurs ...
WebUtil::fileError
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...
WebUtil::logDebug
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
PO_NO_TOP_LEVEL_STATEMENTS
const PO_NO_TOP_LEVEL_STATEMENTS
WebUtil::renderDirectory
*hash renderDirectory(hash cx, string path)
this method is called when a directory should be rendered
WebUtil::handleRequest
hash handleRequest(hash cx, hash hdr, *data body)
this method calls handleRequestImpl() to service the request, if handleRequestImpl() returns NOTHING...
WebUtil::TemplateFileManager::constructor
constructor(int po=TextTemplateBase::DefaultProgramOptions, *code psetup)
creates the object with optional Program options
WebUtil::FileTemplate::render
hash render(date mtime, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
PlatformOS
const PlatformOS
WebUtil::TextTemplateBase::doBlock
static bool doBlock(string end, reference src, string type, reference i, reference l)
a helper method used in parsing
WebUtil::StaticTemplateManager::render
hash render(string tname, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
WebUtil::StaticTemplateManager::th
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:251
hash
hash hash(object obj)
WebUtil::TemplateFileManager::rwl
RWLock rwl()
read-write lock for managing template access
PO_NO_TERMINAL_IO
const PO_NO_TERMINAL_IO
WebUtil::logInfo
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...