aboutsummaryrefslogtreecommitdiff
path: root/src/Routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/Routes')
-rw-r--r--src/Routes/Home.c14
-rw-r--r--src/Routes/Home.h8
-rw-r--r--src/Routes/Images.c277
-rw-r--r--src/Routes/Images.h8
-rw-r--r--src/Routes/Search.c273
-rw-r--r--src/Routes/Search.h8
6 files changed, 588 insertions, 0 deletions
diff --git a/src/Routes/Home.c b/src/Routes/Home.c
new file mode 100644
index 0000000..81370ba
--- /dev/null
+++ b/src/Routes/Home.c
@@ -0,0 +1,14 @@
+#include "Home.h"
+#include <stdlib.h>
+
+int home_handler(UrlParams *params) {
+ (void)params;
+ TemplateContext ctx = new_context();
+ char *rendered_html = render_template("home.html", &ctx);
+ send_response(rendered_html);
+
+ free(rendered_html);
+ free_context(&ctx);
+
+ return 0;
+}
diff --git a/src/Routes/Home.h b/src/Routes/Home.h
new file mode 100644
index 0000000..5d01ab3
--- /dev/null
+++ b/src/Routes/Home.h
@@ -0,0 +1,8 @@
+#ifndef HOME_H
+#define HOME_H
+
+#include <beaker.h>
+
+int home_handler(UrlParams *params);
+
+#endif
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;
+}
diff --git a/src/Routes/Images.h b/src/Routes/Images.h
new file mode 100644
index 0000000..86f4a31
--- /dev/null
+++ b/src/Routes/Images.h
@@ -0,0 +1,8 @@
+#ifndef IMAGES_HANDLER_H
+#define IMAGES_HANDLER_H
+
+#include <beaker.h>
+
+int images_handler(UrlParams *params);
+
+#endif
diff --git a/src/Routes/Search.c b/src/Routes/Search.c
new file mode 100644
index 0000000..110e6f7
--- /dev/null
+++ b/src/Routes/Search.c
@@ -0,0 +1,273 @@
+#include "Search.h"
+#include "../Infobox/Wikipedia.h"
+#include "../Infobox/Calculator.h"
+#include "../Scraping/Scraping.h"
+#include "../Utility/Display.h"
+#include "../Utility/Unescape.h"
+#include <ctype.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+typedef struct {
+ const SearchEngine *engine;
+ const char *query;
+ SearchResult *results;
+ int count;
+} EngineThreadData;
+
+static void *scrape_thread_func(void *arg) {
+ EngineThreadData *data = (EngineThreadData *)arg;
+ data->count = scrape_engine(data->engine, data->query, &data->results, 10);
+ return NULL;
+}
+
+typedef struct {
+ const char *query;
+ InfoBox result;
+ int success;
+} InfoBoxThreadData;
+
+static void *wiki_thread_func(void *arg) {
+ InfoBoxThreadData *data = (InfoBoxThreadData *)arg;
+ char *dynamic_url = construct_wiki_url(data->query);
+ if (dynamic_url) {
+ data->result = fetch_wiki_data(dynamic_url);
+ data->success =
+ (data->result.title != NULL && data->result.extract != NULL &&
+ strlen(data->result.extract) > 10);
+ free(dynamic_url);
+ } else {
+ data->success = 0;
+ }
+ return NULL;
+}
+
+static int is_calculator_query(const char *query) {
+ if (!query) return 0;
+
+ int has_digit = 0;
+ int has_operator = 0;
+
+ for (const char *p = query; *p; p++) {
+ if (isdigit(*p) || *p == '.') {
+ has_digit = 1;
+ }
+ if (*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '=' ||
+ *p == '^') {
+ has_operator = 1;
+ }
+ }
+
+ return has_digit && (has_operator || strchr(query, '.'));
+}
+
+static void *calc_thread_func(void *arg) {
+ InfoBoxThreadData *data = (InfoBoxThreadData *)arg;
+
+ if (is_calculator_query(data->query)) {
+ data->result = fetch_calc_data((char *)data->query);
+ data->success =
+ (data->result.title != NULL && data->result.extract != NULL);
+ } else {
+ data->success = 0;
+ }
+
+ return NULL;
+}
+
+static int add_infobox_to_collection(InfoBox *infobox, char ****collection,
+ int **inner_counts, int current_count) {
+ *collection =
+ (char ***)realloc(*collection, sizeof(char **) * (current_count + 1));
+ *inner_counts =
+ (int *)realloc(*inner_counts, sizeof(int) * (current_count + 1));
+
+ (*collection)[current_count] = (char **)malloc(sizeof(char *) * 4);
+ (*collection)[current_count][0] = infobox->title;
+ (*collection)[current_count][1] = infobox->thumbnail_url;
+ (*collection)[current_count][2] = infobox->extract;
+ (*collection)[current_count][3] = infobox->url;
+ (*inner_counts)[current_count] = 4;
+
+ return current_count + 1;
+}
+
+int results_handler(UrlParams *params) {
+ 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);
+ LOG_INFO("Processing search request for query: '%s'", display_query);
+ context_set(&ctx, "query", display_query);
+
+ if (!encoded_query || strlen(encoded_query) == 0) {
+ LOG_ERROR("Empty search query provided.");
+ send_response("<h1>No query provided</h1>");
+ if (encoded_query) free(encoded_query);
+ if (display_query) free(display_query);
+ free_context(&ctx);
+ return -1;
+ }
+
+ pthread_t wiki_tid, calc_tid;
+ InfoBoxThreadData wiki_data = {.query = display_query, .success = 0};
+ InfoBoxThreadData calc_data = {.query = display_query, .success = 0};
+
+ pthread_create(&wiki_tid, NULL, wiki_thread_func, &wiki_data);
+ pthread_create(&calc_tid, NULL, calc_thread_func, &calc_data);
+
+ pthread_t engine_tids[ENGINE_COUNT];
+ EngineThreadData engine_data[ENGINE_COUNT];
+
+ for (int i = 0; i < ENGINE_COUNT; i++) {
+ engine_data[i].engine = &ENGINE_REGISTRY[i];
+ engine_data[i].query = encoded_query;
+
+ engine_data[i].results = NULL;
+ engine_data[i].count = 0;
+ pthread_create(&engine_tids[i], NULL, scrape_thread_func, &engine_data[i]);
+ }
+
+ pthread_join(wiki_tid, NULL);
+ pthread_join(calc_tid, NULL);
+
+ char ***infobox_matrix = NULL;
+ int *infobox_inner_counts = NULL;
+ int infobox_count = 0;
+
+ if (calc_data.success) {
+ infobox_count =
+ add_infobox_to_collection(&calc_data.result, &infobox_matrix,
+ &infobox_inner_counts, infobox_count);
+ }
+
+ if (wiki_data.success) {
+ infobox_count =
+ add_infobox_to_collection(&wiki_data.result, &infobox_matrix,
+ &infobox_inner_counts, infobox_count);
+ }
+
+ if (infobox_count > 0) {
+ context_set_array_of_arrays(&ctx, "infoboxes", infobox_matrix,
+ infobox_count, infobox_inner_counts);
+ free(infobox_matrix);
+ free(infobox_inner_counts);
+ } else {
+ context_set_array_of_arrays(&ctx, "infoboxes", NULL, 0, NULL);
+ }
+
+ int total_results = 0;
+ for (int i = 0; i < ENGINE_COUNT; i++) {
+ pthread_join(engine_tids[i], NULL);
+ total_results += engine_data[i].count;
+ }
+
+ if (total_results > 0) {
+ char ***results_matrix = (char ***)malloc(sizeof(char **) * total_results);
+ int *results_inner_counts = (int *)malloc(sizeof(int) * total_results);
+ char **seen_urls = (char **)malloc(sizeof(char *) * total_results);
+ int unique_count = 0;
+
+ for (int i = 0; i < ENGINE_COUNT; i++) {
+ for (int j = 0; j < engine_data[i].count; j++) {
+ char *raw_url = engine_data[i].results[j].url;
+ char *clean_url = unescape_search_url(raw_url);
+ char *display_url = clean_url ? clean_url : raw_url;
+
+ int is_duplicate = 0;
+ for (int k = 0; k < unique_count; k++) {
+ if (strcmp(seen_urls[k], display_url) == 0) {
+ is_duplicate = 1;
+ break;
+ }
+ }
+
+ if (is_duplicate) {
+ if (clean_url) free(clean_url);
+ free(engine_data[i].results[j].url);
+ free(engine_data[i].results[j].title);
+ free(engine_data[i].results[j].snippet);
+ continue;
+ }
+
+ seen_urls[unique_count] = strdup(display_url);
+ results_matrix[unique_count] = (char **)malloc(sizeof(char *) * 4);
+ char *pretty_url = pretty_display_url(display_url);
+
+ results_matrix[unique_count][0] = strdup(display_url);
+ results_matrix[unique_count][1] = strdup(pretty_url);
+ results_matrix[unique_count][2] =
+ engine_data[i].results[j].title
+ ? strdup(engine_data[i].results[j].title)
+ : strdup("Untitled");
+ results_matrix[unique_count][3] =
+ engine_data[i].results[j].snippet
+ ? strdup(engine_data[i].results[j].snippet)
+ : strdup("");
+
+ results_inner_counts[unique_count] = 4;
+
+ free(pretty_url);
+ free(engine_data[i].results[j].url);
+ free(engine_data[i].results[j].title);
+ free(engine_data[i].results[j].snippet);
+ if (clean_url) free(clean_url);
+
+ unique_count++;
+ }
+ free(engine_data[i].results);
+ }
+
+ context_set_array_of_arrays(&ctx, "results", results_matrix, unique_count,
+ results_inner_counts);
+
+ char *html = render_template("results.html", &ctx);
+ if (html) {
+ send_response(html);
+ free(html);
+ }
+
+ for (int i = 0; i < unique_count; i++) {
+ for (int j = 0; j < 4; j++) free(results_matrix[i][j]);
+ free(results_matrix[i]);
+ free(seen_urls[i]);
+ }
+ free(seen_urls);
+ free(results_matrix);
+ free(results_inner_counts);
+ } else {
+ char *html = render_template("results.html", &ctx);
+ if (html) {
+ send_response(html);
+ free(html);
+ }
+ }
+
+ if (wiki_data.success) {
+ free_infobox(&wiki_data.result);
+ }
+
+ if (calc_data.success) {
+ free_infobox(&calc_data.result);
+ }
+
+ free(encoded_query);
+ free(display_query);
+ free_context(&ctx);
+
+ return 0;
+}
diff --git a/src/Routes/Search.h b/src/Routes/Search.h
new file mode 100644
index 0000000..c6bc146
--- /dev/null
+++ b/src/Routes/Search.h
@@ -0,0 +1,8 @@
+#ifndef SEARCH_HANDLER_H
+#define SEARCH_HANDLER_H
+
+#include <beaker.h>
+
+int results_handler(UrlParams *params);
+
+#endif