leocad/common/lc_http.cpp

105 lines
2.1 KiB
C++
Raw Normal View History

2018-04-14 20:44:39 +02:00
#include "lc_global.h"
#include "lc_http.h"
#ifdef Q_OS_WIN
2024-04-28 02:19:34 +02:00
#include <windows.h>
2018-04-14 20:44:39 +02:00
#include <wininet.h>
lcHttpReply::lcHttpReply(QObject* Parent, const QString& URL)
: QThread(Parent)
{
mError = true;
mAbort = false;
mURL = URL;
}
void lcHttpReply::run()
{
HINTERNET Session = nullptr;
HINTERNET Request = nullptr;
2019-05-18 20:33:27 +02:00
static_assert(sizeof(wchar_t) == sizeof(QChar), "Character size mismatch");
2018-04-14 20:44:39 +02:00
Session = InternetOpen(L"LeoCAD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2024-11-28 22:07:29 +01:00
2018-04-14 20:44:39 +02:00
if (!Session)
return;
2024-11-28 22:07:29 +01:00
Request = InternetOpenUrl(Session, (WCHAR*)mURL.data(), NULL, 0, INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP, 0);
2018-04-14 20:44:39 +02:00
if (!Request)
{
InternetCloseHandle(Session);
return;
}
for (;;)
{
char Buffer[1024];
DWORD BytesRead;
if (mAbort)
break;
if (!InternetReadFile(Request, Buffer, sizeof(Buffer), &BytesRead))
break;
if (BytesRead)
mBuffer.append(Buffer, BytesRead);
else
{
mError = false;
break;
}
Sleep(0);
}
InternetCloseHandle(Request);
InternetCloseHandle(Session);
}
lcHttpManager::lcHttpManager(QObject* Owner)
: QObject(Owner)
{
}
lcHttpReply* lcHttpManager::DownloadFile(const QString& Url)
{
lcHttpReply* Reply = new lcHttpReply(this, Url);
connect(Reply, &QThread::finished, [this, Reply] { emit DownloadFinished(Reply); });
Reply->start();
return Reply;
}
#else
lcHttpManager::lcHttpManager(QObject* Owner)
: QNetworkAccessManager(Owner)
{
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(Finished(QNetworkReply*)));
}
lcHttpReply* lcHttpManager::lcHttpManager::DownloadFile(const QString& Url)
{
2024-11-28 22:07:29 +01:00
QNetworkRequest Request = QNetworkRequest(QUrl(Url));
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) // default changed in Qt6
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
Request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
#elif (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
Request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
#endif
#endif
2024-11-27 21:33:07 +01:00
2024-11-28 22:07:29 +01:00
return (lcHttpReply*)get(Request);
2018-04-14 20:44:39 +02:00
}
void lcHttpManager::Finished(QNetworkReply* Reply)
{
2018-04-14 20:53:00 +02:00
emit DownloadFinished((lcHttpReply*)Reply);
2018-04-14 20:44:39 +02:00
}
#endif