Qore WebUtil Module Reference  1.4.1
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 
102  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) ;
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 
137  constructor(int po = DefaultProgramOptions);
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 
328  constructor(int po = DefaultProgramOptions) ;
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 
405  *hash getTemplateHash();
406 
407 
409  list getTemplateList();
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 :
515  StaticTemplateManager stm();
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 
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:
619 public:
620 
621 
623 
624 private:
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:
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
date date(date dt)
*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
*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
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
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
AbstractHttpRequestHandler handler
*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
bool txt
text flag
Definition: WebUtil.qm.dox.h:91
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
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:268
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
auto sendImpl()
returns data to send
HttpListenerInterface listener
hash< HttpHandlerResponseInfo > handleRequest()
const PO_NO_INHERIT_USER_FUNC_VARIANTS
string path
the path to the template
Definition: WebUtil.qm.dox.h:196
string type(auto arg)
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
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
const PO_NO_TOP_LEVEL_STATEMENTS
const PlatformOS
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:320
hash hash(object obj)
const PO_NO_TERMINAL_IO
the WebUtil namespace contains all the objects in the WebUtil module
Definition: WebUtil.qm.dox.h:78
int cs
chunk size
Definition: WebUtil.qm.dox.h:95