Skip to content

Commit 2d7099b

Browse files
matvoregitster
authored andcommitted
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 <[email protected]> Signed-off-by: Matthew DeVore <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b93d26 commit 2d7099b

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

credential-store.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ static void store_credential_file(const char *fn, struct credential *c)
7272
struct strbuf buf = STRBUF_INIT;
7373

7474
strbuf_addf(&buf, "%s://", c->protocol);
75-
strbuf_addstr_urlencode(&buf, c->username, 1);
75+
strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
7676
strbuf_addch(&buf, ':');
77-
strbuf_addstr_urlencode(&buf, c->password, 1);
77+
strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
7878
strbuf_addch(&buf, '@');
7979
if (c->host)
80-
strbuf_addstr_urlencode(&buf, c->host, 1);
80+
strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
8181
if (c->path) {
8282
strbuf_addch(&buf, '/');
83-
strbuf_addstr_urlencode(&buf, c->path, 0);
83+
strbuf_addstr_urlencode(&buf, c->path,
84+
is_rfc3986_reserved_or_unreserved);
8485
}
8586

8687
rewrite_credential_file(fn, c, &buf);

http.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,11 @@ static void set_proxyauth_name_password(CURL *result)
513513
#else
514514
struct strbuf s = STRBUF_INIT;
515515

516-
strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
516+
strbuf_addstr_urlencode(&s, proxy_auth.username,
517+
is_rfc3986_unreserved);
517518
strbuf_addch(&s, ':');
518-
strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
519+
strbuf_addstr_urlencode(&s, proxy_auth.password,
520+
is_rfc3986_unreserved);
519521
curl_proxyuserpwd = strbuf_detach(&s, NULL);
520522
curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
521523
#endif

strbuf.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,10 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
774774
}
775775
}
776776

777-
static int is_rfc3986_reserved(char ch)
777+
int is_rfc3986_reserved_or_unreserved(char ch)
778778
{
779+
if (is_rfc3986_unreserved(ch))
780+
return 1;
779781
switch (ch) {
780782
case '!': case '*': case '\'': case '(': case ')': case ';':
781783
case ':': case '@': case '&': case '=': case '+': case '$':
@@ -785,30 +787,29 @@ static int is_rfc3986_reserved(char ch)
785787
return 0;
786788
}
787789

788-
static int is_rfc3986_unreserved(char ch)
790+
int is_rfc3986_unreserved(char ch)
789791
{
790792
return isalnum(ch) ||
791793
ch == '-' || ch == '_' || ch == '.' || ch == '~';
792794
}
793795

794796
static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
795-
int reserved)
797+
char_predicate allow_unencoded_fn)
796798
{
797799
strbuf_grow(sb, len);
798800
while (len--) {
799801
char ch = *s++;
800-
if (is_rfc3986_unreserved(ch) ||
801-
(!reserved && is_rfc3986_reserved(ch)))
802+
if (allow_unencoded_fn(ch))
802803
strbuf_addch(sb, ch);
803804
else
804805
strbuf_addf(sb, "%%%02x", (unsigned char)ch);
805806
}
806807
}
807808

808809
void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
809-
int reserved)
810+
char_predicate allow_unencoded_fn)
810811
{
811-
strbuf_add_urlencode(sb, s, strlen(s), reserved);
812+
strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
812813
}
813814

814815
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)

strbuf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,13 @@ void strbuf_branchname(struct strbuf *sb, const char *name,
666666
*/
667667
int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
668668

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+
669674
void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
670-
int reserved);
675+
char_predicate allow_unencoded_fn);
671676

672677
__attribute__((format (printf,1,2)))
673678
int printf_ln(const char *fmt, ...);

0 commit comments

Comments
 (0)