2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-03-03 18:35:10 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-07 12:36:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-28 09:55:26 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2024-10-27 19:48:04 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-02-09 10:27:36 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-02-14 23:32:33 +00:00
|
|
|
#include <LibCore/Forward.h>
|
2024-06-09 11:46:04 +00:00
|
|
|
#include <LibHTTP/HeaderMap.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/URL.h>
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2020-04-20 21:25:25 +00:00
|
|
|
namespace HTTP {
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class HttpRequest {
|
2019-04-07 12:36:10 +00:00
|
|
|
public:
|
2023-03-22 23:52:06 +00:00
|
|
|
enum class ParseError {
|
|
|
|
RequestTooLarge,
|
2023-03-23 00:06:13 +00:00
|
|
|
RequestIncomplete,
|
2023-03-22 23:52:06 +00:00
|
|
|
OutOfMemory,
|
2023-10-05 17:06:10 +00:00
|
|
|
UnsupportedMethod,
|
|
|
|
InvalidURL
|
2023-03-22 23:52:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static StringView parse_error_to_string(ParseError error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case ParseError::RequestTooLarge:
|
|
|
|
return "Request too large"sv;
|
2023-03-23 00:06:13 +00:00
|
|
|
case ParseError::RequestIncomplete:
|
|
|
|
return "Request is incomplete"sv;
|
2023-03-22 23:52:06 +00:00
|
|
|
case ParseError::OutOfMemory:
|
|
|
|
return "Out of memory"sv;
|
|
|
|
case ParseError::UnsupportedMethod:
|
|
|
|
return "Unsupported method"sv;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum Method {
|
2019-05-28 09:53:16 +00:00
|
|
|
Invalid,
|
|
|
|
HEAD,
|
|
|
|
GET,
|
2022-06-27 20:39:03 +00:00
|
|
|
POST,
|
|
|
|
DELETE,
|
|
|
|
PATCH,
|
|
|
|
OPTIONS,
|
|
|
|
TRACE,
|
|
|
|
CONNECT,
|
|
|
|
PUT,
|
2019-05-28 09:53:16 +00:00
|
|
|
};
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2021-06-06 15:01:35 +00:00
|
|
|
struct BasicAuthenticationCredentials {
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString username;
|
|
|
|
ByteString password;
|
2021-06-06 15:01:35 +00:00
|
|
|
};
|
|
|
|
|
2022-03-03 18:35:10 +00:00
|
|
|
HttpRequest() = default;
|
|
|
|
~HttpRequest() = default;
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2024-10-27 19:48:04 +00:00
|
|
|
AK_MAKE_DEFAULT_MOVABLE(HttpRequest);
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString const& resource() const { return m_resource; }
|
2024-06-09 11:46:04 +00:00
|
|
|
HeaderMap const& headers() const { return m_headers; }
|
2020-02-09 10:27:36 +00:00
|
|
|
|
2024-03-18 03:22:27 +00:00
|
|
|
URL::URL const& url() const { return m_url; }
|
|
|
|
void set_url(URL::URL const& url) { m_url = url; }
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2019-08-10 17:32:03 +00:00
|
|
|
Method method() const { return m_method; }
|
2019-04-07 12:36:10 +00:00
|
|
|
void set_method(Method method) { m_method = method; }
|
|
|
|
|
2021-06-06 15:01:35 +00: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 13:44:12 +00:00
|
|
|
void set_body(ByteBuffer&& body) { m_body = move(body); }
|
2020-09-28 09:55:26 +00:00
|
|
|
|
2023-02-09 03:07:52 +00:00
|
|
|
StringView method_name() const;
|
2023-03-09 14:51:20 +00:00
|
|
|
ErrorOr<ByteBuffer> to_raw_request() const;
|
2019-04-07 12:36:10 +00:00
|
|
|
|
2024-06-09 11:46:04 +00:00
|
|
|
void set_headers(HeaderMap);
|
2020-05-21 10:27:42 +00:00
|
|
|
|
2023-03-22 23:52:06 +00:00
|
|
|
static ErrorOr<HttpRequest, HttpRequest::ParseError> from_raw_request(ReadonlyBytes);
|
2024-03-18 03:22:27 +00:00
|
|
|
static Optional<Header> get_http_basic_authentication_header(URL::URL const&);
|
2023-12-16 14:19:34 +00:00
|
|
|
static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(ByteString const&);
|
2020-02-09 10:27:36 +00:00
|
|
|
|
2019-04-07 12:36:10 +00:00
|
|
|
private:
|
2024-03-18 03:22:27 +00:00
|
|
|
URL::URL m_url;
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_resource;
|
2019-04-07 12:36:10 +00:00
|
|
|
Method m_method { GET };
|
2024-06-09 11:46:04 +00:00
|
|
|
HeaderMap m_headers;
|
2020-09-28 09:55:26 +00:00
|
|
|
ByteBuffer m_body;
|
2019-04-07 12:36:10 +00:00
|
|
|
};
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2023-02-09 03:07:52 +00:00
|
|
|
StringView to_string_view(HttpRequest::Method);
|
2022-10-11 10:27:52 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|