Qore WebUtil Module Reference  1.4.1
 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 - 2017 Qore Technologies, s.r.o.
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.13 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 
77 namespace WebUtil {
81 
82 public:
83 private:
84 
85 public:
86 
87  private :
89  ReadOnlyFile f;
91  bool txt;
93  string ct;
95  int cs;
98 
99 public:
100 
103 
104 
106 
107 private:
109 public:
110 
111 
113 
114 private:
115  auto sendImpl();
116 public:
117 
118  };
119 
122 
123 public:
124  private :
126  Program p;
127 
128 public:
129 
130  public :
133 
134 public:
135 
138 
139 
141 
167  static string add(Qore::Program p, string fn, string name, string src);
168 
170 
171 private:
172  static string getCode(bool bare_refs, string fmt);
173 public:
174 
175 
177 
178 private:
179  static bool doBlock(string end, reference<string> src, string type, reference<int> i, reference<list<hash>> l);
180 public:
181 
182 
184  static string getContentType(string name);
185  };
186 
189 
190 public:
191  private :
193  int po;
194 
196  string path;
197 
199  string ct;
200 
202  const TemplateFunc = "t";
203 
205  *code psetup;
206 
208  RWLock rwl();
209 
212 
213 public:
214 
215  public :
216 
217 public:
218 
220 
224  constructor(string resource_path, int parse_opts = DefaultProgramOptions, *code pgm_setup) ;
225 
226 
228 
243  hash render(date new_mtime, hash ctx, int code = 200, *hash hdr);
244 
245 
246 
247 private:
248  setupTemplateIntern(date new_mtime);
249 public:
250 
251  };
252 
254 
258 
259 public:
260  private :
262  RWLock rwl();
263 
266 
268  int po;
269 
271  *code psetup;
272 
273 public:
274 
276 
280  constructor(int parse_opts = TextTemplateBase::DefaultProgramOptions, *code pgm_setup);
281 
282 
284 
301  hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
302 
303 
305  purge(string tname);
306 
307  };
308 
310 
316 
317 public:
318  private :
321 
323  Sequence seq();
324 
325 public:
326 
329 
330 
332 
357  string add(string name, string src, *string ct);
358 
359 
361  bool hasTemplate(string tname);
362 
363 
365 
382  hash render(string tname, hash ctx, int code = 200, *hash hdr);
383 
384 
386 
401  *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
402 
403 
406 
407 
410 
411  };
412 
415 
416 public:
417  public :
419  string file_root;
420 
422  softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
423 
425  hash template_extensions = Defaults.TemplateExtensions;
426 
428  *string default_target;
429 
431  softint chunked_threshold = Defaults.ChunkedThreshold;
432 
434  softint chunk_size = Defaults.ChunkSize;
435 
437 
442  softint error_level = 0;
443 
445  const Unix = (PlatformOS != "Windows");
446 
448 
452 
454  const Defaults = (
455  // default index file
456  "IndexFile": "index.html",
457  "IndexTemplate": "index.qhtml",
458  "TemplateExtensions": (
459  "qhtml": True,
460  ),
461  "ChunkedThreshold": 10 * 1024,
462  "ChunkSize": 4096,
463  );
464 
466  const Dirlisting = "<html lang=\"en\" >"
467 "{%"
468 " Dir $d();"
469 " $d.chdir($ctx.path);"
470 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
471 "%}"
472 " <head>"
473 " <meta charset=\"utf-8\" />"
474 " <title>{{ $title }}</title>"
475 " </head>"
476 " <body>"
477 " <header>"
478 " <h1>{{ $title }}</h1>"
479 " </header>"
480 " <div class=\"container\">"
481 " <table>"
482 " <tr>"
483 " <th>Name</th>"
484 " <th width=\"40\">Type</th>"
485 " <th width=\"80\">Size</th>"
486 " </tr>"
487 " {% if ($ctx.parent_url) { %}"
488 " <tr>"
489 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
490 " </tr>"
491 " {% } %}"
492 " {% foreach string $dir in (sort($d.listDirs())) { %}"
493 " {% if ($dir[0]!=\".\") {"
494 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
495 " %}"
496 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
497 " {% } %}"
498 " {% } %}"
499 " {% foreach string $file in (sort($d.listFiles())) { %}"
500 " {% if ($file[0]!=\".\") {"
501 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
502 " %}"
503 " <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>"
504 " {% } %}"
505 " {% } %}"
506 " </table>"
507 " </div>"
508 " </body>"
509 "</html>";
510 
511 public:
512 
513  private :
516 
517 public:
518 
520 
531  constructor(string new_file_root, string url_root = "/", *hash opt) ;
532 
533 
534 
535 private:
536  string getDirlistingTemplate();
537 public:
538 
539 
541 
573  hash handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
574 
575 
577  logInfo(string fmt);
578 
579 
581  logError(string fmt);
582 
583 
585  logDebug(string fmt);
586 
587 
589 
592 private:
593  hash unhandledRequest(hash cx, hash hdr, *data body);
594 public:
595 
596 
598 
601 private:
602  hash fileError(hash cx, hash sh);
603 public:
604 
605 
607 
610 private:
611  hash serverError(hash cx, hash ex);
612 public:
613 
614 
616 
617 private:
618  *hash tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
619 public:
620 
621 
623 
624 private:
625  FileStreamRequest getFileStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr);
626 public:
627 
628 
630 
637 private:
638  *hash getResponseHeadersForFile(string path, hash cx, hash hdr);
639 public:
640 
641 
643 
644 private:
645  hash sendFile(ReadOnlyFile f, bool txt, string ct, *hash respHdr);
646 public:
647 
648 
650 
651 private:
652  hash sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr);
653 public:
654 
655 
657 
660  *hash renderDirectory(hash cx, string path);
661 
662 
664 
696 private:
697  *hash handleRequestImpl(reference<hash> cx, hash hdr, *data body);
698 public:
699 
700  };
701 };
const PO_NO_USER_CLASSES
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:466
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:442
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:211
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:188
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:265
constructor(int parse_opts=TextTemplateBase::DefaultProgramOptions, *code pgm_setup)
creates the object with optional Program options
hash serverError(hash cx, hash ex)
this method returns a 500 &quot;Internal Server Error&quot; error code when an exception occurs ...
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:205
string ct
Content-Type.
Definition: WebUtil.qm.dox.h:93
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:199
string add(string name, string src, *string ct)
adds a template to the object
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:193
hash getResponseHeaderMessageImpl()
returns the reponse headers
ReadOnlyFile f
file object
Definition: WebUtil.qm.dox.h:89
hash sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr)
returns a handler hash response with the file&#39;s data to be sent in a HTTP message with chunked transf...
*hash tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body)
tries to serve the request from the filesystem
const True
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:121
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, *hash respHdr)
creates the object
this class manages templates based on files
Definition: WebUtil.qm.dox.h:257
softint chunk_size
HTTP chunk size in bytes.
Definition: WebUtil.qm.dox.h:434
*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:202
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...
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:454
*hash respHdr
response headers
Definition: WebUtil.qm.dox.h:97
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:428
const PO_REQUIRE_OUR
list list(...)
const PO_NO_INHERIT_GLOBAL_VARS
constructor(int po=DefaultProgramOptions)
sets up the object
*hash handleRequestImpl(reference< hash > cx, hash hdr, *data body)
this method is called by this class&#39;s handleRequest() before trying to service the request automatica...
bool txt
text flag
Definition: WebUtil.qm.dox.h:91
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:414
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:271
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:425
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:268
softint chunked_threshold
minimum size in bytes for plain files to be sent with a chnked transfer
Definition: WebUtil.qm.dox.h:431
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:422
constructor(string resource_path, int parse_opts=DefaultProgramOptions, *code pgm_setup)
creates the object based on the pathname and Program options
FileStreamRequest getFileStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr)
must return a FileStreamRequest object to stream the requested file with chunked transfer encoding ...
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:315
constructor(int po=DefaultProgramOptions)
sets up the object
auto sendImpl()
returns data to send
HttpListenerInterface listener
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
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 path
the path to the template
Definition: WebUtil.qm.dox.h:196
*hash getResponseHeadersForFile(string path, hash cx, hash hdr)
this method returns NOTHING by default but can be subclassed to add headers to the response ...
purge(string tname)
this method can be called when a resource is requested that no longer exists in case a template is st...
string type(auto arg)
Sequence seq()
Sequence for template function names.
StaticTemplateManager stm()
static templates
const DirSep
directory separator character
Definition: WebUtil.qm.dox.h:451
this class handles chunked file sends
Definition: WebUtil.qm.dox.h:80
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:419
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:445
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:126
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:132
hash sendFile(ReadOnlyFile f, bool txt, string ct, *hash respHdr)
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
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:320
hash hash(object obj)
RWLock rwl()
read-write lock for managing template access
const PO_NO_TERMINAL_IO
static bool doBlock(string end, reference< string > src, string type, reference< int > i, reference< list< hash >> l)
a helper method used in parsing
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
int cs
chunk size
Definition: WebUtil.qm.dox.h:95