winlin

refine http request post. 2.0.132.

@@ -35,6 +35,7 @@ using namespace std; @@ -35,6 +35,7 @@ using namespace std;
35 #include <srs_app_json.hpp> 35 #include <srs_app_json.hpp>
36 #include <srs_app_http.hpp> 36 #include <srs_app_http.hpp>
37 #include <srs_app_utility.hpp> 37 #include <srs_app_utility.hpp>
  38 +#include <srs_core_autofree.hpp>
38 39
39 SrsHttpHeartbeat::SrsHttpHeartbeat() 40 SrsHttpHeartbeat::SrsHttpHeartbeat()
40 { 41 {
@@ -73,17 +74,22 @@ void SrsHttpHeartbeat::heartbeat() @@ -73,17 +74,22 @@ void SrsHttpHeartbeat::heartbeat()
73 srs_api_dump_summaries(ss); 74 srs_api_dump_summaries(ss);
74 } 75 }
75 ss << __SRS_JOBJECT_END; 76 ss << __SRS_JOBJECT_END;
76 - std::string data = ss.str();  
77 - std::string res;  
78 - int status_code;  
79 77
  78 + std::string data = ss.str();
80 SrsHttpClient http; 79 SrsHttpClient http;
81 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 80 + SrsHttpMessage* msg = NULL;
  81 + if ((ret = http.post(&uri, data, &msg)) != ERROR_SUCCESS) {
82 srs_info("http post hartbeart uri failed. " 82 srs_info("http post hartbeart uri failed. "
83 "url=%s, request=%s, response=%s, ret=%d", 83 "url=%s, request=%s, response=%s, ret=%d",
84 url.c_str(), data.c_str(), res.c_str(), ret); 84 url.c_str(), data.c_str(), res.c_str(), ret);
85 return; 85 return;
86 } 86 }
  87 + SrsAutoFree(SrsHttpMessage, msg);
  88 +
  89 + std::string res;
  90 + if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
  91 + return;
  92 + }
87 93
88 srs_info("http hook hartbeart success. " 94 srs_info("http hook hartbeart success. "
89 "url=%s, request=%s, status_code=%d, response=%s, ret=%d", 95 "url=%s, request=%s, status_code=%d, response=%s, ret=%d",
@@ -1181,22 +1181,8 @@ int SrsHttpMessage::body_read_all(string& body) @@ -1181,22 +1181,8 @@ int SrsHttpMessage::body_read_all(string& body)
1181 { 1181 {
1182 int ret = ERROR_SUCCESS; 1182 int ret = ERROR_SUCCESS;
1183 1183
1184 - // chunked, always read with  
1185 - if (chunked) {  
1186 - return _body->read(body);  
1187 - }  
1188 -  
1189 - int content_length = (int)(int64_t)_header.content_length;  
1190 -  
1191 - // ignore if not set, should be zero length body.  
1192 - if (content_length <= 0) {  
1193 - srs_info("unspecified content-length with body empty.");  
1194 - return ret;  
1195 - }  
1196 -  
1197 - // when content length specified, read specified length.  
1198 - int expect = content_length + (int)body.length();  
1199 - while ((int)body.length() < expect) { 1184 + // whatever, read util EOF.
  1185 + while (!_body->eof()) {
1200 if ((ret = _body->read(body)) != ERROR_SUCCESS) { 1186 if ((ret = _body->read(body)) != ERROR_SUCCESS) {
1201 return ret; 1187 return ret;
1202 } 1188 }
@@ -54,9 +54,9 @@ SrsHttpClient::~SrsHttpClient() @@ -54,9 +54,9 @@ SrsHttpClient::~SrsHttpClient()
54 srs_freep(parser); 54 srs_freep(parser);
55 } 55 }
56 56
57 -int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& res) 57 +int SrsHttpClient::post(SrsHttpUri* uri, string req, SrsHttpMessage** ppmsg)
58 { 58 {
59 - res = ""; 59 + *ppmsg = NULL;
60 60
61 int ret = ERROR_SUCCESS; 61 int ret = ERROR_SUCCESS;
62 62
@@ -103,16 +103,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& r @@ -103,16 +103,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, int& status_code, string& r
103 } 103 }
104 104
105 srs_assert(msg); 105 srs_assert(msg);
106 -  
107 - // always free it in this scope.  
108 - SrsAutoFree(SrsHttpMessage, msg);  
109 -  
110 - status_code = (int)msg->status_code();  
111 -  
112 - // get response body.  
113 - if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {  
114 - return ret;  
115 - } 106 + *ppmsg = msg;
116 srs_info("parse http post response success."); 107 srs_info("parse http post response success.");
117 108
118 return ret; 109 return ret;
@@ -57,10 +57,9 @@ public: @@ -57,10 +57,9 @@ public:
57 /** 57 /**
58 * to post data to the uri. 58 * to post data to the uri.
59 * @param req the data post to uri. empty string to ignore. 59 * @param req the data post to uri. empty string to ignore.
60 - * @param status_code the output status code response by server.  
61 - * @param res output the response data from server. 60 + * @param ppmsg output the http message to read the response.
62 */ 61 */
63 - virtual int post(SrsHttpUri* uri, std::string req, int& status_code, std::string& res); 62 + virtual int post(SrsHttpUri* uri, std::string req, SrsHttpMessage** ppmsg);
64 /** 63 /**
65 * to get data from the uri. 64 * to get data from the uri.
66 * @param req the data post to uri. empty string to ignore. 65 * @param req the data post to uri. empty string to ignore.
@@ -35,6 +35,7 @@ using namespace std; @@ -35,6 +35,7 @@ using namespace std;
35 #include <srs_app_json.hpp> 35 #include <srs_app_json.hpp>
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 +#include <srs_core_autofree.hpp>
38 39
39 #define SRS_HTTP_RESPONSE_OK __SRS_XSTR(ERROR_SUCCESS) 40 #define SRS_HTTP_RESPONSE_OK __SRS_XSTR(ERROR_SUCCESS)
40 41
@@ -53,13 +54,6 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r @@ -53,13 +54,6 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r
53 { 54 {
54 int ret = ERROR_SUCCESS; 55 int ret = ERROR_SUCCESS;
55 56
56 - SrsHttpUri uri;  
57 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
58 - srs_error("http uri parse on_connect url failed. "  
59 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
60 - return ret;  
61 - }  
62 -  
63 std::stringstream ss; 57 std::stringstream ss;
64 ss << __SRS_JOBJECT_START 58 ss << __SRS_JOBJECT_START
65 << __SRS_JFIELD_STR("action", "on_connect") << __SRS_JFIELD_CONT 59 << __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 @@ -70,31 +64,14 @@ int SrsHttpHooks::on_connect(string url, int client_id, string ip, SrsRequest* r
70 << __SRS_JFIELD_STR("tcUrl", req->tcUrl) << __SRS_JFIELD_CONT 64 << __SRS_JFIELD_STR("tcUrl", req->tcUrl) << __SRS_JFIELD_CONT
71 << __SRS_JFIELD_STR("pageUrl", req->pageUrl) 65 << __SRS_JFIELD_STR("pageUrl", req->pageUrl)
72 << __SRS_JOBJECT_END; 66 << __SRS_JOBJECT_END;
  67 +
73 std::string data = ss.str(); 68 std::string data = ss.str();
74 std::string res; 69 std::string res;
75 int status_code; 70 int status_code;
76 -  
77 - SrsHttpClient http;  
78 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 71 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
79 srs_error("http post on_connect uri failed. " 72 srs_error("http post on_connect uri failed. "
80 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
81 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
82 - return ret;  
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 -  
94 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
95 - ret = ERROR_HTTP_DATA_INVLIAD;  
96 - srs_error("http hook on_connect validate failed. "  
97 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 73 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  74 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
98 return ret; 75 return ret;
99 } 76 }
100 77
@@ -109,13 +86,6 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re @@ -109,13 +86,6 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re
109 { 86 {
110 int ret = ERROR_SUCCESS; 87 int ret = ERROR_SUCCESS;
111 88
112 - SrsHttpUri uri;  
113 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
114 - srs_warn("http uri parse on_close url failed, ignored. "  
115 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
116 - return;  
117 - }  
118 -  
119 std::stringstream ss; 89 std::stringstream ss;
120 ss << __SRS_JOBJECT_START 90 ss << __SRS_JOBJECT_START
121 << __SRS_JFIELD_STR("action", "on_close") << __SRS_JFIELD_CONT 91 << __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 @@ -124,31 +94,14 @@ void SrsHttpHooks::on_close(string url, int client_id, string ip, SrsRequest* re
124 << __SRS_JFIELD_STR("vhost", req->vhost) << __SRS_JFIELD_CONT 94 << __SRS_JFIELD_STR("vhost", req->vhost) << __SRS_JFIELD_CONT
125 << __SRS_JFIELD_STR("app", req->app) 95 << __SRS_JFIELD_STR("app", req->app)
126 << __SRS_JOBJECT_END; 96 << __SRS_JOBJECT_END;
  97 +
127 std::string data = ss.str(); 98 std::string data = ss.str();
128 std::string res; 99 std::string res;
129 int status_code; 100 int status_code;
130 -  
131 - SrsHttpClient http;  
132 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 101 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
133 srs_warn("http post on_close uri failed, ignored. " 102 srs_warn("http post on_close uri failed, ignored. "
134 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
135 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
136 - return;  
137 - }  
138 -  
139 - // ensure the http status is ok.  
140 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
141 - if (status_code != SRS_CONSTS_HTTP_OK) {  
142 - ret = ERROR_HTTP_STATUS_INVLIAD;  
143 - srs_error("http hook on_close status failed. "  
144 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
145 - return;  
146 - }  
147 -  
148 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
149 - ret = ERROR_HTTP_DATA_INVLIAD;  
150 - srs_warn("http hook on_close validate failed, ignored. "  
151 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 103 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  104 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
152 return; 105 return;
153 } 106 }
154 107
@@ -163,13 +116,6 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r @@ -163,13 +116,6 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r
163 { 116 {
164 int ret = ERROR_SUCCESS; 117 int ret = ERROR_SUCCESS;
165 118
166 - SrsHttpUri uri;  
167 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
168 - srs_error("http uri parse on_publish url failed. "  
169 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
170 - return ret;  
171 - }  
172 -  
173 std::stringstream ss; 119 std::stringstream ss;
174 ss << __SRS_JOBJECT_START 120 ss << __SRS_JOBJECT_START
175 << __SRS_JFIELD_STR("action", "on_publish") << __SRS_JFIELD_CONT 121 << __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 @@ -179,31 +125,14 @@ int SrsHttpHooks::on_publish(string url, int client_id, string ip, SrsRequest* r
179 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT 125 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT
180 << __SRS_JFIELD_STR("stream", req->stream) 126 << __SRS_JFIELD_STR("stream", req->stream)
181 << __SRS_JOBJECT_END; 127 << __SRS_JOBJECT_END;
  128 +
182 std::string data = ss.str(); 129 std::string data = ss.str();
183 std::string res; 130 std::string res;
184 int status_code; 131 int status_code;
185 -  
186 - SrsHttpClient http;  
187 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 132 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
188 srs_error("http post on_publish uri failed. " 133 srs_error("http post on_publish uri failed. "
189 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
190 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
191 - return ret;  
192 - }  
193 -  
194 - // ensure the http status is ok.  
195 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
196 - if (status_code != SRS_CONSTS_HTTP_OK) {  
197 - ret = ERROR_HTTP_STATUS_INVLIAD;  
198 - srs_error("http hook on_publish status failed. "  
199 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
200 - return ret;  
201 - }  
202 -  
203 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
204 - ret = ERROR_HTTP_DATA_INVLIAD;  
205 - srs_error("http hook on_publish validate failed. "  
206 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 134 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  135 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
207 return ret; 136 return ret;
208 } 137 }
209 138
@@ -218,13 +147,6 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest @@ -218,13 +147,6 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest
218 { 147 {
219 int ret = ERROR_SUCCESS; 148 int ret = ERROR_SUCCESS;
220 149
221 - SrsHttpUri uri;  
222 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
223 - srs_warn("http uri parse on_unpublish url failed, ignored. "  
224 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
225 - return;  
226 - }  
227 -  
228 std::stringstream ss; 150 std::stringstream ss;
229 ss << __SRS_JOBJECT_START 151 ss << __SRS_JOBJECT_START
230 << __SRS_JFIELD_STR("action", "on_unpublish") << __SRS_JFIELD_CONT 152 << __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 @@ -234,31 +156,14 @@ void SrsHttpHooks::on_unpublish(string url, int client_id, string ip, SrsRequest
234 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT 156 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT
235 << __SRS_JFIELD_STR("stream", req->stream) 157 << __SRS_JFIELD_STR("stream", req->stream)
236 << __SRS_JOBJECT_END; 158 << __SRS_JOBJECT_END;
  159 +
237 std::string data = ss.str(); 160 std::string data = ss.str();
238 std::string res; 161 std::string res;
239 int status_code; 162 int status_code;
240 -  
241 - SrsHttpClient http;  
242 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 163 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
243 srs_warn("http post on_unpublish uri failed, ignored. " 164 srs_warn("http post on_unpublish uri failed, ignored. "
244 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
245 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
246 - return;  
247 - }  
248 -  
249 - // ensure the http status is ok.  
250 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
251 - if (status_code != SRS_CONSTS_HTTP_OK) {  
252 - ret = ERROR_HTTP_STATUS_INVLIAD;  
253 - srs_error("http hook on_unpublish status failed. "  
254 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
255 - return;  
256 - }  
257 -  
258 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
259 - ret = ERROR_HTTP_DATA_INVLIAD;  
260 - srs_warn("http hook on_unpublish validate failed, ignored. "  
261 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 165 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  166 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
262 return; 167 return;
263 } 168 }
264 169
@@ -273,13 +178,6 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req) @@ -273,13 +178,6 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req)
273 { 178 {
274 int ret = ERROR_SUCCESS; 179 int ret = ERROR_SUCCESS;
275 180
276 - SrsHttpUri uri;  
277 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
278 - srs_error("http uri parse on_play url failed. "  
279 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
280 - return ret;  
281 - }  
282 -  
283 std::stringstream ss; 181 std::stringstream ss;
284 ss << __SRS_JOBJECT_START 182 ss << __SRS_JOBJECT_START
285 << __SRS_JFIELD_STR("action", "on_play") << __SRS_JFIELD_CONT 183 << __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) @@ -289,31 +187,14 @@ int SrsHttpHooks::on_play(string url, int client_id, string ip, SrsRequest* req)
289 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT 187 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT
290 << __SRS_JFIELD_STR("stream", req->stream) 188 << __SRS_JFIELD_STR("stream", req->stream)
291 << __SRS_JOBJECT_END; 189 << __SRS_JOBJECT_END;
  190 +
292 std::string data = ss.str(); 191 std::string data = ss.str();
293 std::string res; 192 std::string res;
294 int status_code; 193 int status_code;
295 -  
296 - SrsHttpClient http;  
297 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 194 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
298 srs_error("http post on_play uri failed. " 195 srs_error("http post on_play uri failed. "
299 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
300 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
301 - return ret;  
302 - }  
303 -  
304 - // ensure the http status is ok.  
305 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
306 - if (status_code != SRS_CONSTS_HTTP_OK) {  
307 - ret = ERROR_HTTP_STATUS_INVLIAD;  
308 - srs_error("http hook on_play status failed. "  
309 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
310 - return ret;  
311 - }  
312 -  
313 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
314 - ret = ERROR_HTTP_DATA_INVLIAD;  
315 - srs_error("http hook on_play validate failed. "  
316 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 196 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  197 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
317 return ret; 198 return ret;
318 } 199 }
319 200
@@ -328,13 +209,6 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req @@ -328,13 +209,6 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req
328 { 209 {
329 int ret = ERROR_SUCCESS; 210 int ret = ERROR_SUCCESS;
330 211
331 - SrsHttpUri uri;  
332 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
333 - srs_warn("http uri parse on_stop url failed, ignored. "  
334 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
335 - return;  
336 - }  
337 -  
338 std::stringstream ss; 212 std::stringstream ss;
339 ss << __SRS_JOBJECT_START 213 ss << __SRS_JOBJECT_START
340 << __SRS_JFIELD_STR("action", "on_stop") << __SRS_JFIELD_CONT 214 << __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 @@ -344,31 +218,14 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req
344 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT 218 << __SRS_JFIELD_STR("app", req->app) << __SRS_JFIELD_CONT
345 << __SRS_JFIELD_STR("stream", req->stream) 219 << __SRS_JFIELD_STR("stream", req->stream)
346 << __SRS_JOBJECT_END; 220 << __SRS_JOBJECT_END;
  221 +
347 std::string data = ss.str(); 222 std::string data = ss.str();
348 std::string res; 223 std::string res;
349 int status_code; 224 int status_code;
350 -  
351 - SrsHttpClient http;  
352 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 225 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
353 srs_warn("http post on_stop uri failed, ignored. " 226 srs_warn("http post on_stop uri failed, ignored. "
354 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
355 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
356 - return;  
357 - }  
358 -  
359 - // ensure the http status is ok.  
360 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
361 - if (status_code != SRS_CONSTS_HTTP_OK) {  
362 - ret = ERROR_HTTP_STATUS_INVLIAD;  
363 - srs_error("http hook on_stop status failed. "  
364 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
365 - return;  
366 - }  
367 -  
368 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
369 - ret = ERROR_HTTP_DATA_INVLIAD;  
370 - srs_warn("http hook on_stop validate failed, ignored. "  
371 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 227 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  228 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
372 return; 229 return;
373 } 230 }
374 231
@@ -383,13 +240,6 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req, @@ -383,13 +240,6 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req,
383 { 240 {
384 int ret = ERROR_SUCCESS; 241 int ret = ERROR_SUCCESS;
385 242
386 - SrsHttpUri uri;  
387 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
388 - srs_error("http uri parse on_dvr url failed, ignored. "  
389 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
390 - return ret;  
391 - }  
392 -  
393 std::stringstream ss; 243 std::stringstream ss;
394 ss << __SRS_JOBJECT_START 244 ss << __SRS_JOBJECT_START
395 << __SRS_JFIELD_STR("action", "on_dvr") << __SRS_JFIELD_CONT 245 << __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, @@ -401,31 +251,14 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req,
401 << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT 251 << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT
402 << __SRS_JFIELD_STR("file", file) 252 << __SRS_JFIELD_STR("file", file)
403 << __SRS_JOBJECT_END; 253 << __SRS_JOBJECT_END;
  254 +
404 std::string data = ss.str(); 255 std::string data = ss.str();
405 std::string res; 256 std::string res;
406 int status_code; 257 int status_code;
407 -  
408 - SrsHttpClient http;  
409 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 258 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
410 srs_error("http post on_dvr uri failed, ignored. " 259 srs_error("http post on_dvr uri failed, ignored. "
411 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
412 - client_id, url.c_str(), data.c_str(), res.c_str(), ret);  
413 - return ret;  
414 - }  
415 -  
416 - // ensure the http status is ok.  
417 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
418 - if (status_code != SRS_CONSTS_HTTP_OK) {  
419 - ret = ERROR_HTTP_STATUS_INVLIAD;  
420 - srs_error("http hook on_dvr status failed. "  
421 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret);  
422 - return ret;  
423 - }  
424 -  
425 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
426 - ret = ERROR_HTTP_DATA_INVLIAD;  
427 - srs_warn("http hook on_dvr validate failed, ignored. "  
428 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 260 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  261 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
429 return ret; 262 return ret;
430 } 263 }
431 264
@@ -440,13 +273,6 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req @@ -440,13 +273,6 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req
440 { 273 {
441 int ret = ERROR_SUCCESS; 274 int ret = ERROR_SUCCESS;
442 275
443 - SrsHttpUri uri;  
444 - if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {  
445 - srs_error("http uri parse on_dvr_reap_segment url failed, ignored. "  
446 - "client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);  
447 - return ret;  
448 - }  
449 -  
450 std::stringstream ss; 276 std::stringstream ss;
451 ss << __SRS_JOBJECT_START 277 ss << __SRS_JOBJECT_START
452 << __SRS_JFIELD_STR("action", "on_dvr_reap_segment") << __SRS_JFIELD_CONT 278 << __SRS_JFIELD_STR("action", "on_dvr_reap_segment") << __SRS_JFIELD_CONT
@@ -457,37 +283,56 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req @@ -457,37 +283,56 @@ int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req
457 << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT 283 << __SRS_JFIELD_STR("cwd", cwd) << __SRS_JFIELD_CONT
458 << __SRS_JFIELD_STR("file", file) 284 << __SRS_JFIELD_STR("file", file)
459 << __SRS_JOBJECT_END; 285 << __SRS_JOBJECT_END;
  286 +
460 std::string data = ss.str(); 287 std::string data = ss.str();
461 std::string res; 288 std::string res;
462 int status_code; 289 int status_code;
463 -  
464 - SrsHttpClient http;  
465 - if ((ret = http.post(&uri, data, status_code, res)) != ERROR_SUCCESS) { 290 + if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
466 srs_error("http post on_dvr_reap_segment uri failed, ignored. " 291 srs_error("http post on_dvr_reap_segment uri failed, ignored. "
  292 + "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
  293 + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
  294 + return ret;
  295 + }
  296 +
  297 + srs_trace("http hook on_dvr_reap_segment success. "
467 "client_id=%d, url=%s, request=%s, response=%s, ret=%d", 298 "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
468 client_id, url.c_str(), data.c_str(), res.c_str(), ret); 299 client_id, url.c_str(), data.c_str(), res.c_str(), ret);
  300 +
  301 + return ret;
  302 +}
  303 +
  304 +int SrsHttpHooks::do_post(std::string url, std::string req, int& code, string& res)
  305 +{
  306 + int ret = ERROR_SUCCESS;
  307 +
  308 + SrsHttpUri uri;
  309 + if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
  310 + srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret);
469 return ret; 311 return ret;
470 } 312 }
471 313
472 - // ensure the http status is ok.  
473 - // https://github.com/winlinvip/simple-rtmp-server/issues/158  
474 - if (status_code != SRS_CONSTS_HTTP_OK) {  
475 - ret = ERROR_HTTP_STATUS_INVLIAD;  
476 - srs_error("http hook on_dvr_reap_segment status failed. "  
477 - "client_id=%d, code=%d, ret=%d", client_id, status_code, ret); 314 + SrsHttpClient http;
  315 + SrsHttpMessage* msg = NULL;
  316 + if ((ret = http.post(&uri, req, &msg)) != ERROR_SUCCESS) {
478 return ret; 317 return ret;
479 } 318 }
  319 + SrsAutoFree(SrsHttpMessage, msg);
480 320
481 - if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {  
482 - ret = ERROR_HTTP_DATA_INVLIAD;  
483 - srs_warn("http hook on_dvr_reap_segment validate failed, ignored. "  
484 - "client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret); 321 + code = msg->status_code();
  322 + if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
485 return ret; 323 return ret;
486 } 324 }
487 325
488 - srs_trace("http hook on_dvr_reap_segment success. "  
489 - "client_id=%d, url=%s, request=%s, response=%s, ret=%d",  
490 - client_id, url.c_str(), data.c_str(), res.c_str(), ret); 326 + // ensure the http status is ok.
  327 + // https://github.com/winlinvip/simple-rtmp-server/issues/158
  328 + if (code != SRS_CONSTS_HTTP_OK) {
  329 + return ERROR_HTTP_STATUS_INVLIAD;
  330 + }
  331 +
  332 + // TODO: FIXME: parse json.
  333 + if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
  334 + return ERROR_HTTP_DATA_INVLIAD;
  335 + }
491 336
492 return ret; 337 return ret;
493 } 338 }
@@ -113,6 +113,8 @@ public: @@ -113,6 +113,8 @@ public:
113 * @param file the file path, can be relative or absolute path. 113 * @param file the file path, can be relative or absolute path.
114 */ 114 */
115 static int on_dvr_reap_segment(std::string url, int client_id, SrsRequest* req, std::string cwd, std::string file); 115 static int on_dvr_reap_segment(std::string url, int client_id, SrsRequest* req, std::string cwd, std::string file);
  116 +private:
  117 + static int do_post(std::string url, std::string req, int& code, std::string& res);
116 }; 118 };
117 119
118 #endif 120 #endif