From beacbbb38c5ee56b7e95b427bf575952c3d1fd94 Mon Sep 17 00:00:00 2001 From: Soumendra Ganguly Date: Mon, 27 Jul 2020 00:39:23 -0500 Subject: [PATCH] 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. --- .../minimal-http-server-dynamic.c | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c b/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c index 77ca6947a..af0b220f3 100644 --- a/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c +++ b/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c @@ -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 */