[master] d7cb65d04 Add VSB_QUOTE_ABBREVIATE which appreviates the input with "[...]" if it is longer than specified.

Poul-Henning Kamp phk at FreeBSD.org
Mon Dec 8 13:19:04 UTC 2025


commit d7cb65d0423c2bb5552eebcef9fb02caf04383c7
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Dec 8 12:46:01 2025 +0000

    Add VSB_QUOTE_ABBREVIATE which appreviates the input with "[...]" if it is longer than specified.

diff --git a/include/vsb.h b/include/vsb.h
index f8ab8b150..6787a587c 100644
--- a/include/vsb.h
+++ b/include/vsb.h
@@ -123,6 +123,11 @@ void		 VSB_destroy(struct vsb **);
 	 * Not valid with VSB_QUOTE_JSON and VSB_QUOTE_HEX
 	 */
 
+#define VSB_QUOTE_ABBREVIATE	64
+	/*
+	 * Abbreviate with "[...]" If longer than specified length 
+	 */
+
 void		 VSB_quote_pfx(struct vsb *, const char*, const void *,
 		     int len, int how);
 void		 VSB_quote(struct vsb *, const void *, int len, int how);
diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index 2a405987a..42ae51c7e 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -543,6 +543,15 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 		how |= VSB_QUOTE_NONL;
 
 	assert(p != NULL);
+	if (how & VSB_QUOTE_ABBREVIATE) {
+		assert (len > 5);
+		if (strlen(v) < len) {
+			len = strlen(v);
+			how &= ~VSB_QUOTE_ABBREVIATE;
+		} else {
+			len -= 5;
+		}
+	}
 	if (len == -1)
 		len = strlen(v);
 
@@ -582,6 +591,8 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 
 	if (!quote) {
 		VSB_bcat(s, p, len);
+		if (how & VSB_QUOTE_ABBREVIATE)
+			VSB_cat(s, "[...]");
 		if ((how & VSB_QUOTE_NONL) &&
 		    p[len-1] != '\n')
 			(void)VSB_putc(s, '\n');
@@ -640,6 +651,8 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 			break;
 		}
 	}
+	if (how & VSB_QUOTE_ABBREVIATE)
+		VSB_cat(s, "[...]");
 	if (how & VSB_QUOTE_CSTR)
 		VSB_putc(s, '"');
 	if ((how & VSB_QUOTE_NONL) && !nl)
diff --git a/lib/libvarnish/vsb_test.c b/lib/libvarnish/vsb_test.c
index 373e4bada..593c05cd3 100644
--- a/lib/libvarnish/vsb_test.c
+++ b/lib/libvarnish/vsb_test.c
@@ -126,6 +126,11 @@ static struct tc tcs[] = {
 		-1, "\n\t",
 		"PFX\nPFX\\t\n"
 	},
+	{
+		VSB_QUOTE_CSTR | VSB_QUOTE_ABBREVIATE,
+		11, "Hello World\n",
+		"PFX\"Hello [...]\"",
+	},
 	{
 		0, -1, NULL, NULL
 	}
@@ -180,6 +185,8 @@ main(int argc, char *argv[])
 				VSB_cat(vsbo, "\n\tVSB_QUOTE_UNSAFE");
 			if (tc->how & VSB_QUOTE_ESCHEX)
 				VSB_cat(vsbo, "\n\tVSB_QUOTE_ESCHEX");
+			if (tc->how & VSB_QUOTE_ABBREVIATE)
+				VSB_cat(vsbo, "\n\tVSB_QUOTE_ABBREVIATE");
 			VSB_cat(vsbo, "\n\n");
 			err = 1;
 		}



More information about the varnish-commit mailing list