HttpRequest.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. DELETE,
  23. PATCH,
  24. OPTIONS,
  25. TRACE,
  26. CONNECT,
  27. PUT,
  28. };
  29. struct Header {
  30. String name;
  31. String value;
  32. };
  33. struct BasicAuthenticationCredentials {
  34. String username;
  35. String password;
  36. };
  37. HttpRequest() = default;
  38. ~HttpRequest() = default;
  39. String const& resource() const { return m_resource; }
  40. Vector<Header> const& headers() const { return m_headers; }
  41. URL const& url() const { return m_url; }
  42. void set_url(URL const& url) { m_url = url; }
  43. Method method() const { return m_method; }
  44. void set_method(Method method) { m_method = method; }
  45. ByteBuffer const& body() const { return m_body; }
  46. void set_body(ByteBuffer&& body) { m_body = move(body); }
  47. String method_name() const;
  48. ByteBuffer to_raw_request() const;
  49. void set_headers(HashMap<String, String> const&);
  50. static Optional<HttpRequest> from_raw_request(ReadonlyBytes);
  51. static Optional<Header> get_http_basic_authentication_header(URL const&);
  52. static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(String const&);
  53. private:
  54. URL m_url;
  55. String m_resource;
  56. Method m_method { GET };
  57. Vector<Header> m_headers;
  58. ByteBuffer m_body;
  59. };
  60. }