mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:08:11 +00:00
LibWeb/WebDriver: Wait for more data to arrive if request is incomplete
Currently significant portion of requests coming to WebDriver server fails with error while parsing json body because requests are parsed when they are not complete yet. This change solves this by waiting for more data to arrive if HTTP request parser found that there is not enough data yet to parse the whole request. In the future we would probably want to move this logic to LibHTTP because this problem is relevant for any HTTP server.
This commit is contained in:
parent
7b8a857e30
commit
05a2d1f0e0
2 changed files with 16 additions and 3 deletions
|
@ -211,20 +211,32 @@ ErrorOr<void, Client::WrappedError> Client::on_ready_to_read()
|
|||
{
|
||||
// FIXME: All this should be moved to LibHTTP and be made spec compliant.
|
||||
auto buffer = TRY(ByteBuffer::create_uninitialized(m_socket->buffer_size()));
|
||||
StringBuilder builder;
|
||||
|
||||
for (;;) {
|
||||
if (!TRY(m_socket->can_read_without_blocking()))
|
||||
break;
|
||||
|
||||
auto data = TRY(m_socket->read_some(buffer));
|
||||
TRY(builder.try_append(StringView { data }));
|
||||
TRY(m_remaining_request.try_append(StringView { data }));
|
||||
|
||||
if (m_socket->is_eof())
|
||||
break;
|
||||
}
|
||||
|
||||
m_request = TRY(HTTP::HttpRequest::from_raw_request(TRY(builder.to_byte_buffer())));
|
||||
if (m_remaining_request.is_empty())
|
||||
return {};
|
||||
|
||||
auto maybe_parsed_request = HTTP::HttpRequest::from_raw_request(TRY(m_remaining_request.to_byte_buffer()));
|
||||
if (maybe_parsed_request.is_error()) {
|
||||
if (maybe_parsed_request.error() == HTTP::HttpRequest::ParseError::RequestIncomplete) {
|
||||
// If request is not complete we need to wait for more data to arrive
|
||||
return {};
|
||||
}
|
||||
return maybe_parsed_request.error();
|
||||
}
|
||||
|
||||
m_remaining_request.clear();
|
||||
m_request = maybe_parsed_request.value();
|
||||
|
||||
auto body = TRY(read_body_as_json());
|
||||
TRY(handle_request(move(body)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue