use std::sync::LazyLock;
static CURL_VERSION: LazyLock<curl::Version> = LazyLock::new(curl::Version::get);
pub fn version() -> &'static str {
static VERSION_STRING: LazyLock<String> = LazyLock::new(|| {
format!(
"isahc/{} (features:{}) {}",
env!("CARGO_PKG_VERSION"),
env!("ISAHC_FEATURES"),
curl::Version::num(),
)
});
&VERSION_STRING
}
pub fn is_http_version_supported(version: http::Version) -> bool {
match version {
http::Version::HTTP_09 => match curl_version() {
(7, minor, _) if minor < 66 => true,
(major, _, _) if major < 7 => true,
_ => false,
},
http::Version::HTTP_10 => true,
http::Version::HTTP_11 => true,
http::Version::HTTP_2 => CURL_VERSION.feature_http2(),
http::Version::HTTP_3 => CURL_VERSION.feature_http3(),
_ => false,
}
}
fn curl_version() -> (u8, u8, u8) {
let bits = CURL_VERSION.version_num();
((bits >> 16) as u8, (bits >> 8) as u8, bits as u8)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_expected() {
let version = version();
assert!(version.starts_with("isahc/1."));
assert!(version.contains("curl/7.") || version.contains("curl/8."));
}
#[test]
fn curl_version_expected() {
let (major, minor, _patch) = curl_version();
assert!(major >= 7);
assert!(minor > 0);
}
#[test]
fn http1_always_supported() {
assert!(is_http_version_supported(http::Version::HTTP_10));
assert!(is_http_version_supported(http::Version::HTTP_11));
if cfg!(feature = "http2") {
assert!(is_http_version_supported(http::Version::HTTP_2));
}
}
}