1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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;
}
|