centralize reading/writing file

This commit is contained in:
loki 2021-06-09 12:04:51 +02:00
parent 9d52174d6b
commit cf77b301bc
4 changed files with 3 additions and 33 deletions

View File

@ -316,7 +316,7 @@ void saveConfig(resp_https_t response, req_https_t request) {
configStream << kv.first << " = " << value << std::endl;
}
http::write_file(SUNSHINE_ASSETS_DIR "/sunshine.conf", configStream.str());
write_file(SUNSHINE_ASSETS_DIR "/sunshine.conf", configStream.str());
}
catch(std::exception &e) {
BOOST_LOG(warning) << e.what();

View File

@ -33,8 +33,6 @@ namespace pt = boost::property_tree;
int create_creds(const std::string &pkey, const std::string &cert);
int generate_user_creds(const std::string &file);
int reload_user_creds(const std::string &file);
std::string read_file(const char *path);
int write_file(const char *path, const std::string_view &contents);
std::string unique_id;
net::net_e origin_pin_allowed;
@ -156,30 +154,4 @@ int create_creds(const std::string &pkey, const std::string &cert) {
return 0;
}
int write_file(const char *path, const std::string_view &contents) {
std::ofstream out(path);
if(!out.is_open()) {
return -1;
}
out << contents;
return 0;
}
std::string read_file(const char *path) {
std::ifstream in(path);
std::string input;
std::string base64_cert;
//FIXME: Being unable to read file could result in infinite loop
while(!in.eof()) {
std::getline(in, input);
base64_cert += input + '\n';
}
return base64_cert;
}
} // namespace http

View File

@ -5,8 +5,6 @@ namespace http {
void init(std::shared_ptr<safe::signal_t> shutdown_event);
int create_creds(const std::string &pkey, const std::string &cert);
std::string read_file(const char *path);
int write_file(const char *path, const std::string_view &contents);
int reload_user_creds(const std::string &file);
extern std::string unique_id;
extern net::net_e origin_pin_allowed;

View File

@ -741,8 +741,8 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
load_state();
}
conf_intern.pkey = http::read_file(config::nvhttp.pkey.c_str());
conf_intern.servercert = http::read_file(config::nvhttp.cert.c_str());
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());
conf_intern.servercert = read_file(config::nvhttp.cert.c_str());
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
ctx->use_certificate_chain_file(config::nvhttp.cert);