code wiki / _hdl_build / nx_archive_resolve_gate.nx

nx_archive_resolve_gate.nx source

↩ module page · 55 lines · 4087 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_archive_resolve_gate.nx -- proves serve-from-archive: given a rotted URL, the resolver builds a correct 4// HTTP response serving the PRESERVED copy (right Content-Type, X-Nishi-Archive flag, byte-exact body incl binary 5// media), and a never-captured URL yields a clean 404. This is the search-engine-facing endpoint that ends link 6// rot. license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_web_archive.nx" 9import "nx_archive_resolve.nx" 10 11func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 12" as *u8); return ok } 13func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 14func has(hay: *u8, hl: i64, needle: *u8) -> i64 { let nl: i64=gsl(needle); if nl>hl {return 0} var i: i64=0; let last: i64=hl-nl; while i<=last { var j: i64=0; var m: i64=1; while j<nl { if (hay[i+j]&0xff)!=(needle[j]&0xff){m=0;j=nl} else {j=j+1} } if m==1 {return 1} i=i+1 } return 0 } 15func find4(hay: *u8, hl: i64, from: i64) -> i64 { var i: i64=from; while i+4<=hl { if (hay[i]&0xff)==13 { if (hay[i+1]&0xff)==10 { if (hay[i+2]&0xff)==13 { if (hay[i+3]&0xff)==10 { return i+4 } } } } i=i+1 } return 0-1 } 16func memeq(a: *u8, ao: i64, b: *u8, blen: i64) -> i64 { var i: i64=0; while i<blen { if (a[ao+i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 } 17 18func main() -> i64 { 19 gw("archive-resolve SOVEREIGN gate (rotted URL -> serve preserved copy as a correct HTTP response)\n" as *u8) 20 var pass: i64 = 0 21 var ttl: i64 = 0 22 23 let warc: *u8 = sys_mmap(1048576) 24 let page_uri: *u8 = "https://gone.example/watch/7" as *u8 25 let page: *u8 = "<html><body>preserved</body></html>" as *u8 26 let media_uri: *u8 = "https://cdn.gone.example/7/1080.mp4" as *u8 27 let media: *u8 = sys_mmap(16) 28 media[0]=77 as u8; media[1]=80 as u8; media[2]=52 as u8; media[3]=0 as u8; media[4]=0x47 as u8; media[5]=13 as u8; media[6]=10 as u8; media[7]=0xFF as u8; media[8]=90 as u8 29 let mlen: i64 = 9 30 var off: i64 = wa_write_resource(warc, 0, page_uri, "2026-07-11T00:00:00Z" as *u8, "text/html" as *u8, page, gsl(page)) 31 off = wa_write_resource(warc, off, media_uri, "2026-07-11T00:00:00Z" as *u8, "video/mp4" as *u8, media, mlen) 32 33 let resp: *u8 = sys_mmap(65536) 34 // R1: serve the preserved MEDIA -> 200 + video/mp4 + archive flag + byte-exact binary body 35 let n1: i64 = ar_build_response(warc, off, media_uri, gsl(media_uri), resp, 65536) 36 var r1: i64 = 0 37 if has(resp, n1, "HTTP/1.1 200 OK" as *u8) == 1 { if has(resp, n1, "Content-Type: video/mp4" as *u8) == 1 { if has(resp, n1, "X-Nishi-Archive: preserved" as *u8) == 1 { 38 let bo: i64 = find4(resp, n1, 0); if bo >= 0 { if (n1 - bo) == mlen { if memeq(resp, bo, media, mlen) == 1 { r1 = 1 } } } } } } 39 ttl=ttl+1; pass=pass+grow("R1 media -> 200 video/mp4 X-Nishi-Archive + byte-exact body\x00" as *u8, r1) 40 41 // R2: serve the preserved PAGE -> 200 + text/html + body 42 let n2: i64 = ar_build_response(warc, off, page_uri, gsl(page_uri), resp, 65536) 43 var r2: i64 = 0 44 if has(resp, n2, "HTTP/1.1 200 OK" as *u8) == 1 { if has(resp, n2, "Content-Type: text/html" as *u8) == 1 { 45 let bo: i64 = find4(resp, n2, 0); if bo >= 0 { if memeq(resp, bo, page, gsl(page)) == 1 { r2 = 1 } } } } 46 ttl=ttl+1; pass=pass+grow("R2 page -> 200 text/html + body preserved\x00" as *u8, r2) 47 48 // R3: a URL never captured -> clean 404 (search falls through to a dead link, as it does today) 49 let n3: i64 = ar_build_response(warc, off, "https://gone.example/never" as *u8, gsl("https://gone.example/never" as *u8), resp, 65536) 50 ttl=ttl+1; pass=pass+grow("R3 un-captured URL -> 404 Not Found\x00" as *u8, has(resp, n3, "HTTP/1.1 404 Not Found" as *u8)) 51 52 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 53 if pass == ttl { gw("verdict=GREEN (serve-from-archive: the search engine serves the preserved copy on origin rot, byte-exact)\n" as *u8); sys_exit(0); return 0 } 54 gw("verdict=RED\n" as *u8); sys_exit(1); return 1 55}