From cf77b301bcb68304bf568ce11d1a1111b86f5614 Mon Sep 17 00:00:00 2001 From: loki Date: Wed, 9 Jun 2021 12:04:51 +0200 Subject: [PATCH] centralize reading/writing file --- sunshine/confighttp.cpp | 2 +- sunshine/httpcommon.cpp | 28 ---------------------------- sunshine/httpcommon.h | 2 -- sunshine/nvhttp.cpp | 4 ++-- 4 files changed, 3 insertions(+), 33 deletions(-) diff --git a/sunshine/confighttp.cpp b/sunshine/confighttp.cpp index 8eb7ba0e..d5db6fd9 100644 --- a/sunshine/confighttp.cpp +++ b/sunshine/confighttp.cpp @@ -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(); diff --git a/sunshine/httpcommon.cpp b/sunshine/httpcommon.cpp index 24106aa0..a68b7eb3 100644 --- a/sunshine/httpcommon.cpp +++ b/sunshine/httpcommon.cpp @@ -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 \ No newline at end of file diff --git a/sunshine/httpcommon.h b/sunshine/httpcommon.h index 150298e8..49923290 100644 --- a/sunshine/httpcommon.h +++ b/sunshine/httpcommon.h @@ -5,8 +5,6 @@ namespace http { void init(std::shared_ptr 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; diff --git a/sunshine/nvhttp.cpp b/sunshine/nvhttp.cpp index 332f37bd..07e25422 100644 --- a/sunshine/nvhttp.cpp +++ b/sunshine/nvhttp.cpp @@ -741,8 +741,8 @@ void start(std::shared_ptr 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::tls); ctx->use_certificate_chain_file(config::nvhttp.cert);