]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: buffers/channel: replace buffer_insert_line2() with ci_insert_line2()
authorWilly Tarreau <w@1wt.eu>
Thu, 12 Jul 2018 13:43:32 +0000 (15:43 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Jul 2018 14:23:43 +0000 (16:23 +0200)
There was no point keeping that function in the buffer part since it's
exclusively used by HTTP at the channel level, since it also automatically
appends the CRLF. This further cleans up the buffer code.

include/common/buffer.h
include/proto/channel.h
src/buffer.c
src/channel.c
src/proto_http.c

index c9744b308ea7b5995ecbdba1242072ec3521cc3c..3aaca53d3034d396e3dc9643ee2b464eea9b1f39 100644 (file)
@@ -51,7 +51,6 @@ __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock);
 int init_buffer();
 void deinit_buffer();
 int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
-int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
 void buffer_dump(FILE *o, struct buffer *b, int from, int to);
 
 /*****************************************************************/
index b2efae843b50d1f2a7fd8cb065b70667ce9ccf26..26ac619511511b6c7bd28408e7f034e372b7ec43 100644 (file)
@@ -49,6 +49,7 @@ int ci_putblk(struct channel *chn, const char *str, int len);
 int ci_putchr(struct channel *chn, char c);
 int ci_getline_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
 int ci_getblk_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
+int ci_insert_line2(struct channel *c, int pos, const char *str, int len);
 int co_inject(struct channel *chn, const char *msg, int len);
 int co_getline(const struct channel *chn, char *str, int len);
 int co_getblk(const struct channel *chn, char *blk, int len, int offset);
index 306f011d1b221afa5564a6f99b90bb47d46f759f..9e779470d50242c5d8062fb42a85a3b92168de47 100644 (file)
@@ -107,44 +107,6 @@ int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int
        return delta;
 }
 
-/*
- * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
- * argument informs about the length of string <str> so that we don't have to
- * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
- * is only opened for len+2 bytes but nothing is copied in. It may be useful in
- * some circumstances. The send limit is *not* adjusted. Same comments as above
- * for the valid use cases.
- *
- * The number of bytes added is returned on success. 0 is returned on failure.
- */
-int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
-{
-       int delta;
-
-       delta = len + 2;
-
-       if (b_tail(b) + delta >= b_wrap(b))
-               return 0;  /* no space left */
-
-       if (b_data(b) &&
-           b_tail(b) + delta > b_head(b) &&
-           b_head(b) >= b_tail(b))
-               return 0;  /* no space left before wrapping data */
-
-       /* first, protect the end of the buffer */
-       memmove(pos + delta, pos, b_tail(b) - pos);
-
-       /* now, copy str over pos */
-       if (len && str) {
-               memcpy(pos, str, len);
-               pos[len] = '\r';
-               pos[len + 1] = '\n';
-       }
-
-       b_add(b, delta);
-       return delta;
-}
-
 /*
  * Dumps part or all of a buffer.
  */
index fed0cb2135561f444cbc2510ede8dc319bef93a6..bb5e144ad35da08677aa2701dfeea4b188c3f8f3 100644 (file)
@@ -402,6 +402,41 @@ int ci_getline_nc(const struct channel *chn,
        return 0;
 }
 
+/* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
+ * input head. The <len> argument informs about the length of string <str> so
+ * that we don't have to measure it. <str> must be a valid pointer and must not
+ * include the trailing "\r\n".
+ *
+ * The number of bytes added is returned on success. 0 is returned on failure.
+ */
+int ci_insert_line2(struct channel *c, int pos, const char *str, int len)
+{
+       struct buffer *b = c->buf;
+       char *dst = c_ptr(c, pos);
+       int delta;
+
+       delta = len + 2;
+
+       if (b_tail(b) + delta >= b_wrap(b))
+               return 0;  /* no space left */
+
+       if (b_data(b) &&
+           b_tail(b) + delta > b_head(b) &&
+           b_head(b) >= b_tail(b))
+               return 0;  /* no space left before wrapping data */
+
+       /* first, protect the end of the buffer */
+       memmove(dst + delta, dst, b_tail(b) - dst);
+
+       /* now, copy str over dst */
+       memcpy(dst, str, len);
+       dst[len] = '\r';
+       dst[len + 1] = '\n';
+
+       b_add(b, delta);
+       return delta;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8
index 85c2391feabe1b4845262d7bdad51fc12dce5807..acecd4e346cdf493c7c0bc51e3c5ba1c54efcdf8 100644 (file)
@@ -537,27 +537,19 @@ const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
 
 /*
  * Adds a header and its CRLF at the tail of the message's buffer, just before
- * the last CRLF. Text length is measured first, so it cannot be NULL.
+ * the last CRLF.
  * The header is also automatically added to the index <hdr_idx>, and the end
  * of headers is automatically adjusted. The number of bytes added is returned
  * on success, otherwise <0 is returned indicating an error.
  */
-int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
+static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
 {
-       int bytes, len;
-
-       len = strlen(text);
-       bytes = buffer_insert_line2(msg->chn->buf, ci_head(msg->chn) + msg->eoh, text, len);
-       if (!bytes)
-               return -1;
-       http_msg_move_end(msg, bytes);
-       return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
+       return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
 }
 
 /*
  * Adds a header and its CRLF at the tail of the message's buffer, just before
- * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
- * the buffer is only opened and the space reserved, but nothing is copied.
+ * the last CRLF. <len> bytes are copied, not counting the CRLF.
  * The header is also automatically added to the index <hdr_idx>, and the end
  * of headers is automatically adjusted. The number of bytes added is returned
  * on success, otherwise <0 is returned indicating an error.
@@ -567,7 +559,7 @@ int http_header_add_tail2(struct http_msg *msg,
 {
        int bytes;
 
-       bytes = buffer_insert_line2(msg->chn->buf, ci_head(msg->chn) + msg->eoh, text, len);
+       bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
        if (!bytes)
                return -1;
        http_msg_move_end(msg, bytes);