diff options
author | Martin Robinson <mrobinson@igalia.com> | 2023-10-05 19:47:39 +0200 |
---|---|---|
committer | Martin Robinson <mrobinson@igalia.com> | 2023-11-03 15:38:18 +0000 |
commit | f4d3af296c05260dfbb3deea4f8fa400cb6887d3 (patch) | |
tree | 169db2cc68e01a755b30500dd525f1a2ec2da861 /components/shared/net/blob_url_store.rs | |
parent | 863529d9622c68f0a9535225237eb5e5c5b8c757 (diff) | |
download | servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.tar.gz servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.zip |
Move `*_traits` and other shared types to `shared`
This is the start of the organization of types that are in their own
crates in order to break dependency cycles between other crates. The
idea here is that putting these packages into their own directory is the
first step toward cleaning them up. They have grown organically and it
is difficult to explain to new folks where to put new shared types. Many
of these crates contain more than traits or don't contain traits at all.
Notably, `script_traits` isn't touched because it is vendored from
Gecko. Eventually this will move to `third_party`.
Diffstat (limited to 'components/shared/net/blob_url_store.rs')
-rw-r--r-- | components/shared/net/blob_url_store.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/components/shared/net/blob_url_store.rs b/components/shared/net/blob_url_store.rs new file mode 100644 index 00000000000..ab853b702c9 --- /dev/null +++ b/components/shared/net/blob_url_store.rs @@ -0,0 +1,75 @@ +/* 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::str::FromStr; + +use serde::{Deserialize, Serialize}; +use servo_url::ServoUrl; +use url::Url; +use uuid::Uuid; + +use crate::filemanager_thread::FileOrigin; + +/// Errors returned to Blob URL Store request +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +pub enum BlobURLStoreError { + /// Invalid File UUID + InvalidFileID, + /// Invalid URL origin + InvalidOrigin, + /// Invalid entry content + InvalidEntry, + /// Invalid range + InvalidRange, + /// External error, from like file system, I/O etc. + External(String), +} + +/// Standalone blob buffer object +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct BlobBuf { + pub filename: Option<String>, + /// MIME type string + pub type_string: String, + /// Size of content in bytes + pub size: u64, + /// Content of blob + pub bytes: Vec<u8>, +} + +/// Parse URL as Blob URL scheme's definition +/// +/// <https://w3c.github.io/FileAPI/#DefinitionOfScheme> +pub fn parse_blob_url(url: &ServoUrl) -> Result<(Uuid, FileOrigin), ()> { + let url_inner = Url::parse(url.path()).map_err(|_| ())?; + let segs = url_inner + .path_segments() + .map(|c| c.collect::<Vec<_>>()) + .ok_or(())?; + + if url.query().is_some() || segs.len() > 1 { + return Err(()); + } + + let id = { + let id = segs.first().ok_or(())?; + Uuid::from_str(id).map_err(|_| ())? + }; + Ok((id, get_blob_origin(&ServoUrl::from_url(url_inner)))) +} + +/// Given an URL, returning the Origin that a Blob created under this +/// URL should have. +/// +/// HACK(izgzhen): Not well-specified on spec, and it is a bit a hack +/// both due to ambiguity of spec and that we have to serialization the +/// Origin here. +pub fn get_blob_origin(url: &ServoUrl) -> FileOrigin { + if url.scheme() == "file" { + // NOTE: by default this is "null" (Opaque), which is not ideal + "file://".to_string() + } else { + url.origin().ascii_serialization() + } +} |