diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index fe61d168..44777de5 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -32,6 +32,15 @@ void HTTP::Parser::CleanPreserveHeaders() { vars.clear(); } +/// Sets the neccesary headers to allow Cross Origin Resource Sharing with all domains. +void HTTP::Parser::setCORSHeaders(){ + SetHeader("Access-Control-Allow-Origin", "*"); + SetHeader("Access-Control-Allow-Methods", "GET, POST"); + SetHeader("Access-Control-Allow-Headers", "Content-Type, X-Requested-With"); + SetHeader("Access-Control-Allow-Credentials", "true"); +} + + /// Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending. /// The request is build from internal variables set before this call is made. /// To be precise, method, url, protocol, headers and body are used. diff --git a/lib/http_parser.h b/lib/http_parser.h index f3f89e62..1e740547 100644 --- a/lib/http_parser.h +++ b/lib/http_parser.h @@ -21,6 +21,7 @@ namespace HTTP { std::string getUrl(); void SetHeader(std::string i, std::string v); void SetHeader(std::string i, long long v); + void setCORSHeaders(); void SetVar(std::string i, std::string v); void SetBody(std::string s); void SetBody(char * buffer, int len); diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index 2ea8fe6f..d9a883e8 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -558,10 +558,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ } H.Clean(); H.SetHeader("Content-Type", "text/javascript"); - H.SetHeader("Access-Control-Allow-Origin", "*"); - H.SetHeader("Access-Control-Allow-Methods", "GET, POST"); - H.SetHeader("Access-Control-Allow-Headers", "Content-Type, X-Requested-With"); - H.SetHeader("Access-Control-Allow-Credentials", "true"); + H.setCORSHeaders(); if (jsonp == ""){ H.SetBody(Response.toString() + "\n\n"); }else{ diff --git a/src/output/output_hls.cpp b/src/output/output_hls.cpp index 9ae25191..33ea94d9 100644 --- a/src/output/output_hls.cpp +++ b/src/output/output_hls.cpp @@ -168,6 +168,7 @@ namespace Mist { H.Clean(); H.SetHeader("Content-Type", "text/xml"); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver); + H.setCORSHeaders(); H.SetBody(""); H.SendResponse("200", "OK", myConn); H.Clean(); //clean for any possible next requests @@ -240,6 +241,7 @@ namespace Mist { lastVid = from * 90; H.SetHeader("Content-Type", "video/mp2t"); + H.setCORSHeaders(); H.StartResponse(H, myConn, VLCworkaround); unsigned int fragCounter = myMeta.tracks[vidTrack].missedFrags; @@ -266,6 +268,7 @@ namespace Mist { H.SetHeader("Content-Type", "audio/mpegurl"); } H.SetHeader("Cache-Control", "no-cache"); + H.setCORSHeaders(); std::string manifest; if (request.find("/") == std::string::npos){ manifest = liveIndex(); diff --git a/src/output/output_http_internal.cpp b/src/output/output_http_internal.cpp index 3632230d..b846390e 100644 --- a/src/output/output_http_internal.cpp +++ b/src/output/output_http_internal.cpp @@ -156,6 +156,7 @@ namespace Mist { H.Clean(); H.SetHeader("Content-Type", "text/xml"); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION); + H.setCORSHeaders(); H.SetBody(""); H.SendResponse("200", "OK", myConn); return; @@ -165,6 +166,7 @@ namespace Mist { H.Clean(); H.SetHeader("Content-Type", "text/xml"); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION); + H.setCORSHeaders(); H.SetBody(""); H.SendResponse("200", "OK", myConn); return; @@ -191,11 +193,12 @@ namespace Mist { return; } - // send logo icon + // send generic HTML page if (H.url.length() > 6 && H.url.substr(H.url.length() - 5, 5) == ".html"){ H.Clean(); H.SetHeader("Content-Type", "text/html"); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION); + H.setCORSHeaders(); H.SetBody("Stream "+streamName+""); H.SendResponse("200", "OK", myConn); return; @@ -247,6 +250,7 @@ namespace Mist { H.Clean(); H.SetHeader("Content-Type", "application/smil"); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver); + H.setCORSHeaders(); H.SetBody("\n \n \n \n \n \n"+trackSources+" \n \n"); H.SendResponse("200", "OK", myConn); return; @@ -261,6 +265,7 @@ namespace Mist { } H.Clean(); H.SetHeader("Server", "mistserver/" PACKAGE_VERSION); + H.setCORSHeaders(); if (rURL.substr(0, 6) != "/json_"){ H.SetHeader("Content-Type", "application/javascript"); }else{