Skip to content

Commit

Permalink
refine the cros of api for flv
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Dec 15, 2016
1 parent 3df8f11 commit f30b307
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 30 deletions.
42 changes: 14 additions & 28 deletions trunk/src/app/srs_app_http_api.cpp
Expand Up @@ -1312,15 +1312,16 @@ 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);
}

SrsHttpApi::~SrsHttpApi()
{
srs_freep(parser);
srs_freep(cros);

_srs_config->unsubscribe(this);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions trunk/src/app/srs_app_http_api.hpp
Expand Up @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions trunk/src/protocol/srs_http_stack.cpp
Expand Up @@ -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];
Expand Down
20 changes: 20 additions & 0 deletions trunk/src/protocol/srs_http_stack.hpp
Expand Up @@ -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<std::string, std::string> SrsHttpHeaderField;

Expand Down

1 comment on commit f30b307

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

实现 #691 #717 的CROS功能。

Please sign in to comment.