13 #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
14 #include <QStringConverter>
28 static constexpr std::chrono::milliseconds
MAX_WAIT { 30s };
30 #define LOC QString("UPnPSub: ")
36 :
m_url(std::move(url)),
m_path(std::move(path)) { }
52 if (addr.protocol() == QAbstractSocket::IPv6Protocol)
53 host =
"[" + addr.toString() +
"]";
55 host = addr.toString();
57 m_callback = QString(
"http://%1:%2/Subscriptions/event?usn=")
58 .arg(host, QString::number(port));
65 while (!usns.isEmpty())
70 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"Finished");
76 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Subscribe %1 %2 %3")
77 .arg(usn, url.toString(), path));
90 LOG(VB_GENERAL, LOG_WARNING,
LOC +
91 "Re-subscribing with different url and path.");
110 QString uuid = QString();
128 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Renew: %1").arg(usn));
144 LOG(VB_UPNP, LOG_ERR,
LOC + QString(
"Unrecognised renewal usn: %1")
155 LOG(VB_UPNP, LOG_ERR,
LOC + QString(
"No uuid - not renewing usn: %1")
169 LOG(VB_UPNP, LOG_INFO,
LOC + QString(
"Removing %1").arg(usn));
186 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"%1\n%2")
198 if (nt.isEmpty() || nts.isEmpty() || !no)
205 if (nt !=
"upnp:event" || nts !=
"upnp:propchange")
210 if (usn.isEmpty() || sid.isEmpty())
223 int loc = pRequest->
m_sPayload.lastIndexOf(
"propertyset>");
224 QString payload = (loc > -1) ? pRequest->
m_sPayload.left(loc + 12) :
227 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Payload:\n%1").arg(payload));
231 #if QT_VERSION < QT_VERSION_CHECK(6,5,0)
235 if (!body.setContent(payload,
true, &
error, &errorLine, &errorCol))
237 LOG(VB_GENERAL, LOG_ERR,
LOC +
238 QString(
"Failed to parse event: Line: %1 Col: %2 Error: '%3'")
239 .arg(errorLine).arg(errorCol).arg(
error));
244 body.setContent(payload,
245 QDomDocument::ParseOption::UseNamespaceProcessing);
248 LOG(VB_GENERAL, LOG_ERR,
LOC +
249 QString(
"Failed to parse event: Line: %1 Col: %2 Error: '%3'")
250 .arg(parseResult.errorLine).arg(parseResult.errorColumn)
251 .arg(parseResult.errorMessage));
256 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"/n/n" + body.toString(4) +
"/n/n");
258 QDomNodeList properties = body.elementsByTagName(
"property");
263 for (
int i = 0; i < properties.size(); i++)
265 QDomNodeList arguments = properties.at(i).childNodes();
266 for (
int j = 0; j < arguments.size(); j++)
268 QDomElement e = arguments.at(j).toElement();
269 if (!e.isNull() && !e.text().isEmpty() && !e.tagName().isEmpty())
270 results.insert(e.tagName(), e.text());
294 bool success =
false;
295 QString host = url.host();
296 int port = url.port();
299 QTextStream data(&sub);
300 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
301 data.setCodec(QTextCodec::codecForName(
"UTF-8"));
303 data.setEncoding(QStringConverter::Utf8);
306 data << QString(
"UNSUBSCRIBE %1 HTTP/1.1\r\n").arg(path);
307 data << QString(
"HOST: %1:%2\r\n").arg(host, QString::number(port));
308 data << QString(
"SID: uuid:%1\r\n").arg(uuid);
312 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"\n\n" + sub);
314 auto *sockdev =
new MSocketDevice(MSocketDevice::Stream);
316 sockdev->setBlocking(
true);
318 if (sock->Connect(QHostAddress(host), port))
320 if (sock->WriteBlockDirect(sub.constData(), sub.size()) != -1)
322 QString line = sock->ReadLine(
MAX_WAIT);
323 success = !line.isEmpty();
327 LOG(VB_GENERAL, LOG_ERR,
LOC +
328 QString(
"Socket write error for %1:%2") .arg(host).arg(port));
334 LOG(VB_GENERAL, LOG_ERR,
LOC +
335 QString(
"Failed to open socket for %1:%2") .arg(host).arg(port));
341 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Unsubscribed to %1").arg(usn));
343 LOG(VB_UPNP, LOG_WARNING,
LOC + QString(
"Failed to unsubscribe to %1")
352 const QString &uuidin,
355 QString host = url.host();
356 int port = url.port();
359 QTextStream data(&sub);
360 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
361 data.setCodec(QTextCodec::codecForName(
"UTF-8"));
363 data.setEncoding(QStringConverter::Utf8);
366 data << QString(
"SUBSCRIBE %1 HTTP/1.1\r\n").arg(path);
367 data << QString(
"HOST: %1:%2\r\n").arg(host, QString::number(port));
370 if (uuidin.isEmpty())
372 data << QString(
"CALLBACK: <%1%2>\r\n")
374 data <<
"NT: upnp:event\r\n";
379 data << QString(
"SID: uuid:%1\r\n").arg(uuidin);
386 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"\n\n" + sub);
388 auto *sockdev =
new MSocketDevice(MSocketDevice::Stream);
390 sockdev->setBlocking(
true);
394 std::chrono::seconds result = 0s;
396 if (sock->Connect(QHostAddress(host), port))
398 if (sock->WriteBlockDirect(sub.constData(), sub.size()) != -1)
401 QString line = sock->ReadLine(
MAX_WAIT);
402 while (!line.isEmpty())
404 LOG(VB_UPNP, LOG_DEBUG,
LOC + line);
405 if (line.contains(
"HTTP/1.1 200 OK", Qt::CaseInsensitive))
407 if (line.startsWith(
"SID:", Qt::CaseInsensitive))
408 uuid = line.mid(4).trimmed().mid(5).trimmed();
409 if (line.startsWith(
"TIMEOUT:", Qt::CaseInsensitive))
410 timeout = line.mid(8).trimmed().mid(7).trimmed();
411 if (ok && !uuid.isEmpty() && !
timeout.isEmpty())
416 if (ok && !uuid.isEmpty() && !
timeout.isEmpty())
419 result = std::chrono::seconds(
timeout.toUInt());
423 LOG(VB_GENERAL, LOG_ERR,
LOC +
424 QString(
"Failed to subscribe to %1").arg(usn));
429 LOG(VB_GENERAL, LOG_ERR,
LOC +
430 QString(
"Socket write error for %1:%2") .arg(host).arg(port));
436 LOG(VB_GENERAL, LOG_ERR,
LOC +
437 QString(
"Failed to open socket for %1:%2") .arg(host).arg(port));