diff options
author | Carlos Garcia Campos <cgarcia@igalia.com> | 2020-11-16 14:21:13 +0100 |
---|---|---|
committer | Carlos Garcia Campos <cgarcia@igalia.com> | 2020-11-20 11:09:23 +0100 |
commit | 3fb38248dd90de2eac95e6f24416466105ddd682 (patch) | |
tree | 9a42878f6e5aa192d714c9e9298cbb79d679e1de | |
parent | 9f262586f4425721721cbbc7d2feae27642b2213 (diff) | |
download | libsoup-3fb38248dd90de2eac95e6f24416466105ddd682.tar.gz |
uri-utils: add soup_uri_copy()
It's a generic API to copy a GUri, but updating the given components.
-rw-r--r-- | docs/reference/libsoup-3.0-sections.txt | 4 | ||||
-rw-r--r-- | libsoup/auth/soup-auth-manager.c | 2 | ||||
-rw-r--r-- | libsoup/hsts/soup-hsts-enforcer.c | 19 | ||||
-rw-r--r-- | libsoup/server/soup-server.c | 23 | ||||
-rw-r--r-- | libsoup/soup-message.c | 21 | ||||
-rw-r--r-- | libsoup/soup-session.c | 30 | ||||
-rw-r--r-- | libsoup/soup-uri-utils-private.h | 4 | ||||
-rw-r--r-- | libsoup/soup-uri-utils.c | 163 | ||||
-rw-r--r-- | libsoup/soup-uri-utils.h | 25 | ||||
-rw-r--r-- | tests/auth-test.c | 4 | ||||
-rw-r--r-- | tests/continue-test.c | 2 | ||||
-rw-r--r-- | tests/forms-test.c | 14 | ||||
-rw-r--r-- | tests/hsts-db-test.c | 4 | ||||
-rw-r--r-- | tests/hsts-test.c | 4 | ||||
-rw-r--r-- | tests/no-ssl-test.c | 27 | ||||
-rw-r--r-- | tests/sniffing-test.c | 38 | ||||
-rw-r--r-- | tests/test-utils.c | 18 | ||||
-rw-r--r-- | tests/test-utils.h | 2 | ||||
-rw-r--r-- | tests/uri-parsing-test.c | 70 |
19 files changed, 218 insertions, 256 deletions
diff --git a/docs/reference/libsoup-3.0-sections.txt b/docs/reference/libsoup-3.0-sections.txt index abf9008e..e11f838c 100644 --- a/docs/reference/libsoup-3.0-sections.txt +++ b/docs/reference/libsoup-3.0-sections.txt @@ -521,8 +521,8 @@ soup_uri_decode_data_uri <SUBSECTION> soup_uri_get_port_with_default <SUBSECTION> -soup_uri_copy_with_query_from_form -soup_uri_copy_with_query_from_fields +SoupURIComponent +soup_uri_copy </SECTION> <SECTION> diff --git a/libsoup/auth/soup-auth-manager.c b/libsoup/auth/soup-auth-manager.c index 3a52aa7c..26be2ead 100644 --- a/libsoup/auth/soup-auth-manager.c +++ b/libsoup/auth/soup-auth-manager.c @@ -554,7 +554,7 @@ authenticate_auth (SoupAuthManager *manager, SoupAuth *auth, */ if (g_uri_get_password (uri) && g_uri_get_user (uri)) { soup_auth_authenticate (auth, g_uri_get_user (uri), g_uri_get_password (uri)); - GUri *new_uri = soup_uri_copy_with_credentials (uri, NULL, NULL); + GUri *new_uri = soup_uri_copy (uri, SOUP_URI_USER, NULL, SOUP_URI_PASSWORD, NULL, SOUP_URI_NONE); soup_message_set_uri (msg, new_uri); // QUESTION: This didn't emit a signal previously g_uri_unref (new_uri); } else if (!soup_auth_is_authenticated (auth) && can_interact) { diff --git a/libsoup/hsts/soup-hsts-enforcer.c b/libsoup/hsts/soup-hsts-enforcer.c index 05155e0d..5a297bca 100644 --- a/libsoup/hsts/soup-hsts-enforcer.c +++ b/libsoup/hsts/soup-hsts-enforcer.c @@ -488,23 +488,6 @@ got_sts_header_cb (SoupMessage *msg, gpointer user_data) soup_hsts_enforcer_process_sts_header (hsts_enforcer, msg); } -static GUri * -copy_uri_with_new_scheme (GUri *uri, const char *scheme, int port) -{ - return g_uri_build_with_user ( - g_uri_get_flags (uri), - scheme, - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - port, - g_uri_get_path (uri), - g_uri_get_query (uri), - g_uri_get_fragment (uri) - ); -} - static void rewrite_message_uri_to_https (SoupMessage *msg) { @@ -518,7 +501,7 @@ rewrite_message_uri_to_https (SoupMessage *msg) if (port == 80) port = 443; - new_uri = copy_uri_with_new_scheme (uri, "https", port); + new_uri = soup_uri_copy (uri, SOUP_URI_SCHEME, "https", SOUP_URI_PORT, port, SOUP_URI_NONE); soup_message_set_uri (msg, new_uri); g_uri_unref (new_uri); } diff --git a/libsoup/server/soup-server.c b/libsoup/server/soup-server.c index 58bbd9c9..0fe41740 100644 --- a/libsoup/server/soup-server.c +++ b/libsoup/server/soup-server.c @@ -777,23 +777,6 @@ call_handler (SoupServer *server, g_hash_table_unref (form_data_set); } -static GUri * -uri_set_path (GUri *uri, const char *path) -{ - return g_uri_build_with_user ( - g_uri_get_flags (uri) ^ G_URI_FLAGS_ENCODED_PATH, - g_uri_get_scheme (uri), - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - path, - g_uri_get_query (uri), - g_uri_get_fragment (uri) - ); -} - static void got_headers (SoupServer *server, SoupServerMessage *msg) @@ -832,6 +815,7 @@ got_headers (SoupServer *server, if (!priv->raw_paths && g_uri_get_flags (uri) & G_URI_FLAGS_ENCODED_PATH) { char *decoded_path; + GUri *copy; decoded_path = g_uri_unescape_string (g_uri_get_path (uri), NULL); @@ -851,9 +835,10 @@ got_headers (SoupServer *server, return; } - uri = uri_set_path (uri, decoded_path); - soup_server_message_set_uri (msg, uri); + copy = soup_uri_copy (uri, SOUP_URI_PATH, decoded_path, SOUP_URI_NONE); + soup_server_message_set_uri (msg, copy); g_free (decoded_path); + g_uri_unref (copy); } /* Now handle authentication. (We do this here so that if diff --git a/libsoup/soup-message.c b/libsoup/soup-message.c index ee1e2ea7..a302f236 100644 --- a/libsoup/soup-message.c +++ b/libsoup/soup-message.c @@ -812,23 +812,6 @@ soup_message_new_from_uri (const char *method, GUri *uri) NULL); } -static GUri * -copy_uri_with_new_query (GUri *uri, const char *query) -{ - return g_uri_build_with_user ( - g_uri_get_flags (uri), - g_uri_get_scheme (uri), - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - query, - g_uri_get_fragment (uri) - ); -} - /** * soup_message_new_from_encoded_form: * @method: the HTTP method for the created request (GET, POST or PUT) @@ -866,7 +849,7 @@ soup_message_new_from_encoded_form (const char *method, } if (strcmp (method, "GET") == 0) { - GUri *new_uri = copy_uri_with_new_query (uri, encoded_form); + GUri *new_uri = soup_uri_copy (uri, SOUP_URI_QUERY, encoded_form, SOUP_URI_NONE); msg = soup_message_new_from_uri (method, new_uri); g_uri_unref (new_uri); } else if (strcmp (method, "POST") == 0 || strcmp (method, "PUT") == 0) { @@ -2195,4 +2178,4 @@ soup_message_is_options_ping (SoupMessage *msg) SoupMessagePrivate *priv = soup_message_get_instance_private (msg); return priv->options_ping; -}
\ No newline at end of file +} diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c index 4ba0b7b3..608b7411 100644 --- a/libsoup/soup-session.c +++ b/libsoup/soup-session.c @@ -670,24 +670,6 @@ soup_host_uri_equal (gconstpointer v1, gconstpointer v2) return g_ascii_strcasecmp (one_host, two_host) == 0; } -static GUri * -copy_uri_with_new_scheme (GUri *uri, const char *scheme) -{ - return g_uri_build_with_user ( - g_uri_get_flags (uri), - scheme, - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - g_uri_get_query (uri), - g_uri_get_fragment (uri) - ); -} - - static SoupSessionHost * soup_session_host_new (SoupSession *session, GUri *uri) { @@ -699,10 +681,10 @@ soup_session_host_new (SoupSession *session, GUri *uri) g_strcmp0 (scheme, "https")) { SoupSessionPrivate *priv = soup_session_get_instance_private (session); - if (soup_uri_is_https (uri, priv->https_aliases)) - host->uri = copy_uri_with_new_scheme (uri, "https"); - else - host->uri = copy_uri_with_new_scheme (uri, "http"); + host->uri = soup_uri_copy (uri, + SOUP_URI_SCHEME, soup_uri_is_https (uri, priv->https_aliases) ? + "https" : "http", + SOUP_URI_NONE); } else host->uri = g_uri_ref (uri); @@ -735,7 +717,9 @@ get_host_for_uri (SoupSession *session, GUri *uri) return host; if (!soup_uri_is_http (uri, NULL) && !soup_uri_is_https (uri, NULL)) { - uri = uri_tmp = copy_uri_with_new_scheme (uri, https ? "https" : "http"); + uri = uri_tmp = soup_uri_copy (uri, + SOUP_URI_SCHEME, https ? "https" : "http", + SOUP_URI_NONE); } host = soup_session_host_new (session, uri); if (uri_tmp) diff --git a/libsoup/soup-uri-utils-private.h b/libsoup/soup-uri-utils-private.h index ed137f90..b741a6d7 100644 --- a/libsoup/soup-uri-utils-private.h +++ b/libsoup/soup-uri-utils-private.h @@ -18,10 +18,6 @@ gboolean soup_uri_is_https (GUri *uri, gboolean soup_uri_uses_default_port (GUri *uri); -GUri *soup_uri_copy_with_credentials (GUri *uri, - const char *username, - const char *password); - char *soup_uri_get_path_and_query (GUri *uri); GUri *soup_uri_copy_host (GUri *uri); diff --git a/libsoup/soup-uri-utils.c b/libsoup/soup-uri-utils.c index de843ae1..f2e4e76d 100644 --- a/libsoup/soup-uri-utils.c +++ b/libsoup/soup-uri-utils.c @@ -179,86 +179,19 @@ soup_uri_uses_default_port (GUri *uri) return FALSE; } -static GUri * -soup_uri_copy_with_query (GUri *uri, const char *query) -{ - return g_uri_build_with_user ( - g_uri_get_flags (uri) | G_URI_FLAGS_ENCODED_QUERY, - g_uri_get_scheme (uri), - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - query, - g_uri_get_fragment (uri) - ); -} - -/** - * soup_uri_copy_with_query_from_form: - * @uri: a #GUri - * @form: (element-type utf8 utf8): a #GHashTable containing HTML form - * information - * - * Sets @uri's query to the result of encoding @form according to the - * HTML form rules. See soup_form_encode_hash() for more information. - * - * Returns: (transfer full): A new #GUri - **/ -GUri * -soup_uri_copy_with_query_from_form (GUri *uri, GHashTable *form) -{ - g_return_val_if_fail (uri != NULL, NULL); - - char *query = soup_form_encode_hash (form); - GUri *new_uri = soup_uri_copy_with_query (uri, query); - g_free (query); - return new_uri; -} - -/** - * soup_uri_copy_with_query_from_fields: - * @uri: a #GUri - * @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. - * - * Returns: (transfer full): A new #GUri - **/ -GUri * -soup_uri_copy_with_query_from_fields (GUri *uri, - const char *first_field, - ...) -{ - va_list args; - - g_return_val_if_fail (uri != NULL, NULL); - - va_start (args, first_field); - char *query = soup_form_encode_valist (first_field, args); - va_end (args); - - GUri *new_uri = soup_uri_copy_with_query (uri, query); - g_free (query); - return new_uri; -} - GUri * soup_uri_copy_host (GUri *uri) { g_return_val_if_fail (uri != NULL, NULL); - return g_uri_build (g_uri_get_flags (uri), - g_uri_get_scheme (uri), NULL, - g_uri_get_host (uri), - g_uri_get_port (uri), - "/", NULL, NULL); + return soup_uri_copy (uri, + SOUP_URI_USER, NULL, + SOUP_URI_PASSWORD, NULL, + SOUP_URI_AUTH_PARAMS, NULL, + SOUP_URI_PATH, "/", + SOUP_URI_QUERY, NULL, + SOUP_URI_FRAGMENT, NULL, + SOUP_URI_NONE); } /** @@ -453,21 +386,81 @@ soup_uri_decode_data_uri (const char *uri, return bytes; } +/** + * SoupURIComponent: + * @SOUP_URI_NONE: no component + * @SOUP_URI_SCHEME: the URI scheme component + * @SOUP_URI_USER: the URI user component + * @SOUP_URI_PASSWORD: the URI password component + * @SOUP_URI_AUTH_PARAMS: the URI authentication parameters component + * @SOUP_URI_HOST: the URI host component + * @SOUP_URI_PORT: the URI port component + * @SOUP_URI_PATH: the URI path component + * @SOUP_URI_QUERY: the URI query component + * @SOUP_URI_FRAGMENT: the URI fragment component + * + * Enum values passed to soup_uri_copy() to indicate the components of + * the URI that should be updated with the given values. + */ + +/** + * soup_uri_copy: (skip) + * @uri: the #GUri to copy + * @first_component: first #SoupURIComponent to update + * @...: value of @first_component followed by additional + * components and values, terminated by %SOUP_URI_NONE + * + * Return a copy of @uri with the given components updated + * + * Returns: (transfer full): a new #GUri + */ GUri * -soup_uri_copy_with_credentials (GUri *uri, const char *username, const char *password) +soup_uri_copy (GUri *uri, + SoupURIComponent first_component, + ...) { + va_list args; + SoupURIComponent component = first_component; + gpointer values[SOUP_URI_FRAGMENT + 1]; + gboolean values_to_set[SOUP_URI_FRAGMENT + 1]; + GUriFlags flags = g_uri_get_flags (uri); + g_return_val_if_fail (uri != NULL, NULL); + memset (&values_to_set, 0, sizeof (values_to_set)); + + va_start (args, first_component); + while (component != SOUP_URI_NONE) { + if (component == SOUP_URI_PORT) + values[component] = GINT_TO_POINTER (va_arg (args, glong)); + else + values[component] = va_arg (args, gpointer); + values_to_set[component] = TRUE; + component = va_arg (args, SoupURIComponent); + } + va_end (args); + + if (values_to_set[SOUP_URI_PASSWORD]) + flags |= G_URI_FLAGS_HAS_PASSWORD; + if (values_to_set[SOUP_URI_AUTH_PARAMS]) + flags |= G_URI_FLAGS_HAS_AUTH_PARAMS; + if (values_to_set[SOUP_URI_PATH]) + flags |= G_URI_FLAGS_ENCODED_PATH; + if (values_to_set[SOUP_URI_QUERY]) + flags |= G_URI_FLAGS_ENCODED_QUERY; + if (values_to_set[SOUP_URI_FRAGMENT]) + flags |= G_URI_FLAGS_ENCODED_FRAGMENT; return g_uri_build_with_user ( - g_uri_get_flags (uri) | G_URI_FLAGS_HAS_PASSWORD, - g_uri_get_scheme (uri), - username, password, - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - g_uri_get_query (uri), - g_uri_get_fragment (uri) + flags, + values_to_set[SOUP_URI_SCHEME] ? values[SOUP_URI_SCHEME] : g_uri_get_scheme (uri), + values_to_set[SOUP_URI_USER] ? values[SOUP_URI_USER] : g_uri_get_user (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_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/libsoup/soup-uri-utils.h b/libsoup/soup-uri-utils.h index 8b436180..e4072b4c 100644 --- a/libsoup/soup-uri-utils.h +++ b/libsoup/soup-uri-utils.h @@ -11,6 +11,19 @@ G_BEGIN_DECLS +typedef enum { + SOUP_URI_NONE, + SOUP_URI_SCHEME, + SOUP_URI_USER, + SOUP_URI_PASSWORD, + SOUP_URI_AUTH_PARAMS, + SOUP_URI_HOST, + SOUP_URI_PORT, + SOUP_URI_PATH, + SOUP_URI_QUERY, + SOUP_URI_FRAGMENT +} SoupURIComponent; + SOUP_AVAILABLE_IN_ALL GBytes *soup_uri_decode_data_uri (const char *uri, char **content_type); @@ -19,17 +32,13 @@ SOUP_AVAILABLE_IN_ALL gboolean soup_uri_equal (GUri *uri1, GUri *uri2); SOUP_AVAILABLE_IN_ALL -GUri *soup_uri_copy_with_query_from_form (GUri *uri, - GHashTable *form); +int soup_uri_get_port_with_default (GUri *uri); -SOUP_AVAILABLE_IN_ALL -GUri *soup_uri_copy_with_query_from_fields (GUri *uri, - const char *first_field, - ...) G_GNUC_NULL_TERMINATED; SOUP_AVAILABLE_IN_ALL -int soup_uri_get_port_with_default (GUri *uri); - +GUri *soup_uri_copy (GUri *uri, + SoupURIComponent first_component, + ...); #define SOUP_HTTP_URI_FLAGS (G_URI_FLAGS_HAS_PASSWORD | G_URI_FLAGS_ENCODED_PATH | G_URI_FLAGS_ENCODED_QUERY | G_URI_FLAGS_ENCODED_FRAGMENT | G_URI_FLAGS_SCHEME_NORMALIZE) G_END_DECLS diff --git a/tests/auth-test.c b/tests/auth-test.c index 48357162..0d0e80cc 100644 --- a/tests/auth-test.c +++ b/tests/auth-test.c @@ -1317,7 +1317,7 @@ do_batch_tests (gconstpointer data) if (current_tests[i].url_auth) { gchar *username = g_strdup_printf ("user%c", current_tests[i].provided[0]); gchar *password = g_strdup_printf ("realm%c", current_tests[i].provided[0]); - GUri *tmp = soup_uri_copy_with_credentials (soup_uri, username, password); + GUri *tmp = soup_uri_copy (soup_uri, SOUP_URI_USER, username, SOUP_URI_PASSWORD, password, SOUP_URI_NONE); g_uri_unref (soup_uri); soup_uri = tmp; g_free (username); @@ -1410,7 +1410,7 @@ do_message_do_not_use_auth_cache_test (void) * no matter whether the cache is used or not */ soup_uri = g_uri_parse (uri, SOUP_HTTP_URI_FLAGS, NULL); - auth_uri = soup_uri_copy_with_credentials (soup_uri, "user1", "realm1"); + auth_uri = soup_uri_copy (soup_uri, SOUP_URI_USER, "user1", SOUP_URI_PASSWORD, "realm1", SOUP_URI_NONE); msg = soup_message_new_from_uri (SOUP_METHOD_GET, auth_uri); soup_message_add_flags (msg, SOUP_MESSAGE_DO_NOT_USE_AUTH_CACHE); diff --git a/tests/continue-test.c b/tests/continue-test.c index cc1da185..ea91f84c 100644 --- a/tests/continue-test.c +++ b/tests/continue-test.c @@ -98,7 +98,7 @@ do_message (const char *path, gboolean long_body, GBytes *response_body; if (auth) - uri = soup_uri_copy_with_credentials (base_uri, "user", "pass"); + uri = soup_uri_copy (base_uri, SOUP_URI_USER, "user", SOUP_URI_PASSWORD, "pass", SOUP_URI_NONE); else uri = g_uri_ref (base_uri); diff --git a/tests/forms-test.c b/tests/forms-test.c index 803181fa..8bc6a067 100644 --- a/tests/forms-test.c +++ b/tests/forms-test.c @@ -414,6 +414,7 @@ md5_post_callback (SoupServer *server, char *filename, *md5sum, *redirect_uri; GBytes *file; GUri *uri; + char *encoded_form; SoupMultipart *multipart; GBytes *body; SoupMessageHeaders *request_headers; @@ -438,11 +439,14 @@ md5_post_callback (SoupServer *server, md5sum = g_compute_checksum_for_bytes (G_CHECKSUM_MD5, file); g_bytes_unref (file); - uri = soup_uri_copy_with_query_from_fields (soup_server_message_get_uri (msg), - "file", filename ? filename : "", - "md5sum", md5sum, - "fmt", fmt ? fmt : "html", - NULL); + encoded_form = soup_form_encode ("file", filename ? filename : "", + "md5sum", md5sum, + "fmt", fmt ? fmt : "html", + NULL); + uri = soup_uri_copy (soup_server_message_get_uri (msg), + SOUP_URI_QUERY, encoded_form, + SOUP_URI_NONE); + g_free (encoded_form); redirect_uri = g_uri_to_string (uri); soup_server_message_set_redirect (msg, SOUP_STATUS_SEE_OTHER, redirect_uri); diff --git a/tests/hsts-db-test.c b/tests/hsts-db-test.c index e98914e2..655c6d73 100644 --- a/tests/hsts-db-test.c +++ b/tests/hsts-db-test.c @@ -88,9 +88,9 @@ rewrite_message_uri (SoupMessage *msg) { GUri *new_uri; if (soup_uri_is_http (soup_message_get_uri (msg), NULL)) - new_uri = soup_test_uri_set_port (soup_message_get_uri (msg), g_uri_get_port (http_uri)); + new_uri = soup_uri_copy (soup_message_get_uri (msg), SOUP_URI_PORT, g_uri_get_port (http_uri), SOUP_URI_NONE); else if (soup_uri_is_https (soup_message_get_uri (msg), NULL)) - new_uri = soup_test_uri_set_port (soup_message_get_uri (msg), g_uri_get_port (https_uri)); + new_uri = soup_uri_copy (soup_message_get_uri (msg), SOUP_URI_PORT, g_uri_get_port (https_uri), SOUP_URI_NONE); else g_assert_not_reached(); soup_message_set_uri (msg, new_uri); diff --git a/tests/hsts-test.c b/tests/hsts-test.c index 2d75d1bb..b8d7e9e2 100644 --- a/tests/hsts-test.c +++ b/tests/hsts-test.c @@ -134,9 +134,9 @@ rewrite_message_uri (SoupMessage *msg) { GUri *new_uri; if (soup_uri_is_http (soup_message_get_uri (msg), NULL)) - new_uri = soup_test_uri_set_port (soup_message_get_uri (msg), g_uri_get_port (http_uri)); + new_uri = soup_uri_copy (soup_message_get_uri (msg), SOUP_URI_PORT, g_uri_get_port (http_uri), SOUP_URI_NONE); else if (soup_uri_is_https (soup_message_get_uri (msg), NULL)) - new_uri = soup_test_uri_set_port (soup_message_get_uri (msg), g_uri_get_port (https_uri)); + new_uri = soup_uri_copy (soup_message_get_uri (msg), SOUP_URI_PORT, g_uri_get_port (https_uri), SOUP_URI_NONE); else g_assert_not_reached(); soup_message_set_uri (msg, new_uri); diff --git a/tests/no-ssl-test.c b/tests/no-ssl-test.c index 009a254c..cd3cc995 100644 --- a/tests/no-ssl-test.c +++ b/tests/no-ssl-test.c @@ -45,30 +45,12 @@ server_handler (SoupServer *server, "ok\r\n", 4); } -static GUri * -uri_set_scheme (GUri *uri, const char *scheme) -{ - GUri *new_uri = g_uri_build_with_user ( - g_uri_get_flags (uri), - scheme, - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - g_uri_get_query (uri), - g_uri_get_fragment (uri) - ); - g_uri_unref (uri); - return new_uri; -} - int main (int argc, char **argv) { SoupServer *server; GUri *uri; + GUri *ssl_uri; int ret; /* Force this test to use the dummy TLS backend */ @@ -83,13 +65,14 @@ main (int argc, char **argv) server = soup_test_server_new (TRUE); soup_server_add_handler (server, NULL, server_handler, NULL, NULL); uri = soup_test_server_get_uri (server, "http", NULL); - uri = uri_set_scheme (uri, "https"); + ssl_uri = soup_uri_copy (uri, SOUP_URI_SCHEME, "https", SOUP_URI_NONE); + g_uri_unref (uri); - g_test_add_data_func ("/no-ssl/request-error", uri, do_ssl_tests); + g_test_add_data_func ("/no-ssl/request-error", ssl_uri, do_ssl_tests); ret = g_test_run (); - g_uri_unref (uri); + g_uri_unref (ssl_uri); soup_test_server_quit_unref (server); test_cleanup (); diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c index 45338459..1f2bb911 100644 --- a/tests/sniffing-test.c +++ b/tests/sniffing-test.c @@ -159,23 +159,6 @@ got_headers (SoupMessage *msg) g_object_set_data (G_OBJECT (msg), "got-headers", GINT_TO_POINTER (TRUE)); } -static GUri * -uri_set_query (GUri *uri, const char *query) -{ - GUri *new_uri = g_uri_build ( - g_uri_get_flags (uri), - g_uri_get_scheme (uri), - NULL, - g_uri_get_host (uri), - g_uri_get_port (uri), - g_uri_get_path (uri), - query, - g_uri_get_fragment (uri) - ); - g_uri_unref (uri); - return new_uri; -} - static void do_signals_test (gboolean should_content_sniff, gboolean chunked_encoding, @@ -192,16 +175,25 @@ do_signals_test (gboolean should_content_sniff, chunked_encoding ? "" : "!", empty_response ? "" : "!"); - if (chunked_encoding) - uri = uri_set_query (uri, "chunked=yes"); + if (chunked_encoding) { + GUri *copy = soup_uri_copy (uri, SOUP_URI_QUERY, "chunked=yes", SOUP_URI_NONE); + g_uri_unref (uri); + uri = copy; + + } if (empty_response) { if (g_uri_get_query (uri)) { char *new_query = g_strdup_printf ("%s&empty_response=yes", g_uri_get_query (uri)); - uri = uri_set_query (uri, new_query); - g_free (new_query); - } else - uri = uri_set_query (uri, "empty_response=yes"); + GUri *copy = soup_uri_copy (uri, SOUP_URI_QUERY, new_query, SOUP_URI_NONE); + g_free (new_query); + g_uri_unref (uri); + uri = copy; + } else { + GUri *copy = soup_uri_copy (uri, SOUP_URI_QUERY, "empty_response=yes", SOUP_URI_NONE); + g_uri_unref (uri); + uri = copy; + } } soup_message_set_uri (msg, uri); diff --git a/tests/test-utils.c b/tests/test-utils.c index e3de265c..8583f04c 100644 --- a/tests/test-utils.c +++ b/tests/test-utils.c @@ -867,21 +867,3 @@ soup_test_assert (gboolean expr, const char *fmt, ...) } } #endif - -GUri * -soup_test_uri_set_port (GUri *uri, int port) -{ - GUri *new_uri = g_uri_build_with_user ( - g_uri_get_flags (uri), - g_uri_get_scheme (uri), - g_uri_get_user (uri), - g_uri_get_password (uri), - g_uri_get_auth_params (uri), - g_uri_get_host (uri), - port, - g_uri_get_path (uri), - g_uri_get_query (uri), - g_uri_get_fragment (uri) - ); - return new_uri; -} diff --git a/tests/test-utils.h b/tests/test-utils.h index d83e2639..38241cb8 100644 --- a/tests/test-utils.h +++ b/tests/test-utils.h @@ -92,8 +92,6 @@ GBytes *soup_test_load_resource (const char *name, GBytes *soup_test_get_index (void); -GUri *soup_test_uri_set_port (GUri *uri, - int port); #ifdef G_HAVE_ISO_VARARGS #define soup_test_assert(expr, ...) \ diff --git a/tests/uri-parsing-test.c b/tests/uri-parsing-test.c index 8d5c0109..a8dd91c5 100644 --- a/tests/uri-parsing-test.c +++ b/tests/uri-parsing-test.c @@ -46,6 +46,75 @@ do_equality_tests (void) } } +static void +do_copy_tests (void) +{ + GUri *uri; + GUri *copy; + char *str; + + uri = g_uri_parse ("http://127.0.0.1:1234/foo#bar", SOUP_HTTP_URI_FLAGS, NULL); + + /* Exact copy */ + copy = soup_uri_copy (uri, SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/foo#bar"); + g_free (str); + g_uri_unref (copy); + + /* Update the path */ + copy = soup_uri_copy (uri, SOUP_URI_PATH, "/baz", SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/baz#bar"); + g_free (str); + g_uri_unref (copy); + + /* Add credentials */ + copy = soup_uri_copy (uri, SOUP_URI_USER, "user", SOUP_URI_PASSWORD, "password", SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "http://user:password@127.0.0.1:1234/foo#bar"); + g_free (str); + g_uri_unref (copy); + + /* Remove the fragment and add a query */ + copy = soup_uri_copy (uri, SOUP_URI_FRAGMENT, NULL, SOUP_URI_QUERY, "baz=1", SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/foo?baz=1"); + g_free (str); + g_uri_unref (copy); + + /* Update host and port */ + copy = soup_uri_copy (uri, SOUP_URI_HOST, "localhost", SOUP_URI_PORT, -1, SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "http://localhost/foo#bar"); + g_free (str); + g_uri_unref (copy); + + /* Update everything */ + copy = soup_uri_copy (uri, + SOUP_URI_SCHEME, "https", + SOUP_URI_USER, "user", + SOUP_URI_PASSWORD, "password", + SOUP_URI_HOST, "localhost", + SOUP_URI_PORT, 4321, + SOUP_URI_PATH, "/baz", + SOUP_URI_FRAGMENT, "foo", + SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "https://user:password@localhost:4321/baz#foo"); + g_free (str); + g_uri_unref (copy); + + /* Convert to file */ + copy = soup_uri_copy (uri, SOUP_URI_SCHEME, "file", SOUP_URI_HOST, "", SOUP_URI_PORT, -1, SOUP_URI_FRAGMENT, NULL, SOUP_URI_NONE); + str = g_uri_to_string (copy); + g_assert_cmpstr (str, ==, "file:///foo"); + g_free (str); + g_uri_unref (copy); + + g_uri_unref (uri); +} + int main (int argc, char **argv) { @@ -54,6 +123,7 @@ main (int argc, char **argv) test_init (argc, argv, NULL); g_test_add_func ("/uri/equality", do_equality_tests); + g_test_add_func ("/uri/copy", do_copy_tests); ret = g_test_run (); |