Skip to content

Commit 572fb7d

Browse files
committed
refine ENV config SRS_OVERWRITE_BY_ENV_DIRECTIVE
1. don't use static variable to store the result; 2. add more UT to handle the multi value and values with whitespaces;
1 parent 16e569d commit 572fb7d

File tree

2 files changed

+138
-11
lines changed

2 files changed

+138
-11
lines changed

trunk/src/app/srs_app_config.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ const char* _srs_version = "XCORE-" RTMP_SIG_SRS_SERVER;
6969
#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)
7070
#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)
7171
#define SRS_OVERWRITE_BY_ENV_DIRECTIVE(key) { \
72-
static SrsConfDirective* dir = NULL; \
73-
if (!dir && !srs_getenv(key).empty()) { \
72+
if (!srs_getenv(key).empty()) { \
7473
std::vector<string> vec = srs_string_split(srs_getenv(key), " "); \
75-
dir = new SrsConfDirective(); \
74+
SrsConfDirective* dir = new SrsConfDirective(); \
7675
dir->name = key; \
7776
for (size_t i = 0; i < vec.size(); ++i) { \
78-
dir->args.push_back(vec[i]); \
77+
if (!vec[i].empty()) { \
78+
dir->args.push_back(vec[i]); \
79+
} \
7980
} \
81+
return dir; \
8082
} \
81-
if (dir) return dir; \
8283
}
8384

8485
/**

trunk/src/utest/srs_utest_config.cpp

+132-6
Original file line numberDiff line numberDiff line change
@@ -5059,12 +5059,11 @@ VOID TEST(ConfigEnvTest, CheckEnvValuesHooks)
50595059
}
50605060

50615061
if (true) {
5062-
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CONNECT", "http://server/api/connect https://server2/api/connect2");
5062+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CONNECT", "http://server/api/connect");
50635063
SrsConfDirective* dir = conf.get_vhost_on_connect("__defaultVhost__");
50645064
ASSERT_TRUE(dir != NULL);
5065-
ASSERT_EQ((int)dir->args.size(), 2);
5065+
ASSERT_EQ((int)dir->args.size(), 1);
50665066
ASSERT_STREQ("http://server/api/connect", dir->arg0().c_str());
5067-
ASSERT_STREQ("https://server2/api/connect2", dir->arg1().c_str());
50685067
}
50695068

50705069
if (true) {
@@ -5076,12 +5075,11 @@ VOID TEST(ConfigEnvTest, CheckEnvValuesHooks)
50765075
}
50775076

50785077
if (true) {
5079-
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish http://server/api/publish2");
5078+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish");
50805079
SrsConfDirective* dir = conf.get_vhost_on_publish("__defaultVhost__");
50815080
ASSERT_TRUE(dir != NULL);
5082-
ASSERT_EQ((int)dir->args.size(), 2);
5081+
ASSERT_EQ((int)dir->args.size(), 1);
50835082
ASSERT_STREQ("http://server/api/publish", dir->arg0().c_str());
5084-
ASSERT_STREQ("http://server/api/publish2", dir->arg1().c_str());
50855083
}
50865084

50875085
if (true) {
@@ -5132,3 +5130,131 @@ VOID TEST(ConfigEnvTest, CheckEnvValuesHooks)
51325130
ASSERT_STREQ("http://server/api/hls_notify", dir->arg0().c_str());
51335131
}
51345132
}
5133+
5134+
VOID TEST(ConfigEnvTest, CheckEnvValuesHooksMultiValues)
5135+
{
5136+
MockSrsConfig conf;
5137+
5138+
if (true) {
5139+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CONNECT", "http://server/api/connect https://server2/api/connect2");
5140+
5141+
SrsConfDirective* dir = conf.get_vhost_on_connect("__defaultVhost__");
5142+
ASSERT_TRUE(dir != NULL);
5143+
ASSERT_EQ((int)dir->args.size(), 2);
5144+
ASSERT_STREQ("http://server/api/connect", dir->arg0().c_str());
5145+
ASSERT_STREQ("https://server2/api/connect2", dir->arg1().c_str());
5146+
}
5147+
5148+
if (true) {
5149+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_CLOSE", "http://server/api/close close2 close3");
5150+
SrsConfDirective* dir = conf.get_vhost_on_close("__defaultVhost__");
5151+
ASSERT_TRUE(dir != NULL);
5152+
ASSERT_TRUE((int)dir->args.size() == 3);
5153+
ASSERT_STREQ("http://server/api/close", dir->arg0().c_str());
5154+
ASSERT_STREQ("close2", dir->arg1().c_str());
5155+
ASSERT_STREQ("close3", dir->arg2().c_str());
5156+
}
5157+
5158+
if (true) {
5159+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish http://server/api/publish2");
5160+
SrsConfDirective* dir = conf.get_vhost_on_publish("__defaultVhost__");
5161+
ASSERT_TRUE(dir != NULL);
5162+
ASSERT_EQ((int)dir->args.size(), 2);
5163+
ASSERT_STREQ("http://server/api/publish", dir->arg0().c_str());
5164+
ASSERT_STREQ("http://server/api/publish2", dir->arg1().c_str());
5165+
}
5166+
5167+
if (true) {
5168+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_UNPUBLISH", "http://server/api/unpublish 2");
5169+
SrsConfDirective* dir = conf.get_vhost_on_unpublish("__defaultVhost__");
5170+
ASSERT_TRUE(dir != NULL);
5171+
ASSERT_TRUE((int)dir->args.size() == 2);
5172+
ASSERT_STREQ("http://server/api/unpublish", dir->arg0().c_str());
5173+
ASSERT_STREQ("2", dir->arg1().c_str());
5174+
}
5175+
5176+
if (true) {
5177+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PLAY", "http://server/api/play http://server/api/play2");
5178+
SrsConfDirective* dir = conf.get_vhost_on_play("__defaultVhost__");
5179+
ASSERT_TRUE(dir != NULL);
5180+
ASSERT_TRUE((int)dir->args.size() == 2);
5181+
ASSERT_STREQ("http://server/api/play", dir->arg0().c_str());
5182+
ASSERT_STREQ("http://server/api/play2", dir->arg1().c_str());
5183+
}
5184+
5185+
if (true) {
5186+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_STOP", "http://server/api/stop http://server/api/stop2");
5187+
SrsConfDirective* dir = conf.get_vhost_on_stop("__defaultVhost__");
5188+
ASSERT_TRUE(dir != NULL);
5189+
ASSERT_TRUE((int)dir->args.size() == 2);
5190+
ASSERT_STREQ("http://server/api/stop", dir->arg0().c_str());
5191+
ASSERT_STREQ("http://server/api/stop2", dir->arg1().c_str());
5192+
}
5193+
5194+
if (true) {
5195+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_DVR", "http://server/api/dvr http://server/api/dvr2");
5196+
SrsConfDirective* dir = conf.get_vhost_on_dvr("__defaultVhost__");
5197+
ASSERT_TRUE(dir != NULL);
5198+
ASSERT_TRUE((int)dir->args.size() == 2);
5199+
ASSERT_STREQ("http://server/api/dvr", dir->arg0().c_str());
5200+
ASSERT_STREQ("http://server/api/dvr2", dir->arg1().c_str());
5201+
}
5202+
5203+
if (true) {
5204+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_HLS", "http://server/api/hls http://server/api/hls2");
5205+
SrsConfDirective* dir = conf.get_vhost_on_hls("__defaultVhost__");
5206+
ASSERT_TRUE(dir != NULL);
5207+
ASSERT_TRUE((int)dir->args.size() == 2);
5208+
ASSERT_STREQ("http://server/api/hls", dir->arg0().c_str());
5209+
ASSERT_STREQ("http://server/api/hls2", dir->arg1().c_str());
5210+
}
5211+
5212+
if (true) {
5213+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_HLS_NOTIFY", "http://server/api/hls_notify http://server/api/hls_notify2");
5214+
SrsConfDirective* dir = conf.get_vhost_on_hls_notify("__defaultVhost__");
5215+
ASSERT_TRUE(dir != NULL);
5216+
ASSERT_TRUE((int)dir->args.size() == 2);
5217+
ASSERT_STREQ("http://server/api/hls_notify", dir->arg0().c_str());
5218+
ASSERT_STREQ("http://server/api/hls_notify2", dir->arg1().c_str());
5219+
}
5220+
}
5221+
5222+
VOID TEST(ConfigEnvTest, CheckEnvValuesHooksWithWhitespaces)
5223+
{
5224+
MockSrsConfig conf;
5225+
5226+
if (true) {
5227+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PUBLISH", "http://server/api/publish http://server/api/publish2");
5228+
SrsConfDirective* dir = conf.get_vhost_on_publish("__defaultVhost__");
5229+
ASSERT_TRUE(dir != NULL);
5230+
ASSERT_EQ((int)dir->args.size(), 2);
5231+
ASSERT_STREQ("http://server/api/publish", dir->arg0().c_str());
5232+
ASSERT_STREQ("http://server/api/publish2", dir->arg1().c_str());
5233+
}
5234+
5235+
if (true) {
5236+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_UNPUBLISH", "http://server/api/unpublish ");
5237+
SrsConfDirective* dir = conf.get_vhost_on_unpublish("__defaultVhost__");
5238+
ASSERT_TRUE(dir != NULL);
5239+
ASSERT_TRUE((int)dir->args.size() == 1);
5240+
ASSERT_STREQ("http://server/api/unpublish", dir->arg0().c_str());
5241+
}
5242+
5243+
if (true) {
5244+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_UNPUBLISH", " unpublish");
5245+
SrsConfDirective* dir = conf.get_vhost_on_unpublish("__defaultVhost__");
5246+
ASSERT_TRUE(dir != NULL);
5247+
ASSERT_TRUE((int)dir->args.size() == 1);
5248+
ASSERT_STREQ("unpublish", dir->arg0().c_str());
5249+
}
5250+
5251+
if (true) {
5252+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_PLAY", " http://server/api/play play2 play3 ");
5253+
SrsConfDirective* dir = conf.get_vhost_on_play("__defaultVhost__");
5254+
ASSERT_TRUE(dir != NULL);
5255+
ASSERT_TRUE((int)dir->args.size() == 3);
5256+
ASSERT_STREQ("http://server/api/play", dir->arg0().c_str());
5257+
ASSERT_STREQ("play2", dir->arg1().c_str());
5258+
ASSERT_STREQ("play3", dir->arg2().c_str());
5259+
}
5260+
}

0 commit comments

Comments
 (0)