mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:08:12 +00:00
GHttp: Work on bringing this up.
This commit is contained in:
parent
8f30657390
commit
51b4d3fe5a
9 changed files with 93 additions and 16 deletions
|
@ -2,6 +2,7 @@
|
|||
#include <LibGUI/GHttpResponse.h>
|
||||
#include <LibGUI/GTCPSocket.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
GHttpNetworkJob::GHttpNetworkJob(const GHttpRequest& request)
|
||||
: m_request(request)
|
||||
|
@ -26,20 +27,74 @@ void GHttpNetworkJob::start()
|
|||
|
||||
success = m_socket->send(raw_request);
|
||||
if (!success)
|
||||
return did_fail(GNetworkJob::Error::TransmissionFailed);
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::TransmissionFailed); });
|
||||
|
||||
Vector<byte> buffer;
|
||||
while (m_socket->is_connected()) {
|
||||
if (m_state == State::InStatus) {
|
||||
while (!m_socket->can_read_line())
|
||||
usleep(1);
|
||||
ASSERT(m_socket->can_read_line());
|
||||
auto line = m_socket->read_line(1024);
|
||||
if (line.is_null()) {
|
||||
printf("Expected HTTP status\n");
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::TransmissionFailed); });
|
||||
}
|
||||
auto parts = String::from_byte_buffer(line, Chomp).split(' ');
|
||||
if (parts.size() < 3) {
|
||||
printf("Expected 3-part HTTP status, got '%s'\n", line.pointer());
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
bool ok;
|
||||
m_code = parts[1].to_uint(ok);
|
||||
if (!ok) {
|
||||
printf("Expected numeric HTTP status\n");
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
m_state = State::InHeaders;
|
||||
continue;
|
||||
}
|
||||
if (m_state == State::InHeaders) {
|
||||
while (!m_socket->can_read_line())
|
||||
usleep(1);
|
||||
auto line = m_socket->read_line(1024);
|
||||
if (line.is_null()) {
|
||||
printf("Expected HTTP header\n");
|
||||
return did_fail(GNetworkJob::Error::ProtocolFailed);
|
||||
}
|
||||
auto chomped_line = String::from_byte_buffer(line, Chomp);
|
||||
if (chomped_line.is_empty()) {
|
||||
m_state = State::InBody;
|
||||
continue;
|
||||
}
|
||||
auto parts = chomped_line.split(':');
|
||||
if (parts.is_empty()) {
|
||||
printf("Expected HTTP header with key/value\n");
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
auto name = parts[0];
|
||||
if (chomped_line.length() < name.length() + 2) {
|
||||
printf("Malformed HTTP header\n");
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
auto value = chomped_line.substring(name.length() + 2, line.size() - name.length() - 2);
|
||||
m_headers.set(name, value);
|
||||
printf("[%s] = '%s'\n", name.characters(), value.characters());
|
||||
continue;
|
||||
}
|
||||
ASSERT(m_state == State::InBody);
|
||||
auto payload = m_socket->receive(100000);
|
||||
if (!payload) {
|
||||
if (m_socket->eof())
|
||||
if (m_socket->eof()) {
|
||||
m_state = State::Finished;
|
||||
break;
|
||||
return did_fail(GNetworkJob::Error::TransmissionFailed);
|
||||
}
|
||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
buffer.append(payload.pointer(), payload.size());
|
||||
}
|
||||
|
||||
auto response = GHttpResponse::create(1, ByteBuffer::copy(buffer.data(), buffer.size()));
|
||||
auto response = GHttpResponse::create(m_code, move(m_headers), ByteBuffer::copy(buffer.data(), buffer.size()));
|
||||
deferred_invoke([this, response] (GObject&) {
|
||||
printf("in the deferred invoke lambda\n");
|
||||
did_finish(move(response));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue