~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr-util/buckets/apr_brigade.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "apr.h"
 
18
#include "apr_lib.h"
 
19
#include "apr_strings.h"
 
20
#include "apr_pools.h"
 
21
#include "apr_tables.h"
 
22
#include "apr_buckets.h"
 
23
#include "apr_errno.h"
 
24
#define APR_WANT_MEMFUNC
 
25
#define APR_WANT_STRFUNC
 
26
#include "apr_want.h"
 
27
 
 
28
#if APR_HAVE_SYS_UIO_H
 
29
#include <sys/uio.h>
 
30
#endif
 
31
 
 
32
static apr_status_t brigade_cleanup(void *data) 
 
33
{
 
34
    return apr_brigade_cleanup(data);
 
35
}
 
36
 
 
37
APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data)
 
38
{
 
39
    apr_bucket_brigade *b = data;
 
40
    apr_bucket *e;
 
41
 
 
42
    while (!APR_BRIGADE_EMPTY(b)) {
 
43
        e = APR_BRIGADE_FIRST(b);
 
44
        apr_bucket_delete(e);
 
45
    }
 
46
    /* We don't need to free(bb) because it's allocated from a pool. */
 
47
    return APR_SUCCESS;
 
48
}
 
49
 
 
50
APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b)
 
51
{
 
52
    apr_pool_cleanup_kill(b->p, b, brigade_cleanup);
 
53
    return apr_brigade_cleanup(b);
 
54
}
 
55
 
 
56
APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
 
57
                                                     apr_bucket_alloc_t *list)
 
58
{
 
59
    apr_bucket_brigade *b;
 
60
 
 
61
    b = apr_palloc(p, sizeof(*b));
 
62
    b->p = p;
 
63
    b->bucket_alloc = list;
 
64
 
 
65
    APR_RING_INIT(&b->list, apr_bucket, link);
 
66
 
 
67
    apr_pool_cleanup_register(b->p, b, brigade_cleanup, apr_pool_cleanup_null);
 
68
    return b;
 
69
}
 
70
 
 
71
APU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
 
72
                                                    apr_bucket *e)
 
73
{
 
74
    apr_bucket_brigade *a;
 
75
    apr_bucket *f;
 
76
 
 
77
    a = apr_brigade_create(b->p, b->bucket_alloc);
 
78
    /* Return an empty brigade if there is nothing left in 
 
79
     * the first brigade to split off 
 
80
     */
 
81
    if (e != APR_BRIGADE_SENTINEL(b)) {
 
82
        f = APR_RING_LAST(&b->list);
 
83
        APR_RING_UNSPLICE(e, f, link);
 
84
        APR_RING_SPLICE_HEAD(&a->list, e, f, apr_bucket, link);
 
85
    }
 
86
 
 
87
    APR_BRIGADE_CHECK_CONSISTENCY(a);
 
88
    APR_BRIGADE_CHECK_CONSISTENCY(b);
 
89
 
 
90
    return a;
 
91
}
 
92
 
 
93
APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
 
94
                                                apr_off_t point,
 
95
                                                apr_bucket **after_point)
 
96
{
 
97
    apr_bucket *e;
 
98
    const char *s;
 
99
    apr_size_t len;
 
100
    apr_status_t rv;
 
101
 
 
102
    if (point < 0) {
 
103
        /* this could cause weird (not necessarily SEGV) things to happen */
 
104
        return APR_EINVAL;
 
105
    }
 
106
    if (point == 0) {
 
107
        *after_point = APR_BRIGADE_FIRST(b);
 
108
        return APR_SUCCESS;
 
109
    }
 
110
 
 
111
    APR_BRIGADE_CHECK_CONSISTENCY(b);
 
112
 
 
113
    for (e = APR_BRIGADE_FIRST(b);
 
114
         e != APR_BRIGADE_SENTINEL(b);
 
115
         e = APR_BUCKET_NEXT(e))
 
116
    {
 
117
        if ((e->length == (apr_size_t)(-1)) && (point > (apr_size_t)(-1))) {
 
118
            /* point is too far out to simply split this bucket,
 
119
             * we must fix this bucket's size and keep going... */
 
120
            rv = apr_bucket_read(e, &s, &len, APR_BLOCK_READ);
 
121
            if (rv != APR_SUCCESS) {
 
122
                *after_point = e;
 
123
                return rv;
 
124
            }
 
125
        }
 
126
        if ((point < e->length) || (e->length == (apr_size_t)(-1))) {
 
127
            /* We already checked e->length -1 above, so we now
 
128
             * trust e->length < MAX_APR_SIZE_T.
 
129
             * First try to split the bucket natively... */
 
130
            if ((rv = apr_bucket_split(e, (apr_size_t)point)) 
 
131
                    != APR_ENOTIMPL) {
 
132
                *after_point = APR_BUCKET_NEXT(e);
 
133
                return rv;
 
134
            }
 
135
 
 
136
            /* if the bucket cannot be split, we must read from it,
 
137
             * changing its type to one that can be split */
 
138
            rv = apr_bucket_read(e, &s, &len, APR_BLOCK_READ);
 
139
            if (rv != APR_SUCCESS) {
 
140
                *after_point = e;
 
141
                return rv;
 
142
            }
 
143
 
 
144
            /* this assumes that len == e->length, which is okay because e
 
145
             * might have been morphed by the apr_bucket_read() above, but
 
146
             * if it was, the length would have been adjusted appropriately */
 
147
            if (point < e->length) {
 
148
                rv = apr_bucket_split(e, (apr_size_t)point);
 
149
                *after_point = APR_BUCKET_NEXT(e);
 
150
                return rv;
 
151
            }
 
152
        }
 
153
        if (point == e->length) {
 
154
            *after_point = APR_BUCKET_NEXT(e);
 
155
            return APR_SUCCESS;
 
156
        }
 
157
        point -= e->length;
 
158
    }
 
159
    *after_point = APR_BRIGADE_SENTINEL(b); 
 
160
    return APR_INCOMPLETE;
 
161
}
 
162
 
 
163
APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
 
164
                                             int read_all, apr_off_t *length)
 
165
{
 
166
    apr_off_t total = 0;
 
167
    apr_bucket *bkt;
 
168
 
 
169
    for (bkt = APR_BRIGADE_FIRST(bb);
 
170
         bkt != APR_BRIGADE_SENTINEL(bb);
 
171
         bkt = APR_BUCKET_NEXT(bkt))
 
172
    {
 
173
        if (bkt->length == (apr_size_t)(-1)) {
 
174
            const char *ignore;
 
175
            apr_size_t len;
 
176
            apr_status_t status;
 
177
 
 
178
            if (!read_all) {
 
179
                *length = -1;
 
180
                return APR_SUCCESS;
 
181
            }
 
182
 
 
183
            if ((status = apr_bucket_read(bkt, &ignore, &len,
 
184
                                          APR_BLOCK_READ)) != APR_SUCCESS) {
 
185
                return status;
 
186
            }
 
187
        }
 
188
 
 
189
        total += bkt->length;
 
190
    }
 
191
 
 
192
    *length = total;
 
193
    return APR_SUCCESS;
 
194
}
 
195
 
 
196
APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
 
197
                                              char *c, apr_size_t *len)
 
198
{
 
199
    apr_size_t actual = 0;
 
200
    apr_bucket *b;
 
201
 
 
202
    for (b = APR_BRIGADE_FIRST(bb);
 
203
         b != APR_BRIGADE_SENTINEL(bb);
 
204
         b = APR_BUCKET_NEXT(b))
 
205
    {
 
206
        const char *str;
 
207
        apr_size_t str_len;
 
208
        apr_status_t status;
 
209
 
 
210
        status = apr_bucket_read(b, &str, &str_len, APR_BLOCK_READ);
 
211
        if (status != APR_SUCCESS) {
 
212
            return status;
 
213
        }
 
214
 
 
215
        /* If we would overflow. */
 
216
        if (str_len + actual > *len) {
 
217
            str_len = *len - actual;
 
218
        }
 
219
 
 
220
        /* XXX: It appears that overflow of the final bucket
 
221
         * is DISCARDED without any warning to the caller.
 
222
         *
 
223
         * No, we only copy the data up to their requested size.  -- jre
 
224
         */
 
225
        memcpy(c, str, str_len);
 
226
 
 
227
        c += str_len;
 
228
        actual += str_len;
 
229
 
 
230
        /* This could probably be actual == *len, but be safe from stray
 
231
         * photons. */
 
232
        if (actual >= *len) {
 
233
            break;
 
234
        }
 
235
    }
 
236
 
 
237
    *len = actual;
 
238
    return APR_SUCCESS;
 
239
}
 
240
 
 
241
APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
 
242
                                               char **c,
 
243
                                               apr_size_t *len,
 
244
                                               apr_pool_t *pool)
 
245
{
 
246
    apr_off_t actual;
 
247
    apr_size_t total;
 
248
    apr_status_t rv;
 
249
 
 
250
    apr_brigade_length(bb, 1, &actual);
 
251
    
 
252
    /* XXX: This is dangerous beyond belief.  At least in the
 
253
     * apr_brigade_flatten case, the user explicitly stated their
 
254
     * buffer length - so we don't up and palloc 4GB for a single
 
255
     * file bucket.  This API must grow a useful max boundry,
 
256
     * either compiled-in or preset via the *len value.
 
257
     *
 
258
     * Shouldn't both fn's grow an additional return value for 
 
259
     * the case that the brigade couldn't be flattened into the
 
260
     * provided or allocated buffer (such as APR_EMOREDATA?)
 
261
     * Not a failure, simply an advisory result.
 
262
     */
 
263
    total = (apr_size_t)actual;
 
264
 
 
265
    *c = apr_palloc(pool, total);
 
266
    
 
267
    rv = apr_brigade_flatten(bb, *c, &total);
 
268
 
 
269
    if (rv != APR_SUCCESS) {
 
270
        return rv;
 
271
    }
 
272
 
 
273
    *len = total;
 
274
    return APR_SUCCESS;
 
275
}
 
276
 
 
277
APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
 
278
                                                 apr_bucket_brigade *bbIn,
 
279
                                                 apr_read_type_e block,
 
280
                                                 apr_off_t maxbytes)
 
281
{
 
282
    apr_off_t readbytes = 0;
 
283
 
 
284
    while (!APR_BRIGADE_EMPTY(bbIn)) {
 
285
        const char *pos;
 
286
        const char *str;
 
287
        apr_size_t len;
 
288
        apr_status_t rv;
 
289
        apr_bucket *e;
 
290
 
 
291
        e = APR_BRIGADE_FIRST(bbIn);
 
292
        rv = apr_bucket_read(e, &str, &len, block);
 
293
 
 
294
        if (rv != APR_SUCCESS) {
 
295
            return rv;
 
296
        }
 
297
 
 
298
        pos = memchr(str, APR_ASCII_LF, len);
 
299
        /* We found a match. */
 
300
        if (pos != NULL) {
 
301
            apr_bucket_split(e, pos - str + 1);
 
302
            APR_BUCKET_REMOVE(e);
 
303
            APR_BRIGADE_INSERT_TAIL(bbOut, e);
 
304
            return APR_SUCCESS;
 
305
        }
 
306
        APR_BUCKET_REMOVE(e);
 
307
        APR_BRIGADE_INSERT_TAIL(bbOut, e);
 
308
        readbytes += len;
 
309
        /* We didn't find an APR_ASCII_LF within the maximum line length. */
 
310
        if (readbytes >= maxbytes) {
 
311
            break;
 
312
        }
 
313
    }
 
314
 
 
315
    return APR_SUCCESS;
 
316
}
 
317
 
 
318
 
 
319
APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b, 
 
320
                                               struct iovec *vec, int *nvec)
 
321
{
 
322
    int left = *nvec;
 
323
    apr_bucket *e;
 
324
    struct iovec *orig;
 
325
    apr_size_t iov_len;
 
326
    apr_status_t rv;
 
327
 
 
328
    orig = vec;
 
329
 
 
330
    for (e = APR_BRIGADE_FIRST(b);
 
331
         e != APR_BRIGADE_SENTINEL(b);
 
332
         e = APR_BUCKET_NEXT(e))
 
333
    {
 
334
        if (left-- == 0)
 
335
            break;
 
336
 
 
337
        rv = apr_bucket_read(e, (const char **)&vec->iov_base, &iov_len,
 
338
                             APR_NONBLOCK_READ);
 
339
        if (rv != APR_SUCCESS)
 
340
            return rv;
 
341
        vec->iov_len = iov_len; /* set indirectly in case size differs */
 
342
        ++vec;
 
343
    }
 
344
 
 
345
    *nvec = vec - orig;
 
346
    return APR_SUCCESS;
 
347
}
 
348
 
 
349
APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b, 
 
350
                                               apr_brigade_flush flush,
 
351
                                               void *ctx,
 
352
                                               va_list va)
 
353
{
 
354
    for (;;) {
 
355
        const char *str = va_arg(va, const char *);
 
356
        apr_status_t rv;
 
357
 
 
358
        if (str == NULL)
 
359
            break;
 
360
 
 
361
        rv = apr_brigade_write(b, flush, ctx, str, strlen(str));
 
362
        if (rv != APR_SUCCESS)
 
363
            return rv;
 
364
    }
 
365
 
 
366
    return APR_SUCCESS;
 
367
}
 
368
 
 
369
APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
 
370
                                           apr_brigade_flush flush, void *ctx,
 
371
                                           const char c)
 
372
{
 
373
    return apr_brigade_write(b, flush, ctx, &c, 1);
 
374
}
 
375
 
 
376
APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
 
377
                                            apr_brigade_flush flush,
 
378
                                            void *ctx, 
 
379
                                            const char *str, apr_size_t nbyte)
 
380
{
 
381
    apr_bucket *e = APR_BRIGADE_LAST(b);
 
382
    apr_size_t remaining = APR_BUCKET_BUFF_SIZE;
 
383
    char *buf = NULL;
 
384
 
 
385
    if (!APR_BRIGADE_EMPTY(b) && APR_BUCKET_IS_HEAP(e)) {
 
386
        apr_bucket_heap *h = e->data;
 
387
 
 
388
        /* HEAP bucket start offsets are always in-memory, safe to cast */
 
389
        remaining = h->alloc_len - (e->length + (apr_size_t)e->start);
 
390
        buf = h->base + e->start + e->length;
 
391
    }
 
392
 
 
393
    if (nbyte > remaining) {
 
394
        /* either a buffer bucket exists but is full, 
 
395
         * or no buffer bucket exists and the data is too big
 
396
         * to buffer.  In either case, we should flush.  */
 
397
        if (flush) {
 
398
            e = apr_bucket_transient_create(str, nbyte, b->bucket_alloc);
 
399
            APR_BRIGADE_INSERT_TAIL(b, e);
 
400
            return flush(b, ctx);
 
401
        }
 
402
        else {
 
403
            e = apr_bucket_heap_create(str, nbyte, NULL, b->bucket_alloc);
 
404
            APR_BRIGADE_INSERT_TAIL(b, e);
 
405
            return APR_SUCCESS;
 
406
        }
 
407
    }
 
408
    else if (!buf) {
 
409
        /* we don't have a buffer, but the data is small enough
 
410
         * that we don't mind making a new buffer */
 
411
        buf = apr_bucket_alloc(APR_BUCKET_BUFF_SIZE, b->bucket_alloc);
 
412
        e = apr_bucket_heap_create(buf, APR_BUCKET_BUFF_SIZE,
 
413
                                   apr_bucket_free, b->bucket_alloc);
 
414
        APR_BRIGADE_INSERT_TAIL(b, e);
 
415
        e->length = 0;   /* We are writing into the brigade, and
 
416
                          * allocating more memory than we need.  This
 
417
                          * ensures that the bucket thinks it is empty just
 
418
                          * after we create it.  We'll fix the length
 
419
                          * once we put data in it below.
 
420
                          */
 
421
    }
 
422
 
 
423
    /* there is a sufficiently big buffer bucket available now */
 
424
    memcpy(buf, str, nbyte);
 
425
    e->length += nbyte;
 
426
 
 
427
    return APR_SUCCESS;
 
428
}
 
429
 
 
430
APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
 
431
                                             apr_brigade_flush flush,
 
432
                                             void *ctx,
 
433
                                             const struct iovec *vec,
 
434
                                             apr_size_t nvec)
 
435
{
 
436
    apr_bucket *e;
 
437
    apr_size_t total_len;
 
438
    apr_size_t i;
 
439
    char *buf;
 
440
 
 
441
    /* Compute the total length of the data to be written.
 
442
     */
 
443
    total_len = 0;
 
444
    for (i = 0; i < nvec; i++) {
 
445
       total_len += vec[i].iov_len;
 
446
    }
 
447
 
 
448
    /* If the data to be written is very large, try to convert
 
449
     * the iovec to transient buckets rather than copying.
 
450
     */
 
451
    if (total_len > APR_BUCKET_BUFF_SIZE) {
 
452
        if (flush) {
 
453
            for (i = 0; i < nvec; i++) {
 
454
                e = apr_bucket_transient_create(vec[i].iov_base,
 
455
                                                vec[i].iov_len,
 
456
                                                b->bucket_alloc);
 
457
                APR_BRIGADE_INSERT_TAIL(b, e);
 
458
            }
 
459
            return flush(b, ctx);
 
460
        }
 
461
        else {
 
462
            for (i = 0; i < nvec; i++) {
 
463
                e = apr_bucket_heap_create((const char *) vec[i].iov_base,
 
464
                                           vec[i].iov_len, NULL,
 
465
                                           b->bucket_alloc);
 
466
                APR_BRIGADE_INSERT_TAIL(b, e);
 
467
            }
 
468
            return APR_SUCCESS;
 
469
        }
 
470
    }
 
471
 
 
472
    i = 0;
 
473
 
 
474
    /* If there is a heap bucket at the end of the brigade
 
475
     * already, copy into the existing bucket.
 
476
     */
 
477
    e = APR_BRIGADE_LAST(b);
 
478
    if (!APR_BRIGADE_EMPTY(b) && APR_BUCKET_IS_HEAP(e)) {
 
479
        apr_bucket_heap *h = e->data;
 
480
        apr_size_t remaining = h->alloc_len -
 
481
            (e->length + (apr_size_t)e->start);
 
482
        buf = h->base + e->start + e->length;
 
483
 
 
484
        if (remaining >= total_len) {
 
485
            /* Simple case: all the data will fit in the
 
486
             * existing heap bucket
 
487
             */
 
488
            for (; i < nvec; i++) {
 
489
                apr_size_t len = vec[i].iov_len;
 
490
                memcpy(buf, (const void *) vec[i].iov_base, len);
 
491
                buf += len;
 
492
            }
 
493
            e->length += total_len;
 
494
            return APR_SUCCESS;
 
495
        }
 
496
        else {
 
497
            /* More complicated case: not all of the data
 
498
             * will fit in the existing heap bucket.  The
 
499
             * total data size is <= APR_BUCKET_BUFF_SIZE,
 
500
             * so we'll need only one additional bucket.
 
501
             */
 
502
            const char *start_buf = buf;
 
503
            for (; i < nvec; i++) {
 
504
                apr_size_t len = vec[i].iov_len;
 
505
                if (len > remaining) {
 
506
                    break;
 
507
                }
 
508
                memcpy(buf, (const void *) vec[i].iov_base, len);
 
509
                buf += len;
 
510
                remaining -= len;
 
511
            }
 
512
            e->length += (buf - start_buf);
 
513
            total_len -= (buf - start_buf);
 
514
 
 
515
            if (flush) {
 
516
                apr_status_t rv = flush(b, ctx);
 
517
                if (rv != APR_SUCCESS) {
 
518
                    return rv;
 
519
                }
 
520
            }
 
521
 
 
522
            /* Now fall through into the case below to
 
523
             * allocate another heap bucket and copy the
 
524
             * rest of the array.  (Note that i is not
 
525
             * reset to zero here; it holds the index
 
526
             * of the first vector element to be
 
527
             * written to the new bucket.)
 
528
             */
 
529
        }
 
530
    }
 
531
 
 
532
    /* Allocate a new heap bucket, and copy the data into it.
 
533
     * The checks above ensure that the amount of data to be
 
534
     * written here is no larger than APR_BUCKET_BUFF_SIZE.
 
535
     */
 
536
    buf = apr_bucket_alloc(APR_BUCKET_BUFF_SIZE, b->bucket_alloc);
 
537
    e = apr_bucket_heap_create(buf, APR_BUCKET_BUFF_SIZE,
 
538
                               apr_bucket_free, b->bucket_alloc);
 
539
    for (; i < nvec; i++) {
 
540
        apr_size_t len = vec[i].iov_len;
 
541
        memcpy(buf, (const void *) vec[i].iov_base, len);
 
542
        buf += len;
 
543
    }
 
544
    e->length = total_len;
 
545
    APR_BRIGADE_INSERT_TAIL(b, e);
 
546
 
 
547
    return APR_SUCCESS;
 
548
}
 
549
 
 
550
APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
 
551
                                           apr_brigade_flush flush, void *ctx,
 
552
                                           const char *str)
 
553
{
 
554
    apr_size_t len = strlen(str);
 
555
    apr_bucket *bkt = APR_BRIGADE_LAST(bb);
 
556
    if (!APR_BRIGADE_EMPTY(bb) && APR_BUCKET_IS_HEAP(bkt)) {
 
557
        /* If there is enough space available in a heap bucket
 
558
         * at the end of the brigade, copy the string directly
 
559
         * into the heap bucket
 
560
         */
 
561
        apr_bucket_heap *h = bkt->data;
 
562
        apr_size_t bytes_avail = h->alloc_len - bkt->length;
 
563
 
 
564
        if (bytes_avail >= len) {
 
565
            char *buf = h->base + bkt->start + bkt->length;
 
566
            memcpy(buf, str, len);
 
567
            bkt->length += len;
 
568
            return APR_SUCCESS;
 
569
        }
 
570
    }
 
571
 
 
572
    /* If the string could not be copied into an existing heap
 
573
     * bucket, delegate the work to apr_brigade_write(), which
 
574
     * knows how to grow the brigade
 
575
     */
 
576
    return apr_brigade_write(bb, flush, ctx, str, len);
 
577
}
 
578
 
 
579
APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b, 
 
580
                                                     apr_brigade_flush flush,
 
581
                                                     void *ctx, ...)
 
582
{
 
583
    va_list va;
 
584
    apr_status_t rv;
 
585
 
 
586
    va_start(va, ctx);
 
587
    rv = apr_brigade_vputstrs(b, flush, ctx, va);
 
588
    va_end(va);
 
589
    return rv;
 
590
}
 
591
 
 
592
APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b, 
 
593
                                                    apr_brigade_flush flush,
 
594
                                                    void *ctx, 
 
595
                                                    const char *fmt, ...)
 
596
{
 
597
    va_list ap;
 
598
    apr_status_t rv;
 
599
 
 
600
    va_start(ap, fmt);
 
601
    rv = apr_brigade_vprintf(b, flush, ctx, fmt, ap);
 
602
    va_end(ap);
 
603
    return rv;
 
604
}
 
605
 
 
606
struct brigade_vprintf_data_t {
 
607
    apr_vformatter_buff_t vbuff;
 
608
 
 
609
    apr_bucket_brigade *b;  /* associated brigade */
 
610
    apr_brigade_flush *flusher; /* flushing function */
 
611
    void *ctx;
 
612
 
 
613
    char *cbuff; /* buffer to flush from */
 
614
};
 
615
 
 
616
static apr_status_t brigade_flush(apr_vformatter_buff_t *buff)
 
617
{
 
618
    /* callback function passed to ap_vformatter to be
 
619
     * called when vformatter needs to buff and
 
620
     * buff.curpos > buff.endpos
 
621
     */
 
622
 
 
623
    /* "downcast," have really passed a brigade_vprintf_data_t* */
 
624
    struct brigade_vprintf_data_t *vd = (struct brigade_vprintf_data_t*)buff;
 
625
    apr_status_t res = APR_SUCCESS;
 
626
 
 
627
    res = apr_brigade_write(vd->b, *vd->flusher, vd->ctx, vd->cbuff,
 
628
                          APR_BUCKET_BUFF_SIZE);
 
629
 
 
630
    if(res != APR_SUCCESS) {
 
631
      return -1;
 
632
    }
 
633
 
 
634
    vd->vbuff.curpos = vd->cbuff;
 
635
    vd->vbuff.endpos = vd->cbuff + APR_BUCKET_BUFF_SIZE;
 
636
 
 
637
    return res;
 
638
}
 
639
 
 
640
APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
 
641
                                              apr_brigade_flush flush,
 
642
                                              void *ctx,
 
643
                                              const char *fmt, va_list va)
 
644
{
 
645
    /* the cast, in order of appearance */
 
646
    struct brigade_vprintf_data_t vd;
 
647
    char buf[APR_BUCKET_BUFF_SIZE];
 
648
    apr_size_t written;
 
649
 
 
650
    vd.vbuff.curpos = buf;
 
651
    vd.vbuff.endpos = buf + APR_BUCKET_BUFF_SIZE;
 
652
    vd.b = b;
 
653
    vd.flusher = &flush;
 
654
    vd.ctx = ctx;
 
655
    vd.cbuff = buf;
 
656
 
 
657
    written = apr_vformatter(brigade_flush, &vd.vbuff, fmt, va);
 
658
 
 
659
    if (written == -1) {
 
660
      return -1;
 
661
    }
 
662
 
 
663
    /* tack on null terminator to remaining string */
 
664
    *(vd.vbuff.curpos) = '\0';
 
665
 
 
666
    /* write out what remains in the buffer */
 
667
    return apr_brigade_write(b, flush, ctx, buf, vd.vbuff.curpos - buf);
 
668
}
 
669
 
 
670
/* A "safe" maximum bucket size, 1Gb */
 
671
#define MAX_BUCKET_SIZE (0x40000000)
 
672
 
 
673
APU_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb,
 
674
                                                  apr_file_t *f,
 
675
                                                  apr_off_t start,
 
676
                                                  apr_off_t length,
 
677
                                                  apr_pool_t *p)
 
678
{
 
679
    apr_bucket *e;
 
680
 
 
681
    if (sizeof(apr_off_t) == sizeof(apr_size_t) || length < MAX_BUCKET_SIZE) {
 
682
        e = apr_bucket_file_create(f, start, (apr_size_t)length, p, 
 
683
                                   bb->bucket_alloc);
 
684
    }
 
685
    else {
 
686
        /* Several buckets are needed. */        
 
687
        e = apr_bucket_file_create(f, start, MAX_BUCKET_SIZE, p, 
 
688
                                   bb->bucket_alloc);
 
689
 
 
690
        while (length > MAX_BUCKET_SIZE) {
 
691
            apr_bucket *ce;
 
692
            apr_bucket_copy(e, &ce);
 
693
            APR_BRIGADE_INSERT_TAIL(bb, ce);
 
694
            e->start += MAX_BUCKET_SIZE;
 
695
            length -= MAX_BUCKET_SIZE;
 
696
        }
 
697
        e->length = (apr_size_t)length; /* Resize just the last bucket */
 
698
    }
 
699
    
 
700
    APR_BRIGADE_INSERT_TAIL(bb, e);
 
701
    return e;
 
702
}