Headers.cpp 14 KB

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