1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

refine the cros of api for flv

This commit is contained in:
winlin 2016-12-15 14:10:02 +08:00
parent 3df8f11884
commit f30b3073a2
4 changed files with 83 additions and 30 deletions

View file

@ -1312,8 +1312,8 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m
: SrsConnection(cm, fd, cip) : SrsConnection(cm, fd, cip)
{ {
mux = m; mux = m;
cros = new SrsHttpCrosMux();
parser = new SrsHttpParser(); parser = new SrsHttpParser();
crossdomain_required = false;
_srs_config->subscribe(this); _srs_config->subscribe(this);
} }
@ -1321,6 +1321,7 @@ SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m
SrsHttpApi::~SrsHttpApi() SrsHttpApi::~SrsHttpApi()
{ {
srs_freep(parser); srs_freep(parser);
srs_freep(cros);
_srs_config->unsubscribe(this); _srs_config->unsubscribe(this);
} }
@ -1366,8 +1367,11 @@ int SrsHttpApi::do_cycle()
// @see https://github.com/ossrs/srs/issues/398 // @see https://github.com/ossrs/srs/issues/398
skt.set_recv_timeout(SRS_HTTP_RECV_TIMEOUT_US); skt.set_recv_timeout(SRS_HTTP_RECV_TIMEOUT_US);
// initialize the crossdomain // initialize the cros, which will proxy to mux.
crossdomain_enabled = _srs_config->get_http_api_crossdomain(); bool crossdomain_enabled = _srs_config->get_http_api_crossdomain();
if ((ret = cros->initialize(mux, crossdomain_enabled)) != ERROR_SUCCESS) {
return ret;
}
// process http messages. // process http messages.
while(!disposed) { 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(), r->method_str().c_str(), r->url().c_str(), r->content_length(),
hm->is_chunked(), hm->is_infinite_chunked()); 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. // 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)) { if (!srs_is_client_gracefully_close(ret)) {
srs_error("serve http msg failed. ret=%d", 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() 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; return ERROR_SUCCESS;
} }

View file

@ -211,9 +211,8 @@ class SrsHttpApi : virtual public SrsConnection, virtual public ISrsReloadHandle
{ {
private: private:
SrsHttpParser* parser; SrsHttpParser* parser;
SrsHttpCrosMux* cros;
SrsHttpServeMux* mux; SrsHttpServeMux* mux;
bool crossdomain_required;
bool crossdomain_enabled;
public: public:
SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, std::string cip); SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, std::string cip);
virtual ~SrsHttpApi(); virtual ~SrsHttpApi();

View file

@ -761,6 +761,54 @@ bool SrsHttpServeMux::path_match(string pattern, string path)
return false; 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() ISrsHttpMessage::ISrsHttpMessage()
{ {
_http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE];

View file

@ -442,6 +442,26 @@ private:
virtual bool path_match(std::string pattern, std::string path); 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. // for http header.
typedef std::pair<std::string, std::string> SrsHttpHeaderField; typedef std::pair<std::string, std::string> SrsHttpHeaderField;