2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-03-03 15:35:10 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-07 09:36:10 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-09-28 06:55:26 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2023-12-16 11:19:34 -03:00
|
|
|
#include <AK/ByteString.h>
|
2024-10-27 16:48:04 -03:00
|
|
|
#include <AK/Noncopyable.h>
|
2025-11-26 16:13:23 -03:00
|
|
|
#include <LibHTTP/HeaderList.h>
|
2024-03-18 00:22:27 -03:00
|
|
|
#include <LibURL/URL.h>
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2020-04-20 18:25:25 -03:00
|
|
|
namespace HTTP {
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
class HttpRequest {
|
2019-04-07 09:36:10 -03:00
|
|
|
public:
|
2023-03-22 20:52:06 -03:00
|
|
|
enum class ParseError {
|
|
|
|
|
RequestTooLarge,
|
2023-03-22 21:06:13 -03:00
|
|
|
RequestIncomplete,
|
2023-03-22 20:52:06 -03:00
|
|
|
OutOfMemory,
|
2023-10-05 14:06:10 -03:00
|
|
|
UnsupportedMethod,
|
|
|
|
|
InvalidURL
|
2023-03-22 20:52:06 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static StringView parse_error_to_string(ParseError error)
|
|
|
|
|
{
|
|
|
|
|
switch (error) {
|
|
|
|
|
case ParseError::RequestTooLarge:
|
|
|
|
|
return "Request too large"sv;
|
2023-03-22 21:06:13 -03:00
|
|
|
case ParseError::RequestIncomplete:
|
|
|
|
|
return "Request is incomplete"sv;
|
2023-03-22 20:52:06 -03:00
|
|
|
case ParseError::OutOfMemory:
|
|
|
|
|
return "Out of memory"sv;
|
|
|
|
|
case ParseError::UnsupportedMethod:
|
|
|
|
|
return "Unsupported method"sv;
|
2026-02-14 08:31:05 -03:00
|
|
|
case ParseError::InvalidURL:
|
|
|
|
|
return "Invalid URL"sv;
|
2023-03-22 20:52:06 -03:00
|
|
|
}
|
2026-02-14 08:31:05 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2023-03-22 20:52:06 -03:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 12:13:23 -03:00
|
|
|
enum Method {
|
2019-05-28 06:53:16 -03:00
|
|
|
Invalid,
|
|
|
|
|
HEAD,
|
|
|
|
|
GET,
|
2022-06-27 17:39:03 -03:00
|
|
|
POST,
|
|
|
|
|
DELETE,
|
|
|
|
|
PATCH,
|
|
|
|
|
OPTIONS,
|
|
|
|
|
TRACE,
|
|
|
|
|
CONNECT,
|
|
|
|
|
PUT,
|
2019-05-28 06:53:16 -03:00
|
|
|
};
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2025-11-26 16:13:23 -03:00
|
|
|
explicit HttpRequest(NonnullRefPtr<HeaderList>);
|
2022-03-03 15:35:10 -03:00
|
|
|
~HttpRequest() = default;
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2024-10-27 16:48:04 -03:00
|
|
|
AK_MAKE_DEFAULT_MOVABLE(HttpRequest);
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString const& resource() const { return m_resource; }
|
2025-11-26 16:13:23 -03:00
|
|
|
HeaderList const& headers() const { return m_headers; }
|
2020-02-09 07:27:36 -03:00
|
|
|
|
2024-03-18 00:22:27 -03:00
|
|
|
URL::URL const& url() const { return m_url; }
|
|
|
|
|
void set_url(URL::URL const& url) { m_url = url; }
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2019-08-10 14:32:03 -03:00
|
|
|
Method method() const { return m_method; }
|
2019-04-07 09:36:10 -03:00
|
|
|
void set_method(Method method) { m_method = method; }
|
|
|
|
|
|
2021-06-06 12:01:35 -03:00
|
|
|
ByteBuffer const& body() const { return m_body; }
|
ProtocolServer: Stream the downloaded data if possible
This patchset makes ProtocolServer stream the downloads to its client
(LibProtocol), and as such changes the download API; a possible
download lifecycle could be as such:
notation = client->server:'>', server->client:'<', pipe activity:'*'
```
> StartDownload(GET, url, headers, {})
< Response(0, fd 8)
* {data, 1024b}
< HeadersBecameAvailable(0, response_headers, 200)
< DownloadProgress(0, 4K, 1024)
* {data, 1024b}
* {data, 1024b}
< DownloadProgress(0, 4K, 2048)
* {data, 1024b}
< DownloadProgress(0, 4K, 1024)
< DownloadFinished(0, true, 4K)
```
Since managing the received file descriptor is a pain, LibProtocol
implements `Download::stream_into(OutputStream)`, which can be used to
stream the download into any given output stream (be it a file, or
memory, or writing stuff with a delay, etc.).
Also, as some of the users of this API require all the downloaded data
upfront, LibProtocol also implements `set_should_buffer_all_input()`,
which causes the download instance to buffer all the data until the
download is complete, and to call the `on_buffered_download_finish`
hook.
2020-12-26 10:44:12 -03:00
|
|
|
void set_body(ByteBuffer&& body) { m_body = move(body); }
|
2020-09-28 06:55:26 -03:00
|
|
|
|
2023-02-09 00:07:52 -03:00
|
|
|
StringView method_name() const;
|
2023-03-09 11:51:20 -03:00
|
|
|
ErrorOr<ByteBuffer> to_raw_request() const;
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2023-03-22 20:52:06 -03:00
|
|
|
static ErrorOr<HttpRequest, HttpRequest::ParseError> from_raw_request(ReadonlyBytes);
|
2020-02-09 07:27:36 -03:00
|
|
|
|
2019-04-07 09:36:10 -03:00
|
|
|
private:
|
2024-03-18 00:22:27 -03:00
|
|
|
URL::URL m_url;
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString m_resource;
|
2019-04-07 09:36:10 -03:00
|
|
|
Method m_method { GET };
|
2025-11-26 16:13:23 -03:00
|
|
|
NonnullRefPtr<HeaderList> m_headers;
|
2020-09-28 06:55:26 -03:00
|
|
|
ByteBuffer m_body;
|
2019-04-07 09:36:10 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
2023-02-09 00:07:52 -03:00
|
|
|
StringView to_string_view(HttpRequest::Method);
|
2022-10-11 07:27:52 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
}
|