diff --git a/examples/impersonate_settings.rs b/examples/impersonate_settings.rs index 8fd4763d..53762747 100644 --- a/examples/impersonate_settings.rs +++ b/examples/impersonate_settings.rs @@ -6,7 +6,6 @@ use rquest::{ use rquest::{Client, ImpersonateSettings}; use rquest::{Http2Settings, PseudoOrder::*, SettingsOrder::*}; use rquest::{Priority, StreamDependency, StreamId}; -use std::borrow::Cow; // ============== TLS Extension Algorithms ============== @@ -119,7 +118,7 @@ async fn main() -> Result<(), rquest::Error> { .cipher_list(CIPHER_LIST) .sigalgs_list(SIGALGS_LIST) .delegated_credentials(DELEGATED_CREDENTIALS) - .cert_compression_algorithm(Cow::Borrowed(CERT_COMPRESSION_ALGORITHM)) + .cert_compression_algorithm(CERT_COMPRESSION_ALGORITHM) .record_size_limit(RECORD_SIZE_LIMIT) .alpn_protos(AlpnProtos::All) .alps_protos(AlpsProtos::Http2) diff --git a/src/imp/firefox.rs b/src/imp/firefox.rs index 51b75127..a685b76f 100644 --- a/src/imp/firefox.rs +++ b/src/imp/firefox.rs @@ -314,13 +314,13 @@ mod tls { .enable_ocsp_stapling(true) .enable_ech_grease(val.enable_ech_grease) .alpn_protos(AlpnProtos::All) - .cert_compression_algorithm(val.cert_compression_algorithm.map(Cow::Borrowed)) .min_tls_version(TlsVersion::TLS_1_2) .max_tls_version(TlsVersion::TLS_1_3) .key_shares_limit(val.key_shares_limit) .pre_shared_key(val.pre_shared_key) .psk_skip_session_ticket(val.psk_skip_session_tickets) .psk_dhe_ke(val.psk_dhe_ke) + .cert_compression_algorithm(val.cert_compression_algorithm) .extension_permutation_indices(val.extension_permutation_indices) .build() } diff --git a/src/tls/mod.rs b/src/tls/mod.rs index b142dd47..a6225940 100644 --- a/src/tls/mod.rs +++ b/src/tls/mod.rs @@ -369,7 +369,7 @@ pub struct TlsSettings { pub sigalgs_list: Option>, /// Certificates in TLS 1.3 can be compressed [RFC 8879](https://datatracker.ietf.org/doc/html/rfc8879). - #[builder(default, setter(into))] + #[builder(default, setter(transform = |input: impl IntoCertCompressionAlgorithm| input.into()))] pub cert_compression_algorithm: Option>, /// Sets the context's extension permutation indices. @@ -413,3 +413,73 @@ impl_debug!( extension_permutation_indices } ); + +/// A trait for converting various types into an optional `Cow` containing a slice of `CertCompressionAlgorithm`. +/// +/// This trait is used to provide a unified way to convert different types +/// into an optional `Cow` containing a slice of `CertCompressionAlgorithm`. +pub trait IntoCertCompressionAlgorithm { + /// Converts the implementing type into an optional `Cow` containing a slice of `CertCompressionAlgorithm`. + fn into(self) -> Option>; +} + +// Macro to implement IntoCertCompressionAlgorithm for various types +macro_rules! impl_into_cert_compression_algorithm_for_types { + ($($t:ty => $body:expr),*) => { + $( + impl IntoCertCompressionAlgorithm for $t { + fn into(self) -> Option> { + $body(self) + } + } + )* + }; +} + +// Macro to implement IntoCertCompressionAlgorithm for const-sized arrays +macro_rules! impl_into_cert_compression_algorithm_for_arrays { + ($($t:ty => $body:expr),*) => { + $( + impl IntoCertCompressionAlgorithm for $t { + fn into(self) -> Option> { + $body(self) + } + } + )* + }; +} + +impl_into_cert_compression_algorithm_for_types!( + &'static [CertCompressionAlgorithm] => |s| Some(Cow::Borrowed(s)), + Option<&'static [CertCompressionAlgorithm]> => |s: Option<&'static [CertCompressionAlgorithm]>| s.map(Cow::Borrowed) +); + +impl_into_cert_compression_algorithm_for_types!( + Cow<'static, [CertCompressionAlgorithm]> => |s| Some(s), + Option> => |s| s +); + +impl_into_cert_compression_algorithm_for_types!( + &'static CertCompressionAlgorithm => |s: &'static CertCompressionAlgorithm| Some(Cow::Owned(vec![*s])), + Option<&'static CertCompressionAlgorithm> => |s: Option<&'static CertCompressionAlgorithm>| s.map(|alg| Cow::Owned(vec![*alg])) +); + +impl_into_cert_compression_algorithm_for_types!( + CertCompressionAlgorithm => |s| Some(Cow::Owned(vec![s])), + Option => |s: Option| s.map(|alg| Cow::Owned(vec![alg])) +); + +impl_into_cert_compression_algorithm_for_types!( + Vec => |s| Some(Cow::Owned(s)), + Option> => |s: Option>| s.map(Cow::Owned) +); + +impl_into_cert_compression_algorithm_for_arrays!( + &'static [CertCompressionAlgorithm; N] => |s: &'static [CertCompressionAlgorithm; N]| Some(Cow::Borrowed(&s[..])), + Option<&'static [CertCompressionAlgorithm; N]> => |s: Option<&'static [CertCompressionAlgorithm; N]>| s.map(|s| Cow::Borrowed(&s[..])) +); + +impl_into_cert_compression_algorithm_for_arrays!( + [CertCompressionAlgorithm; N] => |s: [CertCompressionAlgorithm; N]| Some(Cow::Owned(s.to_vec())), + Option<[CertCompressionAlgorithm; N]> => |s: Option<[CertCompressionAlgorithm; N]>| s.map(|arr| Cow::Owned(arr.to_vec())) +);