Qore WebUtil Module Reference  1.3
 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 - 2016 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.12 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 // do not use "$" signs, assume local vars
32 
33 
34 
71 namespace WebUtil {
75 
76 public:
77 private:
78 
79 public:
80 
81  private :
83  ReadOnlyFile f;
85  bool txt;
87  string ct;
89  int cs;
90 
91 public:
92 
95 
96 
99 
100 
102  private any sendImpl();
103 
104  };
105 
108 
109 public:
110  private :
112  Program p;
113 
114 public:
115 
116  public :
119 
120 public:
121 
124 
125 
127 
153  static string add(Qore::Program p, string fn, string name, string src);
154 
156 
157 private:
158  static string getCode(bool bare_refs, string fmt);
159 public:
160 
161 
163 
164 private:
165  static bool doBlock(string end, reference src, string type, reference i, reference l);
166 public:
167 
168 
170  static string getContentType(string name);
171  };
172 
175 
176 public:
177  private :
179  int po;
180 
182  string path;
183 
185  string ct;
186 
188  const TemplateFunc = "t";
189 
191  *code psetup;
192 
194  RWLock rwl();
195 
198 
199 public:
200 
201  public :
202 
203 public:
204 
206 
210  constructor(string resource_path, int parse_opts = DefaultProgramOptions, *code pgm_setup) ;
211 
212 
214 
229  hash render(date new_mtime, hash ctx, int code = 200, *hash hdr);
230 
231 
232  private setupTemplateIntern(date new_mtime);
233 
234  };
235 
237 
241 
242 public:
243  private :
245  RWLock rwl();
246 
249 
251  int po;
252 
254  *code psetup;
255 
256 public:
257 
259 
263  constructor(int parse_opts = TextTemplateBase::DefaultProgramOptions, *code pgm_setup);
264 
265 
267 
284  hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
285 
286 
288  purge(string tname);
289 
290  };
291 
293 
299 
300 public:
301  private :
304 
306  Sequence seq();
307 
308 public:
309 
312 
313 
315 
340  string add(string name, string src, *string ct);
341 
342 
344  bool hasTemplate(string tname);
345 
346 
348 
365  hash render(string tname, hash ctx, int code = 200, *hash hdr);
366 
367 
369 
384  *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
385 
386 
389 
390 
393 
394  };
395 
398 
399 public:
400  public :
402  string file_root;
403 
405  softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
406 
408  hash template_extensions = Defaults.TemplateExtensions;
409 
411  *string default_target;
412 
414  softint chunked_threshold = Defaults.ChunkedThreshold;
415 
417  softint chunk_size = Defaults.ChunkSize;
418 
420 
425  softint error_level = 0;
426 
428  const Unix = (PlatformOS != "Windows");
429 
431 
435 
437  const Defaults = (
438  // default index file
439  "IndexFile": "index.html",
440  "IndexTemplate": "index.qhtml",
441  "TemplateExtensions": (
442  "qhtml": True,
443  ),
444  "ChunkedThreshold": 10 * 1024,
445  "ChunkSize": 4096,
446  );
447 
449  const Dirlisting = "<html lang=\"en\" >"
450 "{%"
451 " Dir $d();"
452 " $d.chdir($ctx.path);"
453 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
454 "%}"
455 " <head>"
456 " <meta charset=\"utf-8\" />"
457 " <title>{{ $title }}</title>"
458 " </head>"
459 " <body>"
460 " <header>"
461 " <h1>{{ $title }}</h1>"
462 " </header>"
463 " <div class=\"container\">"
464 " <table>"
465 " <tr>"
466 " <th>Name</th>"
467 " <th width=\"40\">Type</th>"
468 " <th width=\"80\">Size</th>"
469 " </tr>"
470 " {% if ($ctx.parent_url) { %}"
471 " <tr>"
472 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
473 " </tr>"
474 " {% } %}"
475 " {% foreach string $dir in (sort($d.listDirs())) { %}"
476 " {% if ($dir[0]!=\".\") {"
477 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
478 " %}"
479 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
480 " {% } %}"
481 " {% } %}"
482 " {% foreach string $file in (sort($d.listFiles())) { %}"
483 " {% if ($file[0]!=\".\") {"
484 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
485 " %}"
486 " <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>"
487 " {% } %}"
488 " {% } %}"
489 " </table>"
490 " </div>"
491 " </body>"
492 "</html>";
493 
494 public:
495 
496  private :
499 
500 public:
501 
503 
514  constructor(string new_file_root, string url_root = "/", *hash opt) ;
515 
516 
517  private string getDirlistingTemplate();
518 
519 
521 
550  hash handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
551 
552 
554  logInfo(string fmt);
555 
556 
558  logError(string fmt);
559 
560 
562  logDebug(string fmt);
563 
564 
566 
568  private hash unhandledRequest(hash cx, hash hdr, *data body);
569 
570 
572 
574  private hash fileError(hash cx, hash sh);
575 
576 
578 
580  private hash serverError(hash cx, hash ex);
581 
582 
583  private *hash tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
584 
585 
587  private FileStreamRequest getFileStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct);
588 
589 
591  private hash sendFile(ReadOnlyFile f, bool txt, string ct);
592 
593 
595  private hash sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct);
596 
597 
599 
602  *hash renderDirectory(hash cx, string path);
603 
604 
606 
636  private *hash handleRequestImpl(reference cx, hash hdr, *data body);
637 
638  };
639 };
const PO_NO_USER_CLASSES
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:449
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:425
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:197
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:174
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:248
constructor(int parse_opts=TextTemplateBase::DefaultProgramOptions, *code pgm_setup)
creates the object with optional Program options
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:191
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
Content-Type.
Definition: WebUtil.qm.dox.h:87
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:185
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:179
private FileStreamRequest getFileStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct)
must return a FileStreamRequest object to stream the requested file with chunked transfer encoding ...
ReadOnlyFile f
file object
Definition: WebUtil.qm.dox.h:83
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:107
this class manages templates based on files
Definition: WebUtil.qm.dox.h:240
softint chunk_size
HTTP chunk size in bytes.
Definition: WebUtil.qm.dox.h:417
*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...
AbstractHttpRequestHandler handler
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:188
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:437
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:411
const PO_REQUIRE_OUR
list list(...)
const PO_NO_INHERIT_GLOBAL_VARS
constructor(int po=DefaultProgramOptions)
sets up the object
bool txt
text flag
Definition: WebUtil.qm.dox.h:85
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 ...
constructor(string new_file_root, string url_root="/", *hash opt)
create the object optionally with the given HttpServer::AbstractAuthenticator
this class serves files from the file system based on a root location
Definition: WebUtil.qm.dox.h:397
private any sendImpl()
returns data to send
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:254
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 render(date new_mtime, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:408
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:251
softint chunked_threshold
minimum size in bytes for plain files to be sent with a chnked transfer
Definition: WebUtil.qm.dox.h:414
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:405
constructor(string resource_path, int parse_opts=DefaultProgramOptions, *code pgm_setup)
creates the object based on the pathname and Program options
const DirSep
A container for holding static text templates (ie that do not change once created in the template man...
Definition: WebUtil.qm.dox.h:298
constructor(int po=DefaultProgramOptions)
sets up the object
private hash sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct)
returns a handler hash response with the file&#39;s data to be sent in a HTTP message with chunked transf...
HttpListenerInterface listener
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:182
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:434
this class handles chunked file sends
Definition: WebUtil.qm.dox.h:74
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:402
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:428
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:112
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:118
private hash sendFile(ReadOnlyFile f, bool txt, string ct)
returns a handler hash response with the file&#39;s data to be sent in a monolithic message ...
const PO_NO_TOP_LEVEL_STATEMENTS
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:303
hash hash(object obj)
RWLock rwl()
read-write lock for managing template access
constructor(HttpServer::HttpListenerInterface listener, HttpServer::AbstractHttpRequestHandler handler, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile file, bool textflag, string content_type, int chunk_size)
creates the object
const PO_NO_TERMINAL_IO
hash handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body)
this method calls handleRequestImpl() to service the request, if handleRequestImpl() returns NOTHING...
static string add(Qore::Program p, string fn, string name, string src)
adds a template function to a template Program object
private hash getResponseHeaderMessageImpl()
returns the reponse headers
int cs
chunk size
Definition: WebUtil.qm.dox.h:89