![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
Lightweight HTML/XML extraction: links, forms, and sitemap URLs. More...
Go to the source code of this file.
Data Structures | |
| struct | N_FORM_FIELD |
| a single form field (input, select, textarea, button) More... | |
| struct | N_HTML_FORM |
| a parsed HTML form with its fields More... | |
Functions | |
| int | n_html_escape (N_STR **out, const char *s, size_t len) |
append s escaped for HTML text or a quoted attribute value. | |
| 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 | |
| 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, a protocol-relative "//host" URL, or a "/" / "./" / "../" path; a "${...}" template is truncated to its static prefix), deduped, as a LIST of N_STR*; free with n_html_links_free. | |
| 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_STR*; free with n_html_links_free | |
| 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 N_STR*; free with n_html_links_free. | |
| void | n_html_forms_free (LIST **forms) |
| free a list returned by n_html_extract_forms | |
| void | n_html_links_free (LIST **links) |
| free a list returned by n_html_extract_links or n_sitemap_extract_urls | |
| 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, collapse whitespace, and line-break block elements; returns a new N_STR (free with free_nstr) or NULL | |
| 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 | |
Lightweight HTML/XML extraction: links, forms, and sitemap URLs.
A small, dependency-free tag scanner (not a full HTML parser) that pulls the link, form, and sitemap surface out of a document for a web crawler. It is tolerant of malformed markup and never executes anything.
Definition in file n_html.h.
| struct N_FORM_FIELD |
a single form field (input, select, textarea, button)
| struct N_HTML_FORM |
| Data Fields | ||
|---|---|---|
| char * | action | action attribute (may be "" for self) |
| char * | enctype | lower-case enctype, default "application/x-www-form-urlencoded" |
| LIST * | fields | list of N_FORM_FIELD* |
| char * | method | upper-case method, default "GET" |
| int n_html_escape | ( | N_STR ** | out, |
| const char * | s, | ||
| size_t | len ) |
append s escaped for HTML text or a quoted attribute value.
Byte for byte what PHP's htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') returns: & < > " ' become & < > " ', and every ill-formed UTF-8 sequence becomes U+FFFD, consuming the same bytes as PHP does. Other bytes, controls included, are copied. The result is safe inside element text and inside a single- or double-quoted attribute value, never in an unquoted attribute, a URL attribute's scheme, a script or a style.
| out | destination, appended to (created when *out is NULL) |
| s | bytes to escape (may be NULL only when len is 0) |
| len | number of bytes |
append s escaped for HTML text or a quoted attribute value.
| out | destination, appended to (created when *out is NULL) |
| s | bytes to escape (may be NULL only when len is 0) |
| len | number of bytes |
Definition at line 1065 of file n_html.c.
References __n_assert, _html_utf8_char(), new_nstr(), nstrcat_ex(), and run.
Referenced by test_escape().
| 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 at line 392 of file n_html.c.
References _n_form_field_free(), _n_html_attr_lookup(), _n_html_form_free(), _n_html_scan_tags(), _n_html_strdup_upper(), _n_html_strndup(), _n_html_strndup_lower(), N_HTML_FORM::action, N_HTML_TAG_::closing, N_HTML_FORM::enctype, N_HTML_FORM::fields, list_destroy(), list_foreach, list_push(), Malloc, MAX_LIST_ITEMS, N_HTML_FORM::method, N_FORM_FIELD::name, N_HTML_TAG_::name, new_generic_list(), N_FORM_FIELD::required, N_FORM_FIELD::type, and N_FORM_FIELD::value.
Referenced by test_empty_and_null(), test_enctype(), and test_forms().
| 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, a protocol-relative "//host" URL, or a "/" / "./" / "../" path; a "${...}" template is truncated to its static prefix), deduped, as a LIST of N_STR*; free with n_html_links_free.
Recovers fetch()/XHR/route endpoints a static link scan misses.
Definition at line 915 of file n_html.c.
References _n_html_push_js_url(), MAX_LIST_ITEMS, new_generic_list(), and start().
Referenced by test_js_urls(), and test_scripts().
| 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_STR*; free with n_html_links_free
Definition at line 360 of file n_html.c.
References _n_html_attr_lookup(), _n_html_push_link(), _n_html_scan_tags(), N_HTML_TAG_::closing, list_destroy(), list_foreach, MAX_LIST_ITEMS, N_HTML_TAG_::name, and new_generic_list().
Referenced by test_empty_and_null(), and test_links().
| 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 N_STR*; free with n_html_links_free.
Feed each to n_html_extract_js_urls to mine inline endpoints.
Definition at line 941 of file n_html.c.
References _n_html_find_ci(), char_to_nstr_ex(), free_nstr_ptr(), list_push(), MAX_LIST_ITEMS, and new_generic_list().
Referenced by test_scripts().
| void n_html_forms_free | ( | LIST ** | forms | ) |
free a list returned by n_html_extract_forms
Definition at line 984 of file n_html.c.
References list_destroy().
Referenced by test_empty_and_null(), test_enctype(), and test_forms().
| void n_html_links_free | ( | LIST ** | links | ) |
free a list returned by n_html_extract_links or n_sitemap_extract_urls
Definition at line 979 of file n_html.c.
References list_destroy().
Referenced by test_empty_and_null(), test_js_urls(), test_links(), test_scripts(), and test_sitemap().
| 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, collapse whitespace, and line-break block elements; returns a new N_STR (free with free_nstr) or NULL
Definition at line 627 of file n_html.c.
References _n_html_block_kind(), _n_html_entities, _n_html_flush(), _n_html_lc(), _n_html_put_cp(), _n_html_put_str(), char_to_nstr_ex(), Free, LOG_ERR, Malloc, n_log, and start().
Referenced by test_to_text().
| 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 at line 456 of file n_html.c.
References _n_html_strndup(), char_to_nstr(), Free, free_nstr_ptr(), list_push(), MAX_LIST_ITEMS, and new_generic_list().
Referenced by test_empty_and_null(), and test_sitemap().