[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 
> LWS_CALLBACK_HTTP_BODY.

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.

-Andy
-------------- 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
Host: 192.168.1.42:7681
User-Agent: Go-http-client/1.1
Content-Length: 898
Content-Type: application/json
Accept-Encoding: gzip

{"applicationID":"2","applicationName":"stmWLapp","deviceName":"STM32WLendnode","devEUI":"AIDhFQUAXi4=","rxInfo":[{"gatewayID":"qEBBHs2sQVA=","time":"2021-02-25T10:27:11.234371Z","timeSinceGPSEpoch":null,"rssi":-84,"loRaSNR":9,"channel":4,"rfChain":0,"board":0,"antenna":0,"location":{"latitude":0,"longitude":0,"altitude":300,"source":"UNKNOWN","accuracy":0},"fineTimestampType":"NONE","context":"XszlAw==","uplinkID":"lfd3ipaRS/6dTp3JjzCRhQ==","crcStatus":"CRC_OK"}],"txInfo":{"frequency":867300000,"modulation":"LORA","loRaModulationInfo":{"bandwidth":125,"spreadingFactor":7,"codeRate":"4/5","polarizationInversion":false}},"adr":true,"dr":5,"fCnt":1175,"fPort":2,"data":"AGcA3AECAAACAP4DAAQEAQA=","objectJSON":"{\"digitalInput\":{\"2\":254,\"3\":4},\"digitalOutput\":{\"4\":0},\"analogInput\":{\"1\":0},\"temperatureSensor\":{\"0\":22}}","tags":{},"confirmedUplink":false,"devAddr":"L/zacQ=="}


More information about the Libwebsockets mailing list