PageCompilerUserGuide.page 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. POCO C++ Server Page Compiler User Guide
  2. POCO PageCompiler
  3. !!!Introduction
  4. PageCompiler is a command line tool that translates HTML files (and other kinds of files) into
  5. C++ code, more precisely, subclasses of Poco::Net::HTTPRequestHandler.
  6. The source files can contain special directives that allow embedding of C++ code.
  7. The syntax of these directives is based on the syntax used for
  8. Java Server Pages (JSP), Active Server Pages (ASP) or Embedded JavaScript (EJS) templating.
  9. This makes PageCompiler a C++-based HTML templating system. Since the translation of
  10. the HTML template into a C++ class happens at compile time, runtime performance is
  11. excellent.
  12. The following introductory sample shows the code for a simple page that displays the
  13. current date and time.
  14. <%@ page class="TimeHandler" %>
  15. <%!
  16. #include "Poco/DateTime.h"
  17. #include "Poco/DateTimeFormatter.h"
  18. #include "Poco/DateTimeFormat.h"
  19. using Poco::DateTime;
  20. using Poco::DateTimeFormatter;
  21. using Poco::DateTimeFormat;
  22. %>
  23. <%
  24. DateTime now;
  25. std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
  26. %>
  27. <html>
  28. <head>
  29. <title>Time Sample</title>
  30. </head>
  31. <body>
  32. <h1>Time Sample</h1>
  33. <p><%= dt %></p>
  34. </body>
  35. </html>
  36. ----
  37. Sending the above code to the page compiler will generate two files, a header file
  38. (<[TimeHandler.h]>) and an implementation file (<[TimeHandler.cpp]>).
  39. The files define a subclass of Poco::Net::HTTPRequestHandler named <[TimeHandler]>.
  40. The generated <[handleRequest]> member function contains code to send the HTML
  41. code contained in the source file to the client, as well as the C++ code fragments found
  42. in between the Scriptlet tags.
  43. !!!C++ Server Page Syntax
  44. The following special tags are supported in a C++ server page (CPSP) file.
  45. !!Hidden Comment
  46. A hidden comment documents the CPSP file, but is not sent to the client.
  47. <%-- <comment> --%>
  48. ----
  49. !!Implementation Declaration
  50. An implementation declaration is copied to the implementation file immediately after
  51. the block containing the standard <[#include]> directives.
  52. It is used to include additional header files and <[using]> declarations,
  53. as well as to define classes needed later on.
  54. <%!
  55. <declaration>
  56. ...
  57. %>
  58. ----
  59. !!Header Declaration
  60. A header declaration is copied to the header file immediately after the
  61. block containing the standard <[#include]> directives.
  62. It is usually used to include the header file containing the definition
  63. of the base class for the request handler, if a custom base class
  64. is required.
  65. <%!!
  66. <declaration>
  67. ...
  68. %>
  69. ----
  70. !!Expression
  71. The result of any valid C++ expression can be directly inserted into the page,
  72. provided the result can be written to an output stream. Note that the expression
  73. must not end with a semicolon. If HTML escape mode is enabled (new in POCO C++ Libraries
  74. release 1.10.0), HTML special characters < > " &
  75. will be replaced with corresponding character entities.
  76. <%= <expression> %>
  77. ----
  78. An alternative form of this is:
  79. <%- <expression> %>
  80. ----
  81. The latter form always copies the result of expression to the page verbatim,
  82. without escaping special HTML characters.
  83. Note that if escape mode is disabled, both forms are equivalent.
  84. !!Scriptlet
  85. Arbitrary C++ code fragments can be included using the Scriptlet directive.
  86. <%
  87. <statement>
  88. ...
  89. %>
  90. ----
  91. !!Pre-Response Scriptlet
  92. This is similar to an ordinary scriptlet, except that it will be executed
  93. before the HTTP response is sent. This can be used to manipulate the HTTP response
  94. object.
  95. <%%
  96. <statement>
  97. ...
  98. %>
  99. The main feature of this directive is that it allows to send a redirect response to
  100. the client if a certain condition is true.
  101. Example:
  102. <%%
  103. if (!loggedIn)
  104. {
  105. return response.redirect("/");
  106. }
  107. %>
  108. !!Include Directive
  109. Another CPSP file can be included into the current file using the Include
  110. Directive.
  111. <%@ include file="<path>" %>
  112. ----
  113. Alternatively, this can also be written as:
  114. <%@ include page="<path>" %>
  115. ----
  116. !!C++ Header Include Directive
  117. Include a C++ header file in the generated header file.
  118. <%@ header include="<path>" %>
  119. This corresponds to:
  120. <%!! #include "<path>" %>
  121. A variant of this directive is:
  122. <%@ header sinclude="<path>" %>
  123. This corresponds to:
  124. <%!! #include <<path>> %>
  125. !!C++ Implementation Include Directive
  126. Include a C++ header file in the generated implementation file.
  127. <%@ impl include="<path>" %>
  128. This corresponds to:
  129. <%! #include "<path>" %>
  130. A variant of this directive is:
  131. <%@ impl sinclude="<path>" %>
  132. This corresponds to:
  133. <%! #include <<path>> %>
  134. !!Page Directive
  135. The Page Directive allows the definition of attributes that control
  136. various aspects of C++ code generation.
  137. <%@ page <attr>="<value>" ... %>
  138. ----
  139. The following page attributes are supported:
  140. !class
  141. Specifies the name of the generated class.
  142. Defaults to the base name of the source file with the word "Handler" appended.
  143. !namespace
  144. If specified, sets the namespace where the generated classes will be in.
  145. No namespace will be used if omitted.
  146. !baseClass
  147. Specifies the name of the class used as the base class for the generated
  148. request handler class.
  149. Defaults to Poco::Net::HTTPRequestHandler. Do not forget to add a Header Declaration
  150. containing an <[#include]> directive for the header file containing the definition
  151. of that class, otherwise the generated code won't compile.
  152. !context
  153. Allows passing of a context object to the request handler's constructor.
  154. The context object is stored in the request handler object and can be
  155. obtained by calling the context() object.
  156. The class of the context object must be specified.
  157. Cannot be used together with <[ctorArg]>.
  158. !ctorArg
  159. Allows to specify the type of a single argument being passed to the constructor
  160. of the generated request handler class. Can only be used together with <[baseClass]>.
  161. The argument is passed on to the constructor of the base class, therefore, one of the
  162. constructors of the base class must also accept a single argument of the specified type.
  163. Cannot be used together with <[context]>.
  164. !escape
  165. Enable (set to <[true]>) or disable (<[false]>, default) HTML escape mode.
  166. If enabled, the result of any expression enclosed in <%= %> tags will be HTML-escaped,
  167. which means HTML special characters < > " & will be replaced by corresponding
  168. character entities.
  169. !export
  170. Allows to specify a DLL import/export directive that is being added to the request
  171. handler class definition. Useful for exporting a request handler class from a
  172. Windows DLL.
  173. !form
  174. Enable or disable automatic form handling. If enabled, which is the default,
  175. a Poco::Net::HTMLForm object is automatically created in the request handler
  176. and accessible through a variable named <[form]>.
  177. Set the value to <[false]> to disable form handling.
  178. !formPartHandler
  179. Allows you to pass a Poco::Net::PartHandler object to the form object for
  180. processing file uploads. A subclass of Poco::Net::PartHandler must be
  181. defined (using an Implementation Declaration), and the constructor of the part
  182. handler must take a (const) reference to the request handler instance as argument.
  183. !contentType
  184. Allows you to specify the MIME content type for the page. Defaults to text/html.
  185. !contentLanguage
  186. Allows to specify a language tag (e.g., "en") that will be sent in the
  187. response Content-Language header if the client sends an Accept-Language
  188. header in the request.
  189. !contentSecurityPolicy
  190. Allows to specify the value of the HTTP Content-Security-Policy header.
  191. !referrerPolicy
  192. Allows to specify the value of the HTTP Referrer-Policy header.
  193. !chunked
  194. Allows you to specify whether the response is sent using chunked transfer encoding.
  195. Defaults to <[true]>.
  196. Set the value to <[false]> to disable chunked transfer encoding.
  197. !compressed
  198. Enables or disables response body compression. If set to <[true]>, and the client supports
  199. the "gzip" content encoding (indicated by the "Accept-Encoding" header),
  200. the response body will be compressed using the "gzip" format and the
  201. "Content-Encoding" header will be set accordingly.
  202. Defaults to <[false]>. Cannot be enabled together with response buffering.
  203. !compressionLevel
  204. Specify the compression level, if response body compression is enabled.
  205. Valid values are 1 (fastest) to 9 (best compression). Defaults to 1.
  206. !buffered
  207. Enables or disables response buffering. Response buffering is disabled by default.
  208. Set to <[true]> to enable buffering, or to <[false]> to disable it.
  209. If response buffering is enabled, everything written to the response stream
  210. is actually written to a string stream (<[std::ostringstream]>).
  211. Sending of the HTTP response back to the client is deferred to
  212. when the page is complete.
  213. !cacheControl
  214. If set to a non-empty string (e.g., "no-cache, no-store"), adds a "Cache-Control"
  215. header to the response with the given argument as value.
  216. !session (OSP only)
  217. For use with the POCO Open Service Platform only.
  218. Specifies the identifier of the session obtained from the OSP Web Session Manager.
  219. If specified, a Poco::OSP::Web::WebSession object will be available in the
  220. request handler through a variable named <[session]>. The variable is of type
  221. Poco::OSP::Web::WebSession::Ptr. If the identifier starts with an asterisk
  222. ('@'), the identifier is considered to be a bundle property name, and the
  223. session identifier is read from the respective bundle property.
  224. !sessionTimeout (OSP only)
  225. For use with the POCO Open Service Platform only.
  226. Specifies the session timeout in minutes.
  227. If the argument is a string, it is considered to be a bundle property name
  228. and the timeout value is read from the respective bundle property.
  229. !createSession (OSP only)
  230. For use with the POCO Open Service Platform only.
  231. If set to <[true]>, which is the default if the attribute is not specified,
  232. a new session will be created if the request does not contain a (valid) session cookie.
  233. If set to <[false]> and there is no existing session that matches the session
  234. cookie. the <[session]> variable will be null.
  235. !precondition
  236. Allows to specify a C++ boolean expression which will be evaluated before processing
  237. of the page begins. If the expression evaluates to false, processing of the
  238. page is immediately terminated and no response is sent to the client.
  239. The expression can be a call to a member function defined in the handler base class.
  240. If that function returns false, it can send its own response to the client.
  241. Example:
  242. <%@ page precondition="checkCredentials(request, response)" %>
  243. ----
  244. !path
  245. Specify a server path for the page. If specified, the generated handler class
  246. will contain a public static <[const std::string]> member variable named <[PATH]>
  247. containing the specified path.
  248. Example:
  249. <%@ page path="/index.html" %>
  250. ----
  251. !!Implicit Objects
  252. The following objects are available in the handler code.
  253. !request
  254. The HTTP request object - an instance of Poco::Net::HTTPServerRequest.
  255. !response
  256. The HTTP response object - an instance of Poco::Net::HTTPServerRequest.
  257. !responseStream
  258. The output stream where the response body is written to.
  259. !form
  260. An instance of Poco::Net::HTMLForm for processing form arguments.
  261. Only available if form processing has not been disabled by
  262. setting the <[form]> page attribute to <[false]>.
  263. !session (OSP only)
  264. An instance of Poco::OSP::Web::WebSession::Ptr for accessing the
  265. Poco::OSP::Web::WebSession object for the current session.
  266. Only available with the POCO Open Service Platform, and if the
  267. <[session]> page attribute has been specified. May be null if
  268. the <[createSession]> attribute is false and no session exists.
  269. !!!Invoking the Page Compiler
  270. The Page Compiler is invoked from the command line. The file names of the
  271. CPSP files to be compiled are specified as arguments.
  272. A number of options control the code generation. Options are specified
  273. using the usual command-line option syntax for the current operating
  274. system (e.g., <[/help]> on Windows, <[--help]> or <[-h]> on Unix).
  275. * help (h): display usage information
  276. * define (D): define a configuration property
  277. * config-file (f): load configuration properties from a file
  278. * osp (O): add factory class definition/implementation for use with OSP
  279. * apache (A): add factory class definition/implementation and shared library manifest for use with ApacheConnector
  280. * escape (e): make HTML-escape mode default for all pages
  281. Run the PageCompiler with the --help option to see all available options.
  282. !!Configuration Properties
  283. The Page Compiler supports one configuration property, named
  284. <[PageCompiler.fileHeader]>, to optionally specify a header that is
  285. included in every generated file.
  286. The file header can contain references to other configuration properties,
  287. using the usual property syntax: <[${property}]>.
  288. For example, invoking the Page Compiler with the following configuration
  289. file:
  290. PageCompiler.fileHeader = //\n// ${outputFileName}\n//\n
  291. ----
  292. places the following header at the beginning of each generated file
  293. (<[<filename>]> is replaced with the actual name of the file):
  294. //
  295. // <filename>
  296. //
  297. ----
  298. The following pre-defined properties can be used in the file header:
  299. * <[${inputFileName}]>: the name of the input file (with directories removed)
  300. * <[${inputFilePath}]>: the complete path of the input file
  301. * <[${dateTime}]>: the current date and time (YYYY-MM-DD HH:MM:SS)
  302. * <[${outputFileName}]>: the name of the current output file (header or implementation file), with
  303. directories removed
  304. * <[${outputFilePath}]>: the complete path of the current output file