summaryrefslogtreecommitdiffstats
path: root/src/postqueue
diff options
context:
space:
mode:
Diffstat (limited to 'src/postqueue')
-rw-r--r--src/postqueue/showq_json.c79
1 files changed, 11 insertions, 68 deletions
diff --git a/src/postqueue/showq_json.c b/src/postqueue/showq_json.c
index db79404..a2820dd 100644
--- a/src/postqueue/showq_json.c
+++ b/src/postqueue/showq_json.c
@@ -58,69 +58,6 @@
#define STR(x) vstring_str(x)
#define LEN(x) VSTRING_LEN(x)
-/* json_quote - quote JSON string */
-
-static char *json_quote(VSTRING *result, const char *text)
-{
- unsigned char *cp;
- int ch;
-
- /*
- * We use short escape sequences for common control characters. Note that
- * RFC 4627 allows "/" (0x2F) to be sent without quoting. Differences
- * with RFC 4627: we send DEL (0x7f) as \u007F; the result remains RFC
- * 4627 complaint.
- */
- VSTRING_RESET(result);
- for (cp = (unsigned char *) text; (ch = *cp) != 0; cp++) {
- if (UNEXPECTED(ISCNTRL(ch))) {
- switch (ch) {
- case '\b':
- VSTRING_ADDCH(result, '\\');
- VSTRING_ADDCH(result, 'b');
- break;
- case '\f':
- VSTRING_ADDCH(result, '\\');
- VSTRING_ADDCH(result, 'f');
- break;
- case '\n':
- VSTRING_ADDCH(result, '\\');
- VSTRING_ADDCH(result, 'n');
- break;
- case '\r':
- VSTRING_ADDCH(result, '\\');
- VSTRING_ADDCH(result, 'r');
- break;
- case '\t':
- VSTRING_ADDCH(result, '\\');
- VSTRING_ADDCH(result, 't');
- break;
- default:
- vstring_sprintf_append(result, "\\u%04X", ch);
- break;
- }
- } else {
- switch (ch) {
- case '\\':
- case '"':
- VSTRING_ADDCH(result, '\\');
- /* FALLTHROUGH */
- default:
- VSTRING_ADDCH(result, ch);
- break;
- }
- }
- }
- VSTRING_TERMINATE(result);
-
- /*
- * Force the result to be UTF-8 (with SMTPUTF8 enabled) or ASCII (with
- * SMTPUTF8 disabled).
- */
- printable(STR(result), '?');
- return (STR(result));
-}
-
/* json_message - report status for one message */
static void format_json(VSTREAM *showq_stream)
@@ -148,6 +85,12 @@ static void format_json(VSTREAM *showq_stream)
}
/*
+ * Force JSON values to UTF-8 (with SMTPUTF8 enabled) or ASCII (with
+ * SMTPUTF8 disabled).
+ */
+#define QUOTE_JSON(res, src) printable(quote_for_json((res), (src), -1), '?')
+
+ /*
* Read the message properties and sender address.
*/
if (attr_scan(showq_stream, ATTR_FLAG_MORE | ATTR_FLAG_STRICT
@@ -162,14 +105,14 @@ static void format_json(VSTREAM *showq_stream)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
vstream_printf("{");
vstream_printf("\"queue_name\": \"%s\", ",
- json_quote(quote_buf, STR(queue_name)));
+ QUOTE_JSON(quote_buf, STR(queue_name)));
vstream_printf("\"queue_id\": \"%s\", ",
- json_quote(quote_buf, STR(queue_id)));
+ QUOTE_JSON(quote_buf, STR(queue_id)));
vstream_printf("\"arrival_time\": %ld, ", arrival_time);
vstream_printf("\"message_size\": %ld, ", message_size);
vstream_printf("\"forced_expire\": %s, ", forced_expire ? "true" : "false");
vstream_printf("\"sender\": \"%s\", ",
- json_quote(quote_buf, STR(addr)));
+ QUOTE_JSON(quote_buf, STR(addr)));
/*
* Read zero or more (recipient, reason) pair(s) until attr_scan_more()
@@ -188,10 +131,10 @@ static void format_json(VSTREAM *showq_stream)
ATTR_TYPE_END) != 2)
msg_fatal_status(EX_SOFTWARE, "malformed showq server response");
vstream_printf("\"address\": \"%s\"",
- json_quote(quote_buf, STR(addr)));
+ QUOTE_JSON(quote_buf, STR(addr)));
if (LEN(why) > 0)
vstream_printf(", \"delay_reason\": \"%s\"",
- json_quote(quote_buf, STR(why)));
+ QUOTE_JSON(quote_buf, STR(why)));
vstream_printf("}");
}
vstream_printf("]");