diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 20:48:35 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 20:48:35 +0100 |
commit | 1a8f351248c38445189a397035e8a2cb3182ea6a (patch) | |
tree | 507c96dc5fc1e158f799cdeb13cfab382a7a0534 /httpd.cpp | |
parent | 263097e22bdf0a56007644e4d19605371dc79a8f (diff) |
Added HTTPRequest and HTTPResponse classes.
Diffstat (limited to 'httpd.cpp')
-rw-r--r-- | httpd.cpp | 36 |
1 files changed, 12 insertions, 24 deletions
@@ -1,5 +1,6 @@ #include "httpd.h" #include "music.h" +#include "http.h" #include <boost/bind.hpp> #include <boost/algorithm/string/split.hpp> @@ -17,42 +18,29 @@ void HTTPConnection::handle_write(const boost::system::error_code& error, size_t void HTTPConnection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { std::istream is(&buf); - std::string line, firstline; - std::getline(is, firstline); - std::string type, path, httpver; - std::vector<std::string> splitvec; - boost::algorithm::split(splitvec, firstline, boost::algorithm::is_space()); - - type = splitvec[0]; - path = splitvec[1]; - httpver = splitvec[2]; - std::cout << boost::format("%s %s %s\n") % type % path % httpver; - - if(boost::algorithm::ends_with(path, "/")) - path = path.substr(0, path.size()-1); + HTTPRequest req(is); boost::asio::streambuf b; std::ostream os(&b); - MusicListing *ml = music::find(path); + HTTPResponse res; + + MusicListing *ml = music::find(req.path); if(ml) { - os << "HTTP/1.1 200 OK\r\n"; - os << "content-type: text/html\r\n"; - os << "\r\n"; + res.code = 200; + res.status = "OK"; + res.add_header("content-type", "text/html"); + res.write_headers(os); ml->render(os); } else { - os << "HTTP/1.1 404 Not Found\r\n"; - os << "\r\n"; + res.code = 404; + res.status = "Not Found"; + res.write_headers(os); } boost::asio::write(socket, b); - - while(is.good()) { - std::getline(is, line); - std::cout << line << std::endl; - } } HTTPConnection::pointer HTTPConnection::create(boost::asio::io_service& io_service) { |