threads / patch / 24169

v4Do not decode url protocol.

Subject: [PATCH v4] Do not decode url protocol.

## tl;dr

5 messages between Jun 22, 2010 and Jun 23, 2010. Diffs are folded; open one to read it.

replies: 4people: 4as markdown or json

Pascal Obry· Jun 22, 2010, 20:16 UTC · lore

When using the protocol git+ssh:// for example we do not want to decode the '+' as a space. The url decoding must take place only for the server name and parameters.

This fixes a regression introduced in 9d2e942.
---
 url.c |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)

Ok, so this is the fourth version of this patch. Thanks again Matthieu for the review. I think this time I got the place for the message right :)

Anyway, I think this time we properly skip the protocol decoding when needed.

Show changes to url.c +15 −4
diff --git a/url.c b/url.c
index cd32b92..0eb7fb3 100644
--- a/url.c
+++ b/url.c
@@ -67,12 +67,23 @@ static int url_decode_char(const char *q)
        return val;
 }

-static char *url_decode_internal(const char **query, const char *stop_at)
+static char *url_decode_internal(const char **query, const char *stop_at,
+               int with_protocol)
 {
        const char *q = *query;
+       const char *first_slash;
        struct strbuf out;

        strbuf_init(&out, 16);
+
+       /* Skip protocol if present. */
+       if (with_protocol) {
+         first_slash = strchr(*query, '/');
+
+         while (q < first_slash)
+               strbuf_addch(&out, *q++);
+       }
+
        do {
                unsigned char c = *q;

@@ -104,15 +115,15 @@ static char *url_decode_internal(const char
**query, const char *stop_at)

 char *url_decode(const char *url)
 {
-       return url_decode_internal(&url, NULL);
+       return url_decode_internal(&url, NULL, 1);
 }

 char *url_decode_parameter_name(const char **query)
 {
-       return url_decode_internal(query, "&=");
+       return url_decode_internal(query, "&=", 0);
 }

 char *url_decode_parameter_value(const char **query)
 {
-       return url_decode_internal(query, "&");
+       return url_decode_internal(query, "&", 0);
 }
-- 
1.7.1.426.gb436.dirty

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B
Matthieu Moy· Jun 22, 2010, 21:39 UTC · re: Pascal Obry · lore

Re: [PATCH v4] Do not decode url protocol.

Pascal Obry <pascal.obry@gmail.com> writes:
Show 12 quoted lines
> When using the protocol git+ssh:// for example we do not want to
> decode the '+' as a space. The url decoding must take place only
> for the server name and parameters.
>
> This fixes a regression introduced in 9d2e942.
> ---
>  url.c |   19 +++++++++++++++----
>  1 files changed, 15 insertions(+), 4 deletions(-)
>
> Ok, so this is the fourth version of this patch. Thanks again Matthieu
> for the review. I think this time I got the place for the message
> right :)

Hmm, what's so unclear in "between the --- (tripple) and the diffstat." ;-) ? (especially the "between" part)

Show 7 quoted lines
> +       /* Skip protocol if present. */
> +       if (with_protocol) {
> +         first_slash = strchr(*query, '/');
> +
> +         while (q < first_slash)
> +               strbuf_addch(&out, *q++);
> +       }

Nothing personal, but you messed up indentation. Git indents with tabs (8 chars width), not 2 spaces.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Jeff King· Jun 22, 2010, 22:46 UTC · re: Pascal Obry · lore

Re: [PATCH v4] Do not decode url protocol.

On Tue, Jun 22, 2010 at 10:16:57PM +0200, Pascal Obry wrote:
Show 5 quoted lines
> When using the protocol git+ssh:// for example we do not want to
> decode the '+' as a space. The url decoding must take place only
> for the server name and parameters.
> 
> This fixes a regression introduced in 9d2e942.

Sorry, this is completely my fault. I obviously didn't test with git+ssh. I looked at adding a new test case, but I don't think there's an easy way. The only way to trigger it is by using "git+ssh" or "ssh+git"; any other protocol (real or fake) will be handled by a custom protocol handler and won't execute this broken code at all.

The patch looks reasonable (and I agree with all of Matthieu's comments thus far). With respect to the "what if there is no slash" issue discussed earlier, I prefer to be a bit defensive about possible inputs in library-ish functions like this. But:

Show 9 quoted lines
> +
> +       /* Skip protocol if present. */
> +       if (with_protocol) {
> +         first_slash = strchr(*query, '/');
> +
> +         while (q < first_slash)
> +               strbuf_addch(&out, *q++);
> +       }
> +

I think this actually works OK, given that "q < first_slash" will be false if first_slash is NULL, assuming NULL is all-bytes-zero or some low number (which is not guaranteed by the standard, but is a pretty practical assumption these days).

However, you could make things a little more efficient by handing the whole thing to strbuf at once:

  if (with_protocol) {
          const char *first_slash = strchr(q, '/');
          if (first_slash) {
                  strbuf_add(&out, q, first_slash - q);
                  q = first_slash;
          }
  }

which of course needs first_slash to be checked explicitly. Not that the efficiency probably matters in practice.

-Peff
Junio C Hamano· Jun 23, 2010, 17:27 UTC · re: Pascal Obry · lore

Re: [PATCH v4] Do not decode url protocol.

Pascal Obry <pascal.obry@gmail.com> writes:
Show 5 quoted lines
> When using the protocol git+ssh:// for example we do not want to
> decode the '+' as a space. The url decoding must take place only
> for the server name and parameters.
>
> This fixes a regression introduced in 9d2e942.
Sign-off?

As the patch was whitespace-broken, I'm proposing to rewrite it like the following.

-- >8 -- url.c: "<scheme>://" part at the beginning should not be URL decoded

When using the protocol git+ssh:// for example we do not want to decode the '+' as a space. The url decoding must take place only for the server name and parameters.

This fixes a regression introduced in 9d2e942.
Initial-fix-by: Pascal Obry <pascal.obry@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 url.c |   28 ++++++++++++++++++----------
 1 files changed, 18 insertions(+), 10 deletions(-)
Show changes to url.c +18 −10
diff --git a/url.c b/url.c
index cd32b92..bf5bb9c 100644
--- a/url.c
+++ b/url.c
@@ -67,12 +67,10 @@ static int url_decode_char(const char *q)
 	return val;
 }
 
-static char *url_decode_internal(const char **query, const char *stop_at)
+static char *url_decode_internal(const char **query, const char *stop_at, struct strbuf *out)
 {
 	const char *q = *query;
-	struct strbuf out;
 
-	strbuf_init(&out, 16);
 	do {
 		unsigned char c = *q;
 
@@ -86,33 +84,43 @@ static char *url_decode_internal(const char **query, const char *stop_at)
 		if (c == '%') {
 			int val = url_decode_char(q + 1);
 			if (0 <= val) {
-				strbuf_addch(&out, val);
+				strbuf_addch(out, val);
 				q += 3;
 				continue;
 			}
 		}
 
 		if (c == '+')
-			strbuf_addch(&out, ' ');
+			strbuf_addch(out, ' ');
 		else
-			strbuf_addch(&out, c);
+			strbuf_addch(out, c);
 		q++;
 	} while (1);
 	*query = q;
-	return strbuf_detach(&out, NULL);
+	return strbuf_detach(out, NULL);
 }
 
 char *url_decode(const char *url)
 {
-	return url_decode_internal(&url, NULL);
+	struct strbuf out = STRBUF_INIT;
+	const char *slash = strchr(url, '/');
+
+	/* Skip protocol part if present */
+	if (slash && url < slash) {
+		strbuf_add(&out, url, slash - url);
+		url = slash;
+	}
+	return url_decode_internal(&url, NULL, &out);
 }
 
 char *url_decode_parameter_name(const char **query)
 {
-	return url_decode_internal(query, "&=");
+	struct strbuf out = STRBUF_INIT;
+	return url_decode_internal(query, "&=", &out);
 }
 
 char *url_decode_parameter_value(const char **query)
 {
-	return url_decode_internal(query, "&");
+	struct strbuf out = STRBUF_INIT;
+	return url_decode_internal(query, "&", &out);
 }
Jeff King· Jun 23, 2010, 17:38 UTC · re: Junio C Hamano · lore

Re: [PATCH v4] Do not decode url protocol.

On Wed, Jun 23, 2010 at 10:27:39AM -0700, Junio C Hamano wrote:
Show 13 quoted lines
> > When using the protocol git+ssh:// for example we do not want to
> > decode the '+' as a space. The url decoding must take place only
> > for the server name and parameters.
> >
> > This fixes a regression introduced in 9d2e942.
> 
> Sign-off?
> 
> As the patch was whitespace-broken, I'm proposing to rewrite it like the
> following.
> 
> -- >8 -- 
> url.c: "<scheme>://" part at the beginning should not be URL decoded
I think this is cleaner than Pascal's patch.
Acked-by: Jeff King <peff@peff.net>
Thanks all for fixing my bug. :)
-Peff

← back to recent threads