#include "config.h"
#include <stdint.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "compat.h"
#include "ff-plugins.h"
#include "webserver.h"

PLUGIN_INFO *plugin_info(void);
void plugin_handler(WS_CONNINFO *pwsc);
int plugin_can_handle(WS_CONNINFO *pwsc);

PLUGIN_OUTPUT_FN _pofn = { plugin_can_handle, plugin_handler, NULL };

PLUGIN_INFO _pi = {
	PLUGIN_VERSION,
	PLUGIN_OUTPUT,
	"crossdomain.xml/1.0",
	&_pofn,
	NULL,
	NULL,
	NULL,
	NULL
};

PLUGIN_INFO *plugin_info(void) {
    return &_pi;
}

int plugin_can_handle(WS_CONNINFO *pwsc) {
    if(strncasecmp(pi_ws_uri(pwsc),"/crossdomain.xml",17) == 0)
        return TRUE;
    return FALSE;
}

void plugin_handler(WS_CONNINFO *pwsc) {
	pi_ws_addresponseheader(pwsc,"Content-Type","text/xml");
	pi_ws_addresponseheader(pwsc,"Connection","Close");
	pi_ws_will_close(pwsc);
	pi_ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
	pi_ws_emitheaders(pwsc);
	pi_ws_writefd(pwsc, "<?xml version=\"1.0\"?>\n");
	pi_ws_writefd(pwsc, "<cross-domain-policy>\n");
	pi_ws_writefd(pwsc, "  <allow-access-from domain=\"*\" />\n");
	pi_ws_writefd(pwsc, "</cross-domain-policy>\n");
}

