]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] buffers: add buffer_feed2() and make buffer_feed() measure string length
authorWilly Tarreau <w@1wt.eu>
Sat, 10 Oct 2009 16:01:44 +0000 (18:01 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 10 Oct 2009 16:01:44 +0000 (18:01 +0200)
It's inconvenient to always have to compute string lengths when calling
buffer_feed(), so change that.

include/proto/buffers.h
src/buffers.c
src/dumpstats.c

index e061b2c0b6434be53baeeaf06f2381a2c48e9e5e..89f746776f4d5421ee575ebf7f8dff6d2db98d0b 100644 (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;
index ee00f1c8f05d6598649d5757e05b2886103ac671..f5a8c8b6466039f7f5014bc934184dc027d78de3 100644 (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;
 
index 63696d82a18573460b25b5d44fb6b38d5c213e7e..dcbccf7ca2cd73c370ad0729a44cd7a12c194d22 100644 (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;
                        }