nx_torrent_stream_live_gate.nx source
↩ module page · 67 lines · 4260 B
1// nx_torrent_stream_live_gate.nx -- LIVE verification of the deployed /torrent/stream endpoint.
2// Sends real HTTP Range requests to the running daemon (:8097) and checks the 206 responses against
3// the operator's real 300MIUM download. NO bash/curl -- sovereign nx_http_client over a raw socket.
4// Run (daemon must be UP): ./_offc/nx_sov_build_run.elf nx_torrent_stream_live_gate
5// depends: nishi-core.http_client
6import "nx_http_client.nx"
7
8import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
9func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func g_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11func g_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i];i=i+1} return off+g_strlen(s) }
12func g_contains(buf: *u8, n: i64, s: *u8) -> i64 {
13 let sl: i64 = g_strlen(s); if sl == 0 { return 1 }
14 var i: i64 = 0
15 while i + sl <= n { var j: i64=0; var ok: i64=1; while j<sl { if buf[i+j]!=s[j] {ok=0;j=sl} else {j=j+1} } if ok==1 {return 1} i=i+1 }
16 return 0
17}
18func g_report(label: *u8, ok: i64) -> i64 { if ok==1 { g_puts(" [PASS] " as *u8) } else { g_puts(" [FAIL] " as *u8) } g_puts(label); g_puts("\n" as *u8); return 0 }
19// print the response's first line (status) + up to 200 bytes for raw evidence.
20func g_puthead(buf: *u8, n: i64) -> i64 { var m: i64=n; if m>200 {m=200} g_puts(" > " as *u8); sys_write(1, buf, m); g_puts("\n" as *u8); return 0 }
21
22// GET `path` with a Range header, over a fresh bounded socket. returns bytes read.
23func g_get_range(path: *u8, rng: *u8, buf: *u8, cap: i64) -> i64 {
24 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0); if fd < 0 { return 0 - 1 }
25 sys_set_socket_timeout(fd, 8)
26 let addr: *u8 = sys_mmap(16); nx_http_client_sockaddr_ipv4(addr, 127, 0, 0, 1, 8097)
27 if nx_connect_bounded(fd, addr, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 0 - 1 }
28 let req: *u8 = sys_mmap(2048); var o: i64 = 0
29 o = g_cat(req, o, "GET " as *u8); o = g_cat(req, o, path); o = g_cat(req, o, " HTTP/1.1\r\nHost: 127.0.0.1\r\nRange: " as *u8)
30 o = g_cat(req, o, rng); o = g_cat(req, o, "\r\nConnection: close\r\n\r\n" as *u8)
31 sys_write(fd, req, o)
32 let got: i64 = _drain(fd, buf, cap)
33 sys_close(fd)
34 return got
35}
36
37func main() -> i64 {
38 let buf: *u8 = sys_mmap(524288)
39 var pass: i64 = 0; var tot: i64 = 0
40 let id: *u8 = "/torrent/stream/javdb.com_300MIUM-1369" as *u8
41
42 // T1: a bounded Range -> 206 + exact Content-Range over the real video length 3550168778
43 var n: i64 = g_get_range(id, "bytes=0-1023" as *u8, buf, 524288)
44 g_puthead(buf, n)
45 var ok1: i64 = 0
46 if g_contains(buf, n, "206 Partial Content" as *u8) == 1 { if g_contains(buf, n, "Content-Range: bytes 0-1023/3550168778" as *u8) == 1 { if g_contains(buf, n, "video/mp4" as *u8) == 1 { ok1 = 1 } } }
47 tot = tot + 1; if ok1 == 1 { pass = pass + 1 } g_report("GET /stream Range 0-1023 -> 206 + Content-Range 0-1023/3550168778 + video/mp4" as *u8, ok1)
48
49 // T2: SEEK -- a Range 1MB into the file -> 206 + Content-Range at that offset (proves seeking works)
50 n = g_get_range(id, "bytes=1048576-1049599" as *u8, buf, 524288)
51 g_puthead(buf, n)
52 var ok2: i64 = 0
53 if g_contains(buf, n, "206 Partial Content" as *u8) == 1 { if g_contains(buf, n, "Content-Range: bytes 1048576-1049599/3550168778" as *u8) == 1 { ok2 = 1 } }
54 tot = tot + 1; if ok2 == 1 { pass = pass + 1 } g_report("GET /stream Range 1048576-1049599 -> 206 seek" as *u8, ok2)
55
56 // T3 NEG-CONTROL: a nonexistent torrent -> NOT 206 (404 / no-metadata), never a bogus stream
57 n = g_get_range("/torrent/stream/_does_not_exist_zzz" as *u8, "bytes=0-1023" as *u8, buf, 524288)
58 g_puthead(buf, n)
59 var ok3: i64 = 0
60 if g_contains(buf, n, "206 Partial Content" as *u8) == 0 { ok3 = 1 }
61 tot = tot + 1; if ok3 == 1 { pass = pass + 1 } g_report("GET /stream <nonexistent> -> NOT 206 (honest)" as *u8, ok3)
62
63 let bb: *u8 = sys_mmap(8); bb[0]=(48+pass) as u8; bb[1]=47 as u8; bb[2]=(48+tot) as u8; bb[3]=10 as u8
64 g_puts("nx_torrent_stream_live_gate pass=" as *u8); sys_write(1, bb, 4)
65 if pass == tot { return 0 }
66 return 1
67}