Blame view

trunk/src/app/srs_app_http_hooks.cpp 15.2 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <srs_app_http_hooks.hpp>
26
#ifdef SRS_AUTO_HTTP_CALLBACK
27 28 29 30 31

#include <sstream>
using namespace std;

#include <srs_kernel_error.hpp>
32
#include <srs_rtmp_sdk.hpp>
33
#include <srs_app_st_socket.hpp>
34
#include <srs_app_http.hpp>
35
#include <srs_app_json.hpp>
36
#include <srs_app_dvr.hpp>
37
#include <srs_app_http_client.hpp>
38
#include <srs_core_autofree.hpp>
39
#include <srs_app_config.hpp>
40
#include <srs_kernel_utility.hpp>
41
42
#define SRS_HTTP_RESPONSE_OK    SRS_XSTR(ERROR_SUCCESS)
43 44

#define SRS_HTTP_HEADER_BUFFER        1024
45
#define SRS_HTTP_READ_BUFFER    4096
46 47
#define SRS_HTTP_BODY_BUFFER        32 * 1024
48 49 50
// the timeout for hls notify, in us.
#define SRS_HLS_NOTIFY_TIMEOUT_US (int64_t)(10*1000*1000LL)
51 52 53 54 55 56 57 58
SrsHttpHooks::SrsHttpHooks()
{
}

SrsHttpHooks::~SrsHttpHooks()
{
}
59
int SrsHttpHooks::on_connect(string url, SrsRequest* req)
60 61 62
{
    int ret = ERROR_SUCCESS;
    
63 64
    int client_id = _srs_context->get_id();
    
65
    std::stringstream ss;
66 67 68
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_connect") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
69
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
70 71 72 73 74
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("tcUrl", req->tcUrl) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("pageUrl", req->pageUrl)
        << SRS_JOBJECT_END;
75
        
76 77
    std::string data = ss.str();
    std::string res;
78
    int status_code;
79
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
80
        srs_error("http post on_connect uri failed. "
81 82
            "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);
83 84 85 86 87 88 89 90 91 92
        return ret;
    }
    
    srs_trace("http hook on_connect success. "
        "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;
}
93
void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int64_t recv_bytes)
94 95 96
{
    int ret = ERROR_SUCCESS;
    
97 98
    int client_id = _srs_context->get_id();
    
99
    std::stringstream ss;
100 101 102
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_close") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
103
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
104 105 106 107 108
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("send_bytes", send_bytes) << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("recv_bytes", recv_bytes) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app)
        << SRS_JOBJECT_END;
109
        
110 111
    std::string data = ss.str();
    std::string res;
112
    int status_code;
113
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
114
        srs_warn("http post on_close uri failed, ignored. "
115 116
            "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);
117 118 119 120 121 122 123 124 125 126
        return;
    }
    
    srs_trace("http hook on_close success. "
        "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
        client_id, url.c_str(), data.c_str(), res.c_str(), ret);
    
    return;
}
127
int SrsHttpHooks::on_publish(string url, SrsRequest* req)
128 129 130
{
    int ret = ERROR_SUCCESS;
    
131 132
    int client_id = _srs_context->get_id();
    
133
    std::stringstream ss;
134 135 136
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_publish") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
137
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
138 139 140 141
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream)
        << SRS_JOBJECT_END;
142
        
143 144
    std::string data = ss.str();
    std::string res;
145
    int status_code;
146
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
147
        srs_error("http post on_publish uri failed. "
148 149
            "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);
150 151 152 153 154 155 156 157 158 159
        return ret;
    }
    
    srs_trace("http hook on_publish success. "
        "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;
}
160
void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
161 162 163
{
    int ret = ERROR_SUCCESS;
    
164 165
    int client_id = _srs_context->get_id();
    
166
    std::stringstream ss;
167 168 169
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_unpublish") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
170
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
171 172 173 174
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream)
        << SRS_JOBJECT_END;
175
        
176 177
    std::string data = ss.str();
    std::string res;
178
    int status_code;
179
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
180
        srs_warn("http post on_unpublish uri failed, ignored. "
181 182
            "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);
183 184 185 186 187 188 189 190 191 192
        return;
    }
    
    srs_trace("http hook on_unpublish success. "
        "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
        client_id, url.c_str(), data.c_str(), res.c_str(), ret);
    
    return;
}
193
int SrsHttpHooks::on_play(string url, SrsRequest* req)
194 195 196
{
    int ret = ERROR_SUCCESS;
    
197 198
    int client_id = _srs_context->get_id();
    
199
    std::stringstream ss;
200 201 202
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_play") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
203
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
204 205 206 207
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream)
        << SRS_JOBJECT_END;
208
        
209 210
    std::string data = ss.str();
    std::string res;
211
    int status_code;
212
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
213
        srs_error("http post on_play uri failed. "
214 215
            "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);
216 217 218 219 220 221 222 223 224 225
        return ret;
    }
    
    srs_trace("http hook on_play success. "
        "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;
}
226
void SrsHttpHooks::on_stop(string url, SrsRequest* req)
227 228 229
{
    int ret = ERROR_SUCCESS;
    
230 231
    int client_id = _srs_context->get_id();
    
232
    std::stringstream ss;
233 234 235
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_stop") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
236
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
237 238 239 240
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream)
        << SRS_JOBJECT_END;
241
        
242 243
    std::string data = ss.str();
    std::string res;
244
    int status_code;
245
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
246
        srs_warn("http post on_stop uri failed, ignored. "
247 248
            "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);
249 250 251 252 253 254 255 256 257 258
        return;
    }
    
    srs_trace("http hook on_stop success. "
        "client_id=%d, url=%s, request=%s, response=%s, ret=%d",
        client_id, url.c_str(), data.c_str(), res.c_str(), ret);
    
    return;
}
259
int SrsHttpHooks::on_dvr(string url, SrsRequest* req, string file)
260 261 262
{
    int ret = ERROR_SUCCESS;
    
263 264 265
    int client_id = _srs_context->get_id();
    std::string cwd = _srs_config->cwd();
    
266
    std::stringstream ss;
267 268 269
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_STR("action", "on_dvr") << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
270
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
271 272 273 274 275 276
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("cwd", cwd) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("file", file)
        << SRS_JOBJECT_END;
277
        
278 279 280
    std::string data = ss.str();
    std::string res;
    int status_code;
281
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
282
        srs_error("http post on_dvr uri failed, ignored. "
283 284
            "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);
285 286 287 288 289 290 291 292 293
        return ret;
    }
    
    srs_trace("http hook on_dvr success. "
        "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;
}
294
295
int SrsHttpHooks::on_hls(string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, double duration)
296 297 298
{
    int ret = ERROR_SUCCESS;
    
299 300 301
    int client_id = _srs_context->get_id();
    std::string cwd = _srs_config->cwd();
    
302
    std::stringstream ss;
303
    ss << SRS_JOBJECT_START
304
        << SRS_JFIELD_STR("action", "on_hls") << SRS_JFIELD_CONT
305
        << SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
306
        << SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
307 308 309
        << SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("stream", req->stream) << SRS_JFIELD_CONT
310
        << SRS_JFIELD_ORG("duration", duration) << SRS_JFIELD_CONT
311
        << SRS_JFIELD_STR("cwd", cwd) << SRS_JFIELD_CONT
312
        << SRS_JFIELD_STR("file", file) << SRS_JFIELD_CONT
313 314 315
        << SRS_JFIELD_STR("url", ts_url) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("m3u8", m3u8) << SRS_JFIELD_CONT
        << SRS_JFIELD_STR("m3u8_url", m3u8_url) << SRS_JFIELD_CONT
316
        << SRS_JFIELD_ORG("seq_no", sn)
317
        << SRS_JOBJECT_END;
318
        
319 320 321
    std::string data = ss.str();
    std::string res;
    int status_code;
322
    if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
323
        srs_error("http post on_hls uri failed, ignored. "
324 325
            "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);
326 327 328
        return ret;
    }
    
329
    srs_trace("http hook on_hls success. "
330 331 332 333 334 335
        "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;
}
336
int SrsHttpHooks::on_hls_notify(std::string url, SrsRequest* req, std::string ts_url, int nb_notify)
337 338 339 340 341 342 343 344 345 346 347 348 349 350
{
    int ret = ERROR_SUCCESS;
    
    int client_id = _srs_context->get_id();
    std::string cwd = _srs_config->cwd();
    
    if (srs_string_starts_with(ts_url, "http://") || srs_string_starts_with(ts_url, "https://")) {
        url = ts_url;
    }
    
    url = srs_string_replace(url, "[app]", req->app);
    url = srs_string_replace(url, "[stream]", req->stream);
    url = srs_string_replace(url, "[ts_url]", ts_url);
    
351 352
    int64_t starttime = srs_update_system_time_ms();
    
353 354 355 356 357 358 359
    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;
360
    if ((ret = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TIMEOUT_US)) != ERROR_SUCCESS) {
361 362 363
        return ret;
    }
    
364 365 366 367 368 369 370 371 372 373
    std::string path = uri.get_query();
    if (path.empty()) {
        path = uri.get_path();
    } else {
        path = uri.get_path();
        path += "?";
        path += uri.get_query();
    }
    srs_warn("GET %s", path.c_str());
    
374
    SrsHttpMessage* msg = NULL;
375
    if ((ret = http.get(path.c_str(), "", &msg)) != ERROR_SUCCESS) {
376 377 378 379
        return ret;
    }
    SrsAutoFree(SrsHttpMessage, msg);
    
380 381 382 383
    int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER);
    char* buf = new char[nb_buf];
    SrsAutoFree(char, buf);
    
384
    int nb_read = 0;
385
    ISrsHttpResponseReader* br = msg->body_reader();
386
    while (nb_read < nb_notify && !br->eof()) {
387 388
        int nb_bytes = 0;
        if ((ret = br->read(buf, nb_buf, &nb_bytes)) != ERROR_SUCCESS) {
389 390
            break;
        }
391
        nb_read += nb_bytes;
392 393
    }
    
394
    int spenttime = (int)(srs_update_system_time_ms() - starttime);
395 396
    srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, ret=%d",
        client_id, url.c_str(), msg->status_code(), spenttime, nb_read, ret);
397 398 399 400 401 402 403
    
    // ignore any error for on_hls_notify.
    ret = ERROR_SUCCESS;
    
    return ret;
}
404 405 406 407 408 409 410
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);
411 412 413
        return ret;
    }
    
414
    SrsHttpClient http;
415 416 417 418
    if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) {
        return ret;
    }
    
419
    SrsHttpMessage* msg = NULL;
420
    if ((ret = http.post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) {
421 422
        return ret;
    }
423
    SrsAutoFree(SrsHttpMessage, msg);
424
    
425 426 427 428
    code = msg->status_code();
    if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
        return ret;
    }
429
    
430
    // ensure the http status is ok.
431
    // https://github.com/simple-rtmp-server/srs/issues/158
432 433 434 435 436 437 438 439 440
    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;
    }
441 442 443
    return ret;
}
444
#endif