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/QTime>
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 QSslConfiguration m_sslConfiguration;
94 QList<QSslError> m_ignoredSslErrors;
95 bool m_ignoreSslErrors;
96#endif
97#ifndef QT_NO_NETWORKPROXY
98 QNetworkProxy m_proxy;
99#endif
100 QTcpSocket *m_pSocket;
101};
102
103class QWebSocketPrivate : public QObjectPrivate
104{
105 Q_DISABLE_COPY(QWebSocketPrivate)
106
107public:
108 Q_DECLARE_PUBLIC(QWebSocket)
109 explicit QWebSocketPrivate(const QString &origin,
110 QWebSocketProtocol::Version version);
111 ~QWebSocketPrivate() override;
112
113 void init();
114 void abort();
115 QAbstractSocket::SocketError error() const;
116 QString errorString() const;
117 bool flush();
118 bool isValid() const;
119 QHostAddress localAddress() const;
120 quint16 localPort() const;
121 QAbstractSocket::PauseModes pauseMode() const;
122 QHostAddress peerAddress() const;
123 QString peerName() const;
124 quint16 peerPort() const;
125#ifndef QT_NO_NETWORKPROXY
126 QNetworkProxy proxy() const;
127 void setProxy(const QNetworkProxy &networkProxy);
128#endif
129 void setMaskGenerator(const QMaskGenerator *maskGenerator);
130 const QMaskGenerator *maskGenerator() const;
131 qint64 readBufferSize() const;
132 void resume();
133 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
134 void setReadBufferSize(qint64 size);
135 QAbstractSocket::SocketState state() const;
136
137 QWebSocketProtocol::Version version() const;
138 QString resourceName() const;
139 QNetworkRequest request() const;
140 QString origin() const;
141 QString protocol() const;
142 QString extension() const;
143 QWebSocketProtocol::CloseCode closeCode() const;
144 QString closeReason() const;
145
146 qint64 sendTextMessage(const QString &message);
147 qint64 sendBinaryMessage(const QByteArray &data);
148
149#ifndef QT_NO_SSL
150 void ignoreSslErrors(const QList<QSslError> &errors);
151 void ignoreSslErrors();
152 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
153 QSslConfiguration sslConfiguration() const;
154 void _q_updateSslConfiguration();
155#endif
156
157 void closeGoingAway();
158 void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
159 void open(const QNetworkRequest &request, bool mask);
160 void ping(const QByteArray &payload);
161 void setSocketState(QAbstractSocket::SocketState state);
162
163private:
164 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version);
165 void setVersion(QWebSocketProtocol::Version version);
166 void setResourceName(const QString &resourceName);
167 void setRequest(const QNetworkRequest &request);
168 void setOrigin(const QString &origin);
169 void setProtocol(const QString &protocol);
170 void setExtension(const QString &extension);
171 void enableMasking(bool enable);
172 void setErrorString(const QString &errorString);
173
174 void socketDestroyed(QObject *socket);
175
176 void processData();
177 void processPing(const QByteArray &data);
178 void processPong(const QByteArray &data);
179 void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
180 void processHandshake(QTcpSocket *pSocket);
181 void processStateChanged(QAbstractSocket::SocketState socketState);
182
183 Q_REQUIRED_RESULT qint64 doWriteFrames(const QByteArray &data, bool isBinary);
184
185 void makeConnections(const QTcpSocket *pTcpSocket);
186 void releaseConnections(const QTcpSocket *pTcpSocket);
187
188 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength,
189 quint32 maskingKey, bool lastFrame);
190 QString calculateAcceptKey(const QByteArray &key) const;
191 QString createHandShakeRequest(QString resourceName,
192 QString host,
193 QString origin,
194 QString extensions,
195 QString protocols,
196 QByteArray key,
197 const QList<QPair<QString, QString> > &headers);
198
199 Q_REQUIRED_RESULT static QWebSocket *
200 upgradeFrom(QTcpSocket *tcpSocket,
201 const QWebSocketHandshakeRequest &request,
202 const QWebSocketHandshakeResponse &response,
203 QObject *parent = nullptr);
204
205 quint32 generateMaskingKey() const;
206 QByteArray generateKey() const;
207 Q_REQUIRED_RESULT qint64 writeFrames(const QList<QByteArray> &frames);
208 Q_REQUIRED_RESULT qint64 writeFrame(const QByteArray &frame);
209
210 QTcpSocket *m_pSocket;
211 QString m_errorString;
212 QWebSocketProtocol::Version m_version;
213 QUrl m_resource;
214 QString m_resourceName;
215 QNetworkRequest m_request;
216 QString m_origin;
217 QString m_protocol;
218 QString m_extension;
219 QAbstractSocket::SocketState m_socketState;
220 QAbstractSocket::PauseModes m_pauseMode;
221 qint64 m_readBufferSize;
222
223 QByteArray m_key; //identification key used in handshake requests
224
225 bool m_mustMask; //a server must not mask the frames it sends
226
227 bool m_isClosingHandshakeSent;
228 bool m_isClosingHandshakeReceived;
229 QWebSocketProtocol::CloseCode m_closeCode;
230 QString m_closeReason;
231
232 QTime m_pingTimer;
233
234 QWebSocketDataProcessor m_dataProcessor;
235 QWebSocketConfiguration m_configuration;
236
237 QMaskGenerator *m_pMaskGenerator;
238 QDefaultMaskGenerator m_defaultMaskGenerator;
239
240 enum HandshakeState {
241 NothingDoneState,
242 ReadingStatusState,
243 ReadingHeaderState,
244 ParsingHeaderState,
245 AllDoneState
246 } m_handshakeState;
247 QByteArray m_statusLine;
248 int m_httpStatusCode;
249 int m_httpMajorVersion, m_httpMinorVersion;
250 QString m_httpStatusMessage;
251 QMap<QString, QString> m_headers;
252
253 friend class QWebSocketServerPrivate;
254#ifdef Q_OS_WASM
255 emscripten::val socketContext = emscripten::val::null();
256#endif
257};
258
259QT_END_NAMESPACE
260
261#endif // QWEBSOCKET_H
262