[Libwebsockets] POST messages callbacks

Andy Green andy at warmcat.com
Wed Mar 3 17:00:03 CET 2021

On 3/3/21 3:03 PM, mbargauan at comm5g.com wrote:
> Thank you, Andy.
> After some more troubleshooting, I can say that my problem arise from 
> having "/" as URI and default file is delivered as a normal GET even if 
> it is a POST, never calling LWS_CALLBACK_HTTP_BODY.
> Same when  URI points to a file that exist in  LOCAL_RESOURCE_PATH - eg: 
> duplicated index.html as index2.html, file delivered, no 

For lws http serving, what is mapped where in the url space is defined 
by the lws_http_mount list passed into the vhost creation.

You can bind that mount to your protocol (by setting the mount's 
.protocol to the name of your protocol you want to handle it) and have 
it get first dibs on anything happening there.  Otherwise in the case of 
the LWSMPRO_FILE mount and the URL being the exact mount point, like 
"/", the mount will handle its mountpoint (and may define a default 
sub-url to serve if none given, usually "index.html").

> Any other URI works correctly. providing that protocol at 
> LWS_CALLBACK_HTTP does nothing and returns with 0.
> I am not very deep in source code but it seems that decision on calling 
> LWS_CALLBACK_HTTP_BODY is taken inside lws_http_action()
> line 1762 if (lwsi_state(wsi) == LRS_ISSUING_FILE)
> return 0;

No all of that is doing its job OK, it's about making the mount prefer 
your protocol for / instead.  The mount type is LWSMPRO_FILE, it's 
saying it's describing what files to server from the mountpoint as its 
function, normally the internal dummy callback is enough to make all 
that work without any custom protocol.

Attached are the same as before but the txn going to the original / and 
the patch on the example with just two one-line edits

  - additionally binding the mount to your protocol name

  - making the _HTTP callback check for "" (the sub-path from the mount) 
instead of /form1 as it normally does

then it's doing what you want without any changes in lws.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: example-hack-2.diff
Type: text/x-patch
Size: 1674 bytes
Desc: not available
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20210303/76df411f/attachment.bin>
-------------- next part --------------
POST /?event=up HTTP/1.1
User-Agent: Go-http-client/1.1
Content-Length: 898
Content-Type: application/json
Accept-Encoding: gzip


More information about the Libwebsockets mailing list