]> git.ipfire.org Git - thirdparty/git.git/blame - url.c
Merge git://repo.or.cz/git-gui
[thirdparty/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
32static int url_decode_char(const char *q)
33{
34 int i;
35 unsigned char val = 0;
36 for (i = 0; i < 2; i++) {
37 unsigned char c = *q++;
38 val <<= 4;
39 if (c >= '0' && c <= '9')
40 val += c - '0';
41 else if (c >= 'a' && c <= 'f')
42 val += c - 'a' + 10;
43 else if (c >= 'A' && c <= 'F')
44 val += c - 'A' + 10;
45 else
46 return -1;
47 }
48 return val;
49}
50
730220de
TR
51static char *url_decode_internal(const char **query, const char *stop_at,
52 struct strbuf *out, int decode_plus)
638794cd
JK
53{
54 const char *q = *query;
638794cd 55
638794cd
JK
56 do {
57 unsigned char c = *q;
58
59 if (!c)
60 break;
61 if (stop_at && strchr(stop_at, c)) {
62 q++;
63 break;
64 }
65
66 if (c == '%') {
67 int val = url_decode_char(q + 1);
68 if (0 <= val) {
ce83eda1 69 strbuf_addch(out, val);
638794cd
JK
70 q += 3;
71 continue;
72 }
73 }
74
730220de 75 if (decode_plus && c == '+')
ce83eda1 76 strbuf_addch(out, ' ');
638794cd 77 else
ce83eda1 78 strbuf_addch(out, c);
638794cd
JK
79 q++;
80 } while (1);
81 *query = q;
ce83eda1 82 return strbuf_detach(out, NULL);
638794cd
JK
83}
84
85char *url_decode(const char *url)
86{
ce83eda1 87 struct strbuf out = STRBUF_INIT;
3c73a1d5 88 const char *colon = strchr(url, ':');
ce83eda1
JH
89
90 /* Skip protocol part if present */
3c73a1d5
JH
91 if (colon && url < colon) {
92 strbuf_add(&out, url, colon - url);
93 url = colon;
ce83eda1 94 }
730220de 95 return url_decode_internal(&url, NULL, &out, 0);
638794cd
JK
96}
97
98char *url_decode_parameter_name(const char **query)
99{
ce83eda1 100 struct strbuf out = STRBUF_INIT;
730220de 101 return url_decode_internal(query, "&=", &out, 1);
638794cd
JK
102}
103
104char *url_decode_parameter_value(const char **query)
105{
ce83eda1 106 struct strbuf out = STRBUF_INIT;
730220de 107 return url_decode_internal(query, "&", &out, 1);
638794cd 108}
1966d9f3
TRC
109
110void end_url_with_slash(struct strbuf *buf, const char *url)
111{
112 strbuf_addstr(buf, url);
113 if (buf->len && buf->buf[buf->len - 1] != '/')
114 strbuf_addstr(buf, "/");
115}
3793a309
TRC
116
117void str_end_url_with_slash(const char *url, char **dest) {
118 struct strbuf buf = STRBUF_INIT;
119 end_url_with_slash(&buf, url);
120 free(*dest);
121 *dest = strbuf_detach(&buf, NULL);
122}