1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-21 23:16:28 +02:00

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>
This commit is contained in:
Matthew DeVore 2019-06-27 15:54:11 -07:00 committed by Junio C Hamano
parent cf9ceb5a12
commit c2694952e3
4 changed files with 23 additions and 14 deletions

View File

@ -72,15 +72,16 @@ static void store_credential_file(const char *fn, struct credential *c)
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
strbuf_addf(&buf, "%s://", c->protocol); strbuf_addf(&buf, "%s://", c->protocol);
strbuf_addstr_urlencode(&buf, c->username, 1); strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
strbuf_addch(&buf, ':'); strbuf_addch(&buf, ':');
strbuf_addstr_urlencode(&buf, c->password, 1); strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
strbuf_addch(&buf, '@'); strbuf_addch(&buf, '@');
if (c->host) if (c->host)
strbuf_addstr_urlencode(&buf, c->host, 1); strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
if (c->path) { if (c->path) {
strbuf_addch(&buf, '/'); strbuf_addch(&buf, '/');
strbuf_addstr_urlencode(&buf, c->path, 0); strbuf_addstr_urlencode(&buf, c->path,
is_rfc3986_reserved_or_unreserved);
} }
rewrite_credential_file(fn, c, &buf); rewrite_credential_file(fn, c, &buf);

6
http.c
View File

@ -513,9 +513,11 @@ static void set_proxyauth_name_password(CURL *result)
#else #else
struct strbuf s = STRBUF_INIT; struct strbuf s = STRBUF_INIT;
strbuf_addstr_urlencode(&s, proxy_auth.username, 1); strbuf_addstr_urlencode(&s, proxy_auth.username,
is_rfc3986_unreserved);
strbuf_addch(&s, ':'); strbuf_addch(&s, ':');
strbuf_addstr_urlencode(&s, proxy_auth.password, 1); strbuf_addstr_urlencode(&s, proxy_auth.password,
is_rfc3986_unreserved);
curl_proxyuserpwd = strbuf_detach(&s, NULL); curl_proxyuserpwd = strbuf_detach(&s, NULL);
curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd); curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
#endif #endif

View File

@ -774,8 +774,10 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
} }
} }
static int is_rfc3986_reserved(char ch) int is_rfc3986_reserved_or_unreserved(char ch)
{ {
if (is_rfc3986_unreserved(ch))
return 1;
switch (ch) { switch (ch) {
case '!': case '*': case '\'': case '(': case ')': case ';': case '!': case '*': case '\'': case '(': case ')': case ';':
case ':': case '@': case '&': case '=': case '+': case '$': case ':': case '@': case '&': case '=': case '+': case '$':
@ -785,20 +787,19 @@ static int is_rfc3986_reserved(char ch)
return 0; return 0;
} }
static int is_rfc3986_unreserved(char ch) int is_rfc3986_unreserved(char ch)
{ {
return isalnum(ch) || return isalnum(ch) ||
ch == '-' || ch == '_' || ch == '.' || ch == '~'; ch == '-' || ch == '_' || ch == '.' || ch == '~';
} }
static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
int reserved) char_predicate allow_unencoded_fn)
{ {
strbuf_grow(sb, len); strbuf_grow(sb, len);
while (len--) { while (len--) {
char ch = *s++; char ch = *s++;
if (is_rfc3986_unreserved(ch) || if (allow_unencoded_fn(ch))
(!reserved && is_rfc3986_reserved(ch)))
strbuf_addch(sb, ch); strbuf_addch(sb, ch);
else else
strbuf_addf(sb, "%%%02x", (unsigned char)ch); 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,
} }
void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
int reserved) char_predicate allow_unencoded_fn)
{ {
strbuf_add_urlencode(sb, s, strlen(s), reserved); strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
} }
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes) void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)

View File

@ -666,8 +666,13 @@ void strbuf_branchname(struct strbuf *sb, const char *name,
*/ */
int strbuf_check_branch_ref(struct strbuf *sb, const char *name); int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
typedef int (*char_predicate)(char ch);
int is_rfc3986_unreserved(char ch);
int is_rfc3986_reserved_or_unreserved(char ch);
void strbuf_addstr_urlencode(struct strbuf *sb, const char *name, void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
int reserved); char_predicate allow_unencoded_fn);
__attribute__((format (printf,1,2))) __attribute__((format (printf,1,2)))
int printf_ln(const char *fmt, ...); int printf_ln(const char *fmt, ...);