Torc  0.1
torchttpservices.cpp
Go to the documentation of this file.
1 /* Class TorcHTTPServices
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2015-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 <QCoreApplication>
25 #include <QMetaObject>
26 #include <QMetaMethod>
27 #include <QObject>
28 
29 // Torc
30 #include "torclocalcontext.h"
31 #include "torcserialiser.h"
32 #include "torchttpserver.h"
33 #include "torchttprequest.h"
34 #include "torchttpservice.h"
35 #include "torchttpservices.h"
36 #include "torcwebsockettoken.h"
37 #include "torcupnp.h"
38 
47  : QObject(),
48  TorcHTTPService(this, QStringLiteral(""), QStringLiteral("services"), TorcHTTPServices::staticMetaObject, QStringLiteral("HandlersChanged")),
49  serviceList(),
50  returnFormats(),
51  webSocketProtocols()
52 {
54 
56  for ( int i = 0; factory; factory = factory->NextTorcSerialiserFactory(), i++)
57  {
58  QVariantMap item;
59  item.insert(QStringLiteral("name"), factory->Description());
60  item.insert(QStringLiteral("type"), factory->MimeType());
61  returnFormats.append(item);
62  }
63 }
64 
66 {
67  int index = TorcHTTPServices::staticMetaObject.indexOfClassInfo("Version");
68  return (index > -1) ? TorcHTTPServices::staticMetaObject.classInfo(index).value() : QStringLiteral("unknown");
69 }
70 
72 {
73  return tr("Services");
74 }
75 
76 QVariantMap TorcHTTPServices::ProcessRequest(const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated)
77 {
78  // N.B. this returns a result associated with the actual state of the underlying websocket and
79  // not what the browser etc think is the correct state.
80  if (Method.endsWith(QStringLiteral("IsSecure")))
81  {
82  QVariant sec((bool)false);
83  TorcWebSocket *connection = qobject_cast<TorcWebSocket*>(Connection);
84  if (connection && connection->IsSecure())
85  sec = true;
86 
87  QVariantMap result;
88  QVariantMap secure;
89  secure.insert(TORC_SECURE, sec);
90  result.insert(QStringLiteral("result"), secure);
91  return result;
92  }
93 
94  return TorcHTTPService::ProcessRequest(Method, Parameters, Connection, Authenticated);
95 }
96 
97 
98 void TorcHTTPServices::ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
99 {
100  // handle own service
101  if (!Request.GetMethod().isEmpty())
102  {
103  // we handle GetWebSocketToken/IsSecure manually as they need access to the authentication headers.
104  // and they have no value internally (hence dummy implementations)
105  QString method = Request.GetMethod();
106  if (method == QStringLiteral("GetWebSocketToken") || method == QStringLiteral("IsSecure"))
107  {
108  HTTPRequestType type = Request.GetHTTPRequestType();
109  if (type == HTTPOptions)
110  {
111  Request.SetStatus(HTTP_OK);
113  Request.SetAllowed(HTTPGet | HTTPOptions);
114  }
115  else if (type == HTTPGet)
116  {
117  QVariant result;
118  QString type;
119  if (method == QStringLiteral("GetWebSocketToken"))
120  {
121  // force authentication
123  return;
124  result = TorcWebSocketToken::GetWebSocketToken(PeerAddress);
125  type = QStringLiteral("accesstoken");
126  }
127  else if (method == QStringLiteral("IsSecure"))
128  {
129  result = Request.GetSecure();
130  type = TORC_SECURE;
131  }
132 
133  Request.SetStatus(HTTP_OK);
134  Request.Serialise(result, type);
135  }
136  else
137  {
138  Request.SetStatus(HTTP_BadRequest);
140  }
141 
142  return;
143  }
144 
145  TorcHTTPService::ProcessHTTPRequest(PeerAddress, PeerPort, LocalAddress, LocalPort, Request);
146  return;
147  }
148 
149  // handle options request
150  if (Request.GetHTTPRequestType() == HTTPOptions)
151  {
153  return;
154  }
155 
156  Request.SetStatus(HTTP_NotFound);
158 }
159 
160 void TorcHTTPServices::SubscriberDeleted(QObject *Subscriber)
161 {
163 }
164 
171 {
172  // NB keys here match those of the relevant stand alone methods. Take care not to break them.
173  QVariantMap results;
174 
176  results.insert(QStringLiteral("services"), GetServiceList());
177  results.insert(TORC_STARTTIME, GetStartTime());
178  results.insert(TORC_PRIORITY, GetPriority());
179  results.insert(TORC_UUID, GetUuid());
180 
181  return results;
182 }
183 
185 {
187 }
188 
189 QVariantMap TorcHTTPServices::GetServiceDescription(const QString &Service)
190 {
191  // the server has the services - and can lock the list
193 }
194 
196 {
197  return returnFormats;
198 }
199 
201 {
203 }
204 
206 {
207  return gLocalContext->GetStartTime();
208 }
209 
211 {
212  return gLocalContext->GetPriority();
213 }
214 
216 {
217  return gLocalContext->GetUuid();
218 }
219 
226 {
227  return QStringLiteral("");
228 }
229 
231 {
232  return false; // dummy - as for GetWebSocketToken
233 }
234 
236 {
237  emit ServiceListChanged();
238 }
void SetAllowed(int Allowed)
#define TORC_PRIORITY
Definition: torcupnp.h:19
void HandlersChanged(void)
QVariantList returnFormats
QVariantMap GetDetails(void)
Return complete application information.
A class to encapsulate an incoming HTTP request.
HTTPRequestType GetHTTPRequestType(void) const
Overlays the Websocket protocol over a QTcpSocket.
Definition: torcwebsocket.h:23
TorcLocalContext * gLocalContext
void SubscriberDeleted(QObject *Subscriber)
void ServiceListChanged(void)
Top level interface into services.
QVariantList GetWebSocketProtocols(void)
QString GetUuid(void)
bool IsSecure(void)
static TorcSerialiserFactory * GetTorcSerialiserFactory(void)
QString MimeType(void) const
#define TORC_STARTTIME
Definition: torcupnp.h:20
QVariantMap GetServiceList(void)
QString GetUuid(void) const
void HandlersChanged(void)
void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
TorcSerialiserFactory * NextTorcSerialiserFactory(void) const
static QVariantMap GetServiceHandlers(void)
void Serialise(const QVariant &Data, const QString &Type)
qint64 GetStartTime(void)
#define TORC_SECURE
Definition: torcupnp.h:22
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
QString GetMethod(void) const
TorcHTTPServices(TorcHTTPServer *Server)
void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
QVariantMap ProcessRequest(const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated) override
const QString & Description(void) const
void HandleSubscriberDeleted(QObject *Subscriber)
#define TORC_SERVICE_VERSION
QVariantMap ProcessRequest(const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated) override
static QVariantList GetSupportedSubProtocols(void)
Return a list of supported WebSocket sub-protocols.
static QString GetVersion(void)
void SetStatus(HTTPStatus Status)
QVariantMap GetServiceDescription(const QString &Service)
static bool MethodIsAuthorised(TorcHTTPRequest &Request, int Allowed)
Check the current request is authorised and set the authentication header if not. ...
HTTPRequestType
#define TORC_UUID
Definition: torcupnp.h:15
QString GetUIName(void) override
void SetResponseType(HTTPResponseType Type)
qint64 GetStartTime(void)
An HTTP server.
QString GetWebSocketToken(void)
Return a WebSocket token for connecting a WebSocket when authentication is required.
static QVariantMap GetServiceDescription(const QString &Service)
static QString GetWebSocketToken(const QString &Host, const QString &Current=QString())
Retrieve an authentication token for the given request or validate a current token.
QVariantList GetReturnFormats(void)