Store response messages as QByteArray

This prevents conversion of QByteArray to QString from truncating content at a null byte in the QByteArray. This truncation can be a problem if the response body is a binary object (e.g. PDF)

Fixes #322
This commit is contained in:
Dan Ivovich 2012-04-17 12:06:12 -04:00
parent 73618220b0
commit 1ef8f4c844
9 changed files with 18 additions and 14 deletions

View File

@ -62,7 +62,7 @@ void Connection::writeResponse(Response *response) {
else else
m_socket->write("failure\n"); m_socket->write("failure\n");
QByteArray messageUtf8 = response->message().toUtf8(); QByteArray messageUtf8 = response->message();
QString messageLength = QString::number(messageUtf8.size()) + "\n"; QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii()); m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8); m_socket->write(messageUtf8);

View File

@ -55,8 +55,7 @@ void CurrentUrl::start() {
QUrl humanUrl = wasRedirectedAndNotModifiedByJavascript() ? QUrl humanUrl = wasRedirectedAndNotModifiedByJavascript() ?
page()->currentFrame()->url() : page()->currentFrame()->requestedUrl(); page()->currentFrame()->url() : page()->currentFrame()->requestedUrl();
QByteArray encodedBytes = humanUrl.toEncoded(); QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes); emit finished(new Response(true, encodedBytes));
emit finished(new Response(true, urlString));
} }
bool CurrentUrl::wasRegularLoad() { bool CurrentUrl::wasRegularLoad() {

View File

@ -10,7 +10,7 @@ void Execute::start() {
if (result.isValid()) { if (result.isValid()) {
emit finished(new Response(true)); emit finished(new Response(true));
} else { } else {
emit finished(new Response(false, "Javascript failed to execute")); emit finished(new Response(false, QString("Javascript failed to execute")));
} }
} }

View File

@ -13,7 +13,7 @@ void Find::start() {
message = result.toString(); message = result.toString();
emit finished(new Response(true, message)); emit finished(new Response(true, message));
} else { } else {
emit finished(new Response(false, "Invalid XPath expression")); emit finished(new Response(false, QString("Invalid XPath expression")));
} }
} }

View File

@ -50,7 +50,7 @@ void FrameFocus::focusId(QString name) {
void FrameFocus::focusParent() { void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) { if (page()->currentFrame()->parentFrame() == 0) {
emit finished(new Response(false, "Already at parent frame.")); emit finished(new Response(false, QString("Already at parent frame.")));
} else { } else {
page()->currentFrame()->parentFrame()->setFocus(); page()->currentFrame()->parentFrame()->setFocus();
success(); success();
@ -58,7 +58,7 @@ void FrameFocus::focusParent() {
} }
void FrameFocus::frameNotFound() { void FrameFocus::frameNotFound() {
emit finished(new Response(false, "Unable to locate frame. ")); emit finished(new Response(false, QString("Unable to locate frame. ")));
} }
void FrameFocus::success() { void FrameFocus::success() {

View File

@ -7,7 +7,6 @@ RequestedUrl::RequestedUrl(WebPage *page, QStringList &arguments, QObject *paren
void RequestedUrl::start() { void RequestedUrl::start() {
QUrl humanUrl = page()->currentFrame()->requestedUrl(); QUrl humanUrl = page()->currentFrame()->requestedUrl();
QByteArray encodedBytes = humanUrl.toEncoded(); QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes); emit finished(new Response(true, encodedBytes));
emit finished(new Response(true, urlString));
} }

View File

@ -2,6 +2,11 @@
#include <iostream> #include <iostream>
Response::Response(bool success, QString message) { Response::Response(bool success, QString message) {
m_success = success;
m_message = message.toUtf8();
}
Response::Response(bool success, QByteArray message) {
m_success = success; m_success = success;
m_message = message; m_message = message;
} }
@ -14,6 +19,6 @@ bool Response::isSuccess() const {
return m_success; return m_success;
} }
QString Response::message() const { QByteArray Response::message() const {
return m_message; return m_message;
} }

View File

@ -1,13 +1,15 @@
#include <QString> #include <QString>
#include <QByteArray>
class Response { class Response {
public: public:
Response(bool success, QString message); Response(bool success, QString message);
Response(bool success, QByteArray message);
Response(bool success); Response(bool success);
bool isSuccess() const; bool isSuccess() const;
QString message() const; QByteArray message() const;
private: private:
bool m_success; bool m_success;
QString m_message; QByteArray m_message;
}; };

View File

@ -7,7 +7,6 @@ Url::Url(WebPage *page, QStringList &arguments, QObject *parent) : Command(page,
void Url::start() { void Url::start() {
QUrl humanUrl = page()->currentFrame()->url(); QUrl humanUrl = page()->currentFrame()->url();
QByteArray encodedBytes = humanUrl.toEncoded(); QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes); emit finished(new Response(true, encodedBytes));
emit finished(new Response(true, urlString));
} }