summaryrefslogtreecommitdiff
path: root/http_connection.cpp
blob: e6820378eaf3fd917fd10e29be0fd466d76e2f44 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "http_connection.h"

#include "http.h"
#include "music.h"

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

namespace qi = boost::spirit::qi;

HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) {
}

void HTTP::Connection::handle_write(const boost::system::error_code& error, size_t bytes_transferred) {
}

void HTTP::Connection::foo_handler(HTTP::Connection::p connection) {
	std::cout << "Handling!" << std::endl;
	
	HTTPResponse res(connection->socket);
	
	MusicListing::p ml = music::get(connection->path);
	if(ml) {
		res.code = 200;
		res.status = "OK";

		ml->render(connection, res);
	} else {
		res.code = 404;
		res.status = "Not Found";
	}
}

void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
	if(!parse_request(buf)) {
		// Request parse error.
		std::cout << "Request parse error." << std::endl;
		//return;
	}
	
	std::cout << "Path: " << std::endl;
	for(std::vector<std::string>::iterator it = path.begin(); it != path.end(); it++) {
		std::cout << "  " << *it << std::endl;
	}
	
	std::cout << "Args: " << std::endl;
	for(std::map<std::string, std::string>::iterator it = args.begin(); it != args.end(); it++) {
		std::cout << "  " << it->first << " = " << it->second << std::endl;
	}
	
	std::cout << "Headers: " << std::endl;
	for(std::map<std::string, std::string>::iterator it = headers.begin(); it != headers.end(); it++) {
		std::cout << "  " << it->first << " = " << it->second << std::endl;
	}
	
	foo_handler(shared_from_this());
}

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<boost::asio::streambuf::const_buffers_type> Iterator;
	
	Iterator begin = Iterator::begin(buf.data());
	Iterator end = Iterator::end(buf.data());
	
	qi::rule<Iterator, std::string()> word_p = +(qi::char_ - ' ' - '\r');
	
	qi::rule<Iterator, std::vector<std::string>()> path_p = '/' >> +(qi::char_ - '/' - '?' - ' ') % '/';
	
	qi::rule<Iterator, std::pair<std::string, std::string>()> pair_p = +(qi::char_ - '=') >> '=' >> +(qi::char_ - '&' - ' ');
	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);
}

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));
}