diff options
author | Patrick Griffis <pgriffis@igalia.com> | 2023-01-04 13:32:13 -0600 |
---|---|---|
committer | Patrick Griffis <pgriffis@igalia.com> | 2023-01-09 13:16:03 -0600 |
commit | 9a7cc5dd78f58f9587f315b90f5162361d722362 (patch) | |
tree | 5973cc5b845d384b3db8effa7ad721a4fa066948 | |
parent | a95474dddc02b36e39c9806556e7b4c0f5ff79d9 (diff) | |
download | libsoup-9a7cc5dd78f58f9587f315b90f5162361d722362.tar.gz |
soup_uri_copy: Don't retain default ports when copying
Fixes #322
-rw-r--r-- | libsoup/soup-uri-utils.c | 27 | ||||
-rw-r--r-- | tests/uri-parsing-test.c | 10 |
2 files changed, 36 insertions, 1 deletions
diff --git a/libsoup/soup-uri-utils.c b/libsoup/soup-uri-utils.c index d317bea6..be2b79b0 100644 --- a/libsoup/soup-uri-utils.c +++ b/libsoup/soup-uri-utils.c @@ -363,6 +363,26 @@ soup_uri_decode_data_uri (const char *uri, * the URI that should be updated with the given values. */ +static int +get_maybe_default_port (GUri *uri) +{ + const char *scheme = g_uri_get_scheme (uri); + int port = g_uri_get_port (uri); + + switch (port) { + case 80: + if (!strcmp (scheme, "http") || !strcmp (scheme, "ws")) + return -1; + break; + case 443: + if (!strcmp (scheme, "https") || !strcmp (scheme, "wss")) + return -1; + break; + } + + return port; +} + /** * soup_uri_copy: (skip) * @uri: the #GUri to copy @@ -370,6 +390,11 @@ soup_uri_decode_data_uri (const char *uri, * @...: value of @first_component followed by additional * components and values, terminated by %SOUP_URI_NONE * + * As of 3.4.0 this will detect the default ports of HTTP(s) and WS(S) + * URIs when copying and set it to the default port of the new scheme. + * So for example copying `http://localhost:80` while changing the scheme to https + * will result in `https://localhost:443`. + * * Return a copy of @uri with the given components updated. * * Returns: (transfer full): a new #GUri @@ -417,7 +442,7 @@ soup_uri_copy (GUri *uri, values_to_set[SOUP_URI_PASSWORD] ? values[SOUP_URI_PASSWORD] : g_uri_get_password (uri), values_to_set[SOUP_URI_AUTH_PARAMS] ? values[SOUP_URI_AUTH_PARAMS] : g_uri_get_auth_params (uri), values_to_set[SOUP_URI_HOST] ? values[SOUP_URI_HOST] : g_uri_get_host (uri), - values_to_set[SOUP_URI_PORT] ? GPOINTER_TO_INT (values[SOUP_URI_PORT]) : g_uri_get_port (uri), + values_to_set[SOUP_URI_PORT] ? GPOINTER_TO_INT (values[SOUP_URI_PORT]) : get_maybe_default_port (uri), values_to_set[SOUP_URI_PATH] ? values[SOUP_URI_PATH] : g_uri_get_path (uri), values_to_set[SOUP_URI_QUERY] ? values[SOUP_URI_QUERY] : g_uri_get_query (uri), values_to_set[SOUP_URI_FRAGMENT] ? values[SOUP_URI_FRAGMENT] : g_uri_get_fragment (uri) diff --git a/tests/uri-parsing-test.c b/tests/uri-parsing-test.c index 4c16d7e2..1f16273d 100644 --- a/tests/uri-parsing-test.c +++ b/tests/uri-parsing-test.c @@ -51,10 +51,12 @@ static void do_copy_tests (void) { GUri *uri; + GUri *uri2; GUri *copy; char *str; uri = g_uri_parse ("http://127.0.0.1:1234/foo#bar", SOUP_HTTP_URI_FLAGS, NULL); + uri2 = g_uri_parse ("http://127.0.0.1", SOUP_HTTP_URI_FLAGS, NULL); /* Exact copy */ copy = soup_uri_copy (uri, SOUP_URI_NONE); @@ -91,6 +93,13 @@ do_copy_tests (void) g_free (str); g_uri_unref (copy); + /* Switch protocols without explicit port */ + copy = soup_uri_copy (uri2, SOUP_URI_SCHEME, "https", SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "https://127.0.0.1/"); + g_free (str); + g_uri_unref (copy); + /* Update everything */ copy = soup_uri_copy (uri, SOUP_URI_SCHEME, "https", @@ -114,6 +123,7 @@ do_copy_tests (void) g_uri_unref (copy); g_uri_unref (uri); + g_uri_unref (uri2); } #define CONTENT_TYPE_DEFAULT "text/plain;charset=US-ASCII" |