| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtNetwork 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 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 41 | |
| 42 | #include "qnetworkreply.h" |
| 43 | #include "qnetworkreply_p.h" |
| 44 | #include <QtNetwork/qsslconfiguration.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | const int QNetworkReplyPrivate::progressSignalInterval = 100; |
| 49 | |
| 50 | QNetworkReplyPrivate::QNetworkReplyPrivate() |
| 51 | : readBufferMaxSize(0), |
| 52 | emitAllUploadProgressSignals(false), |
| 53 | operation(QNetworkAccessManager::UnknownOperation), |
| 54 | errorCode(QNetworkReply::NoError) |
| 55 | , isFinished(false) |
| 56 | { |
| 57 | // set the default attribute values |
| 58 | attributes.insert(QNetworkRequest::ConnectionEncryptedAttribute, false); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /*! |
| 63 | \class QNetworkReply |
| 64 | \since 4.4 |
| 65 | \brief The QNetworkReply class contains the data and headers for a request |
| 66 | sent with QNetworkAccessManager. |
| 67 | |
| 68 | \reentrant |
| 69 | \ingroup network |
| 70 | \inmodule QtNetwork |
| 71 | |
| 72 | The QNetworkReply class contains the data and meta data related to |
| 73 | a request posted with QNetworkAccessManager. Like QNetworkRequest, |
| 74 | it contains a URL and headers (both in parsed and raw form), some |
| 75 | information about the reply's state and the contents of the reply |
| 76 | itself. |
| 77 | |
| 78 | QNetworkReply is a sequential-access QIODevice, which means that |
| 79 | once data is read from the object, it no longer kept by the |
| 80 | device. It is therefore the application's responsibility to keep |
| 81 | this data if it needs to. Whenever more data is received from the |
| 82 | network and processed, the readyRead() signal is emitted. |
| 83 | |
| 84 | The downloadProgress() signal is also emitted when data is |
| 85 | received, but the number of bytes contained in it may not |
| 86 | represent the actual bytes received, if any transformation is done |
| 87 | to the contents (for example, decompressing and removing the |
| 88 | protocol overhead). |
| 89 | |
| 90 | Even though QNetworkReply is a QIODevice connected to the contents |
| 91 | of the reply, it also emits the uploadProgress() signal, which |
| 92 | indicates the progress of the upload for operations that have such |
| 93 | content. |
| 94 | |
| 95 | \note Do not delete the object in the slot connected to the |
| 96 | errorOccurred() or finished() signal. Use deleteLater(). |
| 97 | |
| 98 | \sa QNetworkRequest, QNetworkAccessManager |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | \enum QNetworkReply::NetworkError |
| 103 | |
| 104 | Indicates all possible error conditions found during the |
| 105 | processing of the request. |
| 106 | |
| 107 | \value NoError no error condition. |
| 108 | \note When the HTTP protocol returns a redirect no error will be |
| 109 | reported. You can check if there is a redirect with the |
| 110 | QNetworkRequest::RedirectionTargetAttribute attribute. |
| 111 | |
| 112 | \value ConnectionRefusedError the remote server refused the |
| 113 | connection (the server is not accepting requests) |
| 114 | |
| 115 | \value RemoteHostClosedError the remote server closed the |
| 116 | connection prematurely, before the entire reply was received and |
| 117 | processed |
| 118 | |
| 119 | \value HostNotFoundError the remote host name was not found |
| 120 | (invalid hostname) |
| 121 | |
| 122 | \value TimeoutError the connection to the remote server |
| 123 | timed out |
| 124 | |
| 125 | \value OperationCanceledError the operation was canceled via calls |
| 126 | to abort() or close() before it was finished. |
| 127 | |
| 128 | \value SslHandshakeFailedError the SSL/TLS handshake failed and the |
| 129 | encrypted channel could not be established. The sslErrors() signal |
| 130 | should have been emitted. |
| 131 | |
| 132 | \value TemporaryNetworkFailureError the connection was broken due |
| 133 | to disconnection from the network, however the system has initiated |
| 134 | roaming to another access point. The request should be resubmitted |
| 135 | and will be processed as soon as the connection is re-established. |
| 136 | |
| 137 | \value NetworkSessionFailedError the connection was broken due |
| 138 | to disconnection from the network or failure to start the network. |
| 139 | |
| 140 | \value BackgroundRequestNotAllowedError the background request |
| 141 | is not currently allowed due to platform policy. |
| 142 | |
| 143 | \value TooManyRedirectsError while following redirects, the maximum |
| 144 | limit was reached. The limit is by default set to 50 or as set by |
| 145 | QNetworkRequest::setMaxRedirectsAllowed(). |
| 146 | (This value was introduced in 5.6.) |
| 147 | |
| 148 | \value InsecureRedirectError while following redirects, the network |
| 149 | access API detected a redirect from a encrypted protocol (https) to an |
| 150 | unencrypted one (http). |
| 151 | (This value was introduced in 5.6.) |
| 152 | |
| 153 | \value ProxyConnectionRefusedError the connection to the proxy |
| 154 | server was refused (the proxy server is not accepting requests) |
| 155 | |
| 156 | \value ProxyConnectionClosedError the proxy server closed the |
| 157 | connection prematurely, before the entire reply was received and |
| 158 | processed |
| 159 | |
| 160 | \value ProxyNotFoundError the proxy host name was not |
| 161 | found (invalid proxy hostname) |
| 162 | |
| 163 | \value ProxyTimeoutError the connection to the proxy |
| 164 | timed out or the proxy did not reply in time to the request sent |
| 165 | |
| 166 | \value ProxyAuthenticationRequiredError the proxy requires |
| 167 | authentication in order to honour the request but did not accept |
| 168 | any credentials offered (if any) |
| 169 | |
| 170 | \value ContentAccessDenied the access to the remote |
| 171 | content was denied (similar to HTTP error 403) |
| 172 | |
| 173 | \value ContentOperationNotPermittedError the operation requested |
| 174 | on the remote content is not permitted |
| 175 | |
| 176 | \value ContentNotFoundError the remote content was not |
| 177 | found at the server (similar to HTTP error 404) |
| 178 | |
| 179 | \value AuthenticationRequiredError the remote server requires |
| 180 | authentication to serve the content but the credentials provided |
| 181 | were not accepted (if any) |
| 182 | |
| 183 | \value ContentReSendError the request needed to be sent |
| 184 | again, but this failed for example because the upload data |
| 185 | could not be read a second time. |
| 186 | |
| 187 | \value ContentConflictError the request could not be completed due |
| 188 | to a conflict with the current state of the resource. |
| 189 | |
| 190 | \value ContentGoneError the requested resource is no longer |
| 191 | available at the server. |
| 192 | |
| 193 | \value InternalServerError the server encountered an unexpected |
| 194 | condition which prevented it from fulfilling the request. |
| 195 | |
| 196 | \value OperationNotImplementedError the server does not support the |
| 197 | functionality required to fulfill the request. |
| 198 | |
| 199 | \value ServiceUnavailableError the server is unable to handle the |
| 200 | request at this time. |
| 201 | |
| 202 | \value ProtocolUnknownError the Network Access API cannot |
| 203 | honor the request because the protocol is not known |
| 204 | |
| 205 | \value ProtocolInvalidOperationError the requested operation is |
| 206 | invalid for this protocol |
| 207 | |
| 208 | \value UnknownNetworkError an unknown network-related |
| 209 | error was detected |
| 210 | |
| 211 | \value UnknownProxyError an unknown proxy-related error |
| 212 | was detected |
| 213 | |
| 214 | \value UnknownContentError an unknown error related to |
| 215 | the remote content was detected |
| 216 | |
| 217 | \value ProtocolFailure a breakdown in protocol was |
| 218 | detected (parsing error, invalid or unexpected responses, etc.) |
| 219 | |
| 220 | \value UnknownServerError an unknown error related to |
| 221 | the server response was detected |
| 222 | |
| 223 | \sa error() |
| 224 | \sa errorOccurred() |
| 225 | */ |
| 226 | |
| 227 | /*! |
| 228 | \fn void QNetworkReply::encrypted() |
| 229 | \since 5.1 |
| 230 | |
| 231 | This signal is emitted when an SSL/TLS session has successfully |
| 232 | completed the initial handshake. At this point, no user data |
| 233 | has been transmitted. The signal can be used to perform |
| 234 | additional checks on the certificate chain, for example to |
| 235 | notify users when the certificate for a website has changed. |
| 236 | If the reply does not match the expected criteria then it should |
| 237 | be aborted by calling QNetworkReply::abort() by a slot connected |
| 238 | to this signal. The SSL configuration in use can be inspected |
| 239 | using the QNetworkReply::sslConfiguration() method. |
| 240 | |
| 241 | Internally, QNetworkAccessManager may open multiple connections |
| 242 | to a server, in order to allow it process requests in parallel. |
| 243 | These connections may be reused, which means that the encrypted() |
| 244 | signal would not be emitted. This means that you are only |
| 245 | guaranteed to receive this signal for the first connection to a |
| 246 | site in the lifespan of the QNetworkAccessManager. |
| 247 | |
| 248 | \sa QSslSocket::encrypted() |
| 249 | \sa QNetworkAccessManager::encrypted() |
| 250 | */ |
| 251 | |
| 252 | /*! |
| 253 | \fn void QNetworkReply::sslErrors(const QList<QSslError> &errors) |
| 254 | |
| 255 | This signal is emitted if the SSL/TLS session encountered errors |
| 256 | during the set up, including certificate verification errors. The |
| 257 | \a errors parameter contains the list of errors. |
| 258 | |
| 259 | To indicate that the errors are not fatal and that the connection |
| 260 | should proceed, the ignoreSslErrors() function should be called |
| 261 | from the slot connected to this signal. If it is not called, the |
| 262 | SSL session will be torn down before any data is exchanged |
| 263 | (including the URL). |
| 264 | |
| 265 | This signal can be used to display an error message to the user |
| 266 | indicating that security may be compromised and display the |
| 267 | SSL settings (see sslConfiguration() to obtain it). If the user |
| 268 | decides to proceed after analyzing the remote certificate, the |
| 269 | slot should call ignoreSslErrors(). |
| 270 | |
| 271 | \sa QSslSocket::sslErrors(), QNetworkAccessManager::sslErrors(), |
| 272 | sslConfiguration(), ignoreSslErrors() |
| 273 | */ |
| 274 | |
| 275 | /*! |
| 276 | \fn void QNetworkReply::preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator) |
| 277 | \since 5.5 |
| 278 | |
| 279 | This signal is emitted if the SSL/TLS handshake negotiates a PSK |
| 280 | ciphersuite, and therefore a PSK authentication is then required. |
| 281 | |
| 282 | When using PSK, the client must send to the server a valid identity and a |
| 283 | valid pre shared key, in order for the SSL handshake to continue. |
| 284 | Applications can provide this information in a slot connected to this |
| 285 | signal, by filling in the passed \a authenticator object according to their |
| 286 | needs. |
| 287 | |
| 288 | \note Ignoring this signal, or failing to provide the required credentials, |
| 289 | will cause the handshake to fail, and therefore the connection to be aborted. |
| 290 | |
| 291 | \note The \a authenticator object is owned by the reply and must not be |
| 292 | deleted by the application. |
| 293 | |
| 294 | \sa QSslPreSharedKeyAuthenticator |
| 295 | */ |
| 296 | |
| 297 | /*! |
| 298 | \fn void QNetworkReply::redirected(const QUrl &url) |
| 299 | \since 5.6 |
| 300 | |
| 301 | This signal is emitted if the QNetworkRequest::ManualRedirectPolicy was |
| 302 | set in the request and the server responded with a 3xx status (specifically |
| 303 | 301, 302, 303, 305, 307 or 308 status code) with a valid url in the location |
| 304 | header, indicating a HTTP redirect. The \a url parameter contains the new |
| 305 | redirect url as returned by the server in the location header. |
| 306 | |
| 307 | \sa QNetworkRequest::RedirectPolicy |
| 308 | */ |
| 309 | |
| 310 | /*! |
| 311 | \fn void QNetworkReply::redirectAllowed() |
| 312 | \since 5.9 |
| 313 | |
| 314 | When client code handling the redirected() signal has verified the new URL, |
| 315 | it emits this signal to allow the redirect to go ahead. This protocol applies |
| 316 | to network requests whose redirects policy is set to |
| 317 | QNetworkRequest::UserVerifiedRedirectPolicy |
| 318 | |
| 319 | \sa QNetworkRequest::UserVerifiedRedirectPolicy, |
| 320 | QNetworkAccessManager::setRedirectPolicy(), |
| 321 | QNetworkRequest::RedirectPolicyAttribute |
| 322 | */ |
| 323 | |
| 324 | /*! |
| 325 | \fn void QNetworkReply::metaDataChanged() |
| 326 | |
| 327 | \omit FIXME: Update name? \endomit |
| 328 | |
| 329 | This signal is emitted whenever the metadata in this reply |
| 330 | changes. metadata is any information that is not the content |
| 331 | (data) itself, including the network headers. In the majority of |
| 332 | cases, the metadata will be known fully by the time the first |
| 333 | byte of data is received. However, it is possible to receive |
| 334 | updates of headers or other metadata during the processing of the |
| 335 | data. |
| 336 | |
| 337 | \sa header(), rawHeaderList(), rawHeader(), hasRawHeader() |
| 338 | */ |
| 339 | |
| 340 | /*! |
| 341 | \fn void QNetworkReply::finished() |
| 342 | |
| 343 | This signal is emitted when the reply has finished |
| 344 | processing. After this signal is emitted, there will be no more |
| 345 | updates to the reply's data or metadata. |
| 346 | |
| 347 | Unless close() or abort() have been called, the reply will be still be opened |
| 348 | for reading, so the data can be retrieved by calls to read() or |
| 349 | readAll(). In particular, if no calls to read() were made as a |
| 350 | result of readyRead(), a call to readAll() will retrieve the full |
| 351 | contents in a QByteArray. |
| 352 | |
| 353 | This signal is emitted in tandem with |
| 354 | QNetworkAccessManager::finished() where that signal's reply |
| 355 | parameter is this object. |
| 356 | |
| 357 | \note Do not delete the object in the slot connected to this |
| 358 | signal. Use deleteLater(). |
| 359 | |
| 360 | You can also use isFinished() to check if a QNetworkReply |
| 361 | has finished even before you receive the finished() signal. |
| 362 | |
| 363 | \sa QNetworkAccessManager::finished(), isFinished() |
| 364 | */ |
| 365 | |
| 366 | /*! |
| 367 | \fn void QNetworkReply::errorOccurred(QNetworkReply::NetworkError code) |
| 368 | \since 5.15 |
| 369 | |
| 370 | This signal is emitted when the reply detects an error in |
| 371 | processing. The finished() signal will probably follow, indicating |
| 372 | that the connection is over. |
| 373 | |
| 374 | The \a code parameter contains the code of the error that was |
| 375 | detected. Call errorString() to obtain a textual representation of |
| 376 | the error condition. |
| 377 | |
| 378 | \note Do not delete the object in the slot connected to this |
| 379 | signal. Use deleteLater(). |
| 380 | |
| 381 | \sa error(), errorString() |
| 382 | */ |
| 383 | |
| 384 | /*! |
| 385 | \fn void QNetworkReply::uploadProgress(qint64 bytesSent, qint64 bytesTotal) |
| 386 | |
| 387 | This signal is emitted to indicate the progress of the upload part |
| 388 | of this network request, if there's any. If there's no upload |
| 389 | associated with this request, this signal will not be emitted. |
| 390 | |
| 391 | The \a bytesSent |
| 392 | parameter indicates the number of bytes uploaded, while \a |
| 393 | bytesTotal indicates the total number of bytes to be uploaded. If |
| 394 | the number of bytes to be uploaded could not be determined, \a |
| 395 | bytesTotal will be -1. |
| 396 | |
| 397 | The upload is finished when \a bytesSent is equal to \a |
| 398 | bytesTotal. At that time, \a bytesTotal will not be -1. |
| 399 | |
| 400 | \sa downloadProgress() |
| 401 | */ |
| 402 | |
| 403 | /*! |
| 404 | \fn void QNetworkReply::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
| 405 | |
| 406 | This signal is emitted to indicate the progress of the download |
| 407 | part of this network request, if there's any. If there's no |
| 408 | download associated with this request, this signal will be emitted |
| 409 | once with 0 as the value of both \a bytesReceived and \a |
| 410 | bytesTotal. |
| 411 | |
| 412 | The \a bytesReceived parameter indicates the number of bytes |
| 413 | received, while \a bytesTotal indicates the total number of bytes |
| 414 | expected to be downloaded. If the number of bytes to be downloaded |
| 415 | is not known, \a bytesTotal will be -1. |
| 416 | |
| 417 | The download is finished when \a bytesReceived is equal to \a |
| 418 | bytesTotal. At that time, \a bytesTotal will not be -1. |
| 419 | |
| 420 | Note that the values of both \a bytesReceived and \a bytesTotal |
| 421 | may be different from size(), the total number of bytes |
| 422 | obtained through read() or readAll(), or the value of the |
| 423 | header(ContentLengthHeader). The reason for that is that there may |
| 424 | be protocol overhead or the data may be compressed during the |
| 425 | download. |
| 426 | |
| 427 | \sa uploadProgress(), bytesAvailable() |
| 428 | */ |
| 429 | |
| 430 | /*! |
| 431 | \fn void QNetworkReply::abort() |
| 432 | |
| 433 | Aborts the operation immediately and close down any network |
| 434 | connections still open. Uploads still in progress are also |
| 435 | aborted. |
| 436 | |
| 437 | The finished() signal will also be emitted. |
| 438 | |
| 439 | \sa close(), finished() |
| 440 | */ |
| 441 | |
| 442 | /*! |
| 443 | Creates a QNetworkReply object with parent \a parent. |
| 444 | |
| 445 | You cannot directly instantiate QNetworkReply objects. Use |
| 446 | QNetworkAccessManager functions to do that. |
| 447 | */ |
| 448 | QNetworkReply::QNetworkReply(QObject *parent) |
| 449 | : QNetworkReply(*new QNetworkReplyPrivate, parent) |
| 450 | { |
| 451 | } |
| 452 | |
| 453 | /*! |
| 454 | \internal |
| 455 | */ |
| 456 | QNetworkReply::QNetworkReply(QNetworkReplyPrivate &dd, QObject *parent) |
| 457 | : QIODevice(dd, parent) |
| 458 | { |
| 459 | } |
| 460 | |
| 461 | /*! |
| 462 | Disposes of this reply and frees any resources associated with |
| 463 | it. If any network connections are still open, they will be |
| 464 | closed. |
| 465 | |
| 466 | \sa abort(), close() |
| 467 | */ |
| 468 | QNetworkReply::~QNetworkReply() |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | /*! |
| 473 | Closes this device for reading. Unread data is discarded, but the |
| 474 | network resources are not discarded until they are finished. In |
| 475 | particular, if any upload is in progress, it will continue until |
| 476 | it is done. |
| 477 | |
| 478 | The finished() signal is emitted when all operations are over and |
| 479 | the network resources are freed. |
| 480 | |
| 481 | \sa abort(), finished() |
| 482 | */ |
| 483 | void QNetworkReply::close() |
| 484 | { |
| 485 | QIODevice::close(); |
| 486 | } |
| 487 | |
| 488 | /*! |
| 489 | \internal |
| 490 | */ |
| 491 | bool QNetworkReply::isSequential() const |
| 492 | { |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | /*! |
| 497 | Returns the size of the read buffer, in bytes. |
| 498 | |
| 499 | \sa setReadBufferSize() |
| 500 | */ |
| 501 | qint64 QNetworkReply::readBufferSize() const |
| 502 | { |
| 503 | return d_func()->readBufferMaxSize; |
| 504 | } |
| 505 | |
| 506 | /*! |
| 507 | Sets the size of the read buffer to be \a size bytes. The read |
| 508 | buffer is the buffer that holds data that is being downloaded off |
| 509 | the network, before it is read with QIODevice::read(). Setting the |
| 510 | buffer size to 0 will make the buffer unlimited in size. |
| 511 | |
| 512 | QNetworkReply will try to stop reading from the network once this |
| 513 | buffer is full (i.e., bytesAvailable() returns \a size or more), |
| 514 | thus causing the download to throttle down as well. If the buffer |
| 515 | is not limited in size, QNetworkReply will try to download as fast |
| 516 | as possible from the network. |
| 517 | |
| 518 | Unlike QAbstractSocket::setReadBufferSize(), QNetworkReply cannot |
| 519 | guarantee precision in the read buffer size. That is, |
| 520 | bytesAvailable() can return more than \a size. |
| 521 | |
| 522 | \sa readBufferSize() |
| 523 | */ |
| 524 | void QNetworkReply::setReadBufferSize(qint64 size) |
| 525 | { |
| 526 | Q_D(QNetworkReply); |
| 527 | d->readBufferMaxSize = size; |
| 528 | } |
| 529 | |
| 530 | /*! |
| 531 | Returns the QNetworkAccessManager that was used to create this |
| 532 | QNetworkReply object. Initially, it is also the parent object. |
| 533 | */ |
| 534 | QNetworkAccessManager *QNetworkReply::manager() const |
| 535 | { |
| 536 | return d_func()->manager; |
| 537 | } |
| 538 | |
| 539 | /*! |
| 540 | Returns the request that was posted for this reply. In special, |
| 541 | note that the URL for the request may be different than that of |
| 542 | the reply. |
| 543 | |
| 544 | \sa QNetworkRequest::url(), url(), setRequest() |
| 545 | */ |
| 546 | QNetworkRequest QNetworkReply::request() const |
| 547 | { |
| 548 | return d_func()->originalRequest; |
| 549 | } |
| 550 | |
| 551 | /*! |
| 552 | Returns the operation that was posted for this reply. |
| 553 | |
| 554 | \sa setOperation() |
| 555 | */ |
| 556 | QNetworkAccessManager::Operation QNetworkReply::operation() const |
| 557 | { |
| 558 | return d_func()->operation; |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | Returns the error that was found during the processing of this |
| 563 | request. If no error was found, returns NoError. |
| 564 | |
| 565 | \sa setError() |
| 566 | */ |
| 567 | QNetworkReply::NetworkError QNetworkReply::error() const |
| 568 | { |
| 569 | return d_func()->errorCode; |
| 570 | } |
| 571 | |
| 572 | /*! |
| 573 | \since 4.6 |
| 574 | |
| 575 | Returns \c true when the reply has finished or was aborted. |
| 576 | |
| 577 | \sa isRunning() |
| 578 | */ |
| 579 | bool QNetworkReply::isFinished() const |
| 580 | { |
| 581 | return d_func()->isFinished; |
| 582 | } |
| 583 | |
| 584 | /*! |
| 585 | \since 4.6 |
| 586 | |
| 587 | Returns \c true when the request is still processing and the |
| 588 | reply has not finished or was aborted yet. |
| 589 | |
| 590 | \sa isFinished() |
| 591 | */ |
| 592 | bool QNetworkReply::isRunning() const |
| 593 | { |
| 594 | return !isFinished(); |
| 595 | } |
| 596 | |
| 597 | /*! |
| 598 | Returns the URL of the content downloaded or uploaded. Note that |
| 599 | the URL may be different from that of the original request. |
| 600 | If redirections were enabled in the request, then this |
| 601 | function returns the current url that the network API is accessing, i.e the |
| 602 | url of the resource the request got redirected to. |
| 603 | |
| 604 | \sa request(), setUrl(), QNetworkRequest::url(), redirected() |
| 605 | */ |
| 606 | QUrl QNetworkReply::url() const |
| 607 | { |
| 608 | return d_func()->url; |
| 609 | } |
| 610 | |
| 611 | /*! |
| 612 | Returns the value of the known header \a header, if that header |
| 613 | was sent by the remote server. If the header was not sent, returns |
| 614 | an invalid QVariant. |
| 615 | |
| 616 | \sa rawHeader(), setHeader(), QNetworkRequest::header() |
| 617 | */ |
| 618 | QVariant QNetworkReply::(QNetworkRequest::KnownHeaders ) const |
| 619 | { |
| 620 | return d_func()->cookedHeaders.value(header); |
| 621 | } |
| 622 | |
| 623 | /*! |
| 624 | Returns \c true if the raw header of name \a headerName was sent by |
| 625 | the remote server |
| 626 | |
| 627 | \sa rawHeader() |
| 628 | */ |
| 629 | bool QNetworkReply::(const QByteArray &) const |
| 630 | { |
| 631 | Q_D(const QNetworkReply); |
| 632 | return d->findRawHeader(headerName) != d->rawHeaders.constEnd(); |
| 633 | } |
| 634 | |
| 635 | /*! |
| 636 | Returns the raw contents of the header \a headerName as sent by |
| 637 | the remote server. If there is no such header, returns an empty |
| 638 | byte array, which may be indistinguishable from an empty |
| 639 | header. Use hasRawHeader() to verify if the server sent such |
| 640 | header field. |
| 641 | |
| 642 | \sa setRawHeader(), hasRawHeader(), header() |
| 643 | */ |
| 644 | QByteArray QNetworkReply::(const QByteArray &) const |
| 645 | { |
| 646 | Q_D(const QNetworkReply); |
| 647 | QNetworkHeadersPrivate::RawHeadersList::ConstIterator it = |
| 648 | d->findRawHeader(headerName); |
| 649 | if (it != d->rawHeaders.constEnd()) |
| 650 | return it->second; |
| 651 | return QByteArray(); |
| 652 | } |
| 653 | |
| 654 | /*! \typedef QNetworkReply::RawHeaderPair |
| 655 | |
| 656 | RawHeaderPair is a QPair<QByteArray, QByteArray> where the first |
| 657 | QByteArray is the header name and the second is the header. |
| 658 | */ |
| 659 | |
| 660 | /*! |
| 661 | Returns a list of raw header pairs. |
| 662 | */ |
| 663 | const QList<QNetworkReply::RawHeaderPair>& QNetworkReply::() const |
| 664 | { |
| 665 | Q_D(const QNetworkReply); |
| 666 | return d->rawHeaders; |
| 667 | } |
| 668 | |
| 669 | /*! |
| 670 | Returns a list of headers fields that were sent by the remote |
| 671 | server, in the order that they were sent. Duplicate headers are |
| 672 | merged together and take place of the latter duplicate. |
| 673 | */ |
| 674 | QList<QByteArray> QNetworkReply::() const |
| 675 | { |
| 676 | return d_func()->rawHeadersKeys(); |
| 677 | } |
| 678 | |
| 679 | /*! |
| 680 | Returns the attribute associated with the code \a code. If the |
| 681 | attribute has not been set, it returns an invalid QVariant (type QMetaType::UnknownType). |
| 682 | |
| 683 | You can expect the default values listed in |
| 684 | QNetworkRequest::Attribute to be applied to the values returned by |
| 685 | this function. |
| 686 | |
| 687 | \sa setAttribute(), QNetworkRequest::Attribute |
| 688 | */ |
| 689 | QVariant QNetworkReply::attribute(QNetworkRequest::Attribute code) const |
| 690 | { |
| 691 | return d_func()->attributes.value(code); |
| 692 | } |
| 693 | |
| 694 | #if QT_CONFIG(ssl) |
| 695 | /*! |
| 696 | Returns the SSL configuration and state associated with this |
| 697 | reply, if SSL was used. It will contain the remote server's |
| 698 | certificate, its certificate chain leading to the Certificate |
| 699 | Authority as well as the encryption ciphers in use. |
| 700 | |
| 701 | The peer's certificate and its certificate chain will be known by |
| 702 | the time sslErrors() is emitted, if it's emitted. |
| 703 | */ |
| 704 | QSslConfiguration QNetworkReply::sslConfiguration() const |
| 705 | { |
| 706 | QSslConfiguration config; |
| 707 | sslConfigurationImplementation(config); |
| 708 | return config; |
| 709 | } |
| 710 | |
| 711 | /*! |
| 712 | Sets the SSL configuration for the network connection associated |
| 713 | with this request, if possible, to be that of \a config. |
| 714 | */ |
| 715 | void QNetworkReply::setSslConfiguration(const QSslConfiguration &config) |
| 716 | { |
| 717 | setSslConfigurationImplementation(config); |
| 718 | } |
| 719 | |
| 720 | /*! |
| 721 | \overload |
| 722 | \since 4.6 |
| 723 | |
| 724 | If this function is called, the SSL errors given in \a errors |
| 725 | will be ignored. |
| 726 | |
| 727 | \note Because most SSL errors are associated with a certificate, for most |
| 728 | of them you must set the expected certificate this SSL error is related to. |
| 729 | If, for instance, you want to issue a request to a server that uses |
| 730 | a self-signed certificate, consider the following snippet: |
| 731 | |
| 732 | \snippet code/src_network_access_qnetworkreply.cpp 0 |
| 733 | |
| 734 | Multiple calls to this function will replace the list of errors that |
| 735 | were passed in previous calls. |
| 736 | You can clear the list of errors you want to ignore by calling this |
| 737 | function with an empty list. |
| 738 | |
| 739 | \note If HTTP Strict Transport Security is enabled for QNetworkAccessManager, |
| 740 | this function has no effect. |
| 741 | |
| 742 | \sa sslConfiguration(), sslErrors(), QSslSocket::ignoreSslErrors(), |
| 743 | QNetworkAccessManager::setStrictTransportSecurityEnabled() |
| 744 | */ |
| 745 | void QNetworkReply::ignoreSslErrors(const QList<QSslError> &errors) |
| 746 | { |
| 747 | ignoreSslErrorsImplementation(errors); |
| 748 | } |
| 749 | |
| 750 | /*! |
| 751 | \fn void QNetworkReply::sslConfigurationImplementation(QSslConfiguration &configuration) const |
| 752 | \since 5.0 |
| 753 | |
| 754 | This virtual method is provided to enable overriding the behavior of |
| 755 | sslConfiguration(). sslConfiguration() is a public wrapper for this method. |
| 756 | The configuration will be returned in \a configuration. |
| 757 | |
| 758 | \sa sslConfiguration() |
| 759 | */ |
| 760 | void QNetworkReply::sslConfigurationImplementation(QSslConfiguration &) const |
| 761 | { |
| 762 | } |
| 763 | |
| 764 | /*! |
| 765 | \fn void QNetworkReply::setSslConfigurationImplementation(const QSslConfiguration &configuration) |
| 766 | \since 5.0 |
| 767 | |
| 768 | This virtual method is provided to enable overriding the behavior of |
| 769 | setSslConfiguration(). setSslConfiguration() is a public wrapper for this method. |
| 770 | If you override this method use \a configuration to set the SSL configuration. |
| 771 | |
| 772 | \sa sslConfigurationImplementation(), setSslConfiguration() |
| 773 | */ |
| 774 | void QNetworkReply::setSslConfigurationImplementation(const QSslConfiguration &) |
| 775 | { |
| 776 | } |
| 777 | |
| 778 | /*! |
| 779 | \fn void QNetworkReply::ignoreSslErrorsImplementation(const QList<QSslError> &errors) |
| 780 | \since 5.0 |
| 781 | |
| 782 | This virtual method is provided to enable overriding the behavior of |
| 783 | ignoreSslErrors(). ignoreSslErrors() is a public wrapper for this method. |
| 784 | \a errors contains the errors the user wishes ignored. |
| 785 | |
| 786 | \sa ignoreSslErrors() |
| 787 | */ |
| 788 | void QNetworkReply::ignoreSslErrorsImplementation(const QList<QSslError> &) |
| 789 | { |
| 790 | } |
| 791 | |
| 792 | #endif // QT_CONFIG(ssl) |
| 793 | |
| 794 | /*! |
| 795 | If this function is called, SSL errors related to network |
| 796 | connection will be ignored, including certificate validation |
| 797 | errors. |
| 798 | |
| 799 | \warning Be sure to always let the user inspect the errors |
| 800 | reported by the sslErrors() signal, and only call this method |
| 801 | upon confirmation from the user that proceeding is ok. |
| 802 | If there are unexpected errors, the reply should be aborted. |
| 803 | Calling this method without inspecting the actual errors will |
| 804 | most likely pose a security risk for your application. Use it |
| 805 | with great care! |
| 806 | |
| 807 | This function can be called from the slot connected to the |
| 808 | sslErrors() signal, which indicates which errors were |
| 809 | found. |
| 810 | |
| 811 | \note If HTTP Strict Transport Security is enabled for QNetworkAccessManager, |
| 812 | this function has no effect. |
| 813 | |
| 814 | \sa sslConfiguration(), sslErrors(), QSslSocket::ignoreSslErrors() |
| 815 | */ |
| 816 | void QNetworkReply::ignoreSslErrors() |
| 817 | { |
| 818 | } |
| 819 | |
| 820 | /*! |
| 821 | \internal |
| 822 | */ |
| 823 | qint64 QNetworkReply::writeData(const char *, qint64) |
| 824 | { |
| 825 | return -1; // you can't write |
| 826 | } |
| 827 | |
| 828 | /*! |
| 829 | Sets the associated operation for this object to be \a |
| 830 | operation. This value will be returned by operation(). |
| 831 | |
| 832 | \note The operation should be set when this object is created and |
| 833 | not changed again. |
| 834 | |
| 835 | \sa operation(), setRequest() |
| 836 | */ |
| 837 | void QNetworkReply::setOperation(QNetworkAccessManager::Operation operation) |
| 838 | { |
| 839 | Q_D(QNetworkReply); |
| 840 | d->operation = operation; |
| 841 | } |
| 842 | |
| 843 | /*! |
| 844 | Sets the associated request for this object to be \a request. This |
| 845 | value will be returned by request(). |
| 846 | |
| 847 | \note The request should be set when this object is created and |
| 848 | not changed again. |
| 849 | |
| 850 | \sa request(), setOperation() |
| 851 | */ |
| 852 | void QNetworkReply::setRequest(const QNetworkRequest &request) |
| 853 | { |
| 854 | Q_D(QNetworkReply); |
| 855 | d->originalRequest = request; |
| 856 | } |
| 857 | |
| 858 | /*! |
| 859 | Sets the error condition to be \a errorCode. The human-readable |
| 860 | message is set with \a errorString. |
| 861 | |
| 862 | Calling setError() does not emit the errorOccurred(QNetworkReply::NetworkError) |
| 863 | signal. |
| 864 | |
| 865 | \sa error(), errorString() |
| 866 | */ |
| 867 | void QNetworkReply::setError(NetworkError errorCode, const QString &errorString) |
| 868 | { |
| 869 | Q_D(QNetworkReply); |
| 870 | d->errorCode = errorCode; |
| 871 | setErrorString(errorString); // in QIODevice |
| 872 | } |
| 873 | |
| 874 | /*! |
| 875 | \since 4.8 |
| 876 | Sets the reply as \a finished. |
| 877 | |
| 878 | After having this set the replies data must not change. |
| 879 | |
| 880 | \sa isFinished() |
| 881 | */ |
| 882 | void QNetworkReply::setFinished(bool finished) |
| 883 | { |
| 884 | Q_D(QNetworkReply); |
| 885 | d->isFinished = finished; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | /*! |
| 890 | Sets the URL being processed to be \a url. Normally, the URL |
| 891 | matches that of the request that was posted, but for a variety of |
| 892 | reasons it can be different (for example, a file path being made |
| 893 | absolute or canonical). |
| 894 | |
| 895 | \sa url(), request(), QNetworkRequest::url() |
| 896 | */ |
| 897 | void QNetworkReply::setUrl(const QUrl &url) |
| 898 | { |
| 899 | Q_D(QNetworkReply); |
| 900 | d->url = url; |
| 901 | } |
| 902 | |
| 903 | /*! |
| 904 | Sets the known header \a header to be of value \a value. The |
| 905 | corresponding raw form of the header will be set as well. |
| 906 | |
| 907 | \sa header(), setRawHeader(), QNetworkRequest::setHeader() |
| 908 | */ |
| 909 | void QNetworkReply::(QNetworkRequest::KnownHeaders , const QVariant &value) |
| 910 | { |
| 911 | Q_D(QNetworkReply); |
| 912 | d->setCookedHeader(header, value); |
| 913 | } |
| 914 | |
| 915 | /*! |
| 916 | Sets the raw header \a headerName to be of value \a value. If \a |
| 917 | headerName was previously set, it is overridden. Multiple HTTP |
| 918 | headers of the same name are functionally equivalent to one single |
| 919 | header with the values concatenated, separated by commas. |
| 920 | |
| 921 | If \a headerName matches a known header, the value \a value will |
| 922 | be parsed and the corresponding parsed form will also be set. |
| 923 | |
| 924 | \sa rawHeader(), header(), setHeader(), QNetworkRequest::setRawHeader() |
| 925 | */ |
| 926 | void QNetworkReply::(const QByteArray &, const QByteArray &value) |
| 927 | { |
| 928 | Q_D(QNetworkReply); |
| 929 | d->setRawHeader(headerName, value); |
| 930 | } |
| 931 | |
| 932 | /*! |
| 933 | Sets the attribute \a code to have value \a value. If \a code was |
| 934 | previously set, it will be overridden. If \a value is an invalid |
| 935 | QVariant, the attribute will be unset. |
| 936 | |
| 937 | \sa attribute(), QNetworkRequest::setAttribute() |
| 938 | */ |
| 939 | void QNetworkReply::setAttribute(QNetworkRequest::Attribute code, const QVariant &value) |
| 940 | { |
| 941 | Q_D(QNetworkReply); |
| 942 | if (value.isValid()) |
| 943 | d->attributes.insert(code, value); |
| 944 | else |
| 945 | d->attributes.remove(code); |
| 946 | } |
| 947 | |
| 948 | QT_END_NAMESPACE |
| 949 | |