summaryrefslogtreecommitdiff
path: root/poco-httpd/poco-httpd.cpp
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2013-02-13 17:11:43 +0000
committerComment <tim@gray.(none)>2013-02-13 17:11:43 +0000
commit95bb2236ac69b3ae54bb84f91a58a89491b94240 (patch)
treec29070fa03b8f3a430081b3dcc6ba0ce75b7bc02 /poco-httpd/poco-httpd.cpp
initial commit
Diffstat (limited to 'poco-httpd/poco-httpd.cpp')
-rw-r--r--poco-httpd/poco-httpd.cpp169
1 files changed, 169 insertions, 0 deletions
diff --git a/poco-httpd/poco-httpd.cpp b/poco-httpd/poco-httpd.cpp
new file mode 100644
index 0000000..5ca33b6
--- /dev/null
+++ b/poco-httpd/poco-httpd.cpp
@@ -0,0 +1,169 @@
+#include "Poco/Net/HTTPServer.h"
+#include "Poco/Net/HTTPRequestHandler.h"
+#include "Poco/Net/HTTPRequestHandlerFactory.h"
+#include "Poco/Net/HTTPServerParams.h"
+#include "Poco/Net/HTTPServerRequest.h"
+#include "Poco/Net/HTTPServerResponse.h"
+#include "Poco/Net/HTTPServerParams.h"
+#include "Poco/Net/ServerSocket.h"
+#include "Poco/Timestamp.h"
+#include "Poco/DateTimeFormatter.h"
+#include "Poco/DateTimeFormat.h"
+#include "Poco/Exception.h"
+#include "Poco/ThreadPool.h"
+#include "Poco/Util/ServerApplication.h"
+#include "Poco/Util/Option.h"
+#include "Poco/Util/OptionSet.h"
+#include "Poco/Util/HelpFormatter.h"
+#include <iostream>
+
+using Poco::Net::ServerSocket;
+using Poco::Net::HTTPRequestHandler;
+using Poco::Net::HTTPRequestHandlerFactory;
+using Poco::Net::HTTPServer;
+using Poco::Net::HTTPServerRequest;
+using Poco::Net::HTTPServerResponse;
+using Poco::Net::HTTPServerParams;
+using Poco::Timestamp;
+using Poco::DateTimeFormatter;
+using Poco::DateTimeFormat;
+using Poco::ThreadPool;
+using Poco::Util::ServerApplication;
+using Poco::Util::Application;
+using Poco::Util::Option;
+using Poco::Util::OptionSet;
+using Poco::Util::OptionCallback;
+using Poco::Util::HelpFormatter;
+
+class TimeRequestHandler: public HTTPRequestHandler
+{
+public:
+ TimeRequestHandler(const std::string& format): _format(format)
+ {
+ }
+
+ void handleRequest(HTTPServerRequest& request,
+ HTTPServerResponse& response)
+ {
+ Application& app = Application::instance();
+ app.logger().information("Request from "
+ + request.clientAddress().toString());
+
+ Timestamp now;
+ std::string dt(DateTimeFormatter::format(now, _format));
+
+ response.setChunkedTransferEncoding(true);
+ response.setContentType("text/html");
+
+ std::ostream& ostr = response.send();
+ ostr << "<html><head><title>HTTPTimeServer powered by "
+ "POCO C++ Libraries</title>";
+ ostr << "<meta http-equiv=\"refresh\" content=\"1\"></head>";
+ ostr << "<body><p style=\"text-align: center; "
+ "font-size: 48px;\">";
+ ostr << dt;
+ ostr << "</p></body></html>";
+ }
+
+private:
+ std::string _format;
+};
+
+class TimeRequestHandlerFactory: public HTTPRequestHandlerFactory
+{
+public:
+ TimeRequestHandlerFactory(const std::string& format):
+ _format(format)
+ {
+ }
+
+ HTTPRequestHandler* createRequestHandler(
+ const HTTPServerRequest& request)
+ {
+ if (request.getURI() == "/")
+ return new TimeRequestHandler(_format);
+ else
+ return 0;
+ }
+
+private:
+ std::string _format;
+};
+
+class HTTPTimeServer: public Poco::Util::ServerApplication
+{
+public:
+ HTTPTimeServer(): _helpRequested(false)
+ {
+ }
+
+ ~HTTPTimeServer()
+ {
+ }
+
+protected:
+ void initialize(Application& self)
+ {
+ loadConfiguration();
+ ServerApplication::initialize(self);
+ }
+
+ void uninitialize()
+ {
+ ServerApplication::uninitialize();
+ }
+
+ void defineOptions(OptionSet& options)
+ {
+ ServerApplication::defineOptions(options);
+
+ options.addOption(
+ Option("help", "h", "display argument help information")
+ .required(false)
+ .repeatable(false)
+ .callback(OptionCallback<HTTPTimeServer>(
+ this, &HTTPTimeServer::handleHelp)));
+ }
+
+ void handleHelp(const std::string& name,
+ const std::string& value)
+ {
+ HelpFormatter helpFormatter(options());
+ helpFormatter.setCommand(commandName());
+ helpFormatter.setUsage("OPTIONS");
+ helpFormatter.setHeader(
+ "A web server that serves the current date and time.");
+ helpFormatter.format(std::cout);
+ stopOptionsProcessing();
+ _helpRequested = true;
+ }
+
+ int main(const std::vector<std::string>& args)
+ {
+ if (!_helpRequested)
+ {
+ unsigned short port = (unsigned short)
+ config().getInt("HTTPTimeServer.port", 9980);
+ std::string format(
+ config().getString("HTTPTimeServer.format",
+ DateTimeFormat::SORTABLE_FORMAT));
+
+ ServerSocket svs(port);
+ HTTPServer srv(new TimeRequestHandlerFactory(format),
+ svs, new HTTPServerParams);
+ srv.start();
+ waitForTerminationRequest();
+ srv.stop();
+ }
+ return Application::EXIT_OK;
+ }
+
+private:
+ bool _helpRequested;
+};
+
+int main(int argc, char** argv)
+{
+ HTTPTimeServer app;
+ return app.run(argc, argv);
+} \ No newline at end of file