code wiki / _hdl_build / nx_apistack_content.nx
nx_apistack_content.nx source
↩ module page · 23 lines · 1329 B
1// nx_apistack_content.nx -- CAP-API-CONTENT: content-negotiation for /api. Reads the Accept header and picks the
2// first SUPPORTED media type the client accepts (or -1 => 406 Not Acceptable). "*/*" matches the first supported.
3// Deterministic serializer selection = the sovereign exceed over ad-hoc fixed Content-Type. license_tier: ORIGINAL
4import "nx_syscalls.nx"
5
6func cn_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
7func cn_contains(hay: *u8, hn: i64, needle: *u8) -> i64 {
8 let nl: i64 = cn_len(needle); var i: i64 = 0
9 while i + nl <= hn { var j: i64=0; var m: i64=1; while j<nl { if hay[i+j]!=needle[j] { m=0; j=nl } else { j=j+1 } } if m==1 { return 1 } i=i+1 }
10 return 0
11}
12// does the Accept header allow `media`? ("*/*" wildcard, or the media substring present).
13func cn_accepts(accept: *u8, accept_n: i64, media: *u8) -> i64 {
14 if cn_contains(accept, accept_n, "*/*" as *u8) == 1 { return 1 }
15 if cn_contains(accept, accept_n, media) == 1 { return 1 }
16 return 0
17}
18// choose the first supported media type index the Accept allows; -1 if none (=> 406).
19func cn_choose(accept: *u8, accept_n: i64, supported: *i64, n: i64) -> i64 {
20 var i: i64 = 0
21 while i < n { if cn_accepts(accept, accept_n, supported[i] as *u8) == 1 { return i } i = i + 1 }
22 return 0 - 1
23}