net/protocols/
data.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 data_url::DataUrl;
9use headers::HeaderValue;
10use net_traits::http_status::HttpStatus;
11use net_traits::request::Request;
12use net_traits::response::{Response, ResponseBody};
13use net_traits::{NetworkError, ResourceFetchTiming};
14
15use crate::fetch::methods::{DoneChannel, FetchContext};
16use crate::protocols::ProtocolHandler;
17
18#[derive(Default)]
19pub struct DataProtocolHander {}
20
21impl ProtocolHandler for DataProtocolHander {
22    fn load(
23        &self,
24        request: &mut Request,
25        _done_chan: &mut DoneChannel,
26        _context: &FetchContext,
27    ) -> Pin<Box<dyn Future<Output = Response> + Send>> {
28        let url = request.current_url();
29
30        assert_eq!(url.scheme(), "data");
31
32        let response = match DataUrl::process(url.clone().as_str()) {
33            Ok(data_url) => match data_url.decode_to_vec() {
34                Ok((bytes, _fragment_id)) => {
35                    let mut response =
36                        Response::new(url, ResourceFetchTiming::new(request.timing_type()));
37                    *response.body.lock().unwrap() = ResponseBody::Done(bytes);
38                    let mime = data_url.mime_type();
39                    response.headers.insert(
40                        http::header::CONTENT_TYPE,
41                        HeaderValue::from_str(&mime.to_string()).unwrap(),
42                    );
43                    response.status = HttpStatus::default();
44                    Some(response)
45                },
46                Err(_) => None,
47            },
48            Err(_) => None,
49        }
50        .unwrap_or_else(|| {
51            Response::network_error(NetworkError::Internal("Decoding data URL failed".into()))
52        });
53
54        Box::pin(std::future::ready(response))
55    }
56
57    fn is_fetchable(&self) -> bool {
58        true
59    }
60}