Add functionality to ignore ssl errors
[contrib/qtwebsockets.git] / src / websockets / qwebsocket_p.h
1 /*
2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #ifndef QWEBSOCKET_P_H
21 #define QWEBSOCKET_P_H
22 //
23 //  W A R N I N G
24 //  -------------
25 //
26 // This file is not part of the Qt API.  It exists purely as an
27 // implementation detail.  This header file may change from version to
28 // version without notice, or even be removed.
29 //
30 // We mean it.
31 //
32
33 #include <QUrl>
34 #include <QHostAddress>
35 #ifndef QT_NO_NETWORKPROXY
36 #include <QNetworkProxy>
37 #endif
38 #ifndef QT_NO_SSL
39 #include <QSslConfiguration>
40 #include <QSslError>
41 #endif
42 #include <QTime>
43
44 #include "qwebsocketprotocol.h"
45 #include "qwebsocketdataprocessor_p.h"
46
47 QT_BEGIN_NAMESPACE
48
49 class QWebSocketHandshakeRequest;
50 class QWebSocketHandshakeResponse;
51 class QTcpSocket;
52 class QWebSocket;
53
54 class QWebSocketPrivate : public QObject
55 {
56     Q_OBJECT
57     Q_DISABLE_COPY(QWebSocketPrivate)
58     Q_DECLARE_PUBLIC(QWebSocket)
59
60 public:
61     explicit QWebSocketPrivate(const QString &origin,
62                                QWebSocketProtocol::Version version,
63                                QWebSocket * const pWebSocket,
64                                QObject *parent = Q_NULLPTR);
65     virtual ~QWebSocketPrivate();
66
67     void abort();
68     QAbstractSocket::SocketError error() const;
69     QString errorString() const;
70     bool flush();
71     bool isValid() const;
72     QHostAddress localAddress() const;
73     quint16 localPort() const;
74     QAbstractSocket::PauseModes pauseMode() const;
75     QHostAddress peerAddress() const;
76     QString peerName() const;
77     quint16 peerPort() const;
78 #ifndef QT_NO_NETWORKPROXY
79     QNetworkProxy proxy() const;
80     void setProxy(const QNetworkProxy &networkProxy);
81 #endif
82     qint64 readBufferSize() const;
83     void resume();
84     void setPauseMode(QAbstractSocket::PauseModes pauseMode);
85     void setReadBufferSize(qint64 size);
86     void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
87     QVariant socketOption(QAbstractSocket::SocketOption option);
88     QAbstractSocket::SocketState state() const;
89
90     bool waitForConnected(int msecs);
91     bool waitForDisconnected(int msecs);
92
93     QWebSocketProtocol::Version version() const;
94     QString resourceName() const;
95     QUrl requestUrl() const;
96     QString origin() const;
97     QString protocol() const;
98     QString extension() const;
99
100     qint64 write(const char *message);          //send data as text
101     qint64 write(const char *message, qint64 maxSize);          //send data as text
102     qint64 write(const QString &message);       //send data as text
103     qint64 write(const QByteArray &data);       //send data as binary
104
105 #ifndef QT_NO_SSL
106     void ignoreSslErrors(const QList<QSslError> &errors);
107     void setSslConfiguration(const QSslConfiguration &sslConfiguration);
108     QSslConfiguration sslConfiguration() const;
109 #endif
110
111 public Q_SLOTS:
112     void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
113     void open(const QUrl &url, bool mask);
114     void ping(const QByteArray &payload);
115
116 #ifndef QT_NO_SSL
117     void ignoreSslErrors();
118 #endif
119
120 private Q_SLOTS:
121     void processData();
122     void processPing(QByteArray data);
123     void processPong(QByteArray data);
124     void processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason);
125     void processHandshake(QTcpSocket *pSocket);
126     void processStateChanged(QAbstractSocket::SocketState socketState);
127
128 private:
129     QWebSocket * const q_ptr;
130 #ifndef QT_NO_SSL
131     QSslConfiguration m_sslConfiguration;
132     QList<QSslError> m_ignoredSslErrors;
133     bool m_ignoreSslErrors;
134 #endif
135
136     QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = Q_NULLPTR);
137     void setVersion(QWebSocketProtocol::Version version);
138     void setResourceName(const QString &resourceName);
139     void setRequestUrl(const QUrl &requestUrl);
140     void setOrigin(const QString &origin);
141     void setProtocol(const QString &protocol);
142     void setExtension(const QString &extension);
143     void enableMasking(bool enable);
144     void setSocketState(QAbstractSocket::SocketState state);
145     void setErrorString(const QString &errorString);
146
147     qint64 doWriteData(const QByteArray &data, bool isBinary);
148     qint64 doWriteFrames(const QByteArray &data, bool isBinary);
149
150     void makeConnections(const QTcpSocket *pTcpSocket);
151     void releaseConnections(const QTcpSocket *pTcpSocket);
152
153     QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
154     QString calculateAcceptKey(const QString &key) const;
155     QString createHandShakeRequest(QString resourceName,
156                                    QString host,
157                                    QString origin,
158                                    QString extensions,
159                                    QString protocols,
160                                    QByteArray key);
161
162     static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
163                                    const QWebSocketHandshakeRequest &request,
164                                    const QWebSocketHandshakeResponse &response,
165                                    QObject *parent = Q_NULLPTR);
166
167     quint32 generateMaskingKey() const;
168     QByteArray generateKey() const;
169     quint32 generateRandomNumber() const;
170     qint64 writeFrames(const QList<QByteArray> &frames);
171     qint64 writeFrame(const QByteArray &frame);
172
173     QTcpSocket *m_pSocket;
174     QString m_errorString;
175     QWebSocketProtocol::Version m_version;
176     QUrl m_resource;
177     QString m_resourceName;
178     QUrl m_requestUrl;
179     QString m_origin;
180     QString m_protocol;
181     QString m_extension;
182     QAbstractSocket::SocketState m_socketState;
183
184     QByteArray m_key;   //identification key used in handshake requests
185
186     bool m_mustMask;    //a server must not mask the frames it sends
187
188     bool m_isClosingHandshakeSent;
189     bool m_isClosingHandshakeReceived;
190
191     QTime m_pingTimer;
192
193     QWebSocketDataProcessor m_dataProcessor;
194
195     friend class QWebSocketServerPrivate;
196 };
197
198 QT_END_NAMESPACE
199
200 #endif // QWEBSOCKET_H