diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-10-11 15:01:47 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-10-11 21:52:13 +0200 |
commit | f754f456808fef6e9013382140d58a72ca9a40f4 (patch) | |
tree | a7395d43b9629a2d28ef2c8a6a7210f1a7602901 /Userland | |
parent | 437c6fc277249dbf253d4c8d1033d136feacf472 (diff) | |
download | serenity-f754f456808fef6e9013382140d58a72ca9a40f4.zip |
LibHTTP: Include request body in HttpRequest::from_raw_request()
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpRequest.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/Userland/Libraries/LibHTTP/HttpRequest.cpp b/Userland/Libraries/LibHTTP/HttpRequest.cpp index e421c369b7..dc1928a104 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.cpp +++ b/Userland/Libraries/LibHTTP/HttpRequest.cpp @@ -83,6 +83,7 @@ Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) InProtocol, InHeaderName, InHeaderValue, + InBody, }; State state { State::InMethod }; @@ -106,6 +107,7 @@ Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) String protocol; Vector<Header> headers; Header current_header; + ByteBuffer body; auto commit_and_advance_to = [&](auto& output, State new_state) { output = String::copy(buffer); @@ -156,12 +158,33 @@ Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) if (peek(0) == '\r' && peek(1) == '\n') { consume(); consume(); - commit_and_advance_to(current_header.value, State::InHeaderName); + + // Detect end of headers + auto next_state = State::InHeaderName; + if (peek(0) == '\r' && peek(1) == '\n') { + consume(); + consume(); + next_state = State::InBody; + } + + commit_and_advance_to(current_header.value, next_state); headers.append(move(current_header)); break; } buffer.append(consume()); break; + case State::InBody: + buffer.append(consume()); + if (index + 1 == raw_request.size()) { + // End of data, so store the body + auto maybe_body = ByteBuffer::copy(buffer); + // FIXME: Propagate this error somehow. + if (maybe_body.is_error()) + return {}; + body = maybe_body.release_value(); + buffer.clear(); + } + break; } } @@ -200,6 +223,8 @@ Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request) request.m_url.set_paths({ resource }); } + request.set_body(move(body)); + return request; } |