2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
20 #include "test-server.h"
22 /* dumb_increment protocol */
25 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
26 void *user, void *in, size_t len)
28 unsigned char buf[LWS_PRE + 512];
29 struct per_session_data__dumb_increment *pss =
30 (struct per_session_data__dumb_increment *)user;
31 unsigned char *p = &buf[LWS_PRE];
36 case LWS_CALLBACK_ESTABLISHED:
40 case LWS_CALLBACK_SERVER_WRITEABLE:
41 n = sprintf((char *)p, "%d", pss->number++);
42 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
44 lwsl_err("ERROR %d writing to di socket\n", n);
47 if (close_testing && pss->number == 50) {
48 lwsl_info("close tesing limit, closing\n");
53 case LWS_CALLBACK_RECEIVE:
56 if (strcmp((const char *)in, "reset\n") == 0)
58 if (strcmp((const char *)in, "closeme\n") == 0) {
59 lwsl_notice("dumb_inc: closing as requested\n");
60 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
61 (unsigned char *)"seeya", 5);
66 * this just demonstrates how to use the protocol filter. If you won't
67 * study and reject connections based on header content, you don't need
68 * to handle this callback
70 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
71 dump_handshake_info(wsi);
72 /* you could return non-zero here and kill the connection */
76 * this just demonstrates how to handle
77 * LWS_CALLBACK_WS_PEER_INITIATED_CLOSE and extract the peer's close
78 * code and auxiliary data. You can just not handle it if you don't
79 * have a use for this.
81 case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
82 lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %d\n",
84 for (n = 0; n < (int)len; n++)
85 lwsl_notice(" %d: 0x%02X\n", n,
86 ((unsigned char *)in)[n]);