summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorn Potter <lorn.potter@gmail.com>2022-08-04 15:09:03 +1000
committerLorn Potter <lorn.potter@gmail.com>2022-08-18 13:26:20 +1000
commiteb28a1e557730b8cf8b8c531b6d0006a90ec017e (patch)
tree6db011c47fb39cb44d26894c7a00414287145ef9
parent9d6d8aab27d3bdbab721b67bb8d6ab4b87aa35f1 (diff)
downloadqtwebsockets-eb28a1e557730b8cf8b8c531b6d0006a90ec017e.tar.gz
wasm: improve close code message
The close reason from emscripten was usually empty, so provide one. Change-Id: I0c0617c805cc00aa9a575f05c8319560e33569d2 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> (cherry picked from commit d46a70113fed1483bea3c44f5e39d64930cfff07)
-rw-r--r--src/websockets/qwebsocket_p.h1
-rw-r--r--src/websockets/qwebsocket_wasm_p.cpp35
2 files changed, 33 insertions, 3 deletions
diff --git a/src/websockets/qwebsocket_p.h b/src/websockets/qwebsocket_p.h
index 20a051c..0579b3d 100644
--- a/src/websockets/qwebsocket_p.h
+++ b/src/websockets/qwebsocket_p.h
@@ -139,6 +139,7 @@ public:
static quint64 maxOutgoingFrameSize();
#ifdef Q_OS_WASM
void setSocketClosed(const EmscriptenWebSocketCloseEvent *emCloseEvent);
+ QString closeCodeToString(QWebSocketProtocol::CloseCode code);
#endif
private:
QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version);
diff --git a/src/websockets/qwebsocket_wasm_p.cpp b/src/websockets/qwebsocket_wasm_p.cpp
index 8176f93..7b142cf 100644
--- a/src/websockets/qwebsocket_wasm_p.cpp
+++ b/src/websockets/qwebsocket_wasm_p.cpp
@@ -144,12 +144,18 @@ void QWebSocketPrivate::open(const QNetworkRequest &request,
bool isSecureContext = (navProtocol.as<std::string>().find("https") == 0);
if (!url.isValid()
- || url.toString().contains(QStringLiteral("\r\n"))
- || (isSecureContext && url.scheme() == QStringLiteral("ws"))) {
+ || url.toString().contains(QStringLiteral("\r\n"))) {
setErrorString(QWebSocket::tr("Connection refused"));
Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
return;
}
+ if (isSecureContext && url.scheme() == QStringLiteral("ws")) {
+ const QString message =
+ QWebSocket::tr("Unsupported WebSocket scheme: %1").arg(url.scheme());
+ setErrorString(message);
+ emit q->error(QAbstractSocket::UnsupportedSocketOperationError);
+ return;
+ }
EmscriptenWebSocketCreateAttributes attr;
@@ -226,8 +232,11 @@ void QWebSocketPrivate::setSocketClosed(const EmscriptenWebSocketCloseEvent *emC
{
Q_Q(QWebSocket);
m_closeCode = (QWebSocketProtocol::CloseCode)emCloseEvent->code;
+
+ m_closeReason = QString::fromUtf8(emCloseEvent->reason);
+
if (m_closeReason.isEmpty()) {
- m_closeReason = QString::fromUtf8(emCloseEvent->reason);
+ m_closeReason = closeCodeToString(m_closeCode);
}
if (m_socketState == QAbstractSocket::ConnectedState) {
@@ -248,3 +257,23 @@ void QWebSocketPrivate::setSocketClosed(const EmscriptenWebSocketCloseEvent *emC
m_socketContext = 0;
}
}
+
+QString QWebSocketPrivate::closeCodeToString(QWebSocketProtocol::CloseCode code)
+{
+ switch (code) {
+ case QWebSocketProtocol::CloseCodeNormal: return QStringLiteral("Normal closure");
+ case QWebSocketProtocol::CloseCodeGoingAway: return QStringLiteral("Going away");
+ case QWebSocketProtocol::CloseCodeProtocolError: return QStringLiteral("Protocol error");
+ case QWebSocketProtocol::CloseCodeDatatypeNotSupported: return QStringLiteral("Unsupported data");
+ case QWebSocketProtocol::CloseCodeReserved1004: return QStringLiteral("Reserved");
+ case QWebSocketProtocol::CloseCodeMissingStatusCode: return QStringLiteral("No status received");
+ case QWebSocketProtocol::CloseCodeAbnormalDisconnection: return QStringLiteral("Abnormal closure");
+ case QWebSocketProtocol::CloseCodeWrongDatatype: return QStringLiteral("Invalid frame payload data");
+ case QWebSocketProtocol::CloseCodePolicyViolated: return QStringLiteral("Policy violation");
+ case QWebSocketProtocol::CloseCodeTooMuchData: return QStringLiteral("Message too big");
+ case QWebSocketProtocol::CloseCodeMissingExtension: return QStringLiteral("Mandatory extension missing");
+ case QWebSocketProtocol::CloseCodeBadOperation: return QStringLiteral("Internal server error");
+ case QWebSocketProtocol::CloseCodeTlsHandshakeFailed: return QStringLiteral("TLS handshake failed");
+ };
+ return QStringLiteral("");
+}