Added setting to configure the default fallback.

This commit is contained in:
tastytea 2018-11-27 15:42:43 +01:00
parent f0406f7e3a
commit 4d0033652d
Signed by untrusted user: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 31 additions and 6 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2) cmake_minimum_required (VERSION 3.2)
project(libravatarserv project(libravatarserv
VERSION 0.5.0 VERSION 0.6.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -14,9 +14,11 @@ images tied to email or OpenID addresses.
* Variable image size (`s` or `size`) * Variable image size (`s` or `size`)
* Default actions (`d` or `default`): 404, URL, mp/mm, identicon * Default actions (`d` or `default`): 404, URL, mp/mm, identicon
The default behaviour for unknown users is to return a 404 error. If a default The default behaviour for unknown users is to return a 404 error. You can change
image is put in the avatar directory, that is returned instead. The default that by setting the environment variable `LIBRAVATARSERV_DEFAULT_FALLBACK` to
image can not be overriden by adding the parameter `d` or `default` to the URL. any value accepted by `d` or `default`. If a default image is put in the avatar
directory, that is returned instead. The default image can not be overridden by
adding the parameter `d` or `default` to the URL.
Clients are asked to cache results for up to 1 day to reduce load. This can Clients are asked to cache results for up to 1 day to reduce load. This can
apply to negative results as well. apply to negative results as well.
@ -50,7 +52,7 @@ HTTPS.
libravatarserv looks in each of `${XDG_DATA_DIRS}` for a directory named libravatarserv looks in each of `${XDG_DATA_DIRS}` for a directory named
`libravatarserv`. You can force a different directory by setting the environment `libravatarserv`. You can force a different directory by setting the environment
variable `${LIBRAVATARSERV_DIR}` (until 0.3.0 `${AVATAR_DIR}`). variable `LIBRAVATARSERV_DIR` (until 0.3.0 `AVATAR_DIR`).
The image files are named like your email address, no file extension. If you The image files are named like your email address, no file extension. If you
want to deliver a default image for unknown email addresses, name it `default`. want to deliver a default image for unknown email addresses, name it `default`.

View File

@ -13,6 +13,7 @@ server {
include /etc/nginx/fastcgi_params; include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1; fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1;
fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv; fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv;
fastcgi_param LIBRAVATARSERV_DEFAULT_FALLBACK 404;
} }
} }
@ -28,5 +29,6 @@ server {
include /etc/nginx/fastcgi_params; include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1; fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1;
fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv; fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv;
fastcgi_param LIBRAVATARSERV_DEFAULT_FALLBACK 404;
} }
} }

View File

@ -24,10 +24,12 @@ using std::cout;
using std::cerr; using std::cerr;
using std::endl; using std::endl;
using global::avatar_dir; using global::avatar_dir;
using global::settings;
// Global variables // Global variables
std::map<const string, const string> hash::table; std::map<const string, const string> hash::table;
fs::path global::avatar_dir = ""; fs::path global::avatar_dir = "";
global::Settings global::settings;
int main() int main()
@ -51,6 +53,7 @@ int main()
return 3; return 3;
} }
hash::fill_table(); hash::fill_table();
read_settings();
http::Request avatar = http::parse_request(request); http::Request avatar = http::parse_request(request);
@ -62,6 +65,10 @@ int main()
else else
{ {
cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n"; cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n";
if (avatar.fallback.empty())
{
avatar.fallback = settings.default_fallback;
}
if (avatar.fallback.substr(0, 4) == "http") if (avatar.fallback.substr(0, 4) == "http")
{ {
cout << "Status: 307 Temporary Redirect\n"; cout << "Status: 307 Temporary Redirect\n";
@ -145,3 +152,12 @@ bool find_avatar_dir()
return true; return true;
} }
void read_settings()
{
const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK");
if (env != nullptr)
{
global::settings.default_fallback = env;
}
}

View File

@ -37,10 +37,15 @@ using std::uint8_t;
int main(); int main();
bool find_avatar_dir(); bool find_avatar_dir();
void read_settings();
namespace global namespace global
{ {
extern fs::path avatar_dir; extern fs::path avatar_dir;
extern struct Settings
{
string default_fallback;
} settings;
} }
namespace http // http.cpp namespace http // http.cpp
@ -49,7 +54,7 @@ namespace http // http.cpp
{ {
const string digest; const string digest;
const uint16_t size; const uint16_t size;
const string fallback; string fallback;
}; };
const Request parse_request(const string &request); const Request parse_request(const string &request);