nx_http_health_lib.nx source
↩ module page · 69 lines · 3336 B
1// nx_http_health_lib.nx -- the REAL health check that was missing (the false-green that hid the outage:
2// supervisor said sites.elf UP, deploy said HEALTHY, maturity said PRODUCTION -- all while the page was DOWN,
3// because they checked "port bound"/"exit 0"/"gate-log green", NEVER "does the URL serve a valid page").
4// hh_probe makes an ACTUAL HTTPS request (the proven sovereign fetcher) and hh_healthy requires HTTP 200 +
5// a non-empty body + (optionally) expected content. Connection-made / 500 / empty / wrong-content = UNHEALTHY.
6// license_tier: ORIGINAL
7import "nx_deploy_lib.nx"
8const K_MAGIC_16384: i64 = 16384
9const K_MAGIC_16380: i64 = 16380
10const K_MAGIC_262144: i64 = 262144
11const K_MAGIC_262140: i64 = 262140
12
13// signed integer immediately following the FIRST occurrence of key in buf (e.g. "status="->200, "body_bytes="->-3).
14// -1 if key absent (which reads as unhealthy -- the safe default).
15func hh_after(buf: *u8, n: i64, key: *u8) -> i64 {
16 let kl: i64 = dp_len(key)
17 if kl == 0 { return 0 - 1 }
18 var i: i64 = 0
19 while i + kl <= n {
20 var k: i64 = 0
21 var m: i64 = 1
22 while k < kl { if buf[i+k] != key[k] { m = 0; k = kl } else { k = k + 1 } }
23 if m == 1 {
24 var j: i64 = i + kl
25 var neg: i64 = 0
26 if j < n { if buf[j] == (45 as u8) { neg = 1; j = j + 1 } }
27 var v: i64 = 0
28 var any: i64 = 0
29 var go: i64 = 1
30 while go == 1 {
31 if j >= n { go = 0 } else {
32 let c: i64 = buf[j] as i64
33 var d: i64 = 0
34 if c >= 48 { if c <= 57 { d = 1 } }
35 if d == 1 { v = v*10 + (c-48); j = j + 1; any = 1 } else { go = 0 }
36 }
37 }
38 if any == 1 { if neg == 1 { return 0 - v } return v }
39 return 0 - 1
40 }
41 i = i + 1
42 }
43 return 0 - 1
44}
45
46// THE real health verdict: HTTP 200 AND a non-empty body AND (expect empty OR body contains expect). Anything
47// else = UNHEALTHY. This is what turns "connection made" into "the page actually loaded".
48func hh_healthy(http_status: i64, body_bytes: i64, body: *u8, body_n: i64, expect: *u8) -> i64 {
49 if http_status != 200 { return 0 }
50 if body_bytes <= 0 { return 0 }
51 var has_expect: i64 = 0
52 if expect[0] != (0 as u8) { has_expect = 1 } // non-empty expect (robust -- no dp_len on a maybe-empty literal)
53 if has_expect == 1 { if dp_contains(body, body_n, expect) == 0 { return 0 } }
54 return 1
55}
56
57// full probe: run the sovereign HTTPS fetcher on url, parse its status + body_bytes, read the fetched body,
58// and apply hh_healthy. outfile = capture of the fetcher's stdout; bodyfile = where the fetcher saved the body.
59func hh_probe(url: *u8, expect: *u8, outfile: *u8, bodyfile: *u8) -> i64 {
60 let args: *i64 = sys_mmap(16) as *i64; args[0] = url as i64
61 dep_run_capture("_offc/nx_research_fetch.elf" as *u8, args, 1, outfile)
62 let ob: *u8 = sys_mmap(K_MAGIC_16384)
63 let on: i64 = dp_read(outfile, ob, K_MAGIC_16380)
64 let status: i64 = hh_after(ob, on, "status=" as *u8)
65 let bb: i64 = hh_after(ob, on, "body_bytes=" as *u8)
66 let body: *u8 = sys_mmap(K_MAGIC_262144)
67 let bn: i64 = dp_read(bodyfile, body, K_MAGIC_262140)
68 return hh_healthy(status, bb, body, bn, expect)
69}