Repository: awslabs/llrt
File: modules/llrt_fetch/src/lib.rs (Rust)

Code before:
        {
            HttpVersion::Http1_1
        }
        #[cfg(all(not(feature = "http1"), feature = "http2"))]
        {
            HttpVersion::Http2
        }
        #[cfg(all(not(feature = "http2"), feature = "http1"))]
        {
            HttpVersion::Http1_1
        }
        #[cfg(all(not(feature = "http2"), not(feature = "http1")))]
        {
            compile_error!("Either the `http1` or `http2` feature must be enabled")
        }
    })
}

pub(crate) fn strip_bom(bytes: &[u8]) -> &[u8] {
    if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
        &bytes[3..]
    } else {
        bytes
    }
}

pub type HyperClient =
    Client<HttpsConnector<HttpConnector<CachedDnsResolver>>, BoxBody<Bytes, Infallible>>;
pub static HTTP_CLIENT: Lazy<io::Result<HyperClient>> = Lazy::new(|| {
    let pool_idle_timeout = get_pool_idle_timeout();

    let maybe_tls_config = match &*TLS_CONFIG {
        Ok(tls_config) => io::Result::Ok(tls_config.clone()),
        Err(e) => io::Result::Err(io::Error::new(e.kind(), e.to_string())),
    };

    let builder = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(maybe_tls_config?)
        .https_or_http();

    let mut cache_dns_connector = CachedDnsResolver::new().into_http_connector();
    cache_dns_connector.enforce_http(false);

    let https = match get_http_version() {
        #[cfg(feature = "http2")]
        HttpVersion::Http2 => builder
            .enable_all_versions()
            .wrap_connector(cache_dns_connector),
        _ => builder.enable_http1().wrap_connector(cache_dns_connector),
    };


Review comment (suggestion):
This should return a Cow instead. If bytes are unmodified we don't need the additional copy. Something like:
```rust
pub(crate) fn strip_bom(bytes: impl Into<Cow<'_, [u8]>>) -> Cow<[u8]> {
    let cow = bytes.into();
    if cow.starts_with(&[0xEF, 0xBB, 0xBF]) {
        Cow::Borrowed(&cow[3..])
    } else {
        cow
    }
}
```

Code after:
        #[cfg(all(feature = "http2", feature = "http1"))]
        {
            HttpVersion::Http1_1
        }
        #[cfg(all(not(feature = "http1"), feature = "http2"))]
        {
            HttpVersion::Http2
        }
        #[cfg(all(not(feature = "http2"), feature = "http1"))]
        {
            HttpVersion::Http1_1
        }
        #[cfg(all(not(feature = "http2"), not(feature = "http1")))]
        {
            compile_error!("Either the `http1` or `http2` feature must be enabled")
        }
    })
}

pub(crate) fn strip_bom<'a>(bytes: impl Into<Cow<'a, [u8]>>) -> Cow<'a, [u8]> {
    let cow = bytes.into();
    if cow.starts_with(&[0xEF, 0xBB, 0xBF]) {
        match cow {
            Cow::Borrowed(bytes) => Cow::Borrowed(&bytes[3..]),
            Cow::Owned(mut bytes) => {
                bytes.drain(0..3); //memmove instead of copy
                Cow::Owned(bytes)
            },
        }
    } else {
        cow
    }
}

pub type HyperClient =
    Client<HttpsConnector<HttpConnector<CachedDnsResolver>>, BoxBody<Bytes, Infallible>>;
pub static HTTP_CLIENT: Lazy<io::Result<HyperClient>> = Lazy::new(|| {
    let pool_idle_timeout = get_pool_idle_timeout();

    let maybe_tls_config = match &*TLS_CONFIG {
        Ok(tls_config) => io::Result::Ok(tls_config.clone()),
        Err(e) => io::Result::Err(io::Error::new(e.kind(), e.to_string())),
    };

    let builder = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(maybe_tls_config?)
        .https_or_http();

    let mut cache_dns_connector = CachedDnsResolver::new().into_http_connector();
    cache_dns_connector.enforce_http(false);