# [PATCH v2] http: use strbuf API in quote_ref_url

2 messages from 2009-03-07 to 2009-03-07. Participants: Tay Ray Chuan, Johannes Schindelin.
Thread: https://gitlist.dev/t/18200

## Tay Ray Chuan, 2009-03-07 16:47

Subject: [PATCH v2] http: use strbuf API in quote_ref_url
Message-ID: <49B2A519.8050802@gmail.com>
URL: https://gitlist.dev/e/49B2A519.8050802%40gmail.com

```
In addition, ''quote_ref_url'' inserts a slash between the base URL and
remote ref path only if needed. Previously, this insertion wasn't
contingent on the lack of a separating slash.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---

Differences from v1:
 *use buf when checking for '/'
 *remove for's braces

 http.c |   30 ++++++++++--------------------
 1 files changed, 10 insertions(+), 20 deletions(-)

diff --git a/http.c b/http.c
index cdedeb6..1691498 100644
--- a/http.c
+++ b/http.c
@@ -577,31 +577,21 @@ static inline int hex(int v)

 static char *quote_ref_url(const char *base, const char *ref)
 {
+	struct strbuf buf = STRBUF_INIT;
 	const char *cp;
-	char *dp, *qref;
-	int len, baselen, ch;
+	int ch;

-	baselen = strlen(base);
-	len = baselen + 2; /* '/' after base and terminating NUL */
-	for (cp = ref; (ch = *cp) != 0; cp++, len++)
+	strbuf_addstr(&buf, base);
+	if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
+		strbuf_addstr(&buf, "/");
+
+	for (cp = ref; (ch = *cp) != 0; cp++)
 		if (needs_quote(ch))
-			len += 2; /* extra two hex plus replacement % */
-	qref = xmalloc(len);
-	memcpy(qref, base, baselen);
-	dp = qref + baselen;
-	*(dp++) = '/';
-	for (cp = ref; (ch = *cp) != 0; cp++) {
-		if (needs_quote(ch)) {
-			*dp++ = '%';
-			*dp++ = hex((ch >> 4) & 0xF);
-			*dp++ = hex(ch & 0xF);
-		}
+			strbuf_addf(&buf, "%%%02x", ch);
 		else
-			*dp++ = ch;
-	}
-	*dp = 0;
+			strbuf_addch(&buf, *cp);

-	return qref;
+	return strbuf_detach(&buf, NULL);
 }

 int http_fetch_ref(const char *base, struct ref *ref)
-- 
1.6.2.rc1

```

## Johannes Schindelin, 2009-03-07 17:32

Subject: Re: [PATCH v2] http: use strbuf API in quote_ref_url
Message-ID: <alpine.DEB.1.00.0903071832270.10279@pacific.mpi-cbg.de>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0903071832270.10279%40pacific.mpi-cbg.de
In-Reply-To: <49B2A519.8050802@gmail.com>

```
Hi,

On Sun, 8 Mar 2009, Tay Ray Chuan wrote:

> In addition, ''quote_ref_url'' inserts a slash between the base URL and
> remote ref path only if needed. Previously, this insertion wasn't
> contingent on the lack of a separating slash.
> 
> Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>

Thanks, and ACK ;-)

Ciao,
Dscho

```
