1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use std::ffi::CStr;
use std::fmt;
use std::str;

use curl_sys;
use libc::{c_char, c_int};

/// Version information about libcurl and the capabilities that it supports.
pub struct Version {
    inner: *mut curl_sys::curl_version_info_data,
}

unsafe impl Send for Version {}
unsafe impl Sync for Version {}

/// An iterator over the list of protocols a version supports.
#[derive(Clone)]
pub struct Protocols<'a> {
    cur: *const *const c_char,
    _inner: &'a Version,
}

impl Version {
    /// Returns the libcurl version that this library is currently linked against.
    pub fn num() -> &'static str {
        unsafe {
            let s = CStr::from_ptr(curl_sys::curl_version() as *const _);
            str::from_utf8(s.to_bytes()).unwrap()
        }
    }

    /// Returns the libcurl version that this library is currently linked against.
    pub fn get() -> Version {
        unsafe {
            let ptr = curl_sys::curl_version_info(curl_sys::CURLVERSION_NOW);
            assert!(!ptr.is_null());
            Version { inner: ptr }
        }
    }

    /// Returns the human readable version string,
    pub fn version(&self) -> &str {
        unsafe { crate::opt_str((*self.inner).version).unwrap() }
    }

    /// Returns a numeric representation of the version number
    ///
    /// This is a 24 bit number made up of the major number, minor, and then
    /// patch number. For example 7.9.8 will return 0x070908.
    pub fn version_num(&self) -> u32 {
        unsafe { (*self.inner).version_num as u32 }
    }

    /// Returns a human readable string of the host libcurl is built for.
    ///
    /// This is discovered as part of the build environment.
    pub fn host(&self) -> &str {
        unsafe { crate::opt_str((*self.inner).host).unwrap() }
    }

    /// Returns whether libcurl supports IPv6
    pub fn feature_ipv6(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_IPV6)
    }

    /// Returns whether libcurl supports SSL
    pub fn feature_ssl(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_SSL)
    }

    /// Returns whether libcurl supports HTTP deflate via libz
    pub fn feature_libz(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_LIBZ)
    }

    /// Returns whether libcurl supports HTTP NTLM
    pub fn feature_ntlm(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_NTLM)
    }

    /// Returns whether libcurl supports HTTP GSSNEGOTIATE
    pub fn feature_gss_negotiate(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_GSSNEGOTIATE)
    }

    /// Returns whether libcurl was built with debug capabilities
    pub fn feature_debug(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_DEBUG)
    }

    /// Returns whether libcurl was built with SPNEGO authentication
    pub fn feature_spnego(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_SPNEGO)
    }

    /// Returns whether libcurl was built with large file support
    pub fn feature_largefile(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_LARGEFILE)
    }

    /// Returns whether libcurl was built with support for IDNA, domain names
    /// with international letters.
    pub fn feature_idn(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_IDN)
    }

    /// Returns whether libcurl was built with support for SSPI.
    pub fn feature_sspi(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_SSPI)
    }

    /// Returns whether libcurl was built with asynchronous name lookups.
    pub fn feature_async_dns(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_ASYNCHDNS)
    }

    /// Returns whether libcurl was built with support for character
    /// conversions.
    pub fn feature_conv(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_CONV)
    }

    /// Returns whether libcurl was built with support for TLS-SRP.
    pub fn feature_tlsauth_srp(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_TLSAUTH_SRP)
    }

    /// Returns whether libcurl was built with support for NTLM delegation to
    /// winbind helper.
    pub fn feature_ntlm_wb(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_NTLM_WB)
    }

    /// Returns whether libcurl was built with support for unix domain socket
    pub fn feature_unix_domain_socket(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_UNIX_SOCKETS)
    }

    /// Returns whether libcurl was built with support for HTTP2.
    pub fn feature_http2(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_HTTP2)
    }

    /// Returns whether libcurl was built with support for HTTP3.
    pub fn feature_http3(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_HTTP3)
    }

    /// Returns whether libcurl was built with support for Brotli.
    pub fn feature_brotli(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_BROTLI)
    }

    /// Returns whether libcurl was built with support for Alt-Svc.
    pub fn feature_altsvc(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_ALTSVC)
    }

    /// Returns whether libcurl was built with support for zstd
    pub fn feature_zstd(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_ZSTD)
    }

    /// Returns whether libcurl was built with support for unicode
    pub fn feature_unicode(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_UNICODE)
    }

    /// Returns whether libcurl was built with support for hsts
    pub fn feature_hsts(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_HSTS)
    }

    /// Returns whether libcurl was built with support for gsasl
    pub fn feature_gsasl(&self) -> bool {
        self.flag(curl_sys::CURL_VERSION_GSASL)
    }

    fn flag(&self, flag: c_int) -> bool {
        unsafe { (*self.inner).features & flag != 0 }
    }

    /// Returns the version of OpenSSL that is used, or None if there is no SSL
    /// support.
    pub fn ssl_version(&self) -> Option<&str> {
        unsafe { crate::opt_str((*self.inner).ssl_version) }
    }

    /// Returns the version of libz that is used, or None if there is no libz
    /// support.
    pub fn libz_version(&self) -> Option<&str> {
        unsafe { crate::opt_str((*self.inner).libz_version) }
    }

    /// Returns an iterator over the list of protocols that this build of
    /// libcurl supports.
    pub fn protocols(&self) -> Protocols {
        unsafe {
            Protocols {
                _inner: self,
                cur: (*self.inner).protocols,
            }
        }
    }

    /// If available, the human readable version of ares that libcurl is linked
    /// against.
    pub fn ares_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SECOND {
                crate::opt_str((*self.inner).ares)
            } else {
                None
            }
        }
    }

    /// If available, the version of ares that libcurl is linked against.
    pub fn ares_version_num(&self) -> Option<u32> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SECOND {
                Some((*self.inner).ares_num as u32)
            } else {
                None
            }
        }
    }

    /// If available, the version of libidn that libcurl is linked against.
    pub fn libidn_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_THIRD {
                crate::opt_str((*self.inner).libidn)
            } else {
                None
            }
        }
    }

    /// If available, the version of iconv libcurl is linked against.
    pub fn iconv_version_num(&self) -> Option<u32> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_FOURTH {
                Some((*self.inner).iconv_ver_num as u32)
            } else {
                None
            }
        }
    }

    /// If available, the version of libssh that libcurl is linked against.
    pub fn libssh_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_FOURTH {
                crate::opt_str((*self.inner).libssh_version)
            } else {
                None
            }
        }
    }

    /// If available, the version of brotli libcurl is linked against.
    pub fn brotli_version_num(&self) -> Option<u32> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_FIFTH {
                Some((*self.inner).brotli_ver_num)
            } else {
                None
            }
        }
    }

    /// If available, the version of brotli libcurl is linked against.
    pub fn brotli_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_FIFTH {
                crate::opt_str((*self.inner).brotli_version)
            } else {
                None
            }
        }
    }

    /// If available, the version of nghttp2 libcurl is linked against.
    pub fn nghttp2_version_num(&self) -> Option<u32> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SIXTH {
                Some((*self.inner).nghttp2_ver_num)
            } else {
                None
            }
        }
    }

    /// If available, the version of nghttp2 libcurl is linked against.
    pub fn nghttp2_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SIXTH {
                crate::opt_str((*self.inner).nghttp2_version)
            } else {
                None
            }
        }
    }

    /// If available, the version of quic libcurl is linked against.
    pub fn quic_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SIXTH {
                crate::opt_str((*self.inner).quic_version)
            } else {
                None
            }
        }
    }

    /// If available, the built-in default of CURLOPT_CAINFO.
    pub fn cainfo(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SEVENTH {
                crate::opt_str((*self.inner).cainfo)
            } else {
                None
            }
        }
    }

    /// If available, the built-in default of CURLOPT_CAPATH.
    pub fn capath(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_SEVENTH {
                crate::opt_str((*self.inner).capath)
            } else {
                None
            }
        }
    }

    /// If avaiable, the numeric zstd version
    ///
    /// Represented as `(MAJOR << 24) | (MINOR << 12) | PATCH`
    pub fn zstd_ver_num(&self) -> Option<u32> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_EIGHTH {
                Some((*self.inner).zstd_ver_num)
            } else {
                None
            }
        }
    }

    /// If available, the human readable version of zstd
    pub fn zstd_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_EIGHTH {
                crate::opt_str((*self.inner).zstd_version)
            } else {
                None
            }
        }
    }

    /// If available, the human readable version of hyper
    pub fn hyper_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_NINTH {
                crate::opt_str((*self.inner).hyper_version)
            } else {
                None
            }
        }
    }

    /// If available, the human readable version of hyper
    pub fn gsasl_version(&self) -> Option<&str> {
        unsafe {
            if (*self.inner).age >= curl_sys::CURLVERSION_TENTH {
                crate::opt_str((*self.inner).gsasl_version)
            } else {
                None
            }
        }
    }
}

impl fmt::Debug for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut f = f.debug_struct("Version");
        f.field("version", &self.version())
            .field("rust_crate_version", &env!("CARGO_PKG_VERSION"))
            .field("rust_sys_crate_version", &curl_sys::rust_crate_version())
            .field("host", &self.host())
            .field("feature_ipv6", &self.feature_ipv6())
            .field("feature_ssl", &self.feature_ssl())
            .field("feature_libz", &self.feature_libz())
            .field("feature_ntlm", &self.feature_ntlm())
            .field("feature_gss_negotiate", &self.feature_gss_negotiate())
            .field("feature_debug", &self.feature_debug())
            .field("feature_spnego", &self.feature_spnego())
            .field("feature_largefile", &self.feature_largefile())
            .field("feature_idn", &self.feature_idn())
            .field("feature_sspi", &self.feature_sspi())
            .field("feature_async_dns", &self.feature_async_dns())
            .field("feature_conv", &self.feature_conv())
            .field("feature_tlsauth_srp", &self.feature_tlsauth_srp())
            .field("feature_ntlm_wb", &self.feature_ntlm_wb())
            .field(
                "feature_unix_domain_socket",
                &self.feature_unix_domain_socket(),
            )
            .field("feature_altsvc", &self.feature_altsvc())
            .field("feature_zstd", &self.feature_zstd())
            .field("feature_unicode", &self.feature_unicode())
            .field("feature_http3", &self.feature_http3())
            .field("feature_http2", &self.feature_http2())
            .field("feature_gsasl", &self.feature_gsasl())
            .field("feature_brotli", &self.feature_brotli());

        if let Some(s) = self.ssl_version() {
            f.field("ssl_version", &s);
        }
        if let Some(s) = self.libz_version() {
            f.field("libz_version", &s);
        }
        if let Some(s) = self.ares_version() {
            f.field("ares_version", &s);
        }
        if let Some(s) = self.libidn_version() {
            f.field("libidn_version", &s);
        }
        if let Some(s) = self.iconv_version_num() {
            f.field("iconv_version_num", &format!("{:x}", s));
        }
        if let Some(s) = self.libssh_version() {
            f.field("libssh_version", &s);
        }
        if let Some(s) = self.brotli_version_num() {
            f.field("brotli_version_num", &format!("{:x}", s));
        }
        if let Some(s) = self.brotli_version() {
            f.field("brotli_version", &s);
        }
        if let Some(s) = self.nghttp2_version_num() {
            f.field("nghttp2_version_num", &format!("{:x}", s));
        }
        if let Some(s) = self.nghttp2_version() {
            f.field("nghttp2_version", &s);
        }
        if let Some(s) = self.quic_version() {
            f.field("quic_version", &s);
        }
        if let Some(s) = self.zstd_ver_num() {
            f.field("zstd_ver_num", &format!("{:x}", s));
        }
        if let Some(s) = self.zstd_version() {
            f.field("zstd_version", &s);
        }
        if let Some(s) = self.cainfo() {
            f.field("cainfo", &s);
        }
        if let Some(s) = self.capath() {
            f.field("capath", &s);
        }
        if let Some(s) = self.hyper_version() {
            f.field("hyper_version", &s);
        }
        if let Some(s) = self.gsasl_version() {
            f.field("gsasl_version", &s);
        }

        f.field("protocols", &self.protocols().collect::<Vec<_>>());

        f.finish()
    }
}

impl<'a> Iterator for Protocols<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<&'a str> {
        unsafe {
            if (*self.cur).is_null() {
                return None;
            }
            let ret = crate::opt_str(*self.cur).unwrap();
            self.cur = self.cur.offset(1);
            Some(ret)
        }
    }
}

impl<'a> fmt::Debug for Protocols<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.clone()).finish()
    }
}