wasm: fix blob download handling

Change-Id: I34a8ec05c18b15ed71787986b5b0316693235b4d
Fixes: QTBUG-72105
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Lorn Potter 2018-12-17 13:16:40 +10:00
parent 4d9ac14bf8
commit 2780f80fae

View File

@ -102,6 +102,8 @@ static void q_loadCallback(val event)
return; return;
} }
QString statusText = QString::fromStdString(xhr["statusText"].as<std::string>()); QString statusText = QString::fromStdString(xhr["statusText"].as<std::string>());
int readyState = xhr["readyState"].as<int>();
if (status == 200 || status == 203) { if (status == 200 || status == 203) {
QString responseString; QString responseString;
const std::string responseType = xhr["responseType"].as<std::string>(); const std::string responseType = xhr["responseType"].as<std::string>();
@ -112,13 +114,15 @@ static void q_loadCallback(val event)
QString::fromStdWString(val::global("JSON").call<std::wstring>("stringify", xhr["response"])); QString::fromStdWString(val::global("JSON").call<std::wstring>("stringify", xhr["response"]));
} else if (responseType == "arraybuffer" || responseType == "blob") { } else if (responseType == "arraybuffer" || responseType == "blob") {
// handle this data in the FileReader, triggered by the call to readAsArrayBuffer // handle this data in the FileReader, triggered by the call to readAsArrayBuffer
val blob = xhr["response"];
val reader = val::global("FileReader").new_(); val reader = val::global("FileReader").new_();
reader.set("onload", val::module_property("QNetworkReplyWasmImplPrivate_readBinary")); reader.set("onload", val::module_property("QNetworkReplyWasmImplPrivate_readBinary"));
reader.set("data-handler", xhr["data-handler"]); reader.set("data-handler", xhr["data-handler"]);
reader.call<void>("readAsArrayBuffer", xhr["response"]);
reader.call<void>("readAsArrayBuffer", blob);
} }
int readyState = xhr["readyState"].as<int>();
if (readyState == 4) { // done if (readyState == 4) { // done
reply->setReplyAttributes(xhr["data-handler"].as<quintptr>(), status, statusText); reply->setReplyAttributes(xhr["data-handler"].as<quintptr>(), status, statusText);
@ -167,9 +171,9 @@ static void q_readBinary(val event)
reinterpret_cast<quintptr>(buffer.data()), size); reinterpret_cast<quintptr>(buffer.data()), size);
destinationTypedArray.call<void>("set", sourceTypedArray); destinationTypedArray.call<void>("set", sourceTypedArray);
reply->dataReceived(buffer, buffer.size()); reply->dataReceived(buffer, buffer.size());
QCoreApplication::processEvents();
} }
EMSCRIPTEN_BINDINGS(network_module) { EMSCRIPTEN_BINDINGS(network_module) {
function("QNetworkReplyWasmImplPrivate_requestErrorCallback", q_requestErrorCallback); function("QNetworkReplyWasmImplPrivate_requestErrorCallback", q_requestErrorCallback);
function("QNetworkReplyWasmImplPrivate_progressCallback", q_progressCallback); function("QNetworkReplyWasmImplPrivate_progressCallback", q_progressCallback);
@ -240,9 +244,6 @@ qint64 QNetworkReplyWasmImpl::bytesAvailable() const
{ {
Q_D(const QNetworkReplyWasmImpl); Q_D(const QNetworkReplyWasmImpl);
if (!d->isFinished)
return QNetworkReply::bytesAvailable();
return QNetworkReply::bytesAvailable() + d->downloadBufferCurrentSize - d->downloadBufferReadPosition; return QNetworkReply::bytesAvailable() + d->downloadBufferCurrentSize - d->downloadBufferReadPosition;
} }
@ -357,8 +358,7 @@ void QNetworkReplyWasmImplPrivate::doSendRequest()
m_xhr.set("responseType", val("json")); m_xhr.set("responseType", val("json"));
dataToSend = val(extraDataString.toStdString()); dataToSend = val(extraDataString.toStdString());
} }
} } else if (contentType.contains("form")) { //construct form data
if (contentType.contains("form")) { //construct form data
if (!extraDataString.isEmpty()) { if (!extraDataString.isEmpty()) {
val formData = val::global("FormData").new_(); val formData = val::global("FormData").new_();
QStringList formList = extraDataString.split('&'); QStringList formList = extraDataString.split('&');
@ -368,6 +368,8 @@ void QNetworkReplyWasmImplPrivate::doSendRequest()
} }
dataToSend = formData; dataToSend = formData;
} }
} else {
m_xhr.set("responseType", val("blob"));
} }
// set request headers // set request headers
for (auto header : request.rawHeaderList()) { for (auto header : request.rawHeaderList()) {
@ -417,10 +419,13 @@ void QNetworkReplyWasmImplPrivate::dataReceived(const QByteArray &buffer, int bu
downloadBuffer.append(buffer, bufferSize); downloadBuffer.append(buffer, bufferSize);
emit q->readyRead();
if (downloadBufferCurrentSize == totalDownloadSize) { if (downloadBufferCurrentSize == totalDownloadSize) {
q->setFinished(true); q->setFinished(true);
emit q->finished(); emit q->readChannelFinished();
} emit q->finished();
}
} }
//taken from qnetworkrequest.cpp //taken from qnetworkrequest.cpp