@@ -5,13 +5,15 @@ use super::{assets::handle_assets, not_found::handle_not_found};
5
5
use crate :: DataConnectors ;
6
6
use actix_web:: {
7
7
http:: StatusCode ,
8
- web:: { Bytes , Data } ,
8
+ web:: { self , Bytes , Data } ,
9
9
HttpRequest , HttpResponse ,
10
10
} ;
11
11
use std:: { fs:: File , io:: Write , sync:: RwLock } ;
12
12
use wws_router:: Routes ;
13
13
use wws_worker:: io:: WasmOutput ;
14
14
15
+ const CORS_HEADER : & str = "Access-Control-Allow-Origin" ;
16
+
15
17
/// Process an HTTP request by passing it to the right Runner. The Runner
16
18
/// will prepare the WASI environment and call the Wasm module with the data.
17
19
///
@@ -29,7 +31,11 @@ use wws_worker::io::WasmOutput;
29
31
///
30
32
/// For these reasons, we are selecting the right handler at this point and not
31
33
/// allowing Actix to select it for us.
32
- pub async fn handle_worker ( req : HttpRequest , body : Bytes ) -> HttpResponse {
34
+ pub async fn handle_worker (
35
+ req : HttpRequest ,
36
+ body : Bytes ,
37
+ cors_origins : web:: Data < Option < Vec < String > > > ,
38
+ ) -> HttpResponse {
33
39
let routes = req. app_data :: < Data < Routes > > ( ) . unwrap ( ) ;
34
40
let stderr_file = req. app_data :: < Data < Option < File > > > ( ) . unwrap ( ) ;
35
41
let data_connectors = req
@@ -97,6 +103,16 @@ pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
97
103
// Default content type
98
104
builder. insert_header ( ( "Content-Type" , "text/html" ) ) ;
99
105
106
+ // Check if cors config has any origins to register
107
+ if let Some ( origins) = cors_origins. as_ref ( ) {
108
+ // Check if worker has overridden the header, if not
109
+ if !handler_result. headers . contains_key ( CORS_HEADER ) {
110
+ // insert those origins in 'Access-Control-Allow-Origin' header
111
+ let header_value = origins. join ( "," ) ;
112
+ builder. insert_header ( ( CORS_HEADER , header_value) ) ;
113
+ }
114
+ }
115
+
100
116
for ( key, val) in handler_result. headers . iter ( ) {
101
117
// Note that QuickJS is replacing the "-" character
102
118
// with "_" on property keys. Here, we rollback it
0 commit comments