Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #include "urldata.h" |
27 | | #include "urlapi-int.h" |
28 | | #include "strcase.h" |
29 | | #include "url.h" |
30 | | #include "escape.h" |
31 | | #include "curlx/inet_pton.h" |
32 | | #include "curlx/inet_ntop.h" |
33 | | #include "curlx/strdup.h" |
34 | | #include "idn.h" |
35 | | #include "curlx/strparse.h" |
36 | | #include "curl_memrchr.h" |
37 | | |
38 | | #ifdef _WIN32 |
39 | | /* MS-DOS/Windows style drive prefix, eg c: in c:foo */ |
40 | | #define STARTS_WITH_DRIVE_PREFIX(str) \ |
41 | | ((('a' <= (str)[0] && (str)[0] <= 'z') || \ |
42 | | ('A' <= (str)[0] && (str)[0] <= 'Z')) && \ |
43 | | ((str)[1] == ':')) |
44 | | #endif |
45 | | |
46 | | /* MS-DOS/Windows style drive prefix, optionally with |
47 | | * a '|' instead of ':', followed by a slash or NUL */ |
48 | | #define STARTS_WITH_URL_DRIVE_PREFIX(str) \ |
49 | 0 | ((('a' <= (str)[0] && (str)[0] <= 'z') || \ |
50 | 0 | ('A' <= (str)[0] && (str)[0] <= 'Z')) && \ |
51 | 0 | ((str)[1] == ':' || (str)[1] == '|') && \ |
52 | 0 | ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0)) |
53 | | |
54 | | /* scheme is not URL encoded, the longest libcurl supported ones are... */ |
55 | 54.3k | #define MAX_SCHEME_LEN 40 |
56 | 0 | #define MAX_ZONEID_LEN 16 |
57 | | |
58 | | /* |
59 | | * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make |
60 | | * sure we have _some_ value for AF_INET6 without polluting our fake value |
61 | | * everywhere. |
62 | | */ |
63 | | #if !defined(USE_IPV6) && !defined(AF_INET6) |
64 | | #define AF_INET6 (AF_INET + 1) |
65 | | #endif |
66 | | |
67 | 0 | #define DEFAULT_SCHEME "https" |
68 | | |
69 | | static void free_urlhandle(struct Curl_URL *u) |
70 | 207k | { |
71 | 207k | curlx_free(u->scheme); |
72 | 207k | curlx_free(u->user); |
73 | 207k | curlx_strzero(u->password); |
74 | 207k | curlx_free(u->password); |
75 | 207k | curlx_free(u->options); |
76 | 207k | curlx_free(u->host); |
77 | 207k | curlx_free(u->zoneid); |
78 | 207k | curlx_free(u->path); |
79 | 207k | curlx_free(u->query); |
80 | 207k | curlx_free(u->fragment); |
81 | 207k | } |
82 | | |
83 | | /* |
84 | | * Find the separator at the end of the hostname, or the '?' in cases like |
85 | | * http://www.example.com?id=2380 |
86 | | */ |
87 | | static const char *find_host_sep(const char *url) |
88 | 776 | { |
89 | | /* Find the start of the hostname */ |
90 | 776 | const char *sep = strstr(url, "//"); |
91 | 776 | if(!sep) |
92 | 769 | sep = url; |
93 | 7 | else |
94 | 7 | sep += 2; |
95 | | |
96 | | /* Find first / or ? */ |
97 | 13.7k | while(*sep && *sep != '/' && *sep != '?') |
98 | 12.9k | sep++; |
99 | | |
100 | 776 | return sep; |
101 | 776 | } |
102 | | |
103 | | /* convert CURLcode to CURLUcode */ |
104 | | #define cc2cu(x) \ |
105 | 101 | ((x) == CURLE_TOO_LARGE ? CURLUE_TOO_LARGE : CURLUE_OUT_OF_MEMORY) |
106 | | |
107 | | /* urlencode_str() writes data into an output dynbuf and URL-encodes the |
108 | | * spaces in the source URL accordingly. |
109 | | * |
110 | | * This function re-encodes the string, meaning that it leaves already encoded |
111 | | * bytes as-is and works by encoding only what *has* to be encoded - unless it |
112 | | * has to uppercase the hex to normalize. |
113 | | * |
114 | | * Illegal percent-encoding sequences are left as-is. |
115 | | * |
116 | | * URL encoding should be skipped for hostnames, otherwise IDN resolution |
117 | | * will fail. |
118 | | * |
119 | | * 'query' tells if it is a query part or not, or if it is allowed to |
120 | | * "transition" into a query part with a question mark. |
121 | | * |
122 | | * @unittest 1675 |
123 | | */ |
124 | | UNITTEST CURLUcode urlencode_str(struct dynbuf *o, const char *url, |
125 | | size_t len, bool relative, |
126 | | unsigned int query); |
127 | | UNITTEST CURLUcode urlencode_str(struct dynbuf *o, const char *url, |
128 | | size_t len, bool relative, |
129 | | unsigned int query) |
130 | 100k | { |
131 | | /* we must add this with whitespace-replacing */ |
132 | 100k | const unsigned char *iptr; |
133 | 100k | const unsigned char *host_sep = (const unsigned char *)url; |
134 | 100k | CURLcode result = CURLE_OK; |
135 | | |
136 | 100k | DEBUGASSERT((query >= QUERY_NO) && (query <= QUERY_YES)); |
137 | | |
138 | 100k | if(!relative) { |
139 | 776 | size_t n; |
140 | 776 | host_sep = (const unsigned char *)find_host_sep(url); |
141 | | |
142 | | /* output the first piece as-is */ |
143 | 776 | n = (const char *)host_sep - url; |
144 | 776 | result = curlx_dyn_addn(o, url, n); |
145 | 776 | len -= n; |
146 | 776 | } |
147 | | |
148 | 3.25M | for(iptr = host_sep; len && !result;) { |
149 | 3.15M | if(*iptr == ' ') { |
150 | 722 | if(query != QUERY_YES) |
151 | 702 | result = curlx_dyn_addn(o, "%20", 3); |
152 | 20 | else |
153 | 20 | result = curlx_dyn_addn(o, "+", 1); |
154 | 722 | iptr++; |
155 | 722 | len--; |
156 | 722 | } |
157 | 3.15M | else if((*iptr < ' ') || (*iptr >= 0x7f)) { |
158 | 2.75M | unsigned char out[3] = { '%' }; |
159 | 2.75M | Curl_hexbyte(&out[1], *iptr); |
160 | 2.75M | result = curlx_dyn_addn(o, out, 3); |
161 | 2.75M | iptr++; |
162 | 2.75M | len--; |
163 | 2.75M | } |
164 | 395k | else if(*iptr == '%' && (len >= 3) && |
165 | 143k | ISXDIGIT(iptr[1]) && ISXDIGIT(iptr[2]) && |
166 | 110k | (ISLOWER(iptr[1]) || ISLOWER(iptr[2]))) { |
167 | | /* uppercase it */ |
168 | 108k | unsigned char hex = (unsigned char)((curlx_hexval(iptr[1]) << 4) | |
169 | 108k | curlx_hexval(iptr[2])); |
170 | 108k | unsigned char out[3] = { '%' }; |
171 | 108k | Curl_hexbyte(&out[1], hex); |
172 | 108k | result = curlx_dyn_addn(o, out, 3); |
173 | 108k | iptr += 3; |
174 | 108k | len -= 3; |
175 | 108k | } |
176 | 287k | else { |
177 | 287k | const unsigned char *start = iptr; |
178 | 60.1M | while(len) { |
179 | 60.0M | if(*iptr == ' ' || *iptr < ' ' || *iptr >= 0x7f) |
180 | 111k | break; |
181 | 59.9M | if(*iptr == '%' && (len >= 3) && |
182 | 18.6M | ISXDIGIT(iptr[1]) && ISXDIGIT(iptr[2]) && |
183 | 18.3M | (ISLOWER(iptr[1]) || ISLOWER(iptr[2]))) |
184 | 74.2k | break; |
185 | 59.8M | if(*iptr == '?') { |
186 | 1.92k | if(query == QUERY_NOT_YET) { |
187 | 1.28k | iptr++; |
188 | 1.28k | len--; |
189 | 1.28k | query = QUERY_YES; |
190 | 1.28k | break; |
191 | 1.28k | } |
192 | 1.92k | } |
193 | 59.8M | iptr++; |
194 | 59.8M | len--; |
195 | 59.8M | } |
196 | 287k | result = curlx_dyn_addn(o, (const char *)start, (size_t)(iptr - start)); |
197 | 287k | } |
198 | 3.15M | } |
199 | | |
200 | 100k | if(result) |
201 | 44 | return cc2cu(result); |
202 | 100k | return CURLUE_OK; |
203 | 100k | } |
204 | | |
205 | | /* |
206 | | * Returns the length of the scheme if the given URL is absolute (as opposed |
207 | | * to relative). Stores the scheme in the buffer if TRUE and 'buf' is |
208 | | * non-NULL. The buflen must be larger than MAX_SCHEME_LEN if buf is set. |
209 | | * |
210 | | * If 'guess_scheme' is TRUE, it means the URL might be provided without |
211 | | * scheme. |
212 | | */ |
213 | | size_t Curl_is_absolute_url(const char *url, char *buf, size_t buflen, |
214 | | bool guess_scheme) |
215 | 247k | { |
216 | 247k | size_t i = 0; |
217 | 247k | DEBUGASSERT(!buf || (buflen > MAX_SCHEME_LEN)); |
218 | 247k | (void)buflen; /* only used in debug-builds */ |
219 | 247k | if(buf) |
220 | 114k | buf[0] = 0; /* always leave a defined value in buf */ |
221 | | #ifdef _WIN32 |
222 | | if(guess_scheme && STARTS_WITH_DRIVE_PREFIX(url)) |
223 | | return 0; |
224 | | #endif |
225 | | /* shortcuts for http and https make the performance test run faster but |
226 | | might also reflect reality? */ |
227 | 247k | if((url[0] | 0x20) == 'h' && |
228 | 237k | (url[1] | 0x20) == 't' && |
229 | 237k | (url[2] | 0x20) == 't' && |
230 | 237k | (url[3] | 0x20) == 'p') { |
231 | 237k | if((url[4] == ':') && ((url[5] == '/') || !guess_scheme)) { |
232 | 506 | if(buf) |
233 | 212 | memcpy(buf, "http", 5); |
234 | 506 | return 4; |
235 | 506 | } |
236 | 237k | if(((url[4] | 0x20) == 's') && (url[5] == ':') && |
237 | 231k | ((url[6] == '/') || !guess_scheme)) { |
238 | 231k | if(buf) |
239 | 114k | memcpy(buf, "https", 6); |
240 | 231k | return 5; |
241 | 231k | } |
242 | 237k | } |
243 | 15.7k | if(ISALPHA(url[0])) { |
244 | 10.1k | if(buf) |
245 | 141 | buf[0] = Curl_raw_tolower(url[0]); |
246 | 54.3k | for(i = 1; i < MAX_SCHEME_LEN; ++i) { |
247 | 54.2k | char s = url[i]; |
248 | 54.2k | if(s && (ISALNUM(s) || (s == '+') || (s == '-') || (s == '.'))) { |
249 | 44.1k | if(buf) |
250 | 825 | buf[i] = Curl_raw_tolower(s); |
251 | 44.1k | } |
252 | 10.0k | else { |
253 | 10.0k | break; |
254 | 10.0k | } |
255 | 54.2k | } |
256 | 10.1k | } |
257 | 15.7k | if(i && (url[i] == ':') && ((url[i + 1] == '/') || !guess_scheme)) { |
258 | | /* If this does not guess scheme, the scheme always ends with the colon so |
259 | | that this also detects data: URLs etc. In guessing mode, data: could |
260 | | be the hostname "data" with a specified port number. */ |
261 | | |
262 | | /* the length of the scheme is the name part only */ |
263 | 385 | size_t len = i; |
264 | 385 | if(buf) |
265 | 141 | buf[i] = 0; |
266 | 385 | return len; |
267 | 385 | } |
268 | 15.3k | if(buf) |
269 | 0 | buf[0] = 0; |
270 | 15.3k | return 0; |
271 | 15.7k | } |
272 | | |
273 | | /* scan for byte values <= 31, 127 and maybe space */ |
274 | | static bool badoctets(const char *input, size_t n, int flags) |
275 | 95.8k | { |
276 | 95.8k | const uint8_t *p = (const unsigned char *)input; |
277 | 95.8k | const uint8_t control = (flags & CURLU_ALLOW_SPACE) ? 0x1f : 0x20; |
278 | | /* SWAR (SIMD within a register) 64-bit comparison: |
279 | | Process 8 bytes at a time in 64-bit words to quickly detect bad octets |
280 | | (byte <= control or byte == 127) without byte-by-byte branches. |
281 | | |
282 | | 1. Detect byte <= control: |
283 | | 'sub' contains (control + 1) in each byte (0x20 or 0x21). For any |
284 | | byte < (control + 1), subtracting 'sub' borrows into the high bit |
285 | | (0x80). Masking with (~w) ensures the byte had bit 7 clear initially, |
286 | | so (~w) & (w - sub) has bit 7 set if and only if byte <= control. |
287 | | |
288 | | 2. Detect byte == 127 (0x7f): |
289 | | XORing with 0x7f turns 0x7f bytes into 0x00. We then detect zero bytes |
290 | | in 'x' using the standard zero-byte test: (~x) & (x - 0x0101...). |
291 | | |
292 | | If any high bit is set in (low | del), at least one bad byte was found; |
293 | | break out to the byte-by-byte loop to confirm and return TRUE. */ |
294 | 95.8k | const uint64_t sub = (flags & CURLU_ALLOW_SPACE) ? |
295 | 74.5k | 0x2020202020202020ULL : 0x2121212121212121ULL; |
296 | | |
297 | 4.72M | while(n >= 16) { |
298 | 4.62M | uint64_t w1, w2; |
299 | 4.62M | uint64_t low, x1, x2, del; |
300 | 4.62M | memcpy(&w1, p, sizeof(w1)); |
301 | 4.62M | memcpy(&w2, p + 8, sizeof(w2)); |
302 | | /* check for bytes <= control */ |
303 | 4.62M | low = ((~w1) & (w1 - sub)) | ((~w2) & (w2 - sub)); |
304 | | /* check for 0x7f */ |
305 | 4.62M | x1 = w1 ^ 0x7f7f7f7f7f7f7f7fULL; |
306 | 4.62M | x2 = w2 ^ 0x7f7f7f7f7f7f7f7fULL; |
307 | 4.62M | del = ((~x1) & (x1 - 0x0101010101010101ULL)) | |
308 | 4.62M | ((~x2) & (x2 - 0x0101010101010101ULL)); |
309 | 4.62M | if((low | del) & 0x8080808080808080ULL) |
310 | 325 | break; |
311 | 4.62M | p += 16; |
312 | 4.62M | n -= 16; |
313 | 4.62M | } |
314 | 106k | while(n >= 8) { |
315 | 10.6k | uint64_t w; |
316 | 10.6k | uint64_t low; |
317 | 10.6k | uint64_t x; |
318 | 10.6k | uint64_t del; |
319 | 10.6k | memcpy(&w, p, sizeof(w)); |
320 | | /* check for bytes <= control */ |
321 | 10.6k | low = (~w) & (w - sub); |
322 | | /* check for 0x7f (DEL) */ |
323 | 10.6k | x = w ^ 0x7f7f7f7f7f7f7f7fULL; |
324 | 10.6k | del = (~x) & (x - 0x0101010101010101ULL); |
325 | 10.6k | if((low | del) & 0x8080808080808080ULL) |
326 | 461 | break; |
327 | 10.1k | p += 8; |
328 | 10.1k | n -= 8; |
329 | 10.1k | } |
330 | 319k | while(n--) { |
331 | 224k | if(*p <= control || *p == 127) |
332 | 710 | return TRUE; |
333 | 223k | p++; |
334 | 223k | } |
335 | 95.0k | return FALSE; |
336 | 95.8k | } |
337 | | |
338 | | /* |
339 | | * parse_hostname_login() |
340 | | * |
341 | | * Parse the login details (username, password and options) from the URL and |
342 | | * strip them out of the hostname |
343 | | * |
344 | | * @unittest 1675 |
345 | | */ |
346 | | UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u, |
347 | | const char *login, |
348 | | size_t len, |
349 | | unsigned int flags, |
350 | | size_t *hostname_offset); |
351 | | UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u, |
352 | | const char *login, |
353 | | size_t len, |
354 | | unsigned int flags, |
355 | | size_t *hostname_offset) |
356 | 114k | { |
357 | 114k | CURLUcode ures = CURLUE_OK; |
358 | 114k | CURLcode result; |
359 | 114k | char *userp = NULL; |
360 | 114k | char *passwdp = NULL; |
361 | 114k | char *optionsp = NULL; |
362 | 114k | const struct Curl_scheme *h = NULL; |
363 | | |
364 | | /* At this point, we assume all the other special cases have been taken |
365 | | * care of, so the host is at most |
366 | | * |
367 | | * [user[:password][;options]]@]hostname |
368 | | * |
369 | | * We need somewhere to put the embedded details, so do that first. |
370 | | */ |
371 | 114k | const char *ptr; |
372 | | |
373 | 114k | DEBUGASSERT(login); |
374 | | |
375 | 114k | *hostname_offset = 0; |
376 | 114k | ptr = memchr(login, '@', len); |
377 | 114k | if(!ptr) { |
378 | 109k | if(u->user) |
379 | 0 | curlx_safefree(u->user); |
380 | 109k | if(u->password) { |
381 | 0 | curlx_strzero(u->password); |
382 | 0 | curlx_safefree(u->password); |
383 | 0 | } |
384 | 109k | if(u->options) |
385 | 0 | curlx_safefree(u->options); |
386 | 109k | return CURLUE_OK; |
387 | 109k | } |
388 | | |
389 | | /* We will now try to extract the |
390 | | * possible login information in a string like: |
391 | | * ftp://user:password@ftp.site.example:8021/README */ |
392 | 4.60k | ptr++; |
393 | | |
394 | | /* if this is a known scheme, get some details */ |
395 | 4.60k | if(u->scheme) |
396 | 4.60k | h = Curl_get_scheme(u->scheme); |
397 | | |
398 | | /* We could use the login information in the URL so extract it. Only parse |
399 | | options if the handler says we should. Note that 'h' might be NULL! */ |
400 | 4.60k | result = Curl_parse_login_details(login, ptr - login - 1, |
401 | 4.60k | &userp, &passwdp, |
402 | 4.60k | (h && (h->flags & PROTOPT_URLOPTIONS)) ? |
403 | 4.60k | &optionsp : NULL); |
404 | 4.60k | if(result) { |
405 | | /* the only possible error from Curl_parse_login_details is out of |
406 | | memory: */ |
407 | 6 | ures = CURLUE_OUT_OF_MEMORY; |
408 | 6 | goto out; |
409 | 6 | } |
410 | | |
411 | 4.60k | if(userp) { |
412 | 4.60k | if(flags & CURLU_DISALLOW_USER) { |
413 | | /* Option DISALLOW_USER is set and URL contains username. */ |
414 | 6 | ures = CURLUE_USER_NOT_ALLOWED; |
415 | 6 | goto out; |
416 | 6 | } |
417 | 4.59k | curlx_free(u->user); |
418 | 4.59k | u->user = userp; |
419 | 4.59k | } |
420 | | |
421 | 4.59k | if(passwdp) { |
422 | 79 | curlx_strzero(u->password); |
423 | 79 | curlx_free(u->password); |
424 | 79 | u->password = passwdp; |
425 | 79 | } |
426 | | |
427 | 4.59k | if(optionsp) { |
428 | 0 | curlx_free(u->options); |
429 | 0 | u->options = optionsp; |
430 | 0 | } |
431 | | |
432 | 4.59k | if(userp && badoctets(userp, strlen(userp), flags)) |
433 | 14 | ures = CURLUE_BAD_USER; |
434 | 4.58k | else if(passwdp && badoctets(passwdp, strlen(passwdp), flags)) |
435 | 6 | ures = CURLUE_BAD_PASSWORD; |
436 | 4.57k | else if(optionsp && badoctets(optionsp, strlen(optionsp), flags)) |
437 | 0 | ures = CURLUE_MALFORMED_INPUT; |
438 | | |
439 | 4.59k | userp = passwdp = optionsp = NULL; |
440 | | |
441 | 4.59k | if(!ures) { |
442 | | /* the hostname starts at this offset */ |
443 | 4.57k | *hostname_offset = ptr - login; |
444 | 4.57k | return CURLUE_OK; |
445 | 4.57k | } |
446 | | |
447 | 32 | out: |
448 | | |
449 | 32 | curlx_free(userp); |
450 | 32 | curlx_strzero(passwdp); |
451 | 32 | curlx_free(passwdp); |
452 | 32 | curlx_free(optionsp); |
453 | 32 | curlx_safefree(u->user); |
454 | 32 | curlx_strzero(u->password); |
455 | 32 | curlx_safefree(u->password); |
456 | 32 | curlx_safefree(u->options); |
457 | | |
458 | 32 | return ures; |
459 | 4.59k | } |
460 | | |
461 | | /* @unittest 1653 */ |
462 | | UNITTEST CURLUcode parse_port(struct Curl_URL *u, struct dynbuf *host, |
463 | | bool has_scheme); |
464 | | UNITTEST CURLUcode parse_port(struct Curl_URL *u, struct dynbuf *host, |
465 | | bool has_scheme) |
466 | 114k | { |
467 | 114k | const char *portptr; |
468 | 114k | const char *hostname = curlx_dyn_ptr(host); |
469 | 114k | size_t hlen = curlx_dyn_len(host); |
470 | | /* |
471 | | * Find the end of an IPv6 address on the ']' ending bracket. |
472 | | */ |
473 | 114k | u->portnum = 0; |
474 | 114k | u->port_present = FALSE; |
475 | 114k | if(hostname[0] == '[') { |
476 | 0 | portptr = memchr(hostname + 1, ']', hlen - 1); |
477 | 0 | if(!portptr) |
478 | 0 | return CURLUE_BAD_IPV6; |
479 | 0 | portptr++; |
480 | | /* this is a RFC2732-style specified IP-address */ |
481 | 0 | if(*portptr) { |
482 | 0 | if(*portptr != ':') |
483 | 0 | return CURLUE_BAD_PORT_NUMBER; |
484 | 0 | } |
485 | 0 | else |
486 | 0 | portptr = NULL; |
487 | 0 | } |
488 | 114k | else |
489 | 114k | portptr = memchr(hostname, ':', hlen); |
490 | | |
491 | 114k | if(portptr) { |
492 | 63 | curl_off_t port; |
493 | 63 | size_t keep = portptr - hostname; |
494 | 63 | int rc; |
495 | | |
496 | | /* Browser behavior adaptation. If there is a colon with no digits after, |
497 | | cut off the name there which makes us ignore the colon and use the |
498 | | default port. Firefox, Chrome and Safari all do that. |
499 | | |
500 | | Do not do it if the URL has no scheme, to make something that looks like |
501 | | a scheme not work! */ |
502 | 63 | curlx_dyn_setlen(host, keep); |
503 | 63 | portptr++; |
504 | 63 | if(!*portptr) |
505 | 39 | return has_scheme ? CURLUE_OK : CURLUE_BAD_PORT_NUMBER; |
506 | 24 | if(*portptr == '\\') |
507 | 0 | return CURLUE_BACKSLASH; |
508 | 24 | rc = curlx_str_number(&portptr, &port, 0xffff); |
509 | 24 | if(rc) |
510 | 21 | return CURLUE_BAD_PORT_NUMBER; |
511 | 3 | else if(*portptr == '\\') |
512 | 0 | return CURLUE_BACKSLASH; |
513 | 3 | else if(*portptr) |
514 | 3 | return CURLUE_BAD_PORT_NUMBER; |
515 | | |
516 | 0 | u->portnum = (uint16_t)port; |
517 | 0 | u->port_present = TRUE; |
518 | 0 | } |
519 | | |
520 | 114k | return CURLUE_OK; |
521 | 114k | } |
522 | | |
523 | | /* characters not allowed in hostnames: |
524 | | " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%|" */ |
525 | | |
526 | | static const bool invalid_host_char[256] = { |
527 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00-0x0F */ |
528 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x10-0x1F */ |
529 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, /* 0x20-0x2F */ |
530 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 0x30-0x3F */ |
531 | | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40-0x4F */ |
532 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 0x50-0x5F */ |
533 | | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x6F */ |
534 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1 /* 0x70-0x7F */ |
535 | | }; |
536 | | |
537 | | /* This function assumes 'hostname' starts with [. It writes the normalized |
538 | | * address to 'host' and sets u->zoneid if present. |
539 | | * |
540 | | * @unittest 1675 |
541 | | */ |
542 | | UNITTEST CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname, |
543 | | size_t hlen, struct dynbuf *host); |
544 | | UNITTEST CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname, |
545 | | size_t hlen, struct dynbuf *host) |
546 | 0 | { |
547 | 0 | char *hp; |
548 | 0 | const char *endbracket; |
549 | 0 | const char *zonep = NULL; |
550 | 0 | size_t zonelen = 0; |
551 | |
|
552 | 0 | DEBUGASSERT(*hostname == '['); |
553 | 0 | if((hlen < 4) || (hlen > (MAX_IPADR_LEN + MAX_ZONEID_LEN))) |
554 | 0 | return CURLUE_BAD_IPV6; |
555 | 0 | endbracket = &hostname[hlen - 1]; |
556 | 0 | if(*endbracket != ']') |
557 | 0 | return CURLUE_BAD_IPV6; |
558 | 0 | hostname++; |
559 | 0 | hlen -= 2; |
560 | | |
561 | | /* zone id */ |
562 | 0 | hp = memchr(hostname, '%', hlen); |
563 | 0 | if(hp) { |
564 | 0 | size_t i; |
565 | 0 | zonep = hp + 1; |
566 | 0 | zonelen = (size_t)(endbracket - zonep); |
567 | 0 | if(!zonelen || (zonelen >= MAX_ZONEID_LEN)) |
568 | 0 | return CURLUE_BAD_IPV6; |
569 | 0 | for(i = 0; i < zonelen; i++) { |
570 | 0 | if(invalid_host_char[(unsigned char)zonep[i]]) |
571 | 0 | return CURLUE_BAD_IPV6; |
572 | 0 | } |
573 | 0 | hlen = (size_t)(hp - hostname); |
574 | 0 | } |
575 | | |
576 | 0 | if((hlen < 2) || (hlen >= MAX_IPADR_LEN)) |
577 | 0 | return CURLUE_BAD_IPV6; |
578 | 0 | else { |
579 | | /* Normalize the IPv6 address */ |
580 | 0 | char addr[16]; /* fits a binary IPv6 address */ |
581 | 0 | char dest[MAX_IPADR_LEN + 2]; |
582 | 0 | char *zoneid = NULL; |
583 | 0 | size_t dlen; |
584 | 0 | CURLcode result; |
585 | |
|
586 | 0 | hostname[hlen] = 0; /* end the address there */ |
587 | 0 | if(curlx_inet_pton(AF_INET6, hostname, addr) != 1) |
588 | 0 | return CURLUE_BAD_IPV6; |
589 | | |
590 | 0 | dest[0] = '['; |
591 | 0 | if(curlx_inet_ntop(AF_INET6, addr, &dest[1], MAX_IPADR_LEN)) |
592 | 0 | return CURLUE_BAD_IPV6; |
593 | 0 | dlen = strlen(&dest[1]); |
594 | 0 | dest[1 + dlen] = ']'; |
595 | 0 | dest[2 + dlen] = 0; |
596 | |
|
597 | 0 | if(zonelen) { |
598 | 0 | zoneid = curlx_memdup0(zonep, zonelen); |
599 | 0 | if(!zoneid) |
600 | 0 | return CURLUE_OUT_OF_MEMORY; |
601 | 0 | } |
602 | | |
603 | 0 | curlx_dyn_reset(host); |
604 | 0 | result = curlx_dyn_addn(host, dest, dlen + 2); |
605 | 0 | if(result) { |
606 | 0 | curlx_free(zoneid); |
607 | 0 | return cc2cu(result); |
608 | 0 | } |
609 | | |
610 | 0 | if(zoneid) { |
611 | 0 | curlx_free(u->zoneid); |
612 | 0 | u->zoneid = zoneid; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | return CURLUE_OK; |
616 | 0 | } |
617 | | |
618 | | /* the input is a confirmed hostname, never an IPv6 address */ |
619 | | CURLUcode Curl_hostname_check(const char *hostname, size_t hlen) |
620 | 129k | { |
621 | 129k | const unsigned char *h = (const unsigned char *)hostname; |
622 | 129k | size_t n = hlen; |
623 | 129k | if((hlen >= 2) && |
624 | 128k | (hostname[hlen - 1] == '.') && (hostname[hlen - 2] == '.')) |
625 | | /* more than one trailing dot is not allowed */ |
626 | 4 | return CURLUE_BAD_HOSTNAME; |
627 | 129k | if((hlen == 1) && (hostname[0] == '.')) |
628 | | /* a single dot alone is not allowed */ |
629 | 0 | return CURLUE_BAD_HOSTNAME; |
630 | | |
631 | | /* doing 8-byte and 4-byte checks makes a difference */ |
632 | 249k | while(n >= 8) { |
633 | 120k | if(invalid_host_char[h[0]] | invalid_host_char[h[1]] | |
634 | 120k | invalid_host_char[h[2]] | invalid_host_char[h[3]] | |
635 | 120k | invalid_host_char[h[4]] | invalid_host_char[h[5]] | |
636 | 120k | invalid_host_char[h[6]] | invalid_host_char[h[7]]) |
637 | 89 | return CURLUE_BAD_HOSTNAME; |
638 | 119k | h += 8; |
639 | 119k | n -= 8; |
640 | 119k | } |
641 | 151k | while(n >= 4) { |
642 | 21.2k | if(invalid_host_char[h[0]] | invalid_host_char[h[1]] | |
643 | 21.2k | invalid_host_char[h[2]] | invalid_host_char[h[3]]) |
644 | 8 | return CURLUE_BAD_HOSTNAME; |
645 | 21.2k | h += 4; |
646 | 21.2k | n -= 4; |
647 | 21.2k | } |
648 | 186k | while(n--) { |
649 | 61.4k | if(invalid_host_char[*h++]) |
650 | 4.50k | return CURLUE_BAD_HOSTNAME; |
651 | 61.4k | } |
652 | 125k | return CURLUE_OK; |
653 | 129k | } |
654 | | |
655 | | /* the input is a hostname or perhaps an IPv6 address */ |
656 | | static CURLUcode hostname_check6(struct Curl_URL *u, char *hostname, |
657 | | size_t hlen, struct dynbuf *host) |
658 | 0 | { |
659 | 0 | DEBUGASSERT(hostname); |
660 | |
|
661 | 0 | if(!hlen) |
662 | 0 | return CURLUE_NO_HOST; |
663 | 0 | else if(hostname[0] == '[') |
664 | 0 | return ipv6_parse(u, hostname, hlen, host); |
665 | | |
666 | 0 | return Curl_hostname_check(hostname, hlen); |
667 | 0 | } |
668 | | |
669 | | /* |
670 | | * Handle partial IPv4 numerical addresses and different bases, like |
671 | | * '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc. |
672 | | * |
673 | | * If the given input string is syntactically wrong IPv4 or any part for |
674 | | * example is too big, this function returns HOST_NAME. |
675 | | * |
676 | | * Output the "normalized" version of that input string in plain quad decimal |
677 | | * integers. |
678 | | * |
679 | | * A single dot following the numerical address is accepted and "swallowed" as |
680 | | * if it was never there. |
681 | | * |
682 | | * Returns the host type. |
683 | | * |
684 | | * @unittest 1675 |
685 | | */ |
686 | | UNITTEST int ipv4_normalize(struct dynbuf *host); |
687 | | UNITTEST int ipv4_normalize(struct dynbuf *host) |
688 | 1.54k | { |
689 | 1.54k | bool done = FALSE; |
690 | 1.54k | int n = 0; |
691 | 1.54k | const char *c = curlx_dyn_ptr(host); |
692 | 1.54k | unsigned int parts[4] = { 0, 0, 0, 0 }; |
693 | 1.54k | CURLcode result = CURLE_OK; |
694 | | |
695 | 1.54k | if(!ISDIGIT(*c)) |
696 | 0 | return HOST_NAME; |
697 | | |
698 | 7.12k | while(!done) { |
699 | 5.81k | int rc; |
700 | 5.81k | curl_off_t l; |
701 | 5.81k | if(*c == '0') { |
702 | 1.62k | if((c[1] | 0x20) == 'x') { |
703 | 0 | c += 2; /* skip the prefix */ |
704 | 0 | rc = curlx_str_hex(&c, &l, UINT_MAX); |
705 | 0 | if(rc) |
706 | 0 | return HOST_NAME; |
707 | 0 | } |
708 | 1.62k | else |
709 | 1.62k | rc = curlx_str_octal(&c, &l, UINT_MAX); |
710 | 1.62k | } |
711 | 4.19k | else |
712 | 4.19k | rc = curlx_str_number(&c, &l, UINT_MAX); |
713 | | |
714 | 5.81k | if(rc) { |
715 | 189 | if(!n || (rc != STRE_NO_NUM) || *c) |
716 | 152 | return HOST_NAME; |
717 | 37 | n--; |
718 | 37 | } |
719 | 5.62k | else |
720 | 5.62k | parts[n] = (unsigned int)l; |
721 | | |
722 | 5.66k | switch(*c) { |
723 | 4.89k | case '.': |
724 | 4.89k | if(n == 3) { |
725 | 630 | if(c[1]) |
726 | | /* something follows this dot */ |
727 | 20 | return HOST_NAME; |
728 | 610 | done = TRUE; |
729 | 610 | } |
730 | 4.26k | else { |
731 | 4.26k | n++; |
732 | 4.26k | c++; |
733 | 4.26k | } |
734 | 4.87k | break; |
735 | | |
736 | 4.87k | case '\0': |
737 | 700 | done = TRUE; |
738 | 700 | break; |
739 | | |
740 | 65 | default: |
741 | 65 | return HOST_NAME; |
742 | 5.66k | } |
743 | 5.66k | } |
744 | | |
745 | 1.31k | switch(n) { |
746 | 32 | case 0: /* a -- 32 bits */ |
747 | 32 | curlx_dyn_reset(host); |
748 | | |
749 | 32 | result = curlx_dyn_addf(host, "%u.%u.%u.%u", |
750 | 32 | (parts[0] >> 24), |
751 | 32 | ((parts[0] >> 16) & 0xff), |
752 | 32 | ((parts[0] >> 8) & 0xff), |
753 | 32 | (parts[0] & 0xff)); |
754 | 32 | break; |
755 | 2 | case 1: /* a.b -- 8.24 bits */ |
756 | 2 | if((parts[0] > 0xff) || (parts[1] > 0xffffff)) |
757 | 0 | return HOST_NAME; |
758 | 2 | curlx_dyn_reset(host); |
759 | 2 | result = curlx_dyn_addf(host, "%u.%u.%u.%u", |
760 | 2 | parts[0], |
761 | 2 | ((parts[1] >> 16) & 0xff), |
762 | 2 | ((parts[1] >> 8) & 0xff), |
763 | 2 | (parts[1] & 0xff)); |
764 | 2 | break; |
765 | 37 | case 2: /* a.b.c -- 8.8.16 bits */ |
766 | 37 | if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff)) |
767 | 34 | return HOST_NAME; |
768 | 3 | curlx_dyn_reset(host); |
769 | 3 | result = curlx_dyn_addf(host, "%u.%u.%u.%u", |
770 | 3 | parts[0], |
771 | 3 | parts[1], |
772 | 3 | ((parts[2] >> 8) & 0xff), |
773 | 3 | (parts[2] & 0xff)); |
774 | 3 | break; |
775 | 1.23k | case 3: /* a.b.c.d -- 8.8.8.8 bits */ |
776 | 1.23k | if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) || |
777 | 796 | (parts[3] > 0xff)) |
778 | 443 | return HOST_NAME; |
779 | 796 | curlx_dyn_reset(host); |
780 | 796 | result = curlx_dyn_addf(host, "%u.%u.%u.%u", |
781 | 796 | parts[0], |
782 | 796 | parts[1], |
783 | 796 | parts[2], |
784 | 796 | parts[3]); |
785 | 796 | break; |
786 | 1.31k | } |
787 | 833 | if(result) |
788 | 0 | return HOST_ERROR; |
789 | 833 | return HOST_IPV4; |
790 | 833 | } |
791 | | |
792 | | /* if necessary, replace the host content with a URL decoded version */ |
793 | | static CURLUcode urldecode_host(struct dynbuf *host) |
794 | 114k | { |
795 | 114k | const char *per; |
796 | 114k | size_t hlen = curlx_dyn_len(host); |
797 | 114k | const char *hostname = curlx_dyn_ptr(host); |
798 | 114k | per = memchr(hostname, '%', hlen); |
799 | 114k | if(!per) |
800 | | /* nothing to decode */ |
801 | 114k | return CURLUE_OK; |
802 | 6 | else { |
803 | | /* encoded */ |
804 | 6 | size_t dlen; |
805 | 6 | char *decoded; |
806 | 6 | CURLcode result = Curl_urldecode(hostname, 0, &decoded, &dlen, |
807 | 6 | REJECT_CTRL); |
808 | 6 | if(result) |
809 | 2 | return CURLUE_BAD_HOSTNAME; |
810 | 4 | curlx_dyn_reset(host); |
811 | 4 | result = curlx_dyn_addn(host, decoded, dlen); |
812 | 4 | curlx_free(decoded); |
813 | 4 | if(result) |
814 | 0 | return cc2cu(result); |
815 | 4 | } |
816 | | |
817 | 4 | return CURLUE_OK; |
818 | 114k | } |
819 | | |
820 | | static CURLUcode parse_authority(struct Curl_URL *u, |
821 | | const char *auth, size_t authlen, |
822 | | unsigned int flags, |
823 | | struct dynbuf *host, |
824 | | bool has_scheme) |
825 | 114k | { |
826 | 114k | size_t offset; |
827 | 114k | CURLUcode uc; |
828 | 114k | CURLcode result; |
829 | | |
830 | | /* |
831 | | * Parse the login details and strip them out of the hostname. |
832 | | */ |
833 | 114k | uc = parse_hostname_login(u, auth, authlen, flags, &offset); |
834 | 114k | if(uc) |
835 | 32 | return uc; |
836 | | |
837 | 114k | result = curlx_dyn_addn(host, auth + offset, authlen - offset); |
838 | 114k | if(result) { |
839 | 57 | uc = cc2cu(result); |
840 | 57 | return uc; |
841 | 57 | } |
842 | | |
843 | | /* parse_port() also sets the hostname length correctly */ |
844 | 114k | uc = parse_port(u, host, has_scheme); |
845 | | |
846 | 114k | if(!curlx_dyn_len(host)) |
847 | | /* this makes no-host errors override port number problems */ |
848 | 3 | uc = CURLUE_NO_HOST; |
849 | 114k | if(!uc) |
850 | 114k | uc = urldecode_host(host); |
851 | 114k | if(uc) |
852 | 26 | ; |
853 | 114k | else if(auth[offset] == '[') |
854 | 0 | uc = ipv6_parse(u, curlx_dyn_ptr(host), curlx_dyn_len(host), host); |
855 | 114k | else { |
856 | 114k | const char *hptr = curlx_dyn_ptr(host); |
857 | 114k | size_t hlen = curlx_dyn_len(host); |
858 | | /* ipv4_normalize() returns *NAME, *IPV4 or *ERROR */ |
859 | 114k | int type = ISDIGIT(hptr[0]) ? ipv4_normalize(host) : HOST_NAME; |
860 | | |
861 | 114k | if(type == HOST_NAME) |
862 | 113k | uc = Curl_hostname_check(hptr, hlen); |
863 | 833 | else if(type == HOST_ERROR) |
864 | 0 | uc = CURLUE_OUT_OF_MEMORY; |
865 | 114k | } |
866 | | |
867 | 114k | return uc; |
868 | 114k | } |
869 | | |
870 | | /* used for HTTP/2 server push */ |
871 | | CURLUcode Curl_url_set_authority(CURLU *u, const char *authority) |
872 | 0 | { |
873 | 0 | CURLUcode ures; |
874 | 0 | struct dynbuf host; |
875 | |
|
876 | 0 | DEBUGASSERT(authority); |
877 | 0 | curlx_dyn_init(&host, CURL_MAX_INPUT_LENGTH); |
878 | |
|
879 | 0 | ures = parse_authority(u, authority, strlen(authority), |
880 | 0 | CURLU_DISALLOW_USER, &host, !!u->scheme); |
881 | 0 | if(ures) |
882 | 0 | curlx_dyn_free(&host); |
883 | 0 | else { |
884 | 0 | curlx_free(u->host); |
885 | 0 | u->host = curlx_dyn_ptr(&host); |
886 | 0 | } |
887 | 0 | return ures; |
888 | 0 | } |
889 | | |
890 | | /* |
891 | | * "Remove Dot Segments" |
892 | | * https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4 |
893 | | */ |
894 | | |
895 | | static bool is_dot(const char **str, size_t *clen) |
896 | 8.33M | { |
897 | 8.33M | const char *p = *str; |
898 | 8.33M | if(*p == '.') { |
899 | 203k | (*str)++; |
900 | 203k | (*clen)--; |
901 | 203k | return TRUE; |
902 | 203k | } |
903 | 8.12M | else if((*clen >= 3) && |
904 | 8.12M | (p[0] == '%') && (p[1] == '2') && ((p[2] | 0x20) == 'e')) { |
905 | 500k | *str += 3; |
906 | 500k | *clen -= 3; |
907 | 500k | return TRUE; |
908 | 500k | } |
909 | 7.62M | return FALSE; |
910 | 8.33M | } |
911 | | |
912 | 2.11M | #define ISSLASH(x) ((x) == '/') |
913 | | |
914 | | /* prescan the string to see if it needs work */ |
915 | | static bool needs_dedotdot(const char *p, size_t pn) |
916 | 30.6k | { |
917 | | /* a single byte path cannot be cleaned up */ |
918 | 30.6k | if(pn < 2) |
919 | 0 | return FALSE; |
920 | 30.6k | if(!memchr(p, '.', pn) && !memchr(p, '%', pn)) |
921 | 17.5k | return FALSE; |
922 | 6.56M | while(pn) { |
923 | 21.5M | while(pn && *p != '.' && *p != '%') { |
924 | 14.9M | p++; |
925 | 14.9M | pn--; |
926 | 14.9M | } |
927 | 6.56M | if(!pn) |
928 | 5.77k | break; |
929 | 6.55M | if(is_dot(&p, &pn)) { |
930 | | /* "./" or dot before end of string */ |
931 | 236k | if(!pn || ISSLASH(*p)) |
932 | 6.76k | return TRUE; |
933 | | /* "../" or ".." before end of string */ |
934 | 229k | else if(is_dot(&p, &pn) && (!pn || ISSLASH(*p))) |
935 | 451 | return TRUE; |
936 | 236k | } |
937 | 6.31M | else { |
938 | 6.31M | p++; |
939 | 6.31M | pn--; |
940 | 6.31M | } |
941 | 6.55M | } |
942 | 5.88k | return FALSE; |
943 | 13.1k | } |
944 | | |
945 | | /* |
946 | | * dedotdotify() |
947 | | * |
948 | | * This function gets a null-terminated path with dot and dotdot sequences |
949 | | * passed in and strips them off according to the rules in RFC 3986 section |
950 | | * 5.2.4. |
951 | | * |
952 | | * The function handles a path. It should not contain the query nor fragment. |
953 | | * |
954 | | * RETURNS |
955 | | * |
956 | | * Zero for success and 'out' set to an allocated string (or NULL if there's |
957 | | * nothing to do). |
958 | | * |
959 | | * @unittest 1395 |
960 | | */ |
961 | | UNITTEST int dedotdotify(const char *input, size_t clen, char **outp); |
962 | | UNITTEST int dedotdotify(const char *input, size_t clen, char **outp) |
963 | 30.6k | { |
964 | 30.6k | struct dynbuf out; |
965 | 30.6k | CURLcode result = CURLE_OK; |
966 | | |
967 | | /* variables for leading dot checks */ |
968 | 30.6k | const char *dinput = input; |
969 | 30.6k | size_t dlen = clen; |
970 | | |
971 | 30.6k | *outp = NULL; |
972 | 30.6k | if(!needs_dedotdot(input, clen)) |
973 | 23.3k | return 0; |
974 | | |
975 | 7.21k | curlx_dyn_init(&out, clen + 1); |
976 | | |
977 | | /* if the input buffer begins with a prefix of "../" or "./", then remove |
978 | | that prefix from the input buffer; otherwise, */ |
979 | 7.21k | if(is_dot(&dinput, &dlen)) { |
980 | 0 | if(ISSLASH(*dinput)) { |
981 | | /* one dot followed by a slash */ |
982 | 0 | input = dinput + 1; |
983 | 0 | clen = dlen - 1; |
984 | 0 | } |
985 | | |
986 | | /* if the input buffer consists only of "." or "..", then remove |
987 | | that from the input buffer; otherwise, */ |
988 | 0 | else if(is_dot(&dinput, &dlen)) { |
989 | 0 | if(!dlen) |
990 | | /* .. [end] */ |
991 | 0 | goto end; |
992 | 0 | else if(ISSLASH(*dinput)) { |
993 | | /* ../ */ |
994 | 0 | input = dinput + 1; |
995 | 0 | clen = dlen - 1; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | } |
999 | | |
1000 | 1.22M | while(clen && !result) { /* until end of path content */ |
1001 | 1.21M | const char *next; |
1002 | 1.21M | size_t seglen; |
1003 | 1.21M | if(ISSLASH(*input)) { |
1004 | 1.21M | const char *p = &input[1]; |
1005 | 1.21M | size_t blen = clen - 1; |
1006 | | /* if the input buffer begins with a prefix of "/./" or "/.", where "." |
1007 | | is a complete path segment, then replace that prefix with "/" in the |
1008 | | input buffer; otherwise, */ |
1009 | 1.21M | if(is_dot(&p, &blen)) { |
1010 | 327k | if(!blen) { /* /. */ |
1011 | 78 | result = curlx_dyn_addn(&out, "/", 1); |
1012 | 78 | break; |
1013 | 78 | } |
1014 | 327k | else if(ISSLASH(*p)) { /* /./ */ |
1015 | 935 | input = p; |
1016 | 935 | clen = blen; |
1017 | 935 | continue; |
1018 | 935 | } |
1019 | | |
1020 | | /* if the input buffer begins with a prefix of "/../" or "/..", where |
1021 | | ".." is a complete path segment, then replace that prefix with "/" |
1022 | | in the input buffer and remove the last segment and its preceding |
1023 | | "/" (if any) from the output buffer; otherwise, */ |
1024 | 326k | else if(is_dot(&p, &blen) && (ISSLASH(*p) || !blen)) { |
1025 | | /* remove the last segment from the output buffer */ |
1026 | 35.5k | size_t len = curlx_dyn_len(&out); |
1027 | 35.5k | if(len) { |
1028 | 34.2k | const char *ptr = curlx_dyn_ptr(&out); |
1029 | 34.2k | const char *last = memrchr(ptr, '/', len); |
1030 | 34.2k | if(last) |
1031 | | /* trim the output at the slash */ |
1032 | 34.2k | curlx_dyn_setlen(&out, last - ptr); |
1033 | 34.2k | } |
1034 | | |
1035 | 35.5k | if(blen) { /* /../ */ |
1036 | 35.5k | input = p; |
1037 | 35.5k | clen = blen; |
1038 | 35.5k | continue; |
1039 | 35.5k | } |
1040 | 15 | result = curlx_dyn_addn(&out, "/", 1); |
1041 | 15 | break; |
1042 | 35.5k | } |
1043 | 327k | } |
1044 | 1.21M | } |
1045 | | |
1046 | | /* move the first path segment in the input buffer to the end of the |
1047 | | output buffer, including the initial "/" character (if any) and any |
1048 | | subsequent characters up to, but not including, the next "/" character |
1049 | | or the end of the input buffer. */ |
1050 | | |
1051 | 1.17M | next = memchr(input + 1, '/', clen - 1); |
1052 | 1.17M | seglen = next ? (size_t)(next - input) : clen; |
1053 | 1.17M | result = curlx_dyn_addn(&out, input, seglen); |
1054 | 1.17M | input += seglen; |
1055 | 1.17M | clen -= seglen; |
1056 | 1.17M | } |
1057 | 7.21k | end: |
1058 | 7.21k | if(!result) { |
1059 | 7.21k | if(curlx_dyn_len(&out)) |
1060 | 7.21k | *outp = curlx_dyn_ptr(&out); |
1061 | 0 | else { |
1062 | 0 | *outp = curlx_strdup(""); |
1063 | 0 | if(!*outp) |
1064 | 0 | return 1; |
1065 | 0 | } |
1066 | 7.21k | } |
1067 | 7.21k | return result ? 1 : 0; /* success */ |
1068 | 7.21k | } |
1069 | | |
1070 | | /* |
1071 | | * @unittest 1675 |
1072 | | */ |
1073 | | UNITTEST CURLUcode parse_file(const char *url, size_t urllen, CURLU *u, |
1074 | | const char **pathp, size_t *pathlenp); |
1075 | | UNITTEST CURLUcode parse_file(const char *url, size_t urllen, CURLU *u, |
1076 | | const char **pathp, size_t *pathlenp) |
1077 | 0 | { |
1078 | 0 | const char *path; |
1079 | 0 | size_t pathlen; |
1080 | |
|
1081 | 0 | *pathp = NULL; |
1082 | 0 | *pathlenp = 0; |
1083 | 0 | if(urllen <= 6) |
1084 | | /* file:/ is not enough to actually be a complete file: URL */ |
1085 | 0 | return CURLUE_BAD_FILE_URL; |
1086 | | |
1087 | | /* path has been allocated large enough to hold this */ |
1088 | 0 | path = &url[5]; |
1089 | 0 | pathlen = urllen - 5; |
1090 | | |
1091 | | /* RFC 8089: file-hier-part = ( "//" auth-path ) / local-path, where |
1092 | | local-path also starts with a "/". So reject anything that does not |
1093 | | start with at least one "/" */ |
1094 | 0 | if(path[0] != '/') |
1095 | 0 | return CURLUE_BAD_FILE_URL; |
1096 | | |
1097 | | /* Extra handling URLs with an authority component (i.e. that start with |
1098 | | * "file://") |
1099 | | * |
1100 | | * We allow omitted hostname (e.g. file:/<path>) -- valid according to |
1101 | | * RFC 8089, but not the (current) WHAT-WG URL spec. |
1102 | | */ |
1103 | 0 | if(path[1] == '/') { |
1104 | | /* swallow the two slashes */ |
1105 | 0 | const char *ptr = &path[2]; |
1106 | | |
1107 | | /* |
1108 | | * According to RFC 8089, a file: URL can be reliably dereferenced if: |
1109 | | * |
1110 | | * o it has no/blank hostname, or |
1111 | | * |
1112 | | * o the hostname matches "localhost" (case-insensitively), or |
1113 | | * |
1114 | | * o the hostname is a FQDN that resolves to this machine, or |
1115 | | * |
1116 | | * For brevity, we only consider URLs with empty, "localhost", or |
1117 | | * "127.0.0.1" hostnames as local, otherwise as an UNC String. |
1118 | | * |
1119 | | * Additionally, there is an exception for URLs with a Windows drive |
1120 | | * letter in the authority (which was accidentally omitted from RFC 8089 |
1121 | | * Appendix E, but believe me, it was meant to be there. --MK) |
1122 | | */ |
1123 | 0 | if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) { |
1124 | | /* the URL includes a hostname, it must match "localhost" or |
1125 | | "127.0.0.1" to be valid */ |
1126 | 0 | if(checkprefix("localhost/", ptr) || |
1127 | 0 | checkprefix("127.0.0.1/", ptr)) { |
1128 | 0 | ptr += 9; /* now points to the slash after the host */ |
1129 | 0 | } |
1130 | 0 | else |
1131 | | /* Invalid file://hostname/, expected localhost or 127.0.0.1 or |
1132 | | none */ |
1133 | 0 | return CURLUE_BAD_FILE_URL; |
1134 | 0 | } |
1135 | | |
1136 | 0 | path = ptr; |
1137 | 0 | pathlen = urllen - (ptr - url); |
1138 | 0 | } |
1139 | | |
1140 | 0 | #if !defined(_WIN32) && !defined(MSDOS) && !defined(__CYGWIN__) |
1141 | | /* Do not allow Windows drive letters when not in Windows. |
1142 | | * This catches both "file:/c:" and "file:c:" */ |
1143 | 0 | if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) || |
1144 | 0 | STARTS_WITH_URL_DRIVE_PREFIX(path)) { |
1145 | | /* File drive letters are only accepted in MS-DOS/Windows */ |
1146 | 0 | return CURLUE_BAD_FILE_URL; |
1147 | 0 | } |
1148 | | #else |
1149 | | /* If the path starts with a slash and a drive letter, ditch the slash */ |
1150 | | if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) { |
1151 | | /* This cannot be done with strcpy, as the memory chunks overlap! */ |
1152 | | path++; |
1153 | | pathlen--; |
1154 | | } |
1155 | | #endif |
1156 | 0 | u->scheme = curlx_strdup("file"); |
1157 | 0 | if(!u->scheme) |
1158 | 0 | return CURLUE_OUT_OF_MEMORY; |
1159 | | |
1160 | 0 | *pathp = path; |
1161 | 0 | *pathlenp = pathlen; |
1162 | 0 | return CURLUE_OK; |
1163 | 0 | } |
1164 | | |
1165 | | static CURLUcode parse_scheme(const char *url, CURLU *u, char *schemebuf, |
1166 | | size_t schemelen, unsigned int flags, |
1167 | | const char **hostpp) |
1168 | 114k | { |
1169 | | /* clear path */ |
1170 | 114k | const char *schemep = NULL; |
1171 | | |
1172 | 114k | if(schemelen) { |
1173 | 114k | int num_slashes = 0; |
1174 | 114k | const char *p = &url[schemelen + 1]; |
1175 | 114k | if(!(flags & CURLU_NON_SUPPORT_SCHEME) && |
1176 | 43.2k | !Curl_getn_scheme(schemebuf, schemelen)) |
1177 | 41 | return CURLUE_UNSUPPORTED_SCHEME; |
1178 | | |
1179 | 114k | if(!ISSLASH(*p)) |
1180 | | /* less than one */ |
1181 | 28 | return CURLUE_BAD_SLASHES; |
1182 | 114k | if(p[0] == '/' && p[1] == '/' && p[2] != '/') { |
1183 | 114k | p += 2; |
1184 | 114k | } |
1185 | 224 | else if((flags & CURLU_NO_AUTHORITY)) { |
1186 | 0 | while(ISSLASH(*p) && (num_slashes < 2)) { |
1187 | 0 | p++; |
1188 | 0 | num_slashes++; |
1189 | 0 | } |
1190 | 0 | } |
1191 | 224 | else { |
1192 | 457 | while(ISSLASH(*p) && (num_slashes < 4)) { |
1193 | 233 | p++; |
1194 | 233 | num_slashes++; |
1195 | 233 | } |
1196 | 224 | if(num_slashes > 3) |
1197 | 3 | return CURLUE_BAD_SLASHES; |
1198 | 224 | } |
1199 | | |
1200 | 114k | schemep = schemebuf; |
1201 | 114k | *hostpp = p; /* hostname starts here */ |
1202 | 114k | } |
1203 | 0 | else { |
1204 | | /* no scheme! */ |
1205 | |
|
1206 | 0 | if(!(flags & (CURLU_DEFAULT_SCHEME | CURLU_GUESS_SCHEME))) |
1207 | 0 | return CURLUE_BAD_SCHEME; |
1208 | | |
1209 | 0 | if(flags & CURLU_DEFAULT_SCHEME) |
1210 | 0 | schemep = DEFAULT_SCHEME; |
1211 | | |
1212 | | /* |
1213 | | * The URL was badly formatted, let's try without scheme specified. |
1214 | | */ |
1215 | 0 | *hostpp = url; |
1216 | 0 | } |
1217 | | |
1218 | 114k | if(schemep) { |
1219 | 114k | u->scheme = curlx_strdup(schemep); |
1220 | 114k | if(!u->scheme) |
1221 | 76 | return CURLUE_OUT_OF_MEMORY; |
1222 | 114k | } |
1223 | 114k | return CURLUE_OK; |
1224 | 114k | } |
1225 | | |
1226 | | static CURLUcode guess_scheme(CURLU *u, struct dynbuf *host) |
1227 | 0 | { |
1228 | 0 | const char *hostname = curlx_dyn_ptr(host); |
1229 | 0 | const char *schemep = NULL; |
1230 | | /* legacy curl-style guess based on hostname */ |
1231 | 0 | if(checkprefix("ftp.", hostname)) |
1232 | 0 | schemep = "ftp"; |
1233 | 0 | else if(checkprefix("dict.", hostname)) |
1234 | 0 | schemep = "dict"; |
1235 | 0 | else if(checkprefix("ldap.", hostname)) |
1236 | 0 | schemep = "ldap"; |
1237 | 0 | else if(checkprefix("imap.", hostname)) |
1238 | 0 | schemep = "imap"; |
1239 | 0 | else if(checkprefix("smtp.", hostname)) |
1240 | 0 | schemep = "smtp"; |
1241 | 0 | else if(checkprefix("pop3.", hostname)) |
1242 | 0 | schemep = "pop3"; |
1243 | 0 | else |
1244 | 0 | schemep = "http"; |
1245 | |
|
1246 | 0 | u->scheme = curlx_strdup(schemep); |
1247 | 0 | if(!u->scheme) |
1248 | 0 | return CURLUE_OUT_OF_MEMORY; |
1249 | | |
1250 | 0 | u->guessed_scheme = TRUE; |
1251 | 0 | return CURLUE_OK; |
1252 | 0 | } |
1253 | | |
1254 | | static CURLUcode handle_fragment(CURLU *u, const char *fragment, |
1255 | | size_t fraglen, unsigned int flags, |
1256 | | const bool badoctets_clean) |
1257 | 1.06k | { |
1258 | 1.06k | CURLUcode ures; |
1259 | 1.06k | u->fragment_present = TRUE; |
1260 | 1.06k | if(fraglen > 1) { |
1261 | | /* skip the leading '#' in the copy but include the null-terminator */ |
1262 | 803 | if(flags & CURLU_URLENCODE) { |
1263 | 335 | struct dynbuf enc; |
1264 | 335 | curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH); |
1265 | 335 | ures = urlencode_str(&enc, fragment + 1, fraglen - 1, TRUE, QUERY_NO); |
1266 | 335 | if(ures) |
1267 | 0 | return ures; |
1268 | 335 | u->fragment = curlx_dyn_ptr(&enc); |
1269 | 335 | } |
1270 | 468 | else { |
1271 | 468 | if(!badoctets_clean && badoctets(fragment, fraglen, flags)) |
1272 | 84 | return CURLUE_BAD_FRAGMENT; |
1273 | 384 | u->fragment = curlx_memdup0(fragment + 1, fraglen - 1); |
1274 | 384 | if(!u->fragment) |
1275 | 2 | return CURLUE_OUT_OF_MEMORY; |
1276 | 384 | } |
1277 | 803 | } |
1278 | 978 | return CURLUE_OK; |
1279 | 1.06k | } |
1280 | | |
1281 | | static CURLUcode handle_query(CURLU *u, const char *query, |
1282 | | size_t qlen, unsigned int flags, |
1283 | | const bool badoctets_clean) |
1284 | 10.9k | { |
1285 | 10.9k | u->query_present = TRUE; |
1286 | 10.9k | if(qlen > 1) { |
1287 | 10.5k | if(flags & CURLU_URLENCODE) { |
1288 | 5.77k | struct dynbuf enc; |
1289 | 5.77k | CURLUcode ures; |
1290 | 5.77k | curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH); |
1291 | | /* skip the leading question mark */ |
1292 | 5.77k | ures = urlencode_str(&enc, query + 1, qlen - 1, TRUE, QUERY_YES); |
1293 | 5.77k | if(ures) |
1294 | 5 | return ures; |
1295 | 5.77k | u->query = curlx_dyn_ptr(&enc); |
1296 | 5.77k | } |
1297 | 4.78k | else { |
1298 | 4.78k | if(!badoctets_clean && badoctets(query, qlen, flags)) |
1299 | 66 | return CURLUE_BAD_QUERY; |
1300 | | |
1301 | 4.71k | u->query = curlx_memdup0(query + 1, qlen - 1); |
1302 | 4.71k | if(!u->query) |
1303 | 3 | return CURLUE_OUT_OF_MEMORY; |
1304 | 4.71k | } |
1305 | 10.5k | } |
1306 | 407 | else { |
1307 | | /* single byte query */ |
1308 | 407 | u->query = curlx_strdup(""); |
1309 | 407 | if(!u->query) |
1310 | 4 | return CURLUE_OUT_OF_MEMORY; |
1311 | 407 | } |
1312 | 10.8k | return CURLUE_OK; |
1313 | 10.9k | } |
1314 | | |
1315 | | static CURLUcode handle_path(CURLU *u, const char *path, |
1316 | | size_t pathlen, unsigned int flags, |
1317 | | bool is_file, |
1318 | | const bool badoctets_clean) |
1319 | 114k | { |
1320 | 114k | CURLUcode ures; |
1321 | 114k | if(pathlen && (flags & CURLU_URLENCODE)) { |
1322 | 37.4k | struct dynbuf enc; |
1323 | 37.4k | curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH); |
1324 | 37.4k | ures = urlencode_str(&enc, path, pathlen, TRUE, QUERY_NO); |
1325 | 37.4k | if(ures) |
1326 | 20 | return ures; |
1327 | 37.4k | pathlen = curlx_dyn_len(&enc); |
1328 | 37.4k | path = u->path = curlx_dyn_ptr(&enc); |
1329 | 37.4k | } |
1330 | | |
1331 | 114k | if(pathlen >= (size_t)(1 + !is_file)) { |
1332 | 31.1k | if(!badoctets_clean && badoctets(path, pathlen, flags)) |
1333 | 195 | return CURLUE_BAD_PATH; |
1334 | | |
1335 | | /* paths for file:// scheme can be one byte, others need to be two */ |
1336 | 30.9k | if(!u->path) { |
1337 | 14.9k | u->path = curlx_memdup0(path, pathlen); |
1338 | 14.9k | if(!u->path) |
1339 | 9 | return CURLUE_OUT_OF_MEMORY; |
1340 | 14.9k | path = u->path; |
1341 | 14.9k | } |
1342 | 16.0k | else if(flags & CURLU_URLENCODE) |
1343 | | /* it might have encoded more than the path so cut it */ |
1344 | 16.0k | u->path[pathlen] = 0; |
1345 | | |
1346 | 30.9k | if(!(flags & CURLU_PATH_AS_IS)) { |
1347 | | /* remove ../ and ./ sequences according to RFC3986 */ |
1348 | 30.6k | char *dedot; |
1349 | 30.6k | int err = dedotdotify(path, pathlen, &dedot); |
1350 | 30.6k | if(err) |
1351 | 7 | return CURLUE_OUT_OF_MEMORY; |
1352 | 30.5k | if(dedot) { |
1353 | 7.21k | curlx_free(u->path); |
1354 | 7.21k | u->path = dedot; |
1355 | 7.21k | } |
1356 | 30.5k | } |
1357 | 30.9k | } |
1358 | 113k | return CURLUE_OK; |
1359 | 114k | } |
1360 | | |
1361 | | static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags) |
1362 | 114k | { |
1363 | 114k | const char *path; |
1364 | 114k | size_t pathlen; |
1365 | 114k | char schemebuf[MAX_SCHEME_LEN + 1]; |
1366 | 114k | size_t schemelen; |
1367 | 114k | size_t urllen; |
1368 | 114k | CURLUcode ures = CURLUE_OK; |
1369 | 114k | struct dynbuf host; |
1370 | 114k | bool is_file = FALSE; |
1371 | 114k | bool badoctets_clean = FALSE; |
1372 | | |
1373 | 114k | DEBUGASSERT(url); |
1374 | | |
1375 | 114k | urllen = strlen(url); |
1376 | 114k | if(urllen > CURL_MAX_INPUT_LENGTH) |
1377 | 0 | return CURLUE_MALFORMED_INPUT; |
1378 | | |
1379 | 114k | curlx_dyn_init(&host, CURL_MAX_INPUT_LENGTH); |
1380 | | |
1381 | 114k | schemelen = Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf), |
1382 | 114k | flags & (CURLU_GUESS_SCHEME | |
1383 | 114k | CURLU_DEFAULT_SCHEME)); |
1384 | | |
1385 | | /* handle the file: scheme */ |
1386 | 114k | if(schemelen == 4 && schemebuf[0] == 'f' && !memcmp(schemebuf, "file", 4)) { |
1387 | 0 | is_file = TRUE; |
1388 | 0 | ures = parse_file(url, urllen, u, &path, &pathlen); |
1389 | 0 | } |
1390 | 114k | else { |
1391 | 114k | const char *hostp = NULL; |
1392 | 114k | size_t hostlen; |
1393 | 114k | ures = parse_scheme(url, u, schemebuf, schemelen, flags, &hostp); |
1394 | 114k | if(ures) |
1395 | 148 | goto fail; |
1396 | | |
1397 | | /* find the end of the hostname + port number */ |
1398 | 114k | hostlen = strcspn(hostp, "/?#"); |
1399 | 114k | path = hostp + hostlen; |
1400 | | |
1401 | | /* this pathlen also contains the query and the fragment */ |
1402 | 114k | pathlen = urllen - (path - url); |
1403 | 114k | if(hostlen) { |
1404 | 114k | ures = parse_authority(u, hostp, hostlen, flags, &host, !!u->scheme); |
1405 | 114k | if(!ures && (flags & CURLU_GUESS_SCHEME) && !u->scheme) |
1406 | 0 | ures = guess_scheme(u, &host); |
1407 | 114k | } |
1408 | 5 | else if(flags & CURLU_NO_AUTHORITY) { |
1409 | | /* allowed to be empty. */ |
1410 | 0 | if(curlx_dyn_add(&host, "")) |
1411 | 0 | ures = CURLUE_OUT_OF_MEMORY; |
1412 | 0 | } |
1413 | 5 | else |
1414 | 5 | ures = CURLUE_NO_HOST; |
1415 | 114k | } |
1416 | 114k | if(!ures) { |
1417 | 114k | const char *fragment; |
1418 | 114k | if(pathlen && !(flags & CURLU_URLENCODE) && |
1419 | 74.7k | !badoctets(path, pathlen, flags)) |
1420 | | /* there is a bad octet in there, but we do not know in which component |
1421 | | so we just set the boolean for now */ |
1422 | 74.4k | badoctets_clean = TRUE; |
1423 | | |
1424 | | /* The path might at this point contain a fragment and/or a query to |
1425 | | handle */ |
1426 | 114k | fragment = memchr(path, '#', pathlen); |
1427 | 114k | if(fragment) { |
1428 | 1.06k | size_t fraglen = pathlen - (fragment - path); |
1429 | 1.06k | ures = handle_fragment(u, fragment, fraglen, flags, badoctets_clean); |
1430 | | /* after this, pathlen still contains the query */ |
1431 | 1.06k | pathlen -= fraglen; |
1432 | 1.06k | } |
1433 | 114k | } |
1434 | 114k | if(!ures) { |
1435 | 114k | const char *query = memchr(path, '?', pathlen); |
1436 | 114k | if(query) { |
1437 | 10.9k | size_t qlen = pathlen - (query - path); |
1438 | 10.9k | ures = handle_query(u, query, qlen, flags, badoctets_clean); |
1439 | 10.9k | pathlen -= qlen; |
1440 | 10.9k | } |
1441 | 114k | } |
1442 | 114k | if(!ures) |
1443 | | /* the fragment and query parts are trimmed off from the path */ |
1444 | 114k | ures = handle_path(u, path, pathlen, flags, is_file, badoctets_clean); |
1445 | 114k | if(!ures) { |
1446 | 113k | u->host = curlx_dyn_ptr(&host); |
1447 | 113k | return CURLUE_OK; |
1448 | 113k | } |
1449 | 688 | fail: |
1450 | 688 | curlx_dyn_free(&host); |
1451 | 688 | free_urlhandle(u); |
1452 | 688 | return ures; |
1453 | 114k | } |
1454 | | |
1455 | | /* |
1456 | | * Parse the URL and, if successful, replace everything in the Curl_URL struct. |
1457 | | */ |
1458 | | static CURLUcode parseurl_and_replace(const char *url, CURLU *u, |
1459 | | unsigned int flags) |
1460 | 114k | { |
1461 | 114k | CURLUcode ures; |
1462 | 114k | CURLU tmpurl; |
1463 | 114k | memset(&tmpurl, 0, sizeof(tmpurl)); |
1464 | 114k | ures = parseurl(url, &tmpurl, flags); |
1465 | 114k | if(!ures) { |
1466 | 113k | free_urlhandle(u); |
1467 | 113k | *u = tmpurl; |
1468 | 113k | } |
1469 | 114k | return ures; |
1470 | 114k | } |
1471 | | |
1472 | | /* |
1473 | | * Concatenate a relative URL onto a base URL making it absolute. |
1474 | | */ |
1475 | | static CURLUcode redirect_url(const char *base, const char *relurl, |
1476 | | CURLU *u, unsigned int flags) |
1477 | 7.59k | { |
1478 | 7.59k | struct dynbuf urlbuf; |
1479 | 7.59k | bool host_changed = FALSE; |
1480 | 7.59k | const char *useurl = relurl; |
1481 | 7.59k | const char *cutoff = NULL; |
1482 | 7.59k | size_t prelen; |
1483 | 7.59k | CURLUcode uc; |
1484 | | /* this can get here with a NULL u->scheme only if asked to use the default |
1485 | | scheme, so allow fallback to that */ |
1486 | 7.59k | const char *scheme = u->scheme ? u->scheme : DEFAULT_SCHEME; |
1487 | | |
1488 | | /* protsep points to the start of the hostname, after [scheme]:// */ |
1489 | 7.59k | const char *protsep = base + strlen(scheme) + 3; |
1490 | 7.59k | DEBUGASSERT(base && relurl && u); /* all set here */ |
1491 | 7.59k | if(!base) |
1492 | 0 | return CURLUE_MALFORMED_INPUT; /* should never happen */ |
1493 | | |
1494 | | /* handle different relative URL types */ |
1495 | 7.59k | switch(relurl[0]) { |
1496 | 1.18k | case '/': |
1497 | 1.18k | if(relurl[1] == '/') { |
1498 | | /* protocol-relative URL: //example.com/path */ |
1499 | 779 | cutoff = protsep; |
1500 | 779 | useurl = &relurl[2]; |
1501 | 779 | host_changed = TRUE; |
1502 | 779 | } |
1503 | 406 | else |
1504 | | /* absolute /path */ |
1505 | 406 | cutoff = strchr(protsep, '/'); |
1506 | 1.18k | break; |
1507 | | |
1508 | 25 | case '#': |
1509 | | /* fragment-only change */ |
1510 | 25 | if(u->fragment_present) |
1511 | 20 | cutoff = strchr(protsep, '#'); |
1512 | 25 | break; |
1513 | | |
1514 | 6.38k | default: |
1515 | | /* path or query-only change */ |
1516 | 6.38k | if(u->query_present) |
1517 | | /* remove existing query */ |
1518 | 649 | cutoff = strchr(protsep, '?'); |
1519 | 5.74k | else if(u->fragment_present) |
1520 | | /* Remove existing fragment */ |
1521 | 24 | cutoff = strchr(protsep, '#'); |
1522 | | |
1523 | 6.38k | if(relurl[0] != '?') { |
1524 | | /* append a relative path after the last slash */ |
1525 | 5.11k | cutoff = memrchr(protsep, '/', |
1526 | 5.11k | cutoff ? (size_t)(cutoff - protsep) : strlen(protsep)); |
1527 | 5.11k | if(cutoff) |
1528 | 5.11k | cutoff++; /* truncate after last slash */ |
1529 | 5.11k | } |
1530 | 6.38k | break; |
1531 | 7.59k | } |
1532 | | |
1533 | 7.59k | prelen = cutoff ? (size_t)(cutoff - base) : strlen(base); |
1534 | | |
1535 | | /* build new URL */ |
1536 | 7.59k | curlx_dyn_init(&urlbuf, CURL_MAX_INPUT_LENGTH); |
1537 | | |
1538 | 7.59k | if(!curlx_dyn_addn(&urlbuf, base, prelen) && |
1539 | 7.59k | !urlencode_str(&urlbuf, useurl, strlen(useurl), !host_changed, |
1540 | 7.59k | QUERY_NOT_YET)) { |
1541 | 7.58k | uc = parseurl_and_replace(curlx_dyn_ptr(&urlbuf), u, |
1542 | 7.58k | flags & ~U_CURLU_PATH_AS_IS); |
1543 | 7.58k | } |
1544 | 12 | else |
1545 | 12 | uc = CURLUE_OUT_OF_MEMORY; |
1546 | | |
1547 | 7.59k | curlx_dyn_free(&urlbuf); |
1548 | 7.59k | return uc; |
1549 | 7.59k | } |
1550 | | |
1551 | | /* |
1552 | | */ |
1553 | | CURLU *curl_url(void) |
1554 | 93.1k | { |
1555 | 93.1k | return curlx_calloc(1, sizeof(struct Curl_URL)); |
1556 | 93.1k | } |
1557 | | |
1558 | | void curl_url_cleanup(CURLU *u) |
1559 | 167k | { |
1560 | 167k | if(u) { |
1561 | 93.0k | free_urlhandle(u); |
1562 | 93.0k | curlx_free(u); |
1563 | 93.0k | } |
1564 | 167k | } |
1565 | | |
1566 | | #define DUP(dest, src, name) \ |
1567 | 0 | do { \ |
1568 | 0 | if((src)->name) { \ |
1569 | 0 | (dest)->name = curlx_strdup((src)->name); \ |
1570 | 0 | if(!(dest)->name) \ |
1571 | 0 | goto fail; \ |
1572 | 0 | } \ |
1573 | 0 | } while(0) |
1574 | | |
1575 | | CURLU *curl_url_dup(const CURLU *in) |
1576 | 0 | { |
1577 | 0 | struct Curl_URL *u; |
1578 | |
|
1579 | 0 | if(!in) |
1580 | 0 | return NULL; |
1581 | | |
1582 | 0 | u = curlx_calloc(1, sizeof(struct Curl_URL)); |
1583 | 0 | if(u) { |
1584 | 0 | DUP(u, in, scheme); |
1585 | 0 | DUP(u, in, user); |
1586 | 0 | DUP(u, in, password); |
1587 | 0 | DUP(u, in, options); |
1588 | 0 | DUP(u, in, host); |
1589 | 0 | DUP(u, in, path); |
1590 | 0 | DUP(u, in, query); |
1591 | 0 | DUP(u, in, fragment); |
1592 | 0 | DUP(u, in, zoneid); |
1593 | 0 | u->portnum = in->portnum; |
1594 | 0 | u->port_present = in->port_present; |
1595 | 0 | u->fragment_present = in->fragment_present; |
1596 | 0 | u->query_present = in->query_present; |
1597 | 0 | u->guessed_scheme = in->guessed_scheme; |
1598 | 0 | } |
1599 | 0 | return u; |
1600 | 0 | fail: |
1601 | 0 | curl_url_cleanup(u); |
1602 | 0 | return NULL; |
1603 | 0 | } |
1604 | | |
1605 | | #ifndef USE_IDN |
1606 | | #define host_decode(x, y) CURLUE_LACKS_IDN |
1607 | | #define host_encode(x, y) CURLUE_LACKS_IDN |
1608 | | #else |
1609 | | static CURLUcode host_decode(const char *host, char **allochost) |
1610 | 0 | { |
1611 | 0 | CURLcode result = Curl_idn_decode(host, allochost); |
1612 | 0 | if(result) |
1613 | 0 | return (result == CURLE_OUT_OF_MEMORY) ? |
1614 | 0 | CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME; |
1615 | 0 | return CURLUE_OK; |
1616 | 0 | } |
1617 | | |
1618 | | static CURLUcode host_encode(const char *host, char **allochost) |
1619 | 0 | { |
1620 | 0 | CURLcode result = Curl_idn_encode(host, allochost); |
1621 | 0 | if(result) |
1622 | 0 | return (result == CURLE_OUT_OF_MEMORY) ? |
1623 | 0 | CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME; |
1624 | 0 | return CURLUE_OK; |
1625 | 0 | } |
1626 | | #endif |
1627 | | |
1628 | | static CURLUcode urlget_format(const CURLU *u, CURLUPart what, |
1629 | | const char *ptr, char **partp, |
1630 | | bool plusdecode, unsigned int flags) |
1631 | 154k | { |
1632 | 154k | CURLUcode uc = CURLUE_OK; |
1633 | 154k | size_t partlen = strlen(ptr); |
1634 | 154k | bool urldecode = (flags & CURLU_URLDECODE) ? 1 : 0; |
1635 | 154k | bool urlencode = (flags & CURLU_URLENCODE) ? 1 : 0; |
1636 | 154k | bool punycode = (flags & CURLU_PUNYCODE) && (what == CURLUPART_HOST); |
1637 | 154k | bool depunyfy = (flags & CURLU_PUNY2IDN) && (what == CURLUPART_HOST); |
1638 | 154k | char *part = curlx_memdup0(ptr, partlen); |
1639 | 154k | *partp = NULL; |
1640 | 154k | if(!part) |
1641 | 40 | return CURLUE_OUT_OF_MEMORY; |
1642 | 154k | if(plusdecode) { |
1643 | | /* convert + to space */ |
1644 | 0 | char *plus = part; |
1645 | 0 | size_t i = 0; |
1646 | 0 | for(i = 0; i < partlen; ++plus, i++) { |
1647 | 0 | if(*plus == '+') |
1648 | 0 | *plus = ' '; |
1649 | 0 | } |
1650 | 0 | } |
1651 | 154k | if(urldecode) { |
1652 | 0 | char *decoded; |
1653 | 0 | size_t dlen; |
1654 | | /* this unconditional rejection of control bytes is documented API |
1655 | | behavior */ |
1656 | 0 | CURLcode result = Curl_urldecode(part, partlen, &decoded, &dlen, |
1657 | 0 | REJECT_CTRL); |
1658 | 0 | curlx_free(part); |
1659 | 0 | if(result) |
1660 | 0 | return CURLUE_URLDECODE; |
1661 | 0 | part = decoded; |
1662 | 0 | partlen = dlen; |
1663 | 0 | } |
1664 | 154k | if(urlencode && partlen) { |
1665 | 49.6k | struct dynbuf enc; |
1666 | 49.6k | curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH); |
1667 | 49.6k | uc = urlencode_str(&enc, part, partlen, TRUE, what == CURLUPART_QUERY ? |
1668 | 49.6k | QUERY_YES : QUERY_NO); |
1669 | 49.6k | curlx_free(part); |
1670 | 49.6k | if(uc) |
1671 | 16 | return uc; |
1672 | 49.6k | part = curlx_dyn_ptr(&enc); |
1673 | 49.6k | } |
1674 | 104k | else if(punycode) { |
1675 | 0 | if(!Curl_is_ASCII_name(u->host)) { |
1676 | 0 | char *punyversion = NULL; |
1677 | 0 | uc = host_decode(part, &punyversion); |
1678 | 0 | curlx_free(part); |
1679 | 0 | if(uc) |
1680 | 0 | return uc; |
1681 | 0 | uc = Curl_hostname_check(punyversion, strlen(punyversion)); |
1682 | 0 | if(uc) { |
1683 | 0 | curlx_free(punyversion); |
1684 | 0 | return uc; |
1685 | 0 | } |
1686 | 0 | part = punyversion; |
1687 | 0 | } |
1688 | 0 | } |
1689 | 104k | else if(depunyfy && Curl_is_ASCII_name(u->host)) { |
1690 | 0 | char *unpunified = NULL; |
1691 | 0 | uc = host_encode(part, &unpunified); |
1692 | 0 | curlx_free(part); |
1693 | 0 | if(uc) |
1694 | 0 | return uc; |
1695 | 0 | part = unpunified; |
1696 | 0 | } |
1697 | 154k | *partp = part; |
1698 | 154k | return CURLUE_OK; |
1699 | 154k | } |
1700 | | |
1701 | | static CURLUcode file_url(const CURLU *u, char **part, |
1702 | | const char *fragmentsep, |
1703 | | const char *querysep) |
1704 | 0 | { |
1705 | 0 | char *url = curl_maprintf("file://%s%s%s%s%s", |
1706 | 0 | u->path ? u->path : "/", |
1707 | 0 | querysep, u->query ? u->query : "", |
1708 | 0 | fragmentsep, u->fragment ? u->fragment : ""); |
1709 | 0 | if(!url) |
1710 | 0 | return CURLUE_OUT_OF_MEMORY; |
1711 | | |
1712 | 0 | *part = url; |
1713 | 0 | return CURLUE_OK; |
1714 | 0 | } |
1715 | | |
1716 | | static CURLUcode urlget_url(const CURLU *u, char **part, unsigned int flags) |
1717 | 100k | { |
1718 | 100k | char *url; |
1719 | 100k | char *allochost = NULL; |
1720 | 100k | const char *fragmentsep = |
1721 | 100k | (u->fragment || (u->fragment_present && flags & CURLU_GET_EMPTY)) ? |
1722 | 99.3k | "#" : ""; |
1723 | 100k | const char *querysep = ((u->query && u->query[0]) || |
1724 | 91.6k | (u->query_present && flags & CURLU_GET_EMPTY)) ? |
1725 | 91.3k | "?" : ""; |
1726 | 100k | char portbuf[7]; |
1727 | 100k | if(curl_strequal("file", u->scheme)) |
1728 | 0 | return file_url(u, part, fragmentsep, querysep); |
1729 | 100k | else if(!u->host) |
1730 | 0 | return CURLUE_NO_HOST; |
1731 | 100k | else { |
1732 | 100k | const char *scheme; |
1733 | 100k | char *options = u->options; |
1734 | 100k | char *port = NULL; |
1735 | 100k | const struct Curl_scheme *h = NULL; |
1736 | 100k | char schemebuf[MAX_SCHEME_LEN + 5]; |
1737 | 100k | if(u->scheme) |
1738 | 100k | scheme = u->scheme; |
1739 | 0 | else if(flags & CURLU_DEFAULT_SCHEME) |
1740 | 0 | scheme = DEFAULT_SCHEME; |
1741 | 0 | else |
1742 | 0 | return CURLUE_NO_SCHEME; |
1743 | | |
1744 | 100k | if(u->port_present) { |
1745 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%u", u->portnum); |
1746 | 0 | port = portbuf; |
1747 | 0 | } |
1748 | | |
1749 | 100k | h = Curl_get_scheme(scheme); |
1750 | 100k | if(h) { |
1751 | 100k | if(!u->port_present && (flags & CURLU_DEFAULT_PORT)) { |
1752 | | /* there is no stored port number, but asked to deliver a default one |
1753 | | for the scheme */ |
1754 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%u", h->defport); |
1755 | 0 | port = portbuf; |
1756 | 0 | } |
1757 | 100k | else if(u->port_present && (h->defport == u->portnum) && |
1758 | 0 | (flags & CURLU_NO_DEFAULT_PORT)) { |
1759 | | /* there is a stored port number, but asked to inhibit if it matches |
1760 | | the default port for the scheme */ |
1761 | 0 | port = NULL; |
1762 | 0 | } |
1763 | | |
1764 | 100k | if(!(h->flags & PROTOPT_URLOPTIONS)) |
1765 | 100k | options = NULL; |
1766 | 100k | } |
1767 | | |
1768 | 100k | if(u->host[0] == '[') { |
1769 | 0 | if(u->zoneid) { |
1770 | | /* make it '[ host %25 zoneid ]' */ |
1771 | 0 | struct dynbuf enc; |
1772 | 0 | size_t hostlen = strlen(u->host); |
1773 | 0 | curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH); |
1774 | 0 | if(curlx_dyn_addf(&enc, "%.*s%%25%s]", (int)hostlen - 1, u->host, |
1775 | 0 | u->zoneid)) |
1776 | 0 | return CURLUE_OUT_OF_MEMORY; |
1777 | 0 | allochost = curlx_dyn_ptr(&enc); |
1778 | 0 | } |
1779 | 0 | } |
1780 | 100k | else if(flags & CURLU_URLENCODE) { |
1781 | 7.28k | allochost = curl_easy_escape(NULL, u->host, 0); |
1782 | 7.28k | if(!allochost) |
1783 | 3 | return CURLUE_OUT_OF_MEMORY; |
1784 | 7.28k | } |
1785 | 92.9k | else if(flags & CURLU_PUNYCODE) { |
1786 | 0 | if(!Curl_is_ASCII_name(u->host)) { |
1787 | 0 | CURLUcode ret = host_decode(u->host, &allochost); |
1788 | 0 | if(!ret) |
1789 | 0 | ret = Curl_hostname_check(allochost, strlen(allochost)); |
1790 | 0 | if(ret) { |
1791 | 0 | curlx_safefree(allochost); |
1792 | 0 | return ret; |
1793 | 0 | } |
1794 | 0 | } |
1795 | 0 | } |
1796 | 92.9k | else if(flags & CURLU_PUNY2IDN) { |
1797 | 0 | if(Curl_is_ASCII_name(u->host)) { |
1798 | 0 | CURLUcode ret = host_encode(u->host, &allochost); |
1799 | 0 | if(ret) |
1800 | 0 | return ret; |
1801 | 0 | } |
1802 | 0 | } |
1803 | | |
1804 | 100k | if(!(flags & CURLU_NO_GUESS_SCHEME) || !u->guessed_scheme) |
1805 | 100k | curl_msnprintf(schemebuf, sizeof(schemebuf), "%s://", scheme); |
1806 | 0 | else |
1807 | 0 | schemebuf[0] = 0; |
1808 | | |
1809 | 100k | url = curl_maprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", |
1810 | 100k | schemebuf, |
1811 | 100k | u->user ? u->user : "", |
1812 | 100k | u->password ? ":" : "", |
1813 | 100k | u->password ? u->password : "", |
1814 | 100k | options ? ";" : "", |
1815 | 100k | options ? options : "", |
1816 | 100k | (u->user || u->password || options) ? "@" : "", |
1817 | 100k | allochost ? allochost : u->host, |
1818 | 100k | port ? ":" : "", |
1819 | 100k | port ? port : "", |
1820 | 100k | u->path ? u->path : "/", |
1821 | 100k | querysep, |
1822 | 100k | u->query ? u->query : "", |
1823 | 100k | fragmentsep, |
1824 | 100k | u->fragment ? u->fragment : ""); |
1825 | 100k | curlx_free(allochost); |
1826 | 100k | } |
1827 | 100k | if(!url) |
1828 | 61 | return CURLUE_OUT_OF_MEMORY; |
1829 | 100k | *part = url; |
1830 | 100k | return CURLUE_OK; |
1831 | 100k | } |
1832 | | |
1833 | | CURLUcode curl_url_get(const CURLU *u, CURLUPart what, |
1834 | | char **part, unsigned int flags) |
1835 | 493k | { |
1836 | 493k | const char *ptr; |
1837 | 493k | CURLUcode ifmissing = CURLUE_UNKNOWN_PART; |
1838 | 493k | char portbuf[7]; |
1839 | 493k | bool plusdecode = FALSE; |
1840 | 493k | if(!u) |
1841 | 0 | return CURLUE_BAD_HANDLE; |
1842 | 493k | if(!part) |
1843 | 0 | return CURLUE_BAD_PARTPOINTER; |
1844 | 493k | *part = NULL; |
1845 | | |
1846 | 493k | switch(what) { |
1847 | 49.6k | case CURLUPART_SCHEME: |
1848 | 49.6k | ptr = u->scheme; |
1849 | 49.6k | ifmissing = CURLUE_NO_SCHEME; |
1850 | 49.6k | flags &= ~U_CURLU_URLDECODE; /* never for schemes */ |
1851 | 49.6k | if((flags & CURLU_NO_GUESS_SCHEME) && u->guessed_scheme) |
1852 | 0 | return CURLUE_NO_SCHEME; |
1853 | 49.6k | break; |
1854 | 49.6k | case CURLUPART_USER: |
1855 | 47.6k | ptr = u->user; |
1856 | 47.6k | ifmissing = CURLUE_NO_USER; |
1857 | 47.6k | break; |
1858 | 47.6k | case CURLUPART_PASSWORD: |
1859 | 47.6k | ptr = u->password; |
1860 | 47.6k | ifmissing = CURLUE_NO_PASSWORD; |
1861 | 47.6k | break; |
1862 | 49.6k | case CURLUPART_OPTIONS: |
1863 | 49.6k | ptr = u->options; |
1864 | 49.6k | ifmissing = CURLUE_NO_OPTIONS; |
1865 | 49.6k | break; |
1866 | 49.6k | case CURLUPART_HOST: |
1867 | 49.6k | ptr = u->host; |
1868 | 49.6k | ifmissing = CURLUE_NO_HOST; |
1869 | 49.6k | break; |
1870 | 49.6k | case CURLUPART_ZONEID: |
1871 | 49.6k | ptr = u->zoneid; |
1872 | 49.6k | ifmissing = CURLUE_NO_ZONEID; |
1873 | 49.6k | break; |
1874 | 0 | case CURLUPART_PORT: |
1875 | 0 | ptr = NULL; |
1876 | 0 | ifmissing = CURLUE_NO_PORT; |
1877 | 0 | flags &= ~U_CURLU_URLDECODE; /* never for port */ |
1878 | 0 | if(u->port_present) { |
1879 | 0 | const struct Curl_scheme *h = u->scheme ? |
1880 | 0 | Curl_get_scheme(u->scheme) : NULL; |
1881 | | /* there is a stored port number, but ask to inhibit if |
1882 | | it matches the default one for the scheme */ |
1883 | 0 | if(h && (h->defport == u->portnum) && |
1884 | 0 | (flags & CURLU_NO_DEFAULT_PORT)) { |
1885 | 0 | ptr = NULL; |
1886 | 0 | } |
1887 | 0 | else { |
1888 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%u", u->portnum); |
1889 | 0 | ptr = portbuf; |
1890 | 0 | } |
1891 | 0 | } |
1892 | 0 | else if((flags & CURLU_DEFAULT_PORT) && u->scheme) { |
1893 | | /* there is no stored port number, but asked to deliver |
1894 | | a default one for the scheme */ |
1895 | 0 | const struct Curl_scheme *h = Curl_get_scheme(u->scheme); |
1896 | 0 | if(h) { |
1897 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%u", h->defport); |
1898 | 0 | ptr = portbuf; |
1899 | 0 | } |
1900 | 0 | } |
1901 | 0 | break; |
1902 | 49.6k | case CURLUPART_PATH: |
1903 | 49.6k | ptr = u->path; |
1904 | 49.6k | if(!ptr) |
1905 | 38.6k | ptr = "/"; |
1906 | 49.6k | break; |
1907 | 49.6k | case CURLUPART_QUERY: |
1908 | 49.6k | ptr = u->query; |
1909 | 49.6k | ifmissing = CURLUE_NO_QUERY; |
1910 | 49.6k | plusdecode = flags & CURLU_URLDECODE; |
1911 | 49.6k | if(ptr && !ptr[0] && !(flags & CURLU_GET_EMPTY)) |
1912 | | /* there was a blank query and the user does not ask for it */ |
1913 | 0 | ptr = NULL; |
1914 | 49.6k | break; |
1915 | 0 | case CURLUPART_FRAGMENT: |
1916 | 0 | ptr = u->fragment; |
1917 | 0 | ifmissing = CURLUE_NO_FRAGMENT; |
1918 | 0 | if(!ptr && u->fragment_present && flags & CURLU_GET_EMPTY) |
1919 | | /* there was a blank fragment and the user asks for it */ |
1920 | 0 | ptr = ""; |
1921 | 0 | break; |
1922 | 100k | case CURLUPART_URL: |
1923 | 100k | return urlget_url(u, part, flags); |
1924 | 0 | default: |
1925 | 0 | ptr = NULL; |
1926 | 0 | break; |
1927 | 493k | } |
1928 | 393k | if(ptr) |
1929 | 154k | return urlget_format(u, what, ptr, part, plusdecode, flags); |
1930 | | |
1931 | 238k | return ifmissing; |
1932 | 393k | } |
1933 | | |
1934 | | static CURLUcode set_url_scheme(CURLU *u, const char *scheme, |
1935 | | unsigned int flags) |
1936 | 0 | { |
1937 | 0 | size_t plen = strlen(scheme); |
1938 | 0 | const struct Curl_scheme *h = NULL; |
1939 | 0 | if((plen > MAX_SCHEME_LEN) || (plen < 1)) |
1940 | | /* too long or too short */ |
1941 | 0 | return CURLUE_BAD_SCHEME; |
1942 | | /* verify that it is a fine scheme */ |
1943 | 0 | h = Curl_get_scheme(scheme); |
1944 | 0 | if(!(flags & CURLU_NON_SUPPORT_SCHEME) && (!h || !h->run)) |
1945 | 0 | return CURLUE_UNSUPPORTED_SCHEME; |
1946 | 0 | if(!h) { |
1947 | 0 | const char *s = scheme; |
1948 | 0 | if(ISALPHA(*s)) { |
1949 | | /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */ |
1950 | 0 | s++; |
1951 | 0 | while(--plen) { |
1952 | 0 | if(ISALNUM(*s) || (*s == '+') || (*s == '-') || (*s == '.')) |
1953 | 0 | s++; /* fine */ |
1954 | 0 | else |
1955 | 0 | return CURLUE_BAD_SCHEME; |
1956 | 0 | } |
1957 | 0 | } |
1958 | 0 | else |
1959 | 0 | return CURLUE_BAD_SCHEME; |
1960 | 0 | } |
1961 | 0 | u->guessed_scheme = FALSE; |
1962 | 0 | return CURLUE_OK; |
1963 | 0 | } |
1964 | | |
1965 | | static CURLUcode set_url_port(CURLU *u, const char *provided_port) |
1966 | 0 | { |
1967 | 0 | curl_off_t port; |
1968 | 0 | if(!ISDIGIT(provided_port[0])) |
1969 | | /* not a number */ |
1970 | 0 | return CURLUE_BAD_PORT_NUMBER; |
1971 | 0 | if(curlx_str_number(&provided_port, &port, 0xffff) || *provided_port) |
1972 | | /* weirdly provided number, not good! */ |
1973 | 0 | return CURLUE_BAD_PORT_NUMBER; |
1974 | 0 | u->portnum = (uint16_t)port; |
1975 | 0 | u->port_present = TRUE; |
1976 | 0 | return CURLUE_OK; |
1977 | 0 | } |
1978 | | |
1979 | | static CURLUcode set_url(CURLU *u, const char *url, size_t part_size, |
1980 | | unsigned int flags) |
1981 | 114k | { |
1982 | | /* |
1983 | | * Allow a new URL to replace the existing (if any) contents. |
1984 | | * |
1985 | | * If the existing contents is enough for a URL, allow a relative URL to |
1986 | | * replace it. |
1987 | | */ |
1988 | 114k | CURLUcode uc; |
1989 | 114k | char *oldurl = NULL; |
1990 | | |
1991 | 114k | if(!part_size) { |
1992 | | /* a blank URL is not a valid URL unless we already have a complete one |
1993 | | and this is a redirect */ |
1994 | 0 | uc = curl_url_get(u, CURLUPART_URL, &oldurl, flags); |
1995 | 0 | if(!uc) { |
1996 | | /* success, meaning the "" is a fine relative URL, and the new URL |
1997 | | inherits scheme/authority/path/query, but not fragment, from the |
1998 | | existing URL (RFC 3986 section 5.2.2) */ |
1999 | 0 | curlx_safefree(u->fragment); |
2000 | 0 | u->fragment_present = FALSE; |
2001 | 0 | curlx_free(oldurl); |
2002 | 0 | return CURLUE_OK; |
2003 | 0 | } |
2004 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) |
2005 | 0 | return uc; |
2006 | 0 | return CURLUE_MALFORMED_INPUT; |
2007 | 0 | } |
2008 | | |
2009 | | /* if the new URL is absolute replace the existing with the new. */ |
2010 | 114k | if(Curl_is_absolute_url(url, NULL, 0, |
2011 | 114k | flags & (CURLU_GUESS_SCHEME | CURLU_DEFAULT_SCHEME))) |
2012 | 106k | return parseurl_and_replace(url, u, flags); |
2013 | | |
2014 | | /* if the old URL is incomplete (we cannot get an absolute URL in |
2015 | | 'oldurl'), replace the existing with the new. |
2016 | | Always include "scheme://" to make the URL "complete" */ |
2017 | | /* Preserve empty query/fragment separators: they affect where relative |
2018 | | references splice into the base URL. */ |
2019 | 7.60k | uc = curl_url_get(u, CURLUPART_URL, &oldurl, |
2020 | 7.60k | (flags & ~CURLU_NO_GUESS_SCHEME) | CURLU_GET_EMPTY); |
2021 | 7.60k | if(uc == CURLUE_OUT_OF_MEMORY) |
2022 | 8 | return uc; |
2023 | 7.59k | else if(uc) |
2024 | 0 | return parseurl_and_replace(url, u, flags); |
2025 | | |
2026 | 7.59k | DEBUGASSERT(oldurl); /* it is set here */ |
2027 | | /* apply the relative part to create a new URL */ |
2028 | 7.59k | uc = redirect_url(oldurl, url, u, flags); |
2029 | 7.59k | curlx_free(oldurl); |
2030 | 7.59k | return uc; |
2031 | 7.59k | } |
2032 | | |
2033 | | static CURLUcode urlset_clear(CURLU *u, CURLUPart what) |
2034 | 44.6k | { |
2035 | 44.6k | switch(what) { |
2036 | 0 | case CURLUPART_URL: |
2037 | 0 | free_urlhandle(u); |
2038 | 0 | memset(u, 0, sizeof(struct Curl_URL)); |
2039 | 0 | break; |
2040 | 0 | case CURLUPART_SCHEME: |
2041 | 0 | curlx_safefree(u->scheme); |
2042 | 0 | u->guessed_scheme = FALSE; |
2043 | 0 | break; |
2044 | 21.7k | case CURLUPART_USER: |
2045 | 21.7k | curlx_safefree(u->user); |
2046 | 21.7k | break; |
2047 | 21.7k | case CURLUPART_PASSWORD: |
2048 | 21.7k | curlx_strzero(u->password); |
2049 | 21.7k | curlx_safefree(u->password); |
2050 | 21.7k | break; |
2051 | 0 | case CURLUPART_OPTIONS: |
2052 | 0 | curlx_safefree(u->options); |
2053 | 0 | break; |
2054 | 0 | case CURLUPART_HOST: |
2055 | 0 | curlx_safefree(u->host); |
2056 | 0 | break; |
2057 | 0 | case CURLUPART_ZONEID: |
2058 | 0 | curlx_safefree(u->zoneid); |
2059 | 0 | break; |
2060 | 0 | case CURLUPART_PORT: |
2061 | 0 | u->portnum = 0; |
2062 | 0 | u->port_present = FALSE; |
2063 | 0 | break; |
2064 | 0 | case CURLUPART_PATH: |
2065 | 0 | curlx_safefree(u->path); |
2066 | 0 | break; |
2067 | 0 | case CURLUPART_QUERY: |
2068 | 0 | curlx_safefree(u->query); |
2069 | 0 | u->query_present = FALSE; |
2070 | 0 | break; |
2071 | 1.21k | case CURLUPART_FRAGMENT: |
2072 | 1.21k | curlx_safefree(u->fragment); |
2073 | 1.21k | u->fragment_present = FALSE; |
2074 | 1.21k | break; |
2075 | 0 | default: |
2076 | 0 | return CURLUE_UNKNOWN_PART; |
2077 | 44.6k | } |
2078 | 44.6k | return CURLUE_OK; |
2079 | 44.6k | } |
2080 | | |
2081 | | static bool allowed_in_path(unsigned char x) |
2082 | 0 | { |
2083 | 0 | switch(x) { |
2084 | 0 | case '!': |
2085 | 0 | case '$': |
2086 | 0 | case '&': |
2087 | 0 | case '\'': |
2088 | 0 | case '(': |
2089 | 0 | case ')': |
2090 | 0 | case '{': |
2091 | 0 | case '}': |
2092 | 0 | case '[': |
2093 | 0 | case ']': |
2094 | 0 | case '*': |
2095 | 0 | case '+': |
2096 | 0 | case ',': |
2097 | 0 | case ';': |
2098 | 0 | case '=': |
2099 | 0 | case ':': |
2100 | 0 | case '@': |
2101 | 0 | case '/': |
2102 | 0 | return TRUE; |
2103 | 0 | } |
2104 | 0 | return FALSE; |
2105 | 0 | } |
2106 | | |
2107 | | static CURLUcode url_encode_part(struct dynbuf *encp, |
2108 | | const char *part, |
2109 | | bool plusencode, |
2110 | | bool pathmode, |
2111 | | bool equalsencode) |
2112 | 0 | { |
2113 | 0 | const unsigned char *i; |
2114 | |
|
2115 | 0 | for(i = (const unsigned char *)part; *i; i++) { |
2116 | 0 | CURLcode result; |
2117 | 0 | if((*i == ' ') && plusencode) |
2118 | 0 | result = curlx_dyn_addn(encp, "+", 1); |
2119 | 0 | else if(ISUNRESERVED(*i) || |
2120 | 0 | (pathmode && allowed_in_path(*i)) || |
2121 | 0 | ((*i == '=') && equalsencode)) { |
2122 | 0 | if((*i == '=') && equalsencode) |
2123 | | /* only skip the first equals sign */ |
2124 | 0 | equalsencode = FALSE; |
2125 | 0 | result = curlx_dyn_addn(encp, i, 1); |
2126 | 0 | } |
2127 | 0 | else { |
2128 | 0 | unsigned char out[3] = { '%' }; |
2129 | 0 | Curl_hexbyte(&out[1], *i); |
2130 | 0 | result = curlx_dyn_addn(encp, out, 3); |
2131 | 0 | } |
2132 | 0 | if(result) |
2133 | 0 | return cc2cu(result); |
2134 | 0 | } |
2135 | 0 | return CURLUE_OK; |
2136 | 0 | } |
2137 | | |
2138 | | static CURLUcode url_uppercasehex_part(struct dynbuf *encp, |
2139 | | const char *part) |
2140 | 0 | { |
2141 | 0 | char *p; |
2142 | 0 | CURLcode result = curlx_dyn_add(encp, part); |
2143 | 0 | if(result) |
2144 | 0 | return cc2cu(result); |
2145 | 0 | p = curlx_dyn_ptr(encp); |
2146 | 0 | while(*p) { |
2147 | | /* make sure percent encoded are upper case */ |
2148 | 0 | if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) && |
2149 | 0 | (ISLOWER(p[1]) || ISLOWER(p[2]))) { |
2150 | 0 | p[1] = Curl_raw_toupper(p[1]); |
2151 | 0 | p[2] = Curl_raw_toupper(p[2]); |
2152 | 0 | p += 3; |
2153 | 0 | } |
2154 | 0 | else |
2155 | 0 | p++; |
2156 | 0 | } |
2157 | 0 | return CURLUE_OK; |
2158 | 0 | } |
2159 | | |
2160 | | static CURLUcode url_append_query(CURLU *u, struct dynbuf *encp) |
2161 | 0 | { |
2162 | | /* Append the 'encp' string onto the old query. Add a '&' separator if none |
2163 | | is already present at the end of the existing query */ |
2164 | |
|
2165 | 0 | size_t querylen = u->query ? strlen(u->query) : 0; |
2166 | 0 | bool addamperand = querylen && (u->query[querylen - 1] != '&'); |
2167 | 0 | if(querylen) { |
2168 | 0 | struct dynbuf qbuf; |
2169 | 0 | CURLcode result; |
2170 | 0 | const char *newp = curlx_dyn_ptr(encp); |
2171 | 0 | curlx_dyn_init(&qbuf, CURL_MAX_INPUT_LENGTH); |
2172 | | |
2173 | | /* add original query */ |
2174 | 0 | result = curlx_dyn_addn(&qbuf, u->query, querylen); |
2175 | 0 | if(!result && addamperand) |
2176 | | /* add ampersand */ |
2177 | 0 | result = curlx_dyn_addn(&qbuf, "&", 1); |
2178 | 0 | if(!result) |
2179 | | /* add new query part */ |
2180 | 0 | result = curlx_dyn_add(&qbuf, newp); |
2181 | 0 | if(result) |
2182 | 0 | goto nomem; |
2183 | 0 | curlx_dyn_free(encp); |
2184 | 0 | curlx_free(u->query); |
2185 | 0 | u->query = curlx_dyn_ptr(&qbuf); |
2186 | 0 | return CURLUE_OK; |
2187 | 0 | nomem: |
2188 | 0 | curlx_dyn_free(encp); |
2189 | 0 | return cc2cu(result); |
2190 | 0 | } |
2191 | 0 | else { |
2192 | 0 | curlx_free(u->query); |
2193 | 0 | u->query = curlx_dyn_ptr(encp); |
2194 | 0 | } |
2195 | 0 | return CURLUE_OK; |
2196 | 0 | } |
2197 | | |
2198 | | static CURLUcode url_sethost(CURLU *u, struct dynbuf *encp, |
2199 | | bool urlencode, |
2200 | | unsigned int flags) |
2201 | 0 | { |
2202 | 0 | size_t n = curlx_dyn_len(encp); |
2203 | 0 | bool bad = FALSE; |
2204 | 0 | char *newp = curlx_dyn_ptr(encp); |
2205 | 0 | if(!n) |
2206 | | /* an empty hostname is okay if told so */ |
2207 | 0 | bad = (flags & CURLU_NO_AUTHORITY) ? FALSE : TRUE; |
2208 | 0 | else if(!urlencode) { |
2209 | | /* if the hostname part was not URL encoded here, it was set already URL |
2210 | | encoded so we need to decode it to check */ |
2211 | 0 | size_t dlen; |
2212 | 0 | char *decoded = NULL; |
2213 | 0 | CURLcode result = Curl_urldecode(newp, n, &decoded, &dlen, REJECT_CTRL); |
2214 | 0 | if(result || hostname_check6(u, decoded, dlen, encp)) |
2215 | 0 | bad = TRUE; |
2216 | 0 | else if(decoded[0] != '[') { |
2217 | 0 | struct dynbuf dbuf; |
2218 | 0 | curlx_dyn_init(&dbuf, CURL_MAX_INPUT_LENGTH); |
2219 | 0 | if(curlx_dyn_addn(&dbuf, decoded, dlen)) |
2220 | 0 | bad = TRUE; |
2221 | 0 | else if(ipv4_normalize(&dbuf) == HOST_IPV4) { |
2222 | 0 | curlx_dyn_reset(encp); |
2223 | 0 | if(curlx_dyn_addn(encp, curlx_dyn_ptr(&dbuf), curlx_dyn_len(&dbuf))) |
2224 | 0 | bad = TRUE; |
2225 | 0 | } |
2226 | 0 | curlx_dyn_free(&dbuf); |
2227 | 0 | } |
2228 | 0 | curlx_free(decoded); |
2229 | 0 | } |
2230 | 0 | else if(hostname_check6(u, newp, n, encp)) |
2231 | 0 | bad = TRUE; |
2232 | 0 | else if(ipv4_normalize(encp) == HOST_ERROR) |
2233 | 0 | bad = TRUE; |
2234 | 0 | if(bad) { |
2235 | 0 | curlx_dyn_free(encp); |
2236 | 0 | return CURLUE_BAD_HOSTNAME; |
2237 | 0 | } |
2238 | 0 | return CURLUE_OK; |
2239 | 0 | } |
2240 | | |
2241 | | CURLUcode curl_url_set(CURLU *u, CURLUPart what, |
2242 | | const char *part, unsigned int flags) |
2243 | 159k | { |
2244 | 159k | char **storep = NULL; |
2245 | 159k | bool urlencode = (flags & CURLU_URLENCODE) ? 1 : 0; |
2246 | 159k | bool plusencode = FALSE; |
2247 | 159k | bool pathmode = FALSE; |
2248 | 159k | bool leadingslash = FALSE; |
2249 | 159k | bool appendquery = FALSE; |
2250 | 159k | bool equalsencode = FALSE; |
2251 | 159k | size_t nalloc; |
2252 | | |
2253 | 159k | if(!u) |
2254 | 0 | return CURLUE_BAD_HANDLE; |
2255 | 159k | if(!part) |
2256 | | /* setting a part to NULL clears it */ |
2257 | 44.6k | return urlset_clear(u, what); |
2258 | | |
2259 | 114k | nalloc = strlen(part); |
2260 | 114k | if(nalloc > CURL_MAX_INPUT_LENGTH) |
2261 | | /* excessive input length */ |
2262 | 0 | return CURLUE_MALFORMED_INPUT; |
2263 | | |
2264 | 114k | switch(what) { |
2265 | 0 | case CURLUPART_SCHEME: { |
2266 | 0 | CURLUcode status = set_url_scheme(u, part, flags); |
2267 | 0 | if(status) |
2268 | 0 | return status; |
2269 | 0 | storep = &u->scheme; |
2270 | 0 | urlencode = FALSE; /* never */ |
2271 | 0 | break; |
2272 | 0 | } |
2273 | 0 | case CURLUPART_USER: |
2274 | 0 | storep = &u->user; |
2275 | 0 | break; |
2276 | 0 | case CURLUPART_PASSWORD: |
2277 | 0 | storep = &u->password; |
2278 | 0 | break; |
2279 | 0 | case CURLUPART_OPTIONS: |
2280 | 0 | storep = &u->options; |
2281 | 0 | break; |
2282 | 0 | case CURLUPART_HOST: |
2283 | 0 | storep = &u->host; |
2284 | 0 | curlx_safefree(u->zoneid); |
2285 | 0 | break; |
2286 | 0 | case CURLUPART_ZONEID: |
2287 | 0 | storep = &u->zoneid; |
2288 | 0 | break; |
2289 | 0 | case CURLUPART_PORT: |
2290 | 0 | return set_url_port(u, part); |
2291 | 0 | case CURLUPART_PATH: |
2292 | 0 | pathmode = TRUE; |
2293 | 0 | leadingslash = TRUE; /* enforce */ |
2294 | 0 | storep = &u->path; |
2295 | 0 | break; |
2296 | 0 | case CURLUPART_QUERY: |
2297 | 0 | plusencode = urlencode; |
2298 | 0 | appendquery = (flags & CURLU_APPENDQUERY) ? 1 : 0; |
2299 | 0 | equalsencode = appendquery; |
2300 | 0 | storep = &u->query; |
2301 | 0 | u->query_present = TRUE; |
2302 | 0 | break; |
2303 | 0 | case CURLUPART_FRAGMENT: |
2304 | 0 | storep = &u->fragment; |
2305 | 0 | u->fragment_present = TRUE; |
2306 | 0 | break; |
2307 | 114k | case CURLUPART_URL: |
2308 | 114k | return set_url(u, part, nalloc, flags); |
2309 | 0 | default: |
2310 | 0 | return CURLUE_UNKNOWN_PART; |
2311 | 114k | } |
2312 | 0 | DEBUGASSERT(storep); |
2313 | 0 | { |
2314 | 0 | const char *newp = NULL; |
2315 | 0 | struct dynbuf enc; |
2316 | 0 | CURLUcode status; |
2317 | 0 | curlx_dyn_init(&enc, (what == CURLUPART_HOST) ? |
2318 | 0 | CURLMAX((nalloc * 3) + 1, MAX_IPADR_LEN + 2) : |
2319 | 0 | (nalloc * 3) + 1 + leadingslash); |
2320 | |
|
2321 | 0 | if(leadingslash && (part[0] != '/')) { |
2322 | 0 | CURLcode result = curlx_dyn_addn(&enc, "/", 1); |
2323 | 0 | if(result) |
2324 | 0 | return cc2cu(result); |
2325 | 0 | } |
2326 | 0 | if(urlencode) |
2327 | 0 | status = url_encode_part(&enc, part, plusencode, pathmode, equalsencode); |
2328 | 0 | else |
2329 | 0 | status = url_uppercasehex_part(&enc, part); |
2330 | 0 | if(!status) { |
2331 | 0 | newp = curlx_dyn_ptr(&enc); |
2332 | |
|
2333 | 0 | if(appendquery && newp) |
2334 | 0 | return url_append_query(u, &enc); |
2335 | 0 | else if(what == CURLUPART_HOST) |
2336 | 0 | status = url_sethost(u, &enc, urlencode, flags); |
2337 | 0 | } |
2338 | 0 | if(status) |
2339 | 0 | return status; |
2340 | | |
2341 | 0 | if(what == CURLUPART_PASSWORD) |
2342 | 0 | curlx_strzero(*storep); |
2343 | 0 | curlx_free(*storep); |
2344 | 0 | *storep = curlx_dyn_ptr(&enc); |
2345 | 0 | } |
2346 | 0 | return CURLUE_OK; |
2347 | 0 | } |
2348 | | |
2349 | | bool Curl_url_same_origin(CURLU *base, CURLU *href) |
2350 | 21.1k | { |
2351 | 21.1k | const struct Curl_scheme *s = NULL; |
2352 | | |
2353 | | /* base must be an absolute URL */ |
2354 | 21.1k | if(!base->scheme || !base->host) |
2355 | 0 | return FALSE; |
2356 | 21.1k | if(href->scheme && !curl_strequal(base->scheme, href->scheme)) |
2357 | 115 | return FALSE; |
2358 | 21.0k | if(href->host) { |
2359 | 21.0k | if(!curl_strequal(base->host, href->host)) |
2360 | 2.77k | return FALSE; |
2361 | | |
2362 | 18.2k | if(base->port_present != href->port_present) { |
2363 | | /* one is present, one is not */ |
2364 | 0 | s = Curl_get_scheme(base->scheme); |
2365 | 0 | if(!s) /* Cannot match default port for unknown scheme */ |
2366 | 0 | return FALSE; |
2367 | | /* to match, the present one must be the default port */ |
2368 | 0 | if((base->port_present && (base->portnum != s->defport)) || |
2369 | 0 | (href->port_present && (href->portnum != s->defport))) |
2370 | 0 | return FALSE; |
2371 | 0 | } |
2372 | 18.2k | else if(base->portnum != href->portnum) /* both present or missing */ |
2373 | 0 | return FALSE; |
2374 | | |
2375 | 18.2k | if(!curl_strequal(base->zoneid ? base->zoneid : "", |
2376 | 18.2k | href->zoneid ? href->zoneid : "")) |
2377 | 0 | return FALSE; |
2378 | 18.2k | } |
2379 | 0 | else if(href->port_present) /* no host in href, then there must be no port */ |
2380 | 0 | return FALSE; |
2381 | 18.2k | return TRUE; |
2382 | 21.0k | } |
2383 | | |
2384 | | CURLUcode Curl_url_get_port(CURLU *u, uint16_t *pport) |
2385 | 49.6k | { |
2386 | 49.6k | if(u->port_present) { |
2387 | 0 | *pport = u->portnum; |
2388 | 0 | return CURLUE_OK; |
2389 | 0 | } |
2390 | 49.6k | else if(u->scheme) { |
2391 | 49.6k | const struct Curl_scheme *s = Curl_get_scheme(u->scheme); |
2392 | 49.6k | if(s && s->defport) { |
2393 | 49.6k | *pport = s->defport; |
2394 | 49.6k | return CURLUE_OK; |
2395 | 49.6k | } |
2396 | 49.6k | } |
2397 | 0 | *pport = 0; |
2398 | 0 | return CURLUE_NO_PORT; |
2399 | 49.6k | } |