From 16b0dc167cb19f9bb8862b0d735765115d2108ae Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 22 Oct 2024 23:33:34 -0700 Subject: [PATCH] Fill as much of TLS read buffer as possible before blocking --- .changeset/stupid-hotels-mate.md | 5 +++++ source/tls.c | 37 ++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 .changeset/stupid-hotels-mate.md diff --git a/.changeset/stupid-hotels-mate.md b/.changeset/stupid-hotels-mate.md new file mode 100644 index 00000000..da106386 --- /dev/null +++ b/.changeset/stupid-hotels-mate.md @@ -0,0 +1,5 @@ +--- +"@nx.js/runtime": patch +--- + +Fill as much of TLS read buffer as possible before blocking diff --git a/source/tls.c b/source/tls.c index 1d590e2f..19634ce3 100644 --- a/source/tls.c +++ b/source/tls.c @@ -159,13 +159,32 @@ void nx_tls_do_read(nx_poll_t *p, nx_watcher_t *watcher, int revents) { nx_tls_read_t *req = (nx_tls_read_t *)watcher; nx_js_callback_t *req_cb = (nx_js_callback_t *)req->opaque; - int ret = mbedtls_ssl_read(&req->data->ssl, req->buffer, req->buffer_size); - if (ret == MBEDTLS_ERR_SSL_WANT_READ) { - return; - } + size_t total_read = 0; + int ret; - if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { - ret = 0; + while (total_read < req->buffer_size) { + ret = mbedtls_ssl_read(&req->data->ssl, req->buffer + total_read, + req->buffer_size - total_read); + + if (ret > 0) { + total_read += ret; + } else if (ret == 0) { + // End of data stream reached + break; + } else if (ret == MBEDTLS_ERR_SSL_WANT_READ) { + // Need more data, break the loop if we've read anything + if (total_read > 0) { + break; + } + return; + } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { + // Connection closed + break; + } else if (ret < 0) { + // Error occurred + req->err = ret; + break; + } } nx_remove_watcher(p, watcher); @@ -175,15 +194,15 @@ void nx_tls_do_read(nx_poll_t *p, nx_watcher_t *watcher, int revents) { JSValue args[] = {JS_UNDEFINED, JS_UNDEFINED}; - if (ret < 0) { + if (req->err) { /* Error during read. */ char error_buf[100]; - mbedtls_strerror(ret, error_buf, 100); + mbedtls_strerror(req->err, error_buf, 100); args[0] = JS_NewError(ctx); JS_SetPropertyStr(ctx, args[0], "message", JS_NewString(ctx, error_buf)); } else { - args[1] = JS_NewInt32(ctx, ret); + args[1] = JS_NewInt32(ctx, total_read); } JSValue ret_val = JS_Call(ctx, req_cb->callback, JS_NULL, 2, args);