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: Implement IntoStreamDependency for tuple and StreamDependency #359

Merged
merged 1 commit into from
Jan 24, 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
2 changes: 1 addition & 1 deletion examples/impersonate_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2278,18 +2277,14 @@ fn add_cookie_header(headers: &mut HeaderMap, cookie_store: &dyn cookie::CookieS
}

fn apply_http2_settings(builder: &mut http2::Builder<Exec>, 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)
.initial_connection_window_size(http2.initial_connection_window_size)
.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);
Expand Down
35 changes: 32 additions & 3 deletions src/http2.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<StreamDependency>,

/// The order of pseudo-header fields.
///
Expand All @@ -101,3 +101,32 @@ pub struct Http2Settings {
#[builder(default, setter(strip_option, into))]
pub priority: Option<Cow<'static, [Priority]>>,
}

/// 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
}
}
Loading