[Libwebsockets] Adding a custom header

Andy Green andy at warmcat.com
Thu Nov 12 01:59:09 CET 2020



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