2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-11-01 00:29:46 -04:00
|
|
|
mod fs_fetch_handler;
|
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
use data_url::DataUrl;
|
2020-09-18 09:20:55 -04:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-01-10 14:54:29 -05:00
|
|
|
use deno_core::futures::Future;
|
|
|
|
use deno_core::futures::Stream;
|
|
|
|
use deno_core::futures::StreamExt;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::include_js_files;
|
|
|
|
use deno_core::op_async;
|
|
|
|
use deno_core::op_sync;
|
2020-09-18 09:20:55 -04:00
|
|
|
use deno_core::url::Url;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2021-06-26 11:34:24 -04:00
|
|
|
use deno_core::ByteString;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2021-01-12 02:50:02 -05:00
|
|
|
use deno_core::CancelTryFuture;
|
2021-06-06 09:37:17 -04:00
|
|
|
use deno_core::Canceled;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-18 09:20:55 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-09-18 09:20:55 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2021-08-07 08:49:38 -04:00
|
|
|
use deno_tls::create_http_client;
|
|
|
|
use deno_tls::rustls::RootCertStore;
|
|
|
|
use deno_tls::Proxy;
|
2021-07-05 09:34:37 -04:00
|
|
|
use http::header::CONTENT_LENGTH;
|
2020-09-18 09:20:55 -04:00
|
|
|
use reqwest::header::HeaderName;
|
|
|
|
use reqwest::header::HeaderValue;
|
2021-06-21 23:42:04 -04:00
|
|
|
use reqwest::header::HOST;
|
2021-01-10 14:54:29 -05:00
|
|
|
use reqwest::Body;
|
2020-09-18 09:20:55 -04:00
|
|
|
use reqwest::Client;
|
|
|
|
use reqwest::Method;
|
2021-07-29 11:29:16 -04:00
|
|
|
use reqwest::RequestBuilder;
|
2020-09-18 09:20:55 -04:00
|
|
|
use reqwest::Response;
|
|
|
|
use serde::Deserialize;
|
2021-04-05 12:40:24 -04:00
|
|
|
use serde::Serialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-18 09:20:55 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::convert::From;
|
2021-02-04 13:53:02 -05:00
|
|
|
use std::path::Path;
|
2020-09-18 09:20:55 -04:00
|
|
|
use std::path::PathBuf;
|
2021-01-10 14:54:29 -05:00
|
|
|
use std::pin::Pin;
|
2020-09-18 09:20:55 -04:00
|
|
|
use std::rc::Rc;
|
2021-01-10 14:54:29 -05:00
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::sync::mpsc;
|
2021-01-12 02:50:02 -05:00
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
|
|
|
use tokio_util::io::StreamReader;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
// Re-export reqwest and data_url
|
|
|
|
pub use data_url;
|
|
|
|
pub use reqwest;
|
2020-09-18 12:31:30 -04:00
|
|
|
|
2021-11-01 00:29:46 -04:00
|
|
|
pub use fs_fetch_handler::FsFetchHandler;
|
|
|
|
|
|
|
|
pub fn init<FP, FH>(
|
2021-04-28 12:41:50 -04:00
|
|
|
user_agent: String,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: Option<RootCertStore>,
|
2021-06-21 23:21:57 -04:00
|
|
|
proxy: Option<Proxy>,
|
2021-07-29 11:29:16 -04:00
|
|
|
request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>,
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2021-08-25 08:25:12 -04:00
|
|
|
client_cert_chain_and_key: Option<(String, String)>,
|
2021-11-01 00:29:46 -04:00
|
|
|
file_fetch_handler: FH,
|
|
|
|
) -> Extension
|
|
|
|
where
|
|
|
|
FP: FetchPermissions + 'static,
|
|
|
|
FH: FetchHandler + 'static,
|
|
|
|
{
|
2021-04-28 18:16:45 -04:00
|
|
|
Extension::builder()
|
|
|
|
.js(include_js_files!(
|
2021-08-11 06:27:05 -04:00
|
|
|
prefix "deno:ext/fetch",
|
2021-04-28 12:41:50 -04:00
|
|
|
"01_fetch_util.js",
|
|
|
|
"20_headers.js",
|
|
|
|
"21_formdata.js",
|
|
|
|
"22_body.js",
|
|
|
|
"22_http_client.js",
|
|
|
|
"23_request.js",
|
|
|
|
"23_response.js",
|
|
|
|
"26_fetch.js",
|
2021-04-28 18:16:45 -04:00
|
|
|
))
|
|
|
|
.ops(vec![
|
2021-11-01 00:29:46 -04:00
|
|
|
("op_fetch", op_sync(op_fetch::<FP, FH>)),
|
2021-04-28 12:41:50 -04:00
|
|
|
("op_fetch_send", op_async(op_fetch_send)),
|
|
|
|
("op_fetch_request_write", op_async(op_fetch_request_write)),
|
|
|
|
("op_fetch_response_read", op_async(op_fetch_response_read)),
|
2021-10-31 14:14:22 -04:00
|
|
|
(
|
|
|
|
"op_fetch_custom_client",
|
2021-11-01 00:29:46 -04:00
|
|
|
op_sync(op_fetch_custom_client::<FP>),
|
2021-10-31 14:14:22 -04:00
|
|
|
),
|
2021-04-28 18:16:45 -04:00
|
|
|
])
|
|
|
|
.state(move |state| {
|
2021-04-28 12:41:50 -04:00
|
|
|
state.put::<reqwest::Client>({
|
2021-08-07 08:49:38 -04:00
|
|
|
create_http_client(
|
|
|
|
user_agent.clone(),
|
|
|
|
root_cert_store.clone(),
|
2021-09-30 03:26:15 -04:00
|
|
|
vec![],
|
2021-08-07 08:49:38 -04:00
|
|
|
proxy.clone(),
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors.clone(),
|
2021-08-25 08:25:12 -04:00
|
|
|
client_cert_chain_and_key.clone(),
|
2021-08-07 08:49:38 -04:00
|
|
|
)
|
|
|
|
.unwrap()
|
2021-04-28 12:41:50 -04:00
|
|
|
});
|
|
|
|
state.put::<HttpClientDefaults>(HttpClientDefaults {
|
|
|
|
user_agent: user_agent.clone(),
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: root_cert_store.clone(),
|
2021-06-21 23:21:57 -04:00
|
|
|
proxy: proxy.clone(),
|
2021-07-29 11:29:16 -04:00
|
|
|
request_builder_hook,
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
2021-08-25 08:25:12 -04:00
|
|
|
client_cert_chain_and_key: client_cert_chain_and_key.clone(),
|
2021-04-28 12:41:50 -04:00
|
|
|
});
|
2021-11-01 00:29:46 -04:00
|
|
|
state.put::<FH>(file_fetch_handler.clone());
|
2021-04-28 12:41:50 -04:00
|
|
|
Ok(())
|
2021-04-28 18:16:45 -04:00
|
|
|
})
|
|
|
|
.build()
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-03-18 18:54:26 -04:00
|
|
|
pub struct HttpClientDefaults {
|
|
|
|
pub user_agent: String,
|
2021-08-07 08:49:38 -04:00
|
|
|
pub root_cert_store: Option<RootCertStore>,
|
2021-06-21 23:21:57 -04:00
|
|
|
pub proxy: Option<Proxy>,
|
2021-07-29 11:29:16 -04:00
|
|
|
pub request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>,
|
2021-08-10 07:19:45 -04:00
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2021-08-25 08:25:12 -04:00
|
|
|
pub client_cert_chain_and_key: Option<(String, String)>,
|
2021-03-18 18:54:26 -04:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:29:46 -04:00
|
|
|
pub type CancelableResponseFuture =
|
|
|
|
Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
|
|
|
|
|
|
|
|
pub trait FetchHandler: Clone {
|
|
|
|
// Return the result of the fetch request consisting of a tuple of the
|
|
|
|
// cancelable response result, the optional fetch body resource and the
|
|
|
|
// optional cancel handle.
|
|
|
|
fn fetch_file(
|
|
|
|
&mut self,
|
|
|
|
url: Url,
|
|
|
|
) -> (
|
|
|
|
CancelableResponseFuture,
|
|
|
|
Option<FetchRequestBodyResource>,
|
|
|
|
Option<Rc<CancelHandle>>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A default implementation which will error for every request.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DefaultFileFetchHandler;
|
|
|
|
|
|
|
|
impl FetchHandler for DefaultFileFetchHandler {
|
|
|
|
fn fetch_file(
|
|
|
|
&mut self,
|
|
|
|
_url: Url,
|
|
|
|
) -> (
|
|
|
|
CancelableResponseFuture,
|
|
|
|
Option<FetchRequestBodyResource>,
|
|
|
|
Option<Rc<CancelHandle>>,
|
|
|
|
) {
|
|
|
|
let fut = async move {
|
|
|
|
Ok(Err(type_error(
|
|
|
|
"NetworkError when attempting to fetch resource.",
|
|
|
|
)))
|
|
|
|
};
|
|
|
|
(Box::pin(fut), None, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:20:55 -04:00
|
|
|
pub trait FetchPermissions {
|
2021-04-11 22:15:43 -04:00
|
|
|
fn check_net_url(&mut self, _url: &Url) -> Result<(), AnyError>;
|
|
|
|
fn check_read(&mut self, _p: &Path) -> Result<(), AnyError>;
|
2020-09-30 10:51:01 -04:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:20:55 -04:00
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts")
|
|
|
|
}
|
|
|
|
|
2021-03-17 17:33:29 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FetchArgs {
|
2021-06-26 11:34:24 -04:00
|
|
|
method: ByteString,
|
2021-03-17 17:33:29 -04:00
|
|
|
url: String,
|
2021-06-26 11:34:24 -04:00
|
|
|
headers: Vec<(ByteString, ByteString)>,
|
2021-03-17 17:33:29 -04:00
|
|
|
client_rid: Option<u32>,
|
|
|
|
has_body: bool,
|
2021-07-05 09:34:37 -04:00
|
|
|
body_length: Option<u64>,
|
2021-03-17 17:33:29 -04:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FetchReturn {
|
|
|
|
request_rid: ResourceId,
|
|
|
|
request_body_rid: Option<ResourceId>,
|
2021-06-06 09:37:17 -04:00
|
|
|
cancel_handle_rid: Option<ResourceId>,
|
2021-04-05 12:40:24 -04:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:29:46 -04:00
|
|
|
pub fn op_fetch<FP, FH>(
|
2021-01-10 14:54:29 -05:00
|
|
|
state: &mut OpState,
|
2021-03-17 17:33:29 -04:00
|
|
|
args: FetchArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
data: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<FetchReturn, AnyError>
|
2020-09-18 09:20:55 -04:00
|
|
|
where
|
|
|
|
FP: FetchPermissions + 'static,
|
2021-11-01 00:29:46 -04:00
|
|
|
FH: FetchHandler + 'static,
|
2020-09-18 09:20:55 -04:00
|
|
|
{
|
|
|
|
let client = if let Some(rid) = args.client_rid {
|
2021-08-15 07:29:19 -04:00
|
|
|
let r = state.resource_table.get::<HttpClientResource>(rid)?;
|
2020-09-18 09:20:55 -04:00
|
|
|
r.client.clone()
|
|
|
|
} else {
|
2021-01-10 14:54:29 -05:00
|
|
|
let client = state.borrow::<reqwest::Client>();
|
2020-09-18 09:20:55 -04:00
|
|
|
client.clone()
|
|
|
|
};
|
|
|
|
|
2021-06-26 11:34:24 -04:00
|
|
|
let method = Method::from_bytes(&args.method)?;
|
2021-04-20 08:47:22 -04:00
|
|
|
let url = Url::parse(&args.url)?;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
|
|
|
// Check scheme before asking for net permission
|
2021-01-07 13:06:08 -05:00
|
|
|
let scheme = url.scheme();
|
2021-06-06 09:37:17 -04:00
|
|
|
let (request_rid, request_body_rid, cancel_handle_rid) = match scheme {
|
2021-11-01 00:29:46 -04:00
|
|
|
"file" => {
|
|
|
|
let path = url.to_file_path().map_err(|_| {
|
|
|
|
type_error("NetworkError when attempting to fetch resource.")
|
|
|
|
})?;
|
|
|
|
let permissions = state.borrow_mut::<FP>();
|
|
|
|
permissions.check_read(&path)?;
|
|
|
|
|
|
|
|
if method != Method::GET {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"Fetching files only supports the GET method. Received {}.",
|
|
|
|
method
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let file_fetch_handler = state.borrow_mut::<FH>();
|
|
|
|
let (request, maybe_request_body, maybe_cancel_handle) =
|
|
|
|
file_fetch_handler.fetch_file(url);
|
|
|
|
let request_rid = state.resource_table.add(FetchRequestResource(request));
|
|
|
|
let maybe_request_body_rid =
|
|
|
|
maybe_request_body.map(|r| state.resource_table.add(r));
|
|
|
|
let maybe_cancel_handle_rid = maybe_cancel_handle
|
|
|
|
.map(|ch| state.resource_table.add(FetchCancelHandle(ch)));
|
|
|
|
|
|
|
|
(request_rid, maybe_request_body_rid, maybe_cancel_handle_rid)
|
|
|
|
}
|
2021-04-10 17:38:15 -04:00
|
|
|
"http" | "https" => {
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<FP>();
|
2021-04-10 17:38:15 -04:00
|
|
|
permissions.check_net_url(&url)?;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-11-09 06:10:40 -05:00
|
|
|
let mut request = client.request(method.clone(), url);
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
let request_body_rid = if args.has_body {
|
|
|
|
match data {
|
|
|
|
None => {
|
|
|
|
// If no body is passed, we return a writer for streaming the body.
|
|
|
|
let (tx, rx) = mpsc::channel::<std::io::Result<Vec<u8>>>(1);
|
2021-07-05 09:34:37 -04:00
|
|
|
|
|
|
|
// If the size of the body is known, we include a content-length
|
|
|
|
// header explicitly.
|
|
|
|
if let Some(body_size) = args.body_length {
|
|
|
|
request =
|
|
|
|
request.header(CONTENT_LENGTH, HeaderValue::from(body_size))
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
request = request.body(Body::wrap_stream(ReceiverStream::new(rx)));
|
2021-01-10 14:54:29 -05:00
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
let request_body_rid =
|
|
|
|
state.resource_table.add(FetchRequestBodyResource {
|
|
|
|
body: AsyncRefCell::new(tx),
|
|
|
|
cancel: CancelHandle::default(),
|
|
|
|
});
|
2021-01-10 14:54:29 -05:00
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
Some(request_body_rid)
|
|
|
|
}
|
|
|
|
Some(data) => {
|
|
|
|
// If a body is passed, we use it, and don't return a body for streaming.
|
|
|
|
request = request.body(Vec::from(&*data));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-09 06:10:40 -05:00
|
|
|
// 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
|
|
|
|
if matches!(method, Method::POST | Method::PUT) {
|
|
|
|
request = request.header(CONTENT_LENGTH, HeaderValue::from(0));
|
|
|
|
}
|
2021-01-10 14:54:29 -05:00
|
|
|
None
|
2021-04-10 17:38:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
for (key, value) in args.headers {
|
2021-09-29 12:42:06 -04:00
|
|
|
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()))?;
|
2021-06-21 23:42:04 -04:00
|
|
|
if name != HOST {
|
|
|
|
request = request.header(name, v);
|
|
|
|
}
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
2021-04-10 17:38:15 -04:00
|
|
|
|
2021-07-27 19:04:08 -04:00
|
|
|
let defaults = state.borrow::<HttpClientDefaults>();
|
2021-07-29 11:29:16 -04:00
|
|
|
if let Some(request_builder_hook) = defaults.request_builder_hook {
|
|
|
|
request = request_builder_hook(request);
|
2021-07-27 19:04:08 -04:00
|
|
|
}
|
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
let cancel_handle = CancelHandle::new_rc();
|
|
|
|
let cancel_handle_ = cancel_handle.clone();
|
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
let fut = async move {
|
|
|
|
request
|
|
|
|
.send()
|
|
|
|
.or_cancel(cancel_handle_)
|
|
|
|
.await
|
|
|
|
.map(|res| res.map_err(|err| type_error(err.to_string())))
|
|
|
|
};
|
2021-04-10 17:38:15 -04:00
|
|
|
|
|
|
|
let request_rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(FetchRequestResource(Box::pin(fut)));
|
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
let cancel_handle_rid =
|
|
|
|
state.resource_table.add(FetchCancelHandle(cancel_handle));
|
|
|
|
|
|
|
|
(request_rid, request_body_rid, Some(cancel_handle_rid))
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
2021-04-10 17:38:15 -04:00
|
|
|
"data" => {
|
|
|
|
let data_url = DataUrl::process(url.as_str())
|
|
|
|
.map_err(|e| type_error(format!("{:?}", e)))?;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
let (body, _) = data_url
|
|
|
|
.decode_to_vec()
|
|
|
|
.map_err(|e| type_error(format!("{:?}", e)))?;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-04-10 17:38:15 -04:00
|
|
|
let response = http::Response::builder()
|
|
|
|
.status(http::StatusCode::OK)
|
|
|
|
.header(http::header::CONTENT_TYPE, data_url.mime_type().to_string())
|
|
|
|
.body(reqwest::Body::from(body))?;
|
2021-01-10 14:54:29 -05:00
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
let fut = async move { Ok(Ok(Response::from(response))) };
|
2021-04-10 17:38:15 -04:00
|
|
|
|
2021-04-11 08:09:10 -04:00
|
|
|
let request_rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(FetchRequestResource(Box::pin(fut)));
|
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
(request_rid, None, None)
|
2021-04-11 08:09:10 -04:00
|
|
|
}
|
|
|
|
"blob" => {
|
fix: a `Request` whose URL is a revoked blob URL should still fetch (#11947)
In the spec, a URL record has an associated "blob URL entry", which for
`blob:` URLs is populated during parsing to contain a reference to the
`Blob` object that backs that object URL. It is this blob URL entry that
the `fetch` API uses to resolve an object URL.
Therefore, since the `Request` constructor parses URL inputs, it will
have an associated blob URL entry which will be used when fetching, even
if the object URL has been revoked since the construction of the
`Request` object. (The `Request` constructor takes the URL as a string
and parses it, so the object URL must be live at the time it is called.)
This PR adds a new `blobFromObjectUrl` JS function (backed by a new
`op_blob_from_object_url` op) that, if the URL is a valid object URL,
returns a new `Blob` object whose parts are references to the same Rust
`BlobPart`s used by the original `Blob` object. It uses this function to
add a new `blobUrlEntry` field to inner requests, which will be `null`
or such a `Blob`, and then uses `Blob.prototype.stream()` as the
response's body. As a result of this, the `blob:` URL resolution from
`op_fetch` is now useless, and has been removed.
2021-09-08 05:29:21 -04:00
|
|
|
// Blob URL resolution happens in the JS side of fetch. If we got here is
|
|
|
|
// because the URL isn't an object URL.
|
|
|
|
return Err(type_error("Blob for the given URL not found."));
|
2021-04-10 17:38:15 -04:00
|
|
|
}
|
|
|
|
_ => return Err(type_error(format!("scheme '{}' not supported", scheme))),
|
|
|
|
};
|
2021-01-10 14:54:29 -05:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(FetchReturn {
|
|
|
|
request_rid,
|
|
|
|
request_body_rid,
|
2021-06-06 09:37:17 -04:00
|
|
|
cancel_handle_rid,
|
2021-04-05 12:40:24 -04:00
|
|
|
})
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
2021-03-17 17:33:29 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-05 12:40:24 -04:00
|
|
|
pub struct FetchResponse {
|
|
|
|
status: u16,
|
|
|
|
status_text: String,
|
2021-06-26 11:34:24 -04:00
|
|
|
headers: Vec<(ByteString, ByteString)>,
|
2021-04-05 12:40:24 -04:00
|
|
|
url: String,
|
|
|
|
response_rid: ResourceId,
|
2021-03-17 17:33:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
pub async fn op_fetch_send(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<FetchResponse, AnyError> {
|
2021-01-10 14:54:29 -05:00
|
|
|
let request = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.take::<FetchRequestResource>(rid)?;
|
2021-01-10 14:54:29 -05:00
|
|
|
|
|
|
|
let request = Rc::try_unwrap(request)
|
|
|
|
.ok()
|
|
|
|
.expect("multiple op_fetch_send ongoing");
|
|
|
|
|
|
|
|
let res = match request.0.await {
|
2021-06-06 09:37:17 -04:00
|
|
|
Ok(Ok(res)) => res,
|
|
|
|
Ok(Err(err)) => return Err(type_error(err.to_string())),
|
|
|
|
Err(_) => return Err(type_error("request was cancelled")),
|
2020-12-26 08:06:00 -05:00
|
|
|
};
|
2020-09-18 09:20:55 -04:00
|
|
|
|
|
|
|
//debug!("Fetch response {}", url);
|
|
|
|
let status = res.status();
|
2021-01-18 07:59:29 -05:00
|
|
|
let url = res.url().to_string();
|
2020-09-18 09:20:55 -04:00
|
|
|
let mut res_headers = Vec::new();
|
|
|
|
for (key, val) in res.headers().iter() {
|
2021-06-26 11:34:24 -04:00
|
|
|
let key_bytes: &[u8] = key.as_ref();
|
|
|
|
res_headers.push((
|
|
|
|
ByteString(key_bytes.to_owned()),
|
|
|
|
ByteString(val.as_bytes().to_owned()),
|
|
|
|
));
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
let stream: BytesStream = Box::pin(res.bytes_stream().map(|r| {
|
|
|
|
r.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
|
|
|
|
}));
|
2021-01-12 02:50:02 -05:00
|
|
|
let stream_reader = StreamReader::new(stream);
|
2021-01-10 14:54:29 -05:00
|
|
|
let rid = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.add(FetchResponseBodyResource {
|
|
|
|
reader: AsyncRefCell::new(stream_reader),
|
|
|
|
cancel: CancelHandle::default(),
|
|
|
|
});
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(FetchResponse {
|
|
|
|
status: status.as_u16(),
|
|
|
|
status_text: status.canonical_reason().unwrap_or("").to_string(),
|
|
|
|
headers: res_headers,
|
|
|
|
url,
|
|
|
|
response_rid: rid,
|
|
|
|
})
|
2021-03-17 17:33:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
pub async fn op_fetch_request_write(
|
2020-09-18 09:20:55 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-10-05 16:38:27 -04:00
|
|
|
data: ZeroCopyBuf,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-10-05 16:38:27 -04:00
|
|
|
let buf = data.to_vec();
|
2021-01-10 14:54:29 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<FetchRequestBodyResource>(rid)?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let body = RcRef::map(&resource, |r| &r.body).borrow_mut().await;
|
2021-01-10 14:54:29 -05:00
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
2021-06-30 12:05:58 -04:00
|
|
|
body.send(Ok(buf)).or_cancel(cancel).await?.map_err(|_| {
|
|
|
|
type_error("request body receiver not connected (request closed)")
|
|
|
|
})?;
|
2021-01-10 14:54:29 -05:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2021-03-17 17:33:29 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
pub async fn op_fetch_response_read(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-10-05 16:38:27 -04:00
|
|
|
data: ZeroCopyBuf,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<FetchResponseBodyResource>(rid)?;
|
2021-01-10 14:54:29 -05:00
|
|
|
let mut reader = RcRef::map(&resource, |r| &r.reader).borrow_mut().await;
|
2020-12-16 11:14:12 -05:00
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
2021-04-02 09:47:57 -04:00
|
|
|
let mut buf = data.clone();
|
2021-01-12 02:50:02 -05:00
|
|
|
let read = reader.read(&mut buf).try_or_cancel(cancel).await?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(read)
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
type CancelableResponseResult = Result<Result<Response, AnyError>, Canceled>;
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
struct FetchRequestResource(
|
2021-06-06 09:37:17 -04:00
|
|
|
Pin<Box<dyn Future<Output = CancelableResponseResult>>>,
|
2021-01-10 14:54:29 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
impl Resource for FetchRequestResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"fetchRequest".into()
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
struct FetchCancelHandle(Rc<CancelHandle>);
|
|
|
|
|
|
|
|
impl Resource for FetchCancelHandle {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"fetchCancelHandle".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.0.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:29:46 -04:00
|
|
|
pub struct FetchRequestBodyResource {
|
2021-01-10 14:54:29 -05:00
|
|
|
body: AsyncRefCell<mpsc::Sender<std::io::Result<Vec<u8>>>>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for FetchRequestBodyResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"fetchRequestBody".into()
|
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel()
|
|
|
|
}
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type BytesStream =
|
|
|
|
Pin<Box<dyn Stream<Item = Result<bytes::Bytes, std::io::Error>> + Unpin>>;
|
|
|
|
|
|
|
|
struct FetchResponseBodyResource {
|
|
|
|
reader: AsyncRefCell<StreamReader<BytesStream, bytes::Bytes>>,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:54:29 -05:00
|
|
|
impl Resource for FetchResponseBodyResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
fn name(&self) -> Cow<str> {
|
2021-01-10 14:54:29 -05:00
|
|
|
"fetchResponseBody".into()
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel()
|
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HttpClientResource {
|
|
|
|
client: Client,
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for HttpClientResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"httpClient".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:20:55 -04:00
|
|
|
impl HttpClientResource {
|
|
|
|
fn new(client: Client) -> Self {
|
|
|
|
Self { client }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 03:26:15 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
2021-03-17 17:33:29 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreateHttpClientOptions {
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs: Vec<String>,
|
2021-06-21 23:21:57 -04:00
|
|
|
proxy: Option<Proxy>,
|
2021-08-25 08:25:12 -04:00
|
|
|
cert_chain: Option<String>,
|
|
|
|
private_key: Option<String>,
|
2021-06-21 23:21:57 -04:00
|
|
|
}
|
|
|
|
|
2021-10-31 14:14:22 -04:00
|
|
|
pub fn op_fetch_custom_client<FP>(
|
2020-09-18 09:20:55 -04:00
|
|
|
state: &mut OpState,
|
2021-03-17 17:33:29 -04:00
|
|
|
args: CreateHttpClientOptions,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError>
|
2020-09-18 09:20:55 -04:00
|
|
|
where
|
|
|
|
FP: FetchPermissions + 'static,
|
|
|
|
{
|
2021-06-21 23:21:57 -04:00
|
|
|
if let Some(proxy) = args.proxy.clone() {
|
|
|
|
let permissions = state.borrow_mut::<FP>();
|
|
|
|
let url = Url::parse(&proxy.url)?;
|
|
|
|
permissions.check_net_url(&url)?;
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:25:12 -04:00
|
|
|
let client_cert_chain_and_key = {
|
|
|
|
if args.cert_chain.is_some() || args.private_key.is_some() {
|
|
|
|
let cert_chain = args
|
|
|
|
.cert_chain
|
|
|
|
.ok_or_else(|| type_error("No certificate chain provided"))?;
|
|
|
|
let private_key = args
|
|
|
|
.private_key
|
|
|
|
.ok_or_else(|| type_error("No private key provided"))?;
|
|
|
|
|
|
|
|
Some((cert_chain, private_key))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-18 18:54:26 -04:00
|
|
|
let defaults = state.borrow::<HttpClientDefaults>();
|
2021-09-30 03:26:15 -04:00
|
|
|
let ca_certs = args
|
|
|
|
.ca_certs
|
|
|
|
.into_iter()
|
|
|
|
.map(|cert| cert.into_bytes())
|
|
|
|
.collect::<Vec<_>>();
|
2021-08-09 10:53:21 -04:00
|
|
|
|
2021-03-18 18:54:26 -04:00
|
|
|
let client = create_http_client(
|
|
|
|
defaults.user_agent.clone(),
|
2021-08-07 08:49:38 -04:00
|
|
|
defaults.root_cert_store.clone(),
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs,
|
2021-06-21 23:21:57 -04:00
|
|
|
args.proxy,
|
2021-08-10 07:19:45 -04:00
|
|
|
defaults.unsafely_ignore_certificate_errors.clone(),
|
2021-08-25 08:25:12 -04:00
|
|
|
client_cert_chain_and_key,
|
|
|
|
)?;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(HttpClientResource::new(client));
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|