1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include "lc_global.h"
#include "lc_http.h"
#ifdef Q_OS_WIN
#include <windows.h>
#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;
static_assert(sizeof(wchar_t) == sizeof(QChar), "Character size mismatch");
Session = InternetOpen(L"LeoCAD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!Session)
return;
Request = InternetOpenUrl(Session, (WCHAR*)mURL.data(), NULL, 0, INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP, 0);
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)
{
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
return (lcHttpReply*)get(Request);
}
void lcHttpManager::Finished(QNetworkReply* Reply)
{
emit DownloadFinished((lcHttpReply*)Reply);
}
#endif
|