@@ -37,10 +37,7 @@ pub fn web_render_html_pub(ctx: &RuntimeContext) -> String {
3737 renderer:: render_complete_document ( & blocks, & event_handlers)
3838}
3939
40- fn web_start (
41- ctx : & mut RuntimeContext ,
42- args : Vec < RuntimeValue > ,
43- ) -> Result < RuntimeValue , RuntimeError > {
40+ fn check_network_capability ( ctx : & RuntimeContext ) -> Result < ( ) , RuntimeError > {
4441 if !ctx. config . capabilities . contains ( & Capability :: Network ) {
4542 return Err ( RuntimeError :: new (
4643 techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation (
@@ -50,15 +47,32 @@ fn web_start(
5047 None ,
5148 ) ) ;
5249 }
53- let port = args[ 0 ] . try_into_int ( ) . map_err ( |e| {
54- RuntimeError :: new (
55- techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation ( e. to_string ( ) ) ,
56- None ,
57- None ,
58- )
59- } ) ? as u16 ;
60- let content = args[ 1 ] . to_string ( ) ;
50+ Ok ( ( ) )
51+ }
52+
53+ fn parse_port_arg ( arg : & RuntimeValue ) -> Result < u16 , RuntimeError > {
54+ arg. try_into_int ( )
55+ . map_err ( |e| {
56+ RuntimeError :: new (
57+ techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation ( e. to_string ( ) ) ,
58+ None ,
59+ None ,
60+ )
61+ } )
62+ . map ( |p| p as u16 )
63+ }
64+
65+ fn set_page_content ( content : String ) {
6166 * server:: PAGE_CONTENT . lock ( ) . unwrap ( ) = content;
67+ }
68+
69+ fn web_start (
70+ ctx : & mut RuntimeContext ,
71+ args : Vec < RuntimeValue > ,
72+ ) -> Result < RuntimeValue , RuntimeError > {
73+ check_network_capability ( ctx) ?;
74+ let port = parse_port_arg ( & args[ 0 ] ) ?;
75+ set_page_content ( args[ 1 ] . to_string ( ) ) ;
6276 server:: start_webforge_server ( port)
6377}
6478
@@ -67,31 +81,16 @@ fn web_page(
6781 args : Vec < RuntimeValue > ,
6882) -> Result < RuntimeValue , RuntimeError > {
6983 let _path = args[ 0 ] . to_string ( ) ;
70- let content = args[ 1 ] . to_string ( ) ;
71- * server:: PAGE_CONTENT . lock ( ) . unwrap ( ) = content;
84+ set_page_content ( args[ 1 ] . to_string ( ) ) ;
7285 Ok ( RuntimeValue :: Null )
7386}
7487
7588fn web_serve (
7689 ctx : & mut RuntimeContext ,
7790 args : Vec < RuntimeValue > ,
7891) -> Result < RuntimeValue , RuntimeError > {
79- if !ctx. config . capabilities . contains ( & Capability :: Network ) {
80- return Err ( RuntimeError :: new (
81- techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation (
82- "Security policy violation: Network capability is denied" . to_string ( ) ,
83- ) ,
84- None ,
85- None ,
86- ) ) ;
87- }
88- let port = args[ 0 ] . try_into_int ( ) . map_err ( |e| {
89- RuntimeError :: new (
90- techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation ( e. to_string ( ) ) ,
91- None ,
92- None ,
93- )
94- } ) ? as u16 ;
92+ check_network_capability ( ctx) ?;
93+ let port = parse_port_arg ( & args[ 0 ] ) ?;
9594 server:: start_webforge_server ( port)
9695}
9796
@@ -107,24 +106,15 @@ fn web_set_content(
107106 _ctx : & mut RuntimeContext ,
108107 args : Vec < RuntimeValue > ,
109108) -> Result < RuntimeValue , RuntimeError > {
110- let new_content = args[ 0 ] . to_string ( ) ;
111- * server:: PAGE_CONTENT . lock ( ) . unwrap ( ) = new_content;
109+ set_page_content ( args[ 0 ] . to_string ( ) ) ;
112110 Ok ( RuntimeValue :: Str ( "Content updated" . to_string ( ) ) )
113111}
114112
115113fn web_fetch (
116114 ctx : & mut RuntimeContext ,
117115 args : Vec < RuntimeValue > ,
118116) -> Result < RuntimeValue , RuntimeError > {
119- if !ctx. config . capabilities . contains ( & Capability :: Network ) {
120- return Err ( RuntimeError :: new (
121- techscript_runtime:: error:: RuntimeErrorKind :: InvalidOperation (
122- "Security policy violation: Network capability is denied" . to_string ( ) ,
123- ) ,
124- None ,
125- None ,
126- ) ) ;
127- }
117+ check_network_capability ( ctx) ?;
128118 let url = args[ 0 ] . to_string ( ) ;
129119
130120 if !server:: is_safe_url ( & url) {
@@ -183,6 +173,7 @@ fn web_start_website(
183173 ctx : & mut RuntimeContext ,
184174 args : Vec < RuntimeValue > ,
185175) -> Result < RuntimeValue , RuntimeError > {
176+ check_network_capability ( ctx) ?;
186177 let port = if let Some ( arg) = args. first ( ) {
187178 arg. try_into_int ( ) . unwrap_or ( 3000 ) as u16
188179 } else {
0 commit comments