You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
lws_http_mount.origin_protocol=LWSMPRO_FILE protocol is designed such a way that the static files get served automatically without hitting the http_callback.
If lws_http_mount.origin_protocol is set to LWSMPRO_CALLBACK, all the requests will hit the http_callback and files to be served using lws_serve_http_file(). For this lws_http_mount.origin is not considered, so path needs to be set properly before calling lws_serve_http_file().
Based on above understanding, I would like to get your inputs on the following.
I would like to have cookie based authentication my server. So looking for any option in which static files gets served only after hitting the http_callback where I can validate the cookies and leave it to library to serve the static files. Is there any possibility to have two different protocols one for file and other for callback, still achieve the required functionality? Please share your opinions.
I had continued my analysis and learnt that LWS_CALLBACK_FILTER_HTTP_CONNECTION event received for all the requests including for static files. So, I set the mount.origin_protocol=LWSMPRO_FILE and handled cookie validation at LWS_CALLBACK_FILTER_HTTP_CONNECTION and proceed with the existing flow as mentioned below.
Incase of invalid cookie, returning -1 to end stream.
case LWS_CALLBACK_FILTER_HTTP_CONNECTION:
{
const char *requested_uri = (const char*)in;
printf("LWS_CALLBACK_FILTER_HTTP_CONNECTION: %s\n", requested_uri);
//Check whether cookie validation needed for a requested URL
bool needCookieValidation = isCookieValidationRequired(requested_uri);
if(needCookieValidation)
{
//Check for cookies in the request headers
char cookie[256] = {0};
int n = lws_hdr_copy(wsi, cookie, sizeof(cookie), WSI_TOKEN_HTTP_COOKIE);
if ((n > 0) && !strstr(cookie, "Cookie"))
{
printf("Received Cookie in the request header\r\n");
if(!isThisValidCookie(cookie))
{
//Redirect to login.html
if(lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
(unsigned char *)"/login.html",
11, &p, end)<0)
return -1;
return -1;
}
}
else //Cookie is not part of request header
{
//Redirect to login.html
if(lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
(unsigned char *)"/login.html",
11, &p, end)<0)
return -1;
return -1;
}
}
else
{
printf("Cookie validation not needed.. proceeding\r\n");
}
}
break;
Is this a good approach or any better option is available? Please provide your suggestions.
Hi Andy,
lws_http_mount.origin_protocol=LWSMPRO_FILE protocol is designed such a way that the static files get served automatically without hitting the http_callback.
If lws_http_mount.origin_protocol is set to LWSMPRO_CALLBACK, all the requests will hit the http_callback and files to be served using lws_serve_http_file(). For this lws_http_mount.origin is not considered, so path needs to be set properly before calling lws_serve_http_file().
Based on above understanding, I would like to get your inputs on the following.
Sample Code of server:
The text was updated successfully, but these errors were encountered: