1use crate::host::{invoke, with_host, JsObj};
25use fusevm::Value;
26use indexmap::IndexMap;
27use rustls::pki_types::ServerName;
28use rustls::{ClientConnection, StreamOwned};
29use std::collections::HashMap;
30use std::io::{Read, Write};
31use std::net::TcpStream;
32
33pub const MODULE_METHODS: &[&str] = &["createServer", "request", "get"];
35
36pub const RESPONSE_METHODS: &[&str] = &[
39 "writeHead",
40 "setHeader",
41 "getHeader",
42 "getHeaderNames",
43 "getHeaders",
44 "hasHeader",
45 "removeHeader",
46 "write",
47 "end",
48 "flushHeaders",
49];
50pub const CLIENT_REQUEST_METHODS: &[&str] = &[
51 "write",
52 "end",
53 "setHeader",
54 "getHeader",
55 "removeHeader",
56 "abort",
57 "destroy",
58 "setTimeout",
59];
60
61pub fn constant(name: &str) -> Option<Value> {
67 match name {
68 "Agent" => Some(with_host(|h| h.alloc(JsObj::Builtin("https.Agent".into())))),
69 "globalAgent" => {
71 thread_local! {
72 static AGENT: std::cell::RefCell<Option<Value>> = const { std::cell::RefCell::new(None) };
73 }
74 if let Some(a) = AGENT.with(|a| a.borrow().clone()) {
75 return Some(a);
76 }
77 let a = with_host(|h| {
78 let mut m = IndexMap::new();
79 m.insert("@@native".into(), h.new_str("Agent"));
80 m.insert("maxSockets".into(), Value::Float(f64::INFINITY));
81 m.insert("protocol".into(), h.new_str("https:"));
82 h.new_object(m)
83 });
84 AGENT.with(|slot| *slot.borrow_mut() = Some(a.clone()));
85 Some(a)
86 }
87 _ => None,
88 }
89}
90
91pub fn call(method: &str, args: &[Value]) -> Option<Result<Value, String>> {
93 match method {
94 "createServer" => Some(create_server(args)),
95 "request" => Some(request(args, false)),
96 "get" => Some(request(args, true)),
97 _ => None,
98 }
99}
100
101fn get_prop(recv: &Value, key: &str) -> Option<Value> {
104 with_host(|h| match h.get(recv) {
105 Some(JsObj::Object(p)) => p.get(key).cloned(),
106 _ => None,
107 })
108}
109fn set_prop(recv: &Value, key: &str, val: Value) {
110 with_host(|h| {
111 if let Some(JsObj::Object(p)) = h.get_mut(recv) {
112 p.insert(key.to_string(), val);
113 }
114 });
115}
116fn u64_prop(recv: &Value, key: &str) -> Option<u64> {
117 get_prop(recv, key).map(|v| with_host(|h| h.to_number(&v)) as u64)
118}
119
120fn value_bytes(v: Option<&Value>) -> Vec<u8> {
122 let Some(v) = v else { return Vec::new() };
123 let is_buffer =
124 with_host(|h| matches!(h.get(v), Some(JsObj::Object(p)) if p.contains_key("@@bytes")));
125 if is_buffer {
126 return with_host(|h| match h.get(v) {
127 Some(JsObj::Object(p)) => crate::stdlib::buffer::window_bytes_h(h, p),
128 _ => Vec::new(),
129 });
130 }
131 with_host(|h| h.str_of(v)).into_bytes()
132}
133
134struct HttpsConn {
137 listener: Value,
138 buf: Vec<u8>,
139}
140
141struct ResState {
142 sock_id: u64,
143 head: bool,
146 status: u16,
147 message: Option<String>,
148 headers: Vec<(String, String)>,
149 body: Vec<u8>,
150}
151
152thread_local! {
153 static CONNS: std::cell::RefCell<HashMap<u64, HttpsConn>> =
154 std::cell::RefCell::new(HashMap::new());
155 static RESPONSES: std::cell::RefCell<HashMap<u64, ResState>> =
156 std::cell::RefCell::new(HashMap::new());
157 static NEXT_RESID: std::cell::Cell<u64> = const { std::cell::Cell::new(1) };
158 static CLIENT_REQS: std::cell::RefCell<HashMap<u64, ClientReq>> =
159 std::cell::RefCell::new(HashMap::new());
160 static NEXT_REQID: std::cell::Cell<u64> = const { std::cell::Cell::new(1) };
161}
162
163fn next_resid() -> u64 {
164 NEXT_RESID.with(|c| {
165 let id = c.get();
166 c.set(id + 1);
167 id
168 })
169}
170fn next_reqid() -> u64 {
171 NEXT_REQID.with(|c| {
172 let id = c.get();
173 c.set(id + 1);
174 id
175 })
176}
177
178pub fn create_server(args: &[Value]) -> Result<Value, String> {
181 let mut options: Option<Value> = None;
182 let mut listener = Value::Undef;
183 for a in args {
184 if with_host(|h| crate::host::is_callable(h, a)) {
185 listener = a.clone();
186 } else if matches!(a, Value::Obj(_)) {
187 options = Some(a.clone());
188 }
189 }
190 let opts = options.ok_or_else(|| {
191 crate::host::type_error(
192 "https.createServer requires an options object with `key` and `cert`",
193 )
194 })?;
195 let cert = value_bytes(get_prop(&opts, "cert").as_ref());
196 let key = value_bytes(get_prop(&opts, "key").as_ref());
197 if cert.is_empty() || key.is_empty() {
198 return Err(crate::host::type_error(
199 "https.createServer requires `key` and `cert`",
200 ));
201 }
202 let config = super::tls::build_server_config(&cert, &key)?;
203
204 let listener_for_hook = listener.clone();
206 let hook: super::tls::ConnHook =
207 std::rc::Rc::new(move |_server: &Value, _socket: &Value, sock_id: u64| {
208 CONNS.with(|c| {
209 c.borrow_mut().insert(
210 sock_id,
211 HttpsConn {
212 listener: listener_for_hook.clone(),
213 buf: Vec::new(),
214 },
215 );
216 });
217 Ok(())
218 });
219 Ok(super::tls::create_server_with_config(
220 config, hook, listener,
221 ))
222}
223
224pub fn drop_conn(sock_id: u64) {
226 CONNS.with(|c| {
227 c.borrow_mut().remove(&sock_id);
228 });
229}
230
231pub fn feed(sock_id: u64, _socket: &Value, bytes: &[u8]) -> Result<(), String> {
236 let is_https = CONNS.with(|c| c.borrow().contains_key(&sock_id));
237 if !is_https {
238 return Ok(());
239 }
240 CONNS.with(|c| {
241 c.borrow_mut()
242 .get_mut(&sock_id)
243 .unwrap()
244 .buf
245 .extend_from_slice(bytes)
246 });
247
248 loop {
249 let (listener, parsed) = CONNS.with(|c| {
250 let mut c = c.borrow_mut();
251 let conn = c.get_mut(&sock_id).unwrap();
252 match parse_request(&conn.buf) {
253 Some((req, consumed)) => {
254 conn.buf.drain(..consumed);
255 (conn.listener.clone(), Some(req))
256 }
257 None => (Value::Undef, None),
258 }
259 });
260 let Some(parsed) = parsed else { break };
261
262 let req = build_incoming(&parsed);
263 let res = build_response(sock_id, parsed.method.eq_ignore_ascii_case("HEAD"));
264 if with_host(|h| crate::host::is_callable(h, &listener)) {
265 invoke(&listener, vec![req.clone(), res], None)?;
266 }
267 if !parsed.body.is_empty() {
273 let encoding = with_host(|h| match h.get(&req) {
274 Some(JsObj::Object(p)) => p.get("@@encoding").map(|v| h.str_of(v)),
275 _ => None,
276 });
277 let chunk = match encoding {
278 Some(enc) => with_host(|h| {
279 let s = super::buffer::encode_bytes(&parsed.body, &enc);
280 h.new_str(s)
281 }),
282 None => super::buffer::from_bytes(&parsed.body),
283 };
284 super::events::instance_call(
285 &req,
286 "emit",
287 vec![with_host(|h| h.new_str("data")), chunk],
288 )?;
289 }
290 super::events::instance_call(&req, "emit", vec![with_host(|h| h.new_str("end"))])?;
291 }
292 Ok(())
293}
294
295struct ParsedReq {
297 method: String,
298 url: String,
299 http_version: String,
300 headers: Vec<(String, String)>,
301 body: Vec<u8>,
302}
303
304fn parse_request(buf: &[u8]) -> Option<(ParsedReq, usize)> {
307 let head_end = find_subslice(buf, b"\r\n\r\n")?;
308 let head = &buf[..head_end];
309 let body_start = head_end + 4;
310
311 let head_str = String::from_utf8_lossy(head);
312 let mut lines = head_str.split("\r\n");
313 let request_line = lines.next()?;
314 let mut parts = request_line.split(' ');
315 let method = parts.next()?.to_string();
316 let url = parts.next()?.to_string();
317 let version = parts.next().unwrap_or("HTTP/1.1");
318 let http_version = version.strip_prefix("HTTP/").unwrap_or("1.1").to_string();
319
320 let mut headers: Vec<(String, String)> = Vec::new();
321 let mut content_length = 0usize;
322 for line in lines {
323 if line.is_empty() {
324 continue;
325 }
326 if let Some((k, v)) = line.split_once(':') {
327 let name = k.trim().to_ascii_lowercase();
328 let value = v.trim().to_string();
329 if name == "content-length" {
330 content_length = value.parse().unwrap_or(0);
331 }
332 headers.push((name, value));
333 }
334 }
335 if buf.len() < body_start + content_length {
336 return None;
337 }
338 let body = buf[body_start..body_start + content_length].to_vec();
339 Some((
340 ParsedReq {
341 method,
342 url,
343 http_version,
344 headers,
345 body,
346 },
347 body_start + content_length,
348 ))
349}
350
351fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
352 haystack.windows(needle.len()).position(|w| w == needle)
353}
354
355fn build_incoming(req: &ParsedReq) -> Value {
356 let headers_obj = with_host(|h| {
357 let mut m = IndexMap::new();
358 for (k, v) in &req.headers {
359 m.insert(k.clone(), h.new_str(v.clone()));
360 }
361 h.new_object(m)
362 });
363 let mut extra = IndexMap::new();
364 extra.insert(
365 "method".into(),
366 with_host(|h| h.new_str(req.method.clone())),
367 );
368 extra.insert("url".into(), with_host(|h| h.new_str(req.url.clone())));
369 extra.insert(
370 "httpVersion".into(),
371 with_host(|h| h.new_str(req.http_version.clone())),
372 );
373 extra.insert("headers".into(), headers_obj);
374 super::tls::new_emitter_object("IncomingMessage", extra)
377}
378
379fn build_response(sock_id: u64, head: bool) -> Value {
380 let resid = next_resid();
381 RESPONSES.with(|r| {
382 r.borrow_mut().insert(
383 resid,
384 ResState {
385 sock_id,
386 head,
387 status: 200,
388 message: None,
389 headers: Vec::new(),
390 body: Vec::new(),
391 },
392 );
393 });
394 let mut extra = IndexMap::new();
395 extra.insert("@@resid".into(), Value::Float(resid as f64));
396 extra.insert("statusCode".into(), Value::Float(200.0));
397 super::tls::new_emitter_object("HTTPSServerResponse", extra)
398}
399
400pub fn instance_call(
403 tag: &str,
404 recv: &Value,
405 method: &str,
406 args: Vec<Value>,
407) -> Result<Value, String> {
408 if super::events::METHODS.contains(&method) {
409 return super::events::instance_call(recv, method, args);
410 }
411 match tag {
412 "HTTPSServerResponse" => response_call(recv, method, args),
413 "HTTPSClientRequest" => client_request_call(recv, method, args),
414 _ => Err(crate::host::type_error(&format!(
415 "{method} is not a function"
416 ))),
417 }
418}
419
420fn resid_of(res: &Value) -> Option<u64> {
421 u64_prop(res, "@@resid")
422}
423
424fn response_call(res: &Value, method: &str, args: Vec<Value>) -> Result<Value, String> {
425 let Some(resid) = resid_of(res) else {
426 return Err(crate::host::type_error("invalid ServerResponse"));
427 };
428 match method {
429 "writeHead" => {
430 let status =
431 with_host(|h| args.first().map(|v| h.to_number(v)).unwrap_or(200.0)) as u16;
432 let mut message: Option<String> = None;
433 let mut headers_arg: Option<Value> = None;
434 if let Some(a) = args.get(1) {
435 if with_host(|h| h.as_str(a)).is_some() {
436 message = Some(with_host(|h| h.str_of(a)));
437 } else if !matches!(a, Value::Undef) {
438 headers_arg = Some(a.clone());
439 }
440 }
441 if let Some(a) = args.get(2) {
442 if !matches!(a, Value::Undef) {
443 headers_arg = Some(a.clone());
444 }
445 }
446 let header_pairs = headers_arg.map(|h| object_pairs(&h)).unwrap_or_default();
447 RESPONSES.with(|r| {
448 if let Some(st) = r.borrow_mut().get_mut(&resid) {
449 st.status = status;
450 st.message = message;
451 for (k, v) in header_pairs {
452 upsert_header(&mut st.headers, &k, v);
453 }
454 }
455 });
456 set_prop(res, "statusCode", Value::Float(status as f64));
457 Ok(res.clone())
458 }
459 "setHeader" => {
460 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)));
461 let v = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
462 RESPONSES.with(|r| {
463 if let Some(st) = r.borrow_mut().get_mut(&resid) {
464 upsert_header(&mut st.headers, &k, v);
465 }
466 });
467 Ok(Value::Undef)
468 }
469 "getHeader" => {
470 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)))
471 .to_ascii_lowercase();
472 let val = RESPONSES.with(|r| {
473 r.borrow().get(&resid).and_then(|st| {
474 st.headers
475 .iter()
476 .find(|(hk, _)| hk.eq_ignore_ascii_case(&k))
477 .map(|(_, v)| v.clone())
478 })
479 });
480 Ok(val
481 .map(|v| with_host(|h| h.new_str(v)))
482 .unwrap_or(Value::Undef))
483 }
484 "removeHeader" => {
485 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)));
486 RESPONSES.with(|r| {
487 if let Some(st) = r.borrow_mut().get_mut(&resid) {
488 st.headers.retain(|(hk, _)| !hk.eq_ignore_ascii_case(&k));
489 }
490 });
491 Ok(Value::Undef)
492 }
493 "hasHeader" => {
499 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)));
500 let found = RESPONSES.with(|r| {
501 r.borrow()
502 .get(&resid)
503 .is_some_and(|st| st.headers.iter().any(|(hk, _)| hk.eq_ignore_ascii_case(&k)))
504 });
505 Ok(Value::Bool(found))
506 }
507 "getHeaderNames" => {
508 let names = RESPONSES.with(|r| {
509 r.borrow()
510 .get(&resid)
511 .map(|st| {
512 st.headers
513 .iter()
514 .map(|(k, _)| k.to_ascii_lowercase())
515 .collect::<Vec<_>>()
516 })
517 .unwrap_or_default()
518 });
519 Ok(with_host(|h| {
520 let items = names.into_iter().map(|n| h.new_str(n)).collect::<Vec<_>>();
521 h.new_array(items)
522 }))
523 }
524 "getHeaders" => {
525 let pairs = RESPONSES.with(|r| {
526 r.borrow()
527 .get(&resid)
528 .map(|st| {
529 st.headers
530 .iter()
531 .map(|(k, v)| (k.to_ascii_lowercase(), v.clone()))
532 .collect::<Vec<_>>()
533 })
534 .unwrap_or_default()
535 });
536 Ok(with_host(|h| {
537 let mut m = IndexMap::new();
538 for (k, v) in pairs {
539 m.insert(k, h.new_str(v));
540 }
541 h.new_object(m)
542 }))
543 }
544 "flushHeaders" => Ok(Value::Undef),
545 "write" => {
546 let bytes = value_bytes(args.first());
547 RESPONSES.with(|r| {
548 if let Some(st) = r.borrow_mut().get_mut(&resid) {
549 st.body.extend_from_slice(&bytes);
550 }
551 });
552 Ok(Value::Bool(true))
553 }
554 "end" => {
555 if let Some(chunk) = args.first().filter(|v| !matches!(v, Value::Undef)) {
556 let bytes = value_bytes(Some(chunk));
557 RESPONSES.with(|r| {
558 if let Some(st) = r.borrow_mut().get_mut(&resid) {
559 st.body.extend_from_slice(&bytes);
560 }
561 });
562 }
563 finish_response(res, resid)?;
564 Ok(res.clone())
565 }
566 _ => Err(crate::host::type_error(&format!(
567 "res.{method} is not a function"
568 ))),
569 }
570}
571
572fn finish_response(res: &Value, resid: u64) -> Result<(), String> {
573 let js_status = u64_prop(res, "statusCode").map(|n| n as u16);
574 let st = RESPONSES.with(|r| r.borrow_mut().remove(&resid));
575 let Some(mut st) = st else { return Ok(()) };
576 if let Some(s) = js_status {
577 st.status = s;
578 }
579 let payload = serialize_response(&mut st);
580 super::tls::socket_write(st.sock_id, &payload);
581 super::tls::socket_end(st.sock_id);
582 super::events::instance_call(res, "emit", vec![with_host(|h| h.new_str("finish"))])?;
583 Ok(())
584}
585
586fn serialize_response(st: &mut ResState) -> Vec<u8> {
590 let reason = st
591 .message
592 .clone()
593 .unwrap_or_else(|| status_text(st.status).to_string());
594 let mut out = format!("HTTP/1.1 {} {}\r\n", st.status, reason).into_bytes();
595 let has = |name: &str| st.headers.iter().any(|(k, _)| k.eq_ignore_ascii_case(name));
596 let chunked = st.headers.iter().any(|(k, v)| {
597 k.eq_ignore_ascii_case("transfer-encoding") && v.to_ascii_lowercase().contains("chunked")
598 });
599 for (k, v) in &st.headers {
600 out.extend_from_slice(format!("{k}: {v}\r\n").as_bytes());
601 }
602 if !chunked && !has("content-length") {
603 out.extend_from_slice(format!("Content-Length: {}\r\n", st.body.len()).as_bytes());
604 }
605 if !has("connection") {
606 out.extend_from_slice(b"Connection: close\r\n");
607 }
608 out.extend_from_slice(b"\r\n");
609 if st.head {
617 } else if chunked {
619 if !st.body.is_empty() {
620 out.extend_from_slice(format!("{:x}\r\n", st.body.len()).as_bytes());
621 out.extend_from_slice(&st.body);
622 out.extend_from_slice(b"\r\n");
623 }
624 out.extend_from_slice(b"0\r\n\r\n");
625 } else {
626 out.extend_from_slice(&st.body);
627 }
628 out
629}
630
631fn upsert_header(headers: &mut Vec<(String, String)>, name: &str, value: String) {
632 if let Some(slot) = headers
633 .iter_mut()
634 .find(|(k, _)| k.eq_ignore_ascii_case(name))
635 {
636 slot.1 = value;
637 } else {
638 headers.push((name.to_string(), value));
639 }
640}
641
642fn object_pairs(obj: &Value) -> Vec<(String, String)> {
643 with_host(|h| match h.get(obj) {
644 Some(JsObj::Object(p)) => p
645 .iter()
646 .filter(|(k, _)| !k.starts_with("@@") && !k.starts_with('#'))
647 .map(|(k, v)| (k.clone(), h.str_of(v)))
648 .collect(),
649 _ => Vec::new(),
650 })
651}
652
653fn status_text(code: u16) -> &'static str {
654 for &(c, msg) in super::http::status_table() {
655 if c == code {
656 return msg;
657 }
658 }
659 "OK"
660}
661
662struct ClientReq {
667 host: String,
668 port: u16,
669 servername: String,
670 reject_unauthorized: bool,
671 method: String,
672 path: String,
673 headers: Vec<(String, String)>,
674 body: Vec<u8>,
675 request: Value,
677 sent: bool,
678}
679
680pub fn request(args: &[Value], is_get: bool) -> Result<Value, String> {
683 let mut host = "localhost".to_string();
684 let mut port: u16 = 443;
685 let mut path = "/".to_string();
686 let mut method = "GET".to_string();
687 let mut servername: Option<String> = None;
688 let mut reject_unauthorized = true;
689 let mut headers: Vec<(String, String)> = Vec::new();
690 let mut cb: Option<Value> = None;
691
692 for a in args {
693 if with_host(|h| crate::host::is_callable(h, a)) {
694 cb = Some(a.clone());
695 } else if with_host(|h| h.as_str(a)).is_some() {
696 let url = with_host(|h| h.str_of(a));
698 parse_url(&url, &mut host, &mut port, &mut path);
699 } else if matches!(a, Value::Obj(_)) {
700 for key in ["hostname", "host"] {
701 if let Some(v) = get_prop(a, key).filter(|v| with_host(|h| h.as_str(v)).is_some()) {
702 host = with_host(|h| h.str_of(&v));
703 }
704 }
705 if let Some(v) = get_prop(a, "port") {
706 let n = with_host(|h| h.to_number(&v));
707 if !n.is_nan() {
708 port = n as u16;
709 }
710 }
711 if let Some(v) = get_prop(a, "path").filter(|v| with_host(|h| h.as_str(v)).is_some()) {
712 path = with_host(|h| h.str_of(&v));
713 }
714 if let Some(v) = get_prop(a, "method").filter(|v| with_host(|h| h.as_str(v)).is_some())
715 {
716 method = with_host(|h| h.str_of(&v));
717 }
718 if let Some(v) =
719 get_prop(a, "servername").filter(|v| with_host(|h| h.as_str(v)).is_some())
720 {
721 servername = Some(with_host(|h| h.str_of(&v)));
722 }
723 if let Some(v) = get_prop(a, "rejectUnauthorized") {
724 reject_unauthorized = with_host(|h| h.truthy(&v));
725 }
726 if let Some(hv) = get_prop(a, "headers").filter(|v| matches!(v, Value::Obj(_))) {
727 for (k, val) in object_pairs(&hv) {
728 headers.push((k, val));
729 }
730 }
731 }
732 }
733 if is_get {
734 method = "GET".to_string();
735 }
736 let servername = servername.unwrap_or_else(|| host.clone());
737
738 let reqid = next_reqid();
739 let mut extra = IndexMap::new();
740 extra.insert("@@reqid".into(), Value::Float(reqid as f64));
741 extra.insert("method".into(), with_host(|h| h.new_str(method.clone())));
742 extra.insert("path".into(), with_host(|h| h.new_str(path.clone())));
743 let request = super::tls::new_emitter_object("HTTPSClientRequest", extra);
744 if let Some(cb) = cb {
747 super::events::instance_call(
748 &request,
749 "on",
750 vec![with_host(|h| h.new_str("response")), cb],
751 )?;
752 }
753 CLIENT_REQS.with(|c| {
754 c.borrow_mut().insert(
755 reqid,
756 ClientReq {
757 host,
758 port,
759 servername,
760 reject_unauthorized,
761 method,
762 path,
763 headers,
764 body: Vec::new(),
765 request: request.clone(),
766 sent: false,
767 },
768 );
769 });
770 if is_get {
772 dispatch_request(reqid)?;
773 }
774 Ok(request)
775}
776
777fn parse_url(url: &str, host: &mut String, port: &mut u16, path: &mut String) {
778 let rest = url.strip_prefix("https://").unwrap_or(url);
779 let (authority, p) = match rest.find('/') {
780 Some(i) => (&rest[..i], &rest[i..]),
781 None => (rest, "/"),
782 };
783 *path = if p.is_empty() {
784 "/".to_string()
785 } else {
786 p.to_string()
787 };
788 if let Some((h, port_str)) = authority.rsplit_once(':') {
789 *host = h.to_string();
790 if let Ok(n) = port_str.parse::<u16>() {
791 *port = n;
792 }
793 } else {
794 *host = authority.to_string();
795 *port = 443;
796 }
797}
798
799fn client_request_call(req: &Value, method: &str, args: Vec<Value>) -> Result<Value, String> {
800 let reqid = u64_prop(req, "@@reqid");
801 match method {
802 "write" => {
803 if let Some(id) = reqid {
804 let bytes = value_bytes(args.first());
805 CLIENT_REQS.with(|c| {
806 if let Some(r) = c.borrow_mut().get_mut(&id) {
807 r.body.extend_from_slice(&bytes);
808 }
809 });
810 }
811 Ok(Value::Bool(true))
812 }
813 "end" => {
814 if let Some(id) = reqid {
815 if let Some(chunk) = args.first().filter(|v| !matches!(v, Value::Undef)) {
816 let bytes = value_bytes(Some(chunk));
817 CLIENT_REQS.with(|c| {
818 if let Some(r) = c.borrow_mut().get_mut(&id) {
819 r.body.extend_from_slice(&bytes);
820 }
821 });
822 }
823 dispatch_request(id)?;
824 }
825 Ok(req.clone())
826 }
827 "setHeader" => {
828 if let Some(id) = reqid {
829 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)));
830 let v = with_host(|h| h.str_of(&args.get(1).cloned().unwrap_or(Value::Undef)));
831 CLIENT_REQS.with(|c| {
832 if let Some(r) = c.borrow_mut().get_mut(&id) {
833 upsert_header(&mut r.headers, &k, v);
834 }
835 });
836 }
837 Ok(Value::Undef)
838 }
839 "getHeader" => {
840 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)))
841 .to_ascii_lowercase();
842 let val = reqid.and_then(|id| {
843 CLIENT_REQS.with(|c| {
844 c.borrow().get(&id).and_then(|r| {
845 r.headers
846 .iter()
847 .find(|(hk, _)| hk.eq_ignore_ascii_case(&k))
848 .map(|(_, v)| v.clone())
849 })
850 })
851 });
852 Ok(val
853 .map(|v| with_host(|h| h.new_str(v)))
854 .unwrap_or(Value::Undef))
855 }
856 "removeHeader" => {
857 if let Some(id) = reqid {
858 let k = with_host(|h| h.str_of(&args.first().cloned().unwrap_or(Value::Undef)));
859 CLIENT_REQS.with(|c| {
860 if let Some(r) = c.borrow_mut().get_mut(&id) {
861 r.headers.retain(|(hk, _)| !hk.eq_ignore_ascii_case(&k));
862 }
863 });
864 }
865 Ok(Value::Undef)
866 }
867 "abort" | "destroy" | "setTimeout" => Ok(req.clone()),
868 _ => Err(crate::host::type_error(&format!(
869 "req.{method} is not a function"
870 ))),
871 }
872}
873
874fn dispatch_request(reqid: u64) -> Result<(), String> {
877 let sent = CLIENT_REQS.with(|c| c.borrow().get(&reqid).map(|r| r.sent).unwrap_or(true));
879 if sent {
880 return Ok(());
881 }
882 CLIENT_REQS.with(|c| {
883 if let Some(r) = c.borrow_mut().get_mut(&reqid) {
884 r.sent = true;
885 }
886 });
887
888 let (host, port, servername, reject, method, path, headers, body) = CLIENT_REQS.with(|c| {
889 let b = c.borrow();
890 let r = b.get(&reqid).unwrap();
891 (
892 r.host.clone(),
893 r.port,
894 r.servername.clone(),
895 r.reject_unauthorized,
896 r.method.clone(),
897 r.path.clone(),
898 r.headers.clone(),
899 r.body.clone(),
900 )
901 });
902
903 let config = super::tls::client_config(reject);
904 let io_tx = with_host(|h| h.io_sender());
905 with_host(|h| h.incr_handle());
906
907 let mut has_host = false;
909 let mut has_len = false;
910 let mut header_block = String::new();
911 for (k, v) in &headers {
912 if k.eq_ignore_ascii_case("host") {
913 has_host = true;
914 }
915 if k.eq_ignore_ascii_case("content-length") {
916 has_len = true;
917 }
918 if k.eq_ignore_ascii_case("connection") {
919 continue; }
921 header_block.push_str(&format!("{k}: {v}\r\n"));
922 }
923 let host_header = if port == 443 {
924 host.clone()
925 } else {
926 format!("{host}:{port}")
927 };
928 let mut request_bytes = format!("{method} {path} HTTP/1.1\r\n");
929 if !has_host {
930 request_bytes.push_str(&format!("Host: {host_header}\r\n"));
931 }
932 request_bytes.push_str(&header_block);
933 if !has_len && !body.is_empty() {
934 request_bytes.push_str(&format!("Content-Length: {}\r\n", body.len()));
935 }
936 request_bytes.push_str("Connection: close\r\n\r\n");
937 let mut wire = request_bytes.into_bytes();
938 wire.extend_from_slice(&body);
939
940 std::thread::spawn(move || {
941 let result = exchange(&host, port, &servername, config, &wire);
942 match result {
943 Ok(raw) => {
944 let _ = io_tx.send(Box::new(move || deliver_response(reqid, raw)));
945 }
946 Err(msg) => {
947 let _ = io_tx.send(Box::new(move || deliver_error(reqid, msg)));
948 }
949 }
950 });
951 Ok(())
952}
953
954pub(crate) fn exchange(
957 host: &str,
958 port: u16,
959 servername: &str,
960 config: std::sync::Arc<rustls::ClientConfig>,
961 request: &[u8],
962) -> Result<Vec<u8>, String> {
963 let server_name = ServerName::try_from(servername.to_string())
964 .map_err(|_| format!("Error: tls: invalid servername '{servername}'"))?;
965 let sock = TcpStream::connect((host, port))
966 .map_err(|e| format!("Error: connect ECONNREFUSED {host}:{port}: {e}"))?;
967 let conn =
968 ClientConnection::new(config, server_name).map_err(|e| format!("Error: tls: {e}"))?;
969 let mut stream = StreamOwned::new(conn, sock);
970 stream
971 .write_all(request)
972 .map_err(|e| format!("Error: https write: {e}"))?;
973 stream
974 .flush()
975 .map_err(|e| format!("Error: https flush: {e}"))?;
976 let head_request = request
977 .split(|b| *b == b' ')
978 .next()
979 .is_some_and(|m| m.eq_ignore_ascii_case(b"HEAD"));
980 let mut raw = Vec::new();
981 let mut buf = [0u8; 16384];
993 loop {
994 if super::http::response_is_complete(&raw, head_request) {
995 break;
996 }
997 match stream.read(&mut buf) {
998 Ok(0) => break,
999 Ok(n) => raw.extend_from_slice(&buf[..n]),
1000 Err(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
1001 Err(e) => {
1002 if raw.is_empty() {
1003 return Err(format!("Error: https read: {e}"));
1004 }
1005 break;
1006 }
1007 }
1008 }
1009 Ok(raw)
1010}
1011
1012fn deliver_response(reqid: u64, raw: Vec<u8>) -> Result<(), String> {
1015 let entry = CLIENT_REQS.with(|c| c.borrow_mut().remove(&reqid));
1016 with_host(|h| h.decr_handle());
1017 let _ = with_host(|h| h.io_sender()).send(Box::new(|| Ok(())));
1018 let Some(entry) = entry else { return Ok(()) };
1019
1020 let (status, message, http_version, headers, body) = parse_response(&raw);
1021
1022 let headers_obj = with_host(|h| {
1023 let mut m = IndexMap::new();
1024 for (k, v) in &headers {
1025 m.insert(k.clone(), h.new_str(v.clone()));
1026 }
1027 h.new_object(m)
1028 });
1029 let mut extra = IndexMap::new();
1030 extra.insert("statusCode".into(), Value::Float(status as f64));
1031 extra.insert("statusMessage".into(), with_host(|h| h.new_str(message)));
1032 extra.insert("httpVersion".into(), with_host(|h| h.new_str(http_version)));
1033 extra.insert("headers".into(), headers_obj);
1034 let res = super::tls::new_emitter_object("IncomingMessage", extra);
1035
1036 super::events::instance_call(
1038 &entry.request,
1039 "emit",
1040 vec![with_host(|h| h.new_str("response")), res.clone()],
1041 )?;
1042 if !body.is_empty() {
1043 let chunk = super::buffer::from_bytes(&body);
1044 super::events::instance_call(&res, "emit", vec![with_host(|h| h.new_str("data")), chunk])?;
1045 }
1046 super::events::instance_call(&res, "emit", vec![with_host(|h| h.new_str("end"))])?;
1047 Ok(())
1048}
1049
1050fn deliver_error(reqid: u64, msg: String) -> Result<(), String> {
1051 let entry = CLIENT_REQS.with(|c| c.borrow_mut().remove(&reqid));
1052 with_host(|h| h.decr_handle());
1053 let _ = with_host(|h| h.io_sender()).send(Box::new(|| Ok(())));
1054 if let Some(entry) = entry {
1055 let err = with_host(|h| {
1056 let mut m = IndexMap::new();
1057 m.insert("message".into(), h.new_str(msg.clone()));
1058 h.new_object(m)
1059 });
1060 super::events::instance_call(
1061 &entry.request,
1062 "emit",
1063 vec![with_host(|h| h.new_str("error")), err],
1064 )?;
1065 }
1066 Ok(())
1067}
1068
1069fn parse_response(raw: &[u8]) -> (u16, String, String, Vec<(String, String)>, Vec<u8>) {
1072 let head_end = find_subslice(raw, b"\r\n\r\n").unwrap_or(raw.len());
1073 let head = String::from_utf8_lossy(&raw[..head_end]);
1074 let body_start = (head_end + 4).min(raw.len());
1075 let mut lines = head.split("\r\n");
1076 let status_line = lines.next().unwrap_or("");
1077 let mut sp = status_line.splitn(3, ' ');
1078 let version = sp
1079 .next()
1080 .unwrap_or("HTTP/1.1")
1081 .strip_prefix("HTTP/")
1082 .unwrap_or("1.1")
1083 .to_string();
1084 let status = sp.next().and_then(|s| s.parse::<u16>().ok()).unwrap_or(0);
1085 let message = sp.next().unwrap_or("").to_string();
1086
1087 let mut headers: Vec<(String, String)> = Vec::new();
1088 let mut chunked = false;
1089 for line in lines {
1090 if line.is_empty() {
1091 continue;
1092 }
1093 if let Some((k, v)) = line.split_once(':') {
1094 let name = k.trim().to_ascii_lowercase();
1095 let value = v.trim().to_string();
1096 if name == "transfer-encoding" && value.to_ascii_lowercase().contains("chunked") {
1097 chunked = true;
1098 }
1099 headers.push((name, value));
1100 }
1101 }
1102 let raw_body = &raw[body_start..];
1103 let body = if chunked {
1104 decode_chunked(raw_body)
1105 } else {
1106 raw_body.to_vec()
1107 };
1108 (status, message, version, headers, body)
1109}
1110
1111fn decode_chunked(mut data: &[u8]) -> Vec<u8> {
1114 let mut out = Vec::new();
1115 while let Some(nl) = find_subslice(data, b"\r\n") {
1116 let size_line = String::from_utf8_lossy(&data[..nl]);
1117 let size_hex = size_line.split(';').next().unwrap_or("").trim();
1118 let size = usize::from_str_radix(size_hex, 16).unwrap_or(0);
1119 if size == 0 {
1120 break;
1121 }
1122 let chunk_start = nl + 2;
1123 let chunk_end = (chunk_start + size).min(data.len());
1124 out.extend_from_slice(&data[chunk_start..chunk_end]);
1125 let next = chunk_end + 2;
1127 if next >= data.len() {
1128 break;
1129 }
1130 data = &data[next..];
1131 }
1132 out
1133}