code wiki / (root) / nx_http_probe.nx

nx_http_probe.nx source

↩ module page · 45 lines · 2528 B

1// nx_http_probe.nx -- sovereign HTTP GET probe; retires Invoke-WebRequest for checking localhost endpoints. Composes 2// nx_http_client_get (TCP connect + GET + read-to-EOF) with positional argv and the canonical nx_gate output. Usage: 3// nx_http_probe <path> [expect-substring] [port] 4// Prints "GET <path> -> status=<code> bytes=<n>"; if expect given, FOUND/MISSING (exit 3 on MISSING, 2 on conn-fail). 5// No shell, no curl, no PowerShell. license_tier: ORIGINAL 6import "nx_http_client.nx" 7import "nx_gate.nx" 8const K_MAGIC_8080: i64 = 8080 9const K_MAGIC_4194304: i64 = 4194304 10 11func hp_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 12// parse up to `len` decimal digits at buf[off..] 13func hp_atoi(buf: *u8, off: i64, len: i64) -> i64 { var v: i64=0; var i: i64=0; while i<len { let d: i64=buf[off+i] as i64; if d>=48 { if d<=57 { v=v*10+(d-48) } else { i=len } } else { i=len } i=i+1 } return v } 14// substring search: 1 if needle occurs in buf[0..n) 15func hp_contains(buf: *u8, n: i64, needle: *u8) -> i64 { 16 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 17 if nl==0 { return 1 } 18 var i: i64=0 19 while i+nl<=n { var m: i64=1; var j: i64=0; while j<nl { if buf[i+j]!=needle[j] { m=0; j=nl } else { j=j+1 } } if m==1 { return 1 } i=i+1 } 20 return 0 21} 22 23func main(argc: i64, argv: *i64) -> i64 { 24 if argc < 2 { gw("usage: nx_http_probe <path> [expect-substring] [port]\n" as *u8); sys_exit(1); return 1 } 25 let path: *u8 = argv[1] as *u8 26 var port: i64 = K_MAGIC_8080 27 if argc >= 4 { port = hp_atoi(argv[3] as *u8, 0, 12) } 28 let addr: *u8 = sys_mmap(16) 29 nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, port) 30 let cap: i64 = K_MAGIC_4194304 31 let buf: *u8 = sys_mmap(cap) 32 let verdict: *i64 = sys_mmap(8) as *i64 33 let n: i64 = nx_http_client_get(addr, path, hp_strlen(path), "localhost" as *u8, 9, buf, cap, verdict) 34 if n < 0 { gw("GET " as *u8); gw(path); gw(" -> CONN-FAIL (verdict=" as *u8); gn(verdict[0]); gw(")\n" as *u8); sys_exit(2); return 2 } 35 var status: i64 = 0 36 if n > 12 { status = hp_atoi(buf, 9, 3) } 37 gw("GET " as *u8); gw(path); gw(" -> status=" as *u8); gn(status); gw(" bytes=" as *u8); gn(n); gw("\n" as *u8) 38 if argc >= 3 { 39 let needle: *u8 = argv[2] as *u8 40 let found: i64 = hp_contains(buf, n, needle) 41 gw(" expect '" as *u8); gw(needle); gw("': " as *u8) 42 if found==1 { gw("FOUND\n" as *u8) } else { gw("MISSING\n" as *u8); sys_exit(3); return 3 } 43 } 44 sys_exit(0); return 0 45}