]>
Commit | Line | Data |
---|---|---|
638794cd | 1 | #include "cache.h" |
c2e86add | 2 | #include "url.h" |
638794cd JK |
3 | |
4 | int 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 | ||
19 | int is_url(const char *url) | |
20 | { | |
21 | const char *url2, *first_slash; | |
22 | ||
23 | if (!url) | |
24 | return 0; | |
25 | url2 = url; | |
26 | first_slash = strchr(url, '/'); | |
27 | ||
28 | /* Input with no slash at all or slash first can't be URL. */ | |
29 | if (!first_slash || first_slash == url) | |
30 | return 0; | |
31 | /* Character before must be : and next must be /. */ | |
32 | if (first_slash[-1] != ':' || first_slash[1] != '/') | |
33 | return 0; | |
34 | /* There must be something before the :// */ | |
35 | if (first_slash == url + 1) | |
36 | return 0; | |
37 | /* | |
38 | * Check all characters up to first slash - 1. Only alphanum | |
39 | * is allowed. | |
40 | */ | |
41 | url2 = url; | |
42 | while (url2 < first_slash - 1) { | |
43 | if (!is_urlschemechar(url2 == url, (unsigned char)*url2)) | |
44 | return 0; | |
45 | url2++; | |
46 | } | |
47 | ||
48 | /* Valid enough. */ | |
49 | return 1; | |
50 | } | |
51 | ||
52 | static int url_decode_char(const char *q) | |
53 | { | |
54 | int i; | |
55 | unsigned char val = 0; | |
56 | for (i = 0; i < 2; i++) { | |
57 | unsigned char c = *q++; | |
58 | val <<= 4; | |
59 | if (c >= '0' && c <= '9') | |
60 | val += c - '0'; | |
61 | else if (c >= 'a' && c <= 'f') | |
62 | val += c - 'a' + 10; | |
63 | else if (c >= 'A' && c <= 'F') | |
64 | val += c - 'A' + 10; | |
65 | else | |
66 | return -1; | |
67 | } | |
68 | return val; | |
69 | } | |
70 | ||
730220de TR |
71 | static char *url_decode_internal(const char **query, const char *stop_at, |
72 | struct strbuf *out, int decode_plus) | |
638794cd JK |
73 | { |
74 | const char *q = *query; | |
638794cd | 75 | |
638794cd JK |
76 | do { |
77 | unsigned char c = *q; | |
78 | ||
79 | if (!c) | |
80 | break; | |
81 | if (stop_at && strchr(stop_at, c)) { | |
82 | q++; | |
83 | break; | |
84 | } | |
85 | ||
86 | if (c == '%') { | |
87 | int val = url_decode_char(q + 1); | |
88 | if (0 <= val) { | |
ce83eda1 | 89 | strbuf_addch(out, val); |
638794cd JK |
90 | q += 3; |
91 | continue; | |
92 | } | |
93 | } | |
94 | ||
730220de | 95 | if (decode_plus && c == '+') |
ce83eda1 | 96 | strbuf_addch(out, ' '); |
638794cd | 97 | else |
ce83eda1 | 98 | strbuf_addch(out, c); |
638794cd JK |
99 | q++; |
100 | } while (1); | |
101 | *query = q; | |
ce83eda1 | 102 | return strbuf_detach(out, NULL); |
638794cd JK |
103 | } |
104 | ||
105 | char *url_decode(const char *url) | |
106 | { | |
ce83eda1 | 107 | struct strbuf out = STRBUF_INIT; |
3c73a1d5 | 108 | const char *colon = strchr(url, ':'); |
ce83eda1 JH |
109 | |
110 | /* Skip protocol part if present */ | |
3c73a1d5 JH |
111 | if (colon && url < colon) { |
112 | strbuf_add(&out, url, colon - url); | |
113 | url = colon; | |
ce83eda1 | 114 | } |
730220de | 115 | return url_decode_internal(&url, NULL, &out, 0); |
638794cd JK |
116 | } |
117 | ||
118 | char *url_decode_parameter_name(const char **query) | |
119 | { | |
ce83eda1 | 120 | struct strbuf out = STRBUF_INIT; |
730220de | 121 | return url_decode_internal(query, "&=", &out, 1); |
638794cd JK |
122 | } |
123 | ||
124 | char *url_decode_parameter_value(const char **query) | |
125 | { | |
ce83eda1 | 126 | struct strbuf out = STRBUF_INIT; |
730220de | 127 | return url_decode_internal(query, "&", &out, 1); |
638794cd | 128 | } |
1966d9f3 TRC |
129 | |
130 | void end_url_with_slash(struct strbuf *buf, const char *url) | |
131 | { | |
132 | strbuf_addstr(buf, url); | |
133 | if (buf->len && buf->buf[buf->len - 1] != '/') | |
134 | strbuf_addstr(buf, "/"); | |
135 | } | |
3793a309 TRC |
136 | |
137 | void str_end_url_with_slash(const char *url, char **dest) { | |
138 | struct strbuf buf = STRBUF_INIT; | |
139 | end_url_with_slash(&buf, url); | |
140 | free(*dest); | |
141 | *dest = strbuf_detach(&buf, NULL); | |
142 | } |