nx_gpu_capture_http_lib.nx source
↩ module page · 41 lines · 2627 B
1// nx_gpu_capture_http_lib.nx -- Handles HTTP requests for GPU capture operations, mapping error codes to JSON responses and sending them via the HTTP server.
2import "nx_gpu_capture_store_lib.nx"
3const GCH_NO_CONTENT:i64=204
4const GCH_BAD_REQUEST:i64=400
5const GCH_CONFLICT:i64=409
6const GCH_TOO_LARGE:i64=413
7const GCH_UNAVAILABLE:i64=503
8func gch_status(rc:i64)->i64 {
9 if rc>=0 { return GCH_NO_CONTENT }
10 if rc==CA_CONFLICT { return GCH_CONFLICT }
11 if rc==GCS_BODY_LIMIT || rc==GCS_VALIDATION_BASE+GTC_LIMIT { return GCH_TOO_LARGE }
12 if rc==GCS_BAD_META || rc==GCS_VALIDATION_BASE+GTC_SCHEMA || rc==GCS_VALIDATION_BASE+GTC_COVERAGE || rc==GCS_VALIDATION_BASE+GTC_INTERVAL || rc==GCS_VALIDATION_BASE+GTC_FRAME_ORDER { return GCH_BAD_REQUEST }
13 return GCH_UNAVAILABLE
14}
15func gch_error(rc:i64)->*u8 {
16 if rc==CA_CONFLICT { return "{\"code\":\"capture_conflict\",\"retry_same_body\":false}" as *u8 }
17 if rc==GCS_BODY_LIMIT || rc==GCS_VALIDATION_BASE+GTC_LIMIT { return "{\"code\":\"capture_limit\",\"retry_same_body\":false}" as *u8 }
18 if rc==GCS_BAD_META { return "{\"code\":\"capture_metadata\",\"retry_same_body\":false}" as *u8 }
19 if gch_status(rc)==GCH_BAD_REQUEST { return "{\"code\":\"capture_validation\",\"retry_same_body\":false}" as *u8 }
20 return "{\"code\":\"capture_store_unavailable\",\"retry_same_body\":true}" as *u8
21}
22
23import "nx_http_server.nx"
24func gch_handle(cfd:i64,prefix:*u8,query:*u8,qn:i64,body:*u8,bn:i64,max_body:i64,max_rows:i64)->i64 {
25 let rc:i64=gcs_append(prefix,query,qn,body,bn,max_body,max_rows)
26 let status:i64=gch_status(rc)
27 let detail:*u8=gch_error(rc)
28 var n:i64=0
29 if status!=GCH_NO_CONTENT { n=ss_len(detail) }
30 nx_http_write_response(cfd,status,"application/json" as *u8,16,detail,n)
31 return rc
32}
33
34// Fixed method-error response needs no allocations; request loop retains socket ownership.
35func gch_method(cfd:i64)->i64 {
36 let response:*u8="HTTP/1.1 405 Method Not Allowed\r\nAllow: POST\r\nContent-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\nX-Content-Type-Options: nosniff\r\nX-Frame-Options: DENY\r\nReferrer-Policy: strict-origin-when-cross-origin\r\nStrict-Transport-Security: max-age=63072000; includeSubDomains; preload\r\nContent-Security-Policy: default-src 'self'; img-src 'self' data:; connect-src 'self' ws: wss:; style-src 'self' 'unsafe-inline'\r\nX-Nishi-Substrate: nws/v3\r\nContent-Length: 29\r\nConnection: close\r\n\r\n{\"code\":\"method_not_allowed\"}" as *u8
37 let n:i64=ss_len(response)
38 var sent:i64=0
39 while sent<n { let wrote:i64=sys_write(cfd,((response as i64)+sent) as *u8,n-sent); if wrote<=0 { return -1 }; sent=sent+wrote }
40 return sent
41}