code wiki / (root) / nx_http_post.nx

nx_http_post.nx source

↩ module page · 48 lines · 3124 B

1// nx_http_post.nx -- sovereign HTTP POST probe (retires Invoke-WebRequest -Method POST for login/api flow tests). 2// Composes nx_http_client's POST request builder + sockaddr, plus a read-to-EOF drain. Usage: 3// nx_http_post <path> <body> [expect-substring] [port] 4// Content-Type application/x-www-form-urlencoded. Prints "POST <path> -> status=<code> bytes=<n>" + FOUND/MISSING. 5// No shell. license_tier: ORIGINAL 6import "nx_http_client.nx" 7import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host 8import "nx_gate.nx" 9const K_MAGIC_8080: i64 = 8080 10const K_MAGIC_8192: i64 = 8192 11const K_MAGIC_4194304: i64 = 4194304 12 13func hp_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 14func 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 } 15func hp_contains(buf: *u8, n: i64, needle: *u8) -> i64 { var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} if nl==0 {return 1} var i: i64=0; 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 } return 0 } 16func hp_drain(fd: i64, buf: *u8, cap: i64) -> i64 { var off: i64=0; var go: i64=1; while go==1 { if off>=cap { go=0 } else { let r: i64=sys_read(fd, ((buf as i64)+off) as *u8, cap-off); if r<=0 { go=0 } else { off=off+r } } } return off } 17 18func main(argc: i64, argv: *i64) -> i64 { 19 if argc < 3 { gw("usage: nx_http_post <path> <body> [expect-substring] [port]\n" as *u8); sys_exit(1); return 1 } 20 let path: *u8 = argv[1] as *u8 21 let body: *u8 = argv[2] as *u8 22 var port: i64 = K_MAGIC_8080 23 if argc >= 5 { port = hp_atoi(argv[4] as *u8, 0, 12) } 24 let blen: i64 = hp_strlen(body) 25 let addr: *u8 = sys_mmap(16) 26 nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, port) 27 let fd: i64 = sys_socket(2, 1, 0) 28 if fd < 0 { gw("POST conn-fail (socket)\n" as *u8); sys_exit(2); return 2 } 29 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); gw("POST " as *u8); gw(path); gw(" -> CONN-FAIL\n" as *u8); sys_exit(2); return 2 } 30 let ct: *u8 = "application/x-www-form-urlencoded" as *u8 31 let req: *u8 = sys_mmap(K_MAGIC_8192 + blen) 32 let reqlen: i64 = nx_http_client_build_request_post(path, hp_strlen(path), "localhost" as *u8, 9, ct, hp_strlen(ct), body, blen, req) 33 sys_write(fd, req, reqlen) 34 let cap: i64 = K_MAGIC_4194304 35 let buf: *u8 = sys_mmap(cap) 36 let n: i64 = hp_drain(fd, buf, cap) 37 sys_close(fd) 38 var status: i64 = 0 39 if n > 12 { status = hp_atoi(buf, 9, 3) } 40 gw("POST " as *u8); gw(path); gw(" -> status=" as *u8); gn(status); gw(" bytes=" as *u8); gn(n); gw("\n" as *u8) 41 if argc >= 4 { 42 let needle: *u8 = argv[3] as *u8 43 let found: i64 = hp_contains(buf, n, needle) 44 gw(" expect '" as *u8); gw(needle); gw("': " as *u8) 45 if found==1 { gw("FOUND\n" as *u8) } else { gw("MISSING\n" as *u8); sys_exit(3); return 3 } 46 } 47 sys_exit(0); return 0 48}