Apache HTTPD
apr_brigade.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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
33{
35}
36
38{
41
42 while (!APR_BRIGADE_EMPTY(b)) {
45 }
46 /* We don't need to free(bb) because it's allocated from a pool. */
47 return APR_SUCCESS;
48}
49
51{
53 return apr_brigade_cleanup(b);
54}
55
58{
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
68 return b;
69}
70
74{
76
77 if (!a) {
78 a = apr_brigade_create(b->p, b->bucket_alloc);
79 }
80 else if (!APR_BRIGADE_EMPTY(a)) {
82 }
83 /* Return an empty brigade if there is nothing left in
84 * the first brigade to split off
85 */
86 if (e != APR_BRIGADE_SENTINEL(b)) {
87 f = APR_RING_LAST(&b->list);
88 APR_RING_UNSPLICE(e, f, link);
90 }
91
94
95 return a;
96}
97
100{
101 return apr_brigade_split_ex(b, e, NULL);
102}
103
107{
108 apr_bucket *e;
109 const char *s;
112 apr_status_t rv;
113
114 if (point < 0) {
115 /* this could cause weird (not necessarily SEGV) things to happen */
116 return APR_EINVAL;
117 }
118 if (point == 0) {
120 return APR_SUCCESS;
121 }
122
123 /*
124 * Try to reduce the following casting mess: We know that point will be
125 * larger equal 0 now and forever and thus that point (apr_off_t) and
126 * apr_size_t will fit into apr_uint64_t in any case.
127 */
129
131
132 for (e = APR_BRIGADE_FIRST(b);
135 {
136 /* For an unknown length bucket, while 'point64' is beyond the possible
137 * size contained in apr_size_t, read and continue...
138 */
139 if ((e->length == (apr_size_t)(-1))
141 /* point64 is too far out to simply split this bucket,
142 * we must fix this bucket's size and keep going... */
144 if (rv != APR_SUCCESS) {
145 *after_point = e;
146 return rv;
147 }
148 }
149 else if ((point64 < (apr_uint64_t)e->length)
150 || (e->length == (apr_size_t)(-1))) {
151 /* We already consumed buckets where point64 is beyond
152 * our interest ( point64 > APR_SIZE_MAX ), above.
153 * Here point falls between 0 and APR_SIZE_MAX
154 * and is within this bucket, or this bucket's len
155 * is undefined, so now we are ready to split it.
156 * First try to split the bucket natively... */
157 if ((rv = apr_bucket_split(e, (apr_size_t)point64))
158 != APR_ENOTIMPL) {
160 return rv;
161 }
162
163 /* if the bucket cannot be split, we must read from it,
164 * changing its type to one that can be split */
166 if (rv != APR_SUCCESS) {
167 *after_point = e;
168 return rv;
169 }
170
171 /* this assumes that len == e->length, which is okay because e
172 * might have been morphed by the apr_bucket_read() above, but
173 * if it was, the length would have been adjusted appropriately */
174 if (point64 < (apr_uint64_t)e->length) {
177 return rv;
178 }
179 }
180 if (point64 == (apr_uint64_t)e->length) {
182 return APR_SUCCESS;
183 }
185 }
187 return APR_INCOMPLETE;
188}
189
192{
193 apr_off_t total = 0;
196
197 for (bkt = APR_BRIGADE_FIRST(bb);
200 {
201 if (bkt->length == (apr_size_t)(-1)) {
202 const char *ignore;
204
205 if (!read_all) {
206 total = -1;
207 break;
208 }
209
212 break;
213 }
214 }
215
216 total += bkt->length;
217 }
218
219 *length = total;
220 return status;
221}
222
224 char *c, apr_size_t *len)
225{
226 apr_size_t actual = 0;
227 apr_bucket *b;
228
229 for (b = APR_BRIGADE_FIRST(bb);
230 b != APR_BRIGADE_SENTINEL(bb);
232 {
233 const char *str;
236
238 if (status != APR_SUCCESS) {
239 return status;
240 }
241
242 /* If we would overflow. */
243 if (str_len + actual > *len) {
244 str_len = *len - actual;
245 }
246
247 /* XXX: It appears that overflow of the final bucket
248 * is DISCARDED without any warning to the caller.
249 *
250 * No, we only copy the data up to their requested size. -- jre
251 */
252 memcpy(c, str, str_len);
253
254 c += str_len;
255 actual += str_len;
256
257 /* This could probably be actual == *len, but be safe from stray
258 * photons. */
259 if (actual >= *len) {
260 break;
261 }
262 }
263
264 *len = actual;
265 return APR_SUCCESS;
266}
267
269 char **c,
272{
274 apr_size_t total;
275 apr_status_t rv;
276
277 apr_brigade_length(bb, 1, &actual);
278
279 /* XXX: This is dangerous beyond belief. At least in the
280 * apr_brigade_flatten case, the user explicitly stated their
281 * buffer length - so we don't up and palloc 4GB for a single
282 * file bucket. This API must grow a useful max boundry,
283 * either compiled-in or preset via the *len value.
284 *
285 * Shouldn't both fn's grow an additional return value for
286 * the case that the brigade couldn't be flattened into the
287 * provided or allocated buffer (such as APR_EMOREDATA?)
288 * Not a failure, simply an advisory result.
289 */
290 total = (apr_size_t)actual;
291
292 *c = apr_palloc(pool, total);
293
294 rv = apr_brigade_flatten(bb, *c, &total);
295
296 if (rv != APR_SUCCESS) {
297 return rv;
298 }
299
300 *len = total;
301 return APR_SUCCESS;
302}
303
308{
310
311 while (!APR_BRIGADE_EMPTY(bbIn)) {
312 const char *pos;
313 const char *str;
315 apr_status_t rv;
316 apr_bucket *e;
317
319 rv = apr_bucket_read(e, &str, &len, block);
320
321 if (rv != APR_SUCCESS) {
322 return rv;
323 }
324
325 pos = memchr(str, APR_ASCII_LF, len);
326 /* We found a match. */
327 if (pos != NULL) {
328 apr_bucket_split(e, pos - str + 1);
331 return APR_SUCCESS;
332 }
336 }
337 else {
338 if (len > 0) {
340 if (rv != APR_SUCCESS) {
341 return rv;
342 }
343 }
345 }
346 readbytes += len;
347 /* We didn't find an APR_ASCII_LF within the maximum line length. */
348 if (readbytes >= maxbytes) {
349 break;
350 }
351 }
352
353 return APR_SUCCESS;
354}
355
356
358 struct iovec *vec, int *nvec)
359{
360 int left = *nvec;
361 apr_bucket *e;
362 struct iovec *orig;
364 const char *iov_base;
365 apr_status_t rv;
366
367 orig = vec;
368
369 for (e = APR_BRIGADE_FIRST(b);
372 {
373 if (left-- == 0)
374 break;
375
377 if (rv != APR_SUCCESS)
378 return rv;
379 /* Set indirectly since types differ: */
380 vec->iov_len = iov_len;
381 vec->iov_base = (void *)iov_base;
382 ++vec;
383 }
384
385 *nvec = (int)(vec - orig);
386 return APR_SUCCESS;
387}
388
391 void *ctx,
392 va_list va)
393{
394#define MAX_VECS 8
395 struct iovec vec[MAX_VECS];
396 apr_size_t i = 0;
397
398 for (;;) {
399 char *str = va_arg(va, char *);
400 apr_status_t rv;
401
402 if (str == NULL)
403 break;
404
405 vec[i].iov_base = str;
406 vec[i].iov_len = strlen(str);
407 i++;
408
409 if (i == MAX_VECS) {
410 rv = apr_brigade_writev(b, flush, ctx, vec, i);
411 if (rv != APR_SUCCESS)
412 return rv;
413 i = 0;
414 }
415 }
416 if (i != 0)
417 return apr_brigade_writev(b, flush, ctx, vec, i);
418
419 return APR_SUCCESS;
420}
421
424 const char c)
425{
426 return apr_brigade_write(b, flush, ctx, &c, 1);
427}
428
431 void *ctx,
432 const char *str, apr_size_t nbyte)
433{
436 char *buf = NULL;
437
438 /*
439 * If the last bucket is a heap bucket and its buffer is not shared with
440 * another bucket, we may write into that bucket.
441 */
443 && ((apr_bucket_heap *)(e->data))->refcount.refcount == 1) {
445
446 /* HEAP bucket start offsets are always in-memory, safe to cast */
447 remaining = h->alloc_len - (e->length + (apr_size_t)e->start);
448 buf = h->base + e->start + e->length;
449 }
450
451 if (nbyte > remaining) {
452 /* either a buffer bucket exists but is full,
453 * or no buffer bucket exists and the data is too big
454 * to buffer. In either case, we should flush. */
455 if (flush) {
456 e = apr_bucket_transient_create(str, nbyte, b->bucket_alloc);
458 return flush(b, ctx);
459 }
460 else {
461 e = apr_bucket_heap_create(str, nbyte, NULL, b->bucket_alloc);
463 return APR_SUCCESS;
464 }
465 }
466 else if (!buf) {
467 /* we don't have a buffer, but the data is small enough
468 * that we don't mind making a new buffer */
469 buf = apr_bucket_alloc(APR_BUCKET_BUFF_SIZE, b->bucket_alloc);
471 apr_bucket_free, b->bucket_alloc);
473 e->length = 0; /* We are writing into the brigade, and
474 * allocating more memory than we need. This
475 * ensures that the bucket thinks it is empty just
476 * after we create it. We'll fix the length
477 * once we put data in it below.
478 */
479 }
480
481 /* there is a sufficiently big buffer bucket available now */
482 memcpy(buf, str, nbyte);
483 e->length += nbyte;
484
485 return APR_SUCCESS;
486}
487
490 void *ctx,
491 const struct iovec *vec,
493{
494 apr_bucket *e;
497 char *buf;
498
499 /* Compute the total length of the data to be written.
500 */
501 total_len = 0;
502 for (i = 0; i < nvec; i++) {
503 total_len += vec[i].iov_len;
504 }
505
506 /* If the data to be written is very large, try to convert
507 * the iovec to transient buckets rather than copying.
508 */
510 if (flush) {
511 for (i = 0; i < nvec; i++) {
513 vec[i].iov_len,
514 b->bucket_alloc);
516 }
517 return flush(b, ctx);
518 }
519 else {
520 for (i = 0; i < nvec; i++) {
521 e = apr_bucket_heap_create((const char *) vec[i].iov_base,
522 vec[i].iov_len, NULL,
523 b->bucket_alloc);
525 }
526 return APR_SUCCESS;
527 }
528 }
529
530 i = 0;
531
532 /* If there is a heap bucket at the end of the brigade
533 * already, and its refcount is 1, copy into the existing bucket.
534 */
537 && ((apr_bucket_heap *)(e->data))->refcount.refcount == 1) {
539 apr_size_t remaining = h->alloc_len -
540 (e->length + (apr_size_t)e->start);
541 buf = h->base + e->start + e->length;
542
543 if (remaining >= total_len) {
544 /* Simple case: all the data will fit in the
545 * existing heap bucket
546 */
547 for (; i < nvec; i++) {
548 apr_size_t len = vec[i].iov_len;
549 memcpy(buf, (const void *) vec[i].iov_base, len);
550 buf += len;
551 }
552 e->length += total_len;
553 return APR_SUCCESS;
554 }
555 else {
556 /* More complicated case: not all of the data
557 * will fit in the existing heap bucket. The
558 * total data size is <= APR_BUCKET_BUFF_SIZE,
559 * so we'll need only one additional bucket.
560 */
561 const char *start_buf = buf;
562 for (; i < nvec; i++) {
563 apr_size_t len = vec[i].iov_len;
564 if (len > remaining) {
565 break;
566 }
567 memcpy(buf, (const void *) vec[i].iov_base, len);
568 buf += len;
569 remaining -= len;
570 }
571 e->length += (buf - start_buf);
572 total_len -= (buf - start_buf);
573
574 if (flush) {
575 apr_status_t rv = flush(b, ctx);
576 if (rv != APR_SUCCESS) {
577 return rv;
578 }
579 }
580
581 /* Now fall through into the case below to
582 * allocate another heap bucket and copy the
583 * rest of the array. (Note that i is not
584 * reset to zero here; it holds the index
585 * of the first vector element to be
586 * written to the new bucket.)
587 */
588 }
589 }
590
591 /* Allocate a new heap bucket, and copy the data into it.
592 * The checks above ensure that the amount of data to be
593 * written here is no larger than APR_BUCKET_BUFF_SIZE.
594 */
595 buf = apr_bucket_alloc(APR_BUCKET_BUFF_SIZE, b->bucket_alloc);
597 apr_bucket_free, b->bucket_alloc);
598 for (; i < nvec; i++) {
599 apr_size_t len = vec[i].iov_len;
600 memcpy(buf, (const void *) vec[i].iov_base, len);
601 buf += len;
602 }
603 e->length = total_len;
605
606 return APR_SUCCESS;
607}
608
611 const char *str)
612{
613 return apr_brigade_write(bb, flush, ctx, str, strlen(str));
614}
615
618 void *ctx, ...)
619{
620 va_list va;
621 apr_status_t rv;
622
623 va_start(va, ctx);
625 va_end(va);
626 return rv;
627}
628
631 void *ctx,
632 const char *fmt, ...)
633{
634 va_list ap;
635 apr_status_t rv;
636
637 va_start(ap, fmt);
639 va_end(ap);
640 return rv;
641}
642
645
646 apr_bucket_brigade *b; /* associated brigade */
647 apr_brigade_flush *flusher; /* flushing function */
648 void *ctx;
649
650 char *cbuff; /* buffer to flush from */
651};
652
654{
655 /* callback function passed to ap_vformatter to be
656 * called when vformatter needs to buff and
657 * buff.curpos > buff.endpos
658 */
659
660 /* "downcast," have really passed a brigade_vprintf_data_t* */
663
664 res = apr_brigade_write(vd->b, *vd->flusher, vd->ctx, vd->cbuff,
666
667 if(res != APR_SUCCESS) {
668 return -1;
669 }
670
671 vd->vbuff.curpos = vd->cbuff;
672 vd->vbuff.endpos = vd->cbuff + APR_BUCKET_BUFF_SIZE;
673
674 return res;
675}
676
679 void *ctx,
680 const char *fmt, va_list va)
681{
682 /* the cast, in order of appearance */
685 int written;
686
687 vd.vbuff.curpos = buf;
688 vd.vbuff.endpos = buf + APR_BUCKET_BUFF_SIZE;
689 vd.b = b;
690 vd.flusher = &flush;
691 vd.ctx = ctx;
692 vd.cbuff = buf;
693
694 written = apr_vformatter(brigade_flush, &vd.vbuff, fmt, va);
695
696 if (written == -1) {
697 return -1;
698 }
699
700 /* write out what remains in the buffer */
701 return apr_brigade_write(b, flush, ctx, buf, vd.vbuff.curpos - buf);
702}
703
704/* A "safe" maximum bucket size, 1Gb */
705#define MAX_BUCKET_SIZE (0x40000000)
706
708 apr_file_t *f,
711 apr_pool_t *p)
712{
713 apr_bucket *e;
714
715 if (sizeof(apr_off_t) == sizeof(apr_size_t) || length < MAX_BUCKET_SIZE) {
717 bb->bucket_alloc);
718 }
719 else {
720 /* Several buckets are needed. */
722 bb->bucket_alloc);
723
724 while (length > MAX_BUCKET_SIZE) {
725 apr_bucket *ce;
730 }
731 e->length = (apr_size_t)length; /* Resize just the last bucket */
732 }
733
735 return e;
736}
const char * buff
Definition ap_regex.h:186
const char apr_size_t len
Definition ap_regex.h:187
#define MAX_BUCKET_SIZE
static apr_status_t brigade_flush(apr_vformatter_buff_t *buff)
#define MAX_VECS
static apr_status_t brigade_cleanup(void *data)
Definition apr_brigade.c:32
APR-UTIL Buckets/Bucket Brigades.
APR Error Codes.
APR general purpose library routines.
APR memory allocation.
APU_DECLARE(void)
Computes SipHash-2-4, producing a 64bit (APR_SIPHASH_DSIZE) hash from a message and a 128bit (APR_SIP...
Definition apr_sha1.c:206
APR Strings library.
APR Table library.
APR Standard Headers Support.
int flush
const unsigned char * buf
Definition util_md5.h:50
#define APR_INCOMPLETE
Definition apr_errno.h:452
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_EINVAL
Definition apr_errno.h:711
apr_file_t * f
apr_bucket_brigade apr_read_type_e apr_off_t maxbytes
apr_brigade_flush void const char apr_size_t nbyte
#define APR_BUCKET_REMOVE(e)
#define APR_BUCKET_IS_HEAP(e)
#define APR_BRIGADE_LAST(b)
#define APR_BUCKET_IS_METADATA(e)
#define APR_BRIGADE_INSERT_TAIL(b, e)
#define apr_bucket_split(e, point)
apr_file_t apr_off_t start
#define APR_BUCKET_NEXT(e)
apr_read_type_e
Definition apr_buckets.h:57
apr_bucket * e
int read_all
#define APR_BUCKET_BUFF_SIZE
Definition apr_buckets.h:54
#define APR_BRIGADE_EMPTY(b)
#define APR_BRIGADE_SENTINEL(b)
#define apr_bucket_delete(e)
apr_brigade_flush void * ctx
apr_off_t point
APU_DECLARE_NONSTD(void)
Definition apr_buckets.c:43
#define apr_bucket_copy(e, c)
apr_bucket apr_bucket_brigade * a
#define APR_BRIGADE_FIRST(b)
apr_off_t apr_bucket ** after_point
#define APR_BRIGADE_CHECK_CONSISTENCY(b)
#define apr_bucket_read(e, str, len, block)
int apr_off_t * length
apr_bucket_brigade * bbIn
apr_status_t(* apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx)
apr_brigade_flush void va_list va
#define apr_bucket_destroy(e)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
@ APR_NONBLOCK_READ
Definition apr_buckets.h:59
apr_pool_t apr_dbd_t apr_dbd_results_t ** res
Definition apr_dbd.h:287
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
const apr_array_header_t * list
Definition apr_cstr.h:105
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const struct iovec * vec
void * data
const struct iovec apr_size_t nvec
void * memchr(const void *s, int c, size_t n)
#define APR_ASCII_LF
Definition apr_general.h:63
const apr_hash_t * h
Definition apr_hash.h:97
apr_vformatter_buff_t const char * fmt
Definition apr_lib.h:175
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_vformatter_buff_t const char va_list ap
Definition apr_lib.h:176
apr_pool_t * b
Definition apr_pools.h:529
#define APR_RING_UNSPLICE(ep1, epN, link)
Definition apr_ring.h:368
#define APR_RING_INIT(hp, elem, link)
Definition apr_ring.h:192
#define APR_RING_SPLICE_HEAD(hp, ep1, epN, elem, link)
Definition apr_ring.h:286
#define APR_RING_LAST(hp)
Definition apr_ring.h:171
const char * s
Definition apr_strings.h:95
int int status
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_bucket_alloc_t * bucket_alloc
apr_size_t length
apr_off_t start
apr_bucket_alloc_t * list
void * data
apr_bucket_brigade * b
apr_brigade_flush * flusher
apr_vformatter_buff_t vbuff
#define str
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray