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