-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcHTTPSServer.h
More file actions
138 lines (113 loc) · 4.06 KB
/
cHTTPSServer.h
File metadata and controls
138 lines (113 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "cBaseWorker_V2.h"
class cHTTPSServer : public cBaseWorker_V2
{
public:
struct st_Field
{
std::string name;
std::string value;
};
using td_Fields = std::vector<st_Field>;
struct st_Request
{
std::string method;
std::string path;
std::string queryString;
std::string httpVersion;
std::string body;
td_Fields headers;
td_Fields queryFields;
std::vector<std::string> pathMatch;
std::string remoteAddress;
std::uint16_t remotePort{0U};
std::string localAddress;
std::uint16_t localPort{0U};
};
class cResponse
{
public:
cResponse() = default;
~cResponse();
cResponse(const cResponse&) = default;
cResponse& operator=(const cResponse&) = default;
cResponse(cResponse&&) noexcept = default;
cResponse& operator=(cResponse&&) noexcept = default;
void write(std::string_view body) const;
void write(
std::uint16_t statusCode,
std::string_view body,
const td_Fields& headers = {}) const;
void write(std::uint16_t statusCode, const td_Fields& headers) const;
void redirect(
const std::string& location,
std::uint16_t statusCode = 302U) const;
[[nodiscard]] bool valid() const noexcept;
private:
struct st_State;
explicit cResponse(std::shared_ptr<st_State> state);
std::shared_ptr<st_State> m_state;
friend class cHTTPSServer;
};
using td_RequestHandler = std::function<void(const st_Request&, cResponse&)>;
using td_ErrorHandler = std::function<void(const st_Request&, int, const std::string&)>;
cHTTPSServer(
std::string name,
std::uint16_t port,
std::string certificateFile,
std::string privateKeyFile,
std::string verifyFile = {},
std::string bindAddress = {},
std::size_t threadPoolSize = 1U);
~cHTTPSServer() noexcept override;
cHTTPSServer(const cHTTPSServer&) = delete;
cHTTPSServer& operator=(const cHTTPSServer&) = delete;
cHTTPSServer(cHTTPSServer&&) = delete;
cHTTPSServer& operator=(cHTTPSServer&&) = delete;
[[nodiscard]] bool addResource(
const std::string& pathRegex,
const std::string& method,
td_RequestHandler handler);
[[nodiscard]] bool setDefaultResource(
const std::string& method,
td_RequestHandler handler);
void setErrorHandler(td_ErrorHandler handler);
void clearErrorHandler();
void setBindAddress(std::string bindAddress);
void setPort(std::uint16_t port) noexcept;
void setThreadPoolSize(std::size_t threadPoolSize) noexcept;
void setReuseAddress(bool reuseAddress) noexcept;
void setFastOpen(bool fastOpen) noexcept;
void setRequestTimeoutSeconds(long timeoutSeconds) noexcept;
void setContentTimeoutSeconds(long timeoutSeconds) noexcept;
void setMaxRequestSize(std::size_t maxRequestSize) noexcept;
void setCertificateFiles(
std::string certificateFile,
std::string privateKeyFile,
std::string verifyFile = {});
[[nodiscard]] std::string bindAddress() const;
[[nodiscard]] std::string certificateFile() const;
[[nodiscard]] std::string privateKeyFile() const;
[[nodiscard]] std::string verifyFile() const;
[[nodiscard]] std::uint16_t configuredPort() const noexcept;
[[nodiscard]] std::uint16_t listeningPort() const noexcept;
[[nodiscard]] std::size_t threadPoolSize() const noexcept;
[[nodiscard]] std::string lastError() const;
[[nodiscard]] bool startThread(
duration_type waitForStartTimeout = kDefaultWaitTimeout);
[[nodiscard]] bool startThread(std::uint16_t waitForStartTimeoutMilliSec);
protected:
bool preRun() override;
void run() override;
void stopTriggered() override;
private:
struct st_Impl;
[[nodiscard]] bool waitUntilListening(duration_type waitForStartTimeout);
std::unique_ptr<st_Impl> m_impl;
};