summaryrefslogtreecommitdiff
path: root/http_connection.cpp
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2011-01-02 16:13:15 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2011-01-02 16:13:15 +0100
commit4a0d339854536f65b9418c79b617bb718062905f (patch)
treec7b80fee61114197269beb37af7dc345c81336db /http_connection.cpp
parent1ec379a805fdc4e7e076f1f59fa054bde8cf89c2 (diff)
Decoupled HTTP::Connection from handler.
Diffstat (limited to 'http_connection.cpp')
-rw-r--r--http_connection.cpp26
1 files changed, 4 insertions, 22 deletions
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 <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
@@ -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));
}