1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
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>
68 #include "qwebsocketprotocol.h"
69 #include "qwebsocketdataprocessor_p.h"
73 class QWebSocketHandshakeRequest;
74 class QWebSocketHandshakeResponse;
78 struct QWebSocketConfiguration
81 QWebSocketConfiguration();
85 QSslConfiguration m_sslConfiguration;
86 QList<QSslError> m_ignoredSslErrors;
87 bool m_ignoreSslErrors;
89 #ifndef QT_NONETWORKPROXY
90 QNetworkProxy m_proxy;
92 QTcpSocket *m_pSocket;
95 class QWebSocketPrivate : public QObject
98 Q_DISABLE_COPY(QWebSocketPrivate)
99 Q_DECLARE_PUBLIC(QWebSocket)
102 explicit QWebSocketPrivate(const QString &origin,
103 QWebSocketProtocol::Version version,
104 QWebSocket * const pWebSocket,
105 QObject *parent = Q_NULLPTR);
106 virtual ~QWebSocketPrivate();
109 QAbstractSocket::SocketError error() const;
110 QString errorString() const;
112 bool isValid() const;
113 QHostAddress localAddress() const;
114 quint16 localPort() const;
115 QAbstractSocket::PauseModes pauseMode() const;
116 QHostAddress peerAddress() const;
117 QString peerName() const;
118 quint16 peerPort() const;
119 #ifndef QT_NO_NETWORKPROXY
120 QNetworkProxy proxy() const;
121 void setProxy(const QNetworkProxy &networkProxy);
123 qint64 readBufferSize() const;
125 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
126 void setReadBufferSize(qint64 size);
127 void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
128 QVariant socketOption(QAbstractSocket::SocketOption option);
129 QAbstractSocket::SocketState state() const;
131 bool waitForConnected(int msecs);
132 bool waitForDisconnected(int msecs);
134 QWebSocketProtocol::Version version() const;
135 QString resourceName() const;
136 QUrl requestUrl() const;
137 QString origin() const;
138 QString protocol() const;
139 QString extension() const;
140 QWebSocketProtocol::CloseCode closeCode() const;
141 QString closeReason() const;
143 qint64 write(const char *message); //send data as text
144 qint64 write(const char *message, qint64 maxSize); //send data as text
145 qint64 write(const QString &message); //send data as text
146 qint64 write(const QByteArray &data); //send data as binary
149 void ignoreSslErrors(const QList<QSslError> &errors);
150 void setSslConfiguration(const QSslConfiguration &sslConfiguration);
151 QSslConfiguration sslConfiguration() const;
155 void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
156 void open(const QUrl &url, bool mask);
157 void ping(const QByteArray &payload);
160 void ignoreSslErrors();
165 void processPing(QByteArray data);
166 void processPong(QByteArray data);
167 void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
168 void processHandshake(QTcpSocket *pSocket);
169 void processStateChanged(QAbstractSocket::SocketState socketState);
172 QWebSocket * const q_ptr;
174 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = Q_NULLPTR);
175 void setVersion(QWebSocketProtocol::Version version);
176 void setResourceName(const QString &resourceName);
177 void setRequestUrl(const QUrl &requestUrl);
178 void setOrigin(const QString &origin);
179 void setProtocol(const QString &protocol);
180 void setExtension(const QString &extension);
181 void enableMasking(bool enable);
182 void setSocketState(QAbstractSocket::SocketState state);
183 void setErrorString(const QString &errorString);
185 qint64 doWriteData(const QByteArray &data, bool isBinary);
186 qint64 doWriteFrames(const QByteArray &data, bool isBinary);
188 void makeConnections(const QTcpSocket *pTcpSocket);
189 void releaseConnections(const QTcpSocket *pTcpSocket);
191 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
192 QString calculateAcceptKey(const QString &key) const;
193 QString createHandShakeRequest(QString resourceName,
200 static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
201 const QWebSocketHandshakeRequest &request,
202 const QWebSocketHandshakeResponse &response,
203 QObject *parent = Q_NULLPTR);
205 quint32 generateMaskingKey() const;
206 QByteArray generateKey() const;
207 quint32 generateRandomNumber() const;
208 qint64 writeFrames(const QList<QByteArray> &frames);
209 qint64 writeFrame(const QByteArray &frame);
211 QTcpSocket *m_pSocket;
212 QString m_errorString;
213 QWebSocketProtocol::Version m_version;
215 QString m_resourceName;
220 QAbstractSocket::SocketState m_socketState;
222 QByteArray m_key; //identification key used in handshake requests
224 bool m_mustMask; //a server must not mask the frames it sends
226 bool m_isClosingHandshakeSent;
227 bool m_isClosingHandshakeReceived;
228 QWebSocketProtocol::CloseCode m_closeCode;
229 QString m_closeReason;
233 QWebSocketDataProcessor m_dataProcessor;
234 QWebSocketConfiguration m_configuration;
236 friend class QWebSocketServerPrivate;
241 #endif // QWEBSOCKET_H