forked from tastytea/libravatarserv
Added settings to redirect to libravatar.org if the user was not found
or the fallback is unknown.
This commit is contained in:
parent
4d0033652d
commit
fbf7437dd9
29
README.md
29
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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
19
src/http.cpp
19
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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user