From efe2251d0547627ef1877193dc788fc2ab2af939 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Mon, 21 Jan 2013 19:45:04 +0100 Subject: Also add content-length to QWebChannelResponder to make it stabler. Still, one can trigger XMLHttpRequest errors occasionally e.g.: POST http://localhost:49158/6e933d76-843e-4b01-b273-fc36b0480ab1/POLL Unknown error This will then render the whole QObject bridge useless - I will investigate. Change-Id: Ifb7343414dac82e9af0a30ac008c3940283b1ecd Reviewed-by: Pierre Rossi --- src/qwebchannel.cpp | 18 ++++++++++++++---- src/qwebchannel.h | 11 +++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/qwebchannel.cpp b/src/qwebchannel.cpp index a4c1a49..3b656c1 100644 --- a/src/qwebchannel.cpp +++ b/src/qwebchannel.cpp @@ -70,10 +70,19 @@ void QWebChannelResponder::retain() void QWebChannelResponder::noop() { - send(""); + open(0); + close(); } -void QWebChannelResponder::open() +void QWebChannelResponder::send(const QString& stringData) +{ + const QByteArray data = stringData.toUtf8(); + open(data.length()); + write(data); + close(); +} + +void QWebChannelResponder::open(uint contentLength) { if (!socket || !socket->isOpen()) { qWarning() << "cannot open response - socket is not open anymore"; @@ -84,16 +93,17 @@ void QWebChannelResponder::open() socket->write("HTTP/1.1 200 OK\r\n" "Content-Type: text/json\r\n" + "Content-Length: " + QByteArray::number(contentLength) + "\r\n" "\r\n"); } -void QWebChannelResponder::write(const QString& data) +void QWebChannelResponder::write(const QByteArray& data) { if (!socket || !socket->isOpen()) { qWarning() << "cannot write response - socket is not open anymore"; return; } - socket->write(data.toUtf8()); + socket->write(data); } void QWebChannelResponder::close() diff --git a/src/qwebchannel.h b/src/qwebchannel.h index b564787..12834a8 100644 --- a/src/qwebchannel.h +++ b/src/qwebchannel.h @@ -55,17 +55,12 @@ public: QWebChannelResponder(QTcpSocket* s); public slots: - void open(); - void write(const QString& data); + void open(uint contentLength); + void write(const QByteArray& data); void close(); void retain(); void noop(); - void send(const QString& data) - { - open(); - write(data); - close(); - } + void send(const QString& stringData); private slots: void closeIfNotRetained(); -- cgit v1.2.1