diff --git a/src/networkaccessmanager.cpp b/src/networkaccessmanager.cpp index 4e64762475..0586dbbbe7 100644 --- a/src/networkaccessmanager.cpp +++ b/src/networkaccessmanager.cpp @@ -190,7 +190,7 @@ NetworkAccessManager::NetworkAccessManager(QObject* parent, const Config* config connect(&m_replyTracker, SIGNAL(started(QNetworkReply*, int)), this, SLOT(handleStarted(QNetworkReply*, int))); connect(&m_replyTracker, SIGNAL(sslErrors(QNetworkReply*, const QList&)), this, SLOT(handleSslErrors(QNetworkReply*, const QList&))); connect(&m_replyTracker, SIGNAL(error(QNetworkReply*, int, QNetworkReply::NetworkError)), this, SLOT(handleNetworkError(QNetworkReply*, int))); - connect(&m_replyTracker, SIGNAL(finished(QNetworkReply*, int, int, const QString&, const QString&)), SLOT(handleFinished(QNetworkReply*, int, int, const QString&, const QString&))); + connect(&m_replyTracker, SIGNAL(finished(QNetworkReply*, int, int, const QString&, const QString&, int)), SLOT(handleFinished(QNetworkReply*, int, int, const QString&, const QString&, int))); } void NetworkAccessManager::prepareSslConfiguration(const Config* config) @@ -468,7 +468,7 @@ void NetworkAccessManager::handleStarted(QNetworkReply* reply, int requestId) data["status"] = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); data["statusText"] = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute); data["contentType"] = reply->header(QNetworkRequest::ContentTypeHeader); - data["bodySize"] = reply->size(); + data["bodySize"] = 0; data["redirectURL"] = reply->header(QNetworkRequest::LocationHeader); data["headers"] = headers; data["time"] = QDateTime::currentDateTime(); @@ -488,7 +488,7 @@ void NetworkAccessManager::provideAuthentication(QNetworkReply* reply, QAuthenti } } -void NetworkAccessManager::handleFinished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body) +void NetworkAccessManager::handleFinished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body, int bodySize) { QVariantList headers; foreach (QByteArray headerName, reply->rawHeaderList()) { @@ -509,7 +509,7 @@ void NetworkAccessManager::handleFinished(QNetworkReply* reply, int requestId, i data["headers"] = headers; data["time"] = QDateTime::currentDateTime(); data["body"] = body; - data["bodySize"] = body.length(); + data["bodySize"] = bodySize; emit resourceReceived(data); diff --git a/src/networkaccessmanager.h b/src/networkaccessmanager.h index 2b342b525e..75c6ec0bef 100644 --- a/src/networkaccessmanager.h +++ b/src/networkaccessmanager.h @@ -114,7 +114,7 @@ class NetworkAccessManager : public QNetworkAccessManager private slots: void handleStarted(QNetworkReply* reply, int requestId); - void handleFinished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body); + void handleFinished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body, int bodySize); void provideAuthentication(QNetworkReply* reply, QAuthenticator* authenticator); void handleSslErrors(QNetworkReply* reply, const QList& errors); void handleNetworkError(QNetworkReply* reply, int requestId); diff --git a/src/networkreplyproxy.cpp b/src/networkreplyproxy.cpp index 6a79fe0bd5..0ac12db021 100644 --- a/src/networkreplyproxy.cpp +++ b/src/networkreplyproxy.cpp @@ -56,6 +56,8 @@ NetworkReplyProxy::NetworkReplyProxy(QObject* parent, QNetworkReply* reply, // for the data proxy... setOpenMode(ReadOnly); + + m_dataSize = 0; } QString NetworkReplyProxy::body() @@ -70,6 +72,11 @@ QString NetworkReplyProxy::body() return ret; } +int NetworkReplyProxy::bodySize() +{ + return m_dataSize; +} + void NetworkReplyProxy::abort() { m_reply->abort(); @@ -163,6 +170,8 @@ void NetworkReplyProxy::readInternal() m_data += data; } + m_dataSize += data.size(); + //this is a temporary buffer, data is wiped after a call to 'readData' m_buffer += data; emit readyRead(); diff --git a/src/networkreplyproxy.h b/src/networkreplyproxy.h index 18f12490c6..450e4db9a3 100644 --- a/src/networkreplyproxy.h +++ b/src/networkreplyproxy.h @@ -57,6 +57,10 @@ class NetworkReplyProxy : public QNetworkReply */ QString body(); + /* + Returns the body size. Even if body isn't being captured. + */ + int bodySize(); /* Returns nested reply @@ -92,6 +96,7 @@ public Q_SLOTS: private: QNetworkReply* m_reply; QByteArray m_data; + int m_dataSize; QByteArray m_buffer; bool m_shouldCaptureResponseBody; }; diff --git a/src/networkreplytracker.cpp b/src/networkreplytracker.cpp index 928f09a9c7..b8738f8182 100644 --- a/src/networkreplytracker.cpp +++ b/src/networkreplytracker.cpp @@ -88,7 +88,7 @@ void NetworkReplyTracker::finishReply(QNetworkReply* reply, int status, const QS m_started.remove(proxy); - emit finished(proxy, requestId, status, statusText, proxy->body()); + emit finished(proxy, requestId, status, statusText, proxy->body(), proxy->bodySize()); } void NetworkReplyTracker::handleIncomingData() diff --git a/src/networkreplytracker.h b/src/networkreplytracker.h index e4245d963b..198fe665e2 100644 --- a/src/networkreplytracker.h +++ b/src/networkreplytracker.h @@ -73,7 +73,7 @@ class NetworkReplyTracker: public QObject signals: void started(QNetworkReply* reply, int requestId); - void finished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body); + void finished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body, int bodySize); void sslErrors(QNetworkReply*, const QList&); void error(QNetworkReply*, int requestId, QNetworkReply::NetworkError); diff --git a/test/module/webpage/bodysize.js b/test/module/webpage/bodysize.js new file mode 100644 index 0000000000..1fd0f08bc1 --- /dev/null +++ b/test/module/webpage/bodysize.js @@ -0,0 +1,40 @@ +var port; +setup(function () { + var server = require("webserver").create(); + + // Find an unused port in the 1024--32767 range on which to run the + // rest of the tests. + for (var i = 1024; i < 32768; i++) { + if (server.listen(i, function(rq,rs){ + rs.statusCode = 200; + rs.write('test'); + setTimeout(function() { + rs.write('test'); + rs.close(); + }, 100); + })) { + port = server.port; + return; + } + } + assert_unreached("unable to find a free TCP port for server tests"); +}, { "test_timeout": 2000 }); + +async_test(function () { + var page = require('webpage').create(); + + // Do NOT capture any body. + page.captureContent = []; + + var size = 0; + page.onResourceReceived = function (req) { + if (req.stage == 'end') { + size = req.bodySize; + } + }; + + page.open('http://localhost:' + port, this.step_func_done(function() { + assert_equals(size, 8); + })); + +}, "bodysize");