diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-12-29 22:08:06 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-12-29 22:08:06 +0100 |
commit | 5d94fe647e59aaec7775f1e1bd4a4982b677af01 (patch) | |
tree | e459388b0d937bdb4034676c1a8276c59904fc68 /httpd.cpp | |
parent | 2133fd579e0d4726b032288d10d053231109c586 (diff) |
HTTPServer/HTTPConnection cleanup.
Diffstat (limited to 'httpd.cpp')
-rw-r--r-- | httpd.cpp | 53 |
1 files changed, 8 insertions, 45 deletions
@@ -1,58 +1,21 @@ #include "httpd.h" -#include "music.h" -#include "http.h" #include <boost/bind.hpp> -#include <boost/algorithm/string/split.hpp> -#include <boost/algorithm/string/classification.hpp> -#include <boost/algorithm/string/predicate.hpp> -#include <boost/format.hpp> -#include <iostream> - -HTTPConnection::HTTPConnection(boost::asio::io_service& io_service) : socket(io_service) { -} - -void HTTPConnection::handle_write(const boost::system::error_code& error, size_t bytes_transferred) { -} - -void HTTPConnection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { - std::istream is(&buf); - HTTPRequest req(is); - - HTTPResponse res(socket); - - MusicListing::p ml = music::get(req.path); - if(ml) { - res.code = 200; - res.status = "OK"; - - ml->render(req, res); - } else { - res.code = 404; - res.status = "Not Found"; - } -} - -HTTPConnection::pointer HTTPConnection::create(boost::asio::io_service& io_service) { - return pointer(new HTTPConnection(io_service)); -} - -void HTTPConnection::start() { - boost::asio::async_read_until(socket, buf, "\r\n\r\n", boost::bind(&HTTPConnection::handle_read, shared_from_this(), - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); +HTTP::Server::Server(boost::asio::io_service& io_service, const tcp::endpoint& endpoint) : acceptor_(io_service, endpoint) { + start_accept(); } -HTTPServer::HTTPServer(boost::asio::io_service& io_service, const tcp::endpoint& endpoint) : acceptor_(io_service, endpoint) { - start_accept(); +void HTTP::Server::add_handler(const std::string& name, Handler handler) { + handlers[name] = handler; } -void HTTPServer::start_accept() { - HTTPConnection::pointer new_connection = HTTPConnection::create(acceptor_.io_service()); - acceptor_.async_accept(new_connection->socket, boost::bind(&HTTPServer::handle_accept, this, new_connection, boost::asio::placeholders::error)); +void HTTP::Server::start_accept() { + Connection::p new_connection = Connection::p(new Connection(acceptor_.io_service())); + acceptor_.async_accept(new_connection->socket, boost::bind(&Server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } -void HTTPServer::handle_accept(HTTPConnection::pointer new_connection, const boost::system::error_code& error) { +void HTTP::Server::handle_accept(Connection::p new_connection, const boost::system::error_code& error) { if(!error) { new_connection->start(); start_accept(); |