MINOR: buffers/channel: replace buffer_insert_line2() with ci_insert_line2()
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.
diff --git a/include/common/buffer.h b/include/common/buffer.h
index c9744b3..3aaca53 100644
--- a/include/common/buffer.h
+++ b/include/common/buffer.h
@@ -51,7 +51,6 @@
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);
/*****************************************************************/
diff --git a/include/proto/channel.h b/include/proto/channel.h
index b2efae8..26ac619 100644
--- a/include/proto/channel.h
+++ b/include/proto/channel.h
@@ -49,6 +49,7 @@
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);
diff --git a/src/buffer.c b/src/buffer.c
index 306f011..9e77947 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -108,44 +108,6 @@
}
/*
- * 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.
*/
void buffer_dump(FILE *o, struct buffer *b, int from, int to)
diff --git a/src/channel.c b/src/channel.c
index fed0cb2..bb5e144 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -402,6 +402,41 @@
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
diff --git a/src/proto_http.c b/src/proto_http.c
index 85c2391..acecd4e 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -537,27 +537,19 @@
/*
* 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 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);