Add user configurable mask generation
[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 #include <private/qobject_p.h>
68
69 #include "qwebsocketprotocol.h"
70 #include "qwebsocketdataprocessor_p.h"
71 #include "qdefaultmaskgenerator_p.h"
72
73 QT_BEGIN_NAMESPACE
74
75 class QWebSocketHandshakeRequest;
76 class QWebSocketHandshakeResponse;
77 class QTcpSocket;
78 class QWebSocket;
79 class QMaskGenerator;
80
81 struct QWebSocketConfiguration
82 {
83     Q_DISABLE_COPY(QWebSocketConfiguration)
84
85 public:
86     QWebSocketConfiguration();
87
88 public:
89 #ifndef QT_NO_SSL
90     QSslConfiguration m_sslConfiguration;
91     QList<QSslError> m_ignoredSslErrors;
92     bool m_ignoreSslErrors;
93 #endif
94 #ifndef QT_NONETWORKPROXY
95     QNetworkProxy m_proxy;
96 #endif
97     QTcpSocket *m_pSocket;
98 };
99
100 class QWebSocketPrivate : public QObjectPrivate
101 {
102     Q_DISABLE_COPY(QWebSocketPrivate)
103
104 public:
105     Q_DECLARE_PUBLIC(QWebSocket)
106     explicit QWebSocketPrivate(const QString &origin,
107                                QWebSocketProtocol::Version version,
108                                QWebSocket * const pWebSocket);
109     virtual ~QWebSocketPrivate();
110
111     void init();
112     void abort();
113     QAbstractSocket::SocketError error() const;
114     QString errorString() const;
115     bool flush();
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);
126 #endif
127     void setMaskGenerator(const QMaskGenerator *maskGenerator);
128     const QMaskGenerator *maskGenerator() const;
129     qint64 readBufferSize() const;
130     void resume();
131     void setPauseMode(QAbstractSocket::PauseModes pauseMode);
132     void setReadBufferSize(qint64 size);
133     QAbstractSocket::SocketState state() const;
134
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;
143
144     qint64 sendTextMessage(const QString &message);
145     qint64 sendBinaryMessage(const QByteArray &data);
146
147 #ifndef QT_NO_SSL
148     void ignoreSslErrors(const QList<QSslError> &errors);
149     void ignoreSslErrors();
150     void setSslConfiguration(const QSslConfiguration &sslConfiguration);
151     QSslConfiguration sslConfiguration() const;
152 #endif
153
154     void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
155     void open(const QUrl &url, bool mask);
156     void ping(const QByteArray &payload);
157
158     QWebSocket * const q_ptr;
159
160 private:
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);
172
173     void processData();
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);
179
180     qint64 doWriteFrames(const QByteArray &data, bool isBinary) Q_REQUIRED_RESULT;
181
182     void makeConnections(const QTcpSocket *pTcpSocket);
183     void releaseConnections(const QTcpSocket *pTcpSocket);
184
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,
189                                    QString host,
190                                    QString origin,
191                                    QString extensions,
192                                    QString protocols,
193                                    QByteArray key);
194
195     static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
196                                    const QWebSocketHandshakeRequest &request,
197                                    const QWebSocketHandshakeResponse &response,
198                                    QObject *parent = Q_NULLPTR) Q_REQUIRED_RESULT;
199
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;
204
205     QScopedPointer<QTcpSocket> m_pSocket;
206     QString m_errorString;
207     QWebSocketProtocol::Version m_version;
208     QUrl m_resource;
209     QString m_resourceName;
210     QUrl m_requestUrl;
211     QString m_origin;
212     QString m_protocol;
213     QString m_extension;
214     QAbstractSocket::SocketState m_socketState;
215     QAbstractSocket::PauseModes m_pauseMode;
216     qint64 m_readBufferSize;
217
218     QByteArray m_key;   //identification key used in handshake requests
219
220     bool m_mustMask;    //a server must not mask the frames it sends
221
222     bool m_isClosingHandshakeSent;
223     bool m_isClosingHandshakeReceived;
224     QWebSocketProtocol::CloseCode m_closeCode;
225     QString m_closeReason;
226
227     QTime m_pingTimer;
228
229     QWebSocketDataProcessor m_dataProcessor;
230     QWebSocketConfiguration m_configuration;
231
232     QMaskGenerator *m_pMaskGenerator;
233     QDefaultMaskGenerator m_defaultMaskGenerator;
234
235     friend class QWebSocketServerPrivate;
236 };
237
238 QT_END_NAMESPACE
239
240 #endif // QWEBSOCKET_H