winlin

for #319, raw api support add new vhost

@@ -200,20 +200,32 @@ SrsConfDirective* SrsConfDirective::get_or_create(string n) @@ -200,20 +200,32 @@ SrsConfDirective* SrsConfDirective::get_or_create(string n)
200 200
201 SrsConfDirective* SrsConfDirective::get_or_create(string n, string a0) 201 SrsConfDirective* SrsConfDirective::get_or_create(string n, string a0)
202 { 202 {
203 - SrsConfDirective* conf = get_or_create(n); 203 + SrsConfDirective* conf = get(n, a0);
204 204
205 - if (conf->arg0() == a0) {  
206 - return conf; 205 + if (!conf) {
  206 + conf = new SrsConfDirective();
  207 + conf->name = n;
  208 + conf->set_arg0(a0);
  209 + directives.push_back(conf);
  210 + }
  211 +
  212 + return conf;
  213 +}
  214 +
  215 +SrsConfDirective* SrsConfDirective::set_arg0(string a0)
  216 +{
  217 + if (arg0() == a0) {
  218 + return this;
207 } 219 }
208 220
209 // update a0. 221 // update a0.
210 - if (!conf->args.empty()) {  
211 - conf->args.erase(conf->args.begin()); 222 + if (!args.empty()) {
  223 + args.erase(args.begin());
212 } 224 }
213 225
214 - conf->args.insert(conf->args.begin(), a0); 226 + args.insert(args.begin(), a0);
215 227
216 - return conf; 228 + return this;
217 } 229 }
218 230
219 bool SrsConfDirective::is_vhost() 231 bool SrsConfDirective::is_vhost()
@@ -693,17 +705,9 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root) @@ -693,17 +705,9 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root)
693 705
694 // DISABLED => ENABLED 706 // DISABLED => ENABLED
695 if (!get_vhost_enabled(old_vhost) && get_vhost_enabled(new_vhost)) { 707 if (!get_vhost_enabled(old_vhost) && get_vhost_enabled(new_vhost)) {
696 - srs_trace("vhost %s added, reload it.", vhost.c_str());  
697 - for (it = subscribes.begin(); it != subscribes.end(); ++it) {  
698 - ISrsReloadHandler* subscribe = *it;  
699 - if ((ret = subscribe->on_reload_vhost_added(vhost)) != ERROR_SUCCESS) {  
700 - srs_error("notify subscribes added "  
701 - "vhost %s failed. ret=%d", vhost.c_str(), ret);  
702 - return ret;  
703 - } 708 + if ((ret = do_reload_vhost_added(vhost)) != ERROR_SUCCESS) {
  709 + return ret;
704 } 710 }
705 -  
706 - srs_trace("reload new vhost %s success.", vhost.c_str());  
707 continue; 711 continue;
708 } 712 }
709 713
@@ -2448,6 +2452,25 @@ int SrsConfig::raw_set_pithy_print_ms(string pithy_print_ms, bool& applied) @@ -2448,6 +2452,25 @@ int SrsConfig::raw_set_pithy_print_ms(string pithy_print_ms, bool& applied)
2448 return ret; 2452 return ret;
2449 } 2453 }
2450 2454
  2455 +int SrsConfig::raw_create_vhost(string vhost, bool& applied)
  2456 +{
  2457 + int ret = ERROR_SUCCESS;
  2458 +
  2459 + applied = false;
  2460 +
  2461 +
  2462 + SrsConfDirective* conf = root->get_or_create("vhost", vhost);
  2463 + conf->get_or_create("enabled")->set_arg0("on");
  2464 +
  2465 + if ((ret = do_reload_vhost_added(vhost)) != ERROR_SUCCESS) {
  2466 + return ret;
  2467 + }
  2468 +
  2469 + applied = true;
  2470 +
  2471 + return ret;
  2472 +}
  2473 +
2451 int SrsConfig::do_reload_listen() 2474 int SrsConfig::do_reload_listen()
2452 { 2475 {
2453 int ret = ERROR_SUCCESS; 2476 int ret = ERROR_SUCCESS;
@@ -2584,6 +2607,26 @@ int SrsConfig::do_reload_pithy_print_ms() @@ -2584,6 +2607,26 @@ int SrsConfig::do_reload_pithy_print_ms()
2584 return ret; 2607 return ret;
2585 } 2608 }
2586 2609
  2610 +int SrsConfig::do_reload_vhost_added(string vhost)
  2611 +{
  2612 + int ret = ERROR_SUCCESS;
  2613 +
  2614 + srs_trace("vhost %s added, reload it.", vhost.c_str());
  2615 +
  2616 + vector<ISrsReloadHandler*>::iterator it;
  2617 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  2618 + ISrsReloadHandler* subscribe = *it;
  2619 + if ((ret = subscribe->on_reload_vhost_added(vhost)) != ERROR_SUCCESS) {
  2620 + srs_error("notify subscribes added vhost %s failed. ret=%d", vhost.c_str(), ret);
  2621 + return ret;
  2622 + }
  2623 + }
  2624 +
  2625 + srs_trace("reload new vhost %s success.", vhost.c_str());
  2626 +
  2627 + return ret;
  2628 +}
  2629 +
2587 string SrsConfig::config() 2630 string SrsConfig::config()
2588 { 2631 {
2589 return config_file; 2632 return config_file;
@@ -3491,7 +3534,7 @@ int SrsConfig::get_stream_caster_rtp_port_max(SrsConfDirective* conf) @@ -3491,7 +3534,7 @@ int SrsConfig::get_stream_caster_rtp_port_max(SrsConfDirective* conf)
3491 return ::atoi(conf->arg0().c_str()); 3534 return ::atoi(conf->arg0().c_str());
3492 } 3535 }
3493 3536
3494 -SrsConfDirective* SrsConfig::get_vhost(string vhost) 3537 +SrsConfDirective* SrsConfig::get_vhost(string vhost, bool try_default_vhost)
3495 { 3538 {
3496 srs_assert(root); 3539 srs_assert(root);
3497 3540
@@ -3507,7 +3550,7 @@ SrsConfDirective* SrsConfig::get_vhost(string vhost) @@ -3507,7 +3550,7 @@ SrsConfDirective* SrsConfig::get_vhost(string vhost)
3507 } 3550 }
3508 } 3551 }
3509 3552
3510 - if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) { 3553 + if (try_default_vhost && vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) {
3511 return get_vhost(SRS_CONSTS_RTMP_DEFAULT_VHOST); 3554 return get_vhost(SRS_CONSTS_RTMP_DEFAULT_VHOST);
3512 } 3555 }
3513 3556
@@ -6268,7 +6311,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) @@ -6268,7 +6311,7 @@ int srs_config_transform_vhost(SrsConfDirective* root)
6268 // SRS3+: 6311 // SRS3+:
6269 // vhost { forward { enabled; destination; } } 6312 // vhost { forward { enabled; destination; } }
6270 if (n == "forward" && conf->directives.empty()) { 6313 if (n == "forward" && conf->directives.empty()) {
6271 - conf->get_or_create("enabled", "on"); 6314 + conf->get_or_create("enabled")->set_arg0("on");
6272 6315
6273 SrsConfDirective* destination = conf->get_or_create("destination"); 6316 SrsConfDirective* destination = conf->get_or_create("destination");
6274 destination->args = conf->args; 6317 destination->args = conf->args;
@@ -130,6 +130,7 @@ public: @@ -130,6 +130,7 @@ public:
130 public: 130 public:
131 virtual SrsConfDirective* get_or_create(std::string n); 131 virtual SrsConfDirective* get_or_create(std::string n);
132 virtual SrsConfDirective* get_or_create(std::string n, std::string a0); 132 virtual SrsConfDirective* get_or_create(std::string n, std::string a0);
  133 + virtual SrsConfDirective* set_arg0(std::string a0);
133 // help utilities 134 // help utilities
134 public: 135 public:
135 /** 136 /**
@@ -373,6 +374,10 @@ public: @@ -373,6 +374,10 @@ public:
373 * raw set the global pithy print interval in ms. 374 * raw set the global pithy print interval in ms.
374 */ 375 */
375 virtual int raw_set_pithy_print_ms(std::string pithy_print_ms, bool& applied); 376 virtual int raw_set_pithy_print_ms(std::string pithy_print_ms, bool& applied);
  377 + /**
  378 + * raw create the new vhost.
  379 + */
  380 + virtual int raw_create_vhost(std::string vhost, bool& applied);
376 private: 381 private:
377 virtual int do_reload_listen(); 382 virtual int do_reload_listen();
378 virtual int do_reload_pid(); 383 virtual int do_reload_pid();
@@ -382,6 +387,7 @@ private: @@ -382,6 +387,7 @@ private:
382 virtual int do_reload_max_connections(); 387 virtual int do_reload_max_connections();
383 virtual int do_reload_utc_time(); 388 virtual int do_reload_utc_time();
384 virtual int do_reload_pithy_print_ms(); 389 virtual int do_reload_pithy_print_ms();
  390 + virtual int do_reload_vhost_added(std::string vhost);
385 public: 391 public:
386 /** 392 /**
387 * get the config file path. 393 * get the config file path.
@@ -503,13 +509,14 @@ public: @@ -503,13 +509,14 @@ public:
503 // vhost specified section 509 // vhost specified section
504 public: 510 public:
505 /** 511 /**
506 - * get the vhost directive by vhost name.  
507 - * @param vhost, the name of vhost to get.  
508 - */  
509 - virtual SrsConfDirective* get_vhost(std::string vhost); 512 + * get the vhost directive by vhost name.
  513 + * @param vhost, the name of vhost to get.
  514 + * @param try_default_vhost whether try default when get specified vhost failed.
  515 + */
  516 + virtual SrsConfDirective* get_vhost(std::string vhost, bool try_default_vhost = true);
510 /** 517 /**
511 - * get all vhosts in config file.  
512 - */ 518 + * get all vhosts in config file.
  519 + */
513 virtual void get_vhosts(std::vector<SrsConfDirective*>& vhosts); 520 virtual void get_vhosts(std::vector<SrsConfDirective*>& vhosts);
514 /** 521 /**
515 * whether vhost is enabled 522 * whether vhost is enabled
@@ -982,10 +982,11 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -982,10 +982,11 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
982 } 982 }
983 983
984 // for rpc=update, to update the configs of server. 984 // for rpc=update, to update the configs of server.
985 - // @param scope the scope to update for config.  
986 - // @param value the updated value for scope. 985 + // @scope the scope to update for config.
  986 + // @value the updated value for scope.
  987 + // @param the extra param for scope.
987 // possible updates: 988 // possible updates:
988 - // @param scope @param value value-description 989 + // @scope @value value-description
989 // listen 1935,1936 the port list. 990 // listen 1935,1936 the port list.
990 // pid ./objs/srs.pid the pid file of srs. 991 // pid ./objs/srs.pid the pid file of srs.
991 // chunk_size 60000 the global RTMP chunk_size. 992 // chunk_size 60000 the global RTMP chunk_size.
@@ -996,6 +997,9 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -996,6 +997,9 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
996 // max_connections 1000 the max connections of srs. 997 // max_connections 1000 the max connections of srs.
997 // utc_time false whether enable utc time. 998 // utc_time false whether enable utc time.
998 // pithy_print_ms 10000 the pithy print interval in ms. 999 // pithy_print_ms 10000 the pithy print interval in ms.
  1000 + // vhost specified updates:
  1001 + // @scope @value @param description
  1002 + // vhost ossrs.net create create vhost ossrs.net
999 if (rpc == "update") { 1003 if (rpc == "update") {
1000 if (!allow_update) { 1004 if (!allow_update) {
1001 ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED; 1005 ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED;
@@ -1005,6 +1009,7 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1005,6 +1009,7 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1005 1009
1006 std::string scope = r->query_get("scope"); 1010 std::string scope = r->query_get("scope");
1007 std::string value = r->query_get("value"); 1011 std::string value = r->query_get("value");
  1012 + std::string param = r->query_get("param");
1008 if (scope.empty()) { 1013 if (scope.empty()) {
1009 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED; 1014 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED;
1010 srs_error("raw api query invalid empty scope. ret=%d", ret); 1015 srs_error("raw api query invalid empty scope. ret=%d", ret);
@@ -1013,12 +1018,17 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1013,12 +1018,17 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1013 if (scope != "listen" && scope != "pid" && scope != "chunk_size" 1018 if (scope != "listen" && scope != "pid" && scope != "chunk_size"
1014 && scope != "ff_log_dir" && scope != "srs_log_tank" && scope != "srs_log_level" 1019 && scope != "ff_log_dir" && scope != "srs_log_tank" && scope != "srs_log_level"
1015 && scope != "srs_log_file" && scope != "max_connections" && scope != "utc_time" 1020 && scope != "srs_log_file" && scope != "max_connections" && scope != "utc_time"
1016 - && scope != "pithy_print_ms" 1021 + && scope != "pithy_print_ms" && scope != "vhost"
1017 ) { 1022 ) {
1018 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED; 1023 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED;
1019 srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret); 1024 srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret);
1020 return srs_api_response_code(w, r, ret); 1025 return srs_api_response_code(w, r, ret);
1021 } 1026 }
  1027 + if (scope == "vhost" && param != "create") {
  1028 + ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED;
  1029 + srs_error("raw api query invalid scope=%s, param=%s. ret=%d", scope.c_str(), param.c_str(), ret);
  1030 + return srs_api_response_code(w, r, ret);
  1031 + }
1022 1032
1023 bool applied = false; 1033 bool applied = false;
1024 if (scope == "listen") { 1034 if (scope == "listen") {
@@ -1145,6 +1155,20 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1145,6 +1155,20 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1145 srs_error("raw api update pithy_print_ms=%s/%d failed. ret=%d", value.c_str(), ppmv, ret); 1155 srs_error("raw api update pithy_print_ms=%s/%d failed. ret=%d", value.c_str(), ppmv, ret);
1146 return srs_api_response_code(w, r, ret); 1156 return srs_api_response_code(w, r, ret);
1147 } 1157 }
  1158 + } else if (scope == "vhost") {
  1159 + if (param == "create") {
  1160 + // when create, the vhost must not exists.
  1161 + if (param.empty() || _srs_config->get_vhost(value, false)) {
  1162 + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
  1163 + srs_error("raw api update check vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
  1164 + return srs_api_response_code(w, r, ret);
  1165 + }
  1166 +
  1167 + if ((ret = _srs_config->raw_create_vhost(value, applied)) != ERROR_SUCCESS) {
  1168 + srs_error("raw api update vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
  1169 + return srs_api_response_code(w, r, ret);
  1170 + }
  1171 + }
1148 } 1172 }
1149 1173
1150 // whether the config applied. 1174 // whether the config applied.