1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibHTTP: Propagate OOM errors from HttpRequest::to_raw_request()

This commit is contained in:
Linus Groh 2023-03-09 14:51:20 +00:00
parent f068ddb79f
commit d0ecd81888
3 changed files with 20 additions and 20 deletions

View file

@ -43,36 +43,36 @@ DeprecatedString HttpRequest::method_name() const
return to_deprecated_string(m_method); return to_deprecated_string(m_method);
} }
ByteBuffer HttpRequest::to_raw_request() const ErrorOr<ByteBuffer> HttpRequest::to_raw_request() const
{ {
StringBuilder builder; StringBuilder builder;
builder.append(method_name()); TRY(builder.try_append(method_name()));
builder.append(' '); TRY(builder.try_append(' '));
// NOTE: The percent_encode is so that e.g. spaces are properly encoded. // NOTE: The percent_encode is so that e.g. spaces are properly encoded.
auto path = m_url.path(); auto path = m_url.path();
VERIFY(!path.is_empty()); VERIFY(!path.is_empty());
builder.append(URL::percent_encode(m_url.path(), URL::PercentEncodeSet::EncodeURI)); TRY(builder.try_append(URL::percent_encode(m_url.path(), URL::PercentEncodeSet::EncodeURI)));
if (!m_url.query().is_empty()) { if (!m_url.query().is_empty()) {
builder.append('?'); TRY(builder.try_append('?'));
builder.append(m_url.query()); TRY(builder.try_append(m_url.query()));
} }
builder.append(" HTTP/1.1\r\nHost: "sv); TRY(builder.try_append(" HTTP/1.1\r\nHost: "sv));
builder.append(m_url.host()); TRY(builder.try_append(m_url.host()));
if (m_url.port().has_value()) if (m_url.port().has_value())
builder.appendff(":{}", *m_url.port()); TRY(builder.try_appendff(":{}", *m_url.port()));
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
for (auto& header : m_headers) { for (auto& header : m_headers) {
builder.append(header.name); TRY(builder.try_append(header.name));
builder.append(": "sv); TRY(builder.try_append(": "sv));
builder.append(header.value); TRY(builder.try_append(header.value));
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
} }
if (!m_body.is_empty() || method() == Method::POST) { if (!m_body.is_empty() || method() == Method::POST) {
builder.appendff("Content-Length: {}\r\n\r\n", m_body.size()); TRY(builder.try_appendff("Content-Length: {}\r\n\r\n", m_body.size()));
builder.append((char const*)m_body.data(), m_body.size()); TRY(builder.try_append((char const*)m_body.data(), m_body.size()));
} }
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
return builder.to_byte_buffer(); return builder.try_to_byte_buffer();
} }
Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request)

View file

@ -57,7 +57,7 @@ public:
void set_body(ByteBuffer&& body) { m_body = move(body); } void set_body(ByteBuffer&& body) { m_body = move(body); }
DeprecatedString method_name() const; DeprecatedString method_name() const;
ByteBuffer to_raw_request() const; ErrorOr<ByteBuffer> to_raw_request() const;
void set_headers(HashMap<DeprecatedString, DeprecatedString> const&); void set_headers(HashMap<DeprecatedString, DeprecatedString> const&);

View file

@ -195,7 +195,7 @@ ErrorOr<ByteBuffer> Job::receive(size_t size)
void Job::on_socket_connected() void Job::on_socket_connected()
{ {
auto raw_request = m_request.to_raw_request(); auto raw_request = m_request.to_raw_request().release_value_but_fixme_should_propagate_errors();
if constexpr (JOB_DEBUG) { if constexpr (JOB_DEBUG) {
dbgln("Job: raw_request:"); dbgln("Job: raw_request:");