apr_buckets.h 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file apr_buckets.h
  18. * @brief APR-UTIL Buckets/Bucket Brigades
  19. */
  20. #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 single bucket at the front of a brigade
  325. * @param b The brigade to add to
  326. * @param e The bucket 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 single bucket at the end of a brigade
  335. * @param b The brigade to add to
  336. * @param e The bucket 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 single bucket before a specified bucket
  363. * @param a The bucket to insert before
  364. * @param b The bucket 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 single bucket after a specified bucket
  373. * @param a The bucket to insert after
  374. * @param b The bucket 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. /** File read block size */
  579. apr_size_t read_size;
  580. };
  581. /** @see apr_bucket_structs */
  582. typedef union apr_bucket_structs apr_bucket_structs;
  583. /**
  584. * A union of all bucket structures so we know what
  585. * the max size is.
  586. */
  587. union apr_bucket_structs {
  588. apr_bucket b; /**< Bucket */
  589. apr_bucket_heap heap; /**< Heap */
  590. apr_bucket_pool pool; /**< Pool */
  591. #if APR_HAS_MMAP
  592. apr_bucket_mmap mmap; /**< MMap */
  593. #endif
  594. apr_bucket_file file; /**< File */
  595. };
  596. /**
  597. * The amount that apr_bucket_alloc() should allocate in the common case.
  598. * Note: this is twice as big as apr_bucket_structs to allow breathing
  599. * room for third-party bucket types.
  600. */
  601. #define APR_BUCKET_ALLOC_SIZE APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
  602. /* ***** Bucket Brigade Functions ***** */
  603. /**
  604. * Create a new bucket brigade. The bucket brigade is originally empty.
  605. * @param p The pool to associate with the brigade. Data is not allocated out
  606. * of the pool, but a cleanup is registered.
  607. * @param list The bucket allocator to use
  608. * @return The empty bucket brigade
  609. */
  610. APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
  611. apr_bucket_alloc_t *list);
  612. /**
  613. * destroy an entire bucket brigade. This includes destroying all of the
  614. * buckets within the bucket brigade's bucket list.
  615. * @param b The bucket brigade to destroy
  616. */
  617. APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
  618. /**
  619. * empty out an entire bucket brigade. This includes destroying all of the
  620. * buckets within the bucket brigade's bucket list. This is similar to
  621. * apr_brigade_destroy(), except that it does not deregister the brigade's
  622. * pool cleanup function.
  623. * @param data The bucket brigade to clean up
  624. * @remark Generally, you should use apr_brigade_destroy(). This function
  625. * can be useful in situations where you have a single brigade that
  626. * you wish to reuse many times by destroying all of the buckets in
  627. * the brigade and putting new buckets into it later.
  628. */
  629. APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
  630. /**
  631. * Move the buckets from the tail end of the existing brigade @a b into
  632. * the brigade @a a. If @a a is NULL a new brigade is created. Buckets
  633. * from @a e to the last bucket (inclusively) of brigade @a b are moved
  634. * from @a b to the returned brigade @a a.
  635. *
  636. * @param b The brigade to split
  637. * @param e The first bucket to move
  638. * @param a The brigade which should be used for the result or NULL if
  639. * a new brigade should be created. The brigade @a a will be
  640. * cleared if it is not empty.
  641. * @return The brigade supplied in @a a or a new one if @a a was NULL.
  642. * @warning Note that this function allocates a new brigade if @a a is
  643. * NULL so memory consumption should be carefully considered.
  644. */
  645. APU_DECLARE(apr_bucket_brigade *) apr_brigade_split_ex(apr_bucket_brigade *b,
  646. apr_bucket *e,
  647. apr_bucket_brigade *a);
  648. /**
  649. * Create a new bucket brigade and move the buckets from the tail end
  650. * of an existing brigade into the new brigade. Buckets from
  651. * @a e to the last bucket (inclusively) of brigade @a b
  652. * are moved from @a b to the returned brigade.
  653. * @param b The brigade to split
  654. * @param e The first bucket to move
  655. * @return The new brigade
  656. * @warning Note that this function always allocates a new brigade
  657. * so memory consumption should be carefully considered.
  658. */
  659. APU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
  660. apr_bucket *e);
  661. /**
  662. * Partition a bucket brigade at a given offset (in bytes from the start of
  663. * the brigade). This is useful whenever a filter wants to use known ranges
  664. * of bytes from the brigade; the ranges can even overlap.
  665. * @param b The brigade to partition
  666. * @param point The offset at which to partition the brigade
  667. * @param after_point Returns a pointer to the first bucket after the partition
  668. * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the
  669. * brigade were shorter than @a point, or an error code.
  670. * @remark if APR_INCOMPLETE is returned, @a after_point will be set to
  671. * the brigade sentinel.
  672. */
  673. APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
  674. apr_off_t point,
  675. apr_bucket **after_point);
  676. /**
  677. * Return the total length of the brigade.
  678. * @param bb The brigade to compute the length of
  679. * @param read_all Read unknown-length buckets to force a size
  680. * @param length Returns the length of the brigade (up to the end, or up
  681. * to a bucket read error), or -1 if the brigade has buckets
  682. * of indeterminate length and read_all is 0.
  683. */
  684. APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
  685. int read_all,
  686. apr_off_t *length);
  687. /**
  688. * Take a bucket brigade and store the data in a flat char*
  689. * @param bb The bucket brigade to create the char* from
  690. * @param c The char* to write into
  691. * @param len The maximum length of the char array. On return, it is the
  692. * actual length of the char array.
  693. */
  694. APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
  695. char *c,
  696. apr_size_t *len);
  697. /**
  698. * Creates a pool-allocated string representing a flat bucket brigade
  699. * @param bb The bucket brigade to create the char array from
  700. * @param c On return, the allocated char array
  701. * @param len On return, the length of the char array.
  702. * @param pool The pool to allocate the string from.
  703. */
  704. APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
  705. char **c,
  706. apr_size_t *len,
  707. apr_pool_t *pool);
  708. /**
  709. * Split a brigade to represent one LF line.
  710. * @param bbOut The bucket brigade that will have the LF line appended to.
  711. * @param bbIn The input bucket brigade to search for a LF-line.
  712. * @param block The blocking mode to be used to split the line.
  713. * @param maxbytes The maximum bytes to read. If this many bytes are seen
  714. * without a LF, the brigade will contain a partial line.
  715. */
  716. APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
  717. apr_bucket_brigade *bbIn,
  718. apr_read_type_e block,
  719. apr_off_t maxbytes);
  720. /**
  721. * Create an iovec of the elements in a bucket_brigade... return number
  722. * of elements used. This is useful for writing to a file or to the
  723. * network efficiently.
  724. * @param b The bucket brigade to create the iovec from
  725. * @param vec The iovec to create
  726. * @param nvec The number of elements in the iovec. On return, it is the
  727. * number of iovec elements actually filled out.
  728. */
  729. APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b,
  730. struct iovec *vec, int *nvec);
  731. /**
  732. * This function writes a list of strings into a bucket brigade.
  733. * @param b The bucket brigade to add to
  734. * @param flush The flush function to use if the brigade is full
  735. * @param ctx The structure to pass to the flush function
  736. * @param va A list of strings to add
  737. * @return APR_SUCCESS or error code.
  738. */
  739. APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
  740. apr_brigade_flush flush,
  741. void *ctx,
  742. va_list va);
  743. /**
  744. * This function writes a string into a bucket brigade.
  745. *
  746. * The apr_brigade_write function attempts to be efficient with the
  747. * handling of heap buckets. Regardless of the amount of data stored
  748. * inside a heap bucket, heap buckets are a fixed size to promote their
  749. * reuse.
  750. *
  751. * If an attempt is made to write a string to a brigade that already
  752. * ends with a heap bucket, this function will attempt to pack the
  753. * string into the remaining space in the previous heap bucket, before
  754. * allocating a new heap bucket.
  755. *
  756. * This function always returns APR_SUCCESS, unless a flush function is
  757. * passed, in which case the return value of the flush function will be
  758. * returned if used.
  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 str The string to add
  763. * @param nbyte The number of bytes to write
  764. * @return APR_SUCCESS or error code
  765. */
  766. APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
  767. apr_brigade_flush flush, void *ctx,
  768. const char *str, apr_size_t nbyte);
  769. /**
  770. * This function writes multiple strings into a bucket brigade.
  771. * @param b The bucket brigade to add to
  772. * @param flush The flush function to use if the brigade is full
  773. * @param ctx The structure to pass to the flush function
  774. * @param vec The strings to add (address plus length for each)
  775. * @param nvec The number of entries in iovec
  776. * @return APR_SUCCESS or error code
  777. */
  778. APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
  779. apr_brigade_flush flush,
  780. void *ctx,
  781. const struct iovec *vec,
  782. apr_size_t nvec);
  783. /**
  784. * This function writes a string into a bucket brigade.
  785. * @param bb The bucket brigade to add to
  786. * @param flush The flush function to use if the brigade is full
  787. * @param ctx The structure to pass to the flush function
  788. * @param str The string to add
  789. * @return APR_SUCCESS or error code
  790. */
  791. APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
  792. apr_brigade_flush flush, void *ctx,
  793. const char *str);
  794. /**
  795. * This function writes a character into a bucket brigade.
  796. * @param b The bucket brigade to add to
  797. * @param flush The flush function to use if the brigade is full
  798. * @param ctx The structure to pass to the flush function
  799. * @param c The character to add
  800. * @return APR_SUCCESS or error code
  801. */
  802. APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
  803. apr_brigade_flush flush, void *ctx,
  804. const char c);
  805. /**
  806. * This function writes an unspecified number of strings into a bucket brigade.
  807. * @param b The bucket brigade to add to
  808. * @param flush The flush function to use if the brigade is full
  809. * @param ctx The structure to pass to the flush function
  810. * @param ... The strings to add
  811. * @return APR_SUCCESS or error code
  812. */
  813. APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
  814. apr_brigade_flush flush,
  815. void *ctx, ...);
  816. /**
  817. * Evaluate a printf and put the resulting string at the end
  818. * of the bucket brigade.
  819. * @param b The brigade to write to
  820. * @param flush The flush function to use if the brigade is full
  821. * @param ctx The structure to pass to the flush function
  822. * @param fmt The format of the string to write
  823. * @param ... The arguments to fill out the format
  824. * @return APR_SUCCESS or error code
  825. */
  826. APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b,
  827. apr_brigade_flush flush,
  828. void *ctx,
  829. const char *fmt, ...)
  830. __attribute__((format(printf,4,5)));
  831. /**
  832. * Evaluate a printf and put the resulting string at the end
  833. * of the bucket brigade.
  834. * @param b The brigade to write to
  835. * @param flush The flush function to use if the brigade is full
  836. * @param ctx The structure to pass to the flush function
  837. * @param fmt The format of the string to write
  838. * @param va The arguments to fill out the format
  839. * @return APR_SUCCESS or error code
  840. */
  841. APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
  842. apr_brigade_flush flush,
  843. void *ctx,
  844. const char *fmt, va_list va);
  845. /**
  846. * Utility function to insert a file (or a segment of a file) onto the
  847. * end of the brigade. The file is split into multiple buckets if it
  848. * is larger than the maximum size which can be represented by a
  849. * single bucket.
  850. * @param bb the brigade to insert into
  851. * @param f the file to insert
  852. * @param start the offset of the start of the segment
  853. * @param len the length of the segment of the file to insert
  854. * @param p pool from which file buckets are allocated
  855. * @return the last bucket inserted
  856. */
  857. APU_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb,
  858. apr_file_t *f,
  859. apr_off_t start,
  860. apr_off_t len,
  861. apr_pool_t *p);
  862. /* ***** Bucket freelist functions ***** */
  863. /**
  864. * Create a bucket allocator.
  865. * @param p This pool's underlying apr_allocator_t is used to allocate memory
  866. * for the bucket allocator. When the pool is destroyed, the bucket
  867. * allocator's cleanup routine will free all memory that has been
  868. * allocated from it.
  869. * @remark The reason the allocator gets its memory from the pool's
  870. * apr_allocator_t rather than from the pool itself is because
  871. * the bucket allocator will free large memory blocks back to the
  872. * allocator when it's done with them, thereby preventing memory
  873. * footprint growth that would occur if we allocated from the pool.
  874. * @warning The allocator must never be used by more than one thread at a time.
  875. */
  876. APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
  877. /**
  878. * Create a bucket allocator.
  879. * @param allocator This apr_allocator_t is used to allocate both the bucket
  880. * allocator and all memory handed out by the bucket allocator. The
  881. * caller is responsible for destroying the bucket allocator and the
  882. * apr_allocator_t -- no automatic cleanups will happen.
  883. * @warning The allocator must never be used by more than one thread at a time.
  884. */
  885. APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
  886. /**
  887. * Destroy a bucket allocator.
  888. * @param list The allocator to be destroyed
  889. */
  890. APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
  891. /**
  892. * Get the aligned size corresponding to the requested size, but minus the
  893. * allocator(s) overhead such that the allocation would remain in the
  894. * same boundary.
  895. * @param list The allocator from which to the memory would be allocated.
  896. * @param size The requested size.
  897. * @return The corresponding aligned/floored size.
  898. */
  899. APU_DECLARE_NONSTD(apr_size_t) apr_bucket_alloc_aligned_floor(apr_bucket_alloc_t *list,
  900. apr_size_t size)
  901. __attribute__((nonnull(1)));
  902. /**
  903. * Allocate memory for use by the buckets.
  904. * @param size The amount to allocate.
  905. * @param list The allocator from which to allocate the memory.
  906. */
  907. APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
  908. /**
  909. * Free memory previously allocated with apr_bucket_alloc().
  910. * @param block The block of memory to be freed.
  911. */
  912. APU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
  913. /* ***** Bucket Functions ***** */
  914. /**
  915. * Free the resources used by a bucket. If multiple buckets refer to
  916. * the same resource it is freed when the last one goes away.
  917. * @see apr_bucket_delete()
  918. * @param e The bucket to destroy
  919. */
  920. #define apr_bucket_destroy(e) do { \
  921. apr_bucket *apr__d = (e); \
  922. apr__d->type->destroy(apr__d->data); \
  923. apr__d->free(apr__d); \
  924. } while (0)
  925. /**
  926. * Delete a bucket by removing it from its brigade (if any) and then
  927. * destroying it.
  928. * @remark This mainly acts as an aid in avoiding code verbosity. It is
  929. * the preferred exact equivalent to:
  930. * <pre>
  931. * APR_BUCKET_REMOVE(e);
  932. * apr_bucket_destroy(e);
  933. * </pre>
  934. * @param e The bucket to delete
  935. */
  936. #define apr_bucket_delete(e) do { \
  937. apr_bucket *apr__b = (e); \
  938. APR_BUCKET_REMOVE(apr__b); \
  939. apr_bucket_destroy(apr__b); \
  940. } while (0)
  941. /**
  942. * Read some data from the bucket.
  943. *
  944. * The apr_bucket_read function returns a convenient amount of data
  945. * from the bucket provided, writing the address and length of the
  946. * data to the pointers provided by the caller. The function tries
  947. * as hard as possible to avoid a memory copy.
  948. *
  949. * Buckets are expected to be a member of a brigade at the time they
  950. * are read.
  951. *
  952. * In typical application code, buckets are read in a loop, and after
  953. * each bucket is read and processed, it is moved or deleted from the
  954. * brigade and the next bucket read.
  955. *
  956. * The definition of "convenient" depends on the type of bucket that
  957. * is being read, and is decided by APR. In the case of memory based
  958. * buckets such as heap and immortal buckets, a pointer will be
  959. * returned to the location of the buffer containing the complete
  960. * contents of the bucket.
  961. *
  962. * Some buckets, such as the socket bucket, might have no concept
  963. * of length. If an attempt is made to read such a bucket, the
  964. * apr_bucket_read function will read a convenient amount of data
  965. * from the socket. The socket bucket is magically morphed into a
  966. * heap bucket containing the just-read data, and a new socket bucket
  967. * is inserted just after this heap bucket.
  968. *
  969. * To understand why apr_bucket_read might do this, consider the loop
  970. * described above to read and process buckets. The current bucket
  971. * is magically morphed into a heap bucket and returned to the caller.
  972. * The caller processes the data, and deletes the heap bucket, moving
  973. * onto the next bucket, the new socket bucket. This process repeats,
  974. * giving the illusion of a bucket brigade that contains potentially
  975. * infinite amounts of data. It is up to the caller to decide at what
  976. * point to stop reading buckets.
  977. *
  978. * Some buckets, such as the file bucket, might have a fixed size,
  979. * but be significantly larger than is practical to store in RAM in
  980. * one go. As with the socket bucket, if an attempt is made to read
  981. * from a file bucket, the file bucket is magically morphed into a
  982. * heap bucket containing a convenient amount of data read from the
  983. * current offset in the file. During the read, the offset will be
  984. * moved forward on the file, and a new file bucket will be inserted
  985. * directly after the current bucket representing the remainder of the
  986. * file. If the heap bucket was large enough to store the whole
  987. * remainder of the file, no more file buckets are inserted, and the
  988. * file bucket will disappear completely.
  989. *
  990. * The pattern for reading buckets described above does create the
  991. * illusion that the code is willing to swallow buckets that might be
  992. * too large for the system to handle in one go. This however is just
  993. * an illusion: APR will always ensure that large (file) or infinite
  994. * (socket) buckets are broken into convenient bite sized heap buckets
  995. * before data is returned to the caller.
  996. *
  997. * There is a potential gotcha to watch for: if buckets are read in a
  998. * loop, and aren't deleted after being processed, the potentially large
  999. * bucket will slowly be converted into RAM resident heap buckets. If
  1000. * the file is larger than available RAM, an out of memory condition
  1001. * could be caused if the application is not careful to manage this.
  1002. *
  1003. * @param e The bucket to read from
  1004. * @param str The location to store a pointer to the data in
  1005. * @param len The location to store the amount of data read
  1006. * @param block Whether the read function blocks
  1007. */
  1008. #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
  1009. /**
  1010. * Setaside data so that stack data is not destroyed on returning from
  1011. * the function
  1012. * @param e The bucket to setaside
  1013. * @param p The pool to setaside into
  1014. */
  1015. #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
  1016. /**
  1017. * Split one bucket in two at the point provided.
  1018. *
  1019. * Once split, the original bucket becomes the first of the two new buckets.
  1020. *
  1021. * (It is assumed that the bucket is a member of a brigade when this
  1022. * function is called).
  1023. * @param e The bucket to split
  1024. * @param point The offset to split the bucket at
  1025. */
  1026. #define apr_bucket_split(e,point) (e)->type->split(e, point)
  1027. /**
  1028. * Copy a bucket.
  1029. * @param e The bucket to copy
  1030. * @param c Returns a pointer to the new bucket
  1031. */
  1032. #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
  1033. /* Bucket type handling */
  1034. /**
  1035. * This function simply returns APR_SUCCESS to denote that the bucket does
  1036. * not require anything to happen for its setaside() function. This is
  1037. * appropriate for buckets that have "immortal" data -- the data will live
  1038. * at least as long as the bucket.
  1039. * @param data The bucket to setaside
  1040. * @param pool The pool defining the desired lifetime of the bucket data
  1041. * @return APR_SUCCESS
  1042. */
  1043. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
  1044. apr_pool_t *pool);
  1045. /**
  1046. * A place holder function that signifies that the setaside function was not
  1047. * implemented for this bucket
  1048. * @param data The bucket to setaside
  1049. * @param pool The pool defining the desired lifetime of the bucket data
  1050. * @return APR_ENOTIMPL
  1051. */
  1052. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
  1053. apr_pool_t *pool);
  1054. /**
  1055. * A place holder function that signifies that the split function was not
  1056. * implemented for this bucket
  1057. * @param data The bucket to split
  1058. * @param point The location to split the bucket
  1059. * @return APR_ENOTIMPL
  1060. */
  1061. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
  1062. apr_size_t point);
  1063. /**
  1064. * A place holder function that signifies that the copy function was not
  1065. * implemented for this bucket
  1066. * @param e The bucket to copy
  1067. * @param c Returns a pointer to the new bucket
  1068. * @return APR_ENOTIMPL
  1069. */
  1070. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
  1071. apr_bucket **c);
  1072. /**
  1073. * A place holder function that signifies that this bucket does not need
  1074. * to do anything special to be destroyed. That's only the case for buckets
  1075. * that either have no data (metadata buckets) or buckets whose data pointer
  1076. * points to something that's not a bucket-type-specific structure, as with
  1077. * simple buckets where data points to a string and pipe buckets where data
  1078. * points directly to the apr_file_t.
  1079. * @param data The bucket data to destroy
  1080. */
  1081. APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
  1082. /**
  1083. * There is no apr_bucket_destroy_notimpl, because destruction is required
  1084. * to be implemented (it could be a noop, but only if that makes sense for
  1085. * the bucket type)
  1086. */
  1087. /* There is no apr_bucket_read_notimpl, because it is a required function
  1088. */
  1089. /* All of the bucket types implemented by the core */
  1090. /**
  1091. * The flush bucket type. This signifies that all data should be flushed to
  1092. * the next filter. The flush bucket should be sent with the other buckets.
  1093. */
  1094. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
  1095. /**
  1096. * The EOS bucket type. This signifies that there will be no more data, ever.
  1097. * All filters MUST send all data to the next filter when they receive a
  1098. * bucket of this type
  1099. */
  1100. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
  1101. /**
  1102. * The FILE bucket type. This bucket represents a file on disk
  1103. */
  1104. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
  1105. /**
  1106. * The HEAP bucket type. This bucket represents a data allocated from the
  1107. * heap.
  1108. */
  1109. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
  1110. #if APR_HAS_MMAP
  1111. /**
  1112. * The MMAP bucket type. This bucket represents an MMAP'ed file
  1113. */
  1114. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
  1115. #endif
  1116. /**
  1117. * The POOL bucket type. This bucket represents a data that was allocated
  1118. * from a pool. IF this bucket is still available when the pool is cleared,
  1119. * the data is copied on to the heap.
  1120. */
  1121. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
  1122. /**
  1123. * The PIPE bucket type. This bucket represents a pipe to another program.
  1124. */
  1125. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
  1126. /**
  1127. * The IMMORTAL bucket type. This bucket represents a segment of data that
  1128. * the creator is willing to take responsibility for. The core will do
  1129. * nothing with the data in an immortal bucket
  1130. */
  1131. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
  1132. /**
  1133. * The TRANSIENT bucket type. This bucket represents a data allocated off
  1134. * the stack. When the setaside function is called, this data is copied on
  1135. * to the heap
  1136. */
  1137. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
  1138. /**
  1139. * The SOCKET bucket type. This bucket represents a socket to another machine
  1140. */
  1141. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
  1142. /* ***** Simple buckets ***** */
  1143. /**
  1144. * Split a simple bucket into two at the given point. Most non-reference
  1145. * counting buckets that allow multiple references to the same block of
  1146. * data (eg transient and immortal) will use this as their split function
  1147. * without any additional type-specific handling.
  1148. * @param b The bucket to be split
  1149. * @param point The offset of the first byte in the new bucket
  1150. * @return APR_EINVAL if the point is not within the bucket;
  1151. * APR_ENOMEM if allocation failed;
  1152. * or APR_SUCCESS
  1153. */
  1154. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
  1155. apr_size_t point);
  1156. /**
  1157. * Copy a simple bucket. Most non-reference-counting buckets that allow
  1158. * multiple references to the same block of data (eg transient and immortal)
  1159. * will use this as their copy function without any additional type-specific
  1160. * handling.
  1161. * @param a The bucket to copy
  1162. * @param b Returns a pointer to the new bucket
  1163. * @return APR_ENOMEM if allocation failed;
  1164. * or APR_SUCCESS
  1165. */
  1166. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
  1167. apr_bucket **b);
  1168. /* ***** Shared, reference-counted buckets ***** */
  1169. /**
  1170. * Initialize a bucket containing reference-counted data that may be
  1171. * shared. The caller must allocate the bucket if necessary and
  1172. * initialize its type-dependent fields, and allocate and initialize
  1173. * its own private data structure. This function should only be called
  1174. * by type-specific bucket creation functions.
  1175. * @param b The bucket to initialize
  1176. * @param data A pointer to the private data structure
  1177. * with the reference count at the start
  1178. * @param start The start of the data in the bucket
  1179. * relative to the private base pointer
  1180. * @param length The length of the data in the bucket
  1181. * @return The new bucket, or NULL if allocation failed
  1182. */
  1183. APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
  1184. apr_off_t start,
  1185. apr_size_t length);
  1186. /**
  1187. * Decrement the refcount of the data in the bucket. This function
  1188. * should only be called by type-specific bucket destruction functions.
  1189. * @param data The private data pointer from the bucket to be destroyed
  1190. * @return TRUE or FALSE; TRUE if the reference count is now
  1191. * zero, indicating that the shared resource itself can
  1192. * be destroyed by the caller.
  1193. */
  1194. APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
  1195. /**
  1196. * Split a bucket into two at the given point, and adjust the refcount
  1197. * to the underlying data. Most reference-counting bucket types will
  1198. * be able to use this function as their split function without any
  1199. * additional type-specific handling.
  1200. * @param b The bucket to be split
  1201. * @param point The offset of the first byte in the new bucket
  1202. * @return APR_EINVAL if the point is not within the bucket;
  1203. * APR_ENOMEM if allocation failed;
  1204. * or APR_SUCCESS
  1205. */
  1206. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
  1207. apr_size_t point);
  1208. /**
  1209. * Copy a refcounted bucket, incrementing the reference count. Most
  1210. * reference-counting bucket types will be able to use this function
  1211. * as their copy function without any additional type-specific handling.
  1212. * @param a The bucket to copy
  1213. * @param b Returns a pointer to the new bucket
  1214. * @return APR_ENOMEM if allocation failed;
  1215. or APR_SUCCESS
  1216. */
  1217. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
  1218. apr_bucket **b);
  1219. /* ***** Functions to Create Buckets of varying types ***** */
  1220. /*
  1221. * Each bucket type foo has two initialization functions:
  1222. * apr_bucket_foo_make which sets up some already-allocated memory as a
  1223. * bucket of type foo; and apr_bucket_foo_create which allocates memory
  1224. * for the bucket, calls apr_bucket_make_foo, and initializes the
  1225. * bucket's list pointers. The apr_bucket_foo_make functions are used
  1226. * inside the bucket code to change the type of buckets in place;
  1227. * other code should call apr_bucket_foo_create. All the initialization
  1228. * functions change nothing if they fail.
  1229. */
  1230. /**
  1231. * Create an End of Stream bucket. This indicates that there is no more data
  1232. * coming from down the filter stack. All filters should flush at this point.
  1233. * @param list The freelist from which this bucket should be allocated
  1234. * @return The new bucket, or NULL if allocation failed
  1235. */
  1236. APU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
  1237. /**
  1238. * Make the bucket passed in an EOS bucket. This indicates that there is no
  1239. * more data coming from down the filter stack. All filters should flush at
  1240. * this point.
  1241. * @param b The bucket to make into an EOS bucket
  1242. * @return The new bucket, or NULL if allocation failed
  1243. */
  1244. APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
  1245. /**
  1246. * Create a flush bucket. This indicates that filters should flush their
  1247. * data. There is no guarantee that they will flush it, but this is the
  1248. * best we can do.
  1249. * @param list The freelist from which this bucket should be allocated
  1250. * @return The new bucket, or NULL if allocation failed
  1251. */
  1252. APU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
  1253. /**
  1254. * Make the bucket passed in a FLUSH bucket. This indicates that filters
  1255. * should flush their data. There is no guarantee that they will flush it,
  1256. * but this is the best we can do.
  1257. * @param b The bucket to make into a FLUSH bucket
  1258. * @return The new bucket, or NULL if allocation failed
  1259. */
  1260. APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
  1261. /**
  1262. * Create a bucket referring to long-lived data.
  1263. * @param buf The data to insert into the bucket
  1264. * @param nbyte The size of the data to insert.
  1265. * @param list The freelist from which this bucket should be allocated
  1266. * @return The new bucket, or NULL if allocation failed
  1267. */
  1268. APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
  1269. apr_size_t nbyte,
  1270. apr_bucket_alloc_t *list);
  1271. /**
  1272. * Make the bucket passed in a bucket refer to long-lived data
  1273. * @param b The bucket to make into a IMMORTAL bucket
  1274. * @param buf The data to insert into the bucket
  1275. * @param nbyte The size of the data to insert.
  1276. * @return The new bucket, or NULL if allocation failed
  1277. */
  1278. APU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b,
  1279. const char *buf,
  1280. apr_size_t nbyte);
  1281. /**
  1282. * Create a bucket referring to data on the stack.
  1283. * @param buf The data to insert into the bucket
  1284. * @param nbyte The size of the data to insert.
  1285. * @param list The freelist from which this bucket should be allocated
  1286. * @return The new bucket, or NULL if allocation failed
  1287. */
  1288. APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
  1289. apr_size_t nbyte,
  1290. apr_bucket_alloc_t *list);
  1291. /**
  1292. * Make the bucket passed in a bucket refer to stack data
  1293. * @param b The bucket to make into a TRANSIENT bucket
  1294. * @param buf The data to insert into the bucket
  1295. * @param nbyte The size of the data to insert.
  1296. * @return The new bucket, or NULL if allocation failed
  1297. */
  1298. APU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b,
  1299. const char *buf,
  1300. apr_size_t nbyte);
  1301. /**
  1302. * Create a bucket referring to memory on the heap. If the caller asks
  1303. * for the data to be copied, this function always allocates 4K of
  1304. * memory so that more data can be added to the bucket without
  1305. * requiring another allocation. Therefore not all the data may be put
  1306. * into the bucket. If copying is not requested then the bucket takes
  1307. * over responsibility for free()ing the memory.
  1308. * @param buf The buffer to insert into the bucket
  1309. * @param nbyte The size of the buffer to insert.
  1310. * @param free_func Function to use to free the data; NULL indicates that the
  1311. * bucket should make a copy of the data
  1312. * @param list The freelist from which this bucket should be allocated
  1313. * @return The new bucket, or NULL if allocation failed
  1314. */
  1315. APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
  1316. apr_size_t nbyte,
  1317. void (*free_func)(void *data),
  1318. apr_bucket_alloc_t *list);
  1319. /**
  1320. * Make the bucket passed in a bucket refer to heap data
  1321. * @param b The bucket to make into a HEAP bucket
  1322. * @param buf The buffer to insert into the bucket
  1323. * @param nbyte The size of the buffer to insert.
  1324. * @param free_func Function to use to free the data; NULL indicates that the
  1325. * bucket should make a copy of the data
  1326. * @return The new bucket, or NULL if allocation failed
  1327. */
  1328. APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
  1329. apr_size_t nbyte,
  1330. void (*free_func)(void *data));
  1331. /**
  1332. * Create a bucket referring to memory allocated from a pool.
  1333. *
  1334. * @param buf The buffer to insert into the bucket
  1335. * @param length The number of bytes referred to by this bucket
  1336. * @param pool The pool the memory was allocated from
  1337. * @param list The freelist from which this bucket should be allocated
  1338. * @return The new bucket, or NULL if allocation failed
  1339. */
  1340. APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
  1341. apr_size_t length,
  1342. apr_pool_t *pool,
  1343. apr_bucket_alloc_t *list);
  1344. /**
  1345. * Make the bucket passed in a bucket refer to pool data
  1346. * @param b The bucket to make into a pool bucket
  1347. * @param buf The buffer to insert into the bucket
  1348. * @param length The number of bytes referred to by this bucket
  1349. * @param pool The pool the memory was allocated from
  1350. * @return The new bucket, or NULL if allocation failed
  1351. */
  1352. APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
  1353. apr_size_t length,
  1354. apr_pool_t *pool);
  1355. #if APR_HAS_MMAP
  1356. /**
  1357. * Create a bucket referring to mmap()ed memory.
  1358. * @param mm The mmap to insert into the bucket
  1359. * @param start The offset of the first byte in the mmap
  1360. * that this bucket refers to
  1361. * @param length The number of bytes referred to by this bucket
  1362. * @param list The freelist from which this bucket should be allocated
  1363. * @return The new bucket, or NULL if allocation failed
  1364. */
  1365. APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
  1366. apr_off_t start,
  1367. apr_size_t length,
  1368. apr_bucket_alloc_t *list);
  1369. /**
  1370. * Make the bucket passed in a bucket refer to an MMAP'ed file
  1371. * @param b The bucket to make into a MMAP bucket
  1372. * @param mm The mmap to insert into the bucket
  1373. * @param start The offset of the first byte in the mmap
  1374. * that this bucket refers to
  1375. * @param length The number of bytes referred to by this bucket
  1376. * @return The new bucket, or NULL if allocation failed
  1377. */
  1378. APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
  1379. apr_off_t start,
  1380. apr_size_t length);
  1381. #endif
  1382. /**
  1383. * Create a bucket referring to a socket.
  1384. * @param thissock The socket to put in the bucket
  1385. * @param list The freelist from which this bucket should be allocated
  1386. * @return The new bucket, or NULL if allocation failed
  1387. */
  1388. APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
  1389. apr_bucket_alloc_t *list);
  1390. /**
  1391. * Make the bucket passed in a bucket refer to a socket
  1392. * @param b The bucket to make into a SOCKET bucket
  1393. * @param thissock The socket to put in the bucket
  1394. * @return The new bucket, or NULL if allocation failed
  1395. */
  1396. APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
  1397. apr_socket_t *thissock);
  1398. /**
  1399. * Create a bucket referring to a pipe.
  1400. * @param thispipe The pipe to put in the bucket
  1401. * @param list The freelist from which this bucket should be allocated
  1402. * @return The new bucket, or NULL if allocation failed
  1403. */
  1404. APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
  1405. apr_bucket_alloc_t *list);
  1406. /**
  1407. * Make the bucket passed in a bucket refer to a pipe
  1408. * @param b The bucket to make into a PIPE bucket
  1409. * @param thispipe The pipe to put in the bucket
  1410. * @return The new bucket, or NULL if allocation failed
  1411. */
  1412. APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
  1413. apr_file_t *thispipe);
  1414. /**
  1415. * Create a bucket referring to a file.
  1416. * @param fd The file to put in the bucket
  1417. * @param offset The offset where the data of interest begins in the file
  1418. * @param len The amount of data in the file we are interested in
  1419. * @param p The pool into which any needed structures should be created
  1420. * while reading from this file bucket
  1421. * @param list The freelist from which this bucket should be allocated
  1422. * @return The new bucket, or NULL if allocation failed
  1423. * @remark If the file is truncated such that the segment of the file
  1424. * referenced by the bucket no longer exists, an attempt to read
  1425. * from the bucket will fail with APR_EOF.
  1426. * @remark apr_brigade_insert_file() should generally be used to
  1427. * insert files into brigades, since that function can correctly
  1428. * handle large file issues.
  1429. */
  1430. APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
  1431. apr_off_t offset,
  1432. apr_size_t len,
  1433. apr_pool_t *p,
  1434. apr_bucket_alloc_t *list);
  1435. /**
  1436. * Make the bucket passed in a bucket refer to a file
  1437. * @param b The bucket to make into a FILE bucket
  1438. * @param fd The file to put in the bucket
  1439. * @param offset The offset where the data of interest begins in the file
  1440. * @param len The amount of data in the file we are interested in
  1441. * @param p The pool into which any needed structures should be created
  1442. * while reading from this file bucket
  1443. * @return The new bucket, or NULL if allocation failed
  1444. */
  1445. APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
  1446. apr_off_t offset,
  1447. apr_size_t len, apr_pool_t *p);
  1448. /**
  1449. * Enable or disable memory-mapping for a FILE bucket (default is enabled)
  1450. * @param b The bucket
  1451. * @param enabled Whether memory-mapping should be enabled
  1452. * @return APR_SUCCESS normally, or an error code if the operation fails
  1453. */
  1454. APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
  1455. int enabled);
  1456. /**
  1457. * Set the size of the read buffer allocated by a FILE bucket (default
  1458. * is @a APR_BUCKET_BUFF_SIZE)
  1459. * memory-mapping is disabled only)
  1460. * @param b The bucket
  1461. * @param size Size of the allocated buffers
  1462. * @return APR_SUCCESS normally, or an error code if the operation fails
  1463. * @remark Relevant/used only when memory-mapping is disabled (@see
  1464. * apr_bucket_file_enable_mmap)
  1465. */
  1466. APU_DECLARE(apr_status_t) apr_bucket_file_set_buf_size(apr_bucket *b,
  1467. apr_size_t size);
  1468. /** @} */
  1469. #ifdef __cplusplus
  1470. }
  1471. #endif
  1472. #endif /* !APR_BUCKETS_H */