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