Qore WebUtil Module Reference 1.7
Loading...
Searching...
No Matches
WebUtil.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* WebUtil.qm Copyright (C) 2013 - 2025 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// minimum Qore version
26
27// require type definitions everywhere
28
29// enable all warnings
30
31// do not use "$" signs, assume local vars
32
33
34
111namespace WebUtil {
114
115public:
116
117
118protected:
120 ReadOnlyFile f;
122 bool txt;
124 int cs;
126 *hash<auto> respHdr;
127
128public:
129
132 ;
133
134
136protected:
137 hash<HttpResponseInfo> getResponseHeaderMessageImpl();
138public:
139
140
142protected:
143 auto sendImpl();
144public:
145
146};
147
150
151public:
152
153
154protected:
156 InputStream stream;
158 int cs;
160 *hash<auto> respHdr;
161
162public:
163
166 ;
167
168
170protected:
171 hash<HttpResponseInfo> getResponseHeaderMessageImpl();
172public:
173
174
176protected:
177 auto sendImpl();
178public:
179
180};
181
182class AbstractTemplate {
183
184public:
186
203 hash<HttpResponseInfo> render(date new_mtime, hash<auto> ctx, int http_code = 200, *hash<auto> hdr);
204
205
207
220protected:
221 abstract hash<HttpResponseInfo> renderImpl(date new_mtime, hash<auto> ctx, int http_code = 200, *hash<auto> hdr);
222public:
223};
224
227
228public:
231
232
234
260 static string add(Qore::Program p, string fn, string name, string src);
261
263protected:
264 static string getCode(bool bare_refs, string fmt, ...);
265public:
266
267
269protected:
270 static bool doBlock(string end, reference<string> src, string type, reference<int> i, reference<list<hash>> l);
271public:
272
273
275 static string getContentType(string name);
276};
277
280
281public:
282protected:
284 Program p;
285
286public:
287
290
291
293 Program getProgram();
294
295};
296
298class FileTemplate : public AbstractTemplate, public TextTemplateBase {
299
300public:
301protected:
303 int po;
304
306 string path;
307
309 string ct;
310
312 const TemplateFunc = "t";
313
315 *code psetup;
316
318 RWLock rwl();
319
321 *date mtime;
322
323public:
324
325
327
331 constructor(string resource_path, int parse_opts = DefaultProgramOptions, *code pgm_setup) ;
332
333
335
350protected:
351 hash<HttpResponseInfo> renderImpl(date new_mtime, hash<auto> ctx, int http_code = 200, *hash<auto> hdr);
352public:
353
354
355protected:
356 setupTemplateIntern(date new_mtime);
357public:
358
359};
360
362
366
367public:
368protected:
370 RWLock rwl();
371
373 hash<string, AbstractTemplate> th;
374
376 int po;
377
379 *code psetup;
380
381public:
382
384
389
390
392
409 hash<HttpResponseInfo> render(string tname, string path, date mtime, hash<auto> ctx, int code = 200, *hash<auto> hdr);
410
411
413 purge(string tname);
414
415};
416
418
424
425public:
426protected:
428 hash<string, TextTemplateBase> toh;
429
431 hash<string, hash<auto>> th;
432
434 Sequence seq();
435
437 int po;
438
439public:
440
443
444
446
471 string add(string name, string src, *string ct);
472
473
475 bool hasTemplate(string tname);
476
477
479
496 hash<HttpResponseInfo> render(string tname, hash<auto> ctx, int code = 200, *hash<auto> hdr);
497
498
500
515 *hash<HttpResponseInfo> tryRender(string tname, hash<auto> ctx, int code = 200, *hash<auto> hdr);
516
517
519 *hash<string, bool> getTemplateHash();
520
521
523 list<string> getTemplateList();
524
525
527 setupProgram(Program p);
528
529};
530
533
534public:
536 string file_root;
537
539 softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
540
542 hash template_extensions = Defaults.TemplateExtensions;
543
546
548 softint chunked_threshold = Defaults.ChunkedThreshold;
549
551 softint chunk_size = Defaults.ChunkSize;
552
554
559 softint error_level = 0;
560
562 const Unix = (PlatformOS != "Windows");
563
566
568
572
574 const Defaults = ...;
575
576
578 const Dirlisting = ...;
579
580
581protected:
584
585public:
586
588
604 constructor(string new_file_root, string url_root = '/', *hash<auto> opt) ;
605
606
607protected:
608 string getDirlistingTemplate();
609public:
610
611
613
629 *hash<HttpResponseInfo> preprocessRequest(HttpServer::HttpListenerInterface listener, reference<hash<auto>> hdr, reference<hash<auto>> cx);
630
631
633
669 hash<HttpResponseInfo> handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash<auto> cx, hash<auto> hdr, *data body);
670
671
673
683 *hash<HttpResponseInfo> authenticateRequest(HttpListenerInterface listener, hash<auto> hdr, reference<hash<auto>> cx);
684
685
687 logInfo(string fmt, ...);
688
689
691 logError(string fmt, ...);
692
693
695 logDebug(string fmt, ...);
696
697
699
701protected:
702 hash<HttpResponseInfo> unhandledRequest(hash<auto> cx, hash<auto> hdr, *data body);
703public:
704
705
707
709protected:
710 hash<HttpResponseInfo> fileError(hash<auto> cx, hash<auto> sh);
711public:
712
713
715
717protected:
718 hash<HttpResponseInfo> serverError(hash<auto> cx, hash<auto> ex);
719public:
720
721
723protected:
724 *hash<HttpResponseInfo> tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash<auto> cx, hash<auto> hdr, *data body);
725public:
726
727
728protected:
729 *hash<HttpResponseInfo> sendFilePath(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash<auto> cx, hash<auto> hdr, *data body, string path, hash<StatInfo> stat_hash);
730public:
731
732
733protected:
734 *hash<StatInfo> statPath(hash<auto> cx, hash<auto> hdr, reference<string> path);
735public:
736
737
739protected:
740 AbstractStreamRequest getStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash<auto> cx, hash<auto> hdr, *data body, InputStream stream, *hash<auto> respHdr);
741public:
742
743
745
756protected:
757 hash<auto> getResponseHeadersForFile(string path, hash<auto> cx, hash<auto> request_hdr);
758public:
759
760
762protected:
763 hash<HttpResponseInfo> sendFile(InputStream stream, *hash<auto> respHdr);
764public:
765
766
768protected:
769 hash<HttpResponseInfo> sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash<auto> cx, hash<auto> hdr, *data body, InputStream stream, *hash<auto> respHdr);
770public:
771
772
774
777 *hash<HttpResponseInfo> renderDirectory(hash<auto> cx, string path);
778
779
781
809protected:
810 *hash<HttpResponseInfo> handleRequestImpl(reference<hash<auto>> cx, hash<auto> hdr, *data body);
811public:
812
813};
814};
AbstractHttpRequestHandler handler
HttpListenerInterface listener
this class serves files from the file system based on a root location
Definition WebUtil.qm.dox.h:532
softint error_level
set for error info level
Definition WebUtil.qm.dox.h:559
hash< HttpResponseInfo > fileError(hash< auto > cx, hash< auto > sh)
this method returns a 400 "Bad Request" error code when a file should be served that's not a regular ...
logInfo(string fmt,...)
default implementation is empty; this method is called with informational log messages; reimplement i...
hash< HttpResponseInfo > serverError(hash< auto > cx, hash< auto > ex)
this method returns a 500 "Internal Server Error" error code when an exception occurs
logDebug(string fmt,...)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
hash< auto > getResponseHeadersForFile(string path, hash< auto > cx, hash< auto > request_hdr)
this method returns a hash giving response headers with a default content type for the file to be ser...
const Defaults
default configuration values
Definition WebUtil.qm.dox.h:574
softint chunk_size
HTTP chunk size in bytes.
Definition WebUtil.qm.dox.h:551
const DefaultFileReadTimeout
default read timeout for serving files
Definition WebUtil.qm.dox.h:565
const Dirlisting
dirlisting template
Definition WebUtil.qm.dox.h:578
*hash< HttpResponseInfo > authenticateRequest(HttpListenerInterface listener, hash< auto > hdr, reference< hash< auto > > cx)
Authenticates the request if possible.
StaticTemplateManager stm()
static templates
AbstractStreamRequest getStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body, InputStream stream, *hash< auto > respHdr)
must return a AbstractStreamRequest object to stream the requested file with chunked transfer encodin...
*hash< HttpResponseInfo > renderDirectory(hash< auto > cx, string path)
this method is called when a directory should be rendered
const Unix
flag for UNIX operating systems
Definition WebUtil.qm.dox.h:562
*hash< HttpResponseInfo > tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body)
tries to serve the request from the filesystem
*string default_target
the default target if a URL cannot be satisfied
Definition WebUtil.qm.dox.h:545
hash< HttpResponseInfo > sendFile(InputStream stream, *hash< auto > respHdr)
returns a handler hash response with the data to be sent in a monolithic message
hash< HttpResponseInfo > handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body)
Handles the request and serves a file from the file system or a rendered template if possible.
const DirSep
directory separator character
Definition WebUtil.qm.dox.h:571
hash< HttpResponseInfo > unhandledRequest(hash< auto > cx, hash< auto > hdr, *data body)
this method returns a 404 "Not Found" error code to GET requests and a 501 "Not Implemented" error co...
*hash< HttpResponseInfo > preprocessRequest(HttpServer::HttpListenerInterface listener, reference< hash< auto > > hdr, reference< hash< auto > > cx)
Preproecsses a request before authentication.
softlist indexes
indexes for directories; handled in order of appearance
Definition WebUtil.qm.dox.h:539
string file_root
root directory for serving files
Definition WebUtil.qm.dox.h:536
softint chunked_threshold
minimum size in bytes for plain files to be sent with a chnked transfer
Definition WebUtil.qm.dox.h:548
logError(string fmt,...)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
hash template_extensions
file extensions handled as templates
Definition WebUtil.qm.dox.h:542
hash< HttpResponseInfo > sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body, InputStream stream, *hash< auto > respHdr)
returns a handler hash response with the file's data to be sent in a HTTP message with chunked transf...
constructor(string new_file_root, string url_root='/', *hash< auto > opt)
create the object optionally with the given HttpServer::AbstractAuthenticator
*hash< HttpResponseInfo > handleRequestImpl(reference< hash< auto > > cx, hash< auto > hdr, *data body)
this method is called by this class's handleRequest() before trying to service the request automatica...
this class handles chunked file sends
Definition WebUtil.qm.dox.h:113
hash< HttpResponseInfo > getResponseHeaderMessageImpl()
returns the reponse headers
bool txt
text flag
Definition WebUtil.qm.dox.h:122
constructor(HttpServer::HttpListenerInterface listener, HttpServer::AbstractHttpRequestHandler handler, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body, Qore::ReadOnlyFile file, bool textflag, int chunk_size, *hash< auto > respHdr)
creates the object
auto sendImpl()
returns data to send
int cs
chunk size
Definition WebUtil.qm.dox.h:124
*hash< auto > respHdr
response headers
Definition WebUtil.qm.dox.h:126
ReadOnlyFile f
file object
Definition WebUtil.qm.dox.h:120
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition WebUtil.qm.dox.h:298
int po
parse options used on the program
Definition WebUtil.qm.dox.h:303
string ct
the content type of the rendered template
Definition WebUtil.qm.dox.h:309
RWLock rwl()
to ensure atomicity regarding contention between setup/recompiles and rendering requests
*code psetup
code to perform Program initialization when creating a new Program object (inport API,...
Definition WebUtil.qm.dox.h:315
const TemplateFunc
the name of the template function
Definition WebUtil.qm.dox.h:312
string path
the path to the template
Definition WebUtil.qm.dox.h:306
*date mtime
file's modification time
Definition WebUtil.qm.dox.h:321
constructor(string resource_path, int parse_opts=DefaultProgramOptions, *code pgm_setup)
creates the object based on the pathname and Program options
hash< HttpResponseInfo > renderImpl(date new_mtime, hash< auto > ctx, int http_code=200, *hash< auto > hdr)
explicitly renders the given template with the given argument
this class handles chunked file sends
Definition WebUtil.qm.dox.h:149
*hash< auto > respHdr
response headers
Definition WebUtil.qm.dox.h:160
InputStream stream
input stream object
Definition WebUtil.qm.dox.h:156
auto sendImpl()
returns data to send
hash< HttpResponseInfo > getResponseHeaderMessageImpl()
returns the reponse headers
constructor(HttpServer::HttpListenerInterface listener, HttpServer::AbstractHttpRequestHandler handler, Qore::Socket s, hash< auto > cx, hash< auto > hdr, *data body, InputStream stream, int chunk_size, *hash< auto > respHdr)
creates the object
int cs
chunk size
Definition WebUtil.qm.dox.h:158
A container for holding static text templates (ie that do not change once created in the template man...
Definition WebUtil.qm.dox.h:423
Sequence seq()
Sequence for template function names.
int po
parse options for template containers
Definition WebUtil.qm.dox.h:437
constructor(int po=DefaultProgramOptions)
sets up the object
hash< string, hash< auto > > th
Maps template names to function names.
Definition WebUtil.qm.dox.h:431
*hash< string, bool > getTemplateHash()
returns a hash of template names, values are True or NOTHING if no templates are currently cached
setupProgram(Program p)
override in subclasses to customize the setup of template Programs; this method performs no action
string add(string name, string src, *string ct)
adds a template to the object
list< string > getTemplateList()
returns a list of template names, an empty list is returned if there are no templates
bool hasTemplate(string tname)
returns True if the given template exists, False if not
hash< string, TextTemplateBase > toh
Maps template names to TextTemplateBase objects.
Definition WebUtil.qm.dox.h:428
*hash< HttpResponseInfo > tryRender(string tname, hash< auto > ctx, int code=200, *hash< auto > hdr)
explicitly renders the given template with the given argument context hash if the template exists; if...
hash< HttpResponseInfo > render(string tname, hash< auto > ctx, int code=200, *hash< auto > hdr)
explicitly renders the given template with the given argument
this is the static base class for all template Program container classes
Definition WebUtil.qm.dox.h:226
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...
const DefaultProgramOptions
default parse options for template Programs
Definition WebUtil.qm.dox.h:230
static string getContentType(string name)
returns the content type from the file name, ignores any leading "q" in the extensions,...
static string add(Qore::Program p, string fn, string name, string src)
adds a template function to a template Program object
static bool doBlock(string end, reference< string > src, string type, reference< int > i, reference< list< hash > > l)
a helper method used in parsing
this class manages templates based on files
Definition WebUtil.qm.dox.h:365
hash< string, AbstractTemplate > th
hash for template storage
Definition WebUtil.qm.dox.h:373
purge(string tname)
this method can be called when a resource is requested that no longer exists in case a template is st...
hash< HttpResponseInfo > render(string tname, string path, date mtime, hash< auto > ctx, int code=200, *hash< auto > hdr)
explicitly renders the given template with the given path and context argument
RWLock rwl()
read-write lock for managing template access
*code psetup
code to perform Program initialization when creating a new Program object (inport API,...
Definition WebUtil.qm.dox.h:379
int po
parse options used on the program
Definition WebUtil.qm.dox.h:376
constructor(int parse_opts=StaticTextTemplateBase::DefaultProgramOptions, *code pgm_setup)
creates the object with optional Program options
this is the base class for all template Program container classes
Definition WebUtil.qm.dox.h:279
Program p
Holds the template generation function.
Definition WebUtil.qm.dox.h:284
Program getProgram()
returns the contained Program object
constructor(int po=DefaultProgramOptions)
sets up the object
const DirSep
the WebUtil namespace contains all the objects in the WebUtil module
Definition WebUtil.qm.dox.h:111