Files
ziran/std/net_http_linux.zi
T

39 lines
1.3 KiB
Ziran

#import "process_capture_linux"
HttpUrl :: (url: string) -> bool {
http := "http://"
https := "https://"
valid := url.count > http.count && url[:http.count] == http
if url.count > https.count && url[:https.count] == https { valid = true }
if !valid || url.count >= 2048 { return false }
for byte: url {
if byte == cast(u8)0 || byte < cast(u8)32 || byte == cast(u8)127 { return false }
}
return true
}
// Native HTTPS JSON transport. curl owns TLS and HTTP framing; Ziran owns the
// request arguments and caller-provided response buffer.
#program_export
PostJson :: (url: string, body: string, output: []u8) -> CaptureResult {
maximum := output.count - 1
return PostJsonCapped(url, body, maximum, output)
}
// Native HTTPS JSON transport with an explicit response limit.
#program_export
PostJsonCapped :: (url: string, body: string, maximum: s64,
output: []u8) -> CaptureResult {
if !HttpUrl(url) {
result: CaptureResult
result.error = true
result.problem = .InvalidUrl
return result
}
arguments: [9]string = .{
"curl", "-fs", "--max-time", "10", "-H",
"Content-Type: application/json", "-d", body, url
}
return CaptureOutputCapped(arguments[:], output, maximum)
}