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. --- http_connection.cpp | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) (limited to 'http_connection.cpp') diff --git a/http_connection.cpp b/http_connection.cpp index ef28089..0e9571c 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -1,7 +1,6 @@ #include "http_connection.h" #include "http.h" -#include "music.h" #include #include @@ -15,24 +14,7 @@ HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_se void HTTP::Connection::handle_write(const boost::system::error_code& error, size_t bytes_transferred) { } -void HTTP::Connection::foo_handler(HTTP::Connection::p connection) { - std::cout << "Handling!" << std::endl; - - HTTPResponse res(connection->socket); - - MusicListing::p ml = music::get(connection->path); - if(ml) { - res.code = 200; - res.status = "OK"; - - ml->render(connection, res); - } else { - res.code = 404; - res.status = "Not Found"; - } -} - -void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { +void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred, Handler callback) { if(!parse_request(buf)) { // Request parse error. std::cout << "Request parse error." << std::endl; @@ -54,7 +36,7 @@ void HTTP::Connection::handle_read(const boost::system::error_code& error, size_ std::cout << " " << it->first << " = " << it->second << std::endl; } - foo_handler(shared_from_this()); + callback(shared_from_this()); } void print(char c) { @@ -92,7 +74,7 @@ bool HTTP::Connection::parse_request(boost::asio::streambuf& buf) { method, path, args, version, headers); } -void HTTP::Connection::start() { +void HTTP::Connection::read_request(Handler callback) { boost::asio::async_read_until(socket, buf, "\r\n\r\n", boost::bind(&Connection::handle_read, shared_from_this(), - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, callback)); } -- cgit v1.2.3