servoshell/desktop/protocols/
urlinfo.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::future::Future;
6use std::pin::Pin;
7
8use headers::{ContentType, HeaderMapExt};
9use net::fetch::methods::{DoneChannel, FetchContext};
10use net::protocols::ProtocolHandler;
11use net_traits::ResourceFetchTiming;
12use net_traits::http_status::HttpStatus;
13use net_traits::request::Request;
14use net_traits::response::{Response, ResponseBody};
15
16#[derive(Default)]
17pub struct UrlInfoProtocolHander {}
18
19// A simple protocol handler that displays information about the url itself.
20impl ProtocolHandler for UrlInfoProtocolHander {
21    fn load(
22        &self,
23        request: &mut Request,
24        _done_chan: &mut DoneChannel,
25        _context: &FetchContext,
26    ) -> Pin<Box<dyn Future<Output = Response> + Send>> {
27        let url = request.current_url();
28
29        let content = format!(
30            r#"Full url: {url}
31  scheme: {}
32    path: {}
33   query: {:?}"#,
34            url.scheme(),
35            url.path(),
36            url.query()
37        );
38        let mut response = Response::new(url, ResourceFetchTiming::new(request.timing_type()));
39        *response.body.lock().unwrap() = ResponseBody::Done(content.as_bytes().to_vec());
40        response.headers.typed_insert(ContentType::text());
41        response.status = HttpStatus::default();
42
43        Box::pin(std::future::ready(response))
44    }
45
46    fn is_fetchable(&self) -> bool {
47        true
48    }
49
50    fn is_secure(&self) -> bool {
51        true
52    }
53}