nx_mock_web.nx source
↩ module page · 85 lines · 4324 B
1// nx_mock_web.nx -- a tiny loopback "web" for end-to-end crawl testing.
2// Serves a few real HTML pages by path (including an adult-aggregator-style
3// page) so we can prove the crawler finds uncensored content over real HTTP,
4// in-sandbox, with no external network. Run alongside nx_crawl_run.
5import "nx_str.nx"
6import "nx_http_server.nx"
7const NX_MAGIC_8192: i64 = 8192
8const NX_MAGIC_16384: i64 = 16384
9
10const NX_MOCK_PORT: i64 = 8088
11
12func bputc(buf: *u8, pos: *i64, cap: i64, c: i64) -> i64 { if pos[0] < cap { buf[pos[0]] = c; pos[0] = pos[0] + 1 } return 0 }
13func bput(buf: *u8, pos: *i64, cap: i64, s: *u8) -> i64 { let n: i64 = nx_str_len(s); var i: i64 = 0; while i < n { bputc(buf, pos, cap, s[i] as i64); i = i + 1 } return 0 }
14
15// does req[off..off+len) contain needle?
16func mw_contains(req: *u8, off: i64, len: i64, needle: *u8) -> i64 {
17 let nlen: i64 = nx_str_len(needle)
18 if nlen == 0 { return 1 }
19 var i: i64 = off
20 let end: i64 = off + len - nlen
21 while i <= end {
22 var j: i64 = 0
23 var ok: i64 = 1
24 while j < nlen {
25 if (req[i + j] as i64) != (needle[j] as i64) { ok = 0; j = nlen } else { j = j + 1 }
26 }
27 if ok == 1 { return 1 }
28 i = i + 1
29 }
30 return 0
31}
32
33func main() -> i64 {
34 let addr: *u8 = sys_mmap(16)
35 nx_http_server_addr_any(addr, NX_MOCK_PORT)
36 let vbox: *i64 = sys_mmap(8) as *i64
37 let lfd: i64 = nx_http_server_listen(addr, 16, vbox)
38 if lfd < 0 { return 2 }
39
40 var serving: i64 = 1
41 while serving == 1 {
42 let cfd: i64 = nx_http_server_accept_one(lfd, vbox)
43 if cfd >= 0 {
44 let req: *u8 = sys_mmap(NX_MAGIC_8192)
45 let m: *i64 = sys_mmap(8) as *i64
46 let po: *i64 = sys_mmap(8) as *i64
47 let pl: *i64 = sys_mmap(8) as *i64
48 let cl: *i64 = sys_mmap(8) as *i64
49 let bo: *i64 = sys_mmap(8) as *i64
50 let rn: *i64 = sys_mmap(8) as *i64
51 nx_http_server_read_request(cfd, req, NX_MAGIC_8192, m, po, pl, cl, bo, rn)
52
53 let resp: *u8 = sys_mmap(NX_MAGIC_16384)
54 let pos: *i64 = sys_mmap(8) as *i64
55 pos[0] = 0
56 bput(resp, pos, NX_MAGIC_16384, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n")
57 var served: i64 = 0
58 if mw_contains(req, po[0], pl[0], "robots") == 1 {
59 bput(resp, pos, NX_MAGIC_16384, "User-agent: *\nDisallow: /private\nCrawl-delay: 1\n")
60 served = 1
61 }
62 if served == 0 { if mw_contains(req, po[0], pl[0], "private") == 1 {
63 bput(resp, pos, NX_MAGIC_16384, "<html><head><title>Diora Baird private notes</title></head><body><p>host-private area, robots-disallowed</p></body></html>")
64 served = 1
65 } }
66 if served == 0 { if mw_contains(req, po[0], pl[0], "thothub") == 1 {
67 bput(resp, pos, NX_MAGIC_16384, "<html><head><title>Diora Baird leaked video - thothub</title></head><body><h1>Diora Baird</h1><p>Diora Baird nude leaked video and photos aggregated here for free streaming.</p></body></html>")
68 served = 1
69 } }
70 if served == 0 { if mw_contains(req, po[0], pl[0], "wiki") == 1 {
71 bput(resp, pos, NX_MAGIC_16384, "<html><head><title>Diora Baird - Wikipedia</title></head><body><h1>Diora Baird</h1><p>Diora Baird is an American actress and model born in 1983, known for Wedding Crashers and Cobra Kai.</p></body></html>")
72 served = 1
73 } }
74 if served == 0 { if mw_contains(req, po[0], pl[0], "fan") == 1 {
75 bput(resp, pos, NX_MAGIC_16384, "<html><head><title>Diora Baird fan gallery</title></head><body><p>Diora Baird comic con photos, candid pictures and a fan tribute gallery.</p></body></html>")
76 served = 1
77 } }
78 if served == 0 {
79 bput(resp, pos, NX_MAGIC_16384, "<html><body><h1>mock web</h1><a href='http://127.0.0.1:8088/wiki'>wiki</a> <a href='http://127.0.0.1:8088/thothub'>thothub</a> <a href='http://127.0.0.1:8088/fan'>fan</a> <a href='http://127.0.0.1:8088/private'>private</a></body></html>")
80 }
81 nx_http_server_send_response(cfd, resp, pos[0])
82 }
83 }
84 return 0
85}