1251876Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251876Speter * contributor license agreements.  See the NOTICE file distributed with
3251876Speter * this work for additional information regarding copyright ownership.
4251876Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251876Speter * (the "License"); you may not use this file except in compliance with
6251876Speter * the License.  You may obtain a copy of the License at
7251876Speter *
8251876Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251876Speter *
10251876Speter * Unless required by applicable law or agreed to in writing, software
11251876Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251876Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251876Speter * See the License for the specific language governing permissions and
14251876Speter * limitations under the License.
15251876Speter */
16251876Speter/**
17251876Speter * @file apr_buckets.h
18251876Speter * @brief APR-UTIL Buckets/Bucket Brigades
19251876Speter */
20251876Speter
21251876Speter#ifndef APR_BUCKETS_H
22251876Speter#define APR_BUCKETS_H
23251876Speter
24251876Speter#if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG)
25251876Speter#define APR_RING_DEBUG
26251876Speter#endif
27251876Speter
28251876Speter#include "apu.h"
29251876Speter#include "apr_network_io.h"
30251876Speter#include "apr_file_io.h"
31251876Speter#include "apr_general.h"
32251876Speter#include "apr_mmap.h"
33251876Speter#include "apr_errno.h"
34251876Speter#include "apr_ring.h"
35251876Speter#include "apr.h"
36251876Speter#if APR_HAVE_SYS_UIO_H
37251876Speter#include <sys/uio.h>	/* for struct iovec */
38251876Speter#endif
39251876Speter#if APR_HAVE_STDARG_H
40251876Speter#include <stdarg.h>
41251876Speter#endif
42251876Speter
43251876Speter#ifdef __cplusplus
44251876Speterextern "C" {
45251876Speter#endif
46251876Speter
47251876Speter/**
48251876Speter * @defgroup APR_Util_Bucket_Brigades Bucket Brigades
49251876Speter * @ingroup APR_Util
50251876Speter * @{
51251876Speter */
52251876Speter
53251876Speter/** default bucket buffer size - 8KB minus room for memory allocator headers */
54251876Speter#define APR_BUCKET_BUFF_SIZE 8000
55251876Speter
56251876Speter/** Determines how a bucket or brigade should be read */
57251876Spetertypedef enum {
58251876Speter    APR_BLOCK_READ,   /**< block until data becomes available */
59251876Speter    APR_NONBLOCK_READ /**< return immediately if no data is available */
60251876Speter} apr_read_type_e;
61251876Speter
62251876Speter/**
63251876Speter * The one-sentence buzzword-laden overview: Bucket brigades represent
64251876Speter * a complex data stream that can be passed through a layered IO
65251876Speter * system without unnecessary copying. A longer overview follows...
66251876Speter *
67251876Speter * A bucket brigade is a doubly linked list (ring) of buckets, so we
68251876Speter * aren't limited to inserting at the front and removing at the end.
69251876Speter * Buckets are only passed around as members of a brigade, although
70251876Speter * singleton buckets can occur for short periods of time.
71251876Speter *
72251876Speter * Buckets are data stores of various types. They can refer to data in
73251876Speter * memory, or part of a file or mmap area, or the output of a process,
74251876Speter * etc. Buckets also have some type-dependent accessor functions:
75251876Speter * read, split, copy, setaside, and destroy.
76251876Speter *
77251876Speter * read returns the address and size of the data in the bucket. If the
78251876Speter * data isn't in memory then it is read in and the bucket changes type
79251876Speter * so that it can refer to the new location of the data. If all the
80251876Speter * data doesn't fit in the bucket then a new bucket is inserted into
81251876Speter * the brigade to hold the rest of it.
82251876Speter *
83251876Speter * split divides the data in a bucket into two regions. After a split
84251876Speter * the original bucket refers to the first part of the data and a new
85251876Speter * bucket inserted into the brigade after the original bucket refers
86251876Speter * to the second part of the data. Reference counts are maintained as
87251876Speter * necessary.
88251876Speter *
89251876Speter * setaside ensures that the data in the bucket has a long enough
90251876Speter * lifetime. Sometimes it is convenient to create a bucket referring
91251876Speter * to data on the stack in the expectation that it will be consumed
92251876Speter * (output to the network) before the stack is unwound. If that
93251876Speter * expectation turns out not to be valid, the setaside function is
94251876Speter * called to move the data somewhere safer.
95251876Speter *
96251876Speter * copy makes a duplicate of the bucket structure as long as it's
97251876Speter * possible to have multiple references to a single copy of the
98251876Speter * data itself.  Not all bucket types can be copied.
99251876Speter *
100251876Speter * destroy maintains the reference counts on the resources used by a
101251876Speter * bucket and frees them if necessary.
102251876Speter *
103251876Speter * Note: all of the above functions have wrapper macros (apr_bucket_read(),
104251876Speter * apr_bucket_destroy(), etc), and those macros should be used rather
105251876Speter * than using the function pointers directly.
106251876Speter *
107251876Speter * To write a bucket brigade, they are first made into an iovec, so that we
108251876Speter * don't write too little data at one time.  Currently we ignore compacting the
109251876Speter * buckets into as few buckets as possible, but if we really want good
110251876Speter * performance, then we need to compact the buckets before we convert to an
111251876Speter * iovec, or possibly while we are converting to an iovec.
112251876Speter */
113251876Speter
114251876Speter/*
115251876Speter * Forward declaration of the main types.
116251876Speter */
117251876Speter
118251876Speter/** @see apr_bucket_brigade */
119251876Spetertypedef struct apr_bucket_brigade apr_bucket_brigade;
120251876Speter/** @see apr_bucket */
121251876Spetertypedef struct apr_bucket apr_bucket;
122251876Speter/** @see apr_bucket_alloc_t */
123251876Spetertypedef struct apr_bucket_alloc_t apr_bucket_alloc_t;
124251876Speter
125251876Speter/** @see apr_bucket_type_t */
126251876Spetertypedef struct apr_bucket_type_t apr_bucket_type_t;
127251876Speter
128251876Speter/**
129251876Speter * Basic bucket type
130251876Speter */
131251876Speterstruct apr_bucket_type_t {
132251876Speter    /**
133251876Speter     * The name of the bucket type
134251876Speter     */
135251876Speter    const char *name;
136251876Speter    /**
137251876Speter     * The number of functions this bucket understands.  Can not be less than
138251876Speter     * five.
139251876Speter     */
140251876Speter    int num_func;
141251876Speter    /**
142251876Speter     * Whether the bucket contains metadata (ie, information that
143251876Speter     * describes the regular contents of the brigade).  The metadata
144251876Speter     * is not returned by apr_bucket_read() and is not indicated by
145251876Speter     * the ->length of the apr_bucket itself.  In other words, an
146251876Speter     * empty bucket is safe to arbitrarily remove if and only if it
147251876Speter     * contains no metadata.  In this sense, "data" is just raw bytes
148251876Speter     * that are the "content" of the brigade and "metadata" describes
149251876Speter     * that data but is not a proper part of it.
150251876Speter     */
151251876Speter    enum {
152251876Speter        /** This bucket type represents actual data to send to the client. */
153251876Speter        APR_BUCKET_DATA = 0,
154251876Speter        /** This bucket type represents metadata. */
155251876Speter        APR_BUCKET_METADATA = 1
156251876Speter    } is_metadata;
157251876Speter    /**
158251876Speter     * Free the private data and any resources used by the bucket (if they
159251876Speter     *  aren't shared with another bucket).  This function is required to be
160251876Speter     *  implemented for all bucket types, though it might be a no-op on some
161251876Speter     *  of them (namely ones that never allocate any private data structures).
162251876Speter     * @param data The private data pointer from the bucket to be destroyed
163251876Speter     */
164251876Speter    void (*destroy)(void *data);
165251876Speter
166251876Speter    /**
167251876Speter     * Read the data from the bucket. This is required to be implemented
168251876Speter     *  for all bucket types.
169251876Speter     * @param b The bucket to read from
170251876Speter     * @param str A place to store the data read.  Allocation should only be
171251876Speter     *            done if absolutely necessary.
172251876Speter     * @param len The amount of data read.
173251876Speter     * @param block Should this read function block if there is more data that
174251876Speter     *              cannot be read immediately.
175251876Speter     */
176251876Speter    apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len,
177251876Speter                         apr_read_type_e block);
178251876Speter
179251876Speter    /**
180251876Speter     * Make it possible to set aside the data for at least as long as the
181251876Speter     *  given pool. Buckets containing data that could potentially die before
182251876Speter     *  this pool (e.g. the data resides on the stack, in a child pool of
183251876Speter     *  the given pool, or in a disjoint pool) must somehow copy, shift, or
184251876Speter     *  transform the data to have the proper lifetime.
185251876Speter     * @param e The bucket to convert
186251876Speter     * @remark Some bucket types contain data that will always outlive the
187251876Speter     *         bucket itself. For example no data (EOS and FLUSH), or the data
188251876Speter     *         resides in global, constant memory (IMMORTAL), or the data is on
189251876Speter     *      the heap (HEAP). For these buckets, apr_bucket_setaside_noop can
190251876Speter     *      be used.
191251876Speter     */
192251876Speter    apr_status_t (*setaside)(apr_bucket *e, apr_pool_t *pool);
193251876Speter
194251876Speter    /**
195251876Speter     * Split one bucket in two at the specified position by duplicating
196251876Speter     *  the bucket structure (not the data) and modifying any necessary
197251876Speter     *  start/end/offset information.  If it's not possible to do this
198251876Speter     *  for the bucket type (perhaps the length of the data is indeterminate,
199251876Speter     *  as with pipe and socket buckets), then APR_ENOTIMPL is returned.
200251876Speter     * @param e The bucket to split
201251876Speter     * @param point The offset of the first byte in the new bucket
202251876Speter     */
203251876Speter    apr_status_t (*split)(apr_bucket *e, apr_size_t point);
204251876Speter
205251876Speter    /**
206251876Speter     * Copy the bucket structure (not the data), assuming that this is
207251876Speter     *  possible for the bucket type. If it's not, APR_ENOTIMPL is returned.
208251876Speter     * @param e The bucket to copy
209251876Speter     * @param c Returns a pointer to the new bucket
210251876Speter     */
211251876Speter    apr_status_t (*copy)(apr_bucket *e, apr_bucket **c);
212251876Speter
213251876Speter};
214251876Speter
215251876Speter/**
216251876Speter * apr_bucket structures are allocated on the malloc() heap and
217251876Speter * their lifetime is controlled by the parent apr_bucket_brigade
218251876Speter * structure. Buckets can move from one brigade to another e.g. by
219251876Speter * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has
220251876Speter * the same lifetime as the bucket and is freed when the bucket is
221251876Speter * destroyed; if the data is shared by more than one bucket (e.g.
222251876Speter * after a split) the data is freed when the last bucket goes away.
223251876Speter */
224251876Speterstruct apr_bucket {
225251876Speter    /** Links to the rest of the brigade */
226251876Speter    APR_RING_ENTRY(apr_bucket) link;
227251876Speter    /** The type of bucket.  */
228251876Speter    const apr_bucket_type_t *type;
229251876Speter    /** The length of the data in the bucket.  This could have been implemented
230251876Speter     *  with a function, but this is an optimization, because the most
231251876Speter     *  common thing to do will be to get the length.  If the length is unknown,
232251876Speter     *  the value of this field will be (apr_size_t)(-1).
233251876Speter     */
234251876Speter    apr_size_t length;
235251876Speter    /** The start of the data in the bucket relative to the private base
236251876Speter     *  pointer.  The vast majority of bucket types allow a fixed block of
237251876Speter     *  data to be referenced by multiple buckets, each bucket pointing to
238251876Speter     *  a different segment of the data.  That segment starts at base+start
239251876Speter     *  and ends at base+start+length.
240251876Speter     *  If the length == (apr_size_t)(-1), then start == -1.
241251876Speter     */
242251876Speter    apr_off_t start;
243251876Speter    /** type-dependent data hangs off this pointer */
244251876Speter    void *data;
245251876Speter    /**
246251876Speter     * Pointer to function used to free the bucket. This function should
247251876Speter     * always be defined and it should be consistent with the memory
248251876Speter     * function used to allocate the bucket. For example, if malloc() is
249251876Speter     * used to allocate the bucket, this pointer should point to free().
250251876Speter     * @param e Pointer to the bucket being freed
251251876Speter     */
252251876Speter    void (*free)(void *e);
253251876Speter    /** The freelist from which this bucket was allocated */
254251876Speter    apr_bucket_alloc_t *list;
255251876Speter};
256251876Speter
257251876Speter/** A list of buckets */
258251876Speterstruct apr_bucket_brigade {
259251876Speter    /** The pool to associate the brigade with.  The data is not allocated out
260251876Speter     *  of the pool, but a cleanup is registered with this pool.  If the
261251876Speter     *  brigade is destroyed by some mechanism other than pool destruction,
262251876Speter     *  the destroying function is responsible for killing the cleanup.
263251876Speter     */
264251876Speter    apr_pool_t *p;
265251876Speter    /** The buckets in the brigade are on this list. */
266251876Speter    /*
267251876Speter     * The apr_bucket_list structure doesn't actually need a name tag
268251876Speter     * because it has no existence independent of struct apr_bucket_brigade;
269251876Speter     * the ring macros are designed so that you can leave the name tag
270251876Speter     * argument empty in this situation but apparently the Windows compiler
271251876Speter     * doesn't like that.
272251876Speter     */
273251876Speter    APR_RING_HEAD(apr_bucket_list, apr_bucket) list;
274251876Speter    /** The freelist from which this bucket was allocated */
275251876Speter    apr_bucket_alloc_t *bucket_alloc;
276251876Speter};
277251876Speter
278251876Speter
279251876Speter/**
280251876Speter * Function called when a brigade should be flushed
281251876Speter */
282251876Spetertypedef apr_status_t (*apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx);
283251876Speter
284251876Speter/*
285251876Speter * define APR_BUCKET_DEBUG if you want your brigades to be checked for
286251876Speter * validity at every possible instant.  this will slow your code down
287251876Speter * substantially but is a very useful debugging tool.
288251876Speter */
289251876Speter#ifdef APR_BUCKET_DEBUG
290251876Speter
291251876Speter#define APR_BRIGADE_CHECK_CONSISTENCY(b)				\
292251876Speter        APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link)
293251876Speter
294251876Speter#define APR_BUCKET_CHECK_CONSISTENCY(e)					\
295251876Speter        APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
296251876Speter
297251876Speter#else
298251876Speter/**
299251876Speter * checks the ring pointers in a bucket brigade for consistency.  an
300251876Speter * abort() will be triggered if any inconsistencies are found.
301251876Speter *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
302251876Speter * @param b The brigade
303251876Speter */
304251876Speter#define APR_BRIGADE_CHECK_CONSISTENCY(b)
305251876Speter/**
306251876Speter * checks the brigade a bucket is in for ring consistency.  an
307251876Speter * abort() will be triggered if any inconsistencies are found.
308251876Speter *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
309251876Speter * @param e The bucket
310251876Speter */
311251876Speter#define APR_BUCKET_CHECK_CONSISTENCY(e)
312251876Speter#endif
313251876Speter
314251876Speter
315251876Speter/**
316251876Speter * Wrappers around the RING macros to reduce the verbosity of the code
317251876Speter * that handles bucket brigades.
318251876Speter */
319251876Speter/**
320251876Speter * The magic pointer value that indicates the head of the brigade
321251876Speter * @remark This is used to find the beginning and end of the brigade, eg:
322251876Speter * <pre>
323251876Speter *      while (e != APR_BRIGADE_SENTINEL(b)) {
324251876Speter *          ...
325251876Speter *          e = APR_BUCKET_NEXT(e);
326251876Speter *      }
327251876Speter * </pre>
328251876Speter * @param  b The brigade
329251876Speter * @return The magic pointer value
330251876Speter */
331251876Speter#define APR_BRIGADE_SENTINEL(b)	APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
332251876Speter
333251876Speter/**
334251876Speter * Determine if the bucket brigade is empty
335251876Speter * @param b The brigade to check
336251876Speter * @return true or false
337251876Speter */
338251876Speter#define APR_BRIGADE_EMPTY(b)	APR_RING_EMPTY(&(b)->list, apr_bucket, link)
339251876Speter
340251876Speter/**
341251876Speter * Return the first bucket in a brigade
342251876Speter * @param b The brigade to query
343251876Speter * @return The first bucket in the brigade
344251876Speter */
345251876Speter#define APR_BRIGADE_FIRST(b)	APR_RING_FIRST(&(b)->list)
346251876Speter/**
347251876Speter * Return the last bucket in a brigade
348251876Speter * @param b The brigade to query
349251876Speter * @return The last bucket in the brigade
350251876Speter */
351251876Speter#define APR_BRIGADE_LAST(b)	APR_RING_LAST(&(b)->list)
352251876Speter
353251876Speter/**
354262253Speter * Insert a single bucket at the front of a brigade
355251876Speter * @param b The brigade to add to
356262253Speter * @param e The bucket to insert
357251876Speter */
358251876Speter#define APR_BRIGADE_INSERT_HEAD(b, e) do {				\
359251876Speter	apr_bucket *ap__b = (e);                                        \
360251876Speter	APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link);	\
361251876Speter        APR_BRIGADE_CHECK_CONSISTENCY((b));				\
362251876Speter    } while (0)
363251876Speter
364251876Speter/**
365262253Speter * Insert a single bucket at the end of a brigade
366251876Speter * @param b The brigade to add to
367262253Speter * @param e The bucket to insert
368251876Speter */
369251876Speter#define APR_BRIGADE_INSERT_TAIL(b, e) do {				\
370251876Speter	apr_bucket *ap__b = (e);					\
371251876Speter	APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link);	\
372251876Speter        APR_BRIGADE_CHECK_CONSISTENCY((b));				\
373251876Speter    } while (0)
374251876Speter
375251876Speter/**
376251876Speter * Concatenate brigade b onto the end of brigade a, leaving brigade b empty
377251876Speter * @param a The first brigade
378251876Speter * @param b The second brigade
379251876Speter */
380251876Speter#define APR_BRIGADE_CONCAT(a, b) do {					\
381251876Speter        APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link);	\
382251876Speter        APR_BRIGADE_CHECK_CONSISTENCY((a));				\
383251876Speter    } while (0)
384251876Speter
385251876Speter/**
386251876Speter * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty
387251876Speter * @param a The first brigade
388251876Speter * @param b The second brigade
389251876Speter */
390251876Speter#define APR_BRIGADE_PREPEND(a, b) do {					\
391251876Speter        APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link);	\
392251876Speter        APR_BRIGADE_CHECK_CONSISTENCY((a));				\
393251876Speter    } while (0)
394251876Speter
395251876Speter/**
396262253Speter * Insert a single bucket before a specified bucket
397251876Speter * @param a The bucket to insert before
398262253Speter * @param b The bucket to insert
399251876Speter */
400251876Speter#define APR_BUCKET_INSERT_BEFORE(a, b) do {				\
401251876Speter	apr_bucket *ap__a = (a), *ap__b = (b);				\
402251876Speter	APR_RING_INSERT_BEFORE(ap__a, ap__b, link);			\
403251876Speter        APR_BUCKET_CHECK_CONSISTENCY(ap__a);				\
404251876Speter    } while (0)
405251876Speter
406251876Speter/**
407262253Speter * Insert a single bucket after a specified bucket
408251876Speter * @param a The bucket to insert after
409262253Speter * @param b The bucket to insert
410251876Speter */
411251876Speter#define APR_BUCKET_INSERT_AFTER(a, b) do {				\
412251876Speter	apr_bucket *ap__a = (a), *ap__b = (b);				\
413251876Speter	APR_RING_INSERT_AFTER(ap__a, ap__b, link);			\
414251876Speter        APR_BUCKET_CHECK_CONSISTENCY(ap__a);				\
415251876Speter    } while (0)
416251876Speter
417251876Speter/**
418251876Speter * Get the next bucket in the list
419251876Speter * @param e The current bucket
420251876Speter * @return The next bucket
421251876Speter */
422251876Speter#define APR_BUCKET_NEXT(e)	APR_RING_NEXT((e), link)
423251876Speter/**
424251876Speter * Get the previous bucket in the list
425251876Speter * @param e The current bucket
426251876Speter * @return The previous bucket
427251876Speter */
428251876Speter#define APR_BUCKET_PREV(e)	APR_RING_PREV((e), link)
429251876Speter
430251876Speter/**
431251876Speter * Remove a bucket from its bucket brigade
432251876Speter * @param e The bucket to remove
433251876Speter */
434251876Speter#define APR_BUCKET_REMOVE(e)	APR_RING_REMOVE((e), link)
435251876Speter
436251876Speter/**
437251876Speter * Initialize a new bucket's prev/next pointers
438251876Speter * @param e The bucket to initialize
439251876Speter */
440251876Speter#define APR_BUCKET_INIT(e)	APR_RING_ELEM_INIT((e), link)
441251876Speter
442251876Speter/**
443251876Speter * Determine if a bucket contains metadata.  An empty bucket is
444251876Speter * safe to arbitrarily remove if and only if this is false.
445251876Speter * @param e The bucket to inspect
446251876Speter * @return true or false
447251876Speter */
448251876Speter#define APR_BUCKET_IS_METADATA(e)    ((e)->type->is_metadata)
449251876Speter
450251876Speter/**
451251876Speter * Determine if a bucket is a FLUSH bucket
452251876Speter * @param e The bucket to inspect
453251876Speter * @return true or false
454251876Speter */
455251876Speter#define APR_BUCKET_IS_FLUSH(e)       ((e)->type == &apr_bucket_type_flush)
456251876Speter/**
457251876Speter * Determine if a bucket is an EOS bucket
458251876Speter * @param e The bucket to inspect
459251876Speter * @return true or false
460251876Speter */
461251876Speter#define APR_BUCKET_IS_EOS(e)         ((e)->type == &apr_bucket_type_eos)
462251876Speter/**
463251876Speter * Determine if a bucket is a FILE bucket
464251876Speter * @param e The bucket to inspect
465251876Speter * @return true or false
466251876Speter */
467251876Speter#define APR_BUCKET_IS_FILE(e)        ((e)->type == &apr_bucket_type_file)
468251876Speter/**
469251876Speter * Determine if a bucket is a PIPE bucket
470251876Speter * @param e The bucket to inspect
471251876Speter * @return true or false
472251876Speter */
473251876Speter#define APR_BUCKET_IS_PIPE(e)        ((e)->type == &apr_bucket_type_pipe)
474251876Speter/**
475251876Speter * Determine if a bucket is a SOCKET bucket
476251876Speter * @param e The bucket to inspect
477251876Speter * @return true or false
478251876Speter */
479251876Speter#define APR_BUCKET_IS_SOCKET(e)      ((e)->type == &apr_bucket_type_socket)
480251876Speter/**
481251876Speter * Determine if a bucket is a HEAP bucket
482251876Speter * @param e The bucket to inspect
483251876Speter * @return true or false
484251876Speter */
485251876Speter#define APR_BUCKET_IS_HEAP(e)        ((e)->type == &apr_bucket_type_heap)
486251876Speter/**
487251876Speter * Determine if a bucket is a TRANSIENT bucket
488251876Speter * @param e The bucket to inspect
489251876Speter * @return true or false
490251876Speter */
491251876Speter#define APR_BUCKET_IS_TRANSIENT(e)   ((e)->type == &apr_bucket_type_transient)
492251876Speter/**
493251876Speter * Determine if a bucket is a IMMORTAL bucket
494251876Speter * @param e The bucket to inspect
495251876Speter * @return true or false
496251876Speter */
497251876Speter#define APR_BUCKET_IS_IMMORTAL(e)    ((e)->type == &apr_bucket_type_immortal)
498251876Speter#if APR_HAS_MMAP
499251876Speter/**
500251876Speter * Determine if a bucket is a MMAP bucket
501251876Speter * @param e The bucket to inspect
502251876Speter * @return true or false
503251876Speter */
504251876Speter#define APR_BUCKET_IS_MMAP(e)        ((e)->type == &apr_bucket_type_mmap)
505251876Speter#endif
506251876Speter/**
507251876Speter * Determine if a bucket is a POOL bucket
508251876Speter * @param e The bucket to inspect
509251876Speter * @return true or false
510251876Speter */
511251876Speter#define APR_BUCKET_IS_POOL(e)        ((e)->type == &apr_bucket_type_pool)
512251876Speter
513251876Speter/*
514251876Speter * General-purpose reference counting for the various bucket types.
515251876Speter *
516251876Speter * Any bucket type that keeps track of the resources it uses (i.e.
517251876Speter * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
518251876Speter * attach a reference count to the resource so that it can be freed
519251876Speter * when the last bucket that uses it goes away. Resource-sharing may
520251876Speter * occur because of bucket splits or buckets that refer to globally
521251876Speter * cached data. */
522251876Speter
523251876Speter/** @see apr_bucket_refcount */
524251876Spetertypedef struct apr_bucket_refcount apr_bucket_refcount;
525251876Speter/**
526251876Speter * The structure used to manage the shared resource must start with an
527251876Speter * apr_bucket_refcount which is updated by the general-purpose refcount
528251876Speter * code. A pointer to the bucket-type-dependent private data structure
529251876Speter * can be cast to a pointer to an apr_bucket_refcount and vice versa.
530251876Speter */
531251876Speterstruct apr_bucket_refcount {
532251876Speter    /** The number of references to this bucket */
533251876Speter    int          refcount;
534251876Speter};
535251876Speter
536251876Speter/*  *****  Reference-counted bucket types  *****  */
537251876Speter
538251876Speter/** @see apr_bucket_heap */
539251876Spetertypedef struct apr_bucket_heap apr_bucket_heap;
540251876Speter/**
541251876Speter * A bucket referring to data allocated off the heap.
542251876Speter */
543251876Speterstruct apr_bucket_heap {
544251876Speter    /** Number of buckets using this memory */
545251876Speter    apr_bucket_refcount  refcount;
546251876Speter    /** The start of the data actually allocated.  This should never be
547251876Speter     * modified, it is only used to free the bucket.
548251876Speter     */
549251876Speter    char    *base;
550251876Speter    /** how much memory was allocated */
551251876Speter    apr_size_t  alloc_len;
552251876Speter    /** function to use to delete the data */
553251876Speter    void (*free_func)(void *data);
554251876Speter};
555251876Speter
556251876Speter/** @see apr_bucket_pool */
557251876Spetertypedef struct apr_bucket_pool apr_bucket_pool;
558251876Speter/**
559251876Speter * A bucket referring to data allocated from a pool
560251876Speter */
561251876Speterstruct apr_bucket_pool {
562251876Speter    /** The pool bucket must be able to be easily morphed to a heap
563251876Speter     * bucket if the pool gets cleaned up before all references are
564251876Speter     * destroyed.  This apr_bucket_heap structure is populated automatically
565251876Speter     * when the pool gets cleaned up, and subsequent calls to pool_read()
566251876Speter     * will result in the apr_bucket in question being morphed into a
567251876Speter     * regular heap bucket.  (To avoid having to do many extra refcount
568251876Speter     * manipulations and b->data manipulations, the apr_bucket_pool
569251876Speter     * struct actually *contains* the apr_bucket_heap struct that it
570251876Speter     * will become as its first element; the two share their
571251876Speter     * apr_bucket_refcount members.)
572251876Speter     */
573251876Speter    apr_bucket_heap  heap;
574251876Speter    /** The block of data actually allocated from the pool.
575251876Speter     * Segments of this block are referenced by adjusting
576251876Speter     * the start and length of the apr_bucket accordingly.
577251876Speter     * This will be NULL after the pool gets cleaned up.
578251876Speter     */
579251876Speter    const char *base;
580251876Speter    /** The pool the data was allocated from.  When the pool
581251876Speter     * is cleaned up, this gets set to NULL as an indicator
582251876Speter     * to pool_read() that the data is now on the heap and
583251876Speter     * so it should morph the bucket into a regular heap
584251876Speter     * bucket before continuing.
585251876Speter     */
586251876Speter    apr_pool_t *pool;
587251876Speter    /** The freelist this structure was allocated from, which is
588251876Speter     * needed in the cleanup phase in order to allocate space on the heap
589251876Speter     */
590251876Speter    apr_bucket_alloc_t *list;
591251876Speter};
592251876Speter
593251876Speter#if APR_HAS_MMAP
594251876Speter/** @see apr_bucket_mmap */
595251876Spetertypedef struct apr_bucket_mmap apr_bucket_mmap;
596251876Speter/**
597251876Speter * A bucket referring to an mmap()ed file
598251876Speter */
599251876Speterstruct apr_bucket_mmap {
600251876Speter    /** Number of buckets using this memory */
601251876Speter    apr_bucket_refcount  refcount;
602251876Speter    /** The mmap this sub_bucket refers to */
603251876Speter    apr_mmap_t *mmap;
604251876Speter};
605251876Speter#endif
606251876Speter
607251876Speter/** @see apr_bucket_file */
608251876Spetertypedef struct apr_bucket_file apr_bucket_file;
609251876Speter/**
610251876Speter * A bucket referring to an file
611251876Speter */
612251876Speterstruct apr_bucket_file {
613251876Speter    /** Number of buckets using this memory */
614251876Speter    apr_bucket_refcount  refcount;
615251876Speter    /** The file this bucket refers to */
616251876Speter    apr_file_t *fd;
617251876Speter    /** The pool into which any needed structures should
618251876Speter     *  be created while reading from this file bucket */
619251876Speter    apr_pool_t *readpool;
620251876Speter#if APR_HAS_MMAP
621251876Speter    /** Whether this bucket should be memory-mapped if
622251876Speter     *  a caller tries to read from it */
623251876Speter    int can_mmap;
624251876Speter#endif /* APR_HAS_MMAP */
625251876Speter};
626251876Speter
627251876Speter/** @see apr_bucket_structs */
628251876Spetertypedef union apr_bucket_structs apr_bucket_structs;
629251876Speter/**
630251876Speter * A union of all bucket structures so we know what
631251876Speter * the max size is.
632251876Speter */
633251876Speterunion apr_bucket_structs {
634251876Speter    apr_bucket      b;      /**< Bucket */
635251876Speter    apr_bucket_heap heap;   /**< Heap */
636251876Speter    apr_bucket_pool pool;   /**< Pool */
637251876Speter#if APR_HAS_MMAP
638251876Speter    apr_bucket_mmap mmap;   /**< MMap */
639251876Speter#endif
640251876Speter    apr_bucket_file file;   /**< File */
641251876Speter};
642251876Speter
643251876Speter/**
644251876Speter * The amount that apr_bucket_alloc() should allocate in the common case.
645251876Speter * Note: this is twice as big as apr_bucket_structs to allow breathing
646251876Speter * room for third-party bucket types.
647251876Speter */
648251876Speter#define APR_BUCKET_ALLOC_SIZE  APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
649251876Speter
650251876Speter/*  *****  Bucket Brigade Functions  *****  */
651251876Speter/**
652251876Speter * Create a new bucket brigade.  The bucket brigade is originally empty.
653251876Speter * @param p The pool to associate with the brigade.  Data is not allocated out
654251876Speter *          of the pool, but a cleanup is registered.
655251876Speter * @param list The bucket allocator to use
656251876Speter * @return The empty bucket brigade
657251876Speter */
658251876SpeterAPU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
659251876Speter                                                     apr_bucket_alloc_t *list);
660251876Speter
661251876Speter/**
662251876Speter * destroy an entire bucket brigade.  This includes destroying all of the
663251876Speter * buckets within the bucket brigade's bucket list.
664251876Speter * @param b The bucket brigade to destroy
665251876Speter */
666251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
667251876Speter
668251876Speter/**
669251876Speter * empty out an entire bucket brigade.  This includes destroying all of the
670251876Speter * buckets within the bucket brigade's bucket list.  This is similar to
671251876Speter * apr_brigade_destroy(), except that it does not deregister the brigade's
672251876Speter * pool cleanup function.
673251876Speter * @param data The bucket brigade to clean up
674251876Speter * @remark Generally, you should use apr_brigade_destroy().  This function
675251876Speter *         can be useful in situations where you have a single brigade that
676251876Speter *         you wish to reuse many times by destroying all of the buckets in
677251876Speter *         the brigade and putting new buckets into it later.
678251876Speter */
679251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
680251876Speter
681251876Speter/**
682251876Speter * Move the buckets from the tail end of the existing brigade @a b into
683251876Speter * the brigade @a a. If @a a is NULL a new brigade is created. Buckets
684251876Speter * from @a e to the last bucket (inclusively) of brigade @a b are moved
685251876Speter * from @a b to the returned brigade @a a.
686251876Speter *
687251876Speter * @param b The brigade to split
688251876Speter * @param e The first bucket to move
689251876Speter * @param a The brigade which should be used for the result or NULL if
690253734Speter *          a new brigade should be created. The brigade @a a will be
691253734Speter *          cleared if it is not empty.
692253734Speter * @return The brigade supplied in @a a or a new one if @a a was NULL.
693253734Speter * @warning Note that this function allocates a new brigade if @a a is
694251876Speter * NULL so memory consumption should be carefully considered.
695251876Speter */
696251876SpeterAPU_DECLARE(apr_bucket_brigade *) apr_brigade_split_ex(apr_bucket_brigade *b,
697251876Speter                                                       apr_bucket *e,
698251876Speter                                                       apr_bucket_brigade *a);
699251876Speter
700251876Speter/**
701251876Speter * Create a new bucket brigade and move the buckets from the tail end
702251876Speter * of an existing brigade into the new brigade.  Buckets from
703253734Speter * @a e to the last bucket (inclusively) of brigade @a b
704253734Speter * are moved from @a b to the returned brigade.
705251876Speter * @param b The brigade to split
706251876Speter * @param e The first bucket to move
707251876Speter * @return The new brigade
708251876Speter * @warning Note that this function always allocates a new brigade
709251876Speter * so memory consumption should be carefully considered.
710251876Speter */
711251876SpeterAPU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
712251876Speter                                                    apr_bucket *e);
713251876Speter
714251876Speter/**
715251876Speter * Partition a bucket brigade at a given offset (in bytes from the start of
716251876Speter * the brigade).  This is useful whenever a filter wants to use known ranges
717251876Speter * of bytes from the brigade; the ranges can even overlap.
718251876Speter * @param b The brigade to partition
719251876Speter * @param point The offset at which to partition the brigade
720251876Speter * @param after_point Returns a pointer to the first bucket after the partition
721251876Speter * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the
722251876Speter * brigade were shorter than @a point, or an error code.
723251876Speter * @remark if APR_INCOMPLETE is returned, @a after_point will be set to
724251876Speter * the brigade sentinel.
725251876Speter */
726251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
727251876Speter                                                apr_off_t point,
728251876Speter                                                apr_bucket **after_point);
729251876Speter
730251876Speter/**
731251876Speter * Return the total length of the brigade.
732251876Speter * @param bb The brigade to compute the length of
733251876Speter * @param read_all Read unknown-length buckets to force a size
734251876Speter * @param length Returns the length of the brigade (up to the end, or up
735251876Speter *               to a bucket read error), or -1 if the brigade has buckets
736251876Speter *               of indeterminate length and read_all is 0.
737251876Speter */
738251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
739251876Speter                                             int read_all,
740251876Speter                                             apr_off_t *length);
741251876Speter
742251876Speter/**
743251876Speter * Take a bucket brigade and store the data in a flat char*
744251876Speter * @param bb The bucket brigade to create the char* from
745251876Speter * @param c The char* to write into
746251876Speter * @param len The maximum length of the char array. On return, it is the
747251876Speter *            actual length of the char array.
748251876Speter */
749251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
750251876Speter                                              char *c,
751251876Speter                                              apr_size_t *len);
752251876Speter
753251876Speter/**
754251876Speter * Creates a pool-allocated string representing a flat bucket brigade
755251876Speter * @param bb The bucket brigade to create the char array from
756251876Speter * @param c On return, the allocated char array
757251876Speter * @param len On return, the length of the char array.
758251876Speter * @param pool The pool to allocate the string from.
759251876Speter */
760251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
761251876Speter                                               char **c,
762251876Speter                                               apr_size_t *len,
763251876Speter                                               apr_pool_t *pool);
764251876Speter
765251876Speter/**
766251876Speter * Split a brigade to represent one LF line.
767251876Speter * @param bbOut The bucket brigade that will have the LF line appended to.
768251876Speter * @param bbIn The input bucket brigade to search for a LF-line.
769251876Speter * @param block The blocking mode to be used to split the line.
770251876Speter * @param maxbytes The maximum bytes to read.  If this many bytes are seen
771251876Speter *                 without a LF, the brigade will contain a partial line.
772251876Speter */
773251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
774251876Speter                                                 apr_bucket_brigade *bbIn,
775251876Speter                                                 apr_read_type_e block,
776251876Speter                                                 apr_off_t maxbytes);
777251876Speter
778251876Speter/**
779251876Speter * Create an iovec of the elements in a bucket_brigade... return number
780251876Speter * of elements used.  This is useful for writing to a file or to the
781251876Speter * network efficiently.
782251876Speter * @param b The bucket brigade to create the iovec from
783251876Speter * @param vec The iovec to create
784251876Speter * @param nvec The number of elements in the iovec. On return, it is the
785251876Speter *             number of iovec elements actually filled out.
786251876Speter */
787251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b,
788251876Speter                                               struct iovec *vec, int *nvec);
789251876Speter
790251876Speter/**
791251876Speter * This function writes a list of strings into a bucket brigade.
792251876Speter * @param b The bucket brigade to add to
793251876Speter * @param flush The flush function to use if the brigade is full
794251876Speter * @param ctx The structure to pass to the flush function
795251876Speter * @param va A list of strings to add
796251876Speter * @return APR_SUCCESS or error code.
797251876Speter */
798251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
799251876Speter                                               apr_brigade_flush flush,
800251876Speter                                               void *ctx,
801251876Speter                                               va_list va);
802251876Speter
803251876Speter/**
804251876Speter * This function writes a string into a bucket brigade.
805251876Speter *
806251876Speter * The apr_brigade_write function attempts to be efficient with the
807251876Speter * handling of heap buckets. Regardless of the amount of data stored
808251876Speter * inside a heap bucket, heap buckets are a fixed size to promote their
809251876Speter * reuse.
810251876Speter *
811251876Speter * If an attempt is made to write a string to a brigade that already
812251876Speter * ends with a heap bucket, this function will attempt to pack the
813251876Speter * string into the remaining space in the previous heap bucket, before
814251876Speter * allocating a new heap bucket.
815251876Speter *
816251876Speter * This function always returns APR_SUCCESS, unless a flush function is
817251876Speter * passed, in which case the return value of the flush function will be
818251876Speter * returned if used.
819251876Speter * @param b The bucket brigade to add to
820251876Speter * @param flush The flush function to use if the brigade is full
821251876Speter * @param ctx The structure to pass to the flush function
822251876Speter * @param str The string to add
823251876Speter * @param nbyte The number of bytes to write
824251876Speter * @return APR_SUCCESS or error code
825251876Speter */
826251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
827251876Speter                                            apr_brigade_flush flush, void *ctx,
828251876Speter                                            const char *str, apr_size_t nbyte);
829251876Speter
830251876Speter/**
831251876Speter * This function writes multiple strings into a bucket brigade.
832251876Speter * @param b The bucket brigade to add to
833251876Speter * @param flush The flush function to use if the brigade is full
834251876Speter * @param ctx The structure to pass to the flush function
835251876Speter * @param vec The strings to add (address plus length for each)
836251876Speter * @param nvec The number of entries in iovec
837251876Speter * @return APR_SUCCESS or error code
838251876Speter */
839251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
840251876Speter                                             apr_brigade_flush flush,
841251876Speter                                             void *ctx,
842251876Speter                                             const struct iovec *vec,
843251876Speter                                             apr_size_t nvec);
844251876Speter
845251876Speter/**
846251876Speter * This function writes a string into a bucket brigade.
847251876Speter * @param bb The bucket brigade to add to
848251876Speter * @param flush The flush function to use if the brigade is full
849251876Speter * @param ctx The structure to pass to the flush function
850251876Speter * @param str The string to add
851251876Speter * @return APR_SUCCESS or error code
852251876Speter */
853251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
854251876Speter                                           apr_brigade_flush flush, void *ctx,
855251876Speter                                           const char *str);
856251876Speter
857251876Speter/**
858251876Speter * This function writes a character into a bucket brigade.
859251876Speter * @param b The bucket brigade to add to
860251876Speter * @param flush The flush function to use if the brigade is full
861251876Speter * @param ctx The structure to pass to the flush function
862251876Speter * @param c The character to add
863251876Speter * @return APR_SUCCESS or error code
864251876Speter */
865251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
866251876Speter                                           apr_brigade_flush flush, void *ctx,
867251876Speter                                           const char c);
868251876Speter
869251876Speter/**
870251876Speter * This function writes an unspecified number of strings into a bucket brigade.
871251876Speter * @param b The bucket brigade to add to
872251876Speter * @param flush The flush function to use if the brigade is full
873251876Speter * @param ctx The structure to pass to the flush function
874251876Speter * @param ... The strings to add
875251876Speter * @return APR_SUCCESS or error code
876251876Speter */
877251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
878251876Speter                                                     apr_brigade_flush flush,
879251876Speter                                                     void *ctx, ...);
880251876Speter
881251876Speter/**
882251876Speter * Evaluate a printf and put the resulting string at the end
883251876Speter * of the bucket brigade.
884251876Speter * @param b The brigade to write to
885251876Speter * @param flush The flush function to use if the brigade is full
886251876Speter * @param ctx The structure to pass to the flush function
887251876Speter * @param fmt The format of the string to write
888251876Speter * @param ... The arguments to fill out the format
889251876Speter * @return APR_SUCCESS or error code
890251876Speter */
891251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b,
892251876Speter                                                    apr_brigade_flush flush,
893251876Speter                                                    void *ctx,
894251876Speter                                                    const char *fmt, ...)
895251876Speter        __attribute__((format(printf,4,5)));
896251876Speter
897251876Speter/**
898251876Speter * Evaluate a printf and put the resulting string at the end
899251876Speter * of the bucket brigade.
900251876Speter * @param b The brigade to write to
901251876Speter * @param flush The flush function to use if the brigade is full
902251876Speter * @param ctx The structure to pass to the flush function
903251876Speter * @param fmt The format of the string to write
904251876Speter * @param va The arguments to fill out the format
905251876Speter * @return APR_SUCCESS or error code
906251876Speter */
907251876SpeterAPU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
908251876Speter                                              apr_brigade_flush flush,
909251876Speter                                              void *ctx,
910251876Speter                                              const char *fmt, va_list va);
911251876Speter
912251876Speter/**
913251876Speter * Utility function to insert a file (or a segment of a file) onto the
914251876Speter * end of the brigade.  The file is split into multiple buckets if it
915251876Speter * is larger than the maximum size which can be represented by a
916251876Speter * single bucket.
917251876Speter * @param bb the brigade to insert into
918251876Speter * @param f the file to insert
919251876Speter * @param start the offset of the start of the segment
920251876Speter * @param len the length of the segment of the file to insert
921251876Speter * @param p pool from which file buckets are allocated
922251876Speter * @return the last bucket inserted
923251876Speter */
924251876SpeterAPU_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb,
925251876Speter                                                  apr_file_t *f,
926251876Speter                                                  apr_off_t start,
927251876Speter                                                  apr_off_t len,
928251876Speter                                                  apr_pool_t *p);
929251876Speter
930251876Speter
931251876Speter
932251876Speter/*  *****  Bucket freelist functions *****  */
933251876Speter/**
934251876Speter * Create a bucket allocator.
935251876Speter * @param p This pool's underlying apr_allocator_t is used to allocate memory
936251876Speter *          for the bucket allocator.  When the pool is destroyed, the bucket
937251876Speter *          allocator's cleanup routine will free all memory that has been
938251876Speter *          allocated from it.
939251876Speter * @remark  The reason the allocator gets its memory from the pool's
940251876Speter *          apr_allocator_t rather than from the pool itself is because
941251876Speter *          the bucket allocator will free large memory blocks back to the
942251876Speter *          allocator when it's done with them, thereby preventing memory
943251876Speter *          footprint growth that would occur if we allocated from the pool.
944251876Speter * @warning The allocator must never be used by more than one thread at a time.
945251876Speter */
946251876SpeterAPU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
947251876Speter
948251876Speter/**
949251876Speter * Create a bucket allocator.
950251876Speter * @param allocator This apr_allocator_t is used to allocate both the bucket
951251876Speter *          allocator and all memory handed out by the bucket allocator.  The
952251876Speter *          caller is responsible for destroying the bucket allocator and the
953251876Speter *          apr_allocator_t -- no automatic cleanups will happen.
954251876Speter * @warning The allocator must never be used by more than one thread at a time.
955251876Speter */
956251876SpeterAPU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
957251876Speter
958251876Speter/**
959251876Speter * Destroy a bucket allocator.
960251876Speter * @param list The allocator to be destroyed
961251876Speter */
962251876SpeterAPU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
963251876Speter
964251876Speter/**
965251876Speter * Allocate memory for use by the buckets.
966251876Speter * @param size The amount to allocate.
967251876Speter * @param list The allocator from which to allocate the memory.
968251876Speter */
969251876SpeterAPU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
970251876Speter
971251876Speter/**
972251876Speter * Free memory previously allocated with apr_bucket_alloc().
973251876Speter * @param block The block of memory to be freed.
974251876Speter */
975251876SpeterAPU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
976251876Speter
977251876Speter
978251876Speter/*  *****  Bucket Functions  *****  */
979251876Speter/**
980251876Speter * Free the resources used by a bucket. If multiple buckets refer to
981251876Speter * the same resource it is freed when the last one goes away.
982251876Speter * @see apr_bucket_delete()
983251876Speter * @param e The bucket to destroy
984251876Speter */
985251876Speter#define apr_bucket_destroy(e) do {					\
986251876Speter        (e)->type->destroy((e)->data);					\
987251876Speter        (e)->free(e);							\
988251876Speter    } while (0)
989251876Speter
990251876Speter/**
991251876Speter * Delete a bucket by removing it from its brigade (if any) and then
992251876Speter * destroying it.
993251876Speter * @remark This mainly acts as an aid in avoiding code verbosity.  It is
994251876Speter * the preferred exact equivalent to:
995251876Speter * <pre>
996251876Speter *      APR_BUCKET_REMOVE(e);
997251876Speter *      apr_bucket_destroy(e);
998251876Speter * </pre>
999251876Speter * @param e The bucket to delete
1000251876Speter */
1001251876Speter#define apr_bucket_delete(e) do {					\
1002251876Speter        APR_BUCKET_REMOVE(e);						\
1003251876Speter        apr_bucket_destroy(e);						\
1004251876Speter    } while (0)
1005251876Speter
1006251876Speter/**
1007251876Speter * Read some data from the bucket.
1008251876Speter *
1009251876Speter * The apr_bucket_read function returns a convenient amount of data
1010251876Speter * from the bucket provided, writing the address and length of the
1011251876Speter * data to the pointers provided by the caller. The function tries
1012251876Speter * as hard as possible to avoid a memory copy.
1013251876Speter *
1014251876Speter * Buckets are expected to be a member of a brigade at the time they
1015251876Speter * are read.
1016251876Speter *
1017251876Speter * In typical application code, buckets are read in a loop, and after
1018251876Speter * each bucket is read and processed, it is moved or deleted from the
1019251876Speter * brigade and the next bucket read.
1020251876Speter *
1021251876Speter * The definition of "convenient" depends on the type of bucket that
1022251876Speter * is being read, and is decided by APR. In the case of memory based
1023251876Speter * buckets such as heap and immortal buckets, a pointer will be
1024251876Speter * returned to the location of the buffer containing the complete
1025251876Speter * contents of the bucket.
1026251876Speter *
1027251876Speter * Some buckets, such as the socket bucket, might have no concept
1028251876Speter * of length. If an attempt is made to read such a bucket, the
1029251876Speter * apr_bucket_read function will read a convenient amount of data
1030251876Speter * from the socket. The socket bucket is magically morphed into a
1031251876Speter * heap bucket containing the just-read data, and a new socket bucket
1032251876Speter * is inserted just after this heap bucket.
1033251876Speter *
1034251876Speter * To understand why apr_bucket_read might do this, consider the loop
1035251876Speter * described above to read and process buckets. The current bucket
1036251876Speter * is magically morphed into a heap bucket and returned to the caller.
1037251876Speter * The caller processes the data, and deletes the heap bucket, moving
1038251876Speter * onto the next bucket, the new socket bucket. This process repeats,
1039251876Speter * giving the illusion of a bucket brigade that contains potentially
1040251876Speter * infinite amounts of data. It is up to the caller to decide at what
1041251876Speter * point to stop reading buckets.
1042251876Speter *
1043251876Speter * Some buckets, such as the file bucket, might have a fixed size,
1044251876Speter * but be significantly larger than is practical to store in RAM in
1045251876Speter * one go. As with the socket bucket, if an attempt is made to read
1046251876Speter * from a file bucket, the file bucket is magically morphed into a
1047251876Speter * heap bucket containing a convenient amount of data read from the
1048251876Speter * current offset in the file. During the read, the offset will be
1049251876Speter * moved forward on the file, and a new file bucket will be inserted
1050251876Speter * directly after the current bucket representing the remainder of the
1051251876Speter * file. If the heap bucket was large enough to store the whole
1052251876Speter * remainder of the file, no more file buckets are inserted, and the
1053251876Speter * file bucket will disappear completely.
1054251876Speter *
1055251876Speter * The pattern for reading buckets described above does create the
1056251876Speter * illusion that the code is willing to swallow buckets that might be
1057251876Speter * too large for the system to handle in one go. This however is just
1058251876Speter * an illusion: APR will always ensure that large (file) or infinite
1059251876Speter * (socket) buckets are broken into convenient bite sized heap buckets
1060251876Speter * before data is returned to the caller.
1061251876Speter *
1062251876Speter * There is a potential gotcha to watch for: if buckets are read in a
1063251876Speter * loop, and aren't deleted after being processed, the potentially large
1064251876Speter * bucket will slowly be converted into RAM resident heap buckets. If
1065251876Speter * the file is larger than available RAM, an out of memory condition
1066251876Speter * could be caused if the application is not careful to manage this.
1067251876Speter *
1068251876Speter * @param e The bucket to read from
1069251876Speter * @param str The location to store a pointer to the data in
1070251876Speter * @param len The location to store the amount of data read
1071251876Speter * @param block Whether the read function blocks
1072251876Speter */
1073251876Speter#define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
1074251876Speter
1075251876Speter/**
1076251876Speter * Setaside data so that stack data is not destroyed on returning from
1077251876Speter * the function
1078251876Speter * @param e The bucket to setaside
1079251876Speter * @param p The pool to setaside into
1080251876Speter */
1081251876Speter#define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
1082251876Speter
1083251876Speter/**
1084251876Speter * Split one bucket in two at the point provided.
1085251876Speter *
1086251876Speter * Once split, the original bucket becomes the first of the two new buckets.
1087251876Speter *
1088251876Speter * (It is assumed that the bucket is a member of a brigade when this
1089251876Speter * function is called).
1090251876Speter * @param e The bucket to split
1091251876Speter * @param point The offset to split the bucket at
1092251876Speter */
1093251876Speter#define apr_bucket_split(e,point) (e)->type->split(e, point)
1094251876Speter
1095251876Speter/**
1096251876Speter * Copy a bucket.
1097251876Speter * @param e The bucket to copy
1098251876Speter * @param c Returns a pointer to the new bucket
1099251876Speter */
1100251876Speter#define apr_bucket_copy(e,c) (e)->type->copy(e, c)
1101251876Speter
1102251876Speter/* Bucket type handling */
1103251876Speter
1104251876Speter/**
1105251876Speter * This function simply returns APR_SUCCESS to denote that the bucket does
1106251876Speter * not require anything to happen for its setaside() function. This is
1107251876Speter * appropriate for buckets that have "immortal" data -- the data will live
1108251876Speter * at least as long as the bucket.
1109251876Speter * @param data The bucket to setaside
1110251876Speter * @param pool The pool defining the desired lifetime of the bucket data
1111251876Speter * @return APR_SUCCESS
1112251876Speter */
1113251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
1114251876Speter                                                          apr_pool_t *pool);
1115251876Speter
1116251876Speter/**
1117251876Speter * A place holder function that signifies that the setaside function was not
1118251876Speter * implemented for this bucket
1119251876Speter * @param data The bucket to setaside
1120251876Speter * @param pool The pool defining the desired lifetime of the bucket data
1121251876Speter * @return APR_ENOTIMPL
1122251876Speter */
1123251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
1124251876Speter                                                             apr_pool_t *pool);
1125251876Speter
1126251876Speter/**
1127251876Speter * A place holder function that signifies that the split function was not
1128251876Speter * implemented for this bucket
1129251876Speter * @param data The bucket to split
1130251876Speter * @param point The location to split the bucket
1131251876Speter * @return APR_ENOTIMPL
1132251876Speter */
1133251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
1134251876Speter                                                          apr_size_t point);
1135251876Speter
1136251876Speter/**
1137251876Speter * A place holder function that signifies that the copy function was not
1138251876Speter * implemented for this bucket
1139251876Speter * @param e The bucket to copy
1140251876Speter * @param c Returns a pointer to the new bucket
1141251876Speter * @return APR_ENOTIMPL
1142251876Speter */
1143251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
1144251876Speter                                                         apr_bucket **c);
1145251876Speter
1146251876Speter/**
1147251876Speter * A place holder function that signifies that this bucket does not need
1148251876Speter * to do anything special to be destroyed.  That's only the case for buckets
1149251876Speter * that either have no data (metadata buckets) or buckets whose data pointer
1150251876Speter * points to something that's not a bucket-type-specific structure, as with
1151251876Speter * simple buckets where data points to a string and pipe buckets where data
1152251876Speter * points directly to the apr_file_t.
1153251876Speter * @param data The bucket data to destroy
1154251876Speter */
1155251876SpeterAPU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
1156251876Speter
1157251876Speter/**
1158251876Speter * There is no apr_bucket_destroy_notimpl, because destruction is required
1159251876Speter * to be implemented (it could be a noop, but only if that makes sense for
1160251876Speter * the bucket type)
1161251876Speter */
1162251876Speter
1163251876Speter/* There is no apr_bucket_read_notimpl, because it is a required function
1164251876Speter */
1165251876Speter
1166251876Speter
1167251876Speter/* All of the bucket types implemented by the core */
1168251876Speter/**
1169251876Speter * The flush bucket type.  This signifies that all data should be flushed to
1170251876Speter * the next filter.  The flush bucket should be sent with the other buckets.
1171251876Speter */
1172251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
1173251876Speter/**
1174251876Speter * The EOS bucket type.  This signifies that there will be no more data, ever.
1175251876Speter * All filters MUST send all data to the next filter when they receive a
1176251876Speter * bucket of this type
1177251876Speter */
1178251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
1179251876Speter/**
1180251876Speter * The FILE bucket type.  This bucket represents a file on disk
1181251876Speter */
1182251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
1183251876Speter/**
1184251876Speter * The HEAP bucket type.  This bucket represents a data allocated from the
1185251876Speter * heap.
1186251876Speter */
1187251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
1188251876Speter#if APR_HAS_MMAP
1189251876Speter/**
1190251876Speter * The MMAP bucket type.  This bucket represents an MMAP'ed file
1191251876Speter */
1192251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
1193251876Speter#endif
1194251876Speter/**
1195251876Speter * The POOL bucket type.  This bucket represents a data that was allocated
1196251876Speter * from a pool.  IF this bucket is still available when the pool is cleared,
1197251876Speter * the data is copied on to the heap.
1198251876Speter */
1199251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
1200251876Speter/**
1201251876Speter * The PIPE bucket type.  This bucket represents a pipe to another program.
1202251876Speter */
1203251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
1204251876Speter/**
1205251876Speter * The IMMORTAL bucket type.  This bucket represents a segment of data that
1206251876Speter * the creator is willing to take responsibility for.  The core will do
1207251876Speter * nothing with the data in an immortal bucket
1208251876Speter */
1209251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
1210251876Speter/**
1211251876Speter * The TRANSIENT bucket type.  This bucket represents a data allocated off
1212251876Speter * the stack.  When the setaside function is called, this data is copied on
1213251876Speter * to the heap
1214251876Speter */
1215251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
1216251876Speter/**
1217251876Speter * The SOCKET bucket type.  This bucket represents a socket to another machine
1218251876Speter */
1219251876SpeterAPU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
1220251876Speter
1221251876Speter
1222251876Speter/*  *****  Simple buckets  *****  */
1223251876Speter
1224251876Speter/**
1225251876Speter * Split a simple bucket into two at the given point.  Most non-reference
1226251876Speter * counting buckets that allow multiple references to the same block of
1227251876Speter * data (eg transient and immortal) will use this as their split function
1228251876Speter * without any additional type-specific handling.
1229251876Speter * @param b The bucket to be split
1230251876Speter * @param point The offset of the first byte in the new bucket
1231251876Speter * @return APR_EINVAL if the point is not within the bucket;
1232251876Speter *         APR_ENOMEM if allocation failed;
1233251876Speter *         or APR_SUCCESS
1234251876Speter */
1235251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
1236251876Speter                                                         apr_size_t point);
1237251876Speter
1238251876Speter/**
1239251876Speter * Copy a simple bucket.  Most non-reference-counting buckets that allow
1240251876Speter * multiple references to the same block of data (eg transient and immortal)
1241251876Speter * will use this as their copy function without any additional type-specific
1242251876Speter * handling.
1243251876Speter * @param a The bucket to copy
1244251876Speter * @param b Returns a pointer to the new bucket
1245251876Speter * @return APR_ENOMEM if allocation failed;
1246251876Speter *         or APR_SUCCESS
1247251876Speter */
1248251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
1249251876Speter                                                        apr_bucket **b);
1250251876Speter
1251251876Speter
1252251876Speter/*  *****  Shared, reference-counted buckets  *****  */
1253251876Speter
1254251876Speter/**
1255251876Speter * Initialize a bucket containing reference-counted data that may be
1256251876Speter * shared. The caller must allocate the bucket if necessary and
1257251876Speter * initialize its type-dependent fields, and allocate and initialize
1258251876Speter * its own private data structure. This function should only be called
1259251876Speter * by type-specific bucket creation functions.
1260251876Speter * @param b The bucket to initialize
1261251876Speter * @param data A pointer to the private data structure
1262251876Speter *             with the reference count at the start
1263251876Speter * @param start The start of the data in the bucket
1264251876Speter *              relative to the private base pointer
1265251876Speter * @param length The length of the data in the bucket
1266251876Speter * @return The new bucket, or NULL if allocation failed
1267251876Speter */
1268251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
1269251876Speter				                 apr_off_t start,
1270251876Speter                                                 apr_size_t length);
1271251876Speter
1272251876Speter/**
1273251876Speter * Decrement the refcount of the data in the bucket. This function
1274251876Speter * should only be called by type-specific bucket destruction functions.
1275251876Speter * @param data The private data pointer from the bucket to be destroyed
1276251876Speter * @return TRUE or FALSE; TRUE if the reference count is now
1277251876Speter *         zero, indicating that the shared resource itself can
1278251876Speter *         be destroyed by the caller.
1279251876Speter */
1280251876SpeterAPU_DECLARE(int) apr_bucket_shared_destroy(void *data);
1281251876Speter
1282251876Speter/**
1283251876Speter * Split a bucket into two at the given point, and adjust the refcount
1284251876Speter * to the underlying data. Most reference-counting bucket types will
1285251876Speter * be able to use this function as their split function without any
1286251876Speter * additional type-specific handling.
1287251876Speter * @param b The bucket to be split
1288251876Speter * @param point The offset of the first byte in the new bucket
1289251876Speter * @return APR_EINVAL if the point is not within the bucket;
1290251876Speter *         APR_ENOMEM if allocation failed;
1291251876Speter *         or APR_SUCCESS
1292251876Speter */
1293251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
1294251876Speter                                                         apr_size_t point);
1295251876Speter
1296251876Speter/**
1297251876Speter * Copy a refcounted bucket, incrementing the reference count. Most
1298251876Speter * reference-counting bucket types will be able to use this function
1299251876Speter * as their copy function without any additional type-specific handling.
1300251876Speter * @param a The bucket to copy
1301251876Speter * @param b Returns a pointer to the new bucket
1302251876Speter * @return APR_ENOMEM if allocation failed;
1303251876Speter           or APR_SUCCESS
1304251876Speter */
1305251876SpeterAPU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
1306251876Speter                                                        apr_bucket **b);
1307251876Speter
1308251876Speter
1309251876Speter/*  *****  Functions to Create Buckets of varying types  *****  */
1310251876Speter/*
1311251876Speter * Each bucket type foo has two initialization functions:
1312251876Speter * apr_bucket_foo_make which sets up some already-allocated memory as a
1313251876Speter * bucket of type foo; and apr_bucket_foo_create which allocates memory
1314251876Speter * for the bucket, calls apr_bucket_make_foo, and initializes the
1315251876Speter * bucket's list pointers. The apr_bucket_foo_make functions are used
1316251876Speter * inside the bucket code to change the type of buckets in place;
1317251876Speter * other code should call apr_bucket_foo_create. All the initialization
1318251876Speter * functions change nothing if they fail.
1319251876Speter */
1320251876Speter
1321251876Speter/**
1322251876Speter * Create an End of Stream bucket.  This indicates that there is no more data
1323251876Speter * coming from down the filter stack.  All filters should flush at this point.
1324251876Speter * @param list The freelist from which this bucket should be allocated
1325251876Speter * @return The new bucket, or NULL if allocation failed
1326251876Speter */
1327251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
1328251876Speter
1329251876Speter/**
1330251876Speter * Make the bucket passed in an EOS bucket.  This indicates that there is no
1331251876Speter * more data coming from down the filter stack.  All filters should flush at
1332251876Speter * this point.
1333251876Speter * @param b The bucket to make into an EOS bucket
1334251876Speter * @return The new bucket, or NULL if allocation failed
1335251876Speter */
1336251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
1337251876Speter
1338251876Speter/**
1339251876Speter * Create a flush  bucket.  This indicates that filters should flush their
1340251876Speter * data.  There is no guarantee that they will flush it, but this is the
1341251876Speter * best we can do.
1342251876Speter * @param list The freelist from which this bucket should be allocated
1343251876Speter * @return The new bucket, or NULL if allocation failed
1344251876Speter */
1345251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
1346251876Speter
1347251876Speter/**
1348251876Speter * Make the bucket passed in a FLUSH  bucket.  This indicates that filters
1349251876Speter * should flush their data.  There is no guarantee that they will flush it,
1350251876Speter * but this is the best we can do.
1351251876Speter * @param b The bucket to make into a FLUSH bucket
1352251876Speter * @return The new bucket, or NULL if allocation failed
1353251876Speter */
1354251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
1355251876Speter
1356251876Speter/**
1357251876Speter * Create a bucket referring to long-lived data.
1358251876Speter * @param buf The data to insert into the bucket
1359251876Speter * @param nbyte The size of the data to insert.
1360251876Speter * @param list The freelist from which this bucket should be allocated
1361251876Speter * @return The new bucket, or NULL if allocation failed
1362251876Speter */
1363251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
1364251876Speter                                                     apr_size_t nbyte,
1365251876Speter                                                     apr_bucket_alloc_t *list);
1366251876Speter
1367251876Speter/**
1368251876Speter * Make the bucket passed in a bucket refer to long-lived data
1369251876Speter * @param b The bucket to make into a IMMORTAL bucket
1370251876Speter * @param buf The data to insert into the bucket
1371251876Speter * @param nbyte The size of the data to insert.
1372251876Speter * @return The new bucket, or NULL if allocation failed
1373251876Speter */
1374251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b,
1375251876Speter                                                   const char *buf,
1376251876Speter                                                   apr_size_t nbyte);
1377251876Speter
1378251876Speter/**
1379251876Speter * Create a bucket referring to data on the stack.
1380251876Speter * @param buf The data to insert into the bucket
1381251876Speter * @param nbyte The size of the data to insert.
1382251876Speter * @param list The freelist from which this bucket should be allocated
1383251876Speter * @return The new bucket, or NULL if allocation failed
1384251876Speter */
1385251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
1386251876Speter                                                      apr_size_t nbyte,
1387251876Speter                                                      apr_bucket_alloc_t *list);
1388251876Speter
1389251876Speter/**
1390251876Speter * Make the bucket passed in a bucket refer to stack data
1391251876Speter * @param b The bucket to make into a TRANSIENT bucket
1392251876Speter * @param buf The data to insert into the bucket
1393251876Speter * @param nbyte The size of the data to insert.
1394251876Speter * @return The new bucket, or NULL if allocation failed
1395251876Speter */
1396251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b,
1397251876Speter                                                    const char *buf,
1398251876Speter                                                    apr_size_t nbyte);
1399251876Speter
1400251876Speter/**
1401251876Speter * Create a bucket referring to memory on the heap. If the caller asks
1402251876Speter * for the data to be copied, this function always allocates 4K of
1403251876Speter * memory so that more data can be added to the bucket without
1404251876Speter * requiring another allocation. Therefore not all the data may be put
1405251876Speter * into the bucket. If copying is not requested then the bucket takes
1406251876Speter * over responsibility for free()ing the memory.
1407251876Speter * @param buf The buffer to insert into the bucket
1408251876Speter * @param nbyte The size of the buffer to insert.
1409251876Speter * @param free_func Function to use to free the data; NULL indicates that the
1410251876Speter *                  bucket should make a copy of the data
1411251876Speter * @param list The freelist from which this bucket should be allocated
1412251876Speter * @return The new bucket, or NULL if allocation failed
1413251876Speter */
1414251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
1415251876Speter                                                 apr_size_t nbyte,
1416251876Speter                                                 void (*free_func)(void *data),
1417251876Speter                                                 apr_bucket_alloc_t *list);
1418251876Speter/**
1419251876Speter * Make the bucket passed in a bucket refer to heap data
1420251876Speter * @param b The bucket to make into a HEAP bucket
1421251876Speter * @param buf The buffer to insert into the bucket
1422251876Speter * @param nbyte The size of the buffer to insert.
1423251876Speter * @param free_func Function to use to free the data; NULL indicates that the
1424251876Speter *                  bucket should make a copy of the data
1425251876Speter * @return The new bucket, or NULL if allocation failed
1426251876Speter */
1427251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
1428251876Speter                                               apr_size_t nbyte,
1429251876Speter                                               void (*free_func)(void *data));
1430251876Speter
1431251876Speter/**
1432251876Speter * Create a bucket referring to memory allocated from a pool.
1433251876Speter *
1434251876Speter * @param buf The buffer to insert into the bucket
1435251876Speter * @param length The number of bytes referred to by this bucket
1436251876Speter * @param pool The pool the memory was allocated from
1437251876Speter * @param list The freelist from which this bucket should be allocated
1438251876Speter * @return The new bucket, or NULL if allocation failed
1439251876Speter */
1440251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
1441251876Speter                                                 apr_size_t length,
1442251876Speter                                                 apr_pool_t *pool,
1443251876Speter                                                 apr_bucket_alloc_t *list);
1444251876Speter
1445251876Speter/**
1446251876Speter * Make the bucket passed in a bucket refer to pool data
1447251876Speter * @param b The bucket to make into a pool bucket
1448251876Speter * @param buf The buffer to insert into the bucket
1449251876Speter * @param length The number of bytes referred to by this bucket
1450251876Speter * @param pool The pool the memory was allocated from
1451251876Speter * @return The new bucket, or NULL if allocation failed
1452251876Speter */
1453251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
1454251876Speter                                               apr_size_t length,
1455251876Speter                                               apr_pool_t *pool);
1456251876Speter
1457251876Speter#if APR_HAS_MMAP
1458251876Speter/**
1459251876Speter * Create a bucket referring to mmap()ed memory.
1460251876Speter * @param mm The mmap to insert into the bucket
1461251876Speter * @param start The offset of the first byte in the mmap
1462251876Speter *              that this bucket refers to
1463251876Speter * @param length The number of bytes referred to by this bucket
1464251876Speter * @param list The freelist from which this bucket should be allocated
1465251876Speter * @return The new bucket, or NULL if allocation failed
1466251876Speter */
1467251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
1468251876Speter                                                 apr_off_t start,
1469251876Speter                                                 apr_size_t length,
1470251876Speter                                                 apr_bucket_alloc_t *list);
1471251876Speter
1472251876Speter/**
1473251876Speter * Make the bucket passed in a bucket refer to an MMAP'ed file
1474251876Speter * @param b The bucket to make into a MMAP bucket
1475251876Speter * @param mm The mmap to insert into the bucket
1476251876Speter * @param start The offset of the first byte in the mmap
1477251876Speter *              that this bucket refers to
1478251876Speter * @param length The number of bytes referred to by this bucket
1479251876Speter * @return The new bucket, or NULL if allocation failed
1480251876Speter */
1481251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
1482251876Speter                                               apr_off_t start,
1483251876Speter                                               apr_size_t length);
1484251876Speter#endif
1485251876Speter
1486251876Speter/**
1487251876Speter * Create a bucket referring to a socket.
1488251876Speter * @param thissock The socket to put in the bucket
1489251876Speter * @param list The freelist from which this bucket should be allocated
1490251876Speter * @return The new bucket, or NULL if allocation failed
1491251876Speter */
1492251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
1493251876Speter                                                   apr_bucket_alloc_t *list);
1494251876Speter/**
1495251876Speter * Make the bucket passed in a bucket refer to a socket
1496251876Speter * @param b The bucket to make into a SOCKET bucket
1497251876Speter * @param thissock The socket to put in the bucket
1498251876Speter * @return The new bucket, or NULL if allocation failed
1499251876Speter */
1500251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
1501251876Speter                                                 apr_socket_t *thissock);
1502251876Speter
1503251876Speter/**
1504251876Speter * Create a bucket referring to a pipe.
1505251876Speter * @param thispipe The pipe to put in the bucket
1506251876Speter * @param list The freelist from which this bucket should be allocated
1507251876Speter * @return The new bucket, or NULL if allocation failed
1508251876Speter */
1509251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
1510251876Speter                                                 apr_bucket_alloc_t *list);
1511251876Speter
1512251876Speter/**
1513251876Speter * Make the bucket passed in a bucket refer to a pipe
1514251876Speter * @param b The bucket to make into a PIPE bucket
1515251876Speter * @param thispipe The pipe to put in the bucket
1516251876Speter * @return The new bucket, or NULL if allocation failed
1517251876Speter */
1518251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
1519251876Speter                                               apr_file_t *thispipe);
1520251876Speter
1521251876Speter/**
1522251876Speter * Create a bucket referring to a file.
1523251876Speter * @param fd The file to put in the bucket
1524251876Speter * @param offset The offset where the data of interest begins in the file
1525251876Speter * @param len The amount of data in the file we are interested in
1526251876Speter * @param p The pool into which any needed structures should be created
1527251876Speter *          while reading from this file bucket
1528251876Speter * @param list The freelist from which this bucket should be allocated
1529251876Speter * @return The new bucket, or NULL if allocation failed
1530251876Speter * @remark If the file is truncated such that the segment of the file
1531251876Speter * referenced by the bucket no longer exists, an attempt to read
1532251876Speter * from the bucket will fail with APR_EOF.
1533251876Speter * @remark apr_brigade_insert_file() should generally be used to
1534251876Speter * insert files into brigades, since that function can correctly
1535251876Speter * handle large file issues.
1536251876Speter */
1537251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
1538251876Speter                                                 apr_off_t offset,
1539251876Speter                                                 apr_size_t len,
1540251876Speter                                                 apr_pool_t *p,
1541251876Speter                                                 apr_bucket_alloc_t *list);
1542251876Speter
1543251876Speter/**
1544251876Speter * Make the bucket passed in a bucket refer to a file
1545251876Speter * @param b The bucket to make into a FILE bucket
1546251876Speter * @param fd The file to put in the bucket
1547251876Speter * @param offset The offset where the data of interest begins in the file
1548251876Speter * @param len The amount of data in the file we are interested in
1549251876Speter * @param p The pool into which any needed structures should be created
1550251876Speter *          while reading from this file bucket
1551251876Speter * @return The new bucket, or NULL if allocation failed
1552251876Speter */
1553251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
1554251876Speter                                               apr_off_t offset,
1555251876Speter                                               apr_size_t len, apr_pool_t *p);
1556251876Speter
1557251876Speter/**
1558251876Speter * Enable or disable memory-mapping for a FILE bucket (default is enabled)
1559251876Speter * @param b The bucket
1560251876Speter * @param enabled Whether memory-mapping should be enabled
1561251876Speter * @return APR_SUCCESS normally, or an error code if the operation fails
1562251876Speter */
1563251876SpeterAPU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
1564251876Speter                                                      int enabled);
1565251876Speter
1566251876Speter/** @} */
1567251876Speter#ifdef __cplusplus
1568251876Speter}
1569251876Speter#endif
1570251876Speter
1571251876Speter#endif /* !APR_BUCKETS_H */
1572