code wiki / _hdl_build / nx_url_get.nx

nx_url_get.nx source

↩ module page · 67 lines · 3014 B

1// nx_url_get.nx -- the sovereign FETCH-VERIFY CLI that was missing from the live-publish loop. A thin, 2// reusable wrapper over nx_fetch_staged (validated TLS-1.3 HTTPS GET + redirect-follow + chunked decode, the 3// same stack the researcher fetches with) so EVERY publish can prove itself: fetch the live URL back, report 4// the byte count, and (optionally) confirm a content marker is present -- which is stronger than a bare 200, 5// because it proves the LIVE bytes are OUR page, not an error/placeholder. Closes the verify leg of the 6// emit -> push -> VERIFY loop (the "live as we work, no hand-waving" standard). 7// usage: nx_url_get <https-url> [expected-substring-marker] 8// prints: FETCH OK url=<u> bytes=<n> [+ VERIFIED marker present | WARN marker NOT found] 9// exit: 0 ok (and marker present if given) / 1 fetch failed / 2 fetched but marker absent / 3 usage 10// Sovereign: nx_fetch_unit (nx_https_get stack: our DNS + TLS-1.3 + X.509 trust) + nx_syscalls. No curl. license_tier: ORIGINAL 11import "nx_fetch_unit.nx" 12import "nx_syscalls.nx" 13 14func ug_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 15func ug_w(s: *u8) -> i64 { sys_write(1, s, ug_len(s)); return 0 } 16func ug_wn(v: i64) -> i64 { 17 let bb: *u8 = sys_mmap(28); var m: i64 = v 18 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 19 let t: *u8 = sys_mmap(28); var k: i64 = 0 20 if m == 0 { t[0] = 48 as u8; k = 1 } 21 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 22 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 23 sys_write(1, bb, k) 24 return 0 25} 26// substring search: is needle present in hay[0..n)? 27func ug_find(hay: *u8, n: i64, needle: *u8) -> i64 { 28 let nn: i64 = ug_len(needle) 29 if nn == 0 { return 0 } 30 var i: i64 = 0 31 while i + nn <= n { 32 var j: i64 = 0 33 var ok: i64 = 1 34 while j < nn { if hay[i + j] != needle[j] { ok = 0; j = nn } else { j = j + 1 } } 35 if ok == 1 { return 1 } 36 i = i + 1 37 } 38 return 0 39} 40 41func main(argc: i64, argv: *i64) -> i64 { 42 if argc < 2 { sys_write(2, "usage: nx_url_get <https-url> [marker]\n" as *u8, 39); sys_exit(3); return 3 } 43 let url: *u8 = argv[1] as *u8 44 var marker: *u8 = 0 as *u8 45 if argc >= 3 { marker = argv[2] as *u8 } 46 47 let body: *u8 = sys_mmap(FU_CAP) 48 let n: i64 = nx_fetch_staged(url, body, FU_CAP) 49 if n <= 0 { 50 ug_w("FETCH FAIL url=" as *u8); ug_w(url); ug_w(" rc=" as *u8); ug_wn(n); ug_w("\n" as *u8) 51 sys_exit(1) 52 return 1 53 } 54 ug_w("FETCH OK url=" as *u8); ug_w(url); ug_w(" bytes=" as *u8); ug_wn(n); ug_w("\n" as *u8) 55 if (marker as i64) != 0 { 56 if ug_find(body, n, marker) == 1 { 57 ug_w("VERIFIED marker present: " as *u8); ug_w(marker); ug_w("\n" as *u8) 58 sys_exit(0) 59 return 0 60 } 61 ug_w("WARN marker NOT found: " as *u8); ug_w(marker); ug_w("\n" as *u8) 62 sys_exit(2) 63 return 2 64 } 65 sys_exit(0) 66 return 0 67}