azadkuh / qhttp

a light-weight and asynchronous HTTP library (both server & client) in Qt5 and c++14
MIT License
484 stars 158 forks source link

QHttpResponse::allBytesWritten stops emiting when writing #38

Open demiantres opened 7 years ago

demiantres commented 7 years ago

The "QHttpResponse::allBytesWritten" signal is emited only a few times when manually writing data to the client. This leads to incomplete downloads.

`req->onEnd([this, req, res]() {

QFile* f = new QFile(filename); // a file of some MB size
if (f->open(QFile::ReadOnly))
{
    res->addHeader("content-length", QString::number(f->size()).toLatin1());
    res->addHeader("content-type", "application/octet-stream");
    res->addHeader("content-disposition", QStringLiteral("attachment; filename=\"%1\"").arg(info.fileName()).toLatin1());
    //res->addHeader("connection", "Keep-Alive");
    //res->addHeader("keep-alive", "timeout=600, max=100");
    res->setStatusCode(qhttp::ESTATUS_OK);
    connect(res, &QHttpResponse::allBytesWritten, this, [this, f, res] {
        qDebug() << "Write more... " << f->pos(); // called only a few times
        QByteArray data = f->read(50000);
        if (data.length() == 0)
        {
            qDebug() << "END"; // never called
            res->end();
            f->close();
            f->deleteLater();
        }
        else
        {
            res->write(data); // called a few times
        }
    });

    QByteArray data = f->read(50000);
    if (data.length() > 0)
    {
        res->write(data);
        return;
    }
    else
    {
        f->close();
    }
}
f->deleteLater();

});`

demiantres commented 7 years ago

Got it. Is a Qt Windows bug related to the QTCPSocker signal.

EDIT The problem also happens with other systems (e.g. iOS).