diff options
-rw-r--r-- | lib/safe-browsing/ephy-gsb-service.c | 186 | ||||
-rw-r--r-- | lib/sync/debug/ephy-sync-debug.c | 129 | ||||
-rw-r--r-- | lib/sync/ephy-sync-service.c | 576 | ||||
-rw-r--r-- | meson.build | 16 | ||||
-rw-r--r-- | meson_options.txt | 6 | ||||
-rw-r--r-- | src/ephy-suggestion-model.c | 35 | ||||
-rw-r--r-- | tests/ephy-web-view-test.c | 41 |
7 files changed, 811 insertions, 178 deletions
diff --git a/lib/safe-browsing/ephy-gsb-service.c b/lib/safe-browsing/ephy-gsb-service.c index 701a361f9..97086038f 100644 --- a/lib/safe-browsing/ephy-gsb-service.c +++ b/lib/safe-browsing/ephy-gsb-service.c @@ -51,6 +51,8 @@ struct _EphyGSBService { gint64 back_off_exit_time; gint64 back_off_num_fails; + GThread *update_thread; + GMainLoop *update_loop; SoupSession *session; }; @@ -168,11 +170,18 @@ ephy_gsb_service_schedule_update (EphyGSBService *self) LOG ("Next update scheduled in %ld seconds", interval); } -static void -ephy_gsb_service_update_thread (GTask *task, - EphyGSBService *self, - gpointer task_data, - GCancellable *cancellable) +static gboolean +ephy_gsb_service_update_finished_cb (EphyGSBService *self) +{ + g_atomic_int_set (&self->is_updating, FALSE); + g_signal_emit (self, signals[UPDATE_FINISHED], 0); + ephy_gsb_service_schedule_update (self); + + return G_SOURCE_REMOVE; +} + +static gboolean +ephy_gsb_service_update_in_thread (EphyGSBService *self) { JsonNode *body_node = NULL; JsonObject *body_obj; @@ -181,6 +190,12 @@ ephy_gsb_service_update_thread (GTask *task, GList *threat_lists = NULL; char *url = NULL; char *body; + g_autoptr (GBytes) response_body = NULL; + guint status_code; +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; + g_autoptr (GError) error = NULL; +#endif g_assert (EPHY_IS_GSB_SERVICE (self)); @@ -205,12 +220,28 @@ ephy_gsb_service_update_thread (GTask *task, body = ephy_gsb_utils_make_list_updates_request (threat_lists); url = g_strdup_printf ("%sthreatListUpdates:fetch?key=%s", API_PREFIX, self->api_key); msg = soup_message_new (SOUP_METHOD_POST, url); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new_take (body, strlen (body)); + soup_message_set_request_body_from_bytes (msg, "application/json", bytes); + response_body = soup_session_send_and_read (self->session, msg, NULL, &error); + if (!response_body) { + LOG ("Cannot update threat lists: %s", error->message); + ephy_gsb_service_update_back_off_mode (self); + self->next_list_updates_time = self->back_off_exit_time; + goto out; + } + + status_code = soup_message_get_status (msg); +#else soup_message_set_request (msg, "application/json", SOUP_MEMORY_TAKE, body, strlen (body)); soup_session_send_message (self->session, msg); + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif /* Handle unsuccessful responses. */ - if (msg->status_code != 200) { - LOG ("Cannot update threat lists, got: %u, %s", msg->status_code, msg->response_body->data); + if (status_code != 200) { + LOG ("Cannot update threat lists, got: %u, %s", status_code, (const char *)g_bytes_get_data (response_body, NULL)); ephy_gsb_service_update_back_off_mode (self); self->next_list_updates_time = self->back_off_exit_time; goto out; @@ -219,7 +250,7 @@ ephy_gsb_service_update_thread (GTask *task, /* Successful response, reset back-off mode. */ ephy_gsb_service_reset_back_off_mode (self); - body_node = json_from_string (msg->response_body->data, NULL); + body_node = json_from_string (g_bytes_get_data (response_body, NULL), NULL); if (!body_node || !JSON_NODE_HOLDS_OBJECT (body_node)) { g_warning ("Response is not a valid JSON object"); goto out; @@ -302,33 +333,46 @@ out: ephy_gsb_storage_set_metadata (self->storage, "next_list_updates_time", self->next_list_updates_time); - g_object_unref (self); + g_idle_add_full (G_PRIORITY_DEFAULT, + (GSourceFunc)ephy_gsb_service_update_finished_cb, + g_object_ref (self), + (GDestroyNotify)g_object_unref); + + return G_SOURCE_REMOVE; } -static void -ephy_gsb_service_update_finished_cb (EphyGSBService *self, - GAsyncResult *result, - gpointer user_data) +static gpointer +run_update_thread (EphyGSBService *self) { - g_atomic_int_set (&self->is_updating, FALSE); - g_signal_emit (self, signals[UPDATE_FINISHED], 0); - ephy_gsb_service_schedule_update (self); + GMainContext *context; + + context = g_main_loop_get_context (self->update_loop); + g_main_context_push_thread_default (context); + self->session = soup_session_new_with_options ("user-agent", ephy_user_agent_get (), NULL); + g_main_loop_run (self->update_loop); + g_object_unref (self->session); + g_main_context_pop_thread_default (context); + + return NULL; } static gboolean ephy_gsb_service_update (EphyGSBService *self) { - GTask *task; + GSource *source; g_assert (EPHY_IS_GSB_SERVICE (self)); g_assert (ephy_gsb_storage_is_operable (self->storage)); g_atomic_int_set (&self->is_updating, TRUE); - task = g_task_new (g_object_ref (self), NULL, - (GAsyncReadyCallback)ephy_gsb_service_update_finished_cb, - NULL); - g_task_run_in_thread (task, (GTaskThreadFunc)ephy_gsb_service_update_thread); - g_object_unref (task); + source = g_timeout_source_new (0); + g_source_set_name (source, "[epiphany] gsb_service_update_in_thread"); + g_source_set_callback (source, + (GSourceFunc)ephy_gsb_service_update_in_thread, + g_object_ref (self), + (GDestroyNotify)g_object_unref); + g_source_attach (source, g_main_loop_get_context (self->update_loop)); + g_source_unref (source); return G_SOURCE_REMOVE; } @@ -383,6 +427,9 @@ ephy_gsb_service_finalize (GObject *object) g_free (self->api_key); + g_thread_join (self->update_thread); + g_main_loop_unref (self->update_loop); + G_OBJECT_CLASS (ephy_gsb_service_parent_class)->finalize (object); } @@ -392,10 +439,12 @@ ephy_gsb_service_dispose (GObject *object) EphyGSBService *self = EPHY_GSB_SERVICE (object); g_clear_object (&self->storage); - g_clear_object (&self->session); g_clear_handle_id (&self->source_id, g_source_remove); + if (g_main_loop_is_running (self->update_loop)) + g_main_loop_quit (self->update_loop); + G_OBJECT_CLASS (ephy_gsb_service_parent_class)->dispose (object); } @@ -441,8 +490,11 @@ ephy_gsb_service_constructed (GObject *object) static void ephy_gsb_service_init (EphyGSBService *self) { - self->session = soup_session_new (); - g_object_set (self->session, "user-agent", ephy_user_agent_get (), NULL); + GMainContext *context = g_main_context_new (); + + self->update_loop = g_main_loop_new (context, FALSE); + self->update_thread = g_thread_new ("EphyGSBService", (GThreadFunc)run_update_thread, self); + g_main_context_unref (context); } static void @@ -501,10 +553,17 @@ ephy_gsb_service_new (const char *api_key, #endif } -static void -ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, - GList *prefixes) +typedef struct { + EphyGSBService *self; + GList *prefixes; + GMutex mutex; + GCond condition; +} UpdateFullHashesData; + +static gboolean +ephy_gsb_service_update_full_hashes_in_thread (UpdateFullHashesData *data) { + EphyGSBService *self = data->self; SoupMessage *msg; GList *threat_lists; JsonNode *body_node; @@ -514,36 +573,60 @@ ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, char *url; char *body; double duration; + g_autoptr (GBytes) response_body = NULL; + guint status_code; +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; + g_autoptr (GError) error = NULL; +#endif g_assert (EPHY_IS_GSB_SERVICE (self)); g_assert (ephy_gsb_storage_is_operable (self->storage)); - g_assert (prefixes); + g_assert (data->prefixes); + + g_mutex_lock (&data->mutex); if (self->next_full_hashes_time > CURRENT_TIME) { LOG ("Cannot send fullHashes:find request. Requests are restricted for %ld seconds", self->next_full_hashes_time - CURRENT_TIME); - return; + goto unlock; } if (ephy_gsb_service_is_back_off_mode (self)) { LOG ("Cannot send fullHashes:find request. Back-off mode is enabled for %ld seconds", self->back_off_exit_time - CURRENT_TIME); - return; + goto unlock; } threat_lists = ephy_gsb_storage_get_threat_lists (self->storage); if (!threat_lists) - return; + goto unlock; - body = ephy_gsb_utils_make_full_hashes_request (threat_lists, prefixes); + body = ephy_gsb_utils_make_full_hashes_request (threat_lists, data->prefixes); url = g_strdup_printf ("%sfullHashes:find?key=%s", API_PREFIX, self->api_key); msg = soup_message_new (SOUP_METHOD_POST, url); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new_take (body, strlen (body)); + soup_message_set_request_body_from_bytes (msg, "application/json", bytes); + response_body = soup_session_send_and_read (self->session, msg, NULL, &error); + if (!response_body) { + LOG ("Cannot update full hashes: %s", error->message); + ephy_gsb_service_update_back_off_mode (self); + self->next_list_updates_time = self->back_off_exit_time; + goto out; + } + + status_code = soup_message_get_status (msg); +#else soup_message_set_request (msg, "application/json", SOUP_MEMORY_TAKE, body, strlen (body)); soup_session_send_message (self->session, msg); + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif /* Handle unsuccessful responses. */ - if (msg->status_code != 200) { - LOG ("Cannot update full hashes, got: %u, %s", msg->status_code, msg->response_body->data); + if (status_code != 200) { + LOG ("Cannot update full hashes, got: %u, %s", status_code, (const char *)g_bytes_get_data (response_body, NULL)); ephy_gsb_service_update_back_off_mode (self); goto out; } @@ -551,7 +634,7 @@ ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, /* Successful response, reset back-off mode. */ ephy_gsb_service_reset_back_off_mode (self); - body_node = json_from_string (msg->response_body->data, NULL); + body_node = json_from_string (g_bytes_get_data (response_body, NULL), NULL); if (!body_node || !JSON_NODE_HOLDS_OBJECT (body_node)) { g_warning ("Response is not a valid JSON object"); goto out; @@ -592,7 +675,7 @@ ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, duration_str = json_object_get_string_member (body_obj, "negativeCacheDuration"); /* g_ascii_strtod() ignores trailing characters, i.e. 's' character. */ duration = g_ascii_strtod (duration_str, NULL); - for (GList *l = prefixes; l && l->data; l = l->next) + for (GList *l = data->prefixes; l && l->data; l = l->next) ephy_gsb_storage_update_hash_prefix_expiration (self->storage, l->data, floor (duration)); /* Handle minimum wait duration. */ @@ -608,7 +691,34 @@ ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, out: g_free (url); g_list_free_full (threat_lists, (GDestroyNotify)ephy_gsb_threat_list_free); - g_object_unref (msg); + g_clear_object (&msg); + +unlock: + g_cond_signal (&data->condition); + g_mutex_unlock (&data->mutex); + + return G_SOURCE_REMOVE; +} + +static void +ephy_gsb_service_update_full_hashes_sync (EphyGSBService *self, + GList *prefixes) +{ + UpdateFullHashesData data = { self, prefixes, { }, { } }; + GSource *source; + + g_mutex_lock (&data.mutex); + + source = g_timeout_source_new (0); + g_source_set_name (source, "[epiphany] gsb_service_update_full_hashes_in_thread"); + g_source_set_callback (source, + (GSourceFunc)ephy_gsb_service_update_full_hashes_in_thread, + &data, NULL); + g_source_attach (source, g_main_loop_get_context (self->update_loop)); + g_source_unref (source); + + g_cond_wait (&data.condition, &data.mutex); + g_mutex_unlock (&data.mutex); } static void diff --git a/lib/sync/debug/ephy-sync-debug.c b/lib/sync/debug/ephy-sync-debug.c index 2eb266770..892ffeabf 100644 --- a/lib/sync/debug/ephy-sync-debug.c +++ b/lib/sync/debug/ephy-sync-debug.c @@ -225,6 +225,7 @@ ephy_sync_debug_prepare_soup_message (const char *url, SyncCryptoHawkOptions *options = NULL; SyncCryptoHawkHeader *header; SoupMessage *msg; + SoupMessageHeaders *request_headers; const char *content_type = "application/json"; g_assert (url); @@ -236,18 +237,33 @@ ephy_sync_debug_prepare_soup_message (const char *url, msg = soup_message_new (method, url); +#if SOUP_CHECK_VERSION (2, 99, 4) + request_headers = soup_message_get_request_headers (msg); +#else + request_headers = msg->request_headers; +#endif + if (body) { +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; +#endif + options = ephy_sync_crypto_hawk_options_new (NULL, NULL, NULL, content_type, NULL, NULL, NULL, body, NULL); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new (body, strlen (body)); + soup_message_set_request_body_from_bytes (msg, content_type, bytes); +#else soup_message_set_request (msg, content_type, SOUP_MEMORY_COPY, body, strlen (body)); +#endif } if (!g_strcmp0 (method, "PUT") || !g_strcmp0 (method, "POST")) - soup_message_headers_append (msg->request_headers, "content-type", content_type); + soup_message_headers_append (request_headers, "content-type", content_type); header = ephy_sync_crypto_hawk_header_new (url, method, hawk_id, hawk_key, hawk_key_len, options); - soup_message_headers_append (msg->request_headers, "authorization", header->header); + soup_message_headers_append (request_headers, "authorization", header->header); ephy_sync_crypto_hawk_header_free (header); if (options) @@ -267,7 +283,7 @@ ephy_sync_debug_get_signed_certificate (const char *session_token, JsonObject *json; JsonObject *public_key; JsonObject *json_body; - GError *error = NULL; + g_autoptr (GError) error = NULL; guint8 *id; guint8 *key; guint8 *tmp; @@ -279,6 +295,7 @@ ephy_sync_debug_get_signed_certificate (const char *session_token, char *e; guint status_code; g_autofree char *accounts_server = NULL; + g_autoptr (GBytes) response_body = NULL; g_assert (session_token); g_assert (keypair); @@ -304,17 +321,27 @@ ephy_sync_debug_get_signed_certificate (const char *session_token, msg = ephy_sync_debug_prepare_soup_message (url, "POST", body, id_hex, key, 32); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 4) + response_body = soup_session_send_and_read (session, msg, NULL, &error); + if (!response_body) { + LOG ("Failed to get signed certificate: %s", error->message); + goto free_session; + } + + status_code = soup_message_get_status (msg); +#else status_code = soup_session_send_message (session, msg); + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif if (status_code != 200) { - LOG ("Failed to get signed certificate: %s", msg->response_body->data); + LOG ("Failed to get signed certificate: %s", (const char *)g_bytes_get_data (response_body, NULL)); goto free_session; } - response = json_from_string (msg->response_body->data, &error); + response = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { LOG ("Response is not a valid JSON: %s", error->message); - g_error_free (error); goto free_session; } @@ -350,7 +377,7 @@ ephy_sync_debug_get_storage_credentials (char **storage_endpoint, JsonNode *response; JsonObject *secrets; JsonObject *json; - GError *error = NULL; + g_autoptr (GError) error = NULL; char *certificate; char *audience; char *assertion; @@ -360,6 +387,8 @@ ephy_sync_debug_get_storage_credentials (char **storage_endpoint, guint8 *kb; const char *session_token; guint status_code; + SoupMessageHeaders *request_headers; + g_autoptr (GBytes) response_body = NULL; gboolean success = FALSE; g_autofree char *token_server = NULL; @@ -381,20 +410,35 @@ ephy_sync_debug_get_storage_credentials (char **storage_endpoint, client_state = g_strndup (hashed_kb, 32); authorization = g_strdup_printf ("BrowserID %s", assertion); msg = soup_message_new ("GET", token_server); - soup_message_headers_append (msg->request_headers, "X-Client-State", client_state); - soup_message_headers_append (msg->request_headers, "authorization", authorization); +#if SOUP_CHECK_VERSION (2, 99, 4) + request_headers = soup_message_get_request_headers (msg); +#else + request_headers = msg->request_headers; +#endif + soup_message_headers_append (request_headers, "X-Client-State", client_state); + soup_message_headers_append (request_headers, "authorization", authorization); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 4) + response_body = soup_session_send_and_read (session, msg, NULL, &error); + if (!response_body) { + LOG ("Failed to get storage credentials: %s", error->message); + goto free_session; + } + + status_code = soup_message_get_status (msg); +#else status_code = soup_session_send_message (session, msg); + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif if (status_code != 200) { - LOG ("Failed to get storage credentials: %s", msg->response_body->data); + LOG ("Failed to get storage credentials: %s", (const char *)g_bytes_get_data (response_body, NULL)); goto free_session; } - response = json_from_string (msg->response_body->data, &error); + response = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { LOG ("Response is not a valid JSON: %s", error->message); - g_error_free (error); goto free_session; } @@ -430,11 +474,12 @@ ephy_sync_debug_send_request (const char *endpoint, SoupSession *session; SoupMessage *msg; char *response = NULL; - char *storage_endpoint; - char *storage_id; - char *storage_key; + char *storage_endpoint = NULL; + char *storage_id = NULL; + char *storage_key = NULL; char *url; guint status_code; + g_autoptr (GBytes) response_body = NULL; g_assert (endpoint); g_assert (method); @@ -453,12 +498,20 @@ ephy_sync_debug_send_request (const char *endpoint, (const guint8 *)storage_key, strlen (storage_key)); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 4) + response_body = soup_session_send_and_read (session, msg, NULL, NULL); + status_code = soup_message_get_status (msg); +#else status_code = soup_session_send_message (session, msg); + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif - if (status_code == 200) - response = g_strdup (msg->response_body->data); - else - LOG ("Failed to send storage request: %s", msg->response_body->data); + if (response_body) { + if (status_code == 200) + response = g_strdup (g_bytes_get_data (response_body, NULL)); + else + LOG ("Failed to send storage request: %s", (const char *)g_bytes_get_data (response_body, NULL)); + } g_free (url); g_free (storage_endpoint); @@ -603,7 +656,7 @@ ephy_sync_debug_view_record (const char *collection, g_assert (collection); g_assert (id); - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); response = ephy_sync_debug_send_request (endpoint, "GET", NULL); @@ -671,7 +724,7 @@ ephy_sync_debug_upload_record (const char *collection, if (!bundle) return; - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); body = ephy_sync_debug_make_upload_body (id, record, bundle); response = ephy_sync_debug_send_request (endpoint, "PUT", body); @@ -724,7 +777,7 @@ ephy_sync_debug_delete_collection (const char *collection) array = json_node_get_array (node); for (guint i = 0; i < json_array_get_length (array); i++) { const char *id = json_array_get_string_element (array, i); - char *id_safe = soup_uri_encode (id, NULL); + char *id_safe = g_uri_escape_string (id, NULL, TRUE); char *body = ephy_sync_debug_make_delete_body (id, bundle); char *to = g_strdup_printf ("storage/%s/%s", collection, id_safe); char *resp = ephy_sync_debug_send_request (to, "PUT", body); @@ -772,7 +825,7 @@ ephy_sync_debug_delete_record (const char *collection, if (!bundle) return; - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); body = ephy_sync_debug_make_delete_body (id, bundle); response = ephy_sync_debug_send_request (endpoint, "PUT", body); @@ -839,7 +892,7 @@ ephy_sync_debug_erase_record (const char *collection, g_assert (collection); g_assert (id); - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); response = ephy_sync_debug_send_request (endpoint, "DELETE", NULL); @@ -1029,6 +1082,7 @@ ephy_sync_debug_view_connected_devices (void) char *url; const char *session_token; g_autofree char *accounts_server = NULL; + g_autoptr (GBytes) response_body = NULL; secrets = ephy_sync_debug_load_secrets (); if (!secrets) @@ -1042,9 +1096,15 @@ ephy_sync_debug_view_connected_devices (void) id_hex = ephy_sync_utils_encode_hex (id, 32); msg = ephy_sync_debug_prepare_soup_message (url, "GET", NULL, id_hex, key, 32); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 4) + response_body = soup_session_send_and_read (session, msg, NULL, NULL); +#else soup_session_send_message (session, msg); + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif - LOG ("%s", msg->response_body->data); + if (response_body) + LOG ("%s", (const char *)g_bytes_get_data (response_body, NULL)); g_object_unref (session); g_object_unref (msg); @@ -1074,7 +1134,7 @@ ephy_sync_debug_get_current_device (void) JsonArray *array; SoupSession *session; SoupMessage *msg; - GError *error = NULL; + g_autoptr (GError) error = NULL; guint8 *id; guint8 *key; guint8 *tmp; @@ -1083,6 +1143,7 @@ ephy_sync_debug_get_current_device (void) const char *session_token; guint status_code; g_autofree char *accounts_server = NULL; + g_autoptr (GBytes) response_body = NULL; secrets = ephy_sync_debug_load_secrets (); if (!secrets) @@ -1096,17 +1157,27 @@ ephy_sync_debug_get_current_device (void) id_hex = ephy_sync_utils_encode_hex (id, 32); msg = ephy_sync_debug_prepare_soup_message (url, "GET", NULL, id_hex, key, 32); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 4) + response_body = soup_session_send_and_read (session, msg, NULL, &error); + if (!response_body) { + LOG ("Failed to GET account devices: %s", error->message); + goto free_session; + } + + status_code = soup_message_get_status (msg); +#else status_code = soup_session_send_message (session, msg); + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif if (status_code != 200) { - LOG ("Failed to GET account devices: %s", msg->response_body->data); + LOG ("Failed to GET account devices: %s", (const char *)g_bytes_get_data (response_body, NULL)); goto free_session; } - response = json_from_string (msg->response_body->data, &error); + response = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { LOG ("Response is not a valid JSON: %s", error->message); - g_error_free (error); goto free_session; } diff --git a/lib/sync/ephy-sync-service.c b/lib/sync/ephy-sync-service.c index a9eec409d..265443b52 100644 --- a/lib/sync/ephy-sync-service.c +++ b/lib/sync/ephy-sync-service.c @@ -97,6 +97,12 @@ enum { static guint signals[LAST_SIGNAL]; +#if SOUP_CHECK_VERSION (2, 99, 4) +typedef void (*SoupSessionCallback) (SoupSession *session, + SoupMessage *msg, + gpointer user_data); +#endif + typedef struct { char *endpoint; char *method; @@ -179,6 +185,72 @@ storage_request_async_data_free (StorageRequestAsyncData *data) g_free (data); } +#if SOUP_CHECK_VERSION (2, 99, 4) +typedef struct { + SoupSessionCallback callback; + gpointer user_data; +} SendAndReadAsyncData; + +static SendAndReadAsyncData * +send_and_read_async_data_new (SoupSessionCallback callback, + gpointer user_data) +{ + SendAndReadAsyncData *data; + + data = g_new (SendAndReadAsyncData, 1); + data->callback = callback; + data->user_data = user_data; + + return data; +} + +static void +send_and_read_async_ready_cb (SoupSession *session, + GAsyncResult *result, + SendAndReadAsyncData *data) +{ + GBytes *bytes; + SoupMessage *msg; + GError *error = NULL; + + bytes = soup_session_send_and_read_finish (session, result, &error); + if (!bytes) { + g_warning ("Failed to send request: %s", error->message); + g_error_free (error); + } + + msg = soup_session_get_async_result_message (session, result); + g_object_set_data_full (G_OBJECT (msg), + "ephy-request-body", bytes ? bytes : g_bytes_new (NULL, 0), + (GDestroyNotify)g_bytes_unref); + data->callback (session, msg, data->user_data); + g_free (data); +} + +static void +storage_request_async_ready_cb (SoupSession *session, + GAsyncResult *result, + StorageRequestAsyncData *data) +{ + GBytes *bytes; + SoupMessage *msg; + GError *error = NULL; + + bytes = soup_session_send_and_read_finish (session, result, &error); + if (!bytes) { + g_warning ("Failed to send storage request: %s", error->message); + g_error_free (error); + } + + msg = soup_session_get_async_result_message (session, result); + g_object_set_data_full (G_OBJECT (msg), + "ephy-request-body", bytes ? bytes : g_bytes_new (NULL, 0), + (GDestroyNotify)g_bytes_unref); + data->callback (session, msg, data->user_data); + storage_request_async_data_free (data); +} +#endif + static SignInAsyncData * sign_in_async_data_new (EphySyncService *service, const char *email, @@ -463,9 +535,13 @@ ephy_sync_service_fxa_hawk_post (EphySyncService *self, SyncCryptoHawkOptions *options; SyncCryptoHawkHeader *header; SoupMessage *msg; + SoupMessageHeaders *request_headers; char *url; const char *content_type = "application/json; charset=utf-8"; g_autofree char *accounts_server = NULL; +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; +#endif g_assert (EPHY_IS_SYNC_SERVICE (self)); g_assert (endpoint); @@ -476,16 +552,29 @@ ephy_sync_service_fxa_hawk_post (EphySyncService *self, accounts_server = ephy_sync_utils_get_accounts_server (); url = g_strdup_printf ("%s/%s", accounts_server, endpoint); msg = soup_message_new (SOUP_METHOD_POST, url); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new (request_body, strlen (request_body)); + soup_message_set_request_body_from_bytes (msg, content_type, bytes); + request_headers = soup_message_get_request_headers (msg); +#else soup_message_set_request (msg, content_type, SOUP_MEMORY_COPY, request_body, strlen (request_body)); + request_headers = msg->request_headers; +#endif options = ephy_sync_crypto_hawk_options_new (NULL, NULL, NULL, content_type, NULL, NULL, NULL, request_body, NULL); header = ephy_sync_crypto_hawk_header_new (url, "POST", id, key, key_len, options); - soup_message_headers_append (msg->request_headers, "authorization", header->header); - soup_message_headers_append (msg->request_headers, "content-type", content_type); + soup_message_headers_append (request_headers, "authorization", header->header); + soup_message_headers_append (request_headers, "content-type", content_type); +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)send_and_read_async_ready_cb, + send_and_read_async_data_new (callback, user_data)); +#else soup_session_queue_message (self->session, msg, callback, user_data); +#endif g_free (url); ephy_sync_crypto_hawk_options_free (options); @@ -503,6 +592,7 @@ ephy_sync_service_fxa_hawk_get (EphySyncService *self, { SyncCryptoHawkHeader *header; SoupMessage *msg; + SoupMessageHeaders *request_headers; char *url; g_autofree char *accounts_server = NULL; @@ -515,8 +605,19 @@ ephy_sync_service_fxa_hawk_get (EphySyncService *self, url = g_strdup_printf ("%s/%s", accounts_server, endpoint); msg = soup_message_new (SOUP_METHOD_GET, url); header = ephy_sync_crypto_hawk_header_new (url, "GET", id, key, key_len, NULL); - soup_message_headers_append (msg->request_headers, "authorization", header->header); +#if SOUP_CHECK_VERSION (2, 99, 4) + request_headers = soup_message_get_request_headers (msg); +#else + request_headers = msg->request_headers; +#endif + soup_message_headers_append (request_headers, "authorization", header->header); +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)send_and_read_async_ready_cb, + send_and_read_async_data_new (callback, user_data)); +#else soup_session_queue_message (self->session, msg, callback, user_data); +#endif g_free (url); ephy_sync_crypto_hawk_header_free (header); @@ -529,6 +630,7 @@ ephy_sync_service_send_storage_request (EphySyncService *self, SyncCryptoHawkOptions *options = NULL; SyncCryptoHawkHeader *header; SoupMessage *msg; + SoupMessageHeaders *request_headers; char *url; char *if_modified_since = NULL; char *if_unmodified_since = NULL; @@ -541,24 +643,39 @@ ephy_sync_service_send_storage_request (EphySyncService *self, msg = soup_message_new (data->method, url); if (data->request_body) { +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; +#endif options = ephy_sync_crypto_hawk_options_new (NULL, NULL, NULL, content_type, NULL, NULL, NULL, data->request_body, NULL); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new (data->request_body, strlen (data->request_body)); + soup_message_set_request_body_from_bytes (msg, content_type, bytes); +#else soup_message_set_request (msg, content_type, SOUP_MEMORY_COPY, data->request_body, strlen (data->request_body)); +#endif } +#if SOUP_CHECK_VERSION (2, 99, 4) + request_headers = soup_message_get_request_headers (msg); +#else + request_headers = msg->request_headers; +#endif + + if (!g_strcmp0 (data->method, SOUP_METHOD_PUT) || !g_strcmp0 (data->method, SOUP_METHOD_POST)) - soup_message_headers_append (msg->request_headers, "content-type", content_type); + soup_message_headers_append (request_headers, "content-type", content_type); if (data->modified_since >= 0) { if_modified_since = g_strdup_printf ("%" PRId64, data->modified_since); - soup_message_headers_append (msg->request_headers, "X-If-Modified-Since", if_modified_since); + soup_message_headers_append (request_headers, "X-If-Modified-Since", if_modified_since); } if (data->unmodified_since >= 0) { if_unmodified_since = g_strdup_printf ("%" PRId64, data->unmodified_since); - soup_message_headers_append (msg->request_headers, "X-If-Unmodified-Since", if_unmodified_since); + soup_message_headers_append (request_headers, "X-If-Unmodified-Since", if_unmodified_since); } header = ephy_sync_crypto_hawk_header_new (url, data->method, @@ -566,8 +683,15 @@ ephy_sync_service_send_storage_request (EphySyncService *self, (guint8 *)self->storage_credentials_key, strlen (self->storage_credentials_key), options); - soup_message_headers_append (msg->request_headers, "authorization", header->header); + soup_message_headers_append (request_headers, "authorization", header->header); +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)storage_request_async_ready_cb, + data); +#else soup_session_queue_message (self->session, msg, data->callback, data->user_data); + storage_request_async_data_free (data); +#endif g_free (url); g_free (if_modified_since); @@ -575,7 +699,6 @@ ephy_sync_service_send_storage_request (EphySyncService *self, ephy_sync_crypto_hawk_header_free (header); if (options) ephy_sync_crypto_hawk_options_free (options); - storage_request_async_data_free (data); } static void @@ -731,14 +854,47 @@ destroy_session_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to destroy session. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } else { LOG ("Successfully destroyed session"); } } +#if SOUP_CHECK_VERSION (2, 99, 4) +static void +destroy_session_send_and_read_ready_cb (SoupSession *session, + GAsyncResult *result, + gpointer user_data) +{ + GBytes *bytes; + SoupMessage *msg; + g_autoptr (GError) error = NULL; + + bytes = soup_session_send_and_read_finish (session, result, &error); + if (!bytes) + g_warning ("Failed to send request: %s", error->message); + + msg = soup_session_get_async_result_message (session, result); + g_object_set_data_full (G_OBJECT (msg), + "ephy-request-body", bytes ? bytes : g_bytes_new (NULL, 0), + (GDestroyNotify)g_bytes_unref); + destroy_session_cb (session, msg, user_data); +} +#endif + static void ephy_sync_service_destroy_session (EphySyncService *self, const char *session_token) @@ -746,6 +902,7 @@ ephy_sync_service_destroy_session (EphySyncService *self, SyncCryptoHawkOptions *options; SyncCryptoHawkHeader *header; SoupMessage *msg; + SoupMessageHeaders *request_headers; guint8 *token_id; guint8 *req_hmac_key; guint8 *tmp; @@ -754,6 +911,9 @@ ephy_sync_service_destroy_session (EphySyncService *self, const char *content_type = "application/json; charset=utf-8"; const char *request_body = "{}"; g_autofree char *accounts_server = NULL; +#if SOUP_CHECK_VERSION (2, 99, 4) + g_autoptr (GBytes) bytes = NULL; +#endif g_assert (EPHY_IS_SYNC_SERVICE (self)); if (!session_token) @@ -768,16 +928,29 @@ ephy_sync_service_destroy_session (EphySyncService *self, token_id_hex = ephy_sync_utils_encode_hex (token_id, 32); msg = soup_message_new (SOUP_METHOD_POST, url); +#if SOUP_CHECK_VERSION (2, 99, 4) + bytes = g_bytes_new_static (request_body, strlen (request_body)); + soup_message_set_request_body_from_bytes (msg, content_type, bytes); + request_headers = soup_message_get_request_headers (msg); +#else soup_message_set_request (msg, content_type, SOUP_MEMORY_STATIC, request_body, strlen (request_body)); + request_headers = msg->request_headers; +#endif options = ephy_sync_crypto_hawk_options_new (NULL, NULL, NULL, content_type, NULL, NULL, NULL, request_body, NULL); header = ephy_sync_crypto_hawk_header_new (url, "POST", token_id_hex, req_hmac_key, 32, options); - soup_message_headers_append (msg->request_headers, "authorization", header->header); - soup_message_headers_append (msg->request_headers, "content-type", content_type); + soup_message_headers_append (request_headers, "authorization", header->header); + soup_message_headers_append (request_headers, "content-type", content_type); +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)destroy_session_send_and_read_ready_cb, + NULL); +#else soup_session_queue_message (self->session, msg, destroy_session_cb, NULL); +#endif g_free (token_id_hex); g_free (token_id); @@ -823,13 +996,23 @@ get_storage_credentials_cb (SoupSession *session, const char *message; const char *suggestion; int duration; - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to obtain storage credentials. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out_error; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -876,10 +1059,33 @@ out: g_error_free (error); } +#if SOUP_CHECK_VERSION (2, 99, 4) +static void +get_storage_credentials_ready_cb (SoupSession *session, + GAsyncResult *result, + gpointer user_data) +{ + GBytes *bytes; + SoupMessage *msg; + g_autoptr (GError) error = NULL; + + bytes = soup_session_send_and_read_finish (session, result, &error); + if (!bytes) + g_warning ("Failed to send store credentials request: %s\n", error->message); + + msg = soup_session_get_async_result_message (session, result); + g_object_set_data_full (G_OBJECT (msg), + "ephy-request-body", bytes ? bytes : g_bytes_new (NULL, 0), + (GDestroyNotify)g_bytes_unref); + get_storage_credentials_cb (session, msg, user_data); +} +#endif + static void ephy_sync_service_trade_browserid_assertion (EphySyncService *self) { SoupMessage *msg; + SoupMessageHeaders *request_headers; guint8 *kb; char *hashed_kb; char *client_state; @@ -902,12 +1108,23 @@ ephy_sync_service_trade_browserid_assertion (EphySyncService *self) authorization = g_strdup_printf ("BrowserID %s", assertion); msg = soup_message_new (SOUP_METHOD_GET, token_server); +#if SOUP_CHECK_VERSION (2, 99, 4) + request_headers = soup_message_get_request_headers (msg); +#else + request_headers = msg->request_headers; +#endif /* We need to add the X-Client-State header so that the Token Server will * recognize accounts that were previously used to sync Firefox data too. */ - soup_message_headers_append (msg->request_headers, "X-Client-State", client_state); - soup_message_headers_append (msg->request_headers, "authorization", authorization); + soup_message_headers_append (request_headers, "X-Client-State", client_state); + soup_message_headers_append (request_headers, "authorization", authorization); +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)get_storage_credentials_ready_cb, + self); +#else soup_session_queue_message (self->session, msg, get_storage_credentials_cb, self); +#endif g_free (kb); g_free (hashed_kb); @@ -929,8 +1146,18 @@ get_signed_certificate_cb (SoupSession *session, const char *suggestion = NULL; const char *message = NULL; const char *certificate = NULL; - - node = json_from_string (msg->response_body->data, &error); + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -941,7 +1168,7 @@ get_signed_certificate_cb (SoupSession *session, goto out_error; } - if (msg->status_code == 200) { + if (status_code == 200) { certificate = json_object_get_string_member (json, "cert"); if (!certificate) { g_warning ("JSON object has missing or invalid 'cert' member"); @@ -971,7 +1198,7 @@ get_signed_certificate_cb (SoupSession *session, } g_warning ("Failed to sign certificate. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); out_error: message = message ? message : _("Failed to obtain signed certificate."); @@ -1100,11 +1327,22 @@ delete_synchronizable_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { - if (msg->status_code == 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code == 200) { LOG ("Successfully deleted from server"); } else { g_warning ("Failed to delete object. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } } @@ -1138,7 +1376,7 @@ ephy_sync_service_delete_synchronizable (EphySyncService *self, /* Firefox uses UUIDs with curly braces as IDs for saved passwords records. * Curly braces are unsafe characters in URLs so they must be encoded. */ - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); node = json_node_new (JSON_NODE_OBJECT); @@ -1180,13 +1418,23 @@ download_synchronizable_cb (SoupSession *session, GType type; const char *collection; gboolean is_deleted; - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to download object. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON"); goto out; @@ -1244,7 +1492,7 @@ ephy_sync_service_download_synchronizable (EphySyncService *self, /* Firefox uses UUIDs with curly braces as IDs for saved passwords records. * Curly braces are unsafe characters in URLs so they must be encoded. */ - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); data = sync_async_data_new (self, manager, synchronizable); @@ -1264,21 +1512,31 @@ upload_synchronizable_cb (SoupSession *session, { SyncAsyncData *data = (SyncAsyncData *)user_data; gint64 time_modified; + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif /* Code 412 means that there is a more recent version on the server. * Download it. */ - if (msg->status_code == 412) { + if (status_code == 412) { LOG ("Found a newer version of the object on the server, downloading it..."); ephy_sync_service_download_synchronizable (data->service, data->manager, data->synchronizable); - } else if (msg->status_code == 200) { + } else if (status_code == 200) { LOG ("Successfully uploaded to server"); - time_modified = ceil (g_ascii_strtod (msg->response_body->data, NULL)); + time_modified = ceil (g_ascii_strtod (g_bytes_get_data (response_body, NULL), NULL)); ephy_synchronizable_set_server_time_modified (data->synchronizable, time_modified); ephy_synchronizable_manager_save (data->manager, data->synchronizable); } else { g_warning ("Failed to upload object. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } sync_async_data_free (data); @@ -1315,7 +1573,7 @@ ephy_sync_service_upload_synchronizable (EphySyncService *self, /* Firefox uses UUIDs with curly braces as IDs for saved passwords records. * Curly braces are unsafe characters in URLs so they must be encoded. */ - id_safe = soup_uri_encode (id, NULL); + id_safe = g_uri_escape_string (id, NULL, TRUE); endpoint = g_strdup_printf ("storage/%s/%s", collection, id_safe); data = sync_async_data_new (self, manager, synchronizable); body = json_to_string (bso, FALSE); @@ -1385,14 +1643,27 @@ commit_batch_cb (SoupSession *session, { BatchUploadAsyncData *data = user_data; const char *last_modified; - - if (msg->status_code != 200) { + guint status_code; + SoupMessageHeaders *response_headers; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_headers = soup_message_get_response_headers (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_headers = msg->response_headers; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to commit batch. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } else { LOG ("Successfully committed batches"); /* Update sync time. */ - last_modified = soup_message_headers_get_one (msg->response_headers, "X-Last-Modified"); + last_modified = soup_message_headers_get_one (response_headers, "X-Last-Modified"); ephy_synchronizable_manager_set_sync_time (data->manager, g_ascii_strtod (last_modified, NULL)); } @@ -1409,11 +1680,21 @@ upload_batch_cb (SoupSession *session, BatchUploadAsyncData *data = user_data; const char *collection; char *endpoint = NULL; + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif /* Note: "202 Accepted" status code. */ - if (msg->status_code != 202) { + if (status_code != 202) { g_warning ("Failed to upload batch. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } else { LOG ("Successfully uploaded batch"); } @@ -1445,26 +1726,36 @@ start_batch_upload_cb (SoupSession *session, GPtrArray *batches = NULL; JsonNode *node = NULL; JsonObject *object; - GError *error = NULL; + g_autoptr (GError) error = NULL; const char *collection; char *endpoint = NULL; + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif /* Note: "202 Accepted" status code. */ - if (msg->status_code != 202) { + if (status_code != 202) { g_warning ("Failed to start batch upload. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); - g_error_free (error); goto out; } object = json_node_get_object (node); - data->batch_id = soup_uri_encode (json_object_get_string_member (object, "batch"), NULL); + data->batch_id = g_uri_escape_string (json_object_get_string_member (object, "batch"), + NULL, TRUE); collection = ephy_synchronizable_manager_get_collection_name (data->manager); endpoint = g_strdup_printf ("storage/%s?batch=%s", collection, data->batch_id); @@ -1536,19 +1827,29 @@ sync_collection_cb (SoupSession *session, SyncCryptoKeyBundle *bundle = NULL; JsonNode *node = NULL; JsonArray *array = NULL; - GError *error = NULL; + g_autoptr (GError) error = NULL; GType type; const char *collection; gboolean is_deleted; + guint status_code; + g_autoptr (GBytes) response_body = NULL; collection = ephy_synchronizable_manager_get_collection_name (data->manager); - if (msg->status_code != 200) { +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to get records in collection %s. Status code: %u, response: %s", - collection, msg->status_code, msg->response_body->data); + collection, status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out_error; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -1597,8 +1898,6 @@ out_no_error: ephy_sync_crypto_key_bundle_free (bundle); if (node) json_node_unref (node); - if (error) - g_error_free (error); } static void @@ -1867,10 +2166,20 @@ upload_client_record_cb (SoupSession *session, gpointer user_data) { EphySyncService *self = EPHY_SYNC_SERVICE (user_data); - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to upload client record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); if (self->is_signing_in) ephy_sync_service_report_sign_in_error (self, _("Failed to upload client record."), NULL, TRUE); } else { @@ -2066,10 +2375,20 @@ upload_crypto_keys_cb (SoupSession *session, gpointer user_data) { EphySyncService *self = EPHY_SYNC_SERVICE (user_data); - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to upload crypto/keys record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); ephy_sync_service_report_sign_in_error (self, _("Failed to upload crypto/keys record."), NULL, TRUE); @@ -2129,24 +2448,34 @@ get_crypto_keys_cb (SoupSession *session, SyncCryptoKeyBundle *bundle = NULL; JsonNode *node = NULL; JsonObject *json = NULL; - GError *error = NULL; + g_autoptr (GError) error = NULL; const char *payload; char *crypto_keys = NULL; guint8 *kb = NULL; - - if (msg->status_code == 404) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code == 404) { LOG ("crypto/keys record not found, uploading new one..."); ephy_sync_service_upload_crypto_keys (self); return; } - if (msg->status_code != 200) { + if (status_code != 200) { g_warning ("Failed to get crypto/keys record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out_error; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -2185,8 +2514,6 @@ out_no_error: ephy_sync_crypto_key_bundle_free (bundle); if (node) json_node_unref (node); - if (error) - g_error_free (error); g_free (crypto_keys); g_free (kb); } @@ -2224,10 +2551,20 @@ upload_meta_global_cb (SoupSession *session, gpointer user_data) { EphySyncService *self = EPHY_SYNC_SERVICE (user_data); - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to upload meta/global record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); ephy_sync_service_report_sign_in_error (self, _("Failed to upload meta/global record."), NULL, TRUE); @@ -2296,25 +2633,35 @@ verify_storage_version_cb (SoupSession *session, EphySyncService *self = EPHY_SYNC_SERVICE (user_data); JsonParser *parser = NULL; JsonObject *json = NULL; - GError *error = NULL; + g_autoptr (GError) error = NULL; char *payload = NULL; char *message = NULL; int storage_version; - - if (msg->status_code == 404) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code == 404) { LOG ("meta/global record not found, uploading new one..."); ephy_sync_service_upload_meta_global (self); return; } - if (msg->status_code != 200) { + if (status_code != 200) { g_warning ("Failed to get meta/global record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out_error; } parser = json_parser_new (); - json_parser_load_from_data (parser, msg->response_body->data, -1, &error); + json_parser_load_from_data (parser, g_bytes_get_data (response_body, NULL), -1, &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -2362,8 +2709,6 @@ out_error: out_no_error: if (parser) g_object_unref (parser); - if (error) - g_error_free (error); g_free (payload); g_free (message); } @@ -2387,18 +2732,27 @@ upload_fxa_device_cb (SoupSession *session, EphySyncService *self = user_data; JsonNode *node; JsonObject *object; - GError *error = NULL; - - if (msg->status_code != 200) { + g_autoptr (GError) error = NULL; + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to upload device info on FxA Server. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); goto out_error; } - node = json_from_string (msg->response_body->data, &error); + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); - g_error_free (error); goto out_error; } @@ -2526,10 +2880,20 @@ get_account_keys_cb (SoupSession *session, SignInAsyncData *data = (SignInAsyncData *)user_data; JsonNode *node = NULL; JsonObject *json = NULL; - GError *error = NULL; + g_autoptr (GError) error = NULL; const char *bundle; - - node = json_from_string (msg->response_body->data, &error); + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + node = json_from_string (g_bytes_get_data (response_body, NULL), &error); if (error) { g_warning ("Response is not a valid JSON: %s", error->message); goto out_error; @@ -2540,7 +2904,7 @@ get_account_keys_cb (SoupSession *session, goto out_error; } - if (msg->status_code == 200) { + if (status_code == 200) { bundle = json_object_get_string_member (json, "bundle"); if (!bundle) { g_warning ("JSON object has invalid or missing 'bundle' member"); @@ -2563,7 +2927,7 @@ get_account_keys_cb (SoupSession *session, } g_warning ("Failed to get /account/keys. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); out_error: ephy_sync_service_report_sign_in_error (data->service, @@ -2573,8 +2937,6 @@ out_error: out_no_error: if (node) json_node_unref (node); - if (error) - g_error_free (error); } void @@ -2721,10 +3083,20 @@ delete_open_tabs_record_cb (SoupSession *session, { EphySyncService *self = EPHY_SYNC_SERVICE (user_data); const char *session_token; - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to delete open tabs record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } else { LOG ("Successfully deleted open tabs record"); } @@ -2748,10 +3120,20 @@ delete_client_record_cb (SoupSession *session, EphySyncService *self = EPHY_SYNC_SERVICE (user_data); char *endpoint; char *device_bso_id; - - if (msg->status_code != 200) { + guint status_code; + g_autoptr (GBytes) response_body = NULL; + +#if SOUP_CHECK_VERSION (2, 99, 4) + status_code = soup_message_get_status (msg); + response_body = g_bytes_ref (g_object_get_data (G_OBJECT (msg), "ephy-request-body")); +#else + status_code = msg->status_code; + response_body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + + if (status_code != 200) { g_warning ("Failed to delete client record. Status code: %u, response: %s", - msg->status_code, msg->response_body->data); + status_code, (const char *)g_bytes_get_data (response_body, NULL)); } else { LOG ("Successfully deleted client record"); } diff --git a/meson.build b/meson.build index ddb4346c9..b8c1a3741 100644 --- a/meson.build +++ b/meson.build @@ -75,7 +75,6 @@ conf.set10('ENABLE_GSB', gsb_api_key != '') glib_requirement = '>= 2.67.1' gtk_requirement = '>= 3.24.0' nettle_requirement = '>= 3.4' -webkitgtk_requirement = '>= 2.31.2' cairo_dep = dependency('cairo', version: '>= 1.2') gcr_dep = dependency('gcr-3', version: '>= 3.5.5') @@ -94,13 +93,22 @@ libarchive_dep = dependency('libarchive') libdazzle_dep = dependency('libdazzle-1.0', version: '>= 3.37.1') libhandy_dep = dependency('libhandy-1', version: '>= 1.1.0') libsecret_dep = dependency('libsecret-1', version: '>= 0.19.0') -libsoup_dep = dependency('libsoup-2.4', version: '>= 2.48.0') libxml_dep = dependency('libxml-2.0', version: '>= 2.6.12') nettle_dep = dependency('nettle', version: nettle_requirement) portal_dep = dependency('libportal', version: '>= 0.0.2', required: get_option('libportal')) sqlite3_dep = dependency('sqlite3', version: '>= 3.22') -webkit2gtk_dep = dependency('webkit2gtk-4.0', version: webkitgtk_requirement) -webkit2gtk_web_extension_dep = dependency('webkit2gtk-web-extension-4.0', version: webkitgtk_requirement) + +if get_option('soup2').enabled() + webkitgtk_requirement = '>= 2.31.2' + libsoup_dep = dependency('libsoup-2.4', version: '>= 2.48.0') + webkit2gtk_dep = dependency('webkit2gtk-4.0', version: webkitgtk_requirement) + webkit2gtk_web_extension_dep = dependency('webkit2gtk-web-extension-4.0', version: webkitgtk_requirement) +else + webkitgtk_requirement = '>= 2.33.0' + libsoup_dep = dependency('libsoup-3.0', version: '>= 2.99.4') + webkit2gtk_dep = dependency('webkit2gtk-4.1', version: webkitgtk_requirement) + webkit2gtk_web_extension_dep = dependency('webkit2gtk-web-extension-4.1', version: webkitgtk_requirement) +endif conf.set10('USE_LIBPORTAL', portal_dep.found()) diff --git a/meson_options.txt b/meson_options.txt index dc59c0b78..87586e78b 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -39,3 +39,9 @@ option('gsb_api_key', value: '', description: 'The API key used to access the Google Safe Browsing API v4' ) + +option('soup2', + type: 'feature', + value: 'enabled', + description: 'Use libsoup2' +) diff --git a/src/ephy-suggestion-model.c b/src/ephy-suggestion-model.c index 562514525..101dcb7d4 100644 --- a/src/ephy-suggestion-model.c +++ b/src/ephy-suggestion-model.c @@ -636,10 +636,17 @@ history_query_completed_cb (EphyHistoryService *service, query_collection_done (self, g_steal_pointer (&task)); } +#if SOUP_CHECK_VERSION (2, 99, 4) +static void +google_search_suggestions_cb (SoupSession *session, + GAsyncResult *result, + gpointer user_data) +#else static void google_search_suggestions_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) +#endif { GTask *task = G_TASK (user_data); EphySuggestionModel *self = g_task_get_source_object (task); @@ -651,17 +658,33 @@ google_search_suggestions_cb (SoupSession *session, JsonArray *suggestions; char *engine; int added = 0; + g_autoptr (GBytes) body = NULL; +#if SOUP_CHECK_VERSION (2, 99, 4) + SoupMessage *msg; + + body = soup_session_send_and_read_finish (session, result, NULL); + if (!body) + goto out; + msg = soup_session_get_async_result_message (session, result); + if (soup_message_get_status (msg) != 200) + goto out; + + /* FIXME: we don't have a way to get the status code */ +#else if (msg->status_code != 200) goto out; + body = g_bytes_new_static (msg->response_body->data, msg->response_body->length); +#endif + shell = ephy_embed_shell_get_default (); manager = ephy_embed_shell_get_search_engine_manager (shell); engine = ephy_search_engine_manager_get_default_engine (manager); - node = json_from_string (msg->response_body->data, NULL); + node = json_from_string (g_bytes_get_data (body, NULL), NULL); if (!node || !JSON_NODE_HOLDS_ARRAY (node)) { - g_warning ("Google search suggestion response is not a valid JSON object: %s", msg->response_body->data); + g_warning ("Google search suggestion response is not a valid JSON object: %s", (const char *)g_bytes_get_data (body, NULL)); goto out; } @@ -707,8 +730,14 @@ google_search_suggestions_query (EphySuggestionModel *self, escaped_query = g_markup_escape_text (query, -1); url = g_strdup_printf ("http://suggestqueries.google.com/complete/search?client=firefox&q=%s", escaped_query); msg = soup_message_new (SOUP_METHOD_GET, url); - +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_session_send_and_read_async (self->session, msg, G_PRIORITY_DEFAULT, NULL, + (GAsyncReadyCallback)google_search_suggestions_cb, + g_steal_pointer (&task)); + g_object_unref (msg); +#else soup_session_queue_message (self->session, g_steal_pointer (&msg), google_search_suggestions_cb, g_steal_pointer (&task)); +#endif } void diff --git a/tests/ephy-web-view-test.c b/tests/ephy-web-view-test.c index 22a473011..05b6558aa 100644 --- a/tests/ephy-web-view-test.c +++ b/tests/ephy-web-view-test.c @@ -38,6 +38,14 @@ #define HTML_STRING "testing-ephy-web-view" #define SERVER_PORT 12321 +#if SOUP_CHECK_VERSION (2, 99, 4) +static void +server_callback (SoupServer *server, + SoupServerMessage *msg, + const char *path, + GHashTable *query, + gpointer data) +#else static void server_callback (SoupServer *server, SoupMessage *msg, @@ -45,20 +53,39 @@ server_callback (SoupServer *server, GHashTable *query, SoupClientContext *context, gpointer data) +#endif { - if (!strcmp (path, "/cancelled")) - soup_message_set_status (msg, SOUP_STATUS_CANT_CONNECT); - else if (!strcmp (path, "/redirect")) { + SoupMessageHeaders *response_headers; + SoupMessageBody *response_body; + +#if SOUP_CHECK_VERSION (2, 99, 4) + response_headers = soup_server_message_get_response_headers (msg); + response_body = soup_server_message_get_response_body (msg); +#else + response_headers = msg->response_headers; + response_body = msg->response_body; +#endif + + if (!strcmp (path, "/redirect")) { +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_server_message_set_status (msg, SOUP_STATUS_MOVED_PERMANENTLY, NULL); +#else soup_message_set_status (msg, SOUP_STATUS_MOVED_PERMANENTLY); - soup_message_headers_append (msg->response_headers, "Location", "/redirect-result"); - } else +#endif + soup_message_headers_append (response_headers, "Location", "/redirect-result"); + } else { +#if SOUP_CHECK_VERSION (2, 99, 4) + soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL); +#else soup_message_set_status (msg, SOUP_STATUS_OK); +#endif + } - soup_message_body_append (msg->response_body, SOUP_MEMORY_STATIC, + soup_message_body_append (response_body, SOUP_MEMORY_STATIC, HTML_STRING, strlen (HTML_STRING)); - soup_message_body_complete (msg->response_body); + soup_message_body_complete (response_body); } static void |