winlin

merge from 2.0release

@@ -111,7 +111,13 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -111,7 +111,13 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
111 << SRS_JFIELD_STR("authors", "the primary authors and contributors") << SRS_JFIELD_CONT 111 << SRS_JFIELD_STR("authors", "the primary authors and contributors") << SRS_JFIELD_CONT
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") 114 + << SRS_JFIELD_STR("streams", "dumps streams to json") << SRS_JFIELD_CONT
  115 + << SRS_JFIELD_ORG("test", SRS_JOBJECT_START)
  116 + << 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("redirects", "always redirect to /api/v1/test/errors") << SRS_JFIELD_CONT
  119 + << SRS_JFIELD_STR(".vhost.", "http vhost for error.srs.com/api/v1/test/errors")
  120 + << SRS_JOBJECT_END
115 << SRS_JOBJECT_END 121 << SRS_JOBJECT_END
116 << SRS_JOBJECT_END; 122 << SRS_JOBJECT_END;
117 123
@@ -443,9 +449,9 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -443,9 +449,9 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
443 std::stringstream ss; 449 std::stringstream ss;
444 450
445 ss << SRS_JOBJECT_START 451 ss << SRS_JOBJECT_START
446 - << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT  
447 - << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT  
448 - << SRS_JFIELD_ORG("vhosts", data.str()) 452 + << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT
  453 + << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
  454 + << SRS_JFIELD_ORG("vhosts", data.str())
449 << SRS_JOBJECT_END; 455 << SRS_JOBJECT_END;
450 456
451 return srs_http_response_json(w, ss.str()); 457 return srs_http_response_json(w, ss.str());
@@ -521,6 +527,27 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -521,6 +527,27 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
521 } 527 }
522 } 528 }
523 529
  530 +SrsGoApiError::SrsGoApiError()
  531 +{
  532 +}
  533 +
  534 +SrsGoApiError::~SrsGoApiError()
  535 +{
  536 +}
  537 +
  538 +int SrsGoApiError::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
  539 +{
  540 + std::stringstream ss;
  541 +
  542 + ss << SRS_JOBJECT_START
  543 + << SRS_JFIELD_ERROR(100) << SRS_JFIELD_CONT
  544 + << SRS_JFIELD_STR("msg", "SRS demo error.") << SRS_JFIELD_CONT
  545 + << SRS_JFIELD_STR("path", r->path())
  546 + << SRS_JOBJECT_END;
  547 +
  548 + return srs_http_response_json(w, ss.str());
  549 +}
  550 +
524 551
525 SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m) 552 SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m)
526 : SrsConnection(cm, fd) 553 : SrsConnection(cm, fd)
@@ -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 SrsGoApiError : public ISrsHttpHandler
  163 +{
  164 +public:
  165 + SrsGoApiError();
  166 + virtual ~SrsGoApiError();
  167 +public:
  168 + virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
  169 +};
  170 +
162 class SrsHttpApi : public SrsConnection 171 class SrsHttpApi : public SrsConnection
163 { 172 {
164 private: 173 private:
@@ -102,10 +102,12 @@ int SrsHttpResponseWriter::write(char* data, int size) @@ -102,10 +102,12 @@ int SrsHttpResponseWriter::write(char* data, int size)
102 { 102 {
103 int ret = ERROR_SUCCESS; 103 int ret = ERROR_SUCCESS;
104 104
  105 + // write the header data in memory.
105 if (!header_wrote) { 106 if (!header_wrote) {
106 write_header(SRS_CONSTS_HTTP_OK); 107 write_header(SRS_CONSTS_HTTP_OK);
107 } 108 }
108 - 109 +
  110 + // whatever header is wrote, we should try to send header.
109 if ((ret = send_header(data, size)) != ERROR_SUCCESS) { 111 if ((ret = send_header(data, size)) != ERROR_SUCCESS) {
110 srs_error("http: send header failed. ret=%d", ret); 112 srs_error("http: send header failed. ret=%d", ret);
111 return ret; 113 return ret;
@@ -261,7 +263,7 @@ int SrsHttpResponseWriter::send_header(char* data, int size) @@ -261,7 +263,7 @@ int SrsHttpResponseWriter::send_header(char* data, int size)
261 263
262 // status_line 264 // status_line
263 ss << "HTTP/1.1 " << status << " " 265 ss << "HTTP/1.1 " << status << " "
264 - << srs_generate_http_status_text(status) << SRS_HTTP_CRLF; 266 + << srs_generate_http_status_text(status) << SRS_HTTP_CRLF;
265 267
266 // detect content type 268 // detect content type
267 if (srs_go_http_body_allowd(status)) { 269 if (srs_go_http_body_allowd(status)) {
@@ -770,7 +770,7 @@ int SrsServer::http_handle() @@ -770,7 +770,7 @@ int SrsServer::http_handle()
770 770
771 #ifdef SRS_AUTO_HTTP_API 771 #ifdef SRS_AUTO_HTTP_API
772 srs_assert(http_api_mux); 772 srs_assert(http_api_mux);
773 - if ((ret = http_api_mux->handle("/", new SrsGoApiRoot())) != 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) {
@@ -800,15 +800,38 @@ int SrsServer::http_handle() @@ -800,15 +800,38 @@ 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/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) {  
804 - return ret;  
805 - }  
806 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) {
807 return ret; 804 return ret;
808 } 805 }
809 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) {
810 return ret; 807 return ret;
811 } 808 }
  809 +
  810 + // test the request info.
  811 + if ((ret = http_api_mux->handle("/api/v1/test/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) {
  812 + return ret;
  813 + }
  814 + // test the error code response.
  815 + if ((ret = http_api_mux->handle("/api/v1/test/errors", new SrsGoApiError())) != ERROR_SUCCESS) {
  816 + return ret;
  817 + }
  818 + // test the redirect mechenism.
  819 + if ((ret = http_api_mux->handle("/api/v1/test/redirects", new SrsHttpRedirectHandler("/api/v1/test/errors", SRS_CONSTS_HTTP_MovedPermanently))) != ERROR_SUCCESS) {
  820 + return ret;
  821 + }
  822 + // test the http vhost.
  823 + if ((ret = http_api_mux->handle("error.srs.com/api/v1/test/errors", new SrsGoApiError())) != ERROR_SUCCESS) {
  824 + return ret;
  825 + }
  826 +
  827 + // TODO: FIXME: for console.
  828 + // TODO: FIXME: support reload.
  829 + std::string dir = _srs_config->get_http_stream_dir() + "/srs-console";
  830 + if ((ret = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != ERROR_SUCCESS) {
  831 + srs_error("http: mount console dir=%s failed. ret=%d", dir.c_str(), ret);
  832 + return ret;
  833 + }
  834 + srs_trace("http: console mount to %s", dir.c_str());
812 #endif 835 #endif
813 836
814 return ret; 837 return ret;
@@ -1490,7 +1490,7 @@ int SrsSource::on_audio(SrsCommonMessage* shared_audio) @@ -1490,7 +1490,7 @@ int SrsSource::on_audio(SrsCommonMessage* shared_audio)
1490 if (!mix_correct && is_monotonically_increase) { 1490 if (!mix_correct && is_monotonically_increase) {
1491 if (last_packet_time > 0 && shared_audio->header.timestamp < last_packet_time) { 1491 if (last_packet_time > 0 && shared_audio->header.timestamp < last_packet_time) {
1492 is_monotonically_increase = false; 1492 is_monotonically_increase = false;
1493 - srs_warn("AUTIO: stream not monotonically increase, please open mix_correct."); 1493 + srs_warn("AUDIO: stream not monotonically increase, please open mix_correct.");
1494 } 1494 }
1495 } 1495 }
1496 last_packet_time = shared_audio->header.timestamp; 1496 last_packet_time = shared_audio->header.timestamp;