1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* soup-uri.c : utility functions to parse URLs */
5 * Copyright 1999-2003 Ximian, Inc.
13 #include "soup-misc-private.h"
14 #include "soup-form.h"
15 #include "soup-misc.h"
19 * @short_description: URIs
21 * A #SoupURI represents a (parsed) URI.
23 * Many applications will not need to use #SoupURI directly at all; on
24 * the client side, soup_message_new() takes a stringified URI, and on
25 * the server side, the path and query components are provided for you
26 * in the server callback.
31 * @scheme: the URI scheme (eg, "http")
32 * @user: a username, or %NULL
33 * @password: a password, or %NULL
34 * @host: the hostname or IP address
35 * @port: the port number on @host
36 * @path: the path on @host
37 * @query: a query for @path, or %NULL
38 * @fragment: a fragment identifier within @path, or %NULL
40 * A #SoupURI represents a (parsed) URI. #SoupURI supports RFC 3986
41 * (URI Generic Syntax), and can parse any valid URI. However, libsoup
42 * only uses "http" and "https" URIs internally; You can use
43 * SOUP_URI_VALID_FOR_HTTP() to test if a #SoupURI is a valid HTTP
46 * @scheme will always be set in any URI. It is an interned string and
47 * is always all lowercase. (If you parse a URI with a non-lowercase
48 * scheme, it will be converted to lowercase.) The macros
49 * %SOUP_URI_SCHEME_HTTP and %SOUP_URI_SCHEME_HTTPS provide the
50 * interned values for "http" and "https" and can be compared against
53 * @user and @password are parsed as defined in the older URI specs
54 * (ie, separated by a colon; RFC 3986 only talks about a single
55 * "userinfo" field). Note that @password is not included in the
56 * output of soup_uri_to_string(). libsoup does not normally use these
57 * fields; authentication is handled via #SoupSession signals.
59 * @host contains the hostname, and @port the port specified in the
60 * URI. If the URI doesn't contain a hostname, @host will be %NULL,
61 * and if it doesn't specify a port, @port may be 0. However, for
62 * "http" and "https" URIs, @host is guaranteed to be non-%NULL
63 * (trying to parse an http URI with no @host will return %NULL), and
64 * @port will always be non-0 (because libsoup knows the default value
65 * to use when it is not specified in the URI).
67 * @path is always non-%NULL. For http/https URIs, @path will never be
68 * an empty string either; if the input URI has no path, the parsed
69 * #SoupURI will have a @path of "/".
71 * @query and @fragment are optional for all URI types.
72 * soup_form_decode() may be useful for parsing @query.
74 * Note that @path, @query, and @fragment may contain
75 * %<!-- -->-encoded characters. soup_uri_new() calls
76 * soup_uri_normalize() on them, but not soup_uri_decode(). This is
77 * necessary to ensure that soup_uri_to_string() will generate a URI
78 * that has exactly the same meaning as the original. (In theory,
79 * #SoupURI should leave @user, @password, and @host partially-encoded
80 * as well, but this would be more annoying than useful.)
87 * Tests whether @uri is a valid #SoupURI; that is, that it is non-%NULL
88 * and its @scheme and @path members are also non-%NULL.
90 * This macro does not check whether http and https URIs have a non-%NULL
93 * Return value: %TRUE if @uri is valid for use.
99 * SOUP_URI_VALID_FOR_HTTP:
102 * Tests if @uri is a valid #SoupURI for HTTP communication; that is, if
103 * it can be used to construct a #SoupMessage.
105 * Return value: %TRUE if @uri is a valid "http" or "https" URI.
110 static void append_uri_encoded (GString *str, const char *in, const char *extra_enc_chars);
111 static char *uri_normalized_copy (const char *str, int length, const char *unescape_extra);
113 gpointer _SOUP_URI_SCHEME_HTTP, _SOUP_URI_SCHEME_HTTPS;
114 gpointer _SOUP_URI_SCHEME_FTP;
115 gpointer _SOUP_URI_SCHEME_FILE, _SOUP_URI_SCHEME_DATA;
117 static inline const char *
118 soup_uri_parse_scheme (const char *scheme, int len)
120 if (len == 4 && !g_ascii_strncasecmp (scheme, "http", len)) {
121 return SOUP_URI_SCHEME_HTTP;
122 } else if (len == 5 && !g_ascii_strncasecmp (scheme, "https", len)) {
123 return SOUP_URI_SCHEME_HTTPS;
127 lower_scheme = g_ascii_strdown (scheme, len);
128 scheme = g_intern_static_string (lower_scheme);
129 if (scheme != (const char *)lower_scheme)
130 g_free (lower_scheme);
136 soup_scheme_default_port (const char *scheme)
138 if (scheme == SOUP_URI_SCHEME_HTTP)
140 else if (scheme == SOUP_URI_SCHEME_HTTPS)
142 else if (scheme == SOUP_URI_SCHEME_FTP)
149 * soup_uri_new_with_base:
151 * @uri_string: the URI
153 * Parses @uri_string relative to @base.
155 * Return value: a parsed #SoupURI.
158 soup_uri_new_with_base (SoupURI *base, const char *uri_string)
160 SoupURI *uri, fixed_base;
161 const char *end, *hash, *colon, *at, *path, *question;
162 const char *p, *hostend;
163 gboolean remove_dot_segments = TRUE;
166 g_return_val_if_fail (uri_string != NULL, NULL);
168 /* Allow a %NULL path in @base, for compatibility */
169 if (base && base->scheme && !base->path) {
170 g_warn_if_fail (SOUP_URI_IS_VALID (base));
172 memcpy (&fixed_base, base, sizeof (SoupURI));
173 fixed_base.path = "";
177 g_return_val_if_fail (base == NULL || SOUP_URI_IS_VALID (base), NULL);
179 /* First some cleanup steps (which are supposed to all be no-ops,
180 * but...). Skip initial whitespace, strip out internal tabs and
181 * line breaks, and ignore trailing whitespace.
183 while (g_ascii_isspace (*uri_string))
186 len = strcspn (uri_string, "\t\n\r");
187 if (uri_string[len]) {
188 char *clean = g_malloc (strlen (uri_string) + 1), *d;
191 for (s = uri_string, d = clean; *s; s++) {
192 if (*s != '\t' && *s != '\n' && *s != '\r')
197 uri = soup_uri_new_with_base (base, clean);
201 end = uri_string + len;
202 while (end > uri_string && g_ascii_isspace (end[-1]))
205 uri = g_slice_new0 (SoupURI);
208 hash = strchr (uri_string, '#');
210 uri->fragment = uri_normalized_copy (hash + 1, end - hash + 1,
215 /* Find scheme: initial [a-z+.-]* substring until ":" */
217 while (p < end && (g_ascii_isalpha (*p) ||
218 *p == '.' || *p == '+' || *p == '-'))
221 if (p > uri_string && *p == ':') {
222 uri->scheme = soup_uri_parse_scheme (uri_string, p - uri_string);
226 if (uri_string == end && !base && !uri->fragment) {
227 uri->path = g_strdup ("");
231 /* Check for authority */
232 if (strncmp (uri_string, "//", 2) == 0) {
235 path = uri_string + strcspn (uri_string, "/?#");
238 at = strchr (uri_string, '@');
239 if (at && at < path) {
240 colon = strchr (uri_string, ':');
241 if (colon && colon < at) {
242 uri->password = uri_decoded_copy (colon + 1,
245 uri->password = NULL;
249 uri->user = uri_decoded_copy (uri_string,
253 uri->user = uri->password = NULL;
255 /* Find host and port. */
256 if (*uri_string == '[') {
258 hostend = strchr (uri_string, ']');
259 if (!hostend || hostend > path) {
263 if (*(hostend + 1) == ':')
268 colon = memchr (uri_string, ':', path - uri_string);
269 hostend = colon ? colon : path;
272 uri->host = uri_decoded_copy (uri_string, hostend - uri_string);
274 if (colon && colon != path - 1) {
276 uri->port = strtoul (colon + 1, &portend, 10);
277 if (portend != (char *)path) {
287 question = memchr (uri_string, '?', end - uri_string);
289 uri->query = uri_normalized_copy (question + 1,
290 end - (question + 1),
295 if (end != uri_string) {
296 uri->path = uri_normalized_copy (uri_string, end - uri_string,
300 /* Apply base URI. This is spelled out in RFC 3986. */
301 if (base && !uri->scheme && uri->host)
302 uri->scheme = base->scheme;
303 else if (base && !uri->scheme) {
304 uri->scheme = base->scheme;
305 uri->user = g_strdup (base->user);
306 uri->password = g_strdup (base->password);
307 uri->host = g_strdup (base->host);
308 uri->port = base->port;
311 uri->path = g_strdup (base->path);
313 uri->query = g_strdup (base->query);
314 remove_dot_segments = FALSE;
315 } else if (*uri->path != '/') {
316 char *newpath, *last;
318 last = strrchr (base->path, '/');
320 newpath = g_strdup_printf ("%.*s%s",
321 (int)(last + 1 - base->path),
325 newpath = g_strdup_printf ("/%s", uri->path);
332 if (remove_dot_segments && uri->path && *uri->path) {
335 /* Remove "./" where "." is a complete segment. */
336 for (p = uri->path + 1; *p; ) {
337 if (*(p - 1) == '/' &&
338 *p == '.' && *(p + 1) == '/')
339 memmove (p, p + 2, strlen (p + 2) + 1);
343 /* Remove "." at end. */
344 if (p > uri->path + 2 &&
345 *(p - 1) == '.' && *(p - 2) == '/')
348 /* Remove "<segment>/../" where <segment> != ".." */
349 for (p = uri->path + 1; *p; ) {
350 if (!strncmp (p, "../", 3)) {
354 q = strchr (p + 1, '/');
357 if (strncmp (q, "/../", 4) != 0) {
361 memmove (p, q + 4, strlen (q + 4) + 1);
364 /* Remove "<segment>/.." at end where <segment> != ".." */
365 q = strrchr (uri->path, '/');
366 if (q && !strcmp (q, "/..")) {
368 while (p > uri->path && *p != '/')
370 if (strncmp (p, "/../", 4) != 0)
374 /* Remove extraneous initial "/.."s */
375 while (!strncmp (uri->path, "/../", 4))
376 memmove (uri->path, uri->path + 3, strlen (uri->path) - 2);
377 if (!strcmp (uri->path, "/.."))
381 /* HTTP-specific stuff */
382 if (uri->scheme == SOUP_URI_SCHEME_HTTP ||
383 uri->scheme == SOUP_URI_SCHEME_HTTPS) {
385 uri->path = g_strdup ("/");
386 if (!SOUP_URI_VALID_FOR_HTTP (uri)) {
392 if (uri->scheme == SOUP_URI_SCHEME_FTP) {
400 uri->port = soup_scheme_default_port (uri->scheme);
402 uri->path = g_strdup ("");
409 * @uri_string: (allow-none): a URI
411 * Parses an absolute URI.
413 * You can also pass %NULL for @uri_string if you want to get back an
414 * "empty" #SoupURI that you can fill in by hand. (You will need to
415 * call at least soup_uri_set_scheme() and soup_uri_set_path(), since
416 * those fields are required.)
418 * Return value: a #SoupURI, or %NULL if the given string was found to be
422 soup_uri_new (const char *uri_string)
427 return g_slice_new0 (SoupURI);
429 uri = soup_uri_new_with_base (NULL, uri_string);
432 if (!SOUP_URI_IS_VALID (uri)) {
442 * soup_uri_to_string:
444 * @just_path_and_query: if %TRUE, output just the path and query portions
446 * Returns a string representing @uri.
448 * If @just_path_and_query is %TRUE, this concatenates the path and query
449 * together. That is, it constructs the string that would be needed in
450 * the Request-Line of an HTTP request for @uri.
452 * Return value: a string representing @uri, which the caller must free.
455 soup_uri_to_string (SoupURI *uri, gboolean just_path_and_query)
460 g_return_val_if_fail (uri != NULL, NULL);
461 g_warn_if_fail (SOUP_URI_IS_VALID (uri));
463 str = g_string_sized_new (20);
465 if (uri->scheme && !just_path_and_query)
466 g_string_append_printf (str, "%s:", uri->scheme);
467 if (uri->host && !just_path_and_query) {
468 g_string_append (str, "//");
470 append_uri_encoded (str, uri->user, ":;@?/");
471 g_string_append_c (str, '@');
473 if (strchr (uri->host, ':')) {
474 g_string_append_c (str, '[');
475 g_string_append (str, uri->host);
476 g_string_append_c (str, ']');
478 append_uri_encoded (str, uri->host, ":/");
479 if (uri->port && uri->port != soup_scheme_default_port (uri->scheme))
480 g_string_append_printf (str, ":%u", uri->port);
481 if (!uri->path && (uri->query || uri->fragment))
482 g_string_append_c (str, '/');
485 if (uri->path && *uri->path)
486 g_string_append (str, uri->path);
489 g_string_append_c (str, '?');
490 g_string_append (str, uri->query);
492 if (uri->fragment && !just_path_and_query) {
493 g_string_append_c (str, '#');
494 g_string_append (str, uri->fragment);
497 return_result = str->str;
498 g_string_free (str, FALSE);
500 return return_result;
509 * Return value: a copy of @uri, which must be freed with soup_uri_free()
512 soup_uri_copy (SoupURI *uri)
516 g_return_val_if_fail (uri != NULL, NULL);
517 g_warn_if_fail (SOUP_URI_IS_VALID (uri));
519 dup = g_slice_new0 (SoupURI);
520 dup->scheme = uri->scheme;
521 dup->user = g_strdup (uri->user);
522 dup->password = g_strdup (uri->password);
523 dup->host = g_strdup (uri->host);
524 dup->port = uri->port;
525 dup->path = g_strdup (uri->path);
526 dup->query = g_strdup (uri->query);
527 dup->fragment = g_strdup (uri->fragment);
532 static inline gboolean
533 parts_equal (const char *one, const char *two, gboolean insensitive)
539 return insensitive ? !g_ascii_strcasecmp (one, two) : !strcmp (one, two);
545 * @uri2: another #SoupURI
547 * Tests whether or not @uri1 and @uri2 are equal in all parts
549 * Return value: %TRUE or %FALSE
552 soup_uri_equal (SoupURI *uri1, SoupURI *uri2)
554 g_return_val_if_fail (uri1 != NULL, FALSE);
555 g_return_val_if_fail (uri2 != NULL, FALSE);
556 g_warn_if_fail (SOUP_URI_IS_VALID (uri1));
557 g_warn_if_fail (SOUP_URI_IS_VALID (uri2));
559 if (uri1->scheme != uri2->scheme ||
560 uri1->port != uri2->port ||
561 !parts_equal (uri1->user, uri2->user, FALSE) ||
562 !parts_equal (uri1->password, uri2->password, FALSE) ||
563 !parts_equal (uri1->host, uri2->host, TRUE) ||
564 !parts_equal (uri1->path, uri2->path, FALSE) ||
565 !parts_equal (uri1->query, uri2->query, FALSE) ||
566 !parts_equal (uri1->fragment, uri2->fragment, FALSE))
579 soup_uri_free (SoupURI *uri)
581 g_return_if_fail (uri != NULL);
584 g_free (uri->password);
588 g_free (uri->fragment);
590 g_slice_free (SoupURI, uri);
594 append_uri_encoded (GString *str, const char *in, const char *extra_enc_chars)
596 const unsigned char *s = (const unsigned char *)in;
599 if (soup_char_is_uri_percent_encoded (*s) ||
600 soup_char_is_uri_gen_delims (*s) ||
601 (extra_enc_chars && strchr (extra_enc_chars, *s)))
602 g_string_append_printf (str, "%%%02X", (int)*s++);
604 g_string_append_c (str, *s++);
611 * @escape_extra: (allow-none): additional reserved characters to
614 * This %<!-- -->-encodes the given URI part and returns the escaped
615 * version in allocated memory, which the caller must free when it is
618 * Return value: the encoded URI part
621 soup_uri_encode (const char *part, const char *escape_extra)
626 g_return_val_if_fail (part != NULL, NULL);
628 str = g_string_new (NULL);
629 append_uri_encoded (str, part, escape_extra);
631 g_string_free (str, FALSE);
636 #define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
637 #define HEXCHAR(s) ((XDIGIT (s[1]) << 4) + XDIGIT (s[2]))
640 uri_decoded_copy (const char *part, int length)
642 unsigned char *s, *d;
643 char *decoded = g_strndup (part, length);
645 g_return_val_if_fail (part != NULL, NULL);
647 s = d = (unsigned char *)decoded;
650 if (!g_ascii_isxdigit (s[1]) ||
651 !g_ascii_isxdigit (s[2])) {
668 * Fully %<!-- -->-decodes @part.
670 * In the past, this would return %NULL if @part contained invalid
671 * percent-encoding, but now it just ignores the problem (as
672 * soup_uri_new() already did).
674 * Return value: the decoded URI part.
677 soup_uri_decode (const char *part)
679 g_return_val_if_fail (part != NULL, NULL);
681 return uri_decoded_copy (part, strlen (part));
685 uri_normalized_copy (const char *part, int length,
686 const char *unescape_extra)
688 unsigned char *s, *d, c;
689 char *normalized = g_strndup (part, length);
690 gboolean need_fixup = FALSE;
692 s = d = (unsigned char *)normalized;
695 if (!g_ascii_isxdigit (s[1]) ||
696 !g_ascii_isxdigit (s[2])) {
702 if (soup_char_is_uri_unreserved (c) ||
703 (unescape_extra && strchr (unescape_extra, c))) {
707 /* We leave it unchanged. We used to uppercase percent-encoded
708 * triplets but we do not do it any more as RFC3986 Section 6.2.2.1
709 * says that they only SHOULD be case normalized.
716 if (!g_ascii_isgraph (*s))
725 fixed = g_string_new (NULL);
726 s = (guchar *)normalized;
728 if (g_ascii_isgraph (*s))
729 g_string_append_c (fixed, *s);
731 g_string_append_printf (fixed, "%%%02X", (int)*s);
735 normalized = g_string_free (fixed, FALSE);
742 * soup_uri_normalize:
744 * @unescape_extra: reserved characters to unescape (or %NULL)
746 * %<!-- -->-decodes any "unreserved" characters (or characters in
747 * @unescape_extra) in @part.
749 * "Unreserved" characters are those that are not allowed to be used
750 * for punctuation according to the URI spec. For example, letters are
751 * unreserved, so soup_uri_normalize() will turn
752 * <literal>http://example.com/foo/b%<!-- -->61r</literal> into
753 * <literal>http://example.com/foo/bar</literal>, which is guaranteed
754 * to mean the same thing. However, "/" is "reserved", so
755 * <literal>http://example.com/foo%<!-- -->2Fbar</literal> would not
756 * be changed, because it might mean something different to the
759 * In the past, this would return %NULL if @part contained invalid
760 * percent-encoding, but now it just ignores the problem (as
761 * soup_uri_new() already did).
763 * Return value: the normalized URI part
766 soup_uri_normalize (const char *part, const char *unescape_extra)
768 g_return_val_if_fail (part != NULL, NULL);
770 return uri_normalized_copy (part, strlen (part), unescape_extra);
775 * soup_uri_uses_default_port:
778 * Tests if @uri uses the default port for its scheme. (Eg, 80 for
779 * http.) (This only works for http, https and ftp; libsoup does not know
780 * the default ports of other protocols.)
782 * Return value: %TRUE or %FALSE
785 soup_uri_uses_default_port (SoupURI *uri)
787 g_return_val_if_fail (uri != NULL, FALSE);
788 g_warn_if_fail (SOUP_URI_IS_VALID (uri));
790 return uri->port == soup_scheme_default_port (uri->scheme);
794 * SOUP_URI_SCHEME_HTTP:
796 * "http" as an interned string. This can be compared directly against
797 * the value of a #SoupURI's <structfield>scheme</structfield>
801 * SOUP_URI_SCHEME_HTTPS:
803 * "https" as an interned string. This can be compared directly
804 * against the value of a #SoupURI's <structfield>scheme</structfield>
808 * soup_uri_get_scheme:
811 * Gets @uri's scheme.
813 * Return value: @uri's scheme.
818 soup_uri_get_scheme (SoupURI *uri)
820 g_return_val_if_fail (uri != NULL, NULL);
826 * soup_uri_set_scheme:
828 * @scheme: the URI scheme
830 * Sets @uri's scheme to @scheme. This will also set @uri's port to
831 * the default port for @scheme, if known.
834 soup_uri_set_scheme (SoupURI *uri, const char *scheme)
836 g_return_if_fail (uri != NULL);
837 g_return_if_fail (scheme != NULL);
839 uri->scheme = soup_uri_parse_scheme (scheme, strlen (scheme));
840 uri->port = soup_scheme_default_port (uri->scheme);
849 * Return value: @uri's user.
854 soup_uri_get_user (SoupURI *uri)
856 g_return_val_if_fail (uri != NULL, NULL);
864 * @user: (allow-none): the username, or %NULL
866 * Sets @uri's user to @user.
869 soup_uri_set_user (SoupURI *uri, const char *user)
871 g_return_if_fail (uri != NULL);
874 uri->user = g_strdup (user);
878 * soup_uri_get_password:
881 * Gets @uri's password.
883 * Return value: @uri's password.
888 soup_uri_get_password (SoupURI *uri)
890 g_return_val_if_fail (uri != NULL, NULL);
892 return uri->password;
896 * soup_uri_set_password:
898 * @password: (allow-none): the password, or %NULL
900 * Sets @uri's password to @password.
903 soup_uri_set_password (SoupURI *uri, const char *password)
905 g_return_if_fail (uri != NULL);
907 g_free (uri->password);
908 uri->password = g_strdup (password);
917 * Return value: @uri's host.
922 soup_uri_get_host (SoupURI *uri)
924 g_return_val_if_fail (uri != NULL, NULL);
932 * @host: (allow-none): the hostname or IP address, or %NULL
934 * Sets @uri's host to @host.
936 * If @host is an IPv6 IP address, it should not include the brackets
937 * required by the URI syntax; they will be added automatically when
938 * converting @uri to a string.
940 * http and https URIs should not have a %NULL @host.
943 soup_uri_set_host (SoupURI *uri, const char *host)
945 g_return_if_fail (uri != NULL);
948 uri->host = g_strdup (host);
957 * Return value: @uri's port.
962 soup_uri_get_port (SoupURI *uri)
964 g_return_val_if_fail (uri != NULL, 0);
972 * @port: the port, or 0
974 * Sets @uri's port to @port. If @port is 0, @uri will not have an
975 * explicitly-specified port.
978 soup_uri_set_port (SoupURI *uri, guint port)
980 g_return_if_fail (uri != NULL);
991 * Return value: @uri's path.
996 soup_uri_get_path (SoupURI *uri)
998 g_return_val_if_fail (uri != NULL, NULL);
1004 * soup_uri_set_path:
1006 * @path: the non-%NULL path
1008 * Sets @uri's path to @path.
1011 soup_uri_set_path (SoupURI *uri, const char *path)
1013 g_return_if_fail (uri != NULL);
1015 /* We allow a NULL path for compatibility, but warn about it. */
1017 g_warn_if_fail (path != NULL);
1022 uri->path = g_strdup (path);
1026 * soup_uri_get_query:
1029 * Gets @uri's query.
1031 * Return value: @uri's query.
1036 soup_uri_get_query (SoupURI *uri)
1038 g_return_val_if_fail (uri != NULL, NULL);
1044 * soup_uri_set_query:
1046 * @query: (allow-none): the query
1048 * Sets @uri's query to @query.
1051 soup_uri_set_query (SoupURI *uri, const char *query)
1053 g_return_if_fail (uri != NULL);
1055 g_free (uri->query);
1056 uri->query = g_strdup (query);
1060 * soup_uri_set_query_from_form:
1062 * @form: (element-type utf8 utf8): a #GHashTable containing HTML form
1065 * Sets @uri's query to the result of encoding @form according to the
1066 * HTML form rules. See soup_form_encode_hash() for more information.
1069 soup_uri_set_query_from_form (SoupURI *uri, GHashTable *form)
1071 g_return_if_fail (uri != NULL);
1073 g_free (uri->query);
1074 uri->query = soup_form_encode_hash (form);
1078 * soup_uri_set_query_from_fields:
1080 * @first_field: name of the first form field to encode into query
1081 * @...: value of @first_field, followed by additional field names
1082 * and values, terminated by %NULL.
1084 * Sets @uri's query to the result of encoding the given form fields
1085 * and values according to the * HTML form rules. See
1086 * soup_form_encode() for more information.
1089 soup_uri_set_query_from_fields (SoupURI *uri,
1090 const char *first_field,
1095 g_return_if_fail (uri != NULL);
1097 g_free (uri->query);
1098 va_start (args, first_field);
1099 uri->query = soup_form_encode_valist (first_field, args);
1104 * soup_uri_get_fragment:
1107 * Gets @uri's fragment.
1109 * Return value: @uri's fragment.
1114 soup_uri_get_fragment (SoupURI *uri)
1116 g_return_val_if_fail (uri != NULL, NULL);
1118 return uri->fragment;
1122 * soup_uri_set_fragment:
1124 * @fragment: (allow-none): the fragment
1126 * Sets @uri's fragment to @fragment.
1129 soup_uri_set_fragment (SoupURI *uri, const char *fragment)
1131 g_return_if_fail (uri != NULL);
1133 g_free (uri->fragment);
1134 uri->fragment = g_strdup (fragment);
1138 * soup_uri_copy_host:
1141 * Makes a copy of @uri, considering only the protocol, host, and port
1143 * Return value: the new #SoupURI
1148 soup_uri_copy_host (SoupURI *uri)
1152 g_return_val_if_fail (uri != NULL, NULL);
1153 g_warn_if_fail (SOUP_URI_IS_VALID (uri));
1155 dup = soup_uri_new (NULL);
1156 dup->scheme = uri->scheme;
1157 dup->host = g_strdup (uri->host);
1158 dup->port = uri->port;
1159 dup->path = g_strdup ("");
1165 * soup_uri_host_hash:
1166 * @key: (type Soup.URI): a #SoupURI with a non-%NULL @host member
1168 * Hashes @key, considering only the scheme, host, and port.
1170 * Return value: a hash
1175 soup_uri_host_hash (gconstpointer key)
1177 const SoupURI *uri = key;
1179 g_return_val_if_fail (uri != NULL && uri->host != NULL, 0);
1180 g_warn_if_fail (SOUP_URI_IS_VALID (uri));
1182 return GPOINTER_TO_UINT (uri->scheme) + uri->port +
1183 soup_str_case_hash (uri->host);
1187 * soup_uri_host_equal:
1188 * @v1: (type Soup.URI): a #SoupURI with a non-%NULL @host member
1189 * @v2: (type Soup.URI): a #SoupURI with a non-%NULL @host member
1191 * Compares @v1 and @v2, considering only the scheme, host, and port.
1193 * Return value: whether or not the URIs are equal in scheme, host,
1199 soup_uri_host_equal (gconstpointer v1, gconstpointer v2)
1201 const SoupURI *one = v1;
1202 const SoupURI *two = v2;
1204 g_return_val_if_fail (one != NULL && two != NULL, one == two);
1205 g_return_val_if_fail (one->host != NULL && two->host != NULL, one->host == two->host);
1206 g_warn_if_fail (SOUP_URI_IS_VALID (one));
1207 g_warn_if_fail (SOUP_URI_IS_VALID (two));
1209 if (one->scheme != two->scheme)
1211 if (one->port != two->port)
1214 return g_ascii_strcasecmp (one->host, two->host) == 0;
1219 soup_uri_get_type (void)
1221 static volatile gsize type_volatile = 0;
1223 if (g_once_init_enter (&type_volatile)) {
1224 GType type = g_boxed_type_register_static (
1225 g_intern_static_string ("SoupURI"),
1226 (GBoxedCopyFunc) soup_uri_copy,
1227 (GBoxedFreeFunc) soup_uri_free);
1228 g_once_init_leave (&type_volatile, type);
1230 return type_volatile;