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-04-20 12:13:40 +00:00
|
|
|
printf("raw_request:\n%s\n", 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-04-10 20:28:10 +00:00
|
|
|
return deferred_invoke([this](auto&){ did_fail(CNetworkJob::Error::TransmissionFailed); });
|
2019-04-07 12:36:10 +00:00
|
|
|
|
|
|
|
Vector<byte> buffer;
|
|
|
|
while (m_socket->is_connected()) {
|
2019-04-07 17:35:07 +00:00
|
|
|
if (m_state == State::InStatus) {
|
|
|
|
while (!m_socket->can_read_line())
|
|
|
|
usleep(1);
|
|
|
|
ASSERT(m_socket->can_read_line());
|
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()) {
|
|
|
|
printf("Expected HTTP status\n");
|
2019-04-10 20:28:10 +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) {
|
|
|
|
printf("Expected 3-part HTTP status, got '%s'\n", line.pointer());
|
2019-04-10 20:28:10 +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) {
|
|
|
|
printf("Expected numeric HTTP status\n");
|
2019-04-10 20:28:10 +00:00
|
|
|
return deferred_invoke([this](auto&){ did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
|
|
|
m_state = State::InHeaders;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (m_state == State::InHeaders) {
|
|
|
|
while (!m_socket->can_read_line())
|
|
|
|
usleep(1);
|
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()) {
|
|
|
|
printf("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;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto parts = chomped_line.split(':');
|
|
|
|
if (parts.is_empty()) {
|
|
|
|
printf("Expected HTTP header with key/value\n");
|
2019-04-10 20:28:10 +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-04-07 20:47:34 +00:00
|
|
|
printf("Malformed HTTP header: '%s' (%d)\n", chomped_line.characters(), chomped_line.length());
|
2019-04-10 20:28:10 +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);
|
|
|
|
printf("[%s] = '%s'\n", name.characters(), value.characters());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT(m_state == State::InBody);
|
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-04-07 17:35:07 +00:00
|
|
|
if (m_socket->eof()) {
|
|
|
|
m_state = State::Finished;
|
2019-04-07 12:36:10 +00:00
|
|
|
break;
|
2019-04-07 17:35:07 +00:00
|
|
|
}
|
2019-04-10 20:28:10 +00:00
|
|
|
return deferred_invoke([this](auto&){ did_fail(CNetworkJob::Error::ProtocolFailed); });
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
|
|
|
buffer.append(payload.pointer(), payload.size());
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:28:10 +00:00
|
|
|
auto response = CHttpResponse::create(m_code, move(m_headers), ByteBuffer::copy(buffer.data(), buffer.size()));
|
2019-04-08 00:24:34 +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-04-10 18:22:23 +00:00
|
|
|
m_socket = new CTCPSocket(this);
|
2019-04-08 02:53:45 +00:00
|
|
|
m_socket->on_connected = [this] {
|
|
|
|
printf("Socket on_connected callback\n");
|
|
|
|
on_socket_connected();
|
|
|
|
};
|
|
|
|
bool success = m_socket->connect(m_request.hostname(), m_request.port());
|
|
|
|
if (!success)
|
2019-04-10 20:28:10 +00:00
|
|
|
return did_fail(CNetworkJob::Error::ConnectionFailed);
|
2019-04-08 02:53:45 +00:00
|
|
|
}
|