Update formatting, add namespace and fix a few warnings.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2020-10-24 11:25:15 +02:00
parent 846e85aecf
commit 891d5d3ca3
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 149 additions and 123 deletions

View File

@ -1,5 +1,5 @@
/* This file is part of libravatarserv. /* This file is part of libravatarserv.
* Copyright © 2018 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,15 +14,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <algorithm>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include "libravatarserv.hpp" #include "libravatarserv.hpp"
using namespace hash; #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <cryptopp/md5.h>
#include <cryptopp/sha.h>
#include <algorithm>
#include <string>
using std::string;
using namespace libravatarserv;
using namespace libravatarserv::hash;
const string hash::md5(const string &text) const string hash::md5(const string &text)
{ {
@ -30,9 +36,9 @@ const string hash::md5(const string &text)
Weak::MD5 hash; Weak::MD5 hash;
string digest; string digest;
StringSource s(text, true, StringSource s(
new HashFilter(hash, text, true,
new HexEncoder(new StringSink(digest)))); new HashFilter(hash, new HexEncoder(new StringSink(digest))));
std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower); std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower);
return digest; return digest;
} }
@ -43,9 +49,9 @@ const string hash::sha256(const string &text)
SHA256 hash; SHA256 hash;
string digest; string digest;
StringSource s(text, true, StringSource s(
new HashFilter(hash, text, true,
new HexEncoder(new StringSink(digest)))); new HashFilter(hash, new HexEncoder(new StringSink(digest))));
std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower); std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower);
return digest; return digest;
@ -59,9 +65,10 @@ bool hash::fill_table()
if (fs::is_regular_file(path)) if (fs::is_regular_file(path))
{ {
string email = path.filename(); string email = path.filename();
std::transform(email.begin(), email.end(), email.begin(), ::tolower); std::transform(email.begin(), email.end(), email.begin(),
table.insert({ md5(email), email }); ::tolower);
table.insert({ sha256(email), email }); table.insert({md5(email), email});
table.insert({sha256(email), email});
} }
} }
return true; return true;
@ -84,11 +91,11 @@ bool hash::is_valid(const string &digest)
bool hash::not_hex(const char &c) bool hash::not_hex(const char &c)
{ {
if (c >= 0x61 && c <= 0x66) if (c >= 0x61 && c <= 0x66)
{ // a-f { // a-f
return false; return false;
} }
if (c >= 0x30 && c <= 0x39) if (c >= 0x30 && c <= 0x39)
{ // 0-9 { // 0-9
return false; return false;
} }

View File

@ -1,5 +1,5 @@
/* This file is part of libravatarserv. /* This file is part of libravatarserv.
* Copyright © 2018,2019 tastytea <tastytea@tastytea.de> * Copyright © 2018,2019,2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,15 +14,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include "libravatarserv.hpp" #include "libravatarserv.hpp"
using std::cout; #include <algorithm>
#include <cstdlib>
#include <iostream>
using std::cerr; using std::cerr;
using std::cout;
using std::endl; using std::endl;
using namespace http; using namespace libravatarserv;
using namespace libravatarserv::http;
const Request http::parse_request(const string &request) 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"; cout << "Status: 404 Not Found\n\n";
std::exit(1); std::exit(1);
} }
if (request.substr(0, 8) != "/avatar/" || if (request.substr(0, 8) != "/avatar/"
request.find("..", 8) != std::string::npos) || request.find("..", 8) != std::string::npos)
{ {
cout << "Status: 400 Bad Request\n\n"; cout << "Status: 400 Bad Request\n\n";
cerr << "Error: Invalid URL.\n"; cerr << "Error: Invalid URL.\n";
@ -55,7 +57,9 @@ const Request http::parse_request(const string &request)
try try
{ {
size = static_cast<int16_t>(std::stoul(answer)); size = static_cast<int16_t>(std::stoul(answer));
} catch (const std::exception &) {} }
catch (const std::exception &)
{}
} }
else else
{ {
@ -65,7 +69,9 @@ const Request http::parse_request(const string &request)
try try
{ {
size = static_cast<int16_t>(std::stoul(answer)); size = static_cast<int16_t>(std::stoul(answer));
} catch (const std::exception &) {} }
catch (const std::exception &)
{}
} }
} }
if (size > 512) if (size > 512)
@ -101,7 +107,7 @@ const Request http::parse_request(const string &request)
digest = digest.substr(0, pos_digest); digest = digest.substr(0, pos_digest);
} }
return { digest, size, fallback }; return {digest, static_cast<uint16_t>(size), fallback};
} }
const string http::get_parameter(const string &request, const string &parameter) const string http::get_parameter(const string &request, const string &parameter)
@ -125,7 +131,7 @@ void http::send_redirect(const Request &request)
const char *env = std::getenv("HTTPS"); const char *env = std::getenv("HTTPS");
string baseurl; string baseurl;
if (env != nullptr && env[1] == 'n') if (env != nullptr && env[1] == 'n')
{ // "on" { // "on"
baseurl = "https://seccdn.libravatar.org"; baseurl = "https://seccdn.libravatar.org";
} }
else else
@ -133,7 +139,7 @@ void http::send_redirect(const Request &request)
baseurl = "http://cdn.libravatar.org"; baseurl = "http://cdn.libravatar.org";
} }
cout << "Status: 307 Temporary Redirect\n"; cout << "Status: 307 Temporary Redirect\n";
cout << "Location: " << baseurl << "/avatar/" cout << "Location: " << baseurl << "/avatar/" << request.digest
<< request.digest << "?s=" << request.size << "?s=" << request.size << "&d=" << request.fallback << endl
<< "&d=" << request.fallback << endl << endl; << endl;
} }

View File

@ -1,5 +1,5 @@
/* This file is part of libravatarserv. /* This file is part of libravatarserv.
* Copyright © 2018 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,19 +14,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream>
#include <sstream>
#include <array>
#include <algorithm>
#include <Magick++/Geometry.h>
#include <Magick++/Color.h>
#include <Magick++/Exception.h>
#include "libravatarserv.hpp" #include "libravatarserv.hpp"
using std::cout; #include <Magick++/Color.h>
#include <Magick++/Exception.h>
#include <Magick++/Geometry.h>
#include <algorithm>
#include <array>
#include <iostream>
#include <sstream>
using std::cerr; using std::cerr;
using std::cout;
using std::endl; using std::endl;
using namespace image; using namespace libravatarserv;
using namespace libravatarserv::image;
const Image image::get(const string &digest, const uint16_t size) 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)) if (!fs::is_regular_file(filename))
{ {
cerr << "Warning: User not found and no default image set.\n"; cerr << "Warning: User not found and no default image set.\n";
return { 2, img }; return {2, img};
} }
} }
else else
@ -65,7 +67,7 @@ const Image image::get(const string &digest, const uint16_t size)
error = 5; error = 5;
} }
return { error, img }; return {error, img};
} }
void image::write(Image &image) void image::write(Image &image)
@ -74,10 +76,11 @@ void image::write(Image &image)
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower); std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
Magick::Blob res_buffer; 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); image.image.write(&res_buffer);
cout << "Content-Type: image/" << magick << endl; cout << "Content-Type: image/" << magick << endl;
cout << "Content-Length: " << res_buffer.length() << endl << endl; cout << "Content-Length: " << res_buffer.length() << endl << endl;
cout.write(static_cast<const char*>(res_buffer.data()), res_buffer.length()); cout.write(static_cast<const char *>(res_buffer.data()),
res_buffer.length());
} }

View File

@ -1,5 +1,5 @@
/* This file is part of libravatarserv. /* This file is part of libravatarserv.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2019, 2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,14 +14,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream> #include "libravatarserv.hpp"
#include "version.hpp"
#include <Magick++/Geometry.h> #include <Magick++/Geometry.h>
#include <identiconpp.hpp> #include <identiconpp.hpp>
#include "version.hpp" #include <iostream>
#include "libravatarserv.hpp"
using std::cout; using namespace libravatarserv;
using std::cerr; using std::cerr;
using std::cout;
using std::endl; using std::endl;
// Global variables // Global variables
@ -84,8 +86,8 @@ int main()
cout << "Status: 307 Temporary Redirect\n"; cout << "Status: 307 Temporary Redirect\n";
cout << "Location: " << avatar.fallback << endl << endl; cout << "Location: " << avatar.fallback << endl << endl;
} }
else if (avatar.fallback.substr(0, 2) == "mp" || else if (avatar.fallback.substr(0, 2) == "mp"
avatar.fallback.substr(0, 2) == "mm") || avatar.fallback.substr(0, 2) == "mm")
{ {
// MD5 hash of 'mp' // MD5 hash of 'mp'
image = image::get("1f2dfa567dcf95833eddf7aec167fec7", avatar.size); image = image::get("1f2dfa567dcf95833eddf7aec167fec7", avatar.size);
@ -100,12 +102,12 @@ int main()
goto not_implemented; goto not_implemented;
} }
} }
else if (avatar.fallback.substr(0, 9) == "identicon" || else if (avatar.fallback.substr(0, 9) == "identicon"
avatar.fallback.substr(0, 5) == "retro") || avatar.fallback.substr(0, 5) == "retro")
{ {
try try
{ {
uint8_t padwidth = avatar.size / 10; auto padwidth = static_cast<uint8_t>(avatar.size / 10);
if (avatar.size < 60) if (avatar.size < 60)
{ {
padwidth = 0; padwidth = 0;
@ -116,17 +118,18 @@ int main()
} }
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil, Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil,
"fefefeff", "fefefeff",
{ // The same colors ivatar uses. {
"2d4fffff", // Blue // The same colors ivatar uses.
"feb42cff", // Yellow "2d4fffff", // Blue
"e279eaff", // Bright pink "feb42cff", // Yellow
"1eb3fdff", // Cyan "e279eaff", // Bright pink
"E84D41ff", // Red "1eb3fdff", // Cyan
"31CB73ff", // Green "E84D41ff", // Red
"8D45AAff" // Dark pink "31CB73ff", // Green
}, "8D45AAff" // Dark pink
{ padwidth, padwidth }); },
{padwidth, padwidth});
image.image = identicon.generate(avatar.digest, avatar.size); image.image = identicon.generate(avatar.digest, avatar.size);
image.image.magick("PNG"); image.image.magick("PNG");
image::write(image); image::write(image);
@ -134,13 +137,13 @@ int main()
catch (const std::exception &e) catch (const std::exception &e)
{ {
cout << "Status: 500 Internal Server Error\n\n"; cout << "Status: 500 Internal Server Error\n\n";
cerr << "Error: Couldn't generate identicon (" cerr << "Error: Couldn't generate identicon (" << e.what()
<< e.what() << ").\n"; << ").\n";
} }
} }
else else
{ {
not_implemented: not_implemented:
if (settings::settings.redirect_nofallback) if (settings::settings.redirect_nofallback)
{ {
http::send_redirect(avatar); http::send_redirect(avatar);

View File

@ -23,63 +23,68 @@
#include <map> #include <map>
#include <string> #include <string>
namespace libravatarserv
{
using std::int8_t;
using std::string; using std::string;
using std::uint16_t; using std::uint16_t;
using std::uint8_t; using std::uint8_t;
using std::int8_t;
int main(); int main();
namespace settings namespace settings
{ // settings.cpp { // settings.cpp
extern fs::path avatar_dir; extern fs::path avatar_dir;
extern struct Settings 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
{ {
struct Request string default_fallback = "404";
{ bool redirect_nofallback = false;
const string digest; bool redirect_nouser = false;
const int16_t size = 0; } settings;
string fallback;
};
const Request parse_request(const string &request); bool find_avatar_dir();
const string get_parameter(const string &request, const string &parameter); void read_settings();
void send_redirect(const Request &request); } // namespace settings
}
namespace hash // hash.cpp namespace http // http.cpp
{ {
extern std::map<const string, const string> table; struct Request
{
const string digest;
const uint16_t size = 0;
string fallback;
};
const string md5(const string &text); const Request parse_request(const string &request);
const string sha256(const string &text); const string get_parameter(const string &request, const string &parameter);
bool fill_table(); void send_redirect(const Request &request);
bool is_valid(const string &digest); } // namespace http
bool not_hex(const char &c);
} namespace hash // hash.cpp
{
extern std::map<const string, const string> 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 namespace image // image.cpp
{ {
struct Image struct Image
{ {
uint8_t error = 0; uint8_t error = 0;
Magick::Image image; Magick::Image image;
}; };
const Image get(const string &digest, const uint16_t size); const Image get(const string &digest, uint16_t size);
void write(Image &image); void write(Image &image);
Image identicon(const string &digest); Image identicon(const string &digest);
} } // namespace image
#endif // LIBRAVATARSERV_HPP } // namespace libravatarserv
#endif // LIBRAVATARSERV_HPP

View File

@ -1,5 +1,5 @@
/* This file is part of libravatarserv. /* This file is part of libravatarserv.
* Copyright © 2018 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,11 +14,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <cstdlib>
#include <basedir.h>
#include "libravatarserv.hpp" #include "libravatarserv.hpp"
using namespace settings; #include <basedir.h>
#include <cstdlib>
using namespace libravatarserv;
using namespace libravatarserv::settings;
bool settings::find_avatar_dir() bool settings::find_avatar_dir()
{ {
@ -42,8 +44,8 @@ bool settings::find_avatar_dir()
for (uint8_t index = 0; index <= size; ++index) for (uint8_t index = 0; index <= size; ++index)
{ {
const string searchdir = data_dirs[index] const string searchdir =
+ string("/libravatarserv"); data_dirs[index] + string("/libravatarserv");
if (fs::is_directory(searchdir)) if (fs::is_directory(searchdir))
{ {
avatar_dir = searchdir; avatar_dir = searchdir;

View File

@ -3,7 +3,7 @@
namespace global namespace global
{ {
static constexpr char version[] = "@PROJECT_VERSION@"; static constexpr char version[] = "@PROJECT_VERSION@";
} } // namespace global
#endif // VERSION_HPP #endif // VERSION_HPP