Skip to content

Commit

Permalink
fix http_hooks env don't support multi values;
Browse files Browse the repository at this point in the history
1. add on_connect & on_close directives to conf/full.conf;
2. let http_hooks env overwrite support multi values;
   e.g. SRS_VHOST_HTTP_HOOKS_ON_CONNECT="http://127.0.0.1/api/connect
   http://localhost/api/connect"
  • Loading branch information
suzp1984 committed Jun 16, 2024
1 parent e3d74fb commit 5ba500c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
38 changes: 38 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,44 @@ vhost hooks.callback.srs.com {
# Overwrite by env SRS_VHOST_HTTP_HOOKS_ENABLED for all vhosts.
# default off.
enabled on;
# when client(encoder) connect to vhost/app/stream, call the hook,
# the request in the POST data string is a object encode by json:
# {
# "action": "on_connect",
# "client_id": "9308h583",
# "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
# "stream": "livestream", "param":"?token=xxx&salt=yyy", "server_id": "vid-werty",
# "tcUrl": "rtmp://127.0.0.1:1935/live", "pageUrl": "http://www.test.com/live.html"
# }
# if valid, the hook must return HTTP code 200(Status OK) and response
# an int value specifies the error code(0 corresponding to success):
# 0
# support multiple api hooks, format:
# on_connect http://xxx/api0 http://xxx/api1 http://xxx/apiN
# @remark For SRS4, the HTTPS url is supported, for example:
# on_connect https://xxx/api0 https://xxx/api1 https://xxx/apiN
# Overwrite by env SRS_VHOST_HTTP_HOOKS_ON_CONNECT for all vhosts.
on_connect http://127.0.0.1:8085/api/v1/connect http://localhost:8085/api/v1/connect;
# when client(encoder) close the connection to server, call the hook,
# the request in the POST data string is a object encode by json:
# {
# "action": "on_close",
# "client_id": "9308h583",
# "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
# "server_id": "vid-werty",
# "stream_id": "vid-124q9y3",
# "send_bytes": 4096,
# "recv_bytes": 1028
# }
# if valid, the hook must return HTTP code 200(Status OK) and response
# an int value specifies the error code(0 corresponding to success):
# 0
# support multiple api hooks, format:
# on_close http://xxx/api0 http://xxx/api1 http://xxx/apiN
# @remark For SRS4, the HTTPS url is supported, for example:
# on_close https://xxx/api0 https://xxx/api1 https://xxx/apiN
# Overwrite by env SRS_VHOST_HTTP_HOOKS_ON_CLOSE for all vhosts.
on_close http://127.0.0.1:8085/api/v1/close http://localhost:8085/api/v1/close;
# when client(encoder) publish to vhost/app/stream, call the hook,
# the request in the POST data string is a object encode by json:
# {
Expand Down
21 changes: 12 additions & 9 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ const char* _srs_version = "XCORE-" RTMP_SIG_SRS_SERVER;
#define SRS_OVERWRITE_BY_ENV_MILLISECONDS(key) if (!srs_getenv(key).empty()) return (srs_utime_t)(::atoi(srs_getenv(key).c_str()) * SRS_UTIME_MILLISECONDS)
#define SRS_OVERWRITE_BY_ENV_FLOAT_SECONDS(key) if (!srs_getenv(key).empty()) return srs_utime_t(::atof(srs_getenv(key).c_str()) * SRS_UTIME_SECONDS)
#define SRS_OVERWRITE_BY_ENV_FLOAT_MILLISECONDS(key) if (!srs_getenv(key).empty()) return srs_utime_t(::atof(srs_getenv(key).c_str()) * SRS_UTIME_MILLISECONDS)
#define SRS_OVERWRITE_BY_ENV_DIRECTIVE(key) { \
static SrsConfDirective* dir = NULL; \
if (!dir && !srs_getenv(key).empty()) { \
string v = srs_getenv(key); \
dir = new SrsConfDirective(); \
dir->name = key; \
dir->args.push_back(v); \
} \
if (dir) return dir; \
#define SRS_OVERWRITE_BY_ENV_DIRECTIVE(key) { \
static SrsConfDirective* dir = NULL; \
if (!dir && !srs_getenv(key).empty()) { \
std::vector<string> vec = srs_string_split(srs_getenv(key), " "); \
std::string v = srs_getenv(key); \
dir = new SrsConfDirective(); \
dir->name = key; \
for (size_t i = 0; i < vec.size(); ++i) { \
dir->args.push_back(vec[i]); \
} \
} \
if (dir) return dir; \
}

/**
Expand Down
38 changes: 34 additions & 4 deletions trunk/src/utest/srs_utest_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3281,7 +3281,20 @@ VOID TEST(ConfigMainTest, CheckVhostConfig3)
if (true) {
MockSrsConfig conf;
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "vhost ossrs.net{http_hooks{on_connect xxx;}}"));
EXPECT_TRUE(conf.get_vhost_on_connect("ossrs.net") != NULL);
SrsConfDirective* dir = conf.get_vhost_on_connect("ossrs.net");
ASSERT_TRUE(dir != NULL);
ASSERT_EQ((int)dir->args.size(), 1);
ASSERT_STREQ("xxx", dir->arg0().c_str());
}

if (true) {
MockSrsConfig conf;
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "vhost ossrs.net{http_hooks{on_connect xxx yyy;}}"));
SrsConfDirective* dir = conf.get_vhost_on_connect("ossrs.net");
ASSERT_TRUE(dir != NULL);
ASSERT_EQ((int)dir->args.size(), 2);
ASSERT_STREQ("xxx", dir->arg0().c_str());
ASSERT_STREQ("yyy", dir->arg1().c_str());
}

if (true) {
Expand Down Expand Up @@ -4958,11 +4971,29 @@ VOID TEST(ConfigEnvTest, CheckEnvValuesHooks)
}

if (true) {
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish");
SrsConfDirective* dir = conf.get_vhost_on_publish("__defaultVhost__");
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CONNECT", "http://server/api/connect https://server2/api/connect2");
SrsConfDirective* dir = conf.get_vhost_on_connect("__defaultVhost__");
ASSERT_TRUE(dir != NULL);
ASSERT_EQ((int)dir->args.size(), 2);
ASSERT_STREQ("http://server/api/connect", dir->arg0().c_str());
ASSERT_STREQ("https://server2/api/connect2", dir->arg1().c_str());
}

if (true) {
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CLOSE", "http://server/api/close");
SrsConfDirective* dir = conf.get_vhost_on_close("__defaultVhost__");
ASSERT_TRUE(dir != NULL);
ASSERT_TRUE((int)dir->args.size() == 1);
ASSERT_STREQ("http://server/api/close", dir->arg0().c_str());
}

if (true) {
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish http://server/api/publish2");
SrsConfDirective* dir = conf.get_vhost_on_publish("__defaultVhost__");
ASSERT_TRUE(dir != NULL);
ASSERT_EQ((int)dir->args.size(), 2);
ASSERT_STREQ("http://server/api/publish", dir->arg0().c_str());
ASSERT_STREQ("http://server/api/publish2", dir->arg1().c_str());
}

if (true) {
Expand Down Expand Up @@ -5013,4 +5044,3 @@ VOID TEST(ConfigEnvTest, CheckEnvValuesHooks)
ASSERT_STREQ("http://server/api/hls_notify", dir->arg0().c_str());
}
}

0 comments on commit 5ba500c

Please sign in to comment.