Added rate limiting and settings fixes.
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
#include "Images.h"
|
||||
#include "../Cache/Cache.h"
|
||||
#include "../Limiter/RateLimit.h"
|
||||
#include "../Scraping/ImageScraping.h"
|
||||
#include "../Utility/Unescape.h"
|
||||
#include "../Utility/Utility.h"
|
||||
#include "Config.h"
|
||||
|
||||
static char *build_images_request_cache_key(const char *query, int page,
|
||||
const char *client_key) {
|
||||
char scope_key[BUFFER_SIZE_MEDIUM];
|
||||
snprintf(scope_key, sizeof(scope_key), "images_request:%s",
|
||||
client_key ? client_key : "unknown");
|
||||
return cache_compute_key(query, page, scope_key);
|
||||
}
|
||||
|
||||
int images_handler(UrlParams *params) {
|
||||
extern Config global_config;
|
||||
TemplateContext ctx = new_context();
|
||||
char *raw_query = "";
|
||||
int page = 1;
|
||||
@@ -52,12 +63,55 @@ int images_handler(UrlParams *params) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char client_key[BUFFER_SIZE_SMALL];
|
||||
rate_limit_get_client_key(client_key, sizeof(client_key));
|
||||
|
||||
char *request_cache_key =
|
||||
build_images_request_cache_key(raw_query, page, client_key);
|
||||
int request_is_cached = 0;
|
||||
|
||||
if (request_cache_key && get_cache_ttl_image() > 0) {
|
||||
char *cached_marker = NULL;
|
||||
size_t cached_marker_size = 0;
|
||||
|
||||
if (cache_get(request_cache_key, (time_t)get_cache_ttl_image(),
|
||||
&cached_marker, &cached_marker_size) == 0) {
|
||||
request_is_cached = 1;
|
||||
}
|
||||
|
||||
free(cached_marker);
|
||||
}
|
||||
|
||||
if (!request_is_cached) {
|
||||
RateLimitConfig rate_limit_config = {
|
||||
.max_requests = global_config.rate_limit_images_requests,
|
||||
.interval_seconds = global_config.rate_limit_images_interval,
|
||||
};
|
||||
RateLimitResult rate_limit_result =
|
||||
rate_limit_check("images", &rate_limit_config);
|
||||
if (rate_limit_result.limited) {
|
||||
char response[256];
|
||||
snprintf(response, sizeof(response),
|
||||
"<h1>Slow down!</h1><p>Too many image searches from you!</p>");
|
||||
send_response(response);
|
||||
free(request_cache_key);
|
||||
free(display_query);
|
||||
free_context(&ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (request_cache_key && get_cache_ttl_image() > 0) {
|
||||
cache_set(request_cache_key, "1", 1);
|
||||
}
|
||||
}
|
||||
|
||||
ImageResult *results = NULL;
|
||||
int result_count = 0;
|
||||
|
||||
if (scrape_images(raw_query, page, &results, &result_count) != 0 ||
|
||||
!results) {
|
||||
send_response("<h1>Error fetching images</h1>");
|
||||
free(request_cache_key);
|
||||
free(display_query);
|
||||
free_context(&ctx);
|
||||
return -1;
|
||||
@@ -72,6 +126,7 @@ int images_handler(UrlParams *params) {
|
||||
if (inner_counts)
|
||||
free(inner_counts);
|
||||
free_image_results(results, result_count);
|
||||
free(request_cache_key);
|
||||
free(display_query);
|
||||
free_context(&ctx);
|
||||
return -1;
|
||||
@@ -106,6 +161,7 @@ int images_handler(UrlParams *params) {
|
||||
free(inner_counts);
|
||||
|
||||
free_image_results(results, result_count);
|
||||
free(request_cache_key);
|
||||
free(display_query);
|
||||
free_context(&ctx);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user