From 67f49d96ebd090420d37bfac5ed0ec19fbfdc509 Mon Sep 17 00:00:00 2001 From: john stone Date: Sat, 18 Jan 2014 14:27:55 +0100 Subject: [PATCH] some progress in static file submitting --- src/InputMemmoryFile.H | 15 +++++++++++++++ src/InputMemmoryFile.cc | 26 ++++++++++++++++++++++++++ src/StaticFileHandler.cc | 21 +++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/InputMemmoryFile.H create mode 100644 src/InputMemmoryFile.cc diff --git a/src/InputMemmoryFile.H b/src/InputMemmoryFile.H new file mode 100644 index 0000000..4060bc3 --- /dev/null +++ b/src/InputMemmoryFile.H @@ -0,0 +1,15 @@ +#pragma once + + +#include +class InputMemoryFile { + public: + explicit InputMemoryFile(const char *pathname); + ~InputMemoryFile(); + const char* data() const { return data_; } + std::size_t size() const { return size_; } + private: + const char* data_; + std::size_t size_; + int file_handle_; +}; diff --git a/src/InputMemmoryFile.cc b/src/InputMemmoryFile.cc new file mode 100644 index 0000000..6973755 --- /dev/null +++ b/src/InputMemmoryFile.cc @@ -0,0 +1,26 @@ +#include "InputMemmoryFile.H" +#include +#include +#include +#include + +InputMemoryFile::InputMemoryFile(const char *pathname): + data_(0), + size_(0), + file_handle_(-1) +{ + + file_handle_ = ::open(pathname, O_RDONLY); + if (file_handle_ == -1) return; + struct stat sbuf; + if (::fstat(file_handle_, &sbuf) == -1) return; + data_ = static_cast(::mmap( + 0, sbuf.st_size, PROT_READ, MAP_SHARED, file_handle_, 0)); + if (data_ == MAP_FAILED) data_ = 0; + else size_ = sbuf.st_size; +} + +InputMemoryFile::~InputMemoryFile() { + ::munmap(const_cast(data_), size_); + ::close(file_handle_); +} diff --git a/src/StaticFileHandler.cc b/src/StaticFileHandler.cc index ac95331..b604272 100644 --- a/src/StaticFileHandler.cc +++ b/src/StaticFileHandler.cc @@ -1,4 +1,7 @@ #include "StaticFileHandler.H" +//#include "InputMemmoryFile.H" + + static const char * hello_world_html = u8R"HERE( @@ -31,7 +34,25 @@ class UltraSimpleStaticFileHandler : StaticFileHandler hello_world_html.size(), hello_world_html.c_str()); return 0; + } }; +class MediocreSimpleStaticFileHandler :StaticFileHandler +{ + private: + std::string cleanpath (const char * const path){ + //adds no security at all + return std::string(path); + } + + public: + int answer_pathreq(const char * const path, + struct mg_connection *conn) + { + mg_send_file(conn,cleanpath(path).to_cstring()); + return 0; + } +} +