Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#pragma once
9
10#include <AK/ByteBuffer.h>
11#include <AK/DeprecatedString.h>
12#include <AK/Optional.h>
13#include <AK/URL.h>
14#include <AK/Vector.h>
15#include <LibCore/Forward.h>
16
17namespace HTTP {
18
19class HttpRequest {
20public:
21 enum Method {
22 Invalid,
23 HEAD,
24 GET,
25 POST,
26 DELETE,
27 PATCH,
28 OPTIONS,
29 TRACE,
30 CONNECT,
31 PUT,
32 };
33
34 struct Header {
35 DeprecatedString name;
36 DeprecatedString value;
37 };
38
39 struct BasicAuthenticationCredentials {
40 DeprecatedString username;
41 DeprecatedString password;
42 };
43
44 HttpRequest() = default;
45 ~HttpRequest() = default;
46
47 DeprecatedString const& resource() const { return m_resource; }
48 Vector<Header> const& headers() const { return m_headers; }
49
50 URL const& url() const { return m_url; }
51 void set_url(URL const& url) { m_url = url; }
52
53 Method method() const { return m_method; }
54 void set_method(Method method) { m_method = method; }
55
56 ByteBuffer const& body() const { return m_body; }
57 void set_body(ByteBuffer&& body) { m_body = move(body); }
58
59 DeprecatedString method_name() const;
60 ErrorOr<ByteBuffer> to_raw_request() const;
61
62 void set_headers(HashMap<DeprecatedString, DeprecatedString> const&);
63
64 static Optional<HttpRequest> from_raw_request(ReadonlyBytes);
65 static Optional<Header> get_http_basic_authentication_header(URL const&);
66 static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(DeprecatedString const&);
67
68private:
69 URL m_url;
70 DeprecatedString m_resource;
71 Method m_method { GET };
72 Vector<Header> m_headers;
73 ByteBuffer m_body;
74};
75
76DeprecatedString to_deprecated_string(HttpRequest::Method);
77
78}