summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-07-13 00:20:58 -0400
committerJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-07-13 00:20:58 -0400
commit9efab5f6c36567bbd8645e65cb0d7f5c75e6b329 (patch)
tree2557b82de11b36dde94573edb801fd782d9b606d
parent41d519ef309539aa7e3116195ce87727c87322ff (diff)
downloadtwister-9efab5f6c36567bbd8645e65cb0d7f5c75e6b329.tar.gz
twister-9efab5f6c36567bbd8645e65cb0d7f5c75e6b329.tar.bz2
twister-9efab5f6c36567bbd8645e65cb0d7f5c75e6b329.zip
twister can now modify headers in http responses
-rw-r--r--src/include/taler_twister_service.h19
-rwxr-xr-xsrc/test/test_twister.sh28
-rw-r--r--src/test/test_twister_webserver.c2
-rw-r--r--src/twister/taler-twister-service.c74
-rw-r--r--src/twister/taler-twister.c51
-rw-r--r--src/twister/twister.h2
-rw-r--r--src/twister/twister_api.c54
7 files changed, 212 insertions, 18 deletions
diff --git a/src/include/taler_twister_service.h b/src/include/taler_twister_service.h
index 13b289d..6583fd9 100644
--- a/src/include/taler_twister_service.h
+++ b/src/include/taler_twister_service.h
@@ -115,6 +115,25 @@ TALER_TWISTER_modify_path_ul (struct TALER_TWISTER_Handle *h,
/**
+ * Change the HTTP response header of @a header to @a value.
+ *
+ * @param h twister instance to control
+ * @param header the HTTP response header to modify
+ * @param value value to use for @a header
+ * @param cb callback to call once twister gets this instruction.
+ * @param cb_cls closure for @a cb_callback
+ *
+ * @return operation handle.
+ */
+struct TALER_TWISTER_Operation *
+TALER_TWISTER_modify_header_dl (struct TALER_TWISTER_Handle *h,
+ const char *header,
+ const char *value,
+ GNUNET_SCHEDULER_TaskCallback cb,
+ void *cb_cls);
+
+
+/**
* Instruct the twister to flip a character into
* the string JSON field that belongs to the object
* being returned to the HTTP client.
diff --git a/src/test/test_twister.sh b/src/test/test_twister.sh
index 0347f66..c6713ce 100755
--- a/src/test/test_twister.sh
+++ b/src/test/test_twister.sh
@@ -2,17 +2,17 @@
# This file is part of GNUnet.
# Copyright (C) 2018 Taler Systems SA
-#
+#
# Twister is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 3, or (at your option) any later version.
-#
+#
# Twister is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
-#
+#
# You should have received a copy of the GNU General Public
# License along with Twister; see the file COPYING. If not,
# write to the Free Software Foundation, Inc., 51 Franklin
@@ -20,9 +20,9 @@
# @author Marcello Stanisci
# @author Christian Grothoff
-#
+#
# @file test_twister.sh
-#
+#
# @brief Twister testcases.
@@ -150,6 +150,24 @@ fi
echo "field-modification with string passed".
+# set header
+taler-twister -c ./test_twister.conf \
+ -H "Date" \
+ --value "January 32nd"
+modobject_body=$(curl -sI ${TWISTER_URL} | grep -Fi Date | tr -d '\r')
+echo $modobject_body
+
+if ! test \
+ 'Date: January 32nd' = "$modobject_body"; then
+ printf "Response header (%s) has not been modified as expected\n" \
+ "$modobject_body"
+ kill $web_server_pid
+ kill $twister_service_pid
+ exit 1
+fi
+
+echo "download header-modification passed".
+
# check if cumulative mods work.
taler-twister -c ./test_twister.conf \
-m "hello" -V "world" \
diff --git a/src/test/test_twister_webserver.c b/src/test/test_twister_webserver.c
index bb434c0..bd1c93c 100644
--- a/src/test/test_twister_webserver.c
+++ b/src/test/test_twister_webserver.c
@@ -36,7 +36,7 @@
#define PORT 8080
-static int
+static enum MHD_Result
answer_to_connection
(void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
diff --git a/src/twister/taler-twister-service.c b/src/twister/taler-twister-service.c
index 25dea13..4549a52 100644
--- a/src/twister/taler-twister-service.c
+++ b/src/twister/taler-twister-service.c
@@ -344,6 +344,14 @@ static char *modify_path_dl;
*/
static char *modify_path_ul;
+
+/**
+ * If not NULL, the name of the header value to
+ * modify in the response.
+ */
+static char *modify_header_dl;
+
+
/**
* If true, will randomly truncate the request body
* to upload to the proxied service.
@@ -1907,14 +1915,21 @@ create_response (void *cls,
NULL != header;
header = header->next)
{
+ const char *value = header->value;
+ if ((NULL != modify_header_dl) &&
+ (0 == strcmp (header->type,
+ modify_header_dl)))
+ {
+ value = modify_value;
+ }
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Adding MHD response header %s->%s\n",
header->type,
- header->value);
+ value);
GNUNET_break
(MHD_YES == MHD_add_response_header (hr->response,
header->type,
- header->value));
+ value));
}
run_mhd_now ();
@@ -2713,6 +2728,56 @@ handle_modify_path_ul (void *cls,
/**
+ * Control handler for changing an HTTP response header.
+ *
+ * @param cls message queue for sending replies
+ * @param src received message
+ */
+static int
+check_modify_header_dl (void *cls,
+ const struct TWISTER_ModifyPath *src)
+{
+ return GNUNET_OK;
+}
+
+
+/**
+ * Control handler for changing an HTTP response header.
+ *
+ * @param cls message queue for sending replies
+ * @param src received message
+ */
+static void
+handle_modify_header_dl (void *cls,
+ const struct TWISTER_ModifyPath *src)
+{
+ struct GNUNET_SERVICE_Client *c = cls;
+ uint16_t tailsize;
+ char *payload_path;
+ char *payload_value;
+
+ tailsize = ntohs (src->header.size) - sizeof (*src);
+
+ if (tailsize != GNUNET_STRINGS_buffer_tokenize
+ ((const char *) &src[1],
+ tailsize,
+ 2,
+ &payload_path,
+ &payload_value))
+ {
+ GNUNET_break_op (0);
+ GNUNET_SERVICE_client_drop (c);
+ return;
+ }
+
+ modify_header_dl = GNUNET_strdup (payload_path);
+ modify_value = GNUNET_strdup (payload_value);
+
+ send_acknowledgement (c);
+}
+
+
+/**
* Control handler for flipping JSON strings into response objects
*
* @param cls message queue for sending replies
@@ -2894,6 +2959,11 @@ GNUNET_SERVICE_MAIN
struct TWISTER_ModifyPath,
NULL),
+ GNUNET_MQ_hd_var_size (modify_header_dl,
+ TWISTER_MESSAGE_TYPE_MODIFY_HEADER_DL,
+ struct TWISTER_ModifyPath,
+ NULL),
+
GNUNET_MQ_hd_fixed_size (malform_upload,
TWISTER_MESSAGE_TYPE_MALFORM_UPLOAD,
struct TWISTER_Malform,
diff --git a/src/twister/taler-twister.c b/src/twister/taler-twister.c
index c59cae2..94a9aa8 100644
--- a/src/twister/taler-twister.c
+++ b/src/twister/taler-twister.c
@@ -86,6 +86,11 @@ static char *modify_path_dl;
static char *modify_path_ul;
/**
+ * Name of the header to modify in the HTTP request.
+ */
+static char *modify_header_dl;
+
+/**
* New value for the object pointed by `modify_path_(ul|dl)`.
*/
static char *modify_value;
@@ -151,7 +156,7 @@ run (void *cls,
{
tth = TALER_TWISTER_connect (cfg);
GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
- NULL);
+ NULL);
if (NULL == tth)
{
GNUNET_break (0);
@@ -172,7 +177,6 @@ run (void *cls,
&handle_acknowledgement,
NULL)))
num_ops++;
-
if (0 != check_alive)
{
@@ -250,6 +254,25 @@ run (void *cls,
num_ops++;
}
+ if (NULL != modify_header_dl)
+ {
+ if (NULL == modify_value)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "New value not given, give -V|--value also\n");
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+
+ if (NULL != TALER_TWISTER_modify_header_dl
+ (tth,
+ modify_header_dl,
+ modify_value,
+ &handle_acknowledgement,
+ NULL))
+ num_ops++;
+ }
+
if (0 == num_ops)
{
fprintf (stderr, "No valid hacks specified!\n");
@@ -291,6 +314,15 @@ main (int argc,
&modify_path_dl),
GNUNET_GETOPT_option_string
+ ('H',
+ "modify-header-dl",
+ "HEADER",
+ gettext_noop
+ ("Modify download HTTP header HEADER,"
+ " require --value.\n"),
+ &modify_header_dl),
+
+ GNUNET_GETOPT_option_string
('F',
"flip-ul",
"PATH",
@@ -360,13 +392,14 @@ main (int argc,
status = 1;
if (GNUNET_OK !=
- GNUNET_PROGRAM_run (argc,
- argv,
- "taler-twister",
- gettext_noop
- ("Control taler-twister service."),
- options,
- &run, NULL))
+ GNUNET_PROGRAM_run (
+ argc,
+ argv,
+ "taler-twister",
+ gettext_noop
+ ("Control taler-twister service."),
+ options,
+ &run, NULL))
return 2;
return status;
}
diff --git a/src/twister/twister.h b/src/twister/twister.h
index 3c00e7c..a83b241 100644
--- a/src/twister/twister.h
+++ b/src/twister/twister.h
@@ -47,6 +47,8 @@
#define TWISTER_MESSAGE_TYPE_FLIP_PATH_UL 9
+#define TWISTER_MESSAGE_TYPE_MODIFY_HEADER_DL 10
+
GNUNET_NETWORK_STRUCT_BEGIN
struct TWISTER_Malform
{
diff --git a/src/twister/twister_api.c b/src/twister/twister_api.c
index 7087d62..8bb215b 100644
--- a/src/twister/twister_api.c
+++ b/src/twister/twister_api.c
@@ -380,7 +380,7 @@ TALER_TWISTER_flip_upload
GNUNET_break (0);
return NULL;
}
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Will UL-flip: %s\n",
path);
@@ -555,6 +555,58 @@ TALER_TWISTER_modify_path_ul (struct TALER_TWISTER_Handle *h,
return op;
}
+
+/**
+ * Change the HTTP response header of @a header to @a value.
+ *
+ * @param h twister instance to control
+ * @param header the HTTP response header to modify
+ * @param value value to use for @a header
+ * @param cb callback to call once twister gets this instruction.
+ * @param cb_cls closure for @a cb_callback
+ *
+ * @return operation handle.
+ */
+struct TALER_TWISTER_Operation *
+TALER_TWISTER_modify_header_dl (struct TALER_TWISTER_Handle *h,
+ const char *header,
+ const char *value,
+ GNUNET_SCHEDULER_TaskCallback cb,
+ void *cb_cls)
+{
+ struct TALER_TWISTER_Operation *op;
+ struct GNUNET_MQ_Envelope *env;
+ struct TWISTER_ModifyPath *src;
+ uint16_t stralloc;
+
+ stralloc = strlen (header) + strlen (value) + 2;
+ if (sizeof (*src) + stralloc > UINT16_MAX)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ op = GNUNET_new (struct TALER_TWISTER_Operation);
+ op->h = h;
+ op->cb = cb;
+ op->cb_cls = cb_cls;
+ GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
+ h->op_tail,
+ op);
+ env = GNUNET_MQ_msg_extra (src,
+ stralloc,
+ TWISTER_MESSAGE_TYPE_MODIFY_HEADER_DL);
+
+ GNUNET_assert
+ (stralloc == GNUNET_STRINGS_buffer_fill ((char *) &src[1],
+ stralloc,
+ 2,
+ header,
+ value));
+ GNUNET_MQ_send (h->mq, env);
+ return op;
+}
+
+
/**
* Change the next response code to @a new_rc.
*