Torc  0.1
torcupnpcontent.cpp
Go to the documentation of this file.
1 /* Class TorcUPnPContent
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
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 <QHostAddress>
25 #include <QXmlStreamWriter>
26 
27 // Torc
28 #include "torclocalcontext.h"
29 #include "torchttphandler.h"
30 #include "torcupnp.h"
31 #include "torcupnpcontent.h"
32 #include "torcnetwork.h"
33 
35  : TorcHTTPHandler(UPNP_DIRECTORY, QStringLiteral("upnp"))
36 {
37 }
38 
39 void TorcUPnPContent::ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
40 {
41  (void)PeerAddress;
42  (void)PeerPort;
43 
44  // handle options request
45  if (Request.GetHTTPRequestType() == HTTPOptions)
46  {
48  return;
49  }
50 
51  // handle device description
52  // N.B. this does not check whether the device is actually published on this interface
53  if (Request.GetMethod().toLower() == QStringLiteral("description"))
54  {
55  QHostAddress base(LocalAddress);
56  QString url = QStringLiteral("http%1://%2").arg(Request.GetSecure() ? QStringLiteral("s") : QStringLiteral(""), TorcNetwork::IPAddressToLiteral(base, LocalPort, false));
57  QByteArray result;
58  QXmlStreamWriter xml(&result);
59  xml.writeStartDocument(QStringLiteral("1.0"));
60  xml.writeStartElement(QStringLiteral("root"));
61  xml.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("urn:schemas-upnp-org:device-1-0"));
62 
63  xml.writeTextElement(QStringLiteral("URLBase"), url);
64  xml.writeStartElement(QStringLiteral("specVersion"));
65  xml.writeTextElement(QStringLiteral("major"), QStringLiteral("1"));
66  xml.writeTextElement(QStringLiteral("minor"), QStringLiteral("0"));
67  xml.writeEndElement(); // specversion
68 
69  xml.writeStartElement(QStringLiteral("device"));
70 
71  xml.writeTextElement(QStringLiteral("deviceType"), TORC_ROOT_UPNP_DEVICE);
72  xml.writeTextElement(QStringLiteral("friendlyName"), QStringLiteral("Torc"));
73  xml.writeTextElement(QStringLiteral("manufacturer"), QStringLiteral("Torc"));
74  xml.writeTextElement(QStringLiteral("modelName"), QStringLiteral("Torc v1.0"));
75  xml.writeTextElement(QStringLiteral("UDN"), QStringLiteral("uuid:%1").arg(gLocalContext->GetUuid()));
76 
77  xml.writeStartElement(QStringLiteral("iconList"));
78  xml.writeStartElement(QStringLiteral("icon"));
79  xml.writeTextElement(QStringLiteral("mimetype"), QStringLiteral("image/png"));
80  xml.writeTextElement(QStringLiteral("width"), QStringLiteral("36"));
81  xml.writeTextElement(QStringLiteral("height"), QStringLiteral("36"));
82  xml.writeTextElement(QStringLiteral("depth"), QStringLiteral("32"));
83  xml.writeTextElement(QStringLiteral("url"), QStringLiteral("img/android-icon-36x36.png"));
84  xml.writeEndElement(); // icon
85  xml.writeEndElement(); // iconlist
86 
87  xml.writeEndElement(); // device
88  xml.writeEndElement(); // root
89  xml.writeEndDocument();
90 
91  Request.SetAllowGZip(true);
92  Request.SetStatus(HTTP_OK);
94  Request.SetResponseContent(result);
95  return;
96  }
97 }
A class to encapsulate an incoming HTTP request.
HTTPRequestType GetHTTPRequestType(void) const
TorcLocalContext * gLocalContext
Base HTTP response handler class.
QString GetUuid(void) const
static QString IPAddressToLiteral(const QHostAddress &Address, int Port, bool UseLocalhost=true)
Convert an IP address to a string literal.
static void HandleOptions(TorcHTTPRequest &Request, int Allowed)
void SetResponseContent(const QByteArray &Content)
QString GetMethod(void) const
void SetAllowGZip(bool Allowed)
Allow gzip compression for the contents of this request.
void ProcessHTTPRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request) override
#define TORC_ROOT_UPNP_DEVICE
Definition: torcupnp.h:7
#define UPNP_DIRECTORY
void SetStatus(HTTPStatus Status)
void SetResponseType(HTTPResponseType Type)