Artifactspublic-v0

curl

master
ply4 git calls32 ms305 KiB3 cachedprotocol v2
2265 lines62 KiBRaw
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"
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"
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
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 ((('a' <= (str)[0] && (str)[0] <= 'z') || \
50 ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
51 ((str)[1] == ':' || (str)[1] == '|') && \
52 ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
54/* scheme is not URL encoded, the longest libcurl supported ones are... */
55#define MAX_SCHEME_LEN 40
56#define MAX_ZONEID_LEN 16
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
67#define DEFAULT_SCHEME "https"
69static void free_urlhandle(struct Curl_URL *u)
71 curlx_free(u->scheme);
72 curlx_free(u->user);
73 curlx_strzero(u->password);
74 curlx_free(u->password);
75 curlx_free(u->options);
76 curlx_free(u->host);
77 curlx_free(u->zoneid);
78 curlx_free(u->path);
79 curlx_free(u->query);
80 curlx_free(u->fragment);
83/*
84 * Find the separator at the end of the hostname, or the '?' in cases like
85 * http://www.example.com?id=2380
86 */
87static const char *find_host_sep(const char *url)
89 /* Find the start of the hostname */
90 const char *sep = strstr(url, "//");
91 if(!sep)
92 sep = url;
93 else
94 sep += 2;
96 /* Find first / or ? */
97 while(*sep && *sep != '/' && *sep != '?')
98 sep++;
100 return sep;
103/* convert CURLcode to CURLUcode */
104#define cc2cu(x) \
105 ((x) == CURLE_TOO_LARGE ? CURLUE_TOO_LARGE : CURLUE_OUT_OF_MEMORY)
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 */
124UNITTEST CURLUcode urlencode_str(struct dynbuf *o, const char *url,
125 size_t len, bool relative,
126 unsigned int query);
127UNITTEST CURLUcode urlencode_str(struct dynbuf *o, const char *url,
128 size_t len, bool relative,
129 unsigned int query)
131 /* we must add this with whitespace-replacing */
132 const unsigned char *iptr;
133 const unsigned char *host_sep = (const unsigned char *)url;
134 CURLcode result = CURLE_OK;
136 DEBUGASSERT((query >= QUERY_NO) && (query <= QUERY_YES));
138 if(!relative) {
139 size_t n;
140 host_sep = (const unsigned char *)find_host_sep(url);
142 /* output the first piece as-is */
143 n = (const char *)host_sep - url;
144 result = curlx_dyn_addn(o, url, n);
145 len -= n;
146 }
148 for(iptr = host_sep; len && !result;) {
149 if(*iptr == ' ') {
150 if(query != QUERY_YES)
151 result = curlx_dyn_addn(o, "%20", 3);
152 else
153 result = curlx_dyn_addn(o, "+", 1);
154 iptr++;
155 len--;
156 }
157 else if((*iptr < ' ') || (*iptr >= 0x7f)) {
158 unsigned char out[3] = { '%' };
159 Curl_hexbyte(&out[1], *iptr);
160 result = curlx_dyn_addn(o, out, 3);
161 iptr++;
162 len--;
163 }
164 else if(*iptr == '%' && (len >= 3) &&
165 ISXDIGIT(iptr[1]) && ISXDIGIT(iptr[2]) &&
166 (ISLOWER(iptr[1]) || ISLOWER(iptr[2]))) {
167 /* uppercase it */
168 unsigned char hex = (unsigned char)((curlx_hexval(iptr[1]) << 4) |
169 curlx_hexval(iptr[2]));
170 unsigned char out[3] = { '%' };
171 Curl_hexbyte(&out[1], hex);
172 result = curlx_dyn_addn(o, out, 3);
173 iptr += 3;
174 len -= 3;
175 }
176 else {
177 const unsigned char *start = iptr;
178 while(len) {
179 if(*iptr == ' ' || *iptr < ' ' || *iptr >= 0x7f)
180 break;
181 if(*iptr == '%' && (len >= 3) &&
182 ISXDIGIT(iptr[1]) && ISXDIGIT(iptr[2]) &&
183 (ISLOWER(iptr[1]) || ISLOWER(iptr[2])))
184 break;
185 if(*iptr == '?') {
186 if(query == QUERY_NOT_YET) {
187 iptr++;
188 len--;
189 query = QUERY_YES;
190 break;
191 }
192 }
193 iptr++;
194 len--;
195 }
196 result = curlx_dyn_addn(o, (const char *)start, (size_t)(iptr - start));
197 }
198 }
200 if(result)
201 return cc2cu(result);
202 return CURLUE_OK;
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 */
213size_t Curl_is_absolute_url(const char *url, char *buf, size_t buflen,
214 bool guess_scheme)
216 size_t i = 0;
217 DEBUGASSERT(!buf || (buflen > MAX_SCHEME_LEN));
218 (void)buflen; /* only used in debug-builds */
219 if(buf)
220 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 if(ISALPHA(url[0])) {
226 if(buf)
227 buf[0] = Curl_raw_tolower(url[0]);
228 for(i = 1; i < MAX_SCHEME_LEN; ++i) {
229 char s = url[i];
230 if(s && (ISALNUM(s) || (s == '+') || (s == '-') || (s == '.'))) {
231 if(buf)
232 buf[i] = Curl_raw_tolower(s);
233 }
234 else {
235 break;
236 }
237 }
238 }
239 if(i && (url[i] == ':') && ((url[i + 1] == '/') || !guess_scheme)) {
240 /* If this does not guess scheme, the scheme always ends with the colon so
241 that this also detects data: URLs etc. In guessing mode, data: could
242 be the hostname "data" with a specified port number. */
244 /* the length of the scheme is the name part only */
245 size_t len = i;
246 if(buf)
247 buf[i] = 0;
248 return len;
249 }
250 if(buf)
251 buf[0] = 0;
252 return 0;
255/* scan for byte values <= 31, 127 and maybe space */
256static bool badoctets(const char *input, size_t n, int flags)
258 const uint8_t *p = (const unsigned char *)input;
259 const uint8_t control = flags & CURLU_ALLOW_SPACE ? 0x1f : 0x20;
260 while(n--) {
261 if(*p <= control || *p == 127)
262 return TRUE;
263 p++;
264 }
265 return FALSE;
268/*
269 * parse_hostname_login()
270 *
271 * Parse the login details (username, password and options) from the URL and
272 * strip them out of the hostname
273 *
274 * @unittest 1675
275 */
276UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u,
277 const char *login,
278 size_t len,
279 unsigned int flags,
280 size_t *hostname_offset);
281UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u,
282 const char *login,
283 size_t len,
284 unsigned int flags,
285 size_t *hostname_offset)
287 CURLUcode ures = CURLUE_OK;
288 CURLcode result;
289 char *userp = NULL;
290 char *passwdp = NULL;
291 char *optionsp = NULL;
292 const struct Curl_scheme *h = NULL;
294 /* At this point, we assume all the other special cases have been taken
295 * care of, so the host is at most
296 *
297 * [user[:password][;options]]@]hostname
298 *
299 * We need somewhere to put the embedded details, so do that first.
300 */
301 const char *ptr;
303 DEBUGASSERT(login);
305 *hostname_offset = 0;
306 ptr = memchr(login, '@', len);
307 if(!ptr)
308 goto out;
310 /* We will now try to extract the
311 * possible login information in a string like:
312 * ftp://user:password@ftp.site.example:8021/README */
313 ptr++;
315 /* if this is a known scheme, get some details */
316 if(u->scheme)
317 h = Curl_get_scheme(u->scheme);
319 /* We could use the login information in the URL so extract it. Only parse
320 options if the handler says we should. Note that 'h' might be NULL! */
321 result = Curl_parse_login_details(login, ptr - login - 1,
322 &userp, &passwdp,
323 (h && (h->flags & PROTOPT_URLOPTIONS)) ?
324 &optionsp : NULL);
325 if(result) {
326 /* the only possible error from Curl_parse_login_details is out of
327 memory: */
328 ures = CURLUE_OUT_OF_MEMORY;
329 goto out;
330 }
332 if(userp) {
333 if(flags & CURLU_DISALLOW_USER) {
334 /* Option DISALLOW_USER is set and URL contains username. */
335 ures = CURLUE_USER_NOT_ALLOWED;
336 goto out;
337 }
338 curlx_free(u->user);
339 u->user = userp;
340 }
342 if(passwdp) {
343 curlx_strzero(u->password);
344 curlx_free(u->password);
345 u->password = passwdp;
346 }
348 if(optionsp) {
349 curlx_free(u->options);
350 u->options = optionsp;
351 }
353 if(userp && badoctets(userp, strlen(userp), flags))
354 ures = CURLUE_BAD_USER;
355 else if(passwdp && badoctets(passwdp, strlen(passwdp), flags))
356 ures = CURLUE_BAD_PASSWORD;
357 else if(optionsp && badoctets(optionsp, strlen(optionsp), flags))
358 ures = CURLUE_MALFORMED_INPUT;
360 userp = passwdp = optionsp = NULL;
362 if(!ures) {
363 /* the hostname starts at this offset */
364 *hostname_offset = ptr - login;
365 return CURLUE_OK;
366 }
368out:
370 curlx_free(userp);
371 curlx_strzero(passwdp);
372 curlx_free(passwdp);
373 curlx_free(optionsp);
374 curlx_safefree(u->user);
375 curlx_strzero(u->password);
376 curlx_safefree(u->password);
377 curlx_safefree(u->options);
379 return ures;
382/* @unittest 1653 */
383UNITTEST CURLUcode parse_port(struct Curl_URL *u, struct dynbuf *host,
384 bool has_scheme);
385UNITTEST CURLUcode parse_port(struct Curl_URL *u, struct dynbuf *host,
386 bool has_scheme)
388 const char *portptr;
389 const char *hostname = curlx_dyn_ptr(host);
390 /*
391 * Find the end of an IPv6 address on the ']' ending bracket.
392 */
393 u->portnum = 0;
394 u->port_present = FALSE;
395 if(hostname[0] == '[') {
396 portptr = memchr(hostname + 1, ']', curlx_dyn_len(host) - 1);
397 if(!portptr)
398 return CURLUE_BAD_IPV6;
399 portptr++;
400 /* this is a RFC2732-style specified IP-address */
401 if(*portptr) {
402 if(*portptr != ':')
403 return CURLUE_BAD_PORT_NUMBER;
404 }
405 else
406 portptr = NULL;
407 }
408 else
409 portptr = memchr(hostname, ':', curlx_dyn_len(host));
411 if(portptr) {
412 curl_off_t port;
413 size_t keep = portptr - hostname;
414 int rc;
416 /* Browser behavior adaptation. If there is a colon with no digits after,
417 cut off the name there which makes us ignore the colon and use the
418 default port. Firefox, Chrome and Safari all do that.
420 Do not do it if the URL has no scheme, to make something that looks like
421 a scheme not work! */
422 curlx_dyn_setlen(host, keep);
423 portptr++;
424 if(!*portptr)
425 return has_scheme ? CURLUE_OK : CURLUE_BAD_PORT_NUMBER;
426 if(*portptr == '\\')
427 return CURLUE_BACKSLASH;
428 rc = curlx_str_number(&portptr, &port, 0xffff);
429 if(rc)
430 return CURLUE_BAD_PORT_NUMBER;
431 else if(*portptr == '\\')
432 return CURLUE_BACKSLASH;
433 else if(*portptr)
434 return CURLUE_BAD_PORT_NUMBER;
436 u->portnum = (uint16_t)port;
437 u->port_present = TRUE;
438 }
440 return CURLUE_OK;
443/* characters not allowed in hostnames:
444 " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%|" */
446static const bool invalid_host_char[256] = {
447 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00-0x0F */
448 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x10-0x1F */
449 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, /* 0x20-0x2F */
450 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 0x30-0x3F */
451 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40-0x4F */
452 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 0x50-0x5F */
453 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x6F */
454 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1 /* 0x70-0x7F */
455};
457/* This function assumes 'hostname' now starts with [. It trims 'hostname' in
458 * place and it sets u->zoneid if present.
459 *
460 * @unittest 1675
461 */
462UNITTEST CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname,
463 size_t hlen);
464UNITTEST CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname,
465 size_t hlen) /* length of hostname */
467 size_t len;
468 DEBUGASSERT(*hostname == '[');
469 if(hlen < 4) /* '[::]' is the shortest possible valid string */
470 return CURLUE_BAD_IPV6;
471 hostname++;
472 hlen -= 2;
474 /* only valid IPv6 letters are ok */
475 len = strspn(hostname, "0123456789abcdefABCDEF:.");
477 if(hlen != len) {
478 hlen = len;
479 if(hostname[len] == '%') {
480 /* this could now be '%[zone id]' */
481 char zoneid[MAX_ZONEID_LEN];
482 int i = 0;
483 char *h = &hostname[len + 1];
484 while(!invalid_host_char[(unsigned char)*h] && (*h != ']') &&
485 (i < (MAX_ZONEID_LEN - 1)))
486 zoneid[i++] = *h++;
487 if(!i || (']' != *h))
488 return CURLUE_BAD_IPV6;
489 zoneid[i] = 0;
490 u->zoneid = curlx_strdup(zoneid);
491 if(!u->zoneid)
492 return CURLUE_OUT_OF_MEMORY;
493 hostname[len] = ']'; /* insert end bracket */
494 hostname[len + 1] = 0; /* terminate the hostname */
495 }
496 else
497 return CURLUE_BAD_IPV6;
498 /* hostname is fine */
499 }
501 /* Normalize the IPv6 address */
502 {
503 char dest[16]; /* fits a binary IPv6 address */
504 hostname[hlen] = 0; /* end the address there */
505 if(curlx_inet_pton(AF_INET6, hostname, dest) != 1)
506 return CURLUE_BAD_IPV6;
507 if(!curlx_inet_ntop(AF_INET6, dest, hostname, hlen + 1)) {
508 hlen = strlen(hostname); /* might be shorter now */
509 hostname[hlen + 1] = 0;
510 }
511 hostname[hlen] = ']'; /* restore ending bracket */
512 }
513 return CURLUE_OK;
516/* the input is a confirmed hostname, never an IPv6 address */
517CURLUcode Curl_hostname_check(const char *hostname, size_t hlen)
519 size_t i;
520 for(i = 0; i < hlen; i++) {
521 if(invalid_host_char[(unsigned char)hostname[i]])
522 return CURLUE_BAD_HOSTNAME;
523 }
524 if((hlen >= 2) &&
525 (hostname[hlen - 1] == '.') && (hostname[hlen - 2] == '.'))
526 /* more than one trailing dot is not allowed */
527 return CURLUE_BAD_HOSTNAME;
528 else if((hlen == 1) && (hostname[0] == '.'))
529 /* a single dot alone is not allowed */
530 return CURLUE_BAD_HOSTNAME;
531 return CURLUE_OK;
534/* the input is a hostname or perhaps an IPv6 address */
535static CURLUcode hostname_check6(struct Curl_URL *u, char *hostname,
536 size_t hlen) /* length of hostname */
538 DEBUGASSERT(hostname);
540 if(!hlen)
541 return CURLUE_NO_HOST;
542 else if(hostname[0] == '[')
543 return ipv6_parse(u, hostname, hlen);
545 return Curl_hostname_check(hostname, hlen);
548/*
549 * Handle partial IPv4 numerical addresses and different bases, like
550 * '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc.
551 *
552 * If the given input string is syntactically wrong IPv4 or any part for
553 * example is too big, this function returns HOST_NAME.
554 *
555 * Output the "normalized" version of that input string in plain quad decimal
556 * integers.
557 *
558 * A single dot following the numerical address is accepted and "swallowed" as
559 * if it was never there.
560 *
561 * Returns the host type.
562 *
563 * @unittest 1675
564 */
565UNITTEST int ipv4_normalize(struct dynbuf *host);
566UNITTEST int ipv4_normalize(struct dynbuf *host)
568 bool done = FALSE;
569 int n = 0;
570 const char *c = curlx_dyn_ptr(host);
571 unsigned int parts[4] = { 0, 0, 0, 0 };
572 CURLcode result = CURLE_OK;
574 if(!ISDIGIT(*c))
575 return HOST_NAME;
577 while(!done) {
578 int rc;
579 curl_off_t l;
580 if(*c == '0') {
581 if((c[1] | 0x20) == 'x') {
582 c += 2; /* skip the prefix */
583 rc = curlx_str_hex(&c, &l, UINT_MAX);
584 if(rc)
585 return HOST_NAME;
586 }
587 else
588 rc = curlx_str_octal(&c, &l, UINT_MAX);
589 }
590 else
591 rc = curlx_str_number(&c, &l, UINT_MAX);
593 if(rc) {
594 if(!n || (rc != STRE_NO_NUM) || *c)
595 return HOST_NAME;
596 n--;
597 }
598 else
599 parts[n] = (unsigned int)l;
601 switch(*c) {
602 case '.':
603 if(n == 3) {
604 if(c[1])
605 /* something follows this dot */
606 return HOST_NAME;
607 done = TRUE;
608 }
609 else {
610 n++;
611 c++;
612 }
613 break;
615 case '\0':
616 done = TRUE;
617 break;
619 default:
620 return HOST_NAME;
621 }
622 }
624 switch(n) {
625 case 0: /* a -- 32 bits */
626 curlx_dyn_reset(host);
628 result = curlx_dyn_addf(host, "%u.%u.%u.%u",
629 (parts[0] >> 24),
630 ((parts[0] >> 16) & 0xff),
631 ((parts[0] >> 8) & 0xff),
632 (parts[0] & 0xff));
633 break;
634 case 1: /* a.b -- 8.24 bits */
635 if((parts[0] > 0xff) || (parts[1] > 0xffffff))
636 return HOST_NAME;
637 curlx_dyn_reset(host);
638 result = curlx_dyn_addf(host, "%u.%u.%u.%u",
639 parts[0],
640 ((parts[1] >> 16) & 0xff),
641 ((parts[1] >> 8) & 0xff),
642 (parts[1] & 0xff));
643 break;
644 case 2: /* a.b.c -- 8.8.16 bits */
645 if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff))
646 return HOST_NAME;
647 curlx_dyn_reset(host);
648 result = curlx_dyn_addf(host, "%u.%u.%u.%u",
649 parts[0],
650 parts[1],
651 ((parts[2] >> 8) & 0xff),
652 (parts[2] & 0xff));
653 break;
654 case 3: /* a.b.c.d -- 8.8.8.8 bits */
655 if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) ||
656 (parts[3] > 0xff))
657 return HOST_NAME;
658 curlx_dyn_reset(host);
659 result = curlx_dyn_addf(host, "%u.%u.%u.%u",
660 parts[0],
661 parts[1],
662 parts[2],
663 parts[3]);
664 break;
665 }
666 if(result)
667 return HOST_ERROR;
668 return HOST_IPV4;
671/* if necessary, replace the host content with a URL decoded version */
672static CURLUcode urldecode_host(struct dynbuf *host)
674 const char *per;
675 const char *hostname = curlx_dyn_ptr(host);
676 per = memchr(hostname, '%', curlx_dyn_len(host));
677 if(!per)
678 /* nothing to decode */
679 return CURLUE_OK;
680 else {
681 /* encoded */
682 size_t dlen;
683 char *decoded;
684 CURLcode result = Curl_urldecode(hostname, 0, &decoded, &dlen,
685 REJECT_CTRL);
686 if(result)
687 return CURLUE_BAD_HOSTNAME;
688 curlx_dyn_reset(host);
689 result = curlx_dyn_addn(host, decoded, dlen);
690 curlx_free(decoded);
691 if(result)
692 return cc2cu(result);
693 }
695 return CURLUE_OK;
698static CURLUcode parse_authority(struct Curl_URL *u,
699 const char *auth, size_t authlen,
700 unsigned int flags,
701 struct dynbuf *host,
702 bool has_scheme)
704 size_t offset;
705 CURLUcode uc;
706 CURLcode result;
708 /*
709 * Parse the login details and strip them out of the hostname.
710 */
711 uc = parse_hostname_login(u, auth, authlen, flags, &offset);
712 if(uc)
713 return uc;
715 result = curlx_dyn_addn(host, auth + offset, authlen - offset);
716 if(result) {
717 uc = cc2cu(result);
718 return uc;
719 }
721 /* parse_port() also sets the hostname length correctly */
722 uc = parse_port(u, host, has_scheme);
724 if(!curlx_dyn_len(host))
725 /* this makes no-host errors override port number problems */
726 uc = CURLUE_NO_HOST;
727 if(!uc)
728 uc = urldecode_host(host);
729 if(uc)
730 ;
731 else if(auth[offset] == '[')
732 uc = ipv6_parse(u, curlx_dyn_ptr(host), curlx_dyn_len(host));
733 else {
734 /* ipv4_normalize() returns *NAME, *IPV4 or *ERROR */
735 int type = ipv4_normalize(host);
737 if(type == HOST_NAME)
738 uc = Curl_hostname_check(curlx_dyn_ptr(host), curlx_dyn_len(host));
739 else if(type == HOST_ERROR)
740 uc = CURLUE_OUT_OF_MEMORY;
741 }
743 return uc;
746/* used for HTTP/2 server push */
747CURLUcode Curl_url_set_authority(CURLU *u, const char *authority)
749 CURLUcode ures;
750 struct dynbuf host;
752 DEBUGASSERT(authority);
753 curlx_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
755 ures = parse_authority(u, authority, strlen(authority),
756 CURLU_DISALLOW_USER, &host, !!u->scheme);
757 if(ures)
758 curlx_dyn_free(&host);
759 else {
760 curlx_free(u->host);
761 u->host = curlx_dyn_ptr(&host);
762 }
763 return ures;
766/*
767 * "Remove Dot Segments"
768 * https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
769 */
771static bool is_dot(const char **str, size_t *clen)
773 const char *p = *str;
774 if(*p == '.') {
775 (*str)++;
776 (*clen)--;
777 return TRUE;
778 }
779 else if((*clen >= 3) &&
780 (p[0] == '%') && (p[1] == '2') && ((p[2] | 0x20) == 'e')) {
781 *str += 3;
782 *clen -= 3;
783 return TRUE;
784 }
785 return FALSE;
788#define ISSLASH(x) ((x) == '/')
790/* prescan the string to see if it needs work */
791static bool needs_dedotdot(const char *p, size_t pn)
793 /* a single byte path cannot be cleaned up */
794 if(pn < 2)
795 return FALSE;
796 if(!memchr(p, '.', pn) && !memchr(p, '%', pn))
797 return FALSE;
798 while(pn) {
799 if(is_dot(&p, &pn)) {
800 /* "./" or dot before end of string */
801 if(!pn || ISSLASH(*p))
802 return TRUE;
803 /* "../" or ".." before end of string */
804 else if(is_dot(&p, &pn) && (!pn || ISSLASH(*p)))
805 return TRUE;
806 }
807 else {
808 p++;
809 pn--;
810 }
811 }
812 return FALSE;
815/*
816 * dedotdotify()
817 *
818 * This function gets a null-terminated path with dot and dotdot sequences
819 * passed in and strips them off according to the rules in RFC 3986 section
820 * 5.2.4.
821 *
822 * The function handles a path. It should not contain the query nor fragment.
823 *
824 * RETURNS
825 *
826 * Zero for success and 'out' set to an allocated string (or NULL if there's
827 * nothing to do).
828 *
829 * @unittest 1395
830 */
831UNITTEST int dedotdotify(const char *input, size_t clen, char **outp);
832UNITTEST int dedotdotify(const char *input, size_t clen, char **outp)
834 struct dynbuf out;
835 CURLcode result = CURLE_OK;
837 /* variables for leading dot checks */
838 const char *dinput = input;
839 size_t dlen = clen;
841 *outp = NULL;
842 if(!needs_dedotdot(input, clen))
843 return 0;
845 curlx_dyn_init(&out, clen + 1);
847 /* if the input buffer begins with a prefix of "../" or "./", then remove
848 that prefix from the input buffer; otherwise, */
849 if(is_dot(&dinput, &dlen)) {
850 if(ISSLASH(*dinput)) {
851 /* one dot followed by a slash */
852 input = dinput + 1;
853 clen = dlen - 1;
854 }
856 /* if the input buffer consists only of "." or "..", then remove
857 that from the input buffer; otherwise, */
858 else if(is_dot(&dinput, &dlen)) {
859 if(!dlen)
860 /* .. [end] */
861 goto end;
862 else if(ISSLASH(*dinput)) {
863 /* ../ */
864 input = dinput + 1;
865 clen = dlen - 1;
866 }
867 }
868 }
870 while(clen && !result) { /* until end of path content */
871 const char *next;
872 size_t seglen;
873 if(ISSLASH(*input)) {
874 const char *p = &input[1];
875 size_t blen = clen - 1;
876 /* if the input buffer begins with a prefix of "/./" or "/.", where "."
877 is a complete path segment, then replace that prefix with "/" in the
878 input buffer; otherwise, */
879 if(is_dot(&p, &blen)) {
880 if(!blen) { /* /. */
881 result = curlx_dyn_addn(&out, "/", 1);
882 break;
883 }
884 else if(ISSLASH(*p)) { /* /./ */
885 input = p;
886 clen = blen;
887 continue;
888 }
890 /* if the input buffer begins with a prefix of "/../" or "/..", where
891 ".." is a complete path segment, then replace that prefix with "/"
892 in the input buffer and remove the last segment and its preceding
893 "/" (if any) from the output buffer; otherwise, */
894 else if(is_dot(&p, &blen) && (ISSLASH(*p) || !blen)) {
895 /* remove the last segment from the output buffer */
896 size_t len = curlx_dyn_len(&out);
897 if(len) {
898 const char *ptr = curlx_dyn_ptr(&out);
899 const char *last = memrchr(ptr, '/', len);
900 if(last)
901 /* trim the output at the slash */
902 curlx_dyn_setlen(&out, last - ptr);
903 }
905 if(blen) { /* /../ */
906 input = p;
907 clen = blen;
908 continue;
909 }
910 result = curlx_dyn_addn(&out, "/", 1);
911 break;
912 }
913 }
914 }
916 /* move the first path segment in the input buffer to the end of the
917 output buffer, including the initial "/" character (if any) and any
918 subsequent characters up to, but not including, the next "/" character
919 or the end of the input buffer. */
921 next = memchr(input + 1, '/', clen - 1);
922 seglen = next ? (size_t)(next - input) : clen;
923 result = curlx_dyn_addn(&out, input, seglen);
924 input += seglen;
925 clen -= seglen;
926 }
927end:
928 if(!result) {
929 if(curlx_dyn_len(&out))
930 *outp = curlx_dyn_ptr(&out);
931 else {
932 *outp = curlx_strdup("");
933 if(!*outp)
934 return 1;
935 }
936 }
937 return result ? 1 : 0; /* success */
940/*
941 * @unittest 1675
942 */
943UNITTEST CURLUcode parse_file(const char *url, size_t urllen, CURLU *u,
944 const char **pathp, size_t *pathlenp);
945UNITTEST CURLUcode parse_file(const char *url, size_t urllen, CURLU *u,
946 const char **pathp, size_t *pathlenp)
948 const char *path;
949 size_t pathlen;
951 *pathp = NULL;
952 *pathlenp = 0;
953 if(urllen <= 6)
954 /* file:/ is not enough to actually be a complete file: URL */
955 return CURLUE_BAD_FILE_URL;
957 /* path has been allocated large enough to hold this */
958 path = &url[5];
959 pathlen = urllen - 5;
961 /* RFC 8089: file-hier-part = ( "//" auth-path ) / local-path, where
962 local-path also starts with a "/". So reject anything that does not
963 start with at least one "/" */
964 if(path[0] != '/')
965 return CURLUE_BAD_FILE_URL;
967 /* Extra handling URLs with an authority component (i.e. that start with
968 * "file://")
969 *
970 * We allow omitted hostname (e.g. file:/<path>) -- valid according to
971 * RFC 8089, but not the (current) WHAT-WG URL spec.
972 */
973 if(path[1] == '/') {
974 /* swallow the two slashes */
975 const char *ptr = &path[2];
977 /*
978 * According to RFC 8089, a file: URL can be reliably dereferenced if:
979 *
980 * o it has no/blank hostname, or
981 *
982 * o the hostname matches "localhost" (case-insensitively), or
983 *
984 * o the hostname is a FQDN that resolves to this machine, or
985 *
986 * For brevity, we only consider URLs with empty, "localhost", or
987 * "127.0.0.1" hostnames as local, otherwise as an UNC String.
988 *
989 * Additionally, there is an exception for URLs with a Windows drive
990 * letter in the authority (which was accidentally omitted from RFC 8089
991 * Appendix E, but believe me, it was meant to be there. --MK)
992 */
993 if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
994 /* the URL includes a hostname, it must match "localhost" or
995 "127.0.0.1" to be valid */
996 if(checkprefix("localhost/", ptr) ||
997 checkprefix("127.0.0.1/", ptr)) {
998 ptr += 9; /* now points to the slash after the host */
999 }
1000 else
1001 /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
1002 none */
1003 return CURLUE_BAD_FILE_URL;
1006 path = ptr;
1007 pathlen = urllen - (ptr - url);
1010#if !defined(_WIN32) && !defined(MSDOS) && !defined(__CYGWIN__)
1011 /* Do not allow Windows drive letters when not in Windows.
1012 * This catches both "file:/c:" and "file:c:" */
1013 if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
1014 STARTS_WITH_URL_DRIVE_PREFIX(path)) {
1015 /* File drive letters are only accepted in MS-DOS/Windows */
1016 return CURLUE_BAD_FILE_URL;
1018#else
1019 /* If the path starts with a slash and a drive letter, ditch the slash */
1020 if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
1021 /* This cannot be done with strcpy, as the memory chunks overlap! */
1022 path++;
1023 pathlen--;
1025#endif
1026 u->scheme = curlx_strdup("file");
1027 if(!u->scheme)
1028 return CURLUE_OUT_OF_MEMORY;
1030 *pathp = path;
1031 *pathlenp = pathlen;
1032 return CURLUE_OK;
1035static CURLUcode parse_scheme(const char *url, CURLU *u, char *schemebuf,
1036 size_t schemelen, unsigned int flags,
1037 const char **hostpp)
1039 /* clear path */
1040 const char *schemep = NULL;
1042 if(schemelen) {
1043 int num_slashes = 0;
1044 const char *p = &url[schemelen + 1];
1045 if(!Curl_getn_scheme(schemebuf, schemelen) &&
1046 !(flags & CURLU_NON_SUPPORT_SCHEME))
1047 return CURLUE_UNSUPPORTED_SCHEME;
1049 if(!ISSLASH(*p))
1050 /* less than one */
1051 return CURLUE_BAD_SLASHES;
1052 if((flags & CURLU_NO_AUTHORITY)) {
1053 while(ISSLASH(*p) && (num_slashes < 2)) {
1054 p++;
1055 num_slashes++;
1058 else {
1059 while(ISSLASH(*p) && (num_slashes < 4)) {
1060 p++;
1061 num_slashes++;
1063 if(num_slashes > 3)
1064 return CURLUE_BAD_SLASHES;
1067 schemep = schemebuf;
1068 *hostpp = p; /* hostname starts here */
1070 else {
1071 /* no scheme! */
1073 if(!(flags & (CURLU_DEFAULT_SCHEME | CURLU_GUESS_SCHEME)))
1074 return CURLUE_BAD_SCHEME;
1076 if(flags & CURLU_DEFAULT_SCHEME)
1077 schemep = DEFAULT_SCHEME;
1079 /*
1080 * The URL was badly formatted, let's try without scheme specified.
1081 */
1082 *hostpp = url;
1085 if(schemep) {
1086 u->scheme = curlx_strdup(schemep);
1087 if(!u->scheme)
1088 return CURLUE_OUT_OF_MEMORY;
1090 return CURLUE_OK;
1093static CURLUcode guess_scheme(CURLU *u, struct dynbuf *host)
1095 const char *hostname = curlx_dyn_ptr(host);
1096 const char *schemep = NULL;
1097 /* legacy curl-style guess based on hostname */
1098 if(checkprefix("ftp.", hostname))
1099 schemep = "ftp";
1100 else if(checkprefix("dict.", hostname))
1101 schemep = "dict";
1102 else if(checkprefix("ldap.", hostname))
1103 schemep = "ldap";
1104 else if(checkprefix("imap.", hostname))
1105 schemep = "imap";
1106 else if(checkprefix("smtp.", hostname))
1107 schemep = "smtp";
1108 else if(checkprefix("pop3.", hostname))
1109 schemep = "pop3";
1110 else
1111 schemep = "http";
1113 u->scheme = curlx_strdup(schemep);
1114 if(!u->scheme)
1115 return CURLUE_OUT_OF_MEMORY;
1117 u->guessed_scheme = TRUE;
1118 return CURLUE_OK;
1121static CURLUcode handle_fragment(CURLU *u, const char *fragment,
1122 size_t fraglen, unsigned int flags)
1124 CURLUcode ures;
1125 u->fragment_present = TRUE;
1126 if(fraglen > 1) {
1127 /* skip the leading '#' in the copy but include the null-terminator */
1128 if(flags & CURLU_URLENCODE) {
1129 struct dynbuf enc;
1130 curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
1131 ures = urlencode_str(&enc, fragment + 1, fraglen - 1, TRUE, QUERY_NO);
1132 if(ures)
1133 return ures;
1134 u->fragment = curlx_dyn_ptr(&enc);
1136 else {
1137 if(badoctets(fragment, fraglen, flags))
1138 return CURLUE_BAD_FRAGMENT;
1139 u->fragment = curlx_memdup0(fragment + 1, fraglen - 1);
1140 if(!u->fragment)
1141 return CURLUE_OUT_OF_MEMORY;
1144 return CURLUE_OK;
1147static CURLUcode handle_query(CURLU *u, const char *query,
1148 size_t qlen, unsigned int flags)
1150 u->query_present = TRUE;
1151 if(qlen > 1) {
1152 if(flags & CURLU_URLENCODE) {
1153 struct dynbuf enc;
1154 CURLUcode ures;
1155 curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
1156 /* skip the leading question mark */
1157 ures = urlencode_str(&enc, query + 1, qlen - 1, TRUE, QUERY_YES);
1158 if(ures)
1159 return ures;
1160 u->query = curlx_dyn_ptr(&enc);
1162 else {
1163 if(badoctets(query, qlen, flags))
1164 return CURLUE_BAD_QUERY;
1166 u->query = curlx_memdup0(query + 1, qlen - 1);
1167 if(!u->query)
1168 return CURLUE_OUT_OF_MEMORY;
1171 else {
1172 /* single byte query */
1173 u->query = curlx_strdup("");
1174 if(!u->query)
1175 return CURLUE_OUT_OF_MEMORY;
1177 return CURLUE_OK;
1180static CURLUcode handle_path(CURLU *u, const char *path,
1181 size_t pathlen, unsigned int flags,
1182 bool is_file)
1184 CURLUcode ures;
1185 if(pathlen && (flags & CURLU_URLENCODE)) {
1186 struct dynbuf enc;
1187 curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
1188 ures = urlencode_str(&enc, path, pathlen, TRUE, QUERY_NO);
1189 if(ures)
1190 return ures;
1191 pathlen = curlx_dyn_len(&enc);
1192 path = u->path = curlx_dyn_ptr(&enc);
1195 if(pathlen >= (size_t)(1 + !is_file)) {
1196 if(badoctets(path, pathlen, flags))
1197 return CURLUE_BAD_PATH;
1199 /* paths for file:// scheme can be one byte, others need to be two */
1200 if(!u->path) {
1201 u->path = curlx_memdup0(path, pathlen);
1202 if(!u->path)
1203 return CURLUE_OUT_OF_MEMORY;
1204 path = u->path;
1206 else if(flags & CURLU_URLENCODE)
1207 /* it might have encoded more than the path so cut it */
1208 u->path[pathlen] = 0;
1210 if(!(flags & CURLU_PATH_AS_IS)) {
1211 /* remove ../ and ./ sequences according to RFC3986 */
1212 char *dedot;
1213 int err = dedotdotify(path, pathlen, &dedot);
1214 if(err)
1215 return CURLUE_OUT_OF_MEMORY;
1216 if(dedot) {
1217 curlx_free(u->path);
1218 u->path = dedot;
1222 return CURLUE_OK;
1225static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
1227 const char *path;
1228 size_t pathlen;
1229 char schemebuf[MAX_SCHEME_LEN + 1];
1230 size_t schemelen = 0;
1231 size_t urllen;
1232 CURLUcode ures = CURLUE_OK;
1233 struct dynbuf host;
1234 bool is_file = FALSE;
1236 DEBUGASSERT(url);
1238 urllen = strlen(url);
1239 if(urllen > CURL_MAX_INPUT_LENGTH)
1240 return CURLUE_MALFORMED_INPUT;
1242 curlx_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
1244 schemelen = Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf),
1245 flags & (CURLU_GUESS_SCHEME |
1246 CURLU_DEFAULT_SCHEME));
1248 /* handle the file: scheme */
1249 if(schemelen == 4 && !memcmp(schemebuf, "file", 4)) {
1250 is_file = TRUE;
1251 ures = parse_file(url, urllen, u, &path, &pathlen);
1253 else {
1254 const char *hostp = NULL;
1255 const char *p;
1256 size_t hostlen;
1257 ures = parse_scheme(url, u, schemebuf, schemelen, flags, &hostp);
1258 if(ures)
1259 goto fail;
1261 /* find the end of the hostname + port number */
1262 p = hostp;
1263 while(*p && *p != '/' && *p != '?' && *p != '#')
1264 p++;
1265 hostlen = p - hostp;
1266 path = p;
1268 /* this pathlen also contains the query and the fragment */
1269 pathlen = urllen - (path - url);
1270 if(hostlen) {
1271 ures = parse_authority(u, hostp, hostlen, flags, &host, !!u->scheme);
1272 if(!ures && (flags & CURLU_GUESS_SCHEME) && !u->scheme)
1273 ures = guess_scheme(u, &host);
1275 else if(flags & CURLU_NO_AUTHORITY) {
1276 /* allowed to be empty. */
1277 if(curlx_dyn_add(&host, ""))
1278 ures = CURLUE_OUT_OF_MEMORY;
1280 else
1281 ures = CURLUE_NO_HOST;
1283 if(!ures) {
1284 /* The path might at this point contain a fragment and/or a query to
1285 handle */
1286 const char *fragment = memchr(path, '#', pathlen);
1287 if(fragment) {
1288 size_t fraglen = pathlen - (fragment - path);
1289 ures = handle_fragment(u, fragment, fraglen, flags);
1290 /* after this, pathlen still contains the query */
1291 pathlen -= fraglen;
1294 if(!ures) {
1295 const char *query = memchr(path, '?', pathlen);
1296 if(query) {
1297 size_t qlen = pathlen - (query - path);
1298 ures = handle_query(u, query, qlen, flags);
1299 pathlen -= qlen;
1302 if(!ures)
1303 /* the fragment and query parts are trimmed off from the path */
1304 ures = handle_path(u, path, pathlen, flags, is_file);
1305 if(!ures) {
1306 u->host = curlx_dyn_ptr(&host);
1307 return CURLUE_OK;
1309fail:
1310 curlx_dyn_free(&host);
1311 free_urlhandle(u);
1312 return ures;
1316 * Parse the URL and, if successful, replace everything in the Curl_URL struct.
1317 */
1318static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
1319 unsigned int flags)
1321 CURLUcode ures;
1322 CURLU tmpurl;
1323 memset(&tmpurl, 0, sizeof(tmpurl));
1324 ures = parseurl(url, &tmpurl, flags);
1325 if(!ures) {
1326 free_urlhandle(u);
1327 *u = tmpurl;
1329 return ures;
1333 * Concatenate a relative URL onto a base URL making it absolute.
1334 */
1335static CURLUcode redirect_url(const char *base, const char *relurl,
1336 CURLU *u, unsigned int flags)
1338 struct dynbuf urlbuf;
1339 bool host_changed = FALSE;
1340 const char *useurl = relurl;
1341 const char *cutoff = NULL;
1342 size_t prelen;
1343 CURLUcode uc;
1344 /* this can get here with a NULL u->scheme only if asked to use the default
1345 scheme, so allow fallback to that */
1346 const char *scheme = u->scheme ? u->scheme : DEFAULT_SCHEME;
1348 /* protsep points to the start of the hostname, after [scheme]:// */
1349 const char *protsep = base + strlen(scheme) + 3;
1350 DEBUGASSERT(base && relurl && u); /* all set here */
1351 if(!base)
1352 return CURLUE_MALFORMED_INPUT; /* should never happen */
1354 /* handle different relative URL types */
1355 switch(relurl[0]) {
1356 case '/':
1357 if(relurl[1] == '/') {
1358 /* protocol-relative URL: //example.com/path */
1359 cutoff = protsep;
1360 useurl = &relurl[2];
1361 host_changed = TRUE;
1363 else
1364 /* absolute /path */
1365 cutoff = strchr(protsep, '/');
1366 break;
1368 case '#':
1369 /* fragment-only change */
1370 if(u->fragment_present)
1371 cutoff = strchr(protsep, '#');
1372 break;
1374 default:
1375 /* path or query-only change */
1376 if(u->query_present)
1377 /* remove existing query */
1378 cutoff = strchr(protsep, '?');
1379 else if(u->fragment_present)
1380 /* Remove existing fragment */
1381 cutoff = strchr(protsep, '#');
1383 if(relurl[0] != '?') {
1384 /* append a relative path after the last slash */
1385 cutoff = memrchr(protsep, '/',
1386 cutoff ? (size_t)(cutoff - protsep) : strlen(protsep));
1387 if(cutoff)
1388 cutoff++; /* truncate after last slash */
1390 break;
1393 prelen = cutoff ? (size_t)(cutoff - base) : strlen(base);
1395 /* build new URL */
1396 curlx_dyn_init(&urlbuf, CURL_MAX_INPUT_LENGTH);
1398 if(!curlx_dyn_addn(&urlbuf, base, prelen) &&
1399 !urlencode_str(&urlbuf, useurl, strlen(useurl), !host_changed,
1400 QUERY_NOT_YET)) {
1401 uc = parseurl_and_replace(curlx_dyn_ptr(&urlbuf), u,
1402 flags & ~U_CURLU_PATH_AS_IS);
1404 else
1405 uc = CURLUE_OUT_OF_MEMORY;
1407 curlx_dyn_free(&urlbuf);
1408 return uc;
1412 */
1413CURLU *curl_url(void)
1415 return curlx_calloc(1, sizeof(struct Curl_URL));
1418void curl_url_cleanup(CURLU *u)
1420 if(u) {
1421 free_urlhandle(u);
1422 curlx_free(u);
1426#define DUP(dest, src, name) \
1427 do { \
1428 if((src)->name) { \
1429 (dest)->name = curlx_strdup((src)->name); \
1430 if(!(dest)->name) \
1431 goto fail; \
1432 } \
1433 } while(0)
1435CURLU *curl_url_dup(const CURLU *in)
1437 struct Curl_URL *u;
1439 if(!in)
1440 return NULL;
1442 u = curlx_calloc(1, sizeof(struct Curl_URL));
1443 if(u) {
1444 DUP(u, in, scheme);
1445 DUP(u, in, user);
1446 DUP(u, in, password);
1447 DUP(u, in, options);
1448 DUP(u, in, host);
1449 DUP(u, in, path);
1450 DUP(u, in, query);
1451 DUP(u, in, fragment);
1452 DUP(u, in, zoneid);
1453 u->portnum = in->portnum;
1454 u->port_present = in->port_present;
1455 u->fragment_present = in->fragment_present;
1456 u->query_present = in->query_present;
1457 u->guessed_scheme = in->guessed_scheme;
1459 return u;
1460fail:
1461 curl_url_cleanup(u);
1462 return NULL;
1465#ifndef USE_IDN
1466#define host_decode(x, y) CURLUE_LACKS_IDN
1467#define host_encode(x, y) CURLUE_LACKS_IDN
1468#else
1469static CURLUcode host_decode(const char *host, char **allochost)
1471 CURLcode result = Curl_idn_decode(host, allochost);
1472 if(result)
1473 return (result == CURLE_OUT_OF_MEMORY) ?
1474 CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
1475 return CURLUE_OK;
1478static CURLUcode host_encode(const char *host, char **allochost)
1480 CURLcode result = Curl_idn_encode(host, allochost);
1481 if(result)
1482 return (result == CURLE_OUT_OF_MEMORY) ?
1483 CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
1484 return CURLUE_OK;
1486#endif
1488static CURLUcode urlget_format(const CURLU *u, CURLUPart what,
1489 const char *ptr, char **partp,
1490 bool plusdecode, unsigned int flags)
1492 CURLUcode uc = CURLUE_OK;
1493 size_t partlen = strlen(ptr);
1494 bool urldecode = (flags & CURLU_URLDECODE) ? 1 : 0;
1495 bool urlencode = (flags & CURLU_URLENCODE) ? 1 : 0;
1496 bool punycode = (flags & CURLU_PUNYCODE) && (what == CURLUPART_HOST);
1497 bool depunyfy = (flags & CURLU_PUNY2IDN) && (what == CURLUPART_HOST);
1498 char *part = curlx_memdup0(ptr, partlen);
1499 *partp = NULL;
1500 if(!part)
1501 return CURLUE_OUT_OF_MEMORY;
1502 if(plusdecode) {
1503 /* convert + to space */
1504 char *plus = part;
1505 size_t i = 0;
1506 for(i = 0; i < partlen; ++plus, i++) {
1507 if(*plus == '+')
1508 *plus = ' ';
1511 if(urldecode) {
1512 char *decoded;
1513 size_t dlen;
1514 /* this unconditional rejection of control bytes is documented API
1515 behavior */
1516 CURLcode result = Curl_urldecode(part, partlen, &decoded, &dlen,
1517 REJECT_CTRL);
1518 curlx_free(part);
1519 if(result)
1520 return CURLUE_URLDECODE;
1521 part = decoded;
1522 partlen = dlen;
1524 if(urlencode && partlen) {
1525 struct dynbuf enc;
1526 curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
1527 uc = urlencode_str(&enc, part, partlen, TRUE, what == CURLUPART_QUERY ?
1528 QUERY_YES : QUERY_NO);
1529 curlx_free(part);
1530 if(uc)
1531 return uc;
1532 part = curlx_dyn_ptr(&enc);
1534 else if(punycode) {
1535 if(!Curl_is_ASCII_name(u->host)) {
1536 char *punyversion = NULL;
1537 uc = host_decode(part, &punyversion);
1538 curlx_free(part);
1539 if(uc)
1540 return uc;
1541 uc = Curl_hostname_check(punyversion, strlen(punyversion));
1542 if(uc) {
1543 curlx_free(punyversion);
1544 return uc;
1546 part = punyversion;
1549 else if(depunyfy && Curl_is_ASCII_name(u->host)) {
1550 char *unpunified = NULL;
1551 uc = host_encode(part, &unpunified);
1552 curlx_free(part);
1553 if(uc)
1554 return uc;
1555 part = unpunified;
1557 *partp = part;
1558 return CURLUE_OK;
1561static CURLUcode file_url(const CURLU *u, char **part,
1562 const char *fragmentsep,
1563 const char *querysep)
1565 char *url = curl_maprintf("file://%s%s%s%s%s",
1566 u->path ? u->path : "/",
1567 querysep, u->query ? u->query : "",
1568 fragmentsep, u->fragment ? u->fragment : "");
1569 if(!url)
1570 return CURLUE_OUT_OF_MEMORY;
1572 *part = url;
1573 return CURLUE_OK;
1576static CURLUcode urlget_url(const CURLU *u, char **part, unsigned int flags)
1578 char *url;
1579 char *allochost = NULL;
1580 const char *fragmentsep =
1581 (u->fragment || (u->fragment_present && flags & CURLU_GET_EMPTY)) ?
1582 "#" : "";
1583 const char *querysep = ((u->query && u->query[0]) ||
1584 (u->query_present && flags & CURLU_GET_EMPTY)) ?
1585 "?" : "";
1586 char portbuf[7];
1587 if(curl_strequal("file", u->scheme))
1588 return file_url(u, part, fragmentsep, querysep);
1589 else if(!u->host)
1590 return CURLUE_NO_HOST;
1591 else {
1592 const char *scheme;
1593 char *options = u->options;
1594 char *port = NULL;
1595 const struct Curl_scheme *h = NULL;
1596 char schemebuf[MAX_SCHEME_LEN + 5];
1597 if(u->scheme)
1598 scheme = u->scheme;
1599 else if(flags & CURLU_DEFAULT_SCHEME)
1600 scheme = DEFAULT_SCHEME;
1601 else
1602 return CURLUE_NO_SCHEME;
1604 if(u->port_present) {
1605 curl_msnprintf(portbuf, sizeof(portbuf), "%u", u->portnum);
1606 port = portbuf;
1609 h = Curl_get_scheme(scheme);
1610 if(h) {
1611 if(!u->port_present && (flags & CURLU_DEFAULT_PORT)) {
1612 /* there is no stored port number, but asked to deliver a default one
1613 for the scheme */
1614 curl_msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
1615 port = portbuf;
1617 else if(u->port_present && (h->defport == u->portnum) &&
1618 (flags & CURLU_NO_DEFAULT_PORT)) {
1619 /* there is a stored port number, but asked to inhibit if it matches
1620 the default port for the scheme */
1621 port = NULL;
1624 if(!(h->flags & PROTOPT_URLOPTIONS))
1625 options = NULL;
1628 if(u->host[0] == '[') {
1629 if(u->zoneid) {
1630 /* make it '[ host %25 zoneid ]' */
1631 struct dynbuf enc;
1632 size_t hostlen = strlen(u->host);
1633 curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
1634 if(curlx_dyn_addf(&enc, "%.*s%%25%s]", (int)hostlen - 1, u->host,
1635 u->zoneid))
1636 return CURLUE_OUT_OF_MEMORY;
1637 allochost = curlx_dyn_ptr(&enc);
1640 else if(flags & CURLU_URLENCODE) {
1641 allochost = curl_easy_escape(NULL, u->host, 0);
1642 if(!allochost)
1643 return CURLUE_OUT_OF_MEMORY;
1645 else if(flags & CURLU_PUNYCODE) {
1646 if(!Curl_is_ASCII_name(u->host)) {
1647 CURLUcode ret = host_decode(u->host, &allochost);
1648 if(!ret)
1649 ret = Curl_hostname_check(allochost, strlen(allochost));
1650 if(ret) {
1651 curlx_safefree(allochost);
1652 return ret;
1656 else if(flags & CURLU_PUNY2IDN) {
1657 if(Curl_is_ASCII_name(u->host)) {
1658 CURLUcode ret = host_encode(u->host, &allochost);
1659 if(ret)
1660 return ret;
1664 if(!(flags & CURLU_NO_GUESS_SCHEME) || !u->guessed_scheme)
1665 curl_msnprintf(schemebuf, sizeof(schemebuf), "%s://", scheme);
1666 else
1667 schemebuf[0] = 0;
1669 url = curl_maprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1670 schemebuf,
1671 u->user ? u->user : "",
1672 u->password ? ":" : "",
1673 u->password ? u->password : "",
1674 options ? ";" : "",
1675 options ? options : "",
1676 (u->user || u->password || options) ? "@" : "",
1677 allochost ? allochost : u->host,
1678 port ? ":" : "",
1679 port ? port : "",
1680 u->path ? u->path : "/",
1681 querysep,
1682 u->query ? u->query : "",
1683 fragmentsep,
1684 u->fragment ? u->fragment : "");
1685 curlx_free(allochost);
1687 if(!url)
1688 return CURLUE_OUT_OF_MEMORY;
1689 *part = url;
1690 return CURLUE_OK;
1693CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
1694 char **part, unsigned int flags)
1696 const char *ptr;
1697 CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
1698 char portbuf[7];
1699 bool plusdecode = FALSE;
1700 if(!u)
1701 return CURLUE_BAD_HANDLE;
1702 if(!part)
1703 return CURLUE_BAD_PARTPOINTER;
1704 *part = NULL;
1706 switch(what) {
1707 case CURLUPART_SCHEME:
1708 ptr = u->scheme;
1709 ifmissing = CURLUE_NO_SCHEME;
1710 flags &= ~U_CURLU_URLDECODE; /* never for schemes */
1711 if((flags & CURLU_NO_GUESS_SCHEME) && u->guessed_scheme)
1712 return CURLUE_NO_SCHEME;
1713 break;
1714 case CURLUPART_USER:
1715 ptr = u->user;
1716 ifmissing = CURLUE_NO_USER;
1717 break;
1718 case CURLUPART_PASSWORD:
1719 ptr = u->password;
1720 ifmissing = CURLUE_NO_PASSWORD;
1721 break;
1722 case CURLUPART_OPTIONS:
1723 ptr = u->options;
1724 ifmissing = CURLUE_NO_OPTIONS;
1725 break;
1726 case CURLUPART_HOST:
1727 ptr = u->host;
1728 ifmissing = CURLUE_NO_HOST;
1729 break;
1730 case CURLUPART_ZONEID:
1731 ptr = u->zoneid;
1732 ifmissing = CURLUE_NO_ZONEID;
1733 break;
1734 case CURLUPART_PORT:
1735 ptr = NULL;
1736 ifmissing = CURLUE_NO_PORT;
1737 flags &= ~U_CURLU_URLDECODE; /* never for port */
1738 if(u->port_present) {
1739 const struct Curl_scheme *h = u->scheme ?
1740 Curl_get_scheme(u->scheme) : NULL;
1741 /* there is a stored port number, but ask to inhibit if
1742 it matches the default one for the scheme */
1743 if(h && (h->defport == u->portnum) &&
1744 (flags & CURLU_NO_DEFAULT_PORT)) {
1745 ptr = NULL;
1747 else {
1748 curl_msnprintf(portbuf, sizeof(portbuf), "%u", u->portnum);
1749 ptr = portbuf;
1752 else if((flags & CURLU_DEFAULT_PORT) && u->scheme) {
1753 /* there is no stored port number, but asked to deliver
1754 a default one for the scheme */
1755 const struct Curl_scheme *h = Curl_get_scheme(u->scheme);
1756 if(h) {
1757 curl_msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
1758 ptr = portbuf;
1761 break;
1762 case CURLUPART_PATH:
1763 ptr = u->path;
1764 if(!ptr)
1765 ptr = "/";
1766 break;
1767 case CURLUPART_QUERY:
1768 ptr = u->query;
1769 ifmissing = CURLUE_NO_QUERY;
1770 plusdecode = flags & CURLU_URLDECODE;
1771 if(ptr && !ptr[0] && !(flags & CURLU_GET_EMPTY))
1772 /* there was a blank query and the user does not ask for it */
1773 ptr = NULL;
1774 break;
1775 case CURLUPART_FRAGMENT:
1776 ptr = u->fragment;
1777 ifmissing = CURLUE_NO_FRAGMENT;
1778 if(!ptr && u->fragment_present && flags & CURLU_GET_EMPTY)
1779 /* there was a blank fragment and the user asks for it */
1780 ptr = "";
1781 break;
1782 case CURLUPART_URL:
1783 return urlget_url(u, part, flags);
1784 default:
1785 ptr = NULL;
1786 break;
1788 if(ptr)
1789 return urlget_format(u, what, ptr, part, plusdecode, flags);
1791 return ifmissing;
1794static CURLUcode set_url_scheme(CURLU *u, const char *scheme,
1795 unsigned int flags)
1797 size_t plen = strlen(scheme);
1798 const struct Curl_scheme *h = NULL;
1799 if((plen > MAX_SCHEME_LEN) || (plen < 1))
1800 /* too long or too short */
1801 return CURLUE_BAD_SCHEME;
1802 /* verify that it is a fine scheme */
1803 h = Curl_get_scheme(scheme);
1804 if(!(flags & CURLU_NON_SUPPORT_SCHEME) && (!h || !h->run))
1805 return CURLUE_UNSUPPORTED_SCHEME;
1806 if(!h) {
1807 const char *s = scheme;
1808 if(ISALPHA(*s)) {
1809 /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
1810 s++;
1811 while(--plen) {
1812 if(ISALNUM(*s) || (*s == '+') || (*s == '-') || (*s == '.'))
1813 s++; /* fine */
1814 else
1815 return CURLUE_BAD_SCHEME;
1818 else
1819 return CURLUE_BAD_SCHEME;
1821 u->guessed_scheme = FALSE;
1822 return CURLUE_OK;
1825static CURLUcode set_url_port(CURLU *u, const char *provided_port)
1827 curl_off_t port;
1828 if(!ISDIGIT(provided_port[0]))
1829 /* not a number */
1830 return CURLUE_BAD_PORT_NUMBER;
1831 if(curlx_str_number(&provided_port, &port, 0xffff) || *provided_port)
1832 /* weirdly provided number, not good! */
1833 return CURLUE_BAD_PORT_NUMBER;
1834 u->portnum = (uint16_t)port;
1835 u->port_present = TRUE;
1836 return CURLUE_OK;
1839static CURLUcode set_url(CURLU *u, const char *url, size_t part_size,
1840 unsigned int flags)
1842 /*
1843 * Allow a new URL to replace the existing (if any) contents.
1845 * If the existing contents is enough for a URL, allow a relative URL to
1846 * replace it.
1847 */
1848 CURLUcode uc;
1849 char *oldurl = NULL;
1851 if(!part_size) {
1852 /* a blank URL is not a valid URL unless we already have a complete one
1853 and this is a redirect */
1854 uc = curl_url_get(u, CURLUPART_URL, &oldurl, flags);
1855 if(!uc) {
1856 /* success, meaning the "" is a fine relative URL, and the new URL
1857 inherits scheme/authority/path/query, but not fragment, from the
1858 existing URL (RFC 3986 section 5.2.2) */
1859 curlx_safefree(u->fragment);
1860 u->fragment_present = FALSE;
1861 curlx_free(oldurl);
1862 return CURLUE_OK;
1864 if(uc == CURLUE_OUT_OF_MEMORY)
1865 return uc;
1866 return CURLUE_MALFORMED_INPUT;
1869 /* if the new URL is absolute replace the existing with the new. */
1870 if(Curl_is_absolute_url(url, NULL, 0,
1871 flags & (CURLU_GUESS_SCHEME | CURLU_DEFAULT_SCHEME)))
1872 return parseurl_and_replace(url, u, flags);
1874 /* if the old URL is incomplete (we cannot get an absolute URL in
1875 'oldurl'), replace the existing with the new.
1876 Always include "scheme://" to make the URL "complete" */
1877 /* Preserve empty query/fragment separators: they affect where relative
1878 references splice into the base URL. */
1879 uc = curl_url_get(u, CURLUPART_URL, &oldurl,
1880 (flags & ~CURLU_NO_GUESS_SCHEME) | CURLU_GET_EMPTY);
1881 if(uc == CURLUE_OUT_OF_MEMORY)
1882 return uc;
1883 else if(uc)
1884 return parseurl_and_replace(url, u, flags);
1886 DEBUGASSERT(oldurl); /* it is set here */
1887 /* apply the relative part to create a new URL */
1888 uc = redirect_url(oldurl, url, u, flags);
1889 curlx_free(oldurl);
1890 return uc;
1893static CURLUcode urlset_clear(CURLU *u, CURLUPart what)
1895 switch(what) {
1896 case CURLUPART_URL:
1897 free_urlhandle(u);
1898 memset(u, 0, sizeof(struct Curl_URL));
1899 break;
1900 case CURLUPART_SCHEME:
1901 curlx_safefree(u->scheme);
1902 u->guessed_scheme = FALSE;
1903 break;
1904 case CURLUPART_USER:
1905 curlx_safefree(u->user);
1906 break;
1907 case CURLUPART_PASSWORD:
1908 curlx_strzero(u->password);
1909 curlx_safefree(u->password);
1910 break;
1911 case CURLUPART_OPTIONS:
1912 curlx_safefree(u->options);
1913 break;
1914 case CURLUPART_HOST:
1915 curlx_safefree(u->host);
1916 break;
1917 case CURLUPART_ZONEID:
1918 curlx_safefree(u->zoneid);
1919 break;
1920 case CURLUPART_PORT:
1921 u->portnum = 0;
1922 u->port_present = FALSE;
1923 break;
1924 case CURLUPART_PATH:
1925 curlx_safefree(u->path);
1926 break;
1927 case CURLUPART_QUERY:
1928 curlx_safefree(u->query);
1929 u->query_present = FALSE;
1930 break;
1931 case CURLUPART_FRAGMENT:
1932 curlx_safefree(u->fragment);
1933 u->fragment_present = FALSE;
1934 break;
1935 default:
1936 return CURLUE_UNKNOWN_PART;
1938 return CURLUE_OK;
1941static bool allowed_in_path(unsigned char x)
1943 switch(x) {
1944 case '!':
1945 case '$':
1946 case '&':
1947 case '\'':
1948 case '(':
1949 case ')':
1950 case '{':
1951 case '}':
1952 case '[':
1953 case ']':
1954 case '*':
1955 case '+':
1956 case ',':
1957 case ';':
1958 case '=':
1959 case ':':
1960 case '@':
1961 case '/':
1962 return TRUE;
1964 return FALSE;
1967static CURLUcode url_encode_part(struct dynbuf *encp,
1968 const char *part,
1969 bool plusencode,
1970 bool pathmode,
1971 bool equalsencode)
1973 const unsigned char *i;
1975 for(i = (const unsigned char *)part; *i; i++) {
1976 CURLcode result;
1977 if((*i == ' ') && plusencode)
1978 result = curlx_dyn_addn(encp, "+", 1);
1979 else if(ISUNRESERVED(*i) ||
1980 (pathmode && allowed_in_path(*i)) ||
1981 ((*i == '=') && equalsencode)) {
1982 if((*i == '=') && equalsencode)
1983 /* only skip the first equals sign */
1984 equalsencode = FALSE;
1985 result = curlx_dyn_addn(encp, i, 1);
1987 else {
1988 unsigned char out[3] = { '%' };
1989 Curl_hexbyte(&out[1], *i);
1990 result = curlx_dyn_addn(encp, out, 3);
1992 if(result)
1993 return cc2cu(result);
1995 return CURLUE_OK;
1998static CURLUcode url_uppercasehex_part(struct dynbuf *encp,
1999 const char *part)
2001 char *p;
2002 CURLcode result = curlx_dyn_add(encp, part);
2003 if(result)
2004 return cc2cu(result);
2005 p = curlx_dyn_ptr(encp);
2006 while(*p) {
2007 /* make sure percent encoded are upper case */
2008 if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
2009 (ISLOWER(p[1]) || ISLOWER(p[2]))) {
2010 p[1] = Curl_raw_toupper(p[1]);
2011 p[2] = Curl_raw_toupper(p[2]);
2012 p += 3;
2014 else
2015 p++;
2017 return CURLUE_OK;
2020static CURLUcode url_append_query(CURLU *u, struct dynbuf *encp)
2022 /* Append the 'encp' string onto the old query. Add a '&' separator if none
2023 is already present at the end of the existing query */
2025 size_t querylen = u->query ? strlen(u->query) : 0;
2026 bool addamperand = querylen && (u->query[querylen - 1] != '&');
2027 if(querylen) {
2028 struct dynbuf qbuf;
2029 CURLcode result;
2030 const char *newp = curlx_dyn_ptr(encp);
2031 curlx_dyn_init(&qbuf, CURL_MAX_INPUT_LENGTH);
2033 /* add original query */
2034 result = curlx_dyn_addn(&qbuf, u->query, querylen);
2035 if(!result && addamperand)
2036 /* add ampersand */
2037 result = curlx_dyn_addn(&qbuf, "&", 1);
2038 if(!result)
2039 /* add new query part */
2040 result = curlx_dyn_add(&qbuf, newp);
2041 if(result)
2042 goto nomem;
2043 curlx_dyn_free(encp);
2044 curlx_free(u->query);
2045 u->query = curlx_dyn_ptr(&qbuf);
2046 return CURLUE_OK;
2047nomem:
2048 curlx_dyn_free(encp);
2049 return cc2cu(result);
2051 else {
2052 curlx_free(u->query);
2053 u->query = curlx_dyn_ptr(encp);
2055 return CURLUE_OK;
2058static CURLUcode url_sethost(CURLU *u, struct dynbuf *encp,
2059 bool urlencode,
2060 unsigned int flags)
2062 size_t n = curlx_dyn_len(encp);
2063 bool bad = FALSE;
2064 char *newp = curlx_dyn_ptr(encp);
2065 if(!n)
2066 /* an empty hostname is okay if told so */
2067 bad = (flags & CURLU_NO_AUTHORITY) ? FALSE : TRUE;
2068 else if(!urlencode) {
2069 /* if the hostname part was not URL encoded here, it was set already URL
2070 encoded so we need to decode it to check */
2071 size_t dlen;
2072 char *decoded = NULL;
2073 CURLcode result = Curl_urldecode(newp, n, &decoded, &dlen, REJECT_CTRL);
2074 if(result || hostname_check6(u, decoded, dlen))
2075 bad = TRUE;
2076 else if(decoded[0] == '[') {
2077 /* store the normalized IPv6 address, the zone id is kept separately */
2078 curlx_dyn_reset(encp);
2079 if(curlx_dyn_add(encp, decoded))
2080 bad = TRUE;
2082 else {
2083 struct dynbuf dbuf;
2084 curlx_dyn_init(&dbuf, CURL_MAX_INPUT_LENGTH);
2085 if(curlx_dyn_addn(&dbuf, decoded, dlen))
2086 bad = TRUE;
2087 else if(ipv4_normalize(&dbuf) == HOST_IPV4) {
2088 curlx_dyn_reset(encp);
2089 if(curlx_dyn_addn(encp, curlx_dyn_ptr(&dbuf), curlx_dyn_len(&dbuf)))
2090 bad = TRUE;
2092 curlx_dyn_free(&dbuf);
2094 curlx_free(decoded);
2096 else if(hostname_check6(u, newp, n))
2097 bad = TRUE;
2098 else if(ipv4_normalize(encp) == HOST_ERROR)
2099 bad = TRUE;
2100 if(bad) {
2101 curlx_dyn_free(encp);
2102 return CURLUE_BAD_HOSTNAME;
2104 return CURLUE_OK;
2107CURLUcode curl_url_set(CURLU *u, CURLUPart what,
2108 const char *part, unsigned int flags)
2110 char **storep = NULL;
2111 bool urlencode = (flags & CURLU_URLENCODE) ? 1 : 0;
2112 bool plusencode = FALSE;
2113 bool pathmode = FALSE;
2114 bool leadingslash = FALSE;
2115 bool appendquery = FALSE;
2116 bool equalsencode = FALSE;
2117 size_t nalloc;
2119 if(!u)
2120 return CURLUE_BAD_HANDLE;
2121 if(!part)
2122 /* setting a part to NULL clears it */
2123 return urlset_clear(u, what);
2125 nalloc = strlen(part);
2126 if(nalloc > CURL_MAX_INPUT_LENGTH)
2127 /* excessive input length */
2128 return CURLUE_MALFORMED_INPUT;
2130 switch(what) {
2131 case CURLUPART_SCHEME: {
2132 CURLUcode status = set_url_scheme(u, part, flags);
2133 if(status)
2134 return status;
2135 storep = &u->scheme;
2136 urlencode = FALSE; /* never */
2137 break;
2139 case CURLUPART_USER:
2140 storep = &u->user;
2141 break;
2142 case CURLUPART_PASSWORD:
2143 storep = &u->password;
2144 break;
2145 case CURLUPART_OPTIONS:
2146 storep = &u->options;
2147 break;
2148 case CURLUPART_HOST:
2149 storep = &u->host;
2150 curlx_safefree(u->zoneid);
2151 break;
2152 case CURLUPART_ZONEID:
2153 storep = &u->zoneid;
2154 break;
2155 case CURLUPART_PORT:
2156 return set_url_port(u, part);
2157 case CURLUPART_PATH:
2158 pathmode = TRUE;
2159 leadingslash = TRUE; /* enforce */
2160 storep = &u->path;
2161 break;
2162 case CURLUPART_QUERY:
2163 plusencode = urlencode;
2164 appendquery = (flags & CURLU_APPENDQUERY) ? 1 : 0;
2165 equalsencode = appendquery;
2166 storep = &u->query;
2167 u->query_present = TRUE;
2168 break;
2169 case CURLUPART_FRAGMENT:
2170 storep = &u->fragment;
2171 u->fragment_present = TRUE;
2172 break;
2173 case CURLUPART_URL:
2174 return set_url(u, part, nalloc, flags);
2175 default:
2176 return CURLUE_UNKNOWN_PART;
2178 DEBUGASSERT(storep);
2180 const char *newp = NULL;
2181 struct dynbuf enc;
2182 CURLUcode status;
2183 curlx_dyn_init(&enc, (what == CURLUPART_HOST) ?
2184 CURLMAX((nalloc * 3) + 1 + leadingslash, 16) :
2185 (nalloc * 3) + 1 + leadingslash);
2187 if(leadingslash && (part[0] != '/')) {
2188 CURLcode result = curlx_dyn_addn(&enc, "/", 1);
2189 if(result)
2190 return cc2cu(result);
2192 if(urlencode)
2193 status = url_encode_part(&enc, part, plusencode, pathmode, equalsencode);
2194 else
2195 status = url_uppercasehex_part(&enc, part);
2196 if(!status) {
2197 newp = curlx_dyn_ptr(&enc);
2199 if(appendquery && newp)
2200 return url_append_query(u, &enc);
2201 else if(what == CURLUPART_HOST)
2202 status = url_sethost(u, &enc, urlencode, flags);
2204 if(status)
2205 return status;
2207 if(what == CURLUPART_PASSWORD)
2208 curlx_strzero(*storep);
2209 curlx_free(*storep);
2210 *storep = (char *)CURL_UNCONST(newp);
2212 return CURLUE_OK;
2215bool Curl_url_same_origin(CURLU *base, CURLU *href)
2217 const struct Curl_scheme *s = NULL;
2219 /* base must be an absolute URL */
2220 if(!base->scheme || !base->host)
2221 return FALSE;
2222 if(href->scheme && !curl_strequal(base->scheme, href->scheme))
2223 return FALSE;
2224 if(href->host) {
2225 if(!curl_strequal(base->host, href->host))
2226 return FALSE;
2228 if(base->port_present != href->port_present) {
2229 /* one is present, one is not */
2230 s = Curl_get_scheme(base->scheme);
2231 if(!s) /* Cannot match default port for unknown scheme */
2232 return FALSE;
2233 /* to match, the present one must be the default port */
2234 if((base->port_present && (base->portnum != s->defport)) ||
2235 (href->port_present && (href->portnum != s->defport)))
2236 return FALSE;
2238 else if(base->portnum != href->portnum) /* both present or missing */
2239 return FALSE;
2241 if(!curl_strequal(base->zoneid ? base->zoneid : "",
2242 href->zoneid ? href->zoneid : ""))
2243 return FALSE;
2245 else if(href->port_present) /* no host in href, then there must be no port */
2246 return FALSE;
2247 return TRUE;
2250CURLUcode Curl_url_get_port(CURLU *u, uint16_t *pport)
2252 if(u->port_present) {
2253 *pport = u->portnum;
2254 return CURLUE_OK;
2256 else if(u->scheme) {
2257 const struct Curl_scheme *s = Curl_get_scheme(u->scheme);
2258 if(s && s->defport) {
2259 *pport = s->defport;
2260 return CURLUE_OK;
2263 *pport = 0;
2264 return CURLUE_NO_PORT;
Diagnostics4 git calls · 32 ms
CallStatusSizeTime032 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=26
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20019 KiB · hit27 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=19055
git.response.cache=HIT
git.response.duration_ms=27
git.fetch.wants=51dab877aa056535a57f4d742195091387cc2905
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=19055
git.pkt.payload.max=19034
git.sideband.band1.bytes=19033
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e280fe4f7b0555
Request
b6728f26…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format