winlin

fix #158: http-callback check http status code ok(200). 2.0.84

@@ -501,6 +501,7 @@ Supported operating systems and hardware: @@ -501,6 +501,7 @@ Supported operating systems and hardware:
501 * 2013-10-17, Created.<br/> 501 * 2013-10-17, Created.<br/>
502 502
503 ## History 503 ## History
  504 +* v2.0, 2014-01-02, fix [#158](https://github.com/winlinvip/simple-rtmp-server/issues/158), http-callback check http status code ok(200). 2.0.84
504 * v2.0, 2015-01-02, hotfix [#216](https://github.com/winlinvip/simple-rtmp-server/issues/216), http-callback post in application/json content-type. 2.0.83 505 * v2.0, 2015-01-02, hotfix [#216](https://github.com/winlinvip/simple-rtmp-server/issues/216), http-callback post in application/json content-type. 2.0.83
505 * v2.0, 2014-01-02, fix [#263](https://github.com/winlinvip/simple-rtmp-server/issues/263), srs-librtmp flv read tag should init size. 2.0.82 506 * v2.0, 2014-01-02, fix [#263](https://github.com/winlinvip/simple-rtmp-server/issues/263), srs-librtmp flv read tag should init size. 2.0.82
506 * v2.0, 2015-01-01, hotfix [#270](https://github.com/winlinvip/simple-rtmp-server/issues/270), memory leak for http client post. 2.0.81 507 * v2.0, 2015-01-01, hotfix [#270](https://github.com/winlinvip/simple-rtmp-server/issues/270), memory leak for http client post. 2.0.81
@@ -87,6 +87,7 @@ class RESTClients(object): @@ -87,6 +87,7 @@ class RESTClients(object):
87 "action": "on_connect", 87 "action": "on_connect",
88 "client_id": 1985, 88 "client_id": 1985,
89 "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", 89 "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
  90 + "tcUrl": "rtmp://video.test.com/live?key=d2fa801d08e3f90ed1e1670e6e52651a",
90 "pageUrl": "http://www.test.com/live.html" 91 "pageUrl": "http://www.test.com/live.html"
91 } 92 }
92 on_close: 93 on_close:
@@ -118,6 +119,7 @@ class RESTClients(object): @@ -118,6 +119,7 @@ class RESTClients(object):
118 119
119 action = json_req["action"] 120 action = json_req["action"]
120 if action == "on_connect": 121 if action == "on_connect":
  122 + raise cherrypy.HTTPError(401)
121 code = self.__on_connect(json_req) 123 code = self.__on_connect(json_req)
122 elif action == "on_close": 124 elif action == "on_close":
123 code = self.__on_close(json_req) 125 code = self.__on_close(json_req)
@@ -75,9 +75,10 @@ void SrsHttpHeartbeat::heartbeat() @@ -75,9 +75,10 @@ void SrsHttpHeartbeat::heartbeat()
75 ss << __SRS_JOBJECT_END; 75 ss << __SRS_JOBJECT_END;
76 std::string data = ss.str(); 76 std::string data = ss.str();
77 std::string res; 77 std::string res;
  78 + int status_code;
78 79
79 SrsHttpClient http; 80 SrsHttpClient http;
80 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 81 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
81 srs_info("http post hartbeart uri failed. " 82 srs_info("http post hartbeart uri failed. "
82 "url=%s, request=%s, response=%s, ret=%d", 83 "url=%s, request=%s, response=%s, ret=%d",
83 url.c_str(), data.c_str(), res.c_str(), ret); 84 url.c_str(), data.c_str(), res.c_str(), ret);
@@ -85,8 +86,8 @@ void SrsHttpHeartbeat::heartbeat() @@ -85,8 +86,8 @@ void SrsHttpHeartbeat::heartbeat()
85 } 86 }
86 87
87 srs_info("http hook hartbeart success. " 88 srs_info("http hook hartbeart success. "
88 - "url=%s, request=%s, response=%s, ret=%d",  
89 - url.c_str(), data.c_str(), res.c_str(), ret); 89 + "url=%s, request=%s, status_code=%d, response=%s, ret=%d",
  90 + url.c_str(), data.c_str(), status_code, res.c_str(), ret);
90 91
91 return; 92 return;
92 } 93 }
@@ -593,6 +593,11 @@ u_int8_t SrsHttpMessage::method() @@ -593,6 +593,11 @@ u_int8_t SrsHttpMessage::method()
593 return (u_int8_t)_header.method; 593 return (u_int8_t)_header.method;
594 } 594 }
595 595
  596 +u_int16_t SrsHttpMessage::status_code()
  597 +{
  598 + return (u_int16_t)_header.status_code;
  599 +}
  600 +
596 string SrsHttpMessage::method_str() 601 string SrsHttpMessage::method_str()
597 { 602 {
598 if (is_http_get()) { 603 if (is_http_get()) {
@@ -249,6 +249,7 @@ public: @@ -249,6 +249,7 @@ public:
249 public: 249 public:
250 virtual bool is_complete(); 250 virtual bool is_complete();
251 virtual u_int8_t method(); 251 virtual u_int8_t method();
  252 + virtual u_int16_t status_code();
252 virtual std::string method_str(); 253 virtual std::string method_str();
253 virtual bool is_http_get(); 254 virtual bool is_http_get();
254 virtual bool is_http_put(); 255 virtual bool is_http_put();
@@ -52,7 +52,7 @@ SrsHttpClient::~SrsHttpClient() @@ -52,7 +52,7 @@ SrsHttpClient::~SrsHttpClient()
52 srs_freep(parser); 52 srs_freep(parser);
53 } 53 }
54 54
55 -int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) 55 +int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& res)
56 { 56 {
57 res = ""; 57 res = "";
58 58
@@ -105,6 +105,8 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) @@ -105,6 +105,8 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res)
105 srs_assert(msg); 105 srs_assert(msg);
106 srs_assert(msg->is_complete()); 106 srs_assert(msg->is_complete());
107 107
  108 + status_code = (int)msg->status_code();
  109 +
108 // get response body. 110 // get response body.
109 if (msg->body_size() > 0) { 111 if (msg->body_size() > 0) {
110 res = msg->body(); 112 res = msg->body();
@@ -54,9 +54,10 @@ public: @@ -54,9 +54,10 @@ public:
54 /** 54 /**
55 * to post data to the uri. 55 * to post data to the uri.
56 * @param req the data post to uri. 56 * @param req the data post to uri.
57 - * @param res the response data from server. 57 + * @param status_code the output status code response by server.
  58 + * @param res output the response data from server.
58 */ 59 */
59 - virtual int post(SrsHttpUri* uri, std::string req, std::string& res); 60 + virtual int post(SrsHttpUri* uri, std::string req, int& status_code, std::string& res);
60 private: 61 private:
61 virtual void disconnect(); 62 virtual void disconnect();
62 virtual int connect(SrsHttpUri* uri); 63 virtual int connect(SrsHttpUri* uri);
@@ -36,7 +36,7 @@ using namespace std; @@ -36,7 +36,7 @@ using namespace std;
36 #include <srs_app_dvr.hpp> 36 #include <srs_app_dvr.hpp>
37 #include <srs_app_http_client.hpp> 37 #include <srs_app_http_client.hpp>
38 38
39 -#define SRS_HTTP_RESPONSE_OK "0" 39 +#define SRS_HTTP_RESPONSE_OK __SRS_XSTR(ERROR_SUCCESS)
40 40
41 #define SRS_HTTP_HEADER_BUFFER 1024 41 #define SRS_HTTP_HEADER_BUFFER 1024
42 #define SRS_HTTP_BODY_BUFFER 32 * 1024 42 #define SRS_HTTP_BODY_BUFFER 32 * 1024
@@ -72,15 +72,25 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r @@ -72,15 +72,25 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r
72 << __SRS_JOBJECT_END; 72 << __SRS_JOBJECT_END;
73 std::string data = ss.str(); 73 std::string data = ss.str();
74 std::string res; 74 std::string res;
  75 + int status_code;
75 76
76 SrsHttpClient http; 77 SrsHttpClient http;
77 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 78 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
78 srs_error("http post on_connect uri failed. " 79 srs_error("http post on_connect uri failed. "
79 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 80 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
80 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 81 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
81 return ret; 82 return ret;
82 } 83 }
83 84
  85 + // ensure the http status is ok.
  86 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  87 + if (status_code != SRS_CONSTS_HTTP_OK) {
  88 + ret = ERROR_HTTP_STATUS_INVLIAD;
  89 + srs_error("http hook on_connect status failed. "
  90 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  91 + return ret;
  92 + }
  93 +
84 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 94 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
85 ret = ERROR_HTTP_DATA_INVLIAD; 95 ret = ERROR_HTTP_DATA_INVLIAD;
86 srs_error("http hook on_connect validate failed. " 96 srs_error("http hook on_connect validate failed. "
@@ -117,15 +127,25 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re @@ -117,15 +127,25 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re
117 << __SRS_JOBJECT_END; 127 << __SRS_JOBJECT_END;
118 std::string data = ss.str(); 128 std::string data = ss.str();
119 std::string res; 129 std::string res;
  130 + int status_code;
120 131
121 SrsHttpClient http; 132 SrsHttpClient http;
122 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 133 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
123 srs_warn("http post on_close uri failed, ignored. " 134 srs_warn("http post on_close uri failed, ignored. "
124 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 135 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
125 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 136 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
126 return; 137 return;
127 } 138 }
128 139
  140 + // ensure the http status is ok.
  141 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  142 + if (status_code != SRS_CONSTS_HTTP_OK) {
  143 + ret = ERROR_HTTP_STATUS_INVLIAD;
  144 + srs_error("http hook on_close status failed. "
  145 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  146 + return;
  147 + }
  148 +
129 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 149 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
130 ret = ERROR_HTTP_DATA_INVLIAD; 150 ret = ERROR_HTTP_DATA_INVLIAD;
131 srs_warn("http hook on_close validate failed, ignored. " 151 srs_warn("http hook on_close validate failed, ignored. "
@@ -163,15 +183,25 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r @@ -163,15 +183,25 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r
163 << __SRS_JOBJECT_END; 183 << __SRS_JOBJECT_END;
164 std::string data = ss.str(); 184 std::string data = ss.str();
165 std::string res; 185 std::string res;
  186 + int status_code;
166 187
167 SrsHttpClient http; 188 SrsHttpClient http;
168 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 189 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
169 srs_error("http post on_publish uri failed. " 190 srs_error("http post on_publish uri failed. "
170 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 191 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
171 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 192 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
172 return ret; 193 return ret;
173 } 194 }
174 195
  196 + // ensure the http status is ok.
  197 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  198 + if (status_code != SRS_CONSTS_HTTP_OK) {
  199 + ret = ERROR_HTTP_STATUS_INVLIAD;
  200 + srs_error("http hook on_publish status failed. "
  201 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  202 + return ret;
  203 + }
  204 +
175 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 205 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
176 ret = ERROR_HTTP_DATA_INVLIAD; 206 ret = ERROR_HTTP_DATA_INVLIAD;
177 srs_error("http hook on_publish validate failed. " 207 srs_error("http hook on_publish validate failed. "
@@ -209,15 +239,25 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest @@ -209,15 +239,25 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest
209 << __SRS_JOBJECT_END; 239 << __SRS_JOBJECT_END;
210 std::string data = ss.str(); 240 std::string data = ss.str();
211 std::string res; 241 std::string res;
  242 + int status_code;
212 243
213 SrsHttpClient http; 244 SrsHttpClient http;
214 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 245 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
215 srs_warn("http post on_unpublish uri failed, ignored. " 246 srs_warn("http post on_unpublish uri failed, ignored. "
216 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 247 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
217 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 248 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
218 return; 249 return;
219 } 250 }
220 251
  252 + // ensure the http status is ok.
  253 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  254 + if (status_code != SRS_CONSTS_HTTP_OK) {
  255 + ret = ERROR_HTTP_STATUS_INVLIAD;
  256 + srs_error("http hook on_unpublish status failed. "
  257 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  258 + return;
  259 + }
  260 +
221 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 261 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
222 ret = ERROR_HTTP_DATA_INVLIAD; 262 ret = ERROR_HTTP_DATA_INVLIAD;
223 srs_warn("http hook on_unpublish validate failed, ignored. " 263 srs_warn("http hook on_unpublish validate failed, ignored. "
@@ -255,15 +295,25 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req) @@ -255,15 +295,25 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req)
255 << __SRS_JOBJECT_END; 295 << __SRS_JOBJECT_END;
256 std::string data = ss.str(); 296 std::string data = ss.str();
257 std::string res; 297 std::string res;
  298 + int status_code;
258 299
259 SrsHttpClient http; 300 SrsHttpClient http;
260 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 301 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
261 srs_error("http post on_play uri failed. " 302 srs_error("http post on_play uri failed. "
262 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 303 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
263 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 304 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
264 return ret; 305 return ret;
265 } 306 }
266 307
  308 + // ensure the http status is ok.
  309 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  310 + if (status_code != SRS_CONSTS_HTTP_OK) {
  311 + ret = ERROR_HTTP_STATUS_INVLIAD;
  312 + srs_error("http hook on_play status failed. "
  313 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  314 + return ret;
  315 + }
  316 +
267 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 317 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
268 ret = ERROR_HTTP_DATA_INVLIAD; 318 ret = ERROR_HTTP_DATA_INVLIAD;
269 srs_error("http hook on_play validate failed. " 319 srs_error("http hook on_play validate failed. "
@@ -301,15 +351,25 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req @@ -301,15 +351,25 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req
301 << __SRS_JOBJECT_END; 351 << __SRS_JOBJECT_END;
302 std::string data = ss.str(); 352 std::string data = ss.str();
303 std::string res; 353 std::string res;
  354 + int status_code;
304 355
305 SrsHttpClient http; 356 SrsHttpClient http;
306 - if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { 357 + if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) {
307 srs_warn("http post on_stop uri failed, ignored. " 358 srs_warn("http post on_stop uri failed, ignored. "
308 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 359 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
309 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 360 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
310 return; 361 return;
311 } 362 }
312 363
  364 + // ensure the http status is ok.
  365 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  366 + if (status_code != SRS_CONSTS_HTTP_OK) {
  367 + ret = ERROR_HTTP_STATUS_INVLIAD;
  368 + srs_error("http hook on_stop status failed. "
  369 + "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);
  370 + return;
  371 + }
  372 +
313 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) { 373 if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
314 ret = ERROR_HTTP_DATA_INVLIAD; 374 ret = ERROR_HTTP_DATA_INVLIAD;
315 srs_warn("http hook on_stop validate failed, ignored. " 375 srs_warn("http hook on_stop validate failed, ignored. "
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 83 34 +#define VERSION_REVISION 84
35 // server info. 35 // server info.
36 #define RTMP_SIG_SRS_KEY "SRS" 36 #define RTMP_SIG_SRS_KEY "SRS"
37 #define RTMP_SIG_SRS_ROLE "origin/edge server" 37 #define RTMP_SIG_SRS_ROLE "origin/edge server"
@@ -196,6 +196,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -196,6 +196,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
196 #define ERROR_AAC_REQUIRED_ADTS 3046 196 #define ERROR_AAC_REQUIRED_ADTS 3046
197 #define ERROR_AAC_ADTS_HEADER 3047 197 #define ERROR_AAC_ADTS_HEADER 3047
198 #define ERROR_AAC_DATA_INVALID 3048 198 #define ERROR_AAC_DATA_INVALID 3048
  199 +#define ERROR_HTTP_STATUS_INVLIAD 3049
199 200
200 /** 201 /**
201 * whether the error code is an system control error. 202 * whether the error code is an system control error.