Initial commit

This commit is contained in:
2019-06-15 12:00:21 +02:00
commit eda5d9df6b
31 changed files with 1328 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
#include "../http/mime.hpp"
#include "../logger.hpp"
#include <filesystem>
#include <fstream>
#include <ios>
#include "staticcontent.hpp"
#include <sstream>
namespace Middleware
{
void ReadAllBytes(std::filesystem::path const & path, std::vector<char> & buffer)
{
std::ifstream ifs(path, std::ios_base::binary | std::ios_base::ate);
std::ifstream::pos_type length = ifs.tellg();
auto const oldBufferSize = buffer.size();
buffer.resize(oldBufferSize + length);
ifs.seekg(0, std::ios_base::beg);
ifs.read(&buffer[oldBufferSize], length);
}
void StaticContent::HandleRequest(Http::Request const & request, Http::Response & response)
{
switch(request.requestType)
{
case HttpRequest::Type::GET:
break;
default:
{
return;
}
}
std::filesystem::path path;
if (request.path.size() == 1)
{
// TODO make configurable?
path = root + "/index.html";
}
else
{
path = root + request.path;
}
if (!std::filesystem::exists(path))
{
std::stringstream ss;
ss << "Static file <";
ss << path.string();
ss << "> not found";
Logger::GetInstance().Info(ss.str());
return;
}
response.code = HttpResponse::Code::OK;
ReadAllBytes(path, response.content);
response.contentType = Http::GetMimeType(path);
return;
}
StaticContent::StaticContent(std::string const & staticFileRoot)
: root(staticFileRoot)
{
std::stringstream ss;
ss << "Using static file root " << root;
Logger::GetInstance().Info(ss.str());
}
}