summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederik Gladhorn <frederik.gladhorn@theqtcompany.com>2015-02-20 15:38:55 +0100
committerFrederik Gladhorn <frederik.gladhorn@theqtcompany.com>2015-02-20 15:38:55 +0100
commitb797778da01f72e64cda8680f29f3fa8434ffb0a (patch)
treea917f080b40188e8f937a50b29e47898e9709a51
parenta834ce5889dc3a79eebe1ac0a1576603b68f5a4b (diff)
parent85a8ea105646c7d871f982b890ef5f6faa91824d (diff)
downloadqtwebsockets-b797778da01f72e64cda8680f29f3fa8434ffb0a.tar.gz
Merge remote-tracking branch 'origin/5.4' into 5.5
Change-Id: I96657102802860c8490c162462324e661592d879
-rw-r--r--examples/websockets/doc/echoserver.qdoc9
-rw-r--r--examples/websockets/doc/images/echoclient-html-example.pngbin0 -> 47215 bytes
-rw-r--r--examples/websockets/echoclient/echoclient.cpp13
-rw-r--r--examples/websockets/echoclient/echoclient.h3
-rw-r--r--examples/websockets/echoclient/main.cpp15
-rw-r--r--src/websockets/doc/qtwebsockets.qdocconf6
-rw-r--r--src/websockets/qwebsockethandshakerequest.cpp18
-rw-r--r--tests/auto/handshakerequest/tst_handshakerequest.cpp22
8 files changed, 75 insertions, 11 deletions
diff --git a/examples/websockets/doc/echoserver.qdoc b/examples/websockets/doc/echoserver.qdoc
index e6d4f2c..ae6218a 100644
--- a/examples/websockets/doc/echoserver.qdoc
+++ b/examples/websockets/doc/echoserver.qdoc
@@ -39,7 +39,14 @@
sends back the messages it receives.
The Echo Server Example shows how to create a simple server application that
- sends back the messages it receives, using the WebSocket API.
+ sends back the messages it receives, using the \l {Qt WebSockets}{WebSocket} API.
+
+ If your web browser supports \l {Qt WebSockets}{WebSocket}, you can also use it
+ to open the \l {echoserver/echoclient.html}{echoclient.html} file, and operate
+ like the following screenshot.
+
+ \image echoclient-html-example.png Screenshot of the HTML version of Echo
+ Client example
\sa {Echo Client Example}
*/
diff --git a/examples/websockets/doc/images/echoclient-html-example.png b/examples/websockets/doc/images/echoclient-html-example.png
new file mode 100644
index 0000000..2cda8fd
--- /dev/null
+++ b/examples/websockets/doc/images/echoclient-html-example.png
Binary files differ
diff --git a/examples/websockets/echoclient/echoclient.cpp b/examples/websockets/echoclient/echoclient.cpp
index 229ee1b..0e7b3c9 100644
--- a/examples/websockets/echoclient/echoclient.cpp
+++ b/examples/websockets/echoclient/echoclient.cpp
@@ -36,10 +36,13 @@
QT_USE_NAMESPACE
//! [constructor]
-EchoClient::EchoClient(const QUrl &url, QObject *parent) :
+EchoClient::EchoClient(const QUrl &url, bool debug, QObject *parent) :
QObject(parent),
- m_url(url)
+ m_url(url),
+ m_debug(debug)
{
+ if (m_debug)
+ qDebug() << "WebSocket server:" << url;
connect(&m_webSocket, &QWebSocket::connected, this, &EchoClient::onConnected);
connect(&m_webSocket, &QWebSocket::disconnected, this, &EchoClient::closed);
m_webSocket.open(QUrl(url));
@@ -49,7 +52,8 @@ EchoClient::EchoClient(const QUrl &url, QObject *parent) :
//! [onConnected]
void EchoClient::onConnected()
{
- qDebug() << "WebSocket connected";
+ if (m_debug)
+ qDebug() << "WebSocket connected";
connect(&m_webSocket, &QWebSocket::textMessageReceived,
this, &EchoClient::onTextMessageReceived);
m_webSocket.sendTextMessage(QStringLiteral("Hello, world!"));
@@ -59,7 +63,8 @@ void EchoClient::onConnected()
//! [onTextMessageReceived]
void EchoClient::onTextMessageReceived(QString message)
{
- qDebug() << "Message received:" << message;
+ if (m_debug)
+ qDebug() << "Message received:" << message;
m_webSocket.close();
}
//! [onTextMessageReceived]
diff --git a/examples/websockets/echoclient/echoclient.h b/examples/websockets/echoclient/echoclient.h
index 2ddef4c..dc005dc 100644
--- a/examples/websockets/echoclient/echoclient.h
+++ b/examples/websockets/echoclient/echoclient.h
@@ -40,7 +40,7 @@ class EchoClient : public QObject
{
Q_OBJECT
public:
- explicit EchoClient(const QUrl &url, QObject *parent = Q_NULLPTR);
+ explicit EchoClient(const QUrl &url, bool debug = false, QObject *parent = Q_NULLPTR);
Q_SIGNALS:
void closed();
@@ -52,6 +52,7 @@ private Q_SLOTS:
private:
QWebSocket m_webSocket;
QUrl m_url;
+ bool m_debug;
};
#endif // ECHOCLIENT_H
diff --git a/examples/websockets/echoclient/main.cpp b/examples/websockets/echoclient/main.cpp
index cdc6d04..911c069 100644
--- a/examples/websockets/echoclient/main.cpp
+++ b/examples/websockets/echoclient/main.cpp
@@ -31,12 +31,25 @@
**
****************************************************************************/
#include <QtCore/QCoreApplication>
+#include <QtCore/QCommandLineParser>
+#include <QtCore/QCommandLineOption>
#include "echoclient.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
- EchoClient client(QUrl(QStringLiteral("ws://localhost:1234")));
+
+ QCommandLineParser parser;
+ parser.setApplicationDescription("QtWebSockets example: echoclient");
+ parser.addHelpOption();
+
+ QCommandLineOption dbgOption(QStringList() << "d" << "debug",
+ QCoreApplication::translate("main", "Debug output [default: off]."));
+ parser.addOption(dbgOption);
+ parser.process(a);
+ bool debug = parser.isSet(dbgOption);
+
+ EchoClient client(QUrl(QStringLiteral("ws://localhost:1234")), debug);
QObject::connect(&client, &EchoClient::closed, &a, &QCoreApplication::quit);
return a.exec();
diff --git a/src/websockets/doc/qtwebsockets.qdocconf b/src/websockets/doc/qtwebsockets.qdocconf
index 3f87cef..2e1e15b 100644
--- a/src/websockets/doc/qtwebsockets.qdocconf
+++ b/src/websockets/doc/qtwebsockets.qdocconf
@@ -37,7 +37,7 @@ qhp.QtWebSockets.subprojects.examples.sortPages = true
tagfile = ../../../doc/qtwebsockets/qtwebsockets.tags
-depends += qtcore qtnetwork qtdoc
+depends += qtcore qtnetwork qtdoc qmake
headerdirs += .. \
../../imports
@@ -50,6 +50,10 @@ sourcedirs += .. \
exampledirs += ../../../examples/websockets \
snippets
+examples.fileextensions += "*.html"
+
+imagedirs += ../../../examples/websockets/doc/images
+
manifestmeta.thumbnail.names += "QtWebSockets/*"
navigation.landingpage = "Qt WebSockets"
diff --git a/src/websockets/qwebsockethandshakerequest.cpp b/src/websockets/qwebsockethandshakerequest.cpp
index 2476a81..6c8c9cf 100644
--- a/src/websockets/qwebsockethandshakerequest.cpp
+++ b/src/websockets/qwebsockethandshakerequest.cpp
@@ -222,10 +222,22 @@ void QWebSocketHandshakeRequest::readHandshake(QTextStream &textStream)
headerLine = textStream.readLine();
}
- const QString host = m_headers.value(QStringLiteral("host"), QString());
m_requestUrl = QUrl::fromEncoded(resourceName.toLatin1());
- if (m_requestUrl.isRelative())
+ QString host = m_headers.value(QStringLiteral("host"), QString());
+ if (m_requestUrl.isRelative()) {
+ // see http://tools.ietf.org/html/rfc6455#page-17
+ // No. 4 item in "The requirements for this handshake"
+ int idx = host.indexOf(":");
+ bool ok = false;
+ int port;
+ if (idx != -1) {
+ port = host.rightRef(host.length() - idx - 1).toInt(&ok);
+ host.truncate(idx);
+ }
m_requestUrl.setHost(host);
+ if (ok)
+ m_requestUrl.setPort(port);
+ }
if (m_requestUrl.scheme().isEmpty()) {
const QString scheme = isSecure() ? QStringLiteral("wss") : QStringLiteral("ws");
m_requestUrl.setScheme(scheme);
@@ -260,7 +272,7 @@ void QWebSocketHandshakeRequest::readHandshake(QTextStream &textStream)
connectionValues << (*c).trimmed();
//optional headers
- m_origin = m_headers.value(QStringLiteral("sec-websocket-origin"), QString());
+ m_origin = m_headers.value(QStringLiteral("origin"), QString());
const QStringList protocolLines = m_headers.values(QStringLiteral("sec-websocket-protocol"));
for (QStringList::const_iterator pl = protocolLines.begin(); pl != protocolLines.end(); ++pl) {
QStringList protocols = (*pl).split(QStringLiteral(","), QString::SkipEmptyParts);
diff --git a/tests/auto/handshakerequest/tst_handshakerequest.cpp b/tests/auto/handshakerequest/tst_handshakerequest.cpp
index 3e6ec40..619a477 100644
--- a/tests/auto/handshakerequest/tst_handshakerequest.cpp
+++ b/tests/auto/handshakerequest/tst_handshakerequest.cpp
@@ -65,6 +65,8 @@ private Q_SLOTS:
void tst_multipleValuesInConnectionHeader();
void tst_multipleVersions();
+
+ void tst_qtbug_39355();
};
tst_HandshakeRequest::tst_HandshakeRequest()
@@ -290,6 +292,26 @@ void tst_HandshakeRequest::tst_multipleVersions()
QCOMPARE(request.versions().at(0), QWebSocketProtocol::Version13);
}
+void tst_HandshakeRequest::tst_qtbug_39355()
+{
+ QString header = QStringLiteral("GET /ABC/DEF/ HTTP/1.1\r\nHost: localhost:1234\r\n") +
+ QStringLiteral("Sec-WebSocket-Version: 13\r\n") +
+ QStringLiteral("Sec-WebSocket-Key: 2Wg20829/4ziWlmsUAD8Dg==\r\n") +
+ QStringLiteral("Upgrade: websocket\r\n") +
+ QStringLiteral("Connection: Upgrade\r\n\r\n");
+ QByteArray data;
+ QTextStream textStream(&data);
+ QWebSocketHandshakeRequest request(8080, false);
+
+ textStream << header;
+ textStream.seek(0);
+ request.readHandshake(textStream);
+
+ QVERIFY(request.isValid());
+ QCOMPARE(request.port(), 1234);
+ QCOMPARE(request.host(), QStringLiteral("localhost"));
+}
+
QTEST_MAIN(tst_HandshakeRequest)
#include "tst_handshakerequest.moc"