summaryrefslogtreecommitdiff
path: root/http_connection.cpp
blob: 36c0f000ab40a6af7e1fca8b2eea2d417251bfea (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
#include "http_connection.h"

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

#include <boost/bind.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>

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::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
	std::istream is(&buf);
	HTTPRequest req(is);

	HTTPResponse res(socket);

	MusicListing::p ml = music::get(req.path);
	if(ml) {
		res.code = 200;
		res.status = "OK";

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

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