Qore WebUtil Module Reference: doxygen/qlib/WebUtil.qm.dox.h Source File

Qore WebUtil Module Reference 1.1

- 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 (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
61 namespace WebUtil {
64 class TextTemplateBase {
65
66 public:
67 private :
69 Program p;
70
71 public:
72
73 public :
75 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;
76
77 public:
78
80 constructor(int po = DefaultProgramOptions);
81
82
84
110 static string add(Program p, string fn, string name, string src);
111
113
114 private:
115 static bool doBlock(string end, reference src, string type, reference i, reference l);
116 public:
117
118
120 static string getContentType(string name);
121 };
122
124 class FileTemplate : public TextTemplateBase {
125
126 public:
127 private :
129 int po;
130
132 string path;
133
135 string ct;
136
138 const TemplateFunc = "t";
139
141 *code psetup;
142
144 RWLock rwl();
145
147 *date mtime;
148
149 public:
150
151 public :
152
153 public:
154
156
160 constructor(string path, int po = DefaultProgramOptions, *code psetup);
161
162
164
179 hash render(date mtime, hash ctx, int code = 200, *hash hdr);
180
181
182 private setupTemplateIntern(date mtime);
183
184 };
185
187
190 class TemplateFileManager {
191
192 public:
193 private :
195 RWLock rwl();
196
198 hash th;
199
201 int po;
202
204 *code psetup;
205
206 public:
207
209
213 constructor(int po = TextTemplateBase::DefaultProgramOptions, *code psetup);
214
215
217
234 hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
235
236
238 purge(string tname);
239
240 };
241
243
248 class StaticTemplateManager : public WebUtil::TextTemplateBase {
249
250 public:
251 private :
253 hash th;
254
256 Sequence seq();
257
258 public:
259
261 constructor(int po = DefaultProgramOptions);
262
263
265
290 string add(string name, string src, *string ct);
291
292
294 bool hasTemplate(string tname);
295
296
298
315 hash render(string tname, hash ctx, int code = 200, *hash hdr);
316
317
319
334 *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
335
336
338 *hash getTemplateHash();
339
340
342 list getTemplateList();
343
344 };
345
347 class FileHandler : public HttpServer::AbstractUrlHandler, private WebUtil::TemplateFileManager {
348
349 public:
350 public :
352 string file_root;
353
355 softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
356
358 hash template_extensions = Defaults.TemplateExtensions;
359
361 *string default_target;
362
364
369 softint error_level = 0;
370
372 const Unix = (PlatformOS != "Windows");
373
375 const DirSep = (Unix ? "/" : "\\");
376
378 const Defaults = (
379 // default index file
380 "IndexFile": "index.html",
381 "IndexTemplate": "index.qhtml",
382 "TemplateExtensions": (
383 "qhtml": True,
384 ),
385 );
386
388 const Dirlisting = "<html lang=\"en\" >"
389 "{%"
390 " Dir $d();"
391 " $d.chdir($ctx.path);"
392 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
393 "%}"
394 " <head>"
395 " <meta charset=\"utf-8\" />"
396 " <title>{{ $title }}</title>"
397 " </head>"
398 " <body>"
399 " <header>"
400 " <h1>{{ $title }}</h1>"
401 " </header>"
402 " <div class=\"container\">"
403 " <table>"
404 " <tr>"
405 " <th>Name</th>"
406 " <th width=\"40\">Type</th>"
407 " <th width=\"80\">Size</th>"
408 " </tr>"
409 " {% if ($ctx.parent_url) { %}"
410 " <tr>"
411 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
412 " </tr>"
413 " {% } %}"
414 " {% foreach string $dir in (sort($d.listDirs())) { %}"
415 " {% if ($dir[0]!=\".\") {"
416 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
417 " %}"
418 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
419 " {% } %}"
420 " {% } %}"
421 " {% foreach string $file in (sort($d.listFiles())) { %}"
422 " {% if ($file[0]!=\".\") {"
423 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
424 " %}"
425 " <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>"
426 " {% } %}"
427 " {% } %}"
428 " </table>"
429 " </div>"
430 " </body>"
431 "</html>";
432
433 public:
434
435 private :
437 StaticTemplateManager stm();
438
439 public:
440
442
451 constructor(string file_root, string url_root = "/", *hash opt);
452
453
454 private string getDirlistingTemplate();
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
505 private hash unhandledRequest(hash cx, hash hdr, *data body);
506
507
509
511 private hash fileError(hash cx, hash sh);
512
513
515
517 private hash serverError(hash cx, hash ex);
518
519
520 private *hash tryServeRequest(hash cx);
521
522
524
527 *hash renderDirectory(hash cx, string path);
528
529
531
561 private *hash handleRequestImpl(reference cx, hash hdr, *data body);
562
563 };
564 };
PO_NO_USER_CLASSES
const PO_NO_USER_CLASSES
WebUtil::FileHandler::Dirlisting
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:388
WebUtil::FileHandler::logInfo
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...
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:369
WebUtil::FileTemplate::mtime
*date mtime
file's modification time
Definition: WebUtil.qm.dox.h:147
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:124
WebUtil::TemplateFileManager::th
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:198
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:141
WebUtil::FileHandler::unhandledRequest
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...
WebUtil::FileTemplate::ct
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:135
WebUtil::StaticTemplateManager::add
string add(string name, string src, *string ct)
adds a template to the object
WebUtil::FileHandler::serverError
private hash serverError(hash cx, hash ex)
this method returns a 500 "Internal Server Error" error code when an exception occurs ...
WebUtil::FileTemplate::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:129
True
const True
WebUtil::FileHandler::handleRequestImpl
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...
WebUtil::TextTemplateBase
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:64
WebUtil::TemplateFileManager
this class manages templates based on files
Definition: WebUtil.qm.dox.h:190
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 "q" in the extensions, assuming a template
WebUtil::FileTemplate::TemplateFunc
const TemplateFunc
the name of the template function
Definition: WebUtil.qm.dox.h:138
WebUtil::FileHandler::Defaults
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:378
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::fileError
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 regula...
WebUtil::FileHandler::default_target
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:361
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::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:347
WebUtil::TemplateFileManager::psetup
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:204
WebUtil::FileHandler::template_extensions
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:358
WebUtil::TemplateFileManager::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:201
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:355
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:248
WebUtil::StaticTemplateManager::constructor
constructor(int po=DefaultProgramOptions)
sets up the object
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::FileHandler::logError
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
WebUtil::FileHandler::renderDirectory
*hash renderDirectory(hash cx, string path)
this method is called when a directory should be rendered
WebUtil::FileTemplate::path
string path
the path to the template
Definition: WebUtil.qm.dox.h:132
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::stm
StaticTemplateManager stm()
static templates
WebUtil::FileHandler::DirSep
const DirSep
directory separator character
Definition: WebUtil.qm.dox.h:375
HttpServer::AbstractUrlHandler
WebUtil::FileHandler::file_root
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:352
WebUtil::FileHandler::Unix
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:372
WebUtil::TextTemplateBase::p
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:69
WebUtil::TextTemplateBase::DefaultProgramOptions
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:75
PO_NO_TOP_LEVEL_STATEMENTS
const PO_NO_TOP_LEVEL_STATEMENTS
WebUtil::FileHandler::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::FileHandler::logDebug
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
WebUtil::StaticTemplateManager::th
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:253
hash
hash hash(object obj)
HttpServer::AbstractUrlHandler::url_root
string url_root
WebUtil::TemplateFileManager::rwl
RWLock rwl()
read-write lock for managing template access
PO_NO_TERMINAL_IO
const PO_NO_TERMINAL_IO
WebUtil::FileHandler::constructor
constructor(string file_root, string url_root="/", *hash opt)
create the object optionally with the given AbstractAuthenticator