script/dom/url/
url.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::default::Default;
6
7use dom_struct::dom_struct;
8use js::rust::HandleObject;
9use net_traits::CoreResourceMsg;
10use net_traits::blob_url_store::parse_blob_url;
11use net_traits::filemanager_thread::FileManagerThreadMsg;
12use profile_traits::generic_channel;
13use script_bindings::cformat;
14use servo_base::generic_channel::GenericSend;
15use servo_url::{ImmutableOrigin, ServoUrl};
16use url::Url;
17use uuid::Uuid;
18
19use crate::dom::bindings::cell::DomRefCell;
20use crate::dom::bindings::codegen::Bindings::URLBinding::URLMethods;
21use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
22use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
23use crate::dom::bindings::root::{DomRoot, MutNullableDom};
24use crate::dom::bindings::str::{DOMString, USVString};
25use crate::dom::blob::Blob;
26use crate::dom::globalscope::GlobalScope;
27use crate::dom::url::urlhelper::UrlHelper;
28use crate::dom::url::urlsearchparams::URLSearchParams;
29use crate::script_runtime::CanGc;
30
31/// <https://url.spec.whatwg.org/#url>
32#[dom_struct]
33#[expect(clippy::upper_case_acronyms)]
34pub(crate) struct URL {
35    reflector_: Reflector,
36
37    /// <https://url.spec.whatwg.org/#concept-url-url>
38    #[no_trace]
39    url: DomRefCell<ServoUrl>,
40
41    /// <https://url.spec.whatwg.org/#dom-url-searchparams>
42    search_params: MutNullableDom<URLSearchParams>,
43}
44
45impl URL {
46    fn new_inherited(url: ServoUrl) -> URL {
47        URL {
48            reflector_: Reflector::new(),
49            url: DomRefCell::new(url),
50            search_params: Default::default(),
51        }
52    }
53
54    fn new(
55        global: &GlobalScope,
56        proto: Option<HandleObject>,
57        url: ServoUrl,
58        can_gc: CanGc,
59    ) -> DomRoot<URL> {
60        reflect_dom_object_with_proto(Box::new(URL::new_inherited(url)), global, proto, can_gc)
61    }
62
63    pub(crate) fn query_pairs(&self) -> Vec<(String, String)> {
64        self.url
65            .borrow()
66            .as_url()
67            .query_pairs()
68            .into_owned()
69            .collect()
70    }
71
72    pub(crate) fn origin(&self) -> ImmutableOrigin {
73        self.url.borrow().origin()
74    }
75
76    pub(crate) fn set_query_pairs(&self, pairs: &[(String, String)]) {
77        let mut url = self.url.borrow_mut();
78
79        if pairs.is_empty() {
80            url.as_mut_url().set_query(None);
81        } else {
82            url.as_mut_url()
83                .query_pairs_mut()
84                .clear()
85                .extend_pairs(pairs);
86        }
87    }
88
89    /// <https://w3c.github.io/FileAPI/#unicodeBlobURL>
90    fn unicode_serialization_blob_url(origin: &ImmutableOrigin, id: &Uuid) -> String {
91        // Step 1. Let result be the empty string.
92        // Step 2. Append the string "blob:" to result.
93        let mut result = "blob:".to_string();
94
95        // Step 3. Let settings be the current settings object.
96        // Step 4. Let origin be settings’s origin.
97        // Step 5. Let serialized be the ASCII serialization of origin.
98        // Step 6. If serialized is "null", set it to an implementation-defined value.
99        // Step 7. Append serialized to result.
100        // N.B. We leave it as "null" right now.
101        result.push_str(&origin.ascii_serialization());
102
103        // Step 8. Append U+0024 SOLIDUS (/) to result.
104        result.push('/');
105
106        // Step 9. Generate a UUID [RFC4122] as a string and append it to result.
107        result.push_str(&id.to_string());
108
109        // Step 10. Return result.
110        result
111    }
112}
113
114impl URLMethods<crate::DomTypeHolder> for URL {
115    /// <https://url.spec.whatwg.org/#constructors>
116    fn Constructor(
117        global: &GlobalScope,
118        proto: Option<HandleObject>,
119        can_gc: CanGc,
120        url: USVString,
121        base: Option<USVString>,
122    ) -> Fallible<DomRoot<URL>> {
123        // Step 1. Parse url with base.
124        let parsed_base = match base {
125            None => None,
126            Some(base) => {
127                match ServoUrl::parse(&base.0) {
128                    Ok(base) => Some(base),
129                    Err(error) => {
130                        // Step 2. Throw a TypeError if URL parsing fails.
131                        return Err(Error::Type(cformat!("could not parse base: {}", error)));
132                    },
133                }
134            },
135        };
136        let parsed_url = match ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0) {
137            Ok(url) => url,
138            Err(error) => {
139                // Step 2. Throw a TypeError if URL parsing fails.
140                return Err(Error::Type(cformat!("could not parse URL: {}", error)));
141            },
142        };
143
144        // Skip the steps below.
145        // Instead of constructing a new `URLSearchParams` object here, construct it
146        // on-demand inside `URL::SearchParams`.
147        //
148        // Step 3. Let query be parsedURL’s query.
149        // Step 5. Set this’s query object to a new URLSearchParams object.
150        // Step 6. Initialize this’s query object with query.
151        // Step 7. Set this’s query object’s URL object to this.
152
153        // Step 4. Set this’s URL to parsedURL.
154        Ok(URL::new(global, proto, parsed_url, can_gc))
155    }
156
157    /// <https://url.spec.whatwg.org/#dom-url-canparse>
158    fn CanParse(_global: &GlobalScope, url: USVString, base: Option<USVString>) -> bool {
159        // Step 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
160        let parsed_url = run_api_url_parser(url, base);
161
162        // Step 2. If parsedURL is failure, then return false.
163        // Step 3. Return true.
164        parsed_url.is_ok()
165    }
166
167    /// <https://url.spec.whatwg.org/#dom-url-parse>
168    fn Parse(
169        global: &GlobalScope,
170        url: USVString,
171        base: Option<USVString>,
172        can_gc: CanGc,
173    ) -> Option<DomRoot<URL>> {
174        // Step 1: Let parsedURL be the result of running the API URL parser on url with base,
175        // if given.
176        // Step 2: If parsedURL is failure, then return null.
177        let parsed_url = run_api_url_parser(url, base).ok()?;
178
179        // Step 3: Let url be a new URL object.
180        // Step 4: Initialize url with parsedURL.
181        // Step 5: Return url.
182        // Regarding initialization, the same condition should apply here as stated in the comments
183        // in Self::Constructor above - construct it on-demand inside `URL::SearchParams`.
184        Some(URL::new(
185            global,
186            None,
187            ServoUrl::from_url(parsed_url),
188            can_gc,
189        ))
190    }
191
192    /// <https://w3c.github.io/FileAPI/#dfn-createObjectURL>
193    fn CreateObjectURL(global: &GlobalScope, blob: &Blob) -> DOMString {
194        // XXX: Second field is an unicode-serialized Origin, it is a temporary workaround
195        //      and should not be trusted. See issue https://github.com/servo/servo/issues/11722
196        let origin = global.origin().immutable();
197
198        let id = blob.get_blob_url_id();
199
200        DOMString::from(URL::unicode_serialization_blob_url(origin, &id))
201    }
202
203    /// <https://w3c.github.io/FileAPI/#dfn-revokeObjectURL>
204    fn RevokeObjectURL(global: &GlobalScope, url: DOMString) {
205        // If the value provided for the url argument is not a Blob URL OR
206        // if the value provided for the url argument does not have an entry in the Blob URL Store,
207        // this method call does nothing. User agents may display a message on the error console.
208        let origin = global.origin().immutable();
209
210        if let Ok(url) = ServoUrl::parse(&url.str()) {
211            if url.fragment().is_none() && *origin == url.origin() {
212                if let Ok((id, _)) = parse_blob_url(&url) {
213                    let resource_threads = global.resource_threads();
214                    let (tx, rx) =
215                        generic_channel::channel(global.time_profiler_chan().clone()).unwrap();
216                    let msg = FileManagerThreadMsg::RevokeBlobURL(id, origin.clone(), tx);
217                    let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));
218
219                    let _ = rx.recv().unwrap();
220                }
221            }
222        }
223    }
224
225    /// <https://url.spec.whatwg.org/#dom-url-hash>
226    fn Hash(&self) -> USVString {
227        UrlHelper::Hash(&self.url.borrow())
228    }
229
230    /// <https://url.spec.whatwg.org/#dom-url-hash>
231    fn SetHash(&self, value: USVString) {
232        UrlHelper::SetHash(&mut self.url.borrow_mut(), value);
233    }
234
235    /// <https://url.spec.whatwg.org/#dom-url-host>
236    fn Host(&self) -> USVString {
237        UrlHelper::Host(&self.url.borrow())
238    }
239
240    /// <https://url.spec.whatwg.org/#dom-url-host>
241    fn SetHost(&self, value: USVString) {
242        UrlHelper::SetHost(&mut self.url.borrow_mut(), value);
243    }
244
245    /// <https://url.spec.whatwg.org/#dom-url-hostname>
246    fn Hostname(&self) -> USVString {
247        UrlHelper::Hostname(&self.url.borrow())
248    }
249
250    /// <https://url.spec.whatwg.org/#dom-url-hostname>
251    fn SetHostname(&self, value: USVString) {
252        UrlHelper::SetHostname(&mut self.url.borrow_mut(), value);
253    }
254
255    /// <https://url.spec.whatwg.org/#dom-url-href>
256    fn Href(&self) -> USVString {
257        UrlHelper::Href(&self.url.borrow())
258    }
259
260    /// <https://url.spec.whatwg.org/#dom-url-href>
261    fn SetHref(&self, value: USVString) -> ErrorResult {
262        match ServoUrl::parse(&value.0) {
263            Ok(url) => {
264                *self.url.borrow_mut() = url;
265                self.search_params.set(None); // To be re-initialized in the SearchParams getter.
266                Ok(())
267            },
268            Err(error) => Err(Error::Type(cformat!("could not parse URL: {}", error))),
269        }
270    }
271
272    /// <https://url.spec.whatwg.org/#dom-url-password>
273    fn Password(&self) -> USVString {
274        UrlHelper::Password(&self.url.borrow())
275    }
276
277    /// <https://url.spec.whatwg.org/#dom-url-password>
278    fn SetPassword(&self, value: USVString) {
279        UrlHelper::SetPassword(&mut self.url.borrow_mut(), value);
280    }
281
282    /// <https://url.spec.whatwg.org/#dom-url-pathname>
283    fn Pathname(&self) -> USVString {
284        UrlHelper::Pathname(&self.url.borrow())
285    }
286
287    /// <https://url.spec.whatwg.org/#dom-url-pathname>
288    fn SetPathname(&self, value: USVString) {
289        UrlHelper::SetPathname(&mut self.url.borrow_mut(), value);
290    }
291
292    /// <https://url.spec.whatwg.org/#dom-url-port>
293    fn Port(&self) -> USVString {
294        UrlHelper::Port(&self.url.borrow())
295    }
296
297    /// <https://url.spec.whatwg.org/#dom-url-port>
298    fn SetPort(&self, value: USVString) {
299        UrlHelper::SetPort(&mut self.url.borrow_mut(), value);
300    }
301
302    /// <https://url.spec.whatwg.org/#dom-url-protocol>
303    fn Protocol(&self) -> USVString {
304        UrlHelper::Protocol(&self.url.borrow())
305    }
306
307    /// <https://url.spec.whatwg.org/#dom-url-protocol>
308    fn SetProtocol(&self, value: USVString) {
309        UrlHelper::SetProtocol(&mut self.url.borrow_mut(), value);
310    }
311
312    /// <https://url.spec.whatwg.org/#dom-url-origin>
313    fn Origin(&self) -> USVString {
314        UrlHelper::Origin(&self.url.borrow())
315    }
316
317    /// <https://url.spec.whatwg.org/#dom-url-search>
318    fn Search(&self) -> USVString {
319        UrlHelper::Search(&self.url.borrow())
320    }
321
322    /// <https://url.spec.whatwg.org/#dom-url-search>
323    fn SetSearch(&self, value: USVString) {
324        UrlHelper::SetSearch(&mut self.url.borrow_mut(), value);
325        if let Some(search_params) = self.search_params.get() {
326            search_params.set_list(self.query_pairs());
327        }
328    }
329
330    /// <https://url.spec.whatwg.org/#dom-url-searchparams>
331    fn SearchParams(&self, can_gc: CanGc) -> DomRoot<URLSearchParams> {
332        self.search_params
333            .or_init(|| URLSearchParams::new(&self.global(), Some(self), can_gc))
334    }
335
336    /// <https://url.spec.whatwg.org/#dom-url-username>
337    fn Username(&self) -> USVString {
338        UrlHelper::Username(&self.url.borrow())
339    }
340
341    /// <https://url.spec.whatwg.org/#dom-url-username>
342    fn SetUsername(&self, value: USVString) {
343        UrlHelper::SetUsername(&mut self.url.borrow_mut(), value);
344    }
345
346    /// <https://url.spec.whatwg.org/#dom-url-tojson>
347    fn ToJSON(&self) -> USVString {
348        self.Href()
349    }
350}
351
352/// <https://url.spec.whatwg.org/#api-url-parser>
353fn run_api_url_parser(url: USVString, base: Option<USVString>) -> Result<Url, url::ParseError> {
354    // Step 1. Let parsedBase be null.
355    // Step 2. If base is non-null:
356    // Step 2.1 Set parsedBase to the result of running the basic URL parser on base.
357    // Step 2.2 If parsedBase is failure, then return failure.
358    let parsed_base = base.map(|base| Url::parse(&base.0)).transpose()?;
359
360    // Step 3. Return the result of running the basic URL parser on url with parsedBase.
361    Url::options().base_url(parsed_base.as_ref()).parse(&url.0)
362}