summaryrefslogtreecommitdiff
path: root/http_connection_parser.cpp
blob: fc1b948cc21177e938556ef481c0cdb5488174b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "http_connection.h"

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;

bool HTTP::Connection::parse_request(boost::asio::streambuf& buf) {
	std::string data(boost::asio::buffers_begin(buf.data()), boost::asio::buffers_end(buf.data()));
	
	typedef std::string::const_iterator Iterator;
	
	Iterator begin = data.begin();
	Iterator end = data.end();
	
	qi::rule<Iterator, std::string()> word_p = +(qi::char_ - ' ' - '\r');
	
	qi::rule<Iterator, char()> urlchar_p = (qi::char_ - '%') | ('%' >> qi::uint_parser<char, 16, 2, 2>());
	
	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);
	
	qi::rule<Iterator, std::pair<std::string, std::string>()> header_p = +(qi::char_ - ':') >> ": " >> +(qi::char_ - '\r');
	qi::rule<Iterator, std::map<std::string, std::string>()> 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);
}