2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-03-03 21:35:10 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-04-07 15:36:10 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-28 12:55:26 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-02-09 13:27:36 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2019-08-10 20:32:03 +03:00
|
|
|
#include <AK/URL.h>
|
2020-02-15 02:32:33 +03:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/Forward.h>
|
2019-04-07 15:36:10 +03:00
|
|
|
|
2020-04-21 00:25:25 +03:00
|
|
|
namespace HTTP {
|
2019-04-07 15:36:10 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
class HttpRequest {
|
2019-04-07 15:36:10 +03:00
|
|
|
public:
|
2019-06-07 18:13:23 +03:00
|
|
|
enum Method {
|
2019-05-28 12:53:16 +03:00
|
|
|
Invalid,
|
|
|
|
HEAD,
|
|
|
|
GET,
|
2022-06-27 23:39:03 +03:00
|
|
|
POST,
|
|
|
|
DELETE,
|
|
|
|
PATCH,
|
|
|
|
OPTIONS,
|
|
|
|
TRACE,
|
|
|
|
CONNECT,
|
|
|
|
PUT,
|
2019-05-28 12:53:16 +03:00
|
|
|
};
|
2019-04-07 15:36:10 +03:00
|
|
|
|
2020-02-09 13:27:36 +03:00
|
|
|
struct Header {
|
|
|
|
String name;
|
|
|
|
String value;
|
|
|
|
};
|
|
|
|
|
2021-06-06 18:01:35 +03:00
|
|
|
struct BasicAuthenticationCredentials {
|
|
|
|
String username;
|
|
|
|
String password;
|
|
|
|
};
|
|
|
|
|
2022-03-03 21:35:10 +03:00
|
|
|
HttpRequest() = default;
|
|
|
|
~HttpRequest() = default;
|
2019-04-07 15:36:10 +03:00
|
|
|
|
2021-06-06 18:01:35 +03:00
|
|
|
String const& resource() const { return m_resource; }
|
|
|
|
Vector<Header> const& headers() const { return m_headers; }
|
2020-02-09 13:27:36 +03:00
|
|
|
|
2021-06-06 18:01:35 +03:00
|
|
|
URL const& url() const { return m_url; }
|
|
|
|
void set_url(URL const& url) { m_url = url; }
|
2019-04-07 15:36:10 +03:00
|
|
|
|
2019-08-10 20:32:03 +03:00
|
|
|
Method method() const { return m_method; }
|
2019-04-07 15:36:10 +03:00
|
|
|
void set_method(Method method) { m_method = method; }
|
|
|
|
|
2021-06-06 18: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 16:44:12 +03:00
|
|
|
void set_body(ByteBuffer&& body) { m_body = move(body); }
|
2020-09-28 12:55:26 +03:00
|
|
|
|
2019-04-07 15:36:10 +03:00
|
|
|
String method_name() const;
|
|
|
|
ByteBuffer to_raw_request() const;
|
|
|
|
|
2021-06-06 18:01:35 +03:00
|
|
|
void set_headers(HashMap<String, String> const&);
|
2020-05-21 13:27:42 +03:00
|
|
|
|
2020-12-19 19:38:33 +03:00
|
|
|
static Optional<HttpRequest> from_raw_request(ReadonlyBytes);
|
2021-06-06 18:01:35 +03:00
|
|
|
static Optional<Header> get_http_basic_authentication_header(URL const&);
|
|
|
|
static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(String const&);
|
2020-02-09 13:27:36 +03:00
|
|
|
|
2019-04-07 15:36:10 +03:00
|
|
|
private:
|
2019-08-10 20:32:03 +03:00
|
|
|
URL m_url;
|
2020-02-09 13:27:36 +03:00
|
|
|
String m_resource;
|
2019-04-07 15:36:10 +03:00
|
|
|
Method m_method { GET };
|
2020-02-09 13:27:36 +03:00
|
|
|
Vector<Header> m_headers;
|
2020-09-28 12:55:26 +03:00
|
|
|
ByteBuffer m_body;
|
2019-04-07 15:36:10 +03:00
|
|
|
};
|
2020-02-02 14:34:39 +03:00
|
|
|
|
2022-10-11 13:27:52 +03:00
|
|
|
String to_string(HttpRequest::Method);
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
}
|