add an apr_brigade_length() function
git-svn-id: https://svn.apache.org/repos/asf/apr/apr-util/trunk@58091 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/buckets/apr_brigade.c b/buckets/apr_brigade.c
index bc54cc6..94e237e 100644
--- a/buckets/apr_brigade.c
+++ b/buckets/apr_brigade.c
@@ -167,6 +167,36 @@
return NULL;
}
+APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
+ int read_all, apr_ssize_t *length)
+{
+ apr_ssize_t total = 0;
+ apr_bucket *bkt;
+
+ APR_BRIGADE_FOREACH(bkt, bb) {
+ if (bkt->length == -1) {
+ const char *ignore;
+ apr_size_t len;
+ apr_status_t status;
+
+ if (!read_all) {
+ *length = -1;
+ return APR_SUCCESS;
+ }
+
+ if ((status = apr_bucket_read(bkt, &ignore, &len,
+ APR_BLOCK_READ)) != APR_SUCCESS) {
+ return status;
+ }
+ }
+
+ total += bkt->length;
+ }
+
+ *length = total;
+ return APR_SUCCESS;
+}
+
APU_DECLARE(int) apr_brigade_to_iovec(apr_bucket_brigade *b,
struct iovec *vec, int nvec)
{
diff --git a/include/apr_buckets.h b/include/apr_buckets.h
index 24e34aa..2eb7f59 100644
--- a/include/apr_buckets.h
+++ b/include/apr_buckets.h
@@ -80,7 +80,10 @@
#define APR_BUCKET_BUFF_SIZE 9000
-typedef enum {APR_BLOCK_READ, APR_NONBLOCK_READ} apr_read_type_e;
+typedef enum {
+ APR_BLOCK_READ,
+ APR_NONBLOCK_READ
+} apr_read_type_e;
/*
* The one-sentence buzzword-laden overview: Bucket brigades represent
@@ -620,6 +623,17 @@
#endif
/**
+ * Return the total length of the brigade.
+ * @param bb The brigade to compute the length of
+ * @param read_all Read unknown-length buckets to force a size
+ @ @param length Set to length of the brigade, or -1 if it has unknown-length buckets
+ * @deffunc apr_status_t apr_brigade_length(apr_bucket_brigade *bb, int read_all)
+ */
+APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
+ int read_all,
+ apr_ssize_t *length);
+
+/**
* create an iovec of the elements in a bucket_brigade... return number
* of elements used. This is useful for writing to a file or to the
* network efficiently.