From 35533f4b968fc14b80a085e0246aa3074c1dec6f Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 3 Jan 2021 16:01:53 -0800 Subject: fixes #1403 http ws listeners added without a lock --- src/sp/transport/ws/ws_test.c | 13 ++++++------- src/supplemental/websocket/websocket.c | 12 +++++++++++- src/supplemental/websocket/wssfile_test.c | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/sp/transport/ws/ws_test.c b/src/sp/transport/ws/ws_test.c index 7cbcd9d7..4a2b58c2 100644 --- a/src/sp/transport/ws/ws_test.c +++ b/src/sp/transport/ws/ws_test.c @@ -56,19 +56,16 @@ test_wild_card_port(void) NUTS_OPEN(s5); NUTS_OPEN(s6); NUTS_PASS(nng_listen(s1, "ws://127.0.0.1:0/one", &l1, 0)); - NUTS_PASS( - nng_listener_get_int(l1, NNG_OPT_TCP_BOUND_PORT, &port1)); + NUTS_PASS(nng_listener_get_int(l1, NNG_OPT_TCP_BOUND_PORT, &port1)); NUTS_TRUE(port1 != 0); snprintf(ws_url, sizeof(ws_url), "ws4://127.0.0.1:%d/two", port1); NUTS_PASS(nng_listen(s2, ws_url, &l2, 0)); - NUTS_PASS( - nng_listener_get_int(l2, NNG_OPT_TCP_BOUND_PORT, &port2)); + NUTS_PASS(nng_listener_get_int(l2, NNG_OPT_TCP_BOUND_PORT, &port2)); NUTS_TRUE(port1 != 0); NUTS_TRUE(port1 == port2); // Now try a different wild card port. NUTS_PASS(nng_listen(s3, "ws4://127.0.0.1:0/three", &l3, 0)); - NUTS_PASS( - nng_listener_get_int(l3, NNG_OPT_TCP_BOUND_PORT, &port3)); + NUTS_PASS(nng_listener_get_int(l3, NNG_OPT_TCP_BOUND_PORT, &port3)); NUTS_TRUE(port3 != 0); NUTS_TRUE(port3 != port1); @@ -147,7 +144,9 @@ test_ws_recv_max(void) nng_socket s1; nng_listener l; size_t sz; - char *addr; + char * addr; + + memset(msg, 0, sizeof(msg)); // required to silence valgrind NUTS_ADDR(addr, "ws"); NUTS_OPEN(s0); diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index daf1be13..416d968c 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. +// Copyright 2021 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // Copyright 2019 Devolutions // @@ -1510,6 +1510,12 @@ ws_handler(nni_aio *aio) conn = nni_aio_get_input(aio, 2); l = nni_http_handler_get_data(h); + nni_mtx_lock(&l->mtx); + if (l->closed) { + status = NNG_HTTP_STATUS_SERVICE_UNAVAILABLE; + goto err; + } + // Now check the headers, etc. if (strcmp(nni_http_req_get_version(req), "HTTP/1.1") != 0) { status = NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP; @@ -1609,6 +1615,7 @@ ws_handler(nni_aio *aio) if (rv != 0) { nni_http_res_free(res); nni_aio_finish_error(aio, rv); + nni_mtx_unlock(&l->mtx); return; } @@ -1625,6 +1632,7 @@ ws_handler(nni_aio *aio) nni_http_req_free(req); nni_aio_set_output(aio, 0, res); nni_aio_finish(aio, 0, 0); + nni_mtx_unlock(&l->mtx); return; } } @@ -1657,6 +1665,7 @@ ws_handler(nni_aio *aio) (void) nni_http_hijack(conn); nni_aio_set_output(aio, 0, NULL); nni_aio_finish(aio, 0, 0); + nni_mtx_unlock(&l->mtx); return; err: @@ -1666,6 +1675,7 @@ err: nni_aio_set_output(aio, 0, res); nni_aio_finish(aio, 0, 0); } + nni_mtx_unlock(&l->mtx); } static void diff --git a/src/supplemental/websocket/wssfile_test.c b/src/supplemental/websocket/wssfile_test.c index 59003123..ee7c8ffd 100644 --- a/src/supplemental/websocket/wssfile_test.c +++ b/src/supplemental/websocket/wssfile_test.c @@ -254,7 +254,7 @@ test_verify_works(void) NUTS_PASS(nng_dialer_start(d, 0)); nng_msleep(100); - NUTS_PASS(nng_send(s1, "hello", 6, 0)); + NUTS_SEND(s1, "hello"); NUTS_PASS(nng_recvmsg(s2, &msg, 0)); NUTS_ASSERT(msg != NULL); NUTS_TRUE(nng_msg_len(msg) == 6); -- cgit v1.2.3-70-g09d2