2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-05-16 19:20:32 -04:00
|
|
|
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-10-07 11:34:03 -04:00
|
|
|
use deno_core::op2;
|
2023-05-16 19:20:32 -04:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::ByteString;
|
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::OpState;
|
2023-12-01 10:56:10 -05:00
|
|
|
use deno_core::ResourceId;
|
2023-05-16 19:20:32 -04:00
|
|
|
use deno_fetch::get_or_create_client_from_state;
|
|
|
|
use deno_fetch::FetchCancelHandle;
|
|
|
|
use deno_fetch::FetchRequestResource;
|
|
|
|
use deno_fetch::FetchReturn;
|
|
|
|
use deno_fetch::HttpClientResource;
|
2023-12-01 10:56:10 -05:00
|
|
|
use deno_fetch::ResourceToBodyAdapter;
|
2024-07-17 19:37:31 -04:00
|
|
|
use http::header::HeaderMap;
|
|
|
|
use http::header::HeaderName;
|
|
|
|
use http::header::HeaderValue;
|
|
|
|
use http::header::CONTENT_LENGTH;
|
|
|
|
use http::Method;
|
|
|
|
use http_body_util::BodyExt;
|
2023-05-16 19:20:32 -04:00
|
|
|
|
2023-10-07 11:34:03 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2023-05-27 09:42:20 -04:00
|
|
|
pub fn op_node_http_request<P>(
|
2023-05-16 19:20:32 -04:00
|
|
|
state: &mut OpState,
|
2023-10-07 11:34:03 -04:00
|
|
|
#[serde] method: ByteString,
|
|
|
|
#[string] url: String,
|
|
|
|
#[serde] headers: Vec<(ByteString, ByteString)>,
|
|
|
|
#[smi] client_rid: Option<u32>,
|
2023-12-01 10:56:10 -05:00
|
|
|
#[smi] body: Option<ResourceId>,
|
2023-05-27 09:42:20 -04:00
|
|
|
) -> Result<FetchReturn, AnyError>
|
|
|
|
where
|
|
|
|
P: crate::NodePermissions + 'static,
|
|
|
|
{
|
2023-05-16 19:20:32 -04:00
|
|
|
let client = if let Some(rid) = client_rid {
|
|
|
|
let r = state.resource_table.get::<HttpClientResource>(rid)?;
|
|
|
|
r.client.clone()
|
|
|
|
} else {
|
|
|
|
get_or_create_client_from_state(state)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let method = Method::from_bytes(&method)?;
|
|
|
|
let url = Url::parse(&url)?;
|
|
|
|
|
2023-05-27 09:42:20 -04:00
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
|
|
|
permissions.check_net_url(&url, "ClientRequest")?;
|
|
|
|
}
|
|
|
|
|
2023-05-16 19:20:32 -04:00
|
|
|
let mut header_map = HeaderMap::new();
|
|
|
|
for (key, value) in headers {
|
|
|
|
let name = HeaderName::from_bytes(&key)
|
|
|
|
.map_err(|err| type_error(err.to_string()))?;
|
|
|
|
let v = HeaderValue::from_bytes(&value)
|
|
|
|
.map_err(|err| type_error(err.to_string()))?;
|
|
|
|
|
|
|
|
header_map.append(name, v);
|
|
|
|
}
|
|
|
|
|
2024-07-17 19:37:31 -04:00
|
|
|
let (body, con_len) = if let Some(body) = body {
|
|
|
|
(
|
|
|
|
ResourceToBodyAdapter::new(state.resource_table.take_any(body)?).boxed(),
|
|
|
|
None,
|
|
|
|
)
|
2023-05-16 19:20:32 -04:00
|
|
|
} else {
|
|
|
|
// POST and PUT requests should always have a 0 length content-length,
|
|
|
|
// if there is no body. https://fetch.spec.whatwg.org/#http-network-or-cache-fetch
|
2024-07-17 19:37:31 -04:00
|
|
|
let len = if matches!(method, Method::POST | Method::PUT) {
|
|
|
|
Some(0)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
(
|
|
|
|
http_body_util::Empty::new()
|
|
|
|
.map_err(|never| match never {})
|
|
|
|
.boxed(),
|
|
|
|
len,
|
|
|
|
)
|
2023-05-16 19:20:32 -04:00
|
|
|
};
|
|
|
|
|
2024-07-17 19:37:31 -04:00
|
|
|
let mut request = http::Request::new(body);
|
|
|
|
*request.method_mut() = method.clone();
|
|
|
|
*request.uri_mut() = url
|
|
|
|
.as_str()
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| type_error("Invalid URL"))?;
|
|
|
|
*request.headers_mut() = header_map;
|
|
|
|
|
|
|
|
if let Some(len) = con_len {
|
|
|
|
request.headers_mut().insert(CONTENT_LENGTH, len.into());
|
|
|
|
}
|
|
|
|
|
2023-05-16 19:20:32 -04:00
|
|
|
let cancel_handle = CancelHandle::new_rc();
|
|
|
|
let cancel_handle_ = cancel_handle.clone();
|
|
|
|
|
|
|
|
let fut = async move {
|
2024-07-17 19:37:31 -04:00
|
|
|
client
|
|
|
|
.send(request)
|
2023-05-16 19:20:32 -04:00
|
|
|
.or_cancel(cancel_handle_)
|
|
|
|
.await
|
|
|
|
.map(|res| res.map_err(|err| type_error(err.to_string())))
|
|
|
|
};
|
|
|
|
|
2024-07-17 19:37:31 -04:00
|
|
|
let request_rid = state.resource_table.add(FetchRequestResource {
|
|
|
|
future: Box::pin(fut),
|
|
|
|
url,
|
|
|
|
});
|
2023-05-16 19:20:32 -04:00
|
|
|
|
|
|
|
let cancel_handle_rid =
|
|
|
|
state.resource_table.add(FetchCancelHandle(cancel_handle));
|
|
|
|
|
|
|
|
Ok(FetchReturn {
|
|
|
|
request_rid,
|
|
|
|
cancel_handle_rid: Some(cancel_handle_rid),
|
|
|
|
})
|
|
|
|
}
|