retina_fetch/
lib.rs

1// Copyright (C) 2023 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4//! This API is modeled after the [Fetch API][spec]
5//!
6//! # References
7//! * [Fetch API - WHATWG Specification][spec]
8//! * [Fetch API - MDN][mdn]
9//!
10//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
11//! [spec]: https://fetch.spec.whatwg.org
12
13#![deny(missing_docs)]
14
15pub(crate) mod destination;
16pub(crate) mod error;
17pub(crate) mod initiator;
18pub(crate) mod fetch;
19pub(crate) mod mode;
20pub(crate) mod promise;
21pub(crate) mod referrer;
22pub(crate) mod request;
23pub(crate) mod response;
24pub(crate) mod status_code;
25
26pub use destination::RequestDestination;
27pub use error::{Error, InternalError, NetworkError};
28pub use fetch::Fetch;
29pub use initiator::RequestInitiator;
30pub use mode::RequestMode;
31pub use promise::FetchPromise;
32pub use referrer::RequestReferrer;
33pub use request::Request;
34pub use response::Response;
35pub use status_code::{StatusCode, StatusCodeClass};
36
37pub use mime;
38pub use url::{
39    self,
40    Url,
41};
42
43/// This type specifies what the [`Fetch::fetch()`][Fetch] API returns.
44pub type FetchResponse = Result<Response, Error>;
45
46/// Parse a page URL. This function is laxer than the regular
47/// [`Url::parse()`][Url] parser, since the user can omit certain elements
48/// (like the scheme).
49pub fn parse_page_url(input: &str) -> Result<Url, url::ParseError> {
50    if input.len() > 3 && &input[1..3] == ":\\" {
51        let url = format!("file:///{}", input.replace('\\', "/"));
52        return Url::parse(&url);
53    }
54
55    let result = Url::parse(input);
56
57    if result == Err(url::ParseError::RelativeUrlWithoutBase) && !input.starts_with("http") {
58        if let Ok(url) = Url::parse(&format!("https://{input}")) {
59            return Ok(url);
60        }
61    }
62
63    result
64}