Responses.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  7. namespace Web::Fetch::Infrastructure {
  8. // https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
  9. // A network error is a response whose status is always 0, status message is always
  10. // the empty byte sequence, header list is always empty, and body is always null.
  11. NonnullOwnPtr<Response> Response::aborted_network_error()
  12. {
  13. auto response = network_error();
  14. response->set_aborted(true);
  15. return response;
  16. }
  17. NonnullOwnPtr<Response> Response::network_error()
  18. {
  19. auto response = make<Response>();
  20. response->set_status(0);
  21. response->set_type(Type::Error);
  22. VERIFY(!response->body().has_value());
  23. return response;
  24. }
  25. // https://fetch.spec.whatwg.org/#concept-aborted-network-error
  26. bool Response::is_aborted_network_error() const
  27. {
  28. // A response whose type is "error" and aborted flag is set is known as an aborted network error.
  29. return m_type == Type::Error && m_aborted;
  30. }
  31. // https://fetch.spec.whatwg.org/#concept-network-error
  32. bool Response::is_network_error() const
  33. {
  34. // A response whose type is "error" is known as a network error.
  35. return m_type == Type::Error;
  36. }
  37. // https://fetch.spec.whatwg.org/#concept-response-url
  38. Optional<AK::URL const&> Response::url() const
  39. {
  40. // A response has an associated URL. It is a pointer to the last URL in response’s URL list and null if response’s URL list is empty.
  41. if (m_url_list.is_empty())
  42. return {};
  43. return m_url_list.last();
  44. }
  45. // https://fetch.spec.whatwg.org/#concept-response-location-url
  46. ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& request_fragment) const
  47. {
  48. // The location URL of a response response, given null or an ASCII string requestFragment, is the value returned by the following steps. They return null, failure, or a URL.
  49. // 1. If response’s status is not a redirect status, then return null.
  50. if (!is_redirect_status(m_status))
  51. return Optional<AK::URL> {};
  52. // FIXME: 2. Let location be the result of extracting header list values given `Location` and response’s header list.
  53. auto location_value = ByteBuffer {};
  54. // 3. If location is a header value, then set location to the result of parsing location with response’s URL.
  55. auto location = AK::URL { StringView { location_value } };
  56. if (!location.is_valid())
  57. return Error::from_string_view("Invalid 'Location' header URL"sv);
  58. // 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
  59. if (location.fragment().is_null())
  60. location.set_fragment(request_fragment.value_or({}));
  61. // 5. Return location.
  62. return location;
  63. }
  64. FilteredResponse::FilteredResponse(Response& internal_response)
  65. : m_internal_response(internal_response)
  66. {
  67. }
  68. FilteredResponse::~FilteredResponse()
  69. {
  70. }
  71. ErrorOr<NonnullOwnPtr<BasicFilteredResponse>> BasicFilteredResponse::create(Response& internal_response)
  72. {
  73. // A basic filtered response is a filtered response whose type is "basic" and header list excludes
  74. // any headers in internal response’s header list whose name is a forbidden response-header name.
  75. HeaderList header_list;
  76. for (auto const& header : internal_response.header_list()) {
  77. if (!is_forbidden_response_header_name(header.name))
  78. TRY(header_list.append(header));
  79. }
  80. return adopt_own(*new BasicFilteredResponse(internal_response, move(header_list)));
  81. }
  82. BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, HeaderList header_list)
  83. : FilteredResponse(internal_response)
  84. , m_header_list(move(header_list))
  85. {
  86. }
  87. ErrorOr<NonnullOwnPtr<CORSFilteredResponse>> CORSFilteredResponse::create(Response& internal_response)
  88. {
  89. // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
  90. // any headers in internal response’s header list whose name is not a CORS-safelisted response-header
  91. // name, given internal response’s CORS-exposed header-name list.
  92. Vector<ReadonlyBytes> cors_exposed_header_name_list;
  93. for (auto const& header_name : internal_response.cors_exposed_header_name_list())
  94. cors_exposed_header_name_list.append(header_name.span());
  95. HeaderList header_list;
  96. for (auto const& header : internal_response.header_list()) {
  97. if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
  98. TRY(header_list.append(header));
  99. }
  100. return adopt_own(*new CORSFilteredResponse(internal_response, move(header_list)));
  101. }
  102. CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, HeaderList header_list)
  103. : FilteredResponse(internal_response)
  104. , m_header_list(move(header_list))
  105. {
  106. }
  107. NonnullOwnPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(Response& internal_response)
  108. {
  109. // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
  110. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  111. return adopt_own(*new OpaqueFilteredResponse(internal_response));
  112. }
  113. OpaqueFilteredResponse::OpaqueFilteredResponse(Response& internal_response)
  114. : FilteredResponse(internal_response)
  115. {
  116. }
  117. NonnullOwnPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(Response& internal_response)
  118. {
  119. return adopt_own(*new OpaqueRedirectFilteredResponse(internal_response));
  120. }
  121. OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(Response& internal_response)
  122. : FilteredResponse(internal_response)
  123. {
  124. }
  125. }