summaryrefslogtreecommitdiff
path: root/http_static.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'http_static.cpp')
-rw-r--r--http_static.cpp33
1 files changed, 33 insertions, 0 deletions
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 <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);
+ }
+}