WebUtil.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// @file WebUtil.qm Qore user module supprting web server operations
/* WebUtil.qm Copyright (C) 2013 - 2017 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// this module requires Qore 0.8.13 or better
// require type definitions everywhere
// enable all warnings
// do not use "$" signs, assume local vars
namespace WebUtil {
class FileStreamRequest : public HttpServer::AbstractStreamRequest {
public:
private:
public:
private :
ReadOnlyFile f;
bool txt;
string ct;
int cs;
*hash respHdr;
public:
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) ;
private:
hash getResponseHeaderMessageImpl();
public:
private:
auto sendImpl();
public:
};
class TextTemplateBase {
public:
private :
Program p;
public:
public :
const DefaultProgramOptions = PO_REQUIRE_OUR|PO_NO_TOP_LEVEL_STATEMENTS|PO_NO_INHERIT_USER_FUNC_VARIANTS|PO_NO_INHERIT_GLOBAL_VARS|PO_NO_USER_CLASSES|PO_NO_TERMINAL_IO;
public:
constructor(int po = DefaultProgramOptions);
static string add(Qore::Program p, string fn, string name, string src);
private:
static string getCode(bool bare_refs, string fmt);
public:
private:
static bool doBlock(string end, reference<string> src, string type, reference<int> i, reference<list<hash>> l);
public:
static string getContentType(string name);
};
class FileTemplate : public TextTemplateBase {
public:
private :
int po;
string path;
string ct;
const TemplateFunc = "t";
*code psetup;
RWLock rwl();
*date mtime;
public:
public :
public:
constructor(string resource_path, int parse_opts = DefaultProgramOptions, *code pgm_setup) ;
hash render(date new_mtime, hash ctx, int code = 200, *hash hdr);
private:
setupTemplateIntern(date new_mtime);
public:
};
class TemplateFileManager {
public:
private :
RWLock rwl();
hash th;
int po;
*code psetup;
public:
constructor(int parse_opts = TextTemplateBase::DefaultProgramOptions, *code pgm_setup);
hash render(string tname, string path, date mtime, hash ctx, int code = 200, *hash hdr);
purge(string tname);
};
class StaticTemplateManager : public WebUtil::TextTemplateBase {
public:
private :
hash th;
Sequence seq();
public:
constructor(int po = DefaultProgramOptions) ;
string add(string name, string src, *string ct);
bool hasTemplate(string tname);
hash render(string tname, hash ctx, int code = 200, *hash hdr);
*hash tryRender(string tname, hash ctx, int code = 200, *hash hdr);
*hash getTemplateHash();
list getTemplateList();
};
class FileHandler : public HttpServer::AbstractUrlHandler, private WebUtil::TemplateFileManager {
public:
public :
string file_root;
softlist indexes = (Defaults.IndexTemplate, Defaults.IndexFile);
hash template_extensions = Defaults.TemplateExtensions;
*string default_target;
softint chunked_threshold = Defaults.ChunkedThreshold;
softint chunk_size = Defaults.ChunkSize;
softint error_level = 0;
const Unix = (PlatformOS != "Windows");
const DirSep = Qore::DirSep;
const Defaults = (
// default index file
"IndexFile": "index.html",
"IndexTemplate": "index.qhtml",
"TemplateExtensions": (
"qhtml": True,
),
"ChunkedThreshold": 10 * 1024,
"ChunkSize": 4096,
);
const Dirlisting = "<html lang=\"en\" >"
"{%"
" Dir $d();"
" $d.chdir($ctx.path);"
" string $title = sprintf(\"Index of /%s\", $ctx.resource_path);"
"%}"
" <head>"
" <meta charset=\"utf-8\" />"
" <title>{{ $title }}</title>"
" </head>"
" <body>"
" <header>"
" <h1>{{ $title }}</h1>"
" </header>"
" <div class=\"container\">"
" <table>"
" <tr>"
" <th>Name</th>"
" <th width=\"40\">Type</th>"
" <th width=\"80\">Size</th>"
" </tr>"
" {% if ($ctx.parent_url) { %}"
" <tr>"
" <td colspan=\"2\"><a href=\"{{ $ctx.parent_url }}\">Parent Directory</a></td>"
" </tr>"
" {% } %}"
" {% foreach string $dir in (sort($d.listDirs())) { %}"
" {% if ($dir[0]!=\".\") {"
" string $path = replace($ctx.path, $ctx.file_root, \"\");"
" %}"
" <tr><td><a href=\"{{ $ctx.url_root + $path + \"/\" + $dir }}\">{{ $dir }}</a></td><td width=\"40\">Directory</td></tr>"
" {% } %}"
" {% } %}"
" {% foreach string $file in (sort($d.listFiles())) { %}"
" {% if ($file[0]!=\".\") {"
" string $path = replace($ctx.path, $ctx.file_root, \"\");"
" %}"
" <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>"
" {% } %}"
" {% } %}"
" </table>"
" </div>"
" </body>"
"</html>";
public:
private :
StaticTemplateManager stm();
public:
constructor(string new_file_root, string url_root = "/", *hash opt) ;
private:
string getDirlistingTemplate();
public:
hash handleRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
logInfo(string fmt);
logError(string fmt);
logDebug(string fmt);
private:
hash unhandledRequest(hash cx, hash hdr, *data body);
public:
private:
hash fileError(hash cx, hash sh);
public:
private:
hash serverError(hash cx, hash ex);
public:
private:
*hash tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body);
public:
private:
FileStreamRequest getFileStreamRequestImpl(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr);
public:
private:
*hash getResponseHeadersForFile(string path, hash cx, hash hdr);
public:
private:
hash sendFile(ReadOnlyFile f, bool txt, string ct, *hash respHdr);
public:
private:
hash sendFileChunked(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body, Qore::ReadOnlyFile f, bool txt, string ct, *hash respHdr);
public:
*hash renderDirectory(hash cx, string path);
private:
*hash handleRequestImpl(reference<hash> cx, hash hdr, *data body);
public:
};
};
PO_NO_USER_CLASSES
const PO_NO_USER_CLASSES
WebUtil::FileHandler::Dirlisting
const Dirlisting
dirlisting template
Definition: WebUtil.qm.dox.h:466
WebUtil::FileHandler::logInfo
logInfo(string fmt)
default implementation is empty; this method is called with informational log messages; reimplement i...
WebUtil::StaticTemplateManager::hasTemplate
bool hasTemplate(string tname)
returns True if the given template exists, False if not
date
date date(date dt)
WebUtil::FileHandler::error_level
softint error_level
set for error info level
Definition: WebUtil.qm.dox.h:442
WebUtil::FileTemplate::mtime
*date mtime
file&#39;s modification time
Definition: WebUtil.qm.dox.h:211
WebUtil::FileTemplate
manages a template resource that may need to be recompiled if the file is updated in the filesystem; ...
Definition: WebUtil.qm.dox.h:188
WebUtil::TemplateFileManager::th
hash th
hash for template storage
Definition: WebUtil.qm.dox.h:265
WebUtil::TemplateFileManager::constructor
constructor(int parse_opts=TextTemplateBase::DefaultProgramOptions, *code pgm_setup)
creates the object with optional Program options
HttpServer::AbstractStreamRequest::hdr
hash hdr
WebUtil::FileHandler::serverError
hash serverError(hash cx, hash ex)
this method returns a 500 "Internal Server Error" error code when an exception occurs ...
WebUtil::FileTemplate::psetup
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:205
HttpServer::AbstractStreamRequest
WebUtil::FileStreamRequest::ct
string ct
Content-Type.
Definition: WebUtil.qm.dox.h:93
WebUtil::FileTemplate::ct
string ct
the content type of the rendered template
Definition: WebUtil.qm.dox.h:199
Qore::Program
HttpServer::AbstractHttpRequestHandler
WebUtil::StaticTemplateManager::add
string add(string name, string src, *string ct)
adds a template to the object
Qore::Socket
WebUtil::FileTemplate::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:193
WebUtil::FileStreamRequest::getResponseHeaderMessageImpl
hash getResponseHeaderMessageImpl()
returns the reponse headers
WebUtil::FileStreamRequest::f
ReadOnlyFile f
file object
Definition: WebUtil.qm.dox.h:89
WebUtil::FileHandler::sendFileChunked
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...
WebUtil::FileHandler::tryServeRequest
*hash tryServeRequest(HttpServer::HttpListenerInterface listener, Qore::Socket s, hash cx, hash hdr, *data body)
tries to serve the request from the filesystem
True
const True
HttpServer::HttpListenerInterface
WebUtil::TextTemplateBase
this is the base class for all template Program container classes
Definition: WebUtil.qm.dox.h:121
WebUtil::FileStreamRequest::constructor
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
WebUtil::TemplateFileManager
this class manages templates based on files
Definition: WebUtil.qm.dox.h:257
WebUtil::FileHandler::chunk_size
softint chunk_size
HTTP chunk size in bytes.
Definition: WebUtil.qm.dox.h:434
WebUtil::StaticTemplateManager::tryRender
*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...
HttpServer::AbstractStreamRequest::handler
AbstractHttpRequestHandler handler
WebUtil::TextTemplateBase::getContentType
static string getContentType(string name)
returns the content type from the file name, ignores any leading "q" in the extensions, assuming a template
WebUtil::FileTemplate::TemplateFunc
const TemplateFunc
the name of the template function
Definition: WebUtil.qm.dox.h:202
WebUtil::FileHandler::fileError
hash fileError(hash cx, hash sh)
this method returns a 400 "Bad Request" error code when a file should be served that&#39;s not a regular ...
WebUtil::FileHandler::Defaults
const Defaults
default configuration values
Definition: WebUtil.qm.dox.h:454
WebUtil::FileStreamRequest::respHdr
*hash respHdr
response headers
Definition: WebUtil.qm.dox.h:97
WebUtil::FileHandler::default_target
*string default_target
the default target if a URL cannot be satisfied
Definition: WebUtil.qm.dox.h:428
PO_REQUIRE_OUR
const PO_REQUIRE_OUR
list
list list(...)
PO_NO_INHERIT_GLOBAL_VARS
const PO_NO_INHERIT_GLOBAL_VARS
WebUtil::TextTemplateBase::constructor
constructor(int po=DefaultProgramOptions)
sets up the object
WebUtil::FileHandler::handleRequestImpl
*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...
WebUtil::FileStreamRequest::txt
bool txt
text flag
Definition: WebUtil.qm.dox.h:91
HttpServer::AbstractStreamRequest::cx
hash cx
HttpServer::AbstractStreamRequest::s
Socket s
WebUtil::TemplateFileManager::render
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
WebUtil::FileTemplate::rwl
RWLock rwl()
to ensure atomicity regarding contention between setup/recompiles and rendering requests ...
WebUtil::FileHandler::constructor
constructor(string new_file_root, string url_root="/", *hash opt)
create the object optionally with the given HttpServer::AbstractAuthenticator
WebUtil::FileHandler
this class serves files from the file system based on a root location
Definition: WebUtil.qm.dox.h:414
WebUtil::TemplateFileManager::psetup
*code psetup
code to perform Program initialization when creating a new Program object (inport API...
Definition: WebUtil.qm.dox.h:271
WebUtil::TextTemplateBase::getCode
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...
WebUtil::FileTemplate::render
hash render(date new_mtime, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
WebUtil::FileHandler::template_extensions
hash template_extensions
file extensions handled as templates
Definition: WebUtil.qm.dox.h:425
WebUtil::TemplateFileManager::po
int po
parse options used on the program
Definition: WebUtil.qm.dox.h:268
WebUtil::FileHandler::chunked_threshold
softint chunked_threshold
minimum size in bytes for plain files to be sent with a chnked transfer
Definition: WebUtil.qm.dox.h:431
WebUtil::StaticTemplateManager::getTemplateList
list getTemplateList()
returns a list of template names, an empty list is returned if there are no templates ...
WebUtil::FileHandler::indexes
softlist indexes
indexes for directories; handled in order of appearance
Definition: WebUtil.qm.dox.h:422
WebUtil::FileTemplate::constructor
constructor(string resource_path, int parse_opts=DefaultProgramOptions, *code pgm_setup)
creates the object based on the pathname and Program options
WebUtil::FileHandler::getFileStreamRequestImpl
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 ...
Qore::DirSep
const DirSep
WebUtil::StaticTemplateManager
A container for holding static text templates (ie that do not change once created in the template man...
Definition: WebUtil.qm.dox.h:315
WebUtil::StaticTemplateManager::constructor
constructor(int po=DefaultProgramOptions)
sets up the object
WebUtil::FileStreamRequest::sendImpl
auto sendImpl()
returns data to send
HttpServer::AbstractStreamRequest::listener
HttpListenerInterface listener
PO_NO_INHERIT_USER_FUNC_VARIANTS
const PO_NO_INHERIT_USER_FUNC_VARIANTS
WebUtil::StaticTemplateManager::getTemplateHash
*hash getTemplateHash()
returns a hash of template names, values are True or NOTHING if no templates are currently cached ...
WebUtil::FileHandler::logError
logError(string fmt)
default implementation is empty; this method is called with error log messages; reimplement in subcla...
WebUtil::FileHandler::renderDirectory
*hash renderDirectory(hash cx, string path)
this method is called when a directory should be rendered
WebUtil::FileHandler::unhandledRequest
hash unhandledRequest(hash cx, hash hdr, *data body)
this method returns a 404 "Not Found" error code to GET requests and a 501 "Not Implemented" error co...
WebUtil::FileTemplate::path
string path
the path to the template
Definition: WebUtil.qm.dox.h:196
WebUtil::FileHandler::getResponseHeadersForFile
*hash getResponseHeadersForFile(string path, hash cx, hash hdr)
this method returns NOTHING by default but can be subclassed to add headers to the response ...
WebUtil::TemplateFileManager::purge
purge(string tname)
this method can be called when a resource is requested that no longer exists in case a template is st...
type
string type(auto arg)
WebUtil::StaticTemplateManager::seq
Sequence seq()
Sequence for template function names.
WebUtil::FileHandler::stm
StaticTemplateManager stm()
static templates
HttpServer::AbstractStreamRequest::body
auto body
WebUtil::FileHandler::DirSep
const DirSep
directory separator character
Definition: WebUtil.qm.dox.h:451
WebUtil::FileStreamRequest
this class handles chunked file sends
Definition: WebUtil.qm.dox.h:80
HttpServer::AbstractUrlHandler
WebUtil::FileHandler::file_root
string file_root
root directory for serving files
Definition: WebUtil.qm.dox.h:419
WebUtil::FileHandler::Unix
const Unix
flag for UNIX operating systems
Definition: WebUtil.qm.dox.h:445
WebUtil::TextTemplateBase::p
Program p
Holds the template generation function.
Definition: WebUtil.qm.dox.h:126
WebUtil::TextTemplateBase::DefaultProgramOptions
const DefaultProgramOptions
default parse options for template Programs
Definition: WebUtil.qm.dox.h:132
WebUtil::FileHandler::sendFile
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 ...
PO_NO_TOP_LEVEL_STATEMENTS
const PO_NO_TOP_LEVEL_STATEMENTS
PlatformOS
const PlatformOS
WebUtil::StaticTemplateManager::render
hash render(string tname, hash ctx, int code=200, *hash hdr)
explicitly renders the given template with the given argument
WebUtil::FileHandler::logDebug
logDebug(string fmt)
default implementation is empty; this method is called with debug log messages; reimplement in subcla...
WebUtil::StaticTemplateManager::th
hash th
Maps template names to function names.
Definition: WebUtil.qm.dox.h:320
hash
hash hash(object obj)
Qore::ReadOnlyFile
HttpServer::AbstractUrlHandler::url_root
string url_root
WebUtil::TemplateFileManager::rwl
RWLock rwl()
read-write lock for managing template access
PO_NO_TERMINAL_IO
const PO_NO_TERMINAL_IO
WebUtil::TextTemplateBase::doBlock
static bool doBlock(string end, reference< string > src, string type, reference< int > i, reference< list< hash >> l)
a helper method used in parsing
WebUtil::FileHandler::handleRequest
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...
WebUtil::TextTemplateBase::add
static string add(Qore::Program p, string fn, string name, string src)
adds a template function to a template Program object
WebUtil
the WebUtil namespace contains all the objects in the WebUtil module
Definition: WebUtil.qm.dox.h:78
WebUtil::FileStreamRequest::cs
int cs
chunk size
Definition: WebUtil.qm.dox.h:95