some progress in static file submitting
This commit is contained in:
parent
e49fa42f7e
commit
67f49d96eb
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
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_;
|
||||
};
|
|
@ -0,0 +1,26 @@
|
|||
#include "InputMemmoryFile.H"
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
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<const char*>(::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<char*>(data_), size_);
|
||||
::close(file_handle_);
|
||||
}
|
|
@ -1,4 +1,7 @@
|
|||
#include "StaticFileHandler.H"
|
||||
//#include "InputMemmoryFile.H"
|
||||
|
||||
|
||||
|
||||
static const char * hello_world_html = u8R"HERE(
|
||||
<!DOCTYPE html>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue