Unverified Commit 7dfd6097 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Add support for constructing sdk body types from http-body 1.0 (#3300)



## Motivation and Context
- aws-sdk-rust#977


## Description
The first of several PRs to make add support for Hyper 1.0. This minimal
change allows the use of Hyper 1.0 bodies although it does not actually
leverage Hyper 1.0.

## Testing
- Unit test suite
- Ran cargo hack check --feature-powerset because we had a lot of
features. I found a couple of issues.


## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent e42d8cd9
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -97,3 +97,15 @@ message = "[`Number`](https://docs.rs/aws-smithy-types/latest/aws_smithy_types/e
references = ["smithy-rs#3294"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all" }
author = "rcoh"

[[smithy-rs]]
message = "Add support for constructing [`SdkBody`] and [`ByteStream`] from `http-body` 1.0 bodies. Note that this is initial support and works via a backwards compatibility shim to http-body 0.4. Hyper 1.0 is not supported."
references = ["smithy-rs#3300", "aws-sdk-rust#977"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "all" }
author = "rcoh"

[[aws-sdk-rust]]
message = "Add support for constructing [`SdkBody`] and [`ByteStream`] from `http-body` 1.0 bodies. Note that this is initial support and works via a backwards compatibility shim to http-body 0.4. Hyper 1.0 is not supported."
references = ["smithy-rs#3300", "aws-sdk-rust#977"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "rcoh"
+4 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ repository = "https://github.com/smithy-lang/smithy-rs"
[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", "dep:http-body-0-4", "dep:http-1x"]
hyper-0-14-x = ["dep:hyper-0-14"]
rt-tokio = [
    "dep:http-body-0-4",
@@ -32,7 +33,10 @@ base64-simd = "0.8"
bytes = "1"
bytes-utils = "0.1"
http = "0.2.3"
http-1x = { package = "http", version = "1", optional = true }
http-body-0-4 = { package = "http-body", version = "0.4.4", optional = true }
http-body-1-0 = { package = "http-body", version = "1", optional = true }
http-body-util = { version = "0.1.0", optional = true }
hyper-0-14 = { package = "hyper", version = "0.14.26", optional = true }
itoa = "1.0.0"
num-integer = "0.1.44"
+3 −0
Original line number Diff line number Diff line
@@ -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
+30 −1
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ use std::task::{Context, Poll};
/// The name has a suffix `_x` to avoid name collision with a third-party `http-body-0-4`.
#[cfg(feature = "http-body-0-4-x")]
pub mod http_body_0_4_x;
#[cfg(feature = "http-body-1-x")]
pub mod http_body_1_x;

/// A generic, boxed error that's `Send` and `Sync`
pub type Error = Box<dyn StdError + Send + Sync>;
@@ -55,7 +57,13 @@ 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(any(
        feature = "http-body-0-4-x",
        feature = "http-body-1-x",
        feature = "rt-tokio"
    ))]
    HttpBody04(http_body_0_4::combinators::BoxBody<Bytes, Error>),
}

@@ -162,6 +170,27 @@ impl SdkBody {
        }
    }

    #[cfg(any(
        feature = "http-body-0-4-x",
        feature = "http-body-1-x",
        feature = "rt-tokio"
    ))]
    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>,
+5 −11
Original line number Diff line number Diff line
@@ -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).
    ///
@@ -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)
    }
}

Loading