1use std::collections::HashMap;
2use std::sync::Arc as Rc;
3
4use aver_rt::{AverList, Header, 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: AverList::empty(),
166 },
167 },
168 Err(e) => HttpResponse {
169 status: 500,
170 body: aver_rt::AverStr::from(format!("HttpServer handler execution error: {}", e)),
171 headers: AverList::empty(),
172 },
173 }
174}
175
176fn http_request_to_value(req: HttpRequest) -> Value {
177 let headers = req
178 .headers
179 .into_iter()
180 .map(|header| Value::Record {
181 type_name: "Header".to_string(),
182 fields: vec![
183 ("name".to_string(), Value::Str(header.name.to_string())),
184 ("value".to_string(), Value::Str(header.value.to_string())),
185 ]
186 .into(),
187 })
188 .collect::<Vec<_>>();
189
190 Value::Record {
191 type_name: "HttpRequest".to_string(),
192 fields: vec![
193 ("method".to_string(), Value::Str(req.method.to_string())),
194 ("path".to_string(), Value::Str(req.path.to_string())),
195 ("body".to_string(), Value::Str(req.body.to_string())),
196 ("headers".to_string(), list_from_vec(headers)),
197 ]
198 .into(),
199 }
200}
201
202fn http_response_from_value(val: Value) -> Result<HttpResponse, RuntimeError> {
203 let (type_name, fields) = match val {
204 Value::Record { type_name, fields } => (type_name, fields),
205 _ => {
206 return Err(RuntimeError::Error(
207 "HttpServer handler must return HttpResponse record".to_string(),
208 ));
209 }
210 };
211
212 if type_name != "HttpResponse" {
213 return Err(RuntimeError::Error(format!(
214 "HttpServer handler must return HttpResponse, got {}",
215 type_name
216 )));
217 }
218
219 let mut status = None;
220 let mut body = None;
221 let mut headers = AverList::empty();
222
223 for (name, value) in fields.iter() {
224 match name.as_str() {
225 "status" => {
226 if let Value::Int(n) = value {
227 status = Some(*n);
228 } else {
229 return Err(RuntimeError::Error(
230 "HttpResponse.status must be Int".to_string(),
231 ));
232 }
233 }
234 "body" => {
235 if let Value::Str(s) = value {
236 body = Some(aver_rt::AverStr::from(s.as_str()));
237 } else {
238 return Err(RuntimeError::Error(
239 "HttpResponse.body must be String".to_string(),
240 ));
241 }
242 }
243 "headers" => {
244 headers = parse_http_response_headers(value.clone())?;
245 }
246 _ => {}
247 }
248 }
249
250 Ok(HttpResponse {
251 status: status
252 .ok_or_else(|| RuntimeError::Error("HttpResponse.status is required".to_string()))?,
253 body: body
254 .ok_or_else(|| RuntimeError::Error("HttpResponse.body is required".to_string()))?,
255 headers,
256 })
257}
258
259fn parse_http_response_headers(val: Value) -> Result<AverList<Header>, RuntimeError> {
260 let list = list_view(&val).ok_or_else(|| {
261 RuntimeError::Error("HttpResponse.headers must be List<Header>".to_string())
262 })?;
263
264 let mut out = Vec::new();
265 for item in list.iter() {
266 let fields = match item {
267 Value::Record { fields, .. } => fields,
268 _ => {
269 return Err(RuntimeError::Error(
270 "HttpResponse.headers entries must be Header records".to_string(),
271 ));
272 }
273 };
274
275 let mut name = None;
276 let mut value = None;
277 for (field_name, field_val) in fields.iter() {
278 match (field_name.as_str(), field_val) {
279 ("name", Value::Str(s)) => name = Some(aver_rt::AverStr::from(s.as_str())),
280 ("value", Value::Str(s)) => value = Some(aver_rt::AverStr::from(s.as_str())),
281 _ => {}
282 }
283 }
284
285 let name = name.ok_or_else(|| {
286 RuntimeError::Error("HttpResponse header missing String 'name'".to_string())
287 })?;
288 let value = value.ok_or_else(|| {
289 RuntimeError::Error("HttpResponse header missing String 'value'".to_string())
290 })?;
291 out.push(Header { name, value });
292 }
293
294 Ok(AverList::from_vec(out))
295}