From 7973f9288f9d3ce5fb6f702a55adb5441e208dae Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Fri, 24 Jan 2025 11:20:57 +0800 Subject: [PATCH] feat: Implement `IntoStreamDependency` for tuple and `StreamDependency` --- examples/impersonate_settings.rs | 2 +- src/client/http.rs | 7 +------ src/http2.rs | 35 +++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/examples/impersonate_settings.rs b/examples/impersonate_settings.rs index 7da66ec6..8fd4763d 100644 --- a/examples/impersonate_settings.rs +++ b/examples/impersonate_settings.rs @@ -137,7 +137,7 @@ async fn main() -> Result<(), rquest::Error> { .initial_stream_window_size(131072) .max_frame_size(16384) .initial_connection_window_size(12517377 + 65535) - .headers_priority((13, 41, false)) + .headers_priority(StreamDependency::new(StreamId::from(13), 41, false)) .headers_pseudo_order([Method, Scheme, Authority, Path]) .settings_order([ HeaderTableSize, diff --git a/src/client/http.rs b/src/client/http.rs index 77fac037..2f97a850 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -22,7 +22,6 @@ use http::header::{ use http::uri::Scheme; use http::{HeaderName, Uri, Version}; use hyper2::client::conn::{http1, http2}; -use hyper2::{StreamDependency, StreamId}; use pin_project_lite::pin_project; use std::future::Future; use std::pin::Pin; @@ -2278,10 +2277,6 @@ fn add_cookie_header(headers: &mut HeaderMap, cookie_store: &dyn cookie::CookieS } fn apply_http2_settings(builder: &mut http2::Builder, http2: Http2Settings) { - let http2_headers_priority = http2 - .headers_priority - .map(|(a, b, c)| StreamDependency::new(StreamId::from(a), b, c)); - builder .initial_stream_id(http2.initial_stream_id) .initial_stream_window_size(http2.initial_stream_window_size) @@ -2289,7 +2284,7 @@ fn apply_http2_settings(builder: &mut http2::Builder, http2: Http2Settings .max_concurrent_streams(http2.max_concurrent_streams) .header_table_size(http2.header_table_size) .max_frame_size(http2.max_frame_size) - .headers_priority(http2_headers_priority) + .headers_priority(http2.headers_priority) .headers_pseudo_order(http2.headers_pseudo_order) .settings_order(http2.settings_order) .priority(http2.priority); diff --git a/src/http2.rs b/src/http2.rs index b228e220..cf772277 100644 --- a/src/http2.rs +++ b/src/http2.rs @@ -1,5 +1,5 @@ //! HTTP/2 settings. -use hyper2::{Priority, PseudoOrder, SettingsOrder}; +use hyper2::{Priority, PseudoOrder, SettingsOrder, StreamDependency, StreamId}; use std::borrow::Cow; use typed_builder::TypedBuilder; @@ -83,8 +83,8 @@ pub struct Http2Settings { /// /// - **Structure:** `(stream_dependency, weight, exclusive_flag)` /// - **Purpose:** Specifies how header frames are prioritized during transmission. - #[builder(default, setter(into))] - pub headers_priority: Option<(u32, u8, bool)>, + #[builder(default, setter(transform = |input: impl IntoStreamDependency| Some(input.into())))] + pub headers_priority: Option, /// The order of pseudo-header fields. /// @@ -101,3 +101,32 @@ pub struct Http2Settings { #[builder(default, setter(strip_option, into))] pub priority: Option>, } + +/// A trait for converting various types into a `StreamDependency`. +/// +/// This trait is used to provide a unified way to convert different types +/// into a `StreamDependency` instance. +pub trait IntoStreamDependency { + /// Converts the implementing type into a `StreamDependency`. + fn into(self) -> StreamDependency; +} + +/// Implements `IntoStreamDependency` for a tuple of `(u32, u8, bool)`. +/// +/// This implementation allows a tuple containing a stream ID, weight, and +/// exclusive flag to be converted into a `StreamDependency`. +impl IntoStreamDependency for (u32, u8, bool) { + fn into(self) -> StreamDependency { + StreamDependency::new(StreamId::from(self.0), self.1, self.2) + } +} + +/// Implements `IntoStreamDependency` for `StreamDependency`. +/// +/// This implementation allows a `StreamDependency` to be converted into +/// itself, which is useful for cases where a generic conversion is needed. +impl IntoStreamDependency for StreamDependency { + fn into(self) -> StreamDependency { + self + } +}