Merge branch 'jk/partial-clone-sparse-blob'
[git/git.git] / url.c
CommitLineData
638794cd 1#include "cache.h"
c2e86add 2#include "url.h"
638794cd
JK
3
4int is_urlschemechar(int first_flag, int ch)
5{
6 /*
7 * The set of valid URL schemes, as per STD66 (RFC3986) is
8 * '[A-Za-z][A-Za-z0-9+.-]*'. But use sightly looser check
9 * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version
10 * of check used '[A-Za-z0-9]+' so not to break any remote
11 * helpers.
12 */
13 int alphanumeric, special;
14 alphanumeric = ch > 0 && isalnum(ch);
15 special = ch == '+' || ch == '-' || ch == '.';
16 return alphanumeric || (!first_flag && special);
17}
18
19int is_url(const char *url)
20{
b33a1b9f
JH
21 /* Is "scheme" part reasonable? */
22 if (!url || !is_urlschemechar(1, *url++))
638794cd 23 return 0;
b33a1b9f
JH
24 while (*url && *url != ':') {
25 if (!is_urlschemechar(0, *url++))
638794cd 26 return 0;
638794cd 27 }
b33a1b9f
JH
28 /* We've seen "scheme"; we want colon-slash-slash */
29 return (url[0] == ':' && url[1] == '/' && url[2] == '/');
638794cd
JK
30}
31
66c84485
JK
32static char *url_decode_internal(const char **query, int len,
33 const char *stop_at, struct strbuf *out,
34 int decode_plus)
638794cd
JK
35{
36 const char *q = *query;
638794cd 37
66c84485 38 while (len) {
638794cd
JK
39 unsigned char c = *q;
40
41 if (!c)
42 break;
43 if (stop_at && strchr(stop_at, c)) {
44 q++;
66c84485 45 len--;
638794cd
JK
46 break;
47 }
48
3f6b8a61 49 if (c == '%' && (len < 0 || len >= 3)) {
d2330973 50 int val = hex2chr(q + 1);
d37dc239 51 if (0 < val) {
ce83eda1 52 strbuf_addch(out, val);
638794cd 53 q += 3;
66c84485 54 len -= 3;
638794cd
JK
55 continue;
56 }
57 }
58
730220de 59 if (decode_plus && c == '+')
ce83eda1 60 strbuf_addch(out, ' ');
638794cd 61 else
ce83eda1 62 strbuf_addch(out, c);
638794cd 63 q++;
66c84485
JK
64 len--;
65 }
638794cd 66 *query = q;
ce83eda1 67 return strbuf_detach(out, NULL);
638794cd
JK
68}
69
70char *url_decode(const char *url)
66c84485
JK
71{
72 return url_decode_mem(url, strlen(url));
73}
74
75char *url_decode_mem(const char *url, int len)
638794cd 76{
ce83eda1 77 struct strbuf out = STRBUF_INIT;
66c84485 78 const char *colon = memchr(url, ':', len);
ce83eda1
JH
79
80 /* Skip protocol part if present */
3c73a1d5
JH
81 if (colon && url < colon) {
82 strbuf_add(&out, url, colon - url);
66c84485 83 len -= colon - url;
3c73a1d5 84 url = colon;
ce83eda1 85 }
66c84485 86 return url_decode_internal(&url, len, NULL, &out, 0);
638794cd
JK
87}
88
e987df5f
MD
89char *url_percent_decode(const char *encoded)
90{
91 struct strbuf out = STRBUF_INIT;
92 return url_decode_internal(&encoded, strlen(encoded), NULL, &out, 0);
93}
94
638794cd
JK
95char *url_decode_parameter_name(const char **query)
96{
ce83eda1 97 struct strbuf out = STRBUF_INIT;
66c84485 98 return url_decode_internal(query, -1, "&=", &out, 1);
638794cd
JK
99}
100
101char *url_decode_parameter_value(const char **query)
102{
ce83eda1 103 struct strbuf out = STRBUF_INIT;
66c84485 104 return url_decode_internal(query, -1, "&", &out, 1);
638794cd 105}
1966d9f3
TRC
106
107void end_url_with_slash(struct strbuf *buf, const char *url)
108{
109 strbuf_addstr(buf, url);
00b6c178 110 strbuf_complete(buf, '/');
1966d9f3 111}
3793a309 112
3b335762
NTND
113void str_end_url_with_slash(const char *url, char **dest)
114{
3793a309
TRC
115 struct strbuf buf = STRBUF_INIT;
116 end_url_with_slash(&buf, url);
117 free(*dest);
118 *dest = strbuf_detach(&buf, NULL);
119}