From 9bcc91e80e4acfd7cf57ed4532f6d94f4f0d1aa3 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 10 Oct 2009 18:01:44 +0200 Subject: [PATCH] [MINOR] buffers: add buffer_feed2() and make buffer_feed() measure string length It's inconvenient to always have to compute string lengths when calling buffer_feed(), so change that. --- include/proto/buffers.h | 17 ++++++++++++++--- src/buffers.c | 4 ++-- src/dumpstats.c | 20 +++++--------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/include/proto/buffers.h b/include/proto/buffers.h index e061b2c0b6..89f746776f 100644 --- a/include/proto/buffers.h +++ b/include/proto/buffers.h @@ -358,7 +358,7 @@ static inline int buffer_si_peekchar(struct buffer *buf) } /* Try to write character into buffer after length controls. This - * work like buffer_feed(buf, &c, 1). + * work like buffer_feed2(buf, &c, 1). * Returns non-zero in case of success, 0 if the buffer was full. * The send limit is automatically adjusted with the amount of data written. */ @@ -389,7 +389,7 @@ static inline int buffer_si_putchar(struct buffer *buf, char c) } int buffer_write(struct buffer *buf, const char *msg, int len); -int buffer_feed(struct buffer *buf, const char *str, int len); +int buffer_feed2(struct buffer *buf, const char *str, int len); int buffer_si_putchar(struct buffer *buf, char c); int buffer_si_peekline(struct buffer *buf, char *str, int len); int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); @@ -426,12 +426,23 @@ static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *chunk) { int ret; - ret = buffer_feed(buf, chunk->str, chunk->len); + ret = buffer_feed2(buf, chunk->str, chunk->len); if (ret == -1) chunk->len = 0; return ret; } +/* Try to write string into buffer after length controls. This is + * the equivalent of buffer_feed2() except that string length is measured by + * the function. Returns -1 in case of success, -2 if it is larger than the + * buffer size, or the number of bytes available otherwise. The send limit is + * automatically adjusted with the amount of data written. + */ +static inline int buffer_feed(struct buffer *buf, const char *str) +{ + return buffer_feed2(buf, str, strlen(str)); +} + static inline void chunk_init(struct chunk *chk, char *str, size_t size) { chk->str = str; chk->len = 0; diff --git a/src/buffers.c b/src/buffers.c index ee00f1c8f0..f5a8c8b646 100644 --- a/src/buffers.c +++ b/src/buffers.c @@ -1,7 +1,7 @@ /* * Buffer management functions. * - * Copyright 2000-2007 Willy Tarreau + * Copyright 2000-2009 Willy Tarreau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -78,7 +78,7 @@ int buffer_write(struct buffer *buf, const char *msg, int len) * otherwise. The send limit is automatically adjusted with the amount of data * written. */ -int buffer_feed(struct buffer *buf, const char *str, int len) +int buffer_feed2(struct buffer *buf, const char *str, int len) { int max; diff --git a/src/dumpstats.c b/src/dumpstats.c index 63696d82a1..dcbccf7ca2 100644 --- a/src/dumpstats.c +++ b/src/dumpstats.c @@ -64,20 +64,10 @@ const char stats_sock_usage_msg[] = " show sess : report the list of current sessions\n" ""; -const struct chunk stats_sock_usage = { - .str = (char *)&stats_sock_usage_msg, - .len = sizeof(stats_sock_usage_msg)-1 -}; - const char stats_permission_denied_msg[] = "Permission denied\n" ""; -const struct chunk stats_permission_denied = { - .str = (char *)&stats_permission_denied_msg, - .len = sizeof(stats_permission_denied_msg)-1 -}; - /* This function parses a "stats" statement in the "global" section. It returns * -1 if there is any error, otherwise zero. If it returns -1, it may write an * error message into ther buffer, for at most bytes, trailing @@ -313,14 +303,14 @@ int stats_sock_parse_request(struct stream_interface *si, char *line) else if (strcmp(args[1], "sess") == 0) { s->data_state = DATA_ST_INIT; if (s->listener->perm.ux.level < ACCESS_LVL_OPER) { - buffer_feed(si->ib, stats_permission_denied.str, stats_permission_denied.len); + buffer_feed(si->ib, stats_permission_denied_msg); return 1; } si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer } else if (strcmp(args[1], "errors") == 0) { if (s->listener->perm.ux.level < ACCESS_LVL_OPER) { - buffer_feed(si->ib, stats_permission_denied.str, stats_permission_denied.len); + buffer_feed(si->ib, stats_permission_denied_msg); return 1; } if (*args[2]) @@ -348,7 +338,7 @@ int stats_sock_parse_request(struct stream_interface *si, char *line) /* check permissions */ if (s->listener->perm.ux.level < ACCESS_LVL_OPER || (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) { - buffer_feed(si->ib, stats_permission_denied.str, stats_permission_denied.len); + buffer_feed(si->ib, stats_permission_denied_msg); return 1; } @@ -495,7 +485,7 @@ void stats_io_handler(struct stream_interface *si) switch (si->st0) { case STAT_CLI_O_HELP: - if (buffer_feed(si->ib, stats_sock_usage.str, stats_sock_usage.len) < 0) + if (buffer_feed(si->ib, stats_sock_usage_msg) < 0) si->st0 = STAT_CLI_PROMPT; break; case STAT_CLI_O_INFO: @@ -517,7 +507,7 @@ void stats_io_handler(struct stream_interface *si) /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */ if (si->st0 == STAT_CLI_PROMPT) { - if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n", si->st1 ? 3 : 1) < 0) + if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0) si->st0 = STAT_CLI_GETREQ; }