feat: update container

This commit is contained in:
2022-07-23 13:03:43 +08:00
parent c40ac23cf5
commit 46b730e235
3 changed files with 15 additions and 9 deletions

View File

@@ -45,8 +45,8 @@ pub fn do_fetch(params: &str) -> FnResult {
m => return FnResult::fail(format!("Unsupported method: {}", m)),
};
let mut request_builder = client.request(request_method.clone(), &fetch_params.url);
let mut has_user_agent = false;
if let Some(options) = &fetch_params.options {
let mut has_user_agent = false;
if let Some(headers) = &options.headers {
for (k, v) in headers {
let k = k.to_lowercase();
@@ -56,10 +56,11 @@ pub fn do_fetch(params: &str) -> FnResult {
request_builder = request_builder.header(k, v.to_string());
}
}
if !has_user_agent {
request_builder = request_builder.header("User-Agent", "JavaScriptSandboxContainer/0.1");
}
}
if !has_user_agent {
request_builder = request_builder.header("User-Agent", "JavaScriptSandboxContainer/0.1");
}
if let Some(options) = &fetch_params.options {
if let Some(body) = &options.body {
if Method::POST == request_method {
let body = reqwest::blocking::Body::from(body.to_string());

View File

@@ -38,10 +38,14 @@ fn main() -> Result<()> {
let a = exports.eval_javascript(&mut store, r##"
function hi(name) { return "hi: " + name; }
let a = [];
a.push(fetch('https://hatter.ink/util/print_request.action'));
a.push(fetch('https://hatter.ink/ip.action'));
a.push(fetch('https://hatter.ink/util/print_request.action', {
headers: {
'Test-Header': 'this is a test header'
}
}));
// a.push(fetch('https://hatter.ink/ip.action'));
a.push(fetch('https://hatter.ink/ip/ip.jsonp'));
a.push(fetch('https://hatter.ink/ip2.action'));
// a.push(fetch('https://hatter.ink/ip2.action'));
a.push({
userId: 'id001',
name: 'Test',

View File

@@ -1,6 +1,7 @@
use boa_engine::{Context, JsResult, JsString, JsValue};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
wit_bindgen_rust::export!("../exports.wit");
wit_bindgen_rust::import!("../container.wit");
@@ -31,7 +32,7 @@ fn do_fetch(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValu
let mut fetch_params_map = Map::new();
fetch_params_map.insert("url".to_string(), args[0].to_json(ctx).expect("error"));
if args.len() > 1 {
fetch_params_map.insert("params".to_string(), args[1].to_json(ctx).expect("error"));
fetch_params_map.insert("options".to_string(), args[1].to_json(ctx).expect("error"));
}
let fetch_params = format!("{}", Value::Object(fetch_params_map));