code wiki / _hdl_build / nx_health_template.nx
nx_health_template.nx source
↩ module page · 55 lines · 3177 B
1// nx_health_template.nx -- CONTENT-AWARE health verdict (operator 2026-06-29: "it also needs to check against
2// KNOWN-GOOD TEMPLATES -- a 404 being 'up' with a 200 status is wrong"). HTTP 200 is NOT enough: an error/404
3// page served WITH a 200 status, or a wrong/blank page, is a FALSE-GREEN -- exactly what the operator hates.
4// This validates the actual response BODY against (a) a KNOWN-GOOD template MARKER the page MUST contain, and
5// (b) a built-in set of error-page fingerprints it must NOT contain. So "200 + 404-content" is correctly DOWN.
6// PURE transform htpl_verdict(body,n,status,expect,en) -> code (independently gateable; the IO/data layers wire
7// the fetch). The known-good marker per URL is data-driven (health_templates.conf). license_tier: ORIGINAL
8import "nx_syscalls.nx"
9
10const HT_OK: i64 = 0 // 200 + matches the known-good template + no error fingerprint
11const HT_WRONG_STATUS: i64 = 1 // non-200 status
12const HT_ERROR_CONTENT: i64 = 2 // body is an error page despite the status (the operator's "200-but-404" case)
13const HT_MISSING_TEMPLATE: i64 = 3 // body does NOT contain the known-good marker (wrong/blank/placeholder page)
14
15func htpl_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
16
17func htpl_contains(hay: *u8, n: i64, needle: *u8, nn: i64) -> i64 {
18 if nn == 0 { return 1 }
19 var i: i64 = 0
20 while i + nn <= n {
21 var j: i64 = 0
22 var m: i64 = 1
23 while j < nn { if hay[i + j] != needle[j] { m = 0; j = nn } else { j = j + 1 } }
24 if m == 1 { return 1 }
25 i = i + 1
26 }
27 return 0
28}
29
30// built-in error-page fingerprints: a body containing any of these IS an error page, even with a 200 status.
31// "404" alone is too weak (could be a legit id/phone) -> require it together with a "ot found" (Not/not found).
32func htpl_has_error(body: *u8, n: i64) -> i64 {
33 if htpl_contains(body, n, "404" as *u8, 3) == 1 { if htpl_contains(body, n, "ot found" as *u8, 8) == 1 { return 1 } }
34 if htpl_contains(body, n, "Not Found" as *u8, 9) == 1 { return 1 }
35 if htpl_contains(body, n, "Internal Server Error" as *u8, 21) == 1 { return 1 }
36 if htpl_contains(body, n, "Bad Gateway" as *u8, 11) == 1 { return 1 }
37 if htpl_contains(body, n, "Service Unavailable" as *u8, 19) == 1 { return 1 }
38 if htpl_contains(body, n, "502 " as *u8, 4) == 1 { return 1 }
39 if htpl_contains(body, n, "503 " as *u8, 4) == 1 { return 1 }
40 return 0
41}
42
43// THE CONTENT-AWARE VERDICT. status = HTTP status code; expect/en = the known-good template MARKER the page MUST
44// contain (data-driven per URL). Order: wrong-status -> error-content (200-but-error) -> missing-template -> OK.
45func htpl_verdict(body: *u8, n: i64, status: i64, expect: *u8, en: i64) -> i64 {
46 if status != 200 { return HT_WRONG_STATUS }
47 if htpl_has_error(body, n) == 1 { return HT_ERROR_CONTENT }
48 if en > 0 { if htpl_contains(body, n, expect, en) == 0 { return HT_MISSING_TEMPLATE } }
49 return HT_OK
50}
51
52func htpl_healthy(body: *u8, n: i64, status: i64, expect: *u8, en: i64) -> i64 {
53 if htpl_verdict(body, n, status, expect, en) == HT_OK { return 1 }
54 return 0
55}