diff options
author | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-02-24 12:21:34 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2015-02-24 12:21:34 +0000 |
commit | b01068a406f6ebf51e103dd541f36fd6ef144805 (patch) | |
tree | 9a1d2d634ce28672a72dfd1c7565f95cc82c2cb4 | |
parent | 9989055693ac6225ae3e6f8153862765b0cd5e7e (diff) | |
parent | b797778da01f72e64cda8680f29f3fa8434ffb0a (diff) | |
download | qtwebsockets-b01068a406f6ebf51e103dd541f36fd6ef144805.tar.gz |
Merge "Merge remote-tracking branch 'origin/5.4' into 5.5" into refs/staging/5.5
-rw-r--r-- | examples/websockets/doc/echoserver.qdoc | 9 | ||||
-rw-r--r-- | examples/websockets/doc/images/echoclient-html-example.png | bin | 0 -> 47215 bytes | |||
-rw-r--r-- | examples/websockets/echoclient/echoclient.cpp | 13 | ||||
-rw-r--r-- | examples/websockets/echoclient/echoclient.h | 3 | ||||
-rw-r--r-- | examples/websockets/echoclient/main.cpp | 15 | ||||
-rw-r--r-- | src/websockets/doc/qtwebsockets.qdocconf | 6 | ||||
-rw-r--r-- | src/websockets/qwebsockethandshakerequest.cpp | 18 | ||||
-rw-r--r-- | tests/auto/handshakerequest/tst_handshakerequest.cpp | 22 |
8 files changed, 75 insertions, 11 deletions
diff --git a/examples/websockets/doc/echoserver.qdoc b/examples/websockets/doc/echoserver.qdoc index 1696589..e547e2a 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 Binary files differnew file mode 100644 index 0000000..2cda8fd --- /dev/null +++ b/examples/websockets/doc/images/echoclient-html-example.png diff --git a/examples/websockets/echoclient/echoclient.cpp b/examples/websockets/echoclient/echoclient.cpp index daccbfb..c666c6e 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 757c56d..165087b 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 91a2535..36157de 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 47586a7..436ac93 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 aa203f3..9c579bd 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" |