From 7dd5ad93a4c147b086a8d82ecb403f96981bb4ee Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 18 Aug 2023 12:12:27 +0200 Subject: Log: fixed typo. Scripted change: $ grep -ril recevied src/ | xargs sed -i s/recevied/received/ Reported-by: Closes: Cc: Cc: Timo Stark Signed-off-by: Alejandro Colomar Reviewed-by: Andrew Clayton Signed-off-by: Andrew Clayton --- src/nxt_conn_write.c | 2 +- src/nxt_main_process.c | 4 ++-- src/nxt_signal_handlers.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nxt_conn_write.c b/src/nxt_conn_write.c index bcf9e8fa..714a3e15 100644 --- a/src/nxt_conn_write.c +++ b/src/nxt_conn_write.c @@ -97,7 +97,7 @@ nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) if (sb.limit == 0) { /* * Postpone writing until next event poll to allow to - * process other recevied events and to get new events. + * process other received events and to get new events. */ c->write_timer.handler = nxt_conn_write_timer_handler; nxt_timer_add(engine, &c->write_timer, 0); diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index 6622f67e..3f317d5e 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -856,7 +856,7 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) nxt_array_t *new_files; nxt_runtime_t *rt; - nxt_log(task, NXT_LOG_NOTICE, "signal %d (%s) recevied, %s", + nxt_log(task, NXT_LOG_NOTICE, "signal %d (%s) received, %s", (int) (uintptr_t) obj, data, "log files rotation"); rt = task->thread->runtime; @@ -1073,7 +1073,7 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) static void nxt_main_process_signal_handler(nxt_task_t *task, void *obj, void *data) { - nxt_trace(task, "signal signo:%d (%s) recevied, ignored", + nxt_trace(task, "signal signo:%d (%s) received, ignored", (int) (uintptr_t) obj, data); } diff --git a/src/nxt_signal_handlers.c b/src/nxt_signal_handlers.c index 69ae2bc4..63b38fab 100644 --- a/src/nxt_signal_handlers.c +++ b/src/nxt_signal_handlers.c @@ -31,7 +31,7 @@ const nxt_sig_event_t nxt_process_signals[] = { static void nxt_signal_handler(nxt_task_t *task, void *obj, void *data) { - nxt_trace(task, "signal signo:%d (%s) recevied, ignored", + nxt_trace(task, "signal signo:%d (%s) received, ignored", (int) (uintptr_t) obj, data); } -- cgit From c9961610ed5651bda63191465e70e8227c3ef711 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Wed, 6 Sep 2023 02:58:19 +0100 Subject: Fix build on musl libc with clang. As reported by @andypost on GitHub, if you try to build Unit on a system that uses musl libc (such as Alpine Linux) with clang then you get the following clang -c -pipe -fPIC -fvisibility=hidden -O -W -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -fstrict-aliasing -Wstrict-overflow=5 -Wmissing-prototypes -Werror -g -I src -I build/include \ \ \ -o build/src/nxt_socketpair.o \ -MMD -MF build/src/nxt_socketpair.dep -MT build/src/nxt_socketpair.o \ src/nxt_socketpair.c In file included from src/nxt_socketpair.c:8: src/nxt_socket_msg.h:138:17: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare] cmsg = CMSG_NXTHDR(&msg, cmsg)) ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/sys/socket.h:358:44: note: expanded from macro 'CMSG_NXTHDR' __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from src/nxt_socketpair.c:8: src/nxt_socket_msg.h:177:17: error: comparison of integers of different signs: 'unsigned long' and 'long' [-Werror,-Wsign-compare] cmsg = CMSG_NXTHDR(&msg, cmsg)) ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/sys/socket.h:358:44: note: expanded from macro 'CMSG_NXTHDR' __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2 errors generated. make: *** [build/Makefile:261: build/src/nxt_socketpair.o] Error 1 GCC works fine, it seems to have some smarts so that it doesn't give warnings on system header files. This seems to be a long standing issue with musl libc (bad casting in the CMSG_NXTHDR macro) and the workaround employed by several projects is to disable the -Wsign-compare clang warning for the code in question. So, that's what we do. We wrap the CMSG_NXTHDR macro in a function, so we can use the pre-processor in it to selectively disable the warning. Link: Link: Link: Link: Closes: Signed-off-by: Andrew Clayton --- src/nxt_socket_msg.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nxt_socket_msg.h b/src/nxt_socket_msg.h index 04de1761..81617bd6 100644 --- a/src/nxt_socket_msg.h +++ b/src/nxt_socket_msg.h @@ -69,6 +69,20 @@ NXT_EXPORT ssize_t nxt_recvmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob, nxt_recv_oob_t *oob); +nxt_inline struct cmsghdr * +NXT_CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg) +{ +#if !defined(__GLIBC__) && defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wsign-compare" +#endif + return CMSG_NXTHDR(msgh, cmsg); +#if !defined(__GLIBC__) && defined(__clang__) +#pragma clang diagnostic pop +#endif +} + + nxt_inline void nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) { @@ -135,7 +149,7 @@ nxt_socket_msg_oob_get_fds(nxt_recv_oob_t *oob, nxt_fd_t *fd) for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; - cmsg = CMSG_NXTHDR(&msg, cmsg)) + cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) { size = cmsg->cmsg_len - CMSG_LEN(0); @@ -174,7 +188,7 @@ nxt_socket_msg_oob_get(nxt_recv_oob_t *oob, nxt_fd_t *fd, nxt_pid_t *pid) for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; - cmsg = CMSG_NXTHDR(&msg, cmsg)) + cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) { size = cmsg->cmsg_len - CMSG_LEN(0); -- cgit From 76086d6d7a027ddf42d86897200a53d724fb4bb7 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Fri, 8 Sep 2023 21:51:25 +0100 Subject: Wasm: Allow to set the HTTP response status. This commit enables WebAssembly modules to set the HTTP response status to something other than the previously hard coded '200 OK'. To do this they can make a call to nxt_wasm_set_resp_status() providing the required status code. If this function isn't called the status code defaults to '200 OK'. The WebAssembly module can also return -1 from the request_handler function as a short cut to signal a '500 Internal Server Error'. Signed-off-by: Andrew Clayton --- src/wasm/nxt_rt_wasmtime.c | 25 +++++++++++++++++++++++-- src/wasm/nxt_wasm.c | 27 +++++++++++++++++++++++---- src/wasm/nxt_wasm.h | 4 +++- 3 files changed, 49 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c index 99786b89..7f05fae0 100644 --- a/src/wasm/nxt_rt_wasmtime.c +++ b/src/wasm/nxt_rt_wasmtime.c @@ -102,6 +102,19 @@ nxt_wasm_send_headers(void *env, wasmtime_caller_t *caller, } +static wasm_trap_t * +nxt_wasm_set_resp_status(void *env, wasmtime_caller_t *caller, + const wasmtime_val_t *args, size_t nargs, + wasmtime_val_t *results, size_t nresults) +{ + nxt_wasm_ctx_t *ctx = env; + + ctx->status = args[0].of.i32; + + return NULL; +} + + static void nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) { @@ -123,8 +136,8 @@ nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) } -static void -nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) +static int +nxt_wasmtime_execute_request(nxt_wasm_ctx_t *ctx) { int i = 0; wasm_trap_t *trap = NULL; @@ -142,7 +155,10 @@ nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) nxt_wasmtime_err_msg(error, trap, "failed to call function [->wasm_request_handler]" ); + return -1; } + + return results[0].of.i32; } @@ -183,6 +199,11 @@ nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx) .func = nxt_wasm_send_headers, .params = { WASM_I32 }, .ft = NXT_WASM_FT_1_0 + }, { + .func_name = "nxt_wasm_set_resp_status", + .func = nxt_wasm_set_resp_status, + .params = { WASM_I32 }, + .ft = NXT_WASM_FT_1_0 }, { } diff --git a/src/wasm/nxt_wasm.c b/src/wasm/nxt_wasm.c index 45a40b4b..796ea847 100644 --- a/src/wasm/nxt_wasm.c +++ b/src/wasm/nxt_wasm.c @@ -24,6 +24,11 @@ static nxt_wasm_ctx_t nxt_wasm_ctx; static const nxt_wasm_operations_t *nxt_wops; +enum { + NXT_WASM_HTTP_OK = 200, + NXT_WASM_HTTP_ERROR = 500 +}; + void nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx) @@ -48,7 +53,7 @@ nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset) fields_len += rh->fields[i].name_len + rh->fields[i].value_len; } - nxt_unit_response_init(ctx->req, 200, rh->nfields, fields_len); + nxt_unit_response_init(ctx->req, ctx->status, rh->nfields, fields_len); for (i = 0; i < rh->nfields; i++) { const char *name; @@ -72,7 +77,7 @@ nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset) nxt_unit_request_info_t *req = ctx->req; if (!nxt_unit_response_is_init(req)) { - nxt_unit_response_init(req, 200, 0, 0); + nxt_unit_response_init(req, ctx->status, 0, 0); } resp = (nxt_wasm_response_t *)(nxt_wasm_ctx.baddr + offset); @@ -84,6 +89,7 @@ nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset) static void nxt_wasm_request_handler(nxt_unit_request_info_t *req) { + int err; size_t offset, read_bytes, content_sent, content_len; ssize_t bytes_read; nxt_unit_field_t *sf, *sf_end; @@ -149,8 +155,12 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) wr->request_size = offset + bytes_read; + nxt_wasm_ctx.status = NXT_WASM_HTTP_OK; nxt_wasm_ctx.req = req; - nxt_wops->exec_request(&nxt_wasm_ctx); + err = nxt_wops->exec_request(&nxt_wasm_ctx); + if (err) { + goto out_err_500; + } if (content_len == content_sent) { goto request_done; @@ -168,9 +178,18 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) wr->request_size = wr->content_sent = bytes_read; wr->total_content_sent = content_sent; - nxt_wops->exec_request(&nxt_wasm_ctx); + err = nxt_wops->exec_request(&nxt_wasm_ctx); + if (err) { + goto out_err_500; + } } while (content_sent < content_len); + goto request_done; + +out_err_500: + nxt_unit_response_init(req, NXT_WASM_HTTP_ERROR, 0, 0); + nxt_unit_request_done(req, NXT_UNIT_OK); + request_done: NXT_WASM_DO_HOOK(NXT_WASM_FH_REQUEST_END); } diff --git a/src/wasm/nxt_wasm.h b/src/wasm/nxt_wasm.h index cb9dbdfe..2748d764 100644 --- a/src/wasm/nxt_wasm.h +++ b/src/wasm/nxt_wasm.h @@ -118,12 +118,14 @@ struct nxt_wasm_ctx_s { size_t baddr_off; size_t response_off; + + uint16_t status; }; struct nxt_wasm_operations_s { int (*init)(nxt_wasm_ctx_t *ctx); void (*destroy)(const nxt_wasm_ctx_t *ctx); - void (*exec_request)(const nxt_wasm_ctx_t *ctx); + int (*exec_request)(nxt_wasm_ctx_t *ctx); void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook); }; -- cgit From d5efa5f11f38d4ca6c64374f848055611d4f315c Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Thu, 14 Sep 2023 00:37:28 +0100 Subject: Wasm: Fix multiple successive calls to the request_handler. When trying to upload files to the luw-upload-reflector demo[0] above a certain size that would mean Unit would need to make more than two calls to the request_handler function in the Wasm module we would get the following error from wasmtime and the upload would stall on the third call to the request_handler WASMTIME ERROR: failed to call function [->wasm_request_handler] error while executing at wasm backtrace: 0: 0x5ce2 - !memcpy 1: 0x7df - luw_req_buf_append at /home/andrew/src/unit-wasm/src/c/libunit-wasm.c:308:14 2: 0x3a1 - luw_request_handler at /home/andrew/src/unit-wasm/examples/c/luw-upload-reflector.c:110:3 Caused by: wasm trap: out of bounds memory access This was due to ->content_off (the offset of where the actual body content starts in the request structure/memory) being some overly large value. This was largely down to me being an idiot! Before calling the loop that makes the calls to the request_handler we would calculate the new offset, which is now just the size of the request structure as we don't re-send all the HTTP meta data and headers etc. However because this value is in the request structure which is in the shared memory and we use this same memory for requests and responses, when we make a response we overwrite this request structure with the response structure, so our ->content_off is now some wacked out value when we make the next call to the request_handler. To fix this we just need to reset ->content_off each time round the loop. There's also no point in setting ->nfields to 0, it has the same issue as above, but doesn't get re-used by the Wasm module anyway. [0]: Signed-off-by: Andrew Clayton --- src/wasm/nxt_wasm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/wasm/nxt_wasm.c b/src/wasm/nxt_wasm.c index 796ea847..92ed57ab 100644 --- a/src/wasm/nxt_wasm.c +++ b/src/wasm/nxt_wasm.c @@ -166,8 +166,7 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) goto request_done; } - wr->nfields = 0; - wr->content_off = offset = sizeof(nxt_wasm_request_t); + offset = sizeof(nxt_wasm_request_t); do { read_bytes = nxt_min(content_len - content_sent, NXT_WASM_MEM_SIZE - offset); @@ -177,6 +176,7 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) content_sent += bytes_read; wr->request_size = wr->content_sent = bytes_read; wr->total_content_sent = content_sent; + wr->content_off = offset; err = nxt_wops->exec_request(&nxt_wasm_ctx); if (err) { -- cgit From 0f630c3f604e76de370529907b51ad29dfdc4ecb Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Thu, 14 Sep 2023 20:35:15 +0100 Subject: Wasm: Allow uploads larger than 4GiB. Currently Wasm modules are limited to a 32bit address space (until at least the memory64 work is completed). All the counters etc in the request structure were u32's. Which matched with 32bit memory limitation. However there is really no need to not allow >4GiB uploads that can be saved off to disk or some such. To do this we just need to increase the ->content_len & ->total_content_sent members to u64's. However because we need the request structure to have the exact same layout on 32bit (for Wasm modules) as it does on 64bit we need to re-jig the order of some of these members and add a four-byte padding member. Thus the request structure now looks like on 64bit (as shown by pahole(1)) struct nxt_wasm_request_s { uint32_t method_off; /* 0 4 */ uint32_t method_len; /* 4 4 */ uint32_t version_off; /* 8 4 */ uint32_t version_len; /* 12 4 */ uint32_t path_off; /* 16 4 */ uint32_t path_len; /* 20 4 */ uint32_t query_off; /* 24 4 */ uint32_t query_len; /* 28 4 */ uint32_t remote_off; /* 32 4 */ uint32_t remote_len; /* 36 4 */ uint32_t local_addr_off; /* 40 4 */ uint32_t local_addr_len; /* 44 4 */ uint32_t local_port_off; /* 48 4 */ uint32_t local_port_len; /* 52 4 */ uint32_t server_name_off; /* 56 4 */ uint32_t server_name_len; /* 60 4 */ /* --- cacheline 1 boundary (64 bytes) --- */ uint64_t content_len; /* 64 8 */ uint64_t total_content_sent; /* 72 8 */ uint32_t content_sent; /* 80 4 */ uint32_t content_off; /* 84 4 */ uint32_t request_size; /* 88 4 */ uint32_t nfields; /* 92 4 */ uint32_t tls; /* 96 4 */ char __pad[4]; /* 100 4 */ nxt_wasm_http_field_t fields[]; /* 104 0 */ /* size: 104, cachelines: 2, members: 25 */ /* last cacheline: 40 bytes */ }; and the same structure (taken from unit-wasm) compiled as 32bit struct luw_req { u32 method_off; /* 0 4 */ u32 method_len; /* 4 4 */ u32 version_off; /* 8 4 */ u32 version_len; /* 12 4 */ u32 path_off; /* 16 4 */ u32 path_len; /* 20 4 */ u32 query_off; /* 24 4 */ u32 query_len; /* 28 4 */ u32 remote_off; /* 32 4 */ u32 remote_len; /* 36 4 */ u32 local_addr_off; /* 40 4 */ u32 local_addr_len; /* 44 4 */ u32 local_port_off; /* 48 4 */ u32 local_port_len; /* 52 4 */ u32 server_name_off; /* 56 4 */ u32 server_name_len; /* 60 4 */ /* --- cacheline 1 boundary (64 bytes) --- */ u64 content_len; /* 64 8 */ u64 total_content_sent; /* 72 8 */ u32 content_sent; /* 80 4 */ u32 content_off; /* 84 4 */ u32 request_size; /* 88 4 */ u32 nr_fields; /* 92 4 */ u32 tls; /* 96 4 */ char __pad[4]; /* 100 4 */ struct luw_hdr_field fields[]; /* 104 0 */ /* size: 104, cachelines: 2, members: 25 */ /* last cacheline: 40 bytes */ }; We can see the structures have the same layout, same size and no padding. We need the __pad member as otherwise I saw gcc and clang on Alpine Linux automatically add the 'packed' attribute to the structure which made the two structures not match. Link: Link: Signed-off-by: Andrew Clayton --- src/wasm/nxt_wasm.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/wasm/nxt_wasm.h b/src/wasm/nxt_wasm.h index 2748d764..f78aef13 100644 --- a/src/wasm/nxt_wasm.h +++ b/src/wasm/nxt_wasm.h @@ -59,10 +59,10 @@ struct nxt_wasm_request_s { uint32_t server_name_off; uint32_t server_name_len; - uint32_t content_off; - uint32_t content_len; + uint64_t content_len; + uint64_t total_content_sent; uint32_t content_sent; - uint32_t total_content_sent; + uint32_t content_off; uint32_t request_size; @@ -70,6 +70,8 @@ struct nxt_wasm_request_s { uint32_t tls; + char __pad[4]; + nxt_wasm_http_field_t fields[]; }; -- cgit From e0c26757740fa7974af6e6592e35b5f2e00339fe Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 26 Sep 2023 12:49:39 +0100 Subject: Node.js: response body chunk can now be a Uint8Array. Starting from Node.js 15.0.0 the chunk parameter of the response.write() can be a Uint8Array. This closes #870 issue on GitHub. --- src/nodejs/unit-http/http_server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 89964ec3..11651ed7 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -243,8 +243,11 @@ ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { } if (chunk) { - if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { - throw new TypeError('First argument must be a string or Buffer'); + if (typeof chunk !== 'string' && !(chunk instanceof Buffer || + chunk instanceof Uint8Array)) { + throw new TypeError( + 'First argument must be a string, Buffer, ' + + 'or Uint8Array'); } if (typeof chunk === 'string') { -- cgit From 2d0e502d2a69ad490e0633c59636e1115afa983d Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 26 Sep 2023 12:49:39 +0100 Subject: Node.js: ServerRequest.destroy() implemented. This closes #871 issue on GitHub. --- src/nodejs/unit-http/http_server.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 11651ed7..0f00b47f 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -33,8 +33,17 @@ ServerResponse.prototype.statusMessage = undefined; ServerResponse.prototype.headers_len = 0; ServerResponse.prototype.headers_count = 0; ServerResponse.prototype.headersSent = false; +ServerResponse.prototype.destroyed = false; ServerResponse.prototype.finished = false; +ServerResponse.prototype.destroy = function destroy(error) { + if (!this.destroyed) { + this.destroyed = true; + } + + return this; +}; + ServerResponse.prototype._finish = function _finish() { this.headers = {}; this.headers_len = 0; -- cgit From b6216f0bb7c4e10afcec00c6660644ff32db0dfb Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Thu, 28 Sep 2023 15:14:21 +0100 Subject: Java: fixed the calculation related to the response buffer. We need to take into account the size of the nxt_unit_response_t structure itself when calculating where to start appending data to in memory. Closes: Reported-by: Alejandro Colomar Reviewed-by: Andrew Clayton Signed-off-by: Andrew Clayton --- src/java/nxt_jni_Response.c | 7 ++++--- src/nxt_unit.c | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/java/nxt_jni_Response.c b/src/java/nxt_jni_Response.c index 2ccfd854..fa698ee8 100644 --- a/src/java/nxt_jni_Response.c +++ b/src/java/nxt_jni_Response.c @@ -334,7 +334,8 @@ nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields, - req->response->fields_count || extra_data > (uint32_t) (buf->end - buf->free)) { - p = buf->start + req->response_max_fields * sizeof(nxt_unit_field_t); + p = buf->start + sizeof(nxt_unit_response_t) + + req->response_max_fields * sizeof(nxt_unit_field_t); max_size = 2 * (buf->end - p); if (max_size > nxt_unit_buf_max()) { @@ -936,8 +937,8 @@ nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr) buf = req->response_buf; - buf->free = buf->start + req->response_max_fields - * sizeof(nxt_unit_field_t); + buf->free = buf->start + sizeof(nxt_unit_response_t) + + req->response_max_fields * sizeof(nxt_unit_field_t); } } diff --git a/src/nxt_unit.c b/src/nxt_unit.c index e1b1897a..b6291b2d 100644 --- a/src/nxt_unit.c +++ b/src/nxt_unit.c @@ -2148,7 +2148,8 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, resp->status = req->response->status; resp->content_length = req->response->content_length; - p = buf->start + max_fields_count * sizeof(nxt_unit_field_t); + p = buf->start + sizeof(nxt_unit_response_t) + + max_fields_count * sizeof(nxt_unit_field_t); f = resp->fields; for (i = 0; i < req->response->fields_count; i++) { -- cgit From 7fac9087424e1f7c4b82bfb8426aca197f1f8904 Mon Sep 17 00:00:00 2001 From: Liam Crilly Date: Mon, 2 Oct 2023 09:43:57 +0100 Subject: Added routes array to the default configuration. The default configuration previously contained just a listeners and applications object. Since routes is now a principle configuration object, and a recommended way of configurating Unit, it is now included in the default configuration. This change benefits new users because it explicitly introduces the three principle configuration objects which leads more intuitively to the documentation. Experienced users may choose to ignore or delete routes. routes is defined as an array instead of an object because this change is designed to assist new users, where the simpler form of routes is easier to understand. --- src/nxt_controller.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nxt_controller.c b/src/nxt_controller.c index 4e2e3749..eb814321 100644 --- a/src/nxt_controller.c +++ b/src/nxt_controller.c @@ -510,8 +510,9 @@ nxt_controller_conf_default(void) nxt_mp_t *mp; nxt_conf_value_t *conf; - static const nxt_str_t json - = nxt_string("{ \"listeners\": {}, \"applications\": {} }"); + static const nxt_str_t json = nxt_string( + "{ \"listeners\": {}, \"routes\": [], \"applications\": {} }" + ); mp = nxt_mp_create(1024, 128, 256, 32); -- cgit From 30142d2a3c862eef0a32805ee3907535ada32c71 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Thu, 5 Oct 2023 00:36:00 +0100 Subject: HTTP: Fix URL with query string rewrite. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Github, @rlandgrebe reported an issue when trying to rewrite URLs that contained query strings. With the PHP language module we were in fact segfaulting (SIGSEGV) in libphp [93960.462952] unitd[20940]: segfault at 7f307cef6476 ip 00007f2f81a94577 sp 00007fff28a777d0 error 4 in libphp-8.2.so[7f2f818df000+2fd000] likely on CPU 0 (core 0, socket 0) #0 0x00007f2abd494577 in php_default_treat_data (arg=1, str=0x0, destArray=) at /usr/src/debug/php-8.2.10-1.fc38.x86_64/main/php_variables.c:488 488 if (c_var && *c_var) { (gdb) p c_var $1 = 0x7f2bb8880676 This was when trying to get the query string which somehow is pointing off into the woods. This gdb debug session when doing rewrite basically shows the core of the issue (gdb) x /64bs req->fields ... 0x7f7eaaaa8090: "GET" 0x7f7eaaaa8094: "HTTP/1.1" 0x7f7eaaaa809d: "::1" 0x7f7eaaaa80a1: "::1" 0x7f7eaaaa80a5: "8080" 0x7f7eaaaa80aa: "localhost" 0x7f7eaaaa80b4: "/test?q=a" 0x7f7eaaaa80be: "/test" ... (gdb) p target_pos $4 = (void *) 0x7f7eaaaa80b4 (gdb) p query_pos $6 = (void *) 0x7f7eaaaa6af6 (gdb) p r->args->start $8 = (u_char *) 0x7f7ea4002b02 "q=a HTTP/1.1\r\nHost: localhost:8080\r\nUser-Agent: curl/8.0.1\r\nAccept: */*\r\n\r\n" (gdb) p r->target.start $9 = (u_char *) 0x7f7ea40040c0 "/test?q=a" That last address, 0x7f7ea40040c0, looks out of wack, it should be smaller than r->args->start. That results in a calculation in nxt_router_prepare_msg() if (r->args->start != NULL) { query_pos = nxt_pointer_to(target_pos, r->args->start - r->target.start); nxt_unit_sptr_set(&req->query, query_pos); } else { that goes negative that then is stored in req->query.offset which is a uint32_t and so wraps backwards from UINT_MAX to give us an offset of a little under 4GiB, hence the above invalid memory access. All this happens due to in nxt_http_rewrite() if we have a URL with a query string, we create a new memory allocation to store the transformed URL and query string. We set r->target to point to this new allocation, but we also need to point r->args->start to the start of the query string in this new allocation. Reported-by: René Landgrebe Tested-by: René Landgrebe Tested-by: Liam Crilly Fixes: 14d6d97b ("HTTP: added basic URI rewrite.") Closes: Signed-off-by: Andrew Clayton --- src/nxt_http_rewrite.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/nxt_http_rewrite.c b/src/nxt_http_rewrite.c index ae5c865a..fb216eeb 100644 --- a/src/nxt_http_rewrite.c +++ b/src/nxt_http_rewrite.c @@ -93,6 +93,7 @@ nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) nxt_memcpy(p, r->args->start, r->args->length); r->target = target; + r->args->start = p; } r->path = nxt_mp_alloc(r->mem_pool, sizeof(nxt_str_t)); -- cgit From 9c8b9a46a42d47b29453eefc4c5dd325f59e46b4 Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Tue, 10 Oct 2023 14:30:02 +0800 Subject: Refactored nxt_vsprintf(). --- src/nxt_sprintf.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 9c8e27ed..875f43a5 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -156,7 +156,8 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) p = va_arg(args, const u_char *); if (nxt_slow_path(p == NULL)) { - goto copy; + buf = nxt_cpymem(buf, null, nxt_length(null)); + continue; } while (*p != '\0' && buf < end) { @@ -174,6 +175,11 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) fmt++; p = va_arg(args, const u_char *); + if (nxt_slow_path(p == NULL)) { + buf = nxt_cpymem(buf, null, nxt_length(null)); + continue; + } + goto copy; } @@ -556,14 +562,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) copy: - if (nxt_slow_path(p == NULL)) { - p = null; - length = nxt_length(null); - - } else { - length = nxt_min((size_t) (end - buf), length); - } - + length = nxt_min((size_t) (end - buf), length); buf = nxt_cpymem(buf, p, length); continue; } -- cgit From 01d185cb52af8879aeeab04765eff439feec664c Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Wed, 27 Sep 2023 21:25:09 +0100 Subject: Wasm: Re-add a removed 'const' qualifier in nxt_rt_wasmtime.c. This was inadvertently removed in 76086d6d ("Wasm: Allow to set the HTTP response status.") Fixes: 76086d6d ("Wasm: Allow to set the HTTP response status.") Signed-off-by: Andrew Clayton --- src/wasm/nxt_rt_wasmtime.c | 2 +- src/wasm/nxt_wasm.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c index 7f05fae0..bf0b0a0f 100644 --- a/src/wasm/nxt_rt_wasmtime.c +++ b/src/wasm/nxt_rt_wasmtime.c @@ -137,7 +137,7 @@ nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) static int -nxt_wasmtime_execute_request(nxt_wasm_ctx_t *ctx) +nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) { int i = 0; wasm_trap_t *trap = NULL; diff --git a/src/wasm/nxt_wasm.h b/src/wasm/nxt_wasm.h index f78aef13..6bc3ae35 100644 --- a/src/wasm/nxt_wasm.h +++ b/src/wasm/nxt_wasm.h @@ -127,7 +127,7 @@ struct nxt_wasm_ctx_s { struct nxt_wasm_operations_s { int (*init)(nxt_wasm_ctx_t *ctx); void (*destroy)(const nxt_wasm_ctx_t *ctx); - int (*exec_request)(nxt_wasm_ctx_t *ctx); + int (*exec_request)(const nxt_wasm_ctx_t *ctx); void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook); }; -- cgit From f1ce2a5ac2ba3266d525544445366fae538d7194 Mon Sep 17 00:00:00 2001 From: Konstantin Pavlov Date: Tue, 26 Sep 2023 16:14:21 -0700 Subject: Node.js: provide reasonable default paths for macOS. --- src/nodejs/unit-http/binding_pub.gyp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nodejs/unit-http/binding_pub.gyp b/src/nodejs/unit-http/binding_pub.gyp index 3c39933a..3dadf4a5 100644 --- a/src/nodejs/unit-http/binding_pub.gyp +++ b/src/nodejs/unit-http/binding_pub.gyp @@ -7,9 +7,28 @@ ['OS=="mac"', { 'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES' - } - }] - ], + }, + 'conditions': [ + [ 'target_arch=="arm64"', { + 'include_dirs': [ + '/opt/homebrew/include' + ], + 'libraries' : [ + '-L/opt/homebrew/lib', + '-lunit' + ], + }], + ['target_arch=="x64"', { + 'include_dirs': [ + '/usr/local/include', + ], + 'libraries' : [ + '-L/usr/local/lib', + '-lunit' + ], + }] + ]} + ]], 'sources': ["unit.cpp", "addon.cpp"], 'libraries': ["-lunit"] }] -- cgit