Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Store response messages as QByteArray
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danivovich committed Apr 17, 2012
1 parent 7361822 commit 1ef8f4c
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Connection.cpp
Expand Up @@ -62,7 +62,7 @@ void Connection::writeResponse(Response *response) {
else
m_socket->write("failure\n");

QByteArray messageUtf8 = response->message().toUtf8();
QByteArray messageUtf8 = response->message();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
Expand Down
3 changes: 1 addition & 2 deletions src/CurrentUrl.cpp
Expand Up @@ -55,8 +55,7 @@ void CurrentUrl::start() {
QUrl humanUrl = wasRedirectedAndNotModifiedByJavascript() ?
page()->currentFrame()->url() : page()->currentFrame()->requestedUrl();
QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
emit finished(new Response(true, encodedBytes));
}

bool CurrentUrl::wasRegularLoad() {
Expand Down
2 changes: 1 addition & 1 deletion src/Execute.cpp
Expand Up @@ -10,7 +10,7 @@ void Execute::start() {
if (result.isValid()) {
emit finished(new Response(true));
} else {
emit finished(new Response(false, "Javascript failed to execute"));
emit finished(new Response(false, QString("Javascript failed to execute")));
}
}

2 changes: 1 addition & 1 deletion src/Find.cpp
Expand Up @@ -13,7 +13,7 @@ void Find::start() {
message = result.toString();
emit finished(new Response(true, message));
} else {
emit finished(new Response(false, "Invalid XPath expression"));
emit finished(new Response(false, QString("Invalid XPath expression")));
}
}

4 changes: 2 additions & 2 deletions src/FrameFocus.cpp
Expand Up @@ -50,15 +50,15 @@ void FrameFocus::focusId(QString name) {

void FrameFocus::focusParent() {
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 {
page()->currentFrame()->parentFrame()->setFocus();
success();
}
}

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() {
Expand Down
3 changes: 1 addition & 2 deletions src/RequestedUrl.cpp
Expand Up @@ -7,7 +7,6 @@ RequestedUrl::RequestedUrl(WebPage *page, QStringList &arguments, QObject *paren
void RequestedUrl::start() {
QUrl humanUrl = page()->currentFrame()->requestedUrl();
QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
emit finished(new Response(true, encodedBytes));
}

7 changes: 6 additions & 1 deletion src/Response.cpp
Expand Up @@ -2,6 +2,11 @@
#include <iostream>

Response::Response(bool success, QString message) {
m_success = success;
m_message = message.toUtf8();
}

Response::Response(bool success, QByteArray message) {
m_success = success;
m_message = message;
}
Expand All @@ -14,6 +19,6 @@ bool Response::isSuccess() const {
return m_success;
}

QString Response::message() const {
QByteArray Response::message() const {
return m_message;
}
6 changes: 4 additions & 2 deletions src/Response.h
@@ -1,13 +1,15 @@
#include <QString>
#include <QByteArray>

class Response {
public:
Response(bool success, QString message);
Response(bool success, QByteArray message);
Response(bool success);
bool isSuccess() const;
QString message() const;
QByteArray message() const;

private:
bool m_success;
QString m_message;
QByteArray m_message;
};
3 changes: 1 addition & 2 deletions src/Url.cpp
Expand Up @@ -7,7 +7,6 @@ Url::Url(WebPage *page, QStringList &arguments, QObject *parent) : Command(page,
void Url::start() {
QUrl humanUrl = page()->currentFrame()->url();
QByteArray encodedBytes = humanUrl.toEncoded();
QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
emit finished(new Response(true, encodedBytes));
}

0 comments on commit 1ef8f4c

Please sign in to comment.