mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-07 15:47:01 +02:00
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.
This commit is contained in:
parent
7b04cc4467
commit
4d893d440c
@ -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);
|
||||
|
||||
/*****************************************************************/
|
||||
|
@ -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);
|
||||
|
38
src/buffer.c
38
src/buffer.c
@ -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.
|
||||
*/
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user