aboutsummaryrefslogtreecommitdiff
path: root/src/Routes/Images.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Routes/Images.c')
-rw-r--r--src/Routes/Images.c277
1 files changed, 277 insertions, 0 deletions
diff --git a/src/Routes/Images.c b/src/Routes/Images.c
new file mode 100644
index 0000000..47e3a72
--- /dev/null
+++ b/src/Routes/Images.c
@@ -0,0 +1,277 @@
+#include "Images.h"
+#include "../Utility/Unescape.h"
+
+#include <curl/curl.h>
+#include <libxml/HTMLparser.h>
+#include <libxml/xpath.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+static void get_image_timestamp(char *buffer, size_t size) {
+ time_t now = time(NULL);
+ struct tm *t = localtime(&now);
+ if (t) {
+ strftime(buffer, size, "%Y-%m-%d %H:%M:%S", t);
+ }
+}
+
+#define IMG_LOG_INFO(msg, ...) \
+ { \
+ char ts[20]; \
+ get_image_timestamp(ts, sizeof(ts)); \
+ fprintf(stderr, "[%s] INFO [ImagesHandler] " msg "\n", ts, \
+ ##__VA_ARGS__); \
+ }
+
+#define IMG_LOG_ERROR(msg, ...) \
+ { \
+ char ts[20]; \
+ get_image_timestamp(ts, sizeof(ts)); \
+ fprintf(stderr, "[%s] ERROR [ImagesHandler] " msg "\n", ts, \
+ ##__VA_ARGS__); \
+ }
+
+struct MemoryBlock {
+ char *response;
+ size_t size;
+};
+
+static size_t ImageWriteCallback(void *data, size_t size, size_t nmemb,
+ void *userp) {
+ size_t realsize = size * nmemb;
+ struct MemoryBlock *mem = (struct MemoryBlock *)userp;
+ char *ptr = (char *)realloc(mem->response, mem->size + realsize + 1);
+ if (ptr == NULL) {
+ IMG_LOG_ERROR("Realloc failed in WriteCallback (out of memory)");
+ return 0;
+ }
+ mem->response = ptr;
+ memcpy(&(mem->response[mem->size]), data, realsize);
+ mem->size += realsize;
+ mem->response[mem->size] = 0;
+ return realsize;
+}
+
+static char *fetch_images_html(const char *url) {
+ CURL *curl_handle;
+ struct MemoryBlock chunk = {.response = malloc(1), .size = 0};
+ if (!chunk.response) {
+ IMG_LOG_ERROR("Initial malloc failed for fetch_images_html");
+ return NULL;
+ }
+
+ IMG_LOG_INFO("Initializing cURL handle for URL: %s", url);
+ curl_handle = curl_easy_init();
+ if (!curl_handle) {
+ IMG_LOG_ERROR("curl_easy_init() failed");
+ free(chunk.response);
+ return NULL;
+ }
+
+ curl_easy_setopt(curl_handle, CURLOPT_URL, url);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, ImageWriteCallback);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+ curl_easy_setopt(
+ curl_handle, CURLOPT_USERAGENT,
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
+ curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
+ curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 10L);
+
+ CURLcode res = curl_easy_perform(curl_handle);
+ if (res != CURLE_OK) {
+ IMG_LOG_ERROR("curl_easy_perform() failed: %s", curl_easy_strerror(res));
+ free(chunk.response);
+ curl_easy_cleanup(curl_handle);
+ return NULL;
+ }
+
+ IMG_LOG_INFO("Successfully fetched %zu bytes from Yahoo Images", chunk.size);
+ curl_easy_cleanup(curl_handle);
+ return chunk.response;
+}
+
+static char *get_json_field_internal(const char *json, const char *key) {
+ if (!json) return NULL;
+ char search_key[64];
+ snprintf(search_key, sizeof(search_key), "\"%s\":\"", key);
+ char *start = strstr(json, search_key);
+ if (!start) return NULL;
+ start += strlen(search_key);
+ char *end = strchr(start, '\"');
+ if (!end) return NULL;
+
+ size_t len = end - start;
+ char *val = (char *)malloc(len + 1);
+ if (!val) return NULL;
+
+ size_t j = 0;
+ for (size_t i = 0; i < len; i++) {
+ if (start[i] == '\\' && i + 1 < len && start[i + 1] == '/') {
+ val[j++] = '/';
+ i++;
+ } else {
+ val[j++] = start[i];
+ }
+ }
+ val[j] = '\0';
+ return val;
+}
+
+int images_handler(UrlParams *params) {
+ IMG_LOG_INFO("Start images_handler request processing");
+ TemplateContext ctx = new_context();
+ char *raw_query = "";
+
+ if (params) {
+ for (int i = 0; i < params->count; i++) {
+ if (strcmp(params->params[i].key, "q") == 0) {
+ raw_query = params->params[i].value;
+ break;
+ }
+ }
+ }
+
+ char *encoded_query = strdup(raw_query);
+
+ char *display_query = url_decode_query(raw_query);
+ context_set(&ctx, "query", display_query);
+
+ if (!encoded_query || strlen(encoded_query) == 0) {
+ IMG_LOG_INFO("Empty search query received, returning early warning");
+ send_response("<h1>No query provided</h1>");
+ if (encoded_query) free(encoded_query);
+ if (display_query) free(display_query);
+ free_context(&ctx);
+ return -1;
+ }
+
+ char url[1024];
+ snprintf(url, sizeof(url),
+ "https://images.search.yahoo.com/search/images?p=%s", encoded_query);
+
+ IMG_LOG_INFO("Requesting external HTML from Yahoo Images...");
+ char *html = fetch_images_html(url);
+ if (!html) {
+ IMG_LOG_ERROR("Failed to fetch image search results from Yahoo");
+ send_response("<h1>Error fetching images</h1>");
+ free(encoded_query);
+ free(display_query);
+ free_context(&ctx);
+ return -1;
+ }
+
+ IMG_LOG_INFO("Parsing HTML with libxml2...");
+ htmlDocPtr doc = htmlReadMemory(html, (int)strlen(html), NULL, NULL,
+ HTML_PARSE_RECOVER | HTML_PARSE_NOERROR);
+ if (!doc) {
+ IMG_LOG_ERROR("htmlReadMemory failed to create document pointer");
+ free(html);
+ free(encoded_query);
+ free(display_query);
+ free_context(&ctx);
+ return -1;
+ }
+
+ xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
+
+ if (!xpathCtx) {
+ IMG_LOG_ERROR("xmlXPathNewContext failed");
+ xmlFreeDoc(doc);
+ free(html);
+ free(encoded_query);
+ free(display_query);
+ free_context(&ctx);
+ return -1;
+ }
+
+ IMG_LOG_INFO("Executing XPath expression: //li[@data]");
+ xmlXPathObjectPtr xpathObj =
+ xmlXPathEvalExpression((const xmlChar *)"//li[@data]", xpathCtx);
+
+ int image_count = 0;
+ char ***image_matrix = NULL;
+ int *inner_counts = NULL;
+
+ if (xpathObj && xpathObj->nodesetval) {
+ int nodes = xpathObj->nodesetval->nodeNr;
+ IMG_LOG_INFO("XPath found %d potential image nodes", nodes);
+
+ int max_images = (nodes < 32) ? nodes : 32;
+ image_matrix = malloc(sizeof(char **) * max_images);
+ inner_counts = malloc(sizeof(int) * max_images);
+
+ for (int i = 0; i < nodes; i++) {
+ if (image_count >= 32) break;
+
+ xmlNodePtr node = xpathObj->nodesetval->nodeTab[i];
+ xmlChar *data_attr = xmlGetProp(node, (const xmlChar *)"data");
+ if (data_attr) {
+ char *iurl = get_json_field_internal((char *)data_attr, "iurl");
+ char *title = get_json_field_internal((char *)data_attr, "alt");
+ char *rurl = get_json_field_internal((char *)data_attr, "rurl");
+
+ if (iurl && strlen(iurl) > 0) {
+ image_matrix[image_count] = malloc(sizeof(char *) * 3);
+ image_matrix[image_count][0] = strdup(iurl);
+ image_matrix[image_count][1] = strdup(title ? title : "Image");
+ image_matrix[image_count][2] = strdup(rurl ? rurl : "#");
+ inner_counts[image_count] = 3;
+ image_count++;
+ }
+
+ if (iurl) free(iurl);
+ if (title) free(title);
+ if (rurl) free(rurl);
+ xmlFree(data_attr);
+ }
+ }
+ IMG_LOG_INFO("Successfully parsed %d valid image results (capped at 32)",
+ image_count);
+ } else {
+ IMG_LOG_INFO("No image nodes found in the HTML document");
+ }
+
+ IMG_LOG_INFO("Setting image array in template context...");
+ context_set_array_of_arrays(&ctx, "images", image_matrix, image_count,
+ inner_counts);
+
+ IMG_LOG_INFO("Rendering images.html template...");
+ char *rendered = render_template("images.html", &ctx);
+ if (rendered) {
+ IMG_LOG_INFO("Sending rendered template to client (%zu bytes)",
+ strlen(rendered));
+ send_response(rendered);
+ free(rendered);
+ } else {
+ IMG_LOG_ERROR("render_template returned NULL for images.html");
+ send_response("<h1>Error rendering image results</h1>");
+ }
+
+ IMG_LOG_INFO("Beginning memory cleanup...");
+
+ if (image_matrix) {
+ for (int i = 0; i < image_count; i++) {
+ for (int j = 0; j < 3; j++) {
+ free(image_matrix[i][j]);
+ }
+ free(image_matrix[i]);
+ }
+ free(image_matrix);
+ }
+ if (inner_counts) {
+ free(inner_counts);
+ }
+
+ if (xpathObj) xmlXPathFreeObject(xpathObj);
+ if (xpathCtx) xmlXPathFreeContext(xpathCtx);
+ if (doc) xmlFreeDoc(doc);
+ free(html);
+ free(encoded_query);
+ free(display_query);
+ free_context(&ctx);
+
+ IMG_LOG_INFO("Images request cycle complete");
+ return 0;
+}