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