diff options
author | Nick Withers <nick.withers@anu.edu.au> | 2014-12-15 13:26:31 +1100 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2014-12-15 07:44:56 +0100 |
commit | f42d4292cbbfa445ab8b89545832960da325fb2f (patch) | |
tree | 867494618a2d959a460702443cbaf71ffdce0f0b /testsuites/libtests/mghttpd01/init.c | |
parent | bsp/lpc32xx: Fix memory map (diff) | |
download | rtems-f42d4292cbbfa445ab8b89545832960da325fb2f.tar.bz2 |
Enable WebSocket support in the Mongoose HTTP server
Diffstat (limited to 'testsuites/libtests/mghttpd01/init.c')
-rw-r--r-- | testsuites/libtests/mghttpd01/init.c | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/testsuites/libtests/mghttpd01/init.c b/testsuites/libtests/mghttpd01/init.c index a5eee8c865..9f285705b3 100644 --- a/testsuites/libtests/mghttpd01/init.c +++ b/testsuites/libtests/mghttpd01/init.c @@ -23,6 +23,7 @@ #include <rtems/rtems_bsdnet.h> #include <stdio.h> +#include <string.h> #include <mghttpd/mongoose.h> #include <rtems/imfs.h> @@ -43,6 +44,9 @@ const char rtems_test_name[] = "MGHTTPD 1"; "\r\n" \ "This is a message from the callback function.\r\n" +#define WSTEST_REQ "Test request" +#define WSTEST_RESP "This is a message from the WebSocket callback function." + #define INDEX_HTML "HTTP/1.1 200 OK\r\n" \ "Date: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \ "Last-Modified: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \ @@ -96,6 +100,22 @@ static int callback(struct mg_connection *conn) return 0; } +static int callback_websocket(struct mg_connection *connection, + int bits, + char *data, + size_t data_len) +{ + if (data_len == strlen(WSTEST_REQ) && strncmp(data, WSTEST_REQ, data_len) == 0) + { + mg_websocket_write(connection, WEBSOCKET_OPCODE_TEXT, WSTEST_RESP, strlen(WSTEST_RESP)); + + /* Don't close the WebSocket */ + return 1; + } + + return 0; +} + static void test_mg_index_html(void) { httpc_context httpc_ctx; @@ -164,10 +184,41 @@ static void test_mg_callback(void) free(buffer); } +static void test_mg_websocket(void) +{ + httpc_context httpc_ctx; + char *buffer = malloc(BUFFERSIZE); + bool brv = false; + int rv = 0; + + rtems_test_assert(buffer != NULL); + + puts("=== Get a WebSocket response generated from a callback function" \ + " from first Mongoose instance:"); + + httpc_init_context(&httpc_ctx); + brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80); + rtems_test_assert(brv); + brv = httpc_ws_open_connection(&httpc_ctx); + rtems_test_assert(brv); + brv = httpc_ws_send_request(&httpc_ctx, WSTEST_REQ, buffer, BUFFERSIZE); + rtems_test_assert(brv); + brv = httpc_close_connection(&httpc_ctx); + rtems_test_assert(brv); + puts(buffer); + rv = strcmp(buffer, WSTEST_RESP); + rtems_test_assert(rv == 0); + + puts("=== OK"); + + free(buffer); +} + static void test_mongoose(void) { const struct mg_callbacks callbacks = { - .begin_request = callback + .begin_request = callback, + .websocket_data = callback_websocket }; const char *options[] = { "listening_ports", "80", @@ -192,6 +243,7 @@ static void test_mongoose(void) test_mg_index_html(); test_mg_callback(); + test_mg_websocket(); mg_stop(mg1); mg_stop(mg2); |