r4301 - in branches/2.0/varnish-cache: bin/varnishd include lib/libvarnish

tfheen at projects.linpro.no tfheen at projects.linpro.no
Thu Oct 8 15:23:37 CEST 2009


Author: tfheen
Date: 2009-10-08 15:23:36 +0200 (Thu, 08 Oct 2009)
New Revision: 4301

Modified:
   branches/2.0/varnish-cache/bin/varnishd/cache_ban.c
   branches/2.0/varnish-cache/include/vsb.h
   branches/2.0/varnish-cache/lib/libvarnish/cli_common.c
   branches/2.0/varnish-cache/lib/libvarnish/vsb.c
Log:
Merge r4176: Give vsb_quote() an optional length paramter (Pass -1 for strlen).

Add a complementary vsb_unquote() function.



Modified: branches/2.0/varnish-cache/bin/varnishd/cache_ban.c
===================================================================
--- branches/2.0/varnish-cache/bin/varnishd/cache_ban.c	2009-10-08 13:17:00 UTC (rev 4300)
+++ branches/2.0/varnish-cache/bin/varnishd/cache_ban.c	2009-10-08 13:23:36 UTC (rev 4301)
@@ -366,7 +366,7 @@
 	sb = vsb_newauto();
 	XXXAN(sb);
 	vsb_printf(sb, "%s %s ", a1, a2);
-	vsb_quote(sb, a3, 0);
+	vsb_quote(sb, a3, -1, 0);
 	vsb_finish(sb);
 	AZ(vsb_overflowed(sb));
 	bt->test = strdup(vsb_data(sb));

Modified: branches/2.0/varnish-cache/include/vsb.h
===================================================================
--- branches/2.0/varnish-cache/include/vsb.h	2009-10-08 13:17:00 UTC (rev 4300)
+++ branches/2.0/varnish-cache/include/vsb.h	2009-10-08 13:23:36 UTC (rev 4301)
@@ -77,7 +77,8 @@
 int		 vsb_len(struct vsb *);
 int		 vsb_done(const struct vsb *);
 void		 vsb_delete(struct vsb *);
-void		 vsb_quote(struct vsb *s, const char *p, int how);
+void		 vsb_quote(struct vsb *s, const char *p, int len, int how);
+const char	*vsb_unquote(struct vsb *s, const char *p, int len, int how);
 #ifdef __cplusplus
 };
 #endif

Modified: branches/2.0/varnish-cache/lib/libvarnish/cli_common.c
===================================================================
--- branches/2.0/varnish-cache/lib/libvarnish/cli_common.c	2009-10-08 13:17:00 UTC (rev 4300)
+++ branches/2.0/varnish-cache/lib/libvarnish/cli_common.c	2009-10-08 13:23:36 UTC (rev 4301)
@@ -73,7 +73,7 @@
 cli_quote(struct cli *cli, const char *s)
 {
 
-	vsb_quote(cli->sb, s, 0);
+	vsb_quote(cli->sb, s, -1, 0);
 }
 
 void

Modified: branches/2.0/varnish-cache/lib/libvarnish/vsb.c
===================================================================
--- branches/2.0/varnish-cache/lib/libvarnish/vsb.c	2009-10-08 13:17:00 UTC (rev 4300)
+++ branches/2.0/varnish-cache/lib/libvarnish/vsb.c	2009-10-08 13:23:36 UTC (rev 4301)
@@ -481,25 +481,27 @@
  * Quote a string
  */
 void
-vsb_quote(struct vsb *s, const char *p, int how)
+vsb_quote(struct vsb *s, const char *p, int len, int how)
 {
 	const char *q;
 	int quote = 0;
 
 	(void)how;	/* For future enhancements */
+	if (len == -1)
+		len = strlen(p);
 
-	for (q = p; *q != '\0'; q++) {
+	for (q = p; q < p + len; q++) {
 		if (!isgraph(*q) || *q == '"') {
 			quote++;
 			break;
 		}
 	}
 	if (!quote) {
-		(void)vsb_cat(s, p);
+		(void)vsb_bcat(s, p, len);
 		return;
 	}
 	(void)vsb_putc(s, '"');
-	for (q = p; *q != '\0'; q++) {
+	for (q = p; q < p + len; q++) {
 		switch (*q) {
 		case ' ':
 			(void)vsb_putc(s, *q);
@@ -522,9 +524,66 @@
 			if (isgraph(*q))
 				(void)vsb_putc(s, *q);
 			else
-				(void)vsb_printf(s, "\\%o", *q);
+				(void)vsb_printf(s, "\\%o", *q & 0xff);
 			break;
 		}
 	}
 	(void)vsb_putc(s, '"');
 }
+
+/*
+ * Unquote a string
+ */
+const char *
+vsb_unquote(struct vsb *s, const char *p, int len, int how)
+{
+	const char *q;
+	char *r;
+	unsigned long u;
+	char c;
+
+	(void)how;	/* For future enhancements */
+
+	if (len == -1)
+		len = strlen(p);
+
+	for (q = p; q < p + len; q++) {
+		if (*q != '\\') {
+			(void)vsb_bcat(s, q, 1);
+			continue;
+		}
+		if (++q >= p + len)
+			return ("Incomplete '\\'-sequence at end of string");
+
+		switch(*q) {
+		case 'n':
+			(void)vsb_bcat(s, "\n", 1);
+			continue;
+		case 'r':
+			(void)vsb_bcat(s, "\r", 1);
+			continue;
+		case 't':
+			(void)vsb_bcat(s, "\t", 1);
+			continue;
+		case '0':
+		case '1':
+		case '2':
+		case '3':
+		case '4':
+		case '5':
+		case '6':
+		case '7':
+			errno = 0;
+			u = strtoul(q, &r, 8);
+			if (errno != 0 || (u & ~0xff))
+				return ("\\ooo sequence out of range");
+			c = (char)u;
+			(void)vsb_bcat(s, &c, 1);
+			q = r - 1;
+			continue;
+		default:
+			(void)vsb_bcat(s, q, 1);
+		}
+	}
+	return (NULL);
+}



More information about the varnish-commit mailing list