From 226fd63fab4b5b605c62049c6a9d765de8d6c4db Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 2 Jan 2011 21:22:57 +0100 Subject: Add PathList, fix path_p. --- http_connection.cpp | 15 +++++++++++++-- http_connection.h | 10 +++++++++- httpd.cpp | 10 +--------- 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::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 urlchar_p = (qi::char_ - '%') | ('%' >> qi::uint_parser()); - qi::rule()> path_p = '/' >> +(urlchar_p - '/' - '?' - ' ') % '/'; + qi::rule path_p = *(+qi::lit('/') >> +(urlchar_p - '/' - '?' - ' ')) >> *qi::lit('/'); qi::rule()> pair_p = +(urlchar_p - '=') >> '=' >> +(urlchar_p - '&' - ' '); qi::rule()> 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 p; typedef boost::function Handler; + typedef std::list PathList; + //! Start reading the request headers. void read_request(Handler callback); //! Request method. std::string method; //! Request path. - std::vector 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 args; diff --git a/httpd.cpp b/httpd.cpp index e2470c2..4d6ed02 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -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. diff --git a/main.cpp b/main.cpp index c66c6ec..ebe3554 100644 --- a/main.cpp +++ b/main.cpp @@ -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"; -- cgit v1.2.3