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_SPWEBHOSTCONTROLLER_H_
24 : #define EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOSTCONTROLLER_H_
25 :
26 : #include "SPWebInfo.h"
27 : #include "SPCrypto.h"
28 : #include "SPPugCache.h"
29 : #include "SPSqlDriver.h"
30 : #include "SPWasm.h"
31 :
32 : namespace STAPPLER_VERSIONIZED stappler::web {
33 :
34 : class Root;
35 : class WebsocketManager;
36 : class Request;
37 : class HostComponent;
38 : class Host;
39 : class DbdModule;
40 :
41 : class HostController : public AllocBase {
42 : public:
43 : virtual ~HostController();
44 :
45 : HostController(Root *, pool_t *);
46 :
47 : virtual bool loadComponent(const Host &serv, const HostComponentInfo &);
48 :
49 : virtual db::Scheme makeUserScheme() const;
50 : virtual db::Scheme makeFileScheme() const;
51 : virtual db::Scheme makeErrorScheme() const;
52 :
53 : void initComponents(const Host &serv, const Vector<HostComponentInfo> &val);
54 :
55 : void initSession(const Value &val);
56 : void initWebhook(const Value &val);
57 :
58 : void setSessionParam(StringView n, StringView v);
59 : void setWebhookParam(StringView n, StringView v);
60 :
61 : void setForceHttps();
62 :
63 : void setHostSecret(StringView w);
64 : void setHostKey(crypto::PrivateKey &&);
65 :
66 : void addAllowed(StringView r);
67 :
68 : void init(const Host &serv);
69 :
70 : bool initKeyPair(const Host &serv, const db::Adapter &a, BytesView fp);
71 :
72 : void initHostKeys(const Host &serv, const db::Adapter &a);
73 :
74 : virtual void handleChildInit(const Host &serv, pool_t *p);
75 :
76 : virtual void initTransaction(db::Transaction &t);
77 :
78 : void setDbParams(StringView str);
79 :
80 575 : const SessionInfo &getSessionInfo() const { return _session; }
81 25 : const WebhookInfo &getWebhookInfo() const { return _webhook; }
82 5425 : const HostInfo &getHostInfo() const { return _hostInfo; }
83 :
84 11649 : Root *getRoot() const { return _root; }
85 : pool_t *getRootPool() const { return _rootPool; }
86 :
87 50 : const Map<StringView, StringView> &getDbParams() const { return _dbParams; }
88 :
89 : virtual db::sql::Driver::Handle openConnection(pool_t *, bool bindConnection) const;
90 : virtual void closeConnection(db::sql::Driver::Handle) const;
91 :
92 : protected:
93 : virtual db::sql::Driver * openInternalDriver(db::sql::Driver::Handle);
94 :
95 : virtual bool loadDsoComponent(const Host &serv, const HostComponentInfo &);
96 : virtual bool loadWasmComponent(const Host &serv, const HostComponentInfo &);
97 :
98 : virtual wasm::Module *loadWasmModule(StringView name, StringView path);
99 :
100 : virtual String resolvePath(StringView path) const;
101 :
102 : void handleTemplateError(const StringView &str);
103 :
104 : friend class Host;
105 :
106 : Root *_root = nullptr;
107 : pool_t *_rootPool = nullptr;
108 :
109 : db::Scheme _defaultUserScheme = db::Scheme(config::USER_SCHEME_NAME);
110 : db::Scheme _defaultFileScheme = db::Scheme(config::FILE_SCHEME_NAME);
111 : db::Scheme _defaultErrorScheme = db::Scheme(config::ERROR_SCHEME_NAME);
112 :
113 : crypto::PrivateKey _hostPrivKey;
114 : crypto::PublicKey _hostPubKey;
115 : string::Sha512::Buf _hostSecret;
116 :
117 : Vector<HostComponentInfo> _componentsToLoad;
118 :
119 : Vector<StringView> _sourceRoot;
120 : StringView _currentComponent;
121 : Vector<Function<Status(Request &)>> _preRequest;
122 : Map<StringView, HostComponent *> _components;
123 : Map<std::type_index, HostComponent *> _typedComponents;
124 : Map<StringView, RequestSchemeInfo> _requests;
125 : Map<const db::Scheme *, ResourceSchemeInfo> _resources;
126 : Map<StringView, const db::Scheme *> _schemes;
127 : Map<StringView, WebsocketManager *> _websockets;
128 : Set<StringView> _protectedList;
129 :
130 : HostInfo _hostInfo;
131 : SessionInfo _session;
132 : WebhookInfo _webhook;
133 : CompressionInfo _compression;
134 :
135 : bool _childInit = false;
136 : bool _loadingFalled = false;
137 : bool _forceHttps = false;
138 :
139 : Time _lastDatabaseCleanup;
140 : Time _lastTemplateUpdate;
141 : int64_t _broadcastId = 0;
142 : pug::Cache _pugCache;
143 :
144 : Vector<Pair<uint32_t, uint32_t>> _allowedIps;
145 : Map<StringView, StringView> _dbParams;
146 : DbdModule *_customDbd = nullptr;
147 : db::sql::Driver *_dbDriver = nullptr;
148 :
149 : Map<StringView, Rc<wasm::Module>> _wasmModules;
150 : };
151 :
152 :
153 : }
154 :
155 : #endif /* EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOSTCONTROLLER_H_ */
|