[Libwebsockets] Adding a custom header

Søren Holm sgh at sgh.dk
Thu Nov 12 07:25:15 CET 2020

well ..... so to be clear I want to set a cookie, so I probably want the callback.
But I never get it. 

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>
	return lws_callback_http_dummy(wsi, reason, user, in, len);

static struct lws_protocols protocols[] = {
		sizeof(struct pss),
		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

Søren Holm

More information about the Libwebsockets mailing list