Skip to content

Commit

Permalink
feat: allow fetch to be initialized with user-provided client
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyChan-okta committed Oct 30, 2024
1 parent 1b96787 commit 39e6fef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
26 changes: 19 additions & 7 deletions modules/llrt_http/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::convert::Infallible;
use std::{collections::HashSet, time::Instant};

use bytes::Bytes;
use http_body_util::combinators::BoxBody;
use http_body_util::Full;
use hyper::{header::HeaderName, Method, Request, Uri};
use hyper_rustls::HttpsConnector;
use hyper_util::client::legacy::connect::{Connect, HttpConnector};
use hyper_util::client::legacy::Client;
use llrt_abort::AbortSignal;
use llrt_utils::{
bytes::ObjectBytes, encoding::bytes_from_b64, mc_oneshot, result::ResultExt, VERSION,
Expand All @@ -23,13 +28,18 @@ use super::{

const MAX_REDIRECT_COUNT: u32 = 20;

pub(crate) fn init(ctx: &Ctx<'_>, globals: &Object) -> Result<()> {
//init eagerly
let client = HTTP_CLIENT.as_ref().or_throw(ctx)?;

pub(crate) fn init<C>(
client: Client<C, BoxBody<Bytes, Infallible>>,
ctx: &Ctx<'_>,
globals: &Object,
) -> Result<()>
where
C: Clone + Send + Sync + Connect + 'static,
{
globals.set(
"fetch",
Func::from(Async(move |ctx, resource, args| {
let client = client.clone();
let start = Instant::now();
let options = get_fetch_options(&ctx, resource, args);

Expand Down Expand Up @@ -162,7 +172,7 @@ fn build_request(
body: Option<&BodyBytes>,
prev_status: &u16,
initial_uri: &Uri,
) -> Result<Request<Full<Bytes>>> {
) -> Result<Request<BoxBody<Bytes, Infallible>>> {
let same_origin = is_same_origin(uri, initial_uri);

let change_method = should_change_method(*prev_status, method);
Expand Down Expand Up @@ -200,8 +210,10 @@ fn build_request(
req = req.header("accept", "*/*");
}

req.body(body.map(|b| b.body.clone()).unwrap_or_default())
.or_throw(ctx)
req.body(BoxBody::new(
body.map(|b| b.body.clone()).unwrap_or_default(),
))
.or_throw(ctx)
}

fn is_same_origin(uri: &Uri, initial_uri: &Uri) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion modules/llrt_http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::{io, sync::OnceLock, time::Duration};
use std::convert::Infallible;

use bytes::Bytes;
use http_body_util::combinators::BoxBody;
use http_body_util::Full;
use hyper_rustls::HttpsConnector;
use hyper_util::{
Expand All @@ -16,6 +18,7 @@ use rustls::{
crypto::ring, pki_types::CertificateDer, ClientConfig, RootCertStore, SupportedProtocolVersion,
};
use webpki_roots::TLS_SERVER_ROOTS;
use llrt_utils::result::ResultExt;

pub use self::security::{get_allow_list, get_deny_list, set_allow_list, set_deny_list};
use self::{file::File, headers::Headers, request::Request, response::Response};
Expand Down Expand Up @@ -146,7 +149,8 @@ pub static HTTP_CLIENT: Lazy<io::Result<Client<HttpsConnector<HttpConnector>, Fu
pub fn init(ctx: &Ctx) -> Result<()> {
let globals = ctx.globals();

fetch::init(ctx, &globals)?;
//init eagerly
fetch::init(HTTP_CLIENT.as_ref().or_throw(&ctx)?.clone(), ctx, &globals)?;

Class::<Request>::define(&globals)?;
Class::<Response>::define(&globals)?;
Expand Down

0 comments on commit 39e6fef

Please sign in to comment.