diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-25 02:51:31 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-25 02:51:31 +0200 |
commit | e45534f433664dc4d68e79b1779d6ff53339e73f (patch) | |
tree | 17939ad990537e3e211e8dc228b5df78c4722145 /src | |
parent | fix m bug (diff) | |
download | mds-e45534f433664dc4d68e79b1779d6ff53339e73f.tar.gz mds-e45534f433664dc4d68e79b1779d6ff53339e73f.tar.bz2 mds-e45534f433664dc4d68e79b1779d6ff53339e73f.tar.xz |
libmds_compose_v: make it easy to exclude headers conditionally
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsclient/proto-util.c | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/src/libmdsclient/proto-util.c b/src/libmdsclient/proto-util.c index b4c580e..a2b24f6 100644 --- a/src/libmdsclient/proto-util.c +++ b/src/libmdsclient/proto-util.c @@ -596,11 +596,14 @@ void libmds_headers_sort(char** restrict headers, size_t header_count) * header line, that is, the header name, colon, blank space and then * the header's value. No LF should be included. The following * arguments should be the argument to format the header line. - * This may be be iterated any number of this. The last argument - * should be `NULL` to specify that there are no more headers. - * The `Length`-header should not be included, it is added automatically. - * A header may not have a length larger than 2¹⁵, otherwise - * the behaviour of this function is undefined. + * If a format for a line begins with a question mark, the remainder + * of the line used, but only if the next argument is non-zero + * (it should be of type `int`,) that argument will not be used + * for the formatting. This may be be iterated any number of this. + * The last argument should be `NULL` to specify that there are no + * more headers. The `Length`-header should not be included, it is + * added automatically. A header may not have a length larger than + * 2¹⁵, otherwise the behaviour of this function is undefined. * @return Zero on success, -1 on error, `errno` will have been set * accordingly on error. * @@ -649,11 +652,14 @@ int libmds_compose(char** restrict buffer, size_t* restrict buffer_size, size_t* * header line, that is, the header name, colon, blank space and then * the header's value. No LF should be included. The following * arguments should be the argument to format the header line. - * This may be be iterated any number of this. The last argument - * should be `NULL` to specify that there are no more headers. - * The `Length`-header should not be included, it is added automatically. - * A header may not have a length larger than 2¹⁵, otherwise - * the behaviour of this function is undefined. + * If a format for a line begins with a question mark, the remainder + * of the line used, but only if the next argument is non-zero + * (it should be of type `int`,) that argument will not be used + * for the formatting. This may be be iterated any number of this. + * The last argument should be `NULL` to specify that there are no + * more headers. The `Length`-header should not be included, it is + * added automatically. A header may not have a length larger than + * 2¹⁵, otherwise the behaviour of this function is undefined. * @return Zero on success, -1 on error, `errno` will have been set * accordingly on error. * @@ -670,6 +676,7 @@ int libmds_compose_v(char** restrict buffer, size_t* restrict buffer_size, size_ char* part_msg; size_t payload_len = 0; const char* format; + int include; int saved_errno; *length = 0; @@ -691,11 +698,26 @@ int libmds_compose_v(char** restrict buffer, size_t* restrict buffer_size, size_ if (format == NULL) break; + include = 1; + if (*format == '?') + { + include = va_arg(args, int); + format++; + } + # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wformat-nonliteral" # pragma GCC diagnostic ignored "-Wsuggest-attribute=format" part_len = vasprintf(&part_msg, format, args); # pragma GCC diagnostic pop + + if (include == 0) + { + if (part_len >= 0) + free(part_msg); + continue; + } + if (part_len < 0) return -1; |