summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--http_connection.cpp38
-rw-r--r--http_connection.h26
-rw-r--r--httpd.cpp53
-rw-r--r--httpd.h57
-rw-r--r--main.cpp6
5 files changed, 101 insertions, 79 deletions
diff --git a/http_connection.cpp b/http_connection.cpp
new file mode 100644
index 0000000..36c0f00
--- /dev/null
+++ b/http_connection.cpp
@@ -0,0 +1,38 @@
+#include "http_connection.h"
+
+#include "http.h"
+#include "music.h"
+
+#include <boost/bind.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/classification.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+
+HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) {
+}
+
+void HTTP::Connection::handle_write(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) {
+ 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";
+ }
+}
+
+void HTTP::Connection::start() {
+ 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));
+}
diff --git a/http_connection.h b/http_connection.h
new file mode 100644
index 0000000..f166f44
--- /dev/null
+++ b/http_connection.h
@@ -0,0 +1,26 @@
+#ifndef HTTP_CONNECTION_H
+#define HTTP_CONNECTION_H
+
+#include <boost/asio.hpp>
+#include <boost/enable_shared_from_this.hpp>
+using boost::asio::ip::tcp;
+
+namespace HTTP {
+ class Connection : public boost::enable_shared_from_this<Connection> {
+ friend class Server;
+
+ private:
+ Connection(boost::asio::io_service& io_service);
+ void handle_write(const boost::system::error_code& error, size_t bytes_transferred);
+ void handle_read(const boost::system::error_code& error, size_t bytes_transferred);
+ tcp::socket socket;
+ boost::asio::streambuf buf;
+
+ public:
+ typedef boost::shared_ptr<Connection> p;
+
+ void start();
+ };
+};
+
+#endif
diff --git a/httpd.cpp b/httpd.cpp
index 9e6d132..d67533d 100644
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -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();
diff --git a/httpd.h b/httpd.h
index f9c0930..0fd601f 100644
--- a/httpd.h
+++ b/httpd.h
@@ -1,37 +1,30 @@
#ifndef HTTPD_H
#define HTTPD_H
-#include <boost/asio.hpp>
-#include <boost/enable_shared_from_this.hpp>
-
-using boost::asio::ip::tcp;
-
-class HTTPConnection : public boost::enable_shared_from_this<HTTPConnection> {
- friend class HTTPServer;
-
- private:
- HTTPConnection(boost::asio::io_service& io_service);
- void handle_write(const boost::system::error_code& error, size_t bytes_transferred);
- void handle_read(const boost::system::error_code& error, size_t bytes_transferred);
- tcp::socket socket;
- boost::asio::streambuf buf;
-
- public:
- typedef boost::shared_ptr<HTTPConnection> pointer;
-
- static pointer create(boost::asio::io_service& io_service);
- void start();
-};
-
-class HTTPServer {
- private:
- void start_accept();
- void handle_accept(HTTPConnection::pointer new_connection, const boost::system::error_code& error);
-
- tcp::acceptor acceptor_;
-
- public:
- HTTPServer(boost::asio::io_service& io_service, const tcp::endpoint& endpoint);
-};
+#include "http_connection.h"
+
+#include <string>
+#include <map>
+
+#include <boost/function.hpp>
+
+namespace HTTP {
+ typedef boost::function<void (Connection::p)> Handler;
+
+ class Server {
+ public:
+ Server(boost::asio::io_service& io_service, const tcp::endpoint& endpoint);
+
+ void add_handler(const std::string& name, Handler handler);
+
+ private:
+ void start_accept();
+ void handle_accept(Connection::p new_connection, const boost::system::error_code& error);
+
+ tcp::acceptor acceptor_;
+
+ std::map<std::string, Handler> handlers;
+ };
+}
#endif
diff --git a/main.cpp b/main.cpp
index c102a5b..19695cf 100644
--- a/main.cpp
+++ b/main.cpp
@@ -13,9 +13,11 @@ int main(int argc, char **argv) {
music::init(argv[1]);
decoder::init();
encoder::init();
-
+
boost::asio::io_service io_service;
- HTTPServer httpd(io_service, tcp::endpoint(tcp::v6(), 8000));
+
+ HTTP::Server httpd(io_service, tcp::endpoint(tcp::v6(), 8000));
+
std::vector<boost::shared_ptr<boost::thread> > threads;
for(std::size_t i = 0; i < 10; i++) {
boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service)));