diff --git a/CMakeLists.txt b/CMakeLists.txt index 261fa14..da4a788 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(libravatarserv - VERSION 0.5.0 + VERSION 0.6.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 582a0bb..a7b034d 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,11 @@ images tied to email or OpenID addresses. * Variable image size (`s` or `size`) * 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 -image is put in the avatar directory, that is returned instead. The default -image can not be overriden by adding the parameter `d` or `default` to the URL. +The default behaviour for unknown users is to return a 404 error. You can change +that by setting the environment variable `LIBRAVATARSERV_DEFAULT_FALLBACK` to +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 apply to negative results as well. @@ -50,7 +52,7 @@ HTTPS. libravatarserv looks in each of `${XDG_DATA_DIRS}` for a directory named `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 want to deliver a default image for unknown email addresses, name it `default`. diff --git a/doc/nginx-example.conf b/doc/nginx-example.conf index 4da88bf..9044b54 100644 --- a/doc/nginx-example.conf +++ b/doc/nginx-example.conf @@ -13,6 +13,7 @@ server { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1; fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv; + fastcgi_param LIBRAVATARSERV_DEFAULT_FALLBACK 404; } } @@ -28,5 +29,6 @@ server { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/cgi-fcgiwrap.socket-1; fastcgi_param SCRIPT_FILENAME /usr/bin/libravatarserv; + fastcgi_param LIBRAVATARSERV_DEFAULT_FALLBACK 404; } } diff --git a/src/libravatarserv.cpp b/src/libravatarserv.cpp index 3863e15..afdb9f6 100644 --- a/src/libravatarserv.cpp +++ b/src/libravatarserv.cpp @@ -24,10 +24,12 @@ using std::cout; using std::cerr; using std::endl; using global::avatar_dir; +using global::settings; // Global variables std::map hash::table; fs::path global::avatar_dir = ""; +global::Settings global::settings; int main() @@ -51,6 +53,7 @@ int main() return 3; } hash::fill_table(); + read_settings(); http::Request avatar = http::parse_request(request); @@ -62,6 +65,10 @@ int main() else { 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") { cout << "Status: 307 Temporary Redirect\n"; @@ -145,3 +152,12 @@ bool find_avatar_dir() return true; } + +void read_settings() +{ + const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK"); + if (env != nullptr) + { + global::settings.default_fallback = env; + } +} diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index ced0036..63451a9 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -37,10 +37,15 @@ using std::uint8_t; int main(); bool find_avatar_dir(); +void read_settings(); namespace global { extern fs::path avatar_dir; + extern struct Settings + { + string default_fallback; + } settings; } namespace http // http.cpp @@ -49,7 +54,7 @@ namespace http // http.cpp { const string digest; const uint16_t size; - const string fallback; + string fallback; }; const Request parse_request(const string &request);