Initial commit
This commit is contained in:
17
src/middleware/base.hpp
Normal file
17
src/middleware/base.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "../http/request.hpp"
|
||||
#include "../http/response.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace Middleware
|
||||
{
|
||||
class BaseMiddleware
|
||||
{
|
||||
public:
|
||||
virtual void HandleRequest(Http::Request const & request, Http::Response & response) = 0;
|
||||
|
||||
BaseMiddleware() = default;
|
||||
virtual ~BaseMiddleware() = default;
|
||||
};
|
||||
}
|
||||
27
src/middleware/notfound.cpp
Normal file
27
src/middleware/notfound.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "notfound.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace Middleware
|
||||
{
|
||||
void NotFound::HandleRequest(Http::Request const & request, Http::Response & response)
|
||||
{
|
||||
if (response.code != HttpResponse::Code::UNKNOWN)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
response.code = HttpResponse::Code::NOT_FOUND;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "404 - file not found\n";
|
||||
ss << "File: ";
|
||||
ss << request.path << '\n';
|
||||
|
||||
auto responseContent = ss.str();
|
||||
response.content.insert(response.content.begin(),
|
||||
responseContent.begin(),
|
||||
responseContent.end());
|
||||
|
||||
response.contentType = "text/plain";
|
||||
}
|
||||
}
|
||||
11
src/middleware/notfound.hpp
Normal file
11
src/middleware/notfound.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "base.hpp"
|
||||
|
||||
namespace Middleware
|
||||
{
|
||||
class NotFound : public BaseMiddleware
|
||||
{
|
||||
public:
|
||||
void HandleRequest(Http::Request const & request, Http::Response & Response) override;
|
||||
};
|
||||
}
|
||||
72
src/middleware/staticcontent.cpp
Normal file
72
src/middleware/staticcontent.cpp
Normal 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());
|
||||
}
|
||||
}
|
||||
19
src/middleware/staticcontent.hpp
Normal file
19
src/middleware/staticcontent.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "base.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Middleware
|
||||
{
|
||||
class StaticContent : public BaseMiddleware
|
||||
{
|
||||
private:
|
||||
std::string root;
|
||||
|
||||
public:
|
||||
virtual void HandleRequest(Http::Request const & request, Http::Response & response) override;
|
||||
|
||||
StaticContent(std::string const & staticFileRoot);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user