| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | /*! |
| 41 | \class QWebSocket |
| 42 | |
| 43 | \inmodule QtWebSockets |
| 44 | \since 5.3 |
| 45 | \brief Implements a TCP socket that talks the WebSocket protocol. |
| 46 | |
| 47 | WebSockets is a web technology providing full-duplex communications channels over |
| 48 | a single TCP connection. |
| 49 | The WebSocket protocol was standardized by the IETF as |
| 50 | \l {RFC 6455} in 2011. |
| 51 | QWebSocket can both be used in a client application and server application. |
| 52 | |
| 53 | This class was modeled after QAbstractSocket. |
| 54 | |
| 55 | QWebSocket currently does not support |
| 56 | \l {WebSocket Extensions} and |
| 57 | \l {WebSocket Subprotocols}. |
| 58 | |
| 59 | QWebSocket only supports version 13 of the WebSocket protocol, as outlined in |
| 60 | \l {RFC 6455}. |
| 61 | |
| 62 | \note Some proxies do not understand certain HTTP headers used during a WebSocket handshake. |
| 63 | In that case, non-secure WebSocket connections fail. The best way to mitigate against |
| 64 | this problem is to use WebSocket over a secure connection. |
| 65 | |
| 66 | \warning To generate masks, this implementation of WebSockets uses the reasonably |
| 67 | secure QRandomGenerator::global()->generate() function. |
| 68 | For more information about the importance of good masking, |
| 69 | see \l {"Talking to Yourself for Fun and Profit" by Lin-Shung Huang et al}. |
| 70 | The best measure against attacks mentioned in the document above, |
| 71 | is to use QWebSocket over a secure connection (\e wss://). |
| 72 | In general, always be careful to not have 3rd party script access to |
| 73 | a QWebSocket in your application. |
| 74 | |
| 75 | \sa QAbstractSocket, QTcpSocket |
| 76 | |
| 77 | \sa {QWebSocket client example} |
| 78 | */ |
| 79 | |
| 80 | /*! |
| 81 | \page echoclient.html example |
| 82 | \title QWebSocket client example |
| 83 | \brief A sample WebSocket client that sends a message and displays the message that |
| 84 | it receives back. |
| 85 | |
| 86 | \section1 Description |
| 87 | The EchoClient example implements a WebSocket client that sends a message to a WebSocket server |
| 88 | and dumps the answer that it gets back. |
| 89 | This example should ideally be used with the EchoServer example. |
| 90 | \section1 Code |
| 91 | We start by connecting to the `connected()` signal. |
| 92 | \snippet echoclient/echoclient.cpp constructor |
| 93 | After the connection, we open the socket to the given \a url. |
| 94 | |
| 95 | \snippet echoclient/echoclient.cpp onConnected |
| 96 | When the client is connected successfully, we connect to the `onTextMessageReceived()` signal, |
| 97 | and send out "Hello, world!". |
| 98 | If connected with the EchoServer, we will receive the same message back. |
| 99 | |
| 100 | \snippet echoclient/echoclient.cpp onTextMessageReceived |
| 101 | Whenever a message is received, we write it out. |
| 102 | */ |
| 103 | |
| 104 | /*! |
| 105 | \fn void QWebSocket::connected() |
| 106 | \brief Emitted when a connection is successfully established. |
| 107 | A connection is successfully established when the socket is connected |
| 108 | and the handshake was successful. |
| 109 | \sa open(), disconnected() |
| 110 | */ |
| 111 | /*! |
| 112 | \fn void QWebSocket::disconnected() |
| 113 | \brief Emitted when the socket is disconnected. |
| 114 | \sa close(), connected() |
| 115 | */ |
| 116 | /*! |
| 117 | \fn void QWebSocket::aboutToClose() |
| 118 | |
| 119 | This signal is emitted when the socket is about to close. |
| 120 | Connect this signal if you have operations that need to be performed before the socket closes |
| 121 | (e.g., if you have data in a separate buffer that needs to be written to the device). |
| 122 | |
| 123 | \sa close() |
| 124 | */ |
| 125 | /*! |
| 126 | \fn void QWebSocket::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) |
| 127 | |
| 128 | This signal can be emitted when a \a proxy that requires |
| 129 | authentication is used. The \a authenticator object can then be |
| 130 | filled in with the required details to allow authentication and |
| 131 | continue the connection. |
| 132 | |
| 133 | \note It is not possible to use a QueuedConnection to connect to |
| 134 | this signal, as the connection will fail if the authenticator has |
| 135 | not been filled in with new information when the signal returns. |
| 136 | |
| 137 | \sa QAuthenticator, QNetworkProxy |
| 138 | */ |
| 139 | /*! |
| 140 | \fn void QWebSocket::stateChanged(QAbstractSocket::SocketState state); |
| 141 | |
| 142 | This signal is emitted whenever QWebSocket's state changes. |
| 143 | The \a state parameter is the new state. |
| 144 | |
| 145 | \note QAbstractSocket::ConnectedState is emitted after the handshake |
| 146 | with the server has succeeded. |
| 147 | |
| 148 | QAbstractSocket::SocketState is not a registered metatype, so for queued |
| 149 | connections, you will have to register it with Q_REGISTER_METATYPE() and |
| 150 | qRegisterMetaType(). |
| 151 | |
| 152 | \sa state() |
| 153 | */ |
| 154 | /*! |
| 155 | \fn void QWebSocket::readChannelFinished() |
| 156 | |
| 157 | This signal is emitted when the input (reading) stream is closed in this device. |
| 158 | It is emitted as soon as the closing is detected. |
| 159 | |
| 160 | \sa close() |
| 161 | */ |
| 162 | /*! |
| 163 | \fn void QWebSocket::bytesWritten(qint64 bytes) |
| 164 | |
| 165 | This signal is emitted every time a payload of data has been written to the socket. |
| 166 | The \a bytes argument is set to the number of bytes that were written in this payload. |
| 167 | |
| 168 | \note This signal has the same meaning both for secure and non-secure WebSockets. |
| 169 | As opposed to QSslSocket, bytesWritten() is only emitted when encrypted data is effectively |
| 170 | written (see QSslSocket::encryptedBytesWritten()). |
| 171 | \sa close() |
| 172 | */ |
| 173 | |
| 174 | /*! |
| 175 | \fn void QWebSocket::textFrameReceived(const QString &frame, bool isLastFrame); |
| 176 | |
| 177 | This signal is emitted whenever a text frame is received. The \a frame contains the data and |
| 178 | \a isLastFrame indicates whether this is the last frame of the complete message. |
| 179 | |
| 180 | This signal can be used to process large messages frame by frame, instead of waiting for the |
| 181 | complete message to arrive. |
| 182 | |
| 183 | \sa binaryFrameReceived() |
| 184 | */ |
| 185 | /*! |
| 186 | \fn void QWebSocket::binaryFrameReceived(const QByteArray &frame, bool isLastFrame); |
| 187 | |
| 188 | This signal is emitted whenever a binary frame is received. The \a frame contains the data and |
| 189 | \a isLastFrame indicates whether this is the last frame of the complete message. |
| 190 | |
| 191 | This signal can be used to process large messages frame by frame, instead of waiting for the |
| 192 | complete message to arrive. |
| 193 | |
| 194 | \sa textFrameReceived() |
| 195 | */ |
| 196 | /*! |
| 197 | \fn void QWebSocket::textMessageReceived(const QString &message); |
| 198 | |
| 199 | This signal is emitted whenever a text message is received. The \a message contains the |
| 200 | received text. |
| 201 | |
| 202 | \sa binaryMessageReceived() |
| 203 | */ |
| 204 | /*! |
| 205 | \fn void QWebSocket::binaryMessageReceived(const QByteArray &message); |
| 206 | |
| 207 | This signal is emitted whenever a binary message is received. The \a message contains the |
| 208 | received bytes. |
| 209 | |
| 210 | \sa textMessageReceived() |
| 211 | */ |
| 212 | /*! |
| 213 | \fn void QWebSocket::error(QAbstractSocket::SocketError error); |
| 214 | |
| 215 | This signal is emitted after an error occurred. The \a error |
| 216 | parameter describes the type of error that occurred. |
| 217 | |
| 218 | QAbstractSocket::SocketError is not a registered metatype, so for queued |
| 219 | connections, you will have to register it with Q_DECLARE_METATYPE() and |
| 220 | qRegisterMetaType(). |
| 221 | |
| 222 | \sa error(), errorString() |
| 223 | */ |
| 224 | /*! |
| 225 | \fn void QWebSocket::sslErrors(const QList<QSslError> &errors) |
| 226 | QWebSocket emits this signal after the SSL handshake to indicate that one or more errors have |
| 227 | occurred while establishing the identity of the peer. |
| 228 | The errors are usually an indication that QWebSocket is unable to securely identify the peer. |
| 229 | Unless any action is taken, the connection will be dropped after this signal has been emitted. |
| 230 | If you want to continue connecting despite the errors that have occurred, you must call |
| 231 | QWebSocket::ignoreSslErrors() from inside a slot connected to this signal. |
| 232 | If you need to access the error list at a later point, you can call sslErrors() |
| 233 | (without arguments). |
| 234 | |
| 235 | \a errors contains one or more errors that prevent QWebSocket from verifying the identity of |
| 236 | the peer. |
| 237 | |
| 238 | \note You cannot use Qt::QueuedConnection when connecting to this signal, or calling |
| 239 | QWebSocket::ignoreSslErrors() will have no effect. |
| 240 | */ |
| 241 | /*! |
| 242 | \fn void QWebSocket::preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator) |
| 243 | \since 5.8 |
| 244 | |
| 245 | This signal is emitted if the SSL/TLS handshake negotiates a PSK |
| 246 | ciphersuite, and therefore a PSK authentication is then required. |
| 247 | |
| 248 | When using PSK, the client must send to the server a valid identity and a |
| 249 | valid pre shared key, in order for the SSL handshake to continue. |
| 250 | Applications can provide this information in a slot connected to this |
| 251 | signal, by filling in the passed \a authenticator object according to their |
| 252 | needs. |
| 253 | |
| 254 | \note Ignoring this signal, or failing to provide the required credentials, |
| 255 | will cause the handshake to fail, and therefore the connection to be aborted. |
| 256 | |
| 257 | \note The \a authenticator object is owned by the websocket and must not be |
| 258 | deleted by the application. |
| 259 | |
| 260 | \sa QSslPreSharedKeyAuthenticator |
| 261 | \sa QSslSocket::preSharedKeyAuthenticationRequired() |
| 262 | */ |
| 263 | /*! |
| 264 | \fn void QWebSocket::pong(quint64 elapsedTime, const QByteArray &payload) |
| 265 | |
| 266 | Emitted when a pong message is received in reply to a previous ping. |
| 267 | \a elapsedTime contains the roundtrip time in milliseconds and \a payload contains an optional |
| 268 | payload that was sent with the ping. |
| 269 | |
| 270 | \sa ping() |
| 271 | */ |
| 272 | #include "qwebsocket.h" |
| 273 | #include "qwebsocket_p.h" |
| 274 | |
| 275 | #include <QtCore/QUrl> |
| 276 | #include <QtNetwork/QTcpSocket> |
| 277 | #include <QtCore/QByteArray> |
| 278 | #include <QtNetwork/QHostAddress> |
| 279 | |
| 280 | #include <QtCore/QDebug> |
| 281 | |
| 282 | #include <limits> |
| 283 | |
| 284 | QT_BEGIN_NAMESPACE |
| 285 | |
| 286 | /*! |
| 287 | * \brief Creates a new QWebSocket with the given \a origin, |
| 288 | * the \a version of the protocol to use and \a parent. |
| 289 | * |
| 290 | * The \a origin of the client is as specified in \l {RFC 6454}. |
| 291 | * (The \a origin is not required for non-web browser clients |
| 292 | * (see \l {RFC 6455})). |
| 293 | * The \a origin may not contain new line characters, otherwise the connection will be |
| 294 | * aborted immediately during the handshake phase. |
| 295 | * \note Currently only V13 (\l {RFC 6455}) is supported |
| 296 | */ |
| 297 | QWebSocket::QWebSocket(const QString &origin, |
| 298 | QWebSocketProtocol::Version version, |
| 299 | QObject *parent) : |
| 300 | QObject(*(new QWebSocketPrivate(origin, version)), parent) |
| 301 | { |
| 302 | Q_D(QWebSocket); |
| 303 | d->init(); |
| 304 | } |
| 305 | |
| 306 | /*! |
| 307 | * \brief Destroys the QWebSocket. Closes the socket if it is still open, |
| 308 | * and releases any used resources. |
| 309 | */ |
| 310 | QWebSocket::~QWebSocket() |
| 311 | { |
| 312 | Q_D(QWebSocket); |
| 313 | d->closeGoingAway(); |
| 314 | } |
| 315 | |
| 316 | /*! |
| 317 | * \brief Aborts the current socket and resets the socket. |
| 318 | * Unlike close(), this function immediately closes the socket, |
| 319 | * discarding any pending data in the write buffer. |
| 320 | */ |
| 321 | void QWebSocket::abort() |
| 322 | { |
| 323 | Q_D(QWebSocket); |
| 324 | d->abort(); |
| 325 | } |
| 326 | |
| 327 | /*! |
| 328 | * Returns the type of error that last occurred |
| 329 | * \sa errorString() |
| 330 | */ |
| 331 | QAbstractSocket::SocketError QWebSocket::error() const |
| 332 | { |
| 333 | Q_D(const QWebSocket); |
| 334 | return d->error(); |
| 335 | } |
| 336 | |
| 337 | //only called by QWebSocketPrivate::upgradeFrom |
| 338 | /*! |
| 339 | \internal |
| 340 | */ |
| 341 | QWebSocket::QWebSocket(QTcpSocket *pTcpSocket, |
| 342 | QWebSocketProtocol::Version version, QObject *parent) : |
| 343 | QObject(*(new QWebSocketPrivate(pTcpSocket, version)), parent) |
| 344 | { |
| 345 | Q_D(QWebSocket); |
| 346 | d->init(); |
| 347 | } |
| 348 | |
| 349 | /*! |
| 350 | * Returns a human-readable description of the last error that occurred |
| 351 | * |
| 352 | * \sa error() |
| 353 | */ |
| 354 | QString QWebSocket::errorString() const |
| 355 | { |
| 356 | Q_D(const QWebSocket); |
| 357 | return d->errorString(); |
| 358 | } |
| 359 | |
| 360 | /*! |
| 361 | This function writes as much as possible from the internal write buffer |
| 362 | to the underlying network socket, without blocking. |
| 363 | If any data was written, this function returns true; otherwise false is returned. |
| 364 | Call this function if you need QWebSocket to start sending buffered data immediately. |
| 365 | The number of bytes successfully written depends on the operating system. |
| 366 | In most cases, you do not need to call this function, |
| 367 | because QWebSocket will start sending data automatically |
| 368 | once control goes back to the event loop. |
| 369 | */ |
| 370 | bool QWebSocket::flush() |
| 371 | { |
| 372 | Q_D(QWebSocket); |
| 373 | return d->flush(); |
| 374 | } |
| 375 | |
| 376 | /*! |
| 377 | \brief Sends the given \a message over the socket as a text message and |
| 378 | returns the number of bytes actually sent. |
| 379 | |
| 380 | \sa sendBinaryMessage() |
| 381 | */ |
| 382 | qint64 QWebSocket::sendTextMessage(const QString &message) |
| 383 | { |
| 384 | Q_D(QWebSocket); |
| 385 | return d->sendTextMessage(message); |
| 386 | } |
| 387 | |
| 388 | /*! |
| 389 | \brief Sends the given \a data over the socket as a binary message and |
| 390 | returns the number of bytes actually sent. |
| 391 | |
| 392 | \sa sendTextMessage() |
| 393 | */ |
| 394 | qint64 QWebSocket::sendBinaryMessage(const QByteArray &data) |
| 395 | { |
| 396 | Q_D(QWebSocket); |
| 397 | return d->sendBinaryMessage(data); |
| 398 | } |
| 399 | |
| 400 | /*! |
| 401 | \brief Gracefully closes the socket with the given \a closeCode and \a reason. |
| 402 | |
| 403 | Any data in the write buffer is flushed before the socket is closed. |
| 404 | The \a closeCode is a QWebSocketProtocol::CloseCode indicating the reason to close, and |
| 405 | \a reason describes the reason of the closure more in detail. All control |
| 406 | frames, including the Close frame, are limited to 125 bytes. Since two of |
| 407 | these are used for \a closeCode the maximum length of \a reason is 123! If |
| 408 | \a reason exceeds this limit it will be truncated. |
| 409 | */ |
| 410 | void QWebSocket::close(QWebSocketProtocol::CloseCode closeCode, const QString &reason) |
| 411 | { |
| 412 | Q_D(QWebSocket); |
| 413 | d->close(closeCode, reason); |
| 414 | } |
| 415 | |
| 416 | /*! |
| 417 | \brief Opens a WebSocket connection using the given \a url. |
| 418 | |
| 419 | If the url contains newline characters (\\r\\n), then the error signal will be emitted |
| 420 | with QAbstractSocket::ConnectionRefusedError as error type. |
| 421 | */ |
| 422 | void QWebSocket::open(const QUrl &url) |
| 423 | { |
| 424 | Q_D(QWebSocket); |
| 425 | QNetworkRequest request(url); |
| 426 | d->open(request, mask: true); |
| 427 | } |
| 428 | |
| 429 | /*! |
| 430 | \brief Opens a WebSocket connection using the given \a request. |
| 431 | \since 5.6 |
| 432 | |
| 433 | The \a request url will be used to open the WebSocket connection. |
| 434 | Headers present in the request will be sent to the server in the upgrade request, |
| 435 | together with the ones needed for the websocket handshake. |
| 436 | */ |
| 437 | void QWebSocket::open(const QNetworkRequest &request) |
| 438 | { |
| 439 | Q_D(QWebSocket); |
| 440 | d->open(request, mask: true); |
| 441 | } |
| 442 | |
| 443 | /*! |
| 444 | \brief Pings the server to indicate that the connection is still alive. |
| 445 | Additional \a payload can be sent along the ping message. |
| 446 | |
| 447 | The size of the \a payload cannot be bigger than 125. |
| 448 | If it is larger, the \a payload is clipped to 125 bytes. |
| 449 | |
| 450 | \note QWebSocket and QWebSocketServer handles ping requests internally, |
| 451 | which means they automatically send back a pong response to the peer. |
| 452 | |
| 453 | \sa pong() |
| 454 | */ |
| 455 | void QWebSocket::ping(const QByteArray &payload) |
| 456 | { |
| 457 | Q_D(QWebSocket); |
| 458 | d->ping(payload); |
| 459 | } |
| 460 | |
| 461 | #ifndef QT_NO_SSL |
| 462 | /*! |
| 463 | This slot tells QWebSocket to ignore errors during QWebSocket's |
| 464 | handshake phase and continue connecting. If you want to continue |
| 465 | with the connection even if errors occur during the handshake |
| 466 | phase, then you must call this slot, either from a slot connected |
| 467 | to sslErrors(), or before the handshake phase. If you don't call |
| 468 | this slot, either in response to errors or before the handshake, |
| 469 | the connection will be dropped after the sslErrors() signal has |
| 470 | been emitted. |
| 471 | |
| 472 | \warning Be sure to always let the user inspect the errors |
| 473 | reported by the sslErrors() signal, and only call this method |
| 474 | upon confirmation from the user that proceeding is ok. |
| 475 | If there are unexpected errors, the connection should be aborted. |
| 476 | Calling this method without inspecting the actual errors will |
| 477 | most likely pose a security risk for your application. Use it |
| 478 | with great care! |
| 479 | |
| 480 | \sa sslErrors(), QSslSocket::ignoreSslErrors(), QNetworkReply::ignoreSslErrors() |
| 481 | */ |
| 482 | void QWebSocket::ignoreSslErrors() |
| 483 | { |
| 484 | Q_D(QWebSocket); |
| 485 | d->ignoreSslErrors(); |
| 486 | } |
| 487 | |
| 488 | /*! |
| 489 | \overload |
| 490 | |
| 491 | This method tells QWebSocket to ignore the errors given in \a errors. |
| 492 | |
| 493 | Note that you can set the expected certificate in the SSL error: |
| 494 | If, for instance, you want to connect to a server that uses |
| 495 | a self-signed certificate, consider the following snippet: |
| 496 | |
| 497 | \snippet src_websockets_ssl_qwebsocket.cpp 6 |
| 498 | |
| 499 | Multiple calls to this function will replace the list of errors that |
| 500 | were passed in previous calls. |
| 501 | You can clear the list of errors you want to ignore by calling this |
| 502 | function with an empty list. |
| 503 | |
| 504 | \sa sslErrors() |
| 505 | */ |
| 506 | void QWebSocket::ignoreSslErrors(const QList<QSslError> &errors) |
| 507 | { |
| 508 | Q_D(QWebSocket); |
| 509 | d->ignoreSslErrors(errors); |
| 510 | } |
| 511 | |
| 512 | /*! |
| 513 | Sets the socket's SSL configuration to be the contents of \a sslConfiguration. |
| 514 | |
| 515 | This function sets the local certificate, the ciphers, the private key and |
| 516 | the CA certificates to those stored in \a sslConfiguration. |
| 517 | It is not possible to set the SSL-state related fields. |
| 518 | \sa sslConfiguration() |
| 519 | */ |
| 520 | void QWebSocket::setSslConfiguration(const QSslConfiguration &sslConfiguration) |
| 521 | { |
| 522 | Q_D(QWebSocket); |
| 523 | d->setSslConfiguration(sslConfiguration); |
| 524 | } |
| 525 | |
| 526 | /*! |
| 527 | Returns the socket's SSL configuration state. |
| 528 | The default SSL configuration of a socket is to use the default ciphers, |
| 529 | default CA certificates, no local private key or certificate. |
| 530 | The SSL configuration also contains fields that can change with time without notice. |
| 531 | |
| 532 | \sa setSslConfiguration() |
| 533 | */ |
| 534 | QSslConfiguration QWebSocket::sslConfiguration() const |
| 535 | { |
| 536 | Q_D(const QWebSocket); |
| 537 | return d->sslConfiguration(); |
| 538 | } |
| 539 | |
| 540 | #endif //not QT_NO_SSL |
| 541 | |
| 542 | /*! |
| 543 | \brief Returns the version the socket is currently using. |
| 544 | */ |
| 545 | QWebSocketProtocol::Version QWebSocket::version() const |
| 546 | { |
| 547 | Q_D(const QWebSocket); |
| 548 | return d->version(); |
| 549 | } |
| 550 | |
| 551 | /*! |
| 552 | \brief Returns the name of the resource currently accessed. |
| 553 | */ |
| 554 | QString QWebSocket::resourceName() const |
| 555 | { |
| 556 | Q_D(const QWebSocket); |
| 557 | return d->resourceName(); |
| 558 | } |
| 559 | |
| 560 | /*! |
| 561 | \brief Returns the url the socket is connected to or will connect to. |
| 562 | */ |
| 563 | QUrl QWebSocket::requestUrl() const |
| 564 | { |
| 565 | Q_D(const QWebSocket); |
| 566 | return d->request().url(); |
| 567 | } |
| 568 | |
| 569 | /*! |
| 570 | \brief Returns the request that was or will be used to open this socket. |
| 571 | \since 5.6 |
| 572 | */ |
| 573 | QNetworkRequest QWebSocket::request() const |
| 574 | { |
| 575 | Q_D(const QWebSocket); |
| 576 | return d->request(); |
| 577 | } |
| 578 | |
| 579 | /*! |
| 580 | \brief Returns the current origin. |
| 581 | */ |
| 582 | QString QWebSocket::origin() const |
| 583 | { |
| 584 | Q_D(const QWebSocket); |
| 585 | return d->origin(); |
| 586 | } |
| 587 | |
| 588 | /*! |
| 589 | \brief Returns the code indicating why the socket was closed. |
| 590 | \sa QWebSocketProtocol::CloseCode, closeReason() |
| 591 | */ |
| 592 | QWebSocketProtocol::CloseCode QWebSocket::closeCode() const |
| 593 | { |
| 594 | Q_D(const QWebSocket); |
| 595 | return d->closeCode(); |
| 596 | } |
| 597 | |
| 598 | /*! |
| 599 | \brief Returns the reason why the socket was closed. |
| 600 | \sa closeCode() |
| 601 | */ |
| 602 | QString QWebSocket::closeReason() const |
| 603 | { |
| 604 | Q_D(const QWebSocket); |
| 605 | return d->closeReason(); |
| 606 | } |
| 607 | |
| 608 | /*! |
| 609 | \brief Returns the current state of the socket. |
| 610 | */ |
| 611 | QAbstractSocket::SocketState QWebSocket::state() const |
| 612 | { |
| 613 | Q_D(const QWebSocket); |
| 614 | return d->state(); |
| 615 | } |
| 616 | |
| 617 | /*! |
| 618 | Returns the local address |
| 619 | */ |
| 620 | QHostAddress QWebSocket::localAddress() const |
| 621 | { |
| 622 | Q_D(const QWebSocket); |
| 623 | return d->localAddress(); |
| 624 | } |
| 625 | |
| 626 | /*! |
| 627 | Returns the local port |
| 628 | */ |
| 629 | quint16 QWebSocket::localPort() const |
| 630 | { |
| 631 | Q_D(const QWebSocket); |
| 632 | return d->localPort(); |
| 633 | } |
| 634 | |
| 635 | /*! |
| 636 | Returns the pause mode of this socket |
| 637 | */ |
| 638 | QAbstractSocket::PauseModes QWebSocket::pauseMode() const |
| 639 | { |
| 640 | Q_D(const QWebSocket); |
| 641 | return d->pauseMode(); |
| 642 | } |
| 643 | |
| 644 | /*! |
| 645 | Returns the peer address |
| 646 | */ |
| 647 | QHostAddress QWebSocket::peerAddress() const |
| 648 | { |
| 649 | Q_D(const QWebSocket); |
| 650 | return d->peerAddress(); |
| 651 | } |
| 652 | |
| 653 | /*! |
| 654 | Returns the peerName |
| 655 | */ |
| 656 | QString QWebSocket::peerName() const |
| 657 | { |
| 658 | Q_D(const QWebSocket); |
| 659 | return d->peerName(); |
| 660 | } |
| 661 | |
| 662 | /*! |
| 663 | Returns the peerport |
| 664 | */ |
| 665 | quint16 QWebSocket::peerPort() const |
| 666 | { |
| 667 | Q_D(const QWebSocket); |
| 668 | return d->peerPort(); |
| 669 | } |
| 670 | |
| 671 | #ifndef QT_NO_NETWORKPROXY |
| 672 | /*! |
| 673 | Returns the currently configured proxy |
| 674 | */ |
| 675 | QNetworkProxy QWebSocket::proxy() const |
| 676 | { |
| 677 | Q_D(const QWebSocket); |
| 678 | return d->proxy(); |
| 679 | } |
| 680 | |
| 681 | /*! |
| 682 | Sets the proxy to \a networkProxy |
| 683 | */ |
| 684 | void QWebSocket::setProxy(const QNetworkProxy &networkProxy) |
| 685 | { |
| 686 | Q_D(QWebSocket); |
| 687 | d->setProxy(networkProxy); |
| 688 | } |
| 689 | #endif |
| 690 | |
| 691 | /*! |
| 692 | Sets the generator to use for creating masks to \a maskGenerator. |
| 693 | The default QWebSocket generator can be reset by supplying a \e nullptr. |
| 694 | The mask generator can be changed at any time, even while the connection is open. |
| 695 | */ |
| 696 | void QWebSocket::setMaskGenerator(const QMaskGenerator *maskGenerator) |
| 697 | { |
| 698 | Q_D(QWebSocket); |
| 699 | d->setMaskGenerator(maskGenerator); |
| 700 | } |
| 701 | |
| 702 | /*! |
| 703 | Returns the mask generator that is currently used by this QWebSocket. |
| 704 | */ |
| 705 | const QMaskGenerator *QWebSocket::maskGenerator() const |
| 706 | { |
| 707 | Q_D(const QWebSocket); |
| 708 | return d->maskGenerator(); |
| 709 | } |
| 710 | |
| 711 | /*! |
| 712 | Returns the size in bytes of the readbuffer that is used by the socket. |
| 713 | */ |
| 714 | qint64 QWebSocket::readBufferSize() const |
| 715 | { |
| 716 | Q_D(const QWebSocket); |
| 717 | return d->readBufferSize(); |
| 718 | } |
| 719 | |
| 720 | /*! |
| 721 | Continues data transfer on the socket. This method should only be used after the socket |
| 722 | has been set to pause upon notifications and a notification has been received. |
| 723 | The only notification currently supported is sslErrors(). |
| 724 | Calling this method if the socket is not paused results in undefined behavior. |
| 725 | |
| 726 | \sa pauseMode(), setPauseMode() |
| 727 | */ |
| 728 | void QWebSocket::resume() |
| 729 | { |
| 730 | Q_D(QWebSocket); |
| 731 | d->resume(); |
| 732 | } |
| 733 | |
| 734 | /*! |
| 735 | Controls whether to pause upon receiving a notification. The \a pauseMode parameter specifies |
| 736 | the conditions in which the socket should be paused. |
| 737 | |
| 738 | The only notification currently supported is sslErrors(). |
| 739 | If set to PauseOnSslErrors, data transfer on the socket will be paused |
| 740 | and needs to be enabled explicitly again by calling resume(). |
| 741 | By default, this option is set to PauseNever. This option must be called |
| 742 | before connecting to the server, otherwise it will result in undefined behavior. |
| 743 | |
| 744 | \sa pauseMode(), resume() |
| 745 | */ |
| 746 | void QWebSocket::setPauseMode(QAbstractSocket::PauseModes pauseMode) |
| 747 | { |
| 748 | Q_D(QWebSocket); |
| 749 | d->setPauseMode(pauseMode); |
| 750 | } |
| 751 | |
| 752 | /*! |
| 753 | Sets the size of QWebSocket's internal read buffer to be \a size bytes. |
| 754 | |
| 755 | If the buffer size is limited to a certain size, QWebSocket won't buffer more than |
| 756 | this size of data. |
| 757 | Exceptionally, a buffer size of 0 means that the read buffer is unlimited and |
| 758 | all incoming data is buffered. This is the default. |
| 759 | This option is useful if you only read the data at certain points in time |
| 760 | (for example, in a real-time streaming application) or if you want to protect your socket against |
| 761 | receiving too much data, which may eventually cause your application to run out of memory. |
| 762 | |
| 763 | \sa readBufferSize() |
| 764 | */ |
| 765 | void QWebSocket::setReadBufferSize(qint64 size) |
| 766 | { |
| 767 | Q_D(QWebSocket); |
| 768 | d->setReadBufferSize(size); |
| 769 | } |
| 770 | |
| 771 | /*! |
| 772 | Returns \c true if the socket is ready for reading and writing; otherwise |
| 773 | returns \c false. |
| 774 | */ |
| 775 | bool QWebSocket::isValid() const |
| 776 | { |
| 777 | Q_D(const QWebSocket); |
| 778 | return d->isValid(); |
| 779 | } |
| 780 | |
| 781 | /*! |
| 782 | \since 5.12 |
| 783 | Returns the number of bytes that are waiting to be written. The bytes are written when control |
| 784 | goes back to the event loop or when flush() is called. |
| 785 | |
| 786 | \sa flush |
| 787 | */ |
| 788 | qint64 QWebSocket::bytesToWrite() const |
| 789 | { |
| 790 | Q_D(const QWebSocket); |
| 791 | return d->m_pSocket ? d->m_pSocket->bytesToWrite() : 0; |
| 792 | } |
| 793 | |
| 794 | /*! |
| 795 | \since 5.15 |
| 796 | Sets the maximum allowed size of an incoming websocket frame to \a maxAllowedIncomingFrameSize. |
| 797 | If an incoming frame exceeds this limit, the peer gets disconnected. |
| 798 | The accepted range is between 0 and maxIncomingFrameSize(), default is maxIncomingFrameSize(). |
| 799 | The purpose of this function is to avoid exhausting virtual memory. |
| 800 | |
| 801 | \sa maxAllowedIncomingFrameSize() |
| 802 | */ |
| 803 | void QWebSocket::setMaxAllowedIncomingFrameSize(quint64 maxAllowedIncomingFrameSize) |
| 804 | { |
| 805 | Q_D(QWebSocket); |
| 806 | d->setMaxAllowedIncomingFrameSize(maxAllowedIncomingFrameSize); |
| 807 | } |
| 808 | |
| 809 | /*! |
| 810 | \since 5.15 |
| 811 | Returns the maximum allowed size of an incoming websocket frame. |
| 812 | |
| 813 | \sa setMaxAllowedIncomingFrameSize() |
| 814 | */ |
| 815 | quint64 QWebSocket::maxAllowedIncomingFrameSize() const |
| 816 | { |
| 817 | Q_D(const QWebSocket); |
| 818 | return d->maxAllowedIncomingFrameSize(); |
| 819 | } |
| 820 | |
| 821 | /*! |
| 822 | \since 5.15 |
| 823 | Sets the maximum allowed size of an incoming websocket message to \a maxAllowedIncomingMessageSize. |
| 824 | If an incoming message exceeds this limit, the peer gets disconnected. |
| 825 | The accepted range is between 0 and maxIncomingMessageSize(), default is maxIncomingMessageSize(). |
| 826 | The purpose of this function is to avoid exhausting virtual memory. |
| 827 | |
| 828 | \sa maxAllowedIncomingMessageSize() |
| 829 | */ |
| 830 | void QWebSocket::setMaxAllowedIncomingMessageSize(quint64 maxAllowedIncomingMessageSize) |
| 831 | { |
| 832 | Q_D(QWebSocket); |
| 833 | d->setMaxAllowedIncomingMessageSize(maxAllowedIncomingMessageSize); |
| 834 | } |
| 835 | |
| 836 | /*! |
| 837 | \since 5.15 |
| 838 | Returns the maximum allowed size of an incoming websocket message. |
| 839 | |
| 840 | \sa setMaxAllowedIncomingMessageSize() |
| 841 | */ |
| 842 | quint64 QWebSocket::maxAllowedIncomingMessageSize() const |
| 843 | { |
| 844 | Q_D(const QWebSocket); |
| 845 | return d->maxAllowedIncomingMessageSize(); |
| 846 | } |
| 847 | |
| 848 | /*! |
| 849 | \since 5.15 |
| 850 | Returns the maximum supported size of an incoming websocket message for this websocket |
| 851 | implementation. |
| 852 | */ |
| 853 | quint64 QWebSocket::maxIncomingMessageSize() |
| 854 | { |
| 855 | return QWebSocketPrivate::maxIncomingMessageSize(); |
| 856 | } |
| 857 | |
| 858 | /*! |
| 859 | \since 5.15 |
| 860 | Returns the maximum supported size of an incoming websocket frame for this websocket |
| 861 | implementation. |
| 862 | */ |
| 863 | quint64 QWebSocket::maxIncomingFrameSize() |
| 864 | { |
| 865 | return QWebSocketPrivate::maxIncomingFrameSize(); |
| 866 | } |
| 867 | |
| 868 | /*! |
| 869 | \since 5.15 |
| 870 | Sets the maximum size of an outgoing websocket frame to \a outgoingFrameSize. |
| 871 | The accepted range is between 0 and maxOutgoingFrameSize(), default is 512kB. |
| 872 | The purpose of this function is to adapt to the maximum allowed frame size |
| 873 | of the receiver. |
| 874 | |
| 875 | \sa outgoingFrameSize() |
| 876 | */ |
| 877 | void QWebSocket::setOutgoingFrameSize(quint64 outgoingFrameSize) |
| 878 | { |
| 879 | Q_D(QWebSocket); |
| 880 | d->setOutgoingFrameSize(outgoingFrameSize); |
| 881 | } |
| 882 | |
| 883 | /*! |
| 884 | \since 5.15 |
| 885 | Returns the maximum size of an outgoing websocket frame. |
| 886 | |
| 887 | \sa setOutgoingFrameSize() |
| 888 | */ |
| 889 | quint64 QWebSocket::outgoingFrameSize() const |
| 890 | { |
| 891 | Q_D(const QWebSocket); |
| 892 | return d->outgoingFrameSize(); |
| 893 | } |
| 894 | |
| 895 | /*! |
| 896 | \since 5.15 |
| 897 | Returns the maximum supported size of an outgoing websocket frame for this websocket |
| 898 | implementation. |
| 899 | */ |
| 900 | quint64 QWebSocket::maxOutgoingFrameSize() |
| 901 | { |
| 902 | return QWebSocketPrivate::maxOutgoingFrameSize(); |
| 903 | } |
| 904 | |
| 905 | QT_END_NAMESPACE |
| 906 | |