summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorØystein Heskestad <oystein.heskestad@qt.io>2023-02-22 18:05:41 +0100
committerØystein Heskestad <oystein.heskestad@qt.io>2023-03-24 14:45:57 +0100
commit99fdf11b808ab9b38239b04399a3fff21fa6ed35 (patch)
tree53184ab9bd7bdbfe39c991faa6fff12c382b93f0
parent12d84ebf5f205498c14e1bfb0174a4982c28337f (diff)
downloadqtwebsockets-99fdf11b808ab9b38239b04399a3fff21fa6ed35.tar.gz
Make endpoint configurable for echoclient and sslechoclient examples
Add handling of command line arguments for hostname and port. Task-number: QTBUG-110894 Pick-to: 6.5 Change-Id: I22d8f9e112cfb6c02b3e741c14720a0f28565984 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
-rw-r--r--examples/websockets/doc/echoclient.qdoc8
-rw-r--r--examples/websockets/doc/sslechoclient.qdoc10
-rw-r--r--examples/websockets/doc/sslechoserver.qdoc9
-rw-r--r--examples/websockets/echoclient/main.cpp28
-rw-r--r--examples/websockets/sslechoclient/main.cpp31
5 files changed, 67 insertions, 19 deletions
diff --git a/examples/websockets/doc/echoclient.qdoc b/examples/websockets/doc/echoclient.qdoc
index f4b3ad3..1f328f0 100644
--- a/examples/websockets/doc/echoclient.qdoc
+++ b/examples/websockets/doc/echoclient.qdoc
@@ -12,10 +12,10 @@
API to send a message to a server and process whatever response the
server returns - in this case, simply reporting the response.
- The client opens a WebSocket connection to a server listening on local
- port 1234. When the connection attempt is successful, the client will
- send a message to the server and print out whatever response the server
- sends. The client then closes the connection.
+ The client by default opens a WebSocket connection to a server listening
+ on local port 1234. When the connection attempt is successful, the client
+ will send a message to the server and print out whatever response the
+ server sends. The client then closes the connection.
\image echoclient-console-example.webp WebSocket Echo Console Client
diff --git a/examples/websockets/doc/sslechoclient.qdoc b/examples/websockets/doc/sslechoclient.qdoc
index 2136d5f..4213722 100644
--- a/examples/websockets/doc/sslechoclient.qdoc
+++ b/examples/websockets/doc/sslechoclient.qdoc
@@ -14,12 +14,10 @@
and when it receives a message back, it prints it out and exits. SSL
support is required for this example to work.
- This example connects to localhost at port 1234 using a secure connection.
- Though SSL errors are ignored in this example, this should never be done
- in production code, because that would leave the client vulnerable to
- man-in-the-middle attacks. To use a certificates not signed by an existing
- CA, add a custom self-signed root CA certificate, trust it in your
- application or system, and sign the server certificate with that.
+ This example connects by default to localhost at port 1234 using a secure
+ connection. It trusts the certificates signed by the CA certificates
+ configured in the SSL backend, and in addition trusts the certificate
+ used by \l {Secure WebSocket Echo Server}.
\image echoclient-console-example.webp Secure WebSocket Echo Console Client
diff --git a/examples/websockets/doc/sslechoserver.qdoc b/examples/websockets/doc/sslechoserver.qdoc
index 6c3b59a..b5b348a 100644
--- a/examples/websockets/doc/sslechoserver.qdoc
+++ b/examples/websockets/doc/sslechoserver.qdoc
@@ -15,11 +15,10 @@
For the sake of illustration, its response is simply a copy of the message
it was sent.
- The server is configured with a self-signed certificate and key.
- Unless the client is configured to ignore certificate verification errors,
- which is not recommenced on production systems, the certificate used by the
- server must be signed by a custom CA certificate that the client explicitly
- trusts.
+ This server is configured with a self-signed certificate and key.
+ Unless the clients contacting this server is configured to trust that
+ certificate, which \l {Secure WebSocket Echo Client} does, they will reject
+ this server.
\image sslechoclient-html-example.webp Secure WebSocket Echo HTML Client
diff --git a/examples/websockets/echoclient/main.cpp b/examples/websockets/echoclient/main.cpp
index 5841610..999df79 100644
--- a/examples/websockets/echoclient/main.cpp
+++ b/examples/websockets/echoclient/main.cpp
@@ -7,19 +7,41 @@
int main(int argc, char *argv[])
{
+ using namespace Qt::Literals::StringLiterals;
QCoreApplication a(argc, argv);
QCommandLineParser parser;
parser.setApplicationDescription("QtWebSockets example: echoclient");
parser.addHelpOption();
- QCommandLineOption dbgOption(QStringList() << "d" << "debug",
+ QCommandLineOption dbgOption(
+ QStringList{ u"d"_s, u"debug"_s },
QCoreApplication::translate("main", "Debug output [default: off]."));
parser.addOption(dbgOption);
+ QCommandLineOption hostnameOption(
+ QStringList{ u"n"_s, u"hostname"_s },
+ QCoreApplication::translate("main", "Hostname [default: localhost]."), "hostname",
+ "localhost");
+ parser.addOption(hostnameOption);
+ QCommandLineOption portOption(QStringList{ u"p"_s, u"port"_s },
+ QCoreApplication::translate("main", "Port [default: 1234]."),
+ "port", "1234");
+ parser.addOption(portOption);
+
parser.process(a);
bool debug = parser.isSet(dbgOption);
-
- EchoClient client(QUrl(QStringLiteral("ws://localhost:1234")), debug);
+ bool ok = true;
+ int port = parser.value(portOption).toInt(&ok);
+ if (!ok || port < 1 || port > 65535) {
+ qWarning("Port invalid, must be a number between 1 and 65535\n%s",
+ qPrintable(parser.helpText()));
+ return 1;
+ }
+ QUrl url;
+ url.setScheme(u"ws"_s);
+ url.setHost(parser.value(hostnameOption));
+ url.setPort(port);
+ EchoClient client(url, debug);
QObject::connect(&client, &EchoClient::closed, &a, &QCoreApplication::quit);
return a.exec();
diff --git a/examples/websockets/sslechoclient/main.cpp b/examples/websockets/sslechoclient/main.cpp
index d70f1a5..2d65ed6 100644
--- a/examples/websockets/sslechoclient/main.cpp
+++ b/examples/websockets/sslechoclient/main.cpp
@@ -1,13 +1,42 @@
// Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtCore/QCoreApplication>
+#include <QtCore/QCommandLineParser>
+#include <QtCore/QCommandLineOption>
#include "sslechoclient.h"
int main(int argc, char *argv[])
{
+ using namespace Qt::Literals::StringLiterals;
QCoreApplication a(argc, argv);
- SslEchoClient client(QUrl(QStringLiteral("wss://localhost:1234")));
+ QCommandLineParser parser;
+ parser.setApplicationDescription("QtWebSockets example: sslechoclient");
+ parser.addHelpOption();
+
+ QCommandLineOption hostnameOption(
+ QStringList{ u"n"_s, u"hostname"_s },
+ QCoreApplication::translate("main", "Hostname [default: localhost]."), "hostname",
+ "localhost");
+ parser.addOption(hostnameOption);
+ QCommandLineOption portOption(QStringList{ u"p"_s, u"port"_s },
+ QCoreApplication::translate("main", "Port [default: 1234]."),
+ "port", "1234");
+ parser.addOption(portOption);
+
+ parser.process(a);
+ bool ok = true;
+ int port = parser.value(portOption).toInt(&ok);
+ if (!ok || port < 1 || port > 65535) {
+ qWarning("Port invalid, must be a number between 1 and 65535\n%s",
+ qPrintable(parser.helpText()));
+ return 1;
+ }
+ QUrl url;
+ url.setScheme(u"wss"_s);
+ url.setHost(parser.value(hostnameOption));
+ url.setPort(port);
+ SslEchoClient client(url);
Q_UNUSED(client);
return a.exec();