LibHTTP: Be more tolerant about bad chunked encoding trailers

Some servers (*glares at cloudflare*) like to send two last chunks,
which is strictly against the spec. Let's be more tolerant of this
behaviour.
This commit is contained in:
AnotherTest 2020-07-26 06:38:33 +04:30 committed by Andreas Kling
parent 49c5acaa3d
commit ec0315883b
Notes: sideshowbarker 2024-07-19 04:36:38 +09:00
2 changed files with 19 additions and 0 deletions

View file

@ -51,6 +51,7 @@ public:
virtual void shutdown() override;
protected:
virtual bool should_fail_on_empty_payload() const override { return false; }
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;

View file

@ -122,6 +122,12 @@ void Job::on_socket_connected()
return;
auto line = read_line(PAGE_SIZE);
if (line.is_null()) {
if (m_state == State::AfterChunkedEncodingTrailer) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Expected HTTP header\n");
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
}
@ -136,11 +142,23 @@ void Job::on_socket_connected()
}
auto parts = chomped_line.split(':');
if (parts.is_empty()) {
if (m_state == State::AfterChunkedEncodingTrailer) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Expected HTTP header with key/value\n");
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto name = parts[0];
if (chomped_line.length() < name.length() + 2) {
if (m_state == State::AfterChunkedEncodingTrailer) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length());
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}