Responses.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/Bodies.h>
  7. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  8. namespace Web::Fetch::Infrastructure {
  9. Response::Response()
  10. : m_header_list(make_ref_counted<HeaderList>())
  11. {
  12. }
  13. NonnullRefPtr<Response> Response::create()
  14. {
  15. return adopt_ref(*new Response());
  16. }
  17. // https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
  18. // A network error is a response whose status is always 0, status message is always
  19. // the empty byte sequence, header list is always empty, and body is always null.
  20. NonnullRefPtr<Response> Response::aborted_network_error()
  21. {
  22. auto response = network_error();
  23. response->set_aborted(true);
  24. return response;
  25. }
  26. NonnullRefPtr<Response> Response::network_error()
  27. {
  28. auto response = Response::create();
  29. response->set_status(0);
  30. response->set_type(Type::Error);
  31. VERIFY(!response->body().has_value());
  32. return response;
  33. }
  34. // https://fetch.spec.whatwg.org/#concept-aborted-network-error
  35. bool Response::is_aborted_network_error() const
  36. {
  37. // A response whose type is "error" and aborted flag is set is known as an aborted network error.
  38. return m_type == Type::Error && m_aborted;
  39. }
  40. // https://fetch.spec.whatwg.org/#concept-network-error
  41. bool Response::is_network_error() const
  42. {
  43. // A response whose type is "error" is known as a network error.
  44. return m_type == Type::Error;
  45. }
  46. // https://fetch.spec.whatwg.org/#concept-response-url
  47. Optional<AK::URL const&> Response::url() const
  48. {
  49. // 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.
  50. if (m_url_list.is_empty())
  51. return {};
  52. return m_url_list.last();
  53. }
  54. // https://fetch.spec.whatwg.org/#concept-response-location-url
  55. ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& request_fragment) const
  56. {
  57. // 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.
  58. // 1. If response’s status is not a redirect status, then return null.
  59. if (!is_redirect_status(m_status))
  60. return Optional<AK::URL> {};
  61. // FIXME: 2. Let location be the result of extracting header list values given `Location` and response’s header list.
  62. auto location_value = ByteBuffer {};
  63. // 3. If location is a header value, then set location to the result of parsing location with response’s URL.
  64. auto location = AK::URL { StringView { location_value } };
  65. if (!location.is_valid())
  66. return Error::from_string_view("Invalid 'Location' header URL"sv);
  67. // 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
  68. if (location.fragment().is_null())
  69. location.set_fragment(request_fragment.value_or({}));
  70. // 5. Return location.
  71. return location;
  72. }
  73. // https://fetch.spec.whatwg.org/#concept-response-clone
  74. WebIDL::ExceptionOr<NonnullRefPtr<Response>> Response::clone() const
  75. {
  76. // To clone a response response, run these steps:
  77. // FIXME: 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of response’s internal response.
  78. // 2. Let newResponse be a copy of response, except for its body.
  79. auto new_response = Infrastructure::Response::create();
  80. new_response->set_type(m_type);
  81. new_response->set_aborted(m_aborted);
  82. new_response->set_url_list(m_url_list);
  83. new_response->set_status(m_status);
  84. new_response->set_status_message(m_status_message);
  85. for (auto const& header : *m_header_list)
  86. MUST(new_response->header_list()->append(header));
  87. new_response->set_cache_state(m_cache_state);
  88. new_response->set_cors_exposed_header_name_list(m_cors_exposed_header_name_list);
  89. new_response->set_range_requested(m_range_requested);
  90. new_response->set_request_includes_credentials(m_request_includes_credentials);
  91. new_response->set_timing_allow_passed(m_timing_allow_passed);
  92. new_response->set_body_info(m_body_info);
  93. // FIXME: service worker timing info
  94. // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
  95. if (m_body.has_value())
  96. new_response->set_body(TRY(m_body->clone()));
  97. // 4. Return newResponse.
  98. return new_response;
  99. }
  100. FilteredResponse::FilteredResponse(NonnullRefPtr<Response> internal_response)
  101. : m_internal_response(move(internal_response))
  102. {
  103. }
  104. FilteredResponse::~FilteredResponse()
  105. {
  106. }
  107. ErrorOr<NonnullRefPtr<BasicFilteredResponse>> BasicFilteredResponse::create(NonnullRefPtr<Response> internal_response)
  108. {
  109. // A basic filtered response is a filtered response whose type is "basic" and header list excludes
  110. // any headers in internal response’s header list whose name is a forbidden response-header name.
  111. auto header_list = make_ref_counted<HeaderList>();
  112. for (auto const& header : *internal_response->header_list()) {
  113. if (!is_forbidden_response_header_name(header.name))
  114. TRY(header_list->append(header));
  115. }
  116. return adopt_ref(*new BasicFilteredResponse(internal_response, move(header_list)));
  117. }
  118. BasicFilteredResponse::BasicFilteredResponse(NonnullRefPtr<Response> internal_response, NonnullRefPtr<HeaderList> header_list)
  119. : FilteredResponse(move(internal_response))
  120. , m_header_list(move(header_list))
  121. {
  122. }
  123. ErrorOr<NonnullRefPtr<CORSFilteredResponse>> CORSFilteredResponse::create(NonnullRefPtr<Response> internal_response)
  124. {
  125. // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
  126. // any headers in internal response’s header list whose name is not a CORS-safelisted response-header
  127. // name, given internal response’s CORS-exposed header-name list.
  128. Vector<ReadonlyBytes> cors_exposed_header_name_list;
  129. for (auto const& header_name : internal_response->cors_exposed_header_name_list())
  130. cors_exposed_header_name_list.append(header_name.span());
  131. auto header_list = make_ref_counted<HeaderList>();
  132. for (auto const& header : *internal_response->header_list()) {
  133. if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
  134. TRY(header_list->append(header));
  135. }
  136. return adopt_ref(*new CORSFilteredResponse(internal_response, move(header_list)));
  137. }
  138. CORSFilteredResponse::CORSFilteredResponse(NonnullRefPtr<Response> internal_response, NonnullRefPtr<HeaderList> header_list)
  139. : FilteredResponse(move(internal_response))
  140. , m_header_list(move(header_list))
  141. {
  142. }
  143. NonnullRefPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(NonnullRefPtr<Response> internal_response)
  144. {
  145. // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
  146. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  147. return adopt_ref(*new OpaqueFilteredResponse(move(internal_response)));
  148. }
  149. OpaqueFilteredResponse::OpaqueFilteredResponse(NonnullRefPtr<Response> internal_response)
  150. : FilteredResponse(move(internal_response))
  151. , m_header_list(make_ref_counted<HeaderList>())
  152. {
  153. }
  154. NonnullRefPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(NonnullRefPtr<Response> internal_response)
  155. {
  156. return adopt_ref(*new OpaqueRedirectFilteredResponse(move(internal_response)));
  157. }
  158. OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(NonnullRefPtr<Response> internal_response)
  159. : FilteredResponse(move(internal_response))
  160. , m_header_list(make_ref_counted<HeaderList>())
  161. {
  162. }
  163. }