From: Willy Tarreau Date: Thu, 12 Jul 2018 13:43:32 +0000 (+0200) Subject: MINOR: buffers/channel: replace buffer_insert_line2() with ci_insert_line2() X-Git-Tag: v1.9-dev1~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d893d440c49018ef8ef94d42f5a32be74ae3963;p=thirdparty%2Fhaproxy.git 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 c9744b308e..3aaca53d30 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -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); /*****************************************************************/ diff --git a/include/proto/channel.h b/include/proto/channel.h index b2efae843b..26ac619511 100644 --- a/include/proto/channel.h +++ b/include/proto/channel.h @@ -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); diff --git a/src/buffer.c b/src/buffer.c index 306f011d1b..9e779470d5 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -107,44 +107,6 @@ int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int return delta; } -/* - * Inserts followed by "\r\n" at position in buffer . The - * argument informs about the length of string so that we don't have to - * measure it. It does not include the "\r\n". If 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. */ diff --git a/src/channel.c b/src/channel.c index fed0cb2135..bb5e144ad3 100644 --- a/src/channel.c +++ b/src/channel.c @@ -402,6 +402,41 @@ int ci_getline_nc(const struct channel *chn, return 0; } +/* Inserts followed by "\r\n" at position relative to channel 's + * input head. The argument informs about the length of string so + * that we don't have to measure it. 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 85c2391fea..acecd4e346 100644 --- a/src/proto_http.c +++ b/src/proto_http.c @@ -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 , 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. bytes are copied, not counting the CRLF. If is NULL, then - * the buffer is only opened and the space reserved, but nothing is copied. + * the last CRLF. bytes are copied, not counting the CRLF. * The header is also automatically added to the index , 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);