Headers.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/VM.h>
  7. #include <LibWeb/Fetch/Headers.h>
  8. #include <LibWeb/HTML/Window.h>
  9. namespace Web::Fetch {
  10. // https://fetch.spec.whatwg.org/#dom-headers
  11. WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init)
  12. {
  13. // The new Headers(init) constructor steps are:
  14. auto* headers = window.heap().allocate<Headers>(window.realm(), window);
  15. // 1. Set this’s guard to "none".
  16. headers->m_guard = Guard::None;
  17. // 2. If init is given, then fill this with init.
  18. if (init.has_value())
  19. TRY(headers->fill(*init));
  20. return JS::NonnullGCPtr(*headers);
  21. }
  22. Headers::Headers(HTML::Window& window)
  23. : PlatformObject(window.realm())
  24. , m_header_list(make_ref_counted<Infrastructure::HeaderList>())
  25. {
  26. set_prototype(&window.cached_web_prototype("Headers"));
  27. }
  28. Headers::~Headers() = default;
  29. // https://fetch.spec.whatwg.org/#dom-headers-append
  30. WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string)
  31. {
  32. // The append(name, value) method steps are to append (name, value) to this.
  33. auto header = Infrastructure::Header {
  34. .name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name_string.bytes())),
  35. .value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value_string.bytes())),
  36. };
  37. TRY(append(move(header)));
  38. return {};
  39. }
  40. // https://fetch.spec.whatwg.org/#dom-headers-delete
  41. WebIDL::ExceptionOr<void> Headers::delete_(String const& name_string)
  42. {
  43. // The delete(name) method steps are:
  44. auto name = name_string.bytes();
  45. // 1. If name is not a header name, then throw a TypeError.
  46. if (!Infrastructure::is_header_name(name))
  47. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
  48. // 2. If this’s guard is "immutable", then throw a TypeError.
  49. if (m_guard == Guard::Immutable)
  50. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
  51. // 3. Otherwise, if this’s guard is "request" and name is a forbidden header name, return.
  52. if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
  53. return {};
  54. // 4. Otherwise, if this’s guard is "request-no-cors", name is not a no-CORS-safelisted request-header name, and name is not a privileged no-CORS request-header name, return.
  55. if (m_guard == Guard::RequestNoCORS && !Infrastructure::is_no_cors_safelisted_request_header_name(name) && !Infrastructure::is_privileged_no_cors_request_header_name(name))
  56. return {};
  57. // 5. Otherwise, if this’s guard is "response" and name is a forbidden response-header name, return.
  58. if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
  59. return {};
  60. // 6. If this’s header list does not contain name, then return.
  61. if (!m_header_list->contains(name))
  62. return {};
  63. // 7. Delete name from this’s header list.
  64. m_header_list->delete_(name);
  65. // 8. If this’s guard is "request-no-cors", then remove privileged no-CORS request headers from this.
  66. if (m_guard == Guard::RequestNoCORS)
  67. remove_privileged_no_cors_headers();
  68. return {};
  69. }
  70. // https://fetch.spec.whatwg.org/#dom-headers-get
  71. WebIDL::ExceptionOr<String> Headers::get(String const& name_string)
  72. {
  73. // The get(name) method steps are:
  74. auto name = name_string.bytes();
  75. // 1. If name is not a header name, then throw a TypeError.
  76. if (!Infrastructure::is_header_name(name))
  77. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
  78. // 2. Return the result of getting name from this’s header list.
  79. auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
  80. // FIXME: Teach BindingsGenerator about Optional<String>
  81. return byte_buffer.has_value() ? String { byte_buffer->span() } : String {};
  82. }
  83. // https://fetch.spec.whatwg.org/#dom-headers-has
  84. WebIDL::ExceptionOr<bool> Headers::has(String const& name_string)
  85. {
  86. // The has(name) method steps are:
  87. auto name = name_string.bytes();
  88. // 1. If name is not a header name, then throw a TypeError.
  89. if (!Infrastructure::is_header_name(name))
  90. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
  91. // 2. Return true if this’s header list contains name; otherwise false.
  92. return m_header_list->contains(name);
  93. }
  94. // https://fetch.spec.whatwg.org/#dom-headers-set
  95. WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const& value_string)
  96. {
  97. // The set(name, value) method steps are:
  98. auto name = name_string.bytes();
  99. auto value = value_string.bytes();
  100. // 1. Normalize value.
  101. auto normalized_value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
  102. auto header = Infrastructure::Header {
  103. .name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
  104. .value = move(normalized_value),
  105. };
  106. // 2. If name is not a header name or value is not a header value, then throw a TypeError.
  107. if (!Infrastructure::is_header_name(name))
  108. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
  109. if (!Infrastructure::is_header_value(value))
  110. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value" };
  111. // 3. If this’s guard is "immutable", then throw a TypeError.
  112. if (m_guard == Guard::Immutable)
  113. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
  114. // 4. Otherwise, if this’s guard is "request" and name is a forbidden header name, return.
  115. if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
  116. return {};
  117. // 5. Otherwise, if this’s guard is "request-no-cors" and name/value is not a no-CORS-safelisted request-header, return.
  118. if (m_guard == Guard::RequestNoCORS && !Infrastructure::is_no_cors_safelisted_request_header(header))
  119. return {};
  120. // 6. Otherwise, if this’s guard is "response" and name is a forbidden response-header name, return.
  121. if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
  122. return {};
  123. // 7. Set (name, value) in this’s header list.
  124. TRY_OR_RETURN_OOM(global_object(), m_header_list->set(move(header)));
  125. // 8. If this’s guard is "request-no-cors", then remove privileged no-CORS request headers from this.
  126. if (m_guard == Guard::RequestNoCORS)
  127. remove_privileged_no_cors_headers();
  128. return {};
  129. }
  130. // https://webidl.spec.whatwg.org/#es-iterable, Step 4
  131. JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
  132. {
  133. // The value pairs to iterate over are the return value of running sort and combine with this’s header list.
  134. auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {
  135. auto headers_or_error = m_header_list->sort_and_combine();
  136. if (headers_or_error.is_error())
  137. return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate);
  138. return headers_or_error.release_value();
  139. };
  140. // 1-5. Are done in the generated wrapper code.
  141. // 6. Let pairs be idlObject’s list of value pairs to iterate over.
  142. auto pairs = TRY(value_pairs_to_iterate_over());
  143. // 7. Let i be 0.
  144. size_t i = 0;
  145. // 8. While i < pairs’s size:
  146. while (i < pairs.size()) {
  147. // 1. Let pair be pairs[i].
  148. auto const& pair = pairs[i];
  149. // 2. Invoke idlCallback with « pair’s value, pair’s key, idlObject » and with thisArg as the callback this value.
  150. TRY(callback(StringView { pair.name }, StringView { pair.value }));
  151. // 3. Set pairs to idlObject’s current list of value pairs to iterate over. (It might have changed.)
  152. pairs = TRY(value_pairs_to_iterate_over());
  153. // 4. Set i to i + 1.
  154. ++i;
  155. }
  156. return {};
  157. }
  158. // https://fetch.spec.whatwg.org/#concept-headers-append
  159. WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
  160. {
  161. // To append a header (name, value) to a Headers object headers, run these steps:
  162. auto& [name, value] = header;
  163. // 1. Normalize value.
  164. value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
  165. // 2. If name is not a header name or value is not a header value, then throw a TypeError.
  166. if (!Infrastructure::is_header_name(name))
  167. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
  168. if (!Infrastructure::is_header_value(value))
  169. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value" };
  170. // 3. If headers’s guard is "immutable", then throw a TypeError.
  171. if (m_guard == Guard::Immutable)
  172. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable" };
  173. // 4. Otherwise, if headers’s guard is "request" and name is a forbidden header name, return.
  174. if (m_guard == Guard::Request && Infrastructure::is_forbidden_header_name(name))
  175. return {};
  176. // 5. Otherwise, if headers’s guard is "request-no-cors":
  177. if (m_guard == Guard::RequestNoCORS) {
  178. // 1. Let temporaryValue be the result of getting name from headers’s header list.
  179. auto temporary_value = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
  180. // 2. If temporaryValue is null, then set temporaryValue to value.
  181. if (!temporary_value.has_value()) {
  182. temporary_value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value));
  183. }
  184. // 3. Otherwise, set temporaryValue to temporaryValue, followed by 0x2C 0x20, followed by value.
  185. else {
  186. TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x2c));
  187. TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x20));
  188. TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(value));
  189. }
  190. auto temporary_header = Infrastructure::Header {
  191. .name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
  192. .value = temporary_value.release_value(),
  193. };
  194. // 4. If name/temporaryValue is not a no-CORS-safelisted request-header, then return.
  195. if (!Infrastructure::is_no_cors_safelisted_request_header(temporary_header))
  196. return {};
  197. }
  198. // 6. Otherwise, if headers’s guard is "response" and name is a forbidden response-header name, return.
  199. if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
  200. return {};
  201. // 7. Append (name, value) to headers’s header list
  202. TRY_OR_RETURN_OOM(global_object(), m_header_list->append(move(header)));
  203. // 8. If headers’s guard is "request-no-cors", then remove privileged no-CORS request headers from headers.
  204. if (m_guard == Guard::RequestNoCORS)
  205. remove_privileged_no_cors_headers();
  206. return {};
  207. }
  208. // https://fetch.spec.whatwg.org/#concept-headers-fill
  209. WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
  210. {
  211. // To fill a Headers object headers with a given object object, run these steps:
  212. return object.visit(
  213. // 1. If object is a sequence, then for each header in object:
  214. [this](Vector<Vector<String>> const& object) -> WebIDL::ExceptionOr<void> {
  215. for (auto const& entry : object) {
  216. // 1. If header does not contain exactly two items, then throw a TypeError.
  217. if (entry.size() != 2)
  218. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair" };
  219. // 2. Append (header’s first item, header’s second item) to headers.
  220. auto header = Fetch::Infrastructure::Header {
  221. .name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[0].bytes())),
  222. .value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[1].bytes())),
  223. };
  224. TRY(append(move(header)));
  225. }
  226. return {};
  227. },
  228. // 2. Otherwise, object is a record, then for each key → value in object, append (key, value) to headers.
  229. [this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
  230. for (auto const& entry : object) {
  231. auto header = Fetch::Infrastructure::Header {
  232. .name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.key.bytes())),
  233. .value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.value.bytes())),
  234. };
  235. TRY(append(move(header)));
  236. }
  237. return {};
  238. });
  239. }
  240. // https://fetch.spec.whatwg.org/#concept-headers-remove-privileged-no-cors-request-headers
  241. void Headers::remove_privileged_no_cors_headers()
  242. {
  243. // To remove privileged no-CORS request headers from a Headers object (headers), run these steps:
  244. static constexpr Array privileged_no_cors_request_header_names = {
  245. "Range"sv,
  246. };
  247. // 1. For each headerName of privileged no-CORS request-header names:
  248. for (auto const& header_name : privileged_no_cors_request_header_names) {
  249. // 1. Delete headerName from headers’s header list.
  250. m_header_list->delete_(header_name.bytes());
  251. }
  252. }
  253. }