Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add implementations for IntoCertCompressionAlgorithm #363

Merged
merged 8 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/impersonate_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==============

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/imp/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
72 changes: 71 additions & 1 deletion src/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub struct TlsSettings {
pub sigalgs_list: Option<Cow<'static, str>>,

/// 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<Cow<'static, [CertCompressionAlgorithm]>>,

/// Sets the context's extension permutation indices.
Expand Down Expand Up @@ -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<Cow<'static, [CertCompressionAlgorithm]>>;
}

// 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<Cow<'static, [CertCompressionAlgorithm]>> {
$body(self)
}
}
)*
};
}

// Macro to implement IntoCertCompressionAlgorithm for const-sized arrays
macro_rules! impl_into_cert_compression_algorithm_for_arrays {
($($t:ty => $body:expr),*) => {
$(
impl<const N: usize> IntoCertCompressionAlgorithm for $t {
fn into(self) -> Option<Cow<'static, [CertCompressionAlgorithm]>> {
$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<Cow<'static, [CertCompressionAlgorithm]>> => |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<CertCompressionAlgorithm> => |s: Option<CertCompressionAlgorithm>| s.map(|alg| Cow::Owned(vec![alg]))
);

impl_into_cert_compression_algorithm_for_types!(
Vec<CertCompressionAlgorithm> => |s| Some(Cow::Owned(s)),
Option<Vec<CertCompressionAlgorithm>> => |s: Option<Vec<CertCompressionAlgorithm>>| 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()))
);
Loading