[evolution-ews/wip/mcrha/office365: 32/50] Fix batch requests, when the message body is application/json




commit 7862ef314cb54a386ab0a933194ddf4f85cbf526
Author: Milan Crha <mcrha redhat com>
Date:   Wed Jul 8 12:36:47 2020 +0200

    Fix batch requests, when the message body is application/json

 src/Office365/common/e-o365-connection.c | 39 ++++++++++++++++++++++++++++----
 src/Office365/common/e-o365-json-utils.c | 12 +++++-----
 2 files changed, 40 insertions(+), 11 deletions(-)
---
diff --git a/src/Office365/common/e-o365-connection.c b/src/Office365/common/e-o365-connection.c
index 91e81161..4412ed11 100644
--- a/src/Office365/common/e-o365-connection.c
+++ b/src/Office365/common/e-o365-connection.c
@@ -1877,7 +1877,7 @@ e_o365_connection_batch_request_internal_sync (EO365Connection *cnc,
 {
        SoupMessage *message;
        JsonBuilder *builder;
-       gboolean success;
+       gboolean success = TRUE;
        gchar *uri, buff[128];
        guint ii;
 
@@ -1904,12 +1904,13 @@ e_o365_connection_batch_request_internal_sync (EO365Connection *cnc,
        e_o365_json_begin_object_member (builder, NULL);
        e_o365_json_begin_array_member (builder, "requests");
 
-       for (ii = 0; ii < requests->len; ii++) {
+       for (ii = 0; success && ii < requests->len; ii++) {
                SoupMessageHeadersIter iter;
                SoupMessage *submessage;
                SoupURI *suri;
                gboolean has_headers = FALSE;
                const gchar *hdr_name, *hdr_value, *use_uri;
+               gboolean is_application_json = FALSE;
 
                submessage = g_ptr_array_index (requests, ii);
 
@@ -1946,7 +1947,13 @@ e_o365_connection_batch_request_internal_sync (EO365Connection *cnc,
                soup_message_headers_iter_init (&iter, submessage->request_headers);
 
                while (soup_message_headers_iter_next (&iter, &hdr_name, &hdr_value)) {
-                       if (hdr_name && *hdr_name && hdr_value) {
+                       if (hdr_name && *hdr_name && hdr_value &&
+                           !camel_strcase_equal (hdr_name, "Connection") &&
+                           !camel_strcase_equal (hdr_name, "User-Agent")) {
+                               if (camel_strcase_equal (hdr_name, "Content-Type") &&
+                                   camel_strcase_equal (hdr_value, "application/json"))
+                                       is_application_json = TRUE;
+
                                if (!has_headers) {
                                        has_headers = TRUE;
 
@@ -1966,7 +1973,29 @@ e_o365_connection_batch_request_internal_sync (EO365Connection *cnc,
                        sbuffer = soup_message_body_flatten (submessage->request_body);
 
                        if (sbuffer && sbuffer->length > 0) {
-                               e_o365_json_add_string_member (builder, "body", sbuffer->data);
+                               if (is_application_json) {
+                                       /* The server needs it unpacked, not as a plain string */
+                                       JsonParser *parser;
+                                       JsonNode *node;
+
+                                       parser = json_parser_new_immutable ();
+
+                                       success = json_parser_load_from_data (parser, sbuffer->data, 
sbuffer->length, error);
+
+                                       if (!success)
+                                               g_prefix_error (error, "%s", _("Failed to parse own Json 
data"));
+
+                                       node = success ? json_parser_steal_root (parser) : NULL;
+
+                                       if (node) {
+                                               json_builder_set_member_name (builder, "body");
+                                               json_builder_add_value (builder, node);
+                                       }
+
+                                       g_clear_object (&parser);
+                               } else {
+                                       e_o365_json_add_string_member (builder, "body", sbuffer->data);
+                               }
                        }
 
                        if (sbuffer)
@@ -1985,7 +2014,7 @@ e_o365_connection_batch_request_internal_sync (EO365Connection *cnc,
 
        g_object_unref (builder);
 
-       success = o365_connection_send_request_sync (cnc, message, e_o365_read_batch_response_cb, NULL, 
requests, cancellable, error);
+       success = success && o365_connection_send_request_sync (cnc, message, e_o365_read_batch_response_cb, 
NULL, requests, cancellable, error);
 
        g_clear_object (&message);
 
diff --git a/src/Office365/common/e-o365-json-utils.c b/src/Office365/common/e-o365-json-utils.c
index 35e21046..d803a26b 100644
--- a/src/Office365/common/e-o365-json-utils.c
+++ b/src/Office365/common/e-o365-json-utils.c
@@ -32,7 +32,7 @@ e_o365_json_get_array_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return NULL;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node), NULL);
@@ -68,7 +68,7 @@ e_o365_json_get_boolean_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return default_value;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
@@ -99,7 +99,7 @@ e_o365_json_get_double_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return default_value;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
@@ -130,7 +130,7 @@ e_o365_json_get_int_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return default_value;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
@@ -190,7 +190,7 @@ e_o365_json_get_object_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return NULL;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), NULL);
@@ -226,7 +226,7 @@ e_o365_json_get_string_member (JsonObject *object,
 
        node = json_object_get_member (object, member_name);
 
-       if (!node)
+       if (!node || JSON_NODE_HOLDS_NULL (node))
                return default_value;
 
        g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);


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