Skip to content

Commit

Permalink
protect staticcontent with a lock and clear previous properties once …
Browse files Browse the repository at this point in the history
…done
  • Loading branch information
astibal committed Nov 29, 2023
1 parent 30fc735 commit 13bd565
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/staticcontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ bool StaticContent::load_files(std::string& dir) {

auto* t_temp = new Template(loader_file);
t_temp->load(dir + name + ".txt");

auto lc_ = std::scoped_lock(lock);
templates_->set(name,t_temp);
}
}
Expand Down Expand Up @@ -81,7 +83,7 @@ std::string StaticContent::render_noargs(std::string const& name) {
return {};
}

std::string StaticContent::render_server_response(std::string& message, unsigned int code) {
std::string StaticContent::render_server_response(std::string const& message, unsigned int code) {
std::stringstream out;
out << string_format("HTTP/1.1 %3d OK\r\n", code);
out << "Server: Smithproxy/1.1\r\n";
Expand All @@ -95,12 +97,18 @@ std::string StaticContent::render_server_response(std::string& message, unsigned
}

std::string StaticContent::render_msg_html_page(std::string const& caption, std::string const& meta, std::string const& content, const char* window_width) {

auto t = get("html_page");

auto lc_ = std::scoped_lock(lock);

t->set("title", caption);
t->set("meta", meta);
t->set("message", content);
t->set("window_width", window_width);

std::string r = t->render();
t->get_properties().clear();

return r;
}
7 changes: 4 additions & 3 deletions src/staticcontent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ using namespace ext::nltemplate;
class StaticContent {

std::unique_ptr<ptr_cache<std::string,Template>> templates_;
StaticContent() : log(get_log()) {
StaticContent() {
templates_ = std::make_unique<ptr_cache<std::string,Template>>("content.replacements");

};
~StaticContent() = default;

logan_lite& log;
logan_lite& log = get_log();
std::mutex lock;
public:
// should be populated externally, on start
static inline uint32_t boot_random {0};
Expand All @@ -64,7 +65,7 @@ class StaticContent {

std::string render_noargs(std::string const& s);

std::string render_server_response(std::string& message, unsigned int code=200);
std::string render_server_response(std::string const& message, unsigned int code=200);
std::string render_msg_html_page(std::string const& caption, std::string const& meta, std::string const& content,const char* window_width="450px");
std::shared_ptr<Template> get(std::string const& s);

Expand Down

0 comments on commit 13bd565

Please sign in to comment.