I want to serve static galleries, and I would like to know if G-WAN can restrict hot-linking to specified files?
Restricting image hot-linking would be good, but what I really want to know if there is possibility to disallow hot-linking images but allow hot-linking images that name ends with '_thumb' (thumbnails)?
if image_name_wo_ext end with '_thumb':
allow image hot-linking
else:
disallow image hot-linking
Thanks!
Here is how to proceed:
This is can be done with a G-WAN "connection handler"
or with a MIME "content-type handler"
if you want to restrict the check to *.gif or to *.png files:
http_t *http = (http_t*)get_env(argv, HTTP_HEADERS);
static char my_site[] = "www.my_site.com";
if(strcmp(my_site, http->h_referer)) // not my site
{
char *request = (char*)get_env(argv, REQUEST);
if(strstr(request, "_thumb"))
return 0; // 0: Close the client connection
}
return 255; // continue normally
Alternatively, you can redirect to another page or image instead of just closing the connection:
char szURI[] = "http://another-place.org";
xbuf_t *reply = get_reply(argv);
xbuf_xcat(reply,
"<html><head><title>Redirect</title></head>"
"<body>Click <a href=\"%s\">here</a>.</body></html>",
szURI);
// set the HTTP reply code accordingly
int *pHTTP_status = (int*)get_env(argv, HTTP_CODE);
if(pHTTP_status)
*pHTTP_status = 301; // 301:'moved permanently'
// 2: Send a server reply based on a reply buffer/HTTP status code
return 2;