Skip to main content

supreme_fs/game_storage/online/
fetch.rs

1//! The generic HTTP-GET/POST primitives live in the
2//! dedicated [`supreme_net`] crate (the hook proxy DLL links ONLY
3//! that tiny crate, so the DLL binary carries no storage vocabulary).
4//! supreme_fs keeps these wrappers for its own public API and
5//! tests: same names, same semantics, mapped onto the storage error type.
6
7use crate::HybridStorageError;
8use std::io;
9use supreme_net::{HttpError, http_get_bytes_opt as net_get_opt, http_post_bytes as net_post};
10
11fn map_err(url: &str, e: HttpError) -> HybridStorageError {
12    match e {
13        HttpError::Io(msg) => HybridStorageError::IoError(io::Error::other(msg)),
14        HttpError::Status { status, body, .. } => {
15            HybridStorageError::UploadFailed(format!("{url}: HTTP {status}: {body}"))
16        }
17    }
18}
19
20/// Plain HTTP GET of an absolute URL: returns the response body bytes
21/// verbatim. Non-2xx responses are errors carrying the status and the
22/// (lossily decoded) body, that keeps a backend error page from ever being
23/// stored as file content.
24///
25/// # Errors
26///
27/// Transport failures and non-2xx statuses both map to
28/// [`HybridStorageError::IoError`] / [`HybridStorageError::UploadFailed`].
29pub fn http_get_bytes(url: &str) -> Result<Vec<u8>, HybridStorageError> {
30    net_get_opt(url)
31        .map_err(|e| map_err(url, e))?
32        .ok_or_else(|| HybridStorageError::UploadFailed(format!("GET {url}: HTTP 404")))
33}
34
35/// Like [`http_get_bytes`], but a `404` answer is `Ok(None)` instead of an
36/// error: the managed-file engine's mirror semantics treat 404 as
37/// "authoritative absence" (the remote file is GONE, remove the local
38/// copy) while every other non-2xx stays an error (keep the local file,
39/// retry on a later cycle)..
40///
41/// # Errors
42///
43/// Transport failures and non-404 non-2xx statuses map to
44/// [`HybridStorageError::IoError`] / [`HybridStorageError::UploadFailed`].
45pub fn http_get_bytes_opt(url: &str) -> Result<Option<Vec<u8>>, HybridStorageError> {
46    net_get_opt(url).map_err(|e| map_err(url, e))
47}
48
49/// Plain HTTP POST of `body` to an absolute URL (the managed-file engine's
50/// write mirror: when the GAME writes a managed file, the engine posts the
51/// new bytes to the file's own `remote` URL). The response body is
52/// discarded; any non-2xx is an error.
53///
54/// # Errors
55///
56/// Transport failures and non-2xx statuses map to
57/// [`HybridStorageError::IoError`] / [`HybridStorageError::UploadFailed`].
58pub fn http_post_bytes(url: &str, body: &[u8]) -> Result<(), HybridStorageError> {
59    net_post(url, body).map_err(|e| map_err(url, e))
60}