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

#include <boost/filesystem/fstream.hpp>
#include <boost/format.hpp>

HTTP::Static::Static(fs::path directory, std::string index) : directory_(directory), index_(index) {
	
}

void HTTP::Static::operator()(Connection::p connection) {
	fs::path path = directory_;
	
	if(connection->path.size()) {
		for(Connection::PathList::iterator it = connection->path.begin(); it != connection->path.end(); it++) {
			path /= *it;
		}
	} else {
		path /= index_;
	}
	
	if(fs::is_regular_file(path)) {
		fs::ifstream is(path, std::ios::in | std::ios::binary);
		is.seekg(0, std::ios::end);
		connection->add_header("content-length", boost::str(boost::format("%d") % is.tellg()));
		is.seekg(0, std::ios::beg);
		
		connection->send_data(is);
	} else if(fs::exists(path)) {
		connection->send_error(403);
	} else {
		connection->send_error(404);
	}
}