[Libwebsockets] Better way to send large http response

Andy Green andy at warmcat.com
Wed Sep 19 01:28:33 CEST 2018



On 19/09/2018 07:17, Chanson Shen wrote:
> Hi all,
> 
> I'm running libwebsocket v3.0-stable and trying to implement RESTful API 
> support.
> 
> When the server tries to send large message(=45431 bytes) back to 
> client. In `LWS_CALLBACK_HTTP_WRITEABLE`, `lws_write()` returns 45431, 
> which means it accepts all the data. And 

Lws accepted all the data, but it's unrelated as to whether it could 
send it on immediately.

> `lws_http_transaction_completed()` returns 1. Then `return -1` is called 
> to close the connection.

Yeah.

> But it seems `LWS_CALLBACK_HTTP_WRITEABLE` will be invoked again with 
> `lws_send_pipe_choked()` returning 1. On client side, it only receives

Hm that shouldn't be so.  You can get unexpected WRITEABLE callbacks, 
but if so the connection should always actually be writeable.

> part of the data. I was expecting all the data had been sent, and the 
> connection should be closed successfully.
> 
> I checked the document, and I am not sure if it is related to `truncated 
> writes` mentioned in this 
> page:https://libwebsockets.org/lws-api-doc-master/html/group__sending-data.html
> 
> Any idea on my issue and what's the best way to send large http response 
> message? Thanks!

I guess it is related to truncated writes... basically lws hides that it 
might take multiple attempts to send what you asked.  When it has 
finished, it should enable WRITABLE callbacks again so you can send 
something more if that's what you want.

There's a race between lws buffering stuff to send at the rate the 
client will accept it, and transaction completed.  But lws knows about 
the possibility and defers the transaction completed if so, returning 0...

https://libwebsockets.org/git/libwebsockets/tree/lib/roles/http/server/server.c?h=v3.0-stable#n1723-1741

But you are saying

 > `lws_http_transaction_completed()` returns 1.

so I am not sure what actually happens.

Can you maybe hack a minimal example to make the same issue come with a 
small patch?

Or try the same thing on master?  Master extended the internal use of 
"buflists" to the implementation of the truncated send buffers, relaxing 
various constraints and changing how this works.

-Andy

> Best,
> Chanson
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list