Skip to content

Commit c65797b

Browse files
fix: #0 rilot to take dynamic value
1 parent c59ad2a commit c65797b

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

examples/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::Serialize;
22

33
#[derive(Serialize)]
44
struct WasmResponse {
5-
app_url: String,
5+
app_url: Option<String>,
66
headers_to_update: std::collections::HashMap<String, String>,
77
headers_to_remove: Vec<String>,
88
}
@@ -21,9 +21,10 @@ pub extern "C" fn modify_request(ptr: *mut u8, len: usize) -> i32 {
2121
let mut headers = std::collections::HashMap::new();
2222
headers.insert("X-Debug-Path".to_string(), path.to_string());
2323
headers.insert("X-Debug-Method".to_string(), method.to_string());
24+
headers.insert("X-Via-Rilot".to_string(), "5".to_string());
2425

2526
let response = WasmResponse {
26-
app_url: "http://127.0.0.1:5501/".to_string(), // change to any backend dynamically
27+
app_url: None, // change to any backend dynamically if needed else default based on config.json
2728
headers_to_update: headers,
2829
headers_to_remove: vec![],
2930
};

src/proxy.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,9 @@ async fn handle_request(mut req: Request<Body>, config: Arc<config::Config>) ->
118118
}
119119
}
120120
}
121-
122-
// TODO:: update body?
123121
}
124122

125-
// Direct forwarding
123+
// Prepare new request
126124
let method = req.method().clone();
127125
let headers = req.headers().clone();
128126
let body = req.into_body();
@@ -143,12 +141,20 @@ async fn handle_request(mut req: Request<Body>, config: Arc<config::Config>) ->
143141
let resp_result = client.request(new_req).await;
144142

145143
match resp_result {
146-
Ok(mut backend_resp) => {
147-
let backend_body = hyper::body::to_bytes(backend_resp.body_mut()).await.unwrap_or_default();
148-
Ok(Response::builder()
149-
.status(backend_resp.status())
150-
.body(Body::from(backend_body))
151-
.expect("Failed to build final response"))
144+
Ok(backend_resp) => {
145+
// Forward body as-is, with original headers
146+
let status = backend_resp.status();
147+
let headers = backend_resp.headers().clone();
148+
let body = backend_resp.into_body();
149+
150+
let mut response = Response::builder()
151+
.status(status)
152+
.body(body)
153+
.expect("Failed to build final response");
154+
155+
*response.headers_mut() = headers;
156+
157+
Ok(response)
152158
}
153159
Err(_) => Ok(Response::builder()
154160
.status(502)

src/wasm_engine.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ pub struct WasmOutput {
99
}
1010

1111
pub fn run_modify_request(wasm_path: &str, input: &str) -> Result<WasmOutput, String> {
12-
let engine = Engine::default();
13-
let module = Module::from_file(&engine, wasm_path).map_err(|e| e.to_string())?;
14-
let mut store = Store::new(&engine, ());
15-
let instance = Instance::new(&mut store, &module, &[]).map_err(|e| e.to_string())?;
12+
let engine: Engine = Engine::default();
13+
let module: Module = Module::from_file(&engine, wasm_path).map_err(|e| e.to_string())?;
14+
let mut store: Store<()> = Store::new(&engine, ());
15+
let instance: Instance = Instance::new(&mut store, &module, &[]).map_err(|e| e.to_string())?;
1616

17-
let func = instance.get_typed_func::<(i32, i32), i32>(&mut store, "modify_request")
17+
let func: TypedFunc<(i32, i32), i32> = instance.get_typed_func::<(i32, i32), i32>(&mut store, "modify_request")
1818
.map_err(|e| e.to_string())?;
1919

20-
let memory = instance.get_memory(&mut store, "memory")
20+
let memory: Memory = instance.get_memory(&mut store, "memory")
2121
.ok_or_else(|| "failed to find memory export".to_string())?;
2222

23-
let input_bytes = input.as_bytes();
24-
let ptr = 0;
23+
let input_bytes: &[u8] = input.as_bytes();
24+
let ptr: usize = 0;
2525
memory.write(&mut store, ptr, input_bytes).map_err(|e| e.to_string())?;
2626

27-
let result_ptr = func.call(&mut store, (ptr as i32, input_bytes.len() as i32))
27+
let result_ptr: i32 = func.call(&mut store, (ptr as i32, input_bytes.len() as i32))
2828
.map_err(|e| e.to_string())?;
2929

30-
let mut buffer = [0u8; 4096];
30+
let mut buffer: [u8; 4096] = [0u8; 4096];
3131
memory.read(&mut store, result_ptr as usize, &mut buffer).map_err(|e| e.to_string())?;
3232
let nul_pos = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
3333

34-
let output = String::from_utf8_lossy(&buffer[..nul_pos]).to_string();
34+
let output: String = String::from_utf8_lossy(&buffer[..nul_pos]).to_string();
3535
serde_json::from_str(&output).map_err(|e| e.to_string())
3636
}

0 commit comments

Comments
 (0)