1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

minimal-examples: http-server-dyn: Check if the first part of received url is our mountpoint

In the case of the http-server-dyn example, there is only one protocol.
The mounts will override the area of the url space they are set up for,
and the "gaps" will fall back to the default protocol, which in turn
defaults to protocol[0] at the moment.  So you always get the dyn
protocol action on unknown parts of the URL space.

This solves that behaviour by adding a new protocols[0] that calls
straight through to the default protocol handler lws_callback_http_dummy(),
and moves the dyn handler to be protocols[1].

In addition it shows how to check the url path in the dyn handler for
cases where user code doesn't want to add the second protocol.
This commit is contained in:
Soumendra Ganguly 2020-07-27 00:39:23 -05:00 committed by Andy Green
parent 80ecbb754d
commit beacbbb38c

View file

@ -49,11 +49,27 @@ callback_dynamic_http(struct lws *wsi, enum lws_callback_reasons reason,
switch (reason) {
case LWS_CALLBACK_HTTP:
/* in contains the url part after our mountpoint /dyn, if any */
lws_snprintf(pss->path, sizeof(pss->path), "%s", (const char *)in);
/*
* If you want to know the full url path used, you can get it
* like this
*
* n = lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI);
*
* The base path is the first (n - strlen((const char *)in))
* chars in buf.
*/
/*
* In contains the url part after the place the mount was
* positioned at, eg, if positioned at "/dyn" and given
* "/dyn/mypath", in will contain /mypath
*/
lws_snprintf(pss->path, sizeof(pss->path), "%s",
(const char *)in);
lws_get_peer_simple(wsi, (char *)buf, sizeof(buf));
lwsl_notice("%s: HTTP: connection %s\n", __func__, (const char *)buf);
lwsl_notice("%s: HTTP: connection %s, path %s\n", __func__,
(const char *)buf, pss->path);
/*
* prepare and write http headers... with regards to content-
@ -178,10 +194,11 @@ callback_dynamic_http(struct lws *wsi, enum lws_callback_reasons reason,
return lws_callback_http_dummy(wsi, reason, user, in, len);
}
static const struct lws_protocols protocol =
static const struct lws_protocols defprot =
{ "defprot", lws_callback_http_dummy, 0, 0 }, protocol =
{ "http", callback_dynamic_http, sizeof(struct pss), 0 };
static const struct lws_protocols *pprotocols[] = { &protocol, NULL };
static const struct lws_protocols *pprotocols[] = { &defprot, &protocol, NULL };
/* override the default mount for /dyn in the URL space */