Torc  0.1
torchttphandler.cpp
Go to the documentation of this file.
1 /* Class TorcHTTPHandler
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 <QFile>
25 #include <QFileInfo>
26 
27 // Torc
28 #include "torclogging.h"
29 #include "torchttpserver.h"
30 #include "torchttphandler.h"
31 
46 TorcHTTPHandler::TorcHTTPHandler(const QString &Signature, const QString &Name)
47  : m_signature(Signature),
48  m_recursive(false),
49  m_name(Name)
50 {
51  if (!m_signature.endsWith('/'))
52  m_signature += '/';
53 
54  if (m_name.isEmpty())
55  LOG(VB_GENERAL, LOG_DEBUG, QStringLiteral("Not registering '%1' as a handler").arg(m_signature));
56  else
58 }
59 
61 {
63 }
64 
65 QString TorcHTTPHandler::Signature(void) const
66 {
67  return m_signature;
68 }
69 
71 {
72  return m_recursive;
73 }
74 
75 QString TorcHTTPHandler::Name(void) const
76 {
77  return m_name;
78 }
79 
80 QVariantMap TorcHTTPHandler::ProcessRequest(const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated)
81 {
82  (void)Method;
83  (void)Parameters;
84  (void)Connection;
85  (void)Authenticated;
86 
87  return QVariantMap();
88 }
89 
93 {
94  if ((Request.IsAuthorised() != HTTPAuthorised) && (Allowed & HTTPAuth))
95  {
97  return false;
98  }
99  return true;
100 }
101 
103 {
104  Request.SetAllowed(Allowed);
105  Request.SetStatus(HTTP_OK);
107  QByteArray empty;
108  Request.SetResponseContent(empty);
109 }
110 
111 void TorcHTTPHandler::HandleFile(TorcHTTPRequest &Request, const QString &Filename, int Cache)
112 {
113  // sanity checks
114  if (QFile::exists(Filename))
115  {
116  if ((QFile::permissions(Filename) & QFile::ReadOther))
117  {
118  QFileInfo fileinfo(Filename);
119  if (fileinfo.size() > 0)
120  {
121  QDateTime modified = fileinfo.lastModified();
122 
123  // set cache handling before we check for modification. This ensures the modification check is
124  // performed and the correct cache headers are re-sent with any 304 Not Modified response.
125  Request.SetCache(Cache, modified.toString(TorcHTTPRequest::DateFormat));
126 
127  // Unmodified will handle the response
128  if (Request.Unmodified(modified))
129  return;
130 
131  Request.SetResponseFile(Filename);
132  Request.SetStatus(HTTP_OK);
133  Request.SetAllowGZip(true);
134  return;
135  }
136  else
137  {
138  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("'%1' is empty - ignoring").arg(Filename));
139  Request.SetStatus(HTTP_NotFound);
140  }
141  }
142  else
143  {
144  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("'%1' is not readable").arg(Filename));
145  Request.SetStatus(HTTP_Forbidden);
146  }
147  }
148  else
149  {
150  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to find '%1'").arg(Filename));
151  Request.SetStatus(HTTP_NotFound);
152  }
153 
155 }
void SetAllowed(int Allowed)
static char DateFormat[]
virtual QVariantMap ProcessRequest(const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated)
A class to encapsulate an incoming HTTP request.
QString Signature(void) const
static void AddAuthenticationHeader(TorcHTTPRequest &Request)
bool Unmodified(const QDateTime &LastModified)
Return true if the resource is unmodified.
HTTPAuthorisation IsAuthorised(void) const
bool GetRecursive(void) const
static void RegisterHandler(TorcHTTPHandler *Handler)
void SetCache(int Cache, const QString &Tag=QStringLiteral(""))
Set the caching behaviour for this response.
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
void SetResponseContent(const QByteArray &Content)
QString Name(void) const
TorcHTTPHandler(const QString &Signature, const QString &Name)
void SetAllowGZip(bool Allowed)
Allow gzip compression for the contents of this request.
static void HandleFile(TorcHTTPRequest &Request, const QString &Filename, int Cache)
static void DeregisterHandler(TorcHTTPHandler *Handler)
void SetStatus(HTTPStatus Status)
#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. ...
void SetResponseFile(const QString &File)
void SetResponseType(HTTPResponseType Type)
virtual ~TorcHTTPHandler()