-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstaticcontent.cpp
More file actions
114 lines (86 loc) · 3.9 KB
/
Copy pathstaticcontent.cpp
File metadata and controls
114 lines (86 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Smithproxy- transparent proxy with SSL inspection capabilities.
Copyright (c) 2014, Ales Stibal <[email protected]>, All rights reserved.
Smithproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Smithproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Smithproxy. If not, see <http://www.gnu.org/licenses/>.
Linking Smithproxy statically or dynamically with other modules is
making a combined work based on Smithproxy. Thus, the terms and
conditions of the GNU General Public License cover the whole combination.
In addition, as a special exception, the copyright holders of Smithproxy
give you permission to combine Smithproxy with free software programs
or libraries that are released under the GNU LGPL and with code
included in the standard release of OpenSSL under the OpenSSL's license
(or modified versions of such code, with unchanged license).
You may copy and distribute such a system following the terms
of the GNU GPL for Smithproxy and the licenses of the other code
concerned, provided that you include the source code of that other code
when and as the GNU GPL requires distribution of source code.
Note that people who make modified versions of Smithproxy are not
obligated to grant this special exception for their modified versions;
it is their choice whether to do so. The GNU General Public License
gives permission to release a modified version without this exception;
this exception also makes it possible to release a modified version
which carries forward this exception.
*/
#include <staticcontent.hpp>
bool StaticContent::load_files(std::string& dir) {
bool ret = true;
try {
LoaderFile loader_file;
for(const std::string name: { "test", "html_page", "html_img_warning"} ) {
_dia("StaticContent::load_files: loading template %s", name.c_str());
auto* t_temp = new Template(loader_file);
t_temp->load(dir + name + ".txt");
auto lc_ = std::scoped_lock(lock);
templates_->set(name,t_temp);
}
}
catch(std::exception& e) {
_err("StaticContent::load_files: exception caught: %s", e.what());
ret = false;
}
return ret;
}
std::shared_ptr<Template> StaticContent::get(std::string const& name) {
auto t = templates_->get(name);
if(!t) {
_err("StaticContent::get: cannot load template '%s'", name.c_str());
}
return t;
}
std::string StaticContent::render_noargs(std::string const& name) {
auto t = get(name);
if(t) {
return t->render();
}
return {};
}
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";
out << "Content-Type: text/html\r\n";
out << "Content-Length: " + std::to_string(message.length()); out << "\r\n";
out << "\r\n";
out << message;
return out.str();
}
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;
}