script/dom/linkprocessingoptions.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::str::FromStr;
6
7use base::id::WebViewId;
8use mime::Mime;
9use net_traits::ReferrerPolicy;
10use net_traits::mime_classifier::{MediaType, MimeClassifier};
11use net_traits::policy_container::PolicyContainer;
12use net_traits::request::{
13 CorsSettings, Destination, Initiator, InsecureRequestsPolicy, Referrer, RequestBuilder,
14};
15use servo_url::{ImmutableOrigin, ServoUrl};
16
17use crate::fetch::create_a_potential_cors_request;
18
19/// <https://html.spec.whatwg.org/multipage/#link-processing-options>
20pub(crate) struct LinkProcessingOptions {
21 pub(crate) href: String,
22 pub(crate) destination: Option<Destination>,
23 pub(crate) integrity: String,
24 pub(crate) link_type: String,
25 pub(crate) cryptographic_nonce_metadata: String,
26 pub(crate) cross_origin: Option<CorsSettings>,
27 pub(crate) referrer_policy: ReferrerPolicy,
28 pub(crate) policy_container: PolicyContainer,
29 pub(crate) source_set: Option<()>,
30 pub(crate) base_url: ServoUrl,
31 pub(crate) origin: ImmutableOrigin,
32 pub(crate) insecure_requests_policy: InsecureRequestsPolicy,
33 pub(crate) has_trustworthy_ancestor_origin: bool,
34 // Some fields that we don't need yet are missing
35}
36
37impl LinkProcessingOptions {
38 /// <https://html.spec.whatwg.org/multipage/#create-a-link-request>
39 pub(crate) fn create_link_request(self, webview_id: WebViewId) -> Option<RequestBuilder> {
40 // Step 1. Assert: options's href is not the empty string.
41 assert!(!self.href.is_empty());
42
43 // Step 2. If options's destination is null, then return null.
44 let destination = self.destination?;
45
46 // Step 3. Let url be the result of encoding-parsing a URL given options's href, relative to options's base URL.
47 let Ok(url) = ServoUrl::parse_with_base(Some(&self.base_url), &self.href) else {
48 // Step 4. If url is failure, then return null.
49 return None;
50 };
51
52 // Step 5. Let request be the result of creating a potential-CORS request given
53 // url, options's destination, and options's crossorigin.
54 // Step 6. Set request's policy container to options's policy container.
55 // Step 7. Set request's integrity metadata to options's integrity.
56 // Step 8. Set request's cryptographic nonce metadata to options's cryptographic nonce metadata.
57 // Step 9. Set request's referrer policy to options's referrer policy.
58 // FIXME: Step 10. Set request's client to options's environment.
59 // FIXME: Step 11. Set request's priority to options's fetch priority.
60 // FIXME: Use correct referrer
61 let builder = create_a_potential_cors_request(
62 Some(webview_id),
63 url,
64 destination,
65 self.cross_origin,
66 None,
67 Referrer::NoReferrer,
68 self.insecure_requests_policy,
69 self.has_trustworthy_ancestor_origin,
70 self.policy_container,
71 )
72 .initiator(Initiator::Link)
73 .origin(self.origin)
74 .integrity_metadata(self.integrity)
75 .cryptographic_nonce_metadata(self.cryptographic_nonce_metadata)
76 .referrer_policy(self.referrer_policy);
77
78 // Step 12. Return request.
79 Some(builder)
80 }
81
82 /// <https://html.spec.whatwg.org/multipage/#match-preload-type>
83 pub(crate) fn type_matches_destination(&self) -> bool {
84 // Step 1. If type is an empty string, then return true.
85 if self.link_type.is_empty() {
86 return true;
87 }
88 // Step 2. If destination is "fetch", then return true.
89 //
90 // Fetch is handled as an empty string destination in the spec:
91 // https://fetch.spec.whatwg.org/#concept-potential-destination-translate
92 let Some(destination) = self.destination else {
93 return false;
94 };
95 if destination == Destination::None {
96 return true;
97 }
98 // Step 3. Let mimeTypeRecord be the result of parsing type.
99 let Ok(mime_type_record) = Mime::from_str(&self.link_type) else {
100 // Step 4. If mimeTypeRecord is failure, then return false.
101 return false;
102 };
103 // Step 5. If mimeTypeRecord is not supported by the user agent, then return false.
104 //
105 // We currently don't check if we actually support the mime type. Only if we can classify
106 // it according to the spec.
107 let Some(mime_type) = MimeClassifier::get_media_type(&mime_type_record) else {
108 return false;
109 };
110 // Step 6. If any of the following are true:
111 if
112 // destination is "audio" or "video", and mimeTypeRecord is an audio or video MIME type;
113 ((destination == Destination::Audio || destination == Destination::Video) &&
114 mime_type == MediaType::AudioVideo)
115 // destination is a script-like destination and mimeTypeRecord is a JavaScript MIME type;
116 || (destination.is_script_like() && mime_type == MediaType::JavaScript)
117 // destination is "image" and mimeTypeRecord is an image MIME type;
118 || (destination == Destination::Image && mime_type == MediaType::Image)
119 // destination is "font" and mimeTypeRecord is a font MIME type;
120 || (destination == Destination::Font && mime_type == MediaType::Font)
121 // destination is "json" and mimeTypeRecord is a JSON MIME type;
122 || (destination == Destination::Json && mime_type == MediaType::Json)
123 // destination is "style" and mimeTypeRecord's essence is text/css; or
124 || (destination == Destination::Style && mime_type_record == mime::TEXT_CSS)
125 // destination is "track" and mimeTypeRecord's essence is text/vtt,
126 || (destination == Destination::Track && mime_type_record.essence_str() == "text/vtt")
127 {
128 // then return true.
129 return true;
130 }
131 // Step 7. Return false.
132 false
133 }
134
135 /// <https://html.spec.whatwg.org/multipage/#preload>
136 pub(crate) fn preload(self, webview_id: WebViewId) -> Option<RequestBuilder> {
137 // Step 1. If options's type doesn't match options's destination, then return.
138 //
139 // Handled by callers, since we need to check the previous destination type
140 assert!(self.type_matches_destination());
141 // Step 2. If options's destination is "image" and options's source set is not null,
142 // then set options's href to the result of selecting an image source from options's source set.
143 // TODO
144 // Step 3. Let request be the result of creating a link request given options.
145 let Some(request) = self.create_link_request(webview_id) else {
146 // Step 4. If request is null, then return.
147 return None;
148 };
149 // Step 5. Let unsafeEndTime be 0.
150 // TODO
151 // Step 6. Let entry be a new preload entry whose integrity metadata is options's integrity.
152 // TODO
153 // Step 7. Let key be the result of creating a preload key given request.
154 // TODO
155 // Step 8. If options's document is "pending", then set request's initiator type to "early hint".
156 // TODO
157 // Step 9. Let controller be null.
158 // Step 10. Let reportTiming given a Document document be to report timing for controller
159 // given document's relevant global object.
160 // Step 11. Set controller to the result of fetching request, with processResponseConsumeBody
161 // set to the following steps given a response response and null, failure, or a byte sequence bodyBytes:
162 Some(request.clone())
163 }
164}