code wiki / _hdl_build / nx_archive_resolve_gate.nx

nx_archive_resolve_gate.nx source

↩ module page · 63 lines · 4473 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" 10import "nx_gate_verdict.nx" 11 12func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 13" as *u8); return ok } 14func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15func 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 } 16func 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 } 17func 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 } 18 19func main() -> i64 { 20 gw("archive-resolve SOVEREIGN gate (rotted URL -> serve preserved copy as a correct HTTP response)\n" as *u8) 21 var pass: i64 = 0 22 var ttl: i64 = 0 23 24 let warc: *u8 = sys_mmap(1048576) 25 let page_uri: *u8 = "https://gone.example/watch/7" as *u8 26 let page: *u8 = "<html><body>preserved</body></html>" as *u8 27 let media_uri: *u8 = "https://cdn.gone.example/7/1080.mp4" as *u8 28 let media: *u8 = sys_mmap(16) 29 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 30 let mlen: i64 = 9 31 var off: i64 = wa_write_resource(warc, 0, page_uri, "2026-07-11T00:00:00Z" as *u8, "text/html" as *u8, page, gsl(page)) 32 off = wa_write_resource(warc, off, media_uri, "2026-07-11T00:00:00Z" as *u8, "video/mp4" as *u8, media, mlen) 33 34 let resp: *u8 = sys_mmap(65536) 35 // R1: serve the preserved MEDIA -> 200 + video/mp4 + archive flag + byte-exact binary body 36 let n1: i64 = ar_build_response(warc, off, media_uri, gsl(media_uri), resp, 65536) 37 var r1: i64 = 0 38 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 { 39 let bo: i64 = find4(resp, n1, 0); if bo >= 0 { if (n1 - bo) == mlen { if memeq(resp, bo, media, mlen) == 1 { r1 = 1 } } } } } } 40 ttl=ttl+1; pass=pass+grow("R1 media -> 200 video/mp4 X-Nishi-Archive + byte-exact body\x00" as *u8, r1) 41 42 // R2: serve the preserved PAGE -> 200 + text/html + body 43 let n2: i64 = ar_build_response(warc, off, page_uri, gsl(page_uri), resp, 65536) 44 var r2: i64 = 0 45 if has(resp, n2, "HTTP/1.1 200 OK" as *u8) == 1 { if has(resp, n2, "Content-Type: text/html" as *u8) == 1 { 46 let bo: i64 = find4(resp, n2, 0); if bo >= 0 { if memeq(resp, bo, page, gsl(page)) == 1 { r2 = 1 } } } } 47 ttl=ttl+1; pass=pass+grow("R2 page -> 200 text/html + body preserved\x00" as *u8, r2) 48 49 // R3: a URL never captured -> clean 404 (search falls through to a dead link, as it does today) 50 let n3: i64 = ar_build_response(warc, off, "https://gone.example/never" as *u8, gsl("https://gone.example/never" as *u8), resp, 65536) 51 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)) 52 53 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 54 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 55 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 56 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 57 let ctr__dry: *i64 = gv_ctr() 58 ctr__dry[0] = pass 59 ctr__dry[1] = ttl 60 let rc__dry: i64 = gv_verdict("ARCHIVE-RESOLVE-GATE" as *u8, ctr__dry, "serve-from-archive: the search engine serves the preserved copy on origin rot, byte-exact)" as *u8) 61 sys_exit(rc__dry) 62 return rc__dry 63}