Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/include/libks/kws.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2023 SignalWire, Inc
* Copyright (c) 2018-2026 SignalWire, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -104,6 +104,16 @@ KS_DECLARE(ks_ssize_t) kws_close(kws_t *kws, int16_t reason);
KS_DECLARE(ks_status_t) kws_create(kws_t **kwsP, ks_pool_t *pool);
KS_DECLARE(void) kws_destroy(kws_t **kwsP);
KS_DECLARE(void) kws_set_init_callback(kws_t *kws, kws_init_callback_t callback);
/**
* @brief Cancel pending blocking operations on a websocket.
*
* Sets the cancel flag so that any in-progress or subsequent blocking
* connect, handshake, or read operation will return with an error.
* Safe to call from any thread. Passing NULL is a no-op.
*
* @param kws Pointer to the websocket object, or NULL.
*/
KS_DECLARE(void) kws_cancel(kws_t *kws);
KS_DECLARE(ks_status_t) kws_connect(kws_t **kwsP, ks_json_t *params, kws_flag_t flags, ks_pool_t *pool);
KS_DECLARE(ks_status_t) kws_connect_ex(kws_t **kwsP, ks_json_t *params, kws_flag_t flags, ks_pool_t *pool, SSL_CTX *ssl_ctx, uint32_t timeout_ms);
KS_DECLARE(ks_status_t) kws_get_buffer(kws_t *kws, char **bufP, ks_size_t *buflen);
Expand Down
30 changes: 26 additions & 4 deletions src/kws.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2023 SignalWire, Inc
* Copyright (c) 2018-2026 SignalWire, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -88,8 +88,11 @@ struct kws_s {
ks_json_t *params;

ks_ssize_t payload_size_max;

volatile int cancel;
};

#define KWS_CANCELLED(kws) ((kws)->cancel)


static int cheezy_get_var(char *data, char *name, char *buf, ks_size_t buflen)
Expand Down Expand Up @@ -460,7 +463,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int
}
}

} while (r < 0 && SSL_ERROR_WANT_READ_WRITE(ssl_err) && kws->x < block_n);
} while (r < 0 && SSL_ERROR_WANT_READ_WRITE(ssl_err) && kws->x < block_n && !KWS_CANCELLED(kws));

goto end;
}
Expand All @@ -479,7 +482,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int
ks_sleep_ms(10);
}
}
} while (r == -1 && ks_errno_is_blocking(ks_errno()) && kws->x < block_n);
} while (r == -1 && ks_errno_is_blocking(ks_errno()) && kws->x < block_n && !KWS_CANCELLED(kws));

end:

Expand Down Expand Up @@ -695,6 +698,12 @@ static int establish_client_logical_layer(kws_t *kws)
SSL_set_tlsext_host_name(kws->ssl, kws->req_host);

do {
if (KWS_CANCELLED(kws)) {
ks_log(KS_LOG_DEBUG, "SSL connect cancelled\n");

return -1;
}

ERR_clear_error();
code = SSL_connect(kws->ssl);

Expand Down Expand Up @@ -735,7 +744,7 @@ static int establish_client_logical_layer(kws_t *kws)
}
}

while (!kws->down && !kws->handshake) {
while (!kws->down && !kws->handshake && !KWS_CANCELLED(kws)) {
int r = ws_client_handshake(kws);

if (r < 0) {
Expand All @@ -749,6 +758,12 @@ static int establish_client_logical_layer(kws_t *kws)

}

if (KWS_CANCELLED(kws)) {
ks_log(KS_LOG_DEBUG, "WebSocket handshake cancelled\n");

return -1;
}

kws->logical_established = 1;
if (kws->ssl) {
strncpy(kws->cipher_name, SSL_get_cipher_name(kws->ssl), sizeof(kws->cipher_name) - 1);
Expand Down Expand Up @@ -997,6 +1012,13 @@ KS_DECLARE(void) kws_set_init_callback(kws_t *kws, kws_init_callback_t callback)
kws->init_callback = callback;
}

KS_DECLARE(void) kws_cancel(kws_t *kws)
{
if (kws) {
kws->cancel = 1;
}
}

KS_DECLARE(ks_status_t) kws_create(kws_t **kwsP, ks_pool_t *pool)
{
kws_t *kws;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ksutil_add_test(sock)
ksutil_add_test(sock2)
ksutil_add_test(websock)
ksutil_add_test(websock2)
ksutil_add_test(kwscancel)
ksutil_add_test(http)
ksutil_add_test(tls)
ksutil_add_test(string)
Expand Down
Loading
Loading