2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-24 16:20:48 -04:00
|
|
|
use super::dispatch_flatbuffers::serialize_response;
|
|
|
|
use super::utils::CliOpResult;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::http_util;
|
2019-08-24 16:20:48 -04:00
|
|
|
use crate::msg;
|
|
|
|
use crate::msg_util;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::resources;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
2019-08-24 16:20:48 -04:00
|
|
|
use flatbuffers::FlatBufferBuilder;
|
2019-08-14 11:03:02 -04:00
|
|
|
use hyper;
|
|
|
|
use hyper::rt::Future;
|
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
|
|
|
|
|
|
|
pub fn op_fetch(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-24 16:20:48 -04:00
|
|
|
base: &msg::Base<'_>,
|
2019-08-14 11:03:02 -04:00
|
|
|
data: Option<PinnedBuf>,
|
2019-08-24 16:20:48 -04:00
|
|
|
) -> CliOpResult {
|
|
|
|
let inner = base.inner_as_fetch().unwrap();
|
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
|
|
|
|
let header = inner.header().unwrap();
|
|
|
|
assert!(header.is_request());
|
|
|
|
let url = header.url().unwrap();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
let body = match data {
|
|
|
|
None => hyper::Body::empty(),
|
|
|
|
Some(buf) => hyper::Body::from(Vec::from(&*buf)),
|
|
|
|
};
|
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
let req = msg_util::deserialize_request(header, body)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
let url_ = url::Url::parse(url).map_err(ErrBox::from)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_net_url(&url_)?;
|
|
|
|
|
|
|
|
let client = http_util::get_client();
|
|
|
|
|
|
|
|
debug!("Before fetch {}", url);
|
|
|
|
let future = client
|
|
|
|
.request(req)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |res| {
|
2019-08-24 16:20:48 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let header_off = msg_util::serialize_http_response(builder, &res);
|
2019-08-14 11:03:02 -04:00
|
|
|
let body = res.into_body();
|
|
|
|
let body_resource = resources::add_hyper_body(body);
|
2019-08-24 16:20:48 -04:00
|
|
|
let inner = msg::FetchRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::FetchResArgs {
|
|
|
|
header: Some(header_off),
|
|
|
|
body_rid: body_resource.rid,
|
|
|
|
},
|
|
|
|
);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::FetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2019-08-14 11:03:02 -04:00
|
|
|
});
|
2019-08-24 16:20:48 -04:00
|
|
|
if base.sync() {
|
|
|
|
let result_buf = future.wait()?;
|
|
|
|
Ok(Op::Sync(result_buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(future)))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|