HttpRequest.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <AK/URL.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/Forward.h>
  14. namespace HTTP {
  15. class HttpRequest {
  16. public:
  17. enum Method {
  18. Invalid,
  19. HEAD,
  20. GET,
  21. POST
  22. };
  23. struct Header {
  24. String name;
  25. String value;
  26. };
  27. struct BasicAuthenticationCredentials {
  28. String username;
  29. String password;
  30. };
  31. HttpRequest() = default;
  32. ~HttpRequest() = default;
  33. String const& resource() const { return m_resource; }
  34. Vector<Header> const& headers() const { return m_headers; }
  35. URL const& url() const { return m_url; }
  36. void set_url(URL const& url) { m_url = url; }
  37. Method method() const { return m_method; }
  38. void set_method(Method method) { m_method = method; }
  39. ByteBuffer const& body() const { return m_body; }
  40. void set_body(ByteBuffer&& body) { m_body = move(body); }
  41. String method_name() const;
  42. ByteBuffer to_raw_request() const;
  43. void set_headers(HashMap<String, String> const&);
  44. static Optional<HttpRequest> from_raw_request(ReadonlyBytes);
  45. static Optional<Header> get_http_basic_authentication_header(URL const&);
  46. static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(String const&);
  47. private:
  48. URL m_url;
  49. String m_resource;
  50. Method m_method { GET };
  51. Vector<Header> m_headers;
  52. ByteBuffer m_body;
  53. };
  54. }