From 20a8818a8ee88955ef1ee9138dd7dbd08e500235 Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 21 Aug 2015 17:15:32 +0800 Subject: [PATCH] refine the error response. --- trunk/src/app/srs_app_http_api.cpp | 41 +++++++-------------------- trunk/src/protocol/srs_http_stack.hpp | 4 +++ 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 63562eb7a..2825713c7 100755 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -509,31 +509,23 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) { ret = ERROR_RTMP_STREAM_NOT_FOUND; srs_error("stream stream_id=%d not found. ret=%d", sid, ret); - - ss << SRS_JOBJECT_START - << SRS_JFIELD_ERROR(ret) - << SRS_JOBJECT_END; - - return srs_http_response_json(w, ss.str()); + return srs_http_response_code(w, ret); } if (r->is_http_delete()) { srs_assert(stream); SrsSource* source = SrsSource::fetch(stream->vhost->vhost, stream->app, stream->stream); - if (source) { - source->set_expired(); - srs_warn("disconnent stream=%d successfully. vhost=%s, app=%s, stream=%s.", - sid, stream->vhost->vhost.c_str(), stream->app.c_str(), stream->stream.c_str()); - } else { + if (!source) { ret = ERROR_SOURCE_NOT_FOUND; + srs_warn("source not found for sid=%d", sid); + return srs_http_response_code(w, ret); } - - ss << SRS_JOBJECT_START - << SRS_JFIELD_ERROR(ret) - << SRS_JOBJECT_END; - - return srs_http_response_json(w, ss.str()); + + source->set_expired(); + srs_warn("disconnent stream=%d successfully. vhost=%s, app=%s, stream=%s.", + sid, stream->vhost->vhost.c_str(), stream->app.c_str(), stream->stream.c_str()); + return srs_http_response_code(w, ret); } else if (r->is_http_get()) { std::stringstream data; @@ -584,10 +576,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) 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()); + return srs_http_response_code(w, ret); } @@ -628,15 +617,7 @@ SrsGoApiError::~SrsGoApiError() int SrsGoApiError::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { - std::stringstream ss; - - ss << SRS_JOBJECT_START - << SRS_JFIELD_ERROR(100) << SRS_JFIELD_CONT - << SRS_JFIELD_STR("msg", "SRS demo error.") << SRS_JFIELD_CONT - << SRS_JFIELD_STR("path", r->path()) - << SRS_JOBJECT_END; - - return srs_http_response_json(w, ss.str()); + return srs_http_response_code(w, 100); } diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 2b683a6af..2888ce186 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -78,6 +78,10 @@ class ISrsHttpResponseWriter; // helper function: response in json format. extern int srs_http_response_json(ISrsHttpResponseWriter* w, std::string data); +/** + * response a typical code object, for example: + * {code : 100} + */ extern int srs_http_response_code(ISrsHttpResponseWriter* w, int code); // get the status text of code.