From 891d5d3ca3de25a2cc555d2c04183afed763b2f2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 24 Oct 2020 11:25:15 +0200 Subject: [PATCH] Update formatting, add namespace and fix a few warnings. --- src/hash.cpp | 45 ++++++++++++--------- src/http.cpp | 36 ++++++++++------- src/image.cpp | 31 +++++++------- src/libravatarserv.cpp | 51 ++++++++++++----------- src/libravatarserv.hpp | 91 ++++++++++++++++++++++-------------------- src/settings.cpp | 14 ++++--- src/version.hpp.in | 4 +- 7 files changed, 149 insertions(+), 123 deletions(-) diff --git a/src/hash.cpp b/src/hash.cpp index fc8b616..aed9aa7 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -1,5 +1,5 @@ /* This file is part of libravatarserv. - * Copyright © 2018 tastytea + * Copyright © 2018, 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,15 +14,21 @@ * along with this program. If not, see . */ -#include -#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 -#include -#include -#include -#include #include "libravatarserv.hpp" -using namespace hash; +#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 +#include +#include +#include +#include + +#include +#include + +using std::string; + +using namespace libravatarserv; +using namespace libravatarserv::hash; const string hash::md5(const string &text) { @@ -30,9 +36,9 @@ const string hash::md5(const string &text) Weak::MD5 hash; string digest; - StringSource s(text, true, - new HashFilter(hash, - new HexEncoder(new StringSink(digest)))); + StringSource s( + text, true, + new HashFilter(hash, new HexEncoder(new StringSink(digest)))); std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower); return digest; } @@ -43,9 +49,9 @@ const string hash::sha256(const string &text) SHA256 hash; string digest; - StringSource s(text, true, - new HashFilter(hash, - new HexEncoder(new StringSink(digest)))); + StringSource s( + text, true, + new HashFilter(hash, new HexEncoder(new StringSink(digest)))); std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower); return digest; @@ -59,9 +65,10 @@ bool hash::fill_table() if (fs::is_regular_file(path)) { string email = path.filename(); - std::transform(email.begin(), email.end(), email.begin(), ::tolower); - table.insert({ md5(email), email }); - table.insert({ sha256(email), email }); + std::transform(email.begin(), email.end(), email.begin(), + ::tolower); + table.insert({md5(email), email}); + table.insert({sha256(email), email}); } } return true; @@ -84,11 +91,11 @@ bool hash::is_valid(const string &digest) bool hash::not_hex(const char &c) { if (c >= 0x61 && c <= 0x66) - { // a-f + { // a-f return false; } if (c >= 0x30 && c <= 0x39) - { // 0-9 + { // 0-9 return false; } diff --git a/src/http.cpp b/src/http.cpp index 7359590..59668e1 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1,5 +1,5 @@ /* This file is part of libravatarserv. - * Copyright © 2018,2019 tastytea + * Copyright © 2018,2019,2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,15 +14,17 @@ * along with this program. If not, see . */ -#include -#include -#include #include "libravatarserv.hpp" -using std::cout; +#include +#include +#include + using std::cerr; +using std::cout; using std::endl; -using namespace http; +using namespace libravatarserv; +using namespace libravatarserv::http; const Request http::parse_request(const string &request) { @@ -31,8 +33,8 @@ const Request http::parse_request(const string &request) cout << "Status: 404 Not Found\n\n"; std::exit(1); } - if (request.substr(0, 8) != "/avatar/" || - request.find("..", 8) != std::string::npos) + if (request.substr(0, 8) != "/avatar/" + || request.find("..", 8) != std::string::npos) { cout << "Status: 400 Bad Request\n\n"; cerr << "Error: Invalid URL.\n"; @@ -55,7 +57,9 @@ const Request http::parse_request(const string &request) try { size = static_cast(std::stoul(answer)); - } catch (const std::exception &) {} + } + catch (const std::exception &) + {} } else { @@ -65,7 +69,9 @@ const Request http::parse_request(const string &request) try { size = static_cast(std::stoul(answer)); - } catch (const std::exception &) {} + } + catch (const std::exception &) + {} } } if (size > 512) @@ -101,7 +107,7 @@ const Request http::parse_request(const string &request) digest = digest.substr(0, pos_digest); } - return { digest, size, fallback }; + return {digest, static_cast(size), fallback}; } const string http::get_parameter(const string &request, const string ¶meter) @@ -125,7 +131,7 @@ void http::send_redirect(const Request &request) const char *env = std::getenv("HTTPS"); string baseurl; if (env != nullptr && env[1] == 'n') - { // "on" + { // "on" baseurl = "https://seccdn.libravatar.org"; } else @@ -133,7 +139,7 @@ void http::send_redirect(const Request &request) baseurl = "http://cdn.libravatar.org"; } cout << "Status: 307 Temporary Redirect\n"; - cout << "Location: " << baseurl << "/avatar/" - << request.digest << "?s=" << request.size - << "&d=" << request.fallback << endl << endl; + cout << "Location: " << baseurl << "/avatar/" << request.digest + << "?s=" << request.size << "&d=" << request.fallback << endl + << endl; } diff --git a/src/image.cpp b/src/image.cpp index 3791883..6a23657 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,5 +1,5 @@ /* This file is part of libravatarserv. - * Copyright © 2018 tastytea + * Copyright © 2018, 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,19 +14,21 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include #include "libravatarserv.hpp" -using std::cout; +#include +#include +#include +#include +#include +#include +#include + using std::cerr; +using std::cout; using std::endl; -using namespace image; +using namespace libravatarserv; +using namespace libravatarserv::image; const Image image::get(const string &digest, const uint16_t size) { @@ -41,7 +43,7 @@ const Image image::get(const string &digest, const uint16_t size) if (!fs::is_regular_file(filename)) { cerr << "Warning: User not found and no default image set.\n"; - return { 2, img }; + return {2, img}; } } else @@ -65,7 +67,7 @@ const Image image::get(const string &digest, const uint16_t size) error = 5; } - return { error, img }; + return {error, img}; } void image::write(Image &image) @@ -74,10 +76,11 @@ void image::write(Image &image) std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower); Magick::Blob res_buffer; - image.image.magick(magick); // force the same format + image.image.magick(magick); // force the same format image.image.write(&res_buffer); cout << "Content-Type: image/" << magick << endl; cout << "Content-Length: " << res_buffer.length() << endl << endl; - cout.write(static_cast(res_buffer.data()), res_buffer.length()); + cout.write(static_cast(res_buffer.data()), + res_buffer.length()); } diff --git a/src/libravatarserv.cpp b/src/libravatarserv.cpp index e97ba10..ba22de2 100644 --- a/src/libravatarserv.cpp +++ b/src/libravatarserv.cpp @@ -1,5 +1,5 @@ /* This file is part of libravatarserv. - * Copyright © 2018, 2019 tastytea + * Copyright © 2018, 2019, 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,14 +14,16 @@ * along with this program. If not, see . */ -#include +#include "libravatarserv.hpp" +#include "version.hpp" + #include #include -#include "version.hpp" -#include "libravatarserv.hpp" +#include -using std::cout; +using namespace libravatarserv; using std::cerr; +using std::cout; using std::endl; // Global variables @@ -84,8 +86,8 @@ int main() cout << "Status: 307 Temporary Redirect\n"; cout << "Location: " << avatar.fallback << endl << endl; } - else if (avatar.fallback.substr(0, 2) == "mp" || - avatar.fallback.substr(0, 2) == "mm") + else if (avatar.fallback.substr(0, 2) == "mp" + || avatar.fallback.substr(0, 2) == "mm") { // MD5 hash of 'mp' image = image::get("1f2dfa567dcf95833eddf7aec167fec7", avatar.size); @@ -100,12 +102,12 @@ int main() goto not_implemented; } } - else if (avatar.fallback.substr(0, 9) == "identicon" || - avatar.fallback.substr(0, 5) == "retro") + else if (avatar.fallback.substr(0, 9) == "identicon" + || avatar.fallback.substr(0, 5) == "retro") { try { - uint8_t padwidth = avatar.size / 10; + auto padwidth = static_cast(avatar.size / 10); if (avatar.size < 60) { padwidth = 0; @@ -116,17 +118,18 @@ int main() } Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil, - "fefefeff", - { // The same colors ivatar uses. - "2d4fffff", // Blue - "feb42cff", // Yellow - "e279eaff", // Bright pink - "1eb3fdff", // Cyan - "E84D41ff", // Red - "31CB73ff", // Green - "8D45AAff" // Dark pink - }, - { padwidth, padwidth }); + "fefefeff", + { + // The same colors ivatar uses. + "2d4fffff", // Blue + "feb42cff", // Yellow + "e279eaff", // Bright pink + "1eb3fdff", // Cyan + "E84D41ff", // Red + "31CB73ff", // Green + "8D45AAff" // Dark pink + }, + {padwidth, padwidth}); image.image = identicon.generate(avatar.digest, avatar.size); image.image.magick("PNG"); image::write(image); @@ -134,13 +137,13 @@ int main() catch (const std::exception &e) { cout << "Status: 500 Internal Server Error\n\n"; - cerr << "Error: Couldn't generate identicon (" - << e.what() << ").\n"; + cerr << "Error: Couldn't generate identicon (" << e.what() + << ").\n"; } } else { - not_implemented: +not_implemented: if (settings::settings.redirect_nofallback) { http::send_redirect(avatar); diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index 41dcd1d..167f936 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -23,63 +23,68 @@ #include #include +namespace libravatarserv +{ + +using std::int8_t; using std::string; using std::uint16_t; using std::uint8_t; -using std::int8_t; int main(); namespace settings -{ // settings.cpp - extern fs::path avatar_dir; - extern struct Settings - { - string default_fallback = "404"; - bool redirect_nofallback = false; - bool redirect_nouser = false; - } settings; - - bool find_avatar_dir(); - void read_settings(); -} - -namespace http // http.cpp +{ // settings.cpp +extern fs::path avatar_dir; +extern struct Settings { - struct Request - { - const string digest; - const int16_t size = 0; - string fallback; - }; + string default_fallback = "404"; + bool redirect_nofallback = false; + bool redirect_nouser = false; +} settings; - const Request parse_request(const string &request); - const string get_parameter(const string &request, const string ¶meter); - void send_redirect(const Request &request); -} +bool find_avatar_dir(); +void read_settings(); +} // namespace settings -namespace hash // hash.cpp +namespace http // http.cpp { - extern std::map table; +struct Request +{ + const string digest; + const uint16_t size = 0; + string fallback; +}; - const string md5(const string &text); - const string sha256(const string &text); - bool fill_table(); - bool is_valid(const string &digest); - bool not_hex(const char &c); -} +const Request parse_request(const string &request); +const string get_parameter(const string &request, const string ¶meter); +void send_redirect(const Request &request); +} // namespace http + +namespace hash // hash.cpp +{ +extern std::map table; + +const string md5(const string &text); +const string sha256(const string &text); +bool fill_table(); +bool is_valid(const string &digest); +bool not_hex(const char &c); +} // namespace hash namespace image // image.cpp { - struct Image - { - uint8_t error = 0; - Magick::Image image; - }; +struct Image +{ + uint8_t error = 0; + Magick::Image image; +}; - const Image get(const string &digest, const uint16_t size); - void write(Image &image); - Image identicon(const string &digest); -} +const Image get(const string &digest, uint16_t size); +void write(Image &image); +Image identicon(const string &digest); +} // namespace image -#endif // LIBRAVATARSERV_HPP +} // namespace libravatarserv + +#endif // LIBRAVATARSERV_HPP diff --git a/src/settings.cpp b/src/settings.cpp index 794e888..2d82e1c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,5 +1,5 @@ /* This file is part of libravatarserv. - * Copyright © 2018 tastytea + * Copyright © 2018, 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,11 +14,13 @@ * along with this program. If not, see . */ -#include -#include #include "libravatarserv.hpp" -using namespace settings; +#include +#include + +using namespace libravatarserv; +using namespace libravatarserv::settings; bool settings::find_avatar_dir() { @@ -42,8 +44,8 @@ bool settings::find_avatar_dir() for (uint8_t index = 0; index <= size; ++index) { - const string searchdir = data_dirs[index] - + string("/libravatarserv"); + const string searchdir = + data_dirs[index] + string("/libravatarserv"); if (fs::is_directory(searchdir)) { avatar_dir = searchdir; diff --git a/src/version.hpp.in b/src/version.hpp.in index cec7915..cc23a3f 100644 --- a/src/version.hpp.in +++ b/src/version.hpp.in @@ -3,7 +3,7 @@ namespace global { - static constexpr char version[] = "@PROJECT_VERSION@"; -} +static constexpr char version[] = "@PROJECT_VERSION@"; +} // namespace global #endif // VERSION_HPP