getcwd, readlink, read, write, bcopy, mempcpy, poll and curl_ws_recv took u64 or s64 for size_t, ssize_t and nfds_t values. On wasm32 that linked with a function signature mismatch and trapped when called. A check now reads std's bindings for these functions, and another builds file_tree_linux for wasm32.
158 lines
5.2 KiB
Ziran
158 lines
5.2 KiB
Ziran
// Blocking, bounded WebSocket text receive for native libcurl hosts.
|
|
// The protocol consumer owns URL construction and JSON validation.
|
|
#import "net_http_curl_linux"
|
|
#import "byte_text_linux"
|
|
#import "c_string"
|
|
|
|
curl :: #system_library "curl";
|
|
c :: #system_library "c";
|
|
curl_easy_init :: () -> *void #foreign curl;
|
|
curl_easy_cleanup :: (handle: *void) #foreign curl;
|
|
curl_easy_setopt :: (handle: *void, option: s32, args: ..any) -> s32 #foreign curl;
|
|
curl_easy_perform :: (handle: *void) -> s32 #foreign curl;
|
|
curl_easy_getinfo :: (handle: *void, option: s32, args: ..any) -> s32 #foreign curl;
|
|
curl_slist_free_all :: (list: *void) #foreign curl;
|
|
curl_ws_recv :: (handle: *void, data: *u8, capacity: usize,
|
|
received: *usize, frame: **CurlWsFrame) -> s32 #foreign curl;
|
|
#if #defined(_WIN32) {
|
|
// curl_socket_t is SOCKET, a pointer-sized handle, on Windows.
|
|
ws2_32 :: #system_library "ws2_32";
|
|
PollDescriptor :: struct {
|
|
fd: usize
|
|
events: s16
|
|
returned: s16
|
|
}
|
|
PollRaw :: (descriptors: *PollDescriptor, count: u32,
|
|
timeout_ms: s32) -> s32 #foreign ws2_32 "WSAPoll";
|
|
PollRead :: 256;
|
|
|
|
NoSocket :: () -> PollDescriptor {
|
|
descriptor: PollDescriptor
|
|
descriptor.fd = cast(usize)-1
|
|
return descriptor
|
|
}
|
|
|
|
ValidSocket :: (descriptor: PollDescriptor) -> bool {
|
|
return descriptor.fd != cast(usize)-1
|
|
}
|
|
} else {
|
|
PollDescriptor :: struct {
|
|
fd: s32
|
|
events: s16
|
|
returned: s16
|
|
}
|
|
// nfds_t is unsigned long on Linux, which is pointer-sized.
|
|
PollRaw :: (descriptors: *PollDescriptor, count: usize,
|
|
timeout_ms: s32) -> s32 #foreign c "poll";
|
|
PollRead :: 1;
|
|
|
|
NoSocket :: () -> PollDescriptor {
|
|
descriptor: PollDescriptor
|
|
descriptor.fd = -1
|
|
return descriptor
|
|
}
|
|
|
|
ValidSocket :: (descriptor: PollDescriptor) -> bool {
|
|
return descriptor.fd >= 0
|
|
}
|
|
}
|
|
time :: (output: *s64) -> s64 #foreign c;
|
|
|
|
CurlWsFrame :: struct {
|
|
age: s32
|
|
flags: s32
|
|
offset: s64
|
|
bytes_left: s64
|
|
length: u64
|
|
}
|
|
|
|
|
|
CurlConnectOnly :: 141;
|
|
CurlActiveSocket :: 5242924;
|
|
CurlAgain :: 81;
|
|
CurlWsText :: 1;
|
|
CurlWsContinue :: 4;
|
|
CurlWsClose :: 8;
|
|
|
|
// Output is NUL terminated on success. status is 401 on an unauthorized
|
|
// handshake so a protocol client can refresh its session and retry.
|
|
#program_export
|
|
ReceiveWebSocketCurl :: (context: *void, url: string, token: string,
|
|
output: []u8, status: *s32) -> bool {
|
|
unused context
|
|
if status == null || output.count < 2 { return false }
|
|
status.* = 0
|
|
output[0] = cast(u8)0
|
|
if url.count >= 2048 || token.count == 0 || token.count >= 4096 ||
|
|
!(StartsWith(url, "ws://") || StartsWith(url, "wss://")) {
|
|
return false
|
|
}
|
|
url_text: [2048]u8
|
|
if !CopyCString(url, url_text[:]) { return false }
|
|
if !CurlReady() { return false }
|
|
handle := curl_easy_init()
|
|
if handle == null { return false }
|
|
headers: *void = null
|
|
header: [8192]u8
|
|
configured := AddCurlHeader(*headers, "Authorization: Bearer ",
|
|
token, header[:]) &&
|
|
curl_easy_setopt(handle, CurlUrl, *url_text[0]) == 0 &&
|
|
SetCurlLong(handle, CurlConnectOnly, 2) &&
|
|
SetCurlLong(handle, CurlNoSignal, 1) &&
|
|
SetCurlLong(handle, CurlConnectTimeoutMs, 10000)
|
|
if configured {
|
|
configured = curl_easy_setopt(handle, CurlHeaders, headers) == 0
|
|
}
|
|
connected := configured && curl_easy_perform(handle) == 0
|
|
if !connected {
|
|
status.* = ReadCurlStatus(handle)
|
|
} else {
|
|
status.* = 200
|
|
}
|
|
ok := false
|
|
if connected {
|
|
descriptor := NoSocket()
|
|
if curl_easy_getinfo(handle, CurlActiveSocket, *descriptor.fd) == 0 &&
|
|
ValidSocket(descriptor) {
|
|
descriptor.events = cast(s16)PollRead
|
|
end_time := time(null) + 35
|
|
length: s64 = 0
|
|
while time(null) < end_time {
|
|
received: usize = 0
|
|
frame: *CurlWsFrame = null
|
|
code := curl_ws_recv(handle, *output[length],
|
|
cast(usize)(output.count - length - 1),
|
|
*received, *frame)
|
|
if code == CurlAgain {
|
|
descriptor.returned = 0
|
|
if PollRaw(*descriptor, 1, 1000) < 0 { break }
|
|
continue
|
|
}
|
|
if code != 0 || frame == null { break }
|
|
if (frame.flags & cast(s32)CurlWsClose) != 0 { break }
|
|
if (frame.flags & cast(s32)CurlWsText) == 0 {
|
|
if length != 0 { break }
|
|
continue
|
|
}
|
|
if received > cast(usize)(output.count - length - 1) {
|
|
break
|
|
}
|
|
length += cast(s64)received
|
|
output[length] = cast(u8)0
|
|
if frame.bytes_left > cast(s64)(output.count - length - 1) {
|
|
break
|
|
}
|
|
if frame.bytes_left == 0 &&
|
|
(frame.flags & cast(s32)CurlWsContinue) == 0 {
|
|
ok = length > 0
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if headers != null { curl_slist_free_all(headers) }
|
|
curl_easy_cleanup(handle)
|
|
if !ok { output[0] = cast(u8)0 }
|
|
return ok
|
|
}
|