d8c97ddc8ec56daa24bd4da6533206e7215955f5
[contrib/qtwebsockets.git] / src / websockets / qwebsocket_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QWEBSOCKET_P_H
43 #define QWEBSOCKET_P_H
44 //
45 //  W A R N I N G
46 //  -------------
47 //
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.
51 //
52 // We mean it.
53 //
54
55 #include <QtCore/QUrl>
56 #include <QtNetwork/QTcpSocket>
57 #include <QtNetwork/QHostAddress>
58 #ifndef QT_NO_NETWORKPROXY
59 #include <QtNetwork/QNetworkProxy>
60 #endif
61 #ifndef QT_NO_SSL
62 #include <QtNetwork/QSslConfiguration>
63 #include <QtNetwork/QSslError>
64 #include <QtNetwork/QSslSocket>
65 #endif
66 #include <QtCore/QTime>
67
68 #include "qwebsocketprotocol.h"
69 #include "qwebsocketdataprocessor_p.h"
70
71 QT_BEGIN_NAMESPACE
72
73 class QWebSocketHandshakeRequest;
74 class QWebSocketHandshakeResponse;
75 class QTcpSocket;
76 class QWebSocket;
77
78 struct QWebSocketConfiguration
79 {
80     Q_DISABLE_COPY(QWebSocketConfiguration)
81
82 public:
83     QWebSocketConfiguration();
84
85 public:
86 #ifndef QT_NO_SSL
87     QSslConfiguration m_sslConfiguration;
88     QList<QSslError> m_ignoredSslErrors;
89     bool m_ignoreSslErrors;
90 #endif
91 #ifndef QT_NONETWORKPROXY
92     QNetworkProxy m_proxy;
93 #endif
94     QTcpSocket *m_pSocket;
95 };
96
97 class QWebSocketPrivate : public QObject
98 {
99     Q_OBJECT
100     Q_DISABLE_COPY(QWebSocketPrivate)
101     Q_DECLARE_PUBLIC(QWebSocket)
102
103 public:
104     explicit QWebSocketPrivate(const QString &origin,
105                                QWebSocketProtocol::Version version,
106                                QWebSocket * const pWebSocket,
107                                QObject *parent = Q_NULLPTR);
108     virtual ~QWebSocketPrivate();
109
110     void init();
111     void abort();
112     QAbstractSocket::SocketError error() const;
113     QString errorString() const;
114     bool flush();
115     bool isValid() const;
116     QHostAddress localAddress() const;
117     quint16 localPort() const;
118     QAbstractSocket::PauseModes pauseMode() const;
119     QHostAddress peerAddress() const;
120     QString peerName() const;
121     quint16 peerPort() const;
122 #ifndef QT_NO_NETWORKPROXY
123     QNetworkProxy proxy() const;
124     void setProxy(const QNetworkProxy &networkProxy);
125 #endif
126     qint64 readBufferSize() const;
127     void resume();
128     void setPauseMode(QAbstractSocket::PauseModes pauseMode);
129     void setReadBufferSize(qint64 size);
130     QAbstractSocket::SocketState state() const;
131
132     QWebSocketProtocol::Version version() const;
133     QString resourceName() const;
134     QUrl requestUrl() const;
135     QString origin() const;
136     QString protocol() const;
137     QString extension() const;
138     QWebSocketProtocol::CloseCode closeCode() const;
139     QString closeReason() const;
140
141     qint64 sendTextMessage(const QString &message);
142     qint64 sendBinaryMessage(const QByteArray &data);
143
144 #ifndef QT_NO_SSL
145     void ignoreSslErrors(const QList<QSslError> &errors);
146     void setSslConfiguration(const QSslConfiguration &sslConfiguration);
147     QSslConfiguration sslConfiguration() const;
148 #endif
149
150 public Q_SLOTS:
151     void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
152     void open(const QUrl &url, bool mask);
153     void ping(const QByteArray &payload);
154
155 #ifndef QT_NO_SSL
156     void ignoreSslErrors();
157 #endif
158
159 private Q_SLOTS:
160     void processData();
161     void processPing(const QByteArray &data);
162     void processPong(const QByteArray &data);
163     void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
164     void processHandshake(QTcpSocket *pSocket);
165     void processStateChanged(QAbstractSocket::SocketState socketState);
166
167 private:
168     QWebSocket * const q_ptr;
169
170     QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version,
171                       QWebSocket *pWebSocket, QObject *parent = Q_NULLPTR);
172     void setVersion(QWebSocketProtocol::Version version);
173     void setResourceName(const QString &resourceName);
174     void setRequestUrl(const QUrl &requestUrl);
175     void setOrigin(const QString &origin);
176     void setProtocol(const QString &protocol);
177     void setExtension(const QString &extension);
178     void enableMasking(bool enable);
179     void setSocketState(QAbstractSocket::SocketState state);
180     void setErrorString(const QString &errorString);
181
182     qint64 doWriteFrames(const QByteArray &data, bool isBinary) Q_REQUIRED_RESULT;
183
184     void makeConnections(const QTcpSocket *pTcpSocket);
185     void releaseConnections(const QTcpSocket *pTcpSocket);
186
187     QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength,
188                               quint32 maskingKey, bool lastFrame);
189     QString calculateAcceptKey(const QByteArray &key) const;
190     QString createHandShakeRequest(QString resourceName,
191                                    QString host,
192                                    QString origin,
193                                    QString extensions,
194                                    QString protocols,
195                                    QByteArray key);
196
197     static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
198                                    const QWebSocketHandshakeRequest &request,
199                                    const QWebSocketHandshakeResponse &response,
200                                    QObject *parent = Q_NULLPTR) Q_REQUIRED_RESULT;
201
202     quint32 generateMaskingKey() const;
203     QByteArray generateKey() const;
204     quint32 generateRandomNumber() const;
205     qint64 writeFrames(const QList<QByteArray> &frames) Q_REQUIRED_RESULT;
206     qint64 writeFrame(const QByteArray &frame) Q_REQUIRED_RESULT;
207
208     QScopedPointer<QTcpSocket> m_pSocket;
209     QString m_errorString;
210     QWebSocketProtocol::Version m_version;
211     QUrl m_resource;
212     QString m_resourceName;
213     QUrl m_requestUrl;
214     QString m_origin;
215     QString m_protocol;
216     QString m_extension;
217     QAbstractSocket::SocketState m_socketState;
218     QAbstractSocket::PauseModes m_pauseMode;
219     qint64 m_readBufferSize;
220
221     QByteArray m_key;   //identification key used in handshake requests
222
223     bool m_mustMask;    //a server must not mask the frames it sends
224
225     bool m_isClosingHandshakeSent;
226     bool m_isClosingHandshakeReceived;
227     QWebSocketProtocol::CloseCode m_closeCode;
228     QString m_closeReason;
229
230     QTime m_pingTimer;
231
232     QWebSocketDataProcessor m_dataProcessor;
233     QWebSocketConfiguration m_configuration;
234
235     friend class QWebSocketServerPrivate;
236 };
237
238 QT_END_NAMESPACE
239
240 #endif // QWEBSOCKET_H