15
15
16
16
#include < algorithm>
17
17
#include < memory>
18
+ #include < thread>
18
19
19
20
QT_BEGIN_NAMESPACE
20
21
@@ -43,6 +44,7 @@ QHttp2Stream::QHttp2Stream(QHttp2Connection *connection, quint32 streamID) noexc
43
44
}
44
45
45
46
QHttp2Stream::~QHttp2Stream () noexcept {
47
+ printf (" ~QHttp2Stream\n " );
46
48
if (auto *connection = getConnection ()) {
47
49
if (m_state == State::Open || m_state == State::HalfClosedRemote) {
48
50
qCDebug (qHttp2ConnectionLog, " [%p] stream %u, destroyed while still open" , connection,
@@ -290,6 +292,7 @@ bool QHttp2Stream::sendDATA(const QByteArray &payload, bool endStream)
290
292
auto *byteDevice = QNonContiguousByteDeviceFactory::create (payload);
291
293
m_owningByteDevice = true ;
292
294
byteDevice->setParent (this );
295
+ printf (" QHttp2Stream::sendDATA, byteDevice = %p\n " , (void *)byteDevice);
293
296
return sendDATA (byteDevice, endStream);
294
297
}
295
298
@@ -310,6 +313,7 @@ bool QHttp2Stream::sendDATA(const QByteArray &payload, bool endStream)
310
313
*/
311
314
bool QHttp2Stream::sendDATA (QIODevice *device, bool endStream)
312
315
{
316
+ printf (" QHttp2Stream::sendDATA(QIODevice *%p, bool %d)\n " , (void *)device, endStream);
313
317
Q_ASSERT (!m_uploadDevice);
314
318
Q_ASSERT (!m_uploadByteDevice);
315
319
Q_ASSERT (device);
@@ -326,6 +330,7 @@ bool QHttp2Stream::sendDATA(QIODevice *device, bool endStream)
326
330
m_owningByteDevice = true ;
327
331
byteDevice->setParent (this );
328
332
m_uploadDevice = device;
333
+ printf (" QHttp2Stream::sendDATA, m_uploadDevice = %p, byteDevice = %p\n " , (void *)device, (void *)byteDevice);
329
334
return sendDATA (byteDevice, endStream);
330
335
}
331
336
@@ -346,6 +351,7 @@ bool QHttp2Stream::sendDATA(QIODevice *device, bool endStream)
346
351
*/
347
352
bool QHttp2Stream::sendDATA (QNonContiguousByteDevice *device, bool endStream)
348
353
{
354
+ printf (" QHttp2Stream::sendDATA(QNonContiguousByteDevice *%p, bool %d)\n " , (void *)device, endStream);
349
355
Q_ASSERT (!m_uploadByteDevice);
350
356
Q_ASSERT (device);
351
357
if (m_state != State::Open && m_state != State::HalfClosedRemote) {
@@ -359,8 +365,11 @@ bool QHttp2Stream::sendDATA(QNonContiguousByteDevice *device, bool endStream)
359
365
getConnection (), m_streamID, device);
360
366
m_uploadByteDevice = device;
361
367
m_endStreamAfterDATA = endStream;
368
+ printf (" QHttp2Stream::sendDATA, m_uploadByteDevice = %p\n " , (void *)device);
362
369
connect (m_uploadByteDevice, &QNonContiguousByteDevice::readyRead, this ,
363
370
&QHttp2Stream::maybeResumeUpload);
371
+ auto functor = [p=m_uploadByteDevice](QObject*o){printf (" m_uploadByteDevice %p destroyed %p\n " ,(void *)p, (void *)o);};
372
+ QObject::connect (m_uploadByteDevice, &QObject::destroyed, m_uploadByteDevice, functor);
364
373
connect (m_uploadByteDevice, &QObject::destroyed, this , &QHttp2Stream::uploadDeviceDestroyed);
365
374
366
375
internalSendDATA ();
@@ -396,17 +405,22 @@ void QHttp2Stream::internalSendDATA()
396
405
};
397
406
398
407
bool sentEND_STREAM = false ;
408
+ bool enteredFirstWhile = false ;
409
+ bool enteredSecondWhile = false ;
399
410
while (remainingWindowSize && deviceCanRead ()) {
411
+ enteredFirstWhile = true ;
400
412
quint32 bytesWritten = 0 ;
401
413
qint32 remainingBytesInFrame = qint32 (connection->maxFrameSize );
402
414
frameWriter.start (FrameType::DATA, FrameFlag::EMPTY, streamID ());
403
415
404
416
while (remainingWindowSize && deviceCanRead () && remainingBytesInFrame) {
417
+ enteredSecondWhile = true ;
405
418
const qint32 maxToWrite = std::min (remainingWindowSize, remainingBytesInFrame);
406
419
407
420
qint64 outBytesAvail = 0 ;
408
421
const char *readPointer = m_uploadByteDevice->readPointer (maxToWrite, outBytesAvail);
409
422
if (!readPointer || outBytesAvail <= 0 ) {
423
+ printf (" not writing, readPointer: %s, outBytesAvail: %d\n " , readPointer?" notnull" :" null" , (int )outBytesAvail);
410
424
qCDebug (qHttp2ConnectionLog,
411
425
" [%p] stream %u, cannot write data, device (%p) has %lld bytes available" ,
412
426
connection, m_streamID, m_uploadByteDevice, outBytesAvail);
@@ -417,6 +431,7 @@ void QHttp2Stream::internalSendDATA()
417
431
m_uploadByteDevice->advanceReadPointer (bytesToWrite);
418
432
419
433
bytesWritten += bytesToWrite;
434
+ printf (" bytesWritten: %d\n " , (int )bytesWritten);
420
435
421
436
m_sendWindow -= bytesToWrite;
422
437
Q_ASSERT (m_sendWindow >= 0 );
@@ -435,26 +450,47 @@ void QHttp2Stream::internalSendDATA()
435
450
frameWriter.addFlag (FrameFlag::END_STREAM);
436
451
}
437
452
if (!frameWriter.write (*socket)) {
453
+ printf (" failed to write to socket\n " );
438
454
qCDebug (qHttp2ConnectionLog, " [%p] stream %u, failed to write to socket" , connection,
439
455
m_streamID);
440
456
return finishWithError (INTERNAL_ERROR, " failed to write to socket" _L1);
441
457
}
442
458
443
459
totalBytesWritten += bytesWritten;
444
460
}
461
+ if (!enteredFirstWhile){
462
+ printf (" Did not enter first while, remainingWindowSize: %d, deviceCanRead(): %d\n " , (int )remainingWindowSize, deviceCanRead ());
463
+ } else if (!enteredSecondWhile) {
464
+ printf (" Did not enter second while, remainingWindowSize: %d, deviceCanRead(): %d, remainingBytesInFrame: %d\n " , (int )remainingWindowSize, deviceCanRead (), int (connection->maxFrameSize ));
465
+ }
445
466
446
467
qCDebug (qHttp2ConnectionLog,
447
468
" [%p] stream %u, wrote %lld bytes total, if the device is not exhausted, we'll write "
448
469
" more later. Remaining window size: %d" ,
449
470
connection, m_streamID, totalBytesWritten, remainingWindowSize);
450
471
472
+ printf (" emit totalBytesWritten(%d)\n " , (int )totalBytesWritten);
451
473
emit bytesWritten (totalBytesWritten);
452
- if (sentEND_STREAM || (!deviceCanRead () && m_uploadByteDevice->atEnd ())) {
453
- qCDebug (qHttp2ConnectionLog,
454
- " [%p] stream %u, exhausted device %p, sent END_STREAM? %d, %ssending end stream "
455
- " after DATA" ,
456
- connection, m_streamID, m_uploadByteDevice, sentEND_STREAM,
457
- m_endStreamAfterDATA ? " " : " not " );
474
+ printf (" sentEND_STREAM: %d\n " , sentEND_STREAM);
475
+ bool dcr;
476
+ bool ae;
477
+ if (!sentEND_STREAM) {
478
+ dcr = deviceCanRead ();
479
+ printf (" deviceCanRead(): %d\n " , dcr);
480
+ if (!dcr) {
481
+ ae = m_uploadByteDevice->atEnd ();
482
+ printf (" m_uploadByteDevice->atEnd(): %d\n " , ae);
483
+ }
484
+ }
485
+ if (sentEND_STREAM || (!dcr && ae)) {
486
+ // qDebug(qHttp2ConnectionLog,
487
+ // "[%p] stream %u, exhausted device %p, sent END_STREAM? %d, %ssending end stream "
488
+ // "after DATA",
489
+ // connection, m_streamID, m_uploadByteDevice, sentEND_STREAM,
490
+ // m_endStreamAfterDATA ? "" : "not ");
491
+ if (!sentEND_STREAM) {
492
+ printf (" m_endStreamAfterDATA: %d\n " , m_endStreamAfterDATA);
493
+ }
458
494
if (!sentEND_STREAM && m_endStreamAfterDATA) {
459
495
// We need to send an empty DATA frame with END_STREAM since we
460
496
// have exhausted the device, but we haven't sent END_STREAM yet.
@@ -465,13 +501,17 @@ void QHttp2Stream::internalSendDATA()
465
501
}
466
502
finishSendDATA ();
467
503
} else if (isUploadBlocked ()) {
468
- qCDebug (qHttp2ConnectionLog, " [%p] stream %u, upload blocked" , connection, m_streamID);
504
+ printf (" Upload is blocked\n " );
505
+ // qDebug(qHttp2ConnectionLog, "[%p] stream %u, upload blocked", connection, m_streamID);
469
506
emit uploadBlocked ();
507
+ } else {
508
+ printf (" Upload is not blocked\n " );
470
509
}
471
510
}
472
511
473
512
void QHttp2Stream::finishSendDATA ()
474
513
{
514
+ printf (" QHttp2Stream::finishSendDATA()\n " );
475
515
if (m_endStreamAfterDATA)
476
516
transitionState (StateTransition::CloseLocal);
477
517
@@ -593,7 +633,9 @@ void QHttp2Stream::sendWINDOW_UPDATE(quint32 delta)
593
633
594
634
void QHttp2Stream::uploadDeviceDestroyed ()
595
635
{
636
+ printf (" uploadDeviceDestroyed\n " );
596
637
if (isUploadingDATA ()) {
638
+ printf (" isUploadingDATA is true\n " );
597
639
// We're in the middle of sending DATA frames, we need to abort
598
640
// the stream.
599
641
streamError (CANCEL, QLatin1String (" Upload device destroyed while uploading" ));
@@ -694,6 +736,7 @@ void QHttp2Stream::handleDATA(const Frame &inboundFrame)
694
736
inboundFrame.dataSize ());
695
737
if (endStream)
696
738
transitionState (StateTransition::CloseRemote);
739
+ printf (" dataReceived: `%s` endStream: %d\n " , fragment.toStdString ().c_str (), endStream);
697
740
emit dataReceived (fragment, endStream);
698
741
m_downloadBuffer.append (std::move (fragment));
699
742
}
@@ -712,6 +755,12 @@ void QHttp2Stream::handleHEADERS(Http2::FrameFlags frameFlags, const HPack::Http
712
755
if (endStream)
713
756
transitionState (StateTransition::CloseRemote);
714
757
if (!headers.empty ()) {
758
+ for (auto &h:headers){
759
+ if ( h.name .toStdString () == " :status" ){
760
+ printf (" header ':status' %s received\n " , h.value .toStdString ().c_str ());
761
+ break ;
762
+ }
763
+ }
715
764
m_headers.insert (m_headers.end (), headers.begin (), headers.end ());
716
765
emit headersUpdated ();
717
766
}
0 commit comments