[Libwebsockets] Adding a custom header

Andy Green andy at warmcat.com
Thu Nov 12 07:31:22 CET 2020



On 11/12/20 6:25 AM, Søren Holm wrote:
> well ..... so to be clear I want to set a cookie, so I probably want the callback.
> But I never get it.

... want to set a cookie when you are serving custom, dynamic http?

Then you already have some code producing the headers and can just add 
your cookie header there.  Eg, like this

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c#n92

LWS_CALLBACK_ADD_HEADERS is for the other cases I mentioned where you 
are not already in control of creating the response and need a hook so 
you can piggyback additional dynamic headers even for, eg, generic 
fileserving case.

-Andy

> My handler and protocols look like this.
> 
> 
> static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
> 	switch (reason) {
> 		<cases for all the unual http reasons>
> 		case LWS_CALLBACK_ADD_HEADERS:
> 	}
> 	return lws_callback_http_dummy(wsi, reason, user, in, len);
> }
> 
> 
> static struct lws_protocols protocols[] = {
> 	{
> 		"http",
> 		callback_http,
> 		sizeof(struct pss),
> 		0, 0
> 
> 	},	{
> 	    "dumb-increment-protocol",
> 	    callback_dumb_increment,
> 	    0,
> 		0
> 	}, {
> 	    NULL, NULL, 0, 0
> 	}
> };
> 
> 
> torsdag den 12. november 2020 01.59.09 CET skrev Andy Green:
>> On 11/11/20 10:14 PM, Søren Holm wrote:
>>> Hi
>>>
>>> According to the documentation I can get a LWS_CALLBACK_ADD_HEADERS in my
>>> http callback function to add additional headers, but the callback never
>>> happens.
>>>
>>> Me code is basically the minimal-http-server-form-post-file with very few
>>> modifications.
>>>
>>> I hope you can help me out - I've been fighting for hours.
>>
>> Sometimes for file post you are creating your own response headers in
>> code, you can put what you want in there at that time.  In the example
>> you mention it takes the approach to do a redirect when the post
>> completes instead.
>>
>> For server static header addition, you can set any static headers you
>> want added in responses by providing a linked-list of pvos at vhost
>> creation time
>>
>> 	const struct lws_protocol_vhost_options *headers;
>> 		/**< VHOST: pointer to optional linked list of per-vhost
>> 		 * canned headers that are added to server responses */
>>
>> Any http responses will go out with those headers, this is, eg, how
>> lwsws / libwebsockets.org is issuing its Content Security Policy headers.
>>
>> LWS_CALLBACK_ADD_HEADERS is covering the case you want to issue custom
>> dynamic headers either on content served via a _FILE mount, or in the ws
>> upgrade response.  For the former, it's going to direct the callback to
>> the .protocol name mentioned in the mount... in the example case, just
>> set the mount's .protocol to "http" instead of NULL so it can bind to
>> the protocol handler in the example and issue ADD_HEADERS there.
>>
>> -Andy
> 
> 


More information about the Libwebsockets mailing list