diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-08-14 17:03:02 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-08-14 11:03:02 -0400 |
commit | e6c349af9f7260c2c4ec713bd231fe554240721e (patch) | |
tree | 7671dabb8270cc0c2d7f7f5a3b5f5d8d4b1e0986 /cli/ops/fetch.rs | |
parent | 58f0e9b9b1b53ca486ef38ae662b98cbde839248 (diff) |
split up ops.rs (#2753)
Note cli/dispatch_minimal.rs ops are not yet included in cli/ops.
This is part of work towards #2730
Diffstat (limited to 'cli/ops/fetch.rs')
-rw-r--r-- | cli/ops/fetch.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/cli/ops/fetch.rs b/cli/ops/fetch.rs new file mode 100644 index 000000000..e4f57972f --- /dev/null +++ b/cli/ops/fetch.rs @@ -0,0 +1,73 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +use crate::http_util; +use crate::msg; +use crate::msg_util; +use crate::ops::serialize_response; +use crate::ops::CliOpResult; +use crate::resources; +use crate::state::ThreadSafeState; +use deno::*; +use flatbuffers::FlatBufferBuilder; +use hyper; +use hyper::rt::Future; +use std; +use std::convert::From; + +pub fn op_fetch( + state: &ThreadSafeState, + base: &msg::Base<'_>, + data: Option<PinnedBuf>, +) -> 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(); + + let body = match data { + None => hyper::Body::empty(), + Some(buf) => hyper::Body::from(Vec::from(&*buf)), + }; + + let req = msg_util::deserialize_request(header, body)?; + + let url_ = url::Url::parse(url).map_err(ErrBox::from)?; + 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| { + let builder = &mut FlatBufferBuilder::new(); + let header_off = msg_util::serialize_http_response(builder, &res); + let body = res.into_body(); + let body_resource = resources::add_hyper_body(body); + let inner = msg::FetchRes::create( + builder, + &msg::FetchResArgs { + header: Some(header_off), + body_rid: body_resource.rid, + }, + ); + + Ok(serialize_response( + cmd_id, + builder, + msg::BaseArgs { + inner: Some(inner.as_union_value()), + inner_type: msg::Any::FetchRes, + ..Default::default() + }, + )) + }); + if base.sync() { + let result_buf = future.wait()?; + Ok(Op::Sync(result_buf)) + } else { + Ok(Op::Async(Box::new(future))) + } +} |