LoadRequest.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/URL.h>
  30. #include <LibWeb/Forward.h>
  31. namespace Web {
  32. class LoadRequest {
  33. public:
  34. LoadRequest()
  35. {
  36. }
  37. bool is_valid() const { return m_url.is_valid(); }
  38. const URL& url() const { return m_url; }
  39. void set_url(const URL& url) { m_url = url; }
  40. const String& method() const { return m_method; }
  41. void set_method(const String& method) { m_method = method; }
  42. const ByteBuffer& body() const { return m_body; }
  43. void set_body(const ByteBuffer& body) { m_body = body; }
  44. unsigned hash() const
  45. {
  46. // FIXME: Include headers in the hash as well
  47. return pair_int_hash(pair_int_hash(m_url.to_string().hash(), m_method.hash()), string_hash((const char*)m_body.data(), m_body.size()));
  48. }
  49. bool operator==(const LoadRequest& other) const
  50. {
  51. if (m_headers.size() != other.m_headers.size())
  52. return false;
  53. for (auto& it : m_headers) {
  54. auto jt = other.m_headers.find(it.key);
  55. if (jt == other.m_headers.end())
  56. return false;
  57. if (it.value != jt->value)
  58. return false;
  59. }
  60. return m_url == other.m_url && m_method == other.m_method && m_body == other.m_body;
  61. }
  62. void set_header(const String& name, const String& value) { m_headers.set(name, value); }
  63. String header(const String& name) const { return m_headers.get(name).value_or({}); }
  64. const HashMap<String, String>& headers() const { return m_headers; }
  65. private:
  66. URL m_url;
  67. String m_method { "GET" };
  68. HashMap<String, String> m_headers;
  69. ByteBuffer m_body;
  70. };
  71. }
  72. namespace AK {
  73. template<>
  74. struct Traits<Web::LoadRequest> : public GenericTraits<Web::LoadRequest> {
  75. static unsigned hash(const Web::LoadRequest& request) { return request.hash(); }
  76. };
  77. }