code wiki / (root) / nx_night_fetch_test.nx

nx_night_fetch_test.nx source

↩ module page · 127 lines · 5435 B

1// nx_night_fetch_test.nx -- SOVEREIGN night-reading research fetch. 2// 3// Reader arc / night-reading rung (W-NIGHT-1): ground the eye-fatigue 4// capability + the contrast/blue-light benchmark organ on REAL sourced 5// numbers, not assumptions (global Rule 4). Reuses the team's OWN proven 6// sovereign HTTPS stack (nx_tls13_client_session_run + nx_https_get_complete; 7// no browser/node/curl), validated against the real Mozilla CA store, with 8// the identified NishiBot User-Agent. 9// 10// Fetches three fact-dense OPEN pages (en.wikipedia.org, static, NishiBot 11// allowed) -> raw HTTP responses for the render+extract rung: 12// /wiki/Web_Content_Accessibility_Guidelines -> night_wcag.raw (contrast thresholds) 13// /wiki/Color_temperature -> night_colortemp.raw (warm-Kelvin target) 14// /wiki/Melanopsin -> night_melanopsin.raw (circadian blue peak) 15// 16// expect_exit: 0 17// license_tier: ORIGINAL 18 19import "nx_syscalls.nx" 20import "nx_x509_trust_store.nx" 21import "nx_trust_store_load_from_certdata.nx" 22import "nx_tls13_client_validate_certificate.nx" 23import "nx_tls13_client_session_run.nx" 24import "nx_https_url_for_fetch.nx" 25import "nx_https_url_connect.nx" 26import "nx_https_get.nx" 27import "nx_https_get_complete.nx" 28import "nx_http_response_parse.nx" 29 30func nf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 31func nf_putn(v: i64) -> i64 { 32 let bb: *u8 = sys_mmap(28); var m: i64 = v 33 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 34 let t: *u8 = sys_mmap(28); var k: i64 = 0 35 if m == 0 { t[0] = 48 as u8; k = 1 } 36 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 37 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 38} 39func nf_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 40 41// Fetch one page over the sovereign TLS1.3 client, save the raw response. 42// Returns the HTTP status (200 ok), or a negative error code. 43func fetch_page(store: *TrustStore, full_url: *u8, path: *u8, path_len: i64, out_path: *u8) -> i64 { 44 nf_puts("--- fetch "); nf_puts(full_url); nf_puts("\n") 45 46 // ephemeral client keys 47 let cr: *u8 = sys_mmap(32) 48 var i: i64 = 0 49 while i < 32 { cr[i] = (0xC0 + i) as u8; i = i + 1 } 50 let priv: *u8 = sys_mmap(32) 51 i = 0 52 while i < 32 { priv[i] = (0xA0 + i) as u8; i = i + 1 } 53 54 let url_p: *NxUrl = nx_url_new() 55 let target_raw: *u8 = sys_mmap(32) 56 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget 57 target.url = url_p 58 target.port = 0 59 if nx_https_url_for_fetch(full_url, target) != NX_HTTPS_URL_OK { return 0 - 41 } 60 61 let fd_p: *i64 = sys_mmap(16) as *i64 62 if nx_https_url_connect(target, full_url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 0 - 42 } 63 let fd: i64 = *fd_p 64 65 let val_ctx_raw: *u8 = sys_mmap(64) 66 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext 67 val_ctx.store = store 68 val_ctx.sni_host = full_url + target.url.host_off 69 val_ctx.sni_host_len = target.url.host_len 70 val_ctx.now_epoch = sys_now_realtime_sec() 71 72 let sr: i64 = nx_tls13_client_session_run( 73 fd, full_url + target.url.host_off, target.url.host_len, 74 cr, priv, val_ctx 75 ) 76 if sr <= 0 { sys_close(fd); return 0 - (200 + (0 - sr)) } 77 78 let session: *Tls13ClientSession = sr as *Tls13ClientSession 79 let buf: *u8 = sys_mmap(2097152) 80 let gc: i64 = nx_https_get_complete( 81 session, fd, path, path_len, 82 full_url + target.url.host_off, target.url.host_len, 83 buf, 2097152 84 ) 85 sys_close(fd) 86 if gc < 0 { return 0 - (100 + (0 - gc)) } 87 88 let rs: *i64 = sys_mmap(128) as *i64 89 nx_http_response_parse(buf, gc, rs) 90 let status: i64 = rs[1] 91 92 let ofd: i64 = sys_openat_wr(out_path, 0x1A4) 93 if ofd <= 0 { return 0 - 70 } 94 sys_write(ofd, buf, gc) 95 sys_close(ofd) 96 97 nf_puts(" ST="); nf_putn(status); nf_puts(" GC="); nf_putn(gc); nf_puts(" -> "); nf_puts(out_path); nf_puts("\n") 98 return status 99} 100 101func main() -> i64 { 102 let cpath: *u8 = "/tmp/mozilla_certdata.txt\x00" 103 let r: i64 = nx_trust_store_load_from_certdata(cpath, 512, 4194304) 104 if r <= 0 { nf_puts("NIGHT-FETCH: certdata load failed\n"); return 1 } 105 let store: *TrustStore = r as *TrustStore 106 let n: i64 = trust_store_count(store) 107 if n < 50 { nf_puts("NIGHT-FETCH: too few CAs\n"); return 3 } 108 nf_puts("CA="); nf_putn(n); nf_puts("\n") 109 110 var ok: i64 = 0 111 112 let u1: *u8 = "https://en.wikipedia.org/wiki/Web_Content_Accessibility_Guidelines\x00" 113 let p1: *u8 = "/wiki/Web_Content_Accessibility_Guidelines\x00" 114 if fetch_page(store, u1, p1, nf_strlen(p1), "knowledge/fetched/night_wcag.raw\x00" as *u8) == 200 { ok = ok + 1 } 115 116 let u2: *u8 = "https://en.wikipedia.org/wiki/Color_temperature\x00" 117 let p2: *u8 = "/wiki/Color_temperature\x00" 118 if fetch_page(store, u2, p2, nf_strlen(p2), "knowledge/fetched/night_colortemp.raw\x00" as *u8) == 200 { ok = ok + 1 } 119 120 let u3: *u8 = "https://en.wikipedia.org/wiki/Melanopsin\x00" 121 let p3: *u8 = "/wiki/Melanopsin\x00" 122 if fetch_page(store, u3, p3, nf_strlen(p3), "knowledge/fetched/night_melanopsin.raw\x00" as *u8) == 200 { ok = ok + 1 } 123 124 nf_puts("NIGHT-SOVEREIGN-FETCH-OK pages_200="); nf_putn(ok); nf_puts("/3\n") 125 if ok < 3 { return 51 } 126 return 0 127}