Skip to content

Commit 57bbb3c

Browse files
authored
A bunch of generalized code cleanup. (#322)
* Replace static_cast<T>(-1) with std::numeric_limits<T>::max(). * Move setting of http_request::cache out of the contructors. * Remove all uses of this-> * Cleanup some extra (). * Use the calling signiture for uri_log as defined by the docs: https://www.gnu.org/software/libmicrohttpd/manual/html_node/microhttpd_002dconst.html#index-logging * Revert setters.
1 parent 8572ca9 commit 57bbb3c

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

src/http_request.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ std::ostream &operator<< (std::ostream &os, const http_request &r) {
299299
}
300300

301301
http_request::~http_request() {
302-
for ( const auto &file_key : this->get_files() ) {
302+
for ( const auto &file_key : get_files() ) {
303303
for ( const auto &files : file_key.second ) {
304304
// C++17 has std::filesystem::remove()
305305
remove(files.second.get_file_system_file_name().c_str());

src/httpserver/create_webserver.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdlib.h>
2929
#include <memory>
3030
#include <functional>
31+
#include <limits>
3132
#include <string>
3233

3334
#include "httpserver/http_response.hpp"
@@ -363,7 +364,7 @@ class create_webserver {
363364
int _max_threads = 0;
364365
int _max_connections = 0;
365366
int _memory_limit = 0;
366-
size_t _content_size_limit = static_cast<size_t>(-1);
367+
size_t _content_size_limit = std::numeric_limits<size_t>::max();
367368
int _connection_timeout = DEFAULT_WS_TIMEOUT;
368369
int _per_IP_connection_limit = 0;
369370
log_access_ptr _log_access = nullptr;

src/httpserver/http_request.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stddef.h>
3535
#include <algorithm>
3636
#include <iosfwd>
37+
#include <limits>
3738
#include <map>
3839
#include <memory>
3940
#include <string>
@@ -98,7 +99,7 @@ class http_request {
9899
**/
99100
const std::string get_path_piece(int index) const {
100101
std::vector<std::string> post_path = get_path_pieces();
101-
if ((static_cast<int>((post_path.size()))) > index) {
102+
if (static_cast<int>(post_path.size()) > index) {
102103
return post_path[index];
103104
}
104105
return EMPTY;
@@ -258,11 +259,11 @@ class http_request {
258259
/**
259260
* Default constructor of the class. It is a specific responsibility of apis to initialize this type of objects.
260261
**/
261-
http_request() : cache(std::make_unique<http_request_data_cache>()) {}
262+
http_request() = default;
262263

263264
http_request(MHD_Connection* underlying_connection, unescaper_ptr unescaper):
264265
underlying_connection(underlying_connection),
265-
unescaper(unescaper), cache(std::make_unique<http_request_data_cache>()) {}
266+
unescaper(unescaper) {}
266267

267268
/**
268269
* Copy constructor. Deleted to make class move-only. The class is move-only for several reasons:
@@ -292,7 +293,7 @@ class http_request {
292293
std::string method;
293294
std::map<std::string, std::map<std::string, http::file_info>> files;
294295
std::string content = "";
295-
size_t content_size_limit = static_cast<size_t>(-1);
296+
size_t content_size_limit = std::numeric_limits<size_t>::max();
296297
std::string version;
297298

298299
struct MHD_Connection* underlying_connection = nullptr;
@@ -390,7 +391,7 @@ class http_request {
390391
**/
391392
void set_args(const std::map<std::string, std::string>& args) {
392393
for (auto const& [key, value] : args) {
393-
this->cache->unescaped_args[key].push_back(value.substr(0, content_size_limit));
394+
cache->unescaped_args[key].push_back(value.substr(0, content_size_limit));
394395
}
395396
}
396397

@@ -410,7 +411,7 @@ class http_request {
410411
std::string digested_user;
411412
std::map<std::string, std::vector<std::string>, http::arg_comparator> unescaped_args;
412413
};
413-
std::unique_ptr<http_request_data_cache> cache;
414+
std::unique_ptr<http_request_data_cache> cache = std::make_unique<http_request_data_cache>();
414415
// Populate the data cache unescaped_args
415416
void populate_args() const;
416417

src/httpserver/http_utils.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct generateFilenameException : public std::exception {
8282
}
8383

8484
const char* what() const noexcept {
85-
return this->error_message.c_str();
85+
return error_message.c_str();
8686
}
8787

8888
private:

src/webserver.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ namespace httpserver {
8282

8383
MHD_Result policy_callback(void *, const struct sockaddr*, socklen_t);
8484
void error_log(void*, const char*, va_list);
85-
void* uri_log(void*, const char*);
85+
void* uri_log(void*, const char*, struct MHD_Connection *con);
8686
void access_log(webserver*, string);
8787
size_t unescaper_func(void*, struct MHD_Connection*, char*);
8888

@@ -414,9 +414,10 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
414414
return MHD_YES;
415415
}
416416

417-
void* uri_log(void* cls, const char* uri) {
417+
void* uri_log(void* cls, const char* uri, struct MHD_Connection *con) {
418418
// Parameter needed to respect MHD interface, but not needed here.
419419
std::ignore = cls;
420+
std::ignore = con;
420421

421422
auto mr = std::make_unique<details::modded_request>();
422423
mr->complete_uri = std::make_unique<string>(uri);

test/integ/basic.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@ class error_resource : public http_resource {
285285

286286
class print_request_resource : public http_resource {
287287
public:
288-
explicit print_request_resource(stringstream* ss) {
289-
this->ss = ss;
290-
}
288+
explicit print_request_resource(stringstream* ss) : ss(ss) {}
291289

292290
shared_ptr<http_response> render_GET(const http_request& req) {
293291
(*ss) << req;
@@ -300,9 +298,7 @@ class print_request_resource : public http_resource {
300298

301299
class print_response_resource : public http_resource {
302300
public:
303-
explicit print_response_resource(stringstream* ss) {
304-
this->ss = ss;
305-
}
301+
explicit print_response_resource(stringstream* ss) : ss(ss) {}
306302

307303
shared_ptr<http_response> render_GET(const http_request&) {
308304
auto hresp = std::make_shared<string_response>("OK", 200, "text/plain");

0 commit comments

Comments
 (0)