Skip to main content

tork_core/
constants.rs

1//! Named constants used across the runtime.
2//!
3//! Centralizing these values avoids magic literals scattered through the code
4//! and keeps content types, limits, and user-facing messages consistent.
5
6use std::time::Duration;
7
8/// `Content-Type` value for JSON payloads.
9pub const APPLICATION_JSON: &str = "application/json";
10
11/// `Content-Type` value for UTF-8 plain text.
12pub const TEXT_PLAIN_UTF8: &str = "text/plain; charset=utf-8";
13
14/// `Content-Type` value for UTF-8 HTML documents.
15pub const TEXT_HTML_UTF8: &str = "text/html; charset=utf-8";
16
17/// `Content-Type` value for UTF-8 JavaScript sources.
18pub const APPLICATION_JAVASCRIPT_UTF8: &str = "application/javascript; charset=utf-8";
19
20/// `Content-Type` value for a Server-Sent Events stream.
21pub const TEXT_EVENT_STREAM: &str = "text/event-stream";
22
23/// Prefix of an `Authorization` header value that carries a bearer token.
24pub const BEARER_PREFIX: &str = "Bearer ";
25
26/// Generic message returned to clients for any server-side (5xx) error.
27///
28/// Server errors never expose internal detail in the response body; only this
29/// fixed message is sent while the real cause is logged server-side.
30pub const INTERNAL_ERROR_MESSAGE: &str = "Internal server error";
31
32/// Maximum number of bytes the framework buffers from a single request body.
33///
34/// Requests whose body exceeds this limit are rejected, guarding against
35/// memory-exhaustion attacks.
36pub const MAX_BODY_BYTES: usize = 2 * 1024 * 1024;
37
38/// Maximum time to wait for in-flight connections to drain during shutdown.
39pub const GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(15);
40
41/// Default deadline for a client to send the complete request head (the request
42/// line and all headers) after a connection is accepted.
43///
44/// Bounds slowloris-style attacks where a client opens a connection and dribbles
45/// header bytes to tie up a worker indefinitely. Configurable via
46/// [`App::header_read_timeout`](crate::App::header_read_timeout).
47pub const DEFAULT_HEADER_READ_TIMEOUT: Duration = Duration::from_secs(30);
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn exported_constants_match_runtime_contract() {
55        assert_eq!(APPLICATION_JSON, "application/json");
56        assert_eq!(TEXT_EVENT_STREAM, "text/event-stream");
57        assert_eq!(BEARER_PREFIX, "Bearer ");
58        assert_eq!(INTERNAL_ERROR_MESSAGE, "Internal server error");
59        assert_eq!(MAX_BODY_BYTES, 2 * 1024 * 1024);
60        assert_eq!(GRACEFUL_SHUTDOWN_TIMEOUT, Duration::from_secs(15));
61    }
62}