Headers.cpp 13 KB

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