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