winlin

refine the cros of api for flv

@@ -1312,8 +1312,8 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m @@ -1312,8 +1312,8 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m
1312 : SrsConnection(cm, fd, cip) 1312 : SrsConnection(cm, fd, cip)
1313 { 1313 {
1314 mux = m; 1314 mux = m;
  1315 + cros = new SrsHttpCrosMux();
1315 parser = new SrsHttpParser(); 1316 parser = new SrsHttpParser();
1316 - crossdomain_required = false;  
1317 1317
1318 _srs_config->subscribe(this); 1318 _srs_config->subscribe(this);
1319 } 1319 }
@@ -1321,6 +1321,7 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m @@ -1321,6 +1321,7 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m
1321 SrsHttpApi::~SrsHttpApi() 1321 SrsHttpApi::~SrsHttpApi()
1322 { 1322 {
1323 srs_freep(parser); 1323 srs_freep(parser);
  1324 + srs_freep(cros);
1324 1325
1325 _srs_config->unsubscribe(this); 1326 _srs_config->unsubscribe(this);
1326 } 1327 }
@@ -1366,8 +1367,11 @@ int SrsHttpApi::do_cycle() @@ -1366,8 +1367,11 @@ int SrsHttpApi::do_cycle()
1366 // @see https://github.com/ossrs/srs/issues/398 1367 // @see https://github.com/ossrs/srs/issues/398
1367 skt.set_recv_timeout(SRS_HTTP_RECV_TIMEOUT_US); 1368 skt.set_recv_timeout(SRS_HTTP_RECV_TIMEOUT_US);
1368 1369
1369 - // initialize the crossdomain  
1370 - crossdomain_enabled = _srs_config->get_http_api_crossdomain(); 1370 + // initialize the cros, which will proxy to mux.
  1371 + bool crossdomain_enabled = _srs_config->get_http_api_crossdomain();
  1372 + if ((ret = cros->initialize(mux, crossdomain_enabled)) != ERROR_SUCCESS) {
  1373 + return ret;
  1374 + }
1371 1375
1372 // process http messages. 1376 // process http messages.
1373 while(!disposed) { 1377 while(!disposed) {
@@ -1420,31 +1424,8 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1420,31 +1424,8 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1420 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(),
1421 hm->is_chunked(), hm->is_infinite_chunked()); 1425 hm->is_chunked(), hm->is_infinite_chunked());
1422 1426
1423 - // method is OPTIONS and enable crossdomain, required crossdomain header.  
1424 - if (r->is_http_options() && _srs_config->get_http_api_crossdomain()) {  
1425 - crossdomain_required = true;  
1426 - }  
1427 -  
1428 - // whenever crossdomain required, set crossdomain header.  
1429 - if (crossdomain_required) {  
1430 - w->header()->set("Access-Control-Allow-Origin", "*");  
1431 - w->header()->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE");  
1432 - w->header()->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type");  
1433 - }  
1434 -  
1435 - // handle the http options.  
1436 - if (r->is_http_options()) {  
1437 - w->header()->set_content_length(0);  
1438 - if (_srs_config->get_http_api_crossdomain()) {  
1439 - w->write_header(SRS_CONSTS_HTTP_OK);  
1440 - } else {  
1441 - w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed);  
1442 - }  
1443 - return w->final_request();  
1444 - }  
1445 -  
1446 // use default server mux to serve http request. 1427 // use default server mux to serve http request.
1447 - if ((ret = mux->serve_http(w, r)) != ERROR_SUCCESS) { 1428 + if ((ret = cros->serve_http(w, r)) != ERROR_SUCCESS) {
1448 if (!srs_is_client_gracefully_close(ret)) { 1429 if (!srs_is_client_gracefully_close(ret)) {
1449 srs_error("serve http msg failed. ret=%d", ret); 1430 srs_error("serve http msg failed. ret=%d", ret);
1450 } 1431 }
@@ -1456,7 +1437,12 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1456,7 +1437,12 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1456 1437
1457 int SrsHttpApi::on_reload_http_api_crossdomain() 1438 int SrsHttpApi::on_reload_http_api_crossdomain()
1458 { 1439 {
1459 - crossdomain_enabled = _srs_config->get_http_api_crossdomain(); 1440 + int ret = ERROR_SUCCESS;
  1441 +
  1442 + bool crossdomain_enabled = _srs_config->get_http_api_crossdomain();
  1443 + if ((ret = cros->initialize(mux, crossdomain_enabled)) != ERROR_SUCCESS) {
  1444 + return ret;
  1445 + }
1460 1446
1461 return ERROR_SUCCESS; 1447 return ERROR_SUCCESS;
1462 } 1448 }
@@ -211,9 +211,8 @@ class SrsHttpApi : virtual public SrsConnection, virtual public ISrsReloadHandle @@ -211,9 +211,8 @@ class SrsHttpApi : virtual public SrsConnection, virtual public ISrsReloadHandle
211 { 211 {
212 private: 212 private:
213 SrsHttpParser* parser; 213 SrsHttpParser* parser;
  214 + SrsHttpCrosMux* cros;
214 SrsHttpServeMux* mux; 215 SrsHttpServeMux* mux;
215 - bool crossdomain_required;  
216 - bool crossdomain_enabled;  
217 public: 216 public:
218 SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, std::string cip); 217 SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, std::string cip);
219 virtual ~SrsHttpApi(); 218 virtual ~SrsHttpApi();
@@ -761,6 +761,54 @@ bool SrsHttpServeMux::path_match(string pattern, string path) @@ -761,6 +761,54 @@ bool SrsHttpServeMux::path_match(string pattern, string path)
761 return false; 761 return false;
762 } 762 }
763 763
  764 +SrsHttpCrosMux::SrsHttpCrosMux()
  765 +{
  766 + next = NULL;
  767 + enabled = false;
  768 + required = false;
  769 +}
  770 +
  771 +SrsHttpCrosMux::~SrsHttpCrosMux()
  772 +{
  773 +}
  774 +
  775 +int SrsHttpCrosMux::initialize(ISrsHttpServeMux* worker, bool cros_enabled)
  776 +{
  777 + next = worker;
  778 + enabled = cros_enabled;
  779 +
  780 + return ERROR_SUCCESS;
  781 +}
  782 +
  783 +int SrsHttpCrosMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
  784 +{
  785 + // method is OPTIONS and enable crossdomain, required crossdomain header.
  786 + if (r->is_http_options() && enabled) {
  787 + required = true;
  788 + }
  789 +
  790 + // whenever crossdomain required, set crossdomain header.
  791 + if (required) {
  792 + w->header()->set("Access-Control-Allow-Origin", "*");
  793 + w->header()->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE");
  794 + w->header()->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type");
  795 + }
  796 +
  797 + // handle the http options.
  798 + if (r->is_http_options()) {
  799 + w->header()->set_content_length(0);
  800 + if (enabled) {
  801 + w->write_header(SRS_CONSTS_HTTP_OK);
  802 + } else {
  803 + w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed);
  804 + }
  805 + return w->final_request();
  806 + }
  807 +
  808 + srs_assert(next);
  809 + return next->serve_http(w, r);
  810 +}
  811 +
764 ISrsHttpMessage::ISrsHttpMessage() 812 ISrsHttpMessage::ISrsHttpMessage()
765 { 813 {
766 _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; 814 _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE];
@@ -442,6 +442,26 @@ private: @@ -442,6 +442,26 @@ private:
442 virtual bool path_match(std::string pattern, std::string path); 442 virtual bool path_match(std::string pattern, std::string path);
443 }; 443 };
444 444
  445 +/**
  446 + * The filter http mux, directly serve the http CROS requests,
  447 + * while proxy to the worker mux for services.
  448 + */
  449 +class SrsHttpCrosMux : public ISrsHttpServeMux
  450 +{
  451 +private:
  452 + bool required;
  453 + bool enabled;
  454 + ISrsHttpServeMux* next;
  455 +public:
  456 + SrsHttpCrosMux();
  457 + virtual ~SrsHttpCrosMux();
  458 +public:
  459 + virtual int initialize(ISrsHttpServeMux* worker, bool cros_enabled);
  460 +// interface ISrsHttpServeMux
  461 +public:
  462 + virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
  463 +};
  464 +
445 // for http header. 465 // for http header.
446 typedef std::pair<std::string, std::string> SrsHttpHeaderField; 466 typedef std::pair<std::string, std::string> SrsHttpHeaderField;
447 467