forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtactioncontext.cpp
More file actions
478 lines (400 loc) · 19.4 KB
/
Copy pathtactioncontext.cpp
File metadata and controls
478 lines (400 loc) · 19.4 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Copyright (c) 2010-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "tabstractwebsocket.h"
#include "thttpsocket.h"
#include "tpublisher.h"
#include "tsessionmanager.h"
#include "tsystemglobal.h"
#include "turlroute.h"
#include <QHostAddress>
#include <QSet>
#include <QtCore>
#include <TActionContext>
#include <TActionController>
#include <TAppSettings>
#include <TCache>
#include <TDispatcher>
#include <THttpRequest>
#include <THttpResponse>
#include <THttpUtility>
#include <TSessionStore>
#include <TWebApplication>
/*!
\class TActionContext
\brief The TActionContext class is the base class of contexts for
action controllers.
*/
TActionContext::TActionContext() :
TDatabaseContext()
{
accessLogger.open();
}
TActionContext::~TActionContext()
{
release();
accessLogger.close();
delete cachep;
}
static bool directViewRenderMode()
{
static const int mode = (int)Tf::appSettings()->value(Tf::DirectViewRenderMode).toBool();
return (bool)mode;
}
void TActionContext::execute(THttpRequest &request, int sid)
{
// App parameters
static const qint64 LimitRequestBodyBytes = Tf::appSettings()->value(Tf::LimitRequestBody, 0).toLongLong();
static const uint ListenPort = Tf::appSettings()->value(Tf::ListenPort).toUInt();
static const bool EnableCsrfProtectionModuleFlag = Tf::appSettings()->value(Tf::EnableCsrfProtectionModule, true).toBool();
static const bool SessionAutoIdRegeneration = Tf::appSettings()->value(Tf::SessionAutoIdRegeneration).toBool();
static const QString SessionCookiePath = Tf::appSettings()->value(Tf::SessionCookiePath).toString().trimmed();
static const QString SessionCookieDomain = Tf::appSettings()->value(Tf::SessionCookieDomain).toString().trimmed();
static const QByteArray SessionCookieSameSite = Tf::appSettings()->value(Tf::SessionCookieSameSite).toByteArray().trimmed();
THttpResponseHeader responseHeader;
try {
httpReq = &request;
const THttpRequestHeader &reqHeader = httpReq->header();
// Access log
if (Tf::isAccessLoggerAvailable()) {
QByteArray firstLine;
firstLine.reserve(200);
firstLine += reqHeader.method();
firstLine += ' ';
firstLine += reqHeader.path();
firstLine += QStringLiteral(" HTTP/%1.%2").arg(reqHeader.majorVersion()).arg(reqHeader.minorVersion()).toLatin1();
accessLogger.setTimestamp(QDateTime::currentDateTime());
accessLogger.setRequest(firstLine);
accessLogger.setRemoteHost((ListenPort > 0) ? originatingClientAddress().toString().toLatin1() : QByteArrayLiteral("(unix)"));
}
tSystemDebug("method : %s", reqHeader.method().data());
tSystemDebug("path : %s", reqHeader.path().data());
// HTTP method
Tf::HttpMethod method = httpReq->method();
QString path = THttpUtility::fromUrlEncoding(reqHeader.path().mid(0, reqHeader.path().indexOf('?')));
if (LimitRequestBodyBytes > 0 && reqHeader.contentLength() > (uint)LimitRequestBodyBytes) {
throw ClientErrorException(Tf::RequestEntityTooLarge, __FILE__, __LINE__); // Request Entity Too Large
}
// Routing info exists?
QStringList components = TUrlRoute::splitPath(path);
TRouting route = TUrlRoute::instance().findRouting(method, components);
tSystemDebug("Routing: controller:%s action:%s", route.controller.data(),
route.action.data());
if (!route.exists) {
// Default URL routing
if (Q_UNLIKELY(directViewRenderMode())) { // Direct view render mode?
// Direct view setting
route.setRouting(QByteArrayLiteral("directcontroller"), QByteArrayLiteral("show"), components);
} else {
QByteArray c = components.value(0).toLatin1().toLower();
if (Q_LIKELY(!c.isEmpty())) {
if (Q_LIKELY(!TActionController::disabledControllers().contains(c))) { // Can not call 'ApplicationController'
// Default action: "index"
QByteArray action = components.value(1, QStringLiteral("index")).toLatin1();
route.setRouting(c + QByteArrayLiteral("controller"), action, components.mid(2));
}
}
tSystemDebug("Active Controller : %s", route.controller.data());
}
}
// Call controller method
TDispatcher<TActionController> ctlrDispatcher(route.controller);
currController = ctlrDispatcher.object();
if (currController) {
currController->setActionName(route.action);
currController->setArguments(route.params);
currController->setSocketId(sid);
// Session
if (currController->sessionEnabled()) {
TSession session;
QByteArray sessionId = httpReq->cookie(TSession::sessionName());
if (!sessionId.isEmpty()) {
// Finds a session
session = TSessionManager::instance().findSession(sessionId);
}
currController->setSession(session);
// Exports flash-variant
currController->exportAllFlashVariants();
}
// Verify authenticity token
if (EnableCsrfProtectionModuleFlag && currController->csrfProtectionEnabled() && !currController->exceptionActionsOfCsrfProtection().contains(route.action)) {
if (method == Tf::Post || method == Tf::Put || method == Tf::Patch || method == Tf::Delete) {
if (!currController->verifyRequest(*httpReq)) {
throw SecurityException("Invalid authenticity token", __FILE__, __LINE__);
}
}
}
if (currController->sessionEnabled()) {
if (SessionAutoIdRegeneration || currController->session().id().isEmpty()) {
TSessionManager::instance().remove(currController->session().sessionId); // Removes the old session
// Re-generate session ID
currController->session().sessionId = TSessionManager::instance().generateId();
tSystemDebug("Re-generate session ID: %s", currController->session().sessionId.data());
}
// Sets CSRF protection information
TActionController::setCsrfProtectionInto(currController->session());
}
// Database Transaction
for (int databaseId = 0; databaseId < Tf::app()->sqlDatabaseSettingsCount(); ++databaseId) {
setTransactionEnabled(currController->transactionEnabled(), databaseId);
}
// Do filters
bool dispatched = false;
if (Q_LIKELY(currController->preFilter())) {
// Dispatches
dispatched = ctlrDispatcher.invoke(route.action, route.params);
if (Q_LIKELY(dispatched)) {
autoRemoveFiles << currController->autoRemoveFiles; // Adds auto-remove files
// Post filter
currController->postFilter();
if (Q_UNLIKELY(currController->rollbackRequested())) {
rollbackTransactions();
} else {
// Commits a transaction to the database
commitTransactions();
}
// Session store
if (currController->sessionEnabled()) {
bool stored = TSessionManager::instance().store(currController->session());
if (Q_LIKELY(stored)) {
static const int SessionCookieMaxAge = ([]() -> int {
QString maxagestr = Tf::appSettings()->value(Tf::SessionCookieMaxAge).toString().trimmed();
if (!maxagestr.isEmpty()) {
return maxagestr.toInt();
} else {
return Tf::appSettings()->value(Tf::SessionLifeTime).toInt();
}
}());
currController->addCookie(TSession::sessionName(), currController->session().id(), SessionCookieMaxAge,
SessionCookiePath, SessionCookieDomain, false, true, SessionCookieSameSite);
// Commits a transaction for session
commitTransactions();
} else {
tSystemError("Failed to store a session");
}
}
// WebSocket tasks
if (!currController->taskList.isEmpty()) {
QVariantList lst;
for (auto &task : (const QList<QPair<int, QVariant>> &)currController->taskList) {
const QVariant &taskData = task.second;
switch (task.first) {
case TActionController::SendTextTo: {
lst = taskData.toList();
TAbstractWebSocket *websocket = TAbstractWebSocket::searchWebSocket(lst[0].toInt());
if (websocket) {
websocket->sendText(lst[1].toString());
}
} break;
case TActionController::SendBinaryTo: {
lst = taskData.toList();
TAbstractWebSocket *websocket = TAbstractWebSocket::searchWebSocket(lst[0].toInt());
if (websocket) {
websocket->sendBinary(lst[1].toByteArray());
}
} break;
case TActionController::SendCloseTo: {
lst = taskData.toList();
TAbstractWebSocket *websocket = TAbstractWebSocket::searchWebSocket(lst[0].toInt());
if (websocket) {
websocket->sendClose(lst[1].toInt());
}
} break;
case TActionController::PublishText: {
lst = taskData.toList();
QString topic = lst[0].toString();
QString text = lst[1].toString();
TPublisher::instance()->publish(topic, text, nullptr);
} break;
case TActionController::PublishBinary: {
lst = taskData.toList();
QString topic = lst[0].toString();
QString binary = lst[1].toByteArray();
TPublisher::instance()->publish(topic, binary, nullptr);
} break;
default:
tSystemError("Invalid logic [%s:%d]", __FILE__, __LINE__);
break;
}
}
}
}
}
// Sets charset to the content-type
QByteArray ctype = currController->response.header().contentType().toLower();
if (ctype.startsWith("text") && !ctype.contains("charset")) {
ctype += "; charset=";
ctype += Tf::app()->codecForHttpOutput()->name();
currController->response.header().setContentType(ctype);
}
// Sets the default status code of HTTP response
int bytes = 0;
if (Q_UNLIKELY(currController->response.isBodyNull())) {
accessLogger.setStatusCode((dispatched) ? Tf::InternalServerError : Tf::NotFound);
bytes = writeResponse(accessLogger.statusCode(), responseHeader);
} else {
accessLogger.setStatusCode(currController->statusCode());
currController->response.header().setStatusLine(currController->statusCode(), THttpUtility::getResponseReasonPhrase(currController->statusCode()));
// Writes a response and access log
qint64 bodyLength = (currController->response.header().contentLength() > 0) ? currController->response.header().contentLength() : currController->response.bodyLength();
bytes = writeResponse(currController->response.header(), currController->response.bodyIODevice(),
bodyLength);
}
accessLogger.setResponseBytes(bytes);
// Session
if (currController->sessionEnabled()) {
// Session GC
TSessionManager::instance().collectGarbage();
// Commits a transaction for session
commitTransactions();
}
} else {
accessLogger.setStatusCode(Tf::BadRequest); // Set a default status code
if (route.controller.startsWith('/')) {
path = route.controller;
}
if (Q_LIKELY(method == Tf::Get)) { // GET Method
QString canonicalPath = QUrl(QStringLiteral(".")).resolved(QUrl(path)).toString().mid(1);
QFile reqPath(Tf::app()->publicPath() + canonicalPath);
QFileInfo fi(reqPath);
tSystemDebug("canonicalPath : %s", qPrintable(canonicalPath));
if (fi.isFile() && fi.isReadable()) {
// Check "If-Modified-Since" header for caching
bool sendfile = true;
QByteArray ifModifiedSince = reqHeader.rawHeader(QByteArrayLiteral("If-Modified-Since"));
if (!ifModifiedSince.isEmpty()) {
QDateTime dt = THttpUtility::fromHttpDateTimeString(ifModifiedSince);
if (dt.isValid()) {
sendfile = (dt.toMSecsSinceEpoch() / 1000 != fi.lastModified().toMSecsSinceEpoch() / 1000);
}
}
if (sendfile) {
// Sends a request file
responseHeader.setRawHeader(QByteArrayLiteral("Last-Modified"), THttpUtility::toHttpDateTimeString(fi.lastModified()));
QByteArray type = Tf::app()->internetMediaType(fi.suffix());
int bytes = writeResponse(Tf::OK, responseHeader, type, &reqPath, reqPath.size());
accessLogger.setResponseBytes(bytes);
} else {
// Not send the data
int bytes = writeResponse(Tf::NotModified, responseHeader);
accessLogger.setResponseBytes(bytes);
}
} else {
if (!route.exists) {
int bytes = writeResponse(Tf::NotFound, responseHeader);
accessLogger.setResponseBytes(bytes);
} else {
// Routing not empty, redirect.
responseHeader.setRawHeader(QByteArrayLiteral("Location"), QUrl(path).toEncoded());
responseHeader.setContentType(QByteArrayLiteral("text/html"));
int bytes = writeResponse(Tf::Found, responseHeader);
accessLogger.setResponseBytes(bytes);
}
}
accessLogger.setStatusCode(responseHeader.statusCode());
} else if (method == Tf::Post) {
// file upload?
} else {
// HEAD, DELETE, ...
}
}
} catch (ClientErrorException &e) {
tWarn("Caught %s: status code:%d", qPrintable(e.className()), e.statusCode());
tSystemWarn("Caught %s: status code:%d", qPrintable(e.className()), e.statusCode());
int bytes = writeResponse(e.statusCode(), responseHeader);
accessLogger.setResponseBytes(bytes);
accessLogger.setStatusCode(e.statusCode());
} catch (TfException &e) {
tError("Caught %s: %s [%s:%d]", qPrintable(e.className()), qPrintable(e.message()), qPrintable(e.fileName()), e.lineNumber());
tSystemError("Caught %s: %s [%s:%d]", qPrintable(e.className()), qPrintable(e.message()), qPrintable(e.fileName()), e.lineNumber());
closeHttpSocket();
accessLogger.setResponseBytes(0);
accessLogger.setStatusCode(Tf::InternalServerError);
} catch (std::exception &e) {
tError("Caught Exception: %s", e.what());
tSystemError("Caught Exception: %s", e.what());
closeHttpSocket();
accessLogger.setResponseBytes(0);
accessLogger.setStatusCode(Tf::InternalServerError);
}
accessLogger.write(); // Writes access log
}
void TActionContext::release()
{
TDatabaseContext::release();
for (auto temp : (const QList<TTemporaryFile *> &)tempFiles) {
delete temp;
}
tempFiles.clear();
for (auto &file : (const QStringList &)autoRemoveFiles) {
QFile(file).remove();
}
autoRemoveFiles.clear();
}
qint64 TActionContext::writeResponse(int statusCode, THttpResponseHeader &header)
{
QByteArray body;
if (statusCode == Tf::NotModified) {
return writeResponse(statusCode, header, QByteArray(), nullptr, 0);
}
if (statusCode >= 400) {
QFile html(Tf::app()->publicPath() + QString::number(statusCode) + QLatin1String(".html"));
if (html.exists() && html.open(QIODevice::ReadOnly)) {
body = html.readAll();
html.close();
}
}
if (body.isEmpty()) {
body.reserve(1024);
body += "<html><body>";
body += THttpUtility::getResponseReasonPhrase(statusCode);
body += " (";
body += QByteArray::number(statusCode);
body += ")</body></html>";
}
QBuffer buf(&body);
return writeResponse(statusCode, header, QByteArrayLiteral("text/html"), &buf, body.length());
}
qint64 TActionContext::writeResponse(int statusCode, THttpResponseHeader &header, const QByteArray &contentType, QIODevice *body, qint64 length)
{
header.setStatusLine(statusCode, THttpUtility::getResponseReasonPhrase(statusCode));
if (!contentType.isEmpty()) {
header.setContentType(contentType);
}
return writeResponse(header, body, length);
}
qint64 TActionContext::writeResponse(THttpResponseHeader &header, QIODevice *body, qint64 length)
{
header.setContentLength(length);
header.setRawHeader(QByteArrayLiteral("Server"), QByteArrayLiteral("TreeFrog server"));
header.setCurrentDate();
// Write data
return writeResponse(header, body);
}
void TActionContext::emitError(int)
{
}
TTemporaryFile &TActionContext::createTemporaryFile()
{
TTemporaryFile *file = new TTemporaryFile();
tempFiles << file;
return *file;
}
QHostAddress TActionContext::clientAddress() const
{
return httpReq->clientAddress();
}
QHostAddress TActionContext::originatingClientAddress() const
{
return httpReq->originatingClientAddress();
}
TCache *TActionContext::cache()
{
if (!cachep) {
cachep = new TCache;
}
return cachep;
}