Qore WebUtil Module Reference  1.3
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 
123  constructor(int po = DefaultProgramOptions);
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 
311  constructor(int po = DefaultProgramOptions) ;
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 
388  *hash getTemplateHash();
389 
390 
392  list getTemplateList();
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 :
498  StaticTemplateManager stm();
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 
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
date date(date dt)
*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
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:191
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
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:179
ReadOnlyFile f
file object
Definition: WebUtil.qm.dox.h:83
const True
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
AbstractHttpRequestHandler handler
*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
bool txt
text flag
Definition: WebUtil.qm.dox.h:85
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
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:251
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
HttpListenerInterface listener
string type(any arg)
const PO_NO_INHERIT_USER_FUNC_VARIANTS
string path
the path to the template
Definition: WebUtil.qm.dox.h:182
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
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
const PO_NO_TOP_LEVEL_STATEMENTS
const PlatformOS
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:303
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:72
private hash getResponseHeaderMessageImpl()
returns the reponse headers
int cs
chunk size
Definition: WebUtil.qm.dox.h:89