@@ -54,7 +54,7 @@ void handle_exception(const std::exception& e, const httplib::Request& request,
54
54
// if user requests an ecflow script and the script does not exists, should
55
55
// 404 be returned (even if the rest api path was correct)?
56
56
57
- auto trimr = [](const std::string& str) -> std::string {
57
+ auto trimr = [](const std::string& str) {
58
58
std::string copy = str;
59
59
copy.erase (copy.find_last_not_of (" \n\r " ) + 1 , std::string::npos);
60
60
return copy;
@@ -122,9 +122,11 @@ void handle_exception(const HttpServerException& e, const httplib::Request& requ
122
122
}
123
123
124
124
void set_last_request_time () {
125
- struct timeval curtime;
125
+ struct timeval curtime
126
+ {
127
+ };
126
128
gettimeofday (&curtime, nullptr );
127
- last_request_time = curtime.tv_sec ;
129
+ last_request_time = static_cast < unsigned int >( curtime.tv_sec ) ;
128
130
}
129
131
130
132
template <typename T>
@@ -147,8 +149,9 @@ void trycatch(const httplib::Request& request, httplib::Response& response, T&&
147
149
ojson filter_json (const ojson& j, const httplib::Request& r) {
148
150
149
151
const std::string path = r.get_param_value (" filter" );
150
- if (path.empty ())
152
+ if (path.empty ()) {
151
153
return j;
154
+ }
152
155
153
156
// split filter path on dot, and reverse the elements
154
157
// the elements are consumed by the dive() function
@@ -157,42 +160,45 @@ ojson filter_json(const ojson& j, const httplib::Request& r) {
157
160
ecf::Str::split (path, path_elems, " ." );
158
161
std::reverse (path_elems.begin (), path_elems.end ());
159
162
160
- if (path_elems.empty ())
163
+ if (path_elems.empty ()) {
161
164
return j;
165
+ }
162
166
163
167
// separate array name and index from a string
164
- auto get_array_info = [](const std::string& str) {
165
- auto start = str.find (" [" ), stop = str.find (" ]" );
166
- const std::string key = str.substr (0 , start);
167
- const int index = std::stoi (str.substr (start + 1 , stop - start));
168
+ auto get_array_info = [](std::string_view str) {
169
+ const auto start = str.find (" [" );
170
+ const auto stop = str.find (" ]" );
171
+ const auto key = str.substr (0 , start);
172
+ const int index = std::stoi (std::string{str.substr (start + 1 , stop - start)});
168
173
return std::make_pair (key, index );
169
174
};
170
175
171
176
// special case: filter is .[INDEX], means that we return the
172
177
// correct array element from root json element assuming it's an array
173
178
if (path_elems.size () == 1 && path_elems[0 ][0 ] == ' [' && path_elems[0 ][path_elems[0 ].size () - 1 ] == ' ]' ) {
174
- const auto arr = get_array_info (path_elems[0 ]);
175
- return j[arr. second ];
179
+ const auto [key, index ] = get_array_info (path_elems[0 ]);
180
+ return j[index ];
176
181
}
177
182
178
183
// recursively find the correct element inside json document
179
- std::function<ojson (const ojson&, std::vector<std::string>&)> dive =
180
- [&]( const ojson& js, std::vector<std::string>& path_elems) -> ojson {
181
- if (path_elems .empty ())
184
+ std::function<ojson (const ojson&, std::vector<std::string>&)> dive = [&]( const ojson& js,
185
+ const std::vector<std::string>& elems) {
186
+ if (elems .empty ()) {
182
187
return js;
188
+ }
183
189
184
190
const auto elem = path_elems.back ();
185
191
path_elems.pop_back ();
186
192
187
193
try {
188
194
if (elem.find (" [" ) != std::string::npos && elem.find (" ]" ) != std::string::npos) {
189
- const auto arr = get_array_info (elem);
190
- return dive (js.at (arr. first )[arr. second ], path_elems);
195
+ const auto [key, index ] = get_array_info (elem);
196
+ return dive (js.at (std::string{key})[ index ], path_elems);
191
197
}
192
198
193
199
return dive (js.at (elem), path_elems);
194
200
}
195
- catch (const ojson::exception & e) {
201
+ catch (const ojson::exception & e [[maybe_unused]] ) {
196
202
// filter path is not found or some other problem with user given path
197
203
return ojson ();
198
204
}
@@ -399,7 +405,7 @@ void node_script_read(const httplib::Request& request, httplib::Response& respon
399
405
client->file (path, " job" );
400
406
j[" job" ] = client->server_reply ().get_string ();
401
407
}
402
- catch (const std::exception & e) {
408
+ catch (const std::exception & e [[maybe_unused]] ) {
403
409
j[" job" ] = " " ;
404
410
}
405
411
@@ -484,27 +490,22 @@ void server_status_read(const httplib::Request& request, httplib::Response& resp
484
490
485
491
void server_status_update (const httplib::Request& request, httplib::Response& response) {
486
492
trycatch (request, response, [&]() {
487
- const ojson payload = ojson::parse (request.body );
488
- const std::string name = payload.at (" action" );
489
-
490
- auto client = get_client (request);
491
-
492
- if (name == " reload_whitelist_file" ) {
493
- client->reloadwsfile ();
493
+ auto payload = ojson::parse (request.body );
494
+ if (const std::string name = payload.at (" action" ); name == " reload_whitelist_file" ) {
495
+ get_client (request)->reloadwsfile ();
494
496
}
495
497
else if (name == " reload_passwd_file" ) {
496
- client ->reloadpasswdfile ();
498
+ get_client (request) ->reloadpasswdfile ();
497
499
}
498
500
else if (name == " reload_custom_passwd_file" ) {
499
- client ->reloadcustompasswdfile ();
501
+ get_client (request) ->reloadcustompasswdfile ();
500
502
}
501
503
else {
502
504
throw HttpServerException (HttpStatusCode::client_error_bad_request, " Invalid action: " + name);
503
505
}
504
506
505
507
ojson j;
506
- j[" message" ] = " Server updated successfully" ;
507
-
508
+ j[" message" ] = " Server updated successfully" ;
508
509
response.status = HttpStatusCode::success_ok;
509
510
response.set_content (j.dump (), " application/json" );
510
511
set_cors (response);
@@ -573,14 +574,15 @@ void server_statistics_read(const httplib::Request& request, httplib::Response&
573
574
trycatch (request, response, [&]() {
574
575
response.status = HttpStatusCode::success_ok;
575
576
const std::time_t t = std::chrono::system_clock::to_time_t (api_startup);
576
- char date[80 ];
577
- const std::tm tm = *gmtime (&t);
578
- strftime (date, 80 , " %Y-%m-%dT%H:%M:%SZ" , &tm );
577
+ std::array<char , 80 > date{};
578
+ std::tm tm {};
579
+ gmtime_r (&t, &tm );
580
+ strftime (date.data (), 80 , " %Y-%m-%dT%H:%M:%SZ" , &tm );
579
581
580
582
ojson j = {{" num_requests" , num_requests.load ()},
581
583
{" num_errors" , num_errors.load ()},
582
584
{" num_cached_requests" , num_cached_requests.load ()},
583
- {" since" , std::string ( date) }};
585
+ {" since" , std::string{ date. data ()} }};
584
586
585
587
j = filter_json (j, request);
586
588
response.set_content (j.dump (), " application/json" );
0 commit comments