Torc  0.1
torchtmlhandler.cpp
Go to the documentation of this file.
1 /* Class TorcHTMLHandler
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-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 <QObject>
25 
26 // Torc
27 #include "torclogging.h"
28 #include "torcdirectories.h"
29 #include "torcmime.h"
30 #include "torchttpserver.h"
31 #include "torchttprequest.h"
32 #include "torchttpservice.h"
33 #include "torchtmlhandler.h"
34 
52 TorcHTMLHandler::TorcHTMLHandler(const QString &Path, const QString &Name)
53  : TorcHTTPHandler(Path, Name),
54  m_pathToContent(GetTorcShareDir()),
55  m_allowedFiles()
56 {
57  if (m_pathToContent.endsWith('/'))
58  m_pathToContent.chop(1);
59  m_pathToContent += QStringLiteral("/html");
60 
61  m_allowedFiles << QStringLiteral("/") << QStringLiteral("/index.html") << QStringLiteral("/torc.xsd") <<
62  QStringLiteral("/browserconfig.xml") << QStringLiteral("/manifest.json");
63 }
64 
65 void TorcHTMLHandler::ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
66 {
67  (void)PeerAddress;
68  (void)PeerPort;
69  (void)LocalAddress;
70  (void)LocalPort;
71 
72  // handle options request
73  if (Request.GetHTTPRequestType() == HTTPOptions)
74  {
75  // this is the 'global' options - return everything possible
76  if (Request.GetUrl() == QStringLiteral("/*"))
78  else
80  return;
81  }
82 
83  // allowed file?
84  QString url = Request.GetUrl();
85  if (m_allowedFiles.contains(url))
86  {
87  if (url == QStringLiteral("/"))
88  url = QStringLiteral("/index.html");
89 
90  HandleFile(Request, m_pathToContent + url, HTTPCacheNone);
91  }
92  // I think this is unused/unreachable. img directory is served by TorcHTMLStaticContent
93  else if (url.endsWith(QStringLiteral(".png"), Qt::CaseInsensitive) || url.endsWith(QStringLiteral(".ico"), Qt::CaseInsensitive))
94  {
95  HandleFile(Request, m_pathToContent + "/img" + url, HTTPCacheLongLife | HTTPCacheLastModified);
96  }
97  else
98  {
100  Request.SetStatus(HTTP_NotFound);
101  }
102 }
virtual void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
A class to encapsulate an incoming HTTP request.
HTTPRequestType GetHTTPRequestType(void) const
Base HTTP response handler class.
QString GetTorcShareDir(void)
Return the path to the installed Torc shared resources.
TorcHTMLHandler(const QString &Path, const QString &Name)
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
static void HandleFile(TorcHTTPRequest &Request, const QString &Filename, int Cache)
void SetStatus(HTTPStatus Status)
void SetResponseType(HTTPResponseType Type)
QString GetUrl(void) const