1pub const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
16
17pub const MAX_BODY_BYTES: usize = 5 * 1024 * 1024;
26
27pub const ERROR_BODY_CAP: usize = 64 * 1024;
30
31#[cfg_attr(not(any(feature = "native", feature = "wasi")), allow(dead_code))]
34pub(crate) fn push_capped(buf: &mut Vec<u8>, chunk: &[u8], max: usize) -> bool {
35 let remaining = max.saturating_sub(buf.len());
36 if chunk.len() >= remaining {
37 buf.extend_from_slice(&chunk[..remaining]);
38 true
39 } else {
40 buf.extend_from_slice(chunk);
41 false
42 }
43}
44
45pub fn transient_status(status: u16) -> bool {
47 (500..600).contains(&status) || status == 429
48}
49
50pub fn status_text(status: u16) -> String {
57 #[cfg(feature = "native")]
58 let reason = reqwest::StatusCode::from_u16(status)
59 .ok()
60 .and_then(|code| code.canonical_reason());
61 #[cfg(not(feature = "native"))]
62 let reason = wasi_reason(status);
63 match reason {
64 Some(reason) => format!("{status} {reason}"),
65 None => status.to_string(),
66 }
67}
68
69#[cfg(not(feature = "native"))]
70fn wasi_reason(status: u16) -> Option<&'static str> {
71 Some(match status {
72 200 => "OK",
73 204 => "No Content",
74 301 => "Moved Permanently",
75 302 => "Found",
76 303 => "See Other",
77 304 => "Not Modified",
78 307 => "Temporary Redirect",
79 308 => "Permanent Redirect",
80 400 => "Bad Request",
81 401 => "Unauthorized",
82 403 => "Forbidden",
83 404 => "Not Found",
84 405 => "Method Not Allowed",
85 408 => "Request Timeout",
86 409 => "Conflict",
87 410 => "Gone",
88 413 => "Payload Too Large",
89 415 => "Unsupported Media Type",
90 422 => "Unprocessable Entity",
91 429 => "Too Many Requests",
92 500 => "Internal Server Error",
93 501 => "Not Implemented",
94 502 => "Bad Gateway",
95 503 => "Service Unavailable",
96 504 => "Gateway Timeout",
97 _ => return None,
98 })
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn status_text_names_the_reason_phrase() {
107 assert_eq!(status_text(404), "404 Not Found");
108 assert_eq!(status_text(401), "401 Unauthorized");
109 assert_eq!(status_text(799), "799");
111 }
112
113 #[test]
114 fn push_capped_truncates_oversized_chunk() {
115 let mut buf = Vec::new();
116 let stopped = push_capped(&mut buf, &[b'x'; 10], 4);
118 assert!(stopped);
119 assert_eq!(buf.len(), 4);
120 }
121
122 #[test]
123 fn push_capped_accumulates_until_cap() {
124 let mut buf = Vec::new();
125 assert!(!push_capped(&mut buf, b"abc", 8));
126 assert!(!push_capped(&mut buf, b"de", 8));
127 assert_eq!(buf, b"abcde");
128 let stopped = push_capped(&mut buf, b"fghij", 8);
130 assert!(stopped);
131 assert_eq!(buf.len(), 8);
132 assert_eq!(buf, b"abcdefgh");
133 }
134
135 #[test]
136 fn push_capped_small_body_unaffected() {
137 let mut buf = Vec::new();
138 let stopped = push_capped(&mut buf, b"hello", 1024);
139 assert!(!stopped);
140 assert_eq!(buf, b"hello");
141 }
142
143 #[test]
144 fn transient_status_covers_5xx_and_429() {
145 assert!(transient_status(500));
146 assert!(transient_status(503));
147 assert!(transient_status(429));
148 assert!(!transient_status(404));
149 assert!(!transient_status(403));
150 assert!(!transient_status(202));
151 }
152}