Skip to content

Commit

Permalink
refine code for api, add clients and parse_rest_id
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 21, 2015
1 parent ab46208 commit a7589b9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 10 deletions.
48 changes: 41 additions & 7 deletions trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
<< SRS_JFIELD_STR("requests", "the request itself, for http debug") << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("vhosts", "dumps vhost to json") << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("streams", "dumps streams to json") << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("clients", "dumps clients to json") << SRS_JFIELD_CONT
<< SRS_JFIELD_ORG("test", SRS_JOBJECT_START)
<< SRS_JFIELD_STR("requests", "show the request info") << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("errors", "always return an error 100") << SRS_JFIELD_CONT
Expand Down Expand Up @@ -468,18 +469,13 @@ SrsGoApiStreams::~SrsGoApiStreams()
int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
int ret = ERROR_SUCCESS;

SrsStatistic* stat = SrsStatistic::instance();
std::stringstream ss;

// path: {pattern}{stream_id}
// e.g. /api/v1/streams/100 pattern= /api/v1/streams/, stream_id=100
int sid = -1;
if (true) {
string stream_id = r->path().substr((int)entry->pattern.length());
if (!stream_id.empty()) {
sid = ::atoi(stream_id.c_str());
}
}
int sid = r->parse_rest_id(entry->pattern);

SrsStatisticStream* stream = NULL;
if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) {
Expand Down Expand Up @@ -537,6 +533,44 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
return ret;
}

SrsGoApiClients::SrsGoApiClients()
{
}

SrsGoApiClients::~SrsGoApiClients()
{
}

int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
int ret = ERROR_SUCCESS;

SrsStatistic* stat = SrsStatistic::instance();
std::stringstream ss;

// path: {pattern}{client_id}
// e.g. /api/v1/clients/100 pattern= /api/v1/clients/, client_id=100
int cid = r->parse_rest_id(entry->pattern);

SrsStatisticClient* client = NULL;
// TODO: FIXME: implements it.
/*if (cid >= 0 && (client = stat->find_client(cid)) == NULL) {
ret = ERROR_RTMP_STREAM_NOT_FOUND;
srs_error("stream client_id=%d not found. ret=%d", cid, ret);
ss << SRS_JOBJECT_START << SRS_JFIELD_ERROR(ret) << SRS_JOBJECT_END;
return srs_http_response_json(w, ss.str());
}*/

if (r->is_http_get()) {

}

return ret;
}

SrsGoApiError::SrsGoApiError()
{
}
Expand Down
9 changes: 9 additions & 0 deletions trunk/src/app/srs_app_http_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ class SrsGoApiStreams : public ISrsHttpHandler
virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
};

class SrsGoApiClients : public ISrsHttpHandler
{
public:
SrsGoApiClients();
virtual ~SrsGoApiClients();
public:
virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
};

class SrsGoApiError : public ISrsHttpHandler
{
public:
Expand Down
16 changes: 16 additions & 0 deletions trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ string SrsHttpMessage::uri()

uri += host();
uri += path();

return uri;
}

Expand All @@ -677,6 +678,21 @@ string SrsHttpMessage::ext()
return _ext;
}

int SrsHttpMessage::parse_rest_id(string pattern)
{
string p = _uri->get_path();
if (p.length() <= pattern.length()) {
return -1;
}

string id = p.substr((int)pattern.length());
if (!id.empty()) {
return ::atoi(id.c_str());
}

return -1;
}

int SrsHttpMessage::body_read_all(string& body)
{
int ret = ERROR_SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ class SrsHttpMessage : public ISrsHttpMessage
virtual std::string host();
virtual std::string path();
virtual std::string ext();
/**
* get the RESTful matched id.
*/
virtual int parse_rest_id(std::string pattern);
public:
/**
* read body to string.
Expand Down
9 changes: 6 additions & 3 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@ int SrsServer::http_handle()
if ((ret = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api", new SrsGoApiApi())) != ERROR_SUCCESS) {
if ((ret = http_api_mux->handle("/api/", new SrsGoApiApi())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api/v1", new SrsGoApiV1())) != ERROR_SUCCESS) {
if ((ret = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) {
Expand All @@ -800,12 +800,15 @@ int SrsServer::http_handle()
if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api/v1/vhosts", new SrsGoApiVhosts())) != ERROR_SUCCESS) {
if ((ret = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != ERROR_SUCCESS) {
return ret;
}

// test the request info.
if ((ret = http_api_mux->handle("/api/v1/test/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) {
Expand Down
8 changes: 8 additions & 0 deletions trunk/src/protocol/srs_http_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ int srs_http_response_json(ISrsHttpResponseWriter* w, string data)
return w->write((char*)data.data(), (int)data.length());
}

int srs_http_response_code(ISrsHttpResponseWriter* w, int code)
{
std::stringstream ss;
// TODO: FIXME: implements it.
//ss << SRS_JOBJECT_START << SRS_JFIELD_ERROR(code) << SRS_JOBJECT_END;
return srs_http_response_json(w, ss.str());
}

SrsHttpHeader::SrsHttpHeader()
{
}
Expand Down
9 changes: 9 additions & 0 deletions trunk/src/protocol/srs_http_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ISrsHttpResponseWriter;

// helper function: response in json format.
extern int srs_http_response_json(ISrsHttpResponseWriter* w, std::string data);
extern int srs_http_response_code(ISrsHttpResponseWriter* w, int code);

// get the status text of code.
extern std::string srs_generate_http_status_text(int status);
Expand Down Expand Up @@ -488,6 +489,14 @@ class ISrsHttpMessage
virtual std::string host() = 0;
virtual std::string path() = 0;
virtual std::string ext() = 0;
/**
* get the RESTful id,
* for example, pattern is /api/v1/streams, path is /api/v1/streams/100,
* then the rest id is 100.
* @param pattern the handler pattern which will serve the request.
* @return the REST id; -1 if not matched.
*/
virtual int parse_rest_id(std::string pattern) = 0;
public:
/**
* read body to string.
Expand Down

1 comment on commit a7589b9

@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.

for #442

Please sign in to comment.