code wiki / _hdl_build / nx_web_archive_gate.nx

nx_web_archive_gate.nx source

↩ module page · 71 lines · 4729 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_web_archive_gate.nx -- proves the link-rot-prevention core: archive a page + a BINARY media file, retrieve 4// each by URL byte-exact (media payload carries an embedded CRLF + NUL + 0xFF -> Content-Length-delimited, so it 5// round-trips where a line-based parser would corrupt it), a vanished URL returns not-found, and the archive 6// survives save->reload (durable). This is what lets the search engine serve a preserved copy after the origin 7// dies. license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_web_archive.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 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 } 16 17func main() -> i64 { 18 gw("web-archive SOVEREIGN gate (preserve media by URL -> retrieve byte-exact after origin dies; durable)\n" as *u8) 19 var pass: i64 = 0 20 var ttl: i64 = 0 21 22 let warc: *u8 = sys_mmap(1048576) 23 var off: i64 = 0 24 let page_uri: *u8 = "https://dead-site.example/watch/42" as *u8 25 let page: *u8 = "<html><body>preserved page</body></html>" as *u8 26 let media_uri: *u8 = "https://cdn.example/v/42/1080.mp4" as *u8 27 // binary media payload with an EMBEDDED CRLF (13,10) + NUL + 0xFF -- the round-trip trap 28 let media: *u8 = sys_mmap(32) 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]=69 as u8; media[9]=78 as u8; media[10]=68 as u8 30 let mlen: i64 = 11 31 32 off = wa_write_resource(warc, off, page_uri, "2026-07-11T00:00:00Z" as *u8, "text/html" as *u8, page, gsl(page)) 33 off = wa_write_resource(warc, off, media_uri, "2026-07-11T00:00:01Z" as *u8, "video/mp4" as *u8, media, mlen) 34 35 // W1: two records archived 36 ttl=ttl+1; pass=pass+grow("W1 archive holds 2 records\x00" as *u8, (wa_count(warc, off) == 2) as i64) 37 38 let op: *i64 = sys_mmap(8) as *i64; let ol: *i64 = sys_mmap(8) as *i64 39 // W2: retrieve the BINARY media by URL, byte-exact (incl embedded CRLF) -- integrity 40 var w2: i64 = 0 41 if wa_lookup(warc, off, media_uri, gsl(media_uri), op, ol) == 1 { if ol[0] == mlen { if memeq(warc, op[0], media, mlen) == 1 { w2 = 1 } } } 42 ttl=ttl+1; pass=pass+grow("W2 media retrieved byte-exact (binary + embedded CRLF preserved)\x00" as *u8, w2) 43 // W3: retrieve the page by URL 44 var w3: i64 = 0 45 if wa_lookup(warc, off, page_uri, gsl(page_uri), op, ol) == 1 { if ol[0] == gsl(page) { if memeq(warc, op[0], page, gsl(page)) == 1 { w3 = 1 } } } 46 ttl=ttl+1; pass=pass+grow("W3 page retrieved by URL\x00" as *u8, w3) 47 // W4: a URL never archived -> not found (clean miss, the search engine falls through) 48 ttl=ttl+1; pass=pass+grow("W4 un-archived URL -> not found\x00" as *u8, (wa_lookup(warc, off, "https://other.example/x" as *u8, gsl("https://other.example/x" as *u8), op, ol) == 0) as i64) 49 // W5: partial-URL must NOT false-match (exact URI, not substring) 50 ttl=ttl+1; pass=pass+grow("W5 partial URL 'https://cdn.example/v/42' does NOT match /1080.mp4\x00" as *u8, (wa_lookup(warc, off, "https://cdn.example/v/42" as *u8, gsl("https://cdn.example/v/42" as *u8), op, ol) == 0) as i64) 51 52 // W6: DURABLE -- save to a .warc file, reload into a fresh buffer, media still retrieves byte-exact 53 let path: *u8 = "nx_archive_test.warc" as *u8 54 wa_save(path, warc, off) 55 let warc2: *u8 = sys_mmap(1048576) 56 let n2: i64 = wa_load(path, warc2, 1048576) 57 var w6: i64 = 0 58 if n2 == off { if wa_lookup(warc2, n2, media_uri, gsl(media_uri), op, ol) == 1 { if ol[0]==mlen { if memeq(warc2, op[0], media, mlen)==1 { w6 = 1 } } } } 59 ttl=ttl+1; pass=pass+grow("W6 save->reload: media still retrievable byte-exact (durable archive)\x00" as *u8, w6) 60 61 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 62 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 63 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 64 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 65 let ctr__dry: *i64 = gv_ctr() 66 ctr__dry[0] = pass 67 ctr__dry[1] = ttl 68 let rc__dry: i64 = gv_verdict("WEB-ARCHIVE-GATE" as *u8, ctr__dry, "sovereign WARC store: media preserved by URL, byte-exact, durable -> link-rot solved for the search engine)" as *u8) 69 sys_exit(rc__dry) 70 return rc__dry 71}