winlin

refine code for api, add clients and parse_rest_id

@@ -112,6 +112,7 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -112,6 +112,7 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
112 << SRS_JFIELD_STR("requests", "the request itself, for http debug") << SRS_JFIELD_CONT 112 << SRS_JFIELD_STR("requests", "the request itself, for http debug") << SRS_JFIELD_CONT
113 << SRS_JFIELD_STR("vhosts", "dumps vhost to json") << SRS_JFIELD_CONT 113 << SRS_JFIELD_STR("vhosts", "dumps vhost to json") << SRS_JFIELD_CONT
114 << SRS_JFIELD_STR("streams", "dumps streams to json") << SRS_JFIELD_CONT 114 << SRS_JFIELD_STR("streams", "dumps streams to json") << SRS_JFIELD_CONT
  115 + << SRS_JFIELD_STR("clients", "dumps clients to json") << SRS_JFIELD_CONT
115 << SRS_JFIELD_ORG("test", SRS_JOBJECT_START) 116 << SRS_JFIELD_ORG("test", SRS_JOBJECT_START)
116 << SRS_JFIELD_STR("requests", "show the request info") << SRS_JFIELD_CONT 117 << SRS_JFIELD_STR("requests", "show the request info") << SRS_JFIELD_CONT
117 << SRS_JFIELD_STR("errors", "always return an error 100") << SRS_JFIELD_CONT 118 << SRS_JFIELD_STR("errors", "always return an error 100") << SRS_JFIELD_CONT
@@ -468,18 +469,13 @@ SrsGoApiStreams::~SrsGoApiStreams() @@ -468,18 +469,13 @@ SrsGoApiStreams::~SrsGoApiStreams()
468 int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) 469 int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
469 { 470 {
470 int ret = ERROR_SUCCESS; 471 int ret = ERROR_SUCCESS;
  472 +
471 SrsStatistic* stat = SrsStatistic::instance(); 473 SrsStatistic* stat = SrsStatistic::instance();
472 std::stringstream ss; 474 std::stringstream ss;
473 475
474 // path: {pattern}{stream_id} 476 // path: {pattern}{stream_id}
475 // e.g. /api/v1/streams/100 pattern= /api/v1/streams/, stream_id=100 477 // e.g. /api/v1/streams/100 pattern= /api/v1/streams/, stream_id=100
476 - int sid = -1;  
477 - if (true) {  
478 - string stream_id = r->path().substr((int)entry->pattern.length());  
479 - if (!stream_id.empty()) {  
480 - sid = ::atoi(stream_id.c_str());  
481 - }  
482 - } 478 + int sid = r->parse_rest_id(entry->pattern);
483 479
484 SrsStatisticStream* stream = NULL; 480 SrsStatisticStream* stream = NULL;
485 if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) { 481 if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) {
@@ -537,6 +533,44 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -537,6 +533,44 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
537 return ret; 533 return ret;
538 } 534 }
539 535
  536 +SrsGoApiClients::SrsGoApiClients()
  537 +{
  538 +}
  539 +
  540 +SrsGoApiClients::~SrsGoApiClients()
  541 +{
  542 +}
  543 +
  544 +int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
  545 +{
  546 + int ret = ERROR_SUCCESS;
  547 +
  548 + SrsStatistic* stat = SrsStatistic::instance();
  549 + std::stringstream ss;
  550 +
  551 + // path: {pattern}{client_id}
  552 + // e.g. /api/v1/clients/100 pattern= /api/v1/clients/, client_id=100
  553 + int cid = r->parse_rest_id(entry->pattern);
  554 +
  555 + SrsStatisticClient* client = NULL;
  556 + // TODO: FIXME: implements it.
  557 + /*if (cid >= 0 && (client = stat->find_client(cid)) == NULL) {
  558 + ret = ERROR_RTMP_STREAM_NOT_FOUND;
  559 + srs_error("stream client_id=%d not found. ret=%d", cid, ret);
  560 +
  561 + ss << SRS_JOBJECT_START << SRS_JFIELD_ERROR(ret) << SRS_JOBJECT_END;
  562 +
  563 + return srs_http_response_json(w, ss.str());
  564 +
  565 + }*/
  566 +
  567 + if (r->is_http_get()) {
  568 +
  569 + }
  570 +
  571 + return ret;
  572 +}
  573 +
540 SrsGoApiError::SrsGoApiError() 574 SrsGoApiError::SrsGoApiError()
541 { 575 {
542 } 576 }
@@ -159,6 +159,15 @@ public: @@ -159,6 +159,15 @@ public:
159 virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); 159 virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
160 }; 160 };
161 161
  162 +class SrsGoApiClients : public ISrsHttpHandler
  163 +{
  164 +public:
  165 + SrsGoApiClients();
  166 + virtual ~SrsGoApiClients();
  167 +public:
  168 + virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
  169 +};
  170 +
162 class SrsGoApiError : public ISrsHttpHandler 171 class SrsGoApiError : public ISrsHttpHandler
163 { 172 {
164 public: 173 public:
@@ -654,6 +654,7 @@ string SrsHttpMessage::uri() @@ -654,6 +654,7 @@ string SrsHttpMessage::uri()
654 654
655 uri += host(); 655 uri += host();
656 uri += path(); 656 uri += path();
  657 +
657 return uri; 658 return uri;
658 } 659 }
659 660
@@ -677,6 +678,21 @@ string SrsHttpMessage::ext() @@ -677,6 +678,21 @@ string SrsHttpMessage::ext()
677 return _ext; 678 return _ext;
678 } 679 }
679 680
  681 +int SrsHttpMessage::parse_rest_id(string pattern)
  682 +{
  683 + string p = _uri->get_path();
  684 + if (p.length() <= pattern.length()) {
  685 + return -1;
  686 + }
  687 +
  688 + string id = p.substr((int)pattern.length());
  689 + if (!id.empty()) {
  690 + return ::atoi(id.c_str());
  691 + }
  692 +
  693 + return -1;
  694 +}
  695 +
680 int SrsHttpMessage::body_read_all(string& body) 696 int SrsHttpMessage::body_read_all(string& body)
681 { 697 {
682 int ret = ERROR_SUCCESS; 698 int ret = ERROR_SUCCESS;
@@ -246,6 +246,10 @@ public: @@ -246,6 +246,10 @@ public:
246 virtual std::string host(); 246 virtual std::string host();
247 virtual std::string path(); 247 virtual std::string path();
248 virtual std::string ext(); 248 virtual std::string ext();
  249 + /**
  250 + * get the RESTful matched id.
  251 + */
  252 + virtual int parse_rest_id(std::string pattern);
249 public: 253 public:
250 /** 254 /**
251 * read body to string. 255 * read body to string.
@@ -773,10 +773,10 @@ int SrsServer::http_handle() @@ -773,10 +773,10 @@ int SrsServer::http_handle()
773 if ((ret = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != ERROR_SUCCESS) { 773 if ((ret = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != ERROR_SUCCESS) {
774 return ret; 774 return ret;
775 } 775 }
776 - if ((ret = http_api_mux->handle("/api", new SrsGoApiApi())) != ERROR_SUCCESS) { 776 + if ((ret = http_api_mux->handle("/api/", new SrsGoApiApi())) != ERROR_SUCCESS) {
777 return ret; 777 return ret;
778 } 778 }
779 - if ((ret = http_api_mux->handle("/api/v1", new SrsGoApiV1())) != ERROR_SUCCESS) { 779 + if ((ret = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != ERROR_SUCCESS) {
780 return ret; 780 return ret;
781 } 781 }
782 if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) { 782 if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) {
@@ -800,12 +800,15 @@ int SrsServer::http_handle() @@ -800,12 +800,15 @@ int SrsServer::http_handle()
800 if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) { 800 if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) {
801 return ret; 801 return ret;
802 } 802 }
803 - if ((ret = http_api_mux->handle("/api/v1/vhosts", new SrsGoApiVhosts())) != ERROR_SUCCESS) { 803 + if ((ret = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != ERROR_SUCCESS) {
804 return ret; 804 return ret;
805 } 805 }
806 if ((ret = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != ERROR_SUCCESS) { 806 if ((ret = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != ERROR_SUCCESS) {
807 return ret; 807 return ret;
808 } 808 }
  809 + if ((ret = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != ERROR_SUCCESS) {
  810 + return ret;
  811 + }
809 812
810 // test the request info. 813 // test the request info.
811 if ((ret = http_api_mux->handle("/api/v1/test/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) { 814 if ((ret = http_api_mux->handle("/api/v1/test/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) {
@@ -143,6 +143,14 @@ int srs_http_response_json(ISrsHttpResponseWriter* w, string data) @@ -143,6 +143,14 @@ int srs_http_response_json(ISrsHttpResponseWriter* w, string data)
143 return w->write((char*)data.data(), (int)data.length()); 143 return w->write((char*)data.data(), (int)data.length());
144 } 144 }
145 145
  146 +int srs_http_response_code(ISrsHttpResponseWriter* w, int code)
  147 +{
  148 + std::stringstream ss;
  149 + // TODO: FIXME: implements it.
  150 + //ss << SRS_JOBJECT_START << SRS_JFIELD_ERROR(code) << SRS_JOBJECT_END;
  151 + return srs_http_response_json(w, ss.str());
  152 +}
  153 +
146 SrsHttpHeader::SrsHttpHeader() 154 SrsHttpHeader::SrsHttpHeader()
147 { 155 {
148 } 156 }
@@ -78,6 +78,7 @@ class ISrsHttpResponseWriter; @@ -78,6 +78,7 @@ class ISrsHttpResponseWriter;
78 78
79 // helper function: response in json format. 79 // helper function: response in json format.
80 extern int srs_http_response_json(ISrsHttpResponseWriter* w, std::string data); 80 extern int srs_http_response_json(ISrsHttpResponseWriter* w, std::string data);
  81 +extern int srs_http_response_code(ISrsHttpResponseWriter* w, int code);
81 82
82 // get the status text of code. 83 // get the status text of code.
83 extern std::string srs_generate_http_status_text(int status); 84 extern std::string srs_generate_http_status_text(int status);
@@ -488,6 +489,14 @@ public: @@ -488,6 +489,14 @@ public:
488 virtual std::string host() = 0; 489 virtual std::string host() = 0;
489 virtual std::string path() = 0; 490 virtual std::string path() = 0;
490 virtual std::string ext() = 0; 491 virtual std::string ext() = 0;
  492 + /**
  493 + * get the RESTful id,
  494 + * for example, pattern is /api/v1/streams, path is /api/v1/streams/100,
  495 + * then the rest id is 100.
  496 + * @param pattern the handler pattern which will serve the request.
  497 + * @return the REST id; -1 if not matched.
  498 + */
  499 + virtual int parse_rest_id(std::string pattern) = 0;
491 public: 500 public:
492 /** 501 /**
493 * read body to string. 502 * read body to string.