diff options
-rw-r--r-- | http_connection.cpp | 15 | ||||
-rw-r--r-- | http_connection.h | 10 | ||||
-rw-r--r-- | httpd.cpp | 10 | ||||
-rw-r--r-- | main.cpp | 5 |
4 files changed, 26 insertions, 14 deletions
diff --git a/http_connection.cpp b/http_connection.cpp index 0e9571c..d940b2f 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -22,7 +22,7 @@ void HTTP::Connection::handle_read(const boost::system::error_code& error, size_ } std::cout << "Path: " << std::endl; - for(std::vector<std::string>::iterator it = path.begin(); it != path.end(); it++) { + for(PathList::iterator it = path.begin(); it != path.end(); it++) { std::cout << " " << *it << std::endl; } @@ -55,7 +55,7 @@ bool HTTP::Connection::parse_request(boost::asio::streambuf& buf) { qi::rule<Iterator, char()> urlchar_p = (qi::char_ - '%') | ('%' >> qi::uint_parser<char, 16, 2, 2>()); - qi::rule<Iterator, std::vector<std::string>()> path_p = '/' >> +(urlchar_p - '/' - '?' - ' ') % '/'; + qi::rule<Iterator, PathList()> path_p = *(+qi::lit('/') >> +(urlchar_p - '/' - '?' - ' ')) >> *qi::lit('/'); qi::rule<Iterator, std::pair<std::string, std::string>()> pair_p = +(urlchar_p - '=') >> '=' >> +(urlchar_p - '&' - ' '); qi::rule<Iterator, std::map<std::string, std::string>()> args_p = '?' >> pair_p >> *('&' >> pair_p); @@ -78,3 +78,14 @@ 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, callback)); } + +std::string HTTP::Connection::pop_path_base() { + if(!path.size()) { + return ""; + } + + base_path.push_back(path.front()); + path.pop_front(); + + return base_path.back(); +} diff --git a/http_connection.h b/http_connection.h index 9ceb8a5..23cadc6 100644 --- a/http_connection.h +++ b/http_connection.h @@ -18,14 +18,22 @@ namespace HTTP { public: typedef boost::shared_ptr<Connection> p; typedef boost::function<void (Connection::p)> Handler; + typedef std::list<std::string> PathList; + //! Start reading the request headers. void read_request(Handler callback); //! Request method. std::string method; //! Request path. - std::vector<std::string> path; + PathList path; + + //! Base path. + PathList base_path; + + //! Pop topmost element of path and add to base_path. + std::string pop_path_base(); //! Request arguments. std::map<std::string, std::string> args; @@ -23,15 +23,7 @@ void HTTP::Server::handle_accept(Connection::p new_connection, const boost::syst } void HTTP::Server::handle_request(Connection::p connection) { - std::string handler; - - if(connection->path.size()) { - // Pop first element of path. - handler = connection->path.front(); - connection->path.erase(connection->path.begin()); - } else { - handler = "index"; - } + std::string handler = connection->pop_path_base(); if(handlers.count(handler)) { // Call handler. @@ -15,12 +15,13 @@ void foo_handler(HTTP::Connection::p connection) { HTTPResponse res(connection->socket); - MusicListing::p ml = music::get(connection->path); + //MusicListing::p ml = music::get(connection->path); + bool ml = 0; if(ml) { res.code = 200; res.status = "OK"; - ml->render(connection, res); + //ml->render(connection, res); } else { res.code = 404; res.status = "Not Found"; |