diff options
| author | frosty <gabriel@bwaaa.monster> | 2026-03-05 04:50:32 +0000 |
|---|---|---|
| committer | frosty <gabriel@bwaaa.monster> | 2026-03-05 04:50:32 +0000 |
| commit | 24cec7a35072ac50e1fe902a925e69330866dbca (patch) | |
| tree | f60bb7c38b0b952e074f9b8f8d1cef758d7f8ec8 /src/Routes/ImageProxy.c | |
| parent | 5ed5a6ecc749e8b557ea62f04a444e203f4e48f6 (diff) | |
| download | omnisearch-24cec7a35072ac50e1fe902a925e69330866dbca.tar.gz | |
Diffstat (limited to 'src/Routes/ImageProxy.c')
| -rw-r--r-- | src/Routes/ImageProxy.c | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/Routes/ImageProxy.c b/src/Routes/ImageProxy.c index 5141cd5..10fbd19 100644 --- a/src/Routes/ImageProxy.c +++ b/src/Routes/ImageProxy.c @@ -16,9 +16,9 @@ typedef struct { static int is_allowed_domain(const char *url) { const char *protocol = strstr(url, "://"); if (!protocol) { - protocol = url; + protocol = url; } else { - protocol += 3; + protocol += 3; } const char *path = strchr(protocol, '/'); @@ -26,49 +26,49 @@ static int is_allowed_domain(const char *url) { char host[256] = {0}; if (host_len >= sizeof(host)) { - host_len = sizeof(host) - 1; + host_len = sizeof(host) - 1; } strncpy(host, protocol, host_len); const char *allowed_domains[] = { - "mm.bing.net", - "th.bing.com", - NULL + "mm.bing.net", + "th.bing.com", + NULL }; for (int i = 0; allowed_domains[i] != NULL; i++) { - size_t domain_len = strlen(allowed_domains[i]); - size_t host_str_len = strlen(host); - - if (host_str_len >= domain_len) { - const char *suffix = host + host_str_len - domain_len; - if (strcmp(suffix, allowed_domains[i]) == 0) { - return 1; - } + size_t domain_len = strlen(allowed_domains[i]); + size_t host_str_len = strlen(host); + + if (host_str_len >= domain_len) { + const char *suffix = host + host_str_len - domain_len; + if (strcmp(suffix, allowed_domains[i]) == 0) { + return 1; } } + } return 0; } static size_t write_callback(void *contents, size_t size, size_t nmemb, - void *userp) { + void *userp) { size_t realsize = size * nmemb; MemoryBuffer *buf = (MemoryBuffer *)userp; if (buf->size + realsize > MAX_IMAGE_SIZE) { - return 0; + return 0; } if (buf->size + realsize > buf->capacity) { - size_t new_capacity = buf->capacity * 2; - if (new_capacity < buf->size + realsize) { - new_capacity = buf->size + realsize; - } - char *new_data = realloc(buf->data, new_capacity); - if (!new_data) return 0; - buf->data = new_data; - buf->capacity = new_capacity; + size_t new_capacity = buf->capacity * 2; + if (new_capacity < buf->size + realsize) { + new_capacity = buf->size + realsize; + } + char *new_data = realloc(buf->data, new_capacity); + if (!new_data) return 0; + buf->data = new_data; + buf->capacity = new_capacity; } memcpy(buf->data + buf->size, contents, realsize); @@ -79,38 +79,38 @@ static size_t write_callback(void *contents, size_t size, size_t nmemb, int image_proxy_handler(UrlParams *params) { const char *url = NULL; for (int i = 0; i < params->count; i++) { - if (strcmp(params->params[i].key, "url") == 0) { - url = params->params[i].value; - break; - } + if (strcmp(params->params[i].key, "url") == 0) { + url = params->params[i].value; + break; + } } if (!url || strlen(url) == 0) { - send_response("Missing 'url' parameter"); - return 0; + send_response("Missing 'url' parameter"); + return 0; } if (!is_allowed_domain(url)) { - send_response("Domain not allowed"); - return 0; + send_response("Domain not allowed"); + return 0; } CURL *curl = curl_easy_init(); if (!curl) { - send_response("Failed to initialize curl"); - return 0; + send_response("Failed to initialize curl"); + return 0; } MemoryBuffer buf = { - .data = malloc(8192), - .size = 0, - .capacity = 8192 + .data = malloc(8192), + .size = 0, + .capacity = 8192 }; if (!buf.data) { - curl_easy_cleanup(curl); - send_response("Memory allocation failed"); - return 0; + curl_easy_cleanup(curl); + send_response("Memory allocation failed"); + return 0; } curl_easy_setopt(curl, CURLOPT_URL, url); @@ -130,15 +130,15 @@ int image_proxy_handler(UrlParams *params) { char content_type[64] = {0}; if (content_type_ptr) { - strncpy(content_type, content_type_ptr, sizeof(content_type) - 1); + strncpy(content_type, content_type_ptr, sizeof(content_type) - 1); } curl_easy_cleanup(curl); if (res != CURLE_OK || response_code != 200) { - free(buf.data); - send_response("Failed to fetch image"); - return 0; + free(buf.data); + send_response("Failed to fetch image"); + return 0; } const char *mime_type = strlen(content_type) > 0 ? content_type : "image/jpeg"; |
