strbuf: give URL-encoding API a char predicate fn

Allow callers to specify exactly what characters need to be URL-encoded and which do not. This new API will be taken advantage of in a patch later in this set. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Matthew DeVore <matvore@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthew DeVore committed Jun 27, 2019 at 15:54 UTC c2694952e33764818983fa247dcee72113c6ac6a
4 files changed +23 -14
credential-store.c
+5 -4
@@ -72,15 +72,16 @@ static void store_credential_file(const char *fn, struct credential *c)
72 struct strbuf buf = STRBUF_INIT;
73
74 strbuf_addf(&buf, "%s://", c->protocol);
75 - strbuf_addstr_urlencode(&buf, c->username, 1);
75 + strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
76 strbuf_addch(&buf, ':');
77 - strbuf_addstr_urlencode(&buf, c->password, 1);
77 + strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
78 strbuf_addch(&buf, '@');
79 if (c->host)
80 - strbuf_addstr_urlencode(&buf, c->host, 1);
80 + strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
81 if (c->path) {
82 strbuf_addch(&buf, '/');
83 - strbuf_addstr_urlencode(&buf, c->path, 0);
83 + strbuf_addstr_urlencode(&buf, c->path,
84 + is_rfc3986_reserved_or_unreserved);
85 }
86
87 rewrite_credential_file(fn, c, &buf);
http.c
+4 -2
@@ -513,9 +513,11 @@ static void set_proxyauth_name_password(CURL *result)
513 #else
514 struct strbuf s = STRBUF_INIT;
515
516 - strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
516 + strbuf_addstr_urlencode(&s, proxy_auth.username,
517 + is_rfc3986_unreserved);
518 strbuf_addch(&s, ':');
518 - strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
519 + strbuf_addstr_urlencode(&s, proxy_auth.password,
520 + is_rfc3986_unreserved);
521 curl_proxyuserpwd = strbuf_detach(&s, NULL);
522 curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
523 #endif
strbuf.c
+8 -7
@@ -774,8 +774,10 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
774 }
775 }
776
777 -static int is_rfc3986_reserved(char ch)
777 +int is_rfc3986_reserved_or_unreserved(char ch)
778 {
779 + if (is_rfc3986_unreserved(ch))
780 + return 1;
781 switch (ch) {
782 case '!': case '*': case '\'': case '(': case ')': case ';':
783 case ':': case '@': case '&': case '=': case '+': case '$':
@@ -785,20 +787,19 @@ static int is_rfc3986_reserved(char ch)
787 return 0;
788 }
789
788 -static int is_rfc3986_unreserved(char ch)
790 +int is_rfc3986_unreserved(char ch)
791 {
792 return isalnum(ch) ||
793 ch == '-' || ch == '_' || ch == '.' || ch == '~';
794 }
795
796 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
795 - int reserved)
797 + char_predicate allow_unencoded_fn)
798 {
799 strbuf_grow(sb, len);
800 while (len--) {
801 char ch = *s++;
800 - if (is_rfc3986_unreserved(ch) ||
801 - (!reserved && is_rfc3986_reserved(ch)))
802 + if (allow_unencoded_fn(ch))
803 strbuf_addch(sb, ch);
804 else
805 strbuf_addf(sb, "%%%02x", (unsigned char)ch);
@@ -806,9 +807,9 @@ static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
807 }
808
809 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
809 - int reserved)
810 + char_predicate allow_unencoded_fn)
811 {
811 - strbuf_add_urlencode(sb, s, strlen(s), reserved);
812 + strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
813 }
814
815 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
strbuf.h
+6 -1
@@ -666,8 +666,13 @@ void strbuf_branchname(struct strbuf *sb, const char *name,
666 */
667 int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
668
669 +typedef int (*char_predicate)(char ch);
670 +
671 +int is_rfc3986_unreserved(char ch);
672 +int is_rfc3986_reserved_or_unreserved(char ch);
673 +
674 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
670 - int reserved);
675 + char_predicate allow_unencoded_fn);
676
677 __attribute__((format (printf,1,2)))
678 int printf_ln(const char *fmt, ...);