Update formatting, add namespace and fix a few warnings.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
846e85aecf
commit
891d5d3ca3
37
src/hash.cpp
37
src/hash.cpp
@ -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,7 +65,8 @@ bool hash::fill_table()
|
||||
if (fs::is_regular_file(path))
|
||||
{
|
||||
string email = path.filename();
|
||||
std::transform(email.begin(), email.end(), email.begin(), ::tolower);
|
||||
std::transform(email.begin(), email.end(), email.begin(),
|
||||
::tolower);
|
||||
table.insert({md5(email), email});
|
||||
table.insert({sha256(email), email});
|
||||
}
|
||||
|
34
src/http.cpp
34
src/http.cpp
@ -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 ¶meter)
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
@ -79,5 +81,6 @@ void image::write(Image &image)
|
||||
|
||||
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());
|
||||
}
|
||||
|
@ -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;
|
||||
@ -117,7 +119,8 @@ int main()
|
||||
|
||||
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil,
|
||||
"fefefeff",
|
||||
{ // The same colors ivatar uses.
|
||||
{
|
||||
// The same colors ivatar uses.
|
||||
"2d4fffff", // Blue
|
||||
"feb42cff", // Yellow
|
||||
"e279eaff", // Bright pink
|
||||
@ -134,8 +137,8 @@ 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
|
||||
|
@ -23,10 +23,13 @@
|
||||
#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();
|
||||
|
||||
@ -42,21 +45,21 @@ namespace settings
|
||||
|
||||
bool find_avatar_dir();
|
||||
void read_settings();
|
||||
}
|
||||
} // namespace settings
|
||||
|
||||
namespace http // http.cpp
|
||||
{
|
||||
struct Request
|
||||
{
|
||||
const string digest;
|
||||
const int16_t size = 0;
|
||||
const uint16_t size = 0;
|
||||
string fallback;
|
||||
};
|
||||
|
||||
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
|
||||
{
|
||||
@ -67,7 +70,7 @@ namespace hash // hash.cpp
|
||||
bool fill_table();
|
||||
bool is_valid(const string &digest);
|
||||
bool not_hex(const char &c);
|
||||
}
|
||||
} // namespace hash
|
||||
|
||||
namespace image // image.cpp
|
||||
{
|
||||
@ -77,9 +80,11 @@ namespace image // image.cpp
|
||||
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);
|
||||
Image identicon(const string &digest);
|
||||
}
|
||||
} // namespace image
|
||||
|
||||
} // namespace libravatarserv
|
||||
|
||||
#endif // LIBRAVATARSERV_HPP
|
||||
|
@ -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;
|
||||
|
@ -4,6 +4,6 @@
|
||||
namespace global
|
||||
{
|
||||
static constexpr char version[] = "@PROJECT_VERSION@";
|
||||
}
|
||||
} // namespace global
|
||||
|
||||
#endif // VERSION_HPP
|
||||
|
Loading…
x
Reference in New Issue
Block a user