summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/java/nxt_jni_Response.c7
-rw-r--r--src/nodejs/unit-http/binding_pub.gyp25
-rw-r--r--src/nodejs/unit-http/http_server.js16
-rw-r--r--src/nxt_conn_write.c2
-rw-r--r--src/nxt_controller.c5
-rw-r--r--src/nxt_http_rewrite.c1
-rw-r--r--src/nxt_main_process.c4
-rw-r--r--src/nxt_signal_handlers.c2
-rw-r--r--src/nxt_socket_msg.h18
-rw-r--r--src/nxt_sprintf.c17
-rw-r--r--src/nxt_unit.c3
-rw-r--r--src/wasm/nxt_rt_wasmtime.c23
-rw-r--r--src/wasm/nxt_wasm.c31
-rw-r--r--src/wasm/nxt_wasm.h12
14 files changed, 129 insertions, 37 deletions
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/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"]
}]
diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js
index 89964ec3..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;
@@ -243,8 +252,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') {
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_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);
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));
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);
}
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);
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;
}
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++) {
diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c
index 99786b89..bf0b0a0f 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,7 +136,7 @@ nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook)
}
-static void
+static int
nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx)
{
int i = 0;
@@ -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..92ed57ab 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,15 +155,18 @@ 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;
}
- 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);
@@ -167,10 +176,20 @@ 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;
- 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..6bc3ae35 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[];
};
@@ -118,12 +120,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)(const nxt_wasm_ctx_t *ctx);
void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook);
};