diff --git a/README.md b/README.md index eee4057ef1..95b61fd511 100755 --- a/README.md +++ b/README.md @@ -379,6 +379,7 @@ Remark: ### History +* v3.0, 2016-12-15, fix #717, #691, http api/static/stream support cors. 3.0.9 * v3.0, 2016-12-08, support log rotate signal SIGUSR1. 3.0.8 * v3.0, 2016-12-07, fix typo and refine grammar. 3.0.7 * v3.0, 2015-10-23, fix [#467][bug #467], support write log to kafka. 3.0.6 diff --git a/trunk/.gitignore b/trunk/.gitignore index 4319156b78..164de0cd70 100644 --- a/trunk/.gitignore +++ b/trunk/.gitignore @@ -1,4 +1,4 @@ -console.conf +console*.conf doc/frozen.2Mbps.1644x1028.flv doc/frozen.500Kbps.766x480.flv doc/kungfupanda3-tlr1_h1080p.200kbps.flv diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 6504c55c48..cd77642d0d 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -178,6 +178,10 @@ http_server { # the default dir for http root. # default: ./objs/nginx/html dir ./objs/nginx/html; + # whether enable crossdomain request. + # for both http static and stream server and apply on all vhosts. + # default: on + crossdomain on; } ############################################################################################# diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index d82b205a52..7e7efb9d84 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1704,6 +1704,17 @@ int SrsConfig::reload_http_stream(SrsConfDirective* old_root) } srs_trace("reload enabled modified http_stream success."); + if (!srs_directive_equals(old_http_stream->get("crossdomain"), new_http_stream->get("crossdomain"))) { + for (it = subscribes.begin(); it != subscribes.end(); ++it) { + ISrsReloadHandler* subscribe = *it; + if ((ret = subscribe->on_reload_http_stream_crossdomain()) != ERROR_SUCCESS) { + srs_error("notify subscribes http_stream crossdomain modified failed. ret=%d", ret); + return ret; + } + } + } + srs_trace("reload crossdomain modified http_stream success."); + return ret; } @@ -3570,7 +3581,7 @@ int SrsConfig::check_config() SrsConfDirective* conf = root->get("http_server"); for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; - if (n != "enabled" && n != "listen" && n != "dir") { + if (n != "enabled" && n != "listen" && n != "dir" && n != "crossdomain") { ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("unsupported http_stream directive %s, ret=%d", n.c_str(), ret); return ret; @@ -6591,6 +6602,23 @@ string SrsConfig::get_http_stream_dir() return conf->arg0(); } +bool SrsConfig::get_http_stream_crossdomain() +{ + static bool DEFAULT = true; + + SrsConfDirective* conf = root->get("http_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("crossdomain"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + + return SRS_CONF_PERFER_TRUE(conf->arg0()); +} + bool SrsConfig::get_vhost_http_enabled(string vhost) { static bool DEFAULT = false; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 37c173dcca..ee0467b8bd 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -1324,6 +1324,10 @@ class SrsConfig * get the http stream root dir. */ virtual std::string get_http_stream_dir(); + /** + * whether enable crossdomain for http static and stream server. + */ + virtual bool get_http_stream_crossdomain(); public: /** * get whether vhost enabled http stream diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index e931c5f5e0..e55eae6474 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include class SrsConnection; @@ -58,7 +59,7 @@ class IConnectionManager * all connections accept from listener must extends from this base class, * server will add the connection to manager, and delete it when remove. */ -class SrsConnection : public virtual ISrsOneCycleThreadHandler, public virtual IKbpsDelta +class SrsConnection : virtual public ISrsOneCycleThreadHandler, virtual public IKbpsDelta, virtual public ISrsReloadHandler { private: /** diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 8cb5b3fbf1..bed765ce65 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -1424,7 +1424,7 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) r->method_str().c_str(), r->url().c_str(), r->content_length(), hm->is_chunked(), hm->is_infinite_chunked()); - // use default server mux to serve http request. + // use cors server mux to serve http request, which will proxy to mux. if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) { if (!srs_is_client_gracefully_close(ret)) { srs_error("serve http msg failed. ret=%d", ret); diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 862cce7e4a..9b14a762f4 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -1091,12 +1091,14 @@ SrsHttpConn::SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux : SrsConnection(cm, fd, cip) { parser = new SrsHttpParser(); + cors = new SrsHttpCorsMux(); http_mux = m; } SrsHttpConn::~SrsHttpConn() { srs_freep(parser); + srs_freep(cors); } void SrsHttpConn::resample() @@ -1139,6 +1141,12 @@ int SrsHttpConn::do_cycle() SrsRequest* last_req = NULL; SrsAutoFree(SrsRequest, last_req); + + // initialize the cors, which will proxy to mux. + bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain(); + if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) { + return ret; + } // process http messages. while (!disposed) { @@ -1193,8 +1201,8 @@ int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) srs_trace("HTTP %s %s, content-length=%"PRId64"", r->method_str().c_str(), r->url().c_str(), r->content_length()); - // use default server mux to serve http request. - if ((ret = http_mux->serve_http(w, r)) != ERROR_SUCCESS) { + // use cors server mux to serve http request, which will proxy to http_remux. + if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) { if (!srs_is_client_gracefully_close(ret)) { srs_error("serve http msg failed. ret=%d", ret); } @@ -1211,6 +1219,19 @@ int SrsHttpConn::on_disconnect(SrsRequest* req) return ret; } +int SrsHttpConn::on_reload_http_stream_crossdomain() +{ + int ret = ERROR_SUCCESS; + + // initialize the cors, which will proxy to mux. + bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain(); + if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) { + return ret; + } + + return ret; +} + SrsResponseOnlyHttpConn::SrsResponseOnlyHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, string cip) : SrsHttpConn(cm, fd, m, cip) { diff --git a/trunk/src/app/srs_app_http_conn.hpp b/trunk/src/app/srs_app_http_conn.hpp index 477b92b699..e54700922e 100644 --- a/trunk/src/app/srs_app_http_conn.hpp +++ b/trunk/src/app/srs_app_http_conn.hpp @@ -353,11 +353,15 @@ class SrsHttpParser static int on_body(http_parser* parser, const char* at, size_t length); }; +/** + * The http connection which request the static or stream content. + */ class SrsHttpConn : public SrsConnection { private: SrsHttpParser* parser; ISrsHttpServeMux* http_mux; + SrsHttpCorsMux* cors; public: SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, std::string cip); virtual ~SrsHttpConn(); @@ -382,6 +386,9 @@ class SrsHttpConn : public SrsConnection * @param request: request which is converted by the last http message. */ virtual int on_disconnect(SrsRequest* req); +// interface ISrsReloadHandler +public: + virtual int on_reload_http_stream_crossdomain(); }; /** diff --git a/trunk/src/app/srs_app_reload.cpp b/trunk/src/app/srs_app_reload.cpp index 465d6316bb..fb4da32623 100644 --- a/trunk/src/app/srs_app_reload.cpp +++ b/trunk/src/app/srs_app_reload.cpp @@ -110,6 +110,11 @@ int ISrsReloadHandler::on_reload_http_stream_updated() return ERROR_SUCCESS; } +int ISrsReloadHandler::on_reload_http_stream_crossdomain() +{ + return ERROR_SUCCESS; +} + int ISrsReloadHandler::on_reload_vhost_http_updated() { return ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_reload.hpp b/trunk/src/app/srs_app_reload.hpp index b38f12884f..e43aa3e951 100644 --- a/trunk/src/app/srs_app_reload.hpp +++ b/trunk/src/app/srs_app_reload.hpp @@ -59,6 +59,7 @@ class ISrsReloadHandler virtual int on_reload_http_stream_enabled(); virtual int on_reload_http_stream_disabled(); virtual int on_reload_http_stream_updated(); + virtual int on_reload_http_stream_crossdomain(); public: // TODO: FIXME: should rename to http_static virtual int on_reload_vhost_http_updated(); diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 99e13e0835..8856a46eff 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define VERSION_REVISION 8 +#define VERSION_REVISION 9 // generated by configure, only macros. #include