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