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_SPWEBROOT_H_ 24 : #define EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBROOT_H_ 25 : 26 : #include "SPWeb.h" 27 : #include "SPWebHost.h" 28 : #include "SPSqlDriver.h" 29 : 30 : namespace STAPPLER_VERSIONIZED stappler::web { 31 : 32 : class AsyncTask; 33 : class Host; 34 : class InputFilter; 35 : class RequestController; 36 : 37 : class Root : public db::ApplicationInterface, public AllocBase { 38 : public: 39 : struct Stat { 40 : uint64_t requestsReceived = 0; 41 : uint64_t heartbeatCounter = 0; 42 : uint64_t dbQueriesReleased = 0; 43 : uint64_t dbQueriesPerformed = 0; 44 : }; 45 : 46 : struct ErrorNotificator : public AllocBase { 47 : Function<void(Value &&)> error; 48 : Function<void(Value &&)> debug; 49 : }; 50 : 51 : static constexpr const char *ErrorNotificatorKey = "web::Root.ErrorNotificator"; 52 : 53 : static Root *getCurrent(); 54 : 55 : static void parseParameterList(Map<StringView, StringView> &target, StringView params); 56 : 57 : static void setErrorNotification(pool_t *, Function<void(Value &&)> errorCb, Function<void(Value &&)> debugCb = nullptr); 58 : 59 : static void dumpCurrentState(StringView filepath = StringView()); 60 : 61 : virtual ~Root(); 62 : 63 : Root(pool_t *); 64 : 65 : Stat getStat() const; 66 : 67 3100 : StringView getServerNameLine() const { return _serverNameLine; } 68 : 69 125 : bool isDebugEnabled() const { return _debug.load(); } 70 : void setDebugEnabled(bool); 71 : 72 : virtual bool performTask(const Host &, AsyncTask *task, bool performFirst) = 0; 73 : virtual bool scheduleTask(const Host &, AsyncTask *task, TimeInterval) = 0; 74 : 75 : virtual void foreachHost(const Callback<void(Host &)> &) = 0; 76 : 77 : virtual bool isSecureConnection(const Request &) const; 78 : 79 : virtual db::sql::Driver * getDbDriver(StringView); 80 : 81 : virtual db::sql::Driver::Handle dbdOpen(pool_t *, const Host &); 82 : virtual void dbdClose(const Host &, db::sql::Driver::Handle); 83 : virtual db::sql::Driver::Handle dbdAcquire(const Request &); 84 : 85 : void addDb(StringView); 86 : void setDbParams(StringView); 87 : void setThreadsCount(StringView, StringView); 88 : 89 : virtual void handleHeartbeat(pool_t *pool); 90 : virtual void handleBroadcast(const Value &); 91 : 92 : virtual void handleChildInit(pool_t *); 93 : 94 : Status runTypeChecker(Request &r); 95 : Status runPostReadRequest(Request &r); 96 : Status runTranslateName(Request &r); 97 : Status runCheckAccess(Request &r); 98 : Status runQuickHandler(Request &r, int v); 99 : void runInsertFilter(Request &r); 100 : Status runHandler(Request &r); 101 : 102 : void handleFilterInit(InputFilter *f); 103 : void handleFilterUpdate(InputFilter *f); 104 : void handleFilterComplete(InputFilter *f); 105 : 106 : virtual db::Adapter getAdapterFromContext() const override; 107 : virtual void scheduleAyncDbTask(const Callback<Function<void(const db::Transaction &)>(pool_t *)> &setupCb) const override; 108 : 109 : virtual StringView getDocumentRoot() const override; 110 : 111 : virtual const db::Scheme *getFileScheme() const override; 112 : virtual const db::Scheme *getUserScheme() const override; 113 : 114 : virtual void pushErrorMessage(Value &&) const override; 115 : virtual void pushDebugMessage(Value &&) const override; 116 : 117 : virtual db::InputFile *getFileFromContext(int64_t id) const override; 118 : virtual int64_t getUserIdFromContext() const override; 119 : 120 : virtual db::RequestData getRequestData() const override; 121 : 122 : virtual void initTransaction(db::Transaction &) const override; 123 : 124 : protected: 125 : struct PendingTask { 126 : Host host; 127 : AsyncTask *task; 128 : TimeInterval interval = nullptr; 129 : bool performFirst; 130 : }; 131 : 132 : StringView findTypeCheckerContentType(Request &r, StringView ext) const; 133 : StringView findTypeCheckerCharset(Request &r, StringView ext) const; 134 : StringView findTypeCheckerContentLanguage(Request &r, StringView ext) const; 135 : StringView findTypeCheckerContentEncoding(Request &r, StringView ext) const; 136 : 137 : virtual void initDatabases(); 138 : virtual void initSignals(); 139 : virtual void initThreads(); 140 : 141 : db::sql::Driver *createDbDriver(StringView driverName); 142 : 143 : pool_t *_workerPool = nullptr; 144 : pool_t *_configPool = nullptr; 145 : pool_t *_rootPool = nullptr; 146 : 147 : size_t _initThreads = std::thread::hardware_concurrency() / 2; 148 : size_t _maxThreads = std::thread::hardware_concurrency(); 149 : 150 : StringView _serverNameLine; 151 : 152 : std::atomic<uint64_t> _requestsReceived = 0; 153 : std::atomic<uint64_t> _heartbeatCounter = 0; 154 : std::atomic<uint64_t> _dbQueriesReleased = 0; 155 : std::atomic<uint64_t> _dbQueriesPerformed = 0; 156 : 157 : std::atomic<bool> _debug = false; 158 : 159 : Vector<PendingTask> *_pending = nullptr; 160 : 161 : Map<StringView, StringView> _dbParams; 162 : Map<StringView, db::sql::Driver *> _dbDrivers; 163 : Vector<StringView> _dbs; 164 : db::sql::Driver *_primaryDriver = nullptr; 165 : 166 : Mutex _mutex; 167 : memory::allocator_t *_allocator = nullptr; 168 : }; 169 : 170 : } 171 : 172 : #endif /* EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBROOT_H_ */