code wiki / (root) / nx_win_r1probe.nx

nx_win_r1probe.nx source

↩ module page · 50 lines · 2052 B

1// nx_win_r1probe.nx -- Relays HTTP/1.0 GET requests to 127.0.0.1:8000 and forwards responses to clients. 2const K_MAGIC_5273: i64 = 5273 3const K_MAGIC_65536: i64 = 65536 4const K_MAGIC_262144: i64 = 262144 5const K_MAGIC_8000: i64 = 8000 6// nx_win_r1probe.nx -- R1 connect-thunk test: on any request, connect to 127.0.0.1:8000 (the gateway), 7// send a fixed HTTP/1.0 GET /api/characters (1.0 -> upstream closes -> no keepalive hang), relay the 8// response to the client. If :5273 returns the real character JSON -> nx_connect works + proxy works. 9func slen(s: *u8) -> i64 { var k: i64 = 0; while s[k] != (0 as u8) { k = k + 1 } return k } 10 11func sys_mmap(n: i64) -> i64 { return 0 } 12func nx_wsa() -> i64 { return 0 } 13func nx_sock() -> i64 { return 0 } 14func nx_bind(s: i64, port: i64) -> i64 { return 0 } 15func nx_listen(s: i64) -> i64 { return 0 } 16func nx_accept(s: i64) -> i64 { return 0 } 17func nx_recv(c: i64, buf: *u8, n: i64) -> i64 { return 0 } 18func nx_send(c: i64, buf: *u8, n: i64) -> i64 { return 0 } 19func nx_close(s: i64) -> i64 { return 0 } 20func nx_connect(s: i64, port: i64) -> i64 { return 0 } 21 22func main() -> i64 { 23 nx_wsa() 24 let srv: i64 = nx_sock() 25 if srv < 0 { return 1 } 26 if nx_bind(srv, K_MAGIC_5273) < 0 { return 2 } 27 if nx_listen(srv) < 0 { return 3 } 28 let req: *u8 = sys_mmap(K_MAGIC_65536) as *u8 29 let resp: *u8 = sys_mmap(K_MAGIC_262144) as *u8 30 let greq: *u8 = "GET /api/characters HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n" as *u8 31 while 1 == 1 { 32 let c: i64 = nx_accept(srv) 33 if c < 0 { return 4 } 34 nx_recv(c, req, K_MAGIC_65536) 35 let up: i64 = nx_sock() 36 if up >= 0 { 37 if nx_connect(up, K_MAGIC_8000) >= 0 { 38 nx_send(up, greq, slen(greq)) 39 var got: i64 = nx_recv(up, resp, K_MAGIC_262144) 40 while got > 0 { 41 nx_send(c, resp, got) 42 got = nx_recv(up, resp, K_MAGIC_262144) 43 } 44 } 45 nx_close(up) 46 } 47 nx_close(c) 48 } 49 return 0 50}