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