nx_browser_page_demo_test.nx source
↩ module page · 133 lines · 4857 B
1// nx_browser_page_demo_test.nx -- the browser renders a REAL webpage.
2//
3// Fetches http://example.com/ with the bits-up stack (DNS + sockets +
4// HTTP/1.1), then renders the HTML to readable text via nx_html_to_text
5// (tag strip + entity decode + script/style removal). This is a
6// sovereign text-mode browser -- Lynx/w3m class -- rendering a real
7// page off the live internet, every byte NishiLang.
8//
9// PROOF (not a claim): the rendered text MUST contain the real page's
10// own words ("Example Domain" + "illustrative examples"). The rendered
11// text is also written to nishi-browser-proofs/rendered_example_com.txt
12// so it can be read + diffed against the live page.
13//
14// expect_exit: 0
15// license_tier: ORIGINAL
16
17import "nx_syscalls.nx"
18import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
19import "nx_dns_resolve_a_record.nx"
20import "nx_https_url_connect.nx"
21import "nx_http_client.nx"
22import "nx_http_response_parse.nx"
23import "nx_html_to_text.nx"
24
25func _contains(hay: *u8, hlen: i64, needle: *u8, nlen: i64) -> i64 {
26 if nlen == 0 { return 1 }
27 if hlen < nlen { return 0 }
28 let last: i64 = hlen - nlen
29 var i: i64 = 0
30 while i <= last {
31 var j: i64 = 0
32 var ok: i64 = 1
33 while j < nlen {
34 if hay[i + j] != needle[j] { ok = 0; j = nlen }
35 j = j + 1
36 }
37 if ok == 1 { return 1 }
38 i = i + 1
39 }
40 return 0
41}
42
43func _slen(s: *u8) -> i64 {
44 var n: i64 = 0
45 while s[n] != 0 { n = n + 1 }
46 return n
47}
48
49func main() -> i64 {
50 let host: *u8 = "example.com\x00"
51 let host_len: i64 = 11
52 let path: *u8 = "/\x00"
53
54 // ---- DNS + TCP:80 ----
55 let dns: *DnsResolveResult = nx_dns_resolve_default(host, host_len, 1779284141)
56 if dns.verdict != NX_DNS_R_OK { return 30 }
57 if dns.ipv4_packed == 0 { return 31 }
58 let fd: i64 = sys_socket(NX_HTTPS_AF_INET, NX_HTTPS_SOCK_STREAM, 0)
59 if fd < 0 { return 32 }
60 let sa: *u8 = sys_mmap(16)
61 nx_https_build_sockaddr(sa, dns.ipv4_packed, 80)
62 if nx_connect_bounded(fd, sa, 16, NX_CONN_DEFAULT_MS) < 0 { sys_close(fd); return 33 }
63
64 // ---- GET / ----
65 let req: *u8 = sys_mmap(256)
66 let req_len: i64 = nx_http_client_build_request(path, 1, host, host_len, req)
67 if sys_write(fd, req, req_len) < 0 { sys_close(fd); return 34 }
68
69 // ---- recv to EOF (Connection: close) ----
70 let cap: i64 = 131072
71 let buf: *u8 = sys_mmap(cap)
72 var total: i64 = 0
73 var budget: i64 = 100000
74 var reading: i64 = 1
75 while reading == 1 {
76 if total >= cap { reading = 0 }
77 if budget <= 0 { reading = 0 }
78 if reading == 1 {
79 let got: i64 = sys_read(fd, buf + total, cap - total)
80 if got <= 0 { reading = 0 }
81 if got > 0 { total = total + got }
82 budget = budget - 1
83 }
84 }
85 sys_close(fd)
86 if total < 16 { return 35 }
87
88 // ---- parse HTTP, locate HTML body ----
89 let resp: *i64 = sys_mmap(128) as *i64
90 if nx_http_response_parse(buf, total, resp) != 0 { return 50 }
91 let status: i64 = resp[1]
92 if status != 200 { return 50 + status }
93 let body_off: i64 = resp[6]
94 let body_kind: i64 = resp[8]
95 // Strip Transfer-Encoding: chunked framing if present (example.com
96 // serves chunked: the body begins with the hex chunk size "210").
97 var html: *u8 = buf + body_off
98 var html_len: i64 = total - body_off
99 if body_kind == NX_HTTP_BODY_CHUNKED {
100 let dch: *u8 = sys_mmap(131072)
101 html_len = nx_http_dechunk(buf + body_off, total - body_off, dch, 131072)
102 html = dch
103 }
104 if html_len <= 0 { return 55 }
105
106 // ---- render HTML -> readable text ----
107 let text: *u8 = sys_mmap(32768)
108 let text_len: i64 = nx_html_to_text(html, html_len, text, 32768)
109 if text_len <= 0 { return 60 }
110
111 // ---- emit rendered page to stdout + proof file ----
112 let banner: *u8 = "\n=== example.com rendered by the Nishi browser (bits-up, text mode) ===\n\x00"
113 sys_write(1, banner, 71)
114 sys_write(1, text, text_len)
115 sys_write(1, banner, 1)
116
117 let outp: *u8 = "/mnt/c/Users/elder/nishi-browser-proofs/rendered_example_com.txt\x00"
118 let ofd: i64 = sys_openat_wr(outp, 0x1A4)
119 if ofd > 0 { sys_write(ofd, text, text_len); sys_close(ofd) }
120
121 // ---- PROOF: rendered text contains the real page's own words ----
122 let n1: *u8 = "Example Domain\x00"
123 let n2: *u8 = "documentation examples\x00"
124 let n3: *u8 = "without needing permission\x00"
125 if _contains(text, text_len, n1, _slen(n1)) != 1 { return 80 }
126 if _contains(text, text_len, n2, _slen(n2)) != 1 { return 81 }
127 if _contains(text, text_len, n3, _slen(n3)) != 1 { return 82 }
128
129 let pass: *u8 = sys_mmap(16)
130 pass[0]=0x50; pass[1]=0x41; pass[2]=0x53; pass[3]=0x53; pass[4]=0x0A
131 sys_write(2, pass, 5)
132 return 0
133}