Skip to content

Commit

Permalink
feat: Add implementations for IntoCertCompressionAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Jan 25, 2025
1 parent e8ac713 commit d0e9239
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
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
51 changes: 50 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,52 @@ 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]>>;
}

/// Implements `IntoCertCompressionAlgorithm` for a static slice of `CertCompressionAlgorithm`.
///
/// This implementation allows a static slice of `CertCompressionAlgorithm` to be converted
/// into an optional `Cow` containing a borrowed slice of `CertCompressionAlgorithm`.
impl IntoCertCompressionAlgorithm for &'static [CertCompressionAlgorithm] {
fn into(self) -> Option<Cow<'static, [CertCompressionAlgorithm]>> {
Some(Cow::Borrowed(&self))
}
}

/// Implements `IntoCertCompressionAlgorithm` for a `Cow` containing a slice of `CertCompressionAlgorithm`.
///
/// This implementation allows a `Cow` containing a slice of `CertCompressionAlgorithm` to be converted
/// into an optional `Cow` containing the same slice of `CertCompressionAlgorithm`.
impl IntoCertCompressionAlgorithm for Cow<'static, [CertCompressionAlgorithm]> {
fn into(self) -> Option<Cow<'static, [CertCompressionAlgorithm]>> {
Some(self)
}
}

/// Implements `IntoCertCompressionAlgorithm` for an optional static slice of `CertCompressionAlgorithm`.
///
/// This implementation allows an optional static slice of `CertCompressionAlgorithm` to be converted
/// into an optional `Cow` containing a borrowed slice of `CertCompressionAlgorithm`.
impl IntoCertCompressionAlgorithm for Option<&'static [CertCompressionAlgorithm]> {
fn into(self) -> Option<Cow<'static, [CertCompressionAlgorithm]>> {
self.map(Cow::Borrowed)
}
}

/// Implements `IntoCertCompressionAlgorithm` for an optional `Cow` containing a slice of `CertCompressionAlgorithm`.
///
/// This implementation allows an optional `Cow` containing a slice of `CertCompressionAlgorithm` to be converted
/// into an optional `Cow` containing the same slice of `CertCompressionAlgorithm`.
impl IntoCertCompressionAlgorithm for Option<Cow<'static, [CertCompressionAlgorithm]>> {
fn into(self) -> Option<Cow<'static, [CertCompressionAlgorithm]>> {
self
}
}

0 comments on commit d0e9239

Please sign in to comment.