diff --git a/control-server/src/certificate.cpp b/control-server/src/certificate.cpp index 4059eb5..0646352 100644 --- a/control-server/src/certificate.cpp +++ b/control-server/src/certificate.cpp @@ -4,13 +4,14 @@ #include #include -void write_to_file(std::filesystem::path path, const std::string& content) { - std::ofstream private_key_file; - private_key_file.exceptions(std::ios::failbit | std::ios::badbit); +#include - // These lines will throw - private_key_file.open(path); - private_key_file << content; +void write_to_file(const std::filesystem::path& path, const std::string& content) { + std::ofstream file; + file.exceptions(std::ios::failbit | std::ios::badbit); + + file.open(path); + file << content; } CertificateManager::CertificateManager(const std::filesystem::path& certificate_root_path) @@ -26,10 +27,10 @@ bool CertificateManager::certificate_exists(const std::string& domain) const { void CertificateManager::write_private_key(const std::string& domain, const std::string& private_key) const { std::filesystem::path path = get_certificate_directory(domain) / "privkey.pem"; - write_to_file(domain, private_key.c_str()); + write_to_file(path, private_key.c_str()); } void CertificateManager::write_certificate(const std::string& domain, const std::string& private_key) const { std::filesystem::path path = get_certificate_directory(domain) / "fullchain.pem"; - write_to_file(domain, private_key.c_str()); + write_to_file(path, private_key.c_str()); } \ No newline at end of file diff --git a/control-server/src/control_server.cpp b/control-server/src/control_server.cpp index ba75c21..11ad1a9 100644 --- a/control-server/src/control_server.cpp +++ b/control-server/src/control_server.cpp @@ -38,7 +38,7 @@ void ControlServer::setup_routes() { catch(const std::exception& e) { response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500); - std::cout << "Failed to reload Nginx: " << e.what() << std::endl; + std::cerr << "Failed to reload Nginx: " << e.what() << std::endl; } }); @@ -49,7 +49,7 @@ void ControlServer::setup_routes() { server_.Post("/certificate/:domain", [this](const httplib::Request& req, httplib::Response &res) { // TODO do we need to sanitize? - std::string domain = req.get_param_value("domain"); + std::string domain = req.path_params.at("domain"); if (!req.form.has_file("certificate") || !req.form.has_file("private_key")) { response_client_error(res, "Missing certificate and/or private_key fields", httplib::StatusCode::BadRequest_400); @@ -70,7 +70,7 @@ void ControlServer::setup_routes() { certificate_manager_.write_certificate(domain, certificate_field.content); } catch (const std::exception& e) { response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500); - std::cout << "Failed to load certificates: " << e.what() << std::endl; + std::cerr << "Failed to save certificates: " << e.what() << std::endl; } }); }