Search code examples
ajaxhttp-headersg-wan

Can I add Access-Control-Allow-Origin: * to the default headers


I'm currently running a G-WAN server that hosts static HTML files. Right now I'm using an iframe to show the contents of these files. I would like to be able to load them cross-domain with JavaScript. Which, by default, is not allowed by the browser.

A simple fix is to add the header: Access-Control-Allow-Origin: * to the HTTP response. The problem is, I would like to avoid making a G-WAN servlet for returning static HTML, just to add the header.

Is it possible to add the header by default?


Solution

  • Is it possible to add [HTTP] headers by default?

    Sure, add the following code in a G-WAN connection handler:

      case HDL_BEFORE_WRITE:
      {
         char head[] = "Access-Control-Allow-Origin: *\r\n\r\n";
         http_header(HEAD_ADD, head, sizeof(head) - 1, argv);
         break;
      }
    

    This will be available for all replies, static and dynamic.

    If you want to be more selective, just add your filter in the code above.