From 65e25154a92ea47384bc540c129288ca9dad96cb Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 10 Jan 2011 13:02:25 +0100 Subject: Added HTTP::Static for serving static content. --- http_static.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 http_static.cpp (limited to 'http_static.cpp') diff --git a/http_static.cpp b/http_static.cpp new file mode 100644 index 0000000..7f56744 --- /dev/null +++ b/http_static.cpp @@ -0,0 +1,33 @@ +#include "http_static.h" + +#include +#include + +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); + } +} -- cgit v1.2.3