1#include "ResourceExecutor.h"
5#include "Import/WebFramework.h"
6#include "Import/WebFrameworkConstants.h"
7#include "Exceptions/FileDoesNotExistException.h"
8#include "Exceptions/BadRequestException.h"
14 void ResourceExecutor::loadHTMLErrorsData()
16 filesystem::path allErrorsFolder(defaultAssets / web_framework_assets::errorsFolder);
17 ifstream html(allErrorsFolder / web_framework_assets::badRequest);
18 auto readFile = [](ifstream& html) ->
string
23 while (getline(html, tem))
33 HTMLErrorsData[HTMLErrors::badRequest400] = readFile(html);
35 html.open(allErrorsFolder / web_framework_assets::notFound);
37 HTMLErrorsData[HTMLErrors::notFound404] = readFile(html);
39 html.open(allErrorsFolder / web_framework_assets::internalServerError);
41 HTMLErrorsData[HTMLErrors::internalServerError500] = readFile(html);
44 void ResourceExecutor::readFile(
string& result, unique_ptr<file_manager::ReadFileHandle>&& handle)
46 result = handle->readAllData();
50 throw exceptions::BadRequestException(
"File is empty");
54 ResourceExecutor::ResourceExecutor(
const json::JSONParser& configuration,
const filesystem::path& assets, uint64_t cachingSize,
const filesystem::path& pathToTemplates) :
57 configuration.getObject(json_settings::webFrameworkObject).contains(json_settings::webFrameworkDefaultAssetsPath, json::utility::variantTypeEnum::jString) ?
58 configuration.getObject(json_settings::webFrameworkObject).getString(json_settings::webFrameworkDefaultAssetsPath) :
59 webFrameworkDefaultAssests
62 dynamicPages(pathToTemplates),
63 fileManager(file_manager::FileManager::getInstance())
65 fileManager.getCache().setCacheSize(cachingSize);
70 if (!filesystem::exists(assets))
72 filesystem::create_directories(assets);
75 if (!filesystem::exists(dynamicPages.getPathToTemplates()))
77 filesystem::create_directories(dynamicPages.getPathToTemplates());
80 this->loadHTMLErrorsData();
83 void ResourceExecutor::sendStaticFile(
const string& filePath,
HTTPResponse& response,
bool isBinary,
const string& fileName)
86 filesystem::path assetFilePath(assets / filePath);
88 if (!filesystem::exists(assetFilePath))
90 throw file_manager::exceptions::FileDoesNotExistException(assetFilePath);
95 fileManager.readBinaryFile(assetFilePath, bind(&ResourceExecutor::readFile,
this, ref(result), placeholders::_1));
99 fileManager.readFile(assetFilePath, bind(&ResourceExecutor::readFile,
this, ref(result), placeholders::_1));
104 response.
addHeader(
"Content-Disposition", format(R
"(attachment; filename="{}")", fileName));
107 response.addBody(move(result));
110 void ResourceExecutor::sendDynamicFile(
const string& filePath,
HTTPResponse& response,
const unordered_map<string, string>& variables,
bool isBinary,
const string& fileName)
113 filesystem::path assetFilePath(assets / filePath);
115 if (!filesystem::exists(assetFilePath))
117 throw file_manager::exceptions::FileDoesNotExistException(assetFilePath);
122 fileManager.readBinaryFile(assetFilePath, bind(&ResourceExecutor::readFile,
this, ref(result), placeholders::_1));
126 fileManager.readFile(assetFilePath, bind(&ResourceExecutor::readFile,
this, ref(result), placeholders::_1));
129 dynamicPages.run(variables, result);
133 response.
addHeader(
"Content-Disposition", format(R
"(attachment; filename="{}")", fileName));
136 response.addBody(move(result));
139 void ResourceExecutor::registerDynamicFunction(
const string& functionName, function<
string(
const vector<string>&)>&& function)
141 dynamicPages.registerDynamicFunction(functionName, move(function));
144 void ResourceExecutor::unregisterDynamicFunction(
const string& functionName)
146 dynamicPages.unregisterDynamicFunction(functionName);
149 bool ResourceExecutor::isDynamicFunctionRegistered(
const string& functionName)
151 return dynamicPages.isDynamicFunctionRegistered(functionName);
164 const filesystem::path& ResourceExecutor::getPathToAssets()
const
169 void ResourceExecutor::notFoundError(
HTTPResponse& response,
const exception* exception)
171 const string& message = HTMLErrorsData[HTMLErrors::notFound404];
178 response.
addBody(format(
"{} Exception: {}", message, exception->what()));
189 void ResourceExecutor::badRequestError(
HTTPResponse& response,
const exception* exception)
191 const string& message = HTMLErrorsData[HTMLErrors::badRequest400];
198 response.
addBody(format(
"{} Exception: {}", message, exception->what()));
209 void ResourceExecutor::internalServerError(
HTTPResponse& response,
const exception* exception)
211 const string& message = HTMLErrorsData[HTMLErrors::internalServerError500];
218 response.
addBody(format(
"{} Exception: {}", message, exception->what()));
229 bool ResourceExecutor::getIsCaching()
const
231 return fileManager.getCache().getCacheSize();
const std::string & getRawParameters() const
Parameters string from HTTP.
void sendAssetFile(const std::string &filePath, HTTPResponse &response, const std::unordered_map< std::string, std::string > &variables={}, bool isBinary=true, const std::string &fileName="")
ResourceExecutor wrapper.
void addBody(const std::string &body)
void addHeader(const std::string &name, const std::string &value)
Set additional HTTP header.
void setResponseCode(web::responseCodes code)
Set HTTP response code.