url.c 51.6 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25
/**@CFILE url.c
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35
 *
 * Implementation of basic URL parsing and handling.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Thu Jun 29 22:44:37 2000 ppessi
 */

#include "config.h"

36 37
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/bnf.h>
38
#include <sofia-sip/hostdomain.h>
39
#include <sofia-sip/url.h>
Pekka Pessi's avatar
Pekka Pessi committed
40

41 42
#include <sofia-sip/string0.h>

Youness Alaoui's avatar
Youness Alaoui committed
43
#include <stdio.h>
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
48
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
49

Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55
/**@def URL_PRINT_FORMAT
 * Format string used when printing url with printf().
 *
 * The macro URL_PRINT_FORMAT is used in format string of printf() or
 * similar printing functions.  A URL can be printed like this:
 * @code
Pekka Pessi's avatar
Pekka Pessi committed
56
 *   printf("%s received URL " URL_PRINT_FORMAT "\n",
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63 64 65 66 67
 *          my_name, URL_PRINT_ARGS(url));
 * @endcode
 */

/** @def URL_PRINT_ARGS(u)
 * Argument list used when printing url with printf().
 *
 * The macro URL_PRINT_ARGS() is used to create a stdarg list for printf()
 * or similar printing functions.  Using it, a URL can be printed like this:
 *
 * @code
Pekka Pessi's avatar
Pekka Pessi committed
68
 *   printf("%s received URL " URL_PRINT_FORMAT "\n",
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 *          my_name, URL_PRINT_ARGS(url));
 * @endcode
 */

#define RESERVED        ";/?:@&=+$,"
#define DELIMS          "<>#%\""
#define UNWISE		"{}|\\^[]`"

#define EXCLUDED	RESERVED DELIMS UNWISE

#define UNRESERVED    	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
                      	"abcdefghijklmnopqrstuvwxyz" \
                      	"0123456789" \
                      	"-_.!~*'()"

#define IS_EXCLUDED(u, m32, m64, m96)			\
  (u <= ' '						\
   || u >= '\177'					\
   || (u < 64 ? (m32 & (1 << (63 - u)))			\
       : (u < 96 ? (m64 & (1 << (95 - u)))		\
89
	  : /*u < 128*/ (m96 & (1 << (127 - u))))) != 0)
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

#define MASKS_WITH_RESERVED(reserved, m32, m64, m96)		\
  if (reserved == NULL) {					\
    m32 = 0xbe19003f, m64 = 0x8000001e, m96 = 0x8000001d;	\
  } else do {							\
    m32 = 0xb400000a, m64 = 0x0000001e, m96 = 0x8000001d;	\
    								\
    for (;reserved[0]; reserved++) {				\
      unsigned r = reserved[0];					\
      RESERVE(r, m32, m64, m96);				\
    }								\
  } while (0)

#define RESERVE(reserved, m32, m64, m96)				\
  if (r < 32)								\
    ;									\
  else if (r < 64)							\
    m32 |= 1U << (63 - r);						\
  else if (r < 96)							\
    m64 |= 1U << (95 - r);						\
  else if (r < 128)							\
    m96 |= 1U << (127 - r)

#define MASKS_WITH_ALLOWED(allowed, mask32, mask64, mask96)	\
  do {								\
    if (allowed) {						\
      for (;allowed[0]; allowed++) {				\
	unsigned a = allowed[0];				\
	ALLOW(a, mask32, mask64, mask96);			\
      }								\
    }								\
  } while (0)

#define ALLOW(a, mask32, mask64, mask96)	\
  if (a < 32)					\
    ;						\
  else if (a < 64)				\
    mask32 &= ~(1U << (63 - a));		\
  else if (a < 96)				\
    mask64 &= ~(1U << (95 - a));		\
  else if (a < 128)				\
    mask96 &= ~(1U << (127 - a))

133 134
#define NUL '\0'
#define NULNULNUL '\0', '\0', '\0'
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139

#define RMASK1 0xbe19003f
#define RMASK2 0x8000001e
#define RMASK3 0x8000001d

Pekka Pessi's avatar
Pekka Pessi committed
140 141 142 143 144
#define RESERVED_MASK 0xbe19003f, 0x8000001e, 0x8000001d
#define URIC_MASK     0xb400000a, 0x0000001e, 0x8000001d

#define IS_EXCLUDED_MASK(u, m) IS_EXCLUDED(u, m)

Pekka Pessi's avatar
Pekka Pessi committed
145
/* Internal prototypes */
146
static char *url_canonize(char *d, char const *s, size_t n,
147
			  unsigned syn33,
148
			  char const allowed[]);
Pekka Pessi's avatar
Pekka Pessi committed
149
static char *url_canonize2(char *d, char const *s, size_t n,
150
			   unsigned syn33,
Pekka Pessi's avatar
Pekka Pessi committed
151 152 153
			   unsigned m32, unsigned m64, unsigned m96);
static int url_tel_cmp_numbers(char const *A, char const *B);

Pekka Pessi's avatar
Pekka Pessi committed
154 155
/**Test if string contains excluded or url-reserved characters.
 *
156
 *
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160
 *
 * @param s  string to be searched
 *
 * @retval 0 if no reserved characters were found.
161
 * @retval l if a reserved character was found.
Pekka Pessi's avatar
Pekka Pessi committed
162 163 164
 */
int url_reserved_p(char const *s)
{
Pekka Pessi's avatar
Pekka Pessi committed
165
  if (s)
Pekka Pessi's avatar
Pekka Pessi committed
166 167 168
    while (*s) {
      unsigned char u = *s++;

Pekka Pessi's avatar
Pekka Pessi committed
169
      if (IS_EXCLUDED(u, RMASK1, RMASK2, RMASK3))
Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175
	return 1;
    }

  return 0;
}

176
/** Calculate length of string when escaped with %-notation.
Pekka Pessi's avatar
Pekka Pessi committed
177
 *
178 179
 * Calculate the length of string @a s when the excluded or reserved
 * characters in it have been escaped.
Pekka Pessi's avatar
Pekka Pessi committed
180
 *
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183
 * @param s         String with reserved URL characters. [IN
 * @param reserved  Optional array of reserved characters [IN]
 *
Pekka Pessi's avatar
Pekka Pessi committed
184
 * @return
185
 * The number of characters in corresponding but escaped string.
Pekka Pessi's avatar
Pekka Pessi committed
186 187
 *
 * You can handle a part of URL with reserved characters like this:
188
 * @code
Pekka Pessi's avatar
Pekka Pessi committed
189 190 191 192 193 194 195 196 197
 * if (url_reserved_p(s))  {
 *   n = malloc(url_esclen(s, NULL) + 1);
 *   if (n) url_escape(n, s);
 * } else {
 *   n = malloc(strlen(s) + 1);
 *   if (n) strcpy(n, s);
 * }
 * @endcode
 */
198
isize_t url_esclen(char const *s, char const reserved[])
Pekka Pessi's avatar
Pekka Pessi committed
199
{
200
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204 205 206 207 208 209 210 211
  unsigned mask32, mask64, mask96;

  MASKS_WITH_RESERVED(reserved, mask32, mask64, mask96);

  for (n = 0; s && *s; n++) {
    unsigned char u = *s++;

    if (IS_EXCLUDED(u, mask32, mask64, mask96))
      n += 2;
  }

212
  return (isize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

/** Escape a string.
 *
 * The function url_escape() copies the string pointed by @a s to the array
 * pointed by @a d, @b excluding the terminating \\0 character.  All reserved
 * characters in @a s are copied in hexadecimal format, for instance, @c
 * "$%#" is copied as @c "%24%25%23".  The destination array @a d must be
 * large enough to receive the escaped copy.
 *
 * @param d         Destination buffer [OUT]
 * @param s         String to be copied [IN]
 * @param reserved  Array of reserved characters [IN]
 *
 * @return Pointer to the destination array.
 */
char *url_escape(char *d, char const *s, char const reserved[])
{
  char *retval = d;
  unsigned mask32, mask64, mask96;
Pekka Pessi's avatar
Pekka Pessi committed
233

Pekka Pessi's avatar
Pekka Pessi committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  MASKS_WITH_RESERVED(reserved, mask32, mask64, mask96);

  while (s && *s) {
    unsigned char u = *s++;

    if (IS_EXCLUDED(u, mask32, mask64, mask96)) {
#     define URL_HEXIFY(u) ((u) + '0' + ((u) >= 10 ? 'A' - '0' - 10 : 0))

      *d++ = '%';
      *d++ = URL_HEXIFY(u >> 4);
      *d++ = URL_HEXIFY(u & 15);

#     undef URL_HEXIFY
    }
    else {
      *d++ = u;
    }
  }

  *d = '\0';
Pekka Pessi's avatar
Pekka Pessi committed
254

Pekka Pessi's avatar
Pekka Pessi committed
255 256 257 258
  return retval;
}


259
/**Unescape url-escaped string fragment.
Pekka Pessi's avatar
Pekka Pessi committed
260
 *
261 262 263 264 265
 * Unescape @a n characters from string @a s to the buffer @a d, including
 * the terminating \\0 character. All %-escaped triplets in @a s are
 * unescaped, for instance, @c "%40%25%23" is copied as @c "@%#". The
 * destination array @a d must be large enough to receive the escaped copy
 * (@a n bytes is always enough).
Pekka Pessi's avatar
Pekka Pessi committed
266 267
 *
 * @param d  destination buffer
268 269
 * @param s  string to be unescaped
 * @param n  maximum number of characters to unescape
Pekka Pessi's avatar
Pekka Pessi committed
270
 *
271
 * @return Length of unescaped string
272 273
 *
 * @NEW_1_12_4.
Pekka Pessi's avatar
Pekka Pessi committed
274
 */
275
size_t url_unescape_to(char *d, char const *s, size_t n)
Pekka Pessi's avatar
Pekka Pessi committed
276
{
277
  size_t i = 0, j = 0;
Pekka Pessi's avatar
Pekka Pessi committed
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  if (s == NULL)
    return 0;

  i = j = strncspn(s, n, "%");

  if (d && d != s)
    memmove(d, s, i);

  for (; i < n;) {
    char c = s[i++];

    if (c == '\0')
      break;

    if (c == '%' && i + 1 < n && IS_HEX(s[i]) && IS_HEX(s[i + 1])) {
Pekka Pessi's avatar
Pekka Pessi committed
294
#define   UNHEX(a) (a - (a >= 'a' ? 'a' - 10 : (a >= 'A' ? 'A' - 10 : '0')))
295
      c = (UNHEX(s[i]) << 4) | UNHEX(s[i + 1]);
Pekka Pessi's avatar
Pekka Pessi committed
296
#undef    UNHEX
297
      i += 2;
Pekka Pessi's avatar
Pekka Pessi committed
298
    }
299 300 301 302

    if (d)
      d[j] = c;
    j++;
Pekka Pessi's avatar
Pekka Pessi committed
303 304
  }

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  return j;
}

/**Unescape url-escaped string.
 *
 * Unescape string @a s to the buffer @a d, including the terminating \\0
 * character. All %-escaped triplets in @a s are unescaped, for instance, @c
 * "%40%25%23" is copied as @c "@%#". The destination array @a d must be
 * large enough to receive the escaped copy.
 *
 * @param d  destination buffer
 * @param s  string to be copied
 *
 * @return Pointer to the destination buffer.
 */
char *url_unescape(char *d, char const *s)
{
  size_t n = url_unescape_to(d, s, SIZE_MAX);
  if (d)
    d[n] = '\0';
  return d;
Pekka Pessi's avatar
Pekka Pessi committed
326 327 328 329
}

/** Canonize a URL component */
static
330
char *url_canonize(char *d, char const *s, size_t n,
331
		   unsigned syn33,
332
		   char const allowed[])
Pekka Pessi's avatar
Pekka Pessi committed
333 334 335 336 337
{
  unsigned mask32 = 0xbe19003f, mask64 = 0x8000001e, mask96 = 0x8000001d;

  MASKS_WITH_ALLOWED(allowed, mask32, mask64, mask96);

338
  return url_canonize2(d, s, n, syn33, mask32, mask64, mask96);
Pekka Pessi's avatar
Pekka Pessi committed
339 340
}

341 342
#define SYN33(c) (1U << (c - 33))
#define IS_SYN33(syn33, c) ((syn33 & (1U << (c - 33))) != 0)
343

Pekka Pessi's avatar
Pekka Pessi committed
344 345
/** Canonize a URL component (with precomputed mask) */
static
346
char *url_canonize2(char *d, char const * const s, size_t n,
347
		    unsigned syn33,
Pekka Pessi's avatar
Pekka Pessi committed
348 349
		    unsigned m32, unsigned m64, unsigned m96)
{
350
  size_t i = 0;
Pekka Pessi's avatar
Pekka Pessi committed
351 352

  if (d == s)
Pekka Pessi's avatar
Pekka Pessi committed
353
    for (;s[i] && i < n; d++, i++)
354
      if (s[i] == '%')
Pekka Pessi's avatar
Pekka Pessi committed
355 356
	break;

357 358
  for (;s[i] && i < n; d++, i++) {
    unsigned char c = s[i], h1, h2;
Pekka Pessi's avatar
Pekka Pessi committed
359 360

    if (c != '%') {
361
      if (!IS_SYN33(syn33, c) && IS_EXCLUDED(c, m32, m64, m96))
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365
	return NULL;
      *d = c;
      continue;
    }
366

367
    h1 = s[i + 1], h2 = s[i + 2];
Pekka Pessi's avatar
Pekka Pessi committed
368

369
    if (!IS_HEX(h1) || !IS_HEX(h2)) {
Pekka Pessi's avatar
Pekka Pessi committed
370 371 372
      *d = '\0';
      return NULL;
    }
Pekka Pessi's avatar
Pekka Pessi committed
373

Pekka Pessi's avatar
Pekka Pessi committed
374
#define UNHEX(a) (a - (a >= 'a' ? 'a' - 10 : (a >= 'A' ? 'A' - 10 : '0')))
375
    c = (UNHEX(h1) << 4) | UNHEX(h2);
Pekka Pessi's avatar
Pekka Pessi committed
376

377
    if (!IS_EXCLUDED(c, m32, m64, m96)) {
378 379
      /* Convert hex to normal character */
      *d = c, i += 2;
380 381 382 383 384 385 386 387 388 389 390
      continue;
    }

    /* Convert hex to uppercase */
    if (h1 >= 'a' /* && h1 <= 'f' */)
      h1 = h1 - 'a' + 'A';
    if (h2 >= 'a' /* && h2 <= 'f' */)
      h2 = h2 - 'a' + 'A';

    d[0] = '%', d[1] = h1, d[2] = h2;

391
    d +=2, i += 2;
Pekka Pessi's avatar
Pekka Pessi committed
392 393
#undef    UNHEX
  }
Pekka Pessi's avatar
Pekka Pessi committed
394

Pekka Pessi's avatar
Pekka Pessi committed
395 396 397 398 399
  *d = '\0';

  return d;
}

400

401 402
/** Canonize a URL component (with precomputed mask).
 *
Pekka Pessi's avatar
Pekka Pessi committed
403
 * This version does not flag error if *s contains character that should
404 405
 * be escaped.
 */
Pekka Pessi's avatar
Pekka Pessi committed
406
static
407
char *url_canonize3(char *d, char const * const s, size_t n,
Pekka Pessi's avatar
Pekka Pessi committed
408 409
		    unsigned m32, unsigned m64, unsigned m96)
{
410
  size_t i = 0;
Pekka Pessi's avatar
Pekka Pessi committed
411 412

  if (d == s)
Pekka Pessi's avatar
Pekka Pessi committed
413
    for (;s[i] && i < n; d++, i++)
414
      if (s[i] == '%')
Pekka Pessi's avatar
Pekka Pessi committed
415 416
	break;

417 418
  for (;s[i] && i < n; d++, i++) {
    unsigned char c = s[i], h1, h2;
Pekka Pessi's avatar
Pekka Pessi committed
419 420 421 422 423

    if (c != '%') {
      *d = c;
      continue;
    }
424

425
    h1 = s[i + 1], h2 = s[i + 2];
Pekka Pessi's avatar
Pekka Pessi committed
426

427
    if (!IS_HEX(h1) || !IS_HEX(h2)) {
Pekka Pessi's avatar
Pekka Pessi committed
428 429 430
      *d = '\0';
      return NULL;
    }
Pekka Pessi's avatar
Pekka Pessi committed
431

Pekka Pessi's avatar
Pekka Pessi committed
432
#define UNHEX(a) (a - (a >= 'a' ? 'a' - 10 : (a >= 'A' ? 'A' - 10 : '0')))
433
    c = (UNHEX(h1) << 4) | UNHEX(h2);
Pekka Pessi's avatar
Pekka Pessi committed
434

435
    if (!IS_EXCLUDED(c, m32, m64, m96)) {
436
      *d = c, i += 2;
437 438 439 440 441 442 443 444 445 446 447
      continue;
    }

    /* Convert hex to uppercase */
    if (h1 >= 'a' /* && h1 <= 'f' */)
      h1 = h1 - 'a' + 'A';
    if (h2 >= 'a' /* && h2 <= 'f' */)
      h2 = h2 - 'a' + 'A';

    d[0] = '%', d[1] = h1, d[2] = h2;

448
    d +=2, i += 2;
Pekka Pessi's avatar
Pekka Pessi committed
449 450
#undef    UNHEX
  }
Pekka Pessi's avatar
Pekka Pessi committed
451

Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  *d = '\0';

  return d;
}


/** Get URL scheme. */
char const* url_scheme(enum url_type_e url_type)
{
  switch (url_type) {
  case url_any:    return "*";
  case url_sip:    return "sip";
  case url_sips:   return "sips";
  case url_tel:    return "tel";
  case url_fax:    return "fax";
  case url_modem:  return "modem";
  case url_http:   return "http";
  case url_https:  return "https";
  case url_ftp:    return "ftp";
  case url_file:   return "file";
  case url_rtsp:   return "rtsp";
  case url_rtspu:  return "rtspu";
  case url_mailto: return "mailto";
  case url_im:     return "im";
  case url_pres:   return "pres";
  case url_cid:    return "cid";
  case url_msrp:   return "msrp";
Remi Denis-Courmont's avatar
Remi Denis-Courmont committed
479
  case url_msrps:  return "msrps";
Pekka Pessi's avatar
Pekka Pessi committed
480
  case url_wv:     return "wv";
Pekka Pessi's avatar
Pekka Pessi committed
481
  default:
Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486
    assert(url_type == url_unknown);
    return NULL;
  }
}

487
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
488 489
int url_type_is_opaque(enum url_type_e url_type)
{
Pekka Pessi's avatar
Pekka Pessi committed
490
  return
Pekka Pessi's avatar
Pekka Pessi committed
491
    url_type == url_invalid ||
Pekka Pessi's avatar
Pekka Pessi committed
492 493
    url_type == url_tel ||
    url_type == url_modem ||
Pekka Pessi's avatar
Pekka Pessi committed
494 495 496 497 498 499 500 501 502 503
    url_type == url_fax ||
    url_type == url_cid;
}

/** Init an url as given type */
void url_init(url_t *url, enum url_type_e type)
{
  memset(url, 0, sizeof(*url));
  url->url_type = type;
  if (type > url_unknown) {
Kai Samposalo's avatar
Kai Samposalo committed
504
    char const *scheme = url_scheme((enum url_type_e)url->url_type);
Pekka Pessi's avatar
Pekka Pessi committed
505 506 507 508 509 510
    if (scheme)
      url->url_scheme = scheme;
  }
}

/** Get url type */
511
su_inline enum url_type_e url_get_type(char const *scheme, size_t len)
Pekka Pessi's avatar
Pekka Pessi committed
512 513 514
{
#define test_scheme(s) \
   if (len == strlen(#s) && !strncasecmp(scheme, #s, len)) return url_##s
Pekka Pessi's avatar
Pekka Pessi committed
515

Pekka Pessi's avatar
Pekka Pessi committed
516
  switch (scheme[0]) {
517 518 519 520
  case '*':
    if (strcmp(scheme, "*") == 0)
      return url_any;
    break;
Pekka Pessi's avatar
Pekka Pessi committed
521
  case 'c': case 'C':
Pekka Pessi's avatar
Pekka Pessi committed
522
    test_scheme(cid); break;
Pekka Pessi's avatar
Pekka Pessi committed
523
  case 'f': case 'F':
524 525
    test_scheme(ftp); test_scheme(file); test_scheme(fax);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
526
  case 'h': case 'H':
527 528
    test_scheme(http); test_scheme(https);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
529
  case 'i': case 'I':
530 531
    test_scheme(im);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
532 533
  case 'm': case 'M':
    test_scheme(mailto); test_scheme(modem);
534 535
    test_scheme(msrp); test_scheme(msrps);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
536
  case 'p': case 'P':
537 538
    test_scheme(pres);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
539
  case 'r': case 'R':
540 541
    test_scheme(rtsp); test_scheme(rtspu);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
542
  case 's': case 'S':
543 544
    test_scheme(sip); test_scheme(sips);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
545
  case 't': case 'T':
546 547
    test_scheme(tel);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
548
  case 'w': case 'W':
549 550 551 552
    test_scheme(wv);
    break;
  default:
    break;
Pekka Pessi's avatar
Pekka Pessi committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  }

#undef test_scheme

  if (len != span_unreserved(scheme))
    return url_invalid;
  else
    return url_unknown;
}

/**
 * Decode a URL.
 *
 * This function decodes a (SIP) URL string to a url_t structure.
 *
 * @param url structure to store the parsing result
 * @param s   NUL-terminated string to be parsed
 *
 * @note The parsed string @a s will be modified when parsing it.
 *
Pekka Pessi's avatar
Pekka Pessi committed
573
 * @retval 0 if successful,
Pekka Pessi's avatar
Pekka Pessi committed
574 575 576 577 578
 * @retval -1 otherwise.
 */
static
int _url_d(url_t *url, char *s)
{
579
  size_t n, p;
580 581
  char *s0, rest_c, *host, *user;
  int have_authority = 1;
Pekka Pessi's avatar
Pekka Pessi committed
582 583

  memset(url, 0, sizeof(*url));
Pekka Pessi's avatar
Pekka Pessi committed
584

Pekka Pessi's avatar
Pekka Pessi committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598
  if (strcmp(s, "*") == 0) {
    url->url_type = url_any;
    url->url_scheme = "*";
    return 0;
  }

  s0 = s;

  n = strcspn(s, ":/?#");

  if (n && s[n] == ':') {
    char *scheme;
    url->url_scheme = scheme = s; s[n] = '\0'; s = s + n + 1;

599
    if (!(scheme = url_canonize(scheme, scheme, SIZE_MAX, 0, "+")))
Pekka Pessi's avatar
Pekka Pessi committed
600 601 602 603 604 605
      return -1;

    n = scheme - url->url_scheme;

    url->url_type = url_get_type(url->url_scheme, n);

Kai Samposalo's avatar
Kai Samposalo committed
606
    have_authority = !url_type_is_opaque((enum url_type_e)url->url_type);
607 608
  }
  else {
Pekka Pessi's avatar
Pekka Pessi committed
609 610 611
    url->url_type = url_unknown;
  }

612
  user = NULL, host = s;
Pekka Pessi's avatar
Pekka Pessi committed
613 614

  if (url->url_type == url_sip || url->url_type == url_sips) {
615 616
    /* SIP URL may have /;? in user part but no path */
    /* user-unreserved  =  "&" / "=" / "+" / "$" / "," / ";" / "?" / "/" */
617
    /* Some #*@#* phones include unescaped # there, too */
618 619 620 621 622 623
    n = strcspn(s, "@/;?#");
    p = strcspn(s + n, "@");
    if (s[n + p] == '@') {
      n += p;
      user = s;
      host = s + n + 1;
624
    }
625

Pekka Pessi's avatar
Pekka Pessi committed
626 627
    n += strcspn(s + n, "/;?#");
  }
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  else if (have_authority) {
    if (url->url_type == url_wv) {
      /* WV URL may have / in user part */
      n = strcspn(s, "@#?;");
      if (s[n] == '@') {
	user = s;
	host = s + n + 1;
	n += strcspn(s + n, ";?#");
      }
    }
    else if (host[0] == '/' && host[1] != '/') {
      /* foo:/bar or /bar - no authority, just path */
      url->url_root = '/';	/* Absolute path */
      host = NULL, n = 0;
    }
    else {
      if (host[0] == '/' && host[1] == '/') {
	/* We have authority, / / foo or foo */
	host += 2; s += 2, url->url_root = '/';
	n = strcspn(s, "/?#@[]");
      }
Pekka Pessi's avatar
Pekka Pessi committed
649
      else
650 651 652 653
	n = strcspn(s, "@;/?#");

      if (s[n] == '@')
	user = host, host = user + n + 1;
Pekka Pessi's avatar
Pekka Pessi committed
654

655
      n += strcspn(s + n, ";/?#");	/* Find path, query and/or fragment */
Pekka Pessi's avatar
Pekka Pessi committed
656
    }
Pekka Pessi's avatar
Pekka Pessi committed
657
  }
658 659 660 661 662 663
  else /* !have_authority */ {
    user = host, host = NULL;
    if (url->url_type != url_invalid)
      n = strcspn(s, "/;?#");	/* Find params, query and/or fragment */
    else
      n = strcspn(s, "#");
Pekka Pessi's avatar
Pekka Pessi committed
664 665 666 667
  }

  rest_c = s[n]; s[n] = 0; s = rest_c ? s + n + 1 : NULL;

668 669 670 671 672 673 674 675 676
  if (user) {
    if (host) host[-1] = '\0';
    url->url_user = user;
    if (url->url_type != url_unknown) {
      n = strcspn(user, ":");
      if (user[n]) {
	user[n] = '\0';
	url->url_password = user + n + 1;
      }
677
    }
678
  }
Pekka Pessi's avatar
Pekka Pessi committed
679

680 681 682 683 684 685 686 687 688 689 690 691
  if (host) {
    url->url_host = host;
    /* IPv6 (and in some cases, IPv4) addresses are quoted with [] */
    if (host[0] == '[') {
      n = strcspn(host, "]");
      if (host[n] && (host[n + 1] == '\0' || host[n + 1] == ':'))
	n++;
      else
	n = 0;
    }
    else {
      n = strcspn(host, ":");
Pekka Pessi's avatar
Pekka Pessi committed
692 693
    }

694 695 696 697 698 699 700 701 702 703
    /* We allow empty host by default */
    if (n == 0) switch (url->url_type) {
    case url_sip:
    case url_sips:
    case url_im:
    case url_pres:
      return -1;
    default:
      break;
    }
Pekka Pessi's avatar
Pekka Pessi committed
704

705 706 707 708 709 710 711 712 713 714 715 716 717
    if (host[n] == ':') {
      char *port = host + n + 1;
      url->url_port = port;
      switch (url->url_type) {
      case url_any:
      case url_sip:
      case url_sips:
      case url_http:
      case url_https:
      case url_ftp:
      case url_file:
      case url_rtsp:
      case url_rtspu:
718
	if (!url_canonize2(port, port, SIZE_MAX, 0, RESERVED_MASK))
719 720 721 722 723 724
	  return -1;

	/* Check that port is really numeric or wildcard */
	/* Port can be *digit, empty string or "*" */
	while (*port >= '0' && *port <= '9')
	  port++;
Pekka Pessi's avatar
Pekka Pessi committed
725

726 727 728 729 730 731 732 733 734
	if (port != url->url_port) {
	  if (port[0] != '\0')
	    return -1;
	}
	else if (port[0] == '\0')
	  /* empty string */;
	else if (port[0] == '*' && port[1] == '\0')
	  /* wildcard */;
	else
Pekka Pessi's avatar
Pekka Pessi committed
735 736
	  return -1;
      }
737
      host[n] = 0;
Pekka Pessi's avatar
Pekka Pessi committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    }
  }

  if (rest_c == '/') {
    url->url_path = s; n = strcspn(s, "?#");
    rest_c = s[n]; s[n] = 0; s = rest_c ? s + n + 1 : NULL;
  }
  if (rest_c == ';') {
    url->url_params = s; n = strcspn(s, "?#");
    rest_c = s[n]; s[n] = 0; s = rest_c ? s + n + 1 : NULL;
  }
  if (rest_c == '?') {
    url->url_headers = s; n = strcspn(s, "#");
    rest_c = s[n]; s[n] = 0; s = rest_c ? s + n + 1 : NULL;
  }
  if (rest_c == '#') {
    url->url_fragment = s;
    rest_c = '\0';
  }
  if (rest_c)
    return -1;

  return 0;
}

/* Unreserved things */

/**
 * Decode a URL.
 *
 * This function decodes a URL string to a url_t structure.
 *
 * @param url structure to store the parsing result
 * @param s   NUL-terminated string to be parsed
 *
 * @note The parsed string @a s will be modified when parsing it.
 *
Pekka Pessi's avatar
Pekka Pessi committed
775
 * @retval 0 if successful,
Pekka Pessi's avatar
Pekka Pessi committed
776 777 778 779 780 781 782 783 784 785 786 787 788
 * @retval -1 otherwise.
 */
int url_d(url_t *url, char *s)
{
  if (url == NULL || _url_d(url, s) < 0)
    return -1;

  /* Canonize  URL */
  /* scheme is canonized by _url_d() */
  if (url->url_type == url_sip || url->url_type == url_sips) {

#   define SIP_USER_UNRESERVED "&=+$,;?/"
    s = (char *)url->url_user;
789
    if (s && !url_canonize(s, s, SIZE_MAX, 0, SIP_USER_UNRESERVED))
Pekka Pessi's avatar
Pekka Pessi committed
790 791
      return -1;

792 793
    /* Having different charset in user and password does not make sense */
    /* but that is how it is defined in RFC 3261 */
Pekka Pessi's avatar
Pekka Pessi committed
794 795
#   define SIP_PASS_UNRESERVED "&=+$,"
    s = (char *)url->url_password;
796
    if (s && !url_canonize(s, s, SIZE_MAX, 0, SIP_PASS_UNRESERVED))
Pekka Pessi's avatar
Pekka Pessi committed
797 798
      return -1;

799 800
  }
  else {
Pekka Pessi's avatar
Pekka Pessi committed
801 802 803

#   define USER_UNRESERVED "&=+$,;"
    s = (char *)url->url_user;
804
    if (s && !url_canonize(s, s, SIZE_MAX, 0, USER_UNRESERVED))
Pekka Pessi's avatar
Pekka Pessi committed
805 806 807 808
      return -1;

#   define PASS_UNRESERVED "&=+$,;:"
    s = (char *)url->url_password;
809
    if (s && !url_canonize(s, s, SIZE_MAX, 0, PASS_UNRESERVED))
Pekka Pessi's avatar
Pekka Pessi committed
810 811 812 813
      return -1;
  }

  s = (char *)url->url_host;
814
  if (s && !url_canonize2(s, s, SIZE_MAX, 0, RESERVED_MASK))
Pekka Pessi's avatar
Pekka Pessi committed
815 816 817 818
    return -1;

  /* port is canonized by _url_d() */
  s = (char *)url->url_path;
Pekka Pessi's avatar
Pekka Pessi committed
819
  if (s && !url_canonize(s, s, SIZE_MAX,
820 821 822 823 824 825
			 /* Allow all URI characters but ? */
			 /* Allow unescaped /;?@, - but do not convert */
			 SYN33('/') | SYN33(';') | SYN33('=') | SYN33('@') |
			 SYN33(','),
			 /* Convert escaped :&+$ to unescaped */
			 ":&+$"))
Pekka Pessi's avatar
Pekka Pessi committed
826 827 828
    return -1;

  s = (char *)url->url_params;
829 830 831 832 833 834
  if (s && !url_canonize(s, s, SIZE_MAX,
			 /* Allow all URI characters but ? */
			 /* Allow unescaped ;=@, - but do not convert */
			 SYN33(';') | SYN33('=') | SYN33('@') | SYN33(','),
			 /* Convert escaped /:&+$ to unescaped */
			 "/:&+$"))
Pekka Pessi's avatar
Pekka Pessi committed
835
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
836

Pekka Pessi's avatar
Pekka Pessi committed
837 838
  /* Unhex alphanumeric and unreserved URI characters */
  s = (char *)url->url_headers;
839
  if (s && !url_canonize3(s, s, SIZE_MAX, RESERVED_MASK))
Pekka Pessi's avatar
Pekka Pessi committed
840 841 842 843
    return -1;

  /* Allow all URI characters (including reserved ones) */
  s = (char *)url->url_fragment;
844
  if (s && !url_canonize2(s, s, SIZE_MAX, 0, URIC_MASK))
Pekka Pessi's avatar
Pekka Pessi committed
845 846 847 848 849
    return -1;

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
850 851
/** Encode an URL.
 *
Pekka Pessi's avatar
Pekka Pessi committed
852 853 854 855 856 857 858 859
 * The function url_e() combines a URL from substrings in url_t structure
 * according the @ref url_syntax "URL syntax" presented above.  The encoded
 * @a url is stored in a @a buffer of @a n bytes.
 *
 * @param buffer memory area to store the encoded @a url.
 * @param n      size of @a buffer.
 * @param url    URL to be encoded.
 *
Pekka Pessi's avatar
Pekka Pessi committed
860 861
 * @return
 * Return the number of bytes in the encoding.
Pekka Pessi's avatar
Pekka Pessi committed
862 863 864 865 866
 *
 * @note The function follows the convention set by C99 snprintf().  Even if
 * the result does not fit into the @a buffer and it is truncated, the
 * function returns the number of bytes in an untruncated encoding.
 */
867
issize_t url_e(char buffer[], isize_t n, url_t const *url)
Pekka Pessi's avatar
Pekka Pessi committed
868
{
869
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
870
  char *b = buffer;
871
  size_t m = n;
Pekka Pessi's avatar
Pekka Pessi committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
  int do_copy = n > 0;

  if (url == NULL)
    return -1;

  if (URL_STRING_P(url)) {
    char const *u = (char *)url;
    i = strlen(u);
    if (!buffer)
      return i;

    if (i >= n) {
      memcpy(buffer, u, n - 2);
      buffer[n - 1] = '\0';
    } else {
      memcpy(buffer, u, i + 1);
    }

    return i;
  }


  if (url->url_type == url_any) {
    if (b && m > 0) {
      if (m > 1) strcpy(b, "*"); else b[0] = '\0';
    }
    return 1;
  }

  if (url->url_scheme && url->url_scheme[0]) {
    i = strlen(url->url_scheme) + 1;
    if (do_copy && (do_copy = i <= n)) {
      memcpy(b, url->url_scheme, i - 1);
      b[i - 1] = ':';
    }
    b += i; n -= i;
  }

  if (url->url_root && (url->url_host || url->url_user)) {
    if (do_copy && (do_copy = 2 <= n))
      memcpy(b, "//", 2);
    b += 2; n -= 2;
  }

  if (url->url_user) {
    i = strlen(url->url_user);
    if (do_copy && (do_copy = i <= n))
      memcpy(b, url->url_user, i);
    b += i; n -= i;

    if (url->url_password) {
      if (do_copy && (do_copy = 1 <= n))
Pekka Pessi's avatar
Pekka Pessi committed
924
	*b = ':';
Pekka Pessi's avatar
Pekka Pessi committed
925 926 927 928 929 930 931 932 933
      b++; n--;
      i = strlen(url->url_password);
      if (do_copy && (do_copy = i <= n))
	memcpy(b, url->url_password, i);
      b += i; n -= i;
    }

    if (url->url_host) {
      if (do_copy && (do_copy = 1 <= n))
Pekka Pessi's avatar
Pekka Pessi committed
934
	*b = '@';
Pekka Pessi's avatar
Pekka Pessi committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
      b++; n--;
    }
  }

  if (url->url_host) {
    i = strlen(url->url_host);
    if (do_copy && (do_copy = i <= n))
      memcpy(b, url->url_host, i);
    b += i; n -= i;

    if (url->url_port) {
      i = strlen(url->url_port) + 1;
      if (do_copy && (do_copy = i <= n)) {
	b[0] = ':';
	memcpy(b + 1, url->url_port, i - 1);
Pekka Pessi's avatar
Pekka Pessi committed
950
      }
Pekka Pessi's avatar
Pekka Pessi committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
      b += i; n -= i;
    }
  }

  if (url->url_path) {
    if (url->url_root) {
      if (do_copy && (do_copy = 1 <= n))
	b[0] = '/';
      b++, n--;
    }
    i = strlen(url->url_path);
    if (do_copy && (do_copy = i < n))
      memcpy(b, url->url_path, i);
    b += i; n -= i;
  }
Pekka Pessi's avatar
Pekka Pessi committed
966

Pekka Pessi's avatar
Pekka Pessi committed
967 968 969
  {
    static char const sep[] = ";?#";
    char const *pp[3];
970
    size_t j;
Pekka Pessi's avatar
Pekka Pessi committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

    pp[0] = url->url_params;
    pp[1] = url->url_headers;
    pp[2] = url->url_fragment;

    for (j = 0; j < 3; j++) {
      char const *p = pp[j];
      if (!p) continue;
      i = strlen(p) + 1;
      if (do_copy && (do_copy = i <= n)) {
	*b = sep[j];
	memcpy(b + 1, p, i - 1);
      }
      b += i; n -= i;
    }
  }

  if (do_copy && (do_copy = 1 <= n))
    *b = '\0';
  else if (buffer && m > 0)
    buffer[m - 1] = '\0';

993
  assert((size_t)(b - buffer) == (size_t)(m - n));
Pekka Pessi's avatar
Pekka Pessi committed
994 995

  /* This follows the snprintf(C99) return value,
Pekka Pessi's avatar
Pekka Pessi committed
996 997 998 999 1000 1001
   * Number of characters written (excluding NUL)
   */
  return b - buffer;
}


Pekka Pessi's avatar
Pekka Pessi committed
1002
/** Calculate the length of URL when encoded.
Pekka Pessi's avatar
Pekka Pessi committed
1003 1004
 *
 */
1005
isize_t url_len(url_t const * url)
Pekka Pessi's avatar
Pekka Pessi committed
1006
{
1007
  size_t rv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1008 1009 1010 1011

  if (url->url_scheme) rv += strlen(url->url_scheme) + 1; /* plus ':' */
  if (url->url_user) {
    rv += strlen(url->url_user);
Pekka Pessi's avatar
Pekka Pessi committed
1012
    if (url->url_password)
Pekka Pessi's avatar
Pekka Pessi committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
      rv += strlen(url->url_password) + 1;   /* plus ':' */
    rv += url->url_host != NULL;  /* plus '@' */
  }
  if (url->url_host) rv += strlen(url->url_host);
  if (url->url_port) rv += strlen(url->url_port) + 1;	        /* plus ':' */
  if (url->url_path) rv += strlen(url->url_path) + 1;     /* plus initial / */
  if (url->url_params) rv += strlen(url->url_params) + 1; /* plus initial ; */
  if (url->url_headers) rv += strlen(url->url_headers) + 1;	/* plus '?' */
  if (url->url_fragment) rv += strlen(url->url_fragment) + 1;   /* plus '#' */

  return rv;
}

Pekka Pessi's avatar
Pekka Pessi committed
1026
/**@def URL_E(buf, end, url)
Pekka Pessi's avatar
Pekka Pessi committed
1027
 * Encode an URL: use @a buf up to @a end.
Pekka Pessi's avatar
Pekka Pessi committed
1028
 * @hideinitializer
Pekka Pessi's avatar
Pekka Pessi committed
1029 1030
 */

Pekka Pessi's avatar
Pekka Pessi committed
1031
/**
Pekka Pessi's avatar
Pekka Pessi committed
1032 1033 1034 1035 1036
 * Calculate the size of strings associated with a #url_t sructure.
 *
 * @param url pointer to a #url_t structure or string
 * @return Number of bytes for URL
 */
1037
isize_t url_xtra(url_t const *url)
Pekka Pessi's avatar
Pekka Pessi committed
1038
{
1039 1040
  size_t xtra;

Pekka Pessi's avatar
Pekka Pessi committed
1041
  if (URL_STRING_P(url)) {
1042
    xtra = strlen((char const *)url) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1043 1044
  }
  else {
1045
    size_t len_scheme, len_user, len_password,
Pekka Pessi's avatar
Pekka Pessi committed
1046
      len_host, len_port, len_path, len_params,
Pekka Pessi's avatar
Pekka Pessi committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
      len_headers, len_fragment;

    len_scheme = (url->url_type <= url_unknown && url->url_scheme) ?
      strlen(url->url_scheme) + 1 : 0;
    len_user = url->url_user ? strlen(url->url_user) + 1 : 0;
    len_password = url->url_password ? strlen(url->url_password) + 1 : 0;
    len_host = url->url_host ? strlen(url->url_host) + 1 : 0;
    len_port = url->url_port ? strlen(url->url_port) + 1 : 0;
    len_path = url->url_path ? strlen(url->url_path) + 1 : 0;
    len_params = url->url_params ? strlen(url->url_params) + 1 : 0;
    len_headers = url->url_headers ? strlen(url->url_headers) + 1 : 0;
    len_fragment = url->url_fragment ? strlen(url->url_fragment) + 1 : 0;

1060
    xtra =
Pekka Pessi's avatar
Pekka Pessi committed
1061 1062 1063
      len_scheme + len_user + len_password + len_host + len_port +
      len_path + len_params + len_headers + len_fragment;
  }
1064 1065

  return xtra;
Pekka Pessi's avatar
Pekka Pessi committed
1066 1067
}

1068
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
char *copy(char *buf, char *end, char const *src)
{
#if HAVE_MEMCCPY
  char *b = memccpy(buf, src, '\0', end - buf);
  if (b)
    return b;
  else
    return end + strlen(src + (end - buf)) + 1;
#else
  for (; buf < end && (*buf = *src); buf++, src++)
    ;

Pekka Pessi's avatar
Pekka Pessi committed
1081
  if (buf >= end)
Pekka Pessi's avatar
Pekka Pessi committed
1082 1083 1084 1085 1086 1087 1088
    while (*src++)
      buf++;

  return buf + 1;
#endif
}

Pekka Pessi's avatar
Pekka Pessi committed
1089
/**
Pekka Pessi's avatar
Pekka Pessi committed
1090
 * Duplicate the url.
Pekka Pessi's avatar
Pekka Pessi committed
1091
 *
Pekka Pessi's avatar
Pekka Pessi committed
1092 1093
 * The function url_dup() copies the url structure @a src and the strings
 * attached to it to @a url.  The non-constant strings in @a src are copied
Pekka Pessi's avatar
Pekka Pessi committed
1094
 * to @a buf.  If the size of duplicated strings exceed @a bufsize, the
Pekka Pessi's avatar
Pekka Pessi committed
1095 1096 1097 1098 1099 1100 1101 1102
 * corresponding string fields in @a url are set to NULL.
 *
 * The calling function can calculate the size of buffer required by calling
 * url_dup() with zero as @a bufsize and NULL as @a dst.

 * @param buf     Buffer for non-constant strings copied from @a src.
 * @param bufsize Size of @a buf.
 * @param dst     Destination URL structure.
1103
 * @param src     Source URL structure.
Pekka Pessi's avatar
Pekka Pessi committed
1104
 *
1105
 * @return Number of characters required for
Pekka Pessi's avatar
Pekka Pessi committed
1106 1107 1108
 * duplicating the strings in @a str, or -1 if an error
 * occurred.
 */
1109
issize_t url_dup(char *buf, isize_t bufsize, url_t *dst, url_t const *src)
Pekka Pessi's avatar
Pekka Pessi committed
1110 1111 1112 1113
{
  if (!src && !dst)
    return -1;
  else if (URL_STRING_P(src)) {
1114
    size_t n = strlen((char *)src) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    if (n > bufsize || dst == NULL)
      return n;

    strcpy(buf, (char *)src);
    memset(dst, 0, sizeof(*dst));
    if (url_d(dst, buf) < 0)
      return -1;

    return n;
  }
  else {
    char *b = buf;
    char *end = b + bufsize;
Pekka Pessi's avatar
Pekka Pessi committed
1128
    char const **dstp;
Pekka Pessi's avatar
Pekka Pessi committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
    char const * const *srcp;
    url_t dst0[1];

    if (dst == NULL)
      dst = dst0;

    memset(dst, 0, sizeof(*dst));

    if (!src)
      return 0;

    memset(dst->url_pad, 0, sizeof dst->url_pad);
    dst->url_type = src->url_type;
    dst->url_root = src->url_root;

    dstp = &dst->url_scheme;
    srcp = &src->url_scheme;

    if (dst->url_type > url_unknown)
Kai Samposalo's avatar
Kai Samposalo committed
1148
      *dstp = url_scheme((enum url_type_e)dst->url_type);
Pekka Pessi's avatar
Pekka Pessi committed
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

    if (*dstp != NULL)
      dstp++, srcp++;	/* Skip scheme if it is constant */

    if (dst != dst0 && buf != NULL && bufsize != 0)
      for (; srcp <= &src->url_fragment; srcp++, dstp++)
	if (*srcp) {
	  char *next = copy(b, end, *srcp);

	  if (next > end)
	    break;

	  *dstp = b, b = next;
	}

    for (; srcp <= &src->url_fragment; srcp++)
      if (*srcp) {
	b += strlen(*srcp) + 1;
      }

    return b - buf;
  }
}

/**@def URL_DUP(buf, end, dst, src)
Pekka Pessi's avatar
Pekka Pessi committed
1174
 *  Duplicate the url: use @a buf up to @a end. @HI
Pekka Pessi's avatar
Pekka Pessi committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
 *
 * The macro URL_DUP() duplicates the url.  The non-constant strings in @a
 * src are copied to @a buf.  However, no strings are copied past @a end.
 * In other words, the size of buffer is @a end - @a buf.
 *
 * The macro updates the buffer pointer @a buf, so that it points to the
 * first unused byte in the buffer.  The buffer pointer @a buf is updated,
 * even if the buffer is too small for the duplicated strings.
 *
 * @param buf     Buffer for non-constant strings copied from @a src.
 * @param end     End of @a buf.
 * @param dst     Destination URL structure.