2019-04-10 20:28:10 +00:00
|
|
|
#include <LibCore/CHttpJob.h>
|
|
|
|
#include <LibCore/CHttpResponse.h>
|
2019-04-10 18:22:23 +00:00
|
|
|
#include <LibCore/CTCPSocket.h>
|
2019-04-07 12:36:10 +00:00
|
|
|
#include <stdio.h>
|
2019-04-07 17:35:07 +00:00
|
|
|
#include <unistd.h>
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-04-10 20:28:10 +00:00
|
|
|
CHttpJob::CHttpJob(const CHttpRequest& request)
|
2019-04-07 12:36:10 +00:00
|
|
|
: m_request(request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:28:10 +00:00
|
|
|
CHttpJob::~CHttpJob()
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:28:10 +00:00
|
|
|
void CHttpJob::on_socket_connected()
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
|
|
|
auto raw_request = m_request.to_raw_request();
|
2019-04-08 02:53:45 +00:00
|
|
|
#if 0
|
2019-08-04 20:10:54 +00:00
|
|
|
dbg() << "CHttpJob: raw_request:";
|
|
|
|
dbg() << String::copy(raw_request).characters();
|
2019-04-08 02:53:45 +00:00
|
|
|
#endif
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-04-08 02:53:45 +00:00
|
|
|
bool success = m_socket->send(raw_request);
|
2019-04-07 12:36:10 +00:00
|
|
|
if (!success)
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::TransmissionFailed); });
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-08-04 16:55:52 +00:00
|
|
|
m_socket->on_ready_to_read = [&] {
|
2019-09-21 15:32:26 +00:00
|
|
|
if (is_cancelled())
|
|
|
|
return;
|
2019-04-07 17:35:07 +00:00
|
|
|
if (m_state == State::InStatus) {
|
2019-08-04 16:55:52 +00:00
|
|
|
if (!m_socket->can_read_line())
|
|
|
|
return;
|
2019-04-08 00:24:34 +00:00
|
|
|
auto line = m_socket->read_line(PAGE_SIZE);
|
2019-04-07 17:35:07 +00:00
|
|
|
if (line.is_null()) {
|
2019-08-04 20:10:54 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Expected HTTP status\n");
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::TransmissionFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
2019-04-20 12:13:40 +00:00
|
|
|
auto parts = String::copy(line, Chomp).split(' ');
|
2019-04-07 17:35:07 +00:00
|
|
|
if (parts.size() < 3) {
|
2019-09-30 06:57:01 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Expected 3-part HTTP status, got '%s'\n", line.data());
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
bool ok;
|
|
|
|
m_code = parts[1].to_uint(ok);
|
|
|
|
if (!ok) {
|
2019-08-04 20:10:54 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Expected numeric HTTP status\n");
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
m_state = State::InHeaders;
|
2019-08-04 16:55:52 +00:00
|
|
|
return;
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
if (m_state == State::InHeaders) {
|
2019-08-04 16:55:52 +00:00
|
|
|
if (!m_socket->can_read_line())
|
|
|
|
return;
|
2019-04-08 00:24:34 +00:00
|
|
|
auto line = m_socket->read_line(PAGE_SIZE);
|
2019-04-07 17:35:07 +00:00
|
|
|
if (line.is_null()) {
|
2019-08-04 20:10:54 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Expected HTTP header\n");
|
2019-04-10 20:28:10 +00:00
|
|
|
return did_fail(CNetworkJob::Error::ProtocolFailed);
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
2019-04-20 12:13:40 +00:00
|
|
|
auto chomped_line = String::copy(line, Chomp);
|
2019-04-07 17:35:07 +00:00
|
|
|
if (chomped_line.is_empty()) {
|
|
|
|
m_state = State::InBody;
|
2019-08-04 16:55:52 +00:00
|
|
|
return;
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
auto parts = chomped_line.split(':');
|
|
|
|
if (parts.is_empty()) {
|
2019-08-04 20:10:54 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Expected HTTP header with key/value\n");
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
auto name = parts[0];
|
|
|
|
if (chomped_line.length() < name.length() + 2) {
|
2019-08-04 20:10:54 +00:00
|
|
|
fprintf(stderr, "CHttpJob: Malformed HTTP header: '%s' (%d)\n", chomped_line.characters(), chomped_line.length());
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
2019-04-07 20:47:34 +00:00
|
|
|
auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2);
|
2019-04-07 17:35:07 +00:00
|
|
|
m_headers.set(name, value);
|
2019-08-04 20:10:54 +00:00
|
|
|
dbg() << "CHttpJob: [" << name << "] = '" << value << "'";
|
2019-08-04 16:55:52 +00:00
|
|
|
return;
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
ASSERT(m_state == State::InBody);
|
2019-06-22 21:10:05 +00:00
|
|
|
ASSERT(m_socket->can_read());
|
2019-04-08 00:24:34 +00:00
|
|
|
auto payload = m_socket->receive(PAGE_SIZE);
|
2019-04-07 12:36:10 +00:00
|
|
|
if (!payload) {
|
2019-08-04 16:55:52 +00:00
|
|
|
if (m_socket->eof())
|
|
|
|
return finish_up();
|
2019-05-28 09:53:16 +00:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
2019-08-04 16:55:52 +00:00
|
|
|
m_received_buffers.append(payload);
|
|
|
|
m_received_size += payload.size();
|
2019-06-22 21:10:05 +00:00
|
|
|
|
2019-08-04 07:25:14 +00:00
|
|
|
auto content_length_header = m_headers.get("Content-Length");
|
|
|
|
if (content_length_header.has_value()) {
|
|
|
|
bool ok;
|
2019-08-04 16:55:52 +00:00
|
|
|
if (m_received_size >= content_length_header.value().to_uint(ok) && ok)
|
|
|
|
return finish_up();
|
2019-06-22 21:10:05 +00:00
|
|
|
}
|
2019-08-04 16:55:52 +00:00
|
|
|
};
|
|
|
|
}
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-08-04 16:55:52 +00:00
|
|
|
void CHttpJob::finish_up()
|
|
|
|
{
|
|
|
|
m_state = State::Finished;
|
|
|
|
auto flattened_buffer = ByteBuffer::create_uninitialized(m_received_size);
|
2019-08-04 12:54:25 +00:00
|
|
|
u8* flat_ptr = flattened_buffer.data();
|
2019-08-04 16:55:52 +00:00
|
|
|
for (auto& received_buffer : m_received_buffers) {
|
2019-08-04 12:54:25 +00:00
|
|
|
memcpy(flat_ptr, received_buffer.data(), received_buffer.size());
|
|
|
|
flat_ptr += received_buffer.size();
|
|
|
|
}
|
2019-08-04 16:55:52 +00:00
|
|
|
m_received_buffers.clear();
|
2019-08-04 12:54:25 +00:00
|
|
|
|
|
|
|
auto response = CHttpResponse::create(m_code, move(m_headers), move(flattened_buffer));
|
2019-05-28 09:53:16 +00:00
|
|
|
deferred_invoke([this, response](auto&) {
|
2019-04-07 12:36:10 +00:00
|
|
|
did_finish(move(response));
|
|
|
|
});
|
|
|
|
}
|
2019-04-08 02:53:45 +00:00
|
|
|
|
2019-04-10 20:28:10 +00:00
|
|
|
void CHttpJob::start()
|
2019-04-08 02:53:45 +00:00
|
|
|
{
|
|
|
|
ASSERT(!m_socket);
|
2019-09-21 08:13:34 +00:00
|
|
|
m_socket = CTCPSocket::construct(this);
|
2019-04-08 02:53:45 +00:00
|
|
|
m_socket->on_connected = [this] {
|
2019-08-04 20:10:54 +00:00
|
|
|
dbg() << "CHttpJob: on_connected callback";
|
2019-04-08 02:53:45 +00:00
|
|
|
on_socket_connected();
|
|
|
|
};
|
2019-08-10 17:32:03 +00:00
|
|
|
bool success = m_socket->connect(m_request.url().host(), m_request.url().port());
|
2019-10-08 17:32:34 +00:00
|
|
|
if (!success) {
|
|
|
|
deferred_invoke([this](auto&) {
|
|
|
|
return did_fail(CNetworkJob::Error::ConnectionFailed);
|
|
|
|
});
|
|
|
|
}
|
2019-04-08 02:53:45 +00:00
|
|
|
}
|
2019-09-21 15:32:26 +00:00
|
|
|
|
|
|
|
void CHttpJob::shutdown()
|
|
|
|
{
|
|
|
|
if (!m_socket)
|
|
|
|
return;
|
|
|
|
m_socket->on_ready_to_read = nullptr;
|
|
|
|
m_socket->on_connected = nullptr;
|
2019-09-21 16:05:34 +00:00
|
|
|
remove_child(*m_socket);
|
2019-09-21 15:32:26 +00:00
|
|
|
m_socket = nullptr;
|
|
|
|
}
|
2019-09-21 16:05:34 +00:00
|
|
|
|