Responses.cpp 6.8 KB

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