HttpRequest.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Optional.h>
  9. #include <AK/String.h>
  10. #include <AK/URL.h>
  11. #include <AK/Vector.h>
  12. #include <LibCore/Forward.h>
  13. namespace HTTP {
  14. class HttpRequest {
  15. public:
  16. enum Method {
  17. Invalid,
  18. HEAD,
  19. GET,
  20. POST
  21. };
  22. struct Header {
  23. String name;
  24. String value;
  25. };
  26. struct BasicAuthenticationCredentials {
  27. String username;
  28. String password;
  29. };
  30. HttpRequest();
  31. ~HttpRequest();
  32. String const& resource() const { return m_resource; }
  33. Vector<Header> const& headers() const { return m_headers; }
  34. URL const& url() const { return m_url; }
  35. void set_url(URL const& url) { m_url = url; }
  36. Method method() const { return m_method; }
  37. void set_method(Method method) { m_method = method; }
  38. ByteBuffer const& body() const { return m_body; }
  39. void set_body(ReadonlyBytes body) { m_body = ByteBuffer::copy(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. }