Apache Portable Runtime
apr_buckets.h
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  * @file apr_buckets.h
18  * @brief APR-UTIL Buckets/Bucket Brigades
19  */
20 
21 #ifndef APR_BUCKETS_H
22 #define APR_BUCKETS_H
23 
24 #if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG)
25 #define APR_RING_DEBUG
26 #endif
27 
28 #include "apu.h"
29 #include "apr_network_io.h"
30 #include "apr_file_io.h"
31 #include "apr_general.h"
32 #include "apr_mmap.h"
33 #include "apr_errno.h"
34 #include "apr_ring.h"
35 #include "apr.h"
36 #if APR_HAVE_SYS_UIO_H
37 #include <sys/uio.h> /* for struct iovec */
38 #endif
39 #if APR_HAVE_STDARG_H
40 #include <stdarg.h>
41 #endif
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @defgroup APR_Util_Bucket_Brigades Bucket Brigades
49  * @ingroup APR
50  * @{
51  */
52 
53 /** default bucket buffer size - 8KB minus room for memory allocator headers */
54 #define APR_BUCKET_BUFF_SIZE 8000
55 
56 /** Determines how a bucket or brigade should be read */
57 typedef enum {
58  APR_BLOCK_READ, /**< block until data becomes available */
59  APR_NONBLOCK_READ /**< return immediately if no data is available */
61 
62 /**
63  * The one-sentence buzzword-laden overview: Bucket brigades represent
64  * a complex data stream that can be passed through a layered IO
65  * system without unnecessary copying. A longer overview follows...
66  *
67  * A bucket brigade is a doubly linked list (ring) of buckets, so we
68  * aren't limited to inserting at the front and removing at the end.
69  * Buckets are only passed around as members of a brigade, although
70  * singleton buckets can occur for short periods of time.
71  *
72  * Buckets are data stores of various types. They can refer to data in
73  * memory, or part of a file or mmap area, or the output of a process,
74  * etc. Buckets also have some type-dependent accessor functions:
75  * read, split, copy, setaside, and destroy.
76  *
77  * read returns the address and size of the data in the bucket. If the
78  * data isn't in memory then it is read in and the bucket changes type
79  * so that it can refer to the new location of the data. If all the
80  * data doesn't fit in the bucket then a new bucket is inserted into
81  * the brigade to hold the rest of it.
82  *
83  * split divides the data in a bucket into two regions. After a split
84  * the original bucket refers to the first part of the data and a new
85  * bucket inserted into the brigade after the original bucket refers
86  * to the second part of the data. Reference counts are maintained as
87  * necessary.
88  *
89  * setaside ensures that the data in the bucket has a long enough
90  * lifetime. Sometimes it is convenient to create a bucket referring
91  * to data on the stack in the expectation that it will be consumed
92  * (output to the network) before the stack is unwound. If that
93  * expectation turns out not to be valid, the setaside function is
94  * called to move the data somewhere safer.
95  *
96  * copy makes a duplicate of the bucket structure as long as it's
97  * possible to have multiple references to a single copy of the
98  * data itself. Not all bucket types can be copied.
99  *
100  * destroy maintains the reference counts on the resources used by a
101  * bucket and frees them if necessary.
102  *
103  * Note: all of the above functions have wrapper macros (apr_bucket_read(),
104  * apr_bucket_destroy(), etc), and those macros should be used rather
105  * than using the function pointers directly.
106  *
107  * To write a bucket brigade, they are first made into an iovec, so that we
108  * don't write too little data at one time. Currently we ignore compacting the
109  * buckets into as few buckets as possible, but if we really want good
110  * performance, then we need to compact the buckets before we convert to an
111  * iovec, or possibly while we are converting to an iovec.
112  */
113 
114 /*
115  * Forward declaration of the main types.
116  */
117 
118 /** @see apr_bucket_brigade */
120 /** @see apr_bucket */
121 typedef struct apr_bucket apr_bucket;
122 /** @see apr_bucket_alloc_t */
124 
125 /** @see apr_bucket_type_t */
127 
128 /**
129  * Basic bucket type
130  */
132  /**
133  * The name of the bucket type
134  */
135  const char *name;
136  /**
137  * The number of functions this bucket understands. Can not be less than
138  * five.
139  */
140  int num_func;
141  /**
142  * Whether the bucket contains metadata (ie, information that
143  * describes the regular contents of the brigade). The metadata
144  * is not returned by apr_bucket_read() and is not indicated by
145  * the ->length of the apr_bucket itself. In other words, an
146  * empty bucket is safe to arbitrarily remove if and only if it
147  * contains no metadata. In this sense, "data" is just raw bytes
148  * that are the "content" of the brigade and "metadata" describes
149  * that data but is not a proper part of it.
150  */
151  enum {
152  /** This bucket type represents actual data to send to the client. */
154  /** This bucket type represents metadata. */
156  } is_metadata;
157  /**
158  * Free the private data and any resources used by the bucket (if they
159  * aren't shared with another bucket). This function is required to be
160  * implemented for all bucket types, though it might be a no-op on some
161  * of them (namely ones that never allocate any private data structures).
162  * @param data The private data pointer from the bucket to be destroyed
163  */
164  void (*destroy)(void *data);
165 
166  /**
167  * Read the data from the bucket. This is required to be implemented
168  * for all bucket types.
169  * @param b The bucket to read from
170  * @param str A place to store the data read. Allocation should only be
171  * done if absolutely necessary.
172  * @param len The amount of data read.
173  * @param block Should this read function block if there is more data that
174  * cannot be read immediately.
175  */
176  apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len,
177  apr_read_type_e block);
178 
179  /**
180  * Make it possible to set aside the data for at least as long as the
181  * given pool. Buckets containing data that could potentially die before
182  * this pool (e.g. the data resides on the stack, in a child pool of
183  * the given pool, or in a disjoint pool) must somehow copy, shift, or
184  * transform the data to have the proper lifetime.
185  * @param e The bucket to convert
186  * @remark Some bucket types contain data that will always outlive the
187  * bucket itself. For example no data (EOS and FLUSH), or the data
188  * resides in global, constant memory (IMMORTAL), or the data is on
189  * the heap (HEAP). For these buckets, apr_bucket_setaside_noop can
190  * be used.
191  */
193 
194  /**
195  * Split one bucket in two at the specified position by duplicating
196  * the bucket structure (not the data) and modifying any necessary
197  * start/end/offset information. If it's not possible to do this
198  * for the bucket type (perhaps the length of the data is indeterminate,
199  * as with pipe and socket buckets), then APR_ENOTIMPL is returned.
200  * @param e The bucket to split
201  * @param point The offset of the first byte in the new bucket
202  */
203  apr_status_t (*split)(apr_bucket *e, apr_size_t point);
204 
205  /**
206  * Copy the bucket structure (not the data), assuming that this is
207  * possible for the bucket type. If it's not, APR_ENOTIMPL is returned.
208  * @param e The bucket to copy
209  * @param c Returns a pointer to the new bucket
210  */
212 
213 };
214 
215 /**
216  * apr_bucket structures are allocated on the malloc() heap and
217  * their lifetime is controlled by the parent apr_bucket_brigade
218  * structure. Buckets can move from one brigade to another e.g. by
219  * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has
220  * the same lifetime as the bucket and is freed when the bucket is
221  * destroyed; if the data is shared by more than one bucket (e.g.
222  * after a split) the data is freed when the last bucket goes away.
223  */
224 struct apr_bucket {
225  /** Links to the rest of the brigade */
227  /** The type of bucket. */
229  /** The length of the data in the bucket. This could have been implemented
230  * with a function, but this is an optimization, because the most
231  * common thing to do will be to get the length. If the length is unknown,
232  * the value of this field will be (apr_size_t)(-1).
233  */
234  apr_size_t length;
235  /** The start of the data in the bucket relative to the private base
236  * pointer. The vast majority of bucket types allow a fixed block of
237  * data to be referenced by multiple buckets, each bucket pointing to
238  * a different segment of the data. That segment starts at base+start
239  * and ends at base+start+length.
240  * If the length == (apr_size_t)(-1), then start == -1.
241  */
242  apr_off_t start;
243  /** type-dependent data hangs off this pointer */
244  void *data;
245  /**
246  * Pointer to function used to free the bucket. This function should
247  * always be defined and it should be consistent with the memory
248  * function used to allocate the bucket. For example, if malloc() is
249  * used to allocate the bucket, this pointer should point to free().
250  * @param e Pointer to the bucket being freed
251  */
252  void (*free)(void *e);
253  /** The freelist from which this bucket was allocated */
255 };
256 
257 /** A list of buckets */
259  /** The pool to associate the brigade with. The data is not allocated out
260  * of the pool, but a cleanup is registered with this pool. If the
261  * brigade is destroyed by some mechanism other than pool destruction,
262  * the destroying function is responsible for killing the cleanup.
263  */
265  /** The buckets in the brigade are on this list. */
266  /*
267  * The apr_bucket_list structure doesn't actually need a name tag
268  * because it has no existence independent of struct apr_bucket_brigade;
269  * the ring macros are designed so that you can leave the name tag
270  * argument empty in this situation but apparently the Windows compiler
271  * doesn't like that.
272  */
273  APR_RING_HEAD(apr_bucket_list, apr_bucket) list;
274  /** The freelist from which this bucket was allocated */
276 };
277 
278 
279 /**
280  * Function called when a brigade should be flushed
281  */
283 
284 /*
285  * define APR_BUCKET_DEBUG if you want your brigades to be checked for
286  * validity at every possible instant. this will slow your code down
287  * substantially but is a very useful debugging tool.
288  */
289 #ifdef APR_BUCKET_DEBUG
290 
291 #define APR_BRIGADE_CHECK_CONSISTENCY(b) do { \
292  APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link); \
293  assert(b->p != NULL); \
294  assert(b->bucket_alloc != NULL); \
295 } while (0)
296 
297 #define APR_BUCKET_CHECK_CONSISTENCY(e) \
298  APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
299 
300 #else
301 /**
302  * checks the ring pointers in a bucket brigade for consistency. an
303  * abort() will be triggered if any inconsistencies are found.
304  * note: this is a no-op unless APR_BUCKET_DEBUG is defined.
305  * @param b The brigade
306  */
307 #define APR_BRIGADE_CHECK_CONSISTENCY(b)
308 /**
309  * checks the brigade a bucket is in for ring consistency. an
310  * abort() will be triggered if any inconsistencies are found.
311  * note: this is a no-op unless APR_BUCKET_DEBUG is defined.
312  * @param e The bucket
313  */
314 #define APR_BUCKET_CHECK_CONSISTENCY(e)
315 #endif
316 
317 
318 /**
319  * Wrappers around the RING macros to reduce the verbosity of the code
320  * that handles bucket brigades.
321  */
322 /**
323  * The magic pointer value that indicates the head of the brigade
324  * @remark This is used to find the beginning and end of the brigade, eg:
325  * <pre>
326  * while (e != APR_BRIGADE_SENTINEL(b)) {
327  * ...
328  * e = APR_BUCKET_NEXT(e);
329  * }
330  * </pre>
331  * @param b The brigade
332  * @return The magic pointer value
333  */
334 #define APR_BRIGADE_SENTINEL(b) APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
335 
336 /**
337  * Determine if the bucket brigade is empty
338  * @param b The brigade to check
339  * @return true or false
340  */
341 #define APR_BRIGADE_EMPTY(b) APR_RING_EMPTY(&(b)->list, apr_bucket, link)
342 
343 /**
344  * Return the first bucket in a brigade
345  * @param b The brigade to query
346  * @return The first bucket in the brigade
347  */
348 #define APR_BRIGADE_FIRST(b) APR_RING_FIRST(&(b)->list)
349 /**
350  * Return the last bucket in a brigade
351  * @param b The brigade to query
352  * @return The last bucket in the brigade
353  */
354 #define APR_BRIGADE_LAST(b) APR_RING_LAST(&(b)->list)
355 
356 /**
357  * Insert a single bucket at the front of a brigade
358  * @param b The brigade to add to
359  * @param e The bucket to insert
360  */
361 #define APR_BRIGADE_INSERT_HEAD(b, e) do { \
362  apr_bucket *ap__b = (e); \
363  APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link); \
364  APR_BRIGADE_CHECK_CONSISTENCY((b)); \
365  } while (0)
366 
367 /**
368  * Insert a single bucket at the end of a brigade
369  * @param b The brigade to add to
370  * @param e The bucket to insert
371  */
372 #define APR_BRIGADE_INSERT_TAIL(b, e) do { \
373  apr_bucket *ap__b = (e); \
374  APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link); \
375  APR_BRIGADE_CHECK_CONSISTENCY((b)); \
376  } while (0)
377 
378 /**
379  * Concatenate brigade b onto the end of brigade a, leaving brigade b empty
380  * @param a The first brigade
381  * @param b The second brigade
382  */
383 #define APR_BRIGADE_CONCAT(a, b) do { \
384  APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link); \
385  APR_BRIGADE_CHECK_CONSISTENCY((a)); \
386  } while (0)
387 
388 /**
389  * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty
390  * @param a The first brigade
391  * @param b The second brigade
392  */
393 #define APR_BRIGADE_PREPEND(a, b) do { \
394  APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link); \
395  APR_BRIGADE_CHECK_CONSISTENCY((a)); \
396  } while (0)
397 
398 /**
399  * Insert a single bucket before a specified bucket
400  * @param a The bucket to insert before
401  * @param b The bucket to insert
402  */
403 #define APR_BUCKET_INSERT_BEFORE(a, b) do { \
404  apr_bucket *ap__a = (a), *ap__b = (b); \
405  APR_RING_INSERT_BEFORE(ap__a, ap__b, link); \
406  APR_BUCKET_CHECK_CONSISTENCY(ap__a); \
407  } while (0)
408 
409 /**
410  * Insert a single bucket after a specified bucket
411  * @param a The bucket to insert after
412  * @param b The bucket to insert
413  */
414 #define APR_BUCKET_INSERT_AFTER(a, b) do { \
415  apr_bucket *ap__a = (a), *ap__b = (b); \
416  APR_RING_INSERT_AFTER(ap__a, ap__b, link); \
417  APR_BUCKET_CHECK_CONSISTENCY(ap__a); \
418  } while (0)
419 
420 /**
421  * Get the next bucket in the list
422  * @param e The current bucket
423  * @return The next bucket
424  */
425 #define APR_BUCKET_NEXT(e) APR_RING_NEXT((e), link)
426 /**
427  * Get the previous bucket in the list
428  * @param e The current bucket
429  * @return The previous bucket
430  */
431 #define APR_BUCKET_PREV(e) APR_RING_PREV((e), link)
432 
433 /**
434  * Remove a bucket from its bucket brigade
435  * @param e The bucket to remove
436  */
437 #define APR_BUCKET_REMOVE(e) APR_RING_REMOVE((e), link)
438 
439 /**
440  * Initialize a new bucket's prev/next pointers
441  * @param e The bucket to initialize
442  */
443 #define APR_BUCKET_INIT(e) APR_RING_ELEM_INIT((e), link)
444 
445 /**
446  * Determine if a bucket contains metadata. An empty bucket is
447  * safe to arbitrarily remove if and only if this is false.
448  * @param e The bucket to inspect
449  * @return true or false
450  */
451 #define APR_BUCKET_IS_METADATA(e) ((e)->type->is_metadata)
452 
453 /**
454  * Determine if a bucket is a FLUSH bucket
455  * @param e The bucket to inspect
456  * @return true or false
457  */
458 #define APR_BUCKET_IS_FLUSH(e) ((e)->type == &apr_bucket_type_flush)
459 /**
460  * Determine if a bucket is an EOS bucket
461  * @param e The bucket to inspect
462  * @return true or false
463  */
464 #define APR_BUCKET_IS_EOS(e) ((e)->type == &apr_bucket_type_eos)
465 /**
466  * Determine if a bucket is a FILE bucket
467  * @param e The bucket to inspect
468  * @return true or false
469  */
470 #define APR_BUCKET_IS_FILE(e) ((e)->type == &apr_bucket_type_file)
471 /**
472  * Determine if a bucket is a PIPE bucket
473  * @param e The bucket to inspect
474  * @return true or false
475  */
476 #define APR_BUCKET_IS_PIPE(e) ((e)->type == &apr_bucket_type_pipe)
477 /**
478  * Determine if a bucket is a SOCKET bucket
479  * @param e The bucket to inspect
480  * @return true or false
481  */
482 #define APR_BUCKET_IS_SOCKET(e) ((e)->type == &apr_bucket_type_socket)
483 /**
484  * Determine if a bucket is a HEAP bucket
485  * @param e The bucket to inspect
486  * @return true or false
487  */
488 #define APR_BUCKET_IS_HEAP(e) ((e)->type == &apr_bucket_type_heap)
489 /**
490  * Determine if a bucket is a TRANSIENT bucket
491  * @param e The bucket to inspect
492  * @return true or false
493  */
494 #define APR_BUCKET_IS_TRANSIENT(e) ((e)->type == &apr_bucket_type_transient)
495 /**
496  * Determine if a bucket is a IMMORTAL bucket
497  * @param e The bucket to inspect
498  * @return true or false
499  */
500 #define APR_BUCKET_IS_IMMORTAL(e) ((e)->type == &apr_bucket_type_immortal)
501 #if APR_HAS_MMAP
502 /**
503  * Determine if a bucket is a MMAP bucket
504  * @param e The bucket to inspect
505  * @return true or false
506  */
507 #define APR_BUCKET_IS_MMAP(e) ((e)->type == &apr_bucket_type_mmap)
508 #endif
509 /**
510  * Determine if a bucket is a POOL bucket
511  * @param e The bucket to inspect
512  * @return true or false
513  */
514 #define APR_BUCKET_IS_POOL(e) ((e)->type == &apr_bucket_type_pool)
515 
516 /*
517  * General-purpose reference counting for the various bucket types.
518  *
519  * Any bucket type that keeps track of the resources it uses (i.e.
520  * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
521  * attach a reference count to the resource so that it can be freed
522  * when the last bucket that uses it goes away. Resource-sharing may
523  * occur because of bucket splits or buckets that refer to globally
524  * cached data. */
525 
526 /** @see apr_bucket_refcount */
528 /**
529  * The structure used to manage the shared resource must start with an
530  * apr_bucket_refcount which is updated by the general-purpose refcount
531  * code. A pointer to the bucket-type-dependent private data structure
532  * can be cast to a pointer to an apr_bucket_refcount and vice versa.
533  */
535  /** The number of references to this bucket */
536  int refcount;
537 };
538 
539 /* ***** Reference-counted bucket types ***** */
540 
541 /** @see apr_bucket_heap */
543 /**
544  * A bucket referring to data allocated off the heap.
545  */
547  /** Number of buckets using this memory */
549  /** The start of the data actually allocated. This should never be
550  * modified, it is only used to free the bucket.
551  */
552  char *base;
553  /** how much memory was allocated */
554  apr_size_t alloc_len;
555  /** function to use to delete the data */
556  void (*free_func)(void *data);
557 };
558 
559 /** @see apr_bucket_pool */
561 /**
562  * A bucket referring to data allocated from a pool
563  */
565  /** The pool bucket must be able to be easily morphed to a heap
566  * bucket if the pool gets cleaned up before all references are
567  * destroyed. This apr_bucket_heap structure is populated automatically
568  * when the pool gets cleaned up, and subsequent calls to pool_read()
569  * will result in the apr_bucket in question being morphed into a
570  * regular heap bucket. (To avoid having to do many extra refcount
571  * manipulations and b->data manipulations, the apr_bucket_pool
572  * struct actually *contains* the apr_bucket_heap struct that it
573  * will become as its first element; the two share their
574  * apr_bucket_refcount members.)
575  */
577  /** The block of data actually allocated from the pool.
578  * Segments of this block are referenced by adjusting
579  * the start and length of the apr_bucket accordingly.
580  * This will be NULL after the pool gets cleaned up.
581  */
582  const char *base;
583  /** The pool the data was allocated from. When the pool
584  * is cleaned up, this gets set to NULL as an indicator
585  * to pool_read() that the data is now on the heap and
586  * so it should morph the bucket into a regular heap
587  * bucket before continuing.
588  */
590  /** The freelist this structure was allocated from, which is
591  * needed in the cleanup phase in order to allocate space on the heap
592  */
594 };
595 
596 #if APR_HAS_MMAP
597 /** @see apr_bucket_mmap */
599 /**
600  * A bucket referring to an mmap()ed file
601  */
603  /** Number of buckets using this memory */
605  /** The mmap this sub_bucket refers to */
607 };
608 #endif
609 
610 /** @see apr_bucket_file */
612 /**
613  * A bucket referring to an file
614  */
616  /** Number of buckets using this memory */
618  /** The file this bucket refers to */
620  /** The pool into which any needed structures should
621  * be created while reading from this file bucket */
623 #if APR_HAS_MMAP
624  /** Whether this bucket should be memory-mapped if
625  * a caller tries to read from it */
626  int can_mmap;
627 #endif /* APR_HAS_MMAP */
628 };
629 
630 /** @see apr_bucket_structs */
632 /**
633  * A union of all bucket structures so we know what
634  * the max size is.
635  */
637  apr_bucket b; /**< Bucket */
638  apr_bucket_heap heap; /**< Heap */
639  apr_bucket_pool pool; /**< Pool */
640 #if APR_HAS_MMAP
641  apr_bucket_mmap mmap; /**< MMap */
642 #endif
643  apr_bucket_file file; /**< File */
644 };
645 
646 /**
647  * The amount that apr_bucket_alloc() should allocate in the common case.
648  * Note: this is twice as big as apr_bucket_structs to allow breathing
649  * room for third-party bucket types.
650  */
651 #define APR_BUCKET_ALLOC_SIZE APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
652 
653 /* ***** Bucket Brigade Functions ***** */
654 /**
655  * Create a new bucket brigade. The bucket brigade is originally empty.
656  * @param p The pool to associate with the brigade. Data is not allocated out
657  * of the pool, but a cleanup is registered.
658  * @param list The bucket allocator to use
659  * @return The empty bucket brigade
660  */
662  apr_bucket_alloc_t *list)
663  __attribute__((nonnull(1,2)));
664 
665 /**
666  * Destroy an entire bucket brigade. This includes destroying all of the
667  * buckets within the bucket brigade's bucket list.
668  * @param b The bucket brigade to destroy
669  */
671  __attribute__((nonnull(1)));
672 
673 /**
674  * Empty out an entire bucket brigade. This includes destroying all of the
675  * buckets within the bucket brigade's bucket list. This is similar to
676  * apr_brigade_destroy(), except that it does not deregister the brigade's
677  * pool cleanup function.
678  * @param data The bucket brigade to clean up
679  * @remark Generally, you should use apr_brigade_destroy(). This function
680  * can be useful in situations where you have a single brigade that
681  * you wish to reuse many times by destroying all of the buckets in
682  * the brigade and putting new buckets into it later.
683  */
685  __attribute__((nonnull(1)));
686 
687 /**
688  * Move the buckets from the tail end of the existing brigade @a b into
689  * the brigade @a a. If @a a is NULL a new brigade is created. Buckets
690  * from @a e to the last bucket (inclusively) of brigade @a b are moved
691  * from @a b to the returned brigade @a a.
692  *
693  * @param b The brigade to split
694  * @param e The first bucket to move
695  * @param a The brigade which should be used for the result or NULL if
696  * a new brigade should be created. The brigade @a a will be
697  * cleared if it is not empty.
698  * @return The brigade supplied in @a a or a new one if @a a was NULL.
699  * @warning Note that this function allocates a new brigade if @a a is
700  * NULL so memory consumption should be carefully considered.
701  */
703  apr_bucket *e,
705  __attribute__((nonnull(1,2)));
706 
707 /**
708  * Create a new bucket brigade and move the buckets from the tail end
709  * of an existing brigade into the new brigade. Buckets from
710  * @a e to the last bucket (inclusively) of brigade @a b
711  * are moved from @a b to the returned brigade.
712  * @param b The brigade to split
713  * @param e The first bucket to move
714  * @return The new brigade
715  * @warning Note that this function always allocates a new brigade
716  * so memory consumption should be carefully considered.
717  */
719  apr_bucket *e)
720  __attribute__((nonnull(1,2)));
721 
722 /**
723  * Partition a bucket brigade at a given offset (in bytes from the start of
724  * the brigade). This is useful whenever a filter wants to use known ranges
725  * of bytes from the brigade; the ranges can even overlap.
726  * @param b The brigade to partition
727  * @param point The offset at which to partition the brigade
728  * @param after_point Returns a pointer to the first bucket after the partition
729  * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the
730  * brigade were shorter than @a point, or an error code.
731  * @remark if APR_INCOMPLETE is returned, @a after_point will be set to
732  * the brigade sentinel.
733  */
735  apr_off_t point,
736  apr_bucket **after_point)
737  __attribute__((nonnull(1,3)));
738 
739 /**
740  * Return the total length of the brigade.
741  * @param bb The brigade to compute the length of
742  * @param read_all Read unknown-length buckets to force a size
743  * @param length Returns the length of the brigade (up to the end, or up
744  * to a bucket read error), or -1 if the brigade has buckets
745  * of indeterminate length and read_all is 0.
746  */
748  int read_all,
749  apr_off_t *length)
750  __attribute__((nonnull(1,3)));
751 
752 /**
753  * Take a bucket brigade and store the data in a flat char*
754  * @param bb The bucket brigade to create the char* from
755  * @param c The char* to write into
756  * @param len The maximum length of the char array. On return, it is the
757  * actual length of the char array.
758  */
760  char *c,
761  apr_size_t *len)
762  __attribute__((nonnull(1,2,3)));
763 
764 /**
765  * Creates a pool-allocated string representing a flat bucket brigade
766  * @param bb The bucket brigade to create the char array from
767  * @param c On return, the allocated char array
768  * @param len On return, the length of the char array.
769  * @param pool The pool to allocate the string from.
770  */
772  char **c,
773  apr_size_t *len,
774  apr_pool_t *pool)
775  __attribute__((nonnull(1,2,3,4)));
776 
777 /**
778  * Split a brigade to represent one LF line.
779  * @param bbOut The bucket brigade that will have the LF line appended to.
780  * @param bbIn The input bucket brigade to search for a LF-line.
781  * @param block The blocking mode to be used to split the line.
782  * @param maxbytes The maximum bytes to read. If this many bytes are seen
783  * without a LF, the brigade will contain a partial line.
784  */
786  apr_bucket_brigade *bbIn,
787  apr_read_type_e block,
788  apr_off_t maxbytes)
789  __attribute__((nonnull(1,2)));
790 
791 /**
792  * Create an iovec of the elements in a bucket_brigade... return number
793  * of elements used. This is useful for writing to a file or to the
794  * network efficiently.
795  * @param b The bucket brigade to create the iovec from
796  * @param vec The iovec to create
797  * @param nvec The number of elements in the iovec. On return, it is the
798  * number of iovec elements actually filled out.
799  */
801  struct iovec *vec, int *nvec)
802  __attribute__((nonnull(1,2,3)));
803 
804 /**
805  * This function writes a list of strings into a bucket brigade.
806  * @param b The bucket brigade to add to
807  * @param flush The flush function to use if the brigade is full
808  * @param ctx The structure to pass to the flush function
809  * @param va A list of strings to add
810  * @return APR_SUCCESS or error code.
811  */
813  apr_brigade_flush flush,
814  void *ctx,
815  va_list va)
816  __attribute__((nonnull(1)));
817 
818 /**
819  * This function writes a string into a bucket brigade.
820  *
821  * The apr_brigade_write function attempts to be efficient with the
822  * handling of heap buckets. Regardless of the amount of data stored
823  * inside a heap bucket, heap buckets are a fixed size to promote their
824  * reuse.
825  *
826  * If an attempt is made to write a string to a brigade that already
827  * ends with a heap bucket, this function will attempt to pack the
828  * string into the remaining space in the previous heap bucket, before
829  * allocating a new heap bucket.
830  *
831  * This function always returns APR_SUCCESS, unless a flush function is
832  * passed, in which case the return value of the flush function will be
833  * returned if used.
834  * @param b The bucket brigade to add to
835  * @param flush The flush function to use if the brigade is full
836  * @param ctx The structure to pass to the flush function
837  * @param str The string to add
838  * @param nbyte The number of bytes to write
839  * @return APR_SUCCESS or error code
840  */
842  apr_brigade_flush flush, void *ctx,
843  const char *str, apr_size_t nbyte)
844  __attribute__((nonnull(1,4)));
845 
846 /**
847  * This function writes multiple strings into a bucket brigade.
848  * @param b The bucket brigade to add to
849  * @param flush The flush function to use if the brigade is full
850  * @param ctx The structure to pass to the flush function
851  * @param vec The strings to add (address plus length for each)
852  * @param nvec The number of entries in iovec
853  * @return APR_SUCCESS or error code
854  */
856  apr_brigade_flush flush,
857  void *ctx,
858  const struct iovec *vec,
859  apr_size_t nvec)
860  __attribute__((nonnull(1,4)));
861 
862 /**
863  * This function writes a string into a bucket brigade.
864  * @param bb The bucket brigade to add to
865  * @param flush The flush function to use if the brigade is full
866  * @param ctx The structure to pass to the flush function
867  * @param str The string to add
868  * @return APR_SUCCESS or error code
869  */
871  apr_brigade_flush flush, void *ctx,
872  const char *str)
873  __attribute__((nonnull(1,4)));
874 
875 /**
876  * This function writes a character into a bucket brigade.
877  * @param b The bucket brigade to add to
878  * @param flush The flush function to use if the brigade is full
879  * @param ctx The structure to pass to the flush function
880  * @param c The character to add
881  * @return APR_SUCCESS or error code
882  */
884  apr_brigade_flush flush, void *ctx,
885  const char c)
886  __attribute__((nonnull(1)));
887 
888 /**
889  * This function writes an unspecified number of strings into a bucket brigade.
890  * @param b The bucket brigade to add to
891  * @param flush The flush function to use if the brigade is full
892  * @param ctx The structure to pass to the flush function
893  * @param ... The strings to add. The final string must be NULL
894  * @return APR_SUCCESS or error code
895  */
897  apr_brigade_flush flush,
898  void *ctx, ...)
899 #if defined(__GNUC__) && __GNUC__ >= 4
900  __attribute__((sentinel))
901 #endif
902  __attribute__((nonnull(1)))
903  ;
904 
905 /**
906  * Evaluate a printf and put the resulting string at the end
907  * of the bucket brigade.
908  * @param b The brigade to write to
909  * @param flush The flush function to use if the brigade is full
910  * @param ctx The structure to pass to the flush function
911  * @param fmt The format of the string to write
912  * @param ... The arguments to fill out the format
913  * @return APR_SUCCESS or error code
914  */
916  apr_brigade_flush flush,
917  void *ctx,
918  const char *fmt, ...)
919  __attribute__((format(printf,4,5)))
920  __attribute__((nonnull(1)));
921 
922 /**
923  * Evaluate a printf and put the resulting string at the end
924  * of the bucket brigade.
925  * @param b The brigade to write to
926  * @param flush The flush function to use if the brigade is full
927  * @param ctx The structure to pass to the flush function
928  * @param fmt The format of the string to write
929  * @param va The arguments to fill out the format
930  * @return APR_SUCCESS or error code
931  */
933  apr_brigade_flush flush,
934  void *ctx,
935  const char *fmt, va_list va)
936  __attribute__((nonnull(1,4)));
937 
938 /**
939  * Utility function to insert a file (or a segment of a file) onto the
940  * end of the brigade. The file is split into multiple buckets if it
941  * is larger than the maximum size which can be represented by a
942  * single bucket.
943  * @param bb the brigade to insert into
944  * @param f the file to insert
945  * @param start the offset of the start of the segment
946  * @param len the length of the segment of the file to insert
947  * @param p pool from which file buckets are allocated
948  * @return the last bucket inserted
949  */
951  apr_file_t *f,
952  apr_off_t start,
953  apr_off_t len,
954  apr_pool_t *p)
955  __attribute__((nonnull(1,2,5)));
956 
957 
958 
959 /* ***** Bucket freelist functions ***** */
960 /**
961  * Create a bucket allocator.
962  * @param p This pool's underlying apr_allocator_t is used to allocate memory
963  * for the bucket allocator. When the pool is destroyed, the bucket
964  * allocator's cleanup routine will free all memory that has been
965  * allocated from it.
966  * @remark The reason the allocator gets its memory from the pool's
967  * apr_allocator_t rather than from the pool itself is because
968  * the bucket allocator will free large memory blocks back to the
969  * allocator when it's done with them, thereby preventing memory
970  * footprint growth that would occur if we allocated from the pool.
971  * @warning The allocator must never be used by more than one thread at a time.
972  */
974 
975 /**
976  * Create a bucket allocator.
977  * @param allocator This apr_allocator_t is used to allocate both the bucket
978  * allocator and all memory handed out by the bucket allocator. The
979  * caller is responsible for destroying the bucket allocator and the
980  * apr_allocator_t -- no automatic cleanups will happen.
981  * @warning The allocator must never be used by more than one thread at a time.
982  */
984  apr_allocator_t *allocator)
985  __attribute__((nonnull(1)));
986 
987 /**
988  * Destroy a bucket allocator.
989  * @param list The allocator to be destroyed
990  */
992  __attribute__((nonnull(1)));
993 
994 /**
995  * Allocate memory for use by the buckets.
996  * @param size The amount to allocate.
997  * @param list The allocator from which to allocate the memory.
998  */
999 APR_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size,
1000  apr_bucket_alloc_t *list)
1001  __attribute__((nonnull(2)));
1002 
1003 /**
1004  * Free memory previously allocated with apr_bucket_alloc().
1005  * @param block The block of memory to be freed.
1006  */
1007 APR_DECLARE_NONSTD(void) apr_bucket_free(void *block)
1008  __attribute__((nonnull(1)));
1009 
1010 
1011 /* ***** Bucket Functions ***** */
1012 /**
1013  * Free the resources used by a bucket. If multiple buckets refer to
1014  * the same resource it is freed when the last one goes away.
1015  * @see apr_bucket_delete()
1016  * @param e The bucket to destroy
1017  */
1018 #define apr_bucket_destroy(e) do { \
1019  (e)->type->destroy((e)->data); \
1020  (e)->free(e); \
1021  } while (0)
1022 
1023 /**
1024  * Delete a bucket by removing it from its brigade (if any) and then
1025  * destroying it.
1026  * @remark This mainly acts as an aid in avoiding code verbosity. It is
1027  * the preferred exact equivalent to:
1028  * <pre>
1029  * APR_BUCKET_REMOVE(e);
1030  * apr_bucket_destroy(e);
1031  * </pre>
1032  * @param e The bucket to delete
1033  */
1034 #define apr_bucket_delete(e) do { \
1035  APR_BUCKET_REMOVE(e); \
1036  apr_bucket_destroy(e); \
1037  } while (0)
1038 
1039 /**
1040  * Read some data from the bucket.
1041  *
1042  * The apr_bucket_read function returns a convenient amount of data
1043  * from the bucket provided, writing the address and length of the
1044  * data to the pointers provided by the caller. The function tries
1045  * as hard as possible to avoid a memory copy.
1046  *
1047  * Buckets are expected to be a member of a brigade at the time they
1048  * are read.
1049  *
1050  * In typical application code, buckets are read in a loop, and after
1051  * each bucket is read and processed, it is moved or deleted from the
1052  * brigade and the next bucket read.
1053  *
1054  * The definition of "convenient" depends on the type of bucket that
1055  * is being read, and is decided by APR. In the case of memory based
1056  * buckets such as heap and immortal buckets, a pointer will be
1057  * returned to the location of the buffer containing the complete
1058  * contents of the bucket.
1059  *
1060  * Some buckets, such as the socket bucket, might have no concept
1061  * of length. If an attempt is made to read such a bucket, the
1062  * apr_bucket_read function will read a convenient amount of data
1063  * from the socket. The socket bucket is magically morphed into a
1064  * heap bucket containing the just-read data, and a new socket bucket
1065  * is inserted just after this heap bucket.
1066  *
1067  * To understand why apr_bucket_read might do this, consider the loop
1068  * described above to read and process buckets. The current bucket
1069  * is magically morphed into a heap bucket and returned to the caller.
1070  * The caller processes the data, and deletes the heap bucket, moving
1071  * onto the next bucket, the new socket bucket. This process repeats,
1072  * giving the illusion of a bucket brigade that contains potentially
1073  * infinite amounts of data. It is up to the caller to decide at what
1074  * point to stop reading buckets.
1075  *
1076  * Some buckets, such as the file bucket, might have a fixed size,
1077  * but be significantly larger than is practical to store in RAM in
1078  * one go. As with the socket bucket, if an attempt is made to read
1079  * from a file bucket, the file bucket is magically morphed into a
1080  * heap bucket containing a convenient amount of data read from the
1081  * current offset in the file. During the read, the offset will be
1082  * moved forward on the file, and a new file bucket will be inserted
1083  * directly after the current bucket representing the remainder of the
1084  * file. If the heap bucket was large enough to store the whole
1085  * remainder of the file, no more file buckets are inserted, and the
1086  * file bucket will disappear completely.
1087  *
1088  * The pattern for reading buckets described above does create the
1089  * illusion that the code is willing to swallow buckets that might be
1090  * too large for the system to handle in one go. This however is just
1091  * an illusion: APR will always ensure that large (file) or infinite
1092  * (socket) buckets are broken into convenient bite sized heap buckets
1093  * before data is returned to the caller.
1094  *
1095  * There is a potential gotcha to watch for: if buckets are read in a
1096  * loop, and aren't deleted after being processed, the potentially large
1097  * bucket will slowly be converted into RAM resident heap buckets. If
1098  * the file is larger than available RAM, an out of memory condition
1099  * could be caused if the application is not careful to manage this.
1100  *
1101  * @param e The bucket to read from
1102  * @param str The location to store a pointer to the data in
1103  * @param len The location to store the amount of data read
1104  * @param block Whether the read function blocks
1105  */
1106 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
1107 
1108 /**
1109  * Setaside data so that stack data is not destroyed on returning from
1110  * the function
1111  * @param e The bucket to setaside
1112  * @param p The pool to setaside into
1113  */
1114 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
1115 
1116 /**
1117  * Split one bucket in two at the point provided.
1118  *
1119  * Once split, the original bucket becomes the first of the two new buckets.
1120  *
1121  * (It is assumed that the bucket is a member of a brigade when this
1122  * function is called).
1123  * @param e The bucket to split
1124  * @param point The offset to split the bucket at
1125  */
1126 #define apr_bucket_split(e,point) (e)->type->split(e, point)
1127 
1128 /**
1129  * Copy a bucket.
1130  * @param e The bucket to copy
1131  * @param c Returns a pointer to the new bucket
1132  */
1133 #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
1134 
1135 /* Bucket type handling */
1136 
1137 /**
1138  * This function simply returns APR_SUCCESS to denote that the bucket does
1139  * not require anything to happen for its setaside() function. This is
1140  * appropriate for buckets that have "immortal" data -- the data will live
1141  * at least as long as the bucket.
1142  * @param data The bucket to setaside
1143  * @param pool The pool defining the desired lifetime of the bucket data
1144  * @return APR_SUCCESS
1145  */
1147  apr_pool_t *pool);
1148 
1149 /**
1150  * A place holder function that signifies that the setaside function was not
1151  * implemented for this bucket
1152  * @param data The bucket to setaside
1153  * @param pool The pool defining the desired lifetime of the bucket data
1154  * @return APR_ENOTIMPL
1155  */
1157  apr_pool_t *pool);
1158 
1159 /**
1160  * A place holder function that signifies that the split function was not
1161  * implemented for this bucket
1162  * @param data The bucket to split
1163  * @param point The location to split the bucket
1164  * @return APR_ENOTIMPL
1165  */
1167  apr_size_t point);
1168 
1169 /**
1170  * A place holder function that signifies that the copy function was not
1171  * implemented for this bucket
1172  * @param e The bucket to copy
1173  * @param c Returns a pointer to the new bucket
1174  * @return APR_ENOTIMPL
1175  */
1177  apr_bucket **c);
1178 
1179 /**
1180  * A place holder function that signifies that this bucket does not need
1181  * to do anything special to be destroyed. That's only the case for buckets
1182  * that either have no data (metadata buckets) or buckets whose data pointer
1183  * points to something that's not a bucket-type-specific structure, as with
1184  * simple buckets where data points to a string and pipe buckets where data
1185  * points directly to the apr_file_t.
1186  * @param data The bucket data to destroy
1187  */
1189 
1190 /**
1191  * There is no apr_bucket_destroy_notimpl, because destruction is required
1192  * to be implemented (it could be a noop, but only if that makes sense for
1193  * the bucket type)
1194  */
1195 
1196 /* There is no apr_bucket_read_notimpl, because it is a required function
1197  */
1198 
1199 
1200 /* All of the bucket types implemented by the core */
1201 /**
1202  * The flush bucket type. This signifies that all data should be flushed to
1203  * the next filter. The flush bucket should be sent with the other buckets.
1204  */
1206 /**
1207  * The EOS bucket type. This signifies that there will be no more data, ever.
1208  * All filters MUST send all data to the next filter when they receive a
1209  * bucket of this type
1210  */
1212 /**
1213  * The FILE bucket type. This bucket represents a file on disk
1214  */
1216 /**
1217  * The HEAP bucket type. This bucket represents a data allocated from the
1218  * heap.
1219  */
1221 #if APR_HAS_MMAP
1222 /**
1223  * The MMAP bucket type. This bucket represents an MMAP'ed file
1224  */
1226 #endif
1227 /**
1228  * The POOL bucket type. This bucket represents a data that was allocated
1229  * from a pool. IF this bucket is still available when the pool is cleared,
1230  * the data is copied on to the heap.
1231  */
1233 /**
1234  * The PIPE bucket type. This bucket represents a pipe to another program.
1235  */
1237 /**
1238  * The IMMORTAL bucket type. This bucket represents a segment of data that
1239  * the creator is willing to take responsibility for. The core will do
1240  * nothing with the data in an immortal bucket
1241  */
1243 /**
1244  * The TRANSIENT bucket type. This bucket represents a data allocated off
1245  * the stack. When the setaside function is called, this data is copied on
1246  * to the heap
1247  */
1249 /**
1250  * The SOCKET bucket type. This bucket represents a socket to another machine
1251  */
1253 
1254 
1255 /* ***** Simple buckets ***** */
1256 
1257 /**
1258  * Split a simple bucket into two at the given point. Most non-reference
1259  * counting buckets that allow multiple references to the same block of
1260  * data (eg transient and immortal) will use this as their split function
1261  * without any additional type-specific handling.
1262  * @param b The bucket to be split
1263  * @param point The offset of the first byte in the new bucket
1264  * @return APR_EINVAL if the point is not within the bucket;
1265  * APR_ENOMEM if allocation failed;
1266  * or APR_SUCCESS
1267  */
1269  apr_size_t point);
1270 
1271 /**
1272  * Copy a simple bucket. Most non-reference-counting buckets that allow
1273  * multiple references to the same block of data (eg transient and immortal)
1274  * will use this as their copy function without any additional type-specific
1275  * handling.
1276  * @param a The bucket to copy
1277  * @param b Returns a pointer to the new bucket
1278  * @return APR_ENOMEM if allocation failed;
1279  * or APR_SUCCESS
1280  */
1282  apr_bucket **b);
1283 
1284 
1285 /* ***** Shared, reference-counted buckets ***** */
1286 
1287 /**
1288  * Initialize a bucket containing reference-counted data that may be
1289  * shared. The caller must allocate the bucket if necessary and
1290  * initialize its type-dependent fields, and allocate and initialize
1291  * its own private data structure. This function should only be called
1292  * by type-specific bucket creation functions.
1293  * @param b The bucket to initialize
1294  * @param data A pointer to the private data structure
1295  * with the reference count at the start
1296  * @param start The start of the data in the bucket
1297  * relative to the private base pointer
1298  * @param length The length of the data in the bucket
1299  * @return The new bucket, or NULL if allocation failed
1300  */
1302  apr_off_t start,
1303  apr_size_t length);
1304 
1305 /**
1306  * Decrement the refcount of the data in the bucket. This function
1307  * should only be called by type-specific bucket destruction functions.
1308  * @param data The private data pointer from the bucket to be destroyed
1309  * @return TRUE or FALSE; TRUE if the reference count is now
1310  * zero, indicating that the shared resource itself can
1311  * be destroyed by the caller.
1312  */
1313 APR_DECLARE(int) apr_bucket_shared_destroy(void *data);
1314 
1315 /**
1316  * Split a bucket into two at the given point, and adjust the refcount
1317  * to the underlying data. Most reference-counting bucket types will
1318  * be able to use this function as their split function without any
1319  * additional type-specific handling.
1320  * @param b The bucket to be split
1321  * @param point The offset of the first byte in the new bucket
1322  * @return APR_EINVAL if the point is not within the bucket;
1323  * APR_ENOMEM if allocation failed;
1324  * or APR_SUCCESS
1325  */
1327  apr_size_t point);
1328 
1329 /**
1330  * Copy a refcounted bucket, incrementing the reference count. Most
1331  * reference-counting bucket types will be able to use this function
1332  * as their copy function without any additional type-specific handling.
1333  * @param a The bucket to copy
1334  * @param b Returns a pointer to the new bucket
1335  * @return APR_ENOMEM if allocation failed;
1336  or APR_SUCCESS
1337  */
1339  apr_bucket **b);
1340 
1341 
1342 /* ***** Functions to Create Buckets of varying types ***** */
1343 /*
1344  * Each bucket type foo has two initialization functions:
1345  * apr_bucket_foo_make which sets up some already-allocated memory as a
1346  * bucket of type foo; and apr_bucket_foo_create which allocates memory
1347  * for the bucket, calls apr_bucket_make_foo, and initializes the
1348  * bucket's list pointers. The apr_bucket_foo_make functions are used
1349  * inside the bucket code to change the type of buckets in place;
1350  * other code should call apr_bucket_foo_create. All the initialization
1351  * functions change nothing if they fail.
1352  */
1353 
1354 /**
1355  * Create an End of Stream bucket. This indicates that there is no more data
1356  * coming from down the filter stack. All filters should flush at this point.
1357  * @param list The freelist from which this bucket should be allocated
1358  * @return The new bucket, or NULL if allocation failed
1359  */
1361 
1362 /**
1363  * Make the bucket passed in an EOS bucket. This indicates that there is no
1364  * more data coming from down the filter stack. All filters should flush at
1365  * this point.
1366  * @param b The bucket to make into an EOS bucket
1367  * @return The new bucket, or NULL if allocation failed
1368  */
1370  __attribute__((nonnull(1)));
1371 
1372 /**
1373  * Create a flush bucket. This indicates that filters should flush their
1374  * data. There is no guarantee that they will flush it, but this is the
1375  * best we can do.
1376  * @param list The freelist from which this bucket should be allocated
1377  * @return The new bucket, or NULL if allocation failed
1378  */
1380  __attribute__((nonnull(1)));
1381 
1382 /**
1383  * Make the bucket passed in a FLUSH bucket. This indicates that filters
1384  * should flush their data. There is no guarantee that they will flush it,
1385  * but this is the best we can do.
1386  * @param b The bucket to make into a FLUSH bucket
1387  * @return The new bucket, or NULL if allocation failed
1388  */
1390  __attribute__((nonnull(1)));
1391 
1392 /**
1393  * Create a bucket referring to long-lived data.
1394  * @param buf The data to insert into the bucket
1395  * @param nbyte The size of the data to insert.
1396  * @param list The freelist from which this bucket should be allocated
1397  * @return The new bucket, or NULL if allocation failed
1398  */
1400  apr_size_t nbyte,
1401  apr_bucket_alloc_t *list)
1402  __attribute__((nonnull(1,3)));
1403 
1404 /**
1405  * Make the bucket passed in a bucket refer to long-lived data
1406  * @param b The bucket to make into a IMMORTAL bucket
1407  * @param buf The data to insert into the bucket
1408  * @param nbyte The size of the data to insert.
1409  * @return The new bucket, or NULL if allocation failed
1410  */
1412  const char *buf,
1413  apr_size_t nbyte)
1414  __attribute__((nonnull(1,2)));
1415 
1416 /**
1417  * Create a bucket referring to data on the stack.
1418  * @param buf The data to insert into the bucket
1419  * @param nbyte The size of the data to insert.
1420  * @param list The freelist from which this bucket should be allocated
1421  * @return The new bucket, or NULL if allocation failed
1422  */
1424  apr_size_t nbyte,
1425  apr_bucket_alloc_t *list)
1426  __attribute__((nonnull(1,3)));
1427 
1428 /**
1429  * Make the bucket passed in a bucket refer to stack data
1430  * @param b The bucket to make into a TRANSIENT bucket
1431  * @param buf The data to insert into the bucket
1432  * @param nbyte The size of the data to insert.
1433  * @return The new bucket, or NULL if allocation failed
1434  */
1436  const char *buf,
1437  apr_size_t nbyte)
1438  __attribute__((nonnull(1,2)));
1439 
1440 /**
1441  * Create a bucket referring to memory on the heap. If the caller asks
1442  * for the data to be copied, this function always allocates 4K of
1443  * memory so that more data can be added to the bucket without
1444  * requiring another allocation. Therefore not all the data may be put
1445  * into the bucket. If copying is not requested then the bucket takes
1446  * over responsibility for free()ing the memory.
1447  * @param buf The buffer to insert into the bucket
1448  * @param nbyte The size of the buffer to insert.
1449  * @param free_func Function to use to free the data; NULL indicates that the
1450  * bucket should make a copy of the data
1451  * @param list The freelist from which this bucket should be allocated
1452  * @return The new bucket, or NULL if allocation failed
1453  */
1454 APR_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
1455  apr_size_t nbyte,
1456  void (*free_func)(void *data),
1457  apr_bucket_alloc_t *list)
1458  __attribute__((nonnull(1,4)));
1459 /**
1460  * Make the bucket passed in a bucket refer to heap data
1461  * @param b The bucket to make into a HEAP bucket
1462  * @param buf The buffer to insert into the bucket
1463  * @param nbyte The size of the buffer to insert.
1464  * @param free_func Function to use to free the data; NULL indicates that the
1465  * bucket should make a copy of the data
1466  * @return The new bucket, or NULL if allocation failed
1467  */
1468 APR_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
1469  apr_size_t nbyte,
1470  void (*free_func)(void *data))
1471  __attribute__((nonnull(1,2)));
1472 
1473 /**
1474  * Create a bucket referring to memory allocated from a pool.
1475  *
1476  * @param buf The buffer to insert into the bucket
1477  * @param length The number of bytes referred to by this bucket
1478  * @param pool The pool the memory was allocated from
1479  * @param list The freelist from which this bucket should be allocated
1480  * @return The new bucket, or NULL if allocation failed
1481  */
1482 APR_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
1483  apr_size_t length,
1484  apr_pool_t *pool,
1485  apr_bucket_alloc_t *list)
1486  __attribute__((nonnull(1,3,4)));
1487 
1488 /**
1489  * Make the bucket passed in a bucket refer to pool data
1490  * @param b The bucket to make into a pool bucket
1491  * @param buf The buffer to insert into the bucket
1492  * @param length The number of bytes referred to by this bucket
1493  * @param pool The pool the memory was allocated from
1494  * @return The new bucket, or NULL if allocation failed
1495  */
1496 APR_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
1497  apr_size_t length,
1498  apr_pool_t *pool)
1499  __attribute__((nonnull(1,2,4)));
1500 
1501 #if APR_HAS_MMAP
1502 /**
1503  * Create a bucket referring to mmap()ed memory.
1504  * @param mm The mmap to insert into the bucket
1505  * @param start The offset of the first byte in the mmap
1506  * that this bucket refers to
1507  * @param length The number of bytes referred to by this bucket
1508  * @param list The freelist from which this bucket should be allocated
1509  * @return The new bucket, or NULL if allocation failed
1510  */
1512  apr_off_t start,
1513  apr_size_t length,
1514  apr_bucket_alloc_t *list)
1515  __attribute__((nonnull(1,4)));
1516 
1517 /**
1518  * Make the bucket passed in a bucket refer to an MMAP'ed file
1519  * @param b The bucket to make into a MMAP bucket
1520  * @param mm The mmap to insert into the bucket
1521  * @param start The offset of the first byte in the mmap
1522  * that this bucket refers to
1523  * @param length The number of bytes referred to by this bucket
1524  * @return The new bucket, or NULL if allocation failed
1525  */
1527  apr_off_t start,
1528  apr_size_t length)
1529  __attribute__((nonnull(1,2)));
1530 #endif
1531 
1532 /**
1533  * Create a bucket referring to a socket.
1534  * @param thissock The socket to put in the bucket
1535  * @param list The freelist from which this bucket should be allocated
1536  * @return The new bucket, or NULL if allocation failed
1537  */
1539  apr_bucket_alloc_t *list)
1540  __attribute__((nonnull(1,2)));
1541 /**
1542  * Make the bucket passed in a bucket refer to a socket
1543  * @param b The bucket to make into a SOCKET bucket
1544  * @param thissock The socket to put in the bucket
1545  * @return The new bucket, or NULL if allocation failed
1546  */
1548  apr_socket_t *thissock)
1549  __attribute__((nonnull(1,2)));
1550 
1551 /**
1552  * Create a bucket referring to a pipe.
1553  * @param thispipe The pipe to put in the bucket
1554  * @param list The freelist from which this bucket should be allocated
1555  * @return The new bucket, or NULL if allocation failed
1556  */
1558  apr_bucket_alloc_t *list)
1559  __attribute__((nonnull(1,2)));
1560 
1561 /**
1562  * Make the bucket passed in a bucket refer to a pipe
1563  * @param b The bucket to make into a PIPE bucket
1564  * @param thispipe The pipe to put in the bucket
1565  * @return The new bucket, or NULL if allocation failed
1566  */
1568  apr_file_t *thispipe)
1569  __attribute__((nonnull(1,2)));
1570 
1571 /**
1572  * Create a bucket referring to a file.
1573  * @param fd The file to put in the bucket
1574  * @param offset The offset where the data of interest begins in the file
1575  * @param len The amount of data in the file we are interested in
1576  * @param p The pool into which any needed structures should be created
1577  * while reading from this file bucket
1578  * @param list The freelist from which this bucket should be allocated
1579  * @return The new bucket, or NULL if allocation failed
1580  * @remark If the file is truncated such that the segment of the file
1581  * referenced by the bucket no longer exists, an attempt to read
1582  * from the bucket will fail with APR_EOF.
1583  * @remark apr_brigade_insert_file() should generally be used to
1584  * insert files into brigades, since that function can correctly
1585  * handle large file issues.
1586  */
1588  apr_off_t offset,
1589  apr_size_t len,
1590  apr_pool_t *p,
1591  apr_bucket_alloc_t *list)
1592  __attribute__((nonnull(1,4,5)));
1593 
1594 /**
1595  * Make the bucket passed in a bucket refer to a file
1596  * @param b The bucket to make into a FILE bucket
1597  * @param fd The file to put in the bucket
1598  * @param offset The offset where the data of interest begins in the file
1599  * @param len The amount of data in the file we are interested in
1600  * @param p The pool into which any needed structures should be created
1601  * while reading from this file bucket
1602  * @return The new bucket, or NULL if allocation failed
1603  */
1605  apr_off_t offset,
1606  apr_size_t len, apr_pool_t *p)
1607  __attribute__((nonnull(1,2,5)));
1608 
1609 /**
1610  * Enable or disable memory-mapping for a FILE bucket (default is enabled)
1611  * @param b The bucket
1612  * @param enabled Whether memory-mapping should be enabled
1613  * @return APR_SUCCESS normally, or an error code if the operation fails
1614  */
1616  int enabled)
1617  __attribute__((nonnull(1)));
1618 
1619 /** @} */
1620 #ifdef __cplusplus
1621 }
1622 #endif
1623 
1624 #endif /* !APR_BUCKETS_H */
apr_bucket * apr_bucket_flush_create(apr_bucket_alloc_t *list)
apr_status_t apr_brigade_split_line(apr_bucket_brigade *bbOut, apr_bucket_brigade *bbIn, apr_read_type_e block, apr_off_t maxbytes)
struct apr_socket_t apr_socket_t
Definition: apr_network_io.h:246
apr_file_t * fd
Definition: apr_buckets.h:619
apr_status_t apr_bucket_copy_notimpl(apr_bucket *e, apr_bucket **c)
Definition: apr_buckets.h:546
apr_status_t apr_brigade_putstrs(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx,...)
APR Network library.
apr_status_t apr_bucket_setaside_notimpl(apr_bucket *data, apr_pool_t *pool)
apr_bucket * apr_bucket_immortal_make(apr_bucket *b, const char *buf, apr_size_t nbyte)
char * base
Definition: apr_buckets.h:552
apr_status_t apr_brigade_printf(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, const char *fmt,...)
void * data
Definition: apr_buckets.h:244
apr_bucket * apr_bucket_eos_create(apr_bucket_alloc_t *list)
const apr_bucket_type_t apr_bucket_type_pool
apr_status_t apr_brigade_vprintf(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, const char *fmt, va_list va)
APR_RING_ENTRY(apr_bucket) link
int can_mmap
Definition: apr_buckets.h:626
const apr_bucket_type_t apr_bucket_type_pipe
apr_status_t apr_brigade_write(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, const char *str, apr_size_t nbyte)
enum apr_bucket_type_t::@0 is_metadata
apr_status_t(* setaside)(apr_bucket *e, apr_pool_t *pool)
Definition: apr_buckets.h:192
Definition: apr_buckets.h:615
#define APR_DECLARE_DATA
Definition: apr.h:529
apr_status_t apr_brigade_vputstrs(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, va_list va)
apr_bucket * apr_bucket_eos_make(apr_bucket *b)
apr_status_t(* read)(apr_bucket *b, const char **str, apr_size_t *len, apr_read_type_e block)
Definition: apr_buckets.h:176
apr_bucket_alloc_t * bucket_alloc
Definition: apr_buckets.h:275
apr_status_t(* split)(apr_bucket *e, apr_size_t point)
Definition: apr_buckets.h:203
struct apr_allocator_t apr_allocator_t
Definition: apr_allocator.h:41
apr_pool_t * readpool
Definition: apr_buckets.h:622
apr_bucket * apr_bucket_heap_create(const char *buf, apr_size_t nbyte, void(*free_func)(void *data), apr_bucket_alloc_t *list)
void apr_bucket_free(void *block)
apr_bucket * apr_bucket_socket_create(apr_socket_t *thissock, apr_bucket_alloc_t *list)
const apr_bucket_type_t apr_bucket_type_transient
apr_read_type_e
Definition: apr_buckets.h:57
apr_bucket * apr_bucket_shared_make(apr_bucket *b, void *data, apr_off_t start, apr_size_t length)
apr_pool_t * p
Definition: apr_buckets.h:264
apr_status_t apr_brigade_length(apr_bucket_brigade *bb, int read_all, apr_off_t *length)
apr_status_t apr_brigade_writev(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, const struct iovec *vec, apr_size_t nvec)
apr_bucket * apr_bucket_file_create(apr_file_t *fd, apr_off_t offset, apr_size_t len, apr_pool_t *p, apr_bucket_alloc_t *list)
apr_bucket_refcount refcount
Definition: apr_buckets.h:548
apr_bucket b
Definition: apr_buckets.h:637
Definition: apr_buckets.h:258
APR File I/O Handling.
APR Miscellaneous library routines.
apr_bucket_alloc_t * apr_bucket_alloc_create_ex(apr_allocator_t *allocator)
apr_bucket * apr_bucket_flush_make(apr_bucket *b)
apr_size_t alloc_len
Definition: apr_buckets.h:554
struct apr_bucket_alloc_t apr_bucket_alloc_t
Definition: apr_buckets.h:123
apr_bucket_refcount refcount
Definition: apr_buckets.h:604
apr_status_t apr_bucket_shared_split(apr_bucket *b, apr_size_t point)
const apr_bucket_type_t apr_bucket_type_eos
void(* free)(void *e)
Definition: apr_buckets.h:252
apr_bucket_file file
Definition: apr_buckets.h:643
const char * base
Definition: apr_buckets.h:582
Definition: apr_mmap.h:62
int apr_bucket_shared_destroy(void *data)
apr_status_t apr_bucket_file_enable_mmap(apr_bucket *b, int enabled)
struct apr_file_t apr_file_t
Definition: apr_file_io.h:216
apr_status_t apr_bucket_simple_copy(apr_bucket *a, apr_bucket **b)
APR_RING_HEAD(apr_bucket_list, apr_bucket) list
apr_pool_t * pool
Definition: apr_buckets.h:589
apr_bucket * apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm, apr_off_t start, apr_size_t length)
const apr_bucket_type_t apr_bucket_type_immortal
apr_bucket * apr_bucket_heap_make(apr_bucket *b, const char *buf, apr_size_t nbyte, void(*free_func)(void *data))
const apr_bucket_type_t apr_bucket_type_mmap
Definition: apr_buckets.h:224
Definition: apr_buckets.h:636
apr_bucket * apr_bucket_pipe_make(apr_bucket *b, apr_file_t *thispipe)
apr_status_t apr_brigade_to_iovec(apr_bucket_brigade *b, struct iovec *vec, int *nvec)
apr_status_t apr_brigade_puts(apr_bucket_brigade *bb, apr_brigade_flush flush, void *ctx, const char *str)
APR Error Codes.
apr_bucket * apr_bucket_mmap_create(apr_mmap_t *mm, apr_off_t start, apr_size_t length, apr_bucket_alloc_t *list)
#define APR_DECLARE(type)
Definition: apr.h:500
apr_status_t apr_brigade_cleanup(void *data)
Definition: apr_buckets.h:564
APR Platform Definitions.
apr_bucket * apr_brigade_insert_file(apr_bucket_brigade *bb, apr_file_t *f, apr_off_t start, apr_off_t len, apr_pool_t *p)
apr_bucket * apr_bucket_immortal_create(const char *buf, apr_size_t nbyte, apr_bucket_alloc_t *list)
APR MMAP routines.
apr_status_t(* apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx)
Definition: apr_buckets.h:282
Definition: apr_buckets.h:534
apr_status_t(* copy)(apr_bucket *e, apr_bucket **c)
Definition: apr_buckets.h:211
const apr_bucket_type_t apr_bucket_type_flush
const apr_bucket_type_t apr_bucket_type_heap
apr_status_t apr_brigade_pflatten(apr_bucket_brigade *bb, char **c, apr_size_t *len, apr_pool_t *pool)
apr_bucket_brigade * apr_brigade_split_ex(apr_bucket_brigade *b, apr_bucket *e, apr_bucket_brigade *a)
int num_func
Definition: apr_buckets.h:140
void apr_bucket_alloc_destroy(apr_bucket_alloc_t *list)
const apr_bucket_type_t apr_bucket_type_socket
apr_bucket * apr_bucket_pool_create(const char *buf, apr_size_t length, apr_pool_t *pool, apr_bucket_alloc_t *list)
apr_status_t apr_brigade_partition(apr_bucket_brigade *b, apr_off_t point, apr_bucket **after_point)
apr_status_t apr_bucket_shared_copy(apr_bucket *a, apr_bucket **b)
apr_bucket * apr_bucket_socket_make(apr_bucket *b, apr_socket_t *thissock)
apr_off_t start
Definition: apr_buckets.h:242
apr_bucket_brigade * apr_brigade_split(apr_bucket_brigade *b, apr_bucket *e)
int refcount
Definition: apr_buckets.h:536
apr_bucket * apr_bucket_pool_make(apr_bucket *b, const char *buf, apr_size_t length, apr_pool_t *pool)
void apr_bucket_destroy_noop(void *data)
Definition: apr_buckets.h:131
void(* destroy)(void *data)
Definition: apr_buckets.h:164
apr_bucket * apr_bucket_transient_create(const char *buf, apr_size_t nbyte, apr_bucket_alloc_t *list)
#define APR_DECLARE_NONSTD(type)
Definition: apr.h:513
Definition: apr_buckets.h:58
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
Definition: apr_buckets.h:153
apr_bucket_alloc_t * list
Definition: apr_buckets.h:593
int apr_status_t
Definition: apr_errno.h:44
apr_bucket * apr_bucket_file_make(apr_bucket *b, apr_file_t *fd, apr_off_t offset, apr_size_t len, apr_pool_t *p)
const apr_bucket_type_t * type
Definition: apr_buckets.h:228
apr_status_t apr_bucket_split_notimpl(apr_bucket *data, apr_size_t point)
apr_bucket_refcount refcount
Definition: apr_buckets.h:617
apr_bucket_mmap mmap
Definition: apr_buckets.h:641
apr_bucket_brigade * apr_brigade_create(apr_pool_t *p, apr_bucket_alloc_t *list)
apr_bucket_pool pool
Definition: apr_buckets.h:639
Definition: apr_buckets.h:602
apr_status_t apr_bucket_setaside_noop(apr_bucket *data, apr_pool_t *pool)
void * apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list)
apr_bucket * apr_bucket_pipe_create(apr_file_t *thispipe, apr_bucket_alloc_t *list)
apr_bucket * apr_bucket_transient_make(apr_bucket *b, const char *buf, apr_size_t nbyte)
apr_bucket_alloc_t * list
Definition: apr_buckets.h:254
apr_status_t apr_brigade_flatten(apr_bucket_brigade *bb, char *c, apr_size_t *len)
Definition: apr_buckets.h:59
apr_bucket_heap heap
Definition: apr_buckets.h:638
apr_size_t length
Definition: apr_buckets.h:234
apr_status_t apr_brigade_destroy(apr_bucket_brigade *b)
apr_status_t apr_bucket_simple_split(apr_bucket *b, apr_size_t point)
const apr_bucket_type_t apr_bucket_type_file
apr_bucket_heap heap
Definition: apr_buckets.h:576
apr_status_t apr_brigade_putc(apr_bucket_brigade *b, apr_brigade_flush flush, void *ctx, const char c)
APR Rings.
Definition: apr_buckets.h:155
const char * name
Definition: apr_buckets.h:135
apr_mmap_t * mmap
Definition: apr_buckets.h:606
apr_bucket_alloc_t * apr_bucket_alloc_create(apr_pool_t *p)