2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
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.
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.
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
20 #ifndef QWEBSOCKET_P_H
21 #define QWEBSOCKET_P_H
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.
34 #include <QAbstractSocket>
35 #include <QHostAddress>
36 #ifndef QT_NO_NETWORKPROXY
37 #include <QNetworkProxy>
40 #include "qwebsocketsglobal.h"
41 #include "qwebsocketprotocol.h"
42 #include "dataprocessor_p.h"
46 class HandshakeRequest;
47 class HandshakeResponse;
51 class QWebSocketPrivate:public QObject
56 explicit QWebSocketPrivate(const QString &origin,
57 QWebSocketProtocol::Version version,
58 QWebSocket * const pWebSocket,
60 virtual ~QWebSocketPrivate();
63 QAbstractSocket::SocketError error() const;
64 QString errorString() const;
67 QHostAddress localAddress() const;
68 quint16 localPort() const;
69 QAbstractSocket::PauseModes pauseMode() const;
70 QHostAddress peerAddress() const;
71 QString peerName() const;
72 quint16 peerPort() const;
73 #ifndef QT_NO_NETWORKPROXY
74 QNetworkProxy proxy() const;
75 void setProxy(const QNetworkProxy &networkProxy);
77 qint64 readBufferSize() const;
79 void setPauseMode(QAbstractSocket::PauseModes pauseMode);
80 void setReadBufferSize(qint64 size);
81 void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
82 QVariant socketOption(QAbstractSocket::SocketOption option);
83 QAbstractSocket::SocketState state() const;
85 bool waitForConnected(int msecs);
86 bool waitForDisconnected(int msecs);
88 QWebSocketProtocol::Version version() const;
89 QString resourceName() const;
90 QUrl requestUrl() const;
91 QString origin() const;
92 QString protocol() const;
93 QString extension() const;
95 qint64 write(const char *message); //send data as text
96 qint64 write(const char *message, qint64 maxSize); //send data as text
97 qint64 write(const QString &message); //send data as text
98 qint64 write(const QByteArray &data); //send data as binary
101 void close(QWebSocketProtocol::CloseCode closeCode, QString reason);
102 void open(const QUrl &url, bool mask);
107 void processControlFrame(QWebSocketProtocol::OpCode opCode, QByteArray frame);
108 void processHandshake(QTcpSocket *pSocket);
109 void processStateChanged(QAbstractSocket::SocketState socketState);
112 Q_DISABLE_COPY(QWebSocketPrivate)
113 Q_DECLARE_PUBLIC(QWebSocket)
115 QWebSocket * const q_ptr;
117 QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QWebSocket *pWebSocket, QObject *parent = 0);
118 void setVersion(QWebSocketProtocol::Version version);
119 void setResourceName(const QString &resourceName);
120 void setRequestUrl(const QUrl &requestUrl);
121 void setOrigin(const QString &origin);
122 void setProtocol(const QString &protocol);
123 void setExtension(const QString &extension);
124 void enableMasking(bool enable);
125 void setSocketState(QAbstractSocket::SocketState state);
126 void setErrorString(const QString &errorString);
128 qint64 doWriteData(const QByteArray &data, bool isBinary);
129 qint64 doWriteFrames(const QByteArray &data, bool isBinary);
131 void makeConnections(const QTcpSocket *pTcpSocket);
132 void releaseConnections(const QTcpSocket *pTcpSocket);
134 QByteArray getFrameHeader(QWebSocketProtocol::OpCode opCode, quint64 payloadLength, quint32 maskingKey, bool lastFrame) const;
135 QString calculateAcceptKey(const QString &key) const;
136 QString createHandShakeRequest(QString resourceName,
143 static QWebSocket *upgradeFrom(QTcpSocket *tcpSocket,
144 const HandshakeRequest &request,
145 const HandshakeResponse &response,
146 QObject *parent = 0);
148 quint32 generateMaskingKey() const;
149 QByteArray generateKey() const;
150 quint32 generateRandomNumber() const;
151 qint64 writeFrames(const QList<QByteArray> &frames);
152 qint64 writeFrame(const QByteArray &frame);
154 QTcpSocket *m_pSocket;
155 QString m_errorString;
156 QWebSocketProtocol::Version m_version;
158 QString m_resourceName;
163 QAbstractSocket::SocketState m_socketState;
165 QByteArray m_key; //identification key used in handshake requests
167 bool m_mustMask; //a server must not mask the frames it sends
169 bool m_isClosingHandshakeSent;
170 bool m_isClosingHandshakeReceived;
174 DataProcessor m_dataProcessor;
176 friend class QWebSocketServerPrivate;
181 #endif // QWEBSOCKET_H