WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
HTTPRequest.h
1#pragma once
2
3#include "Import/WebFrameworkCore.h"
4
5#include "BaseTCPServer.h"
6#include "HTTPParser.h"
7#include "MultiLocalizationManager.h"
8#include "Log.h"
9
10#include "Managers/SessionsManager.h"
11#include "SQLite3/SQLiteManager.h"
12#include "Interfaces/IStaticFile.h"
13#include "Interfaces/IDynamicFile.h"
14#include "Utility/ChunkGenerator.h"
15#include "Utility/WebFrameworkConcepts.h"
16
17namespace framework
18{
24 class WEB_FRAMEWORK_API HTTPRequest
25 {
26 private:
27 SessionsManager& session;
28 const web::BaseTCPServer& serverReference;
29 streams::IOSocketStream& stream;
30 sqlite::SQLiteManager& database;
31 std::unordered_map<std::string, std::variant<std::string, int64_t, double>> routeParameters;
32 sockaddr clientAddr;
33 web::HTTPParser parser;
34 interfaces::IStaticFile& staticResources;
35 interfaces::IDynamicFile& dynamicResources;
36
37 private:
38 static bool isWebFrameworkDynamicPages(const std::string& filePath);
39
40 public:
41 static web::HTTPParser sendRequestToAnotherServer(std::string_view ip, std::string_view port, std::string_view request, DWORD timeout = 30'000, bool useHTTPS = false);
42
43 public:
44 HTTPRequest(SessionsManager& session, const web::BaseTCPServer& serverReference, interfaces::IStaticFile& staticResources, interfaces::IDynamicFile& dynamicResources, sqlite::SQLiteManager& database, sockaddr clientAddr, streams::IOSocketStream& stream);
45
46 HTTPRequest(HTTPRequest&&) noexcept = default;
47
48 HTTPRequest(const HTTPRequest&) = default;
49
50 HTTPRequest& operator =(HTTPRequest&&) noexcept = default;
51
52 HTTPRequest& operator =(const HTTPRequest&) = default;
53
58 const std::string& getRawParameters() const;
59
64 const std::string& getMethod() const;
65
70 const std::unordered_map<std::string, std::string>& getKeyValueParameters() const;
71
76 std::string getHTTPVersion() const;
77
82 const web::HeadersMap& getHeaders() const;
83
88 const std::string& getBody() const;
89
95 void setAttribute(const std::string& name, const std::string& value);
96
103 std::string getAttribute(const std::string& name);
104
108 void deleteSession();
109
114 void deleteAttribute(const std::string& name);
115
120 web::HeadersMap getCookies() const;
121
129 void sendAssetFile(const std::string& filePath, HTTPResponse& response, const std::unordered_map<std::string, std::string>& variables = {}, bool isBinary = true, const std::string& fileName = "");
130
137 void sendStaticFile(const std::string& filePath, HTTPResponse& response, bool isBinary = true, const std::string& fileName = "");
138
146 void sendDynamicFile(const std::string& filePath, HTTPResponse& response, const std::unordered_map<std::string, std::string>& variables, bool isBinary = false, const std::string& fileName = "");
147
154 void streamFile(std::string_view filePath, HTTPResponse& response, std::string_view fileName, size_t chunkSize = defaultChunkSize);
155
159 void registerDynamicFunction(const std::string& functionName, std::function<std::string(const std::vector<std::string>&)>&& function);
160
163 void unregisterDynamicFunction(const std::string& functionName);
164
168 bool isDynamicFunctionRegistered(const std::string& functionName);
169
174 const json::JSONParser& getJSON() const;
175
180 const std::vector<std::string>& getChunks() const;
181
186 const web::HTTPParser& getParser() const;
187
192 std::string getClientIpV4() const;
193
198 std::string getServerIpV4() const;
199
204 uint16_t getClientPort() const;
205
210 uint16_t getServerPort() const;
211
219 template<utility::concepts::RouteParameterType T>
220 const T& getRouteParameter(const std::string& routeParameterName);
221
229 template<std::derived_from<sqlite::SQLiteDatabaseModel> T, typename... Args>
230 std::shared_ptr<T> createModel(Args&&... args);
231
237 template<std::derived_from<sqlite::SQLiteDatabaseModel> T>
238 std::shared_ptr<T> getModel() const;
239
247 template<std::derived_from<utility::ChunkGenerator> T, typename... Args>
248 void sendChunks(HTTPResponse& response, Args&&... args);
249
258 template<std::derived_from<utility::ChunkGenerator> T, typename... Args>
259 void sendFileChunks(HTTPResponse& response, std::string_view fileName, Args&&... args);
260
268 friend streams::IOSocketStream& operator >> (streams::IOSocketStream& stream, HTTPRequest& request);
269
276 friend std::ostream& operator << (std::ostream& stream, const HTTPRequest& request);
277
278 friend class ExecutorsManager;
279
280 ~HTTPRequest() = default;
281 };
282
283 template<std::derived_from<sqlite::SQLiteDatabaseModel> T, typename... Args>
284 std::shared_ptr<T> HTTPRequest::createModel(Args&&... args)
285 {
286 return database.add<T>(std::forward<Args>(args)...);
287 }
288
289 template<std::derived_from<sqlite::SQLiteDatabaseModel> T>
290 std::shared_ptr<T> HTTPRequest::getModel() const
291 {
292 std::shared_ptr<T> result = database.get<T>();
293
294 if constexpr (std::is_default_constructible_v<T>)
295 {
296 if (!result)
297 {
298 result = database.add<T>();
299 }
300 }
301 else if (!result)
302 {
303 if (Log::isValid())
304 {
305 Log::error("Can't get or create model in HTTPRequest::getModel<T> function where T is {}", "LogWebFrameworkModels", typeid(T).name());
306 }
307 }
308
309 return result;
310 }
311
312 template<std::derived_from<utility::ChunkGenerator> T, typename... Args>
313 void HTTPRequest::sendChunks(HTTPResponse& response, Args&&... args)
314 {
315 this->sendFileChunks<T>(response, "", std::forward<Args>(args)...);
316 }
317
318 template<std::derived_from<utility::ChunkGenerator> T, typename... Args>
319 void HTTPRequest::sendFileChunks(HTTPResponse& response, std::string_view fileName, Args&&... args)
320 {
321 T generator(std::forward<Args>(args)...);
322
323 web::HTTPBuilder builder = web::HTTPBuilder().chunk(generator.generate()).partialChunks().responseCode(web::responseCodes::ok).headers
324 (
326 "Server", "WebFramework-Server",
327 "Connection", "keep-alive"
328 );
329
330 if (fileName.size())
331 {
332 builder.headers
333 (
334 "Content-Disposition", std::format(R"(attachment; filename="{}")", fileName)
335 );
336 }
337
338 stream << builder.build();
339
340 response.setIsValid(false);
341
342 while (true)
343 {
344 std::string data = generator.generate();
345
346 stream << web::HTTPBuilder::getChunk(data);
347
348 if (stream.eof() || data.empty())
349 {
350 break;
351 }
352 }
353 }
354}
Parsing HTTP request.
Definition HTTPRequest.h:25
const T & getRouteParameter(const std::string &routeParameterName)
Getter for route parameters.
void sendChunks(HTTPResponse &response, Args &&... args)
Send runtime generated content.
void sendFileChunks(HTTPResponse &response, std::string_view fileName, Args &&... args)
Send file.
std::shared_ptr< T > createModel(Args &&... args)
First call creates model with arguments other calls returns created model.
std::shared_ptr< T > getModel() const
First call creates model without arguments other calls returns created model.
HTTPBuilder wrapper.
static std::string getFullDate()
Adds full date GMT to response.