summaryrefslogtreecommitdiff
path: root/glib/guri.c
diff options
context:
space:
mode:
Diffstat (limited to 'glib/guri.c')
-rw-r--r--glib/guri.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/glib/guri.c b/glib/guri.c
index 23b8e6dd8..19fe4ac71 100644
--- a/glib/guri.c
+++ b/glib/guri.c
@@ -794,6 +794,21 @@ normalize_port (const char *scheme,
return port;
}
+static int
+default_scheme_port (const char *scheme)
+{
+ if (strcmp (scheme, "http") == 0 || strcmp (scheme, "ws") == 0)
+ return 80;
+
+ if (strcmp (scheme, "https") == 0 || strcmp (scheme, "wss") == 0)
+ return 443;
+
+ if (strcmp (scheme, "ftp") == 0)
+ return 21;
+
+ return -1;
+}
+
static gboolean
g_uri_split_internal (const gchar *uri_string,
GUriFlags flags,
@@ -989,8 +1004,8 @@ g_uri_split_internal (const gchar *uri_string,
*path = g_strdup ("/");
}
- if (port && *port != -1)
- *port = normalize_port (scheme_str, *port);
+ if (port && *port == -1)
+ *port = default_scheme_port (scheme_str);
}
g_free (normalized_scheme);
@@ -1568,6 +1583,7 @@ g_uri_join_internal (GUriFlags flags,
{
gboolean encoded = (flags & G_URI_FLAGS_ENCODED);
GString *str;
+ char *normalized_scheme = NULL;
/* Restrictions on path prefixes. See:
* https://tools.ietf.org/html/rfc3986#section-3
@@ -1580,6 +1596,9 @@ g_uri_join_internal (GUriFlags flags,
if (scheme)
g_string_append_c (str, ':');
+ if (flags & G_URI_FLAGS_SCHEME_NORMALIZE && scheme && ((host && port != -1) || path[0] == '\0'))
+ normalized_scheme = g_ascii_strdown (scheme, -1);
+
if (host)
{
g_string_append (str, "//");
@@ -1640,15 +1659,19 @@ g_uri_join_internal (GUriFlags flags,
g_string_append_uri_escaped (str, host, HOST_ALLOWED_CHARS, TRUE);
}
- if (port != -1)
+ if (port != -1 && (!normalized_scheme || normalize_port (normalized_scheme, port) != -1))
g_string_append_printf (str, ":%d", port);
}
- if (encoded || flags & G_URI_FLAGS_ENCODED_PATH)
+ if (path[0] == '\0' && normalized_scheme && should_normalize_empty_path (normalized_scheme))
+ g_string_append (str, "/");
+ else if (encoded || flags & G_URI_FLAGS_ENCODED_PATH)
g_string_append (str, path);
else
g_string_append_uri_escaped (str, path, PATH_ALLOWED_CHARS, TRUE);
+ g_free (normalized_scheme);
+
if (query)
{
g_string_append_c (str, '?');
@@ -2432,6 +2455,9 @@ g_uri_get_port (GUri *uri)
{
g_return_val_if_fail (uri != NULL, -1);
+ if (uri->port == -1 && uri->flags & G_URI_FLAGS_SCHEME_NORMALIZE)
+ return default_scheme_port (uri->scheme);
+
return uri->port;
}