apr_buckets.h 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. (e)->type->destroy((e)->data); \
  922. (e)->free(e); \
  923. } while (0)
  924. /**
  925. * Delete a bucket by removing it from its brigade (if any) and then
  926. * destroying it.
  927. * @remark This mainly acts as an aid in avoiding code verbosity. It is
  928. * the preferred exact equivalent to:
  929. * <pre>
  930. * APR_BUCKET_REMOVE(e);
  931. * apr_bucket_destroy(e);
  932. * </pre>
  933. * @param e The bucket to delete
  934. */
  935. #define apr_bucket_delete(e) do { \
  936. APR_BUCKET_REMOVE(e); \
  937. apr_bucket_destroy(e); \
  938. } while (0)
  939. /**
  940. * Read some data from the bucket.
  941. *
  942. * The apr_bucket_read function returns a convenient amount of data
  943. * from the bucket provided, writing the address and length of the
  944. * data to the pointers provided by the caller. The function tries
  945. * as hard as possible to avoid a memory copy.
  946. *
  947. * Buckets are expected to be a member of a brigade at the time they
  948. * are read.
  949. *
  950. * In typical application code, buckets are read in a loop, and after
  951. * each bucket is read and processed, it is moved or deleted from the
  952. * brigade and the next bucket read.
  953. *
  954. * The definition of "convenient" depends on the type of bucket that
  955. * is being read, and is decided by APR. In the case of memory based
  956. * buckets such as heap and immortal buckets, a pointer will be
  957. * returned to the location of the buffer containing the complete
  958. * contents of the bucket.
  959. *
  960. * Some buckets, such as the socket bucket, might have no concept
  961. * of length. If an attempt is made to read such a bucket, the
  962. * apr_bucket_read function will read a convenient amount of data
  963. * from the socket. The socket bucket is magically morphed into a
  964. * heap bucket containing the just-read data, and a new socket bucket
  965. * is inserted just after this heap bucket.
  966. *
  967. * To understand why apr_bucket_read might do this, consider the loop
  968. * described above to read and process buckets. The current bucket
  969. * is magically morphed into a heap bucket and returned to the caller.
  970. * The caller processes the data, and deletes the heap bucket, moving
  971. * onto the next bucket, the new socket bucket. This process repeats,
  972. * giving the illusion of a bucket brigade that contains potentially
  973. * infinite amounts of data. It is up to the caller to decide at what
  974. * point to stop reading buckets.
  975. *
  976. * Some buckets, such as the file bucket, might have a fixed size,
  977. * but be significantly larger than is practical to store in RAM in
  978. * one go. As with the socket bucket, if an attempt is made to read
  979. * from a file bucket, the file bucket is magically morphed into a
  980. * heap bucket containing a convenient amount of data read from the
  981. * current offset in the file. During the read, the offset will be
  982. * moved forward on the file, and a new file bucket will be inserted
  983. * directly after the current bucket representing the remainder of the
  984. * file. If the heap bucket was large enough to store the whole
  985. * remainder of the file, no more file buckets are inserted, and the
  986. * file bucket will disappear completely.
  987. *
  988. * The pattern for reading buckets described above does create the
  989. * illusion that the code is willing to swallow buckets that might be
  990. * too large for the system to handle in one go. This however is just
  991. * an illusion: APR will always ensure that large (file) or infinite
  992. * (socket) buckets are broken into convenient bite sized heap buckets
  993. * before data is returned to the caller.
  994. *
  995. * There is a potential gotcha to watch for: if buckets are read in a
  996. * loop, and aren't deleted after being processed, the potentially large
  997. * bucket will slowly be converted into RAM resident heap buckets. If
  998. * the file is larger than available RAM, an out of memory condition
  999. * could be caused if the application is not careful to manage this.
  1000. *
  1001. * @param e The bucket to read from
  1002. * @param str The location to store a pointer to the data in
  1003. * @param len The location to store the amount of data read
  1004. * @param block Whether the read function blocks
  1005. */
  1006. #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
  1007. /**
  1008. * Setaside data so that stack data is not destroyed on returning from
  1009. * the function
  1010. * @param e The bucket to setaside
  1011. * @param p The pool to setaside into
  1012. */
  1013. #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
  1014. /**
  1015. * Split one bucket in two at the point provided.
  1016. *
  1017. * Once split, the original bucket becomes the first of the two new buckets.
  1018. *
  1019. * (It is assumed that the bucket is a member of a brigade when this
  1020. * function is called).
  1021. * @param e The bucket to split
  1022. * @param point The offset to split the bucket at
  1023. */
  1024. #define apr_bucket_split(e,point) (e)->type->split(e, point)
  1025. /**
  1026. * Copy a bucket.
  1027. * @param e The bucket to copy
  1028. * @param c Returns a pointer to the new bucket
  1029. */
  1030. #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
  1031. /* Bucket type handling */
  1032. /**
  1033. * This function simply returns APR_SUCCESS to denote that the bucket does
  1034. * not require anything to happen for its setaside() function. This is
  1035. * appropriate for buckets that have "immortal" data -- the data will live
  1036. * at least as long as the bucket.
  1037. * @param data The bucket to setaside
  1038. * @param pool The pool defining the desired lifetime of the bucket data
  1039. * @return APR_SUCCESS
  1040. */
  1041. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
  1042. apr_pool_t *pool);
  1043. /**
  1044. * A place holder function that signifies that the setaside function was not
  1045. * implemented for this bucket
  1046. * @param data The bucket to setaside
  1047. * @param pool The pool defining the desired lifetime of the bucket data
  1048. * @return APR_ENOTIMPL
  1049. */
  1050. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
  1051. apr_pool_t *pool);
  1052. /**
  1053. * A place holder function that signifies that the split function was not
  1054. * implemented for this bucket
  1055. * @param data The bucket to split
  1056. * @param point The location to split the bucket
  1057. * @return APR_ENOTIMPL
  1058. */
  1059. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
  1060. apr_size_t point);
  1061. /**
  1062. * A place holder function that signifies that the copy function was not
  1063. * implemented for this bucket
  1064. * @param e The bucket to copy
  1065. * @param c Returns a pointer to the new bucket
  1066. * @return APR_ENOTIMPL
  1067. */
  1068. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
  1069. apr_bucket **c);
  1070. /**
  1071. * A place holder function that signifies that this bucket does not need
  1072. * to do anything special to be destroyed. That's only the case for buckets
  1073. * that either have no data (metadata buckets) or buckets whose data pointer
  1074. * points to something that's not a bucket-type-specific structure, as with
  1075. * simple buckets where data points to a string and pipe buckets where data
  1076. * points directly to the apr_file_t.
  1077. * @param data The bucket data to destroy
  1078. */
  1079. APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
  1080. /**
  1081. * There is no apr_bucket_destroy_notimpl, because destruction is required
  1082. * to be implemented (it could be a noop, but only if that makes sense for
  1083. * the bucket type)
  1084. */
  1085. /* There is no apr_bucket_read_notimpl, because it is a required function
  1086. */
  1087. /* All of the bucket types implemented by the core */
  1088. /**
  1089. * The flush bucket type. This signifies that all data should be flushed to
  1090. * the next filter. The flush bucket should be sent with the other buckets.
  1091. */
  1092. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
  1093. /**
  1094. * The EOS bucket type. This signifies that there will be no more data, ever.
  1095. * All filters MUST send all data to the next filter when they receive a
  1096. * bucket of this type
  1097. */
  1098. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
  1099. /**
  1100. * The FILE bucket type. This bucket represents a file on disk
  1101. */
  1102. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
  1103. /**
  1104. * The HEAP bucket type. This bucket represents a data allocated from the
  1105. * heap.
  1106. */
  1107. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
  1108. #if APR_HAS_MMAP
  1109. /**
  1110. * The MMAP bucket type. This bucket represents an MMAP'ed file
  1111. */
  1112. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
  1113. #endif
  1114. /**
  1115. * The POOL bucket type. This bucket represents a data that was allocated
  1116. * from a pool. IF this bucket is still available when the pool is cleared,
  1117. * the data is copied on to the heap.
  1118. */
  1119. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
  1120. /**
  1121. * The PIPE bucket type. This bucket represents a pipe to another program.
  1122. */
  1123. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
  1124. /**
  1125. * The IMMORTAL bucket type. This bucket represents a segment of data that
  1126. * the creator is willing to take responsibility for. The core will do
  1127. * nothing with the data in an immortal bucket
  1128. */
  1129. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
  1130. /**
  1131. * The TRANSIENT bucket type. This bucket represents a data allocated off
  1132. * the stack. When the setaside function is called, this data is copied on
  1133. * to the heap
  1134. */
  1135. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
  1136. /**
  1137. * The SOCKET bucket type. This bucket represents a socket to another machine
  1138. */
  1139. APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
  1140. /* ***** Simple buckets ***** */
  1141. /**
  1142. * Split a simple bucket into two at the given point. Most non-reference
  1143. * counting buckets that allow multiple references to the same block of
  1144. * data (eg transient and immortal) will use this as their split function
  1145. * without any additional type-specific handling.
  1146. * @param b The bucket to be split
  1147. * @param point The offset of the first byte in the new bucket
  1148. * @return APR_EINVAL if the point is not within the bucket;
  1149. * APR_ENOMEM if allocation failed;
  1150. * or APR_SUCCESS
  1151. */
  1152. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
  1153. apr_size_t point);
  1154. /**
  1155. * Copy a simple bucket. Most non-reference-counting buckets that allow
  1156. * multiple references to the same block of data (eg transient and immortal)
  1157. * will use this as their copy function without any additional type-specific
  1158. * handling.
  1159. * @param a The bucket to copy
  1160. * @param b Returns a pointer to the new bucket
  1161. * @return APR_ENOMEM if allocation failed;
  1162. * or APR_SUCCESS
  1163. */
  1164. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
  1165. apr_bucket **b);
  1166. /* ***** Shared, reference-counted buckets ***** */
  1167. /**
  1168. * Initialize a bucket containing reference-counted data that may be
  1169. * shared. The caller must allocate the bucket if necessary and
  1170. * initialize its type-dependent fields, and allocate and initialize
  1171. * its own private data structure. This function should only be called
  1172. * by type-specific bucket creation functions.
  1173. * @param b The bucket to initialize
  1174. * @param data A pointer to the private data structure
  1175. * with the reference count at the start
  1176. * @param start The start of the data in the bucket
  1177. * relative to the private base pointer
  1178. * @param length The length of the data in the bucket
  1179. * @return The new bucket, or NULL if allocation failed
  1180. */
  1181. APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
  1182. apr_off_t start,
  1183. apr_size_t length);
  1184. /**
  1185. * Decrement the refcount of the data in the bucket. This function
  1186. * should only be called by type-specific bucket destruction functions.
  1187. * @param data The private data pointer from the bucket to be destroyed
  1188. * @return TRUE or FALSE; TRUE if the reference count is now
  1189. * zero, indicating that the shared resource itself can
  1190. * be destroyed by the caller.
  1191. */
  1192. APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
  1193. /**
  1194. * Split a bucket into two at the given point, and adjust the refcount
  1195. * to the underlying data. Most reference-counting bucket types will
  1196. * be able to use this function as their split function without any
  1197. * additional type-specific handling.
  1198. * @param b The bucket to be split
  1199. * @param point The offset of the first byte in the new bucket
  1200. * @return APR_EINVAL if the point is not within the bucket;
  1201. * APR_ENOMEM if allocation failed;
  1202. * or APR_SUCCESS
  1203. */
  1204. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
  1205. apr_size_t point);
  1206. /**
  1207. * Copy a refcounted bucket, incrementing the reference count. Most
  1208. * reference-counting bucket types will be able to use this function
  1209. * as their copy function without any additional type-specific handling.
  1210. * @param a The bucket to copy
  1211. * @param b Returns a pointer to the new bucket
  1212. * @return APR_ENOMEM if allocation failed;
  1213. or APR_SUCCESS
  1214. */
  1215. APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
  1216. apr_bucket **b);
  1217. /* ***** Functions to Create Buckets of varying types ***** */
  1218. /*
  1219. * Each bucket type foo has two initialization functions:
  1220. * apr_bucket_foo_make which sets up some already-allocated memory as a
  1221. * bucket of type foo; and apr_bucket_foo_create which allocates memory
  1222. * for the bucket, calls apr_bucket_make_foo, and initializes the
  1223. * bucket's list pointers. The apr_bucket_foo_make functions are used
  1224. * inside the bucket code to change the type of buckets in place;
  1225. * other code should call apr_bucket_foo_create. All the initialization
  1226. * functions change nothing if they fail.
  1227. */
  1228. /**
  1229. * Create an End of Stream bucket. This indicates that there is no more data
  1230. * coming from down the filter stack. All filters should flush at this point.
  1231. * @param list The freelist from which this bucket should be allocated
  1232. * @return The new bucket, or NULL if allocation failed
  1233. */
  1234. APU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
  1235. /**
  1236. * Make the bucket passed in an EOS bucket. This indicates that there is no
  1237. * more data coming from down the filter stack. All filters should flush at
  1238. * this point.
  1239. * @param b The bucket to make into an EOS bucket
  1240. * @return The new bucket, or NULL if allocation failed
  1241. */
  1242. APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
  1243. /**
  1244. * Create a flush bucket. This indicates that filters should flush their
  1245. * data. There is no guarantee that they will flush it, but this is the
  1246. * best we can do.
  1247. * @param list The freelist from which this bucket should be allocated
  1248. * @return The new bucket, or NULL if allocation failed
  1249. */
  1250. APU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
  1251. /**
  1252. * Make the bucket passed in a FLUSH bucket. This indicates that filters
  1253. * should flush their data. There is no guarantee that they will flush it,
  1254. * but this is the best we can do.
  1255. * @param b The bucket to make into a FLUSH bucket
  1256. * @return The new bucket, or NULL if allocation failed
  1257. */
  1258. APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
  1259. /**
  1260. * Create a bucket referring to long-lived data.
  1261. * @param buf The data to insert into the bucket
  1262. * @param nbyte The size of the data to insert.
  1263. * @param list The freelist from which this bucket should be allocated
  1264. * @return The new bucket, or NULL if allocation failed
  1265. */
  1266. APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
  1267. apr_size_t nbyte,
  1268. apr_bucket_alloc_t *list);
  1269. /**
  1270. * Make the bucket passed in a bucket refer to long-lived data
  1271. * @param b The bucket to make into a IMMORTAL bucket
  1272. * @param buf The data to insert into the bucket
  1273. * @param nbyte The size of the data to insert.
  1274. * @return The new bucket, or NULL if allocation failed
  1275. */
  1276. APU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b,
  1277. const char *buf,
  1278. apr_size_t nbyte);
  1279. /**
  1280. * Create a bucket referring to data on the stack.
  1281. * @param buf The data to insert into the bucket
  1282. * @param nbyte The size of the data to insert.
  1283. * @param list The freelist from which this bucket should be allocated
  1284. * @return The new bucket, or NULL if allocation failed
  1285. */
  1286. APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
  1287. apr_size_t nbyte,
  1288. apr_bucket_alloc_t *list);
  1289. /**
  1290. * Make the bucket passed in a bucket refer to stack data
  1291. * @param b The bucket to make into a TRANSIENT bucket
  1292. * @param buf The data to insert into the bucket
  1293. * @param nbyte The size of the data to insert.
  1294. * @return The new bucket, or NULL if allocation failed
  1295. */
  1296. APU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b,
  1297. const char *buf,
  1298. apr_size_t nbyte);
  1299. /**
  1300. * Create a bucket referring to memory on the heap. If the caller asks
  1301. * for the data to be copied, this function always allocates 4K of
  1302. * memory so that more data can be added to the bucket without
  1303. * requiring another allocation. Therefore not all the data may be put
  1304. * into the bucket. If copying is not requested then the bucket takes
  1305. * over responsibility for free()ing the memory.
  1306. * @param buf The buffer to insert into the bucket
  1307. * @param nbyte The size of the buffer to insert.
  1308. * @param free_func Function to use to free the data; NULL indicates that the
  1309. * bucket should make a copy of the data
  1310. * @param list The freelist from which this bucket should be allocated
  1311. * @return The new bucket, or NULL if allocation failed
  1312. */
  1313. APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
  1314. apr_size_t nbyte,
  1315. void (*free_func)(void *data),
  1316. apr_bucket_alloc_t *list);
  1317. /**
  1318. * Make the bucket passed in a bucket refer to heap data
  1319. * @param b The bucket to make into a HEAP bucket
  1320. * @param buf The buffer to insert into the bucket
  1321. * @param nbyte The size of the buffer to insert.
  1322. * @param free_func Function to use to free the data; NULL indicates that the
  1323. * bucket should make a copy of the data
  1324. * @return The new bucket, or NULL if allocation failed
  1325. */
  1326. APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
  1327. apr_size_t nbyte,
  1328. void (*free_func)(void *data));
  1329. /**
  1330. * Create a bucket referring to memory allocated from a pool.
  1331. *
  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. * @param list The freelist from which this bucket should be allocated
  1336. * @return The new bucket, or NULL if allocation failed
  1337. */
  1338. APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
  1339. apr_size_t length,
  1340. apr_pool_t *pool,
  1341. apr_bucket_alloc_t *list);
  1342. /**
  1343. * Make the bucket passed in a bucket refer to pool data
  1344. * @param b The bucket to make into a pool bucket
  1345. * @param buf The buffer to insert into the bucket
  1346. * @param length The number of bytes referred to by this bucket
  1347. * @param pool The pool the memory was allocated from
  1348. * @return The new bucket, or NULL if allocation failed
  1349. */
  1350. APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
  1351. apr_size_t length,
  1352. apr_pool_t *pool);
  1353. #if APR_HAS_MMAP
  1354. /**
  1355. * Create a bucket referring to mmap()ed memory.
  1356. * @param mm The mmap to insert into the bucket
  1357. * @param start The offset of the first byte in the mmap
  1358. * that this bucket refers to
  1359. * @param length The number of bytes referred to by this bucket
  1360. * @param list The freelist from which this bucket should be allocated
  1361. * @return The new bucket, or NULL if allocation failed
  1362. */
  1363. APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
  1364. apr_off_t start,
  1365. apr_size_t length,
  1366. apr_bucket_alloc_t *list);
  1367. /**
  1368. * Make the bucket passed in a bucket refer to an MMAP'ed file
  1369. * @param b The bucket to make into a MMAP bucket
  1370. * @param mm The mmap to insert into the bucket
  1371. * @param start The offset of the first byte in the mmap
  1372. * that this bucket refers to
  1373. * @param length The number of bytes referred to by this bucket
  1374. * @return The new bucket, or NULL if allocation failed
  1375. */
  1376. APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
  1377. apr_off_t start,
  1378. apr_size_t length);
  1379. #endif
  1380. /**
  1381. * Create a bucket referring to a socket.
  1382. * @param thissock The socket to put in the bucket
  1383. * @param list The freelist from which this bucket should be allocated
  1384. * @return The new bucket, or NULL if allocation failed
  1385. */
  1386. APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
  1387. apr_bucket_alloc_t *list);
  1388. /**
  1389. * Make the bucket passed in a bucket refer to a socket
  1390. * @param b The bucket to make into a SOCKET bucket
  1391. * @param thissock The socket to put in the bucket
  1392. * @return The new bucket, or NULL if allocation failed
  1393. */
  1394. APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
  1395. apr_socket_t *thissock);
  1396. /**
  1397. * Create a bucket referring to a pipe.
  1398. * @param thispipe The pipe to put in the bucket
  1399. * @param list The freelist from which this bucket should be allocated
  1400. * @return The new bucket, or NULL if allocation failed
  1401. */
  1402. APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
  1403. apr_bucket_alloc_t *list);
  1404. /**
  1405. * Make the bucket passed in a bucket refer to a pipe
  1406. * @param b The bucket to make into a PIPE bucket
  1407. * @param thispipe The pipe to put in the bucket
  1408. * @return The new bucket, or NULL if allocation failed
  1409. */
  1410. APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
  1411. apr_file_t *thispipe);
  1412. /**
  1413. * Create a bucket referring to a file.
  1414. * @param fd The file to put in the bucket
  1415. * @param offset The offset where the data of interest begins in the file
  1416. * @param len The amount of data in the file we are interested in
  1417. * @param p The pool into which any needed structures should be created
  1418. * while reading from this file bucket
  1419. * @param list The freelist from which this bucket should be allocated
  1420. * @return The new bucket, or NULL if allocation failed
  1421. * @remark If the file is truncated such that the segment of the file
  1422. * referenced by the bucket no longer exists, an attempt to read
  1423. * from the bucket will fail with APR_EOF.
  1424. * @remark apr_brigade_insert_file() should generally be used to
  1425. * insert files into brigades, since that function can correctly
  1426. * handle large file issues.
  1427. */
  1428. APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
  1429. apr_off_t offset,
  1430. apr_size_t len,
  1431. apr_pool_t *p,
  1432. apr_bucket_alloc_t *list);
  1433. /**
  1434. * Make the bucket passed in a bucket refer to a file
  1435. * @param b The bucket to make into a FILE bucket
  1436. * @param fd The file to put in the bucket
  1437. * @param offset The offset where the data of interest begins in the file
  1438. * @param len The amount of data in the file we are interested in
  1439. * @param p The pool into which any needed structures should be created
  1440. * while reading from this file bucket
  1441. * @return The new bucket, or NULL if allocation failed
  1442. */
  1443. APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
  1444. apr_off_t offset,
  1445. apr_size_t len, apr_pool_t *p);
  1446. /**
  1447. * Enable or disable memory-mapping for a FILE bucket (default is enabled)
  1448. * @param b The bucket
  1449. * @param enabled Whether memory-mapping should be enabled
  1450. * @return APR_SUCCESS normally, or an error code if the operation fails
  1451. */
  1452. APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
  1453. int enabled);
  1454. /**
  1455. * Set the size of the read buffer allocated by a FILE bucket (default
  1456. * is @a APR_BUCKET_BUFF_SIZE)
  1457. * memory-mapping is disabled only)
  1458. * @param b The bucket
  1459. * @param size Size of the allocated buffers
  1460. * @return APR_SUCCESS normally, or an error code if the operation fails
  1461. * @remark Relevant/used only when memory-mapping is disabled (@see
  1462. * apr_bucket_file_enable_mmap)
  1463. */
  1464. APU_DECLARE(apr_status_t) apr_bucket_file_set_buf_size(apr_bucket *e,
  1465. apr_size_t size);
  1466. /** @} */
  1467. #ifdef __cplusplus
  1468. }
  1469. #endif
  1470. #endif /* !APR_BUCKETS_H */