wasmer_wasix/runtime/resolver/
utils.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::Error;
4use http::{HeaderMap, StatusCode};
5use url::Url;
6
7use crate::http::{HttpResponse, USER_AGENT};
8
9/// Polyfill for [`Url::from_file_path()`] that works on `wasm32-unknown-unknown`.
10pub(crate) fn url_from_file_path(path: impl AsRef<Path>) -> Option<Url> {
11    let path = path.as_ref();
12
13    if !path.is_absolute() {
14        return None;
15    }
16
17    let mut buffer = String::new();
18
19    for component in path {
20        if !buffer.ends_with('/') {
21            buffer.push('/');
22        }
23
24        buffer.push_str(component.to_str()?);
25    }
26
27    buffer.insert_str(0, "file://");
28
29    buffer.parse().ok()
30}
31
32pub(crate) fn webc_headers() -> HeaderMap {
33    let mut headers = HeaderMap::new();
34    headers.insert("Accept", "application/webc".parse().unwrap());
35    headers.insert("User-Agent", USER_AGENT.parse().unwrap());
36    headers
37}
38
39pub(crate) fn http_error(response: &HttpResponse) -> Error {
40    let status = response.status;
41
42    if status == StatusCode::SERVICE_UNAVAILABLE
43        && let Some(retry_after) = response
44            .headers
45            .get("Retry-After")
46            .and_then(|retry_after| retry_after.to_str().ok())
47    {
48        tracing::debug!(
49            %retry_after,
50            "Received 503 Service Unavailable while looking up a package. The backend may still be generating the *.webc file.",
51        );
52        return anyhow::anyhow!("{status} (Retry After: {retry_after})");
53    }
54
55    Error::msg(status)
56}
57
58pub(crate) fn file_path_from_url(url: &Url) -> Result<PathBuf, Error> {
59    debug_assert_eq!(url.scheme(), "file");
60
61    // Note: The Url::to_file_path() method is platform-specific
62    cfg_if::cfg_if! {
63        if #[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] {
64            use anyhow::Context;
65
66            if let Ok(path) = url.to_file_path() {
67                return Ok(path);
68            }
69
70            // Sometimes we'll get a UNC-like path (e.g.
71            // "file:///?\\C:/\\/path/to/file.txt") and Url::to_file_path()
72            // won't be able to handle the "\\?" so we try to "massage" the URL
73            // a bit.
74            // See <https://github.com/servo/rust-url/issues/450> for more.
75            let modified = url.as_str().replace(r"\\?", "").replace("//?", "").replace('\\', "/");
76            Url::parse(&modified)
77                .ok()
78                .and_then(|url| url.to_file_path().ok())
79                .context("Unable to extract the file path")
80        } else {
81            anyhow::bail!("Url::to_file_path() is not supported on this platform");
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    #[allow(unused_imports)]
89    use super::*;
90
91    #[test]
92    #[cfg(unix)]
93    fn from_file_path_behaviour_is_identical() {
94        let inputs = [
95            "/",
96            "/path",
97            "/path/to/file.txt",
98            "./path/to/file.txt",
99            ".",
100            "",
101        ];
102
103        for path in inputs {
104            let got = url_from_file_path(path);
105            let expected = Url::from_file_path(path).ok();
106            assert_eq!(got, expected, "Mismatch for \"{path}\"");
107        }
108    }
109
110    #[test]
111    #[cfg(windows)]
112    fn to_file_path_can_handle_unc_paths() {
113        let path = Path::new(env!("CARGO_MANIFEST_DIR"))
114            .canonicalize()
115            .unwrap();
116        let url = Url::from_file_path(&path).unwrap();
117
118        let got = file_path_from_url(&url).unwrap();
119
120        assert_eq!(got.canonicalize().unwrap(), path);
121    }
122}