Qore WebUtil Module Reference  1.4
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 
74 namespace WebUtil {
78 
79 public:
80 private:
81 
82 public:
83 
84  private :
86  ReadOnlyFile f;
88  bool txt;
90  string ct;
92  int cs;
93 
94 public:
95 
98 
99 
101 
102 private:
104 public:
105 
106 
108 
109 private:
110  auto sendImpl();
111 public:
112 
113  };
114 
117 
118 public:
119  private :
121  Program p;
122 
123 public:
124 
125  public :
128 
129 public:
130 
132  constructor(int po = DefaultProgramOptions);
133 
134 
136 
162  static string add(Qore::Program p, string fn, string name, string src);
163 
165 
166 private:
167  static string getCode(bool bare_refs, string fmt);
168 public:
169 
170 
172 
173 private:
174  static bool doBlock(string end, reference<string> src, string type, reference<int> i, reference<list<hash>> l);
175 public:
176 
177 
179  static string getContentType(string name);
180  };
181 
184 
185 public:
186  private :
188  int po;
189 
191  string path;
192 
194  string ct;
195 
197  const TemplateFunc = "t";
198 
200  *code psetup;
201 
203  RWLock rwl();
204 
207 
208 public:
209 
210  public :
211 
212 public:
213 
215 
219  constructor(string resource_path, int parse_opts = DefaultProgramOptions, *code pgm_setup) ;
220 
221 
223 
238  hash render(date new_mtime, hash ctx, int code = 200, *hash hdr);
239 
240 
241 
242 private:
243  setupTemplateIntern(date new_mtime);
244 public:
245 
246  };
247 
249 
253 
254 public:
255  private :
257  RWLock rwl();
258 
261 
263  int po;
264 
266  *code psetup;
267 
268 public:
269 
271 
275  constructor(int parse_opts = TextTemplateBase::DefaultProgramOptions, *code pgm_setup);
276 
277 
279 
296  hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
297 
298 
300  purge(string tname);
301 
302  };
303 
305 
311 
312 public:
313  private :
316 
318  Sequence seq();
319 
320 public:
321 
323  constructor(int po = DefaultProgramOptions) ;
324 
325 
327 
352  string add(string name, string src, *string ct);
353 
354 
356  bool hasTemplate(string tname);
357 
358 
360 
377  hash render(string tname, hash ctx, int code = 200, *hash hdr);
378 
379 
381 
396  *hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
397 
398 
400  *hash getTemplateHash();
401 
402 
404  list getTemplateList();
405 
406  };
407 
410 
411 public:
412  public :
414  string file_root;
415 
417  softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
418 
420  hash template_extensions = Defaults.TemplateExtensions;
421 
423  *string default_target;
424 
426  softint chunked_threshold = Defaults.ChunkedThreshold;
427 
429  softint chunk_size = Defaults.ChunkSize;
430 
432 
437  softint error_level = 0;
438 
440  const Unix = (PlatformOS != "Windows");
441 
443 
447 
449  const Defaults = (
450  // default index file
451  "IndexFile": "index.html",
452  "IndexTemplate": "index.qhtml",
453  "TemplateExtensions": (
454  "qhtml": True,
455  ),
456  "ChunkedThreshold": 10 * 1024,
457  "ChunkSize": 4096,
458  );
459 
461  const Dirlisting = "<html lang=\"en\" >"
462 "{%"
463 " Dir $d();"
464 " $d.chdir($ctx.path);"
465 " string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
466 "%}"
467 " <head>"
468 " <meta charset=\"utf-8\" />"
469 " <title>{{ $title }}</title>"
470 " </head>"
471 " <body>"
472 " <header>"
473 " <h1>{{ $title }}</h1>"
474 " </header>"
475 " <div class=\"container\">"
476 " <table>"
477 " <tr>"
478 " <th>Name</th>"
479 " <th width=\"40\">Type</th>"
480 " <th width=\"80\">Size</th>"
481 " </tr>"
482 " {% if ($ctx.parent_url) { %}"
483 " <tr>"
484 " <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
485 " </tr>"
486 " {% } %}"
487 " {% foreach string $dir in (sort($d.listDirs())) { %}"
488 " {% if ($dir[0]!=\".\") {"
489 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
490 " %}"
491 " <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
492 " {% } %}"
493 " {% } %}"
494 " {% foreach string $file in (sort($d.listFiles())) { %}"
495 " {% if ($file[0]!=\".\") {"
496 " string $path = replace($ctx.path, $ctx.file_root, \"\");"
497 " %}"
498 " <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>"
499 " {% } %}"
500 " {% } %}"
501 " </table>"
502 " </div>"
503 " </body>"
504 "</html>";
505 
506 public:
507 
508  private :
510  StaticTemplateManager stm();
511 
512 public:
513 
515 
526  constructor(string new_file_root, string url_root = "/", *hash opt) ;
527 
528 
529 
530 private:
531  string getDirlistingTemplate();
532 public:
533 
534 
536 
569 
570 
572  logInfo(string fmt);
573 
574 
576  logError(string fmt);
577 
578 
580  logDebug(string fmt);
581 
582 
584 
587 private:
588  hash unhandledRequest(hash cx, hash hdr, *data body);
589 public:
590 
591 
593 
596 private:
597  hash fileError(hash cx, hash sh);
598 public:
599 
600 
602 
605 private:
606  hash serverError(hash cx, hash ex);
607 public:
608 
609 
611 
612 private:
614 public:
615 
616 
618 
619 private:
621 public:
622 
623 
625 
626 private:
627  hash sendFile(ReadOnlyFile f, bool txt, string ct);
628 public:
629 
630 
632 
633 private:
635 public:
636 
637 
639 
642  *hash renderDirectory(hash cx, string path);
643 
644 
646 
678 private:
679  *hash handleRequestImpl(reference<hash> cx, hash hdr, *data body);
680 public:
681 
682  };
683 };
const PO_NO_USER_CLASSES
date date(date dt)
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:206
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:183
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:260
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:200
string ct
Content-Type.
Definition: WebUtil.qm.dox.h:90
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:194
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:188
hash getResponseHeaderMessageImpl()
returns the reponse headers
ReadOnlyFile f
file object
Definition: WebUtil.qm.dox.h:86
const True
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:116
this class manages templates based on files
Definition: WebUtil.qm.dox.h:252
AbstractHttpRequestHandler handler
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:423
const PO_REQUIRE_OUR
list list(...)
const PO_NO_INHERIT_GLOBAL_VARS
bool txt
text flag
Definition: WebUtil.qm.dox.h:88
this class serves files from the file system based on a root location
Definition: WebUtil.qm.dox.h:409
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:266
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:263
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:310
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:191
string type(auto arg)
this class handles chunked file sends
Definition: WebUtil.qm.dox.h:77
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:414
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:121
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:127
const PO_NO_TOP_LEVEL_STATEMENTS
const PlatformOS
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:315
hash hash(object obj)
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
the WebUtil namespace contains all the objects in the WebUtil module
Definition: WebUtil.qm.dox.h:75
int cs
chunk size
Definition: WebUtil.qm.dox.h:92