From 2e2221b2eda1c089688b3e87b42b0dc6720f6c59 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 30 Dec 2010 22:30:23 +0100 Subject: Added request parser based on Boost.Spirit. --- http_connection.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++--- http_connection.h | 23 ++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/http_connection.cpp b/http_connection.cpp index 36c0f00..fb45aac 100644 --- a/http_connection.cpp +++ b/http_connection.cpp @@ -4,9 +4,10 @@ #include "music.h" #include -#include -#include -#include +#include +#include + +namespace qi = boost::spirit::qi; HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) { } @@ -15,7 +16,30 @@ void HTTP::Connection::handle_write(const boost::system::error_code& error, size } void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { + if(!parse_request(buf)) { + // Request parse error. + return; + } + + std::cout << "Path: " << std::endl; + for(std::vector::iterator it = path.begin(); it != path.end(); it++) { + std::cout << " " << *it << std::endl; + } + + std::cout << "Args: " << std::endl; + for(std::map::iterator it = args.begin(); it != args.end(); it++) { + std::cout << " " << it->first << " = " << it->second << std::endl; + } + + std::cout << "Headers: " << std::endl; + for(std::map::iterator it = headers.begin(); it != headers.end(); it++) { + std::cout << " " << it->first << " = " << it->second << std::endl; + } + + return; + std::istream is(&buf); + HTTPRequest req(is); HTTPResponse res(socket); @@ -32,6 +56,37 @@ void HTTP::Connection::handle_read(const boost::system::error_code& error, size_ } } +void print(char c) { + std::cout << "Char: " << int(c) << std::endl; +} + +bool HTTP::Connection::parse_request(boost::asio::streambuf& buf) { + typedef boost::asio::buffers_iterator Iterator; + + Iterator begin = Iterator::begin(buf.data()); + Iterator end = Iterator::end(buf.data()); + + qi::rule word_p = +(qi::char_ - ' ' - '\r'); + + qi::rule()> path_p = '/' >> +(qi::char_ - '/' - '?' - ' ') % '/'; + + qi::rule()> pair_p = +(qi::char_ - '=') >> '=' >> +(qi::char_ - '&' - ' '); + qi::rule()> args_p = '?' >> pair_p >> *('&' >> pair_p); + + qi::rule()> header_p = +(qi::char_ - ':') >> ": " >> +(qi::char_ - '\r'); + qi::rule()> headers_p = *(header_p >> "\r\n"); + + return qi::parse(begin, end, + // Method, path, args, version: + word_p >> ' ' >> path_p >> -args_p >> ' ' >> word_p >> "\r\n" >> + // Headers: + headers_p >> + // End of headers: + "\r\n", + // Store into: + method, path, args, version, headers); +} + 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 index f166f44..b6d6e27 100644 --- a/http_connection.h +++ b/http_connection.h @@ -1,6 +1,10 @@ #ifndef HTTP_CONNECTION_H #define HTTP_CONNECTION_H +#include +#include +#include + #include #include using boost::asio::ip::tcp; @@ -15,6 +19,25 @@ namespace HTTP { void handle_read(const boost::system::error_code& error, size_t bytes_transferred); tcp::socket socket; boost::asio::streambuf buf; + + //! Request method. + std::string method; + + //! Request path. + std::vector path; + + //! Request arguments. + std::map args; + + //! Request version. + std::string version; + + //! Request headers. + std::map headers; + + + //! Parse request headers. + bool parse_request(boost::asio::streambuf& buf); public: typedef boost::shared_ptr p; -- cgit v1.2.3