From f30b3073a2bec6d6b2c37bd5f30203b0541bb496 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 15 Dec 2016 14:10:02 +0800 Subject: [PATCH] refine the cros of api for flv --- trunk/src/app/srs_app_http_api.cpp | 42 ++++++++--------------- trunk/src/app/srs_app_http_api.hpp | 3 +- trunk/src/protocol/srs_http_stack.cpp | 48 +++++++++++++++++++++++++++ trunk/src/protocol/srs_http_stack.hpp | 20 +++++++++++ 4 files changed, 83 insertions(+), 30 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 04be1ae4cf..8b5278b021 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -1312,8 +1312,8 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m : SrsConnection(cm, fd, cip) { mux = m; + cros = new SrsHttpCrosMux(); parser = new SrsHttpParser(); - crossdomain_required = false; _srs_config->subscribe(this); } @@ -1321,6 +1321,7 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m SrsHttpApi::~SrsHttpApi() { srs_freep(parser); + srs_freep(cros); _srs_config->unsubscribe(this); } @@ -1366,8 +1367,11 @@ int SrsHttpApi::do_cycle() // @see https://github.com/ossrs/srs/issues/398 skt.set_recv_timeout(SRS_HTTP_RECV_TIMEOUT_US); - // initialize the crossdomain - crossdomain_enabled = _srs_config->get_http_api_crossdomain(); + // initialize the cros, which will proxy to mux. + bool crossdomain_enabled = _srs_config->get_http_api_crossdomain(); + if ((ret = cros->initialize(mux, crossdomain_enabled)) != ERROR_SUCCESS) { + return ret; + } // process http messages. while(!disposed) { @@ -1420,31 +1424,8 @@ 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()); - // method is OPTIONS and enable crossdomain, required crossdomain header. - if (r->is_http_options() && _srs_config->get_http_api_crossdomain()) { - crossdomain_required = true; - } - - // whenever crossdomain required, set crossdomain header. - if (crossdomain_required) { - w->header()->set("Access-Control-Allow-Origin", "*"); - w->header()->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE"); - w->header()->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type"); - } - - // handle the http options. - if (r->is_http_options()) { - w->header()->set_content_length(0); - if (_srs_config->get_http_api_crossdomain()) { - w->write_header(SRS_CONSTS_HTTP_OK); - } else { - w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed); - } - return w->final_request(); - } - // use default server mux to serve http request. - if ((ret = mux->serve_http(w, r)) != ERROR_SUCCESS) { + if ((ret = cros->serve_http(w, r)) != ERROR_SUCCESS) { if (!srs_is_client_gracefully_close(ret)) { srs_error("serve http msg failed. ret=%d", ret); } @@ -1456,7 +1437,12 @@ int SrsHttpApi::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) int SrsHttpApi::on_reload_http_api_crossdomain() { - crossdomain_enabled = _srs_config->get_http_api_crossdomain(); + int ret = ERROR_SUCCESS; + + bool crossdomain_enabled = _srs_config->get_http_api_crossdomain(); + if ((ret = cros->initialize(mux, crossdomain_enabled)) != ERROR_SUCCESS) { + return ret; + } return ERROR_SUCCESS; } diff --git a/trunk/src/app/srs_app_http_api.hpp b/trunk/src/app/srs_app_http_api.hpp index b8cd63314d..0a4f717565 100644 --- a/trunk/src/app/srs_app_http_api.hpp +++ b/trunk/src/app/srs_app_http_api.hpp @@ -211,9 +211,8 @@ class SrsHttpApi : virtual public SrsConnection, virtual public ISrsReloadHandle { private: SrsHttpParser* parser; + SrsHttpCrosMux* cros; SrsHttpServeMux* mux; - bool crossdomain_required; - bool crossdomain_enabled; public: SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, std::string cip); virtual ~SrsHttpApi(); diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index 57668a7f91..035c17e151 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -761,6 +761,54 @@ bool SrsHttpServeMux::path_match(string pattern, string path) return false; } +SrsHttpCrosMux::SrsHttpCrosMux() +{ + next = NULL; + enabled = false; + required = false; +} + +SrsHttpCrosMux::~SrsHttpCrosMux() +{ +} + +int SrsHttpCrosMux::initialize(ISrsHttpServeMux* worker, bool cros_enabled) +{ + next = worker; + enabled = cros_enabled; + + return ERROR_SUCCESS; +} + +int SrsHttpCrosMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) +{ + // method is OPTIONS and enable crossdomain, required crossdomain header. + if (r->is_http_options() && enabled) { + required = true; + } + + // whenever crossdomain required, set crossdomain header. + if (required) { + w->header()->set("Access-Control-Allow-Origin", "*"); + w->header()->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE"); + w->header()->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type"); + } + + // handle the http options. + if (r->is_http_options()) { + w->header()->set_content_length(0); + if (enabled) { + w->write_header(SRS_CONSTS_HTTP_OK); + } else { + w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed); + } + return w->final_request(); + } + + srs_assert(next); + return next->serve_http(w, r); +} + ISrsHttpMessage::ISrsHttpMessage() { _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 63a26b254d..d3a1d980c9 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -442,6 +442,26 @@ class SrsHttpServeMux : public ISrsHttpServeMux virtual bool path_match(std::string pattern, std::string path); }; +/** + * The filter http mux, directly serve the http CROS requests, + * while proxy to the worker mux for services. + */ +class SrsHttpCrosMux : public ISrsHttpServeMux +{ +private: + bool required; + bool enabled; + ISrsHttpServeMux* next; +public: + SrsHttpCrosMux(); + virtual ~SrsHttpCrosMux(); +public: + virtual int initialize(ISrsHttpServeMux* worker, bool cros_enabled); +// interface ISrsHttpServeMux +public: + virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); +}; + // for http header. typedef std::pair SrsHttpHeaderField;