apr_buckets.h 62 KB

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