net_traits/
blob_url_store.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::str::FromStr;
6
7use serde::{Deserialize, Serialize};
8use servo_url::{ImmutableOrigin, ServoUrl};
9use url::Url;
10use uuid::Uuid;
11
12/// Errors returned to Blob URL Store request
13#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
14pub enum BlobURLStoreError {
15    /// Invalid File UUID
16    InvalidFileID,
17    /// Invalid URL origin
18    InvalidOrigin,
19    /// Invalid entry content
20    InvalidEntry,
21    /// Invalid range
22    InvalidRange,
23    /// External error, from like file system, I/O etc.
24    External(String),
25}
26
27/// Standalone blob buffer object
28#[derive(Clone, Debug, Deserialize, Serialize)]
29pub struct BlobBuf {
30    pub filename: Option<String>,
31    /// MIME type string
32    pub type_string: String,
33    /// Size of content in bytes
34    pub size: u64,
35    /// Content of blob
36    pub bytes: Vec<u8>,
37}
38
39/// Parse URL as Blob URL scheme's definition
40///
41/// <https://w3c.github.io/FileAPI/#DefinitionOfScheme>
42pub fn parse_blob_url(url: &ServoUrl) -> Result<(Uuid, ImmutableOrigin), &'static str> {
43    let url_inner = Url::parse(url.path()).map_err(|_| "Failed to parse URL path")?;
44    let segs = url_inner
45        .path_segments()
46        .map(|c| c.collect::<Vec<_>>())
47        .ok_or("URL has no path segments")?;
48
49    if url.query().is_some() {
50        return Err("URL should not contain a query");
51    }
52
53    if segs.len() > 1 {
54        return Err("URL should not have more than one path segment");
55    }
56
57    let id = {
58        let id = segs.first().ok_or("URL has no path segments")?;
59        Uuid::from_str(id).map_err(|_| "Failed to parse UUID from path segment")?
60    };
61    Ok((id, ServoUrl::from_url(url_inner).origin()))
62}