1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_http_hooks.cpp

540 lines
19 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-01-01 13:37:28 +00:00
* Copyright (c) 2013-2019 Winlin
2017-03-25 09:21:39 +00:00
*
* 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.
*/
2014-04-01 10:40:24 +00:00
#include <srs_app_http_hooks.hpp>
#include <sstream>
using namespace std;
#include <srs_kernel_error.hpp>
2015-06-13 08:04:59 +00:00
#include <srs_rtmp_stack.hpp>
2015-06-14 00:43:38 +00:00
#include <srs_app_st.hpp>
2015-08-21 08:20:19 +00:00
#include <srs_protocol_json.hpp>
#include <srs_app_dvr.hpp>
2014-05-19 09:39:01 +00:00
#include <srs_app_http_client.hpp>
2015-03-06 03:51:20 +00:00
#include <srs_core_autofree.hpp>
#include <srs_app_config.hpp>
#include <srs_kernel_utility.hpp>
2015-05-22 14:34:03 +00:00
#include <srs_app_http_conn.hpp>
2015-09-22 01:05:21 +00:00
#include <srs_protocol_amf0.hpp>
2015-12-11 03:29:10 +00:00
#include <srs_app_utility.hpp>
2014-04-01 10:40:24 +00:00
#define SRS_HTTP_RESPONSE_OK SRS_XSTR(ERROR_SUCCESS)
2014-04-01 10:40:24 +00:00
#define SRS_HTTP_HEADER_BUFFER 1024
#define SRS_HTTP_READ_BUFFER 4096
#define SRS_HTTP_BODY_BUFFER (32 * 1024)
2014-04-01 10:40:24 +00:00
2019-04-18 00:11:16 +00:00
// the timeout for hls notify, in srs_utime_t.
#define SRS_HLS_NOTIFY_TIMEOUT (10 * SRS_UTIME_SECONDS)
2015-04-10 05:45:21 +00:00
2014-04-01 10:40:24 +00:00
SrsHttpHooks::SrsHttpHooks()
{
}
SrsHttpHooks::~SrsHttpHooks()
{
}
srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_connect"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("tcUrl", SrsJsonAny::str(req->tcUrl.c_str()));
obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_connect failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
2014-04-01 10:40:24 +00:00
}
srs_trace("http: on_connect ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return err;
2014-04-01 10:40:24 +00:00
}
void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int64_t recv_bytes)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("action", SrsJsonAny::str("on_close"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("send_bytes", SrsJsonAny::integer(send_bytes));
obj->set("recv_bytes", SrsJsonAny::integer(recv_bytes));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
int ret = srs_error_code(err);
srs_freep(err);
srs_warn("http: ignore on_close failed, 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);
2014-04-01 10:40:24 +00:00
return;
}
srs_trace("http: on_close ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return;
}
srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("action", SrsJsonAny::str("on_publish"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
2016-01-09 08:06:22 +00:00
obj->set("tcUrl", SrsJsonAny::str(req->tcUrl.c_str()));
2015-09-19 04:27:31 +00:00
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_publish failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
2014-04-01 10:40:24 +00:00
}
srs_trace("http: on_publish ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return err;
2014-04-01 10:40:24 +00:00
}
void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("action", SrsJsonAny::str("on_unpublish"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
int ret = srs_error_code(err);
srs_freep(err);
srs_warn("http: ignore on_unpublish failed, client_id=%d, url=%s, request=%s, response=%s, status=%d, ret=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
2014-04-01 10:40:24 +00:00
return;
}
srs_trace("http: on_unpublish ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return;
}
srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("action", SrsJsonAny::str("on_play"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2015-09-19 04:27:31 +00:00
obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_play failed, client_id=%d, url=%s, request=%s, response=%s, status=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
2014-04-01 10:40:24 +00:00
}
srs_trace("http: on_play ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return err;
2014-04-01 10:40:24 +00:00
}
void SrsHttpHooks::on_stop(string url, SrsRequest* req)
2014-04-01 10:40:24 +00:00
{
srs_error_t err = srs_success;
2014-04-01 10:40:24 +00:00
int client_id = _srs_context->get_id();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("action", SrsJsonAny::str("on_stop"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
2014-04-01 10:40:24 +00:00
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
int ret = srs_error_code(err);
srs_freep(err);
srs_warn("http: ignore on_stop failed, 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);
2014-04-01 10:40:24 +00:00
return;
}
srs_trace("http: on_stop ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2014-04-01 10:40:24 +00:00
return;
}
srs_error_t SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file)
{
srs_error_t err = srs_success;
int client_id = cid;
std::string cwd = _srs_config->cwd();
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_dvr"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2015-09-19 04:27:31 +00:00
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
obj->set("file", SrsJsonAny::str(file.c_str()));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http post on_dvr uri failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
}
srs_trace("http hook on_dvr success. client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
return err;
}
2014-08-02 14:18:39 +00:00
2019-04-14 23:53:29 +00:00
srs_error_t SrsHttpHooks::on_hls(int cid, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, srs_utime_t duration)
{
srs_error_t err = srs_success;
int client_id = cid;
std::string cwd = _srs_config->cwd();
2015-12-11 03:29:10 +00:00
// the ts_url is under the same dir of m3u8_url.
string prefix = srs_path_dirname(m3u8_url);
if (!prefix.empty() && !srs_string_is_http(ts_url)) {
ts_url = prefix + "/" + ts_url;
}
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_hls"));
obj->set("client_id", SrsJsonAny::integer(client_id));
2015-09-19 04:27:31 +00:00
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str()));
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
obj->set("param", SrsJsonAny::str(req->param.c_str()));
2019-04-14 23:53:29 +00:00
obj->set("duration", SrsJsonAny::number(srsu2ms(duration)/1000.0));
2015-09-19 04:27:31 +00:00
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
obj->set("file", SrsJsonAny::str(file.c_str()));
2015-12-29 10:33:02 +00:00
obj->set("url", SrsJsonAny::str(ts_url.c_str()));
2015-09-19 04:27:31 +00:00
obj->set("m3u8", SrsJsonAny::str(m3u8.c_str()));
obj->set("m3u8_url", SrsJsonAny::str(m3u8_url.c_str()));
obj->set("seq_no", SrsJsonAny::integer(sn));
2017-03-25 09:21:39 +00:00
2015-09-19 05:37:56 +00:00
std::string data = obj->dumps();
std::string res;
int status_code;
2015-09-17 03:37:35 +00:00
SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: post %s with %s, status=%d, res=%s", url.c_str(), data.c_str(), status_code, res.c_str());
}
srs_trace("http: on_hls ok, client_id=%d, url=%s, request=%s, response=%s",
client_id, url.c_str(), data.c_str(), res.c_str());
2015-03-06 03:51:20 +00:00
return err;
2015-03-06 03:51:20 +00:00
}
srs_error_t SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify)
{
srs_error_t err = srs_success;
int client_id = cid;
std::string cwd = _srs_config->cwd();
2015-12-11 03:29:10 +00:00
if (srs_string_is_http(ts_url)) {
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);
url = srs_string_replace(url, "[param]", req->param);
int64_t starttime = srsu2ms(srs_update_system_time());
SrsHttpUri uri;
2018-01-01 11:39:57 +00:00
if ((err = uri.initialize(url)) != srs_success) {
return srs_error_wrap(err, "http: init url=%s", url.c_str());
}
SrsHttpClient http;
if ((err = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TIMEOUT)) != srs_success) {
return srs_error_wrap(err, "http: init client for %s", url.c_str());
}
std::string path = uri.get_query();
if (path.empty()) {
path = uri.get_path();
} else {
path = uri.get_path();
path += "?";
path += uri.get_query();
}
srs_info("GET %s", path.c_str());
ISrsHttpMessage* msg = NULL;
if ((err = http.get(path.c_str(), "", &msg)) != srs_success) {
return srs_error_wrap(err, "http: get %s", url.c_str());
}
SrsAutoFree(ISrsHttpMessage, msg);
2015-04-10 07:00:07 +00:00
int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER);
char* buf = new char[nb_buf];
SrsAutoFreeA(char, buf);
2015-04-10 07:00:07 +00:00
int nb_read = 0;
ISrsHttpResponseReader* br = msg->body_reader();
2015-04-10 06:44:18 +00:00
while (nb_read < nb_notify && !br->eof()) {
ssize_t nb_bytes = 0;
2018-01-01 11:39:57 +00:00
if ((err = br->read(buf, nb_buf, &nb_bytes)) != srs_success) {
2015-04-10 06:44:18 +00:00
break;
}
nb_read += (int)nb_bytes;
}
int spenttime = (int)(srsu2ms(srs_update_system_time()) - starttime);
2018-01-01 11:39:57 +00:00
srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, err=%s",
client_id, url.c_str(), msg->status_code(), spenttime, nb_read, srs_error_desc(err).c_str());
// ignore any error for on_hls_notify.
2018-01-01 11:39:57 +00:00
srs_error_reset(err);
return srs_success;
}
2018-02-16 08:39:07 +00:00
srs_error_t SrsHttpHooks::discover_co_workers(string url, string& host, int& port)
{
srs_error_t err = srs_success;
std::string res;
int status_code;
SrsHttpClient http;
if ((err = do_post(&http, url, "", status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: post %s, status=%d, res=%s", url.c_str(), status_code, res.c_str());
}
SrsJsonObject* robj = NULL;
SrsAutoFree(SrsJsonObject, robj);
if (true) {
SrsJsonAny* jr = NULL;
if ((jr = SrsJsonAny::loads(res)) == NULL) {
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "load json from %s", res.c_str());
}
if (!jr->is_object()) {
srs_freep(jr);
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "response %s", res.c_str());
}
robj = jr->to_object();
}
SrsJsonAny* prop = NULL;
if ((prop = robj->ensure_property_object("data")) == NULL) {
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
}
SrsJsonObject* p = prop->to_object();
if ((prop = p->ensure_property_object("origin")) == NULL) {
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
}
p = prop->to_object();
if ((prop = p->ensure_property_string("ip")) == NULL) {
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
}
host = prop->to_str();
if ((prop = p->ensure_property_integer("port")) == NULL) {
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
}
port = (int)prop->to_integer();
2019-10-03 22:51:15 +00:00
srs_trace("http: cluster redirect %s:%d ok, url=%s, response=%s", host.c_str(), port, url.c_str(), res.c_str());
2018-02-16 08:39:07 +00:00
return err;
}
srs_error_t SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, string& res)
2015-03-06 03:51:20 +00:00
{
srs_error_t err = srs_success;
2015-03-06 03:51:20 +00:00
SrsHttpUri uri;
2018-01-01 11:39:57 +00:00
if ((err = uri.initialize(url)) != srs_success) {
return srs_error_wrap(err, "http: post failed. url=%s", url.c_str());
}
if ((err = hc->initialize(uri.get_host(), uri.get_port())) != srs_success) {
return srs_error_wrap(err, "http: init client");
}
2018-02-16 08:39:07 +00:00
string path = uri.get_path();
if (!uri.get_query().empty()) {
path += "?" + uri.get_query();
}
ISrsHttpMessage* msg = NULL;
2018-02-16 08:39:07 +00:00
if ((err = hc->post(path, req, &msg)) != srs_success) {
return srs_error_wrap(err, "http: client post");
}
SrsAutoFree(ISrsHttpMessage, msg);
2015-03-06 03:51:20 +00:00
code = msg->status_code();
2018-01-01 11:39:57 +00:00
if ((err = msg->body_read_all(res)) != srs_success) {
return srs_error_wrap(err, "http: body read");
2015-03-06 03:51:20 +00:00
}
2015-03-06 03:51:20 +00:00
// ensure the http status is ok.
2015-11-11 02:37:50 +00:00
// https://github.com/ossrs/srs/issues/158
2015-12-29 10:33:02 +00:00
if (code != SRS_CONSTS_HTTP_OK && code != SRS_CONSTS_HTTP_Created) {
return srs_error_new(ERROR_HTTP_STATUS_INVALID, "http: status %d", code);
2015-03-06 03:51:20 +00:00
}
// should never be empty.
if (res.empty()) {
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: empty response");
2015-03-06 03:51:20 +00:00
}
// parse string res to json.
SrsJsonAny* info = SrsJsonAny::loads(res);
if (!info) {
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: not json %s", res.c_str());
}
SrsAutoFree(SrsJsonAny, info);
// response error code in string.
if (!info->is_object()) {
if (res == SRS_HTTP_RESPONSE_OK) {
return err;
}
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: response number code %s", res.c_str());
}
// response standard object, format in json: {"code": 0, "data": ""}
SrsJsonObject* res_info = info->to_object();
SrsJsonAny* res_code = NULL;
if ((res_code = res_info->ensure_property_integer("code")) == NULL) {
return srs_error_new(ERROR_RESPONSE_CODE, "http: response object no code %s", res.c_str());
}
2015-09-17 05:36:02 +00:00
if ((res_code->to_integer()) != ERROR_SUCCESS) {
return srs_error_new(ERROR_RESPONSE_CODE, "http: response object code %d %s", res_code->to_integer(), res.c_str());
}
2015-09-17 05:36:02 +00:00
return err;
}