[Libwebsockets] Need to send additional headers from websockets client to server

Andy Green andy at warmcat.com
Sat Jan 9 21:10:31 CET 2016



On 01/10/2016 04:05 AM, Mark Goldman wrote:
> After looking closer at the code and the API documentation, I see that
> I missed that this was funneled through protocol[0].  Sorry for the
> ID10T error.

This is a bit complicated... we provide nice examples in the test server 
for adding headers but not in the client code.

> Would there be any harm in the client looking in the protocols array

At that time, the protocol that will be used simply hasn't been 
negotiated yet.  Your client may offer multiple supported protocols and 
there is no way to know which one will get negotiated until after the 
back-and-forth with the server has happened.  So we can't bind these 
pre-handshake callbacks to a specific ws protocol.

-Andy

> so the "correct" callback can be used for client connects the that
> should be known at libwebsocket_client_connect() time?  I feel like
> that would be less surprising for client users that have multiple
> protocols.
>
> Also, thank you.
>
> -mdg
>
> On Sat, Jan 9, 2016 at 2:31 PM, Andy Green <andy at warmcat.com> wrote:
>>
>>
>> On January 10, 2016 2:17:58 AM GMT+08:00, Mark Goldman <mgoldman at securityevaluators.com> wrote:
>>> I've been trying to figure out how my websockets client can send an
>>> additional header to the server but I can't seem to find any guidance.
>>> After looking through the code, it appears that there isn't a way to
>>> get an additional header in from the client side, is this correct?
>>
>> No, you get a user callback LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER to protocols[0], after the connect but before sending the upgrade request, where you can stick cookies in or whatever.
>>
>> For that callback the in parameter is a char ** pointing to the header buffer you may add up to len chars to.  *p must be set to the end of what you added.
>>
>> case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
>> {
>>     char **p = (char **)in;
>>
>>     if (len < 20)
>>        return 1;
>>
>>     *p += sprintf(*p, "myheader: awesome\r\n");
>> }
>> break;
>>
>> Watch out though user data has not been allocated and is NULL.
>>
>> -Andy
>>
>>> -mdg
>>> _______________________________________________
>>> Libwebsockets mailing list
>>> Libwebsockets at ml.libwebsockets.org
>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>



More information about the Libwebsockets mailing list