diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index a8828c1d8..764b74198 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -35,6 +35,7 @@ using namespace std; #include #include #include +#include SrsHttpHeartbeat::SrsHttpHeartbeat() { @@ -73,17 +74,22 @@ void SrsHttpHeartbeat::heartbeat() srs_api_dump_summaries(ss); } ss << __SRS_JOBJECT_END; - std::string data = ss.str(); - std::string res; - int status_code; + std::string data = ss.str(); SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + SrsHttpMessage* msg = NULL; + if ((ret = http.post(&uri, data, &msg)) != ERROR_SUCCESS) { srs_info("http post hartbeart uri failed. " "url=%s, request=%s, response=%s, ret=%d", url.c_str(), data.c_str(), res.c_str(), ret); return; } + SrsAutoFree(SrsHttpMessage, msg); + + std::string res; + if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) { + return; + } srs_info("http hook hartbeart success. " "url=%s, request=%s, status_code=%d, response=%s, ret=%d", diff --git a/trunk/src/app/srs_app_http.cpp b/trunk/src/app/srs_app_http.cpp index 6b7535ec9..0f2e52903 100644 --- a/trunk/src/app/srs_app_http.cpp +++ b/trunk/src/app/srs_app_http.cpp @@ -1181,22 +1181,8 @@ int SrsHttpMessage::body_read_all(string& body) { int ret = ERROR_SUCCESS; - // chunked, always read with - if (chunked) { - return _body->read(body); - } - - int content_length = (int)(int64_t)_header.content_length; - - // ignore if not set, should be zero length body. - if (content_length <= 0) { - srs_info("unspecified content-length with body empty."); - return ret; - } - - // when content length specified, read specified length. - int expect = content_length + (int)body.length(); - while ((int)body.length() < expect) { + // whatever, read util EOF. + while (!_body->eof()) { if ((ret = _body->read(body)) != ERROR_SUCCESS) { return ret; } diff --git a/trunk/src/app/srs_app_http_client.cpp b/trunk/src/app/srs_app_http_client.cpp index 2139285f4..412c8669e 100644 --- a/trunk/src/app/srs_app_http_client.cpp +++ b/trunk/src/app/srs_app_http_client.cpp @@ -54,9 +54,9 @@ SrsHttpClient::~SrsHttpClient() srs_freep(parser); } -int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& res) +int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg) { - res = ""; + *ppmsg = NULL; int ret = ERROR_SUCCESS; @@ -103,16 +103,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& r } srs_assert(msg); - - // always free it in this scope. - SrsAutoFree(SrsHttpMessage, msg); - - status_code = (int)msg->status_code(); - - // get response body. - if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) { - return ret; - } + *ppmsg = msg; srs_info("parse http post response success."); return ret; diff --git a/trunk/src/app/srs_app_http_client.hpp b/trunk/src/app/srs_app_http_client.hpp index 0dbcd2475..52a560273 100644 --- a/trunk/src/app/srs_app_http_client.hpp +++ b/trunk/src/app/srs_app_http_client.hpp @@ -57,10 +57,9 @@ public: /** * to post data to the uri. * @param req the data post to uri. empty string to ignore. - * @param status_code the output status code response by server. - * @param res output the response data from server. + * @param ppmsg output the http message to read the response. */ - virtual int post(SrsHttpUri* uri, std::string req, int& status_code, std::string& res); + virtual int post(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg); /** * to get data from the uri. * @param req the data post to uri. empty string to ignore. diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index 4a02232cc..e685feff5 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -35,6 +35,7 @@ using namespace std; #include #include #include +#include #define SRS_HTTP_RESPONSE_OK __SRS_XSTR(ERROR_SUCCESS) @@ -53,13 +54,6 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http uri parse on_connect url failed. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return ret; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_connect") << __SRS_JFIELD_CONT @@ -70,31 +64,14 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r << __SRS_JFIELD_STR("tcUrl", req->tcUrl) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("pageUrl", req->pageUrl) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_error("http post on_connect uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return ret; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_connect status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return ret; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_error("http hook on_connect validate failed. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return ret; } @@ -109,13 +86,6 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_warn("http uri parse on_close url failed, ignored. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_close") << __SRS_JFIELD_CONT @@ -124,31 +94,14 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re << __SRS_JFIELD_STR("vhost", req->vhost) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("app", req->app) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_warn("http post on_close uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_close status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_warn("http hook on_close validate failed, ignored. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } @@ -163,13 +116,6 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http uri parse on_publish url failed. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return ret; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_publish") << __SRS_JFIELD_CONT @@ -179,31 +125,14 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("stream", req->stream) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_error("http post on_publish uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return ret; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_publish status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return ret; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_error("http hook on_publish validate failed. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return ret; } @@ -218,13 +147,6 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_warn("http uri parse on_unpublish url failed, ignored. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_unpublish") << __SRS_JFIELD_CONT @@ -234,31 +156,14 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("stream", req->stream) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_warn("http post on_unpublish uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_unpublish status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_warn("http hook on_unpublish validate failed, ignored. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } @@ -273,13 +178,6 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req) { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http uri parse on_play url failed. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return ret; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_play") << __SRS_JFIELD_CONT @@ -289,31 +187,14 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req) << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("stream", req->stream) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_error("http post on_play uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return ret; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_play status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return ret; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_error("http hook on_play validate failed. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return ret; } @@ -328,13 +209,6 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_warn("http uri parse on_stop url failed, ignored. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_stop") << __SRS_JFIELD_CONT @@ -344,31 +218,14 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("stream", req->stream) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_warn("http post on_stop uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_stop status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_warn("http hook on_stop validate failed, ignored. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } @@ -383,13 +240,6 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req, { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http uri parse on_dvr url failed, ignored. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return ret; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_dvr") << __SRS_JFIELD_CONT @@ -401,31 +251,14 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req, << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("file", file) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_error("http post on_dvr uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return ret; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_dvr status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return ret; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_warn("http hook on_dvr validate failed, ignored. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return ret; } @@ -440,13 +273,6 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req { int ret = ERROR_SUCCESS; - SrsHttpUri uri; - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http uri parse on_dvr_reap_segment url failed, ignored. " - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret); - return ret; - } - std::stringstream ss; ss << __SRS_JOBJECT_START << __SRS_JFIELD_STR("action", "on_dvr_reap_segment") << __SRS_JFIELD_CONT @@ -457,31 +283,14 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("file", file) << __SRS_JOBJECT_END; + std::string data = ss.str(); std::string res; int status_code; - - SrsHttpClient http; - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) { srs_error("http post on_dvr_reap_segment uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); - return ret; - } - - // ensure the http status is ok. - // https://github.com/winlinvip/simple-rtmp-server/issues/158 - if (status_code != SRS_CONSTS_HTTP_OK) { - ret = ERROR_HTTP_STATUS_INVLIAD; - srs_error("http hook on_dvr_reap_segment status failed. " - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); - return ret; - } - - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVLIAD; - srs_warn("http hook on_dvr_reap_segment validate failed, ignored. " - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return ret; } @@ -492,4 +301,40 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req return ret; } +int SrsHttpHooks::do_post(std::string url, std::string req, int& code, string& res) +{ + int ret = ERROR_SUCCESS; + + SrsHttpUri uri; + if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { + srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret); + return ret; + } + + SrsHttpClient http; + SrsHttpMessage* msg = NULL; + if ((ret = http.post(&uri, req, &msg)) != ERROR_SUCCESS) { + return ret; + } + SrsAutoFree(SrsHttpMessage, msg); + + code = msg->status_code(); + if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) { + return ret; + } + + // ensure the http status is ok. + // https://github.com/winlinvip/simple-rtmp-server/issues/158 + if (code != SRS_CONSTS_HTTP_OK) { + return ERROR_HTTP_STATUS_INVLIAD; + } + + // TODO: FIXME: parse json. + if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { + return ERROR_HTTP_DATA_INVLIAD; + } + + return ret; +} + #endif diff --git a/trunk/src/app/srs_app_http_hooks.hpp b/trunk/src/app/srs_app_http_hooks.hpp index ebc1452ba..49c387fb9 100644 --- a/trunk/src/app/srs_app_http_hooks.hpp +++ b/trunk/src/app/srs_app_http_hooks.hpp @@ -113,6 +113,8 @@ public: * @param file the file path, can be relative or absolute path. */ static int on_dvr_reap_segment(std::string url, int client_id, SrsRequest* req, std::string cwd, std::string file); +private: + static int do_post(std::string url, std::string req, int& code, std::string& res); }; #endif