fix: safely print URI and do not limit then length of URI - webdump - HTML to plain-text converter for webpages
HTML git clone git://git.codemadness.org/webdump
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit f86c73c2c7bdc1f6a1af0191c71a7ce0fa7e2d35
DIR parent 278d829beb658d1eb18dba03c804d4a949e7bd43
HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Wed, 24 Jun 2026 18:42:04 +0200
fix: safely print URI and do not limit then length of URI
- control characters for URI's were printed, this was unsafe. Now
still allow control characters but print them percent-encoded.
hprinturi(): print an URI with whitespace or indent handling and
line-wrapping enabled.
printuri(): print an URI directly with no whitespace handling, indent and
line-wrapping etc, for link references etc.
- hprintf was used for links and has a buffer limit of 256 bytes. This is not a
limit anymore (at least for links here).
Diffstat:
M webdump.c | 55 +++++++++++++++++++++++++++----
1 file changed, 49 insertions(+), 6 deletions(-)
---
DIR diff --git a/webdump.c b/webdump.c
@@ -686,6 +686,24 @@ uri_format(char *buf, size_t bufsiz, struct uri *u)
u->fragment);
}
+static void
+printuri(const char *s)
+{
+ static char hex[] = "0123456789ABCDEF";
+ unsigned char c;
+
+ for (; *s; s++) {
+ c = (unsigned char)*s;
+ if (ISCNTRL((unsigned char)*s)) {
+ putchar('%');
+ putchar(hex[c >> 4]);
+ putchar(hex[c & 0x0f]);
+ } else {
+ putchar(*s);
+ }
+ }
+}
+
/* compare tag name (case-insensitive) */
static int
tagcmp(const char *s1, const char *s2)
@@ -985,6 +1003,24 @@ hprintf(const char *fmt, ...)
}
static void
+hprinturi(const char *s)
+{
+ static char hex[] = "0123456789ABCDEF";
+ unsigned char c;
+
+ for (; *s; s++) {
+ c = (unsigned char)*s;
+ if (ISCNTRL((unsigned char)*s)) {
+ hputchar('%');
+ hputchar(hex[c >> 4]);
+ hputchar(hex[c & 0x0f]);
+ } else {
+ hputchar(*s);
+ }
+ }
+}
+
+static void
newline(void)
{
if (skipinitialws)
@@ -1523,7 +1559,9 @@ printlinkrefs(void)
for (i = 0; i < nvisrefs; i++) {
ref = visrefs[i];
- printf(" %zu. %s (%s)\n", ref->linknr, ref->url, ref->type);
+ printf(" %zu. ", ref->linknr);
+ printuri(ref->url);
+ printf(" (%s)\n", ref->type);
}
if (nhiddenrefs > 0)
@@ -1531,7 +1569,9 @@ printlinkrefs(void)
/* hidden links don't have a link number, just count them */
for (i = 0; i < nhiddenrefs; i++) {
ref = hiddenrefs[i];
- printf(" %zu. %s (%s)\n", ref->linknr, ref->url, ref->type);
+ printf(" %zu. ", ref->linknr);
+ printuri(ref->url);
+ printf(" (%s)\n", ref->type);
}
}
@@ -1748,10 +1788,13 @@ endnode(struct node *cur)
if (showrefinline)
hprintf("[%zu]", ref->linknr);
if (showurlinline) {
- if (ref->tagid == TagA)
- hprintf("[%s]", ref->url);
- else
- hprintf("[%s: %s]", ref->type, ref->url);
+ hputchar('[');
+ if (ref->tagid != TagA) {
+ hprint(ref->type);
+ hprint(": ");
+ }
+ hprinturi(ref->url);
+ hputchar(']');
}
if (showrefinline || showurlinline) {
endmarkup(MarkupReverse);