[epiphany/wip/sync] sync-utils: Add 'escape' flag to _build_json_string()



commit 24c631364f9653156104e3650dc7e524968f3af4
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Thu Mar 16 00:55:50 2017 +0200

    sync-utils: Add 'escape' flag to _build_json_string()
    
    This will be used when creating the string representation of a BSO

 src/sync/ephy-sync-secret.c  |    3 ++-
 src/sync/ephy-sync-service.c |    2 +-
 src/sync/ephy-sync-utils.c   |   20 ++++++++++++++++----
 src/sync/ephy-sync-utils.h   |    3 ++-
 4 files changed, 21 insertions(+), 7 deletions(-)
---
diff --git a/src/sync/ephy-sync-secret.c b/src/sync/ephy-sync-secret.c
index f9abdec..8fa0b15 100644
--- a/src/sync/ephy-sync-secret.c
+++ b/src/sync/ephy-sync-secret.c
@@ -218,7 +218,8 @@ ephy_sync_secret_store_tokens (EphySyncService *service)
   char *tokens;
   char *label;
 
-  tokens = ephy_sync_utils_build_json_string (ephy_sync_utils_token_name_from_type (TOKEN_UID),
+  tokens = ephy_sync_utils_build_json_string (FALSE,
+                                              ephy_sync_utils_token_name_from_type (TOKEN_UID),
                                               ephy_sync_service_get_token (service, TOKEN_UID),
                                               ephy_sync_utils_token_name_from_type (TOKEN_SESSIONTOKEN),
                                               ephy_sync_service_get_token (service, TOKEN_SESSIONTOKEN),
diff --git a/src/sync/ephy-sync-service.c b/src/sync/ephy-sync-service.c
index a1064d1..b1cfb6d 100644
--- a/src/sync/ephy-sync-service.c
+++ b/src/sync/ephy-sync-service.c
@@ -488,7 +488,7 @@ ephy_sync_service_obtain_signed_certificate (EphySyncService *self)
 
   n = mpz_get_str (NULL, 10, self->keypair->public.n);
   e = mpz_get_str (NULL, 10, self->keypair->public.e);
-  public_key_json = ephy_sync_utils_build_json_string ("algorithm", "RS", "n", n, "e", e, NULL);
+  public_key_json = ephy_sync_utils_build_json_string (FALSE, "algorithm", "RS", "n", n, "e", e, NULL);
   request_body = g_strdup_printf ("{\"publicKey\": %s, \"duration\": %d}",
                                   public_key_json, CERTIFICATE_DURATION);
   ephy_sync_service_fxa_hawk_post_async (self, "certificate/sign", tokenID_hex,
diff --git a/src/sync/ephy-sync-utils.c b/src/sync/ephy-sync-utils.c
index 73b17d4..fbd714c 100644
--- a/src/sync/ephy-sync-utils.c
+++ b/src/sync/ephy-sync-utils.c
@@ -25,7 +25,8 @@
 #include <string.h>
 
 char *
-ephy_sync_utils_build_json_string (const char *key,
+ephy_sync_utils_build_json_string (gboolean    escape,
+                                   const char *key,
                                    const char *value,
                                    ...)
 {
@@ -35,13 +36,24 @@ ephy_sync_utils_build_json_string (const char *key,
   char *next_value;
   char *tmp;
 
-  json = g_strconcat ("{\"", key, "\": \"", value, "\"", NULL);
+  json = g_strconcat (escape ? "{\\\"" : "{\"",
+                      key,
+                      escape ? "\\\": \\\"" : "\": \"",
+                      value,
+                      escape ? "\\\"" : "\"",
+                      NULL);
   va_start (args, value);
 
   while ((next_key = va_arg (args, char *)) != NULL) {
     next_value = va_arg (args, char *);
     tmp = json;
-    json = g_strconcat (json, ", \"", next_key, "\": \"", next_value, "\"", NULL);
+    json = g_strconcat (json,
+                        escape ? ", \\\"" : ", \"",
+                        next_key,
+                        escape ? "\\\": \\\"" : "\": \"",
+                        next_value,
+                        escape ? "\\\"" : "\"",
+                        NULL);
     g_free (tmp);
   }
 
@@ -57,7 +69,7 @@ char *
 ephy_sync_utils_create_bso_json (const char *id,
                                  const char *payload)
 {
-  return ephy_sync_utils_build_json_string ("id", id, "payload", payload, NULL);
+  return ephy_sync_utils_build_json_string (FALSE, "id", id, "payload", payload, NULL);
 }
 
 char *
diff --git a/src/sync/ephy-sync-utils.h b/src/sync/ephy-sync-utils.h
index 10340a8..cd37050 100644
--- a/src/sync/ephy-sync-utils.h
+++ b/src/sync/ephy-sync-utils.h
@@ -30,7 +30,8 @@ typedef enum {
 
 G_BEGIN_DECLS
 
-char               *ephy_sync_utils_build_json_string     (const char        *key,
+char               *ephy_sync_utils_build_json_string     (gboolean           escape,
+                                                           const char        *key,
                                                            const char        *value,
                                                            ...) G_GNUC_NULL_TERMINATED;
 char               *ephy_sync_utils_create_bso_json       (const char        *id,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]