[httpd] Enable CORS headers by default (Access-Control-Allow-Origin: *)

This commit is contained in:
ejurgensen 2016-10-17 22:03:32 +02:00
parent 7dc2a46261
commit 0b29b4368d
2 changed files with 10 additions and 5 deletions

View File

@ -56,7 +56,7 @@ static cfg_opt_t sec_general[] =
CFG_STR("cache_path", STATEDIR "/cache/" PACKAGE "/cache.db", CFGF_NONE), CFG_STR("cache_path", STATEDIR "/cache/" PACKAGE "/cache.db", CFGF_NONE),
CFG_INT("cache_daap_threshold", 1000, CFGF_NONE), CFG_INT("cache_daap_threshold", 1000, CFGF_NONE),
CFG_BOOL("speaker_autoselect", cfg_true, CFGF_NONE), CFG_BOOL("speaker_autoselect", cfg_true, CFGF_NONE),
CFG_STR("allow_origin", NULL, CFGF_NONE), CFG_STR("allow_origin", "*", CFGF_NONE),
CFG_END() CFG_END()
}; };

View File

@ -134,6 +134,8 @@ static struct event *exitev;
static struct evhttp *evhttpd; static struct evhttp *evhttpd;
static pthread_t tid_httpd; static pthread_t tid_httpd;
static char *allow_origin;
#ifdef HAVE_LIBEVENT2_OLD #ifdef HAVE_LIBEVENT2_OLD
struct stream_ctx *g_st; struct stream_ctx *g_st;
#endif #endif
@ -765,7 +767,6 @@ httpd_send_reply(struct evhttp_request *req, int code, const char *reason, struc
struct evkeyvalq *output_headers; struct evkeyvalq *output_headers;
const char *param; const char *param;
int do_gzip; int do_gzip;
char *origin;
if (!req) if (!req)
return; return;
@ -779,9 +780,8 @@ httpd_send_reply(struct evhttp_request *req, int code, const char *reason, struc
(strstr(param, "gzip") || strstr(param, "*")) (strstr(param, "gzip") || strstr(param, "*"))
); );
origin = cfg_getstr(cfg_getsec(cfg, "general"), "allow_origin"); if (allow_origin)
if (origin && strlen(origin)) evhttp_add_header(output_headers, "Access-Control-Allow-Origin", allow_origin);
evhttp_add_header(output_headers, "Access-Control-Allow-Origin", origin);
if (do_gzip && (gzbuf = httpd_gzip_deflate(evbuf))) if (do_gzip && (gzbuf = httpd_gzip_deflate(evbuf)))
{ {
@ -1379,6 +1379,11 @@ httpd_init(void)
v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6"); v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6");
port = cfg_getint(cfg_getsec(cfg, "library"), "port"); port = cfg_getint(cfg_getsec(cfg, "library"), "port");
// For CORS headers
allow_origin = cfg_getstr(cfg_getsec(cfg, "general"), "allow_origin");
if (allow_origin && (strlen(allow_origin) == 0))
allow_origin = NULL;
if (v6enabled) if (v6enabled)
{ {
ret = evhttp_bind_socket(evhttpd, "::", port); ret = evhttp_bind_socket(evhttpd, "::", port);