winlin

fix the http error header.

@@ -109,7 +109,8 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -109,7 +109,8 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
109 << SRS_JFIELD_STR("authors", "the primary authors and contributors") << SRS_JFIELD_CONT 109 << SRS_JFIELD_STR("authors", "the primary authors and contributors") << SRS_JFIELD_CONT
110 << SRS_JFIELD_STR("requests", "the request itself, for http debug") << SRS_JFIELD_CONT 110 << SRS_JFIELD_STR("requests", "the request itself, for http debug") << SRS_JFIELD_CONT
111 << SRS_JFIELD_STR("vhosts", "dumps vhost to json") << SRS_JFIELD_CONT 111 << SRS_JFIELD_STR("vhosts", "dumps vhost to json") << SRS_JFIELD_CONT
112 - << SRS_JFIELD_STR("streams", "dumps streams to json") 112 + << SRS_JFIELD_STR("streams", "dumps streams to json") << SRS_JFIELD_CONT
  113 + << SRS_JFIELD_STR("errors", "always return an error 100.")
113 << SRS_JOBJECT_END 114 << SRS_JOBJECT_END
114 << SRS_JOBJECT_END; 115 << SRS_JOBJECT_END;
115 116
@@ -474,6 +475,26 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -474,6 +475,26 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
474 return srs_http_response_json(w, ss.str()); 475 return srs_http_response_json(w, ss.str());
475 } 476 }
476 477
  478 +SrsGoApiError::SrsGoApiError()
  479 +{
  480 +}
  481 +
  482 +SrsGoApiError::~SrsGoApiError()
  483 +{
  484 +}
  485 +
  486 +int SrsGoApiError::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
  487 +{
  488 + std::stringstream ss;
  489 +
  490 + ss << SRS_JOBJECT_START
  491 + << SRS_JFIELD_ERROR(100) << SRS_JFIELD_CONT
  492 + << SRS_JFIELD_STR("msg", "SRS demo error.")
  493 + << SRS_JOBJECT_END;
  494 +
  495 + return srs_http_response_json(w, ss.str());
  496 +}
  497 +
477 SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m) 498 SrsHttpApi::SrsHttpApi(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m)
478 : SrsConnection(cm, fd) 499 : SrsConnection(cm, fd)
479 { 500 {
@@ -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,13 +102,15 @@ int SrsHttpResponseWriter::write(char* data, int size) @@ -102,13 +102,15 @@ 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 - if ((ret = send_header(data, size)) != ERROR_SUCCESS) {  
109 - srs_error("http: send header failed. ret=%d", ret);  
110 - return ret;  
111 - } 108 + }
  109 +
  110 + // whatever header is wrote, we should try to send header.
  111 + if ((ret = send_header(data, size)) != ERROR_SUCCESS) {
  112 + srs_error("http: send header failed. ret=%d", ret);
  113 + return ret;
112 } 114 }
113 115
114 // check the bytes send and content length. 116 // check the bytes send and content length.
@@ -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) {
@@ -809,6 +809,19 @@ int SrsServer::http_handle() @@ -809,6 +809,19 @@ int SrsServer::http_handle()
809 if ((ret = http_api_mux->handle("/api/v1/streams", new SrsGoApiStreams())) != ERROR_SUCCESS) { 809 if ((ret = http_api_mux->handle("/api/v1/streams", new SrsGoApiStreams())) != ERROR_SUCCESS) {
810 return ret; 810 return ret;
811 } 811 }
  812 + // for error test which always response error code 100.
  813 + if ((ret = http_api_mux->handle("/api/v1/errors", new SrsGoApiError())) != ERROR_SUCCESS) {
  814 + return ret;
  815 + }
  816 +
  817 + // TODO: FIXME: for console.
  818 + // TODO: FIXME: support reload.
  819 + std::string dir = _srs_config->get_http_stream_dir() + "/srs-console";
  820 + if ((ret = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != ERROR_SUCCESS) {
  821 + srs_error("http: mount console dir=%s failed. ret=%d", dir.c_str(), ret);
  822 + return ret;
  823 + }
  824 + srs_trace("http: console mount to %s", dir.c_str());
812 #endif 825 #endif
813 826
814 return ret; 827 return ret;