LoadRequest.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 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/HashMap.h>
  9. #include <AK/Time.h>
  10. #include <AK/URL.h>
  11. #include <LibCore/ElapsedTimer.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/Page/Page.h>
  14. namespace Web {
  15. class LoadRequest {
  16. public:
  17. LoadRequest()
  18. {
  19. }
  20. static LoadRequest create_for_url_on_page(const AK::URL& url, Page* page);
  21. // The main resource is the file being displayed in a frame (unlike subresources like images, scripts, etc.)
  22. // If a main resource fails with an HTTP error, we may still display its content if non-empty, e.g a custom 404 page.
  23. bool is_main_resource() const { return m_main_resource; }
  24. void set_main_resource(bool b) { m_main_resource = b; }
  25. bool is_valid() const { return m_url.is_valid(); }
  26. const AK::URL& url() const { return m_url; }
  27. void set_url(const AK::URL& url) { m_url = url; }
  28. DeprecatedString const& method() const { return m_method; }
  29. void set_method(DeprecatedString const& method) { m_method = method; }
  30. ByteBuffer const& body() const { return m_body; }
  31. void set_body(ByteBuffer body) { m_body = move(body); }
  32. void start_timer() { m_load_timer.start(); };
  33. Time load_time() const { return m_load_timer.elapsed_time(); }
  34. Optional<Page&>& page() { return m_page; };
  35. void set_page(Page& page) { m_page = page; }
  36. unsigned hash() const
  37. {
  38. auto body_hash = string_hash((char const*)m_body.data(), m_body.size());
  39. auto body_and_headers_hash = pair_int_hash(body_hash, m_headers.hash());
  40. auto url_and_method_hash = pair_int_hash(m_url.to_deprecated_string().hash(), m_method.hash());
  41. return pair_int_hash(body_and_headers_hash, url_and_method_hash);
  42. }
  43. bool operator==(LoadRequest const& other) const
  44. {
  45. if (m_headers.size() != other.m_headers.size())
  46. return false;
  47. for (auto const& it : m_headers) {
  48. auto jt = other.m_headers.find(it.key);
  49. if (jt == other.m_headers.end())
  50. return false;
  51. if (it.value != jt->value)
  52. return false;
  53. }
  54. return m_url == other.m_url && m_method == other.m_method && m_body == other.m_body;
  55. }
  56. void set_header(DeprecatedString const& name, DeprecatedString const& value) { m_headers.set(name, value); }
  57. DeprecatedString header(DeprecatedString const& name) const { return m_headers.get(name).value_or({}); }
  58. HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& headers() const { return m_headers; }
  59. private:
  60. AK::URL m_url;
  61. DeprecatedString m_method { "GET" };
  62. HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> m_headers;
  63. ByteBuffer m_body;
  64. Core::ElapsedTimer m_load_timer;
  65. Optional<Page&> m_page;
  66. bool m_main_resource { false };
  67. };
  68. }
  69. namespace AK {
  70. template<>
  71. struct Traits<Web::LoadRequest> : public GenericTraits<Web::LoadRequest> {
  72. static unsigned hash(Web::LoadRequest const& request) { return request.hash(); }
  73. };
  74. }