1 /****************************************************************************
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QWEBSOCKET_P_H
43 #define QWEBSOCKET_P_H
48 // This file is not part of the Qt API. It exists purely as an
49 // implementation detail. This header file may change from version to
50 // version without notice, or even be removed.
55 #include <QtCore/QUrl>
56 #include <QtNetwork/QTcpSocket>
57 #include <QtNetwork/QHostAddress>
58 #ifndef QT_NO_NETWORKPROXY
59 #include <QtNetwork/QNetworkProxy>
62 #include <QtNetwork/QSslConfiguration>
63 #include <QtNetwork/QSslError>
64 #include <QtNetwork/QSslSocket>
66 #include <QtCore/QTime>
67 #include <private/qobject_p.h>
69 #include "qwebsocketprotocol.h"
70 #include "qwebsocketdataprocessor_p.h"
71 #include "qdefaultmaskgenerator_p.h"
75 class QWebSocketHandshakeRequest;
76 class QWebSocketHandshakeResponse;
81 struct QWebSocketConfiguration
83 Q_DISABLE_COPY(QWebSocketConfiguration)
86 QWebSocketConfiguration();
90 QSslConfiguration m_sslConfiguration;
91 QList<QSslError> m_ignoredSslErrors;
92 bool m_ignoreSslErrors;
94 #ifndef QT_NONETWORKPROXY
95 QNetworkProxy m_proxy;
97 QTcpSocket *m_pSocket;
100 class QWebSocketPrivate : public QObjectPrivate
102 Q_DISABLE_COPY(QWebSocketPrivate)
105 Q_DECLARE_PUBLIC(QWebSocket)
106 explicit QWebSocketPrivate(const QString &origin,
107 QWebSocketProtocol::Version version,
108 QWebSocket * const pWebSocket);
109 virtual ~QWebSocketPrivate();
113 QAbstractSocket::SocketError error() const;
114 QString errorString() const;
116 bool isValid() const;
117 QHostAddress localAddress() const;
118 quint16 localPort() const;
119 QAbstractSocket::PauseModes pauseMode() const;
120 QHostAddress peerAddress() const;
121 QString peerName() const;
122 quint16 peerPort() const;
123 #ifndef QT_NO_NETWORKPROXY
124 QNetworkProxy proxy() const;
125 void setProxy(const QNetworkProxy &networkProxy);
127 void setMaskGenerator(const QMaskGenerator *maskGenerator);
128 const QMaskGenerator *maskGenerator() const;
129 qint64 readBufferSize() const;
131 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
132 void setReadBufferSize(qint64 size);
133 QAbstractSocket::SocketState state() const;
135 QWebSocketProtocol::Version version() const;
136 QString resourceName() const;
137 QUrl requestUrl() const;
138 QString origin() const;
139 QString protocol() const;
140 QString extension() const;
141 QWebSocketProtocol::CloseCode closeCode() const;
142 QString closeReason() const;
144 qint64 sendTextMessage(const QString &message);
145 qint64 sendBinaryMessage(const QByteArray &data);
148 void ignoreSslErrors(const QList<QSslError> &errors);
149 void ignoreSslErrors();
150 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
151 QSslConfiguration sslConfiguration() const;
154 void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
155 void open(const QUrl &url, bool mask);
156 void ping(const QByteArray &payload);
158 QWebSocket * const q_ptr;
161 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version,
162 QWebSocket *pWebSocket);
163 void setVersion(QWebSocketProtocol::Version version);
164 void setResourceName(const QString &resourceName);
165 void setRequestUrl(const QUrl &requestUrl);
166 void setOrigin(const QString &origin);
167 void setProtocol(const QString &protocol);
168 void setExtension(const QString &extension);
169 void enableMasking(bool enable);
170 void setSocketState(QAbstractSocket::SocketState state);
171 void setErrorString(const QString &errorString);
174 void processPing(const QByteArray &data);
175 void processPong(const QByteArray &data);
176 void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
177 void processHandshake(QTcpSocket *pSocket);
178 void processStateChanged(QAbstractSocket::SocketState socketState);
180 qint64 doWriteFrames(const QByteArray &data, bool isBinary) Q_REQUIRED_RESULT;
182 void makeConnections(const QTcpSocket *pTcpSocket);
183 void releaseConnections(const QTcpSocket *pTcpSocket);
185 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength,
186 quint32 maskingKey, bool lastFrame);
187 QString calculateAcceptKey(const QByteArray &key) const;
188 QString createHandShakeRequest(QString resourceName,
195 static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
196 const QWebSocketHandshakeRequest &request,
197 const QWebSocketHandshakeResponse &response,
198 QObject *parent = Q_NULLPTR) Q_REQUIRED_RESULT;
200 quint32 generateMaskingKey() const;
201 QByteArray generateKey() const;
202 qint64 writeFrames(const QList<QByteArray> &frames) Q_REQUIRED_RESULT;
203 qint64 writeFrame(const QByteArray &frame) Q_REQUIRED_RESULT;
205 QScopedPointer<QTcpSocket> m_pSocket;
206 QString m_errorString;
207 QWebSocketProtocol::Version m_version;
209 QString m_resourceName;
214 QAbstractSocket::SocketState m_socketState;
215 QAbstractSocket::PauseModes m_pauseMode;
216 qint64 m_readBufferSize;
218 QByteArray m_key; //identification key used in handshake requests
220 bool m_mustMask; //a server must not mask the frames it sends
222 bool m_isClosingHandshakeSent;
223 bool m_isClosingHandshakeReceived;
224 QWebSocketProtocol::CloseCode m_closeCode;
225 QString m_closeReason;
229 QWebSocketDataProcessor m_dataProcessor;
230 QWebSocketConfiguration m_configuration;
232 QMaskGenerator *m_pMaskGenerator;
233 QDefaultMaskGenerator m_defaultMaskGenerator;
235 friend class QWebSocketServerPrivate;
240 #endif // QWEBSOCKET_H