Add close code and close reason getter functions
[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 public:
81     QWebSocketConfiguration();
82
83 public:
84 #ifndef QT_NO_SSL
85     QSslConfiguration m_sslConfiguration;
86     QList<QSslError> m_ignoredSslErrors;
87     bool m_ignoreSslErrors;
88 #endif
89 #ifndef QT_NONETWORKPROXY
90     QNetworkProxy m_proxy;
91 #endif
92     QTcpSocket *m_pSocket;
93 };
94
95 class QWebSocketPrivate : public QObject
96 {
97     Q_OBJECT
98     Q_DISABLE_COPY(QWebSocketPrivate)
99     Q_DECLARE_PUBLIC(QWebSocket)
100
101 public:
102     explicit QWebSocketPrivate(const QString &origin,
103                                QWebSocketProtocol::Version version,
104                                QWebSocket * const pWebSocket,
105                                QObject *parent = Q_NULLPTR);
106     virtual ~QWebSocketPrivate();
107
108     void abort();
109     QAbstractSocket::SocketError error() const;
110     QString errorString() const;
111     bool flush();
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);
122 #endif
123     qint64 readBufferSize() const;
124     void resume();
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;
130
131     bool waitForConnected(int msecs);
132     bool waitForDisconnected(int msecs);
133
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;
142
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
147
148 #ifndef QT_NO_SSL
149     void ignoreSslErrors(const QList<QSslError> &errors);
150     void setSslConfiguration(const QSslConfiguration &sslConfiguration);
151     QSslConfiguration sslConfiguration() const;
152 #endif
153
154 public Q_SLOTS:
155     void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
156     void open(const QUrl &url, bool mask);
157     void ping(const QByteArray &payload);
158
159 #ifndef QT_NO_SSL
160     void ignoreSslErrors();
161 #endif
162
163 private Q_SLOTS:
164     void processData();
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);
170
171 private:
172     QWebSocket * const q_ptr;
173
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);
184
185     qint64 doWriteData(const QByteArray &data, bool isBinary);
186     qint64 doWriteFrames(const QByteArray &data, bool isBinary);
187
188     void makeConnections(const QTcpSocket *pTcpSocket);
189     void releaseConnections(const QTcpSocket *pTcpSocket);
190
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,
194                                    QString host,
195                                    QString origin,
196                                    QString extensions,
197                                    QString protocols,
198                                    QByteArray key);
199
200     static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
201                                    const QWebSocketHandshakeRequest &request,
202                                    const QWebSocketHandshakeResponse &response,
203                                    QObject *parent = Q_NULLPTR);
204
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);
210
211     QTcpSocket *m_pSocket;
212     QString m_errorString;
213     QWebSocketProtocol::Version m_version;
214     QUrl m_resource;
215     QString m_resourceName;
216     QUrl m_requestUrl;
217     QString m_origin;
218     QString m_protocol;
219     QString m_extension;
220     QAbstractSocket::SocketState m_socketState;
221
222     QByteArray m_key;   //identification key used in handshake requests
223
224     bool m_mustMask;    //a server must not mask the frames it sends
225
226     bool m_isClosingHandshakeSent;
227     bool m_isClosingHandshakeReceived;
228     QWebSocketProtocol::CloseCode m_closeCode;
229     QString m_closeReason;
230
231     QTime m_pingTimer;
232
233     QWebSocketDataProcessor m_dataProcessor;
234     QWebSocketConfiguration m_configuration;
235
236     friend class QWebSocketServerPrivate;
237 };
238
239 QT_END_NAMESPACE
240
241 #endif // QWEBSOCKET_H