code wiki / _hdl_build / nx_web_archive_gate.nx

nx_web_archive_gate.nx source

↩ module page · 63 lines · 4347 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" 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 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 } 15 16func main() -> i64 { 17 gw("web-archive SOVEREIGN gate (preserve media by URL -> retrieve byte-exact after origin dies; durable)\n" as *u8) 18 var pass: i64 = 0 19 var ttl: i64 = 0 20 21 let warc: *u8 = sys_mmap(1048576) 22 var off: i64 = 0 23 let page_uri: *u8 = "https://dead-site.example/watch/42" as *u8 24 let page: *u8 = "<html><body>preserved page</body></html>" as *u8 25 let media_uri: *u8 = "https://cdn.example/v/42/1080.mp4" as *u8 26 // binary media payload with an EMBEDDED CRLF (13,10) + NUL + 0xFF -- the round-trip trap 27 let media: *u8 = sys_mmap(32) 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]=69 as u8; media[9]=78 as u8; media[10]=68 as u8 29 let mlen: i64 = 11 30 31 off = wa_write_resource(warc, off, 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:01Z" as *u8, "video/mp4" as *u8, media, mlen) 33 34 // W1: two records archived 35 ttl=ttl+1; pass=pass+grow("W1 archive holds 2 records\x00" as *u8, (wa_count(warc, off) == 2) as i64) 36 37 let op: *i64 = sys_mmap(8) as *i64; let ol: *i64 = sys_mmap(8) as *i64 38 // W2: retrieve the BINARY media by URL, byte-exact (incl embedded CRLF) -- integrity 39 var w2: i64 = 0 40 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 } } } 41 ttl=ttl+1; pass=pass+grow("W2 media retrieved byte-exact (binary + embedded CRLF preserved)\x00" as *u8, w2) 42 // W3: retrieve the page by URL 43 var w3: i64 = 0 44 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 } } } 45 ttl=ttl+1; pass=pass+grow("W3 page retrieved by URL\x00" as *u8, w3) 46 // W4: a URL never archived -> not found (clean miss, the search engine falls through) 47 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) 48 // W5: partial-URL must NOT false-match (exact URI, not substring) 49 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) 50 51 // W6: DURABLE -- save to a .warc file, reload into a fresh buffer, media still retrieves byte-exact 52 let path: *u8 = "nx_archive_test.warc" as *u8 53 wa_save(path, warc, off) 54 let warc2: *u8 = sys_mmap(1048576) 55 let n2: i64 = wa_load(path, warc2, 1048576) 56 var w6: i64 = 0 57 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 } } } } 58 ttl=ttl+1; pass=pass+grow("W6 save->reload: media still retrievable byte-exact (durable archive)\x00" as *u8, w6) 59 60 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 61 if pass == ttl { gw("verdict=GREEN (sovereign WARC store: media preserved by URL, byte-exact, durable -> link-rot solved for the search engine)\n" as *u8); sys_exit(0); return 0 } 62 gw("verdict=RED\n" as *u8); sys_exit(1); return 1 63}