aws_smithy_runtime/client/orchestrator/
http.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use aws_smithy_runtime_api::client::orchestrator::{HttpResponse, SensitiveOutput};
7use aws_smithy_types::body::SdkBody;
8use aws_smithy_types::config_bag::ConfigBag;
9use bytes::{Buf, Bytes};
10use http_body_04x::Body;
11use pin_utils::pin_mut;
12use tracing::trace;
13
14const LOG_SENSITIVE_BODIES: &str = "LOG_SENSITIVE_BODIES";
15
16async fn body_to_bytes(body: SdkBody) -> Result<Bytes, <SdkBody as Body>::Error> {
17    let mut output = Vec::new();
18    pin_mut!(body);
19    while let Some(buf) = body.data().await {
20        let mut buf = buf?;
21        while buf.has_remaining() {
22            output.extend_from_slice(buf.chunk());
23            buf.advance(buf.chunk().len())
24        }
25    }
26
27    Ok(Bytes::from(output))
28}
29
30pub(crate) async fn read_body(response: &mut HttpResponse) -> Result<(), <SdkBody as Body>::Error> {
31    let mut body = SdkBody::taken();
32    std::mem::swap(&mut body, response.body_mut());
33
34    let bytes = body_to_bytes(body).await?;
35    let mut body = SdkBody::from(bytes);
36    std::mem::swap(&mut body, response.body_mut());
37
38    Ok(())
39}
40
41pub(crate) fn log_response_body(response: &HttpResponse, cfg: &ConfigBag) {
42    if cfg.load::<SensitiveOutput>().is_none()
43        || std::env::var(LOG_SENSITIVE_BODIES)
44            .map(|v| v.eq_ignore_ascii_case("true"))
45            .unwrap_or_default()
46    {
47        trace!(response = ?response, "read HTTP response body");
48    } else {
49        trace!(
50            response = "** REDACTED **. To print, set LOG_SENSITIVE_BODIES=true",
51            "read HTTP response body"
52        )
53    }
54}