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.
* 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
* 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/>.
*/
#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"
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)
{
@ -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;
}

View File

@ -1,5 +1,5 @@
/* 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
* 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/>.
*/
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include "libravatarserv.hpp"
using std::cout;
#include <algorithm>
#include <cstdlib>
#include <iostream>
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<int16_t>(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<int16_t>(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<uint16_t>(size), fallback};
}
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");
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;
}

View File

@ -1,5 +1,5 @@
/* 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
* 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/>.
*/
#include <iostream>
#include <sstream>
#include <array>
#include <algorithm>
#include <Magick++/Geometry.h>
#include <Magick++/Color.h>
#include <Magick++/Exception.h>
#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::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<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.
* 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
* 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/>.
*/
#include <iostream>
#include "libravatarserv.hpp"
#include "version.hpp"
#include <Magick++/Geometry.h>
#include <identiconpp.hpp>
#include "version.hpp"
#include "libravatarserv.hpp"
#include <iostream>
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<uint8_t>(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);

View File

@ -23,63 +23,68 @@
#include <map>
#include <string>
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 &parameter);
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<const string, const string> 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 &parameter);
void send_redirect(const Request &request);
} // namespace http
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
{
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

View File

@ -1,5 +1,5 @@
/* 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
* 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/>.
*/
#include <cstdlib>
#include <basedir.h>
#include "libravatarserv.hpp"
using namespace settings;
#include <basedir.h>
#include <cstdlib>
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;

View File

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