winlin

fix #717, #691, http api/static/stream support cors. 3.0.9

@@ -379,6 +379,7 @@ Remark: @@ -379,6 +379,7 @@ Remark:
379 379
380 ### History 380 ### History
381 381
  382 +* v3.0, 2016-12-15, fix #717, #691, http api/static/stream support cors. 3.0.9
382 * v3.0, 2016-12-08, support log rotate signal SIGUSR1. 3.0.8 383 * v3.0, 2016-12-08, support log rotate signal SIGUSR1. 3.0.8
383 * v3.0, 2016-12-07, fix typo and refine grammar. 3.0.7 384 * v3.0, 2016-12-07, fix typo and refine grammar. 3.0.7
384 * v3.0, 2015-10-23, fix [#467][bug #467], support write log to kafka. 3.0.6 385 * v3.0, 2015-10-23, fix [#467][bug #467], support write log to kafka. 3.0.6
1 -console.conf 1 +console*.conf
2 doc/frozen.2Mbps.1644x1028.flv 2 doc/frozen.2Mbps.1644x1028.flv
3 doc/frozen.500Kbps.766x480.flv 3 doc/frozen.500Kbps.766x480.flv
4 doc/kungfupanda3-tlr1_h1080p.200kbps.flv 4 doc/kungfupanda3-tlr1_h1080p.200kbps.flv
@@ -178,6 +178,10 @@ http_server { @@ -178,6 +178,10 @@ http_server {
178 # the default dir for http root. 178 # the default dir for http root.
179 # default: ./objs/nginx/html 179 # default: ./objs/nginx/html
180 dir ./objs/nginx/html; 180 dir ./objs/nginx/html;
  181 + # whether enable crossdomain request.
  182 + # for both http static and stream server and apply on all vhosts.
  183 + # default: on
  184 + crossdomain on;
181 } 185 }
182 186
183 ############################################################################################# 187 #############################################################################################
@@ -1704,6 +1704,17 @@ int SrsConfig::reload_http_stream(SrsConfDirective* old_root) @@ -1704,6 +1704,17 @@ int SrsConfig::reload_http_stream(SrsConfDirective* old_root)
1704 } 1704 }
1705 srs_trace("reload enabled modified http_stream success."); 1705 srs_trace("reload enabled modified http_stream success.");
1706 1706
  1707 + if (!srs_directive_equals(old_http_stream->get("crossdomain"), new_http_stream->get("crossdomain"))) {
  1708 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  1709 + ISrsReloadHandler* subscribe = *it;
  1710 + if ((ret = subscribe->on_reload_http_stream_crossdomain()) != ERROR_SUCCESS) {
  1711 + srs_error("notify subscribes http_stream crossdomain modified failed. ret=%d", ret);
  1712 + return ret;
  1713 + }
  1714 + }
  1715 + }
  1716 + srs_trace("reload crossdomain modified http_stream success.");
  1717 +
1707 return ret; 1718 return ret;
1708 } 1719 }
1709 1720
@@ -3570,7 +3581,7 @@ int SrsConfig::check_config() @@ -3570,7 +3581,7 @@ int SrsConfig::check_config()
3570 SrsConfDirective* conf = root->get("http_server"); 3581 SrsConfDirective* conf = root->get("http_server");
3571 for (int i = 0; conf && i < (int)conf->directives.size(); i++) { 3582 for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
3572 string n = conf->at(i)->name; 3583 string n = conf->at(i)->name;
3573 - if (n != "enabled" && n != "listen" && n != "dir") { 3584 + if (n != "enabled" && n != "listen" && n != "dir" && n != "crossdomain") {
3574 ret = ERROR_SYSTEM_CONFIG_INVALID; 3585 ret = ERROR_SYSTEM_CONFIG_INVALID;
3575 srs_error("unsupported http_stream directive %s, ret=%d", n.c_str(), ret); 3586 srs_error("unsupported http_stream directive %s, ret=%d", n.c_str(), ret);
3576 return ret; 3587 return ret;
@@ -6591,6 +6602,23 @@ string SrsConfig::get_http_stream_dir() @@ -6591,6 +6602,23 @@ string SrsConfig::get_http_stream_dir()
6591 return conf->arg0(); 6602 return conf->arg0();
6592 } 6603 }
6593 6604
  6605 +bool SrsConfig::get_http_stream_crossdomain()
  6606 +{
  6607 + static bool DEFAULT = true;
  6608 +
  6609 + SrsConfDirective* conf = root->get("http_server");
  6610 + if (!conf) {
  6611 + return DEFAULT;
  6612 + }
  6613 +
  6614 + conf = conf->get("crossdomain");
  6615 + if (!conf || conf->arg0().empty()) {
  6616 + return DEFAULT;
  6617 + }
  6618 +
  6619 + return SRS_CONF_PERFER_TRUE(conf->arg0());
  6620 +}
  6621 +
6594 bool SrsConfig::get_vhost_http_enabled(string vhost) 6622 bool SrsConfig::get_vhost_http_enabled(string vhost)
6595 { 6623 {
6596 static bool DEFAULT = false; 6624 static bool DEFAULT = false;
@@ -1324,6 +1324,10 @@ public: @@ -1324,6 +1324,10 @@ public:
1324 * get the http stream root dir. 1324 * get the http stream root dir.
1325 */ 1325 */
1326 virtual std::string get_http_stream_dir(); 1326 virtual std::string get_http_stream_dir();
  1327 + /**
  1328 + * whether enable crossdomain for http static and stream server.
  1329 + */
  1330 + virtual bool get_http_stream_crossdomain();
1327 public: 1331 public:
1328 /** 1332 /**
1329 * get whether vhost enabled http stream 1333 * get whether vhost enabled http stream
@@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include <srs_app_st.hpp> 35 #include <srs_app_st.hpp>
36 #include <srs_app_thread.hpp> 36 #include <srs_app_thread.hpp>
37 #include <srs_protocol_kbps.hpp> 37 #include <srs_protocol_kbps.hpp>
  38 +#include <srs_app_reload.hpp>
38 39
39 class SrsConnection; 40 class SrsConnection;
40 41
@@ -58,7 +59,7 @@ public: @@ -58,7 +59,7 @@ public:
58 * all connections accept from listener must extends from this base class, 59 * all connections accept from listener must extends from this base class,
59 * server will add the connection to manager, and delete it when remove. 60 * server will add the connection to manager, and delete it when remove.
60 */ 61 */
61 -class SrsConnection : public virtual ISrsOneCycleThreadHandler, public virtual IKbpsDelta 62 +class SrsConnection : virtual public ISrsOneCycleThreadHandler, virtual public IKbpsDelta, virtual public ISrsReloadHandler
62 { 63 {
63 private: 64 private:
64 /** 65 /**
@@ -1424,7 +1424,7 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1424,7 +1424,7 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1424 r->method_str().c_str(), r->url().c_str(), r->content_length(), 1424 r->method_str().c_str(), r->url().c_str(), r->content_length(),
1425 hm->is_chunked(), hm->is_infinite_chunked()); 1425 hm->is_chunked(), hm->is_infinite_chunked());
1426 1426
1427 - // use default server mux to serve http request. 1427 + // use cors server mux to serve http request, which will proxy to mux.
1428 if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) { 1428 if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) {
1429 if (!srs_is_client_gracefully_close(ret)) { 1429 if (!srs_is_client_gracefully_close(ret)) {
1430 srs_error("serve http msg failed. ret=%d", ret); 1430 srs_error("serve http msg failed. ret=%d", ret);
@@ -1091,12 +1091,14 @@ SrsHttpConn::SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux @@ -1091,12 +1091,14 @@ SrsHttpConn::SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux
1091 : SrsConnection(cm, fd, cip) 1091 : SrsConnection(cm, fd, cip)
1092 { 1092 {
1093 parser = new SrsHttpParser(); 1093 parser = new SrsHttpParser();
  1094 + cors = new SrsHttpCorsMux();
1094 http_mux = m; 1095 http_mux = m;
1095 } 1096 }
1096 1097
1097 SrsHttpConn::~SrsHttpConn() 1098 SrsHttpConn::~SrsHttpConn()
1098 { 1099 {
1099 srs_freep(parser); 1100 srs_freep(parser);
  1101 + srs_freep(cors);
1100 } 1102 }
1101 1103
1102 void SrsHttpConn::resample() 1104 void SrsHttpConn::resample()
@@ -1139,6 +1141,12 @@ int SrsHttpConn::do_cycle() @@ -1139,6 +1141,12 @@ int SrsHttpConn::do_cycle()
1139 1141
1140 SrsRequest* last_req = NULL; 1142 SrsRequest* last_req = NULL;
1141 SrsAutoFree(SrsRequest, last_req); 1143 SrsAutoFree(SrsRequest, last_req);
  1144 +
  1145 + // initialize the cors, which will proxy to mux.
  1146 + bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain();
  1147 + if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) {
  1148 + return ret;
  1149 + }
1142 1150
1143 // process http messages. 1151 // process http messages.
1144 while (!disposed) { 1152 while (!disposed) {
@@ -1193,8 +1201,8 @@ int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1193,8 +1201,8 @@ int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1193 srs_trace("HTTP %s %s, content-length=%"PRId64"", 1201 srs_trace("HTTP %s %s, content-length=%"PRId64"",
1194 r->method_str().c_str(), r->url().c_str(), r->content_length()); 1202 r->method_str().c_str(), r->url().c_str(), r->content_length());
1195 1203
1196 - // use default server mux to serve http request.  
1197 - if ((ret = http_mux->serve_http(w, r)) != ERROR_SUCCESS) { 1204 + // use cors server mux to serve http request, which will proxy to http_remux.
  1205 + if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) {
1198 if (!srs_is_client_gracefully_close(ret)) { 1206 if (!srs_is_client_gracefully_close(ret)) {
1199 srs_error("serve http msg failed. ret=%d", ret); 1207 srs_error("serve http msg failed. ret=%d", ret);
1200 } 1208 }
@@ -1211,6 +1219,19 @@ int SrsHttpConn::on_disconnect(SrsRequest* req) @@ -1211,6 +1219,19 @@ int SrsHttpConn::on_disconnect(SrsRequest* req)
1211 return ret; 1219 return ret;
1212 } 1220 }
1213 1221
  1222 +int SrsHttpConn::on_reload_http_stream_crossdomain()
  1223 +{
  1224 + int ret = ERROR_SUCCESS;
  1225 +
  1226 + // initialize the cors, which will proxy to mux.
  1227 + bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain();
  1228 + if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) {
  1229 + return ret;
  1230 + }
  1231 +
  1232 + return ret;
  1233 +}
  1234 +
1214 SrsResponseOnlyHttpConn::SrsResponseOnlyHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, string cip) 1235 SrsResponseOnlyHttpConn::SrsResponseOnlyHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, string cip)
1215 : SrsHttpConn(cm, fd, m, cip) 1236 : SrsHttpConn(cm, fd, m, cip)
1216 { 1237 {
@@ -353,11 +353,15 @@ private: @@ -353,11 +353,15 @@ private:
353 static int on_body(http_parser* parser, const char* at, size_t length); 353 static int on_body(http_parser* parser, const char* at, size_t length);
354 }; 354 };
355 355
  356 +/**
  357 + * The http connection which request the static or stream content.
  358 + */
356 class SrsHttpConn : public SrsConnection 359 class SrsHttpConn : public SrsConnection
357 { 360 {
358 private: 361 private:
359 SrsHttpParser* parser; 362 SrsHttpParser* parser;
360 ISrsHttpServeMux* http_mux; 363 ISrsHttpServeMux* http_mux;
  364 + SrsHttpCorsMux* cors;
361 public: 365 public:
362 SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, std::string cip); 366 SrsHttpConn(IConnectionManager* cm, st_netfd_t fd, ISrsHttpServeMux* m, std::string cip);
363 virtual ~SrsHttpConn(); 367 virtual ~SrsHttpConn();
@@ -382,6 +386,9 @@ private: @@ -382,6 +386,9 @@ private:
382 * @param request: request which is converted by the last http message. 386 * @param request: request which is converted by the last http message.
383 */ 387 */
384 virtual int on_disconnect(SrsRequest* req); 388 virtual int on_disconnect(SrsRequest* req);
  389 +// interface ISrsReloadHandler
  390 +public:
  391 + virtual int on_reload_http_stream_crossdomain();
385 }; 392 };
386 393
387 /** 394 /**
@@ -110,6 +110,11 @@ int ISrsReloadHandler::on_reload_http_stream_updated() @@ -110,6 +110,11 @@ int ISrsReloadHandler::on_reload_http_stream_updated()
110 return ERROR_SUCCESS; 110 return ERROR_SUCCESS;
111 } 111 }
112 112
  113 +int ISrsReloadHandler::on_reload_http_stream_crossdomain()
  114 +{
  115 + return ERROR_SUCCESS;
  116 +}
  117 +
113 int ISrsReloadHandler::on_reload_vhost_http_updated() 118 int ISrsReloadHandler::on_reload_vhost_http_updated()
114 { 119 {
115 return ERROR_SUCCESS; 120 return ERROR_SUCCESS;
@@ -59,6 +59,7 @@ public: @@ -59,6 +59,7 @@ public:
59 virtual int on_reload_http_stream_enabled(); 59 virtual int on_reload_http_stream_enabled();
60 virtual int on_reload_http_stream_disabled(); 60 virtual int on_reload_http_stream_disabled();
61 virtual int on_reload_http_stream_updated(); 61 virtual int on_reload_http_stream_updated();
  62 + virtual int on_reload_http_stream_crossdomain();
62 public: 63 public:
63 // TODO: FIXME: should rename to http_static 64 // TODO: FIXME: should rename to http_static
64 virtual int on_reload_vhost_http_updated(); 65 virtual int on_reload_vhost_http_updated();
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR 3 32 #define VERSION_MAJOR 3
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 8 34 +#define VERSION_REVISION 9
35 35
36 // generated by configure, only macros. 36 // generated by configure, only macros.
37 #include <srs_auto_headers.hpp> 37 #include <srs_auto_headers.hpp>