Skip to content

Commit

Permalink
Ensure http-body-0-4 is not inadvertently publicly exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Dec 11, 2023
1 parent 0dfc603 commit 36c60e1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
8 changes: 5 additions & 3 deletions rust-runtime/aws-smithy-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/smithy-lang/smithy-rs"

# DO NOT USE `dep:http-body-0-4` in this table.
# Doing so disables the implicit feature which we rely on internally.
[features]
byte-stream-poll-next = []
http-body-0-4-x = ["dep:http-body-0-4"]
http-body-1-x = ["dep:http-body-1-0", "dep:http-body-util", "http-body-0-4-x"]
http-body-0-4-x = ["http-body-0-4"]
http-body-1-x = ["dep:http-body-1-0", "dep:http-body-util", "http-body-0-4"]
hyper-0-14-x = ["dep:hyper-0-14"]
rt-tokio = [
"http-body-0-4-x",
"http-body-0-4",
"dep:tokio-util",
"dep:tokio",
"tokio?/rt",
Expand Down
3 changes: 3 additions & 0 deletions rust-runtime/aws-smithy-types/additional-ci
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ cargo tree -d --edges normal --all-features

echo "### Checking whether the features are properly feature-gated"
! cargo tree -e no-dev | grep serde

echo "### Checking feature powerset"
cargo hack check --feature-powerset --exclude-all-features
21 changes: 20 additions & 1 deletion rust-runtime/aws-smithy-types/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ impl Debug for SdkBody {

/// A boxed generic HTTP body that, when consumed, will result in [`Bytes`] or an [`Error`].
enum BoxBody {
#[cfg(feature = "http-body-0-4-x")]
// This is enabled by the **dependency**, not the feature. This allows us to construct it
// whenever we have the dependency and keep the APIs private
#[cfg(feature = "http-body-0-4")]
HttpBody04(http_body_0_4::combinators::BoxBody<Bytes, Error>),
}

Expand Down Expand Up @@ -164,6 +166,23 @@ impl SdkBody {
}
}

#[cfg(feature = "http-body-0-4")]
pub(crate) fn from_body_0_4_internal<T, E>(body: T) -> Self
where
T: http_body_0_4::Body<Data = Bytes, Error = E> + Send + Sync + 'static,
E: Into<Error> + 'static,
{
Self {
inner: Inner::Dyn {
inner: BoxBody::HttpBody04(http_body_0_4::combinators::BoxBody::new(
body.map_err(Into::into),
)),
},
rebuild: None,
bytes_contents: None,
}
}

#[cfg(feature = "http-body-0-4-x")]
pub(crate) fn poll_next_trailers(
self: Pin<&mut Self>,
Expand Down
16 changes: 5 additions & 11 deletions rust-runtime/aws-smithy-types/src/body/http_body_0_4_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

use crate::body::{BoxBody, Error, Inner, SdkBody};
use bytes::Bytes;
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;

use crate::body::{Error, SdkBody};

impl SdkBody {
/// Construct an `SdkBody` from a type that implements [`http_body_0_4::Body<Data = Bytes>`](http_body_0_4::Body).
///
Expand All @@ -17,15 +19,7 @@ impl SdkBody {
T: http_body_0_4::Body<Data = Bytes, Error = E> + Send + Sync + 'static,
E: Into<Error> + 'static,
{
Self {
inner: Inner::Dyn {
inner: BoxBody::HttpBody04(http_body_0_4::combinators::BoxBody::new(
body.map_err(Into::into),
)),
},
rebuild: None,
bytes_contents: None,
}
SdkBody::from_body_0_4_internal(body)
}
}

Expand Down
49 changes: 15 additions & 34 deletions rust-runtime/aws-smithy-types/src/body/http_body_1_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,18 @@ use bytes::Bytes;
use http_body_util::BodyExt;
use pin_project_lite::pin_project;

use crate::body::{BoxBody, Error, Inner, SdkBody};
use crate::body::{Error, SdkBody};

impl SdkBody {
/// Construct an `SdkBody` from a type that implements [`http_body_1_0::Body<Data = Bytes>`](http_body_1_0::Body).
///
/// _Note: This is only available with `http-body-1-0` enabled._
pub fn from_body_1_0<T, E>(body: T) -> Self
pub fn from_body_1_x<T, E>(body: T) -> Self
where
T: http_body_1_0::Body<Data = Bytes, Error = E> + Send + Sync + 'static,
E: Into<Error> + 'static,
{
Self {
inner: Inner::Dyn {
inner: BoxBody::HttpBody04(http_body_0_4::combinators::BoxBody::new(
Http1toHttp04::new(body.map_err(Into::into)),
)),
},
rebuild: None,
bytes_contents: None,
}
SdkBody::from_body_0_4_internal(Http1toHttp04::new(body.map_err(Into::into)))
}
}

Expand Down Expand Up @@ -59,19 +51,6 @@ where
type Data = B::Data;
type Error = B::Error;

///
///
/// # Arguments
///
/// * `cx`:
///
/// returns: Poll<Option<Result<<Http1toHttp04<B> as Body>::Data, <Http1toHttp04<B> as Body>::Error>>>
///
/// # Examples
///
/// ```
///
/// ```
fn poll_data(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -146,17 +125,19 @@ fn convert_header_map(input: http_1x::HeaderMap) -> http::HeaderMap {

#[cfg(test)]
mod test {
use crate::body::http_body_1_x::convert_header_map;
use crate::body::{Error, SdkBody};
use crate::byte_stream::ByteStream;
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;
use http::header::{CONTENT_LENGTH as CL0, CONTENT_TYPE as CT0};
use http_1x::header::{CONTENT_LENGTH as CL1, CONTENT_TYPE as CT1};
use http_1x::{HeaderMap, HeaderName, HeaderValue};
use http_body_1_0::Frame;
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::body::http_body_1_x::convert_header_map;
use crate::body::{Error, SdkBody};
use crate::byte_stream::ByteStream;

struct TestBody {
chunks: VecDeque<Chunk>,
Expand Down Expand Up @@ -216,7 +197,7 @@ mod test {
]
.into(),
};
let body = SdkBody::from_body_1_0(body);
let body = SdkBody::from_body_1_x(body);
let data = ByteStream::new(body);
assert_eq!(data.collect().await.unwrap().to_vec(), b"123456789");
}
Expand All @@ -232,7 +213,7 @@ mod test {
]
.into(),
};
let mut body = SdkBody::from_body_1_0(body);
let mut body = SdkBody::from_body_1_x(body);
while let Some(_data) = http_body_0_4::Body::data(&mut body).await {}
assert_eq!(
http_body_0_4::Body::trailers(&mut body).await.unwrap(),
Expand All @@ -252,7 +233,7 @@ mod test {
.into(),
};

let body = SdkBody::from_body_1_0(body);
let body = SdkBody::from_body_1_x(body);
let body = ByteStream::new(body);
body.collect().await.expect_err("body returned an error");
}
Expand All @@ -262,7 +243,7 @@ mod test {
chunks: vec![Chunk::Data("123"), Chunk::Data("456"), Chunk::Data("789")].into(),
};

let body = SdkBody::from_body_1_0(body);
let body = SdkBody::from_body_1_x(body);
let body = ByteStream::new(body);
assert_eq!(body.collect().await.unwrap().to_vec(), b"123456789");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl FsBuilder {
let body_loader = move || {
// If an offset was provided, seeking will be handled in `PathBody::poll_data` each
// time the file is loaded.
SdkBody::from_body_0_4(PathBody::from_path(
SdkBody::from_body_0_4_internal(PathBody::from_path(
path.clone(),
length,
buffer_size,
Expand All @@ -208,7 +208,8 @@ impl FsBuilder {
let _s = file.seek(io::SeekFrom::Start(offset)).await?;
}

let body = SdkBody::from_body_0_4(PathBody::from_file(file, length, buffer_size));
let body =
SdkBody::from_body_0_4_internal(PathBody::from_file(file, length, buffer_size));

Ok(ByteStream::new(body))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ impl ByteStream {
T: http_body_1_0::Body<Data = Bytes, Error = E> + Send + Sync + 'static,
E: Into<crate::body::Error> + 'static,
{
ByteStream::new(SdkBody::from_body_1_0(body))
ByteStream::new(SdkBody::from_body_1_x(body))
}
}

0 comments on commit 36c60e1

Please sign in to comment.