Skip to content

Commit

Permalink
feat(client): use max http version of client by default instead of ht…
Browse files Browse the repository at this point in the history
…tp 1.1
  • Loading branch information
joelwurtz committed Dec 19, 2024
1 parent 660f354 commit 35eb6e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
17 changes: 15 additions & 2 deletions client/src/middleware/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ where
type Error = Error;

async fn call(&self, req: ServiceRequest<'r, 'c>) -> Result<Self::Response, Self::Error> {
let ServiceRequest { req, client, timeout } = req;
let ServiceRequest {
req,
client,
timeout,
version,
} = req;
let mut headers = req.headers().clone();
let mut method = req.method().clone();
let mut uri = req.uri().clone();
loop {
let mut res = self.service.call(ServiceRequest { req, client, timeout }).await?;
let mut res = self
.service
.call(ServiceRequest {
req,
client,
timeout,
version,
})
.await?;
match res.status() {
StatusCode::MOVED_PERMANENTLY | StatusCode::FOUND | StatusCode::SEE_OTHER => {
if method != Method::HEAD {
Expand Down
10 changes: 9 additions & 1 deletion client/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
/// builder type for [http::Request] with extended functionalities.
pub struct RequestBuilder<'a, M = marker::Http> {
pub(crate) req: http::Request<BoxBody>,
version: Option<Version>,
err: Vec<Error>,
client: &'a Client,
timeout: Duration,
Expand Down Expand Up @@ -101,6 +102,7 @@ impl<'a, M> RequestBuilder<'a, M> {
{
Self {
req: req.map(BoxBody::new),
version: None,
err: Vec::new(),
client,
timeout: client.timeout_config.request_timeout,
Expand All @@ -111,6 +113,7 @@ impl<'a, M> RequestBuilder<'a, M> {
pub(crate) fn mutate_marker<M2>(self) -> RequestBuilder<'a, M2> {
RequestBuilder {
req: self.req,
version: self.version,
err: self.err,
client: self.client,
timeout: self.timeout,
Expand All @@ -122,6 +125,7 @@ impl<'a, M> RequestBuilder<'a, M> {
pub(crate) async fn _send(self) -> Result<Response<'a>, Error> {
let Self {
mut req,
version,
err,
client,
timeout,
Expand All @@ -136,6 +140,7 @@ impl<'a, M> RequestBuilder<'a, M> {
.service
.call(ServiceRequest {
req: &mut req,
version,
client,
timeout,
})
Expand Down Expand Up @@ -172,7 +177,9 @@ impl<'a, M> RequestBuilder<'a, M> {

/// Set HTTP version of this request.
///
/// By default request's HTTP version depends on network stream
/// By default request's HTTP version will use the maximum version supported by the client.
/// If the network does not support the version, the client will downgrade to the highest
/// version supported by the network.
///
/// # Panic
/// - when received a version beyond the range crate is able to handle.
Expand All @@ -195,6 +202,7 @@ impl<'a, M> RequestBuilder<'a, M> {
/// ```
pub fn version(mut self, version: Version) -> Self {
crate::builder::version_check(version);
self.version = Some(version);
*self.req.version_mut() = version;
self
}
Expand Down
10 changes: 8 additions & 2 deletions client/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ where
/// [RequestBuilder]: crate::request::RequestBuilder
pub struct ServiceRequest<'r, 'c> {
pub req: &'r mut Request<BoxBody>,
pub version: Option<Version>,
pub client: &'c Client,
pub timeout: Duration,
}
Expand All @@ -85,14 +86,19 @@ pub(crate) fn base_service() -> HttpService {
#[cfg(any(feature = "http1", feature = "http2", feature = "http3"))]
use crate::{error::TimeoutError, timeout::Timeout};

let ServiceRequest { req, client, timeout } = req;
let ServiceRequest {
req,
version,
client,
timeout,
} = req;

let uri = Uri::try_parse(req.uri())?;

// temporary version to record possible version downgrade/upgrade happens when making connections.
// alpn protocol and alt-svc header are possible source of version change.
#[allow(unused_mut)]
let mut version = req.version();
let mut version = version.unwrap_or(client.max_http_version);

let mut connect = Connect::new(uri);

Expand Down

0 comments on commit 35eb6e6

Please sign in to comment.