Skip to main content

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