PageCompilerUserGuide.page 12 KB

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