Update formatting, add namespace and fix a few warnings.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
846e85aecf
commit
891d5d3ca3
41
src/hash.cpp
41
src/hash.cpp
|
@ -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;
|
||||||
|
|
34
src/http.cpp
34
src/http.cpp
|
@ -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 ¶meter)
|
const string http::get_parameter(const string &request, const string ¶meter)
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -79,5 +81,6 @@ void image::write(Image &image)
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -117,7 +119,8 @@ int main()
|
||||||
|
|
||||||
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil,
|
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil,
|
||||||
"fefefeff",
|
"fefefeff",
|
||||||
{ // The same colors ivatar uses.
|
{
|
||||||
|
// The same colors ivatar uses.
|
||||||
"2d4fffff", // Blue
|
"2d4fffff", // Blue
|
||||||
"feb42cff", // Yellow
|
"feb42cff", // Yellow
|
||||||
"e279eaff", // Bright pink
|
"e279eaff", // Bright pink
|
||||||
|
@ -126,7 +129,7 @@ int main()
|
||||||
"31CB73ff", // Green
|
"31CB73ff", // Green
|
||||||
"8D45AAff" // Dark pink
|
"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);
|
||||||
|
|
|
@ -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";
|
string default_fallback = "404";
|
||||||
bool redirect_nofallback = false;
|
bool redirect_nofallback = false;
|
||||||
bool redirect_nouser = false;
|
bool redirect_nouser = false;
|
||||||
} settings;
|
} settings;
|
||||||
|
|
||||||
bool find_avatar_dir();
|
bool find_avatar_dir();
|
||||||
void read_settings();
|
void read_settings();
|
||||||
}
|
} // namespace settings
|
||||||
|
|
||||||
namespace http // http.cpp
|
namespace http // http.cpp
|
||||||
{
|
{
|
||||||
struct Request
|
struct Request
|
||||||
{
|
{
|
||||||
const string digest;
|
const string digest;
|
||||||
const int16_t size = 0;
|
const uint16_t size = 0;
|
||||||
string fallback;
|
string fallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Request parse_request(const string &request);
|
const Request parse_request(const string &request);
|
||||||
const string get_parameter(const string &request, const string ¶meter);
|
const string get_parameter(const string &request, const string ¶meter);
|
||||||
void send_redirect(const Request &request);
|
void send_redirect(const Request &request);
|
||||||
}
|
} // namespace http
|
||||||
|
|
||||||
namespace hash // hash.cpp
|
namespace hash // hash.cpp
|
||||||
{
|
{
|
||||||
extern std::map<const string, const string> table;
|
extern std::map<const string, const string> table;
|
||||||
|
|
||||||
const string md5(const string &text);
|
const string md5(const string &text);
|
||||||
const string sha256(const string &text);
|
const string sha256(const string &text);
|
||||||
bool fill_table();
|
bool fill_table();
|
||||||
bool is_valid(const string &digest);
|
bool is_valid(const string &digest);
|
||||||
bool not_hex(const char &c);
|
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
|
||||||
|
|
||||||
|
} // namespace libravatarserv
|
||||||
|
|
||||||
#endif // LIBRAVATARSERV_HPP
|
#endif // LIBRAVATARSERV_HPP
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue