Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 2 | #include "hex.h" |
Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 3 | #include "strbuf.h" |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 4 | #include "url.h" |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 5 | |
| 6 | int is_urlschemechar(int first_flag, int ch) |
| 7 | { |
| 8 | /* |
| 9 | * The set of valid URL schemes, as per STD66 (RFC3986) is |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 10 | * '[A-Za-z][A-Za-z0-9+.-]*'. But use slightly looser check |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 11 | * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version |
| 12 | * of check used '[A-Za-z0-9]+' so not to break any remote |
| 13 | * helpers. |
| 14 | */ |
| 15 | int alphanumeric, special; |
| 16 | alphanumeric = ch > 0 && isalnum(ch); |
| 17 | special = ch == '+' || ch == '-' || ch == '.'; |
| 18 | return alphanumeric || (!first_flag && special); |
| 19 | } |
| 20 | |
| 21 | int is_url(const char *url) |
| 22 | { |
Junio C Hamano | b33a1b9 | 2011-10-03 10:56:42 -0700 | [diff] [blame] | 23 | /* Is "scheme" part reasonable? */ |
| 24 | if (!url || !is_urlschemechar(1, *url++)) |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 25 | return 0; |
Junio C Hamano | b33a1b9 | 2011-10-03 10:56:42 -0700 | [diff] [blame] | 26 | while (*url && *url != ':') { |
| 27 | if (!is_urlschemechar(0, *url++)) |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 28 | return 0; |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 29 | } |
Junio C Hamano | b33a1b9 | 2011-10-03 10:56:42 -0700 | [diff] [blame] | 30 | /* We've seen "scheme"; we want colon-slash-slash */ |
| 31 | return (url[0] == ':' && url[1] == '/' && url[2] == '/'); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 34 | static char *url_decode_internal(const char **query, int len, |
| 35 | const char *stop_at, struct strbuf *out, |
| 36 | int decode_plus) |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 37 | { |
| 38 | const char *q = *query; |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 39 | |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 40 | while (len) { |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 41 | unsigned char c = *q; |
| 42 | |
| 43 | if (!c) |
| 44 | break; |
| 45 | if (stop_at && strchr(stop_at, c)) { |
| 46 | q++; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 47 | len--; |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 48 | break; |
| 49 | } |
| 50 | |
Matthew DeVore | 3f6b8a6 | 2019-06-04 10:57:04 -0700 | [diff] [blame] | 51 | if (c == '%' && (len < 0 || len >= 3)) { |
René Scharfe | d233097 | 2016-09-03 17:59:20 +0200 | [diff] [blame] | 52 | int val = hex2chr(q + 1); |
Matthew DeVore | d37dc23 | 2019-06-04 10:57:05 -0700 | [diff] [blame] | 53 | if (0 < val) { |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 54 | strbuf_addch(out, val); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 55 | q += 3; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 56 | len -= 3; |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 57 | continue; |
| 58 | } |
| 59 | } |
| 60 | |
Thomas Rast | 730220d | 2010-07-24 16:49:04 +0200 | [diff] [blame] | 61 | if (decode_plus && c == '+') |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 62 | strbuf_addch(out, ' '); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 63 | else |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 64 | strbuf_addch(out, c); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 65 | q++; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 66 | len--; |
| 67 | } |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 68 | *query = q; |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 69 | return strbuf_detach(out, NULL); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | char *url_decode(const char *url) |
| 73 | { |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 74 | return url_decode_mem(url, strlen(url)); |
| 75 | } |
| 76 | |
| 77 | char *url_decode_mem(const char *url, int len) |
| 78 | { |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 79 | struct strbuf out = STRBUF_INIT; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 80 | const char *colon = memchr(url, ':', len); |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 81 | |
| 82 | /* Skip protocol part if present */ |
Junio C Hamano | 3c73a1d | 2010-06-24 13:36:30 -0700 | [diff] [blame] | 83 | if (colon && url < colon) { |
| 84 | strbuf_add(&out, url, colon - url); |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 85 | len -= colon - url; |
Junio C Hamano | 3c73a1d | 2010-06-24 13:36:30 -0700 | [diff] [blame] | 86 | url = colon; |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 87 | } |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 88 | return url_decode_internal(&url, len, NULL, &out, 0); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Matthew DeVore | e987df5 | 2019-06-27 15:54:08 -0700 | [diff] [blame] | 91 | char *url_percent_decode(const char *encoded) |
| 92 | { |
| 93 | struct strbuf out = STRBUF_INIT; |
| 94 | return url_decode_internal(&encoded, strlen(encoded), NULL, &out, 0); |
| 95 | } |
| 96 | |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 97 | char *url_decode_parameter_name(const char **query) |
| 98 | { |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 99 | struct strbuf out = STRBUF_INIT; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 100 | return url_decode_internal(query, -1, "&=", &out, 1); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | char *url_decode_parameter_value(const char **query) |
| 104 | { |
Junio C Hamano | ce83eda | 2010-06-23 10:27:39 -0700 | [diff] [blame] | 105 | struct strbuf out = STRBUF_INIT; |
Jeff King | 66c8448 | 2011-07-18 03:48:51 -0400 | [diff] [blame] | 106 | return url_decode_internal(query, -1, "&", &out, 1); |
Jeff King | 638794c | 2010-05-23 05:17:55 -0400 | [diff] [blame] | 107 | } |
Tay Ray Chuan | 1966d9f | 2010-11-25 16:21:04 +0800 | [diff] [blame] | 108 | |
| 109 | void end_url_with_slash(struct strbuf *buf, const char *url) |
| 110 | { |
| 111 | strbuf_addstr(buf, url); |
Jeff King | 00b6c17 | 2015-09-24 17:08:35 -0400 | [diff] [blame] | 112 | strbuf_complete(buf, '/'); |
Tay Ray Chuan | 1966d9f | 2010-11-25 16:21:04 +0800 | [diff] [blame] | 113 | } |
Tay Ray Chuan | 3793a30 | 2010-11-25 16:21:05 +0800 | [diff] [blame] | 114 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 115 | void str_end_url_with_slash(const char *url, char **dest) |
| 116 | { |
Tay Ray Chuan | 3793a30 | 2010-11-25 16:21:05 +0800 | [diff] [blame] | 117 | struct strbuf buf = STRBUF_INIT; |
| 118 | end_url_with_slash(&buf, url); |
| 119 | free(*dest); |
| 120 | *dest = strbuf_detach(&buf, NULL); |
| 121 | } |