HttpRequest.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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(ByteBuffer&& body) { m_body = move(body); }
  40. String method_name() const;
  41. ByteBuffer to_raw_request() const;
  42. void set_headers(HashMap<String, String> const&);
  43. static Optional<HttpRequest> from_raw_request(ReadonlyBytes);
  44. static Optional<Header> get_http_basic_authentication_header(URL const&);
  45. static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(String const&);
  46. private:
  47. URL m_url;
  48. String m_resource;
  49. Method m_method { GET };
  50. Vector<Header> m_headers;
  51. ByteBuffer m_body;
  52. };
  53. }