Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_html.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#include "nilorea/n_common.h"
28#include "nilorea/n_html.h"
29#include "nilorea/n_list.h"
30#include "nilorea/n_log.h"
31#include "nilorea/n_str.h"
32
33#include <stdio.h>
34#include <string.h>
35
36static int failures = 0;
37
38/* Parse the -V LOG_LEVEL verbosity argument. */
39void process_args(int argc, char** argv) {
40 int opt = 0;
41 while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
42 switch (opt) {
43 case 'V':
44 if (!strncmp("LOG_NULL", optarg, 8))
46 else if (!strncmp("LOG_NOTICE", optarg, 10))
48 else if (!strncmp("LOG_INFO", optarg, 8))
50 else if (!strncmp("LOG_ERR", optarg, 7))
52 else if (!strncmp("LOG_DEBUG", optarg, 9))
54 else {
55 fprintf(stderr, "Unknown log level %s\n", optarg);
56 exit(1);
57 }
58 break;
59 case 'v':
60 fprintf(stderr, "ex_html\n");
61 exit(1);
62 case 'h':
63 default:
64 fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
65 break;
66 }
67 }
68}
69
70static int find_link(LIST* links, const char* want) {
71 if (!links) return 0;
72 list_foreach(node, links) {
73 N_STR* s = (N_STR*)node->ptr;
74 if (s && s->data && strcmp(s->data, want) == 0) return 1;
75 }
76 return 0;
77}
78
79static void expect_link_present(LIST* links, const char* want) {
80 if (!find_link(links, want)) {
81 n_log(LOG_ERR, "missing expected link: '%s'", want);
82 failures++;
83 } else {
84 n_log(LOG_INFO, "link present: '%s'", want);
85 }
86}
87
88static void expect_link_absent(LIST* links, const char* probe) {
89 if (find_link(links, probe)) {
90 n_log(LOG_ERR, "unexpected link present: '%s'", probe);
91 failures++;
92 }
93}
94
95static void test_links(void) {
96 const char* html =
97 "<!DOCTYPE html>\n"
98 "<html><head>\n"
99 " <link rel=\"stylesheet\" HREF=\"/css/main.css\">\n"
100 " <script src='/js/app.js'></script>\n"
101 " <!-- a comment with <a href=\"/in/comment\">trap</a> -->\n"
102 "</head><body>\n"
103 " <a href=\"/about\">about</a>\n"
104 " <a HREF=/contact>contact</a>\n"
105 " <a href = \"/help\" target=_blank>help</a>\n"
106 " <a>no href</a>\n"
107 " <form action=\"/login\" method=POST><input name=u></form>\n"
108 " <img src=\"/img/logo.png\"/>\n"
109 " <iframe src=\"//cdn.example.com/frame\"></iframe>\n"
110 " <area shape=rect href=\"/map/area1\"/>\n"
111 " <script>var x = '<a href=\"/in/script\">trap</a>';</script>\n"
112 " <style>a[href=\"/in/style\"] { color: red; }</style>\n"
113 "</body></html>";
114 size_t len = strlen(html);
115
116 LIST* links = n_html_extract_links(html, len);
117 if (!links) {
118 n_log(LOG_ERR, "extract_links returned NULL");
119 failures++;
120 return;
121 }
122
123 expect_link_present(links, "/css/main.css");
124 expect_link_present(links, "/js/app.js");
125 expect_link_present(links, "/about");
126 expect_link_present(links, "/contact");
127 expect_link_present(links, "/help");
128 expect_link_present(links, "/login");
129 expect_link_present(links, "/img/logo.png");
130 expect_link_present(links, "//cdn.example.com/frame");
131 expect_link_present(links, "/map/area1");
132
133 /* contents of comments, <script>, and <style> must not leak as links */
134 expect_link_absent(links, "/in/comment");
135 expect_link_absent(links, "/in/script");
136 expect_link_absent(links, "/in/style");
137
138 n_html_links_free(&links);
139 if (links) {
140 n_log(LOG_ERR, "n_html_links_free did not NULL the pointer");
141 failures++;
142 }
143}
144
145static void test_forms(void) {
146 const char* html =
147 "<form action=\"/signup\" method=\"post\">"
148 " <input type='text' name='user' value=\"alice\" required>"
149 " <input type=password name=\"pass\">"
150 " <textarea name=bio></textarea>"
151 " <select name=country><option>fr</option></select>"
152 " <button type=submit name=go value=1>Go</button>"
153 "</form>"
154 "<form>" /* no method/action: defaults */
155 " <input name=q>"
156 "</form>";
157 size_t len = strlen(html);
158
159 LIST* forms = n_html_extract_forms(html, len);
160 if (!forms || forms->nb_items != 2) {
161 n_log(LOG_ERR, "expected 2 forms, got %zu", forms ? forms->nb_items : (size_t)0);
162 failures++;
163 if (forms) n_html_forms_free(&forms);
164 return;
165 }
166
167 LIST_NODE* node = forms->start;
168 N_HTML_FORM* f0 = (N_HTML_FORM*)node->ptr;
169 N_HTML_FORM* f1 = (N_HTML_FORM*)node->next->ptr;
170
171 if (!f0->method || strcmp(f0->method, "POST") != 0) {
172 n_log(LOG_ERR, "form0 method: got '%s' expected 'POST'", f0->method ? f0->method : "(null)");
173 failures++;
174 }
175 if (!f0->action || strcmp(f0->action, "/signup") != 0) {
176 n_log(LOG_ERR, "form0 action: got '%s'", f0->action ? f0->action : "(null)");
177 failures++;
178 }
179 if (!f0->fields || f0->fields->nb_items != 5) {
180 n_log(LOG_ERR, "form0 fields: got %zu expected 5", f0->fields ? f0->fields->nb_items : (size_t)0);
181 failures++;
182 } else {
183 LIST_NODE* fn = f0->fields->start;
184 N_FORM_FIELD* user = (N_FORM_FIELD*)fn->ptr;
185 if (strcmp(user->name, "user") != 0 || strcmp(user->type, "text") != 0 || strcmp(user->value, "alice") != 0 || user->required != 1) {
186 n_log(LOG_ERR, "form0 field0 wrong: name=%s type=%s value=%s required=%d", user->name, user->type, user->value, user->required);
187 failures++;
188 }
189 fn = fn->next;
190 N_FORM_FIELD* pass = (N_FORM_FIELD*)fn->ptr;
191 if (strcmp(pass->name, "pass") != 0 || strcmp(pass->type, "password") != 0 || pass->required != 0) {
192 n_log(LOG_ERR, "form0 field1 wrong: name=%s type=%s required=%d", pass->name, pass->type, pass->required);
193 failures++;
194 }
195 fn = fn->next;
196 N_FORM_FIELD* bio = (N_FORM_FIELD*)fn->ptr;
197 if (strcmp(bio->name, "bio") != 0 || strcmp(bio->type, "text") != 0) {
198 n_log(LOG_ERR, "form0 field2 wrong: name=%s type=%s", bio->name, bio->type);
199 failures++;
200 }
201 }
202
203 if (!f1->method || strcmp(f1->method, "GET") != 0) {
204 n_log(LOG_ERR, "form1 method: got '%s' expected 'GET'", f1->method ? f1->method : "(null)");
205 failures++;
206 }
207 if (!f1->action || strcmp(f1->action, "") != 0) {
208 n_log(LOG_ERR, "form1 action: got '%s' expected ''", f1->action ? f1->action : "(null)");
209 failures++;
210 }
211 if (!f1->fields || f1->fields->nb_items != 1) {
212 n_log(LOG_ERR, "form1 fields: got %zu expected 1", f1->fields ? f1->fields->nb_items : (size_t)0);
213 failures++;
214 }
215
216 n_html_forms_free(&forms);
217 if (forms) {
218 n_log(LOG_ERR, "n_html_forms_free did not NULL the pointer");
219 failures++;
220 }
221}
222
223static void test_sitemap(void) {
224 const char* xml =
225 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
226 "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
227 " <url><loc>https://example.com/a</loc><lastmod>2026-01-01</lastmod></url>"
228 " <url>\n <LOC>\n https://example.com/b\n </LOC>\n</url>"
229 " <url><loc>https://example.com/c</loc></url>"
230 "</urlset>";
231 size_t len = strlen(xml);
232
233 LIST* urls = n_sitemap_extract_urls(xml, len);
234 if (!urls || urls->nb_items != 3) {
235 n_log(LOG_ERR, "sitemap: expected 3 urls, got %zu", urls ? urls->nb_items : (size_t)0);
236 failures++;
237 if (urls) n_html_links_free(&urls);
238 return;
239 }
240
241 LIST_NODE* node = urls->start;
242 const char* want[3] = {"https://example.com/a", "https://example.com/b", "https://example.com/c"};
243 for (int i = 0; i < 3; i++) {
244 N_STR* s = (N_STR*)node->ptr;
245 if (!s || !s->data || strcmp(s->data, want[i]) != 0) {
246 n_log(LOG_ERR, "sitemap url %d: got '%s' expected '%s'", i, s && s->data ? s->data : "(null)", want[i]);
247 failures++;
248 }
249 node = node->next;
250 }
251
252 n_html_links_free(&urls);
253}
254
255static void test_empty_and_null(void) {
256 LIST* a = n_html_extract_links(NULL, 0);
257 LIST* b = n_html_extract_links("", 0);
258 LIST* c = n_html_extract_forms(NULL, 0);
259 LIST* d = n_sitemap_extract_urls("", 0);
260 if (!a || a->nb_items != 0) failures++;
261 if (!b || b->nb_items != 0) failures++;
262 if (!c || c->nb_items != 0) failures++;
263 if (!d || d->nb_items != 0) failures++;
268}
269
270static void test_enctype(void) {
271 const char* html =
272 "<form action=\"/up\" method=\"post\" enctype=\"multipart/form-data\"><input name=f></form>"
273 "<form action=\"/x\"><input name=g></form>"; /* no enctype: default */
274 LIST* forms = n_html_extract_forms(html, strlen(html));
275 if (!forms || forms->nb_items != 2) {
276 n_log(LOG_ERR, "enctype test: expected 2 forms");
277 failures++;
278 if (forms) n_html_forms_free(&forms);
279 return;
280 }
281 {
282 N_HTML_FORM* f0 = (N_HTML_FORM*)forms->start->ptr;
283 N_HTML_FORM* f1 = (N_HTML_FORM*)forms->start->next->ptr;
284 if (!f0->enctype || strcmp(f0->enctype, "multipart/form-data") != 0) {
285 n_log(LOG_ERR, "form0 enctype: got '%s'", f0->enctype ? f0->enctype : "(null)");
286 failures++;
287 }
288 if (!f1->enctype || strcmp(f1->enctype, "application/x-www-form-urlencoded") != 0) {
289 n_log(LOG_ERR, "form1 default enctype: got '%s'", f1->enctype ? f1->enctype : "(null)");
290 failures++;
291 }
292 }
293 n_html_forms_free(&forms);
294}
295
296/* Assert that to-text output contains (or, when want_present is 0, omits) a
297 substring. */
298static void expect_text(const char* label, N_STR* txt, const char* needle, int want_present) {
299 int present = (txt && txt->data && strstr(txt->data, needle) != NULL);
300 if (present != want_present) {
301 n_log(LOG_ERR, "to_text %s: '%s' should be %s in '%s'", label, needle, want_present ? "present" : "absent", (txt && txt->data) ? txt->data : "(null)");
302 failures++;
303 }
304}
305
306static void test_to_text(void) {
307 /* tags drop, block elements line-break, inline text is kept */
308 {
309 const char* html = "<html><body><h1>Title</h1><p>Hello <b>world</b></p></body></html>";
310 N_STR* t = n_html_to_text(html, strlen(html));
311 expect_text("basic", t, "Title", 1);
312 expect_text("basic", t, "Hello world", 1);
313 expect_text("basic", t, "<", 0); /* no markup leaks through */
314 if (t) free_nstr(&t);
315 }
316 /* entity decoding: named and numeric (decimal + hex) */
317 {
318 const char* html = "<p>a &amp; b &lt;c&gt; d&#65;&#x42;e &quot;q&quot; n&nbsp;m</p>";
319 N_STR* t = n_html_to_text(html, strlen(html));
320 expect_text("entities", t, "a & b <c> dABe \"q\" n m", 1);
321 if (t) free_nstr(&t);
322 }
323 /* script and style content is dropped wholesale */
324 {
325 const char* html = "<p>x</p><script>var a = 1 < 2;</script><style>.c{color:red}</style><p>y</p>";
326 N_STR* t = n_html_to_text(html, strlen(html));
327 expect_text("script", t, "x", 1);
328 expect_text("script", t, "y", 1);
329 expect_text("script", t, "var a", 0);
330 expect_text("script", t, "color:red", 0);
331 if (t) free_nstr(&t);
332 }
333 /* whitespace runs collapse to a single space; no leading/trailing space */
334 {
335 const char* html = "<p> a b\n\t c </p>";
336 N_STR* t = n_html_to_text(html, strlen(html));
337 if (!t || !t->data || strcmp(t->data, "a b c") != 0) {
338 n_log(LOG_ERR, "to_text whitespace: got '%s'", (t && t->data) ? t->data : "(null)");
339 failures++;
340 }
341 if (t) free_nstr(&t);
342 }
343 /* comments are skipped */
344 {
345 const char* html = "<p>a<!-- secret comment -->b</p>";
346 N_STR* t = n_html_to_text(html, strlen(html));
347 expect_text("comment", t, "secret", 0);
348 expect_text("comment", t, "ab", 1);
349 if (t) free_nstr(&t);
350 }
351 /* NULL input returns NULL; empty input returns an empty N_STR */
352 {
353 N_STR* a = n_html_to_text(NULL, 0);
354 N_STR* b = n_html_to_text("", 0);
355 if (a != NULL) failures++;
356 if (!b || !b->data || b->data[0] != '\0') failures++;
357 if (b) free_nstr(&b);
358 }
359}
360
361static size_t list_count(LIST* l) {
362 size_t n = 0;
363 if (!l) return 0;
364 list_foreach(node, l) {
365 (void)node;
366 n++;
367 }
368 return n;
369}
370
371static void test_js_urls(void) {
372 const char* js =
373 "const API = '/api/v1';\n"
374 "fetch(\"/api/v1/users?id=1\").then(r => r.json());\n"
375 "axios.get('https://api.example.com/orders');\n"
376 "const u = `/api/v1/item/${id}/detail`;\n"
377 "xhr.open('GET', '//cdn.example.com/data.json');\n"
378 "const rel = './partials/menu.html';\n"
379 "const mime = 'text/html';\n" /* no leading slash: not a URL */
380 "const re = '/^\\\\d+$/';\n" /* regex in a string: backslash rejects it */
381 "const cls = 'btn btn-primary';\n"; /* spaces: rejected */
382 LIST* urls = n_html_extract_js_urls(js, strlen(js));
383 if (!urls) {
384 n_log(LOG_ERR, "extract_js_urls returned NULL");
385 failures++;
386 return;
387 }
388 expect_link_present(urls, "/api/v1");
389 expect_link_present(urls, "/api/v1/users?id=1");
390 expect_link_present(urls, "https://api.example.com/orders");
391 expect_link_present(urls, "/api/v1/item/"); /* ${id} template truncated */
392 expect_link_present(urls, "//cdn.example.com/data.json");
393 expect_link_present(urls, "./partials/menu.html");
394 expect_link_absent(urls, "text/html");
395 expect_link_absent(urls, "btn btn-primary");
396 n_html_links_free(&urls);
397
398 /* NULL / empty inputs are safe */
399 {
400 LIST* a = n_html_extract_js_urls(NULL, 0);
401 LIST* b = n_html_extract_js_urls("", 0);
402 if (!a || list_count(a) != 0) failures++;
403 if (!b || list_count(b) != 0) failures++;
406 }
407}
408
409static void test_scripts(void) {
410 const char* html =
411 "<html><head>\n"
412 "<script src='/js/app.js'></script>\n" /* external: skipped */
413 "<script>const a = '/inline/one';</script>\n"
414 "<SCRIPT type='text/javascript'>fetch('/inline/two');</SCRIPT>\n"
415 "</head><body>text</body></html>";
416 LIST* scripts = n_html_extract_scripts(html, strlen(html));
417 LIST* mined;
418 N_STR* joined;
419 if (!scripts) {
420 n_log(LOG_ERR, "extract_scripts returned NULL");
421 failures++;
422 return;
423 }
424 if (list_count(scripts) != 2) { /* two inline bodies, the external one skipped */
425 n_log(LOG_ERR, "expected 2 inline scripts, got %zu", list_count(scripts));
426 failures++;
427 }
428 /* the inline URLs are mineable from the extracted bodies */
429 joined = new_nstr(256);
430 list_foreach(node, scripts) {
431 N_STR* s = (N_STR*)node->ptr;
432 if (s && s->data) nstrprintf_cat(joined, "%s\n", s->data);
433 }
434 mined = n_html_extract_js_urls(joined->data, joined->written);
435 expect_link_present(mined, "/inline/one");
436 expect_link_present(mined, "/inline/two");
437 expect_link_absent(mined, "/js/app.js"); /* external body was not inlined here */
438 n_html_links_free(&mined);
439 free_nstr(&joined);
440 n_html_links_free(&scripts);
441}
442
443int main(int argc, char** argv) {
445 process_args(argc, argv);
446
447 test_links();
448 test_forms();
449 test_sitemap();
450 test_enctype();
452 test_to_text();
453 test_js_urls();
454 test_scripts();
455
456 if (failures) {
457 n_log(LOG_ERR, "ex_html: %d failure(s)", failures);
458 return 1;
459 }
460 n_log(LOG_NOTICE, "ex_html: all checks passed");
461 return 0;
462}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
static size_t list_count(LIST *l)
Definition ex_html.c:361
static void test_scripts(void)
Definition ex_html.c:409
static void test_js_urls(void)
Definition ex_html.c:371
static void test_empty_and_null(void)
Definition ex_html.c:255
static void test_enctype(void)
Definition ex_html.c:270
static void test_to_text(void)
Definition ex_html.c:306
static void expect_link_present(LIST *links, const char *want)
Definition ex_html.c:79
static void test_links(void)
Definition ex_html.c:95
static void expect_link_absent(LIST *links, const char *probe)
Definition ex_html.c:88
static void test_forms(void)
Definition ex_html.c:145
static void expect_text(const char *label, N_STR *txt, const char *needle, int want_present)
Definition ex_html.c:298
static void test_sitemap(void)
Definition ex_html.c:223
static int find_link(LIST *links, const char *want)
Definition ex_html.c:70
void * ptr
void pointer to store
Definition n_list.h:46
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:66
size_t nb_items
number of item currently in the list
Definition n_list.h:61
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:52
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:89
Structure of a generic LIST container.
Definition n_list.h:59
Structure of a generic list node.
Definition n_list.h:44
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
Definition n_str.h:121
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:207
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
void n_html_links_free(LIST **links)
free a list returned by n_html_extract_links or n_sitemap_extract_urls
Definition n_html.c:979
LIST * n_html_extract_js_urls(const char *js, size_t len)
extract URL/path tokens from JavaScript source (quoted string literals that look like an http(s) URL,...
Definition n_html.c:915
LIST * n_sitemap_extract_urls(const char *xml, size_t len)
extract <loc> URLs from a sitemap.xml as a LIST of N_STR*; free with n_html_links_free
Definition n_html.c:456
LIST * n_html_extract_scripts(const char *html, size_t len)
extract the inline <script> bodies (those without a src attribute) from an HTML document as a LIST of...
Definition n_html.c:941
N_STR * n_html_to_text(const char *html, size_t len)
render HTML to readable plain text: drop tags, skip script/style, decode common entities,...
Definition n_html.c:627
void n_html_forms_free(LIST **forms)
free a list returned by n_html_extract_forms
Definition n_html.c:984
LIST * n_html_extract_links(const char *html, size_t len)
extract link URLs (a/href, form/action, img/src, script/src, link/href, iframe/src) as a LIST of N_ST...
Definition n_html.c:360
LIST * n_html_extract_forms(const char *html, size_t len)
extract forms as a LIST of N_HTML_FORM*; free with n_html_forms_free
Definition n_html.c:392
Lightweight HTML/XML extraction: links, forms, and sitemap URLs.
char * action
action attribute (may be "" for self)
Definition n_html.h:55
LIST * fields
list of N_FORM_FIELD*
Definition n_html.h:57
char * value
default value attribute, or ""
Definition n_html.h:48
char * type
field type (lowercased), default "text"
Definition n_html.h:47
char * name
field name attribute, or ""
Definition n_html.h:46
int required
1 when the required attribute is present
Definition n_html.h:49
char * enctype
lower-case enctype, default "application/x-www-form-urlencoded"
Definition n_html.h:56
char * method
upper-case method, default "GET"
Definition n_html.h:54
a single form field (input, select, textarea, button)
Definition n_html.h:45
a parsed HTML form with its fields
Definition n_html.h:53
List structures and definitions.
Generic log system.
N_STR and string function declaration.