Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOST_H_
24 : #define EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOST_H_
25 :
26 : #include "SPWebInfo.h"
27 : #include "SPPugCache.h"
28 : #include "SPPugTemplate.h"
29 : #include "SPCrypto.h"
30 : #include "SPSqlDriver.h"
31 :
32 : namespace STAPPLER_VERSIONIZED stappler::web {
33 :
34 : class Root;
35 : class AsyncTask;
36 : class Request;
37 : class HostController;
38 : class HostComponent;
39 : class WebsocketManager;
40 :
41 : class Host final : public AllocBase {
42 : public:
43 : using HandlerCallback = Function<RequestHandler *()>;
44 :
45 : static Host getCurrent();
46 :
47 : Host();
48 : Host(HostController *);
49 : Host & operator =(HostController *);
50 :
51 : Host(Host &&);
52 : Host & operator =(Host &&);
53 :
54 : Host(const Host &);
55 : Host & operator =(const Host &);
56 :
57 : void checkBroadcasts();
58 :
59 : void handleChildInit(pool_t *rootPool);
60 : void handleHeartBeat(pool_t *);
61 : void handleBroadcast(const Value &);
62 : void handleBroadcast(const BytesView &);
63 : Status handleRequest(Request &);
64 :
65 : void initTransaction(db::Transaction &);
66 :
67 : void addSourceRoot(StringView file);
68 : void addComponentByParams(StringView w);
69 : void addWasmComponentByParams(StringView path, StringView command);
70 : void addAllow(StringView);
71 : void setSessionParams(StringView w);
72 : void setHostSecret(StringView w);
73 : void setWebHookParams(StringView w);
74 : void setForceHttps();
75 : void setProtectedList(StringView w);
76 : void setDbParams(StringView w);
77 :
78 : void addProtectedLocation(const StringView &);
79 :
80 : template <typename Component = HostComponent>
81 : auto getComponent(const StringView &) const -> Component *;
82 :
83 : template <typename Component>
84 : auto getComponent() const -> Component *;
85 :
86 : template <typename Component>
87 : auto addComponent(Component *) -> Component *;
88 :
89 : const Map<StringView, HostComponent *> &getComponents() const;
90 :
91 : void addPreRequest(Function<Status(Request &)> &&) const;
92 :
93 : void addHandler(StringView, const HandlerCallback &, const Value & = Value::Null) const;
94 : void addHandler(std::initializer_list<StringView>, const HandlerCallback &, const Value & = Value::Null) const;
95 : void addHandler(StringView, const RequestHandlerMap *) const;
96 : void addHandler(std::initializer_list<StringView>, const RequestHandlerMap *) const;
97 :
98 : void addResourceHandler(StringView, const db::Scheme &) const;
99 : void addResourceHandler(StringView, const db::Scheme &, const Value &val) const;
100 : void addMultiResourceHandler(StringView, std::initializer_list<Pair<const StringView, const db::Scheme *>> &&) const;
101 :
102 : void addWebsocket(StringView, WebsocketManager *) const;
103 :
104 : const db::Scheme * exportScheme(const db::Scheme &) const;
105 :
106 : const db::Scheme * getScheme(const StringView &) const;
107 : const db::Scheme * getFileScheme() const;
108 : const db::Scheme * getUserScheme() const;
109 : const db::Scheme * getErrorScheme() const;
110 :
111 : db::Scheme * getMutable(const db::Scheme *) const;
112 :
113 : StringView getResourcePath(const db::Scheme &) const;
114 :
115 : const Map<StringView, const db::Scheme *> &getSchemes() const;
116 : const Map<const db::Scheme *, ResourceSchemeInfo> &getResources() const;
117 : const Map<StringView, RequestSchemeInfo> &getRequestHandlers() const;
118 :
119 : void reportError(const Value &);
120 :
121 : bool performTask(AsyncTask *task, bool performFirst = false) const;
122 : bool scheduleTask(AsyncTask *task, TimeInterval) const;
123 :
124 : void performWithStorage(const Callback<void(const db::Transaction &)> &cb, bool openNewConnecton = false) const;
125 :
126 : db::BackendInterface *acquireDbForRequest(const Request &) const;
127 :
128 5525 : explicit operator bool () const { return _config != nullptr; }
129 :
130 : pool_t *getThreadPool() const;
131 :
132 : Host next() const;
133 :
134 : const HostInfo &getHostInfo() const;
135 : const SessionInfo &getSessionInfo() const;
136 :
137 875 : HostController *getController() const { return _config; }
138 :
139 : Root *getRoot() const;
140 :
141 : pug::Cache *getPugCache() const;
142 : db::sql::Driver *getDbDriver() const;
143 :
144 : bool setHostKey(BytesView priv) const;
145 : void setHostKey(crypto::PrivateKey &&) const;
146 :
147 : const crypto::PublicKey &getHostPublicKey() const;
148 : const crypto::PrivateKey &getHostPrivateKey() const;
149 : BytesView getHostSecret() const;
150 :
151 : bool isSecureAuthAllowed(const Request &rctx) const;
152 :
153 : CompressionInfo *getCompressionConfig() const;
154 :
155 : String getDocumentRootPath(StringView) const;
156 :
157 : protected:
158 : void processReports() const;
159 :
160 : void addComponentWithName(const StringView &, HostComponent *);
161 :
162 : HostComponent *getHostComponent(const StringView &name) const;
163 : HostComponent *getHostComponent(std::type_index name) const;
164 :
165 : void runErrorReportTask(const Request &, const Vector<Value> &);
166 :
167 : HostController *_config = nullptr;
168 : };
169 :
170 :
171 : template <typename Component>
172 : inline auto Host::getComponent(const StringView &name) const -> Component * {
173 : return dynamic_cast<Component *>(getHostComponent(name));
174 : }
175 :
176 : template <typename Component>
177 : inline auto Host::getComponent() const -> Component * {
178 : if (auto c = getHostComponent(std::type_index(typeid(Component)))) {
179 : return static_cast<Component *>(c);
180 : }
181 :
182 : auto &cmp = getComponents();
183 : for (auto &it : cmp) {
184 : if (auto c = dynamic_cast<Component *>(it.second)) {
185 : return c;
186 : }
187 : }
188 :
189 : return nullptr;
190 : }
191 :
192 : template <typename Component>
193 : auto Host::addComponent(Component *c) -> Component * {
194 : addComponentWithName(c->getName(), c);
195 : return c;
196 : }
197 :
198 : }
199 :
200 : #endif /* EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOST_H_ */
|