Get libcurl version from http module

This commit is contained in:
Fabrice Reix 2021-11-09 16:31:00 +01:00 committed by Fabrice Reix
parent d9337d24e5
commit c3718de9a3
5 changed files with 96 additions and 37 deletions

View File

@ -5,3 +5,5 @@ cargo build --release --verbose
strip target/release/hurl
strip target/release/hurlfmt
target/release/hurl --version

View File

@ -26,6 +26,7 @@ pub use self::request_spec::{Body, FileParam, Method, MultipartParam, RequestSpe
#[cfg(test)]
pub use self::response::tests::*;
pub use self::response::{Response, Version};
pub use self::version::libcurl_version_info;
mod client;
mod core;
@ -33,3 +34,4 @@ mod options;
mod request;
mod request_spec;
mod response;
mod version;

View File

@ -0,0 +1,71 @@
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
///
/// get libcurl version
///
// Output should be similar to curl
// https://github.com/curl/curl/blob/master/lib/version.c
//
// Remarks:
// 1) you must make the data returned from libcurl uniform: prefix, string version
// 2) can not find libpsl info in curl crate
//
pub fn libcurl_version_info() -> Vec<String> {
let version = curl::Version::get();
let mut versions = vec![format!("libcurl/{}", version.version())];
if let Some(s) = version.ssl_version() {
versions.push(s.to_string());
}
if let Some(s) = version.libz_version() {
versions.push(format!("zlib/{}", s.to_string()));
}
if let Some(s) = version.brotli_version() {
versions.push(format!("brotli/{}", s.to_string()));
}
if let Some(s) = version.zstd_version() {
versions.push(s.to_string());
}
if let Some(s) = version.ares_version() {
versions.push(format!("c-ares/{}", s.to_string()));
}
if let Some(s) = version.libidn_version() {
versions.push(format!("libidn2/{}", s.to_string()));
}
if let Some(s) = version.iconv_version_num() {
if s != 0 {
versions.push(format!("iconv/{}", s.to_string()));
}
}
if let Some(s) = version.libssh_version() {
versions.push(s.to_string());
}
if let Some(s) = version.nghttp2_version() {
versions.push(s.to_string());
}
if let Some(s) = version.quic_version() {
versions.push(s.to_string());
}
if let Some(s) = version.hyper_version() {
versions.push(format!("hyper/{}", s.to_string()));
}
if let Some(s) = version.gsasl_version() {
versions.push(format!("libgsal/{}", s.to_string()));
}
versions
}

View File

@ -16,7 +16,6 @@
*
*/
use std::fmt::Write as FmtWrite;
use std::io::prelude::*;
use std::io::{self};
use std::path::{Path, PathBuf};
@ -25,7 +24,6 @@ use std::time::Instant;
use atty::Stream;
use colored::*;
use curl::Version;
use hurl::cli;
use hurl::cli::{CliError, CliOptions, OutputType};
use hurl::http;
@ -248,7 +246,11 @@ pub fn unwrap_or_exit<T>(
}
fn main() {
let version_info = get_version_info();
let version_info = format!(
"{} {}",
clap::crate_version!(),
http::libcurl_version_info().join(" ")
);
let app = cli::app().version(version_info.as_str());
let matches = app.clone().get_matches();
init_colored();
@ -582,37 +584,3 @@ fn get_summary(duration: u128, hurl_results: Vec<HurlResult>) -> String {
s.push_str(format!("Duration: {}ms\n", duration).as_str());
s
}
fn get_version_info() -> String {
let mut ver_string = String::new();
let curl_v = Version::get();
writeln!(ver_string, clap::crate_version!()).expect("Failed to write hurl version string");
for (lib, ver) in [
("libcurl", Some(curl_v.version())),
("", curl_v.ssl_version()),
("zlib", curl_v.libz_version()),
("", curl_v.nghttp2_version()),
("ares", curl_v.ares_version()),
("brotli", curl_v.brotli_version()),
("gsasl", curl_v.gsasl_version()),
("hyper", curl_v.hyper_version()),
(
"iconv",
curl_v.iconv_version_num().map(|v| v.to_string()).as_deref(),
),
("libidn", curl_v.libidn_version()),
("libssh", curl_v.libssh_version()),
("quic", curl_v.quic_version()),
] {
if let Some(version) = ver {
if !lib.is_empty() {
write!(ver_string, "{}/{} ", lib, version)
.expect("Failed to write custom lib version");
} else {
write!(ver_string, "{} ", version).expect("Failed to write lib version string");
}
}
}
writeln!(ver_string).expect("Failed to write version string");
ver_string
}

View File

@ -1100,3 +1100,19 @@ fn test_insecure() {
assert_eq!(request.url, "https://localhost:8001/hello");
assert_eq!(response.status, 200);
}
#[test]
fn test_version() {
// This test if only informative for the time-being
let output = std::process::Command::new("curl")
.args(["--version"])
.output()
.expect("failed to execute process");
let curl_version = std::str::from_utf8(&output.stdout).unwrap();
let index = curl_version.find("libcurl").expect("libcurl substring");
let expected_version = &curl_version[index..];
eprintln!("{:?}", expected_version);
let versions = libcurl_version_info();
eprintln!("{:?}", versions);
}