Skip to content

Commit 9f3bdda

Browse files
committed
httpserver: allow compiling out features not needed for monitoring api
This patch modifies httpserver-api source code to add pre-processor conditionals - "#if !defined(MONITORING)" - to disable fragments of code that are not needed for monitoring api. More specifically it disables all non-GET routes (with exception of trace API) and YAML file-based configuration. It also disables SSL. All of which is done to allow creating minimal "read-only" version of httpserver API intended for monitoring purposes only. Finally it also explicitly makes only certains symbols (like "main") public by using compiler directives like "__attribute__((visibility("default")))" and "#pragma GCC visibility push(default)/#pragma GCC visibility pop". The latter is used to make portion of common code public so that is available to modules like trace API. Hiding most symbols in monitoring module also helps to reduce its size. Signed-off-by: Waldemar Kozaczuk <[email protected]>
1 parent 2006f3b commit 9f3bdda

26 files changed

+105
-8
lines changed

modules/httpserver-api/api/api.cc

+4
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,23 @@ class api_param_handler : public handler_base {
240240
routes& _routes;
241241
};
242242

243+
#if !defined(MONITORING)
243244
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
244245
httpserver::api::api::init(*routes);
245246
}
247+
#endif
246248

247249
void init(routes& routes)
248250
{
249251
api_json_init_path("Advanced API options");
250252

251253
api_batch.set_handler(new api_param_handler(routes));
254+
#if !defined(MONITORING)
252255
stop_api.set_handler([](const_req req){
253256
global_server::stop();
254257
return "";
255258
});
259+
#endif
256260

257261
}
258262

modules/httpserver-api/api/env.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ using namespace json;
2020
using namespace std;
2121
using namespace env_json;
2222

23+
#if !defined(MONITORING)
2324
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
2425
httpserver::api::env::init(*routes);
2526
}
27+
#endif
2628

2729
void init(routes& routes)
2830
{
@@ -46,6 +48,7 @@ void init(routes& routes)
4648
return res;
4749
});
4850

51+
#if !defined(MONITORING)
4952
setEnv.set_handler([](const_req req) {
5053
string param = req.param.at("var").substr(1);
5154
if (setenv(param.c_str(),
@@ -62,7 +65,7 @@ void init(routes& routes)
6265
}
6366
return "";
6467
});
65-
68+
#endif
6669
}
6770

6871
}

modules/httpserver-api/api/file.cc

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static string file_name(const string& path)
9898
return path.substr(found + 1);
9999
}
100100

101+
#if !defined(MONITORING)
101102
/**
102103
* Generate a temporary file name in a target directory
103104
* according to a file name
@@ -152,6 +153,7 @@ static void copy(const std::string& from, const std::string& to)
152153
+ e.code().message());
153154
}
154155
}
156+
#endif
155157

156158
class get_file_handler : public file_interaction_handler {
157159
virtual void handle(const std::string& path, parameters* params,
@@ -318,6 +320,7 @@ class get_file_handler : public file_interaction_handler {
318320
}
319321
};
320322

323+
#if !defined(MONITORING)
321324
class del_file_handler : public handler_base {
322325
virtual void handle(const std::string& path, parameters* params,
323326
const http::server::request& req, http::server::reply& rep)
@@ -442,18 +445,23 @@ class put_file_handler : public handler_base {
442445
set_headers(rep, "json");
443446
}
444447
};
448+
#endif
445449

450+
#if !defined(MONITORING)
446451
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
447452
httpserver::api::file::init(*routes);
448453
}
454+
#endif
449455

450456
void init(routes& routes)
451457
{
452458
file_json_init_path("file API");
453459
getFile.set_handler(new get_file_handler());
460+
#if !defined(MONITORING)
454461
delFile.set_handler(new del_file_handler());
455462
putFile.set_handler(new put_file_handler());
456463
upload.set_handler(new post_file_handler());
464+
#endif
457465
}
458466

459467
}

modules/httpserver-api/api/fs.cc

+2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ static void fill_dfstat(DFStat& dfstat, const osv::mount_desc& mount, const stru
3333
dfstat.blocksize = st.f_frsize;
3434
}
3535

36+
#if !defined(MONITORING)
3637
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
3738
httpserver::api::fs::init(*routes);
3839
}
40+
#endif
3941

4042
void init(routes& routes) {
4143

modules/httpserver-api/api/hardware.cc

+2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ using namespace std;
2424
using namespace json;
2525
using namespace hardware_json;
2626

27+
#if !defined(MONITORING)
2728
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
2829
httpserver::api::hardware::init(*routes);
2930
}
31+
#endif
3032

3133
void init(routes& routes)
3234
{

modules/httpserver-api/api/network.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ static Interface get_interface(const string& name, ifnet* ifp, long time)
3030
Interface f;
3131
interface intf(name);
3232

33-
3433
if_data cur_data = { 0 };
3534
if (!set_interface_info(ifp, cur_data, intf)) {
3635
throw server_error_exception("Failed getting interface information");
@@ -43,9 +42,11 @@ static Interface get_interface(const string& name, ifnet* ifp, long time)
4342
return f;
4443
}
4544

45+
#if !defined(MONITORING)
4646
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
4747
httpserver::api::network::init(*routes);
4848
}
49+
#endif
4950

5051
/**
5152
* Initialize the routes object with specific routes mapping

modules/httpserver-api/api/os.cc

+8
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ using namespace std;
3333
using namespace json;
3434
using namespace os_json;
3535

36+
#if !defined(MONITORING)
3637
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
3738
httpserver::api::os::init(*routes);
3839
}
40+
#endif
3941

4042
void init(routes& routes)
4143
{
@@ -83,6 +85,7 @@ void init(routes& routes)
8385
return memory::get_balloon_size();
8486
});
8587

88+
#if !defined(MONITORING)
8689
os_shutdown.set_handler([](const_req req) {
8790
osv::shutdown();
8891
return "";
@@ -97,6 +100,7 @@ void init(routes& routes)
97100
osv::reboot();
98101
return "";
99102
});
103+
#endif
100104

101105
os_dmesg.set_handler([](const_req req) {
102106
return debug_buffer;
@@ -109,11 +113,13 @@ void init(routes& routes)
109113
return json_return_type(hostname);
110114
});
111115

116+
#if !defined(MONITORING)
112117
os_set_hostname.set_handler([](const_req req) {
113118
string hostname = req.get_query_param("name");
114119
sethostname(hostname.c_str(), hostname.size());
115120
return "";
116121
});
122+
#endif
117123

118124
os_threads.set_handler([](const_req req) {
119125
using namespace std::chrono;
@@ -143,6 +149,7 @@ void init(routes& routes)
143149
return osv::getcmdline();
144150
});
145151

152+
#if !defined(MONITORING)
146153
os_set_cmdline.set_handler([](const_req req) {
147154
string newcmd = req.get_query_param("cmdline");
148155

@@ -157,6 +164,7 @@ void init(routes& routes)
157164
return osv::getcmdline();
158165

159166
});
167+
#endif
160168

161169
}
162170

modules/httpserver-api/api/trace.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace httpserver::json::trace_json;
2525
static std::unordered_map<tracepoint_base*,
2626
std::unique_ptr<tracepoint_counter>> counters;
2727

28-
extern "C" void httpserver_plugin_register_routes(httpserver::routes* routes) {
28+
extern "C" void __attribute__((visibility("default"))) httpserver_plugin_register_routes(httpserver::routes* routes) {
2929
httpserver::api::trace::init(*routes);
3030
}
3131

modules/httpserver-api/global_server.cc

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@
1313
#include <dlfcn.h>
1414
#include <boost/filesystem.hpp>
1515
#include <boost/foreach.hpp>
16-
#include "yaml-cpp/yaml.h"
1716
#include "json/api_docs.hh"
1817
#include <osv/debug.h>
1918
#include "transformers.hh"
2019
#include <osv/options.hh>
20+
#if !defined(MONITORING)
21+
#include "yaml-cpp/yaml.h"
22+
#else
23+
#include "api/os.hh"
24+
#include "api/fs.hh"
25+
#include "api/file.hh"
26+
#include "api/network.hh"
27+
#include "api/hardware.hh"
28+
#include "api/api.hh"
29+
#include "api/env.hh"
30+
#endif
2131

2232
namespace httpserver {
2333

@@ -36,6 +46,7 @@ bool global_server::run(std::map<std::string,std::vector<std::string>>& _config)
3646
if (get().s != nullptr) {
3747
return false;
3848
}
49+
#if !defined(MONITORING)
3950
std::string config_file_path = "/tmp/httpserver.conf";
4051
if (options::option_value_exists(_config, "config-file")) {
4152
config_file_path = options::extract_option_value(_config, "config-file");
@@ -79,16 +90,19 @@ bool global_server::run(std::map<std::string,std::vector<std::string>>& _config)
7990
throw e;
8091
}
8192
}
93+
#endif
8294

8395
set(_config);
8496
get().set("ipaddress", "0.0.0.0");
8597
get().set("port", "8000");
8698

99+
#if !defined(MONITORING)
87100
if (get().config.count("ssl")) {
88101
get().set("cert", "/etc/pki/server.pem");
89102
get().set("key", "/etc/pki/private/server.key");
90103
get().set("cacert", "/etc/pki/CA/cacert.pem");
91104
}
105+
#endif
92106

93107
auto port = get().config["port"][0];
94108
get().s = new http::server::server(get().config, &get()._routes);
@@ -105,6 +119,7 @@ bool global_server::run(std::map<std::string,std::vector<std::string>>& _config)
105119
return true;
106120
}
107121

122+
#if !defined(MONITORING)
108123
void global_server::setup_file_mappings(const YAML::Node& file_mappings_node) {
109124
for (auto node : file_mappings_node) {
110125
const YAML::Node path = node["path"];
@@ -154,6 +169,7 @@ void global_server::setup_redirects(const YAML::Node& redirects_node) {
154169
}
155170
}
156171
}
172+
#endif
157173

158174
global_server::global_server()
159175
: s(nullptr)
@@ -182,7 +198,15 @@ void global_server::set_routes()
182198
{
183199
path_holder::set_routes(&_routes);
184200
json::api_doc_init(_routes);
185-
201+
#if defined(MONITORING)
202+
httpserver::api::api::init(_routes);
203+
httpserver::api::fs::init(_routes);
204+
httpserver::api::os::init(_routes);
205+
httpserver::api::network::init(_routes);
206+
httpserver::api::hardware::init(_routes);
207+
httpserver::api::env::init(_routes);
208+
httpserver::api::file::init(_routes);
209+
#endif
186210
{
187211
namespace fs = boost::filesystem;
188212
fs::path plugin_path("/usr/mgmt/plugins/");

modules/httpserver-api/global_server.hh

+4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include <vector>
1313
#include <mutex>
1414
#include <condition_variable>
15+
#if !defined(MONITORING)
1516
#include <yaml-cpp/node/iterator.h>
17+
#endif
1618

1719
namespace httpserver {
1820
/**
@@ -64,8 +66,10 @@ private:
6466

6567
global_server();
6668
void set_routes();
69+
#if !defined(MONITORING)
6770
void setup_redirects(const YAML::Node& node);
6871
void setup_file_mappings(const YAML::Node& node);
72+
#endif
6973
void load_plugin(const std::string& path);
7074
static global_server* instance;
7175
routes _routes;

modules/httpserver-api/handlers.cc

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const std::string handler_base::ERROR_500_PAGE("<h1>Something went wrong</h1>");
1919
const std::string handler_base::ERROR_404_PAGE(
2020
"<h1>We didn't find the page you were looking for</h1>");
2121

22+
#pragma GCC visibility push(default)
2223
void handler_base::set_headers_explicit(http::server::reply& rep, const std::string& mime)
2324
{
2425
const int contentLength = rep.content.size();
@@ -53,6 +54,7 @@ void handler_base::reply500(http::server::reply& rep, const std::string& alterna
5354
rep = http::server::reply::stock_reply(http::server::reply::internal_server_error,
5455
&alternative_message);
5556
}
57+
#pragma GCC visibility pop
5658

5759
directory_handler::directory_handler(const string& doc_root,
5860
file_transformer* transformer)
@@ -76,6 +78,7 @@ void directory_handler::handle(const string& path, parameters* parts,
7678
read(full_path, req, rep);
7779
}
7880

81+
#pragma GCC visibility push(default)
7982
file_interaction_handler::~file_interaction_handler()
8083
{
8184
delete transformer;
@@ -128,6 +131,7 @@ bool file_interaction_handler::redirect_if_needed(
128131
}
129132
return false;
130133
}
134+
#pragma GCC visibility pop
131135

132136
void file_handler::handle(const string& path, parameters* parts,
133137
const http::server::request& req, http::server::reply& rep)

modules/httpserver-api/handlers.hh

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef const http::server::request& const_req;
3232
* file_handler - map a url path to a file
3333
* function_handler - uses a lambda expression to handle a reqeust
3434
*/
35+
#pragma GCC visibility push(default)
3536
class handler_base {
3637
public:
3738
/**
@@ -90,7 +91,7 @@ public:
9091
virtual void reply500(http::server::reply& rep,
9192
const std::string& alternative_message = ERROR_500_PAGE);
9293

93-
/**
94+
/**
9495
* Add a mandatory parameter
9596
* @param param a parameter name
9697
* @return a reference to the handler
@@ -107,6 +108,7 @@ public:
107108
std::vector<std::string> mandatory_param;
108109

109110
};
111+
#pragma GCC visibility pop
110112

111113
/**
112114
* This is a base class for file transformer.
@@ -132,6 +134,7 @@ public:
132134
virtual ~file_transformer() = default;
133135
};
134136

137+
#pragma GCC visibility push(default)
135138
/**
136139
* A base class for handlers that interact with files.
137140
* directory and file handlers both share some common logic
@@ -188,6 +191,7 @@ protected:
188191
http::server::reply& rep);
189192
file_transformer* transformer;
190193
};
194+
#pragma GCC visibility pop
191195

192196
/**
193197
* The directory handler get a disk path in the

0 commit comments

Comments
 (0)