servo/ports/servoshell/desktop/protocols/urlinfo.rs
Tony 894fbd003d
Initial support for marking custom protocol secure (#36656)
Add initial support for marking custom protocol as secure, this makes it
possible to `fetch` a custom protocol inside secure contexts (e.g.
http://localhost)

Some additional contexts:

- [#embedding > Custom protocol secure
context](https://servo.zulipchat.com/#narrow/channel/437943-embedding/topic/Custom.20protocol.20secure.20context)
-
https://github.com/versotile-org/tauri-runtime-verso/issues/6#issuecomment-2820776128

Testing: use `fetch('urlinfo://abc').then(async (response) =>
console.log(await response.text())).catch(console.log)` in servoshell
with in a secure context (e.g. https://servo.org), and see the response
should not be an error

---------

Signed-off-by: Tony <legendmastertony@gmail.com>
Signed-off-by: Tony <68118705+Legend-Master@users.noreply.github.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2025-04-25 10:39:33 +00:00

53 lines
1.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::future::Future;
use std::pin::Pin;
use headers::{ContentType, HeaderMapExt};
use net::fetch::methods::{DoneChannel, FetchContext};
use net::protocols::ProtocolHandler;
use net_traits::ResourceFetchTiming;
use net_traits::http_status::HttpStatus;
use net_traits::request::Request;
use net_traits::response::{Response, ResponseBody};
#[derive(Default)]
pub struct UrlInfoProtocolHander {}
// A simple protocol handler that displays information about the url itself.
impl ProtocolHandler for UrlInfoProtocolHander {
fn load(
&self,
request: &mut Request,
_done_chan: &mut DoneChannel,
_context: &FetchContext,
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
let url = request.current_url();
let content = format!(
r#"Full url: {url}
scheme: {}
path: {}
query: {:?}"#,
url.scheme(),
url.path(),
url.query()
);
let mut response = Response::new(url, ResourceFetchTiming::new(request.timing_type()));
*response.body.lock().unwrap() = ResponseBody::Done(content.as_bytes().to_vec());
response.headers.typed_insert(ContentType::text());
response.status = HttpStatus::default();
Box::pin(std::future::ready(response))
}
fn is_fetchable(&self) -> bool {
true
}
fn is_secure(&self) -> bool {
true
}
}