Skip to main content

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