7#include <QtCore/qtimer.h>
8#include <QtCore/qdatetime.h>
9#include <QtCore/qcoreapplication.h>
10#include <QtCore/qfileinfo.h>
11#include <QtCore/qthread.h>
12#include <QtCore/private/qeventdispatcher_wasm_p.h>
13#include <QtCore/private/qoffsetstringarray_p.h>
14#include <QtCore/private/qtools_p.h>
16#include <private/qnetworkaccessmanager_p.h>
17#include <private/qnetworkfile_p.h>
19#include <emscripten.h>
20#include <emscripten/fetch.h>
31 "access-control-request-headers",
32 "access-control-request-method",
62 , downloadBufferReadPosition(0)
63 , downloadBufferCurrentSize(0)
64 , totalDownloadSize(0)
104QByteArray QNetworkReplyWasmImpl::methodName()
const
136 emscripten_fetch_close(
d->m_fetch);
164 q->setFinished(
true);
192 qint64 howMuch =
qMin(maxlen, (
d->downloadBuffer.size() -
d->downloadBufferReadPosition));
193 memcpy(
data,
d->downloadBuffer.constData() +
d->downloadBufferReadPosition, howMuch);
194 d->downloadBufferReadPosition += howMuch;
210 bool bufferingDisallowed =
213 if (bufferingDisallowed) {
238 if (!statusReason.isEmpty())
243 return 2 * factor + 1;
251 emscripten_fetch_attr_t attr;
252 emscripten_fetch_attr_init(&attr);
253 qstrncpy(attr.requestMethod,
q->methodName().constData(), 32);
255 attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
261 attr.attributes += EMSCRIPTEN_FETCH_NO_DOWNLOAD;
264 attr.attributes += EMSCRIPTEN_FETCH_APPEND;
269 attr.attributes -= EMSCRIPTEN_FETCH_PERSIST_FILE;
277 attr.timeoutMSecs =
request.transferTimeout();
290 std::unique_lock
lock{ fetchContext->mutex };
292 fetchContext->state = FetchContext::State::FINISHED;
299 const auto reply = fetchContext->reply;
314 if (headersData.count() > 0) {
316 for (
const auto &headerName : headersData) {
320 customHeaders[
i++] = headerName.constData();
324 if (!trimmedHeaders.isEmpty()) {
325 qWarning() <<
"Qt has trimmed the following forbidden headers from the request:"
328 customHeaders[
i] =
nullptr;
329 attr.requestHeaders = customHeaders;
335 attr.destinationPath = destinationPath.constData();
336 reply->m_fetch = emscripten_fetch(&attr,
url.constData());
356 percentFinished = bytesTotal ? (bytesReceived / bytesTotal) * 100 : 100;
358 emit q->downloadProgress(bytesReceived, bytesTotal);
367 q->setReadBufferSize(bufferSize);
386 if (headerName.isEmpty())
389 auto is = [&](
const char *what) {
390 return qstrnicmp(headerName.data(), headerName.size(), what) == 0;
395 if (is(
"content-type"))
397 else if (is(
"content-length"))
399 else if (is(
"cookie"))
406 else if (is(
"last-modified"))
411 if (is(
"set-cookie"))
413 else if (is(
"server"))
418 if (is(
"user-agent"))
439 if (headerName.isEmpty() || headersValue.isEmpty())
444 if (headerIndex == -1)
445 q->setRawHeader(headerName, headersValue);
451 emit q->metaDataChanged();
490 if (bytesToBuffer <= 0)
491 bytesToBuffer = 2*1024;
516 auto fetchContext =
static_cast<FetchContext *
>(fetch->userData);
517 std::unique_lock
lock{ fetchContext->mutex };
527 const auto reply = fetchContext->reply;
532 reply->setStatusCode(fetch->status, statusText);
533 reply->setReplyFinished();
535 reply->m_fetch =
nullptr;
544 q->setFinished(
true);
545 emit q->readChannelFinished();
558 const auto fetchContext =
static_cast<FetchContext*
>(fetch->userData);
559 const auto reply = fetchContext->reply;
561 if (fetch->readyState == 2) {
562 size_t headerLength = emscripten_fetch_get_response_headers_length(fetch);
564 emscripten_fetch_get_response_headers(fetch,
str.
data(),
str.
size());
572 const auto fetchContext =
static_cast<FetchContext*
>(fetch->userData);
573 const auto reply = fetchContext->reply;
575 if (fetch->status < 400) {
576 uint64_t bytes = fetch->dataOffset + fetch->numBytes;
577 uint64_t tBytes = fetch->totalBytes;
580 reply->emitDataReadProgress(bytes, tBytes);
587 const auto fetchContext =
static_cast<FetchContext*
>(fetch->userData);
588 std::unique_lock
lock{ fetchContext->mutex };
598 const auto reply = fetchContext->reply;
601 if (fetch->status > 600)
608 reply->setStatusCode(fetch->status, statusText);
611 reply->setReplyFinished();
613 reply->m_fetch =
nullptr;
623 switch (httpStatusCode) {
676 if (httpStatusCode > 500) {
679 }
else if (httpStatusCode >= 400) {
683 qWarning(
"QNetworkAccess: got HTTP status code %d which is not expected from url: \"%s\"",
694#include "moc_qnetworkreplywasmimpl_p.cpp"
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static void runOnMainThread(std::function< void(void)> fn)
\inmodule QtCore \reentrant
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
Operation
Indicates the operation this reply is processing.
QNetworkAccessManager::Operation operation
QNetworkReply::NetworkError errorCode
std::shared_ptr< QRingBuffer > outgoingDataBuffer
qint64 downloadBufferCurrentSize
static void setReplyAttributes(quintptr data, int statusCode, const QString &statusReason)
~QNetworkReplyWasmImplPrivate()
void setup(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
static void downloadFailed(emscripten_fetch_t *fetch)
QNetworkReplyWasmImplPrivate()
static void downloadSucceeded(emscripten_fetch_t *fetch)
void emitReplyError(QNetworkReply::NetworkError errorCode, const QString &)
QByteArray downloadBuffer
void setStatusCode(int status, const QByteArray &statusText)
void _q_bufferOutgoingData()
void headersReceived(const QByteArray &buffer)
static void stateChange(emscripten_fetch_t *fetch)
void emitDataReadProgress(qint64 done, qint64 total)
FetchContext * m_fetchContext
void _q_bufferOutgoingDataFinished()
static QNetworkReply::NetworkError statusCodeFromHttp(int httpStatusCode, const QUrl &url)
static void downloadProgress(emscripten_fetch_t *fetch)
void dataReceived(const QByteArray &buffer)
qint64 size() const override
For open random-access devices, this function returns the size of the device.
virtual qint64 bytesAvailable() const override
Returns the number of bytes that are available for reading.
virtual qint64 readData(char *data, qint64 maxlen) override
virtual void abort() override
Aborts the operation immediately and close down any network connections still open.
virtual bool isSequential() const override
QNetworkReplyWasmImpl(QObject *parent=nullptr)
virtual void close() override
Closes this device for reading.
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
virtual void close() override
Closes this device for reading.
QNetworkAccessManager::Operation operation() const
Returns the operation that was posted for this reply.
NetworkError
Indicates all possible error conditions found during the processing of the request.
@ ContentOperationNotPermittedError
@ OperationNotImplementedError
@ ServiceUnavailableError
@ ProtocolInvalidOperationError
@ ProxyAuthenticationRequiredError
@ AuthenticationRequiredError
QNetworkRequest request() const
Returns the request that was posted for this reply.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
KnownHeaders
List of known header types that QNetworkRequest parses.
@ UseCredentialsAttribute
@ HttpStatusCodeAttribute
@ CacheSaveControlAttribute
@ CacheLoadControlAttribute
@ DoNotBufferUploadDataAttribute
@ HttpReasonPhraseAttribute
QVariant attribute(Attribute code, const QVariant &defaultValue=QVariant()) const
Returns the attribute associated with the code code.
QVariant header(KnownHeaders header) const
Returns the value of the known network header header if it is present in this request.
QByteArray rawHeader(QAnyStringView headerName) const
Returns the raw form of header headerName.
QList< QByteArray > rawHeaderList() const
Returns a list of all raw headers that are set in this network request.
QUrl url() const
Returns the URL this network request is referring to.
CacheLoadControl
Controls the caching mechanism of QNetworkAccessManager.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
\macro QT_RESTRICTED_CAST_FROM_ASCII
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
qsizetype size() const noexcept
Returns the number of characters in this string.
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QChar * data()
Returns a pointer to the data stored in the QString.
QByteArray toUtf8() const &
QString userInfo(ComponentFormattingOptions options=PrettyDecoded) const
Returns the user info of the URL, or an empty string if the user info is undefined.
QString fileName(ComponentFormattingOptions options=FullyDecoded) const
QString userName(ComponentFormattingOptions options=FullyDecoded) const
Returns the user name of the URL if it is defined; otherwise an empty string is returned.
QString password(ComponentFormattingOptions=FullyDecoded) const
Returns the password of the URL if it is defined; otherwise an empty string is returned.
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
Combined button and popup list for selecting options.
constexpr char toAsciiLower(char ch) noexcept
constexpr Initialization Uninitialized
static int arrayLength(const QString &rawType)
int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
static QString header(const QString &name)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
constexpr const T & qMin(const T &a, const T &b)
constexpr int getArraySize(int factor)
static int parseHeaderName(const QByteArray &headerName)
constexpr auto qOffsetStringArray(const char(&...strings)[Nx]) noexcept
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble GLdouble GLdouble q
#define qPrintable(string)
#define QStringLiteral(str)
if(qFloatDistance(a, b)<(1<< 7))
[0]
QUrl url("example.com")
[constructor-url-reference]
The FetchContext class ensures the requestData object remains valid while a fetch operation is pendin...
QNetworkReplyWasmImplPrivate * reply