Skip to content

Commit 26f6368

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 26f6368

File tree

3 files changed

+145
-8
lines changed

3 files changed

+145
-8
lines changed

trunk/src/app/srs_app_config.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +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; \
72+
SrsConfDirective* dir = env_dirs->get(key); \
7373
if (!dir && !srs_getenv(key).empty()) { \
7474
std::vector<string> vec = srs_string_split(srs_getenv(key), " "); \
7575
dir = new SrsConfDirective(); \
7676
dir->name = key; \
7777
for (size_t i = 0; i < vec.size(); ++i) { \
78-
dir->args.push_back(vec[i]); \
78+
if (!vec[i].empty()) { \
79+
dir->args.push_back(vec[i]); \
80+
} \
7981
} \
82+
env_dirs->directives.push_back(dir); \
8083
} \
8184
if (dir) return dir; \
8285
}
@@ -1345,11 +1348,15 @@ SrsConfig::SrsConfig()
13451348
root = new SrsConfDirective();
13461349
root->conf_line = 0;
13471350
root->name = "root";
1351+
1352+
env_dirs = new SrsConfDirective();
1353+
env_dirs->name = "env";
13481354
}
13491355

13501356
SrsConfig::~SrsConfig()
13511357
{
13521358
srs_freep(root);
1359+
srs_freep(env_dirs);
13531360
}
13541361

13551362
void SrsConfig::subscribe(ISrsReloadHandler* handler)

trunk/src/app/srs_app_config.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ class SrsConfig
305305
protected:
306306
// The directive root.
307307
SrsConfDirective* root;
308+
309+
// The Env directives.
310+
SrsConfDirective* env_dirs;
308311
// Reload section
309312
private:
310313
// The reload subscribers, when reload, callback all handlers.

trunk/src/utest/srs_utest_config.cpp

+133-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,132 @@ 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_PLAY", " http://server/api/play play2 play3 ");
5245+
SrsConfDirective* dir = conf.get_vhost_on_play("__defaultVhost__");
5246+
ASSERT_TRUE(dir != NULL);
5247+
ASSERT_TRUE((int)dir->args.size() == 3);
5248+
ASSERT_STREQ("http://server/api/play", dir->arg0().c_str());
5249+
ASSERT_STREQ("play2", dir->arg1().c_str());
5250+
ASSERT_STREQ("play3", dir->arg2().c_str());
5251+
}
5252+
5253+
if (true) {
5254+
SrsSetEnvConfig(hooks, "SRS_VHOST_HTTP_HOOKS_ON_DVR", " dvr");
5255+
SrsConfDirective* dir = conf.get_vhost_on_dvr("__defaultVhost__");
5256+
ASSERT_TRUE(dir != NULL);
5257+
ASSERT_TRUE((int)dir->args.size() == 1);
5258+
ASSERT_STREQ("dvr", dir->arg0().c_str());
5259+
}
5260+
5261+
}

0 commit comments

Comments
 (0)