From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- .../examples/_obsolete/websocket_client/Makefile | 37 ++ .../_obsolete/websocket_client/ssl/server.crt | 13 + .../_obsolete/websocket_client/ssl/server.csr | 11 + .../_obsolete/websocket_client/ssl/server.key | 15 + .../_obsolete/websocket_client/ssl/server.key.orig | 18 + .../_obsolete/websocket_client/ssl/server.pem | 28 ++ .../_obsolete/websocket_client/websocket_client.c | 421 +++++++++++++++++++++ 7 files changed, 543 insertions(+) create mode 100644 src/civetweb/examples/_obsolete/websocket_client/Makefile create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.key create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem create mode 100644 src/civetweb/examples/_obsolete/websocket_client/websocket_client.c (limited to 'src/civetweb/examples/_obsolete/websocket_client') diff --git a/src/civetweb/examples/_obsolete/websocket_client/Makefile b/src/civetweb/examples/_obsolete/websocket_client/Makefile new file mode 100644 index 000000000..e3ef13468 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/Makefile @@ -0,0 +1,37 @@ +# +# Copyright (c) 2014 Jordan Shelley +# https://github.com/jshelley +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = websocket_client +SRC = websocket_client.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt new file mode 100644 index 000000000..a26359abf --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE----- +MIICATCCAWoCCQC2BCIqIvgSUTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE0MDgyMTEyMzAwMVoXDTI0MDgxODEyMzAwMVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA9k9s +gH22BCo9neTeB/YnilK7n0sMe0+pjS9KSWhU59Q4w8hqPrW0tuYikIDd0wVggkJF +BZNg4WPoulTdwXsgNBeG88q2wnNtUosXTS+KQTQBSiQof9Ay9GHQtgxnogI1zIXb +HOppyyG5zre8a/X6fzDOnFc4iJMBwxTAnjCqObkCAwEAATANBgkqhkiG9w0BAQUF +AAOBgQBX9V46VUVsB9P9fb8sFuMx2ezFE42ynEeJPrKRrof+dFYbjvR1OUZFSLCy +aZKwVH7iCnVBJiU12JxO7PR3L6ob3FYPyNHQWYq1/IFUvqBRagehldj5H8iFeEDz +Wtz2+p1rUyVxcSUqTjobaji0aC8lzPZio0nd1KKM6A92/adHyQ== +-----END CERTIFICATE----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr new file mode 100644 index 000000000..4d4723b24 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEh +MB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB +AQUAA4GNADCBiQKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjD +yGo+tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFK +JCh/0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQID +AQABoAAwDQYJKoZIhvcNAQEFBQADgYEA1EOFwyFJ2NAnRNktZCy5yVcLx9C78HoC +oHPPCOElu0VDIqe6ZecYdaqWbYlhGE0+isbOQn2CwHOeBGN8mIDsNUYzVEpsEfgg +9OK873LpE5pf4mdjSiRBXkk/h8BxuqkcKi+Qx+qEE7+dH2nK5aKeIHVvbLyfGOch +9I85q+msBNE= +-----END CERTIFICATE REQUEST----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key new file mode 100644 index 000000000..f09cc38cb --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjDyGo+ +tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFKJCh/ +0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQIDAQAB +AoGAYwospr3lomcZv5N3c9wWqhf6OWMD8dFma87IIBxDh7Rd3tuHXQ/TSnffDhvD +FkbjN31OI5/PJNH3knTtdg78MywPloE4jYsbt4+fEaW7Fzww2nU61N1p+mYk5d/b +SCTAHhGzF9g9ZMw25CCUFGjDU2z+Ty6my22Euxhk2Qq8tMECQQD9ZYIxWkPhywDW +pX3v70dqIv7411hEYpuL/ZJl26UCmQsj4HPtXQCraQksVPs74WY5aTtd6MAV9V3M +UnErHO5/AkEA+NdG2MmfBOBPusDB/WwQaUPiCWGITS9llGTR2JXbvDqmKgL1+UTG +o27sLNIFCrF1wejpyRGqwjcObFYR0yKrxwJBAOB2uPuK4DL1psp9Uq/mIDbOxVod +OF1rlCpP9w0vol5Iv+uJ+mc7SUqOAsg4h0yl/+2/YA1yDiXlcq96IDF2sXUCQGAv +Nh9Nr72+xpK1N0axopZNuu1NWdYb3/PAFKzXIBxdvyS2CEXVo8JAeeHJPFGpzo6p +bNRfk9WGWnjdu/4UhLkCQQCekR9zpIpzdJiPYCd6XMya+TPCDYlOQL1jlnJIRa2V +BEOz0rSpzXAGh0PyCB/kMneyVk87LWn8joE6179RoUfv +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig new file mode 100644 index 000000000..58e5653ca --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig @@ -0,0 +1,18 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,89778A6427F05D4A + +4aXqO/8oCHVfMLB+a1DfjbXyEddjbd7nB+YVFLPKy68Tam9PRTvC1zRHBet59ll0 +1w7R8tXR6/xH7HRhBeqDHCcuvBhtw3xGxtXWv54WBFhzuq7TvKOAaCFl++cw/JHq +PCS0rAaYnqF2MAgMi7QBjZKmHFHL43Gy60VfOrB0mmOdxqqXA0NBFC2uEd7Z/MAx +S2A85bNJJKQaWEeDThP1u0OOlNCq99lkLJ31jiOH7ntdL0/vqcbZ+PUtdPLwAG4L +1GUHuiC2v5FvDlPiejMk2dvrxCNpcu2e3tQKHpg2KcsTVrpB7EVzRSazln4HywUZ +EJfBvxqqrS7plImZgj4LXSnln0JPuBb+aHnhKIFvisjYSwqDGJnnp/OaD7YdRhYh +UCcL011Ge+yUbRipeAmHdtJlSUSdB14KWq+WdIX/KgCRGx06QZm9s1PBLH+fww+I +EL3A/LFX0a5KUHkCp29akYYv9bUYaQ79Nt7BlaEON+/SW3pJMbGr+nx8aqogr0Yo +SJ/Zz5TSDBhecRjbCDGkT6DizVZ8cbm2xl8QLBd0H+ZA6uYMgfpAOJGrJx3Nm4Lv +prEApgFtjSrsQDGYHAcmDMW1UWOVHuNp7BSvwUze9Ftnzr/jlpdzES2rhgMyGhg1 +0Szbsfs3vgw4iM83LFJXza07GQJzF8gRF79dY5JiQX/sOKUprA6Lofk631jE0G8r +3z59cxblaq9y7EgFsE944Gk7/HIEimBRiqIZzGVJVukD0itynQ+XmYTdbyH1lpvi +c0ZheZPUoGwUW9RYy+nle5gEDFyZWXcCAuJasQvDBXt//r/bso3ZpA== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem new file mode 100644 index 000000000..300834e39 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem @@ -0,0 +1,28 @@ +-----BEGIN CERTIFICATE----- +MIICATCCAWoCCQC2BCIqIvgSUTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE0MDgyMTEyMzAwMVoXDTI0MDgxODEyMzAwMVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA9k9s +gH22BCo9neTeB/YnilK7n0sMe0+pjS9KSWhU59Q4w8hqPrW0tuYikIDd0wVggkJF +BZNg4WPoulTdwXsgNBeG88q2wnNtUosXTS+KQTQBSiQof9Ay9GHQtgxnogI1zIXb +HOppyyG5zre8a/X6fzDOnFc4iJMBwxTAnjCqObkCAwEAATANBgkqhkiG9w0BAQUF +AAOBgQBX9V46VUVsB9P9fb8sFuMx2ezFE42ynEeJPrKRrof+dFYbjvR1OUZFSLCy +aZKwVH7iCnVBJiU12JxO7PR3L6ob3FYPyNHQWYq1/IFUvqBRagehldj5H8iFeEDz +Wtz2+p1rUyVxcSUqTjobaji0aC8lzPZio0nd1KKM6A92/adHyQ== +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjDyGo+ +tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFKJCh/ +0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQIDAQAB +AoGAYwospr3lomcZv5N3c9wWqhf6OWMD8dFma87IIBxDh7Rd3tuHXQ/TSnffDhvD +FkbjN31OI5/PJNH3knTtdg78MywPloE4jYsbt4+fEaW7Fzww2nU61N1p+mYk5d/b +SCTAHhGzF9g9ZMw25CCUFGjDU2z+Ty6my22Euxhk2Qq8tMECQQD9ZYIxWkPhywDW +pX3v70dqIv7411hEYpuL/ZJl26UCmQsj4HPtXQCraQksVPs74WY5aTtd6MAV9V3M +UnErHO5/AkEA+NdG2MmfBOBPusDB/WwQaUPiCWGITS9llGTR2JXbvDqmKgL1+UTG +o27sLNIFCrF1wejpyRGqwjcObFYR0yKrxwJBAOB2uPuK4DL1psp9Uq/mIDbOxVod +OF1rlCpP9w0vol5Iv+uJ+mc7SUqOAsg4h0yl/+2/YA1yDiXlcq96IDF2sXUCQGAv +Nh9Nr72+xpK1N0axopZNuu1NWdYb3/PAFKzXIBxdvyS2CEXVo8JAeeHJPFGpzo6p +bNRfk9WGWnjdu/4UhLkCQQCekR9zpIpzdJiPYCd6XMya+TPCDYlOQL1jlnJIRa2V +BEOz0rSpzXAGh0PyCB/kMneyVk87LWn8joE6179RoUfv +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c b/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c new file mode 100644 index 000000000..158ad7d85 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c @@ -0,0 +1,421 @@ +/* +* Copyright (c) 2014-2016 the Civetweb developers +* Copyright (c) 2014 Jordan Shelley +* https://github.com/jshelley +* License http://opensource.org/licenses/mit-license.php MIT License +*/ + +/* This example is superseeded by other examples, and no longer + * actively maintained. + * See examples/embedded_c for an up to date example. + */ + +// Simple example program on how to use websocket client embedded C interface. +#ifdef _WIN32 +#include +#define sleep(x) Sleep(1000 * (x)) +#else +#include +#endif + +#include +#include +#include "civetweb.h" + +#define DOCUMENT_ROOT "." +#define PORT "8888" +#define SSL_CERT "./ssl/server.pem" + +const char *websocket_welcome_msg = "websocket welcome\n"; +const size_t websocket_welcome_msg_len = 18 /* strlen(websocket_welcome_msg) */; +const char *websocket_acknowledge_msg = "websocket msg ok\n"; +const size_t websocket_acknowledge_msg_len = + 17 /* strlen(websocket_acknowledge_msg) */; +const char *websocket_goodbye_msg = "websocket bye\n"; +const size_t websocket_goodbye_msg_len = 14 /* strlen(websocket_goodbye_msg) */; + + +/*************************************************************************************/ +/* WEBSOCKET SERVER */ +/*************************************************************************************/ +#if defined(MG_LEGACY_INTERFACE) +int +websock_server_connect(const struct mg_connection *conn) +#else +int +websocket_server_connect(const struct mg_connection *conn, void *_ignored) +#endif +{ + printf("Server: Websocket connected\n"); + return 0; /* return 0 to accept every connection */ +} + + +#if defined(MG_LEGACY_INTERFACE) +void +websocket_server_ready(struct mg_connection *conn) +#else +void +websocket_server_ready(struct mg_connection *conn, void *_ignored) +#endif +{ + printf("Server: Websocket ready\n"); + + /* Send websocket welcome message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_welcome_msg, + websocket_welcome_msg_len); + mg_unlock_connection(conn); +} + + +#if defined(MG_LEGACY_INTERFACE) +int +websocket_server_data(struct mg_connection *conn, + int bits, + char *data, + size_t data_len) +#else +int +websocket_server_data(struct mg_connection *conn, + int bits, + char *data, + size_t data_len, + void *_ignored) +#endif +{ + printf("Server: Got %lu bytes from the client\n", (unsigned long)data_len); + printf("Server received data from client: "); + fwrite(data, 1, data_len, stdout); + printf("\n"); + + if (data_len < 3 || 0 != memcmp(data, "bye", 3)) { + /* Send websocket acknowledge message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len); + mg_unlock_connection(conn); + } else { + /* Send websocket acknowledge message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_goodbye_msg, + websocket_goodbye_msg_len); + mg_unlock_connection(conn); + } + + return 1; /* return 1 to keep the connetion open */ +} + + +#if defined(MG_LEGACY_INTERFACE) +void +websocket_server_connection_close(const struct mg_connection *conn) +#else +void +websocket_server_connection_close(const struct mg_connection *conn, + void *_ignored) +#endif +{ + printf("Server: Close connection\n"); + + /* Can not send a websocket goodbye message here - the connection is already + * closed */ +} + + +struct mg_context * +start_websocket_server() +{ + const char *options[] = {"document_root", + DOCUMENT_ROOT, + "ssl_certificate", + SSL_CERT, + "listening_ports", + PORT, + "request_timeout_ms", + "5000", + 0}; + struct mg_callbacks callbacks; + struct mg_context *ctx; + + memset(&callbacks, 0, sizeof(callbacks)); + +#if defined(MG_LEGACY_INTERFACE) + /* Obsolete: */ + callbacks.websocket_connect = websock_server_connect; + callbacks.websocket_ready = websocket_server_ready; + callbacks.websocket_data = websocket_server_data; + callbacks.connection_close = websocket_server_connection_close; + + ctx = mg_start(&callbacks, 0, options); +#else + /* New interface: */ + ctx = mg_start(&callbacks, 0, options); + + mg_set_websocket_handler(ctx, + "/websocket", + websocket_server_connect, + websocket_server_ready, + websocket_server_data, + websocket_server_connection_close, + NULL); +#endif + + return ctx; +} + + +/*************************************************************************************/ +/* WEBSOCKET CLIENT */ +/*************************************************************************************/ +struct tclient_data { + void *data; + size_t len; + int closed; +}; + +static int +websocket_client_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *user_data) +{ + struct mg_context *ctx = mg_get_context(conn); + struct tclient_data *pclient_data = + (struct tclient_data *)mg_get_user_data(ctx); + + printf("Client received data from server: "); + fwrite(data, 1, data_len, stdout); + printf("\n"); + + pclient_data->data = malloc(data_len); + assert(pclient_data->data != NULL); + memcpy(pclient_data->data, data, data_len); + pclient_data->len = data_len; + + return 1; +} + +static void +websocket_client_close_handler(const struct mg_connection *conn, + void *user_data) +{ + struct mg_context *ctx = mg_get_context(conn); + struct tclient_data *pclient_data = + (struct tclient_data *)mg_get_user_data(ctx); + + printf("Client: Close handler\n"); + pclient_data->closed++; +} + + +int +main(int argc, char *argv[]) +{ + struct mg_context *ctx = NULL; + struct tclient_data client1_data = {NULL, 0, 0}; + struct tclient_data client2_data = {NULL, 0, 0}; + struct tclient_data client3_data = {NULL, 0, 0}; + struct mg_connection *newconn1 = NULL; + struct mg_connection *newconn2 = NULL; + struct mg_connection *newconn3 = NULL; + char ebuf[100] = {0}; + + assert(websocket_welcome_msg_len == strlen(websocket_welcome_msg)); + + /* First set up a websocket server */ + ctx = start_websocket_server(); + assert(ctx != NULL); + printf("Server init\n\n"); + + /* Then connect a first client */ + newconn1 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client1_data); + + if (newconn1 == NULL) { + printf("Error: %s", ebuf); + return 1; + } + + sleep(1); /* Should get the websocket welcome message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_welcome_msg_len); + assert(!memcmp(client1_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5); + + sleep(1); /* Should get the acknowledge message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_acknowledge_msg_len); + assert(!memcmp(client1_data.data, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + /* Now connect a second client */ + newconn2 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client2_data); + + if (newconn2 == NULL) { + printf("Error: %s", ebuf); + return 1; + } + + sleep(1); /* Client 2 should get the websocket welcome message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data != NULL); + assert(client2_data.len == websocket_welcome_msg_len); + assert(!memcmp(client2_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client2_data.data); + client2_data.data = NULL; + client2_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5); + + sleep(1); /* Should get the acknowledge message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_acknowledge_msg_len); + assert(!memcmp(client1_data.data, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3); + + sleep(1); /* Should get the goodbye message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_goodbye_msg_len); + assert(!memcmp(client1_data.data, + websocket_goodbye_msg, + websocket_goodbye_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_close_connection(newconn1); + + sleep(1); /* Won't get any message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + + mg_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3); + + sleep(1); /* Should get the goodbye message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data != NULL); + assert(client2_data.len == websocket_goodbye_msg_len); + assert(!memcmp(client2_data.data, + websocket_goodbye_msg, + websocket_goodbye_msg_len)); + free(client2_data.data); + client2_data.data = NULL; + client2_data.len = 0; + + mg_close_connection(newconn2); + + sleep(1); /* Won't get any message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 1); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + + /* Connect client 3 */ + newconn3 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client3_data); + + sleep(1); /* Client 3 should get the websocket welcome message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 1); + assert(client3_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client3_data.data != NULL); + assert(client3_data.len == websocket_welcome_msg_len); + assert(!memcmp(client3_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client3_data.data); + client3_data.data = NULL; + client3_data.len = 0; + + mg_stop(ctx); + printf("Server shutdown\n"); + + sleep(10); + + assert(client3_data.closed == 1); + + return 0; +} -- cgit v1.2.3