Skip to main content

libfw_core/
constants.rs

1//! Protocol constants shared between server and client.
2
3/// Fixed chunk size for fragmented transfers (2 MiB).
4pub const CHUNK_SIZE: u64 = 2 * 1024 * 1024;
5
6/// Constant sliding-window size for streaming (64 KiB).
7pub const STREAM_BUF_SIZE: usize = 64 * 1024;
8
9/// Maximum default concurrent connections in the WASM engine.
10pub const DEFAULT_CONCURRENCY: usize = 4;
11
12/// Default maximum retry count for a failed chunk before failing the task.
13pub const MAX_RETRIES: u32 = 3;
14
15/// Default cap for a single upload (100 GiB, configurable by the server).
16pub const DEFAULT_MAX_UPLOAD_SIZE: u64 = 100 * 1024 * 1024 * 1024;
17
18/// HTTP header advertising the compression algorithm on a body stream.
19///
20/// Value is a [`CompressionFormat`](crate::compress::CompressionFormat)
21/// name (e.g. `zrip`). Mirrored by `Content-Encoding` when applicable.
22pub const HEADER_COMPRESS: &str = "x-libfw-compress";
23
24/// HTTP header carrying JSON-encoded [`FileMeta`](crate::metadata::FileMeta)
25/// for an upload or download.
26pub const HEADER_FILE_META: &str = "x-libfw-file-meta";
27
28/// HTTP header carrying the byte offset to resume an interrupted upload.
29pub const HEADER_OFFSET: &str = "x-libfw-offset";
30
31/// HTTP header marking an upload request as the FINAL chunk of a file.
32///
33/// When present (value `1`/`true`), the server verifies that the resulting
34/// file size equals the declared `x-libfw-file-meta` size before committing,
35/// so a client cannot commit a truncated file. Absent (older clients) skips
36/// the check — the header is optional and backward compatible.
37pub const HEADER_FINAL: &str = "x-libfw-final";
38
39/// HTTP header carrying the (optional) transfer version handshake.
40pub const HEADER_PROTOCOL: &str = "x-libfw-protocol";
41
42/// Protocol name used in the handshake header.
43pub const PROTOCOL_NAME: &str = "libfw";
44
45/// Protocol version used in the handshake header.
46pub const PROTOCOL_VERSION: &str = "1";
47
48/// The canonical `x-libfw-protocol` header value (e.g. `libfw/1`).
49///
50/// Both the server and the WASM client must agree on this exact value; the
51/// server rejects (426) requests that explicitly advertise a different one.
52/// Keep in sync with [`PROTOCOL_NAME`] / [`PROTOCOL_VERSION`].
53pub fn protocol_header_value() -> &'static str {
54    "libfw/1"
55}
56
57/// Whether a received `x-libfw-protocol` handshake value is compatible with
58/// this build of the library.
59pub fn protocol_compatible(value: &str) -> bool {
60    value.trim().eq_ignore_ascii_case(protocol_header_value())
61}