Skip to content

Commit

Permalink
Fix bodySize in onResourceReceived end, fix ariya#10156
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Dec 27, 2015
1 parent c7d7c65 commit 80ce3e5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/networkaccessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QSslError>&)), this, SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError>&)));
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)
Expand Down Expand Up @@ -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();
Expand All @@ -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()) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/networkaccessmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QSslError>& errors);
void handleNetworkError(QNetworkReply* reply, int requestId);
Expand Down
9 changes: 9 additions & 0 deletions src/networkreplyproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ NetworkReplyProxy::NetworkReplyProxy(QObject* parent, QNetworkReply* reply,

// for the data proxy...
setOpenMode(ReadOnly);

m_dataSize = 0;
}

QString NetworkReplyProxy::body()
Expand All @@ -70,6 +72,11 @@ QString NetworkReplyProxy::body()
return ret;
}

int NetworkReplyProxy::bodySize()
{
return m_dataSize;
}

void NetworkReplyProxy::abort()
{
m_reply->abort();
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/networkreplyproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,6 +96,7 @@ public Q_SLOTS:
private:
QNetworkReply* m_reply;
QByteArray m_data;
int m_dataSize;
QByteArray m_buffer;
bool m_shouldCaptureResponseBody;
};
Expand Down
2 changes: 1 addition & 1 deletion src/networkreplytracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/networkreplytracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QSslError>&);
void error(QNetworkReply*, int requestId, QNetworkReply::NetworkError);

Expand Down
40 changes: 40 additions & 0 deletions test/module/webpage/bodysize.js
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit 80ce3e5

Please sign in to comment.