summaryrefslogtreecommitdiff
path: root/http.cpp
blob: 8d69f93e1f40efbb86e326726388004b9266ed0c (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
#include "http.h"

#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>

#include <vector>

HTTPRequest::HTTPRequest(std::istream& is) {
	std::string line, firstline;
	std::getline(is, firstline);

	std::vector<std::string> splitvec;
	boost::algorithm::split(splitvec, firstline, boost::algorithm::is_space());

	type = splitvec[0];
	path = splitvec[1];
	httpver = splitvec[2];
	std::cout << boost::format("%s %s %s\n") % type % path % httpver;

	while(is.good()) {
		std::string line;
		std::getline(is, line);
		boost::trim(line);
		if(!line.size()) continue;
		std::vector<std::string> v;
		boost::algorithm::split_regex(v, line, boost::regex(": "));
		headers[v[0]] = v[1];
	}
}

HTTPResponse::HTTPResponse() {
	httpver = "1.1";
}

void HTTPResponse::add_header(std::string key, std::string value) {
	headers[key] = value;
}

void HTTPResponse::write_headers(std::ostream& os) {
	os << boost::format("HTTP/%s %d %s\r\n") % httpver % code % status;
	for(HTTPHeaders::iterator it = headers.begin(); it != headers.end(); it++) {
		os << boost::format("%s: %s\r\n") % it->first % it->second;
	}
	os << "\r\n";
}