forked from nedrysoft/regex101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegExApiEndpoint.h
365 lines (333 loc) · 17.6 KB
/
RegExApiEndpoint.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (C) 2020 Adrian Carpenter
*
* This file is part of a regex101.com offline application.
*
* https://github.com/fizzyade/regex101
*
* =====================================================================
* The regex101 web content is owned and used with permission from
* Firas Dib at regex101.com. This application is an unofficial
* tool to provide an offline application version of the website.
* =====================================================================
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REGEXAPIENDPOINT_H
#define REGEXAPIENDPOINT_H
#include <QJsonObject>
#include <QObject>
#include <QSettings>
#include <QSqlDatabase>
#include <QVariant>
namespace Nedrysoft {
class RegExDatabase;
/**
* @brief RegExApiEndpoint class
*
* @details Provides functions that can be called from javascript in the web browser.
*/
class RegExApiEndpoint :
public QObject
{
private:
Q_OBJECT
private:
/**
* @brief Constructs an api endpoint class.
*
* @note The constructor is private and the api endpoint is a singleton object
* that should be retrieved using the getInstance() method.
*/
RegExApiEndpoint();
public:
/**
* @brief Constructs an api endpoint class.
*/
static RegExApiEndpoint *getInstance();
/**
* @brief Retrieves an expression from storage
*
* @details Finds the expression with the given permalink and version number and returns
* the expression details as a variant map.
*
* @param[in,out] state is a json object containing the current web application state
* @param[in] permalinkFragment the permalink fragment id
* @param[in] version contains the version number of the expression to load
*
* @returns true if the result was found; otherwise false.
*/
bool regex(QJsonObject &state, QString permalinkFragment, int version);
/**
* @brief Method for fetching data from a web page.
*
* @details This function uses the same signature as the fetch api provided by the web browser,
* javascript is injected into the web page to replace the web browsers implementation
* with this custom version which allows us to reply without having to make real network requests.
*
* @param[in] pathParameter contains the path of the fetch request.
* @param[in] requestParameter contains the requesst parameters send via javascript.
*
* @returns a QVariant containing the body of the response.
*/
Q_INVOKABLE QVariant fetch(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Method setting a localstorage value
*
* @details This function sets the storage item identified by the parameter key to the given value,
* the injected javascript overrides the native implmentation of the local storage api
* so that we have full control over it.
*
* @param[in] key contains the key of item.
* @param[in] value contains the value of the item.
*
* @returns a null QVariant
*/
Q_INVOKABLE QVariant localStorageSetItem(const QVariant &key, const QVariant &value);
/**
* @brief Method retrieving a localstorage value
*
* @details This function retrieves the storage item identified by the parameter key,
* the injected javascript overrides the native implmentation of the local storage api
* so that we have full control over it.
*
* @param[in] key contains the key of item.
*
* @returns the value if set; otherwise null
*/
Q_INVOKABLE QVariant localStorageGetItem(const QVariant &key);
/**
* @brief Method removing a localstorage value
*
* @details This function removes the storage item identified by the parameter key,
* the injected javascript overrides the native implmentation of the local storage api
* so that we have full control over it.
*
* @param[in] key contains the key of item.
*
* @returns a null QVariant
*/
Q_INVOKABLE QVariant localStorageRemoveItem(const QVariant &key);
/**
* @brief Method clearing the localstorage
*
* @details This function clears the local storage. The injected javascript overrides the native implmentation
* of the local storage api so that we have full control over it.
*
* @param[in] key contains the key of item.
*
* @returns a null QVariant
*/
Q_INVOKABLE QVariant localStorageClear();
/**
* @brief Method for sending message from javascript to c++
*
* @param[in] message contains the message received.
*/
Q_INVOKABLE void notifyApplication(QVariant message) const;
private:
/**
* @brief Creates a random string of characters
*
* @details Creates a string which is made of random characters from the set (a-z, A-z, 0-9)
*
* @param[in] numberOfCharacters is the desired length of the string
*
* @returns a string of the desired length consisting of random characters
*/
QString createRandomString(int numberOfCharacters) const;
/**
* @brief Creates a permalink fragment
*
* @details Creates a string which is made of random characters from the set (a-z, A-z, 0-9)
*
* @returns a string containing the permalink fragment
*/
QString createPermalinkFragment() const;
/**
* @brief Creates a deletecode
*
* @details Creates a string which is made of random characters from the set (a-z, A-z, 0-9)
*
* @returns a string containing the deletecode fragment
*/
QString createDeleteCode() const;
/**
* @brief Processes HTTP GET request
*
* @details Any GET request that is made to the api endpoint is handled with this function
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processGetRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes HTTP PUT request
*
* @details Any PUT request that is made to the api endpoint is handled with this function
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processPutRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes HTTP POST request
*
* @details Any POST request that is made to the api endpoint is handled with this function
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processPostRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes HTTP DELETE request
*
* @details Any DELETE request that is made to the api endpoint is handled with this function
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processDeleteRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes a request made to the save endpoint
*
* @details Function for handling a request made to the save api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processSaveRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes a request made to the fork endpoint
*
* @details Function for handling a request made to the save api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processForkRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Processes a request made to the get library items endpoint
*
* @details Function for handling a request made to the save api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processGetLibraryItems(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Processes a request made to the get items endpoint
*
* @details Function for handling a request made to the save api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processGetItemDetails(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Processes a request made to the static endpoint
*
* @details Function for handling a request made to the save api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processGetStatic(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Processes a request made to the put history endpoint
*
* @details Function for handling a request made to the put history endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processPutHistoryRequest(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Processes a request made to the get regex endpoint
*
* @details Function for handling a request made to the get regex endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processGetRegEx(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Uploads a regular expression to the library
*
* @details Function for handling a request made to the library api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
*
* @returns a QVariant response to the request
*/
QVariant processUploadToLibraryRequest(const QVariant &pathParameter, const QVariant &requestParameter) const;
/**
* @brief Sets or unsets a favourite
*
* @details Function for handling a request made to the favourite api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processSetFavorite(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
/**
* @brief Handles calls to the get history api endpoint
*
* @details Function for handling a request made to the get history api endpoint
*
* @param[in] pathParameter contains the path of the api request
* @param[in] requestParameter contains the request detail such as HTTP headers and body
* @param[in] match contains the regular expression that was used determine the endpoint
*
* @returns a QVariant response to the request
*/
QVariant processGetHistory(const QVariant &pathParameter, const QVariant &requestParameter, const QRegularExpressionMatch &match) const;
private:
QSettings *m_settings; //! settings object to store the web application local storage data
RegExDatabase *m_database; //! the database
QMap<QString, QString> m_librarySearchSortMap; //! map containing mapping from application sort type to database field
};
}
#endif // REGEXAPIENDPOINT_H