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>
31 #include <QSslConfiguration>
45 static const QString
LOC { QStringLiteral(
"[netstream] ") };
63 static const QEvent::Type
kType = QEvent::User;
79 static const QEvent::Type
kType =
static_cast< QEvent::Type
>(QEvent::User + 1);
99 m_cert(std::move(cert))
101 setObjectName(
"NetStream " + url.toString());
103 QNetworkRequest::CacheLoadControl attr {QNetworkRequest::PreferNetwork};
105 attr = QNetworkRequest::AlwaysCache;
107 attr = QNetworkRequest::PreferCache;
109 attr = QNetworkRequest::AlwaysNetwork;
110 m_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, attr);
138 static inline QString
Source(
const QNetworkRequest &request)
140 switch (request.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt())
142 case QNetworkRequest::AlwaysCache:
return "cache";
143 case QNetworkRequest::PreferCache:
return "cache-preferred";
144 case QNetworkRequest::PreferNetwork:
return "net-preferred";
145 case QNetworkRequest::AlwaysNetwork:
return "net";
150 static inline QString
Source(
const QNetworkReply* reply)
152 return reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool() ?
162 LOG(VB_GENERAL, LOG_WARNING,
LOC +
163 QString(
"(%1) Request unsupported URL: %2")
164 .arg(
m_id).arg(url.toString()) );
187 const QByteArray ua(
"User-Agent");
189 m_request.setRawHeader(ua,
"UK-MHEG/2 MYT001/001 MHGGNU/001");
192 m_request.setRawHeader(
"Range", QString(
"bytes=%1-").arg(
m_pos).toLatin1());
194 #ifndef QT_NO_OPENSSL
197 QSslConfiguration ssl(QSslConfiguration::defaultConfiguration());
199 QList<QSslCertificate> clist;
202 clist = QSslCertificate::fromData(
m_cert, QSsl::Der);
204 LOG(VB_GENERAL, LOG_WARNING,
LOC + QString(
"Invalid certificate: %1")
205 .arg(
m_cert.toPercentEncoding().constData()) );
211 ssl.setPeerVerifyMode(QSslSocket::VerifyNone);
215 ssl.setCaCertificates(clist);
222 if (!fname.isEmpty())
225 if (f1.open(QIODevice::ReadOnly))
227 QSslCertificate cert(&f1, QSsl::Pem);
229 ssl.setLocalCertificate(cert);
231 LOG(VB_GENERAL, LOG_WARNING,
LOC +
232 QString(
"'%1' is an invalid certificate").arg(f1.fileName()) );
236 LOG(VB_GENERAL, LOG_WARNING,
LOC +
237 QString(
"Opening client certificate '%1': %2")
238 .arg(f1.fileName(), f1.errorString()) );
243 if (!fname.isEmpty())
246 if (f2.open(QIODevice::ReadOnly))
248 QSslKey key(&f2, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey,
251 ssl.setPrivateKey(key);
253 LOG(VB_GENERAL, LOG_WARNING,
LOC +
254 QString(
"'%1' is an invalid key").arg(f2.fileName()) );
258 LOG(VB_GENERAL, LOG_WARNING,
LOC +
259 QString(
"Opening private key '%1': %2")
260 .arg(f2.fileName(), f2.errorString()) );
269 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Request %2 bytes=%3- from %4")
289 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Started 0x%2")
290 .arg(
m_id).arg(quintptr(reply),0,16) );
302 #ifndef QT_NO_OPENSSL
303 connect(reply, &QNetworkReply::sslErrors,
this,
311 LOG(VB_GENERAL, LOG_ERR,
LOC +
312 QString(
"(%1) Started but m_reply not NULL").arg(
m_id));
319 qlonglong len = reply->header(QNetworkRequest::ContentLengthHeader)
321 return ok ? len : -1;
325 qulonglong &first, qulonglong &last)
327 QByteArray range = reply->rawHeader(
"Content-Range");
333 const char *fmt =
" bytes %20" SCNd64
" - %20" SCNd64
" / %20" SCNd64;
334 if (3 != std::sscanf(range.constData(), fmt, &first, &last, &len))
336 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Invalid Content-Range:'%1'")
337 .arg(range.constData()) );
341 return static_cast<qlonglong
>(len);
345 static bool inline RequestRange(
const QNetworkRequest &request,
346 qlonglong &first, qlonglong &last)
350 QByteArray range = request.rawHeader(
"Range");
354 if (1 > std::sscanf(range.constData(),
" bytes %20lld - %20lld", &first, &last))
356 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Invalid Range:'%1'")
357 .arg(range.constData()) );
372 qint64 avail =
m_reply->bytesAvailable();
374 (avail <= 4 *
kMaxBuffer) ? LOG_INFO : LOG_WARNING,
375 LOC + QString(
"(%1) Ready 0x%2, %3 bytes available").arg(
m_id)
376 .arg(quintptr(
m_reply),0,16).arg(avail) );
380 qulonglong first = 0;
386 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Ready 0x%2, range %3-%4/%5")
387 .arg(
m_id).arg(quintptr(
m_reply),0,16).arg(first).arg(last).arg(len) );
392 if (m_state < kReady || m_size >= 0)
394 LOG(VB_FILE, LOG_INFO,
LOC +
395 QString(
"(%1) Ready 0x%2, content length %3")
401 m_state = std::max(m_state,
kReady);
411 LOG(VB_GENERAL, LOG_ERR,
LOC +
412 QString(
"(%1) ReadyRead but m_reply = NULL").arg(
m_id));
424 if (QNetworkReply::NoError ==
error)
428 QNetworkRequest::RedirectionTargetAttribute).toUrl();
435 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1) Too many redirections")
444 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1) Redirection loop to %2")
445 .arg(
m_id).arg(url.toString()) );
450 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Redirecting").arg(
m_id));
457 LOG(VB_FILE, LOG_WARNING,
LOC + QString(
"(%1): %2")
467 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Finished 0x%2 %3/%4 bytes from %5")
479 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"(%1) Finished but m_reply = NULL")
484 #ifndef QT_NO_OPENSSL
493 for (
const auto& e : std::as_const(errors))
495 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) SSL error %2: ")
496 .arg(
m_id).arg(e.error()) + e.errorString() );
499 #if 1 // The BBC use a self certified cert
500 case QSslError::SelfSignedCertificateInChain:
511 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) SSL errors ignored").arg(
m_id));
512 m_reply->ignoreSslErrors(errors);
517 LOG(VB_GENERAL, LOG_ERR,
LOC +
518 QString(
"(%1) SSL error but m_reply = NULL").arg(
m_id) );
530 return url.isValid() &&
531 (url.scheme() ==
"http" || url.scheme() ==
"https") &&
532 !url.authority().isEmpty() &&
533 !url.path().isEmpty();
548 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Cancelled").arg(
m_id) );
556 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Abort 0x%2")
569 QElapsedTimer
t;
t.start();
577 unsigned elapsed =
t.elapsed();
578 if (elapsed >= millisecs)
589 qint64 avail =
m_reply->read(
reinterpret_cast< char*
>(data), sz);
593 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) safe_read @ %4 => %2/%3, %5 mS")
594 .arg(
m_id).arg(avail).arg(sz).arg(
m_pos).arg(
t.elapsed()) );
608 LOG(VB_GENERAL, LOG_ERR,
LOC +
609 QString(
"(%1) Seek(%2) out of range [0..%3]")
614 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) Seek(%2) curr %3 end %4")
642 QElapsedTimer
t;
t.start();
645 auto elapsed = std::chrono::milliseconds(
t.elapsed());
659 QElapsedTimer
t;
t.start();
662 auto elapsed = std::chrono::milliseconds(
t.elapsed());
675 return !
m_reply ? QNetworkReply::OperationCanceledError :
m_reply->error();
697 QByteArray data =
m_reply->readAll();
698 m_pos += data.size();
746 QMutexLocker locker(&
s_mtx);
756 setObjectName(
"NAMThread");
758 #ifndef QT_NO_OPENSSL
761 qRegisterMetaType< QList<QSslError> >();
775 LOG(VB_FILE, LOG_INFO,
LOC +
"NAMThread starting");
777 m_nam =
new QNetworkAccessManager();
778 m_nam->setObjectName(
"NetStream NAM");
781 std::unique_ptr<QNetworkDiskCache> cache(
new QNetworkDiskCache());
783 cache->setCacheDirectory(
GetConfDir() +
"/cache/netstream-" +
786 m_nam->setCache(cache.release());
790 QString proxy(qEnvironmentVariable(
"MYTHMHEG_PROXY"));
791 if (!proxy.isEmpty())
793 QUrl url(proxy, QUrl::TolerantMode);
794 QNetworkProxy::ProxyType
type {QNetworkProxy::NoProxy};
795 if (url.scheme().isEmpty()
796 || (url.scheme() ==
"http")
797 || (url.scheme() ==
"https"))
798 type = QNetworkProxy::HttpProxy;
799 else if (url.scheme() ==
"socks")
800 type = QNetworkProxy::Socks5Proxy;
801 else if (url.scheme() ==
"cache")
802 type = QNetworkProxy::HttpCachingProxy;
803 else if (url.scheme() ==
"ftp")
804 type = QNetworkProxy::FtpCachingProxy;
806 if (QNetworkProxy::NoProxy !=
type)
808 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Using proxy: " + proxy);
809 m_nam->setProxy(QNetworkProxy(
810 type, url.host(), url.port(), url.userName(), url.password() ));
814 LOG(VB_MHEG, LOG_ERR,
LOC + QString(
"Unknown proxy type %1")
815 .arg(url.scheme()) );
820 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
829 QCoreApplication::processEvents();
840 QScopedPointer< QEvent > ev(
m_workQ.dequeue());
852 LOG(VB_FILE, LOG_INFO,
LOC +
"NAMThread stopped");
870 switch (event->type())
874 #pragma GCC diagnostic push
875 #pragma GCC diagnostic ignored "-Wswitch"
878 #pragma GCC diagnostic pop
889 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Invalid NetStreamRequest");
893 if (!
p->m_bCancelled)
895 QNetworkReply *reply =
m_nam->get(
p->m_req);
896 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) StartRequest 0x%2")
897 .arg(
p->m_id).arg(quintptr(reply),0,16) );
902 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"(%1) NetStreamRequest cancelled").arg(
p->m_id) );
911 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Invalid NetStreamAbort");
915 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"(%1) AbortRequest 0x%2").arg(
p->m_id)
916 .arg(quintptr(
p->m_reply),0,16) );
918 p->m_reply->disconnect();
926 auto interfaces = QNetworkInterface::allInterfaces();
927 return std::any_of(interfaces.begin(), interfaces.end(),
928 [](
const QNetworkInterface& iface)
930 auto f = iface.flags();
931 if (f.testFlag(QNetworkInterface::IsLoopBack))
933 return f.testFlag(QNetworkInterface::IsRunning);
943 QMutexLocker locker(&m.
m_mutex);
948 QAbstractNetworkCache *cache = m.
m_nam->cache();
952 QNetworkCacheMetaData meta = cache->metaData(url);
955 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"GetLastModified('%1') not in cache")
956 .arg(url.toString()));
961 QDateTime
const now(QDateTime::currentDateTime());
962 QDateTime expire = meta.expirationDate();
963 if (expire.isValid() && expire.toLocalTime() < now)
965 LOG(VB_FILE, LOG_INFO,
LOC + QString(
"GetLastModified('%1') past expiration %2")
966 .arg(url.toString(), expire.toString()));
971 QDateTime lastMod = meta.lastModified();
973 QNetworkCacheMetaData::RawHeaderList
headers = meta.rawHeaders();
974 for (
const auto& h : std::as_const(
headers))
977 static const QString kSzFormat {
"ddd, dd MMM yyyy HH:mm:ss 'GMT'" };
979 QString
const first(h.first.toLower());
980 if (first ==
"cache-control")
982 QString
const second(h.second.toLower());
983 if (second ==
"no-cache" || second ==
"no-store")
985 LOG(VB_FILE, LOG_INFO,
LOC +
986 QString(
"GetLastModified('%1') Cache-Control disabled")
987 .arg(url.toString()) );
992 else if (first ==
"date")
997 LOG(VB_GENERAL, LOG_WARNING,
LOC +
998 QString(
"GetLastModified invalid Date header '%1'")
999 .arg(h.second.constData()));
1002 d.setTimeSpec(Qt::UTC);
1007 LOG(VB_FILE, LOG_DEBUG,
LOC + QString(
"GetLastModified('%1') last modified %2")
1008 .arg(url.toString(), lastMod.toString()));