ApacheConnectorUserGuide.page 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. POCO ApacheConnector User Guide
  2. POCO ApacheConnector
  3. !!!Introduction
  4. ApacheConnector (<[mod_poco]>) is an Apache module that allows one to write Apache server extensions using
  5. the POCO Net HTTPServer framework. Almost any subclass of Poco::Net::HTTPRequestHandler written
  6. for Poco::Net::HTTPServer can be used with ApacheConnector. For this to work, the request handler
  7. subclass, together with its factory (Poco::Net::HTTPRequestHandlerFactory) must be contained
  8. in a shared library. The ApacheConnector uses the Poco::ClassLoader to load request handler
  9. factories from shared libraries.
  10. !!!Adding ApacheConnector to Apache
  11. ApacheConnector is implemented as an ordinary Apache 2 module, named <[mod_poco]>.
  12. To add <[mod_poco]> to Apache, add the following entry to the Apache configuration
  13. file (usually <[httpd.conf]>):
  14. LoadModule poco_module modules/mod_pocod.so
  15. ----
  16. !!!Configuring ApacheConnector
  17. ApacheConnector must be able to find shared libraries containing request handler, as well
  18. as optional configuration files. ApacheConnector provides an Poco::Util::Application class
  19. to request handlers that can be used to access configuration data, loaded from configuration
  20. files.
  21. !!Request Handler Configuration
  22. To have the ApacheConnector load a request handler class, the <[AddPocoRequestHandler]> directive
  23. is used in the Apache configuration file:
  24. AddPocoRequestHandler <FactoryClass> <SharedLibrary> <Path>...
  25. ----
  26. The first argument specifies the name of the request handler factory class. The second argument
  27. contains the path of the shared library containing the request handler.
  28. The third (and optionally following) argument(s) specify the URI paths handled by the
  29. request handler. For example:
  30. AddPocoRequestHandler TimeRequestHandlerFactory p:/Poco/ApacheConnector/samples/TimeServer/bin/TimeServerd.dll /time
  31. ----
  32. loads the TimeRequestHandlerFactory from TimeServerd.dll. Whenever a request for a URI starting with "/time"
  33. is sent by a client, this request will be handled by the TimeRequestHandler.
  34. !!Configuration Files
  35. ApacheConnector can also load POCO configuration files (.ini, .properties or .xml) for later use by
  36. request handlers. This is done using the <[AddPocoConfig]> directive:
  37. AddPocoConfig <Path>
  38. ----
  39. where <Path> specifies the full path to the configuration file.
  40. In a request handler, the configuration properties loaded this way can be accessed using:
  41. Poco::Util::Application& app = Poco::Util::Application::instance();
  42. std::string myProp = app.config().getString("MyProperty");
  43. ----
  44. !!!Logging in Request Handlers
  45. ApacheConnector provides a special logging channel that logs to Apache's error log file.
  46. This channel is set as the root channel when the ApacheConnector is loaded, so every
  47. logger will automatically inherit this channel.
  48. !!!A Sample Request Handler
  49. Following is a sample for a request handler implementation. The complete sample project can be found in the
  50. <[samples]> subdirectory of the ApacheConnector directory.
  51. #include "Poco/Net/HTTPServer.h"
  52. #include "Poco/Net/HTTPRequestHandler.h"
  53. #include "Poco/Net/HTTPRequestHandlerFactory.h"
  54. #include "Poco/Net/HTTPServerRequest.h"
  55. #include "Poco/Net/HTTPServerResponse.h"
  56. #include "Poco/Timestamp.h"
  57. #include "Poco/DateTimeFormatter.h"
  58. #include "Poco/DateTimeFormat.h"
  59. #include "Poco/ClassLibrary.h"
  60. using Poco::Net::HTTPRequestHandler;
  61. using Poco::Net::HTTPRequestHandlerFactory;
  62. using Poco::Net::HTTPServerRequest;
  63. using Poco::Net::HTTPServerResponse;
  64. using Poco::Timestamp;
  65. using Poco::DateTimeFormatter;
  66. using Poco::DateTimeFormat;
  67. class TimeRequestHandler: public HTTPRequestHandler
  68. /// Return a HTML document with the current date and time.
  69. {
  70. public:
  71. TimeRequestHandler()
  72. {
  73. }
  74. void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
  75. {
  76. Timestamp now;
  77. std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
  78. response.setChunkedTransferEncoding(true);
  79. response.setContentType("text/html");
  80. std::ostream& ostr = response.send();
  81. ostr << "<html><head><title>TimeServer powered by POCO ApacheConnector</title>";
  82. ostr << "<meta http-equiv=\"refresh\" content=\"1\"></head>";
  83. ostr << "<body><p style=\"text-align: center; font-size: 48px;\">";
  84. ostr << dt;
  85. ostr << "</p></body></html>";
  86. }
  87. };
  88. class TimeRequestHandlerFactory: public HTTPRequestHandlerFactory
  89. {
  90. public:
  91. TimeRequestHandlerFactory()
  92. {
  93. }
  94. HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
  95. {
  96. return new TimeRequestHandler;
  97. }
  98. };
  99. POCO_BEGIN_MANIFEST(HTTPRequestHandlerFactory)
  100. POCO_EXPORT_CLASS(TimeRequestHandlerFactory)
  101. POCO_END_MANIFEST
  102. ----
  103. Both the TimeRequestHandler class and the TimeRequestHandlerFactory class are the same as for
  104. an ordinary Poco::Net::HTTPServer. The only addition in this sample is the definition
  105. of the class loader manifest, which allows the ApacheConnector to load the TimeRequestHandlerFactory
  106. class from the shared library.
  107. To run this sample, the following directives must be added to the Apache configuration files:
  108. LoadModule poco_module p:/Poco/ApacheConnector/bin/mod_poco.so
  109. AddPocoRequestHandler TimeRequestHandlerFactory p:/Poco/ApacheConnector/samples/TimeServer/bin/TimeServer.dll /time
  110. ----
  111. You'll have to change the paths to <[mod_poco.so]> and <[TimeServer.dll]> to match your
  112. own environment.
  113. To test the sample, simply direct your favorite web browser to <http://localhost/time>.