3 #include <QNetworkProxy>
4 #include "qwebsocketserver.h"
5 #include "qwebsocketserver_p.h"
6 #include "qwebsocketprotocol.h"
7 #include "handshakerequest_p.h"
8 #include "handshakeresponse_p.h"
9 #include "qwebsocket.h"
10 #include "qwebsocket_p.h"
17 QWebSocketServerPrivate::QWebSocketServerPrivate(const QString &serverName, QWebSocketServer * const pWebSocketServer, QObject *parent) :
19 q_ptr(pWebSocketServer),
21 m_serverName(serverName),
22 m_pendingConnections()
24 Q_ASSERT(pWebSocketServer != 0);
25 m_pTcpServer = new QTcpServer(this);
26 connect(m_pTcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
32 QWebSocketServerPrivate::~QWebSocketServerPrivate()
34 while (!m_pendingConnections.isEmpty())
36 QWebSocket *pWebSocket = m_pendingConnections.dequeue();
37 pWebSocket->close(QWebSocketProtocol::CC_GOING_AWAY, "Server closed.");
38 pWebSocket->deleteLater();
40 m_pTcpServer->deleteLater();
46 void QWebSocketServerPrivate::close()
48 m_pTcpServer->close();
54 QString QWebSocketServerPrivate::errorString() const
56 return m_pTcpServer->errorString();
62 bool QWebSocketServerPrivate::hasPendingConnections() const
64 return !m_pendingConnections.isEmpty();
70 bool QWebSocketServerPrivate::isListening() const
72 return m_pTcpServer->isListening();
78 bool QWebSocketServerPrivate::listen(const QHostAddress &address, quint16 port)
80 return m_pTcpServer->listen(address, port);
86 int QWebSocketServerPrivate::maxPendingConnections() const
88 return m_pTcpServer->maxPendingConnections();
94 void QWebSocketServerPrivate::addPendingConnection(QWebSocket *pWebSocket)
96 if (m_pendingConnections.size() < maxPendingConnections())
98 m_pendingConnections.enqueue(pWebSocket);
105 QWebSocket *QWebSocketServerPrivate::nextPendingConnection()
107 QWebSocket *pWebSocket = 0;
108 if (!m_pendingConnections.isEmpty())
110 pWebSocket = m_pendingConnections.dequeue();
118 void QWebSocketServerPrivate::pauseAccepting()
120 m_pTcpServer->pauseAccepting();
123 #ifndef QT_NO_NETWORKPROXY
127 QNetworkProxy QWebSocketServerPrivate::proxy() const
129 return m_pTcpServer->proxy();
135 void QWebSocketServerPrivate::setProxy(const QNetworkProxy &networkProxy)
137 m_pTcpServer->setProxy(networkProxy);
143 void QWebSocketServerPrivate::resumeAccepting()
145 m_pTcpServer->resumeAccepting();
151 QHostAddress QWebSocketServerPrivate::serverAddress() const
153 return m_pTcpServer->serverAddress();
159 QAbstractSocket::SocketError QWebSocketServerPrivate::serverError() const
161 return m_pTcpServer->serverError();
167 quint16 QWebSocketServerPrivate::serverPort() const
169 return m_pTcpServer->serverPort();
175 void QWebSocketServerPrivate::setMaxPendingConnections(int numConnections)
177 m_pTcpServer->setMaxPendingConnections(numConnections);
183 bool QWebSocketServerPrivate::setSocketDescriptor(int socketDescriptor)
185 return m_pTcpServer->setSocketDescriptor(socketDescriptor);
191 int QWebSocketServerPrivate::socketDescriptor() const
193 return m_pTcpServer->socketDescriptor();
199 bool QWebSocketServerPrivate::waitForNewConnection(int msec, bool *timedOut)
201 return m_pTcpServer->waitForNewConnection(msec, timedOut);
207 QList<QWebSocketProtocol::Version> QWebSocketServerPrivate::supportedVersions() const
209 QList<QWebSocketProtocol::Version> supportedVersions;
210 supportedVersions << QWebSocketProtocol::currentVersion(); //we only support V13
211 return supportedVersions;
217 QList<QString> QWebSocketServerPrivate::supportedProtocols() const
219 QList<QString> supportedProtocols;
220 return supportedProtocols; //no protocols are currently supported
226 QList<QString> QWebSocketServerPrivate::supportedExtensions() const
228 QList<QString> supportedExtensions;
229 return supportedExtensions; //no extensions are currently supported
235 void QWebSocketServerPrivate::onNewConnection()
237 QTcpSocket *pTcpSocket = m_pTcpServer->nextPendingConnection();
238 connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(handshakeReceived()));
244 void QWebSocketServerPrivate::onCloseConnection()
246 QTcpSocket *pTcpSocket = qobject_cast<QTcpSocket*>(sender());
256 void QWebSocketServerPrivate::handshakeReceived()
258 QTcpSocket *pTcpSocket = qobject_cast<QTcpSocket*>(sender());
261 bool success = false;
262 bool isSecure = false;
263 HandshakeRequest request(pTcpSocket->peerPort(), isSecure);
264 QTextStream textStream(pTcpSocket);
265 textStream >> request;
267 HandshakeResponse response(request,
269 q_ptr->isOriginAllowed(request.getOrigin()),
271 supportedProtocols(),
272 supportedExtensions());
273 disconnect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(handshakeReceived()));
275 if (response.isValid())
277 QTextStream httpStream(pTcpSocket);
278 httpStream << response;
281 if (response.canUpgrade())
283 QWebSocket *pWebSocket = QWebSocketPrivate::upgradeFrom(pTcpSocket, request, response);
286 pWebSocket->setParent(this);
287 addPendingConnection(pWebSocket);
288 Q_EMIT q_ptr->newConnection();
293 qDebug() << "QWebSocketServerPrivate::handshakeReceived: Upgrading to WebSocket failed.";
298 qDebug() << "QWebSocketServerPrivate::handshakeReceived: Cannot upgrade to websocket.";
303 qDebug() << "QWebSocketServerPrivate::handshakeReceived: Invalid response. This should not happen!!!";
307 qDebug() << "QWebSocketServerPrivate::handshakeReceived: Closing socket because of invalid or unsupported request";
313 qDebug() << "WebSocketServerPrivate::handshakeReceived: Sender socket is NULL. This should not happen!!!";