mirror of
https://github.com/servo/servo
synced 2026-05-09 08:32:31 +02:00
Replace NetworkError::Internal with structured enum variants - Adds UnsupportedScheme,CorsViolation,ConnectionFailure,Timeout,RedirectError,InvalidMethod,ResourceError,SecurityBlock,MixedContent,CacheError,InvalidPort, LocalDirectoryError, variants in NetworkError enum. - Refactored the usage of NetworkError::Internal(String) to use the appropriate new variant Testing: Changes does not require test. Fixes: https://github.com/servo/servo/issues/36434 --------- Signed-off-by: Uthman Yahaya Baba <uthmanyahayababa@gmail.com> Signed-off-by: Usman Yahaya Baba <91813795+uthmaniv@users.noreply.github.com> Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com> Co-authored-by: Tim van der Lippe <tvanderlippe@gmail.com>
63 lines
2.0 KiB
Rust
63 lines
2.0 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 data_url::DataUrl;
|
|
use headers::HeaderValue;
|
|
use net_traits::http_status::HttpStatus;
|
|
use net_traits::request::Request;
|
|
use net_traits::response::{Response, ResponseBody};
|
|
use net_traits::{NetworkError, ResourceFetchTiming};
|
|
|
|
use crate::fetch::methods::{DoneChannel, FetchContext};
|
|
use crate::protocols::ProtocolHandler;
|
|
|
|
#[derive(Default)]
|
|
pub struct DataProtocolHander {}
|
|
|
|
impl ProtocolHandler for DataProtocolHander {
|
|
fn load(
|
|
&self,
|
|
request: &mut Request,
|
|
_done_chan: &mut DoneChannel,
|
|
_context: &FetchContext,
|
|
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
|
|
let url = request.current_url();
|
|
|
|
assert_eq!(url.scheme(), "data");
|
|
|
|
let response = match DataUrl::process(url.clone().as_str()) {
|
|
Ok(data_url) => match data_url.decode_to_vec() {
|
|
Ok((bytes, _fragment_id)) => {
|
|
let mut response =
|
|
Response::new(url, ResourceFetchTiming::new(request.timing_type()));
|
|
*response.body.lock() = ResponseBody::Done(bytes);
|
|
let mime = data_url.mime_type();
|
|
response.headers.insert(
|
|
http::header::CONTENT_TYPE,
|
|
HeaderValue::from_str(&mime.to_string()).unwrap(),
|
|
);
|
|
response.status = HttpStatus::default();
|
|
Some(response)
|
|
},
|
|
Err(_) => None,
|
|
},
|
|
Err(_) => None,
|
|
}
|
|
.unwrap_or_else(|| {
|
|
Response::network_error(NetworkError::ResourceLoadError(
|
|
"Decoding data URL failed".into(),
|
|
))
|
|
});
|
|
|
|
Box::pin(std::future::ready(response))
|
|
}
|
|
|
|
fn is_fetchable(&self) -> bool {
|
|
true
|
|
}
|
|
}
|