Rewrite middleware to support epoll

This commit is contained in:
2019-06-29 12:46:29 +02:00
parent 72a6a745ff
commit 4d90d18660
27 changed files with 670 additions and 318 deletions

View File

@@ -4,6 +4,7 @@
#include <ios>
#include "staticcontent.hpp"
#include <sstream>
#include <unistd.h>
namespace Middleware
{
@@ -48,17 +49,17 @@ namespace Middleware
return false;
}
void StaticContent::HandleRequest(Http::Request const & request, Http::Response & response)
void StaticContent::HandleRequest(State & state, ScheduleHelper & scheduleHelper)
{
if (!(request.type == HttpRequest::Type::GET || request.type == HttpRequest::Type::HEAD))
if (!(state.request.type == HttpRequest::Type::GET || state.request.type == HttpRequest::Type::HEAD))
{
return;
}
std::string path;
if (request.url.HasPath())
if (state.request.url.HasPath())
{
path = root + request.url.GetPath();
path = root + state.request.url.GetPath();
}
else
{
@@ -66,20 +67,31 @@ namespace Middleware
path = root + "/index.html";
}
if (ContainsDoubleDots(request.url.GetPath()))
if (ContainsDoubleDots(state.request.url.GetPath()))
{
// We cannot deal with this, we are not going to bother checking if
// this double dot escapes our root directory
return;
}
if (request.type == HttpRequest::Type::GET && !TryReadAllBytes(path, response.content))
if (access(path.c_str(), F_OK) != 0)
{
// File does not exist
return;
}
response.code = HttpResponse::Code::OK;
response.contentType = Http::GetMimeType(path);
state.response.code = HttpResponse::Code::OK;
state.response.contentType = Http::GetMimeType(path);
// Regular file descriptors are not supported by epoll, so we have to "just read it"
if (state.request.type == HttpRequest::Type::GET && !TryReadAllBytes(path, state.response.content))
{
state.response.code = HttpResponse::Code::INTERNAL_SERVER_ERROR;
state.response.contentType = Http::GetMimeType("");
}
// HEAD request
state.finished = true;
return;
}