Skip to content

Commit d209278

Browse files
author
Arturo Bernal
committed
Add to the varbuf API in server/util.c
1 parent 6f2a011 commit d209278

File tree

3 files changed

+68
-72
lines changed

3 files changed

+68
-72
lines changed

include/util_varbuf.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
#include "httpd.h"
3838

39+
#include "apr_pools.h"
40+
#include "apr_errno.h"
41+
3942
#ifdef __cplusplus
4043
extern "C" {
4144
#endif
@@ -66,6 +69,8 @@ struct ap_varbuf {
6669
struct ap_varbuf_info *info;
6770
};
6871

72+
typedef struct ap_varbuf ap_varbuf;
73+
6974
/**
7075
* Initialize a resizable buffer. It is safe to re-initialize a previously
7176
* used ap_varbuf. The old buffer will be released when the corresponding
@@ -189,6 +194,35 @@ AP_DECLARE(apr_status_t) ap_varbuf_cfg_getline(struct ap_varbuf *vb,
189194
ap_configfile_t *cfp,
190195
apr_size_t max_len);
191196

197+
/**
198+
* @brief Allocate and initialize a new dynamic variable buffer.
199+
*
200+
* This function creates a new ap_varbuf structure, allocates an initial buffer
201+
* of the specified size from the given APR pool, and initializes its members.
202+
*
203+
* @param vb Pointer to the pointer that will be set to the new ap_varbuf.
204+
* @param p The APR memory pool from which to allocate the buffer.
205+
* @param initial_size The initial size in bytes for the buffer.
206+
* @return APR_SUCCESS on success.
207+
*/
208+
AP_DECLARE(apr_status_t) ap_varbuf_make(ap_varbuf **vb, apr_pool_t *p, apr_size_t initial_size);
209+
210+
/**
211+
* @brief Append a specified number of characters to a dynamic variable buffer.
212+
*
213+
* This function appends up to @c len characters from the string @c str to the
214+
* dynamic buffer represented by @c vb. If the buffer is not large enough to hold
215+
* the additional characters plus a null terminator, the function reallocates the
216+
* buffer with a larger size. The buffer is always null-terminated after the operation.
217+
*
218+
* @param vb The dynamic variable buffer.
219+
* @param str The string containing the characters to append.
220+
* @param len The number of characters from @c str to append.
221+
* @return APR_SUCCESS on success.
222+
*/
223+
AP_DECLARE(apr_status_t) ap_varbuf_strncat(ap_varbuf *vb, const char *str, apr_size_t len);
224+
225+
192226
#ifdef __cplusplus
193227
}
194228
#endif

modules/proxy/mod_proxy_fcgi.c

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "util_fcgi.h"
1919
#include "util_script.h"
2020
#include "ap_expr.h"
21-
#include <ctype.h>
21+
#include "util_varbuf.h"
2222

2323
module AP_MODULE_DECLARE_DATA proxy_fcgi_module;
2424

@@ -594,71 +594,6 @@ static int handle_headers(request_rec *r, int *state,
594594
return 0;
595595
}
596596

597-
/**
598-
* @struct ap_varbuf
599-
* @brief Structure representing a dynamically resizable buffer.
600-
*
601-
* This structure holds a character buffer along with its current length and the total
602-
* allocated size. The buffer is allocated from the provided APR pool.
603-
*/
604-
typedef struct {
605-
char *buf;
606-
apr_size_t cur_len;
607-
apr_size_t size;
608-
apr_pool_t *pool;
609-
} ap_varbuf;
610-
611-
/**
612-
* @brief Allocate and initialize a new dynamic variable buffer.
613-
*
614-
* This function creates a new ap_varbuf structure, allocates an initial buffer
615-
* of the specified size from the given APR pool, and initializes its members.
616-
*
617-
* @param vb A pointer to the pointer that will hold the allocated ap_varbuf.
618-
* @param p The APR memory pool from which to allocate the buffer.
619-
* @param initial_size The initial size in bytes for the buffer.
620-
* @return APR_SUCCESS on success.
621-
*/
622-
static apr_status_t ap_varbuf_make(ap_varbuf **vb, apr_pool_t *p, apr_size_t initial_size)
623-
{
624-
*vb = apr_pcalloc(p, sizeof(ap_varbuf));
625-
(*vb)->pool = p;
626-
(*vb)->buf = apr_palloc(p, initial_size);
627-
(*vb)->size = initial_size;
628-
(*vb)->cur_len = 0;
629-
(*vb)->buf[0] = '\0';
630-
return APR_SUCCESS;
631-
}
632-
633-
634-
/**
635-
* @brief Append a specified number of characters to a dynamic variable buffer.
636-
*
637-
* This function appends up to @c len characters from the string @c str to the
638-
* dynamic buffer represented by @c vb. If the buffer is not large enough to hold
639-
* the additional characters plus a null terminator, the function reallocates the
640-
* buffer with a larger size. The buffer is always null-terminated after the operation.
641-
*
642-
* @param vb The dynamic variable buffer to which the string will be appended.
643-
* @param str The string containing the characters to append.
644-
* @param len The number of characters from @c str to append.
645-
* @return APR_SUCCESS on success.
646-
*/
647-
static apr_status_t ap_varbuf_strncat(ap_varbuf *vb, const char *str, apr_size_t len)
648-
{
649-
if (vb->cur_len + len + 1 > vb->size) {
650-
apr_size_t new_size = vb->cur_len + len + 1;
651-
char *newbuf = apr_palloc(vb->pool, new_size);
652-
memcpy(newbuf, vb->buf, vb->cur_len);
653-
vb->buf = newbuf;
654-
vb->size = new_size;
655-
}
656-
memcpy(vb->buf + vb->cur_len, str, len);
657-
vb->cur_len += len;
658-
vb->buf[vb->cur_len] = '\0';
659-
return APR_SUCCESS;
660-
}
661-
662597

663598
static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,
664599
request_rec *r, apr_pool_t *setaside_pool,
@@ -896,18 +831,18 @@ static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,
896831
/* Try our dynamic header buffer approach first */
897832
ap_varbuf_strncat(header_vb, iobuf, readbuflen);
898833
if (strstr(header_vb->buf, "\r\n\r\n") != NULL) {
899-
while (header_vb->cur_len > 0 &&
834+
while (header_vb->strlen > 0 &&
900835
isspace((unsigned char)header_vb->buf[0])) {
901-
memmove(header_vb->buf, header_vb->buf + 1, header_vb->cur_len - 1);
902-
header_vb->cur_len--;
903-
header_vb->buf[header_vb->cur_len] = '\0';
836+
memmove(header_vb->buf, header_vb->buf + 1, header_vb->strlen - 1);
837+
header_vb->strlen--;
838+
header_vb->buf[header_vb->strlen] = '\0';
904839
}
905840
seen_end_of_headers = 1;
906841
{
907842
int status_hdr;
908843
apr_bucket_brigade *tmp_bb = apr_brigade_create(r->pool, c->bucket_alloc);
909844
apr_bucket *hdr_bucket = apr_bucket_heap_create(header_vb->buf,
910-
header_vb->cur_len, NULL, c->bucket_alloc);
845+
header_vb->strlen, NULL, c->bucket_alloc);
911846
APR_BRIGADE_INSERT_TAIL(tmp_bb, hdr_bucket);
912847
hdr_bucket = apr_bucket_eos_create(c->bucket_alloc);
913848
APR_BRIGADE_INSERT_TAIL(tmp_bb, hdr_bucket);
@@ -921,7 +856,7 @@ static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,
921856
}
922857
{
923858
char *hdr_end = strstr(header_vb->buf, "\r\n\r\n") + 4;
924-
apr_size_t hdr_total = header_vb->cur_len;
859+
apr_size_t hdr_total = header_vb->strlen;
925860
apr_size_t remaining = hdr_total - (hdr_end - header_vb->buf);
926861
if (remaining > 0) {
927862
apr_bucket *rem_bucket = apr_bucket_heap_create(hdr_end,

server/util.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,3 +3922,30 @@ AP_DECLARE(const char *)ap_dir_fnmatch(ap_dir_match_t *w, const char *path,
39223922

39233923
return NULL;
39243924
}
3925+
3926+
AP_DECLARE(apr_status_t) ap_varbuf_make(ap_varbuf **vb, apr_pool_t *p, apr_size_t initial_size)
3927+
{
3928+
*vb = apr_pcalloc(p, sizeof(ap_varbuf));
3929+
(*vb)->pool = p;
3930+
(*vb)->buf = apr_palloc(p, initial_size);
3931+
(*vb)->avail = initial_size;
3932+
(*vb)->strlen = 0;
3933+
(*vb)->buf[0] = '\0';
3934+
return APR_SUCCESS;
3935+
}
3936+
3937+
AP_DECLARE(apr_status_t) ap_varbuf_strncat(ap_varbuf *vb, const char *str, apr_size_t len)
3938+
{
3939+
if (vb->strlen + len + 1 > vb->avail) {
3940+
apr_size_t new_size = vb->strlen + len + 1;
3941+
char *newbuf = apr_palloc(vb->pool, new_size);
3942+
memcpy(newbuf, vb->buf, vb->strlen);
3943+
vb->buf = newbuf;
3944+
vb->avail = new_size;
3945+
}
3946+
memcpy(vb->buf + vb->strlen, str, len);
3947+
vb->strlen += len;
3948+
vb->buf[vb->strlen] = '\0';
3949+
return APR_SUCCESS;
3950+
}
3951+

0 commit comments

Comments
 (0)