diff --git a/README.md b/README.md index a7b034d..95b3fa2 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,35 @@ The avatar directory could look like this: └── [ 16] user+newsletter@example.com -> user@example.com ``` +### Configuration + +Configuration is done through environment variables. + +#### LIBRAVATARSERV_DIR + +The directory containing the avatars. + +Default: empty + +#### LIBRAVATARSERV_DEFAULT_FALLBACK + +Controls what happens if no fallback was requested. + +Default: 404 + +#### LIBRAVATARSERV_REDIRECT_NOFALLBACK + +Set to 1 to redirect to libravatar.org if the requested fallback is not +supported. + +Default: 0 + +#### LIBRAVATARSERV_REDIRECT_NOUSER + +Set to 1 to redirect to libravatar.org if the user is not found. + +Default: 0 + ## Install ### Gentoo diff --git a/doc/nginx-example.conf b/doc/nginx-example.conf index 9044b54..8bb7a27 100644 --- a/doc/nginx-example.conf +++ b/doc/nginx-example.conf @@ -13,7 +13,10 @@ 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; + + # fastcgi_param LIBRAVATARSERV_DEFAULT_FALLBACK 404; + # fastcgi_param LIBRAVATARSERV_REDIRECT_NOFALLBACK 0; + # fastcgi_param LIBRAVATARSERV_REDIRECT_NOUSER 0; } } @@ -29,6 +32,5 @@ 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/http.cpp b/src/http.cpp index 3c8cefe..e6e565c 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -21,6 +21,7 @@ using std::cout; using std::cerr; +using std::endl; using namespace http; const Request http::parse_request(const string &request) @@ -109,3 +110,21 @@ const string http::get_parameter(const string &request, const string ¶meter) return ""; } + +void http::send_redirect(const Request &request) +{ + const char *env = std::getenv("HTTPS"); + string baseurl; + if (env != nullptr && env[1] == 'n') + { + baseurl = "https://seccdn.libravatar.org"; + } + else + { + 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; +} diff --git a/src/libravatarserv.cpp b/src/libravatarserv.cpp index afdb9f6..5b345fa 100644 --- a/src/libravatarserv.cpp +++ b/src/libravatarserv.cpp @@ -65,11 +65,20 @@ int main() else { cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n"; + if (global::settings.redirect_nouser) + { + http::send_redirect(avatar); + return 0; + } if (avatar.fallback.empty()) { avatar.fallback = settings.default_fallback; } - if (avatar.fallback.substr(0, 4) == "http") + if (avatar.fallback.substr(0, 3) == "404") + { + cout << "Status: 404 Not Found\n\n"; + } + else if (avatar.fallback.substr(0, 4) == "http") { cout << "Status: 307 Temporary Redirect\n"; cout << "Location: " << avatar.fallback << endl << endl; @@ -106,8 +115,16 @@ int main() } else { - // If fallback is empty or unknown - cout << "Status: 404 Not Found\n\n"; + if (global::settings.redirect_nofallback) + { + cout << "Status: 307 Temporary Redirect\n"; + http::send_redirect(avatar); + return 0; + } + else + { + cout << "Status: 404 Not Found\n\n"; + } } } @@ -160,4 +177,16 @@ void read_settings() { global::settings.default_fallback = env; } + + env = std::getenv("LIBRAVATARSERV_REDIRECT_NOFALLBACK"); + if (env != nullptr && env[0] == '1') + { + global::settings.redirect_nofallback = true; + } + + env = std::getenv("LIBRAVATARSERV_REDIRECT_NOUSER"); + if (env != nullptr && env[0] == '1') + { + global::settings.redirect_nouser = true; + } } diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index 63451a9..b39e64f 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -44,7 +44,9 @@ namespace global extern fs::path avatar_dir; extern struct Settings { - string default_fallback; + string default_fallback = ""; + bool redirect_nofallback = false; + bool redirect_nouser = false; } settings; } @@ -59,6 +61,7 @@ namespace http // http.cpp const Request parse_request(const string &request); const string get_parameter(const string &request, const string ¶meter); + void send_redirect(const Request &request); } namespace hash // hash.cpp