2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-03-11 18:19:24 -04:00
|
|
|
use super::io::{StreamResource, StreamResourceHolder};
|
2020-01-16 10:10:01 -05:00
|
|
|
use crate::http_util::{create_http_client, HttpBody};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-26 08:50:21 -04:00
|
|
|
use http::header::HeaderName;
|
2019-04-25 13:29:21 -04:00
|
|
|
use http::header::HeaderValue;
|
2019-08-26 08:50:21 -04:00
|
|
|
use http::Method;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::convert::From;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn init(i: &mut Isolate, s: &State) {
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_fetch", s.stateful_json_op(op_fetch));
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct FetchArgs {
|
|
|
|
method: Option<String>,
|
|
|
|
url: String,
|
|
|
|
headers: Vec<(String, String)>,
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
pub fn op_fetch(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
data: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: FetchArgs = serde_json::from_value(args)?;
|
|
|
|
let url = args.url;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-02-17 11:59:51 -05:00
|
|
|
let client =
|
|
|
|
create_http_client(state.borrow().global_state.flags.ca_file.clone())?;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
let method = match args.method {
|
2020-02-23 14:51:29 -05:00
|
|
|
Some(method_str) => Method::from_bytes(method_str.as_bytes())
|
|
|
|
.map_err(|e| OpError::other(e.to_string()))?,
|
2019-04-25 13:29:21 -04:00
|
|
|
None => Method::GET,
|
|
|
|
};
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
let url_ = url::Url::parse(&url).map_err(OpError::from)?;
|
2020-02-23 09:45:02 -05:00
|
|
|
|
|
|
|
// Check scheme before asking for net permission
|
|
|
|
let scheme = url_.scheme();
|
|
|
|
if scheme != "http" && scheme != "https" {
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(OpError::type_error(format!(
|
|
|
|
"scheme '{}' not supported",
|
|
|
|
scheme
|
|
|
|
)));
|
2020-02-23 09:45:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_net_url(&url_)?;
|
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
let mut request = client.request(method, url_);
|
|
|
|
|
|
|
|
if let Some(buf) = data {
|
|
|
|
request = request.body(Vec::from(&*buf));
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
for (key, value) in args.headers {
|
|
|
|
let name = HeaderName::from_bytes(key.as_bytes()).unwrap();
|
|
|
|
let v = HeaderValue::from_str(&value).unwrap();
|
|
|
|
request = request.header(name, v);
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("Before fetch {}", url);
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
let future = async move {
|
|
|
|
let res = request.send().await?;
|
|
|
|
debug!("Fetch response {}", url);
|
|
|
|
let status = res.status();
|
|
|
|
let mut res_headers = Vec::new();
|
|
|
|
for (key, val) in res.headers().iter() {
|
|
|
|
res_headers.push((key.to_string(), val.to_str().unwrap().to_owned()));
|
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-12-31 09:09:58 -05:00
|
|
|
let body = HttpBody::from(res);
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state_.borrow_mut();
|
|
|
|
let rid = state.resource_table.add(
|
2019-12-30 08:57:17 -05:00
|
|
|
"httpBody",
|
2020-03-11 18:19:24 -04:00
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::HttpBody(
|
|
|
|
Box::new(body),
|
|
|
|
))),
|
2019-12-30 08:57:17 -05:00
|
|
|
);
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
let json_res = json!({
|
|
|
|
"bodyRid": rid,
|
|
|
|
"status": status.as_u16(),
|
|
|
|
"statusText": status.canonical_reason().unwrap_or(""),
|
|
|
|
"headers": res_headers
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2019-04-25 13:29:21 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
Ok(json_res)
|
|
|
|
};
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(future.boxed_local()))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|