| 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 "qhttpnetworkconnection_p.h" |
| 5 | #include "qhttp2protocolhandler_p.h" |
| 6 | |
| 7 | #include "http2/http2frames_p.h" |
| 8 | #include "http2/bitstreams_p.h" |
| 9 | |
| 10 | #include <private/qnoncontiguousbytedevice_p.h> |
| 11 | |
| 12 | #include <QtNetwork/qabstractsocket.h> |
| 13 | |
| 14 | #include <QtCore/qloggingcategory.h> |
| 15 | #include <QtCore/qendian.h> |
| 16 | #include <QtCore/qdebug.h> |
| 17 | #include <QtCore/qlist.h> |
| 18 | #include <QtCore/qnumeric.h> |
| 19 | #include <QtCore/qurl.h> |
| 20 | |
| 21 | #include <qhttp2configuration.h> |
| 22 | |
| 23 | #ifndef QT_NO_NETWORKPROXY |
| 24 | #include <QtNetwork/qnetworkproxy.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <qcoreapplication.h> |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <vector> |
| 31 | #include <optional> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | using namespace Qt::StringLiterals; |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | |
| 40 | HPack::HttpHeader (const QHttpNetworkRequest &request, quint32 , |
| 41 | bool useProxy) |
| 42 | { |
| 43 | using namespace HPack; |
| 44 | |
| 45 | HttpHeader ; |
| 46 | header.reserve(n: 300); |
| 47 | |
| 48 | // 1. Before anything - mandatory fields, if they do not fit into maxHeaderList - |
| 49 | // then stop immediately with error. |
| 50 | const auto auth = request.url().authority(options: QUrl::FullyEncoded | QUrl::RemoveUserInfo).toLatin1(); |
| 51 | header.emplace_back(args: ":authority" , args: auth); |
| 52 | header.emplace_back(args: ":method" , args: request.methodName()); |
| 53 | header.emplace_back(args: ":path" , args: request.uri(throughProxy: useProxy)); |
| 54 | header.emplace_back(args: ":scheme" , args: request.url().scheme().toLatin1()); |
| 55 | |
| 56 | HeaderSize size = header_size(header); |
| 57 | if (!size.first) // Ooops! |
| 58 | return HttpHeader(); |
| 59 | |
| 60 | if (size.second > maxHeaderListSize) |
| 61 | return HttpHeader(); // Bad, we cannot send this request ... |
| 62 | |
| 63 | const QHttpHeaders = request.header(); |
| 64 | for (qsizetype i = 0; i < requestHeader.size(); ++i) { |
| 65 | const auto name = requestHeader.nameAt(i); |
| 66 | const auto value = requestHeader.valueAt(i); |
| 67 | const HeaderSize delta = entry_size(name, value); |
| 68 | if (!delta.first) // Overflow??? |
| 69 | break; |
| 70 | if (std::numeric_limits<quint32>::max() - delta.second < size.second) |
| 71 | break; |
| 72 | size.second += delta.second; |
| 73 | if (size.second > maxHeaderListSize) |
| 74 | break; |
| 75 | |
| 76 | if (name == "connection"_L1 || name == "host"_L1 || name == "keep-alive"_L1 |
| 77 | || name == "proxy-connection"_L1 || name == "transfer-encoding"_L1 ) { |
| 78 | continue; // Those headers are not valid (section 3.2.1) - from QSpdyProtocolHandler |
| 79 | } |
| 80 | // TODO: verify with specs, which fields are valid to send .... |
| 81 | // |
| 82 | // Note: RFC 7450 8.1.2 (HTTP/2) states that header field names must be lower-cased |
| 83 | // prior to their encoding in HTTP/2; header name fields in QHttpHeaders are already |
| 84 | // lower-cased |
| 85 | header.emplace_back(args: QByteArray{name.data(), name.size()}, |
| 86 | args: QByteArray{value.data(), value.size()}); |
| 87 | } |
| 88 | |
| 89 | return header; |
| 90 | } |
| 91 | |
| 92 | QUrl urlkey_from_request(const QHttpNetworkRequest &request) |
| 93 | { |
| 94 | QUrl url; |
| 95 | |
| 96 | url.setScheme(request.url().scheme()); |
| 97 | url.setAuthority(authority: request.url().authority(options: QUrl::FullyEncoded | QUrl::RemoveUserInfo)); |
| 98 | url.setPath(path: QLatin1StringView(request.uri(throughProxy: false))); |
| 99 | |
| 100 | return url; |
| 101 | } |
| 102 | |
| 103 | }// Unnamed namespace |
| 104 | |
| 105 | // Since we anyway end up having this in every function definition: |
| 106 | using namespace Http2; |
| 107 | |
| 108 | QHttp2ProtocolHandler::QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel) |
| 109 | : QAbstractProtocolHandler(channel), |
| 110 | decoder(HPack::FieldLookupTable::DefaultSize), |
| 111 | encoder(HPack::FieldLookupTable::DefaultSize, true) |
| 112 | { |
| 113 | Q_ASSERT(channel && m_connection); |
| 114 | continuedFrames.reserve(n: 20); |
| 115 | |
| 116 | const auto h2Config = m_connection->http2Parameters(); |
| 117 | maxSessionReceiveWindowSize = h2Config.sessionReceiveWindowSize(); |
| 118 | pushPromiseEnabled = h2Config.serverPushEnabled(); |
| 119 | streamInitialReceiveWindowSize = h2Config.streamReceiveWindowSize(); |
| 120 | encoder.setCompressStrings(h2Config.huffmanCompressionEnabled()); |
| 121 | |
| 122 | if (!channel->ssl && m_connection->connectionType() != QHttpNetworkConnection::ConnectionTypeHTTP2Direct) { |
| 123 | // We upgraded from HTTP/1.1 to HTTP/2. channel->request was already sent |
| 124 | // as HTTP/1.1 request. The response with status code 101 triggered |
| 125 | // protocol switch and now we are waiting for the real response, sent |
| 126 | // as HTTP/2 frames. |
| 127 | Q_ASSERT(channel->reply); |
| 128 | const quint32 initialStreamID = createNewStream(message: HttpMessagePair(channel->request, channel->reply), |
| 129 | uploadDone: true /* uploaded by HTTP/1.1 */); |
| 130 | Q_ASSERT(initialStreamID == 1); |
| 131 | Stream &stream = activeStreams[initialStreamID]; |
| 132 | stream.state = Stream::halfClosedLocal; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void QHttp2ProtocolHandler::handleConnectionClosure() |
| 137 | { |
| 138 | // The channel has just received RemoteHostClosedError and since it will |
| 139 | // not try (for HTTP/2) to re-connect, it's time to finish all replies |
| 140 | // with error. |
| 141 | |
| 142 | // Maybe we still have some data to read and can successfully finish |
| 143 | // a stream/request? |
| 144 | _q_receiveReply(); |
| 145 | |
| 146 | // Finish all still active streams. If we previously had GOAWAY frame, |
| 147 | // we probably already closed some (or all) streams with ContentReSend |
| 148 | // error, but for those still active, not having any data to finish, |
| 149 | // we now report RemoteHostClosedError. |
| 150 | const auto errorString = QCoreApplication::translate(context: "QHttp" , key: "Connection closed" ); |
| 151 | for (auto it = activeStreams.begin(), eIt = activeStreams.end(); it != eIt; ++it) |
| 152 | finishStreamWithError(stream&: it.value(), error: QNetworkReply::RemoteHostClosedError, message: errorString); |
| 153 | |
| 154 | // Make sure we'll never try to read anything later: |
| 155 | activeStreams.clear(); |
| 156 | goingAway = true; |
| 157 | } |
| 158 | |
| 159 | void QHttp2ProtocolHandler::ensureClientPrefaceSent() |
| 160 | { |
| 161 | if (!prefaceSent) |
| 162 | sendClientPreface(); |
| 163 | } |
| 164 | |
| 165 | void QHttp2ProtocolHandler::_q_uploadDataReadyRead() |
| 166 | { |
| 167 | if (!sender()) // QueuedConnection, firing after sender (byte device) was deleted. |
| 168 | return; |
| 169 | |
| 170 | auto data = qobject_cast<QNonContiguousByteDevice *>(object: sender()); |
| 171 | Q_ASSERT(data); |
| 172 | const qint32 streamID = streamIDs.value(key: data); |
| 173 | Q_ASSERT(streamID != 0); |
| 174 | Q_ASSERT(activeStreams.contains(streamID)); |
| 175 | auto &stream = activeStreams[streamID]; |
| 176 | |
| 177 | if (!sendDATA(stream)) { |
| 178 | finishStreamWithError(stream, error: QNetworkReply::UnknownNetworkError, message: "failed to send DATA"_L1 ); |
| 179 | sendRST_STREAM(streamID, errorCoder: INTERNAL_ERROR); |
| 180 | markAsReset(streamID); |
| 181 | deleteActiveStream(streamID); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void QHttp2ProtocolHandler::_q_replyDestroyed(QObject *reply) |
| 186 | { |
| 187 | const quint32 streamID = streamIDs.take(key: reply); |
| 188 | if (activeStreams.contains(key: streamID)) { |
| 189 | sendRST_STREAM(streamID, errorCoder: CANCEL); |
| 190 | markAsReset(streamID); |
| 191 | deleteActiveStream(streamID); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void QHttp2ProtocolHandler::_q_uploadDataDestroyed(QObject *uploadData) |
| 196 | { |
| 197 | streamIDs.remove(key: uploadData); |
| 198 | } |
| 199 | |
| 200 | void QHttp2ProtocolHandler::_q_readyRead() |
| 201 | { |
| 202 | if (!goingAway || activeStreams.size()) |
| 203 | _q_receiveReply(); |
| 204 | } |
| 205 | |
| 206 | void QHttp2ProtocolHandler::_q_receiveReply() |
| 207 | { |
| 208 | Q_ASSERT(m_socket); |
| 209 | Q_ASSERT(m_channel); |
| 210 | |
| 211 | if (goingAway && activeStreams.isEmpty()) { |
| 212 | m_channel->close(); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | while (!goingAway || activeStreams.size()) { |
| 217 | const auto result = frameReader.read(socket&: *m_socket); |
| 218 | switch (result) { |
| 219 | case FrameStatus::incompleteFrame: |
| 220 | return; |
| 221 | case FrameStatus::protocolError: |
| 222 | return connectionError(errorCode: PROTOCOL_ERROR, message: "invalid frame" ); |
| 223 | case FrameStatus::sizeError: |
| 224 | return connectionError(errorCode: FRAME_SIZE_ERROR, message: "invalid frame size" ); |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | Q_ASSERT(result == FrameStatus::goodFrame); |
| 230 | |
| 231 | inboundFrame = std::move(frameReader.inboundFrame()); |
| 232 | |
| 233 | const auto frameType = inboundFrame.type(); |
| 234 | if (continuationExpected && frameType != FrameType::CONTINUATION) |
| 235 | return connectionError(errorCode: PROTOCOL_ERROR, message: "CONTINUATION expected" ); |
| 236 | |
| 237 | switch (frameType) { |
| 238 | case FrameType::DATA: |
| 239 | handleDATA(); |
| 240 | break; |
| 241 | case FrameType::HEADERS: |
| 242 | handleHEADERS(); |
| 243 | break; |
| 244 | case FrameType::PRIORITY: |
| 245 | handlePRIORITY(); |
| 246 | break; |
| 247 | case FrameType::RST_STREAM: |
| 248 | handleRST_STREAM(); |
| 249 | break; |
| 250 | case FrameType::SETTINGS: |
| 251 | handleSETTINGS(); |
| 252 | break; |
| 253 | case FrameType::PUSH_PROMISE: |
| 254 | handlePUSH_PROMISE(); |
| 255 | break; |
| 256 | case FrameType::PING: |
| 257 | handlePING(); |
| 258 | break; |
| 259 | case FrameType::GOAWAY: |
| 260 | handleGOAWAY(); |
| 261 | break; |
| 262 | case FrameType::WINDOW_UPDATE: |
| 263 | handleWINDOW_UPDATE(); |
| 264 | break; |
| 265 | case FrameType::CONTINUATION: |
| 266 | handleCONTINUATION(); |
| 267 | break; |
| 268 | case FrameType::LAST_FRAME_TYPE: |
| 269 | // 5.1 - ignore unknown frames. |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | bool QHttp2ProtocolHandler::sendRequest() |
| 276 | { |
| 277 | if (goingAway) { |
| 278 | // Stop further calls to this method: we have received GOAWAY |
| 279 | // so we cannot create new streams. |
| 280 | m_channel->emitFinishedWithError(error: QNetworkReply::ProtocolUnknownError, |
| 281 | message: "GOAWAY received, cannot start a request" ); |
| 282 | m_channel->h2RequestsToSend.clear(); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // Process 'fake' (created by QNetworkAccessManager::connectToHostEncrypted()) |
| 287 | // requests first: |
| 288 | auto &requests = m_channel->h2RequestsToSend; |
| 289 | for (auto it = requests.begin(), endIt = requests.end(); it != endIt;) { |
| 290 | const auto &pair = *it; |
| 291 | if (pair.first.isPreConnect()) { |
| 292 | m_connection->preConnectFinished(); |
| 293 | emit pair.second->finished(); |
| 294 | it = requests.erase(it); |
| 295 | if (!requests.size()) { |
| 296 | // Normally, after a connection was established and H2 |
| 297 | // was negotiated, we send a client preface. connectToHostEncrypted |
| 298 | // though is not meant to send any data, it's just a 'preconnect'. |
| 299 | // Thus we return early: |
| 300 | return true; |
| 301 | } |
| 302 | } else { |
| 303 | ++it; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (!requests.size()) |
| 308 | return true; |
| 309 | |
| 310 | if (!prefaceSent && !sendClientPreface()) |
| 311 | return false; |
| 312 | |
| 313 | m_channel->state = QHttpNetworkConnectionChannel::WritingState; |
| 314 | // Check what was promised/pushed, maybe we do not have to send a request |
| 315 | // and have a response already? |
| 316 | |
| 317 | for (auto it = requests.begin(), endIt = requests.end(); it != endIt;) { |
| 318 | const auto key = urlkey_from_request(request: it->first).toString(); |
| 319 | if (!promisedData.contains(key)) { |
| 320 | ++it; |
| 321 | continue; |
| 322 | } |
| 323 | // Woo-hoo, we do not have to ask, the answer is ready for us: |
| 324 | HttpMessagePair message = *it; |
| 325 | it = requests.erase(it); |
| 326 | initReplyFromPushPromise(message, cacheKey: key); |
| 327 | } |
| 328 | |
| 329 | const auto isClientSide = [](const auto &pair) -> bool { return (pair.first & 1) == 1; }; |
| 330 | const auto activeClientSideStreams = std::count_if( |
| 331 | first: activeStreams.constKeyValueBegin(), last: activeStreams.constKeyValueEnd(), pred: isClientSide); |
| 332 | const qint64 streamsToUse = qBound(min: 0, val: qint64(maxConcurrentStreams) - activeClientSideStreams, |
| 333 | max: requests.size()); |
| 334 | auto it = requests.begin(); |
| 335 | for (qint64 i = 0; i < streamsToUse; ++i) { |
| 336 | const qint32 newStreamID = createNewStream(message: *it); |
| 337 | if (!newStreamID) { |
| 338 | // TODO: actually we have to open a new connection. |
| 339 | qCCritical(QT_HTTP2, "sendRequest: out of stream IDs" ); |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | it = requests.erase(it); |
| 344 | |
| 345 | Stream &newStream = activeStreams[newStreamID]; |
| 346 | if (!sendHEADERS(stream&: newStream)) { |
| 347 | finishStreamWithError(stream&: newStream, error: QNetworkReply::UnknownNetworkError, |
| 348 | message: "failed to send HEADERS frame(s)"_L1 ); |
| 349 | deleteActiveStream(streamID: newStreamID); |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if (newStream.data() && !sendDATA(stream&: newStream)) { |
| 354 | finishStreamWithError(stream&: newStream, error: QNetworkReply::UnknownNetworkError, |
| 355 | message: "failed to send DATA frame(s)"_L1 ); |
| 356 | sendRST_STREAM(streamID: newStreamID, errorCoder: INTERNAL_ERROR); |
| 357 | markAsReset(streamID: newStreamID); |
| 358 | deleteActiveStream(streamID: newStreamID); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | m_channel->state = QHttpNetworkConnectionChannel::IdleState; |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | bool QHttp2ProtocolHandler::sendClientPreface() |
| 369 | { |
| 370 | // 3.5 HTTP/2 Connection Preface |
| 371 | Q_ASSERT(m_socket); |
| 372 | |
| 373 | if (prefaceSent) |
| 374 | return true; |
| 375 | |
| 376 | const qint64 written = m_socket->write(data: Http2::Http2clientPreface, |
| 377 | len: Http2::clientPrefaceLength); |
| 378 | if (written != Http2::clientPrefaceLength) |
| 379 | return false; |
| 380 | |
| 381 | // 6.5 SETTINGS |
| 382 | frameWriter.setOutboundFrame(Http2::configurationToSettingsFrame(configuration: m_connection->http2Parameters())); |
| 383 | Q_ASSERT(frameWriter.outboundFrame().payloadSize()); |
| 384 | |
| 385 | if (!frameWriter.write(socket&: *m_socket)) |
| 386 | return false; |
| 387 | |
| 388 | sessionReceiveWindowSize = maxSessionReceiveWindowSize; |
| 389 | // We only send WINDOW_UPDATE for the connection if the size differs from the |
| 390 | // default 64 KB: |
| 391 | const auto delta = maxSessionReceiveWindowSize - Http2::defaultSessionWindowSize; |
| 392 | if (delta && !sendWINDOW_UPDATE(streamID: Http2::connectionStreamID, delta)) |
| 393 | return false; |
| 394 | |
| 395 | prefaceSent = true; |
| 396 | waitingForSettingsACK = true; |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool QHttp2ProtocolHandler::sendSETTINGS_ACK() |
| 402 | { |
| 403 | Q_ASSERT(m_socket); |
| 404 | |
| 405 | if (!prefaceSent && !sendClientPreface()) |
| 406 | return false; |
| 407 | |
| 408 | frameWriter.start(type: FrameType::SETTINGS, flags: FrameFlag::ACK, streamID: Http2::connectionStreamID); |
| 409 | |
| 410 | return frameWriter.write(socket&: *m_socket); |
| 411 | } |
| 412 | |
| 413 | bool QHttp2ProtocolHandler::sendHEADERS(Stream &stream) |
| 414 | { |
| 415 | using namespace HPack; |
| 416 | |
| 417 | frameWriter.start(type: FrameType::HEADERS, flags: FrameFlag::PRIORITY | FrameFlag::END_HEADERS, |
| 418 | streamID: stream.streamID); |
| 419 | |
| 420 | if (!stream.data()) { |
| 421 | frameWriter.addFlag(flag: FrameFlag::END_STREAM); |
| 422 | stream.state = Stream::halfClosedLocal; |
| 423 | } else { |
| 424 | stream.state = Stream::open; |
| 425 | } |
| 426 | |
| 427 | frameWriter.append(val: quint32()); // No stream dependency in Qt. |
| 428 | frameWriter.append(val: stream.weight()); |
| 429 | |
| 430 | bool useProxy = false; |
| 431 | #ifndef QT_NO_NETWORKPROXY |
| 432 | useProxy = m_connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy; |
| 433 | #endif |
| 434 | if (stream.request().withCredentials()) { |
| 435 | m_connection->d_func()->createAuthorization(socket: m_socket, request&: stream.request()); |
| 436 | stream.request().d->needResendWithCredentials = false; |
| 437 | } |
| 438 | const auto = build_headers(request: stream.request(), maxHeaderListSize, useProxy); |
| 439 | if (!headers.size()) // nothing fits into maxHeaderListSize |
| 440 | return false; |
| 441 | |
| 442 | // Compress in-place: |
| 443 | BitOStream outputStream(frameWriter.outboundFrame().buffer); |
| 444 | |
| 445 | // Possibly perform and notify of dynamic table size update: |
| 446 | for (auto &maybePendingTableSizeUpdate : pendingTableSizeUpdates) { |
| 447 | if (!maybePendingTableSizeUpdate) |
| 448 | break; // They are ordered, so if the first one is null, the other one is too. |
| 449 | encoder.setMaxDynamicTableSize(*maybePendingTableSizeUpdate); |
| 450 | encoder.encodeSizeUpdate(outputStream, newSize: *maybePendingTableSizeUpdate); |
| 451 | maybePendingTableSizeUpdate.reset(); |
| 452 | } |
| 453 | |
| 454 | if (!encoder.encodeRequest(outputStream, header: headers)) |
| 455 | return false; |
| 456 | |
| 457 | return frameWriter.writeHEADERS(socket&: *m_socket, sizeLimit: maxFrameSize); |
| 458 | } |
| 459 | |
| 460 | bool QHttp2ProtocolHandler::sendDATA(Stream &stream) |
| 461 | { |
| 462 | Q_ASSERT(maxFrameSize > frameHeaderSize); |
| 463 | Q_ASSERT(m_socket); |
| 464 | Q_ASSERT(stream.data()); |
| 465 | |
| 466 | const auto &request = stream.request(); |
| 467 | auto reply = stream.reply(); |
| 468 | Q_ASSERT(reply); |
| 469 | const auto replyPrivate = reply->d_func(); |
| 470 | Q_ASSERT(replyPrivate); |
| 471 | |
| 472 | auto slot = std::min<qint32>(a: sessionSendWindowSize, b: stream.sendWindow); |
| 473 | while (replyPrivate->totallyUploadedData < request.contentLength() && slot) { |
| 474 | qint64 chunkSize = 0; |
| 475 | const uchar *src = |
| 476 | reinterpret_cast<const uchar *>(stream.data()->readPointer(maximumLength: slot, len&: chunkSize)); |
| 477 | |
| 478 | if (chunkSize == -1) |
| 479 | return false; |
| 480 | |
| 481 | if (!src || !chunkSize) { |
| 482 | // Stream is not suspended by the flow control, |
| 483 | // we do not have data ready yet. |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | frameWriter.start(type: FrameType::DATA, flags: FrameFlag::EMPTY, streamID: stream.streamID); |
| 488 | const qint32 bytesWritten = qint32(std::min<qint64>(a: slot, b: chunkSize)); |
| 489 | |
| 490 | if (!frameWriter.writeDATA(socket&: *m_socket, sizeLimit: maxFrameSize, src, size: bytesWritten)) |
| 491 | return false; |
| 492 | |
| 493 | stream.data()->advanceReadPointer(amount: bytesWritten); |
| 494 | stream.sendWindow -= bytesWritten; |
| 495 | sessionSendWindowSize -= bytesWritten; |
| 496 | replyPrivate->totallyUploadedData += bytesWritten; |
| 497 | emit reply->dataSendProgress(done: replyPrivate->totallyUploadedData, |
| 498 | total: request.contentLength()); |
| 499 | slot = std::min(a: sessionSendWindowSize, b: stream.sendWindow); |
| 500 | } |
| 501 | |
| 502 | if (replyPrivate->totallyUploadedData == request.contentLength()) { |
| 503 | frameWriter.start(type: FrameType::DATA, flags: FrameFlag::END_STREAM, streamID: stream.streamID); |
| 504 | frameWriter.setPayloadSize(0); |
| 505 | frameWriter.write(socket&: *m_socket); |
| 506 | stream.state = Stream::halfClosedLocal; |
| 507 | stream.data()->disconnect(receiver: this); |
| 508 | removeFromSuspended(streamID: stream.streamID); |
| 509 | } else if (!stream.data()->atEnd()) { |
| 510 | addToSuspended(stream); |
| 511 | } |
| 512 | |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | bool QHttp2ProtocolHandler::sendWINDOW_UPDATE(quint32 streamID, quint32 delta) |
| 517 | { |
| 518 | Q_ASSERT(m_socket); |
| 519 | |
| 520 | frameWriter.start(type: FrameType::WINDOW_UPDATE, flags: FrameFlag::EMPTY, streamID); |
| 521 | frameWriter.append(val: delta); |
| 522 | return frameWriter.write(socket&: *m_socket); |
| 523 | } |
| 524 | |
| 525 | bool QHttp2ProtocolHandler::sendRST_STREAM(quint32 streamID, quint32 errorCode) |
| 526 | { |
| 527 | Q_ASSERT(m_socket); |
| 528 | |
| 529 | frameWriter.start(type: FrameType::RST_STREAM, flags: FrameFlag::EMPTY, streamID); |
| 530 | frameWriter.append(val: errorCode); |
| 531 | return frameWriter.write(socket&: *m_socket); |
| 532 | } |
| 533 | |
| 534 | bool QHttp2ProtocolHandler::sendGOAWAY(quint32 errorCode) |
| 535 | { |
| 536 | Q_ASSERT(m_socket); |
| 537 | |
| 538 | frameWriter.start(type: FrameType::GOAWAY, flags: FrameFlag::EMPTY, streamID: connectionStreamID); |
| 539 | frameWriter.append(val: quint32(connectionStreamID)); |
| 540 | frameWriter.append(val: errorCode); |
| 541 | return frameWriter.write(socket&: *m_socket); |
| 542 | } |
| 543 | |
| 544 | void QHttp2ProtocolHandler::handleDATA() |
| 545 | { |
| 546 | Q_ASSERT(inboundFrame.type() == FrameType::DATA); |
| 547 | |
| 548 | const auto streamID = inboundFrame.streamID(); |
| 549 | if (streamID == connectionStreamID) |
| 550 | return connectionError(errorCode: PROTOCOL_ERROR, message: "DATA on stream 0x0" ); |
| 551 | |
| 552 | if (!activeStreams.contains(key: streamID) && !streamWasReset(streamID)) |
| 553 | return connectionError(errorCode: ENHANCE_YOUR_CALM, message: "DATA on invalid stream" ); |
| 554 | |
| 555 | if (qint32(inboundFrame.payloadSize()) > sessionReceiveWindowSize) |
| 556 | return connectionError(errorCode: FLOW_CONTROL_ERROR, message: "Flow control error" ); |
| 557 | |
| 558 | sessionReceiveWindowSize -= inboundFrame.payloadSize(); |
| 559 | |
| 560 | auto it = activeStreams.find(key: streamID); |
| 561 | if (it != activeStreams.end()) { |
| 562 | Stream &stream = it.value(); |
| 563 | |
| 564 | if (qint32(inboundFrame.payloadSize()) > stream.recvWindow) { |
| 565 | finishStreamWithError(stream, error: QNetworkReply::ProtocolFailure, message: "flow control error"_L1 ); |
| 566 | sendRST_STREAM(streamID, errorCode: FLOW_CONTROL_ERROR); |
| 567 | markAsReset(streamID); |
| 568 | deleteActiveStream(streamID); |
| 569 | } else { |
| 570 | stream.recvWindow -= inboundFrame.payloadSize(); |
| 571 | // Uncompress data if needed and append it ... |
| 572 | updateStream(stream, dataFrame: inboundFrame); |
| 573 | |
| 574 | if (inboundFrame.flags().testFlag(flag: FrameFlag::END_STREAM)) { |
| 575 | finishStream(stream); |
| 576 | deleteActiveStream(streamID: stream.streamID); |
| 577 | } else if (stream.recvWindow < streamInitialReceiveWindowSize / 2) { |
| 578 | QMetaObject::invokeMethod(obj: this, member: "sendWINDOW_UPDATE" , c: Qt::QueuedConnection, |
| 579 | Q_ARG(quint32, stream.streamID), |
| 580 | Q_ARG(quint32, streamInitialReceiveWindowSize - stream.recvWindow)); |
| 581 | stream.recvWindow = streamInitialReceiveWindowSize; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (sessionReceiveWindowSize < maxSessionReceiveWindowSize / 2) { |
| 587 | QMetaObject::invokeMethod(obj: this, member: "sendWINDOW_UPDATE" , c: Qt::QueuedConnection, |
| 588 | Q_ARG(quint32, connectionStreamID), |
| 589 | Q_ARG(quint32, maxSessionReceiveWindowSize - sessionReceiveWindowSize)); |
| 590 | sessionReceiveWindowSize = maxSessionReceiveWindowSize; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void QHttp2ProtocolHandler::handleHEADERS() |
| 595 | { |
| 596 | Q_ASSERT(inboundFrame.type() == FrameType::HEADERS); |
| 597 | |
| 598 | const auto streamID = inboundFrame.streamID(); |
| 599 | if (streamID == connectionStreamID) |
| 600 | return connectionError(errorCode: PROTOCOL_ERROR, message: "HEADERS on 0x0 stream" ); |
| 601 | |
| 602 | if (!activeStreams.contains(key: streamID) && !streamWasReset(streamID)) |
| 603 | return connectionError(errorCode: ENHANCE_YOUR_CALM, message: "HEADERS on invalid stream" ); |
| 604 | |
| 605 | const auto flags = inboundFrame.flags(); |
| 606 | if (flags.testFlag(flag: FrameFlag::PRIORITY)) { |
| 607 | handlePRIORITY(); |
| 608 | if (goingAway) |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | const bool = flags.testFlag(flag: FrameFlag::END_HEADERS); |
| 613 | continuedFrames.clear(); |
| 614 | continuedFrames.push_back(x: std::move(inboundFrame)); |
| 615 | if (!endHeaders) { |
| 616 | continuationExpected = true; |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | handleContinuedHEADERS(); |
| 621 | } |
| 622 | |
| 623 | void QHttp2ProtocolHandler::handlePRIORITY() |
| 624 | { |
| 625 | Q_ASSERT(inboundFrame.type() == FrameType::PRIORITY || |
| 626 | inboundFrame.type() == FrameType::HEADERS); |
| 627 | |
| 628 | const auto streamID = inboundFrame.streamID(); |
| 629 | if (streamID == connectionStreamID) |
| 630 | return connectionError(errorCode: PROTOCOL_ERROR, message: "PIRORITY on 0x0 stream" ); |
| 631 | |
| 632 | if (!activeStreams.contains(key: streamID) && !streamWasReset(streamID)) |
| 633 | return connectionError(errorCode: ENHANCE_YOUR_CALM, message: "PRIORITY on invalid stream" ); |
| 634 | |
| 635 | quint32 streamDependency = 0; |
| 636 | uchar weight = 0; |
| 637 | const bool noErr = inboundFrame.priority(streamID: &streamDependency, weight: &weight); |
| 638 | Q_UNUSED(noErr); |
| 639 | Q_ASSERT(noErr); |
| 640 | |
| 641 | |
| 642 | const bool exclusive = streamDependency & 0x80000000; |
| 643 | streamDependency &= ~0x80000000; |
| 644 | |
| 645 | // Ignore this for now ... |
| 646 | // Can be used for streams (re)prioritization - 5.3 |
| 647 | Q_UNUSED(exclusive); |
| 648 | Q_UNUSED(weight); |
| 649 | } |
| 650 | |
| 651 | void QHttp2ProtocolHandler::handleRST_STREAM() |
| 652 | { |
| 653 | Q_ASSERT(inboundFrame.type() == FrameType::RST_STREAM); |
| 654 | |
| 655 | // "RST_STREAM frames MUST be associated with a stream. |
| 656 | // If a RST_STREAM frame is received with a stream identifier of 0x0, |
| 657 | // the recipient MUST treat this as a connection error (Section 5.4.1) |
| 658 | // of type PROTOCOL_ERROR. |
| 659 | const auto streamID = inboundFrame.streamID(); |
| 660 | if (streamID == connectionStreamID) |
| 661 | return connectionError(errorCode: PROTOCOL_ERROR, message: "RST_STREAM on 0x0" ); |
| 662 | |
| 663 | if (!(streamID & 0x1)) { |
| 664 | // RST_STREAM on a promised stream: |
| 665 | // since we do not keep track of such streams, |
| 666 | // just ignore. |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | if (streamID >= nextID) { |
| 671 | // "RST_STREAM frames MUST NOT be sent for a stream |
| 672 | // in the "idle" state. .. the recipient MUST treat this |
| 673 | // as a connection error (Section 5.4.1) of type PROTOCOL_ERROR." |
| 674 | return connectionError(errorCode: PROTOCOL_ERROR, message: "RST_STREAM on idle stream" ); |
| 675 | } |
| 676 | |
| 677 | if (!activeStreams.contains(key: streamID)) { |
| 678 | // 'closed' stream, ignore. |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | Q_ASSERT(inboundFrame.dataSize() == 4); |
| 683 | |
| 684 | Stream &stream = activeStreams[streamID]; |
| 685 | finishStreamWithError(stream, errorCode: qFromBigEndian<quint32>(src: inboundFrame.dataBegin())); |
| 686 | markAsReset(streamID: stream.streamID); |
| 687 | deleteActiveStream(streamID: stream.streamID); |
| 688 | } |
| 689 | |
| 690 | void QHttp2ProtocolHandler::handleSETTINGS() |
| 691 | { |
| 692 | // 6.5 SETTINGS. |
| 693 | Q_ASSERT(inboundFrame.type() == FrameType::SETTINGS); |
| 694 | |
| 695 | if (inboundFrame.streamID() != connectionStreamID) |
| 696 | return connectionError(errorCode: PROTOCOL_ERROR, message: "SETTINGS on invalid stream" ); |
| 697 | |
| 698 | if (inboundFrame.flags().testFlag(flag: FrameFlag::ACK)) { |
| 699 | if (!waitingForSettingsACK) |
| 700 | return connectionError(errorCode: PROTOCOL_ERROR, message: "unexpected SETTINGS ACK" ); |
| 701 | waitingForSettingsACK = false; |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | if (inboundFrame.dataSize()) { |
| 706 | auto src = inboundFrame.dataBegin(); |
| 707 | for (const uchar *end = src + inboundFrame.dataSize(); src != end; src += 6) { |
| 708 | const Settings identifier = Settings(qFromBigEndian<quint16>(src)); |
| 709 | const quint32 intVal = qFromBigEndian<quint32>(src: src + 2); |
| 710 | if (!acceptSetting(identifier, newValue: intVal)) { |
| 711 | // If not accepted - we finish with connectionError. |
| 712 | return; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | sendSETTINGS_ACK(); |
| 718 | } |
| 719 | |
| 720 | |
| 721 | void QHttp2ProtocolHandler::handlePUSH_PROMISE() |
| 722 | { |
| 723 | // 6.6 PUSH_PROMISE. |
| 724 | Q_ASSERT(inboundFrame.type() == FrameType::PUSH_PROMISE); |
| 725 | |
| 726 | if (!pushPromiseEnabled && prefaceSent && !waitingForSettingsACK) { |
| 727 | // This means, server ACKed our 'NO PUSH', |
| 728 | // but sent us PUSH_PROMISE anyway. |
| 729 | return connectionError(errorCode: PROTOCOL_ERROR, message: "unexpected PUSH_PROMISE frame" ); |
| 730 | } |
| 731 | |
| 732 | const auto streamID = inboundFrame.streamID(); |
| 733 | if (streamID == connectionStreamID) { |
| 734 | return connectionError(errorCode: PROTOCOL_ERROR, |
| 735 | message: "PUSH_PROMISE with invalid associated stream (0x0)" ); |
| 736 | } |
| 737 | |
| 738 | if (!activeStreams.contains(key: streamID) && !streamWasReset(streamID)) { |
| 739 | return connectionError(errorCode: ENHANCE_YOUR_CALM, |
| 740 | message: "PUSH_PROMISE with invalid associated stream" ); |
| 741 | } |
| 742 | |
| 743 | const auto reservedID = qFromBigEndian<quint32>(src: inboundFrame.dataBegin()); |
| 744 | if ((reservedID & 1) || reservedID <= lastPromisedID || |
| 745 | reservedID > Http2::lastValidStreamID) { |
| 746 | return connectionError(errorCode: PROTOCOL_ERROR, |
| 747 | message: "PUSH_PROMISE with invalid promised stream ID" ); |
| 748 | } |
| 749 | |
| 750 | lastPromisedID = reservedID; |
| 751 | |
| 752 | if (!pushPromiseEnabled) { |
| 753 | // "ignoring a PUSH_PROMISE frame causes the stream state to become |
| 754 | // indeterminate" - let's send RST_STREAM frame with REFUSE_STREAM code. |
| 755 | resetPromisedStream(pushPromiseFrame: inboundFrame, reason: Http2::REFUSE_STREAM); |
| 756 | } |
| 757 | |
| 758 | const bool = inboundFrame.flags().testFlag(flag: FrameFlag::END_HEADERS); |
| 759 | continuedFrames.clear(); |
| 760 | continuedFrames.push_back(x: std::move(inboundFrame)); |
| 761 | |
| 762 | if (!endHeaders) { |
| 763 | continuationExpected = true; |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | handleContinuedHEADERS(); |
| 768 | } |
| 769 | |
| 770 | void QHttp2ProtocolHandler::handlePING() |
| 771 | { |
| 772 | // Since we're implementing a client and not |
| 773 | // a server, we only reply to a PING, ACKing it. |
| 774 | Q_ASSERT(inboundFrame.type() == FrameType::PING); |
| 775 | Q_ASSERT(m_socket); |
| 776 | |
| 777 | if (inboundFrame.streamID() != connectionStreamID) |
| 778 | return connectionError(errorCode: PROTOCOL_ERROR, message: "PING on invalid stream" ); |
| 779 | |
| 780 | if (inboundFrame.flags() & FrameFlag::ACK) |
| 781 | return connectionError(errorCode: PROTOCOL_ERROR, message: "unexpected PING ACK" ); |
| 782 | |
| 783 | Q_ASSERT(inboundFrame.dataSize() == 8); |
| 784 | |
| 785 | frameWriter.start(type: FrameType::PING, flags: FrameFlag::ACK, streamID: connectionStreamID); |
| 786 | frameWriter.append(begin: inboundFrame.dataBegin(), end: inboundFrame.dataBegin() + 8); |
| 787 | frameWriter.write(socket&: *m_socket); |
| 788 | } |
| 789 | |
| 790 | void QHttp2ProtocolHandler::handleGOAWAY() |
| 791 | { |
| 792 | // 6.8 GOAWAY |
| 793 | |
| 794 | Q_ASSERT(inboundFrame.type() == FrameType::GOAWAY); |
| 795 | // "An endpoint MUST treat a GOAWAY frame with a stream identifier |
| 796 | // other than 0x0 as a connection error (Section 5.4.1) of type PROTOCOL_ERROR." |
| 797 | if (inboundFrame.streamID() != connectionStreamID) |
| 798 | return connectionError(errorCode: PROTOCOL_ERROR, message: "GOAWAY on invalid stream" ); |
| 799 | |
| 800 | const auto src = inboundFrame.dataBegin(); |
| 801 | quint32 lastStreamID = qFromBigEndian<quint32>(src); |
| 802 | const quint32 errorCode = qFromBigEndian<quint32>(src: src + 4); |
| 803 | |
| 804 | if (!lastStreamID) { |
| 805 | // "The last stream identifier can be set to 0 if no |
| 806 | // streams were processed." |
| 807 | lastStreamID = 1; |
| 808 | } else if (!(lastStreamID & 0x1)) { |
| 809 | // 5.1.1 - we (client) use only odd numbers as stream identifiers. |
| 810 | return connectionError(errorCode: PROTOCOL_ERROR, message: "GOAWAY with invalid last stream ID" ); |
| 811 | } else if (lastStreamID >= nextID) { |
| 812 | // "A server that is attempting to gracefully shut down a connection SHOULD |
| 813 | // send an initial GOAWAY frame with the last stream identifier set to 2^31-1 |
| 814 | // and a NO_ERROR code." |
| 815 | if (lastStreamID != Http2::lastValidStreamID || errorCode != HTTP2_NO_ERROR) |
| 816 | return connectionError(errorCode: PROTOCOL_ERROR, message: "GOAWAY invalid stream/error code" ); |
| 817 | } else { |
| 818 | lastStreamID += 2; |
| 819 | } |
| 820 | |
| 821 | goingAway = true; |
| 822 | |
| 823 | // For the requests (and streams) we did not start yet, we have to report an |
| 824 | // error. |
| 825 | m_channel->emitFinishedWithError(error: QNetworkReply::ProtocolUnknownError, |
| 826 | message: "GOAWAY received, cannot start a request" ); |
| 827 | // Also, prevent further calls to sendRequest: |
| 828 | m_channel->h2RequestsToSend.clear(); |
| 829 | |
| 830 | QNetworkReply::NetworkError error = QNetworkReply::NoError; |
| 831 | QString message; |
| 832 | qt_error(errorCode, error, errorString&: message); |
| 833 | |
| 834 | // Even if the GOAWAY frame contains NO_ERROR we must send an error |
| 835 | // when terminating streams to ensure users can distinguish from a |
| 836 | // successful completion. |
| 837 | if (errorCode == HTTP2_NO_ERROR) { |
| 838 | error = QNetworkReply::ContentReSendError; |
| 839 | message = "Server stopped accepting new streams before this stream was established"_L1 ; |
| 840 | } |
| 841 | |
| 842 | for (quint32 id = lastStreamID; id < nextID; id += 2) { |
| 843 | const auto it = activeStreams.find(key: id); |
| 844 | if (it != activeStreams.end()) { |
| 845 | Stream &stream = *it; |
| 846 | finishStreamWithError(stream, error, message); |
| 847 | markAsReset(streamID: id); |
| 848 | deleteActiveStream(streamID: id); |
| 849 | } else { |
| 850 | removeFromSuspended(streamID: id); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if (!activeStreams.size()) |
| 855 | closeSession(); |
| 856 | } |
| 857 | |
| 858 | void QHttp2ProtocolHandler::handleWINDOW_UPDATE() |
| 859 | { |
| 860 | Q_ASSERT(inboundFrame.type() == FrameType::WINDOW_UPDATE); |
| 861 | |
| 862 | |
| 863 | const quint32 delta = qFromBigEndian<quint32>(src: inboundFrame.dataBegin()); |
| 864 | const bool valid = delta && delta <= quint32(std::numeric_limits<qint32>::max()); |
| 865 | const auto streamID = inboundFrame.streamID(); |
| 866 | |
| 867 | if (streamID == Http2::connectionStreamID) { |
| 868 | qint32 sum = 0; |
| 869 | if (!valid || qAddOverflow(v1: sessionSendWindowSize, v2: qint32(delta), r: &sum)) |
| 870 | return connectionError(errorCode: PROTOCOL_ERROR, message: "WINDOW_UPDATE invalid delta" ); |
| 871 | sessionSendWindowSize = sum; |
| 872 | } else { |
| 873 | auto it = activeStreams.find(key: streamID); |
| 874 | if (it == activeStreams.end()) { |
| 875 | // WINDOW_UPDATE on closed streams can be ignored. |
| 876 | return; |
| 877 | } |
| 878 | Stream &stream = it.value(); |
| 879 | qint32 sum = 0; |
| 880 | if (!valid || qAddOverflow(v1: stream.sendWindow, v2: qint32(delta), r: &sum)) { |
| 881 | finishStreamWithError(stream, error: QNetworkReply::ProtocolFailure, |
| 882 | message: "invalid WINDOW_UPDATE delta"_L1 ); |
| 883 | sendRST_STREAM(streamID, errorCode: PROTOCOL_ERROR); |
| 884 | markAsReset(streamID); |
| 885 | deleteActiveStream(streamID); |
| 886 | return; |
| 887 | } |
| 888 | stream.sendWindow = sum; |
| 889 | } |
| 890 | |
| 891 | // Since we're in _q_receiveReply at the moment, let's first handle other |
| 892 | // frames and resume suspended streams (if any) == start sending our own frame |
| 893 | // after handling these frames, since one them can be e.g. GOAWAY. |
| 894 | QMetaObject::invokeMethod(obj: this, member: "resumeSuspendedStreams" , c: Qt::QueuedConnection); |
| 895 | } |
| 896 | |
| 897 | void QHttp2ProtocolHandler::handleCONTINUATION() |
| 898 | { |
| 899 | Q_ASSERT(inboundFrame.type() == FrameType::CONTINUATION); |
| 900 | Q_ASSERT(continuedFrames.size()); // HEADERS frame must be already in. |
| 901 | |
| 902 | if (inboundFrame.streamID() != continuedFrames.front().streamID()) |
| 903 | return connectionError(errorCode: PROTOCOL_ERROR, message: "CONTINUATION on invalid stream" ); |
| 904 | |
| 905 | const bool = inboundFrame.flags().testFlag(flag: FrameFlag::END_HEADERS); |
| 906 | continuedFrames.push_back(x: std::move(inboundFrame)); |
| 907 | |
| 908 | if (!endHeaders) |
| 909 | return; |
| 910 | |
| 911 | continuationExpected = false; |
| 912 | handleContinuedHEADERS(); |
| 913 | } |
| 914 | |
| 915 | void QHttp2ProtocolHandler::handleContinuedHEADERS() |
| 916 | { |
| 917 | // 'Continued' HEADERS can be: the initial HEADERS/PUSH_PROMISE frame |
| 918 | // with/without END_HEADERS flag set plus, if no END_HEADERS flag, |
| 919 | // a sequence of one or more CONTINUATION frames. |
| 920 | Q_ASSERT(continuedFrames.size()); |
| 921 | const auto firstFrameType = continuedFrames[0].type(); |
| 922 | Q_ASSERT(firstFrameType == FrameType::HEADERS || |
| 923 | firstFrameType == FrameType::PUSH_PROMISE); |
| 924 | |
| 925 | const auto streamID = continuedFrames[0].streamID(); |
| 926 | |
| 927 | const auto streamIt = activeStreams.find(key: streamID); |
| 928 | if (firstFrameType == FrameType::HEADERS) { |
| 929 | if (streamIt != activeStreams.end()) { |
| 930 | Stream &stream = streamIt.value(); |
| 931 | if (stream.state != Stream::halfClosedLocal |
| 932 | && stream.state != Stream::remoteReserved |
| 933 | && stream.state != Stream::open) { |
| 934 | // We can receive HEADERS on streams initiated by our requests |
| 935 | // (these streams are in halfClosedLocal or open state) or |
| 936 | // remote-reserved streams from a server's PUSH_PROMISE. |
| 937 | finishStreamWithError(stream, error: QNetworkReply::ProtocolFailure, |
| 938 | message: "HEADERS on invalid stream"_L1 ); |
| 939 | sendRST_STREAM(streamID, errorCode: CANCEL); |
| 940 | markAsReset(streamID); |
| 941 | deleteActiveStream(streamID); |
| 942 | return; |
| 943 | } |
| 944 | } else if (!streamWasReset(streamID)) { |
| 945 | return connectionError(errorCode: PROTOCOL_ERROR, message: "HEADERS on invalid stream" ); |
| 946 | } |
| 947 | // Else: we cannot just ignore our peer's HEADERS frames - they change |
| 948 | // HPACK context - even though the stream was reset; apparently the peer |
| 949 | // has yet to see the reset. |
| 950 | } |
| 951 | |
| 952 | std::vector<uchar> hpackBlock(Http2::assemble_hpack_block(frames: continuedFrames)); |
| 953 | const bool = !hpackBlock.empty(); |
| 954 | if (hasHeaderFields) { |
| 955 | HPack::BitIStream inputStream{&hpackBlock[0], &hpackBlock[0] + hpackBlock.size()}; |
| 956 | if (!decoder.decodeHeaderFields(inputStream)) |
| 957 | return connectionError(errorCode: COMPRESSION_ERROR, message: "HPACK decompression failed" ); |
| 958 | } else if (firstFrameType == FrameType::PUSH_PROMISE) { |
| 959 | // It could be a PRIORITY sent in HEADERS - already handled by this |
| 960 | // point in handleHEADERS. If it was PUSH_PROMISE (HTTP/2 8.2.1): |
| 961 | // "The header fields in PUSH_PROMISE and any subsequent CONTINUATION |
| 962 | // frames MUST be a valid and complete set of request header fields |
| 963 | // (Section 8.1.2.3) ... If a client receives a PUSH_PROMISE that does |
| 964 | // not include a complete and valid set of header fields or the :method |
| 965 | // pseudo-header field identifies a method that is not safe, it MUST |
| 966 | // respond with a stream error (Section 5.4.2) of type PROTOCOL_ERROR." |
| 967 | resetPromisedStream(pushPromiseFrame: continuedFrames[0], reason: Http2::PROTOCOL_ERROR); |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | switch (firstFrameType) { |
| 972 | case FrameType::HEADERS: |
| 973 | if (streamIt != activeStreams.end()) { |
| 974 | Stream &stream = streamIt.value(); |
| 975 | if (hasHeaderFields) |
| 976 | updateStream(stream, headers: decoder.decodedHeader()); |
| 977 | // Needs to resend the request; we should finish and delete the current stream |
| 978 | const bool needResend = stream.request().d->needResendWithCredentials; |
| 979 | // No DATA frames. Or needs to resend. |
| 980 | if (continuedFrames[0].flags() & FrameFlag::END_STREAM || needResend) { |
| 981 | finishStream(stream); |
| 982 | deleteActiveStream(streamID: stream.streamID); |
| 983 | } |
| 984 | } |
| 985 | break; |
| 986 | case FrameType::PUSH_PROMISE: |
| 987 | if (!tryReserveStream(pushPromiseFrame: continuedFrames[0], requestHeader: decoder.decodedHeader())) |
| 988 | resetPromisedStream(pushPromiseFrame: continuedFrames[0], reason: Http2::PROTOCOL_ERROR); |
| 989 | break; |
| 990 | default: |
| 991 | break; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | bool QHttp2ProtocolHandler::acceptSetting(Http2::Settings identifier, quint32 newValue) |
| 996 | { |
| 997 | if (identifier == Settings::HEADER_TABLE_SIZE_ID) { |
| 998 | if (newValue > maxAcceptableTableSize) { |
| 999 | connectionError(errorCode: PROTOCOL_ERROR, message: "SETTINGS invalid table size" ); |
| 1000 | return false; |
| 1001 | } |
| 1002 | if (!pendingTableSizeUpdates[0] && encoder.dynamicTableCapacity() == newValue) |
| 1003 | return true; // No change, no need to update. |
| 1004 | |
| 1005 | if (pendingTableSizeUpdates[0].value_or(u: std::numeric_limits<quint32>::max()) >= newValue) { |
| 1006 | pendingTableSizeUpdates[0] = newValue; |
| 1007 | pendingTableSizeUpdates[1].reset(); // 0 is the latest _and_ smallest, so we don't need 1 |
| 1008 | } else { |
| 1009 | pendingTableSizeUpdates[1] = newValue; // newValue was larger than 0, so it goes to 1 |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | if (identifier == Settings::INITIAL_WINDOW_SIZE_ID) { |
| 1014 | // For every active stream - adjust its window |
| 1015 | // (and handle possible overflows as errors). |
| 1016 | if (newValue > quint32(std::numeric_limits<qint32>::max())) { |
| 1017 | connectionError(errorCode: FLOW_CONTROL_ERROR, message: "SETTINGS invalid initial window size" ); |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
| 1021 | const qint32 delta = qint32(newValue) - streamInitialSendWindowSize; |
| 1022 | streamInitialSendWindowSize = newValue; |
| 1023 | |
| 1024 | std::vector<quint32> brokenStreams; |
| 1025 | brokenStreams.reserve(n: activeStreams.size()); |
| 1026 | for (auto &stream : activeStreams) { |
| 1027 | qint32 sum = 0; |
| 1028 | if (qAddOverflow(v1: stream.sendWindow, v2: delta, r: &sum)) { |
| 1029 | brokenStreams.push_back(x: stream.streamID); |
| 1030 | continue; |
| 1031 | } |
| 1032 | stream.sendWindow = sum; |
| 1033 | } |
| 1034 | |
| 1035 | for (auto id : brokenStreams) { |
| 1036 | auto &stream = activeStreams[id]; |
| 1037 | finishStreamWithError(stream, error: QNetworkReply::ProtocolFailure, |
| 1038 | message: "SETTINGS window overflow"_L1 ); |
| 1039 | sendRST_STREAM(streamID: id, errorCode: PROTOCOL_ERROR); |
| 1040 | markAsReset(streamID: id); |
| 1041 | deleteActiveStream(streamID: id); |
| 1042 | } |
| 1043 | |
| 1044 | QMetaObject::invokeMethod(obj: this, member: "resumeSuspendedStreams" , c: Qt::QueuedConnection); |
| 1045 | } |
| 1046 | |
| 1047 | if (identifier == Settings::MAX_CONCURRENT_STREAMS_ID) |
| 1048 | maxConcurrentStreams = newValue; |
| 1049 | |
| 1050 | if (identifier == Settings::MAX_FRAME_SIZE_ID) { |
| 1051 | if (newValue < Http2::minPayloadLimit || newValue > Http2::maxPayloadSize) { |
| 1052 | connectionError(errorCode: PROTOCOL_ERROR, message: "SETTINGS max frame size is out of range" ); |
| 1053 | return false; |
| 1054 | } |
| 1055 | maxFrameSize = newValue; |
| 1056 | } |
| 1057 | |
| 1058 | if (identifier == Settings::MAX_HEADER_LIST_SIZE_ID) { |
| 1059 | // We just remember this value, it can later |
| 1060 | // prevent us from sending any request (and this |
| 1061 | // will end up in request/reply error). |
| 1062 | maxHeaderListSize = newValue; |
| 1063 | } |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | void QHttp2ProtocolHandler::updateStream(Stream &stream, const HPack::HttpHeader &, |
| 1069 | Qt::ConnectionType connectionType) |
| 1070 | { |
| 1071 | const auto httpReply = stream.reply(); |
| 1072 | auto &httpRequest = stream.request(); |
| 1073 | Q_ASSERT(httpReply || stream.state == Stream::remoteReserved); |
| 1074 | |
| 1075 | if (!httpReply) { |
| 1076 | // It's a PUSH_PROMISEd HEADERS, no actual request/reply |
| 1077 | // exists yet, we have to cache this data for a future |
| 1078 | // (potential) request. |
| 1079 | |
| 1080 | // TODO: the part with assignment is not especially cool |
| 1081 | // or beautiful, good that at least QByteArray is implicitly |
| 1082 | // sharing data. To be refactored (std::move). |
| 1083 | Q_ASSERT(promisedData.contains(stream.key)); |
| 1084 | PushPromise &promise = promisedData[stream.key]; |
| 1085 | promise.responseHeader = headers; |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | const auto httpReplyPrivate = httpReply->d_func(); |
| 1090 | |
| 1091 | // For HTTP/1 'location' is handled (and redirect URL set) when a protocol |
| 1092 | // handler emits channel->allDone(). Http/2 protocol handler never emits |
| 1093 | // allDone, since we have many requests multiplexed in one channel at any |
| 1094 | // moment and we are probably not done yet. So we extract url and set it |
| 1095 | // here, if needed. |
| 1096 | int statusCode = 0; |
| 1097 | for (const auto &pair : headers) { |
| 1098 | const auto &name = pair.name; |
| 1099 | const auto value = QByteArrayView(pair.value); |
| 1100 | |
| 1101 | // TODO: part of this code copies what SPDY protocol handler does when |
| 1102 | // processing headers. Binary nature of HTTP/2 and SPDY saves us a lot |
| 1103 | // of parsing and related errors/bugs, but it would be nice to have |
| 1104 | // more detailed validation of headers. |
| 1105 | if (name == ":status" ) { |
| 1106 | statusCode = value.left(n: 3).toInt(); |
| 1107 | httpReply->setStatusCode(statusCode); |
| 1108 | m_channel->lastStatus = statusCode; // Mostly useless for http/2, needed for auth |
| 1109 | httpReply->setReasonPhrase(QString::fromLatin1(ba: value.mid(pos: 4))); |
| 1110 | } else if (name == ":version" ) { |
| 1111 | httpReply->setMajorVersion(value.at(n: 5) - '0'); |
| 1112 | httpReply->setMinorVersion(value.at(n: 7) - '0'); |
| 1113 | } else if (name == "content-length" ) { |
| 1114 | bool ok = false; |
| 1115 | const qlonglong length = value.toLongLong(ok: &ok); |
| 1116 | if (ok) |
| 1117 | httpReply->setContentLength(length); |
| 1118 | } else { |
| 1119 | const auto binder = name == "set-cookie" ? QByteArrayView("\n" ) : QByteArrayView(", " ); |
| 1120 | httpReply->appendHeaderField(name, data: QByteArray(pair.value).replace(before: '\0', after: binder)); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | // Discard all informational (1xx) replies with the exception of 101. |
| 1125 | // Also see RFC 9110 (Chapter 15.2) |
| 1126 | if (statusCode == 100 || (102 <= statusCode && statusCode <= 199)) { |
| 1127 | httpReplyPrivate->clearHttpLayerInformation(); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | if (QHttpNetworkReply::isHttpRedirect(statusCode) && httpRequest.isFollowRedirects()) { |
| 1132 | QHttpNetworkConnectionPrivate::ParseRedirectResult result = |
| 1133 | m_connection->d_func()->parseRedirectResponse(reply: httpReply); |
| 1134 | if (result.errorCode != QNetworkReply::NoError) { |
| 1135 | auto errorString = m_connection->d_func()->errorDetail(errorCode: result.errorCode, socket: m_socket); |
| 1136 | finishStreamWithError(stream, error: result.errorCode, message: errorString); |
| 1137 | sendRST_STREAM(streamID: stream.streamID, errorCode: INTERNAL_ERROR); |
| 1138 | markAsReset(streamID: stream.streamID); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | if (result.redirectUrl.isValid()) |
| 1143 | httpReply->setRedirectUrl(result.redirectUrl); |
| 1144 | } |
| 1145 | |
| 1146 | if (httpReplyPrivate->isCompressed() && httpRequest.d->autoDecompress) |
| 1147 | httpReplyPrivate->removeAutoDecompressHeader(); |
| 1148 | |
| 1149 | if (QHttpNetworkReply::isHttpRedirect(statusCode)) { |
| 1150 | // Note: This status code can trigger uploadByteDevice->reset() in |
| 1151 | // QHttpNetworkConnectionChannel::handleStatus. Alas, we have no single |
| 1152 | // request/reply, we multiplex several requests and thus we never simply |
| 1153 | // call 'handleStatus'. If we have a byte-device - we try to reset it |
| 1154 | // here, we don't (and can't) handle any error during reset operation. |
| 1155 | if (stream.data()) { |
| 1156 | stream.data()->reset(); |
| 1157 | httpReplyPrivate->totallyUploadedData = 0; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | if (connectionType == Qt::DirectConnection) |
| 1162 | emit httpReply->headerChanged(); |
| 1163 | else |
| 1164 | QMetaObject::invokeMethod(obj: httpReply, member: "headerChanged" , c: connectionType); |
| 1165 | } |
| 1166 | |
| 1167 | void QHttp2ProtocolHandler::updateStream(Stream &stream, const Frame &frame, |
| 1168 | Qt::ConnectionType connectionType) |
| 1169 | { |
| 1170 | Q_ASSERT(frame.type() == FrameType::DATA); |
| 1171 | auto httpReply = stream.reply(); |
| 1172 | Q_ASSERT(httpReply || stream.state == Stream::remoteReserved); |
| 1173 | |
| 1174 | if (!httpReply) { |
| 1175 | Q_ASSERT(promisedData.contains(stream.key)); |
| 1176 | PushPromise &promise = promisedData[stream.key]; |
| 1177 | // TODO: refactor this to use std::move. |
| 1178 | promise.dataFrames.push_back(x: frame); |
| 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | if (const auto length = frame.dataSize()) { |
| 1183 | const char *data = reinterpret_cast<const char *>(frame.dataBegin()); |
| 1184 | auto replyPrivate = httpReply->d_func(); |
| 1185 | |
| 1186 | replyPrivate->totalProgress += length; |
| 1187 | |
| 1188 | replyPrivate->responseData.append(bd: QByteArray(data, length)); |
| 1189 | |
| 1190 | if (replyPrivate->shouldEmitSignals()) { |
| 1191 | if (connectionType == Qt::DirectConnection) { |
| 1192 | emit httpReply->readyRead(); |
| 1193 | emit httpReply->dataReadProgress(done: replyPrivate->totalProgress, |
| 1194 | total: replyPrivate->bodyLength); |
| 1195 | } else { |
| 1196 | QMetaObject::invokeMethod(obj: httpReply, member: "readyRead" , c: connectionType); |
| 1197 | QMetaObject::invokeMethod(obj: httpReply, member: "dataReadProgress" , c: connectionType, |
| 1198 | Q_ARG(qint64, replyPrivate->totalProgress), |
| 1199 | Q_ARG(qint64, replyPrivate->bodyLength)); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | // After calling this function, either the request will be re-sent or |
| 1206 | // the reply will be finishedWithError! Do not emit finished() or similar on the |
| 1207 | // reply after this! |
| 1208 | void QHttp2ProtocolHandler::handleAuthorization(Stream &stream) |
| 1209 | { |
| 1210 | auto *httpReply = stream.reply(); |
| 1211 | auto *httpReplyPrivate = httpReply->d_func(); |
| 1212 | auto &httpRequest = stream.request(); |
| 1213 | |
| 1214 | Q_ASSERT(httpReply && (httpReply->statusCode() == 401 || httpReply->statusCode() == 407)); |
| 1215 | |
| 1216 | const auto handleAuth = [&, this](QByteArrayView authField, bool isProxy) -> bool { |
| 1217 | Q_ASSERT(httpReply); |
| 1218 | const QByteArrayView auth = authField.trimmed(); |
| 1219 | if (auth.startsWith(other: "Negotiate" ) || auth.startsWith(other: "NTLM" )) { |
| 1220 | // @todo: We're supposed to fall back to http/1.1: |
| 1221 | // https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-10/http2-on-iis#when-is-http2-not-supported |
| 1222 | // "Windows authentication (NTLM/Kerberos/Negotiate) is not supported with HTTP/2. |
| 1223 | // In this case IIS will fall back to HTTP/1.1." |
| 1224 | // Though it might be OK to ignore this. The server shouldn't let us connect with |
| 1225 | // HTTP/2 if it doesn't support us using it. |
| 1226 | return false; |
| 1227 | } |
| 1228 | // Somewhat mimics parts of QHttpNetworkConnectionChannel::handleStatus |
| 1229 | bool resend = false; |
| 1230 | const bool authenticateHandled = m_connection->d_func()->handleAuthenticateChallenge( |
| 1231 | socket: m_socket, reply: httpReply, isProxy, resend); |
| 1232 | if (authenticateHandled) { |
| 1233 | if (resend) { |
| 1234 | httpReply->d_func()->eraseData(); |
| 1235 | // Add the request back in queue, we'll retry later now that |
| 1236 | // we've gotten some username/password set on it: |
| 1237 | httpRequest.d->needResendWithCredentials = true; |
| 1238 | m_channel->h2RequestsToSend.insert(key: httpRequest.priority(), value: stream.httpPair); |
| 1239 | httpReply->d_func()->clearHeaders(); |
| 1240 | // If we have data we were uploading we need to reset it: |
| 1241 | if (stream.data()) { |
| 1242 | stream.data()->reset(); |
| 1243 | httpReplyPrivate->totallyUploadedData = 0; |
| 1244 | } |
| 1245 | // We automatically try to send new requests when the stream is |
| 1246 | // closed, so we don't need to call sendRequest ourselves. |
| 1247 | return true; |
| 1248 | } // else: we're just not resending the request. |
| 1249 | // @note In the http/1.x case we (at time of writing) call close() |
| 1250 | // for the connectionChannel (which is a bit weird, we could surely |
| 1251 | // reuse the open socket outside "connection:close"?), but in http2 |
| 1252 | // we only have one channel, so we won't close anything. |
| 1253 | } else { |
| 1254 | // No authentication header or authentication isn't supported, but |
| 1255 | // we got a 401/407 so we cannot succeed. We need to emit signals |
| 1256 | // for headers and data, and then finishWithError. |
| 1257 | emit httpReply->headerChanged(); |
| 1258 | emit httpReply->readyRead(); |
| 1259 | QNetworkReply::NetworkError error = httpReply->statusCode() == 401 |
| 1260 | ? QNetworkReply::AuthenticationRequiredError |
| 1261 | : QNetworkReply::ProxyAuthenticationRequiredError; |
| 1262 | finishStreamWithError(stream, error: QNetworkReply::AuthenticationRequiredError, |
| 1263 | message: m_connection->d_func()->errorDetail(errorCode: error, socket: m_socket)); |
| 1264 | } |
| 1265 | return false; |
| 1266 | }; |
| 1267 | |
| 1268 | // These statuses would in HTTP/1.1 be handled by |
| 1269 | // QHttpNetworkConnectionChannel::handleStatus. But because h2 has |
| 1270 | // multiple streams/requests in a single channel this structure does not |
| 1271 | // map properly to that function. |
| 1272 | bool authOk = true; |
| 1273 | switch (httpReply->statusCode()) { |
| 1274 | case 401: |
| 1275 | authOk = handleAuth(httpReply->headerField(name: "www-authenticate" ), false); |
| 1276 | break; |
| 1277 | case 407: |
| 1278 | authOk = handleAuth(httpReply->headerField(name: "proxy-authenticate" ), true); |
| 1279 | break; |
| 1280 | default: |
| 1281 | Q_UNREACHABLE(); |
| 1282 | } |
| 1283 | if (authOk) { |
| 1284 | markAsReset(streamID: stream.streamID); |
| 1285 | deleteActiveStream(streamID: stream.streamID); |
| 1286 | } // else: errors handled inside handleAuth |
| 1287 | } |
| 1288 | |
| 1289 | // Called when we have received a frame with the END_STREAM flag set |
| 1290 | void QHttp2ProtocolHandler::finishStream(Stream &stream, Qt::ConnectionType connectionType) |
| 1291 | { |
| 1292 | Q_ASSERT(stream.state == Stream::remoteReserved || stream.reply()); |
| 1293 | |
| 1294 | stream.state = Stream::closed; |
| 1295 | auto httpReply = stream.reply(); |
| 1296 | if (httpReply) { |
| 1297 | int statusCode = httpReply->statusCode(); |
| 1298 | if (statusCode == 401 || statusCode == 407) { |
| 1299 | // handleAuthorization will either re-send the request or |
| 1300 | // finishWithError. In either case we don't want to emit finished |
| 1301 | // here. |
| 1302 | handleAuthorization(stream); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | httpReply->disconnect(receiver: this); |
| 1307 | if (stream.data()) |
| 1308 | stream.data()->disconnect(receiver: this); |
| 1309 | |
| 1310 | if (!stream.request().d->needResendWithCredentials) { |
| 1311 | if (connectionType == Qt::DirectConnection) |
| 1312 | emit httpReply->finished(); |
| 1313 | else |
| 1314 | QMetaObject::invokeMethod(obj: httpReply, member: "finished" , c: connectionType); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | qCDebug(QT_HTTP2) << "stream" << stream.streamID << "closed" ; |
| 1319 | } |
| 1320 | |
| 1321 | void QHttp2ProtocolHandler::finishStreamWithError(Stream &stream, quint32 errorCode) |
| 1322 | { |
| 1323 | QNetworkReply::NetworkError error = QNetworkReply::NoError; |
| 1324 | QString message; |
| 1325 | qt_error(errorCode, error, errorString&: message); |
| 1326 | finishStreamWithError(stream, error, message); |
| 1327 | } |
| 1328 | |
| 1329 | void QHttp2ProtocolHandler::finishStreamWithError(Stream &stream, QNetworkReply::NetworkError error, |
| 1330 | const QString &message) |
| 1331 | { |
| 1332 | Q_ASSERT(stream.state == Stream::remoteReserved || stream.reply()); |
| 1333 | |
| 1334 | stream.state = Stream::closed; |
| 1335 | if (auto httpReply = stream.reply()) { |
| 1336 | httpReply->disconnect(receiver: this); |
| 1337 | if (stream.data()) |
| 1338 | stream.data()->disconnect(receiver: this); |
| 1339 | |
| 1340 | // TODO: error message must be translated!!! (tr) |
| 1341 | emit httpReply->finishedWithError(errorCode: error, detail: message); |
| 1342 | } |
| 1343 | |
| 1344 | qCWarning(QT_HTTP2) << "stream" << stream.streamID |
| 1345 | << "finished with error:" << message; |
| 1346 | } |
| 1347 | |
| 1348 | quint32 QHttp2ProtocolHandler::createNewStream(const HttpMessagePair &message, bool uploadDone) |
| 1349 | { |
| 1350 | const qint32 newStreamID = allocateStreamID(); |
| 1351 | if (!newStreamID) |
| 1352 | return 0; |
| 1353 | |
| 1354 | Q_ASSERT(!activeStreams.contains(newStreamID)); |
| 1355 | |
| 1356 | const auto reply = message.second; |
| 1357 | const auto replyPrivate = reply->d_func(); |
| 1358 | replyPrivate->connection = m_connection; |
| 1359 | replyPrivate->connectionChannel = m_channel; |
| 1360 | reply->setHttp2WasUsed(true); |
| 1361 | streamIDs.insert(key: reply, value: newStreamID); |
| 1362 | connect(sender: reply, SIGNAL(destroyed(QObject*)), |
| 1363 | receiver: this, SLOT(_q_replyDestroyed(QObject*))); |
| 1364 | |
| 1365 | const Stream newStream(message, newStreamID, |
| 1366 | streamInitialSendWindowSize, |
| 1367 | streamInitialReceiveWindowSize); |
| 1368 | |
| 1369 | if (!uploadDone) { |
| 1370 | if (auto src = newStream.data()) { |
| 1371 | connect(sender: src, SIGNAL(readyRead()), receiver: this, |
| 1372 | SLOT(_q_uploadDataReadyRead()), Qt::QueuedConnection); |
| 1373 | connect(sender: src, signal: &QHttp2ProtocolHandler::destroyed, |
| 1374 | context: this, slot: &QHttp2ProtocolHandler::_q_uploadDataDestroyed); |
| 1375 | streamIDs.insert(key: src, value: newStreamID); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | QMetaObject::invokeMethod(obj: reply, member: "requestSent" , c: Qt::QueuedConnection); |
| 1380 | |
| 1381 | activeStreams.insert(key: newStreamID, value: newStream); |
| 1382 | |
| 1383 | return newStreamID; |
| 1384 | } |
| 1385 | |
| 1386 | void QHttp2ProtocolHandler::addToSuspended(Stream &stream) |
| 1387 | { |
| 1388 | qCDebug(QT_HTTP2) << "stream" << stream.streamID |
| 1389 | << "suspended by flow control" ; |
| 1390 | const auto priority = stream.priority(); |
| 1391 | Q_ASSERT(int(priority) >= 0 && int(priority) < 3); |
| 1392 | suspendedStreams[priority].push_back(x: stream.streamID); |
| 1393 | } |
| 1394 | |
| 1395 | void QHttp2ProtocolHandler::markAsReset(quint32 streamID) |
| 1396 | { |
| 1397 | Q_ASSERT(streamID); |
| 1398 | |
| 1399 | qCDebug(QT_HTTP2) << "stream" << streamID << "was reset" ; |
| 1400 | // This part is quite tricky: I have to clear this set |
| 1401 | // so that it does not become tOOO big. |
| 1402 | if (recycledStreams.size() > maxRecycledStreams) { |
| 1403 | // At least, I'm erasing the oldest first ... |
| 1404 | recycledStreams.erase(first: recycledStreams.begin(), |
| 1405 | last: recycledStreams.begin() + |
| 1406 | recycledStreams.size() / 2); |
| 1407 | } |
| 1408 | |
| 1409 | const auto it = std::lower_bound(first: recycledStreams.begin(), last: recycledStreams.end(), |
| 1410 | val: streamID); |
| 1411 | if (it != recycledStreams.end() && *it == streamID) |
| 1412 | return; |
| 1413 | |
| 1414 | recycledStreams.insert(position: it, x: streamID); |
| 1415 | } |
| 1416 | |
| 1417 | quint32 QHttp2ProtocolHandler::popStreamToResume() |
| 1418 | { |
| 1419 | quint32 streamID = connectionStreamID; |
| 1420 | using QNR = QHttpNetworkRequest; |
| 1421 | const QNR::Priority ranks[] = {QNR::HighPriority, |
| 1422 | QNR::NormalPriority, |
| 1423 | QNR::LowPriority}; |
| 1424 | |
| 1425 | for (const QNR::Priority rank : ranks) { |
| 1426 | auto &queue = suspendedStreams[rank]; |
| 1427 | auto it = queue.begin(); |
| 1428 | for (; it != queue.end(); ++it) { |
| 1429 | auto stream = activeStreams.constFind(key: *it); |
| 1430 | if (stream == activeStreams.cend()) |
| 1431 | continue; |
| 1432 | if (stream->sendWindow > 0) |
| 1433 | break; |
| 1434 | } |
| 1435 | |
| 1436 | if (it != queue.end()) { |
| 1437 | streamID = *it; |
| 1438 | queue.erase(position: it); |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | return streamID; |
| 1444 | } |
| 1445 | |
| 1446 | void QHttp2ProtocolHandler::removeFromSuspended(quint32 streamID) |
| 1447 | { |
| 1448 | for (auto &q : suspendedStreams) { |
| 1449 | q.erase(first: std::remove(first: q.begin(), last: q.end(), value: streamID), last: q.end()); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | void QHttp2ProtocolHandler::deleteActiveStream(quint32 streamID) |
| 1454 | { |
| 1455 | if (const auto it = activeStreams.constFind(key: streamID); it != activeStreams.cend()) { |
| 1456 | const Stream &stream = it.value(); |
| 1457 | if (stream.reply()) { |
| 1458 | stream.reply()->disconnect(receiver: this); |
| 1459 | streamIDs.remove(key: stream.reply()); |
| 1460 | } |
| 1461 | if (stream.data()) { |
| 1462 | stream.data()->disconnect(receiver: this); |
| 1463 | streamIDs.remove(key: stream.data()); |
| 1464 | } |
| 1465 | activeStreams.erase(it); |
| 1466 | } |
| 1467 | |
| 1468 | removeFromSuspended(streamID); |
| 1469 | if (m_channel->h2RequestsToSend.size()) |
| 1470 | QMetaObject::invokeMethod(obj: this, member: "sendRequest" , c: Qt::QueuedConnection); |
| 1471 | } |
| 1472 | |
| 1473 | bool QHttp2ProtocolHandler::streamWasReset(quint32 streamID) const |
| 1474 | { |
| 1475 | const auto it = std::lower_bound(first: recycledStreams.begin(), |
| 1476 | last: recycledStreams.end(), |
| 1477 | val: streamID); |
| 1478 | return it != recycledStreams.end() && *it == streamID; |
| 1479 | } |
| 1480 | |
| 1481 | void QHttp2ProtocolHandler::resumeSuspendedStreams() |
| 1482 | { |
| 1483 | while (sessionSendWindowSize > 0) { |
| 1484 | const auto streamID = popStreamToResume(); |
| 1485 | if (!streamID) |
| 1486 | return; |
| 1487 | |
| 1488 | auto it = activeStreams.find(key: streamID); |
| 1489 | if (it == activeStreams.end()) |
| 1490 | continue; |
| 1491 | Stream &stream = it.value(); |
| 1492 | |
| 1493 | if (!sendDATA(stream)) { |
| 1494 | finishStreamWithError(stream, error: QNetworkReply::UnknownNetworkError, |
| 1495 | message: "failed to send DATA"_L1 ); |
| 1496 | sendRST_STREAM(streamID, errorCode: INTERNAL_ERROR); |
| 1497 | markAsReset(streamID); |
| 1498 | deleteActiveStream(streamID); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | quint32 QHttp2ProtocolHandler::allocateStreamID() |
| 1504 | { |
| 1505 | // With protocol upgrade streamID == 1 will become |
| 1506 | // invalid. The logic must be updated. |
| 1507 | if (nextID > Http2::lastValidStreamID) |
| 1508 | return 0; |
| 1509 | |
| 1510 | const quint32 streamID = nextID; |
| 1511 | nextID += 2; |
| 1512 | |
| 1513 | return streamID; |
| 1514 | } |
| 1515 | |
| 1516 | bool QHttp2ProtocolHandler::tryReserveStream(const Http2::Frame &pushPromiseFrame, |
| 1517 | const HPack::HttpHeader &) |
| 1518 | { |
| 1519 | Q_ASSERT(pushPromiseFrame.type() == FrameType::PUSH_PROMISE); |
| 1520 | |
| 1521 | const auto url = HPack::makePromiseKeyUrl(requestHeader); |
| 1522 | if (!url.has_value()) |
| 1523 | return false; |
| 1524 | |
| 1525 | Q_ASSERT(activeStreams.contains(pushPromiseFrame.streamID())); |
| 1526 | const Stream &associatedStream = activeStreams[pushPromiseFrame.streamID()]; |
| 1527 | |
| 1528 | const auto associatedUrl = urlkey_from_request(request: associatedStream.request()); |
| 1529 | if (url->adjusted(options: QUrl::RemovePath) != associatedUrl.adjusted(options: QUrl::RemovePath)) |
| 1530 | return false; |
| 1531 | |
| 1532 | const auto urlKey = url->toString(); |
| 1533 | if (promisedData.contains(key: urlKey)) // duplicate push promise |
| 1534 | return false; |
| 1535 | |
| 1536 | const auto reservedID = qFromBigEndian<quint32>(src: pushPromiseFrame.dataBegin()); |
| 1537 | // By this time all sanity checks on reservedID were done already |
| 1538 | // in handlePUSH_PROMISE. We do not repeat them, only those below: |
| 1539 | Q_ASSERT(!activeStreams.contains(reservedID)); |
| 1540 | Q_ASSERT(!streamWasReset(reservedID)); |
| 1541 | |
| 1542 | auto &promise = promisedData[urlKey]; |
| 1543 | promise.reservedID = reservedID; |
| 1544 | promise.pushHeader = requestHeader; |
| 1545 | |
| 1546 | activeStreams.insert(key: reservedID, value: Stream(urlKey, reservedID, streamInitialReceiveWindowSize)); |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | void QHttp2ProtocolHandler::resetPromisedStream(const Frame &pushPromiseFrame, |
| 1551 | Http2::Http2Error reason) |
| 1552 | { |
| 1553 | Q_ASSERT(pushPromiseFrame.type() == FrameType::PUSH_PROMISE); |
| 1554 | const auto reservedID = qFromBigEndian<quint32>(src: pushPromiseFrame.dataBegin()); |
| 1555 | sendRST_STREAM(streamID: reservedID, errorCode: reason); |
| 1556 | markAsReset(streamID: reservedID); |
| 1557 | } |
| 1558 | |
| 1559 | void QHttp2ProtocolHandler::initReplyFromPushPromise(const HttpMessagePair &message, |
| 1560 | const QString &cacheKey) |
| 1561 | { |
| 1562 | Q_ASSERT(promisedData.contains(cacheKey)); |
| 1563 | auto promise = promisedData.take(key: cacheKey); |
| 1564 | Q_ASSERT(message.second); |
| 1565 | message.second->setHttp2WasUsed(true); |
| 1566 | |
| 1567 | qCDebug(QT_HTTP2) << "found cached/promised response on stream" << promise.reservedID; |
| 1568 | |
| 1569 | bool replyFinished = false; |
| 1570 | Stream *promisedStream = nullptr; |
| 1571 | if (auto it = activeStreams.find(key: promise.reservedID); it != activeStreams.end()) { |
| 1572 | promisedStream = &it.value(); |
| 1573 | // Ok, we have an active (not closed yet) stream waiting for more frames, |
| 1574 | // let's pretend we requested it: |
| 1575 | promisedStream->httpPair = message; |
| 1576 | } else { |
| 1577 | // Let's pretent we're sending a request now: |
| 1578 | Stream closedStream(message, promise.reservedID, |
| 1579 | streamInitialSendWindowSize, |
| 1580 | streamInitialReceiveWindowSize); |
| 1581 | closedStream.state = Stream::halfClosedLocal; |
| 1582 | it = activeStreams.insert(key: promise.reservedID, value: closedStream); |
| 1583 | promisedStream = &it.value(); |
| 1584 | replyFinished = true; |
| 1585 | } |
| 1586 | |
| 1587 | Q_ASSERT(promisedStream); |
| 1588 | |
| 1589 | if (!promise.responseHeader.empty()) |
| 1590 | updateStream(stream&: *promisedStream, headers: promise.responseHeader, connectionType: Qt::QueuedConnection); |
| 1591 | |
| 1592 | for (const auto &frame : promise.dataFrames) |
| 1593 | updateStream(stream&: *promisedStream, frame, connectionType: Qt::QueuedConnection); |
| 1594 | |
| 1595 | if (replyFinished) { |
| 1596 | // Good, we already have received ALL the frames of that PUSH_PROMISE, |
| 1597 | // nothing more to do. |
| 1598 | finishStream(stream&: *promisedStream, connectionType: Qt::QueuedConnection); |
| 1599 | deleteActiveStream(streamID: promisedStream->streamID); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | void QHttp2ProtocolHandler::connectionError(Http2::Http2Error errorCode, |
| 1604 | const char *message) |
| 1605 | { |
| 1606 | Q_ASSERT(message); |
| 1607 | Q_ASSERT(!goingAway); |
| 1608 | |
| 1609 | qCCritical(QT_HTTP2) << "connection error:" << message; |
| 1610 | |
| 1611 | goingAway = true; |
| 1612 | sendGOAWAY(errorCode); |
| 1613 | const auto error = qt_error(errorCode); |
| 1614 | m_channel->emitFinishedWithError(error, message); |
| 1615 | |
| 1616 | for (auto &stream: activeStreams) |
| 1617 | finishStreamWithError(stream, error, message: QLatin1StringView(message)); |
| 1618 | |
| 1619 | closeSession(); |
| 1620 | } |
| 1621 | |
| 1622 | void QHttp2ProtocolHandler::closeSession() |
| 1623 | { |
| 1624 | activeStreams.clear(); |
| 1625 | for (auto &q: suspendedStreams) |
| 1626 | q.clear(); |
| 1627 | recycledStreams.clear(); |
| 1628 | |
| 1629 | m_channel->close(); |
| 1630 | } |
| 1631 | |
| 1632 | QT_END_NAMESPACE |
| 1633 | |
| 1634 | #include "moc_qhttp2protocolhandler_p.cpp" |
| 1635 | |