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