script/dom/processingoptions.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::str::FromStr;
6
7use base::id::WebViewId;
8use cssparser::match_ignore_ascii_case;
9use http::header::HeaderMap;
10use hyper_serde::Serde;
11use mime::Mime;
12use net_traits::fetch::headers::get_decode_and_split_header_name;
13use net_traits::mime_classifier::{MediaType, MimeClassifier};
14use net_traits::policy_container::PolicyContainer;
15use net_traits::request::{
16 CorsSettings, Destination, Initiator, InsecureRequestsPolicy, PreloadId, PreloadKey, Referrer,
17 RequestBuilder, RequestClient, RequestId,
18};
19use net_traits::response::{Response, ResponseBody};
20use net_traits::{
21 CoreResourceMsg, FetchMetadata, NetworkError, ReferrerPolicy, ResourceFetchTiming,
22};
23pub use nom_rfc8288::complete::LinkDataOwned as LinkHeader;
24use nom_rfc8288::complete::link_lenient as parse_link_header;
25use servo_url::{ImmutableOrigin, ServoUrl};
26use strum::IntoStaticStr;
27
28use crate::dom::bindings::refcounted::Trusted;
29use crate::dom::bindings::reflector::DomGlobal;
30use crate::dom::bindings::root::DomRoot;
31use crate::dom::csp::{GlobalCspReporting, Violation};
32use crate::dom::document::Document;
33use crate::dom::globalscope::GlobalScope;
34use crate::dom::medialist::MediaList;
35use crate::dom::node::NodeTraits;
36use crate::dom::performance::performanceresourcetiming::InitiatorType;
37use crate::dom::types::HTMLLinkElement;
38use crate::fetch::create_a_potential_cors_request;
39use crate::network_listener::{FetchResponseListener, ResourceTimingListener, submit_timing};
40use crate::script_runtime::CanGc;
41
42trait ValueForKeyInLinkHeader {
43 fn has_key_in_link_header(&self, key: &str) -> bool;
44 fn value_for_key_in_link_header(&self, key: &str) -> Option<&str>;
45}
46
47impl ValueForKeyInLinkHeader for LinkHeader {
48 fn has_key_in_link_header(&self, key: &str) -> bool {
49 self.params.iter().any(|p| p.key == key)
50 }
51 fn value_for_key_in_link_header(&self, key: &str) -> Option<&str> {
52 let param = self.params.iter().find(|p| p.key == key)?;
53 param.val.as_deref()
54 }
55}
56
57#[derive(PartialEq)]
58pub(crate) enum LinkProcessingPhase {
59 Media,
60 PreMedia,
61}
62
63/// <https://html.spec.whatwg.org/multipage/#link-processing-options>
64#[derive(Debug)]
65pub(crate) struct LinkProcessingOptions {
66 /// <https://html.spec.whatwg.org/multipage/#link-options-href>
67 pub(crate) href: String,
68 /// <https://html.spec.whatwg.org/multipage/#link-options-destination>
69 pub(crate) destination: Destination,
70 /// <https://html.spec.whatwg.org/multipage/#link-options-integrity>
71 pub(crate) integrity: String,
72 /// <https://html.spec.whatwg.org/multipage/#link-options-type>
73 pub(crate) link_type: String,
74 /// <https://html.spec.whatwg.org/multipage/#link-options-nonce>
75 pub(crate) cryptographic_nonce_metadata: String,
76 /// <https://html.spec.whatwg.org/multipage/#link-options-crossorigin>
77 pub(crate) cross_origin: Option<CorsSettings>,
78 /// <https://html.spec.whatwg.org/multipage/#link-options-referrer-policy>
79 pub(crate) referrer_policy: ReferrerPolicy,
80 /// <https://html.spec.whatwg.org/multipage/#link-options-policy-container>
81 pub(crate) policy_container: PolicyContainer,
82 /// <https://html.spec.whatwg.org/multipage/#link-options-source-set>
83 pub(crate) source_set: Option<()>,
84 /// <https://html.spec.whatwg.org/multipage/#link-options-base-url>
85 pub(crate) base_url: ServoUrl,
86 /// <https://html.spec.whatwg.org/multipage/#link-options-origin>
87 pub(crate) origin: ImmutableOrigin,
88 pub(crate) insecure_requests_policy: InsecureRequestsPolicy,
89 pub(crate) has_trustworthy_ancestor_origin: bool,
90 pub(crate) referrer: Referrer,
91 // https://html.spec.whatwg.org/multipage/#link-options-environment
92 pub(crate) request_client: RequestClient,
93 // https://html.spec.whatwg.org/multipage/#link-options-document
94 // TODO
95 // https://html.spec.whatwg.org/multipage/#link-options-on-document-ready
96 // TODO
97 // https://html.spec.whatwg.org/multipage/#link-options-fetch-priority
98 // TODO
99}
100
101impl LinkProcessingOptions {
102 /// <https://html.spec.whatwg.org/multipage/#apply-link-options-from-parsed-header-attributes>
103 fn apply_link_options_from_parsed_header(
104 &mut self,
105 link_object: &LinkHeader,
106 rel: &str,
107 ) -> bool {
108 // Step 1. If rel is "preload":
109 if rel == "preload" {
110 // Step 1.1. If attribs["as"] does not exist, then return false.
111 let Some(as_) = link_object.value_for_key_in_link_header("as") else {
112 return false;
113 };
114 // Step 1.2. Let destination be the result of translating attribs["as"].
115 let Some(destination) = Self::translate_a_preload_destination(as_) else {
116 // Step 1.3. If destination is null, then return false.
117 return false;
118 };
119 // Step 1.4. Set options's destination to destination.
120 self.destination = destination;
121 }
122 // Step 2. If attribs["crossorigin"] exists and is an ASCII case-insensitive match for one of the
123 // CORS settings attribute keywords, then set options's crossorigin to the CORS settings attribute
124 // state corresponding to that keyword.
125 if let Some(cross_origin) = link_object.value_for_key_in_link_header("crossorigin") {
126 self.cross_origin = determine_cors_settings_for_token(cross_origin);
127 }
128 // Step 3. If attribs["integrity"] exists, then set options's integrity to attribs["integrity"].
129 if let Some(integrity) = link_object.value_for_key_in_link_header("integrity") {
130 self.integrity = integrity.to_owned();
131 }
132 // Step 4. If attribs["referrerpolicy"] exists and is an ASCII case-insensitive match for
133 // some referrer policy, then set options's referrer policy to that referrer policy.
134 if let Some(referrer_policy) = link_object.value_for_key_in_link_header("referrerpolicy") {
135 self.referrer_policy = ReferrerPolicy::from(referrer_policy);
136 }
137 // Step 5. If attribs["nonce"] exists, then set options's nonce to attribs["nonce"].
138 if let Some(nonce) = link_object.value_for_key_in_link_header("nonce") {
139 self.cryptographic_nonce_metadata = nonce.to_owned();
140 }
141 // Step 6. If attribs["type"] exists, then set options's type to attribs["type"].
142 if let Some(link_type) = link_object.value_for_key_in_link_header("type") {
143 self.link_type = link_type.to_owned();
144 }
145 // Step 7. If attribs["fetchpriority"] exists and is an ASCII case-insensitive match
146 // for a fetch priority attribute keyword, then set options's fetch priority to that
147 // fetch priority attribute keyword.
148 // TODO
149 // Step 8. Return true.
150 true
151 }
152
153 /// <https://html.spec.whatwg.org/multipage/#process-a-link-header>
154 fn process_link_header(self, rel: &str, document: &Document) {
155 if rel == "preload" {
156 // https://html.spec.whatwg.org/multipage/#link-type-preload:process-a-link-header
157 // The process a link header step for this type of link given a link processing options options
158 // is to preload options.
159 if !self.type_matches_destination() {
160 return;
161 }
162 self.preload(document.window().webview_id(), None, document);
163 }
164 }
165
166 /// <https://html.spec.whatwg.org/multipage/#translate-a-preload-destination>
167 pub(crate) fn translate_a_preload_destination(
168 potential_destination: &str,
169 ) -> Option<Destination> {
170 // Step 2. Return the result of translating destination.
171 Some(match potential_destination {
172 "fetch" => Destination::None,
173 "font" => Destination::Font,
174 "image" => Destination::Image,
175 "script" => Destination::Script,
176 "style" => Destination::Style,
177 "track" => Destination::Track,
178 // Step 1. If destination is not "fetch", "font", "image",
179 // "script", "style", or "track", then return null.
180 _ => return None,
181 })
182 }
183
184 /// <https://html.spec.whatwg.org/multipage/#create-a-link-request>
185 pub(crate) fn create_link_request(self, webview_id: WebViewId) -> Option<RequestBuilder> {
186 // Step 1. Assert: options's href is not the empty string.
187 assert!(!self.href.is_empty());
188
189 // Step 3. Let url be the result of encoding-parsing a URL given options's href, relative to options's base URL.
190 let Ok(url) = ServoUrl::parse_with_base(Some(&self.base_url), &self.href) else {
191 // Step 4. If url is failure, then return null.
192 return None;
193 };
194
195 // Step 5. Let request be the result of creating a potential-CORS request given
196 // url, options's destination, and options's crossorigin.
197 // Step 6. Set request's policy container to options's policy container.
198 // Step 7. Set request's integrity metadata to options's integrity.
199 // Step 8. Set request's cryptographic nonce metadata to options's cryptographic nonce metadata.
200 // Step 9. Set request's referrer policy to options's referrer policy.
201 // Step 10. Set request's client to options's environment.
202 // FIXME: Step 11. Set request's priority to options's fetch priority.
203 let builder = create_a_potential_cors_request(
204 Some(webview_id),
205 url,
206 self.destination,
207 self.cross_origin,
208 None,
209 self.referrer,
210 )
211 .insecure_requests_policy(self.insecure_requests_policy)
212 .has_trustworthy_ancestor_origin(self.has_trustworthy_ancestor_origin)
213 .policy_container(self.policy_container)
214 .client(self.request_client)
215 .initiator(Initiator::Link)
216 .origin(self.origin)
217 .integrity_metadata(self.integrity)
218 .cryptographic_nonce_metadata(self.cryptographic_nonce_metadata)
219 .referrer_policy(self.referrer_policy);
220
221 // Step 12. Return request.
222 Some(builder)
223 }
224
225 /// <https://html.spec.whatwg.org/multipage/#match-preload-type>
226 pub(crate) fn type_matches_destination(&self) -> bool {
227 // Step 1. If type is an empty string, then return true.
228 if self.link_type.is_empty() {
229 return true;
230 }
231 // Step 2. If destination is "fetch", then return true.
232 //
233 // Fetch is handled as an empty string destination in the spec:
234 // https://fetch.spec.whatwg.org/#concept-potential-destination-translate
235 let destination = self.destination;
236 if destination == Destination::None {
237 return true;
238 }
239 // Step 3. Let mimeTypeRecord be the result of parsing type.
240 let Ok(mime_type_record) = Mime::from_str(&self.link_type) else {
241 // Step 4. If mimeTypeRecord is failure, then return false.
242 return false;
243 };
244 // Step 5. If mimeTypeRecord is not supported by the user agent, then return false.
245 //
246 // We currently don't check if we actually support the mime type. Only if we can classify
247 // it according to the spec.
248 let Some(mime_type) = MimeClassifier::get_media_type(&mime_type_record) else {
249 return false;
250 };
251 // Step 6. If any of the following are true:
252 if
253 // destination is "audio" or "video", and mimeTypeRecord is an audio or video MIME type;
254 ((destination == Destination::Audio || destination == Destination::Video) &&
255 mime_type == MediaType::AudioVideo)
256 // destination is a script-like destination and mimeTypeRecord is a JavaScript MIME type;
257 || (destination.is_script_like() && mime_type == MediaType::JavaScript)
258 // destination is "image" and mimeTypeRecord is an image MIME type;
259 || (destination == Destination::Image && mime_type == MediaType::Image)
260 // destination is "font" and mimeTypeRecord is a font MIME type;
261 || (destination == Destination::Font && mime_type == MediaType::Font)
262 // destination is "json" and mimeTypeRecord is a JSON MIME type;
263 || (destination == Destination::Json && mime_type == MediaType::Json)
264 // destination is "style" and mimeTypeRecord's essence is text/css; or
265 || (destination == Destination::Style && mime_type_record == mime::TEXT_CSS)
266 // destination is "track" and mimeTypeRecord's essence is text/vtt,
267 || (destination == Destination::Track && mime_type_record.essence_str() == "text/vtt")
268 {
269 // then return true.
270 return true;
271 }
272 // Step 7. Return false.
273 false
274 }
275
276 /// <https://html.spec.whatwg.org/multipage/#preload>
277 pub(crate) fn preload(
278 self,
279 webview_id: WebViewId,
280 link: Option<Trusted<HTMLLinkElement>>,
281 document: &Document,
282 ) {
283 // Step 1. If options's type doesn't match options's destination, then return.
284 //
285 // Handled by callers, since we need to check the previous destination type
286 assert!(self.type_matches_destination());
287 // Step 2. If options's destination is "image" and options's source set is not null,
288 // then set options's href to the result of selecting an image source from options's source set.
289 // TODO
290 // Step 3. Let request be the result of creating a link request given options.
291 let Some(request) = self.create_link_request(webview_id) else {
292 // Step 4. If request is null, then return.
293 return;
294 };
295 let preload_id = PreloadId::default();
296 let request = request.preload_id(preload_id.clone());
297 // Step 5. Let unsafeEndTime be 0.
298 // TODO
299 // Step 6. Let entry be a new preload entry whose integrity metadata is options's integrity.
300 //
301 // This is performed in `CoreResourceManager::fetch`
302 // Step 7. Let key be the result of creating a preload key given request.
303 let key = PreloadKey::new(&request);
304 // Step 8. If options's document is "pending", then set request's initiator type to "early hint".
305 // TODO
306 // Step 9. Let controller be null.
307 // Step 10. Let reportTiming given a Document document be to report timing for controller
308 // given document's relevant global object.
309 let url = request.url.clone();
310 let fetch_context = LinkFetchContext {
311 url,
312 link,
313 document: Trusted::new(document),
314 global: Trusted::new(&document.global()),
315 type_: LinkFetchContextType::Preload(key.clone()),
316 response_body: vec![],
317 };
318 document.insert_preloaded_resource(key, preload_id);
319 // Step 11. Set controller to the result of fetching request, with processResponseConsumeBody
320 // set to the following steps given a response response and null, failure, or a byte sequence bodyBytes:
321 document.fetch_background(request, fetch_context);
322 }
323}
324
325pub(crate) fn determine_cors_settings_for_token(token: &str) -> Option<CorsSettings> {
326 match_ignore_ascii_case! { token,
327 "anonymous" => Some(CorsSettings::Anonymous),
328 "use-credentials" => Some(CorsSettings::UseCredentials),
329 _ => None,
330 }
331}
332
333/// <https://html.spec.whatwg.org/multipage/#extract-links-from-headers>
334pub(crate) fn extract_links_from_headers(headers: &Option<Serde<HeaderMap>>) -> Vec<LinkHeader> {
335 // Step 1. Let links be a new list.
336 let mut links = Vec::new();
337 let Some(headers) = headers else {
338 return links;
339 };
340 // Step 2. Let rawLinkHeaders be the result of getting, decoding, and splitting `Link` from headers.
341 let Some(raw_link_headers) = get_decode_and_split_header_name("Link", headers) else {
342 return links;
343 };
344 // Step 3. For each linkHeader of rawLinkHeaders:
345 for link_header in raw_link_headers {
346 // Step 3.1. Let linkObject be the result of parsing linkHeader. [WEBLINK]
347 let Ok(parsed_link_header) = parse_link_header(&link_header) else {
348 continue;
349 };
350 for link_object in parsed_link_header {
351 let Some(link_object) = link_object else {
352 // Step 3.2. If linkObject["target_uri"] does not exist, then continue.
353 continue;
354 };
355 // Step 3.3. Append linkObject to links.
356 links.push(link_object.to_owned());
357 }
358 }
359 // Step 4. Return links.
360 links
361}
362
363/// <https://html.spec.whatwg.org/multipage/#process-link-headers>
364pub(crate) fn process_link_headers(
365 link_headers: &[LinkHeader],
366 document: &Document,
367 phase: LinkProcessingPhase,
368) {
369 let global = document.owner_global();
370 // Step 1. Let links be the result of extracting links from response's header list.
371 //
372 // Already performed once when parsing headers by caller
373 // Step 2. For each linkObject in links:
374 for link_object in link_headers {
375 // Step 2.1. Let rel be linkObject["relation_type"].
376 let Some(rel) = link_object.value_for_key_in_link_header("rel") else {
377 continue;
378 };
379 // Step 2.2. Let attribs be linkObject["target_attributes"].
380 //
381 // Not applicable, that's in `link_object.params`
382 // Step 2.3. Let expectedPhase be "media" if either "srcset", "imagesrcset",
383 // or "media" exist in attribs; otherwise "pre-media".
384 let expected_phase = if link_object.has_key_in_link_header("srcset") ||
385 link_object.has_key_in_link_header("imagesrcset") ||
386 link_object.has_key_in_link_header("media")
387 {
388 LinkProcessingPhase::Media
389 } else {
390 LinkProcessingPhase::PreMedia
391 };
392 // Step 2.4. If expectedPhase is not phase, then continue.
393 if expected_phase != phase {
394 continue;
395 }
396 // Step 2.5. If attribs["media"] exists and attribs["media"] does not match the environment, then continue.
397 if let Some(media) = link_object.value_for_key_in_link_header("media") {
398 if !MediaList::matches_environment(document, media) {
399 continue;
400 }
401 }
402 // Step 2.6. Let options be a new link processing options with
403 let mut options = LinkProcessingOptions {
404 href: link_object.url.clone(),
405 destination: Destination::None,
406 integrity: String::new(),
407 link_type: String::new(),
408 cryptographic_nonce_metadata: String::new(),
409 cross_origin: None,
410 referrer_policy: ReferrerPolicy::EmptyString,
411 policy_container: document.policy_container().to_owned(),
412 source_set: None,
413 origin: document.origin().immutable().to_owned(),
414 base_url: document.base_url(),
415 insecure_requests_policy: document.insecure_requests_policy(),
416 has_trustworthy_ancestor_origin: document.has_trustworthy_ancestor_or_current_origin(),
417 request_client: global.request_client(),
418 referrer: global.get_referrer(),
419 };
420 // Step 2.7. Apply link options from parsed header attributes to options given attribs and rel.
421 // If that returned false, then return.
422 if !options.apply_link_options_from_parsed_header(link_object, rel) {
423 return;
424 }
425 // Step 2.8. If attribs["imagesrcset"] exists and attribs["imagesizes"] exists,
426 // then set options's source set to the result of creating a source set given
427 // linkObject["target_uri"], attribs["imagesrcset"], attribs["imagesizes"], and null.
428 // TODO
429 // Step 2.9. Run the process a link header steps for rel given options.
430 options.process_link_header(rel, document);
431 }
432}
433
434#[derive(Clone, IntoStaticStr)]
435#[strum(serialize_all = "lowercase")]
436pub(crate) enum LinkFetchContextType {
437 Prefetch,
438 Preload(PreloadKey),
439}
440
441impl From<LinkFetchContextType> for InitiatorType {
442 fn from(other: LinkFetchContextType) -> Self {
443 let name: &'static str = other.into();
444 InitiatorType::LocalName(name.to_owned())
445 }
446}
447
448pub(crate) struct LinkFetchContext {
449 /// The `<link>` element (if any) that caused this fetch
450 pub(crate) link: Option<Trusted<HTMLLinkElement>>,
451
452 pub(crate) global: Trusted<GlobalScope>,
453 pub(crate) document: Trusted<Document>,
454
455 /// The url being prefetched
456 pub(crate) url: ServoUrl,
457
458 /// The type of fetching we perform, used when report timings.
459 pub(crate) type_: LinkFetchContextType,
460
461 pub(crate) response_body: Vec<u8>,
462}
463
464impl FetchResponseListener for LinkFetchContext {
465 fn process_request_body(&mut self, _: RequestId) {}
466
467 fn process_request_eof(&mut self, _: RequestId) {}
468
469 fn process_response(
470 &mut self,
471 _: RequestId,
472 fetch_metadata: Result<FetchMetadata, NetworkError>,
473 ) {
474 _ = fetch_metadata;
475 }
476
477 fn process_response_chunk(&mut self, _: RequestId, mut chunk: Vec<u8>) {
478 if matches!(self.type_, LinkFetchContextType::Preload(..)) {
479 self.response_body.append(&mut chunk);
480 }
481 }
482
483 /// Step 7 of <https://html.spec.whatwg.org/multipage/#link-type-prefetch:fetch-and-process-the-linked-resource-2>
484 /// and step 3.1 of <https://html.spec.whatwg.org/multipage/#link-type-preload:fetch-and-process-the-linked-resource-2>
485 fn process_response_eof(
486 mut self,
487 cx: &mut js::context::JSContext,
488 _: RequestId,
489 response_result: Result<(), NetworkError>,
490 timing: ResourceFetchTiming,
491 ) {
492 // Steps for https://html.spec.whatwg.org/multipage/#preload
493 if let LinkFetchContextType::Preload(key) = &self.type_ {
494 let response = if response_result.is_ok() {
495 // Step 11.1. If bodyBytes is a byte sequence, then set response's body to bodyBytes as a body.
496 let response = Response::new(self.url.clone(), timing.clone());
497 *response.body.lock() = ResponseBody::Done(std::mem::take(&mut self.response_body));
498 response
499 } else {
500 // Step 11.2. Otherwise, set response to a network error.
501 Response::network_error(NetworkError::ResourceLoadError("Failed to preload".into()))
502 };
503 // Step 11.5. If entry's on response available is null, then set entry's response to response;
504 // otherwise call entry's on response available given response.
505 // Step 12. Let commit be the following steps given a Document document:
506 // Step 12.1. If entry's response is not null, then call reportTiming given document.
507 // Step 12.2. Set document's map of preloaded resources[key] to entry.
508 // Step 13. If options's document is null, then set options's on document ready to commit. Otherwise, call commit with options's document.
509 let document = self.document.root();
510 let document_preloaded_resources = document.preloaded_resources();
511 let Some(preload_id) = document_preloaded_resources.get(key) else {
512 unreachable!(
513 "Must only be able to lookup preloaded resources if they already exist in document"
514 );
515 };
516 let _ = self.global.root().core_resource_thread().send(
517 CoreResourceMsg::StorePreloadedResponse(preload_id.clone(), response),
518 );
519 }
520
521 submit_timing(&self, &response_result, &timing, CanGc::from_cx(cx));
522
523 // Step 11.6. If processResponse is given, then call processResponse with response.
524 //
525 // Part of Preload
526 //
527 // Step 6. Let processPrefetchResponse be the following steps given a response response and null, failure, or a byte sequence bytesOrNull:
528 //
529 // Part of Prefetch
530 if let Some(link) = self.link.as_ref() {
531 link.root()
532 .fire_event_after_response(response_result, CanGc::from_cx(cx));
533 }
534 }
535
536 fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
537 let global = &self.resource_timing_global();
538 global.report_csp_violations(violations, None, None);
539 }
540}
541
542impl ResourceTimingListener for LinkFetchContext {
543 fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
544 (self.type_.clone().into(), self.url.clone())
545 }
546
547 fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
548 self.global.root()
549 }
550}