eggfetch_core/
http_version.rs1use crate::error::Error;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19pub enum HttpVersionPolicy {
20 Http1Only,
23 Http2Only,
26 Http3Only,
29 Auto {
36 allow_http3: bool,
38 },
39}
40
41impl Default for HttpVersionPolicy {
42 fn default() -> Self {
43 Self::Auto { allow_http3: false }
44 }
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub(crate) enum Http3Capability {
53 Compiled,
55 NotCompiled,
57}
58
59impl Http3Capability {
60 pub(crate) const CURRENT: Self = if cfg!(feature = "http3") {
62 Self::Compiled
63 } else {
64 Self::NotCompiled
65 };
66
67 pub(crate) const fn is_available(self) -> bool {
69 matches!(self, Self::Compiled)
70 }
71}
72
73pub(crate) fn missing_client_error(policy: HttpVersionPolicy) -> Error {
91 if matches!(policy, HttpVersionPolicy::Http3Only) && !Http3Capability::CURRENT.is_available() {
92 return Error::Unsupported(
93 "HTTP/3 is not supported by this build: the http3 feature was not compiled in".into(),
94 );
95 }
96 Error::Unsupported("HTTP client not available for this protocol".into())
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102pub(crate) struct HttpVersionPolicyEnabler(HttpVersionPolicy);
103
104impl HttpVersionPolicyEnabler {
105 #[must_use]
110 pub(crate) fn from_policy(policy: HttpVersionPolicy) -> Self {
111 match policy {
112 #[cfg(not(feature = "transport-http2"))]
113 HttpVersionPolicy::Http2Only => Self(HttpVersionPolicy::Http1Only),
114 #[cfg(not(feature = "http3"))]
115 HttpVersionPolicy::Http3Only => Self(HttpVersionPolicy::Http1Only),
116 #[allow(
117 unused_variables,
118 reason = "the `Auto` arm ignores the caller's HTTP/3 flag when the http2 or http3 feature is compiled out, so the binding is unused in those builds"
119 )]
120 HttpVersionPolicy::Auto { allow_http3 } => {
121 #[cfg(not(feature = "transport-http2"))]
122 return Self(HttpVersionPolicy::Http1Only);
123 #[cfg(all(feature = "transport-http2", not(feature = "http3")))]
124 return Self(HttpVersionPolicy::Auto { allow_http3: false });
125 #[cfg(all(feature = "transport-http2", feature = "http3"))]
126 return Self(HttpVersionPolicy::Auto { allow_http3 });
127 }
128 _ => Self(policy),
129 }
130 }
131
132 #[must_use]
134 #[allow(
135 dead_code,
136 reason = "the HTTP/1.1 switch is only read by the ALPN and connector builders, which are compiled out when neither tls-rustls nor transport-http2 is enabled"
137 )]
138 pub(crate) const fn enable_http1(self) -> bool {
139 matches!(
140 self.0,
141 HttpVersionPolicy::Http1Only | HttpVersionPolicy::Auto { .. }
142 )
143 }
144
145 #[must_use]
147 #[allow(
148 dead_code,
149 reason = "the HTTP/2 switch is only read by the ALPN and connector builders, which are compiled out when neither tls-rustls nor transport-http2 is enabled"
150 )]
151 pub(crate) const fn enable_http2(self) -> bool {
152 matches!(
153 self.0,
154 HttpVersionPolicy::Http2Only | HttpVersionPolicy::Auto { .. }
155 )
156 }
157
158 #[must_use]
160 #[allow(
161 dead_code,
162 reason = "the HTTP/3 switch is only read by the HTTP/3 connector setup, which is compiled out when the http3 feature is disabled"
163 )]
164 pub(crate) fn use_http3(self) -> bool {
165 match self.0 {
166 HttpVersionPolicy::Http3Only => true,
167 HttpVersionPolicy::Auto { allow_http3 } => allow_http3,
168 _ => false,
169 }
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176
177 #[test]
178 fn default_is_auto() {
179 assert_eq!(
180 HttpVersionPolicy::default(),
181 HttpVersionPolicy::Auto { allow_http3: false }
182 );
183 }
184
185 #[test]
186 fn http1_only_enables_http1_only() {
187 let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http1Only);
188 assert!(enabler.enable_http1());
189 assert!(!enabler.enable_http2());
190 assert!(!enabler.use_http3());
191 }
192
193 #[test]
194 #[cfg(feature = "transport-http2")]
195 fn auto_enables_both() {
196 let enabler =
197 HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
198 assert!(enabler.enable_http1());
199 assert!(enabler.enable_http2());
200 assert!(!enabler.use_http3());
201 }
202
203 #[test]
204 #[cfg(not(feature = "transport-http2"))]
205 fn auto_downgrades_without_feature() {
206 let enabler =
207 HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
208 assert!(enabler.enable_http1());
209 assert!(!enabler.enable_http2());
210 }
211
212 #[test]
213 #[cfg(feature = "transport-http2")]
214 fn http2_only_enables_http2_only() {
215 let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
216 assert!(!enabler.enable_http1());
217 assert!(enabler.enable_http2());
218 }
219
220 #[test]
221 #[cfg(not(feature = "transport-http2"))]
222 fn http2_only_downgrades_without_feature() {
223 let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
224 assert!(enabler.enable_http1());
225 assert!(!enabler.enable_http2());
226 }
227
228 #[test]
229 #[cfg(feature = "http3")]
230 fn http3_only_enables_http3_only() {
231 let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http3Only);
232 assert!(!enabler.enable_http1());
233 assert!(!enabler.enable_http2());
234 assert!(enabler.use_http3());
235 }
236
237 #[test]
238 #[cfg(not(feature = "http3"))]
239 fn http3_only_downgrades_without_feature() {
240 let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http3Only);
241 assert!(enabler.enable_http1());
242 assert!(!enabler.enable_http2());
243 assert!(!enabler.use_http3());
244 }
245
246 #[test]
247 #[cfg(not(feature = "http3"))]
248 fn missing_client_error_names_the_http3_capability_gap() {
249 let error = missing_client_error(HttpVersionPolicy::Http3Only);
250 let message = error.to_string();
251 assert!(
252 message.contains("HTTP/3"),
253 "the failure must name HTTP/3: {message}"
254 );
255 assert!(
256 !message.contains("HTTP client not available"),
257 "the failure must not reuse the generic missing-client wording: {message}"
258 );
259 }
260
261 #[test]
262 #[cfg(feature = "http3")]
263 fn missing_client_error_keeps_the_generic_wording_when_http3_is_compiled() {
264 let error = missing_client_error(HttpVersionPolicy::Http3Only);
265 let message = error.to_string();
266 assert!(
267 message.contains("HTTP client not available"),
268 "a compiled-in HTTP/3 build has no capability gap to report: {message}"
269 );
270 }
271
272 #[test]
273 fn missing_client_error_keeps_the_generic_wording_for_other_policies() {
274 for policy in [
275 HttpVersionPolicy::Http1Only,
276 HttpVersionPolicy::Http2Only,
277 HttpVersionPolicy::Auto { allow_http3: false },
278 HttpVersionPolicy::Auto { allow_http3: true },
279 ] {
280 let message = missing_client_error(policy).to_string();
281 assert!(
282 message.contains("HTTP client not available"),
283 "unexpected classification for {policy:?}: {message}"
284 );
285 }
286 }
287
288 #[test]
289 fn classification_is_typed_and_does_not_render_the_policy() {
290 let message = missing_client_error(HttpVersionPolicy::Http3Only).to_string();
294 assert!(
295 !message.contains("Http3Only"),
296 "the classification must not embed a rendered policy: {message}"
297 );
298 assert!(
299 !message.contains("Auto"),
300 "the classification must not embed a rendered policy: {message}"
301 );
302 }
303
304 #[test]
305 fn http3_capability_matches_the_compiled_feature_set() {
306 assert_eq!(
307 Http3Capability::CURRENT.is_available(),
308 cfg!(feature = "http3"),
309 "the typed capability must track the compiled feature set"
310 );
311 }
312}