Headers.cpp 13 KB

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