mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-30 00:00:16 +01:00

This switches the current hello_world to be hello_world-policy now, and hello_world is simplified to use the __default policy without any explicit policy of its own. Using the default policy means it relies on the system tls library to validate the tls connections using system trust arrangements, that won't work in cases where the configured tls library does not have its own trust store.
81 lines
2.1 KiB
C
81 lines
2.1 KiB
C
/*
|
|
* hello_world example
|
|
*
|
|
* Written in 2010-2022 by Andy Green <andy@warmcat.com>
|
|
*
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
* Universal Public Domain Dedication.
|
|
*
|
|
* Demonstrates the simplest example using the LWS high-level SS apis.
|
|
*
|
|
* - main.c: boilerplate to create the lws_context and event loop
|
|
* - hello_world-ss.c: (this file) the secure stream user code
|
|
*/
|
|
|
|
#include <libwebsockets.h>
|
|
#include <signal.h>
|
|
|
|
extern int test_result;
|
|
|
|
LWS_SS_USER_TYPEDEF
|
|
/* Your per-stream instantiation members go here */
|
|
} hello_world_t;
|
|
|
|
static lws_ss_state_return_t
|
|
hello_world_rx(void *userobj, const uint8_t *in, size_t len, int flags)
|
|
{
|
|
hello_world_t *g = (hello_world_t *)userobj;
|
|
struct lws_ss_handle *h = lws_ss_from_user(g);
|
|
|
|
lwsl_ss_user(h, "RX %zu, flags 0x%x", len, (unsigned int)flags);
|
|
|
|
#if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR)
|
|
if ((flags & LWSSS_FLAG_SOM) == LWSSS_FLAG_SOM) {
|
|
char *md;
|
|
size_t md_len;
|
|
|
|
if (!lws_ss_get_metadata(g->ss, "content-length:", (const void **)&md, &md_len))
|
|
lwsl_ss_user(g->ss, "clen %s", md);
|
|
else
|
|
lwsl_ss_user(g->ss, "Unable to get content length");
|
|
}
|
|
#endif
|
|
|
|
if (len) { /* log the first 16 and last 16 bytes of the chunk */
|
|
lwsl_hexdump_ss_info(h, in, len >= 16 ? 16 : len);
|
|
if (len >= 16)
|
|
lwsl_hexdump_ss_info(h, in + len - 16, 16);
|
|
}
|
|
|
|
if ((flags & LWSSS_FLAG_EOM) == LWSSS_FLAG_EOM) /* had whole message */
|
|
test_result &= ~2;
|
|
|
|
return LWSSSSRET_OK;
|
|
}
|
|
|
|
static lws_ss_state_return_t
|
|
hello_world_state(void *userobj, void *h_src, lws_ss_constate_t state,
|
|
lws_ss_tx_ordinal_t ack)
|
|
{
|
|
hello_world_t *g = (hello_world_t *)userobj;
|
|
|
|
switch ((int)state) {
|
|
case LWSSSCS_CREATING: /* start the transaction as soon as we exist */
|
|
return lws_ss_request_tx(lws_ss_from_user(g));
|
|
|
|
case LWSSSCS_QOS_ACK_REMOTE: /* server liked our request */
|
|
test_result &= ~1;
|
|
break;
|
|
|
|
case LWSSSCS_DISCONNECTED: /* for our example, disconnect = done */
|
|
lws_default_loop_exit(lws_ss_cx_from_user(g));
|
|
break;
|
|
}
|
|
|
|
return LWSSSSRET_OK;
|
|
}
|
|
|
|
LWS_SS_INFO("__default", hello_world_t)
|
|
.rx = hello_world_rx,
|
|
.state = hello_world_state,
|
|
};
|