soup-uri.c 32.1 KB
Newer Older
alex's avatar
alex committed
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* soup-uri.c : utility functions to parse URLs */

Alex Graveley's avatar
Alex Graveley committed
4
/*
5
 * Copyright 1999-2003 Ximian, Inc.
alex's avatar
alex committed
6 7
 */

8 9 10 11
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

alex's avatar
alex committed
12 13
#include <string.h>
#include <stdlib.h>
alex's avatar
alex committed
14

alex's avatar
alex committed
15
#include "soup-uri.h"
Dan Winship's avatar
Dan Winship committed
16
#include "soup.h"
17
#include "soup-misc-private.h"
18

19 20 21 22 23 24 25 26 27 28 29
/**
 * SECTION:soup-uri
 * @short_description: URIs
 *
 * A #SoupURI represents a (parsed) URI.
 *
 * Many applications will not need to use #SoupURI directly at all; on
 * the client side, soup_message_new() takes a stringified URI, and on
 * the server side, the path and query components are provided for you
 * in the server callback.
 **/
30

31 32 33 34 35
/**
 * SoupURI:
 * @scheme: the URI scheme (eg, "http")
 * @user: a username, or %NULL
 * @password: a password, or %NULL
36
 * @host: the hostname or IP address, or %NULL
37 38 39 40 41 42 43
 * @port: the port number on @host
 * @path: the path on @host
 * @query: a query for @path, or %NULL
 * @fragment: a fragment identifier within @path, or %NULL
 *
 * A #SoupURI represents a (parsed) URI. #SoupURI supports RFC 3986
 * (URI Generic Syntax), and can parse any valid URI. However, libsoup
44 45 46
 * only uses "http" and "https" URIs internally; You can use
 * SOUP_URI_VALID_FOR_HTTP() to test if a #SoupURI is a valid HTTP
 * URI.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 *
 * @scheme will always be set in any URI. It is an interned string and
 * is always all lowercase. (If you parse a URI with a non-lowercase
 * scheme, it will be converted to lowercase.) The macros
 * %SOUP_URI_SCHEME_HTTP and %SOUP_URI_SCHEME_HTTPS provide the
 * interned values for "http" and "https" and can be compared against
 * URI @scheme values.
 *
 * @user and @password are parsed as defined in the older URI specs
 * (ie, separated by a colon; RFC 3986 only talks about a single
 * "userinfo" field). Note that @password is not included in the
 * output of soup_uri_to_string(). libsoup does not normally use these
 * fields; authentication is handled via #SoupSession signals.
 *
 * @host contains the hostname, and @port the port specified in the
 * URI. If the URI doesn't contain a hostname, @host will be %NULL,
 * and if it doesn't specify a port, @port may be 0. However, for
 * "http" and "https" URIs, @host is guaranteed to be non-%NULL
 * (trying to parse an http URI with no @host will return %NULL), and
 * @port will always be non-0 (because libsoup knows the default value
 * to use when it is not specified in the URI).
 *
 * @path is always non-%NULL. For http/https URIs, @path will never be
 * an empty string either; if the input URI has no path, the parsed
 * #SoupURI will have a @path of "/".
 *
 * @query and @fragment are optional for all URI types.
74
 * soup_form_decode() may be useful for parsing @query.
75 76 77 78 79 80 81 82 83
 *
 * Note that @path, @query, and @fragment may contain
 * %<!-- -->-encoded characters. soup_uri_new() calls
 * soup_uri_normalize() on them, but not soup_uri_decode(). This is
 * necessary to ensure that soup_uri_to_string() will generate a URI
 * that has exactly the same meaning as the original. (In theory,
 * #SoupURI should leave @user, @password, and @host partially-encoded
 * as well, but this would be more annoying than useful.)
 **/
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * SOUP_URI_IS_VALID:
 * @uri: a #SoupURI
 *
 * Tests whether @uri is a valid #SoupURI; that is, that it is non-%NULL
 * and its @scheme and @path members are also non-%NULL.
 *
 * This macro does not check whether http and https URIs have a non-%NULL
 * @host member.
 *
 * Return value: %TRUE if @uri is valid for use.
 *
 * Since: 2.38
 **/

100 101 102 103 104 105 106 107
/**
 * SOUP_URI_VALID_FOR_HTTP:
 * @uri: a #SoupURI
 *
 * Tests if @uri is a valid #SoupURI for HTTP communication; that is, if
 * it can be used to construct a #SoupMessage.
 *
 * Return value: %TRUE if @uri is a valid "http" or "https" URI.
108 109
 *
 * Since: 2.24
110 111
 **/

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/**
 * SOUP_URI_SCHEME_HTTP:
 *
 * "http" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 */
/**
 * SOUP_URI_SCHEME_HTTPS:
 *
 * "https" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 */
/**
 * SOUP_URI_SCHEME_FTP:
 *
 * "ftp" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.30
 */
/**
 * SOUP_URI_SCHEME_FILE:
 *
 * "file" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.30
 */
/**
 * SOUP_URI_SCHEME_DATA:
 *
 * "data" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.30
 */
/**
 * SOUP_URI_SCHEME_RESOURCE:
 *
 * "data" as an interned string; you can compare this directly to a
 * #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.42
 */
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/**
 * SOUP_URI_SCHEME_WS:
 *
 * "ws" (WebSocket) as an interned string; you can compare this
 * directly to a #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.50
 */
/**
 * SOUP_URI_SCHEME_WSS:
 *
 * "wss" (WebSocket over TLS) as an interned string; you can compare
 * this directly to a #SoupURI's <literal>scheme</literal> field using
 * <literal>==</literal>.
 *
 * Since: 2.50
 */
180

181
static void append_uri_encoded (GString *str, const char *in, const char *extra_enc_chars);
182
static char *uri_normalized_copy (const char *str, int length, const char *unescape_extra);
183

184
gpointer _SOUP_URI_SCHEME_HTTP, _SOUP_URI_SCHEME_HTTPS;
185
gpointer _SOUP_URI_SCHEME_WS, _SOUP_URI_SCHEME_WSS;
186
gpointer _SOUP_URI_SCHEME_FTP;
187
gpointer _SOUP_URI_SCHEME_FILE, _SOUP_URI_SCHEME_DATA, _SOUP_URI_SCHEME_RESOURCE;
188

189
static inline const char *
190
soup_uri_parse_scheme (const char *scheme, int len)
191
{
192
	if (len == 4 && !g_ascii_strncasecmp (scheme, "http", len)) {
193
		return SOUP_URI_SCHEME_HTTP;
194
	} else if (len == 5 && !g_ascii_strncasecmp (scheme, "https", len)) {
195
		return SOUP_URI_SCHEME_HTTPS;
196 197
	} else if (len == 8 && !g_ascii_strncasecmp (scheme, "resource", len)) {
		return SOUP_URI_SCHEME_RESOURCE;
198 199 200 201
	} else if (len == 2 && !g_ascii_strncasecmp (scheme, "ws", len)) {
		return SOUP_URI_SCHEME_WS;
	} else if (len == 3 && !g_ascii_strncasecmp (scheme, "wss", len)) {
		return SOUP_URI_SCHEME_WSS;
202 203 204 205
	} else {
		char *lower_scheme;

		lower_scheme = g_ascii_strdown (scheme, len);
206 207 208
		scheme = g_intern_static_string (lower_scheme);
		if (scheme != (const char *)lower_scheme)
			g_free (lower_scheme);
209 210
		return scheme;
	}
211 212
}

213
static inline guint
214
soup_scheme_default_port (const char *scheme)
alex's avatar
alex committed
215
{
216
	if (scheme == SOUP_URI_SCHEME_HTTP || scheme == SOUP_URI_SCHEME_WS)
217
		return 80;
218
	else if (scheme == SOUP_URI_SCHEME_HTTPS || scheme == SOUP_URI_SCHEME_WSS)
219
		return 443;
220 221
	else if (scheme == SOUP_URI_SCHEME_FTP)
		return 21;
222 223
	else
		return 0;
alex's avatar
alex committed
224 225
}

226
/**
227
 * soup_uri_new_with_base: (constructor)
228 229 230 231 232
 * @base: a base URI
 * @uri_string: the URI
 *
 * Parses @uri_string relative to @base.
 *
233
 * Returns: a parsed #SoupURI.
234
 **/
235 236
SoupURI *
soup_uri_new_with_base (SoupURI *base, const char *uri_string)
237
{
238
	SoupURI *uri, fixed_base;
239 240 241
	const char *end, *hash, *colon, *at, *path, *question;
	const char *p, *hostend;
	gboolean remove_dot_segments = TRUE;
242
	int len;
243

244 245
	g_return_val_if_fail (uri_string != NULL, NULL);

246 247 248 249 250 251 252 253 254 255 256
	/* Allow a %NULL path in @base, for compatibility */
	if (base && base->scheme && !base->path) {
		g_warn_if_fail (SOUP_URI_IS_VALID (base));

		memcpy (&fixed_base, base, sizeof (SoupURI));
		fixed_base.path = "";
		base = &fixed_base;
	}

	g_return_val_if_fail (base == NULL || SOUP_URI_IS_VALID (base), NULL);

257 258 259
	/* First some cleanup steps (which are supposed to all be no-ops,
	 * but...). Skip initial whitespace, strip out internal tabs and
	 * line breaks, and ignore trailing whitespace.
260
	 */
261 262 263 264 265
	while (g_ascii_isspace (*uri_string))
		uri_string++;

	len = strcspn (uri_string, "\t\n\r");
	if (uri_string[len]) {
266
		char *clean = g_malloc (strlen (uri_string) + 1), *d;
267 268 269 270 271 272 273
		const char *s;

		for (s = uri_string, d = clean; *s; s++) {
			if (*s != '\t' && *s != '\n' && *s != '\r')
				*d++ = *s;
		}
		*d = '\0';
274 275 276 277 278 279 280 281 282 283

		uri = soup_uri_new_with_base (base, clean);
		g_free (clean);
		return uri;
	}
	end = uri_string + len;
	while (end > uri_string && g_ascii_isspace (end[-1]))
		end--;

	uri = g_slice_new0 (SoupURI);
284

285
	/* Find fragment. */
286 287 288
	hash = strchr (uri_string, '#');
	if (hash) {
		uri->fragment = uri_normalized_copy (hash + 1, end - hash + 1,
289
						     NULL);
290 291
		end = hash;
	}
292

293
	/* Find scheme */
294
	p = uri_string;
295
	while (p < end && (g_ascii_isalpha (*p) ||
296 297 298 299
			   (p > uri_string && (g_ascii_isdigit (*p) ||
					       *p == '.' ||
					       *p == '+' ||
					       *p == '-'))))
300 301 302
		p++;

	if (p > uri_string && *p == ':') {
303
		uri->scheme = soup_uri_parse_scheme (uri_string, p - uri_string);
304
		uri_string = p + 1;
305
	}
306

307 308
	if (uri_string == end && !base && !uri->fragment) {
		uri->path = g_strdup ("");
309
		return uri;
310
        }
311 312 313 314 315

	/* Check for authority */
	if (strncmp (uri_string, "//", 2) == 0) {
		uri_string += 2;

316
		path = uri_string + strcspn (uri_string, "/?#");
317 318
		if (path > end)
			path = end;
319
		at = strchr (uri_string, '@');
320
		if (at && at < path) {
321 322
			colon = strchr (uri_string, ':');
			if (colon && colon < at) {
323 324
				uri->password = soup_uri_decoded_copy (colon + 1,
								       at - colon - 1, NULL);
325
			} else {
326
				uri->password = NULL;
327 328 329
				colon = at;
			}

330 331
			uri->user = soup_uri_decoded_copy (uri_string,
							   colon - uri_string, NULL);
332 333
			uri_string = at + 1;
		} else
334
			uri->user = uri->password = NULL;
335 336

		/* Find host and port. */
337
		if (*uri_string == '[') {
Dan Winship's avatar
Dan Winship committed
338 339
			const char *pct;

340 341 342 343 344 345 346 347 348 349
			uri_string++;
			hostend = strchr (uri_string, ']');
			if (!hostend || hostend > path) {
				soup_uri_free (uri);
				return NULL;
			}
			if (*(hostend + 1) == ':')
				colon = hostend + 1;
			else
				colon = NULL;
Dan Winship's avatar
Dan Winship committed
350 351

			pct = memchr (uri_string, '%', hostend - uri_string);
352 353 354 355
			if (!pct || (pct[1] == '2' && pct[2] == '5')) {
				uri->host = soup_uri_decoded_copy (uri_string,
								   hostend - uri_string, NULL);
			} else
Dan Winship's avatar
Dan Winship committed
356
				uri->host = g_strndup (uri_string, hostend - uri_string);
357
		} else {
358 359
			colon = memchr (uri_string, ':', path - uri_string);
			hostend = colon ? colon : path;
360 361
			uri->host = soup_uri_decoded_copy (uri_string,
							   hostend - uri_string, NULL);
362 363
		}

364 365 366 367 368 369 370 371 372 373
		if (colon && colon != path - 1) {
			char *portend;
			uri->port = strtoul (colon + 1, &portend, 10);
			if (portend != (char *)path) {
				soup_uri_free (uri);
				return NULL;
			}
		}

		uri_string = path;
374 375 376 377 378
	}

	/* Find query */
	question = memchr (uri_string, '?', end - uri_string);
	if (question) {
379 380
		uri->query = uri_normalized_copy (question + 1,
						  end - (question + 1),
381
						  NULL);
382
		end = question;
383
	}
384 385

	if (end != uri_string) {
386
		uri->path = uri_normalized_copy (uri_string, end - uri_string,
387
						 NULL);
388 389
	}

390
	/* Apply base URI. This is spelled out in RFC 3986. */
391 392 393 394
	if (base && !uri->scheme && uri->host)
		uri->scheme = base->scheme;
	else if (base && !uri->scheme) {
		uri->scheme = base->scheme;
395
		uri->user = g_strdup (base->user);
396
		uri->password = g_strdup (base->password);
397 398 399 400
		uri->host = g_strdup (base->host);
		uri->port = base->port;

		if (!uri->path) {
401 402
			uri->path = g_strdup (base->path);
			if (!uri->query)
403
				uri->query = g_strdup (base->query);
404 405 406
			remove_dot_segments = FALSE;
		} else if (*uri->path != '/') {
			char *newpath, *last;
407 408 409

			last = strrchr (base->path, '/');
			if (last) {
410 411
				newpath = g_strdup_printf ("%.*s%s",
							   (int)(last + 1 - base->path),
412 413 414 415 416 417 418
							   base->path,
							   uri->path);
			} else
				newpath = g_strdup_printf ("/%s", uri->path);

			g_free (uri->path);
			uri->path = newpath;
419 420 421
		}
	}

422
	if (remove_dot_segments && uri->path && *uri->path) {
423
		char *p, *q;
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

		/* Remove "./" where "." is a complete segment. */
		for (p = uri->path + 1; *p; ) {
			if (*(p - 1) == '/' &&
			    *p == '.' && *(p + 1) == '/')
				memmove (p, p + 2, strlen (p + 2) + 1);
			else
				p++;
		}
		/* Remove "." at end. */
		if (p > uri->path + 2 &&
		    *(p - 1) == '.' && *(p - 2) == '/')
			*(p - 1) = '\0';

		/* Remove "<segment>/../" where <segment> != ".." */
		for (p = uri->path + 1; *p; ) {
			if (!strncmp (p, "../", 3)) {
				p += 3;
				continue;
			}
			q = strchr (p + 1, '/');
			if (!q)
				break;
			if (strncmp (q, "/../", 4) != 0) {
				p = q + 1;
				continue;
			}
			memmove (p, q + 4, strlen (q + 4) + 1);
			p = uri->path + 1;
		}
		/* Remove "<segment>/.." at end where <segment> != ".." */
		q = strrchr (uri->path, '/');
456
		if (q && q != uri->path && !strcmp (q, "/..")) {
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
			p = q - 1;
			while (p > uri->path && *p != '/')
				p--;
			if (strncmp (p, "/../", 4) != 0)
				*(p + 1) = 0;
		}

		/* Remove extraneous initial "/.."s */
		while (!strncmp (uri->path, "/../", 4))
			memmove (uri->path, uri->path + 3, strlen (uri->path) - 2);
		if (!strcmp (uri->path, "/.."))
			uri->path[1] = '\0';
	}

	/* HTTP-specific stuff */
472 473
	if (uri->scheme == SOUP_URI_SCHEME_HTTP ||
	    uri->scheme == SOUP_URI_SCHEME_HTTPS) {
474 475
		if (!uri->path)
			uri->path = g_strdup ("/");
476
		if (!SOUP_URI_VALID_FOR_HTTP (uri)) {
477 478 479
			soup_uri_free (uri);
			return NULL;
		}
480 481
	}

482 483 484 485 486 487 488
	if (uri->scheme == SOUP_URI_SCHEME_FTP) {
		if (!uri->host) {
			soup_uri_free (uri);
			return NULL;
		}
	}

489
	if (!uri->port)
490
		uri->port = soup_scheme_default_port (uri->scheme);
491 492 493 494
	if (!uri->path)
		uri->path = g_strdup ("");

	return uri;
495 496
}

alex's avatar
alex committed
497
/**
498
 * soup_uri_new:
499
 * @uri_string: (allow-none): a URI
Alex Graveley's avatar
Alex Graveley committed
500
 *
501
 * Parses an absolute URI.
Alex Graveley's avatar
Alex Graveley committed
502
 *
503
 * You can also pass %NULL for @uri_string if you want to get back an
504 505 506
 * "empty" #SoupURI that you can fill in by hand. (You will need to
 * call at least soup_uri_set_scheme() and soup_uri_set_path(), since
 * those fields are required.)
507
 *
508 509
 * Return value: (nullable): a #SoupURI, or %NULL if the given string
 *  was found to be invalid.
alex's avatar
alex committed
510
 **/
511
SoupURI *
512
soup_uri_new (const char *uri_string)
alex's avatar
alex committed
513
{
514 515 516 517
	SoupURI *uri;

	if (!uri_string)
		return g_slice_new0 (SoupURI);
518

519 520 521
	uri = soup_uri_new_with_base (NULL, uri_string);
	if (!uri)
		return NULL;
522
	if (!SOUP_URI_IS_VALID (uri)) {
523
		soup_uri_free (uri);
524 525
		return NULL;
	}
alex's avatar
alex committed
526

527 528
	return uri;
}
alex's avatar
alex committed
529

530

531
char *
532
soup_uri_to_string_internal (SoupURI *uri, gboolean just_path_and_query,
533
			     gboolean include_password, gboolean force_port)
534 535 536
{
	GString *str;
	char *return_result;
alex's avatar
alex committed
537

538 539
	g_return_val_if_fail (uri != NULL, NULL);
	g_warn_if_fail (SOUP_URI_IS_VALID (uri));
540

541
	str = g_string_sized_new (40);
542

543
	if (uri->scheme && !just_path_and_query)
544
		g_string_append_printf (str, "%s:", uri->scheme);
545
	if (uri->host && !just_path_and_query) {
546 547
		g_string_append (str, "//");
		if (uri->user) {
548
			append_uri_encoded (str, uri->user, ":;@?/");
549 550 551 552
			if (uri->password && include_password) {
				g_string_append_c (str, ':');
				append_uri_encoded (str, uri->password, ";@?/");
			}
553 554
			g_string_append_c (str, '@');
		}
555
		if (strchr (uri->host, ':')) {
Dan Winship's avatar
Dan Winship committed
556 557
			const char *pct;

558
			g_string_append_c (str, '[');
Dan Winship's avatar
Dan Winship committed
559 560 561 562 563 564 565
			pct = strchr (uri->host, '%');
			if (pct) {
				g_string_append_printf (str, "%.*s%%25%s",
							(int) (pct - uri->host),
							uri->host, pct + 1);
			} else
				g_string_append (str, uri->host);
566 567 568
			g_string_append_c (str, ']');
		} else
			append_uri_encoded (str, uri->host, ":/");
569
		if (uri->port && (force_port || uri->port != soup_scheme_default_port (uri->scheme)))
570
			g_string_append_printf (str, ":%u", uri->port);
571 572
		if (!uri->path && (uri->query || uri->fragment))
			g_string_append_c (str, '/');
573 574 575 576
		else if ((!uri->path || !*uri->path) &&
			 (uri->scheme == SOUP_URI_SCHEME_HTTP ||
			  uri->scheme == SOUP_URI_SCHEME_HTTPS))
			g_string_append_c (str, '/');
alex's avatar
alex committed
577 578
	}

579
	if (uri->path && *uri->path)
580
		g_string_append (str, uri->path);
581 582
	else if (just_path_and_query)
		g_string_append_c (str, '/');
583 584 585

	if (uri->query) {
		g_string_append_c (str, '?');
586
		g_string_append (str, uri->query);
alex's avatar
alex committed
587
	}
588
	if (uri->fragment && !just_path_and_query) {
589
		g_string_append_c (str, '#');
590
		g_string_append (str, uri->fragment);
alex's avatar
alex committed
591
	}
alex's avatar
alex committed
592

593 594
	return_result = str->str;
	g_string_free (str, FALSE);
595

596
	return return_result;
alex's avatar
alex committed
597 598
}

599 600 601 602 603 604 605 606 607 608 609
/**
 * soup_uri_to_string:
 * @uri: a #SoupURI
 * @just_path_and_query: if %TRUE, output just the path and query portions
 *
 * Returns a string representing @uri.
 *
 * If @just_path_and_query is %TRUE, this concatenates the path and query
 * together. That is, it constructs the string that would be needed in
 * the Request-Line of an HTTP request for @uri.
 *
610 611 612
 * Note that the output will never contain a password, even if @uri
 * does.
 *
613 614 615 616 617
 * Return value: a string representing @uri, which the caller must free.
 **/
char *
soup_uri_to_string (SoupURI *uri, gboolean just_path_and_query)
{
618
	return soup_uri_to_string_internal (uri, just_path_and_query, FALSE, FALSE);
619 620
}

621 622
/**
 * soup_uri_copy:
623
 * @uri: a #SoupURI
624 625 626 627 628
 *
 * Copies @uri
 *
 * Return value: a copy of @uri, which must be freed with soup_uri_free()
 **/
629 630
SoupURI *
soup_uri_copy (SoupURI *uri)
alex's avatar
alex committed
631
{
632
	SoupURI *dup;
alex's avatar
alex committed
633

634 635
	g_return_val_if_fail (uri != NULL, NULL);
	g_warn_if_fail (SOUP_URI_IS_VALID (uri));
alex's avatar
alex committed
636

637 638
	dup = g_slice_new0 (SoupURI);
	dup->scheme   = uri->scheme;
639
	dup->user     = g_strdup (uri->user);
640
	dup->password = g_strdup (uri->password);
641 642 643 644 645
	dup->host     = g_strdup (uri->host);
	dup->port     = uri->port;
	dup->path     = g_strdup (uri->path);
	dup->query    = g_strdup (uri->query);
	dup->fragment = g_strdup (uri->fragment);
alex's avatar
alex committed
646 647 648 649

	return dup;
}

650
static inline gboolean
651
parts_equal (const char *one, const char *two, gboolean insensitive)
652
{
653
	if (!one && !two)
654
		return TRUE;
655 656
	if (!one || !two)
		return FALSE;
657
	return insensitive ? !g_ascii_strcasecmp (one, two) : !strcmp (one, two);
658
}
659

660 661
/**
 * soup_uri_equal:
662 663
 * @uri1: a #SoupURI
 * @uri2: another #SoupURI
664
 *
665
 * Tests whether or not @uri1 and @uri2 are equal in all parts
666 667 668
 *
 * Return value: %TRUE or %FALSE
 **/
669
gboolean 
670
soup_uri_equal (SoupURI *uri1, SoupURI *uri2)
671
{
672 673 674 675
	g_return_val_if_fail (uri1 != NULL, FALSE);
	g_return_val_if_fail (uri2 != NULL, FALSE);
	g_warn_if_fail (SOUP_URI_IS_VALID (uri1));
	g_warn_if_fail (SOUP_URI_IS_VALID (uri2));
676

677 678 679 680 681 682 683 684
	if (uri1->scheme != uri2->scheme                         ||
	    uri1->port   != uri2->port                           ||
	    !parts_equal (uri1->user, uri2->user, FALSE)         ||
	    !parts_equal (uri1->password, uri2->password, FALSE) ||
	    !parts_equal (uri1->host, uri2->host, TRUE)          ||
	    !parts_equal (uri1->path, uri2->path, FALSE)         ||
	    !parts_equal (uri1->query, uri2->query, FALSE)       ||
	    !parts_equal (uri1->fragment, uri2->fragment, FALSE))
685 686 687
		return FALSE;

	return TRUE;
688 689
}

690 691
/**
 * soup_uri_free:
692
 * @uri: a #SoupURI
693 694 695
 *
 * Frees @uri.
 **/
alex's avatar
alex committed
696
void
697
soup_uri_free (SoupURI *uri)
alex's avatar
alex committed
698
{
Alex Graveley's avatar
Alex Graveley committed
699
	g_return_if_fail (uri != NULL);
alex's avatar
alex committed
700 701

	g_free (uri->user);
702
	g_free (uri->password);
alex's avatar
alex committed
703 704
	g_free (uri->host);
	g_free (uri->path);
705 706
	g_free (uri->query);
	g_free (uri->fragment);
alex's avatar
alex committed
707

708
	g_slice_free (SoupURI, uri);
alex's avatar
alex committed
709
}
710

711 712
static void
append_uri_encoded (GString *str, const char *in, const char *extra_enc_chars)
713
{
714 715 716
	const unsigned char *s = (const unsigned char *)in;

	while (*s) {
717 718
		if (soup_char_is_uri_percent_encoded (*s) ||
		    soup_char_is_uri_gen_delims (*s) ||
719
		    (extra_enc_chars && strchr (extra_enc_chars, *s)))
720
			g_string_append_printf (str, "%%%02X", (int)*s++);
721 722 723 724 725 726 727 728
		else
			g_string_append_c (str, *s++);
	}
}

/**
 * soup_uri_encode:
 * @part: a URI part
729 730
 * @escape_extra: (allow-none): additional reserved characters to
 * escape (or %NULL)
731
 *
732 733 734
 * This %<!-- -->-encodes the given URI part and returns the escaped
 * version in allocated memory, which the caller must free when it is
 * done.
735 736
 *
 * Return value: the encoded URI part
737 738 739 740 741 742 743
 **/
char *
soup_uri_encode (const char *part, const char *escape_extra)
{
	GString *str;
	char *encoded;

744 745
	g_return_val_if_fail (part != NULL, NULL);

746 747 748 749
	str = g_string_new (NULL);
	append_uri_encoded (str, part, escape_extra);
	encoded = str->str;
	g_string_free (str, FALSE);
750

751
	return encoded;
752 753
}

754 755 756
#define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
#define HEXCHAR(s) ((XDIGIT (s[1]) << 4) + XDIGIT (s[2]))

757
char *
758
soup_uri_decoded_copy (const char *part, int length, int *decoded_length)
759 760
{
	unsigned char *s, *d;
761
	char *decoded;
762

763 764
	g_return_val_if_fail (part != NULL, NULL);

765
	decoded = g_strndup (part, length);
766 767 768 769 770
	s = d = (unsigned char *)decoded;
	do {
		if (*s == '%') {
			if (!g_ascii_isxdigit (s[1]) ||
			    !g_ascii_isxdigit (s[2])) {
771 772
				*d++ = *s;
				continue;
773 774 775 776 777 778 779
			}
			*d++ = HEXCHAR (s);
			s += 2;
		} else
			*d++ = *s;
	} while (*s++);

780 781 782
	if (decoded_length)
		*decoded_length = d - (unsigned char *)decoded - 1;

783 784 785
	return decoded;
}

786 787 788 789
/**
 * soup_uri_decode:
 * @part: a URI part
 *
790 791
 * Fully %<!-- -->-decodes @part.
 *
792 793 794 795 796
 * In the past, this would return %NULL if @part contained invalid
 * percent-encoding, but now it just ignores the problem (as
 * soup_uri_new() already did).
 *
 * Return value: the decoded URI part.
797
 */
798 799
char *
soup_uri_decode (const char *part)
800
{
801 802
	g_return_val_if_fail (part != NULL, NULL);

803
	return soup_uri_decoded_copy (part, strlen (part), NULL);
804
}
805

806
static char *
807
uri_normalized_copy (const char *part, int length,
808
		     const char *unescape_extra)
809 810 811
{
	unsigned char *s, *d, c;
	char *normalized = g_strndup (part, length);
812
	gboolean need_fixup = FALSE;
813

814 815 816
	if (!unescape_extra)
		unescape_extra = "";

817
	s = d = (unsigned char *)normalized;
818
	while (*s) {
819 820 821
		if (*s == '%') {
			if (!g_ascii_isxdigit (s[1]) ||
			    !g_ascii_isxdigit (s[2])) {
822
				*d++ = *s++;
823
				continue;
824 825 826
			}

			c = HEXCHAR (s);
827
			if (soup_char_is_uri_unreserved (c) ||
828
			    (c && strchr (unescape_extra, c))) {
829
				*d++ = c;
830
				s += 3;
831
			} else {
832 833 834 835
				/* We leave it unchanged. We used to uppercase percent-encoded
				 * triplets but we do not do it any more as RFC3986 Section 6.2.2.1
				 * says that they only SHOULD be case normalized.
				 */
836
				*d++ = *s++;
837
				*d++ = *s++;
838
				*d++ = *s++;
839
			}
840
		} else {
841 842
			if (!g_ascii_isgraph (*s) &&
			    !strchr (unescape_extra, *s))
843
				need_fixup = TRUE;
844
			*d++ = *s++;
845
		}
846 847
	}
	*d = '\0';
848

849
	if (need_fixup) {
850 851 852
		GString *fixed;

		fixed = g_string_new (NULL);
853 854
		s = (guchar *)normalized;
		while (*s) {
855 856
			if (g_ascii_isgraph (*s) ||
			    strchr (unescape_extra, *s))
857 858 859 860
				g_string_append_c (fixed, *s);
			else
				g_string_append_printf (fixed, "%%%02X", (int)*s);
			s++;
861 862 863
		}
		g_free (normalized);
		normalized = g_string_free (fixed, FALSE);
864 865
	}

866
	return normalized;
867
}
868

869 870 871
/**
 * soup_uri_normalize:
 * @part: a URI part
872
 * @unescape_extra: (allow-none): reserved characters to unescape (or %NULL)
873 874
 *
 * %<!-- -->-decodes any "unreserved" characters (or characters in
875 876
 * @unescape_extra) in @part, and %<!-- -->-encodes any non-ASCII
 * characters, spaces, and non-printing characters in @part.
877 878 879 880 881 882 883 884 885 886 887
 *
 * "Unreserved" characters are those that are not allowed to be used
 * for punctuation according to the URI spec. For example, letters are
 * unreserved, so soup_uri_normalize() will turn
 * <literal>http://example.com/foo/b%<!-- -->61r</literal> into
 * <literal>http://example.com/foo/bar</literal>, which is guaranteed
 * to mean the same thing. However, "/" is "reserved", so
 * <literal>http://example.com/foo%<!-- -->2Fbar</literal> would not
 * be changed, because it might mean something different to the
 * server.
 *
888 889 890 891 892
 * In the past, this would return %NULL if @part contained invalid
 * percent-encoding, but now it just ignores the problem (as
 * soup_uri_new() already did).
 *
 * Return value: the normalized URI part
893 894 895 896
 */
char *
soup_uri_normalize (const char *part, const char *unescape_extra)
{
897 898
	g_return_val_if_fail (part != NULL, NULL);

899
	return uri_normalized_copy (part, strlen (part), unescape_extra);
900 901 902
}


903 904
/**
 * soup_uri_uses_default_port:
905
 * @uri: a #SoupURI
906
 *
907
 * Tests if @uri uses the default port for its scheme. (Eg, 80 for
908
 * http.) (This only works for http, https and ftp; libsoup does not know
909
 * the default ports of other protocols.)
910 911 912
 *
 * Return value: %TRUE or %FALSE
 **/
913
gboolean
914 915
soup_uri_uses_default_port (SoupURI *uri)
{
916
	g_return_val_if_fail (uri != NULL, FALSE);
917
	g_warn_if_fail (SOUP_URI_IS_VALID (uri));
918

919 920 921
	return uri->port == soup_scheme_default_port (uri->scheme);
}

922 923 924 925 926 927 928 929 930 931 932 933 934
/**
 * soup_uri_get_scheme:
 * @uri: a #SoupURI
 *
 * Gets @uri's scheme.
 *
 * Return value: @uri's scheme.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_scheme (SoupURI *uri)
{
935
	g_return_val_if_fail (uri != NULL, NULL);
936

937 938 939
	return uri->scheme;
}

940 941 942 943 944 945 946 947 948 949 950
/**
 * soup_uri_set_scheme:
 * @uri: a #SoupURI
 * @scheme: the URI scheme
 *
 * Sets @uri's scheme to @scheme. This will also set @uri's port to
 * the default port for @scheme, if known.
 **/
void
soup_uri_set_scheme (SoupURI *uri, const char *scheme)
{
951 952 953
	g_return_if_fail (uri != NULL);
	g_return_if_fail (scheme != NULL);

954
	uri->scheme = soup_uri_parse_scheme (scheme, strlen (scheme));
955 956 957
	uri->port = soup_scheme_default_port (uri->scheme);
}

958 959 960 961 962 963 964 965 966 967 968 969 970
/**
 * soup_uri_get_user:
 * @uri: a #SoupURI
 *
 * Gets @uri's user.
 *
 * Return value: @uri's user.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_user (SoupURI *uri)
{
971
	g_return_val_if_fail (uri != NULL, NULL);
972

973 974 975
	return uri->user;
}

976 977 978
/**
 * soup_uri_set_user:
 * @uri: a #SoupURI
979
 * @user: (allow-none): the username, or %NULL
980 981 982 983 984 985
 *
 * Sets @uri's user to @user.
 **/
void
soup_uri_set_user (SoupURI *uri, const char *user)
{
986 987
	g_return_if_fail (uri != NULL);

988 989 990 991
	g_free (uri->user);
	uri->user = g_strdup (user);
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/**
 * soup_uri_get_password:
 * @uri: a #SoupURI
 *
 * Gets @uri's password.
 *
 * Return value: @uri's password.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_password (SoupURI *uri)
{
1005
	g_return_val_if_fail (uri != NULL, NULL);
1006

1007 1008 1009
	return uri->password;
}

1010 1011 1012
/**
 * soup_uri_set_password:
 * @uri: a #SoupURI
1013
 * @password: (allow-none): the password, or %NULL
1014 1015 1016 1017 1018 1019
 *
 * Sets @uri's password to @password.
 **/
void
soup_uri_set_password (SoupURI *uri, const char *password)
{
1020 1021
	g_return_if_fail (uri != NULL);

1022 1023 1024 1025
	g_free (uri->password);
	uri->password = g_strdup (password);
}

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
/**
 * soup_uri_get_host:
 * @uri: a #SoupURI
 *
 * Gets @uri's host.
 *
 * Return value: @uri's host.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_host (SoupURI *uri)
{
1039
	g_return_val_if_fail (uri != NULL, NULL);
1040

1041 1042 1043
	return uri->host;
}

1044 1045 1046
/**
 * soup_uri_set_host:
 * @uri: a #SoupURI
1047
 * @host: (allow-none): the hostname or IP address, or %NULL
1048 1049 1050 1051 1052 1053
 *
 * Sets @uri's host to @host.
 *
 * If @host is an IPv6 IP address, it should not include the brackets
 * required by the URI syntax; they will be added automatically when
 * converting @uri to a string.
1054 1055
 *
 * http and https URIs should not have a %NULL @host.
1056 1057 1058
 **/
void
soup_uri_set_host (SoupURI *uri, const char *host)
1059
{
1060 1061
	g_return_if_fail (uri != NULL);

1062 1063 1064 1065
	g_free (uri->host);
	uri->host = g_strdup (host);
}

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
/**
 * soup_uri_get_port:
 * @uri: a #SoupURI
 *
 * Gets @uri's port.
 *
 * Return value: @uri's port.
 *
 * Since: 2.32
 **/
guint
soup_uri_get_port (SoupURI *uri)
{
1079
	g_return_val_if_fail (uri != NULL, 0);
1080

1081 1082 1083
	return uri->port;
}

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
/**
 * soup_uri_set_port:
 * @uri: a #SoupURI
 * @port: the port, or 0
 *
 * Sets @uri's port to @port. If @port is 0, @uri will not have an
 * explicitly-specified port.
 **/
void
soup_uri_set_port (SoupURI *uri, guint port)
{
1095 1096
	g_return_if_fail (uri != NULL);

1097 1098 1099
	uri->port = port;
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/**
 * soup_uri_get_path:
 * @uri: a #SoupURI
 *
 * Gets @uri's path.
 *
 * Return value: @uri's path.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_path (SoupURI *uri)
{
1113
	g_return_val_if_fail (uri != NULL, NULL);
1114

1115 1116 1117
	return uri->path;
}

1118 1119 1120
/**
 * soup_uri_set_path:
 * @uri: a #SoupURI
1121
 * @path: the non-%NULL path
1122 1123 1124 1125 1126 1127
 *
 * Sets @uri's path to @path.
 **/
void
soup_uri_set_path (SoupURI *uri, const char *path)
{
1128
	g_return_if_fail (uri != NULL);
1129 1130 1131 1132 1133 1134

	/* We allow a NULL path for compatibility, but warn about it. */
	if (!path) {
		g_warn_if_fail (path != NULL);
		path = "";
	}
1135

1136 1137 1138 1139
	g_free (uri->path);
	uri->path = g_strdup (path);
}

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
/**
 * soup_uri_get_query:
 * @uri: a #SoupURI
 *
 * Gets @uri's query.
 *
 * Return value: @uri's query.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_query (SoupURI *uri)
{
1153
	g_return_val_if_fail (uri != NULL, NULL);
1154

1155 1156 1157
	return uri->query;
}

1158 1159 1160
/**
 * soup_uri_set_query:
 * @uri: a #SoupURI
1161
 * @query: (allow-none): the query
1162 1163 1164 1165 1166 1167
 *
 * Sets @uri's query to @query.
 **/
void
soup_uri_set_query (SoupURI *uri, const char *query)
{
1168 1169
	g_return_if_fail (uri != NULL);

1170 1171 1172 1173 1174 1175 1176
	g_free (uri->query);
	uri->query = g_strdup (query);
}

/**
 * soup_uri_set_query_from_form:
 * @uri: a #SoupURI
1177 1178
 * @form: (element-type utf8 utf8): a #GHashTable containing HTML form
 * information
1179 1180
 *
 * Sets @uri's query to the result of encoding @form according to the
Dan Winship's avatar
Dan Winship committed
1181
 * HTML form rules. See soup_form_encode_hash() for more information.
1182 1183 1184 1185
 **/
void
soup_uri_set_query_from_form (SoupURI *uri, GHashTable *form)
{
1186 1187
	g_return_if_fail (uri != NULL);

1188
	g_free (uri->query);
1189
	uri->query = soup_form_encode_hash (form);
1190 1191
}

Dan Winship's avatar
Dan Winship committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
/**
 * soup_uri_set_query_from_fields:
 * @uri: a #SoupURI
 * @first_field: name of the first form field to encode into query
 * @...: value of @first_field, followed by additional field names
 * and values, terminated by %NULL.
 *
 * Sets @uri's query to the result of encoding the given form fields
 * and values according to the * HTML form rules. See
 * soup_form_encode() for more information.
 **/
void
soup_uri_set_query_from_fields (SoupURI    *uri,
				const char *first_field,
				...)
{
	va_list args;

1210 1211
	g_return_if_fail (uri != NULL);

Dan Winship's avatar
Dan Winship committed
1212 1213 1214 1215 1216 1217
	g_free (uri->query);
	va_start (args, first_field);
	uri->query = soup_form_encode_valist (first_field, args);
	va_end (args);
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
/**
 * soup_uri_get_fragment:
 * @uri: a #SoupURI
 *
 * Gets @uri's fragment.
 *
 * Return value: @uri's fragment.
 *
 * Since: 2.32
 **/
const char *
soup_uri_get_fragment (SoupURI *uri)
{
1231
	g_return_val_if_fail (uri != NULL, NULL);
1232

1233 1234 1235
	return uri->fragment;
}

1236 1237 1238
/**
 * soup_uri_set_fragment:
 * @uri: a #SoupURI
1239
 * @fragment: (allow-none): the fragment
1240 1241 1242 1243 1244 1245
 *
 * Sets @uri's fragment to @fragment.
 **/
void
soup_uri_set_fragment (SoupURI *uri, const char *fragment)
{
1246 1247
	g_return_if_fail (uri != NULL);

1248 1249 1250 1251
	g_free (uri->fragment);
	uri->fragment = g_strdup (fragment);
}

1252 1253
/**
 * soup_uri_copy_host:
1254
 * @uri: a #SoupURI
1255 1256 1257
 *
 * Makes a copy of @uri, considering only the protocol, host, and port
 *
1258
 * Return value: the new #SoupURI
1259
 *
1260
 * Since: 2.28
1261 1262 1263 1264 1265 1266
 **/
SoupURI *
soup_uri_copy_host (SoupURI *uri)
{
	SoupURI *dup;

1267 1268
	g_return_val_if_fail (uri != NULL, NULL);
	g_warn_if_fail (SOUP_URI_IS_VALID (uri));
1269 1270 1271 1272 1273

	dup = soup_uri_new (NULL);
	dup->scheme = uri->scheme;
	dup->host   = g_strdup (uri->host);
	dup->port   = uri->port;
1274
	dup->path   = g_strdup ("");
1275 1276 1277 1278 1279 1280

	return dup;
}

/**
 * soup_uri_host_hash:
1281
 * @key: (type Soup.URI): a #SoupURI with a non-%NULL @host member
1282 1283 1284 1285 1286
 *
 * Hashes @key, considering only the scheme, host, and port.
 *
 * Return value: a hash
 *
1287
 * Since: 2.28
1288 1289 1290 1291 1292 1293
 **/
guint
soup_uri_host_hash (gconstpointer key)
{
	const SoupURI *uri = key;

1294 1295
	g_return_val_if_fail (uri != NULL && uri->host != NULL, 0);
	g_warn_if_fail (SOUP_URI_IS_VALID (uri));
1296

1297 1298 1299 1300 1301 1302
	return GPOINTER_TO_UINT (uri->scheme) + uri->port +
		soup_str_case_hash (uri->host);
}

/**
 * soup_uri_host_equal:
1303 1304
 * @v1: (type Soup.URI): a #SoupURI with a non-%NULL @host member
 * @v2: (type Soup.URI): a #SoupURI with a non-%NULL @host member
1305 1306 1307 1308 1309 1310
 *
 * Compares @v1 and @v2, considering only the scheme, host, and port.
 *
 * Return value: whether or not the URIs are equal in scheme, host,
 * and port.
 *
1311
 * Since: 2.28
1312 1313 1314 1315 1316 1317 1318
 **/
gboolean
soup_uri_host_equal (gconstpointer v1, gconstpointer v2)
{
	const SoupURI *one = v1;
	const SoupURI *two = v2;

1319 1320
	g_return_val_if_fail (one != NULL && two != NULL, one == two);
	g_return_val_if_fail (one->host != NULL && two->host != NULL, one->host == two->host);
1321 1322
	g_warn_if_fail (SOUP_URI_IS_VALID (one));
	g_warn_if_fail (SOUP_URI_IS_VALID (two));
1323

1324 1325 1326 1327 1328 1329 1330 1331
	if (one->scheme != two->scheme)
		return FALSE;
	if (one->port != two->port)
		return FALSE;

	return g_ascii_strcasecmp (one->host, two->host) == 0;
}

1332 1333 1334 1335 1336
gboolean
soup_uri_is_http (SoupURI *uri, char **aliases)
{
	int i;

1337 1338
	if (uri->scheme == SOUP_URI_SCHEME_HTTP ||
	    uri->scheme == SOUP_URI_SCHEME_WS)
1339
		return TRUE;
1340 1341
	else if (uri->scheme == SOUP_URI_SCHEME_HTTPS ||
		 uri->scheme == SOUP_URI_SCHEME_WSS)
1342 1343 1344 1345 1346 1347 1348 1349 1350
		return FALSE;
	else if (!aliases)
		return FALSE;

	for (i = 0; aliases[i]; i++) {
		if (uri->scheme == aliases[i])
			return TRUE;
	}

1351
	if (aliases[0] && !aliases[1] && !strcmp (aliases[0], "*"))
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
		return TRUE;
	else
		return FALSE;
}

gboolean
soup_uri_is_https (SoupURI *uri, char **aliases)
{
	int i;

1362 1363
	if (uri->scheme == SOUP_URI_SCHEME_HTTPS ||
	    uri->scheme == SOUP_URI_SCHEME_WSS)
1364
		return TRUE;
1365 1366
	else if (uri->scheme == SOUP_URI_SCHEME_HTTP ||
		 uri->scheme == SOUP_URI_SCHEME_WS)
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		return FALSE;
	else if (!aliases)
		return FALSE;

	for (i = 0; aliases[i]; i++) {
		if (uri->scheme == aliases[i])
			return TRUE;
	}

	return FALSE;
}

1379
G_DEFINE_BOXED_TYPE (SoupURI, soup_uri, soup_uri_copy, soup_uri_free)