fb6486e88b60df040045760367b4cd212efdafcd
[contrib/qtwebsockets.git] / src / websockets / qwebsocket_p.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwebsocket.h"
43 #include "qwebsocket_p.h"
44 #include "qwebsocketprotocol_p.h"
45 #include "qwebsockethandshakerequest_p.h"
46 #include "qwebsockethandshakeresponse_p.h"
47 #include "qdefaultmaskgenerator_p.h"
48
49 #include <QtCore/QUrl>
50 #include <QtNetwork/QAuthenticator>
51 #include <QtNetwork/QTcpSocket>
52 #include <QtCore/QByteArray>
53 #include <QtCore/QtEndian>
54 #include <QtCore/QCryptographicHash>
55 #include <QtCore/QRegularExpression>
56 #include <QtCore/QStringList>
57 #include <QtNetwork/QHostAddress>
58 #include <QtCore/QStringBuilder>   //for more efficient string concatenation
59 #ifndef QT_NONETWORKPROXY
60 #include <QtNetwork/QNetworkProxy>
61 #endif
62 #ifndef QT_NO_SSL
63 #include <QtNetwork/QSslConfiguration>
64 #include <QtNetwork/QSslError>
65 #endif
66
67 #include <QtCore/QDebug>
68
69 #include <limits>
70
71 QT_BEGIN_NAMESPACE
72
73 const quint64 FRAME_SIZE_IN_BYTES = 512 * 512 * 2;      //maximum size of a frame when sending a message
74
75 QWebSocketConfiguration::QWebSocketConfiguration() :
76 #ifndef QT_NO_SSL
77     m_sslConfiguration(QSslConfiguration::defaultConfiguration()),
78     m_ignoredSslErrors(),
79     m_ignoreSslErrors(false),
80 #endif
81 #ifndef QT_NO_NETWORKPROXY
82     m_proxy(QNetworkProxy::DefaultProxy),
83 #endif
84     m_pSocket(Q_NULLPTR)
85 {
86 }
87
88 /*!
89     \internal
90 */
91 QWebSocketPrivate::QWebSocketPrivate(const QString &origin, QWebSocketProtocol::Version version,
92                                      QWebSocket *pWebSocket) :
93     QObjectPrivate(),
94     q_ptr(pWebSocket),
95     m_pSocket(),
96     m_errorString(),
97     m_version(version),
98     m_resourceName(),
99     m_requestUrl(),
100     m_origin(origin),
101     m_protocol(),
102     m_extension(),
103     m_socketState(QAbstractSocket::UnconnectedState),
104     m_pauseMode(QAbstractSocket::PauseNever),
105     m_readBufferSize(0),
106     m_key(),
107     m_mustMask(true),
108     m_isClosingHandshakeSent(false),
109     m_isClosingHandshakeReceived(false),
110     m_closeCode(QWebSocketProtocol::CloseCodeNormal),
111     m_closeReason(),
112     m_pingTimer(),
113     m_dataProcessor(),
114     m_configuration(),
115     m_pMaskGenerator(&m_defaultMaskGenerator),
116     m_defaultMaskGenerator()
117 {
118 }
119
120 /*!
121     \internal
122 */
123 QWebSocketPrivate::QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version,
124                                      QWebSocket *pWebSocket) :
125     QObjectPrivate(),
126     q_ptr(pWebSocket),
127     m_pSocket(pTcpSocket),
128     m_errorString(pTcpSocket->errorString()),
129     m_version(version),
130     m_resourceName(),
131     m_requestUrl(),
132     m_origin(),
133     m_protocol(),
134     m_extension(),
135     m_socketState(pTcpSocket->state()),
136     m_pauseMode(pTcpSocket->pauseMode()),
137     m_readBufferSize(pTcpSocket->readBufferSize()),
138     m_key(),
139     m_mustMask(true),
140     m_isClosingHandshakeSent(false),
141     m_isClosingHandshakeReceived(false),
142     m_closeCode(QWebSocketProtocol::CloseCodeNormal),
143     m_closeReason(),
144     m_pingTimer(),
145     m_dataProcessor(),
146     m_configuration(),
147     m_pMaskGenerator(&m_defaultMaskGenerator),
148     m_defaultMaskGenerator()
149 {
150 }
151
152 /*!
153     \internal
154 */
155 void QWebSocketPrivate::init()
156 {
157     Q_ASSERT(q_ptr);
158     Q_ASSERT(m_pMaskGenerator);
159
160     m_pMaskGenerator->seed();
161
162     if (m_pSocket) {
163         makeConnections(m_pSocket.data());
164     }
165 }
166
167 /*!
168     \internal
169 */
170 QWebSocketPrivate::~QWebSocketPrivate()
171 {
172     if (!m_pSocket)
173         return;
174     if (state() == QAbstractSocket::ConnectedState)
175         close(QWebSocketProtocol::CloseCodeGoingAway, QWebSocket::tr("Connection closed"));
176     releaseConnections(m_pSocket.data());
177 }
178
179 /*!
180     \internal
181  */
182 void QWebSocketPrivate::abort()
183 {
184     if (m_pSocket)
185         m_pSocket->abort();
186 }
187
188 /*!
189     \internal
190  */
191 QAbstractSocket::SocketError QWebSocketPrivate::error() const
192 {
193     QAbstractSocket::SocketError err = QAbstractSocket::UnknownSocketError;
194     if (Q_LIKELY(m_pSocket))
195         err = m_pSocket->error();
196     return err;
197 }
198
199 /*!
200     \internal
201  */
202 QString QWebSocketPrivate::errorString() const
203 {
204     QString errMsg;
205     if (!m_errorString.isEmpty())
206         errMsg = m_errorString;
207     else if (m_pSocket)
208         errMsg = m_pSocket->errorString();
209     return errMsg;
210 }
211
212 /*!
213     \internal
214  */
215 bool QWebSocketPrivate::flush()
216 {
217     bool result = true;
218     if (Q_LIKELY(m_pSocket))
219         result = m_pSocket->flush();
220     return result;
221 }
222
223 /*!
224     \internal
225  */
226 qint64 QWebSocketPrivate::sendTextMessage(const QString &message)
227 {
228     return doWriteFrames(message.toUtf8(), false);
229 }
230
231 /*!
232     \internal
233  */
234 qint64 QWebSocketPrivate::sendBinaryMessage(const QByteArray &data)
235 {
236     return doWriteFrames(data, true);
237 }
238
239 #ifndef QT_NO_SSL
240 /*!
241     \internal
242  */
243 void QWebSocketPrivate::setSslConfiguration(const QSslConfiguration &sslConfiguration)
244 {
245     m_configuration.m_sslConfiguration = sslConfiguration;
246 }
247
248 /*!
249     \internal
250  */
251 QSslConfiguration QWebSocketPrivate::sslConfiguration() const
252 {
253     return m_configuration.m_sslConfiguration;
254 }
255
256 /*!
257     \internal
258  */
259 void QWebSocketPrivate::ignoreSslErrors(const QList<QSslError> &errors)
260 {
261     m_configuration.m_ignoredSslErrors = errors;
262 }
263
264 /*!
265  * \internal
266  */
267 void QWebSocketPrivate::ignoreSslErrors()
268 {
269     m_configuration.m_ignoreSslErrors = true;
270     if (Q_LIKELY(m_pSocket)) {
271         QSslSocket *pSslSocket = qobject_cast<QSslSocket *>(m_pSocket.data());
272         if (Q_LIKELY(pSslSocket))
273             pSslSocket->ignoreSslErrors();
274     }
275 }
276
277 #endif
278
279 /*!
280   Called from QWebSocketServer
281   \internal
282  */
283 QWebSocket *QWebSocketPrivate::upgradeFrom(QTcpSocket *pTcpSocket,
284                                            const QWebSocketHandshakeRequest &request,
285                                            const QWebSocketHandshakeResponse &response,
286                                            QObject *parent)
287 {
288     QWebSocket *pWebSocket = new QWebSocket(pTcpSocket, response.acceptedVersion(), parent);
289     if (Q_LIKELY(pWebSocket)) {
290         pWebSocket->d_func()->setExtension(response.acceptedExtension());
291         pWebSocket->d_func()->setOrigin(request.origin());
292         pWebSocket->d_func()->setRequestUrl(request.requestUrl());
293         pWebSocket->d_func()->setProtocol(response.acceptedProtocol());
294         pWebSocket->d_func()->setResourceName(request.requestUrl().toString(QUrl::RemoveUserInfo));
295         //a server should not send masked frames
296         pWebSocket->d_func()->enableMasking(false);
297     }
298
299     return pWebSocket;
300 }
301
302 /*!
303     \internal
304  */
305 void QWebSocketPrivate::close(QWebSocketProtocol::CloseCode closeCode, QString reason)
306 {
307     if (Q_UNLIKELY(!m_pSocket))
308         return;
309     if (!m_isClosingHandshakeSent) {
310         Q_Q(QWebSocket);
311         const quint16 code = qToBigEndian<quint16>(closeCode);
312         QByteArray payload;
313         payload.append(static_cast<const char *>(static_cast<const void *>(&code)), 2);
314         if (!reason.isEmpty())
315             payload.append(reason.toUtf8());
316         quint32 maskingKey = 0;
317         if (m_mustMask) {
318             maskingKey = generateMaskingKey();
319             QWebSocketProtocol::mask(payload.data(), payload.size(), maskingKey);
320         }
321         QByteArray frame = getFrameHeader(QWebSocketProtocol::OpCodeClose,
322                                           payload.size(), maskingKey, true);
323         frame.append(payload);
324         m_pSocket->write(frame);
325         m_pSocket->flush();
326
327         m_isClosingHandshakeSent = true;
328
329         Q_EMIT q->aboutToClose();
330     }
331     m_pSocket->close();
332 }
333
334 /*!
335     \internal
336  */
337 void QWebSocketPrivate::open(const QUrl &url, bool mask)
338 {
339     //just delete the old socket for the moment;
340     //later, we can add more 'intelligent' handling by looking at the URL
341     //m_pSocket.reset();
342     Q_Q(QWebSocket);
343     if (!url.isValid() || url.toString().contains(QStringLiteral("\r\n"))) {
344         setErrorString(QWebSocket::tr("Invalid URL."));
345         Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
346         return;
347     }
348     QTcpSocket *pTcpSocket = m_pSocket.take();
349     if (pTcpSocket) {
350         releaseConnections(pTcpSocket);
351         pTcpSocket->deleteLater();
352     }
353     //if (m_url != url)
354     if (Q_LIKELY(!m_pSocket)) {
355         m_dataProcessor.clear();
356         m_isClosingHandshakeReceived = false;
357         m_isClosingHandshakeSent = false;
358
359         setRequestUrl(url);
360         QString resourceName = url.path();
361         if (resourceName.contains(QStringLiteral("\r\n"))) {
362             setRequestUrl(QUrl());  //clear requestUrl
363             setErrorString(QWebSocket::tr("Invalid resource name."));
364             Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
365             return;
366         }
367         if (!url.query().isEmpty()) {
368             if (!resourceName.endsWith(QChar::fromLatin1('?'))) {
369                 resourceName.append(QChar::fromLatin1('?'));
370             }
371             resourceName.append(url.query());
372         }
373         if (resourceName.isEmpty())
374             resourceName = QStringLiteral("/");
375         setResourceName(resourceName);
376         enableMasking(mask);
377
378     #ifndef QT_NO_SSL
379         if (url.scheme() == QStringLiteral("wss")) {
380             if (!QSslSocket::supportsSsl()) {
381                 const QString message =
382                         QWebSocket::tr("SSL Sockets are not supported on this platform.");
383                 setErrorString(message);
384                 Q_EMIT q->error(QAbstractSocket::UnsupportedSocketOperationError);
385             } else {
386                 QSslSocket *sslSocket = new QSslSocket;
387                 m_pSocket.reset(sslSocket);
388                 if (Q_LIKELY(m_pSocket)) {
389                     m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
390                     m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
391                     m_pSocket->setReadBufferSize(m_readBufferSize);
392                     m_pSocket->setPauseMode(m_pauseMode);
393
394                     makeConnections(m_pSocket.data());
395                     QObject::connect(sslSocket, &QSslSocket::encryptedBytesWritten, q,
396                                      &QWebSocket::bytesWritten);
397                     typedef void (QSslSocket:: *sslErrorSignalType)(const QList<QSslError> &);
398                     QObject::connect(sslSocket,
399                                      static_cast<sslErrorSignalType>(&QSslSocket::sslErrors),
400                                      q, &QWebSocket::sslErrors);
401                     setSocketState(QAbstractSocket::ConnectingState);
402
403                     sslSocket->setSslConfiguration(m_configuration.m_sslConfiguration);
404                     if (Q_UNLIKELY(m_configuration.m_ignoreSslErrors))
405                         sslSocket->ignoreSslErrors();
406                     else
407                         sslSocket->ignoreSslErrors(m_configuration.m_ignoredSslErrors);
408     #ifndef QT_NO_NETWORKPROXY
409                     sslSocket->setProxy(m_configuration.m_proxy);
410     #endif
411                     sslSocket->connectToHostEncrypted(url.host(), url.port(443));
412                 } else {
413                     const QString message = QWebSocket::tr("Out of memory.");
414                     setErrorString(message);
415                     Q_EMIT q->error(QAbstractSocket::SocketResourceError);
416                 }
417             }
418         } else
419     #endif
420         if (url.scheme() == QStringLiteral("ws")) {
421             m_pSocket.reset(new QTcpSocket);
422             if (Q_LIKELY(m_pSocket)) {
423                 m_pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
424                 m_pSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
425                 m_pSocket->setReadBufferSize(m_readBufferSize);
426                 m_pSocket->setPauseMode(m_pauseMode);
427
428                 makeConnections(m_pSocket.data());
429                 QObject::connect(m_pSocket.data(), &QAbstractSocket::bytesWritten, q,
430                                  &QWebSocket::bytesWritten);
431                 setSocketState(QAbstractSocket::ConnectingState);
432     #ifndef QT_NO_NETWORKPROXY
433                 m_pSocket->setProxy(m_configuration.m_proxy);
434     #endif
435                 m_pSocket->connectToHost(url.host(), url.port(80));
436             } else {
437                 const QString message = QWebSocket::tr("Out of memory.");
438                 setErrorString(message);
439                 Q_EMIT q->error(QAbstractSocket::SocketResourceError);
440             }
441         } else {
442             const QString message =
443                     QWebSocket::tr("Unsupported websockets scheme: %1").arg(url.scheme());
444             setErrorString(message);
445             Q_EMIT q->error(QAbstractSocket::UnsupportedSocketOperationError);
446         }
447     }
448 }
449
450 /*!
451     \internal
452  */
453 void QWebSocketPrivate::ping(const QByteArray &payload)
454 {
455     QByteArray payloadTruncated = payload.left(125);
456     m_pingTimer.restart();
457     QByteArray pingFrame = getFrameHeader(QWebSocketProtocol::OpCodePing, payloadTruncated.size(),
458                                           0 /*do not mask*/, true);
459     pingFrame.append(payloadTruncated);
460     qint64 ret = writeFrame(pingFrame);
461     Q_UNUSED(ret);
462 }
463
464 /*!
465   \internal
466     Sets the version to use for the websocket protocol;
467     this must be set before the socket is opened.
468 */
469 void QWebSocketPrivate::setVersion(QWebSocketProtocol::Version version)
470 {
471     if (m_version != version)
472         m_version = version;
473 }
474
475 /*!
476     \internal
477     Sets the resource name of the connection; must be set before the socket is openend
478 */
479 void QWebSocketPrivate::setResourceName(const QString &resourceName)
480 {
481     if (m_resourceName != resourceName)
482         m_resourceName = resourceName;
483 }
484
485 /*!
486   \internal
487  */
488 void QWebSocketPrivate::setRequestUrl(const QUrl &requestUrl)
489 {
490     if (m_requestUrl != requestUrl)
491         m_requestUrl = requestUrl;
492 }
493
494 /*!
495   \internal
496  */
497 void QWebSocketPrivate::setOrigin(const QString &origin)
498 {
499     if (m_origin != origin)
500         m_origin = origin;
501 }
502
503 /*!
504   \internal
505  */
506 void QWebSocketPrivate::setProtocol(const QString &protocol)
507 {
508     if (m_protocol != protocol)
509         m_protocol = protocol;
510 }
511
512 /*!
513   \internal
514  */
515 void QWebSocketPrivate::setExtension(const QString &extension)
516 {
517     if (m_extension != extension)
518         m_extension = extension;
519 }
520
521 /*!
522   \internal
523  */
524 void QWebSocketPrivate::enableMasking(bool enable)
525 {
526     if (m_mustMask != enable)
527         m_mustMask = enable;
528 }
529
530 /*!
531  * \internal
532  */
533 void QWebSocketPrivate::makeConnections(const QTcpSocket *pTcpSocket)
534 {
535     Q_ASSERT(pTcpSocket);
536     Q_Q(QWebSocket);
537
538     if (Q_LIKELY(pTcpSocket)) {
539         //pass through signals
540         typedef void (QAbstractSocket:: *ASErrorSignal)(QAbstractSocket::SocketError);
541         typedef void (QWebSocket:: *WSErrorSignal)(QAbstractSocket::SocketError);
542         QObject::connect(pTcpSocket,
543                          static_cast<ASErrorSignal>(&QAbstractSocket::error),
544                          q, static_cast<WSErrorSignal>(&QWebSocket::error));
545 #ifndef QT_NO_NETWORKPROXY
546         QObject::connect(pTcpSocket, &QAbstractSocket::proxyAuthenticationRequired, q,
547                          &QWebSocket::proxyAuthenticationRequired);
548 #endif
549         QObject::connect(pTcpSocket, &QAbstractSocket::readChannelFinished, q,
550                          &QWebSocket::readChannelFinished);
551         QObject::connect(pTcpSocket, &QAbstractSocket::aboutToClose, q, &QWebSocket::aboutToClose);
552
553         //catch signals
554         QObjectPrivate::connect(pTcpSocket, &QAbstractSocket::stateChanged, this,
555                                 &QWebSocketPrivate::processStateChanged);
556         //!!!important to use a QueuedConnection here;
557         //with QTcpSocket there is no problem, but with QSslSocket the processing hangs
558         QObjectPrivate::connect(pTcpSocket, &QAbstractSocket::readyRead, this,
559                                 &QWebSocketPrivate::processData, Qt::QueuedConnection);
560     }
561
562     QObject::connect(&m_dataProcessor, &QWebSocketDataProcessor::textFrameReceived, q,
563                      &QWebSocket::textFrameReceived);
564     QObject::connect(&m_dataProcessor, &QWebSocketDataProcessor::binaryFrameReceived, q,
565                      &QWebSocket::binaryFrameReceived);
566     QObject::connect(&m_dataProcessor, &QWebSocketDataProcessor::binaryMessageReceived, q,
567                      &QWebSocket::binaryMessageReceived);
568     QObject::connect(&m_dataProcessor, &QWebSocketDataProcessor::textMessageReceived, q,
569                      &QWebSocket::textMessageReceived);
570     QObjectPrivate::connect(&m_dataProcessor, &QWebSocketDataProcessor::errorEncountered, this,
571                             &QWebSocketPrivate::close);
572     QObjectPrivate::connect(&m_dataProcessor, &QWebSocketDataProcessor::pingReceived, this,
573                             &QWebSocketPrivate::processPing);
574     QObjectPrivate::connect(&m_dataProcessor, &QWebSocketDataProcessor::pongReceived, this,
575                             &QWebSocketPrivate::processPong);
576     QObjectPrivate::connect(&m_dataProcessor, &QWebSocketDataProcessor::closeReceived, this,
577                             &QWebSocketPrivate::processClose);
578 }
579
580 /*!
581  * \internal
582  */
583 void QWebSocketPrivate::releaseConnections(const QTcpSocket *pTcpSocket)
584 {
585     if (Q_LIKELY(pTcpSocket))
586         pTcpSocket->disconnect(pTcpSocket);
587     m_dataProcessor.disconnect();
588 }
589
590 /*!
591     \internal
592  */
593 QWebSocketProtocol::Version QWebSocketPrivate::version() const
594 {
595     return m_version;
596 }
597
598 /*!
599     \internal
600  */
601 QString QWebSocketPrivate::resourceName() const
602 {
603     return m_resourceName;
604 }
605
606 /*!
607     \internal
608  */
609 QUrl QWebSocketPrivate::requestUrl() const
610 {
611     return m_requestUrl;
612 }
613
614 /*!
615     \internal
616  */
617 QString QWebSocketPrivate::origin() const
618 {
619     return m_origin;
620 }
621
622 /*!
623     \internal
624  */
625 QString QWebSocketPrivate::protocol() const
626 {
627     return m_protocol;
628 }
629
630 /*!
631     \internal
632  */
633 QString QWebSocketPrivate::extension() const
634 {
635     return m_extension;
636 }
637
638 /*!
639  * \internal
640  */
641 QWebSocketProtocol::CloseCode QWebSocketPrivate::closeCode() const
642 {
643     return m_closeCode;
644 }
645
646 /*!
647  * \internal
648  */
649 QString QWebSocketPrivate::closeReason() const
650 {
651     return m_closeReason;
652 }
653
654 /*!
655  * \internal
656  */
657 QByteArray QWebSocketPrivate::getFrameHeader(QWebSocketProtocol::OpCode opCode,
658                                              quint64 payloadLength, quint32 maskingKey,
659                                              bool lastFrame)
660 {
661     QByteArray header;
662     quint8 byte = 0x00;
663     bool ok = payloadLength <= 0x7FFFFFFFFFFFFFFFULL;
664
665     if (Q_LIKELY(ok)) {
666         //FIN, RSV1-3, opcode (RSV-1, RSV-2 and RSV-3 are zero)
667         byte = static_cast<quint8>((opCode & 0x0F) | (lastFrame ? 0x80 : 0x00));
668         header.append(static_cast<char>(byte));
669
670         byte = 0x00;
671         if (maskingKey != 0)
672             byte |= 0x80;
673         if (payloadLength <= 125) {
674             byte |= static_cast<quint8>(payloadLength);
675             header.append(static_cast<char>(byte));
676         } else if (payloadLength <= 0xFFFFU) {
677             byte |= 126;
678             header.append(static_cast<char>(byte));
679             quint16 swapped = qToBigEndian<quint16>(static_cast<quint16>(payloadLength));
680             header.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 2);
681         } else if (payloadLength <= 0x7FFFFFFFFFFFFFFFULL) {
682             byte |= 127;
683             header.append(static_cast<char>(byte));
684             quint64 swapped = qToBigEndian<quint64>(payloadLength);
685             header.append(static_cast<const char *>(static_cast<const void *>(&swapped)), 8);
686         }
687
688         if (maskingKey != 0) {
689             const quint32 mask = qToBigEndian<quint32>(maskingKey);
690             header.append(static_cast<const char *>(static_cast<const void *>(&mask)),
691                           sizeof(quint32));
692         }
693     } else {
694         setErrorString(QStringLiteral("WebSocket::getHeader: payload too big!"));
695         Q_EMIT q_ptr->error(QAbstractSocket::DatagramTooLargeError);
696     }
697
698     return header;
699 }
700
701 /*!
702  * \internal
703  */
704 qint64 QWebSocketPrivate::doWriteFrames(const QByteArray &data, bool isBinary)
705 {
706     qint64 payloadWritten = 0;
707     if (Q_UNLIKELY(!m_pSocket) || (state() != QAbstractSocket::ConnectedState))
708         return payloadWritten;
709
710     Q_Q(QWebSocket);
711     const QWebSocketProtocol::OpCode firstOpCode = isBinary ?
712                 QWebSocketProtocol::OpCodeBinary : QWebSocketProtocol::OpCodeText;
713
714     int numFrames = data.size() / FRAME_SIZE_IN_BYTES;
715     QByteArray tmpData(data);
716     tmpData.detach();
717     char *payload = tmpData.data();
718     quint64 sizeLeft = quint64(data.size()) % FRAME_SIZE_IN_BYTES;
719     if (Q_LIKELY(sizeLeft))
720         ++numFrames;
721
722     //catch the case where the payload is zero bytes;
723     //in this case, we still need to send a frame
724     if (Q_UNLIKELY(numFrames == 0))
725         numFrames = 1;
726     quint64 currentPosition = 0;
727     qint64 bytesWritten = 0;
728     quint64 bytesLeft = data.size();
729
730     for (int i = 0; i < numFrames; ++i) {
731         quint32 maskingKey = 0;
732         if (m_mustMask)
733             maskingKey = generateMaskingKey();
734
735         const bool isLastFrame = (i == (numFrames - 1));
736         const bool isFirstFrame = (i == 0);
737
738         const quint64 size = qMin(bytesLeft, FRAME_SIZE_IN_BYTES);
739         const QWebSocketProtocol::OpCode opcode = isFirstFrame ? firstOpCode
740                                                                : QWebSocketProtocol::OpCodeContinue;
741
742         //write header
743         bytesWritten += m_pSocket->write(getFrameHeader(opcode, size, maskingKey, isLastFrame));
744
745         //write payload
746         if (Q_LIKELY(size > 0)) {
747             char *currentData = payload + currentPosition;
748             if (m_mustMask)
749                 QWebSocketProtocol::mask(currentData, size, maskingKey);
750             qint64 written = m_pSocket->write(currentData, static_cast<qint64>(size));
751             if (Q_LIKELY(written > 0)) {
752                 bytesWritten += written;
753                 payloadWritten += written;
754             } else {
755                 m_pSocket->flush();
756                 setErrorString(QWebSocket::tr("Error writing bytes to socket: %1.")
757                                .arg(m_pSocket->errorString()));
758                 Q_EMIT q->error(QAbstractSocket::NetworkError);
759                 break;
760             }
761         }
762         currentPosition += size;
763         bytesLeft -= size;
764     }
765     if (Q_UNLIKELY(payloadWritten != data.size())) {
766         setErrorString(QWebSocket::tr("Bytes written %1 != %2.")
767                        .arg(payloadWritten).arg(data.size()));
768         Q_EMIT q->error(QAbstractSocket::NetworkError);
769     }
770     return payloadWritten;
771 }
772
773 /*!
774     \internal
775  */
776 quint32 QWebSocketPrivate::generateMaskingKey() const
777 {
778     return m_pMaskGenerator->nextMask();
779 }
780
781 /*!
782     \internal
783  */
784 QByteArray QWebSocketPrivate::generateKey() const
785 {
786     QByteArray key;
787
788     for (int i = 0; i < 4; ++i) {
789         const quint32 tmp = m_pMaskGenerator->nextMask();
790         key.append(static_cast<const char *>(static_cast<const void *>(&tmp)), sizeof(quint32));
791     }
792
793     return key.toBase64();
794 }
795
796
797 /*!
798     \internal
799  */
800 QString QWebSocketPrivate::calculateAcceptKey(const QByteArray &key) const
801 {
802     const QByteArray tmpKey = key + QByteArrayLiteral("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
803     const QByteArray hash = QCryptographicHash::hash(tmpKey, QCryptographicHash::Sha1).toBase64();
804     return QString::fromLatin1(hash);
805 }
806
807 /*!
808     \internal
809  */
810 qint64 QWebSocketPrivate::writeFrames(const QList<QByteArray> &frames)
811 {
812     qint64 written = 0;
813     if (Q_LIKELY(m_pSocket)) {
814         QList<QByteArray>::const_iterator it;
815         for (it = frames.cbegin(); it < frames.cend(); ++it)
816             written += writeFrame(*it);
817     }
818     return written;
819 }
820
821 /*!
822     \internal
823  */
824 qint64 QWebSocketPrivate::writeFrame(const QByteArray &frame)
825 {
826     qint64 written = 0;
827     if (Q_LIKELY(m_pSocket))
828         written = m_pSocket->write(frame);
829     return written;
830 }
831
832 /*!
833     \internal
834  */
835 QString readLine(QTcpSocket *pSocket)
836 {
837     Q_ASSERT(pSocket);
838     QString line;
839     char c;
840     while (pSocket->getChar(&c)) {
841         if (c == char('\r')) {
842             pSocket->getChar(&c);
843             break;
844         } else {
845             line.append(QChar::fromLatin1(c));
846         }
847     }
848     return line;
849 }
850
851 //called on the client for a server handshake response
852 /*!
853     \internal
854  */
855 void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket)
856 {
857     Q_Q(QWebSocket);
858     if (Q_UNLIKELY(!pSocket))
859         return;
860
861     bool ok = false;
862     QString errorDescription;
863
864     const QString regExpStatusLine(QStringLiteral("^(HTTP/[0-9]+\\.[0-9]+)\\s([0-9]+)\\s(.*)"));
865     const QRegularExpression regExp(regExpStatusLine);
866     const QString statusLine = readLine(pSocket);
867     QString httpProtocol;
868     int httpStatusCode;
869     QString httpStatusMessage;
870     const QRegularExpressionMatch match = regExp.match(statusLine);
871     if (Q_LIKELY(match.hasMatch())) {
872         QStringList tokens = match.capturedTexts();
873         tokens.removeFirst();   //remove the search string
874         if (tokens.length() == 3) {
875             httpProtocol = tokens[0];
876             httpStatusCode = tokens[1].toInt();
877             httpStatusMessage = tokens[2].trimmed();
878             ok = true;
879         }
880     }
881     if (Q_UNLIKELY(!ok)) {
882         errorDescription = QWebSocket::tr("Invalid statusline in response: %1.").arg(statusLine);
883     } else {
884         QString headerLine = readLine(pSocket);
885         QMap<QString, QString> headers;
886         while (!headerLine.isEmpty()) {
887             const QStringList headerField = headerLine.split(QStringLiteral(": "),
888                                                              QString::SkipEmptyParts);
889             if (headerField.size() == 2) {
890                 headers.insertMulti(headerField[0], headerField[1]);
891             }
892             headerLine = readLine(pSocket);
893         }
894
895         const QString acceptKey = headers.value(QStringLiteral("Sec-WebSocket-Accept"),
896                                                 QString());
897         const QString upgrade = headers.value(QStringLiteral("Upgrade"), QString());
898         const QString connection = headers.value(QStringLiteral("Connection"), QString());
899 //        unused for the moment
900 //        const QString extensions = headers.value(QStringLiteral("Sec-WebSocket-Extensions"),
901 //                                                 QString());
902 //        const QString protocol = headers.value(QStringLiteral("Sec-WebSocket-Protocol"),
903 //                                               QString());
904         const QString version = headers.value(QStringLiteral("Sec-WebSocket-Version"),
905                                               QString());
906
907         if (Q_LIKELY(httpStatusCode == 101)) {
908             //HTTP/x.y 101 Switching Protocols
909             bool conversionOk = false;
910             const float version = httpProtocol.midRef(5).toFloat(&conversionOk);
911             //TODO: do not check the httpStatusText right now
912             ok = !(acceptKey.isEmpty() ||
913                    (!conversionOk || (version < 1.1f)) ||
914                    (upgrade.toLower() != QStringLiteral("websocket")) ||
915                    (connection.toLower() != QStringLiteral("upgrade")));
916             if (ok) {
917                 const QString accept = calculateAcceptKey(m_key);
918                 ok = (accept == acceptKey);
919                 if (!ok)
920                     errorDescription =
921                       QWebSocket::tr("Accept-Key received from server %1 does not match the client key %2.")
922                             .arg(acceptKey).arg(accept);
923             } else {
924                 errorDescription =
925                     QWebSocket::tr("QWebSocketPrivate::processHandshake: Invalid statusline in response: %1.")
926                         .arg(statusLine);
927             }
928         } else if (httpStatusCode == 400) {
929             //HTTP/1.1 400 Bad Request
930             if (!version.isEmpty()) {
931                 const QStringList versions = version.split(QStringLiteral(", "),
932                                                            QString::SkipEmptyParts);
933                 if (!versions.contains(QString::number(QWebSocketProtocol::currentVersion()))) {
934                     //if needed to switch protocol version, then we are finished here
935                     //because we cannot handle other protocols than the RFC one (v13)
936                     errorDescription =
937                             QWebSocket::tr("Handshake: Server requests a version that we don't support: %1.")
938                             .arg(versions.join(QStringLiteral(", ")));
939                     ok = false;
940                 } else {
941                     //we tried v13, but something different went wrong
942                     errorDescription =
943                         QWebSocket::tr("QWebSocketPrivate::processHandshake: Unknown error condition encountered. Aborting connection.");
944                     ok = false;
945                 }
946             }
947         } else {
948             errorDescription =
949                     QWebSocket::tr("QWebSocketPrivate::processHandshake: Unhandled http status code: %1 (%2).")
950                         .arg(httpStatusCode).arg(httpStatusMessage);
951             ok = false;
952         }
953
954         if (!ok) {
955             setErrorString(errorDescription);
956             Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
957         } else {
958             //handshake succeeded
959             setSocketState(QAbstractSocket::ConnectedState);
960             Q_EMIT q->connected();
961         }
962     }
963 }
964
965 /*!
966     \internal
967  */
968 void QWebSocketPrivate::processStateChanged(QAbstractSocket::SocketState socketState)
969 {
970     Q_ASSERT(m_pSocket);
971     Q_Q(QWebSocket);
972     QAbstractSocket::SocketState webSocketState = this->state();
973     switch (socketState) {
974     case QAbstractSocket::ConnectedState:
975         if (webSocketState == QAbstractSocket::ConnectingState) {
976             m_key = generateKey();
977             const QString handshake =
978                     createHandShakeRequest(m_resourceName,
979                                            m_requestUrl.host()
980                                                 % QStringLiteral(":")
981                                                 % QString::number(m_requestUrl.port(80)),
982                                            origin(),
983                                            QString(),
984                                            QString(),
985                                            m_key);
986             if (handshake.isEmpty()) {
987                 m_pSocket->abort();
988                 Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
989                 return;
990             }
991             m_pSocket->write(handshake.toLatin1());
992         }
993         break;
994
995     case QAbstractSocket::ClosingState:
996         if (webSocketState == QAbstractSocket::ConnectedState)
997             setSocketState(QAbstractSocket::ClosingState);
998         break;
999
1000     case QAbstractSocket::UnconnectedState:
1001         if (webSocketState != QAbstractSocket::UnconnectedState) {
1002             setSocketState(QAbstractSocket::UnconnectedState);
1003             Q_EMIT q->disconnected();
1004         }
1005         break;
1006
1007     case QAbstractSocket::HostLookupState:
1008     case QAbstractSocket::ConnectingState:
1009     case QAbstractSocket::BoundState:
1010     case QAbstractSocket::ListeningState:
1011         //do nothing
1012         //to make C++ compiler happy;
1013         break;
1014     default:
1015         break;
1016     }
1017 }
1018
1019 /*!
1020  \internal
1021  */
1022 void QWebSocketPrivate::processData()
1023 {
1024     Q_ASSERT(m_pSocket);
1025     while (m_pSocket->bytesAvailable()) {
1026         if (state() == QAbstractSocket::ConnectingState)
1027             processHandshake(m_pSocket.data());
1028         else
1029             m_dataProcessor.process(m_pSocket.data());
1030     }
1031 }
1032
1033 /*!
1034  \internal
1035  */
1036 void QWebSocketPrivate::processPing(const QByteArray &data)
1037 {
1038     Q_ASSERT(m_pSocket);
1039     quint32 maskingKey = 0;
1040     if (m_mustMask)
1041         maskingKey = generateMaskingKey();
1042     m_pSocket->write(getFrameHeader(QWebSocketProtocol::OpCodePong, data.size(), maskingKey, true));
1043     if (data.size() > 0) {
1044         QByteArray maskedData = data;
1045         if (m_mustMask)
1046             QWebSocketProtocol::mask(&maskedData, maskingKey);
1047         m_pSocket->write(maskedData);
1048     }
1049 }
1050
1051 /*!
1052  \internal
1053  */
1054 void QWebSocketPrivate::processPong(const QByteArray &data)
1055 {
1056     Q_Q(QWebSocket);
1057     Q_EMIT q->pong(static_cast<quint64>(m_pingTimer.elapsed()), data);
1058 }
1059
1060 /*!
1061  \internal
1062  */
1063 void QWebSocketPrivate::processClose(QWebSocketProtocol::CloseCode closeCode, QString closeReason)
1064 {
1065     m_isClosingHandshakeReceived = true;
1066     close(closeCode, closeReason);
1067 }
1068
1069 /*!
1070     \internal
1071  */
1072 QString QWebSocketPrivate::createHandShakeRequest(QString resourceName,
1073                                                   QString host,
1074                                                   QString origin,
1075                                                   QString extensions,
1076                                                   QString protocols,
1077                                                   QByteArray key)
1078 {
1079     QStringList handshakeRequest;
1080     if (resourceName.contains(QStringLiteral("\r\n"))) {
1081         setErrorString(QWebSocket::tr("The resource name contains newlines. " \
1082                                       "Possible attack detected."));
1083         return QString();
1084     }
1085     if (host.contains(QStringLiteral("\r\n"))) {
1086         setErrorString(QWebSocket::tr("The hostname contains newlines. " \
1087                                       "Possible attack detected."));
1088         return QString();
1089     }
1090     if (origin.contains(QStringLiteral("\r\n"))) {
1091         setErrorString(QWebSocket::tr("The origin contains newlines. " \
1092                                       "Possible attack detected."));
1093         return QString();
1094     }
1095     if (extensions.contains(QStringLiteral("\r\n"))) {
1096         setErrorString(QWebSocket::tr("The extensions attribute contains newlines. " \
1097                                       "Possible attack detected."));
1098         return QString();
1099     }
1100     if (protocols.contains(QStringLiteral("\r\n"))) {
1101         setErrorString(QWebSocket::tr("The protocols attribute contains newlines. " \
1102                                       "Possible attack detected."));
1103         return QString();
1104     }
1105
1106     handshakeRequest << QStringLiteral("GET ") % resourceName % QStringLiteral(" HTTP/1.1") <<
1107                         QStringLiteral("Host: ") % host <<
1108                         QStringLiteral("Upgrade: websocket") <<
1109                         QStringLiteral("Connection: Upgrade") <<
1110                         QStringLiteral("Sec-WebSocket-Key: ") % QString::fromLatin1(key);
1111     if (!origin.isEmpty())
1112         handshakeRequest << QStringLiteral("Origin: ") % origin;
1113     handshakeRequest << QStringLiteral("Sec-WebSocket-Version: ")
1114                             % QString::number(QWebSocketProtocol::currentVersion());
1115     if (extensions.length() > 0)
1116         handshakeRequest << QStringLiteral("Sec-WebSocket-Extensions: ") % extensions;
1117     if (protocols.length() > 0)
1118         handshakeRequest << QStringLiteral("Sec-WebSocket-Protocol: ") % protocols;
1119     handshakeRequest << QStringLiteral("\r\n");
1120
1121     return handshakeRequest.join(QStringLiteral("\r\n"));
1122 }
1123
1124 /*!
1125     \internal
1126  */
1127 QAbstractSocket::SocketState QWebSocketPrivate::state() const
1128 {
1129     return m_socketState;
1130 }
1131
1132 /*!
1133     \internal
1134  */
1135 void QWebSocketPrivate::setSocketState(QAbstractSocket::SocketState state)
1136 {
1137     Q_Q(QWebSocket);
1138     if (m_socketState != state) {
1139         m_socketState = state;
1140         Q_EMIT q->stateChanged(m_socketState);
1141     }
1142 }
1143
1144 /*!
1145     \internal
1146  */
1147 void QWebSocketPrivate::setErrorString(const QString &errorString)
1148 {
1149     if (m_errorString != errorString)
1150         m_errorString = errorString;
1151 }
1152
1153 /*!
1154     \internal
1155  */
1156 QHostAddress QWebSocketPrivate::localAddress() const
1157 {
1158     QHostAddress address;
1159     if (Q_LIKELY(m_pSocket))
1160         address = m_pSocket->localAddress();
1161     return address;
1162 }
1163
1164 /*!
1165     \internal
1166  */
1167 quint16 QWebSocketPrivate::localPort() const
1168 {
1169     quint16 port = 0;
1170     if (Q_LIKELY(m_pSocket))
1171         port = m_pSocket->localPort();
1172     return port;
1173 }
1174
1175 /*!
1176     \internal
1177  */
1178 QAbstractSocket::PauseModes QWebSocketPrivate::pauseMode() const
1179 {
1180     return m_pauseMode;
1181 }
1182
1183 /*!
1184     \internal
1185  */
1186 QHostAddress QWebSocketPrivate::peerAddress() const
1187 {
1188     QHostAddress address;
1189     if (Q_LIKELY(m_pSocket))
1190         address = m_pSocket->peerAddress();
1191     return address;
1192 }
1193
1194 /*!
1195     \internal
1196  */
1197 QString QWebSocketPrivate::peerName() const
1198 {
1199     QString name;
1200     if (Q_LIKELY(m_pSocket))
1201         name = m_pSocket->peerName();
1202     return name;
1203 }
1204
1205 /*!
1206     \internal
1207  */
1208 quint16 QWebSocketPrivate::peerPort() const
1209 {
1210     quint16 port = 0;
1211     if (Q_LIKELY(m_pSocket))
1212         port = m_pSocket->peerPort();
1213     return port;
1214 }
1215
1216 #ifndef QT_NO_NETWORKPROXY
1217 /*!
1218     \internal
1219  */
1220 QNetworkProxy QWebSocketPrivate::proxy() const
1221 {
1222     return m_configuration.m_proxy;
1223 }
1224
1225 /*!
1226     \internal
1227  */
1228 void QWebSocketPrivate::setProxy(const QNetworkProxy &networkProxy)
1229 {
1230     if (networkProxy != networkProxy)
1231         m_configuration.m_proxy = networkProxy;
1232 }
1233 #endif  //QT_NO_NETWORKPROXY
1234
1235 /*!
1236     \internal
1237  */
1238 void QWebSocketPrivate::setMaskGenerator(const QMaskGenerator *maskGenerator)
1239 {
1240     if (!maskGenerator)
1241         m_pMaskGenerator = &m_defaultMaskGenerator;
1242     else if (maskGenerator != m_pMaskGenerator)
1243         m_pMaskGenerator = const_cast<QMaskGenerator *>(maskGenerator);
1244 }
1245
1246 /*!
1247     \internal
1248  */
1249 const QMaskGenerator *QWebSocketPrivate::maskGenerator() const
1250 {
1251     Q_ASSERT(m_pMaskGenerator);
1252     return m_pMaskGenerator;
1253 }
1254
1255 /*!
1256     \internal
1257  */
1258 qint64 QWebSocketPrivate::readBufferSize() const
1259 {
1260     return m_readBufferSize;
1261 }
1262
1263 /*!
1264     \internal
1265  */
1266 void QWebSocketPrivate::resume()
1267 {
1268     if (Q_LIKELY(m_pSocket))
1269         m_pSocket->resume();
1270 }
1271
1272 /*!
1273   \internal
1274  */
1275 void QWebSocketPrivate::setPauseMode(QAbstractSocket::PauseModes pauseMode)
1276 {
1277     m_pauseMode = pauseMode;
1278     if (Q_LIKELY(m_pSocket))
1279         m_pSocket->setPauseMode(m_pauseMode);
1280 }
1281
1282 /*!
1283     \internal
1284  */
1285 void QWebSocketPrivate::setReadBufferSize(qint64 size)
1286 {
1287     m_readBufferSize = size;
1288     if (Q_LIKELY(m_pSocket))
1289         m_pSocket->setReadBufferSize(m_readBufferSize);
1290 }
1291
1292 /*!
1293     \internal
1294  */
1295 bool QWebSocketPrivate::isValid() const
1296 {
1297     return (m_pSocket && m_pSocket->isValid() &&
1298             (m_socketState == QAbstractSocket::ConnectedState));
1299 }
1300
1301 QT_END_NAMESPACE