155714Skris/* Licensed to the Apache Software Foundation (ASF) under one or more
255714Skris * contributor license agreements.  See the NOTICE file distributed with
355714Skris * this work for additional information regarding copyright ownership.
455714Skris * The ASF licenses this file to You under the Apache License, Version 2.0
555714Skris * (the "License"); you may not use this file except in compliance with
655714Skris * the License.  You may obtain a copy of the License at
755714Skris *
855714Skris *     http://www.apache.org/licenses/LICENSE-2.0
955714Skris *
1055714Skris * Unless required by applicable law or agreed to in writing, software
1155714Skris * distributed under the License is distributed on an "AS IS" BASIS,
1255714Skris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355714Skris * See the License for the specific language governing permissions and
1455714Skris * limitations under the License.
1555714Skris */
1655714Skris/**
1755714Skris * @file apr_buckets.h
1855714Skris * @brief APR-UTIL Buckets/Bucket Brigades
1955714Skris */
2055714Skris
2155714Skris#ifndef APR_BUCKETS_H
2255714Skris#define APR_BUCKETS_H
2355714Skris
2455714Skris#if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG)
2555714Skris#define APR_RING_DEBUG
2655714Skris#endif
2755714Skris
2855714Skris#include "apu.h"
2955714Skris#include "apr_network_io.h"
3055714Skris#include "apr_file_io.h"
3155714Skris#include "apr_general.h"
3255714Skris#include "apr_mmap.h"
3355714Skris#include "apr_errno.h"
3455714Skris#include "apr_ring.h"
3555714Skris#include "apr.h"
3655714Skris#if APR_HAVE_SYS_UIO_H
3755714Skris#include <sys/uio.h>	/* for struct iovec */
3855714Skris#endif
3955714Skris#if APR_HAVE_STDARG_H
4055714Skris#include <stdarg.h>
4155714Skris#endif
4255714Skris
4355714Skris#ifdef __cplusplus
4455714Skrisextern "C" {
4555714Skris#endif
4655714Skris
4755714Skris/**
4855714Skris * @defgroup APR_Util_Bucket_Brigades Bucket Brigades
4955714Skris * @ingroup APR_Util
5055714Skris * @{
5155714Skris */
5255714Skris
5355714Skris/** default bucket buffer size - 8KB minus room for memory allocator headers */
5455714Skris#define APR_BUCKET_BUFF_SIZE 8000
5555714Skris
5655714Skris/** Determines how a bucket or brigade should be read */
5755714Skristypedef enum {
58109998Smarkm    APR_BLOCK_READ,   /**< block until data becomes available */
59109998Smarkm    APR_NONBLOCK_READ /**< return immediately if no data is available */
60109998Smarkm} apr_read_type_e;
61109998Smarkm
62109998Smarkm/**
63109998Smarkm * The one-sentence buzzword-laden overview: Bucket brigades represent
64109998Smarkm * a complex data stream that can be passed through a layered IO
65109998Smarkm * system without unnecessary copying. A longer overview follows...
66109998Smarkm *
67109998Smarkm * A bucket brigade is a doubly linked list (ring) of buckets, so we
68109998Smarkm * aren't limited to inserting at the front and removing at the end.
69109998Smarkm * Buckets are only passed around as members of a brigade, although
70109998Smarkm * singleton buckets can occur for short periods of time.
71109998Smarkm *
72109998Smarkm * Buckets are data stores of various types. They can refer to data in
73109998Smarkm * memory, or part of a file or mmap area, or the output of a process,
74109998Smarkm * etc. Buckets also have some type-dependent accessor functions:
75109998Smarkm * read, split, copy, setaside, and destroy.
76109998Smarkm *
77109998Smarkm * read returns the address and size of the data in the bucket. If the
78109998Smarkm * data isn't in memory then it is read in and the bucket changes type
79109998Smarkm * so that it can refer to the new location of the data. If all the
80109998Smarkm * data doesn't fit in the bucket then a new bucket is inserted into
81109998Smarkm * the brigade to hold the rest of it.
82109998Smarkm *
83109998Smarkm * split divides the data in a bucket into two regions. After a split
84109998Smarkm * the original bucket refers to the first part of the data and a new
85109998Smarkm * bucket inserted into the brigade after the original bucket refers
86109998Smarkm * to the second part of the data. Reference counts are maintained as
87109998Smarkm * necessary.
88109998Smarkm *
89109998Smarkm * setaside ensures that the data in the bucket has a long enough
90109998Smarkm * lifetime. Sometimes it is convenient to create a bucket referring
91109998Smarkm * to data on the stack in the expectation that it will be consumed
92109998Smarkm * (output to the network) before the stack is unwound. If that
93109998Smarkm * expectation turns out not to be valid, the setaside function is
94109998Smarkm * called to move the data somewhere safer.
95109998Smarkm *
96109998Smarkm * copy makes a duplicate of the bucket structure as long as it's
97109998Smarkm * possible to have multiple references to a single copy of the
98109998Smarkm * data itself.  Not all bucket types can be copied.
99109998Smarkm *
100109998Smarkm * destroy maintains the reference counts on the resources used by a
101109998Smarkm * bucket and frees them if necessary.
102109998Smarkm *
103109998Smarkm * Note: all of the above functions have wrapper macros (apr_bucket_read(),
104109998Smarkm * apr_bucket_destroy(), etc), and those macros should be used rather
105109998Smarkm * than using the function pointers directly.
106109998Smarkm *
107109998Smarkm * To write a bucket brigade, they are first made into an iovec, so that we
108109998Smarkm * don't write too little data at one time.  Currently we ignore compacting the
109109998Smarkm * buckets into as few buckets as possible, but if we really want good
110109998Smarkm * performance, then we need to compact the buckets before we convert to an
111160814Ssimon * iovec, or possibly while we are converting to an iovec.
112160814Ssimon */
113160814Ssimon
114160814Ssimon/*
115160814Ssimon * Forward declaration of the main types.
11655714Skris */
117109998Smarkm
118109998Smarkm/** @see apr_bucket_brigade */
119109998Smarkmtypedef struct apr_bucket_brigade apr_bucket_brigade;
12059191Skris/** @see apr_bucket */
12159191Skristypedef struct apr_bucket apr_bucket;
12259191Skris/** @see apr_bucket_alloc_t */
12355714Skristypedef struct apr_bucket_alloc_t apr_bucket_alloc_t;
12455714Skris
12555714Skris/** @see apr_bucket_type_t */
12659191Skristypedef struct apr_bucket_type_t apr_bucket_type_t;
12755714Skris
128109998Smarkm/**
129109998Smarkm * Basic bucket type
13055714Skris */
131160814Ssimonstruct apr_bucket_type_t {
132160814Ssimon    /**
133160814Ssimon     * The name of the bucket type
134160814Ssimon     */
13555714Skris    const char *name;
13655714Skris    /**
13759191Skris     * The number of functions this bucket understands.  Can not be less than
13855714Skris     * five.
139160814Ssimon     */
14055714Skris    int num_func;
141111147Snectar    /**
142109998Smarkm     * Whether the bucket contains metadata (ie, information that
143111147Snectar     * describes the regular contents of the brigade).  The metadata
14455714Skris     * is not returned by apr_bucket_read() and is not indicated by
14559191Skris     * the ->length of the apr_bucket itself.  In other words, an
146160814Ssimon     * empty bucket is safe to arbitrarily remove if and only if it
147160814Ssimon     * contains no metadata.  In this sense, "data" is just raw bytes
148160814Ssimon     * that are the "content" of the brigade and "metadata" describes
149160814Ssimon     * that data but is not a proper part of it.
150160814Ssimon     */
151160814Ssimon    enum {
152160814Ssimon        /** This bucket type represents actual data to send to the client. */
153160814Ssimon        APR_BUCKET_DATA = 0,
154160814Ssimon        /** This bucket type represents metadata. */
155160814Ssimon        APR_BUCKET_METADATA = 1
156109998Smarkm    } is_metadata;
157109998Smarkm    /**
158109998Smarkm     * Free the private data and any resources used by the bucket (if they
159109998Smarkm     *  aren't shared with another bucket).  This function is required to be
160109998Smarkm     *  implemented for all bucket types, though it might be a no-op on some
161109998Smarkm     *  of them (namely ones that never allocate any private data structures).
162109998Smarkm     * @param data The private data pointer from the bucket to be destroyed
163109998Smarkm     */
164109998Smarkm    void (*destroy)(void *data);
165109998Smarkm
166109998Smarkm    /**
16755714Skris     * Read the data from the bucket. This is required to be implemented
16855714Skris     *  for all bucket types.
169109998Smarkm     * @param b The bucket to read from
17055714Skris     * @param str A place to store the data read.  Allocation should only be
17155714Skris     *            done if absolutely necessary.
172109998Smarkm     * @param len The amount of data read.
173109998Smarkm     * @param block Should this read function block if there is more data that
174109998Smarkm     *              cannot be read immediately.
175160814Ssimon     */
176160814Ssimon    apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len,
177160814Ssimon                         apr_read_type_e block);
17855714Skris
17955714Skris    /**
18055714Skris     * Make it possible to set aside the data for at least as long as the
18155714Skris     *  given pool. Buckets containing data that could potentially die before
18255714Skris     *  this pool (e.g. the data resides on the stack, in a child pool of
183109998Smarkm     *  the given pool, or in a disjoint pool) must somehow copy, shift, or
184109998Smarkm     *  transform the data to have the proper lifetime.
185160814Ssimon     * @param e The bucket to convert
186160814Ssimon     * @remark Some bucket types contain data that will always outlive the
187109998Smarkm     *         bucket itself. For example no data (EOS and FLUSH), or the data
18859191Skris     *         resides in global, constant memory (IMMORTAL), or the data is on
189109998Smarkm     *      the heap (HEAP). For these buckets, apr_bucket_setaside_noop can
19055714Skris     *      be used.
19168651Skris     */
19255714Skris    apr_status_t (*setaside)(apr_bucket *e, apr_pool_t *pool);
193109998Smarkm
194160814Ssimon    /**
195160814Ssimon     * Split one bucket in two at the specified position by duplicating
196160814Ssimon     *  the bucket structure (not the data) and modifying any necessary
197160814Ssimon     *  start/end/offset information.  If it's not possible to do this
198160814Ssimon     *  for the bucket type (perhaps the length of the data is indeterminate,
199160814Ssimon     *  as with pipe and socket buckets), then APR_ENOTIMPL is returned.
200160814Ssimon     * @param e The bucket to split
201160814Ssimon     * @param point The offset of the first byte in the new bucket
202160814Ssimon     */
203109998Smarkm    apr_status_t (*split)(apr_bucket *e, apr_size_t point);
204109998Smarkm
20555714Skris    /**
20659191Skris     * Copy the bucket structure (not the data), assuming that this is
20759191Skris     *  possible for the bucket type. If it's not, APR_ENOTIMPL is returned.
20855714Skris     * @param e The bucket to copy
20955714Skris     * @param c Returns a pointer to the new bucket
21059191Skris     */
21159191Skris    apr_status_t (*copy)(apr_bucket *e, apr_bucket **c);
21259191Skris
21355714Skris};
21459191Skris
21559191Skris/**
21655714Skris * apr_bucket structures are allocated on the malloc() heap and
21755714Skris * their lifetime is controlled by the parent apr_bucket_brigade
21855714Skris * structure. Buckets can move from one brigade to another e.g. by
21955714Skris * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has
22055714Skris * the same lifetime as the bucket and is freed when the bucket is
22155714Skris * destroyed; if the data is shared by more than one bucket (e.g.
22255714Skris * after a split) the data is freed when the last bucket goes away.
22359191Skris */
22455714Skrisstruct apr_bucket {
22559191Skris    /** Links to the rest of the brigade */
22655714Skris    APR_RING_ENTRY(apr_bucket) link;
227160814Ssimon    /** The type of bucket.  */
22855714Skris    const apr_bucket_type_t *type;
22955714Skris    /** The length of the data in the bucket.  This could have been implemented
23055714Skris     *  with a function, but this is an optimization, because the most
23155714Skris     *  common thing to do will be to get the length.  If the length is unknown,
23255714Skris     *  the value of this field will be (apr_size_t)(-1).
23355714Skris     */
234160814Ssimon    apr_size_t length;
235160814Ssimon    /** The start of the data in the bucket relative to the private base
236160814Ssimon     *  pointer.  The vast majority of bucket types allow a fixed block of
23755714Skris     *  data to be referenced by multiple buckets, each bucket pointing to
23855714Skris     *  a different segment of the data.  That segment starts at base+start
23955714Skris     *  and ends at base+start+length.
24055714Skris     *  If the length == (apr_size_t)(-1), then start == -1.
24155714Skris     */
242109998Smarkm    apr_off_t start;
24359191Skris    /** type-dependent data hangs off this pointer */
24459191Skris    void *data;
24559191Skris    /**
24655714Skris     * Pointer to function used to free the bucket. This function should
247160814Ssimon     * always be defined and it should be consistent with the memory
248160814Ssimon     * function used to allocate the bucket. For example, if malloc() is
249160814Ssimon     * used to allocate the bucket, this pointer should point to free().
250109998Smarkm     * @param e Pointer to the bucket being freed
25155714Skris     */
25255714Skris    void (*free)(void *e);
253109998Smarkm    /** The freelist from which this bucket was allocated */
25455714Skris    apr_bucket_alloc_t *list;
25555714Skris};
256109998Smarkm
25755714Skris/** A list of buckets */
25855714Skrisstruct apr_bucket_brigade {
25955714Skris    /** The pool to associate the brigade with.  The data is not allocated out
26055714Skris     *  of the pool, but a cleanup is registered with this pool.  If the
26159191Skris     *  brigade is destroyed by some mechanism other than pool destruction,
26259191Skris     *  the destroying function is responsible for killing the cleanup.
26359191Skris     */
26459191Skris    apr_pool_t *p;
26555714Skris    /** The buckets in the brigade are on this list. */
26655714Skris    /*
26755714Skris     * The apr_bucket_list structure doesn't actually need a name tag
26859191Skris     * because it has no existence independent of struct apr_bucket_brigade;
269109998Smarkm     * the ring macros are designed so that you can leave the name tag
270160814Ssimon     * argument empty in this situation but apparently the Windows compiler
271160814Ssimon     * doesn't like that.
272160814Ssimon     */
273160814Ssimon    APR_RING_HEAD(apr_bucket_list, apr_bucket) list;
274160814Ssimon    /** The freelist from which this bucket was allocated */
275160814Ssimon    apr_bucket_alloc_t *bucket_alloc;
276160814Ssimon};
27755714Skris
27855714Skris
27959191Skris/**
28059191Skris * Function called when a brigade should be flushed
28159191Skris */
28259191Skristypedef apr_status_t (*apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx);
28359191Skris
28459191Skris/*
28559191Skris * define APR_BUCKET_DEBUG if you want your brigades to be checked for
28659191Skris * validity at every possible instant.  this will slow your code down
28759191Skris * substantially but is a very useful debugging tool.
28859191Skris */
28959191Skris#ifdef APR_BUCKET_DEBUG
29059191Skris
29159191Skris#define APR_BRIGADE_CHECK_CONSISTENCY(b)				\
29259191Skris        APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link)
29359191Skris
29459191Skris#define APR_BUCKET_CHECK_CONSISTENCY(e)					\
29559191Skris        APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
29659191Skris
29759191Skris#else
298109998Smarkm/**
29959191Skris * checks the ring pointers in a bucket brigade for consistency.  an
30059191Skris * abort() will be triggered if any inconsistencies are found.
30159191Skris *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
30259191Skris * @param b The brigade
30359191Skris */
30459191Skris#define APR_BRIGADE_CHECK_CONSISTENCY(b)
30559191Skris/**
306109998Smarkm * checks the brigade a bucket is in for ring consistency.  an
30759191Skris * abort() will be triggered if any inconsistencies are found.
30859191Skris *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
30959191Skris * @param e The bucket
31059191Skris */
31159191Skris#define APR_BUCKET_CHECK_CONSISTENCY(e)
31259191Skris#endif
31359191Skris
31459191Skris
31559191Skris/**
31659191Skris * Wrappers around the RING macros to reduce the verbosity of the code
31759191Skris * that handles bucket brigades.
31859191Skris */
31959191Skris/**
32059191Skris * The magic pointer value that indicates the head of the brigade
32159191Skris * @remark This is used to find the beginning and end of the brigade, eg:
32259191Skris * <pre>
323109998Smarkm *      while (e != APR_BRIGADE_SENTINEL(b)) {
324109998Smarkm *          ...
325109998Smarkm *          e = APR_BUCKET_NEXT(e);
326109998Smarkm *      }
327109998Smarkm * </pre>
328109998Smarkm * @param  b The brigade
329109998Smarkm * @return The magic pointer value
330109998Smarkm */
331109998Smarkm#define APR_BRIGADE_SENTINEL(b)	APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
332109998Smarkm
333109998Smarkm/**
334109998Smarkm * Determine if the bucket brigade is empty
335109998Smarkm * @param b The brigade to check
336120631Snectar * @return true or false
337109998Smarkm */
338109998Smarkm#define APR_BRIGADE_EMPTY(b)	APR_RING_EMPTY(&(b)->list, apr_bucket, link)
339109998Smarkm
340109998Smarkm/**
341109998Smarkm * Return the first bucket in a brigade
342109998Smarkm * @param b The brigade to query
343109998Smarkm * @return The first bucket in the brigade
344109998Smarkm */
345109998Smarkm#define APR_BRIGADE_FIRST(b)	APR_RING_FIRST(&(b)->list)
346109998Smarkm/**
347109998Smarkm * Return the last bucket in a brigade
348109998Smarkm * @param b The brigade to query
349109998Smarkm * @return The last bucket in the brigade
350109998Smarkm */
351109998Smarkm#define APR_BRIGADE_LAST(b)	APR_RING_LAST(&(b)->list)
352109998Smarkm
353109998Smarkm/**
354109998Smarkm * Insert a single bucket at the front of a brigade
355109998Smarkm * @param b The brigade to add to
356109998Smarkm * @param e The bucket to insert
357109998Smarkm */
358109998Smarkm#define APR_BRIGADE_INSERT_HEAD(b, e) do {				\
359109998Smarkm	apr_bucket *ap__b = (e);                                        \
360109998Smarkm	APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link);	\
361109998Smarkm        APR_BRIGADE_CHECK_CONSISTENCY((b));				\
362109998Smarkm    } while (0)
363109998Smarkm
364109998Smarkm/**
365109998Smarkm * Insert a single bucket at the end of a brigade
366109998Smarkm * @param b The brigade to add to
367109998Smarkm * @param e The bucket to insert
368109998Smarkm */
369109998Smarkm#define APR_BRIGADE_INSERT_TAIL(b, e) do {				\
370109998Smarkm	apr_bucket *ap__b = (e);					\
371109998Smarkm	APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link);	\
372109998Smarkm        APR_BRIGADE_CHECK_CONSISTENCY((b));				\
373109998Smarkm    } while (0)
374109998Smarkm
375109998Smarkm/**
376109998Smarkm * Concatenate brigade b onto the end of brigade a, leaving brigade b empty
377109998Smarkm * @param a The first brigade
378109998Smarkm * @param b The second brigade
379109998Smarkm */
380109998Smarkm#define APR_BRIGADE_CONCAT(a, b) do {					\
381109998Smarkm        APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link);	\
382109998Smarkm        APR_BRIGADE_CHECK_CONSISTENCY((a));				\
383109998Smarkm    } while (0)
384109998Smarkm
385109998Smarkm/**
386160814Ssimon * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty
38755714Skris * @param a The first brigade
38855714Skris * @param b The second brigade
38955714Skris */
39055714Skris#define APR_BRIGADE_PREPEND(a, b) do {					\
39155714Skris        APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link);	\
39255714Skris        APR_BRIGADE_CHECK_CONSISTENCY((a));				\
39355714Skris    } while (0)
39455714Skris
39555714Skris/**
396160814Ssimon * Insert a single bucket before a specified bucket
397160814Ssimon * @param a The bucket to insert before
39855714Skris * @param b The bucket to insert
39959191Skris */
40055714Skris#define APR_BUCKET_INSERT_BEFORE(a, b) do {				\
40159191Skris	apr_bucket *ap__a = (a), *ap__b = (b);				\
402160814Ssimon	APR_RING_INSERT_BEFORE(ap__a, ap__b, link);			\
403160814Ssimon        APR_BUCKET_CHECK_CONSISTENCY(ap__a);				\
404160814Ssimon    } while (0)
40555714Skris
40655714Skris/**
40755714Skris * Insert a single bucket after a specified bucket
40855714Skris * @param a The bucket to insert after
40955714Skris * @param b The bucket to insert
410160814Ssimon */
411109998Smarkm#define APR_BUCKET_INSERT_AFTER(a, b) do {				\
41255714Skris	apr_bucket *ap__a = (a), *ap__b = (b);				\
41359191Skris	APR_RING_INSERT_AFTER(ap__a, ap__b, link);			\
41455714Skris        APR_BUCKET_CHECK_CONSISTENCY(ap__a);				\
415160814Ssimon    } while (0)
416160814Ssimon
417160814Ssimon/**
41859191Skris * Get the next bucket in the list
419160814Ssimon * @param e The current bucket
42059191Skris * @return The next bucket
42159191Skris */
422109998Smarkm#define APR_BUCKET_NEXT(e)	APR_RING_NEXT((e), link)
423160814Ssimon/**
424109998Smarkm * Get the previous bucket in the list
425142425Snectar * @param e The current bucket
426160814Ssimon * @return The previous bucket
427160814Ssimon */
42855714Skris#define APR_BUCKET_PREV(e)	APR_RING_PREV((e), link)
42959191Skris
43059191Skris/**
43159191Skris * Remove a bucket from its bucket brigade
432109998Smarkm * @param e The bucket to remove
433109998Smarkm */
434109998Smarkm#define APR_BUCKET_REMOVE(e)	APR_RING_REMOVE((e), link)
435109998Smarkm
436109998Smarkm/**
437109998Smarkm * Initialize a new bucket's prev/next pointers
438109998Smarkm * @param e The bucket to initialize
439109998Smarkm */
440109998Smarkm#define APR_BUCKET_INIT(e)	APR_RING_ELEM_INIT((e), link)
441109998Smarkm
442109998Smarkm/**
443109998Smarkm * Determine if a bucket contains metadata.  An empty bucket is
444109998Smarkm * safe to arbitrarily remove if and only if this is false.
445109998Smarkm * @param e The bucket to inspect
446109998Smarkm * @return true or false
447109998Smarkm */
44859191Skris#define APR_BUCKET_IS_METADATA(e)    ((e)->type->is_metadata)
44959191Skris
45059191Skris/**
45159191Skris * Determine if a bucket is a FLUSH bucket
45255714Skris * @param e The bucket to inspect
45355714Skris * @return true or false
45455714Skris */
45555714Skris#define APR_BUCKET_IS_FLUSH(e)       ((e)->type == &apr_bucket_type_flush)
45655714Skris/**
45755714Skris * Determine if a bucket is an EOS bucket
45855714Skris * @param e The bucket to inspect
459160814Ssimon * @return true or false
46055714Skris */
46155714Skris#define APR_BUCKET_IS_EOS(e)         ((e)->type == &apr_bucket_type_eos)
46255714Skris/**
463160814Ssimon * Determine if a bucket is a FILE bucket
464160814Ssimon * @param e The bucket to inspect
465160814Ssimon * @return true or false
466160814Ssimon */
467160814Ssimon#define APR_BUCKET_IS_FILE(e)        ((e)->type == &apr_bucket_type_file)
468160814Ssimon/**
469160814Ssimon * Determine if a bucket is a PIPE bucket
470160814Ssimon * @param e The bucket to inspect
471160814Ssimon * @return true or false
472160814Ssimon */
47355714Skris#define APR_BUCKET_IS_PIPE(e)        ((e)->type == &apr_bucket_type_pipe)
47455714Skris/**
47555714Skris * Determine if a bucket is a SOCKET bucket
47655714Skris * @param e The bucket to inspect
47755714Skris * @return true or false
47855714Skris */
47955714Skris#define APR_BUCKET_IS_SOCKET(e)      ((e)->type == &apr_bucket_type_socket)
480109998Smarkm/**
481109998Smarkm * Determine if a bucket is a HEAP bucket
48255714Skris * @param e The bucket to inspect
483109998Smarkm * @return true or false
484109998Smarkm */
485109998Smarkm#define APR_BUCKET_IS_HEAP(e)        ((e)->type == &apr_bucket_type_heap)
486109998Smarkm/**
48759191Skris * Determine if a bucket is a TRANSIENT bucket
488109998Smarkm * @param e The bucket to inspect
489109998Smarkm * @return true or false
49059191Skris */
491109998Smarkm#define APR_BUCKET_IS_TRANSIENT(e)   ((e)->type == &apr_bucket_type_transient)
492109998Smarkm/**
49359191Skris * Determine if a bucket is a IMMORTAL bucket
494109998Smarkm * @param e The bucket to inspect
49559191Skris * @return true or false
49659191Skris */
497160814Ssimon#define APR_BUCKET_IS_IMMORTAL(e)    ((e)->type == &apr_bucket_type_immortal)
498160814Ssimon#if APR_HAS_MMAP
49955714Skris/**
50055714Skris * Determine if a bucket is a MMAP bucket
50155714Skris * @param e The bucket to inspect
50255714Skris * @return true or false
50355714Skris */
50455714Skris#define APR_BUCKET_IS_MMAP(e)        ((e)->type == &apr_bucket_type_mmap)
50555714Skris#endif
50655714Skris/**
50755714Skris * Determine if a bucket is a POOL bucket
50855714Skris * @param e The bucket to inspect
50955714Skris * @return true or false
51055714Skris */
51155714Skris#define APR_BUCKET_IS_POOL(e)        ((e)->type == &apr_bucket_type_pool)
51255714Skris
51355714Skris/*
51455714Skris * General-purpose reference counting for the various bucket types.
51555714Skris *
51655714Skris * Any bucket type that keeps track of the resources it uses (i.e.
51755714Skris * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
51855714Skris * attach a reference count to the resource so that it can be freed
51955714Skris * when the last bucket that uses it goes away. Resource-sharing may
52055714Skris * occur because of bucket splits or buckets that refer to globally
52155714Skris * cached data. */
52255714Skris
52355714Skris/** @see apr_bucket_refcount */
52455714Skristypedef struct apr_bucket_refcount apr_bucket_refcount;
52555714Skris/**
52655714Skris * The structure used to manage the shared resource must start with an
52755714Skris * apr_bucket_refcount which is updated by the general-purpose refcount
52855714Skris * code. A pointer to the bucket-type-dependent private data structure
52955714Skris * can be cast to a pointer to an apr_bucket_refcount and vice versa.
53059191Skris */
53159191Skrisstruct apr_bucket_refcount {
53259191Skris    /** The number of references to this bucket */
53359191Skris    int          refcount;
53459191Skris};
53559191Skris
53659191Skris/*  *****  Reference-counted bucket types  *****  */
53759191Skris
53859191Skris/** @see apr_bucket_heap */
53959191Skristypedef struct apr_bucket_heap apr_bucket_heap;
54055714Skris/**
54155714Skris * A bucket referring to data allocated off the heap.
54255714Skris */
54355714Skrisstruct apr_bucket_heap {
54455714Skris    /** Number of buckets using this memory */
54559191Skris    apr_bucket_refcount  refcount;
54659191Skris    /** The start of the data actually allocated.  This should never be
54759191Skris     * modified, it is only used to free the bucket.
54859191Skris     */
54959191Skris    char    *base;
55055714Skris    /** how much memory was allocated */
55155714Skris    apr_size_t  alloc_len;
55255714Skris    /** function to use to delete the data */
55355714Skris    void (*free_func)(void *data);
55455714Skris};
55555714Skris
55655714Skris/** @see apr_bucket_pool */
55755714Skristypedef struct apr_bucket_pool apr_bucket_pool;
55855714Skris/**
55955714Skris * A bucket referring to data allocated from a pool
56055714Skris */
56155714Skrisstruct apr_bucket_pool {
56255714Skris    /** The pool bucket must be able to be easily morphed to a heap
56355714Skris     * bucket if the pool gets cleaned up before all references are
56455714Skris     * destroyed.  This apr_bucket_heap structure is populated automatically
56555714Skris     * when the pool gets cleaned up, and subsequent calls to pool_read()
56655714Skris     * will result in the apr_bucket in question being morphed into a
56755714Skris     * regular heap bucket.  (To avoid having to do many extra refcount
56855714Skris     * manipulations and b->data manipulations, the apr_bucket_pool
56955714Skris     * struct actually *contains* the apr_bucket_heap struct that it
57055714Skris     * will become as its first element; the two share their
57155714Skris     * apr_bucket_refcount members.)
57255714Skris     */
57359191Skris    apr_bucket_heap  heap;
57459191Skris    /** The block of data actually allocated from the pool.
57559191Skris     * Segments of this block are referenced by adjusting
57659191Skris     * the start and length of the apr_bucket accordingly.
577109998Smarkm     * This will be NULL after the pool gets cleaned up.
578109998Smarkm     */
579109998Smarkm    const char *base;
580109998Smarkm    /** The pool the data was allocated from.  When the pool
581109998Smarkm     * is cleaned up, this gets set to NULL as an indicator
582109998Smarkm     * to pool_read() that the data is now on the heap and
583109998Smarkm     * so it should morph the bucket into a regular heap
584109998Smarkm     * bucket before continuing.
585160814Ssimon     */
586160814Ssimon    apr_pool_t *pool;
587160814Ssimon    /** The freelist this structure was allocated from, which is
588160814Ssimon     * needed in the cleanup phase in order to allocate space on the heap
589160814Ssimon     */
590160814Ssimon    apr_bucket_alloc_t *list;
591160814Ssimon};
592160814Ssimon
593160814Ssimon#if APR_HAS_MMAP
594160814Ssimon/** @see apr_bucket_mmap */
595109998Smarkmtypedef struct apr_bucket_mmap apr_bucket_mmap;
596109998Smarkm/**
597160814Ssimon * A bucket referring to an mmap()ed file
598109998Smarkm */
599160814Ssimonstruct apr_bucket_mmap {
600160814Ssimon    /** Number of buckets using this memory */
601160814Ssimon    apr_bucket_refcount  refcount;
602160814Ssimon    /** The mmap this sub_bucket refers to */
603160814Ssimon    apr_mmap_t *mmap;
604160814Ssimon};
605160814Ssimon#endif
606160814Ssimon
60755714Skris/** @see apr_bucket_file */
60855714Skristypedef struct apr_bucket_file apr_bucket_file;
60955714Skris/**
61055714Skris * A bucket referring to an file
61155714Skris */
61255714Skrisstruct apr_bucket_file {
61355714Skris    /** Number of buckets using this memory */
61455714Skris    apr_bucket_refcount  refcount;
61555714Skris    /** The file this bucket refers to */
61655714Skris    apr_file_t *fd;
61755714Skris    /** The pool into which any needed structures should
61855714Skris     *  be created while reading from this file bucket */
61955714Skris    apr_pool_t *readpool;
62055714Skris#if APR_HAS_MMAP
62155714Skris    /** Whether this bucket should be memory-mapped if
62255714Skris     *  a caller tries to read from it */
623160814Ssimon    int can_mmap;
624160814Ssimon#endif /* APR_HAS_MMAP */
625160814Ssimon};
626160814Ssimon
627160814Ssimon/** @see apr_bucket_structs */
628160814Ssimontypedef union apr_bucket_structs apr_bucket_structs;
629160814Ssimon/**
630160814Ssimon * A union of all bucket structures so we know what
631142425Snectar * the max size is.
63255714Skris */
63355714Skrisunion apr_bucket_structs {
63459191Skris    apr_bucket      b;      /**< Bucket */
63559191Skris    apr_bucket_heap heap;   /**< Heap */
63659191Skris    apr_bucket_pool pool;   /**< Pool */
63759191Skris#if APR_HAS_MMAP
638109998Smarkm    apr_bucket_mmap mmap;   /**< MMap */
63955714Skris#endif
64055714Skris    apr_bucket_file file;   /**< File */
64159191Skris};
64259191Skris
64359191Skris/**
64459191Skris * The amount that apr_bucket_alloc() should allocate in the common case.
64559191Skris * Note: this is twice as big as apr_bucket_structs to allow breathing
64659191Skris * room for third-party bucket types.
64759191Skris */
64859191Skris#define APR_BUCKET_ALLOC_SIZE  APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
64959191Skris
65059191Skris/*  *****  Bucket Brigade Functions  *****  */
65159191Skris/**
65255714Skris * Create a new bucket brigade.  The bucket brigade is originally empty.
65355714Skris * @param p The pool to associate with the brigade.  Data is not allocated out
65455714Skris *          of the pool, but a cleanup is registered.
65555714Skris * @param list The bucket allocator to use
65655714Skris * @return The empty bucket brigade
657160814Ssimon */
658109998SmarkmAPU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
659109998Smarkm                                                     apr_bucket_alloc_t *list);
660109998Smarkm
661109998Smarkm/**
662109998Smarkm * destroy an entire bucket brigade.  This includes destroying all of the
663127128Snectar * buckets within the bucket brigade's bucket list.
664127128Snectar * @param b The bucket brigade to destroy
665127128Snectar */
666127128SnectarAPU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
667127128Snectar
668127128Snectar/**
669127128Snectar * empty out an entire bucket brigade.  This includes destroying all of the
670127128Snectar * buckets within the bucket brigade's bucket list.  This is similar to
671109998Smarkm * apr_brigade_destroy(), except that it does not deregister the brigade's
672109998Smarkm * pool cleanup function.
673109998Smarkm * @param data The bucket brigade to clean up
674109998Smarkm * @remark Generally, you should use apr_brigade_destroy().  This function
675109998Smarkm *         can be useful in situations where you have a single brigade that
676109998Smarkm *         you wish to reuse many times by destroying all of the buckets in
677109998Smarkm *         the brigade and putting new buckets into it later.
678109998Smarkm */
679109998SmarkmAPU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
680109998Smarkm
681160814Ssimon/**
682160814Ssimon * Move the buckets from the tail end of the existing brigade @a b into
683160814Ssimon * the brigade @a a. If @a a is NULL a new brigade is created. Buckets
684160814Ssimon * from @a e to the last bucket (inclusively) of brigade @a b are moved
685160814Ssimon * from @a b to the returned brigade @a a.
686160814Ssimon *
687160814Ssimon * @param b The brigade to split
688160814Ssimon * @param e The first bucket to move
689160814Ssimon * @param a The brigade which should be used for the result or NULL if
690160814Ssimon *          a new brigade should be created. The brigade @a a will be
691160814Ssimon *          cleared if it is not empty.
692160814Ssimon * @return The brigade supplied in @a a or a new one if @a a was NULL.
693160814Ssimon * @warning Note that this function allocates a new brigade if @a a is
694160814Ssimon * NULL so memory consumption should be carefully considered.
695109998Smarkm */
696109998SmarkmAPU_DECLARE(apr_bucket_brigade *) apr_brigade_split_ex(apr_bucket_brigade *b,
69755714Skris                                                       apr_bucket *e,
69855714Skris                                                       apr_bucket_brigade *a);
69955714Skris
70055714Skris/**
70155714Skris * Create a new bucket brigade and move the buckets from the tail end
70255714Skris * of an existing brigade into the new brigade.  Buckets from
70355714Skris * @a e to the last bucket (inclusively) of brigade @a b
70455714Skris * are moved from @a b to the returned brigade.
70555714Skris * @param b The brigade to split
70655714Skris * @param e The first bucket to move
70755714Skris * @return The new brigade
708109998Smarkm * @warning Note that this function always allocates a new brigade
70955714Skris * so memory consumption should be carefully considered.
71055714Skris */
71155714SkrisAPU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
71255714Skris                                                    apr_bucket *e);
71355714Skris
71455714Skris/**
71555714Skris * Partition a bucket brigade at a given offset (in bytes from the start of
71655714Skris * the brigade).  This is useful whenever a filter wants to use known ranges
71755714Skris * of bytes from the brigade; the ranges can even overlap.
71855714Skris * @param b The brigade to partition
71955714Skris * @param point The offset at which to partition the brigade
72055714Skris * @param after_point Returns a pointer to the first bucket after the partition
72155714Skris * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the
72255714Skris * brigade were shorter than @a point, or an error code.
72355714Skris * @remark if APR_INCOMPLETE is returned, @a after_point will be set to
72455714Skris * the brigade sentinel.
72555714Skris */
72655714SkrisAPU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
72755714Skris                                                apr_off_t point,
72855714Skris                                                apr_bucket **after_point);
729109998Smarkm
73059191Skris/**
73155714Skris * Return the total length of the brigade.
73259191Skris * @param bb The brigade to compute the length of
73355714Skris * @param read_all Read unknown-length buckets to force a size
73459191Skris * @param length Returns the length of the brigade (up to the end, or up
73559191Skris *               to a bucket read error), or -1 if the brigade has buckets
73659191Skris *               of indeterminate length and read_all is 0.
73755714Skris */
73859191SkrisAPU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
73959191Skris                                             int read_all,
74059191Skris                                             apr_off_t *length);
74159191Skris
74259191Skris/**
74359191Skris * Take a bucket brigade and store the data in a flat char*
74455714Skris * @param bb The bucket brigade to create the char* from
74559191Skris * @param c The char* to write into
74659191Skris * @param len The maximum length of the char array. On return, it is the
74755714Skris *            actual length of the char array.
74855714Skris */
749160814SsimonAPU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
750160814Ssimon                                              char *c,
751160814Ssimon                                              apr_size_t *len);
752160814Ssimon
753160814Ssimon/**
754160814Ssimon * Creates a pool-allocated string representing a flat bucket brigade
755160814Ssimon * @param bb The bucket brigade to create the char array from
756160814Ssimon * @param c On return, the allocated char array
757160814Ssimon * @param len On return, the length of the char array.
758160814Ssimon * @param pool The pool to allocate the string from.
759160814Ssimon */
760160814SsimonAPU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
761160814Ssimon                                               char **c,
762160814Ssimon                                               apr_size_t *len,
763160814Ssimon                                               apr_pool_t *pool);
764160814Ssimon
765160814Ssimon/**
766160814Ssimon * Split a brigade to represent one LF line.
767160814Ssimon * @param bbOut The bucket brigade that will have the LF line appended to.
768160814Ssimon * @param bbIn The input bucket brigade to search for a LF-line.
769160814Ssimon * @param block The blocking mode to be used to split the line.
770160814Ssimon * @param maxbytes The maximum bytes to read.  If this many bytes are seen
771160814Ssimon *                 without a LF, the brigade will contain a partial line.
772160814Ssimon */
773160814SsimonAPU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
774160814Ssimon                                                 apr_bucket_brigade *bbIn,
775160814Ssimon                                                 apr_read_type_e block,
776160814Ssimon                                                 apr_off_t maxbytes);
777160814Ssimon
778160814Ssimon/**
779160814Ssimon * Create an iovec of the elements in a bucket_brigade... return number
780160814Ssimon * of elements used.  This is useful for writing to a file or to the
781109998Smarkm * network efficiently.
78255714Skris * @param b The bucket brigade to create the iovec from
78355714Skris * @param vec The iovec to create
78455714Skris * @param nvec The number of elements in the iovec. On return, it is the
78555714Skris *             number of iovec elements actually filled out.
78655714Skris */
78755714SkrisAPU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b,
78855714Skris                                               struct iovec *vec, int *nvec);
78959191Skris
79059191Skris/**
79155714Skris * This function writes a list of strings into a bucket brigade.
79255714Skris * @param b The bucket brigade to add to
79355714Skris * @param flush The flush function to use if the brigade is full
79455714Skris * @param ctx The structure to pass to the flush function
79555714Skris * @param va A list of strings to add
79655714Skris * @return APR_SUCCESS or error code.
79755714Skris */
79855714SkrisAPU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
79955714Skris                                               apr_brigade_flush flush,
80059191Skris                                               void *ctx,
80159191Skris                                               va_list va);
80255714Skris
80355714Skris/**
80455714Skris * This function writes a string into a bucket brigade.
80555714Skris *
80655714Skris * The apr_brigade_write function attempts to be efficient with the
80755714Skris * handling of heap buckets. Regardless of the amount of data stored
80855714Skris * inside a heap bucket, heap buckets are a fixed size to promote their
80955714Skris * reuse.
81055714Skris *
81155714Skris * If an attempt is made to write a string to a brigade that already
81255714Skris * ends with a heap bucket, this function will attempt to pack the
81355714Skris * string into the remaining space in the previous heap bucket, before
81455714Skris * allocating a new heap bucket.
81555714Skris *
81655714Skris * This function always returns APR_SUCCESS, unless a flush function is
81759191Skris * passed, in which case the return value of the flush function will be
81855714Skris * returned if used.
81955714Skris * @param b The bucket brigade to add to
82055714Skris * @param flush The flush function to use if the brigade is full
821160814Ssimon * @param ctx The structure to pass to the flush function
82255714Skris * @param str The string to add
82355714Skris * @param nbyte The number of bytes to write
82455714Skris * @return APR_SUCCESS or error code
82559191Skris */
82655714SkrisAPU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
82755714Skris                                            apr_brigade_flush flush, void *ctx,
828160814Ssimon                                            const char *str, apr_size_t nbyte);
82955714Skris
83059191Skris/**
83159191Skris * This function writes multiple strings into a bucket brigade.
83259191Skris * @param b The bucket brigade to add to
83359191Skris * @param flush The flush function to use if the brigade is full
83459191Skris * @param ctx The structure to pass to the flush function
83555714Skris * @param vec The strings to add (address plus length for each)
83655714Skris * @param nvec The number of entries in iovec
83755714Skris * @return APR_SUCCESS or error code
83855714Skris */
839109998SmarkmAPU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
840109998Smarkm                                             apr_brigade_flush flush,
841109998Smarkm                                             void *ctx,
842109998Smarkm                                             const struct iovec *vec,
843109998Smarkm                                             apr_size_t nvec);
844109998Smarkm
845109998Smarkm/**
846109998Smarkm * This function writes a string into a bucket brigade.
847109998Smarkm * @param bb The bucket brigade to add to
848109998Smarkm * @param flush The flush function to use if the brigade is full
849109998Smarkm * @param ctx The structure to pass to the flush function
850109998Smarkm * @param str The string to add
851109998Smarkm * @return APR_SUCCESS or error code
852109998Smarkm */
853109998SmarkmAPU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
854109998Smarkm                                           apr_brigade_flush flush, void *ctx,
855109998Smarkm                                           const char *str);
856109998Smarkm
857109998Smarkm/**
85855714Skris * This function writes a character into a bucket brigade.
85955714Skris * @param b The bucket brigade to add to
86055714Skris * @param flush The flush function to use if the brigade is full
86155714Skris * @param ctx The structure to pass to the flush function
86259191Skris * @param c The character to add
86355714Skris * @return APR_SUCCESS or error code
86455714Skris */
86555714SkrisAPU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
86655714Skris                                           apr_brigade_flush flush, void *ctx,
86755714Skris                                           const char c);
86855714Skris
86959191Skris/**
87055714Skris * This function writes an unspecified number of strings into a bucket brigade.
87155714Skris * @param b The bucket brigade to add to
87259191Skris * @param flush The flush function to use if the brigade is full
87359191Skris * @param ctx The structure to pass to the flush function
87459191Skris * @param ... The strings to add
87559191Skris * @return APR_SUCCESS or error code
87659191Skris */
87759191SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
87859191Skris                                                     apr_brigade_flush flush,
87959191Skris                                                     void *ctx, ...);
88059191Skris
88159191Skris/**
88259191Skris * Evaluate a printf and put the resulting string at the end
88359191Skris * of the bucket brigade.
88459191Skris * @param b The brigade to write to
88559191Skris * @param flush The flush function to use if the brigade is full
88659191Skris * @param ctx The structure to pass to the flush function
88759191Skris * @param fmt The format of the string to write
88859191Skris * @param ... The arguments to fill out the format
88959191Skris * @return APR_SUCCESS or error code
89059191Skris */
89159191SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b,
89259191Skris                                                    apr_brigade_flush flush,
89359191Skris                                                    void *ctx,
89455714Skris                                                    const char *fmt, ...)
89555714Skris        __attribute__((format(printf,4,5)));
89655714Skris
89755714Skris/**
89855714Skris * Evaluate a printf and put the resulting string at the end
89955714Skris * of the bucket brigade.
90055714Skris * @param b The brigade to write to
90155714Skris * @param flush The flush function to use if the brigade is full
90255714Skris * @param ctx The structure to pass to the flush function
90355714Skris * @param fmt The format of the string to write
904109998Smarkm * @param va The arguments to fill out the format
90568651Skris * @return APR_SUCCESS or error code
90668651Skris */
907111147SnectarAPU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
908109998Smarkm                                              apr_brigade_flush flush,
909111147Snectar                                              void *ctx,
910109998Smarkm                                              const char *fmt, va_list va);
91155714Skris
91255714Skris/**
91355714Skris * Utility function to insert a file (or a segment of a file) onto the
91455714Skris * end of the brigade.  The file is split into multiple buckets if it
91559191Skris * is larger than the maximum size which can be represented by a
91655714Skris * single bucket.
917160814Ssimon * @param bb the brigade to insert into
91855714Skris * @param f the file to insert
91955714Skris * @param start the offset of the start of the segment
92059191Skris * @param len the length of the segment of the file to insert
92159191Skris * @param p pool from which file buckets are allocated
92255714Skris * @return the last bucket inserted
92355714Skris */
92455714SkrisAPU_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb,
92555714Skris                                                  apr_file_t *f,
92655714Skris                                                  apr_off_t start,
92755714Skris                                                  apr_off_t len,
92855714Skris                                                  apr_pool_t *p);
92955714Skris
93055714Skris
93155714Skris
93255714Skris/*  *****  Bucket freelist functions *****  */
93355714Skris/**
93455714Skris * Create a bucket allocator.
93555714Skris * @param p This pool's underlying apr_allocator_t is used to allocate memory
93655714Skris *          for the bucket allocator.  When the pool is destroyed, the bucket
93755714Skris *          allocator's cleanup routine will free all memory that has been
93855714Skris *          allocated from it.
93955714Skris * @remark  The reason the allocator gets its memory from the pool's
94055714Skris *          apr_allocator_t rather than from the pool itself is because
94155714Skris *          the bucket allocator will free large memory blocks back to the
94255714Skris *          allocator when it's done with them, thereby preventing memory
94355714Skris *          footprint growth that would occur if we allocated from the pool.
94455714Skris * @warning The allocator must never be used by more than one thread at a time.
94555714Skris */
94655714SkrisAPU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
94755714Skris
94855714Skris/**
94955714Skris * Create a bucket allocator.
95055714Skris * @param allocator This apr_allocator_t is used to allocate both the bucket
95155714Skris *          allocator and all memory handed out by the bucket allocator.  The
95255714Skris *          caller is responsible for destroying the bucket allocator and the
95355714Skris *          apr_allocator_t -- no automatic cleanups will happen.
95455714Skris * @warning The allocator must never be used by more than one thread at a time.
95555714Skris */
95655714SkrisAPU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
95755714Skris
95855714Skris/**
95955714Skris * Destroy a bucket allocator.
96055714Skris * @param list The allocator to be destroyed
96155714Skris */
96255714SkrisAPU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
96355714Skris
96455714Skris/**
96555714Skris * Allocate memory for use by the buckets.
96655714Skris * @param size The amount to allocate.
96755714Skris * @param list The allocator from which to allocate the memory.
96855714Skris */
96955714SkrisAPU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
97055714Skris
97155714Skris/**
97255714Skris * Free memory previously allocated with apr_bucket_alloc().
97355714Skris * @param block The block of memory to be freed.
97455714Skris */
97555714SkrisAPU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
97655714Skris
97755714Skris
97855714Skris/*  *****  Bucket Functions  *****  */
97955714Skris/**
98055714Skris * Free the resources used by a bucket. If multiple buckets refer to
98159191Skris * the same resource it is freed when the last one goes away.
98255714Skris * @see apr_bucket_delete()
98355714Skris * @param e The bucket to destroy
98455714Skris */
98555714Skris#define apr_bucket_destroy(e) do {					\
98655714Skris        (e)->type->destroy((e)->data);					\
98755714Skris        (e)->free(e);							\
98855714Skris    } while (0)
98955714Skris
99055714Skris/**
99155714Skris * Delete a bucket by removing it from its brigade (if any) and then
99255714Skris * destroying it.
99355714Skris * @remark This mainly acts as an aid in avoiding code verbosity.  It is
99455714Skris * the preferred exact equivalent to:
99555714Skris * <pre>
99655714Skris *      APR_BUCKET_REMOVE(e);
99755714Skris *      apr_bucket_destroy(e);
99859191Skris * </pre>
99955714Skris * @param e The bucket to delete
1000109998Smarkm */
1001109998Smarkm#define apr_bucket_delete(e) do {					\
100255714Skris        APR_BUCKET_REMOVE(e);						\
100355714Skris        apr_bucket_destroy(e);						\
100455714Skris    } while (0)
100555714Skris
100655714Skris/**
100755714Skris * Read some data from the bucket.
100855714Skris *
100955714Skris * The apr_bucket_read function returns a convenient amount of data
101055714Skris * from the bucket provided, writing the address and length of the
101155714Skris * data to the pointers provided by the caller. The function tries
101255714Skris * as hard as possible to avoid a memory copy.
101355714Skris *
101455714Skris * Buckets are expected to be a member of a brigade at the time they
101555714Skris * are read.
101659191Skris *
101755714Skris * In typical application code, buckets are read in a loop, and after
101855714Skris * each bucket is read and processed, it is moved or deleted from the
101955714Skris * brigade and the next bucket read.
102055714Skris *
102155714Skris * The definition of "convenient" depends on the type of bucket that
102255714Skris * is being read, and is decided by APR. In the case of memory based
102355714Skris * buckets such as heap and immortal buckets, a pointer will be
102455714Skris * returned to the location of the buffer containing the complete
102555714Skris * contents of the bucket.
102655714Skris *
102755714Skris * Some buckets, such as the socket bucket, might have no concept
102855714Skris * of length. If an attempt is made to read such a bucket, the
102955714Skris * apr_bucket_read function will read a convenient amount of data
103055714Skris * from the socket. The socket bucket is magically morphed into a
103155714Skris * heap bucket containing the just-read data, and a new socket bucket
103255714Skris * is inserted just after this heap bucket.
103355714Skris *
103455714Skris * To understand why apr_bucket_read might do this, consider the loop
103555714Skris * described above to read and process buckets. The current bucket
103655714Skris * is magically morphed into a heap bucket and returned to the caller.
103755714Skris * The caller processes the data, and deletes the heap bucket, moving
103855714Skris * onto the next bucket, the new socket bucket. This process repeats,
103955714Skris * giving the illusion of a bucket brigade that contains potentially
104055714Skris * infinite amounts of data. It is up to the caller to decide at what
104155714Skris * point to stop reading buckets.
104255714Skris *
104355714Skris * Some buckets, such as the file bucket, might have a fixed size,
104455714Skris * but be significantly larger than is practical to store in RAM in
104555714Skris * one go. As with the socket bucket, if an attempt is made to read
104655714Skris * from a file bucket, the file bucket is magically morphed into a
104755714Skris * heap bucket containing a convenient amount of data read from the
104855714Skris * current offset in the file. During the read, the offset will be
104955714Skris * moved forward on the file, and a new file bucket will be inserted
105055714Skris * directly after the current bucket representing the remainder of the
105155714Skris * file. If the heap bucket was large enough to store the whole
105255714Skris * remainder of the file, no more file buckets are inserted, and the
105355714Skris * file bucket will disappear completely.
105455714Skris *
105555714Skris * The pattern for reading buckets described above does create the
105655714Skris * illusion that the code is willing to swallow buckets that might be
105755714Skris * too large for the system to handle in one go. This however is just
105855714Skris * an illusion: APR will always ensure that large (file) or infinite
105955714Skris * (socket) buckets are broken into convenient bite sized heap buckets
106055714Skris * before data is returned to the caller.
106155714Skris *
106255714Skris * There is a potential gotcha to watch for: if buckets are read in a
106355714Skris * loop, and aren't deleted after being processed, the potentially large
106455714Skris * bucket will slowly be converted into RAM resident heap buckets. If
106555714Skris * the file is larger than available RAM, an out of memory condition
106655714Skris * could be caused if the application is not careful to manage this.
106759191Skris *
106859191Skris * @param e The bucket to read from
106959191Skris * @param str The location to store a pointer to the data in
107059191Skris * @param len The location to store the amount of data read
107159191Skris * @param block Whether the read function blocks
107259191Skris */
107359191Skris#define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
107459191Skris
107559191Skris/**
107659191Skris * Setaside data so that stack data is not destroyed on returning from
107755714Skris * the function
107855714Skris * @param e The bucket to setaside
107955714Skris * @param p The pool to setaside into
108055714Skris */
108155714Skris#define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
108255714Skris
108355714Skris/**
108459191Skris * Split one bucket in two at the point provided.
108555714Skris *
1086109998Smarkm * Once split, the original bucket becomes the first of the two new buckets.
1087109998Smarkm *
108855714Skris * (It is assumed that the bucket is a member of a brigade when this
108955714Skris * function is called).
109055714Skris * @param e The bucket to split
109155714Skris * @param point The offset to split the bucket at
109255714Skris */
109355714Skris#define apr_bucket_split(e,point) (e)->type->split(e, point)
109455714Skris
109555714Skris/**
109655714Skris * Copy a bucket.
109755714Skris * @param e The bucket to copy
109855714Skris * @param c Returns a pointer to the new bucket
109955714Skris */
110055714Skris#define apr_bucket_copy(e,c) (e)->type->copy(e, c)
110155714Skris
110259191Skris/* Bucket type handling */
110355714Skris
110455714Skris/**
110555714Skris * This function simply returns APR_SUCCESS to denote that the bucket does
110655714Skris * not require anything to happen for its setaside() function. This is
110755714Skris * appropriate for buckets that have "immortal" data -- the data will live
110855714Skris * at least as long as the bucket.
110955714Skris * @param data The bucket to setaside
111055714Skris * @param pool The pool defining the desired lifetime of the bucket data
111155714Skris * @return APR_SUCCESS
111255714Skris */
111355714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
111455714Skris                                                          apr_pool_t *pool);
111555714Skris
111655714Skris/**
111755714Skris * A place holder function that signifies that the setaside function was not
111855714Skris * implemented for this bucket
111955714Skris * @param data The bucket to setaside
112055714Skris * @param pool The pool defining the desired lifetime of the bucket data
112155714Skris * @return APR_ENOTIMPL
112255714Skris */
112355714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
112455714Skris                                                             apr_pool_t *pool);
112555714Skris
112655714Skris/**
112755714Skris * A place holder function that signifies that the split function was not
112855714Skris * implemented for this bucket
112955714Skris * @param data The bucket to split
113055714Skris * @param point The location to split the bucket
113155714Skris * @return APR_ENOTIMPL
113255714Skris */
113355714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
113455714Skris                                                          apr_size_t point);
113555714Skris
113655714Skris/**
113755714Skris * A place holder function that signifies that the copy function was not
113855714Skris * implemented for this bucket
113955714Skris * @param e The bucket to copy
114055714Skris * @param c Returns a pointer to the new bucket
114155714Skris * @return APR_ENOTIMPL
114255714Skris */
114355714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
114455714Skris                                                         apr_bucket **c);
114555714Skris
114655714Skris/**
114755714Skris * A place holder function that signifies that this bucket does not need
114855714Skris * to do anything special to be destroyed.  That's only the case for buckets
114955714Skris * that either have no data (metadata buckets) or buckets whose data pointer
115059191Skris * points to something that's not a bucket-type-specific structure, as with
115159191Skris * simple buckets where data points to a string and pipe buckets where data
115255714Skris * points directly to the apr_file_t.
115355714Skris * @param data The bucket data to destroy
115455714Skris */
115555714SkrisAPU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
115655714Skris
115755714Skris/**
115859191Skris * There is no apr_bucket_destroy_notimpl, because destruction is required
115959191Skris * to be implemented (it could be a noop, but only if that makes sense for
116059191Skris * the bucket type)
116159191Skris */
116259191Skris
116355714Skris/* There is no apr_bucket_read_notimpl, because it is a required function
116455714Skris */
116555714Skris
116659191Skris
116755714Skris/* All of the bucket types implemented by the core */
116855714Skris/**
116959191Skris * The flush bucket type.  This signifies that all data should be flushed to
117059191Skris * the next filter.  The flush bucket should be sent with the other buckets.
117155714Skris */
117259191SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
117359191Skris/**
117459191Skris * The EOS bucket type.  This signifies that there will be no more data, ever.
117555714Skris * All filters MUST send all data to the next filter when they receive a
117655714Skris * bucket of this type
117755714Skris */
117855714SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
117955714Skris/**
118059191Skris * The FILE bucket type.  This bucket represents a file on disk
118159191Skris */
118255714SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
118355714Skris/**
118455714Skris * The HEAP bucket type.  This bucket represents a data allocated from the
118559191Skris * heap.
118659191Skris */
118759191SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
118859191Skris#if APR_HAS_MMAP
118959191Skris/**
119059191Skris * The MMAP bucket type.  This bucket represents an MMAP'ed file
119155714Skris */
119255714SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
119355714Skris#endif
119455714Skris/**
119555714Skris * The POOL bucket type.  This bucket represents a data that was allocated
119655714Skris * from a pool.  IF this bucket is still available when the pool is cleared,
119755714Skris * the data is copied on to the heap.
119855714Skris */
119955714SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
120059191Skris/**
120159191Skris * The PIPE bucket type.  This bucket represents a pipe to another program.
120259191Skris */
120359191SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
120455714Skris/**
120555714Skris * The IMMORTAL bucket type.  This bucket represents a segment of data that
120655714Skris * the creator is willing to take responsibility for.  The core will do
120755714Skris * nothing with the data in an immortal bucket
120859191Skris */
120959191SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
121059191Skris/**
121159191Skris * The TRANSIENT bucket type.  This bucket represents a data allocated off
121255714Skris * the stack.  When the setaside function is called, this data is copied on
121359191Skris * to the heap
121459191Skris */
121559191SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
121659191Skris/**
121759191Skris * The SOCKET bucket type.  This bucket represents a socket to another machine
121859191Skris */
121955714SkrisAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
122055714Skris
122155714Skris
122255714Skris/*  *****  Simple buckets  *****  */
122355714Skris
122459191Skris/**
122559191Skris * Split a simple bucket into two at the given point.  Most non-reference
122655714Skris * counting buckets that allow multiple references to the same block of
122755714Skris * data (eg transient and immortal) will use this as their split function
122859191Skris * without any additional type-specific handling.
122959191Skris * @param b The bucket to be split
123059191Skris * @param point The offset of the first byte in the new bucket
123155714Skris * @return APR_EINVAL if the point is not within the bucket;
1232100928Snectar *         APR_ENOMEM if allocation failed;
123359191Skris *         or APR_SUCCESS
1234100928Snectar */
1235100928SnectarAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
123659191Skris                                                         apr_size_t point);
123755714Skris
123855714Skris/**
123955714Skris * Copy a simple bucket.  Most non-reference-counting buckets that allow
124055714Skris * multiple references to the same block of data (eg transient and immortal)
124155714Skris * will use this as their copy function without any additional type-specific
124255714Skris * handling.
124355714Skris * @param a The bucket to copy
1244100928Snectar * @param b Returns a pointer to the new bucket
1245100928Snectar * @return APR_ENOMEM if allocation failed;
1246100928Snectar *         or APR_SUCCESS
1247100928Snectar */
1248100928SnectarAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
1249100928Snectar                                                        apr_bucket **b);
1250100928Snectar
125159191Skris
125255714Skris/*  *****  Shared, reference-counted buckets  *****  */
125359191Skris
125459191Skris/**
125559191Skris * Initialize a bucket containing reference-counted data that may be
125659191Skris * shared. The caller must allocate the bucket if necessary and
125755714Skris * initialize its type-dependent fields, and allocate and initialize
125859191Skris * its own private data structure. This function should only be called
125955714Skris * by type-specific bucket creation functions.
126055714Skris * @param b The bucket to initialize
126155714Skris * @param data A pointer to the private data structure
126255714Skris *             with the reference count at the start
126355714Skris * @param start The start of the data in the bucket
126455714Skris *              relative to the private base pointer
126555714Skris * @param length The length of the data in the bucket
126655714Skris * @return The new bucket, or NULL if allocation failed
126755714Skris */
126855714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
126955714Skris				                 apr_off_t start,
127055714Skris                                                 apr_size_t length);
127155714Skris
127255714Skris/**
127355714Skris * Decrement the refcount of the data in the bucket. This function
127455714Skris * should only be called by type-specific bucket destruction functions.
127555714Skris * @param data The private data pointer from the bucket to be destroyed
127655714Skris * @return TRUE or FALSE; TRUE if the reference count is now
127755714Skris *         zero, indicating that the shared resource itself can
127855714Skris *         be destroyed by the caller.
127955714Skris */
128055714SkrisAPU_DECLARE(int) apr_bucket_shared_destroy(void *data);
128155714Skris
128255714Skris/**
128355714Skris * Split a bucket into two at the given point, and adjust the refcount
128455714Skris * to the underlying data. Most reference-counting bucket types will
128559191Skris * be able to use this function as their split function without any
128659191Skris * additional type-specific handling.
128755714Skris * @param b The bucket to be split
128855714Skris * @param point The offset of the first byte in the new bucket
128955714Skris * @return APR_EINVAL if the point is not within the bucket;
129055714Skris *         APR_ENOMEM if allocation failed;
129155714Skris *         or APR_SUCCESS
129255714Skris */
129355714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
129455714Skris                                                         apr_size_t point);
129555714Skris
129655714Skris/**
129755714Skris * Copy a refcounted bucket, incrementing the reference count. Most
129855714Skris * reference-counting bucket types will be able to use this function
129955714Skris * as their copy function without any additional type-specific handling.
130055714Skris * @param a The bucket to copy
130155714Skris * @param b Returns a pointer to the new bucket
130255714Skris * @return APR_ENOMEM if allocation failed;
130355714Skris           or APR_SUCCESS
130455714Skris */
130555714SkrisAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
130655714Skris                                                        apr_bucket **b);
130755714Skris
130855714Skris
130955714Skris/*  *****  Functions to Create Buckets of varying types  *****  */
131055714Skris/*
131155714Skris * Each bucket type foo has two initialization functions:
131255714Skris * apr_bucket_foo_make which sets up some already-allocated memory as a
131355714Skris * bucket of type foo; and apr_bucket_foo_create which allocates memory
131455714Skris * for the bucket, calls apr_bucket_make_foo, and initializes the
131555714Skris * bucket's list pointers. The apr_bucket_foo_make functions are used
131655714Skris * inside the bucket code to change the type of buckets in place;
131755714Skris * other code should call apr_bucket_foo_create. All the initialization
131855714Skris * functions change nothing if they fail.
131955714Skris */
132055714Skris
132155714Skris/**
132255714Skris * Create an End of Stream bucket.  This indicates that there is no more data
132355714Skris * coming from down the filter stack.  All filters should flush at this point.
132455714Skris * @param list The freelist from which this bucket should be allocated
132555714Skris * @return The new bucket, or NULL if allocation failed
132655714Skris */
132755714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
132855714Skris
132955714Skris/**
133055714Skris * Make the bucket passed in an EOS bucket.  This indicates that there is no
1331109998Smarkm * more data coming from down the filter stack.  All filters should flush at
1332109998Smarkm * this point.
1333109998Smarkm * @param b The bucket to make into an EOS bucket
133455714Skris * @return The new bucket, or NULL if allocation failed
133555714Skris */
133655714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
133755714Skris
133855714Skris/**
133955714Skris * Create a flush  bucket.  This indicates that filters should flush their
134055714Skris * data.  There is no guarantee that they will flush it, but this is the
134155714Skris * best we can do.
134255714Skris * @param list The freelist from which this bucket should be allocated
134355714Skris * @return The new bucket, or NULL if allocation failed
134455714Skris */
134555714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
134655714Skris
134755714Skris/**
134855714Skris * Make the bucket passed in a FLUSH  bucket.  This indicates that filters
134955714Skris * should flush their data.  There is no guarantee that they will flush it,
135055714Skris * but this is the best we can do.
135155714Skris * @param b The bucket to make into a FLUSH bucket
135255714Skris * @return The new bucket, or NULL if allocation failed
135355714Skris */
135455714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
135555714Skris
135655714Skris/**
135755714Skris * Create a bucket referring to long-lived data.
135855714Skris * @param buf The data to insert into the bucket
135955714Skris * @param nbyte The size of the data to insert.
136055714Skris * @param list The freelist from which this bucket should be allocated
136155714Skris * @return The new bucket, or NULL if allocation failed
136255714Skris */
136355714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
136455714Skris                                                     apr_size_t nbyte,
136555714Skris                                                     apr_bucket_alloc_t *list);
136655714Skris
136755714Skris/**
136855714Skris * Make the bucket passed in a bucket refer to long-lived data
136955714Skris * @param b The bucket to make into a IMMORTAL bucket
137055714Skris * @param buf The data to insert into the bucket
137155714Skris * @param nbyte The size of the data to insert.
137255714Skris * @return The new bucket, or NULL if allocation failed
137355714Skris */
137455714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b,
137555714Skris                                                   const char *buf,
137655714Skris                                                   apr_size_t nbyte);
137755714Skris
137855714Skris/**
137955714Skris * Create a bucket referring to data on the stack.
138055714Skris * @param buf The data to insert into the bucket
138155714Skris * @param nbyte The size of the data to insert.
138255714Skris * @param list The freelist from which this bucket should be allocated
138355714Skris * @return The new bucket, or NULL if allocation failed
138455714Skris */
138555714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
138655714Skris                                                      apr_size_t nbyte,
138755714Skris                                                      apr_bucket_alloc_t *list);
138855714Skris
138955714Skris/**
139055714Skris * Make the bucket passed in a bucket refer to stack data
139155714Skris * @param b The bucket to make into a TRANSIENT bucket
139255714Skris * @param buf The data to insert into the bucket
139355714Skris * @param nbyte The size of the data to insert.
139455714Skris * @return The new bucket, or NULL if allocation failed
139555714Skris */
139655714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b,
139755714Skris                                                    const char *buf,
139855714Skris                                                    apr_size_t nbyte);
139955714Skris
140055714Skris/**
140155714Skris * Create a bucket referring to memory on the heap. If the caller asks
140255714Skris * for the data to be copied, this function always allocates 4K of
140355714Skris * memory so that more data can be added to the bucket without
140455714Skris * requiring another allocation. Therefore not all the data may be put
140555714Skris * into the bucket. If copying is not requested then the bucket takes
140655714Skris * over responsibility for free()ing the memory.
140755714Skris * @param buf The buffer to insert into the bucket
1408160814Ssimon * @param nbyte The size of the buffer to insert.
1409160814Ssimon * @param free_func Function to use to free the data; NULL indicates that the
141055714Skris *                  bucket should make a copy of the data
141155714Skris * @param list The freelist from which this bucket should be allocated
141255714Skris * @return The new bucket, or NULL if allocation failed
141355714Skris */
141455714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
141555714Skris                                                 apr_size_t nbyte,
141655714Skris                                                 void (*free_func)(void *data),
141755714Skris                                                 apr_bucket_alloc_t *list);
141855714Skris/**
141955714Skris * Make the bucket passed in a bucket refer to heap data
142055714Skris * @param b The bucket to make into a HEAP bucket
142155714Skris * @param buf The buffer to insert into the bucket
142255714Skris * @param nbyte The size of the buffer to insert.
142355714Skris * @param free_func Function to use to free the data; NULL indicates that the
142455714Skris *                  bucket should make a copy of the data
142555714Skris * @return The new bucket, or NULL if allocation failed
142655714Skris */
142755714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
142855714Skris                                               apr_size_t nbyte,
142955714Skris                                               void (*free_func)(void *data));
143055714Skris
143155714Skris/**
143255714Skris * Create a bucket referring to memory allocated from a pool.
143355714Skris *
143455714Skris * @param buf The buffer to insert into the bucket
143555714Skris * @param length The number of bytes referred to by this bucket
143655714Skris * @param pool The pool the memory was allocated from
143755714Skris * @param list The freelist from which this bucket should be allocated
143855714Skris * @return The new bucket, or NULL if allocation failed
143955714Skris */
144055714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
144155714Skris                                                 apr_size_t length,
144255714Skris                                                 apr_pool_t *pool,
144355714Skris                                                 apr_bucket_alloc_t *list);
144455714Skris
144555714Skris/**
144655714Skris * Make the bucket passed in a bucket refer to pool data
144755714Skris * @param b The bucket to make into a pool bucket
144855714Skris * @param buf The buffer to insert into the bucket
144955714Skris * @param length The number of bytes referred to by this bucket
145055714Skris * @param pool The pool the memory was allocated from
145155714Skris * @return The new bucket, or NULL if allocation failed
145255714Skris */
145355714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
145455714Skris                                               apr_size_t length,
145555714Skris                                               apr_pool_t *pool);
145655714Skris
145755714Skris#if APR_HAS_MMAP
145855714Skris/**
145955714Skris * Create a bucket referring to mmap()ed memory.
146055714Skris * @param mm The mmap to insert into the bucket
146155714Skris * @param start The offset of the first byte in the mmap
146255714Skris *              that this bucket refers to
146355714Skris * @param length The number of bytes referred to by this bucket
146455714Skris * @param list The freelist from which this bucket should be allocated
146555714Skris * @return The new bucket, or NULL if allocation failed
146655714Skris */
146755714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
146855714Skris                                                 apr_off_t start,
146955714Skris                                                 apr_size_t length,
147055714Skris                                                 apr_bucket_alloc_t *list);
147155714Skris
147255714Skris/**
147355714Skris * Make the bucket passed in a bucket refer to an MMAP'ed file
147455714Skris * @param b The bucket to make into a MMAP bucket
147555714Skris * @param mm The mmap to insert into the bucket
147655714Skris * @param start The offset of the first byte in the mmap
147755714Skris *              that this bucket refers to
147855714Skris * @param length The number of bytes referred to by this bucket
147955714Skris * @return The new bucket, or NULL if allocation failed
148055714Skris */
148155714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
148255714Skris                                               apr_off_t start,
148355714Skris                                               apr_size_t length);
148455714Skris#endif
148555714Skris
148655714Skris/**
148755714Skris * Create a bucket referring to a socket.
148855714Skris * @param thissock The socket to put in the bucket
148955714Skris * @param list The freelist from which this bucket should be allocated
149055714Skris * @return The new bucket, or NULL if allocation failed
149155714Skris */
149255714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
149355714Skris                                                   apr_bucket_alloc_t *list);
149455714Skris/**
149555714Skris * Make the bucket passed in a bucket refer to a socket
149655714Skris * @param b The bucket to make into a SOCKET bucket
149755714Skris * @param thissock The socket to put in the bucket
149855714Skris * @return The new bucket, or NULL if allocation failed
149955714Skris */
150055714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
150155714Skris                                                 apr_socket_t *thissock);
150255714Skris
150355714Skris/**
150455714Skris * Create a bucket referring to a pipe.
150555714Skris * @param thispipe The pipe to put in the bucket
150655714Skris * @param list The freelist from which this bucket should be allocated
150755714Skris * @return The new bucket, or NULL if allocation failed
150855714Skris */
150955714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
151055714Skris                                                 apr_bucket_alloc_t *list);
151155714Skris
151255714Skris/**
151355714Skris * Make the bucket passed in a bucket refer to a pipe
151455714Skris * @param b The bucket to make into a PIPE bucket
151555714Skris * @param thispipe The pipe to put in the bucket
151655714Skris * @return The new bucket, or NULL if allocation failed
151755714Skris */
151855714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
151955714Skris                                               apr_file_t *thispipe);
152055714Skris
152155714Skris/**
152255714Skris * Create a bucket referring to a file.
152355714Skris * @param fd The file to put in the bucket
152455714Skris * @param offset The offset where the data of interest begins in the file
152555714Skris * @param len The amount of data in the file we are interested in
152655714Skris * @param p The pool into which any needed structures should be created
152755714Skris *          while reading from this file bucket
152855714Skris * @param list The freelist from which this bucket should be allocated
152955714Skris * @return The new bucket, or NULL if allocation failed
153055714Skris * @remark If the file is truncated such that the segment of the file
153155714Skris * referenced by the bucket no longer exists, an attempt to read
153255714Skris * from the bucket will fail with APR_EOF.
153355714Skris * @remark apr_brigade_insert_file() should generally be used to
153455714Skris * insert files into brigades, since that function can correctly
153555714Skris * handle large file issues.
153655714Skris */
153755714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
153855714Skris                                                 apr_off_t offset,
1539160814Ssimon                                                 apr_size_t len,
1540160814Ssimon                                                 apr_pool_t *p,
154155714Skris                                                 apr_bucket_alloc_t *list);
154255714Skris
154355714Skris/**
154455714Skris * Make the bucket passed in a bucket refer to a file
154555714Skris * @param b The bucket to make into a FILE bucket
154655714Skris * @param fd The file to put in the bucket
154755714Skris * @param offset The offset where the data of interest begins in the file
154855714Skris * @param len The amount of data in the file we are interested in
154955714Skris * @param p The pool into which any needed structures should be created
155055714Skris *          while reading from this file bucket
155155714Skris * @return The new bucket, or NULL if allocation failed
155255714Skris */
155355714SkrisAPU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
155455714Skris                                               apr_off_t offset,
155555714Skris                                               apr_size_t len, apr_pool_t *p);
155655714Skris
155755714Skris/**
155855714Skris * Enable or disable memory-mapping for a FILE bucket (default is enabled)
155955714Skris * @param b The bucket
156055714Skris * @param enabled Whether memory-mapping should be enabled
156155714Skris * @return APR_SUCCESS normally, or an error code if the operation fails
156255714Skris */
156355714SkrisAPU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
156455714Skris                                                      int enabled);
156555714Skris
156655714Skris/** @} */
156755714Skris#ifdef __cplusplus
156855714Skris}
156955714Skris#endif
157055714Skris
157155714Skris#endif /* !APR_BUCKETS_H */
157255714Skris