Skip to content

Commit

Permalink
port cli/ops/fetch.rs to json ops
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 23, 2019
1 parent 7d92640 commit a1fe53a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 263 deletions.
1 change: 0 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ mod http_body;
mod http_util;
mod import_map;
pub mod msg;
pub mod msg_util;
pub mod ops;
pub mod permissions;
mod progress;
Expand Down
25 changes: 0 additions & 25 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ union Any {
Cwd,
CwdRes,
Dial,
Fetch,
FetchRes,
GetRandomValues,
GlobalTimer,
GlobalTimerRes,
Expand Down Expand Up @@ -267,29 +265,6 @@ table PermissionsRes {
hrtime: bool;
}

// Note this represents The WHOLE header of an http message, not just the key
// value pairs. That means it includes method and url for Requests and status
// for responses. This is why it is singular "Header" instead of "Headers".
table HttpHeader {
is_request: bool;
// Request only:
method: string;
url: string;
// Response only:
status: uint16;
// Both:
fields: [KeyValue];
}

table Fetch {
header: HttpHeader;
}

table FetchRes {
header: HttpHeader;
body_rid: uint32;
}

table MakeTempDir {
dir: string;
prefix: string;
Expand Down
124 changes: 0 additions & 124 deletions cli/msg_util.rs

This file was deleted.

8 changes: 4 additions & 4 deletions cli/ops/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use deno::*;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Cache {
struct CacheArgs {
module_id: String,
contents: String,
extension: String,
Expand All @@ -17,7 +17,7 @@ pub fn op_cache(
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: Cache = serde_json::from_value(args)?;
let args: CacheArgs = serde_json::from_value(args)?;

let module_specifier = ModuleSpecifier::resolve_url(&args.module_id)
.expect("Should be valid module specifier");
Expand All @@ -32,7 +32,7 @@ pub fn op_cache(
}

#[derive(Deserialize)]
struct FetchSourceFile {
struct FetchSourceFileArgs {
specifier: String,
referrer: String,
}
Expand All @@ -42,7 +42,7 @@ pub fn op_fetch_source_file(
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: FetchSourceFile = serde_json::from_value(args)?;
let args: FetchSourceFileArgs = serde_json::from_value(args)?;

// TODO(ry) Maybe a security hole. Only the compiler worker should have access
// to this. Need a test to demonstrate the hole.
Expand Down
2 changes: 0 additions & 2 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use deno::*;
use flatbuffers::FlatBufferBuilder;
use hyper::rt::Future;

use super::fetch::op_fetch;
use super::files::{op_read, op_write};
use super::fs::{
op_chdir, op_chmod, op_chown, op_copy_file, op_cwd, op_link,
Expand Down Expand Up @@ -148,7 +147,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::CreateWorker => Some(op_create_worker),
msg::Any::Cwd => Some(op_cwd),
msg::Any::Dial => Some(op_dial),
msg::Any::Fetch => Some(op_fetch),
msg::Any::GetRandomValues => Some(op_get_random_values),
msg::Any::GlobalTimer => Some(op_global_timer),
msg::Any::GlobalTimerStop => Some(op_global_timer_stop),
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use deno::*;
use std::collections::HashMap;

#[derive(Deserialize)]
struct FormatError {
struct FormatErrorArgs {
error: String,
}

Expand All @@ -17,7 +17,7 @@ pub fn op_format_error(
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: FormatError = serde_json::from_value(args)?;
let args: FormatErrorArgs = serde_json::from_value(args)?;
let error = JSError::from_json(&args.error, &state.ts_compiler);

Ok(JsonOp::Sync(json!({
Expand Down
87 changes: 48 additions & 39 deletions cli/ops/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_flatbuffers::serialize_response;
use super::utils::CliOpResult;
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::http_util;
use crate::msg;
use crate::msg_util;
use crate::resources;
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use http::header::HeaderName;
use http::uri::Uri;
use http::Method;
use hyper;
use hyper::header::HeaderValue;
use hyper::rt::Future;
use hyper::Request;
use std;
use std::convert::From;
use std::str::FromStr;

#[derive(Deserialize)]
struct FetchArgs {
method: Option<String>,
url: String,
headers: Vec<(String, String)>,
}

pub fn op_fetch(
state: &ThreadSafeState,
base: &msg::Base<'_>,
args: Value,
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();
) -> Result<JsonOp, ErrBox> {
let args: FetchArgs = serde_json::from_value(args)?;
let url = args.url;

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 mut req = Request::new(body);
let uri = Uri::from_str(&url).map_err(ErrBox::from)?;
*req.uri_mut() = uri;

if let Some(method) = args.method {
let method = Method::from_str(&method).unwrap();
*req.method_mut() = method;
}

let headers = req.headers_mut();
for header_pair in args.headers {
let name = HeaderName::from_bytes(header_pair.0.as_bytes()).unwrap();
let v = HeaderValue::from_str(&header_pair.1).unwrap();
headers.insert(name, v);
}

let url_ = url::Url::parse(url).map_err(ErrBox::from)?;
let url_ = url::Url::parse(&url).map_err(ErrBox::from)?;
state.check_net_url(&url_)?;

let client = http_util::get_client();
Expand All @@ -42,32 +61,22 @@ pub fn op_fetch(
.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 status = res.status().as_u16();
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()));
}
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()
},
))
let json_res = json!({
"bodyRid": body_resource.rid,
"status": status,
"headers": res_headers
});

futures::future::ok(json_res)
});
if base.sync() {
let result_buf = future.wait()?;
Ok(Op::Sync(result_buf))
} else {
Ok(Op::Async(Box::new(future)))
}

Ok(JsonOp::Async(Box::new(future)))
}
4 changes: 4 additions & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub const OP_FETCH_SOURCE_FILE: OpId = 14;
pub const OP_OPEN: OpId = 15;
pub const OP_CLOSE: OpId = 16;
pub const OP_SEEK: OpId = 17;
pub const OP_FETCH: OpId = 18;

pub fn dispatch(
state: &ThreadSafeState,
Expand Down Expand Up @@ -99,6 +100,9 @@ pub fn dispatch(
OP_SEEK => {
dispatch_json::dispatch(files::op_seek, state, control, zero_copy)
}
OP_FETCH => {
dispatch_json::dispatch(fetch::op_fetch, state, control, zero_copy)
}
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
2 changes: 2 additions & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const OP_FETCH_SOURCE_FILE = 14;
export const OP_OPEN = 15;
export const OP_CLOSE = 16;
export const OP_SEEK = 17;
export const OP_FETCH = 18;

export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand All @@ -41,6 +42,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_OPEN:
case OP_CLOSE:
case OP_SEEK:
case OP_FETCH:
json.asyncMsgFromRust(opId, ui8);
break;
default:
Expand Down
Loading

0 comments on commit a1fe53a

Please sign in to comment.