a921827a84b70f2e039de7df3306cf7d91e4f068
[contrib/qtwebsockets.git] / src / qwebsocket_p.h
1 /**
2  * @file websocket.h
3  * @brief Defines the WebSocket class.
4  *
5  * \note Currently, only V13 (RFC6455) is supported.
6  * \note Both text and binary websockets are supported.
7  * \note The secure version (wss) is currently not implemented.
8  * @author Kurt Pattyn (pattyn.kurt@gmail.com)
9  */
10
11 #ifndef QWEBSOCKET_P_H
12 #define QWEBSOCKET_P_H
13 //
14 //  W A R N I N G
15 //  -------------
16 //
17 // This file is not part of the Qt API.  It exists purely as an
18 // implementation detail.  This header file may change from version to
19 // version without notice, or even be removed.
20 //
21 // We mean it.
22 //
23
24 #include <QUrl>
25 #include <QAbstractSocket>
26 #include <QHostAddress>
27 #ifndef QT_NO_NETWORKPROXY
28 #include <QNetworkProxy>
29 #endif
30 #include <QTime>
31 #include "qwebsocketsglobal.h"
32 #include "qwebsocketprotocol.h"
33 #include "dataprocessor_p.h"
34
35 QT_BEGIN_NAMESPACE
36
37 class HandshakeRequest;
38 class HandshakeResponse;
39 class QTcpSocket;
40 class QWebSocket;
41
42 class QWebSocketPrivate:public QObject
43 {
44         Q_OBJECT
45
46 public:
47         explicit QWebSocketPrivate(QString origin,
48                                                            QWebSocketProtocol::Version version,
49                                                            QWebSocket * const pWebSocket,
50                                                            QObject *parent = 0);
51         virtual ~QWebSocketPrivate();
52
53         void abort();
54         QAbstractSocket::SocketError error() const;
55         QString errorString() const;
56         bool flush();
57         bool isValid();
58         QHostAddress localAddress() const;
59         quint16 localPort() const;
60         QAbstractSocket::PauseModes pauseMode() const;
61         QHostAddress peerAddress() const;
62         QString peerName() const;
63         quint16 peerPort() const;
64 #ifndef QT_NO_NETWORKPROXY
65         QNetworkProxy proxy() const;
66         void setProxy(const QNetworkProxy &networkProxy);
67 #endif
68         qint64 readBufferSize() const;
69         void resume();
70         void setPauseMode(QAbstractSocket::PauseModes pauseMode);
71         void setReadBufferSize(qint64 size);
72         void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
73         QVariant socketOption(QAbstractSocket::SocketOption option);
74         QAbstractSocket::SocketState state() const;
75
76         bool waitForConnected(int msecs = 30000);
77         bool waitForDisconnected(int msecs = 30000);
78
79         QWebSocketProtocol::Version version();
80         QString resourceName();
81         QUrl requestUrl();
82         QString origin();
83         QString protocol();
84         QString extension();
85
86         qint64 write(const char *message);              //send data as text
87         qint64 write(const char *message, qint64 maxSize);              //send data as text
88         qint64 write(const QString &message);   //send data as text
89         qint64 write(const QByteArray &data);   //send data as binary
90
91 public Q_SLOTS:
92         virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
93         virtual void open(const QUrl &url, bool mask = true);
94         void ping();
95
96 Q_SIGNALS:
97         void aboutToClose();
98         void connected();
99         void disconnected();
100         void stateChanged(QAbstractSocket::SocketState state);
101         void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
102         void readChannelFinished();
103         void textFrameReceived(QString frame, bool isLastFrame);
104         void binaryFrameReceived(QByteArray frame, bool isLastFrame);
105         void textMessageReceived(QString message);
106         void binaryMessageReceived(QByteArray message);
107         void error(QAbstractSocket::SocketError error);
108         void pong(quint64 elapsedTime);
109
110 private Q_SLOTS:
111         void processData();
112         void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
113         void processHandshake(QTcpSocket *pSocket);
114         void processStateChanged(QAbstractSocket::SocketState socketState);
115
116 private:
117         Q_DISABLE_COPY(QWebSocketPrivate)
118
119         QWebSocket * const q_ptr;
120
121         QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = 0);
122         void setVersion(QWebSocketProtocol::Version version);
123         void setResourceName(QString resourceName);
124         void setRequestUrl(QUrl requestUrl);
125         void setOrigin(QString origin);
126         void setProtocol(QString protocol);
127         void setExtension(QString extension);
128         void enableMasking(bool enable);
129         void setSocketState(QAbstractSocket::SocketState state);
130         void setErrorString(QString errorString);
131
132         qint64 doWriteData(const QByteArray &data, bool isBinary);
133         qint64 doWriteFrames(const QByteArray &data, bool isBinary);
134
135         void makeConnections(const QTcpSocket *pTcpSocket);
136         void releaseConnections(const QTcpSocket *pTcpSocket);
137
138         QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
139         QString calculateAcceptKey(const QString &key) const;
140         QString createHandShakeRequest(QString resourceName,
141                                                                    QString host,
142                                                                    QString origin,
143                                                                    QString extensions,
144                                                                    QString protocols,
145                                                                    QByteArray key);
146
147         static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
148                                                                    const HandshakeRequest &request,
149                                                                    const HandshakeResponse &response,
150                                                                    QObject *parent = 0);
151
152         quint32 generateMaskingKey() const;
153         QByteArray generateKey() const;
154         quint32 generateRandomNumber() const;
155         qint64 writeFrames(const QList<QByteArray> &frames);
156         qint64 writeFrame(const QByteArray &frame);
157
158         QTcpSocket *m_pSocket;
159         QString m_errorString;
160         QWebSocketProtocol::Version m_version;
161         QUrl m_resource;
162         QString m_resourceName;
163         QUrl m_requestUrl;
164         QString m_origin;
165         QString m_protocol;
166         QString m_extension;
167         QAbstractSocket::SocketState m_socketState;
168
169         QByteArray m_key;       //identification key used in handshake requests
170
171         bool m_mustMask;        //a server must not mask the frames it sends
172
173         bool m_isClosingHandshakeSent;
174         bool m_isClosingHandshakeReceived;
175
176         QTime m_pingTimer;
177
178         DataProcessor m_dataProcessor;
179
180
181         friend class QWebSocketServerPrivate;
182         friend class QWebSocket;
183 };
184
185 QT_END_NAMESPACE
186
187 #endif // QWEBSOCKET_H