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_TOOLS_SPWEBTOOLS_H_
24 : #define EXTRA_WEBSERVER_WEBSERVER_TOOLS_SPWEBTOOLS_H_
25 :
26 : #include "SPWebInfo.h"
27 : #include "SPWebRequestHandler.h"
28 : #include "SPWebWebsocketManager.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::web {
31 :
32 : class Resource;
33 :
34 : StringView getCompileDate();
35 : Time getCompileUnixTime();
36 :
37 : }
38 :
39 : namespace STAPPLER_VERSIONIZED stappler::web::tools {
40 :
41 : /* Simple auth handler, creates ащгк virtual pages:
42 : *
43 : * $PREFIX$/auth/setup? name=$NAME$ & passwd=$PASSWD$
44 : * - create new admin user, if there is no other users
45 : *
46 : * $PREFIX$/auth/login? name=$NAME$ & passwd=$PASSWD$ & maxAge=$MAXAGE$
47 : * - init new session for user, creates new token pair for $MAXAGE$ (720 seconds max)
48 : *
49 : * $PREFIX$/auth/update? token=$TOKEN$ & maxAge=$MAXAGE$
50 : * - updates session token pair for $MAXAGE$ (720 seconds max)
51 : *
52 : * $PREFIX$/auth/cancel? token=$TOKEN$
53 : * - cancel session
54 : *
55 : */
56 : class AuthHandler : public DataHandler {
57 : public:
58 : virtual bool isRequestPermitted(Request &) override;
59 : virtual Status onTranslateName(Request &) override;
60 : virtual bool processDataHandler(Request &, Value &result, Value &input) override;
61 : };
62 :
63 : /* WebSocket shell interface */
64 : class ShellSocket : public WebsocketManager {
65 : public:
66 : SP_COVERAGE_TRIVIAL
67 : virtual ~ShellSocket() = default;
68 :
69 25 : ShellSocket(const Host &host) : WebsocketManager(host) { };
70 :
71 : virtual WebsocketHandler * onAccept(const Request &, pool_t *) override;
72 : virtual bool onBroadcast(const Value &) override;
73 : };
74 :
75 : /* WebSocket shell interface GUI */
76 : class ShellGui : public RequestHandler {
77 : public:
78 : SP_COVERAGE_TRIVIAL
79 : virtual bool isRequestPermitted(Request &) override { return true; }
80 : virtual Status onPostReadRequest(Request &) override;
81 :
82 : virtual void onInsertFilter(Request &) override;
83 : virtual Status onHandler(Request &) override;
84 :
85 : virtual void onFilterComplete(InputFilter *f) override;
86 :
87 : protected:
88 : Resource *_resource = nullptr;
89 : };
90 :
91 : /* WebSocket shell interface GUI */
92 : class ServerGui : public DataHandler {
93 : public:
94 : static void defineBasics(pug::Context &exec, Request &req, db::User *u);
95 :
96 75 : ServerGui() {
97 75 : _allow = AllowMethod::Get | AllowMethod::Post;
98 75 : _config = db::InputConfig({
99 : db::InputConfig::Require::Data | db::InputConfig::Require::FilesAsData,
100 : 512,
101 : 256,
102 : 0
103 75 : });
104 75 : }
105 :
106 : virtual bool isRequestPermitted(Request &) override;
107 : virtual Status onTranslateName(Request &) override;
108 : virtual void onFilterComplete(InputFilter *f) override;
109 :
110 : protected:
111 : db::Transaction _transaction = nullptr;
112 : };
113 :
114 : class TestHandler : public DataHandler {
115 : public:
116 : TestHandler();
117 : virtual bool isRequestPermitted(Request &) override;
118 : virtual bool processDataHandler(Request &, Value &, Value &) override;
119 :
120 : protected:
121 : bool processEmailTest(Request &rctx, Value &ret, const Value &input);
122 : bool processUrlTest(Request &rctx, Value &ret, const Value &input);
123 : bool processUserTest(Request &rctx, Value &ret, const Value &input);
124 : bool processImageTest(Request &rctx, Value &ret, const Value &input, db::InputFile &);
125 : };
126 :
127 : class ErrorsGui : public RequestHandler {
128 : public:
129 75 : virtual bool isRequestPermitted(Request &) override { return true; }
130 : virtual Status onTranslateName(Request &) override;
131 : };
132 :
133 : class HandlersGui : public RequestHandler {
134 : public:
135 50 : virtual bool isRequestPermitted(Request &) override { return true; }
136 : virtual Status onTranslateName(Request &) override;
137 : };
138 :
139 : class ReportsGui : public RequestHandler {
140 : public:
141 250 : virtual bool isRequestPermitted(Request &) override { return true; }
142 : virtual Status onTranslateName(Request &) override;
143 : };
144 :
145 : class VirtualGui : public RequestHandler {
146 : public:
147 : virtual bool isRequestPermitted(Request &) override { return true; }
148 : virtual Status onTranslateName(Request &) override;
149 : virtual Status onHandler(Request &) override;
150 :
151 : virtual void onInsertFilter(Request &) override;
152 : virtual void onFilterComplete(InputFilter *filter) override;
153 :
154 : protected:
155 : Value readMeta(StringView) const;
156 : void writeData(Value &) const;
157 :
158 : bool createArticle(const Value &);
159 : bool createCategory(const Value &);
160 :
161 : void makeMdContents(Request &req, pug::Context &exec, StringView path) const;
162 : Value makeDirInfo(StringView path, bool forFile = false) const;
163 :
164 : bool _virtual = true;
165 : #if DEBUG
166 : bool _editable = true;
167 : #else
168 : bool _editable = false;
169 : #endif
170 : };
171 :
172 : class VirtualFilesystem : public RequestHandler {
173 : public:
174 75 : virtual bool isRequestPermitted(Request &) override { return true; }
175 : virtual Status onTranslateName(Request &) override;
176 : };
177 :
178 : void registerTools(StringView prefix, Host &);
179 :
180 : }
181 :
182 : #endif /* EXTRA_WEBSERVER_WEBSERVER_TOOLS_SPWEBTOOLS_H_ */
|