mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Support include empty config file. v5.0.173 v6.0.68 (#3768)
SRS supports including another configuration in the include package. When generating configurations, we can only generate the changed configurations, while the unchanged configurations are in the fixed files, for example: ```nginx listen 1935; include server.conf; ``` In `server.conf`, we can manage the changing configurations with the program: ```nginx http_api { enabled on; } ``` However, during system initialization, we often create an empty `server.conf`, and the content is generated only after the program starts, so `server.conf` might be an empty file. This also makes it convenient to use a script to confirm the existence of this file: ```bash touch server.conf ``` Currently, SRS does not support empty configurations and will report an error. This PR is to solve this problem, making it more convenient to use include. `TRANS_BY_GPT4` --------- Co-authored-by: Haibo Chen <495810242@qq.com>
This commit is contained in:
parent
b5347e19f7
commit
cf46dae80f
9 changed files with 124 additions and 22 deletions
|
@ -85,11 +85,14 @@ srs_error_t MockSrsConfig::build_buffer(std::string src, srs_internal::SrsConfig
|
|||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
string content = included_files[src];
|
||||
if(content.empty()) {
|
||||
// No file, error.
|
||||
if(included_files.find(src) == included_files.end()) {
|
||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "file %s: no found", src.c_str());
|
||||
}
|
||||
|
||||
string content = included_files[src];
|
||||
|
||||
// Empty file, ok.
|
||||
*pbuffer = new MockSrsConfigBuffer(content);
|
||||
|
||||
return err;
|
||||
|
@ -689,10 +692,30 @@ VOID TEST(ConfigDirectiveTest, ParseInvalidNoEndOfDirective)
|
|||
VOID TEST(ConfigDirectiveTest, ParseInvalidNoEndOfSubDirective)
|
||||
{
|
||||
srs_error_t err;
|
||||
|
||||
MockSrsConfigBuffer buf("dir0 {");
|
||||
SrsConfDirective conf;
|
||||
HELPER_ASSERT_FAILED(conf.parse(&buf));
|
||||
|
||||
if (true) {
|
||||
MockSrsConfigBuffer buf("");
|
||||
SrsConfDirective conf;
|
||||
HELPER_ASSERT_SUCCESS(conf.parse(&buf));
|
||||
}
|
||||
|
||||
if (true) {
|
||||
MockSrsConfigBuffer buf("# OK");
|
||||
SrsConfDirective conf;
|
||||
HELPER_ASSERT_SUCCESS(conf.parse(&buf));
|
||||
}
|
||||
|
||||
if (true) {
|
||||
MockSrsConfigBuffer buf("dir0 {");
|
||||
SrsConfDirective conf;
|
||||
HELPER_ASSERT_FAILED(conf.parse(&buf));
|
||||
}
|
||||
|
||||
if (true) {
|
||||
MockSrsConfigBuffer buf("dir0 {} dir1 {");
|
||||
SrsConfDirective conf;
|
||||
HELPER_ASSERT_FAILED(conf.parse(&buf));
|
||||
}
|
||||
}
|
||||
|
||||
VOID TEST(ConfigDirectiveTest, ParseInvalidNoStartOfSubDirective)
|
||||
|
|
47
trunk/src/utest/srs_utest_config2.cpp
Normal file
47
trunk/src/utest/srs_utest_config2.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// Copyright (c) 2013-2023 The SRS Authors
|
||||
//
|
||||
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
||||
//
|
||||
#include <srs_utest_config2.hpp>
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_kernel_file.hpp>
|
||||
#include <srs_utest_kernel.hpp>
|
||||
|
||||
VOID TEST(ConfigMainTest, CheckIncludeEmptyConfig)
|
||||
{
|
||||
srs_error_t err;
|
||||
|
||||
if (true) {
|
||||
string filepath = _srs_tmp_file_prefix + "utest-main.conf";
|
||||
MockFileRemover _mfr(filepath);
|
||||
|
||||
string included = _srs_tmp_file_prefix + "utest-included-empty.conf";
|
||||
MockFileRemover _mfr2(included);
|
||||
|
||||
if (true) {
|
||||
SrsFileWriter fw;
|
||||
fw.open(included);
|
||||
}
|
||||
|
||||
if (true) {
|
||||
SrsFileWriter fw;
|
||||
fw.open(filepath);
|
||||
string content = _MIN_OK_CONF "include " + included + ";";
|
||||
fw.write((void*)content.data(), (int)content.length(), NULL);
|
||||
}
|
||||
|
||||
SrsConfig conf;
|
||||
HELPER_ASSERT_SUCCESS(conf.parse_file(filepath.c_str()));
|
||||
EXPECT_EQ(1, (int)conf.get_listens().size());
|
||||
}
|
||||
|
||||
if (true) {
|
||||
MockSrsConfig conf;
|
||||
conf.mock_include("test.conf", "");
|
||||
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "include test.conf;"));
|
||||
EXPECT_EQ(1, (int)conf.get_listens().size());
|
||||
}
|
||||
}
|
||||
|
16
trunk/src/utest/srs_utest_config2.hpp
Normal file
16
trunk/src/utest/srs_utest_config2.hpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Copyright (c) 2013-2023 The SRS Authors
|
||||
//
|
||||
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
||||
//
|
||||
|
||||
#ifndef SRS_UTEST_CONFIG2_HPP
|
||||
#define SRS_UTEST_CONFIG2_HPP
|
||||
|
||||
/*
|
||||
#include <srs_utest_config2.hpp>
|
||||
*/
|
||||
#include <srs_utest_config.hpp>
|
||||
|
||||
#endif
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue