WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
ResourceExecutor.h
1#pragma once
2
3#include "Import/WebFrameworkCore.h"
4
5#include "FileManager.h"
6
7#include "BaseStatelessExecutor.h"
8#include "DynamicPages/WebFrameworkDynamicPages.h"
9#include "WebNetwork/Interfaces/IStaticFile.h"
10#include "WebNetwork/Interfaces/IDynamicFile.h"
11
12namespace framework
13{
18 {
19 private:
20 enum HTMLErrors
21 {
22 badRequest400,
23 notFound404,
24 internalServerError500,
25 HTMLErrorsSize
26 };
27
28 private:
29 const std::filesystem::path defaultAssets;
30 const std::filesystem::path assets;
31 WebFrameworkDynamicPages dynamicPages;
32 std::array<std::string, HTMLErrors::HTMLErrorsSize> HTMLErrorsData;
33 file_manager::FileManager& fileManager;
34
35 private:
39 void loadHTMLErrorsData();
40
41 void readFile(std::string& result, std::unique_ptr<file_manager::ReadFileHandle>&& handle);
42
43 public:
44 ResourceExecutor(const json::JSONParser& configuration, const std::filesystem::path& assets, uint64_t cachingSize, const std::filesystem::path& pathToTemplates);
45
50 void init(const utility::JSONSettingsParser::ExecutorSettings& settings) override;
51
58 void sendStaticFile(const std::string& filePath, HTTPResponse& response, bool isBinary = true, const std::string& fileName = "") override;
59
66 void sendDynamicFile(const std::string& filePath, HTTPResponse& response, const std::unordered_map<std::string, std::string>& variables = {}, bool isBinary = true, const std::string& fileName = "") override;
67
71 void registerDynamicFunction(const std::string& functionName, std::function<std::string(const std::vector<std::string>&)>&& function) override;
72
75 void unregisterDynamicFunction(const std::string& functionName) override;
76
80 bool isDynamicFunctionRegistered(const std::string& functionName) override;
81
88 void doGet(HTTPRequest& request, HTTPResponse& response) override;
89
95 void doPost(HTTPRequest& request, HTTPResponse& response) override;
96
97 const std::filesystem::path& getPathToAssets() const final override;
98
103 void notFoundError(HTTPResponse& response, const std::exception* exception);
104
109 void badRequestError(HTTPResponse& response, const std::exception* exception);
110
115 void internalServerError(HTTPResponse& response, const std::exception* exception);
116
117 bool getIsCaching() const;
118
119 ~ResourceExecutor() = default;
120 };
121}
Base class for executors with constant or static states.
Parsing HTTP request.
Definition HTTPRequest.h:25
HTTPBuilder wrapper.
Used for sending asset files.
void registerDynamicFunction(const std::string &functionName, std::function< std::string(const std::vector< std::string > &)> &&function) override
Add new function in .wfdp interpreter.
void sendDynamicFile(const std::string &filePath, HTTPResponse &response, const std::unordered_map< std::string, std::string > &variables={}, bool isBinary=true, const std::string &fileName="") override
Override from IDynamicFile interface.
void internalServerError(HTTPResponse &response, const std::exception *exception)
Send 500.html from WebFrameworkAssets.
void badRequestError(HTTPResponse &response, const std::exception *exception)
Send 400.html from WebFrameworkAssets.
void doPost(HTTPRequest &request, HTTPResponse &response) override
Send file via POST request.
void init(const utility::JSONSettingsParser::ExecutorSettings &settings) override
Create assets folder.
void notFoundError(HTTPResponse &response, const std::exception *exception)
Send 404.html from WebFrameworkAssets.
void doGet(HTTPRequest &request, HTTPResponse &response) override
Send file via GET request.
bool isDynamicFunctionRegistered(const std::string &functionName) override
Check if function is registered.
void sendStaticFile(const std::string &filePath, HTTPResponse &response, bool isBinary=true, const std::string &fileName="") override
Override from IStaticFile interface.
void unregisterDynamicFunction(const std::string &functionName) override
Remove function from .wfdp interpreter.