code wiki / _hdl_build / nx_cleanserve_resolve.nx
nx_cleanserve_resolve.nx source
↩ module page · 40 lines · 2684 B
1// nx_cleanserve_resolve.nx -- shared last-resort media-resolve helpers for the clean-serve daemon + nx_cleanview.
2// Extracted so the URL parser is GATE-TESTABLE (a bug here = a broken <video src> the deploy health-check can't
3// catch). No state, no network -- pure parsing over a sniff plan + a cheap page pre-check. Co-located in _hdl_build
4// with nx_video_sniff.nx (its dependency) so imports resolve. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_video_sniff.nx" // vs_sniff_page_bundled (GUARDED, no-network) / vs_extract_script_srcs / mjx_contains / VSK_*
7import "nx_video_sniff_fetch.nx" // nx_hls_resolve_uri -- absolute-URL resolver for external-bundle _best-fetch
8
9const CSD_SNIFF_CPU_SECS: i64 = 2 // JS-exec resolve is CPU-bounded; only fires when static extraction found nothing on a video-ish page
10const CSD_MAXSRC: i64 = 6 // bounded external <script src> bundles fetched per request -- DoS-amplification guard for the PUBLIC route (1 req !-> unbounded outbound)
11const CSD_SRCCAP: i64 = 65536 // <script src> list buffer
12const CSD_EXTCAP: i64 = 4194304 // 4 MiB total concat of fetched bundle bodies fed to the JS-VM
13const CSD_BUNCAP: i64 = 4194304 // 4 MiB per-bundle fetch buffer
14
15// extract the URL= field from a sniff plan ("ROUTE=<r> URL=<url>") into out; returns length (0 if none / "-" placeholder)
16func csd_plan_url(plan: *u8, out: *u8, cap: i64) -> i64 {
17 var i: i64 = 0; var found: i64 = 0-1
18 while plan[i] != (0 as u8) {
19 if found < 0 { if (plan[i]&0xff)==85 { if (plan[i+1]&0xff)==82 { if (plan[i+2]&0xff)==76 { if (plan[i+3]&0xff)==61 { found = i+4 } } } } }
20 i=i+1
21 }
22 if found < 0 { return 0 }
23 var j: i64 = found; var n: i64 = 0; var stop: i64 = 0
24 while stop==0 { let c: i64 = plan[j]&0xff
25 if c==0 { stop=1 } else { if c==32 { stop=1 } else { if n<cap { out[n]=c as u8; n=n+1 } j=j+1 } }
26 }
27 if n==1 { if (out[0]&0xff)==45 { return 0 } } // bare "-" = no stream surfaced
28 return n
29}
30// cheap pre-check: does the page even look like it has a player? gates the (bounded) JS-VM so text/news pages stay fast
31func csd_looks_videoish(body: *u8, blen: i64) -> i64 {
32 if mjx_contains(body, blen, ".m3u8" as *u8, 5)==1 { return 1 }
33 if mjx_contains(body, blen, "jwplayer" as *u8, 8)==1 { return 1 }
34 if mjx_contains(body, blen, "video-js" as *u8, 8)==1 { return 1 }
35 if mjx_contains(body, blen, "videojs" as *u8, 7)==1 { return 1 }
36 if mjx_contains(body, blen, "hls.js" as *u8, 6)==1 { return 1 }
37 if mjx_contains(body, blen, ".mp4" as *u8, 4)==1 { return 1 }
38 if mjx_contains(body, blen, "<video" as *u8, 6)==1 { return 1 }
39 return 0
40}