From 4a0d339854536f65b9418c79b617bb718062905f Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 2 Jan 2011 16:13:15 +0100 Subject: Decoupled HTTP::Connection from handler. --- httpd.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'httpd.cpp') diff --git a/httpd.cpp b/httpd.cpp index d67533d..e2470c2 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -17,7 +17,26 @@ void HTTP::Server::start_accept() { void HTTP::Server::handle_accept(Connection::p new_connection, const boost::system::error_code& error) { if(!error) { - new_connection->start(); + new_connection->read_request(boost::bind(&Server::handle_request, this, _1)); start_accept(); } } + +void HTTP::Server::handle_request(Connection::p connection) { + std::string handler; + + if(connection->path.size()) { + // Pop first element of path. + handler = connection->path.front(); + connection->path.erase(connection->path.begin()); + } else { + handler = "index"; + } + + if(handlers.count(handler)) { + // Call handler. + handlers[handler](connection); + } else { + // Error 404. + } +} -- cgit v1.2.3