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