Torc  0.1
torchtmlstaticcontent.cpp
Go to the documentation of this file.
1 /* Class TorcHTMLStaticContent
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2013-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclocaldefs.h"
25 #include "torclogging.h"
26 #include "torclanguage.h"
27 #include "torcdirectories.h"
28 #include "torcnetwork.h"
29 #include "torchttpserver.h"
30 #include "torchttprequest.h"
31 #include "torchttpservice.h"
32 #include "torchtmlstaticcontent.h"
33 
39  : TorcHTTPHandler(STATIC_DIRECTORY, QStringLiteral("static")),
40  m_pathToContent(QStringLiteral(""))
41 {
42  m_pathToContent = GetTorcShareDir();
43  if (m_pathToContent.endsWith('/'))
44  m_pathToContent.chop(1);
45  m_pathToContent += QStringLiteral("/html");
46 
47  m_recursive = true;
48 }
49 
50 void TorcHTMLStaticContent::ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
51 {
52  (void)PeerAddress;
53  (void)PeerPort;
54  (void)LocalAddress;
55  (void)LocalPort;
56 
57  // handle options request
58  if (Request.GetHTTPRequestType() == HTTPOptions)
59  {
61  return;
62  }
63 
64  // get the requested file subpath
65  QString subpath = Request.GetUrl();
66 
67  // request for Torc configuration. This is used to add some appropriate Javascript globals
68  // and translated strings
69 
70  if (subpath == QStringLiteral("/js/torcconfiguration.js"))
71  {
73  Request.SetAllowGZip(true);
74  return;
75  }
76 
77  HandleFile(Request, m_pathToContent + subpath, HTTPCacheLongLife | HTTPCacheLastModified);
78 }
79 
86 {
87  // populate the list of static constants and translations
88  QVariantMap strings = TorcStringFactory::GetTorcStrings();
89 
90  // generate dynamic variables
91  strings.insert(QStringLiteral("ServicesPath"), TORC_SERVICES_DIR);
92  strings.insert(QStringLiteral("UserService"), TORC_USER_SERVICE);
93  strings.insert(QStringLiteral("SettingsPath"), TORC_SETTINGS_DIR);
94  strings.insert(QStringLiteral("RootSetting"), TORC_ROOT_SETTING);
95  strings.insert(QStringLiteral("TorcRealm"), TORC_REALM);
96  strings.insert(QStringLiteral("PortSetting"), TORC_PORT_SERVICE);
97  strings.insert(QStringLiteral("SecureSetting"),TORC_SSL_SERVICE);
98  strings.insert(QStringLiteral("TorcConfFile"), TORC_CONTENT_DIR + TORC_CONFIG_FILE);
99 
100  // and generate javascript
101  QJsonObject json = QJsonObject::fromVariantMap(strings);
102  QByteArray result("var torc = ");
103  result.append(QJsonDocument(json).toJson());
104  if (result.endsWith("\n"))
105  result.chop(1);
106  result.append(";\r\n\r\nif (Object.freeze) { Object.freeze(torc); }\r\n");
107 
108  Request.SetStatus(HTTP_OK);
110  Request.SetResponseContent(result);
111 }
A class to encapsulate an incoming HTTP request.
#define TORC_ROOT_SETTING
Definition: torclocaldefs.h:17
HTTPRequestType GetHTTPRequestType(void) const
#define STATIC_DIRECTORY
#define TORC_SETTINGS_DIR
Definition: torclocaldefs.h:16
Base HTTP response handler class.
#define TORC_CONTENT_DIR
Definition: torclocaldefs.h:19
#define TORC_PORT_SERVICE
Definition: torclocaldefs.h:14
#define TORC_SERVICES_DIR
Definition: torclocaldefs.h:12
QString GetTorcShareDir(void)
Return the path to the installed Torc shared resources.
#define TORC_USER_SERVICE
Definition: torclocaldefs.h:13
void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
void SetResponseContent(const QByteArray &Content)
void SetAllowGZip(bool Allowed)
Allow gzip compression for the contents of this request.
static void HandleFile(TorcHTTPRequest &Request, const QString &Filename, int Cache)
#define TORC_CONFIG_FILE
Definition: torclocaldefs.h:18
void SetStatus(HTTPStatus Status)
static QVariantMap GetTorcStrings(void)
Return a map of string constants and their translations.
#define TORC_REALM
Definition: torclocaldefs.h:9
void SetResponseType(HTTPResponseType Type)
QString GetUrl(void) const
static void GetJavascriptConfiguration(TorcHTTPRequest &Request)
Construct a Javascript object that encapsulates Torc variables, enumerations and translated strings...
#define TORC_SSL_SERVICE
Definition: torclocaldefs.h:15