Implement of secure web sockets
[contrib/qtwebsockets.git] / src / websockets / qwebsocket.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_H
21 #define QWEBSOCKET_H
22
23 #include <QUrl>
24 #ifndef QT_NO_NETWORKPROXY
25 #include <QNetworkProxy>
26 #endif
27 #ifndef QT_NO_SSL
28 #include <QSslError>
29 #include <QSslConfiguration>
30 #endif
31 #include "qwebsockets_global.h"
32 #include "qwebsocketprotocol.h"
33
34 QT_BEGIN_NAMESPACE
35
36 class QTcpSocket;
37 class QWebSocketPrivate;
38
39 class Q_WEBSOCKETS_EXPORT QWebSocket : public QObject
40 {
41     Q_OBJECT
42     Q_DISABLE_COPY(QWebSocket)
43     Q_DECLARE_PRIVATE(QWebSocket)
44
45 public:
46     explicit QWebSocket(const QString &origin = QString(), QWebSocketProtocol::Version version = QWebSocketProtocol::V_LATEST, QObject *parent = Q_NULLPTR);
47     virtual ~QWebSocket();
48
49     void abort();
50     QAbstractSocket::SocketError error() const;
51     QString errorString() const;
52     bool flush();
53     bool isValid() const;
54     QHostAddress localAddress() const;
55     quint16 localPort() const;
56     QAbstractSocket::PauseModes pauseMode() const;
57     QHostAddress peerAddress() const;
58     QString peerName() const;
59     quint16 peerPort() const;
60 #ifndef QT_NO_NETWORKPROXY
61     QNetworkProxy proxy() const;
62     void setProxy(const QNetworkProxy &networkProxy);
63 #endif
64     qint64 readBufferSize() const;
65     void setReadBufferSize(qint64 size);
66
67     void resume();
68     void setPauseMode(QAbstractSocket::PauseModes pauseMode);
69
70     void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
71     QVariant socketOption(QAbstractSocket::SocketOption option);
72     QAbstractSocket::SocketState state() const;
73
74     bool waitForConnected(int msecs = 30000);
75     bool waitForDisconnected(int msecs = 30000);
76
77     QWebSocketProtocol::Version version() const;
78     QString resourceName() const;
79     QUrl requestUrl() const;
80     QString origin() const;
81     QString protocol() const;
82     QString extension() const;
83
84     qint64 write(const char *message);          //send data as text
85     qint64 write(const char *message, qint64 maxSize);          //send data as text
86     qint64 write(const QString &message);       //send data as text
87     qint64 write(const QByteArray &data);       //send data as binary
88
89 #ifndef QT_NO_SSL
90     void ignoreSslErrors(const QList<QSslError> &errors);
91     void setSslConfiguration(const QSslConfiguration &sslConfiguration);
92     QSslConfiguration sslConfiguration() const;
93 #endif
94
95 public Q_SLOTS:
96     void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, const QString &reason = QString());
97     void open(const QUrl &url, bool mask = true);
98     void ping(const QByteArray &payload = QByteArray());
99
100 Q_SIGNALS:
101     void aboutToClose();
102     void connected();
103     void disconnected();
104     void stateChanged(QAbstractSocket::SocketState state);
105 #ifndef QT_NO_NETWORKPROXY
106     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
107 #endif
108     void readChannelFinished();
109     void textFrameReceived(QString frame, bool isLastFrame);
110     void binaryFrameReceived(QByteArray frame, bool isLastFrame);
111     void textMessageReceived(QString message);
112     void binaryMessageReceived(QByteArray message);
113     void error(QAbstractSocket::SocketError error);
114     void pong(quint64 elapsedTime, QByteArray payload);
115     void bytesWritten(qint64 bytes);
116
117 private:
118     QWebSocket(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QObject *parent = Q_NULLPTR);
119     QWebSocketPrivate * const d_ptr;
120 };
121
122 QT_END_NAMESPACE
123
124 #endif // QWEBSOCKET_H