15 #include <QCoreApplication>
16 #include <QDesktopServices>
17 #include <QElapsedTimer>
21 #include <QMutexLocker>
22 #include <QNetworkAccessManager>
23 #include <QNetworkDiskCache>
24 #include <QNetworkInterface>
25 #include <QNetworkProxy>
26 #include <QNetworkReply>
27 #include <QNetworkRequest>
32 #include <QSslConfiguration>
46 static const QString
LOC { QStringLiteral(
"[netstream] ") };
64 static const QEvent::Type
kType = QEvent::User;
80 static const QEvent::Type
kType =
static_cast< QEvent::Type
>(QEvent::User + 1);
100 m_cert(std::move(cert))
102 setObjectName(
"NetStream " + url.toString());
104 QNetworkRequest::CacheLoadControl attr {QNetworkRequest::PreferNetwork};
106 attr = QNetworkRequest::AlwaysCache;
108 attr = QNetworkRequest::PreferCache;
110 attr = QNetworkRequest::AlwaysNetwork;
111 m_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, attr);
139 static inline QString
Source(
const QNetworkRequest &request)
141 switch (request.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt())
143 case QNetworkRequest::AlwaysCache:
return "cache";
144 case QNetworkRequest::PreferCache:
return "cache-preferred";
145 case QNetworkRequest::PreferNetwork:
return "net-preferred";
146 case QNetworkRequest::AlwaysNetwork:
return "net";
151 static inline QString
Source(
const QNetworkReply* reply)
153 return reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool() ?
163 LOG(VB_GENERAL, LOG_WARNING,
LOC +
164 QString(
"(%1) Request unsupported URL: %2")
165 .arg(
m_id).arg(url.toString()) );
188 const QByteArray ua(
"User-Agent");
190 m_request.setRawHeader(ua,
"UK-MHEG/2 MYT001/001 MHGGNU/001");
193 m_request.setRawHeader(
"Range", QString(
"bytes=%1-").arg(
m_pos).toLatin1());
195 #ifndef QT_NO_OPENSSL
198 QSslConfiguration ssl(QSslConfiguration::defaultConfiguration());
200 QList<QSslCertificate> clist;
203 clist = QSslCertificate::fromData(
m_cert, QSsl::Der);
205 LOG(VB_GENERAL, LOG_WARNING,
LOC + QString(
"Invalid certificate: %1")
206 .arg(
m_cert.toPercentEncoding().constData()) );
212 ssl.setPeerVerifyMode(QSslSocket::VerifyNone);
216 ssl.setCaCertificates(clist);
223 if (!fname.isEmpty())
226 if (f1.open(QIODevice::ReadOnly))
228 QSslCertificate cert(&f1, QSsl::Pem);
230 ssl.setLocalCertificate(cert);
232 LOG(VB_GENERAL, LOG_WARNING,
LOC +
233 QString(
"'%1' is an invalid certificate").arg(f1.fileName()) );
237 LOG(VB_GENERAL, LOG_WARNING,
LOC +
238 QString(
"Opening client certificate '%1': %2")
239 .arg(f1.fileName(), f1.errorString()) );
244 if (!fname.isEmpty())
247 if (f2.open(QIODevice::ReadOnly))
249 QSslKey key(&f2, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey,
252 ssl.setPrivateKey(key);
254 LOG(VB_GENERAL, LOG_WARNING,
LOC +
255 QString(
"'%1' is an invalid key").arg(f2.fileName()) );
259 LOG(VB_GENERAL, LOG_WARNING,
LOC +
260 QString(
"Opening private key '%1': %2")
261 .arg(f2.fileName(), f2.errorString()) );
270 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Request %2 bytes=%3- from %4")
290 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Started 0x%2")
291 .arg(
m_id).arg(quintptr(reply),0,16) );
303 #ifndef QT_NO_OPENSSL
304 connect(reply, &QNetworkReply::sslErrors,
this,
312 LOG(VB_GENERAL, LOG_ERR,
LOC +
313 QString(
"(%1) Started but m_reply not NULL").arg(
m_id));
320 qlonglong len = reply->header(QNetworkRequest::ContentLengthHeader)
322 return ok ? len : -1;
326 qulonglong &first, qulonglong &last)
328 QByteArray range = reply->rawHeader(
"Content-Range");
334 const char *fmt =
" bytes %20" SCNd64
" - %20" SCNd64
" / %20" SCNd64;
335 if (3 != std::sscanf(range.constData(), fmt, &first, &last, &len))
337 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Invalid Content-Range:'%1'")
338 .arg(range.constData()) );
342 return static_cast<qlonglong
>(len);
346 static bool inline RequestRange(
const QNetworkRequest &request,
347 qlonglong &first, qlonglong &last)
351 QByteArray range = request.rawHeader(
"Range");
355 if (1 > std::sscanf(range.constData(),
" bytes %20lld - %20lld", &first, &last))
357 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Invalid Range:'%1'")
358 .arg(range.constData()) );
373 qint64 avail =
m_reply->bytesAvailable();
375 (avail <= 4 *
kMaxBuffer) ? LOG_INFO : LOG_WARNING,
376 LOC + QString(
"(%1) Ready 0x%2, %3 bytes available").arg(
m_id)
377 .arg(quintptr(
m_reply),0,16).arg(avail) );
381 qulonglong first = 0;
387 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Ready 0x%2, range %3-%4/%5")
388 .arg(
m_id).arg(quintptr(
m_reply),0,16).arg(first).arg(last).arg(len) );
393 if (m_state < kReady || m_size >= 0)
395 LOG(VB_FILE, LOG_INFO,
LOC +
396 QString(
"(%1) Ready 0x%2, content length %3")
402 m_state = std::max(m_state,
kReady);
412 LOG(VB_GENERAL, LOG_ERR,
LOC +
413 QString(
"(%1) ReadyRead but m_reply = NULL").arg(
m_id));
425 if (QNetworkReply::NoError ==
error)
429 QNetworkRequest::RedirectionTargetAttribute).toUrl();
436 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1) Too many redirections")
445 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1) Redirection loop to %2")
446 .arg(
m_id).arg(url.toString()) );
451 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Redirecting").arg(
m_id));
458 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1): %2")
468 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Finished 0x%2 %3/%4 bytes from %5")
480 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"(%1) Finished but m_reply = NULL")
485 #ifndef QT_NO_OPENSSL
494 for (
const auto& e : std::as_const(errors))
496 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) SSL error %2: ")
497 .arg(
m_id).arg(e.error()) + e.errorString() );
500 #if 1 // The BBC use a self certified cert
501 case QSslError::SelfSignedCertificateInChain:
512 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) SSL errors ignored").arg(
m_id));
513 m_reply->ignoreSslErrors(errors);
518 LOG(VB_GENERAL, LOG_ERR,
LOC +
519 QString(
"(%1) SSL error but m_reply = NULL").arg(
m_id) );
531 return url.isValid() &&
532 (url.scheme() ==
"http" || url.scheme() ==
"https") &&
533 !url.authority().isEmpty() &&
534 !url.path().isEmpty();
549 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Cancelled").arg(
m_id) );
557 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Abort 0x%2")
570 QElapsedTimer
t;
t.start();
578 unsigned elapsed =
t.elapsed();
579 if (elapsed >= millisecs)
590 qint64 avail =
m_reply->read(
reinterpret_cast< char*
>(data), sz);
594 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) safe_read @ %4 => %2/%3, %5 mS")
595 .arg(
m_id).arg(avail).arg(sz).arg(
m_pos).arg(
t.elapsed()) );
609 LOG(VB_GENERAL, LOG_ERR,
LOC +
610 QString(
"(%1) Seek(%2) out of range [0..%3]")
615 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Seek(%2) curr %3 end %4")
643 QElapsedTimer
t;
t.start();
646 auto elapsed = std::chrono::milliseconds(
t.elapsed());
660 QElapsedTimer
t;
t.start();
663 auto elapsed = std::chrono::milliseconds(
t.elapsed());
676 return !
m_reply ? QNetworkReply::OperationCanceledError :
m_reply->error();
698 QByteArray data =
m_reply->readAll();
699 m_pos += data.size();
747 QMutexLocker locker(&
s_mtx);
757 setObjectName(
"NAMThread");
759 #ifndef QT_NO_OPENSSL
762 qRegisterMetaType< QList<QSslError> >();
776 LOG(VB_FILE, LOG_INFO,
LOC +
"NAMThread starting");
778 m_nam =
new QNetworkAccessManager();
779 m_nam->setObjectName(
"NetStream NAM");
782 std::unique_ptr<QNetworkDiskCache> cache(
new QNetworkDiskCache());
784 cache->setCacheDirectory(
GetConfDir() +
"/cache/netstream-" +
787 m_nam->setCache(cache.release());
791 QString proxy(qEnvironmentVariable(
"MYTHMHEG_PROXY"));
792 if (!proxy.isEmpty())
794 QUrl url(proxy, QUrl::TolerantMode);
795 QNetworkProxy::ProxyType
type {QNetworkProxy::NoProxy};
796 if (url.scheme().isEmpty()
797 || (url.scheme() ==
"http")
798 || (url.scheme() ==
"https"))
799 type = QNetworkProxy::HttpProxy;
800 else if (url.scheme() ==
"socks")
801 type = QNetworkProxy::Socks5Proxy;
802 else if (url.scheme() ==
"cache")
803 type = QNetworkProxy::HttpCachingProxy;
804 else if (url.scheme() ==
"ftp")
805 type = QNetworkProxy::FtpCachingProxy;
807 if (QNetworkProxy::NoProxy !=
type)
809 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Using proxy: " + proxy);
810 m_nam->setProxy(QNetworkProxy(
811 type, url.host(), url.port(), url.userName(), url.password() ));
815 LOG(VB_MHEG, LOG_ERR,
LOC + QString(
"Unknown proxy type %1")
816 .arg(url.scheme()) );
821 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
830 QCoreApplication::processEvents();
841 QScopedPointer< QEvent > ev(
m_workQ.dequeue());
853 LOG(VB_FILE, LOG_INFO,
LOC +
"NAMThread stopped");
871 switch (event->type())
875 #pragma GCC diagnostic push
876 #pragma GCC diagnostic ignored "-Wswitch"
879 #pragma GCC diagnostic pop
890 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Invalid NetStreamRequest");
894 if (!
p->m_bCancelled)
896 QNetworkReply *reply =
m_nam->get(
p->m_req);
897 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) StartRequest 0x%2")
898 .arg(
p->m_id).arg(quintptr(reply),0,16) );
903 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) NetStreamRequest cancelled").arg(
p->m_id) );
912 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Invalid NetStreamAbort");
916 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) AbortRequest 0x%2").arg(
p->m_id)
917 .arg(quintptr(
p->m_reply),0,16) );
919 p->m_reply->disconnect();
927 auto interfaces = QNetworkInterface::allInterfaces();
928 return std::any_of(interfaces.begin(), interfaces.end(),
929 [](
const QNetworkInterface& iface)
931 auto f = iface.flags();
932 if (f.testFlag(QNetworkInterface::IsLoopBack))
934 return f.testFlag(QNetworkInterface::IsRunning);
944 QMutexLocker locker(&m.
m_mutex);
949 QAbstractNetworkCache *cache = m.
m_nam->cache();
953 QNetworkCacheMetaData meta = cache->metaData(url);
956 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"GetLastModified('%1') not in cache")
957 .arg(url.toString()));
962 QDateTime
const now(QDateTime::currentDateTime());
963 QDateTime expire = meta.expirationDate();
964 if (expire.isValid() && expire.toLocalTime() < now)
966 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"GetLastModified('%1') past expiration %2")
967 .arg(url.toString(), expire.toString()));
972 QDateTime lastMod = meta.lastModified();
974 QNetworkCacheMetaData::RawHeaderList
headers = meta.rawHeaders();
975 for (
const auto& h : std::as_const(
headers))
978 static const QString kSzFormat {
"ddd, dd MMM yyyy HH:mm:ss 'GMT'" };
980 QString
const first(h.first.toLower());
981 if (first ==
"cache-control")
983 QString
const second(h.second.toLower());
984 if (second ==
"no-cache" || second ==
"no-store")
986 LOG(VB_FILE, LOG_INFO,
LOC +
987 QString(
"GetLastModified('%1') Cache-Control disabled")
988 .arg(url.toString()) );
993 else if (first ==
"date")
998 LOG(VB_GENERAL, LOG_WARNING,
LOC +
999 QString(
"GetLastModified invalid Date header '%1'")
1000 .arg(h.second.constData()));
1003 #if QT_VERSION < QT_VERSION_CHECK(6,5,0)
1004 d.setTimeSpec(Qt::UTC);
1006 d.setTimeZone(QTimeZone(QTimeZone::UTC));
1012 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"GetLastModified('%1') last modified %2")
1013 .arg(url.toString(), lastMod.toString()));