Coverage Report

Created: 2025-10-10 06:31

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