diff options
author | Chun-wei Fan <fanchunwei@src.gnome.org> | 2022-12-07 12:56:38 +0800 |
---|---|---|
committer | Chun-wei Fan <fanchunwei@src.gnome.org> | 2022-12-07 16:39:32 +0800 |
commit | fac03857ffe632f270060a99bd0e229d551f6300 (patch) | |
tree | 564f9a260c87b3c8575037ee3f95915e9b4282d5 | |
parent | dc0584db12dabd35dbe63ebdc2c3aff5cf37a4bd (diff) | |
download | librest-fac03857ffe632f270060a99bd0e229d551f6300.tar.gz |
rest sources: Remove g_auto*()
Sadly, it is a GCCism, so go back to the old way...
-rw-r--r-- | rest/rest-oauth2-proxy-call.c | 23 | ||||
-rw-r--r-- | rest/rest-oauth2-proxy.c | 112 | ||||
-rw-r--r-- | rest/rest-proxy-call.c | 19 | ||||
-rw-r--r-- | rest/rest-utils.c | 3 |
4 files changed, 91 insertions, 66 deletions
diff --git a/rest/rest-oauth2-proxy-call.c b/rest/rest-oauth2-proxy-call.c index cfed247..bbac7ca 100644 --- a/rest/rest-oauth2-proxy-call.c +++ b/rest/rest-oauth2-proxy-call.c @@ -26,8 +26,9 @@ rest_oauth2_proxy_call_prepare (RestProxyCall *call, GError **error) { RestOAuth2Proxy *proxy = NULL; - g_autoptr(GDateTime) now = NULL; + GDateTime *now; GDateTime *expiration_date = NULL; + gboolean failed = FALSE; g_return_val_if_fail (REST_IS_OAUTH2_PROXY_CALL (call), FALSE); @@ -37,16 +38,16 @@ rest_oauth2_proxy_call_prepare (RestProxyCall *call, expiration_date = rest_oauth2_proxy_get_expiration_date (proxy); // access token expired -> refresh - if (g_date_time_compare (now, expiration_date) > 0) - { - g_set_error (error, - REST_OAUTH2_ERROR, - REST_OAUTH2_ERROR_ACCESS_TOKEN_EXPIRED, - "Access token is expired"); - return FALSE; - } - - return TRUE; + failed = (g_date_time_compare (now, expiration_date) > 0); + + if (failed) + g_set_error (error, + REST_OAUTH2_ERROR, + REST_OAUTH2_ERROR_ACCESS_TOKEN_EXPIRED, + "Access token is expired"); + + g_date_time_unref (now); + return !failed; } static void diff --git a/rest/rest-oauth2-proxy.c b/rest/rest-oauth2-proxy.c index 9511f97..489c5ce 100644 --- a/rest/rest-oauth2-proxy.c +++ b/rest/rest-oauth2-proxy.c @@ -60,8 +60,8 @@ rest_oauth2_proxy_parse_access_token (RestOAuth2Proxy *self, GBytes *payload, GTask *task) { - g_autoptr(JsonParser) parser = NULL; - g_autoptr(GError) error = NULL; + JsonParser *parser; + GError *error; JsonNode *root; JsonObject *root_object; const gchar *data; @@ -76,12 +76,18 @@ rest_oauth2_proxy_parse_access_token (RestOAuth2Proxy *self, parser = json_parser_new (); json_parser_load_from_data (parser, data, size, &error); if (error != NULL) + g_task_return_error (task, error); + else + root = json_parser_get_root (parser); + + g_object_unref (parser); + + if (error != NULL) { - g_task_return_error (task, error); + g_clear_error (&error); return; } - root = json_parser_get_root (parser); root_object = json_node_get_object (root); if (json_object_has_member (root_object, "access_token")) @@ -98,9 +104,10 @@ rest_oauth2_proxy_parse_access_token (RestOAuth2Proxy *self, } else if (json_object_has_member (root_object, "expires_in")) { - g_autoptr(GDateTime) now = g_date_time_new_now_utc (); + GDateTime *now = g_date_time_new_now_utc (); expires_in = json_object_get_int_member (root_object, "expires_in"); rest_oauth2_proxy_set_expiration_date (self, g_date_time_add_seconds (now, expires_in)); + g_date_time_unref (now); } g_task_return_boolean (task, TRUE); @@ -111,7 +118,7 @@ rest_oauth2_proxy_new_call (RestProxy *proxy) { RestOAuth2Proxy *self = (RestOAuth2Proxy *)proxy; RestProxyCall *call; - g_autofree gchar *auth = NULL; + gchar *auth; g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); @@ -119,6 +126,7 @@ rest_oauth2_proxy_new_call (RestProxy *proxy) call = g_object_new (REST_TYPE_OAUTH2_PROXY_CALL, "proxy", proxy, NULL); rest_proxy_call_add_header (call, "Authorization", auth); + g_free (auth); return call; } @@ -349,10 +357,11 @@ rest_oauth2_proxy_build_authorization_url (RestOAuth2Proxy *self, gchar **state) { RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); - g_autoptr(GHashTable) params = NULL; - g_autoptr(GUri) auth = NULL; - g_autoptr(GUri) authorization_url = NULL; - g_autofree gchar *params_string = NULL; + GHashTable *params; + GUri *auth; + GUri *authorization_url; + gchar *params_string; + gchar *result; g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); @@ -380,7 +389,13 @@ rest_oauth2_proxy_build_authorization_url (RestOAuth2Proxy *self, g_uri_get_path (auth), params_string, NULL); - return g_uri_to_string (authorization_url); + g_free (params_string); + g_hash_table_destroy (params); + result = g_uri_to_string (authorization_url); + g_uri_unref (authorization_url); + g_uri_unref (auth); + + return result; } static void @@ -389,7 +404,7 @@ rest_oauth2_proxy_fetch_access_token_cb (SoupMessage *msg, GError *error, gpointer user_data) { - g_autoptr(GTask) task = user_data; + GTask *task = user_data; RestOAuth2Proxy *self; g_assert (G_IS_TASK (task)); @@ -399,10 +414,11 @@ rest_oauth2_proxy_fetch_access_token_cb (SoupMessage *msg, if (error) { g_task_return_error (task, error); + g_object_unref (task); return; } - REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, body, g_steal_pointer (&task)); + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, body, task); } void @@ -414,9 +430,9 @@ rest_oauth2_proxy_fetch_access_token_async (RestOAuth2Proxy *self, gpointer user_data) { RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); - g_autoptr(SoupMessage) msg = NULL; - g_autoptr(GTask) task = NULL; - g_autoptr(GHashTable) params = NULL; + SoupMessage *msg; + GTask *task; + GHashTable *params; g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); g_return_if_fail (authorization_code != NULL); @@ -438,13 +454,12 @@ rest_oauth2_proxy_fetch_access_token_async (RestOAuth2Proxy *self, #endif _rest_proxy_queue_message (REST_PROXY (self), -#if WITH_SOUP_2 - g_steal_pointer (&msg), -#else msg, + cancellable, rest_oauth2_proxy_fetch_access_token_cb, task); + g_hash_table_destroy (params); +#if !WITH_SOUP_2 + g_object_unref (msg); #endif - cancellable, rest_oauth2_proxy_fetch_access_token_cb, g_steal_pointer (&task)); - } /** @@ -471,12 +486,11 @@ rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self, GError **error) { RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); - g_autoptr(SoupMessage) msg = NULL; - g_autoptr(GHashTable) params = NULL; - g_autoptr(GTask) task = NULL; + SoupMessage *msg; + GHashTable *params; + GTask *task; GBytes *payload; - - task = g_task_new (self, NULL, NULL, NULL); + gboolean failed = FALSE; g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), FALSE); @@ -488,6 +502,8 @@ rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self, return FALSE; } + task = g_task_new (self, NULL, NULL, NULL); + params = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_insert (params, "client_id", priv->client_id); @@ -501,13 +517,17 @@ rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self, msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash (params)); #endif payload = _rest_proxy_send_message (REST_PROXY (self), msg, NULL, error); - if (error && *error) - { - return FALSE; - } + failed = (error && *error); + + if (!failed) + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, task); + else + g_object_unref (task); - REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task)); - return TRUE; + g_hash_table_destroy (params); + g_object_unref (msg); + + return !failed; } static void @@ -516,7 +536,7 @@ rest_oauth2_proxy_refresh_access_token_cb (SoupMessage *msg, GError *error, gpointer user_data) { - g_autoptr(GTask) task = user_data; + GTask *task = user_data; RestOAuth2Proxy *self; g_assert (G_IS_TASK (task)); @@ -526,10 +546,11 @@ rest_oauth2_proxy_refresh_access_token_cb (SoupMessage *msg, if (error) { g_task_return_error (task, error); + g_object_unref (task); return; } - REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task)); + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, task); } void @@ -539,20 +560,21 @@ rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy *self, gpointer user_data) { RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); - g_autoptr(SoupMessage) msg = NULL; - g_autoptr(GHashTable) params = NULL; - g_autoptr(GTask) task = NULL; - - task = g_task_new (self, cancellable, callback, user_data); + SoupMessage *msg; + GHashTable *params; + GTask *task; g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + task = g_task_new (self, cancellable, callback, user_data); + if (priv->refresh_token == NULL) { g_task_return_new_error (task, REST_OAUTH2_ERROR, REST_OAUTH2_ERROR_NO_REFRESH_TOKEN, "No refresh token available"); + g_object_unref (task); return; } @@ -569,14 +591,16 @@ rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy *self, msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash (params)); #endif _rest_proxy_queue_message (REST_PROXY (self), -#if WITH_SOUP_2 - g_steal_pointer (&msg), -#else msg, -#endif cancellable, rest_oauth2_proxy_refresh_access_token_cb, - g_steal_pointer (&task)); + task); + +#if !WITH_SOUP2 + g_object_unref (msg); +#endif + + g_hash_table_destroy (params); } /** diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c index 851b397..712d617 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -762,14 +762,16 @@ authenticate (RestProxyCall *call, SoupMessage *message) { RestProxyCallPrivate *priv = GET_PRIVATE (call); - g_autofree char *username; - g_autofree char *password; + char *username; + char *password; if (retrying) return FALSE; g_object_get (priv->proxy, "username", &username, "password", &password, NULL); soup_auth_authenticate (soup_auth, username, password); + g_free (password); + g_free (username); return TRUE; } @@ -1007,23 +1009,20 @@ _call_message_call_completed_cb (SoupMessage *message, GError *error, gpointer user_data) { - g_autoptr(GTask) task = user_data; + GTask *task = user_data; RestProxyCall *call; call = REST_PROXY_CALL (g_task_get_source_object (task)); - if (error) - { - g_task_return_error (task, error); - return; - } - - finish_call (call, message, payload, &error); + if (!error) + finish_call (call, message, payload, &error); if (error != NULL) g_task_return_error (task, error); else g_task_return_boolean (task, TRUE); + + g_object_unref (task); } /** diff --git a/rest/rest-utils.c b/rest/rest-utils.c index df283e0..72451c9 100644 --- a/rest/rest-utils.c +++ b/rest/rest-utils.c @@ -29,7 +29,7 @@ gchar * random_string (guint length) { - g_autoptr(GRand) rand = g_rand_new (); + GRand *rand = g_rand_new (); gchar *buffer = g_malloc0 (sizeof (gchar) * length + 1); gchar alphabeth[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"; @@ -38,6 +38,7 @@ random_string (guint length) buffer[i] = alphabeth[g_rand_int (rand) % (sizeof (alphabeth) - 1)]; } buffer[length] = '\0'; + g_rand_free (rand); return buffer; } |