code wiki / _hdl_build / nx_browser_view_serve.nx
nx_browser_view_serve.nx source
↩ module page · 59 lines · 2756 B
1// nx_browser_view_serve.nx -- surface the nishi browser's render as a web page reachable from the
2// WINDOWS browser via WSL2 localhost-forwarding (identical path to the :8097 media daemon, nx_torrent_daemon).
3// Pure COMPOSITION, no new server machinery: the sovereign HTTP primitive (nx_http_server) + static file
4// serving (nx_pages_static, sets image/png). Bind 0.0.0.0 -> accept -> read_request -> serve _offc/<file>.
5// Route bare "/" -> the viewer page (nx_pages_serve_file 404s on "/" by design). Both imports pull only
6// nx_syscalls.nx (no nx_syscalls_x86_64 dup), so the composite assembles.
7import "nx_syscalls.nx"
8import "nx_http.nx"
9import "nx_http_server.nx"
10import "nx_pages_static.nx"
11
12const BV_PORT: i64 = 8099
13const BV_BACKLOG: i64 = 64
14const BV_REQ_CAP: i64 = 16384
15const BV_RESP_CAP: i64 = 4194304 // 4 MB (a 1000x1000 PNG fits with room to spare)
16
17// serve one connection: read the request, remap bare "/" to the viewer, serve the file from _offc/.
18func bv_serve_one(cfd: i64) -> i64 {
19 let req_buf: *u8 = sys_mmap(BV_REQ_CAP)
20 let method: *i64 = sys_mmap(8) as *i64
21 let poff: *i64 = sys_mmap(8) as *i64
22 let plen: *i64 = sys_mmap(8) as *i64
23 let clen: *i64 = sys_mmap(8) as *i64
24 let boff: *i64 = sys_mmap(8) as *i64
25 let rn: *i64 = sys_mmap(8) as *i64
26 let rc: i64 = nx_http_server_read_request(cfd, req_buf, BV_REQ_CAP, method, poff, plen, clen, boff, rn)
27 if rc != NXS_OK { sys_close(cfd); return 1 }
28
29 var path: *u8 = ((req_buf as i64) + poff[0]) as *u8
30 var path_n: i64 = plen[0]
31 if path_n == 1 { path = "/browser_view.html\x00" as *u8; path_n = 18 } // bare "/" -> viewer
32
33 let resp_buf: *u8 = sys_mmap(BV_RESP_CAP)
34 let resp_n: *i64 = sys_mmap(8) as *i64
35 resp_n[0] = 0
36 nx_pages_serve_file(path, path_n, "_offc\x00" as *u8, 5, method[0], resp_buf, BV_RESP_CAP, resp_n)
37 nx_http_server_send_response(cfd, resp_buf, resp_n[0])
38 return 0
39}
40
41func main() -> i64 {
42 let addr_buf: *u8 = sys_mmap(16)
43 nx_http_server_addr_any(addr_buf, BV_PORT) // 0.0.0.0 : WSL2 -> Windows localhost fwd
44 let verdict: *i64 = sys_mmap(8) as *i64
45 let listen_fd: i64 = nx_http_server_listen(addr_buf, BV_BACKLOG, verdict)
46 if listen_fd < 0 { return 30 + verdict[0] }
47 if listen_fd == 0 { return 21 }
48 let banner: *u8 = "nx_browser_view_serve: http://localhost:8099/ (0.0.0.0:8099, docroot _offc)\n\x00" as *u8
49 var bn: i64 = 0
50 while banner[bn] != 0 as u8 { bn = bn + 1 }
51 sys_write(1, banner, bn)
52 var running: i64 = 1
53 while running == 1 {
54 let cfd: i64 = nx_http_server_accept_one(listen_fd, verdict)
55 if cfd >= 0 { bv_serve_one(cfd) }
56 }
57 sys_close(listen_fd)
58 return 0
59}