Torc  0.1
torchtmldynamiccontent.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 // Qt
24 #include <QDir>
25 
26 // Torc
27 #include "torclogging.h"
28 #include "torclocaldefs.h"
29 #include "torcdirectories.h"
30 #include "torchttprequest.h"
31 #include "torchtmldynamiccontent.h"
32 
38  : TorcHTTPHandler(TORC_CONTENT_DIR, QStringLiteral("dynamic")),
39  m_pathToContent(GetTorcConfigDir())
40 {
41  m_recursive = true;
42 
43  // Create the content directory if needed
44  // TorcLocalContext should already have created the config directory
45  if (m_pathToContent.endsWith('/'))
46  m_pathToContent.chop(1);
47  QString configdir = GetTorcContentDir();
48 
49  QDir dir(configdir);
50  if (!dir.exists())
51  if (!dir.mkpath(configdir))
52  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to create content directory ('%1')").arg(configdir));
53  }
54 
55 void TorcHTMLDynamicContent::ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
56 {
57  (void)PeerAddress;
58  (void)PeerPort;
59  (void)LocalAddress;
60  (void)LocalPort;
61 
62  // handle options request
63  if (Request.GetHTTPRequestType() == HTTPOptions)
64  {
66  return;
67  }
68 
69  // restrict access to config file - require authentication
70  if (Request.GetUrl().trimmed().endsWith(TORC_CONFIG_FILE))
71  {
73  return;
74  }
75 
76  // get the requested file subpath
77  HandleFile(Request, m_pathToContent + Request.GetUrl(), HTTPCacheNone);
78 }
A class to encapsulate an incoming HTTP request.
HTTPRequestType GetHTTPRequestType(void) const
Base HTTP response handler class.
#define TORC_CONTENT_DIR
Definition: torclocaldefs.h:19
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
static void HandleFile(TorcHTTPRequest &Request, const QString &Filename, int Cache)
void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
#define TORC_CONFIG_FILE
Definition: torclocaldefs.h:18
QString GetTorcContentDir(void)
brief Return the path to generated content
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
static bool MethodIsAuthorised(TorcHTTPRequest &Request, int Allowed)
Check the current request is authorised and set the authentication header if not. ...
QString GetUrl(void) const
QString GetTorcConfigDir(void)
Return the path to the application configuration directory.