1/****************************************************************************
2**
3** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWebSockets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QWEBSOCKET_P_H
41#define QWEBSOCKET_P_H
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <QtCore/QUrl>
54#include <QtNetwork/QTcpSocket>
55#include <QtNetwork/QHostAddress>
56#ifndef QT_NO_NETWORKPROXY
57#include <QtNetwork/QNetworkProxy>
58#endif
59#ifndef QT_NO_SSL
60#include <QtNetwork/QSslConfiguration>
61#include <QtNetwork/QSslError>
62#include <QtNetwork/QSslSocket>
63#endif
64#include <QtCore/QElapsedTimer>
65#include <private/qobject_p.h>
66
67#include "qwebsocket.h"
68#include "qwebsocketprotocol.h"
69#include "qwebsocketdataprocessor_p.h"
70#include "qdefaultmaskgenerator_p.h"
71
72#ifdef Q_OS_WASM
73#include <emscripten/val.h>
74#endif
75
76QT_BEGIN_NAMESPACE
77
78class QWebSocketHandshakeRequest;
79class QWebSocketHandshakeResponse;
80class QTcpSocket;
81class QWebSocket;
82class QMaskGenerator;
83
84struct QWebSocketConfiguration
85{
86 Q_DISABLE_COPY(QWebSocketConfiguration)
87
88public:
89 QWebSocketConfiguration();
90
91public:
92#ifndef QT_NO_SSL
93 struct TlsConfigurationLazy {
94 TlsConfigurationLazy &operator = (const QSslConfiguration &rhs)
95 {
96 tlsConfiguration.reset(p: new QSslConfiguration(rhs));
97 return *this;
98 }
99
100 operator QSslConfiguration() const
101 {
102 if (!tlsConfiguration.get())
103 tlsConfiguration.reset(p: new QSslConfiguration(QSslConfiguration::defaultConfiguration()));
104 return *tlsConfiguration.get();
105 }
106
107 mutable std::unique_ptr<QSslConfiguration> tlsConfiguration;
108 };
109
110 TlsConfigurationLazy m_sslConfiguration;
111 QList<QSslError> m_ignoredSslErrors;
112 bool m_ignoreSslErrors;
113#endif
114#ifndef QT_NO_NETWORKPROXY
115 QNetworkProxy m_proxy;
116#endif
117 QTcpSocket *m_pSocket;
118};
119
120class QWebSocketPrivate : public QObjectPrivate
121{
122 Q_DISABLE_COPY(QWebSocketPrivate)
123
124public:
125 Q_DECLARE_PUBLIC(QWebSocket)
126 explicit QWebSocketPrivate(const QString &origin,
127 QWebSocketProtocol::Version version);
128 ~QWebSocketPrivate() override;
129
130 void init();
131 void abort();
132 QAbstractSocket::SocketError error() const;
133 QString errorString() const;
134 bool flush();
135 bool isValid() const;
136 QHostAddress localAddress() const;
137 quint16 localPort() const;
138 QAbstractSocket::PauseModes pauseMode() const;
139 QHostAddress peerAddress() const;
140 QString peerName() const;
141 quint16 peerPort() const;
142#ifndef QT_NO_NETWORKPROXY
143 QNetworkProxy proxy() const;
144 void setProxy(const QNetworkProxy &networkProxy);
145#endif
146 void setMaskGenerator(const QMaskGenerator *maskGenerator);
147 const QMaskGenerator *maskGenerator() const;
148 qint64 readBufferSize() const;
149 void resume();
150 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
151 void setReadBufferSize(qint64 size);
152 QAbstractSocket::SocketState state() const;
153
154 QWebSocketProtocol::Version version() const;
155 QString resourceName() const;
156 QNetworkRequest request() const;
157 QString origin() const;
158 QString protocol() const;
159 QString extension() const;
160 QWebSocketProtocol::CloseCode closeCode() const;
161 QString closeReason() const;
162
163 qint64 sendTextMessage(const QString &message);
164 qint64 sendBinaryMessage(const QByteArray &data);
165
166#ifndef QT_NO_SSL
167 void ignoreSslErrors(const QList<QSslError> &errors);
168 void ignoreSslErrors();
169 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
170 QSslConfiguration sslConfiguration() const;
171 void _q_updateSslConfiguration();
172#endif
173
174 void closeGoingAway();
175 void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
176 void open(const QNetworkRequest &request, bool mask);
177 void ping(const QByteArray &payload);
178 void setSocketState(QAbstractSocket::SocketState state);
179
180 void setMaxAllowedIncomingFrameSize(quint64 maxAllowedIncomingFrameSize);
181 quint64 maxAllowedIncomingFrameSize() const;
182 void setMaxAllowedIncomingMessageSize(quint64 maxAllowedIncomingMessageSize);
183 quint64 maxAllowedIncomingMessageSize() const;
184 static quint64 maxIncomingMessageSize();
185 static quint64 maxIncomingFrameSize();
186
187 void setOutgoingFrameSize(quint64 outgoingFrameSize);
188 quint64 outgoingFrameSize() const;
189 static quint64 maxOutgoingFrameSize();
190
191private:
192 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version);
193 void setVersion(QWebSocketProtocol::Version version);
194 void setResourceName(const QString &resourceName);
195 void setRequest(const QNetworkRequest &request);
196 void setOrigin(const QString &origin);
197 void setProtocol(const QString &protocol);
198 void setExtension(const QString &extension);
199 void enableMasking(bool enable);
200 void setErrorString(const QString &errorString);
201
202 void socketDestroyed(QObject *socket);
203
204 void processData();
205 void processPing(const QByteArray &data);
206 void processPong(const QByteArray &data);
207 void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
208 void processHandshake(QTcpSocket *pSocket);
209 void processStateChanged(QAbstractSocket::SocketState socketState);
210
211 Q_REQUIRED_RESULT qint64 doWriteFrames(const QByteArray &data, bool isBinary);
212
213 void makeConnections(QTcpSocket *pTcpSocket);
214 void releaseConnections(const QTcpSocket *pTcpSocket);
215
216 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength,
217 quint32 maskingKey, bool lastFrame);
218 QString calculateAcceptKey(const QByteArray &key) const;
219 QString createHandShakeRequest(QString resourceName,
220 QString host,
221 QString origin,
222 QString extensions,
223 QString protocols,
224 QByteArray key,
225 const QList<QPair<QString, QString> > &headers);
226
227 Q_REQUIRED_RESULT static QWebSocket *
228 upgradeFrom(QTcpSocket *tcpSocket,
229 const QWebSocketHandshakeRequest &request,
230 const QWebSocketHandshakeResponse &response,
231 QObject *parent = nullptr);
232
233 quint32 generateMaskingKey() const;
234 QByteArray generateKey() const;
235 Q_REQUIRED_RESULT qint64 writeFrames(const QList<QByteArray> &frames);
236 Q_REQUIRED_RESULT qint64 writeFrame(const QByteArray &frame);
237
238 QTcpSocket *m_pSocket;
239 QString m_errorString;
240 QWebSocketProtocol::Version m_version;
241 QUrl m_resource;
242 QString m_resourceName;
243 QNetworkRequest m_request;
244 QString m_origin;
245 QString m_protocol;
246 QString m_extension;
247 QAbstractSocket::SocketState m_socketState;
248 QAbstractSocket::PauseModes m_pauseMode;
249 qint64 m_readBufferSize;
250
251 QByteArray m_key; //identification key used in handshake requests
252
253 bool m_mustMask; //a server must not mask the frames it sends
254
255 bool m_isClosingHandshakeSent;
256 bool m_isClosingHandshakeReceived;
257 QWebSocketProtocol::CloseCode m_closeCode;
258 QString m_closeReason;
259
260 QElapsedTimer m_pingTimer;
261
262 QWebSocketDataProcessor *m_dataProcessor = new QWebSocketDataProcessor();
263 QWebSocketConfiguration m_configuration;
264
265 QMaskGenerator *m_pMaskGenerator;
266 QDefaultMaskGenerator m_defaultMaskGenerator;
267
268 enum HandshakeState {
269 NothingDoneState,
270 ReadingStatusState,
271 ReadingHeaderState,
272 ParsingHeaderState,
273 AllDoneState
274 } m_handshakeState;
275 QByteArray m_statusLine;
276 int m_httpStatusCode;
277 int m_httpMajorVersion, m_httpMinorVersion;
278 QString m_httpStatusMessage;
279 QMultiMap<QString, QString> m_headers;
280
281 quint64 m_outgoingFrameSize;
282
283 friend class QWebSocketServerPrivate;
284#ifdef Q_OS_WASM
285 emscripten::val socketContext = emscripten::val::null();
286#endif
287};
288
289QT_END_NAMESPACE
290
291#endif // QWEBSOCKET_H
292

source code of qtwebsockets/src/websockets/qwebsocket_p.h