Skip to content

Commit

Permalink
Add new CURLINFO_ constants and fn response_http_version()
Browse files Browse the repository at this point in the history
Use `CURLINFO_HTTP_VERSION` (a new constant for the `curl-sys` crate) to
determine the HTTP version used in the last/latest connection.
  • Loading branch information
MarijnS95 committed Jan 7, 2024
1 parent 0c1dddf commit 13a600d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
24 changes: 24 additions & 0 deletions curl-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ pub const CURLINFO_STRING: CURLINFO = 0x100000;
pub const CURLINFO_LONG: CURLINFO = 0x200000;
pub const CURLINFO_DOUBLE: CURLINFO = 0x300000;
pub const CURLINFO_SLIST: CURLINFO = 0x400000;
pub const CURLINFO_PTR: CURLINFO = 0x400000; /* same as SLIST */
pub const CURLINFO_SOCKET: CURLINFO = 0x500000;
pub const CURLINFO_OFF_T: CURLINFO = 0x600000;
pub const CURLINFO_MASK: CURLINFO = 0x0fffff;
pub const CURLINFO_TYPEMASK: CURLINFO = 0xf00000;

Expand Down Expand Up @@ -809,6 +812,27 @@ pub const CURLINFO_PRIMARY_PORT: CURLINFO = CURLINFO_LONG + 40;
pub const CURLINFO_LOCAL_IP: CURLINFO = CURLINFO_STRING + 41;
pub const CURLINFO_LOCAL_PORT: CURLINFO = CURLINFO_LONG + 42;
// pub const CURLINFO_TLS_SESSION: CURLINFO = CURLINFO_SLIST + 43;
pub const CURLINFO_ACTIVESOCKET: CURLINFO = CURLINFO_SOCKET + 44;
pub const CURLINFO_TLS_SSL_PTR: CURLINFO = CURLINFO_PTR + 45;
pub const CURLINFO_HTTP_VERSION: CURLINFO = CURLINFO_LONG + 46;
pub const CURLINFO_PROXY_SSL_VERIFYRESULT: CURLINFO = CURLINFO_LONG + 47;
pub const CURLINFO_PROTOCOL: CURLINFO = CURLINFO_LONG + 48;
pub const CURLINFO_SCHEME: CURLINFO = CURLINFO_STRING + 49;
pub const CURLINFO_TOTAL_TIME_T: CURLINFO = CURLINFO_OFF_T + 50;
pub const CURLINFO_NAMELOOKUP_TIME_T: CURLINFO = CURLINFO_OFF_T + 51;
pub const CURLINFO_CONNECT_TIME_T: CURLINFO = CURLINFO_OFF_T + 52;
pub const CURLINFO_PRETRANSFER_TIME_T: CURLINFO = CURLINFO_OFF_T + 53;
pub const CURLINFO_STARTTRANSFER_TIME_T: CURLINFO = CURLINFO_OFF_T + 54;
pub const CURLINFO_REDIRECT_TIME_T: CURLINFO = CURLINFO_OFF_T + 55;
pub const CURLINFO_APPCONNECT_TIME_T: CURLINFO = CURLINFO_OFF_T + 56;
pub const CURLINFO_RETRY_AFTER: CURLINFO = CURLINFO_OFF_T + 57;
pub const CURLINFO_EFFECTIVE_METHOD: CURLINFO = CURLINFO_STRING + 58;
pub const CURLINFO_PROXY_ERROR: CURLINFO = CURLINFO_LONG + 59;
pub const CURLINFO_REFERER: CURLINFO = CURLINFO_STRING + 60;
pub const CURLINFO_CAINFO: CURLINFO = CURLINFO_STRING + 61;
pub const CURLINFO_CAPATH: CURLINFO = CURLINFO_STRING + 62;
pub const CURLINFO_XFER_ID: CURLINFO = CURLINFO_OFF_T + 63;
pub const CURLINFO_CONN_ID: CURLINFO = CURLINFO_OFF_T + 64;

pub type curl_closepolicy = __enum_ty;
pub const CURLCLOSEPOLICY_NONE: curl_closepolicy = 0;
Expand Down
28 changes: 27 additions & 1 deletion src/easy/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub enum IpResolve {

/// Possible values to pass to the `http_version` method.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HttpVersion {
/// We don't care what http version to use, and we'd like the library to
/// choose the best possible for us.
Expand Down Expand Up @@ -3128,6 +3128,23 @@ impl<H> Easy2<H> {
Ok(list::from_raw(list))
}
}
/// Get the last http version number
///
/// Corresponds to `CURLINFO_HTTP_VERSION` and may return an error if the
/// option isn't supported.
pub fn get_http_version(&self) -> Result<HttpVersionInfo, Error> {
self.getopt_long(curl_sys::CURLINFO_HTTP_VERSION).map(|c| {
HttpVersionInfo::HttpVersion(match c as i32 {
curl_sys::CURL_HTTP_VERSION_1_0 => HttpVersion::V10,
curl_sys::CURL_HTTP_VERSION_1_1 => HttpVersion::V11,
curl_sys::CURL_HTTP_VERSION_2_0 => HttpVersion::V2,
curl_sys::CURL_HTTP_VERSION_2TLS => HttpVersion::V2TLS,
curl_sys::CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE => HttpVersion::V2PriorKnowledge,
curl_sys::CURL_HTTP_VERSION_3 => HttpVersion::V3,
c => return HttpVersionInfo::Unknown(c),
})
})
}

/// Wait for pipelining/multiplexing
///
Expand Down Expand Up @@ -3989,3 +4006,12 @@ impl fmt::Debug for PostRedirections {
.finish()
}
}

/// Possible values returned by [`Easy2::get_http_version()`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HttpVersionInfo {
/// Known HTTP version described by [`HttpVersion`].
HttpVersion(HttpVersion),
/// Invalid or unknown version returned for [`curl_sys::CURLINFO_HTTP_VERSION`].
Unknown(i32),
}

0 comments on commit 13a600d

Please sign in to comment.