apr_buckets.h 56 KB

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