Skip to main content

aver/services/
http_server.rs

1use std::collections::HashMap;
2use std::sync::Arc as Rc;
3
4use aver_rt::{AverList, AverStr, HttpHeaders, HttpRequest, HttpResponse};
5
6use crate::nan_value::{Arena, NanValue};
7use crate::value::{RuntimeError, Value, list_from_vec, list_view};
8
9pub fn register(global: &mut HashMap<String, Value>) {
10    let mut members = HashMap::new();
11    members.insert(
12        "listen".to_string(),
13        Value::Builtin("HttpServer.listen".to_string()),
14    );
15    members.insert(
16        "listenWith".to_string(),
17        Value::Builtin("HttpServer.listenWith".to_string()),
18    );
19    global.insert(
20        "HttpServer".to_string(),
21        Value::Namespace {
22            name: "HttpServer".to_string(),
23            members,
24        },
25    );
26}
27
28pub fn register_nv(global: &mut HashMap<String, NanValue>, arena: &mut Arena) {
29    let mut members: Vec<(Rc<str>, NanValue)> = Vec::with_capacity(2);
30    let idx1 = arena.push_builtin("HttpServer.listen");
31    members.push((Rc::from("listen"), NanValue::new_builtin(idx1)));
32    let idx2 = arena.push_builtin("HttpServer.listenWith");
33    members.push((Rc::from("listenWith"), NanValue::new_builtin(idx2)));
34    let ns_idx = arena.push(crate::nan_value::ArenaEntry::Namespace {
35        name: Rc::from("HttpServer"),
36        members,
37    });
38    global.insert("HttpServer".to_string(), NanValue::new_namespace(ns_idx));
39}
40
41pub const DECLARED_EFFECTS: &[&str] = &["HttpServer.listen", "HttpServer.listenWith"];
42
43pub fn effects(name: &str) -> &'static [&'static str] {
44    match name {
45        "HttpServer.listen" => &["HttpServer.listen"],
46        "HttpServer.listenWith" => &["HttpServer.listenWith"],
47        "SelfHostRuntime.httpServerListen" => &["HttpServer.listen"],
48        "SelfHostRuntime.httpServerListenWith" => &["HttpServer.listenWith"],
49        _ => &[],
50    }
51}
52
53pub fn call(_name: &str, _args: &[Value]) -> Option<Result<Value, RuntimeError>> {
54    None
55}
56
57pub fn call_with_runtime<F>(
58    name: &str,
59    args: &[Value],
60    mut invoke_handler: F,
61    skip_server: bool,
62) -> Option<Result<Value, RuntimeError>>
63where
64    F: FnMut(Value, Vec<Value>, String) -> Result<Value, RuntimeError>,
65{
66    match name {
67        "HttpServer.listen" => Some(listen(args, false, &mut invoke_handler, skip_server)),
68        "HttpServer.listenWith" => Some(listen(args, true, &mut invoke_handler, skip_server)),
69        "SelfHostRuntime.httpServerListen" => {
70            Some(listen(args, false, &mut invoke_handler, skip_server))
71        }
72        "SelfHostRuntime.httpServerListenWith" => {
73            Some(listen(args, true, &mut invoke_handler, skip_server))
74        }
75        _ => None,
76    }
77}
78
79fn listen<F>(
80    args: &[Value],
81    with_context: bool,
82    invoke_handler: &mut F,
83    skip_server: bool,
84) -> Result<Value, RuntimeError>
85where
86    F: FnMut(Value, Vec<Value>, String) -> Result<Value, RuntimeError>,
87{
88    let expected = if with_context { 3 } else { 2 };
89    if args.len() != expected {
90        let sig = if with_context {
91            "HttpServer.listenWith(port, context, handler)"
92        } else {
93            "HttpServer.listen(port, handler)"
94        };
95        return Err(RuntimeError::Error(format!(
96            "{} expects {} arguments, got {}",
97            sig,
98            expected,
99            args.len()
100        )));
101    }
102
103    if skip_server {
104        return Ok(Value::Unit);
105    }
106
107    let port = match &args[0] {
108        Value::Int(n) if (0..=65535).contains(n) => *n,
109        Value::Int(n) => {
110            return Err(RuntimeError::Error(format!(
111                "HttpServer.listen: port {} is out of range (0-65535)",
112                n
113            )));
114        }
115        _ => {
116            return Err(RuntimeError::Error(
117                "HttpServer.listen: port must be an Int".to_string(),
118            ));
119        }
120    };
121    let handler = if with_context {
122        args[2].clone()
123    } else {
124        args[1].clone()
125    };
126
127    let result = if with_context {
128        let context = args[1].clone();
129        aver_rt::http_server::listen_with(port, context, |ctx, request| {
130            dispatch_handler(&handler, Some(ctx), request, invoke_handler)
131        })
132    } else {
133        aver_rt::http_server::listen(port, |request| {
134            dispatch_handler(&handler, None, request, invoke_handler)
135        })
136    };
137
138    result.map_err(RuntimeError::Error)?;
139    Ok(Value::Unit)
140}
141
142fn dispatch_handler<F>(
143    handler: &Value,
144    context: Option<Value>,
145    request: HttpRequest,
146    invoke_handler: &mut F,
147) -> HttpResponse
148where
149    F: FnMut(Value, Vec<Value>, String) -> Result<Value, RuntimeError>,
150{
151    let callback_entry = format!("<HttpServer {} {}>", &*request.method, &*request.path);
152    let mut callback_args = Vec::new();
153    if let Some(ctx) = context {
154        callback_args.push(ctx);
155    }
156    callback_args.push(http_request_to_value(request));
157
158    let callback_result = invoke_handler(handler.clone(), callback_args, callback_entry);
159    match callback_result {
160        Ok(value) => match http_response_from_value(value) {
161            Ok(resp) => resp,
162            Err(e) => HttpResponse {
163                status: 500,
164                body: aver_rt::AverStr::from(format!("HttpServer handler return error: {}", e)),
165                headers: HttpHeaders::default(),
166            },
167        },
168        Err(e) => HttpResponse {
169            status: 500,
170            body: aver_rt::AverStr::from(format!("HttpServer handler execution error: {}", e)),
171            headers: HttpHeaders::default(),
172        },
173    }
174}
175
176fn http_request_to_value(req: HttpRequest) -> Value {
177    let mut headers = HashMap::new();
178    for (name, values) in req.headers.iter() {
179        let value_list: Vec<Value> = values.iter().map(|v| Value::Str(v.to_string())).collect();
180        headers.insert(Value::Str(name.to_string()), list_from_vec(value_list));
181    }
182
183    Value::Record {
184        type_name: "HttpRequest".to_string(),
185        fields: vec![
186            ("method".to_string(), Value::Str(req.method.to_string())),
187            ("path".to_string(), Value::Str(req.path.to_string())),
188            ("body".to_string(), Value::Str(req.body.to_string())),
189            ("headers".to_string(), Value::Map(headers)),
190        ]
191        .into(),
192    }
193}
194
195fn http_response_from_value(val: Value) -> Result<HttpResponse, RuntimeError> {
196    let (type_name, fields) = match val {
197        Value::Record { type_name, fields } => (type_name, fields),
198        _ => {
199            return Err(RuntimeError::Error(
200                "HttpServer handler must return HttpResponse record".to_string(),
201            ));
202        }
203    };
204
205    if type_name != "HttpResponse" {
206        return Err(RuntimeError::Error(format!(
207            "HttpServer handler must return HttpResponse, got {}",
208            type_name
209        )));
210    }
211
212    let mut status = None;
213    let mut body = None;
214    let mut headers = HttpHeaders::default();
215
216    for (name, value) in fields.iter() {
217        match name.as_str() {
218            "status" => {
219                if let Value::Int(n) = value {
220                    status = Some(*n);
221                } else {
222                    return Err(RuntimeError::Error(
223                        "HttpResponse.status must be Int".to_string(),
224                    ));
225                }
226            }
227            "body" => {
228                if let Value::Str(s) = value {
229                    body = Some(aver_rt::AverStr::from(s.as_str()));
230                } else {
231                    return Err(RuntimeError::Error(
232                        "HttpResponse.body must be String".to_string(),
233                    ));
234                }
235            }
236            "headers" => {
237                headers = parse_http_response_headers(value.clone())?;
238            }
239            _ => {}
240        }
241    }
242
243    Ok(HttpResponse {
244        status: status
245            .ok_or_else(|| RuntimeError::Error("HttpResponse.status is required".to_string()))?,
246        body: body
247            .ok_or_else(|| RuntimeError::Error("HttpResponse.body is required".to_string()))?,
248        headers,
249    })
250}
251
252fn parse_http_response_headers(val: Value) -> Result<HttpHeaders, RuntimeError> {
253    let map = match val {
254        Value::Map(m) => m,
255        _ => {
256            return Err(RuntimeError::Error(
257                "HttpResponse.headers must be Map<String, List<String>>".to_string(),
258            ));
259        }
260    };
261
262    let mut out = HttpHeaders::default();
263    for (k, v) in map.into_iter() {
264        let name = match k {
265            Value::Str(s) => s.to_ascii_lowercase(),
266            _ => {
267                return Err(RuntimeError::Error(
268                    "HttpResponse.headers keys must be Strings".to_string(),
269                ));
270            }
271        };
272        let values = list_view(&v).ok_or_else(|| {
273            RuntimeError::Error("HttpResponse.headers values must be List<String>".to_string())
274        })?;
275        let mut buf = Vec::new();
276        for item in values.iter() {
277            match item {
278                Value::Str(s) => buf.push(AverStr::from(s.as_str())),
279                _ => {
280                    return Err(RuntimeError::Error(
281                        "HttpResponse.headers values must be Strings".to_string(),
282                    ));
283                }
284            }
285        }
286        out = out.insert(AverStr::from(name), AverList::from_vec(buf));
287    }
288
289    Ok(out)
290}