diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index e22b2c073..deeacd0a9 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -57,6 +57,10 @@ size_t htx_add_data(struct htx *htx, const struct ist data); struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data); void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref); int htx_append_msg(struct htx *dst, const struct htx *src); +struct buffer *htx_move_to_small_buffer(struct buffer *dst, struct buffer *src); +struct buffer *htx_move_to_large_buffer(struct buffer *dst, struct buffer *src); +struct buffer *htx_copy_to_small_buffer(struct buffer *dst, struct buffer *src); +struct buffer *htx_copy_to_large_buffer(struct buffer *dst, struct buffer *src); #define HTX_XFER_DEFAULT 0x00000000 /* Default XFER: no partial xfer / remove blocks from source */ #define HTX_XFER_KEEP_SRC_BLKS 0x00000001 /* Don't remove xfer blocks from source messages during xfer */ diff --git a/src/htx.c b/src/htx.c index 695a69aac..13b6a0cbc 100644 --- a/src/htx.c +++ b/src/htx.c @@ -11,6 +11,7 @@ */ #include +#include #include #include #include @@ -1325,3 +1326,68 @@ int htx_append_msg(struct htx *dst, const struct htx *src) htx_truncate(dst, offset); return 0; } + + +/* If possible, trasnfer HTX blocks from to a small buffer. This function + * allocate the small buffer and makes point on it. If is not empty + * or if contains to many data, NULL is returned. If the allocation + * failed, NULL is returned. Otherwise is returned. instructs how + * the transfer must be performed. + */ +struct buffer *__htx_xfer_to_small_buffer(struct buffer *dst, struct buffer *src, unsigned int flags) +{ + struct htx *dst_htx; + struct htx *src_htx = htxbuf(src); + size_t sz = (sizeof(struct htx) + htx_used_space(src_htx)); + + if (dst->size || sz > global.tune.bufsize_small || !b_alloc_small(dst)) + return NULL; + dst_htx = htx_from_buf(dst); + htx_xfer(dst_htx, src_htx, src_htx->size, flags); + htx_to_buf(dst_htx, dst); + return dst; +} + +/* If possible, trasnfer HTX blocks from to a large buffer. This function + * allocate the small buffer and makes point on it. If is not empty + * or if contains to many data, NULL is returned. If the allocation + * failed, NULL is returned. Otherwise is returned. instructs how + * the transfer must be performed. + */ +struct buffer *__htx_xfer_to_large_buffer(struct buffer *dst, struct buffer *src, unsigned int flags) +{ + struct htx *dst_htx; + struct htx *src_htx = htxbuf(src); + size_t sz = (sizeof(struct htx) + htx_used_space(src_htx)); + + if (dst->size || sz > global.tune.bufsize_large || !b_alloc_large(dst)) + return NULL; + dst_htx = htx_from_buf(dst); + htx_xfer(dst_htx, src_htx, src_htx->size, flags); + htx_to_buf(dst_htx, dst); + return dst; +} + +/* Move HTX blocks from to . Relies on __htx_xfer_to_small_buffer() */ +struct buffer *htx_move_to_small_buffer(struct buffer *dst, struct buffer *src) +{ + return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_DEFAULT); +} + +/* Move HTX blocks from to . Relies on __htx_xfer_to_large_buffer() */ +struct buffer *htx_move_to_large_buffer(struct buffer *dst, struct buffer *src) +{ + return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_DEFAULT); +} + +/* Copy HTX blocks from to . Relies on __htx_xfer_to_small_buffer() */ +struct buffer *htx_copy_to_small_buffer(struct buffer *dst, struct buffer *src) +{ + return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS); +} + +/* Copy HTX blocks from to . Relies on __htx_xfer_to_large_buffer() */ +struct buffer *htx_copy_to_large_buffer(struct buffer *dst, struct buffer *src) +{ + return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS); +}