Ignore:
Timestamp:
Dec 15, 2014, 2:26:31 AM (7 years ago)
Author:
Nick Withers <nick.withers@…>
Branches:
4.11, 5, master
Children:
b2ee119f
Parents:
3acc619
git-author:
Nick Withers <nick.withers@…> (12/15/14 02:26:31)
git-committer:
Sebastian Huber <sebastian.huber@…> (12/15/14 06:44:56)
Message:

Enable WebSocket? support in the Mongoose HTTP server

File:
1 edited

Legend:

Unmodified
Added
Removed
  • testsuites/libtests/mghttpd01/init.c

    r3acc619 rf42d429  
    2424
    2525#include <stdio.h>
     26#include <string.h>
    2627#include <mghttpd/mongoose.h>
    2728
     
    4344                        "\r\n" \
    4445                        "This is a message from the callback function.\r\n"
     46
     47#define WSTEST_REQ      "Test request"
     48#define WSTEST_RESP     "This is a message from the WebSocket callback function."
    4549
    4650#define INDEX_HTML      "HTTP/1.1 200 OK\r\n" \
     
    97101}
    98102
     103static int callback_websocket(struct mg_connection *connection,
     104                              int                  bits,
     105                              char                 *data,
     106                              size_t               data_len)
     107{
     108  if (data_len == strlen(WSTEST_REQ) && strncmp(data, WSTEST_REQ, data_len) == 0)
     109  {
     110    mg_websocket_write(connection, WEBSOCKET_OPCODE_TEXT, WSTEST_RESP, strlen(WSTEST_RESP));
     111
     112    /* Don't close the WebSocket */
     113    return 1;
     114  }
     115
     116  return 0;
     117}
     118
    99119static void test_mg_index_html(void)
    100120{
     
    165185}
    166186
     187static void test_mg_websocket(void)
     188{
     189  httpc_context httpc_ctx;
     190  char *buffer = malloc(BUFFERSIZE);
     191  bool brv = false;
     192  int rv = 0;
     193
     194  rtems_test_assert(buffer != NULL);
     195
     196  puts("=== Get a WebSocket response generated from a callback function" \
     197      " from first Mongoose instance:");
     198
     199  httpc_init_context(&httpc_ctx);
     200  brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80);
     201  rtems_test_assert(brv);
     202  brv = httpc_ws_open_connection(&httpc_ctx);
     203  rtems_test_assert(brv);
     204  brv = httpc_ws_send_request(&httpc_ctx, WSTEST_REQ, buffer, BUFFERSIZE);
     205  rtems_test_assert(brv);
     206  brv = httpc_close_connection(&httpc_ctx);
     207  rtems_test_assert(brv);
     208  puts(buffer);
     209  rv = strcmp(buffer, WSTEST_RESP);
     210  rtems_test_assert(rv == 0);
     211
     212  puts("=== OK");
     213
     214  free(buffer);
     215}
     216
    167217static void test_mongoose(void)
    168218{
    169219  const struct mg_callbacks callbacks = {
    170     .begin_request = callback
     220    .begin_request = callback,
     221    .websocket_data = callback_websocket
    171222  };
    172223  const char *options[] = {
     
    193244  test_mg_index_html();
    194245  test_mg_callback();
     246  test_mg_websocket();
    195247
    196248  mg_stop(mg1);
Note: See TracChangeset for help on using the changeset viewer.