diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2018-03-09 22:56:22 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2018-03-09 22:56:22 +0100 |
commit | 07b6e87b6366a7b1893a0edf75a493b2e38af6ff (patch) | |
tree | 178e45c1ea3f0d062acb55d3e7c11c3d6f659a07 | |
parent | ed4f74e6d159419637ca71910c4c9f3674f1656b (diff) | |
download | twister-07b6e87b6366a7b1893a0edf75a493b2e38af6ff.tar.gz twister-07b6e87b6366a7b1893a0edf75a493b2e38af6ff.tar.bz2 twister-07b6e87b6366a7b1893a0edf75a493b2e38af6ff.zip |
make mods cumulative
-rwxr-xr-x | src/test/test_twister.sh | 26 | ||||
-rw-r--r-- | src/test/test_twister_webserver.c | 3 | ||||
-rw-r--r-- | src/twister/taler-twister-service.c | 197 |
3 files changed, 99 insertions, 127 deletions
diff --git a/src/test/test_twister.sh b/src/test/test_twister.sh index d2c371e..7609edf 100755 --- a/src/test/test_twister.sh +++ b/src/test/test_twister.sh @@ -19,6 +19,8 @@ status_code=$(curl -s ${TWISTER_URL} -o /dev/null \ # check status code was hacked if ! test 202 = $status_code; then echo "Response code has not been hacked." + kill $web_server_pid + kill $twister_service_pid exit 1 fi @@ -27,9 +29,11 @@ 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 +if ! test '{"hello":[],"when":"today"}' = "$emptied_body"; then printf "Response body (%s) has not been emptied as expected\n" \ "$emptied_body" + kill $web_server_pid + kill $twister_service_pid exit 1 fi @@ -39,9 +43,27 @@ taler-twister -c ./test_twister.conf \ --value "fake" modobject_body=$(curl -s ${TWISTER_URL}) -if ! test '{"hello":"fake"}' = "$modobject_body"; then +if ! test \ + '{"hello":"fake","when":"today"}' = "$modobject_body"; then printf "Response body (%s) has not been modified as expected\n" \ "$modobject_body" + kill $web_server_pid + kill $twister_service_pid + exit 1 +fi + +# check if cumulative mods work. +taler-twister -c ./test_twister.conf \ + --modobject "hello" -V "7" \ + --deleteobject "when" +cum_mods=$(curl -s ${TWISTER_URL}) + +if ! test '{"hello":7}' = $cum_mods; then + printf "Response body (%s) has not been" \ + " cumulative-modified as expected\n" \ + "$cum_mods" + kill $web_server_pid + kill $twister_service_pid exit 1 fi diff --git a/src/test/test_twister_webserver.c b/src/test/test_twister_webserver.c index 0fdc291..7616e1e 100644 --- a/src/test/test_twister_webserver.c +++ b/src/test/test_twister_webserver.c @@ -47,7 +47,8 @@ answer_to_connection const char *page = "<html><body>Hello, browser!</body></html>"; #endif - const char *page = "{\"hello\": [{\"this\": \"browser!\"}]}"; + const char *page = "{\"hello\": [{\"this\": \"browser!\"}],"\ + "\"when\": \"today\"}"; struct MHD_Response *response; int ret; diff --git a/src/twister/taler-twister-service.c b/src/twister/taler-twister-service.c index 087b1fb..b0207c5 100644 --- a/src/twister/taler-twister-service.c +++ b/src/twister/taler-twister-service.c @@ -147,6 +147,11 @@ struct HttpRequest char *url; /** + * Response's JSON. + */ + json_t *json; + + /** * Handle to cURL */ CURL *curl; @@ -438,7 +443,7 @@ static int create_mhd_response_from_hr (struct HttpRequest *hr) { long resp_code; - struct HttpResponseHeader *header; + json_error_t error; if (NULL != hr->response) { @@ -455,24 +460,10 @@ create_mhd_response_from_hr (struct HttpRequest *hr) "Creating MHD response with code %u\n", (unsigned int) resp_code); hr->response_code = resp_code; - hr->response = MHD_create_response_from_buffer - (hr->io_len, - hr->io_buf, - MHD_RESPMEM_MUST_COPY); - - for (header = hr->header_head; - NULL != header; - header = header->next) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Adding MHD response header %s->%s\n", - header->type, - header->value); - GNUNET_break (MHD_YES == - MHD_add_response_header (hr->response, - header->type, - header->value)); - } + hr->json = json_loadb (hr->io_buf, + hr->io_len, + JSON_DECODE_ANY, + &error); #if 0 if (NULL != hr->leho) { @@ -833,31 +824,21 @@ con_val_iter (void *cls, * 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. + * @return GNUNET_OK if @a path was valid. */ -static json_t * +static unsigned int 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 *path_dup; - if (NULL == (parsed_response = json_loadb - (hr->io_buf, hr->io_len, JSON_DECODE_ANY, &error))) - { - TALER_LOG_WARNING ("Could not parse response\n"); - return NULL; - } - GNUNET_asprintf (&path_dup, ".%s", /* Make sure path starts with dot. */ path); @@ -865,7 +846,7 @@ walk_response_object (const char *path, last_token = strrchr (path_dup, '.') + 1; /* Give first nondelim char. */ token = strtok (path_dup, "."); - element = parsed_response; + element = hr->json; while (last_token != token) { @@ -873,7 +854,7 @@ walk_response_object (const char *path, token, token, last_token, last_token); if (NULL == token) - return NULL; // path was ".", refuse to process it. + return GNUNET_SYSERR; // path was ".", refuse to process it. if (NULL != (cur = json_object_get (element, token))) @@ -895,70 +876,62 @@ walk_response_object (const char *path, token); GNUNET_free (path_dup); - return NULL; + return GNUNET_NO; } *target = last_token; *parent = element; - return parsed_response; + return GNUNET_OK; } /** * TODO. */ -static unsigned int +static void modify_object (struct MHD_Connection *con, struct HttpRequest *hr) { char *target; - char *mod_body; unsigned long long n; unsigned int ret_modify; json_t *parent; - json_t *parsed_response; json_t *new_value; json_error_t error; - if (NULL == (parsed_response = walk_response_object - (modify_path, &parent, &target, hr))) - { - return MHD_queue_response (con, - hr->response_code, - hr->response); - } + walk_response_object (modify_path, + &parent, + &target, + hr); + /* At this point, the parent and the target are pointed to. */ - /* The new value has uncertain type; we try until find someting, - * or error. */ - if (NULL != (new_value = json_loadb (modify_value, strlen (modify_value), JSON_DECODE_ANY, &error))) - goto ret_modbody; - - if (NULL != (new_value = json_string (modify_value))) - goto ret_modbody; + goto perform_modbody; if (ULONG_MAX > (n = strtoull (modify_value, NULL, 10))) { if (NULL != (new_value = json_integer ((json_int_t) n))) - goto ret_modbody; + goto perform_modbody; } + if (NULL != (new_value = json_string (modify_value))) + goto perform_modbody; + TALER_LOG_ERROR ("Unvalid new value given: %s\n", modify_value); /* Resetting operation too. */ modify_path[0] = '\0'; - return MHD_queue_response (con, - hr->response_code, - hr->response); - ret_modbody: + return; + + perform_modbody: ret_modify = -1; if (json_is_object (parent)) @@ -973,33 +946,11 @@ modify_object (struct MHD_Connection *con, 10), new_value); if (-1 == ret_modify) - { - TALER_LOG_WARNING ("Could not replace '%s'\n", target); - json_decref (parsed_response); - return MHD_queue_response (con, - hr->response_code, - hr->response); - } + TALER_LOG_WARNING ("Could not replace '%s'\n", + target); - mod_body = json_dumps (parsed_response, JSON_COMPACT); - hr->mod_response = MHD_create_response_from_buffer - (strlen (mod_body), - mod_body, - MHD_RESPMEM_MUST_COPY); - - struct HttpResponseHeader *header; - for (header = hr->header_head; - NULL != header; - header = header->next) - GNUNET_break - (MHD_YES == MHD_add_response_header (hr->mod_response, - header->type, - header->value)); - json_decref (parsed_response); modify_path[0] = '\0'; - return MHD_queue_response (con, - hr->response_code, - hr->mod_response); + return; } /** @@ -1012,23 +963,18 @@ modify_object (struct MHD_Connection *con, * @return MHD_YES / MHD_NO depending on successful / failing * response queueing. */ -static unsigned int +static void 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); - } - + walk_response_object (delete_path, + &parent, + &target, + hr); + /* here, element is the parent of the element to be deleted. */ int ret_deletion = -1; @@ -1043,32 +989,10 @@ delete_object (struct MHD_Connection *con, 10)); } if (-1 == ret_deletion) - { TALER_LOG_WARNING ("Could not delete '%s'\n", target); - json_decref (parsed_response); - return MHD_queue_response (con, - hr->response_code, - hr->response); - } - mod_body = json_dumps (parsed_response, JSON_COMPACT); - hr->mod_response = MHD_create_response_from_buffer - (strlen (mod_body), - mod_body, - MHD_RESPMEM_MUST_COPY); - struct HttpResponseHeader *header; - for (header = hr->header_head; - NULL != header; - header = header->next) - GNUNET_break - (MHD_YES == MHD_add_response_header (hr->mod_response, - header->type, - header->value)); - json_decref (parsed_response); delete_path[0] = '\0'; - return MHD_queue_response (con, - hr->response_code, - hr->mod_response); + return; } @@ -1107,10 +1031,14 @@ create_response (void *cls, void **con_cls) { struct HttpRequest *hr = *con_cls; + struct HttpResponseHeader *header; (void) cls; (void) url; + char *body; + size_t body_len; + if (NULL == hr) { GNUNET_break (0); @@ -1329,12 +1257,6 @@ create_response (void *cls, if (REQUEST_STATE_DOWNLOAD_DONE != hr->state) return MHD_YES; /* wait for curl */ - if (NULL == hr->response) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Still waiting for response to be ready\n"); - return MHD_YES; - } if (0 != hack_response_code) { hr->response_code = hack_response_code; @@ -1345,7 +1267,7 @@ create_response (void *cls, { TALER_LOG_DEBUG ("Will delete path: %s\n", delete_path); - return delete_object (con, hr); + delete_object (con, hr); } if ('\0' != modify_path[0]) @@ -1353,10 +1275,37 @@ create_response (void *cls, TALER_LOG_DEBUG ("Will modify path: %s to value %s\n", modify_path, modify_value); + modify_object (con, hr); + } + + body_len = hr->io_len; + body = hr->io_buf; - return modify_object (con, hr); + if (NULL != hr->json) + { + body = json_dumps (hr->json, JSON_COMPACT); + body_len = strlen (body); + json_decref (hr->json); } + hr->response = MHD_create_response_from_buffer + (body_len, + body, + MHD_RESPMEM_MUST_COPY); + + for (header = hr->header_head; + NULL != header; + header = header->next) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Adding MHD response header %s->%s\n", + header->type, + header->value); + GNUNET_break + (MHD_YES == MHD_add_response_header (hr->response, + header->type, + header->value)); + } return MHD_queue_response (con, hr->response_code, hr->response); |