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