summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2018-03-09 16:09:16 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2018-03-09 16:09:16 +0100
commitc5156b4054f0f376fb7be15b57a95b4db015c2a1 (patch)
tree53925931b3b1958be10ce327cd58dae4b80748fe
parentef7127b4b5362e0f458edad2986f4bdac7c100a6 (diff)
downloadtwister-c5156b4054f0f376fb7be15b57a95b4db015c2a1.tar.gz
twister-c5156b4054f0f376fb7be15b57a95b4db015c2a1.tar.bz2
twister-c5156b4054f0f376fb7be15b57a95b4db015c2a1.zip
object walker as separate function.
-rwxr-xr-xsrc/test/test_twister.sh30
-rw-r--r--src/twister/taler-twister-service.c96
2 files changed, 87 insertions, 39 deletions
diff --git a/src/test/test_twister.sh b/src/test/test_twister.sh
index 85fa5d5..e8eec9a 100755
--- a/src/test/test_twister.sh
+++ b/src/test/test_twister.sh
@@ -16,22 +16,16 @@ taler-twister -c ./test_twister.conf --responsecode 202
status_code=$(curl -s ${TWISTER_URL} -o /dev/null \
-w "%{http_code}")
-# delete object.
-taler-twister -c ./test_twister.conf -d "hello.0"
-
-# get emptied response.
-emptied_body=$(curl -s ${TWISTER_URL})
-
-# shutdown twister and webserver
-kill $web_server_pid
-kill $twister_service_pid
-
# check status code was hacked
if ! test 202 = $status_code; then
echo "Response code has not been hacked."
exit 1
fi
+# delete object.
+taler-twister -c ./test_twister.conf -d "hello.0"
+emptied_body=$(curl -s ${TWISTER_URL})
+
# check response body has been emptied
if ! test '{"hello":[]}' = "$emptied_body"; then
printf "Response body (%s) has not been emptied as expected\n" \
@@ -39,4 +33,20 @@ if ! test '{"hello":[]}' = "$emptied_body"; then
exit 1
fi
+# set field
+#taler-twister -c ./test_twister.conf \
+# --setfield "hello" \
+# --value "fake"
+#modfield_body=$(curl -s ${TWISTER_URL})
+
+# shutdown twister and webserver
+kill $web_server_pid
+kill $twister_service_pid
+
+#if ! test '{"hello":"fake"}' = "$modfield_body"; then
+# printf "Response body (%s) has not been modified as expected\n" \
+# "$modfield_body"
+# exit 1
+#fi
+
exit 0
diff --git a/src/twister/taler-twister-service.c b/src/twister/taler-twister-service.c
index 786768d..dd6a1e1 100644
--- a/src/twister/taler-twister-service.c
+++ b/src/twister/taler-twister-service.c
@@ -807,28 +807,37 @@ con_val_iter (void *cls,
return MHD_YES;
}
-
/**
- * Delete object within the proxied response.
- * Always queues a response; only deletes the object if it is
- * found within the response, otherwise return it verbatim (but
- * will look for it into the next response). Will flush the
- * operation once the wanted object has been found.
+ * Walk a JSON object (tipically a response we got
+ * from a proxied service), and return the last token
+ * from the path, plus the parent object of the object
+ * targeted by path.
*
- * @return MHD_YES / MHD_NO depending on successful / failing
- * response queueing.
+ * @param path the path pointing to a object in the response
+ * @param[out] parent will point to the parent of the targeted
+ * object
+ * @param[out] response parsed original response, to be decref'd
+ * by the caller.
+ * @param[out] target last token of the path. E.g. given a x.y.z,
+ * will point to 'z'.
+ * @param hr contains original response.
+ *
+ * @return original response, or NULL upon errors; the response
+ * must be free'd by the caller.
*/
-static unsigned int
-delete_object (struct MHD_Connection *con,
- struct HttpRequest *hr)
+static json_t *
+walk_response_object (const char *path,
+ json_t **parent,
+ char **target,
+ struct HttpRequest *hr)
{
+
json_t *parsed_response;
json_t *element;
json_t *cur;
json_error_t error;
char *token;
char *last_token;
- char *mod_body;
char *path_dup;
unsigned int index;
@@ -836,9 +845,7 @@ delete_object (struct MHD_Connection *con,
(hr->io_buf, hr->io_len, JSON_DECODE_ANY, &error)))
{
TALER_LOG_WARNING ("Could not parse response\n");
- return MHD_queue_response (con,
- hr->response_code,
- hr->response);
+ return NULL;
}
path_dup = GNUNET_strdup (delete_path);
@@ -859,9 +866,7 @@ delete_object (struct MHD_Connection *con,
json_decref (parsed_response);
GNUNET_free (path_dup);
- return MHD_queue_response (con,
- hr->response_code,
- hr->response);
+ return NULL;
}
if (NULL != (cur = json_object_get (element,
token)))
@@ -881,29 +886,63 @@ delete_object (struct MHD_Connection *con,
TALER_LOG_WARNING ("Path token '%s' not found\n",
token);
GNUNET_free (path_dup);
+
+ return NULL;
+ }
+ while (last_token != (token = strtok (NULL, ".")));
+
+ *target = last_token;
+ *parent = element;
+
+ return parsed_response;
+}
+
+
+
+/**
+ * Delete object within the proxied response.
+ * Always queues a response; only deletes the object if it is
+ * found within the response, otherwise return it verbatim (but
+ * will look for it into the next response). Will flush the
+ * operation once the wanted object has been found.
+ *
+ * @return MHD_YES / MHD_NO depending on successful / failing
+ * response queueing.
+ */
+static unsigned int
+delete_object (struct MHD_Connection *con,
+ struct HttpRequest *hr)
+{
+ char *mod_body;
+ char *target;
+ json_t *parent;
+ json_t *parsed_response;
+
+ if (NULL == (parsed_response = walk_response_object
+ (delete_path, &parent, &target, hr)))
+ {
return MHD_queue_response (con,
hr->response_code,
hr->response);
}
- while (last_token != (token = strtok (NULL, ".")));
/* here, element is the parent of the element to be deleted. */
int ret_deletion = -1;
- if (json_is_object (element))
- ret_deletion = json_object_del (element, last_token);
- if (json_is_array (element))
+ if (json_is_object (parent))
+ ret_deletion = json_object_del (parent, target);
+
+ if (json_is_array (parent))
{
- index = (unsigned int) strtoul (token,
- NULL,
- 10);
- ret_deletion = json_array_remove (element, index);
+ ret_deletion = json_array_remove
+ (parent, (unsigned int) strtoul (target,
+ NULL,
+ 10));
}
if (-1 == ret_deletion)
{
- TALER_LOG_WARNING ("Could not delete '%s'\n", last_token);
+ TALER_LOG_WARNING ("Could not delete '%s'\n", target);
json_decref (parsed_response);
- GNUNET_free (path_dup);
return MHD_queue_response (con,
hr->response_code,
hr->response);
@@ -923,7 +962,6 @@ delete_object (struct MHD_Connection *con,
(MHD_YES == MHD_add_response_header (hr->mod_response,
header->type,
header->value));
- GNUNET_free (path_dup);
return MHD_queue_response (con,
hr->response_code,
hr->mod_response);