I had to fight for hours to get to these few lines of code that solve the problem perfectly, so I share them here below.
First, you have to create a main.c file into handlers folder (please create/rename the folder if it's not existing or it's disabled) of your G-WAN virtualhost and copy/paste what follows:
// Add simple CORS header (Access-Control-Allow-Origin: *) to all resources
#include "gwan.h" // G-WAN exported functions
int init(int argc, char *argv[])
{
u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
*states = (1 << HDL_BEFORE_WRITE);
return 0;
}
void clean(int argc, char *argv[])
{
}
int main(int argc, char *argv[])
{
char head[] = "Access-Control-Allow-Origin: *\r\n";
http_header(HEAD_ADD, head, sizeof(head) - 1, argv);
return(255);
}
Then, kill g-wan, run it as root to compile the script, re-kill and run it as your web user (please change /var/www path with your g-wan root and www-data-user with the web user you're using...also, use sudo if you're not logged in as root):
/var/www/gwan -k
/var/www/gwan
(now CTRL-C to exit)
/var/www/gwan -k
/var/www/gwan -d:www-data:www-data-user
Now all your resources will have a beautiful Access-Control-Allow-Origin: * header :-)