[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.
This commit is contained in:
Willy Tarreau 2009-10-10 18:01:44 +02:00
parent 6162db2a81
commit 9bcc91e80e
3 changed files with 21 additions and 20 deletions

View File

@ -358,7 +358,7 @@ static inline int buffer_si_peekchar(struct buffer *buf)
}
/* Try to write character <c> into buffer <buf> 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 <str> into buffer <buf> 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;

View File

@ -1,7 +1,7 @@
/*
* Buffer management functions.
*
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
* Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
*
* 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;

View File

@ -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 <err> buffer, for at most <errlen> 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;
}