From b1acb373661a9a799428b50fa402be231013e374 Mon Sep 17 00:00:00 2001 From: Andrew Gough Date: Fri, 9 Sep 2016 11:43:09 +1000 Subject: Add ability to specify headers on the command line for amqp-publish --- tools/doc/amqp-publish.xml | 9 +++++++++ tools/publish.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'tools') diff --git a/tools/doc/amqp-publish.xml b/tools/doc/amqp-publish.xml index e609972..aae07f4 100644 --- a/tools/doc/amqp-publish.xml +++ b/tools/doc/amqp-publish.xml @@ -118,6 +118,15 @@ + + + =header + + + Specifies an optional header in the form "key: value". + + + diff --git a/tools/publish.c b/tools/publish.c index c65bdf6..acb63bc 100644 --- a/tools/publish.c +++ b/tools/publish.c @@ -64,12 +64,14 @@ int main(int argc, const char **argv) static char *routing_key = NULL; static char *content_type = NULL; static char *content_encoding = NULL; + static char **headers = NULL; static char *reply_to = NULL; static char *body = NULL; amqp_basic_properties_t props; amqp_bytes_t body_bytes; static int delivery = 1; /* non-persistent by default */ static int line_buffered = 0; + static char **pos; struct poptOption options[] = { INCLUDE_OPTIONS(connect_options), @@ -102,6 +104,10 @@ int main(int argc, const char **argv) &content_encoding, 0, "the content-encoding for the message", "content encoding" }, + { + "header", 'H', POPT_ARG_ARGV, &headers, 0, + "set a message header (may be specified multiple times)", "\"key: value\"" + }, { "body", 'b', POPT_ARG_STRING, &body, 0, "specify the message body", "body" @@ -137,6 +143,35 @@ int main(int argc, const char **argv) props.reply_to = amqp_cstring_bytes(reply_to); } + if (headers) { + int num = 0; + for (pos = headers; *pos; pos++) { + num++; + } + + if (num > 0) { + amqp_table_t *table = &props.headers; + table->num_entries = num; + table->entries = calloc(num, sizeof(amqp_table_entry_t)); + int i = 0; + for (pos = headers; *pos; pos++) { + char *colon = strchr(*pos, ':'); + if (colon) { + *colon++ = '\0'; + while (*colon == ' ') colon++; + table->entries[i].key = amqp_cstring_bytes(*pos); + table->entries[i].value.kind = AMQP_FIELD_KIND_UTF8; + table->entries[i].value.value.bytes = amqp_cstring_bytes(colon); + i++; + } + else { + fprintf(stderr, "Ignored header definition missing ':' delimiter in \"%s\"\n", *pos); + } + } + props._flags |= AMQP_BASIC_HEADERS_FLAG; + } + } + conn = make_connection(); if (body) { @@ -157,6 +192,10 @@ int main(int argc, const char **argv) do_publish(conn, exchange, routing_key, &props, body_bytes); } + if (props.headers.num_entries > 0) { + free(props.headers.entries); + } + if (!body) { free(body_bytes.bytes); } -- cgit v1.2.1