CHttpJob: Block until we can actually read during State::InBody.

Also move to State::Finished once we've received >= Content-Length.
This commit is contained in:
Andreas Kling 2019-06-22 23:10:05 +02:00
parent 6d5633904f
commit dc8333067f
Notes: sideshowbarker 2024-07-19 13:30:35 +09:00

View file

@ -78,6 +78,9 @@ void CHttpJob::on_socket_connected()
continue;
}
ASSERT(m_state == State::InBody);
while (!m_socket->can_read())
usleep(1);
ASSERT(m_socket->can_read());
auto payload = m_socket->receive(PAGE_SIZE);
if (!payload) {
if (m_socket->eof()) {
@ -87,6 +90,12 @@ void CHttpJob::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
}
buffer.append(payload.pointer(), payload.size());
bool ok;
if (buffer.size() >= m_headers.get("Content-Length").to_int(ok) && ok) {
m_state = State::Finished;
break;
}
}
auto response = CHttpResponse::create(m_code, move(m_headers), ByteBuffer::copy(buffer.data(), buffer.size()));