Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 32 additions & 41 deletions stdlib/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ pub fn web_render_html_pub(ctx: &RuntimeContext) -> String {
renderer::render_complete_document(&blocks, &event_handlers)
}

fn web_start(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
fn check_network_capability(ctx: &RuntimeContext) -> Result<(), RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
Expand All @@ -50,15 +47,32 @@ fn web_start(
None,
));
}
let port = args[0].try_into_int().map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})? as u16;
let content = args[1].to_string();
Ok(())
}

fn parse_port_arg(arg: &RuntimeValue) -> Result<u16, RuntimeError> {
arg.try_into_int()
.map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})
.map(|p| p as u16)
}

fn set_page_content(content: String) {
*server::PAGE_CONTENT.lock().unwrap() = content;
}

fn web_start(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
check_network_capability(ctx)?;
let port = parse_port_arg(&args[0])?;
set_page_content(args[1].to_string());
server::start_webforge_server(port)
}

Expand All @@ -67,31 +81,16 @@ fn web_page(
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let _path = args[0].to_string();
let content = args[1].to_string();
*server::PAGE_CONTENT.lock().unwrap() = content;
set_page_content(args[1].to_string());
Ok(RuntimeValue::Null)
}

fn web_serve(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied".to_string(),
),
None,
None,
));
}
let port = args[0].try_into_int().map_err(|e| {
RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})? as u16;
check_network_capability(ctx)?;
let port = parse_port_arg(&args[0])?;
server::start_webforge_server(port)
}

Expand All @@ -107,24 +106,15 @@ fn web_set_content(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let new_content = args[0].to_string();
*server::PAGE_CONTENT.lock().unwrap() = new_content;
set_page_content(args[0].to_string());
Ok(RuntimeValue::Str("Content updated".to_string()))
}

fn web_fetch(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Network) {
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
"Security policy violation: Network capability is denied".to_string(),
),
None,
None,
));
}
check_network_capability(ctx)?;
let url = args[0].to_string();

if !server::is_safe_url(&url) {
Expand Down Expand Up @@ -183,6 +173,7 @@ fn web_start_website(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
check_network_capability(ctx)?;
let port = if let Some(arg) = args.first() {
arg.try_into_int().unwrap_or(3000) as u16
} else {
Expand Down
10 changes: 9 additions & 1 deletion stdlib/src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ pub fn start_webforge_server(port: u16) -> Result<RuntimeValue, RuntimeError> {
}
SERVER_RUNNING.store(true, Ordering::SeqCst);

let server = match tiny_http::Server::http(format!("0.0.0.0:{}", port)) {
let server_res = tiny_http::Server::http(format!("0.0.0.0:{}", port)).or_else(|e| {
if port == 3000 {
tiny_http::Server::http("0.0.0.0:0")
} else {
Err(e)
}
});

let server = match server_res {
Ok(s) => Mutex::new(s),
Err(e) => {
SERVER_RUNNING.store(false, Ordering::SeqCst);
Expand Down
Loading