[Libwebsockets] Multiple packets/transmission over http

Andy Green andy at warmcat.com
Sat Jul 30 00:03:56 CEST 2016

On July 30, 2016 4:53:01 AM GMT+08:00, Srikar Vadlamani <SrikarVadlamani at trafficware.com> wrote:
>I need to be able to transmit multiple packets/data over http as a
>response to one request to the server,
>The data will be aggregated outside the context of the callback
>function, what is the proper way of achieving this?

Two ways... first is do it by hand

 - use the HTTP callback to look at the URL path and signal the other thread to start generating the page data

 - send headers either telling the final content-length if known, or telling you will use chunked encoding if not


 - as the chunks of page data come ready in a buffer, use lws_callback_on_writable(wsi) from the other thread to tell the lws thread something to send

 - from the WRITEABLE callback write the chunk out and signal the other thread the buffer is done... if it was the last chunk, complete the transaction with

if (lws_http_transaction_completed(wsi))
		return -1;

	return 0;

The other way is use lws CGI support to spawn a script or separate program, and send his stdout back to the peer.  The peer request information gets passed into the script / program as his stdin and via environment vars.  If the data is being produced by running a separate process anyway, putting a cgi script around it could be a very natural solution.


https://libwebsockets.org runs on lws, and eg the gitc gitweb stuff and mailman is produced using this lws cgi support


CGIs are still responsible for generating http headers and producing chunked encoding format if they choose to use that.  But they can do that just randomly doing echo or printf, lws takes care of regulating sending it as the peer becomes writable etc.


 - do one of a) use lwsws, b) make info.protocols NULL at context creation time so the internal lws dummy http handler is used, which knows how to map CGI to http, or c) cut and paste the cgi handling from the dummy handler into your http protocol callback


 - add a mount to your vhost of cgi:// (LWSMPRO_CGI) with the origin string the filepath of the program / script to run when a client requests a url at the mountpoint


>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list