Apache Portable Runtime Utility Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
apr_buckets.h
Go to the documentation of this file.
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  */
21 #ifndef APR_BUCKETS_H
22 #define APR_BUCKETS_H
23 
24 #if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG)
25 #define APR_RING_DEBUG
26 #endif
27 
28 #include "apu.h"
29 #include "apr_network_io.h"
30 #include "apr_file_io.h"
31 #include "apr_general.h"
32 #include "apr_mmap.h"
33 #include "apr_errno.h"
34 #include "apr_ring.h"
35 #include "apr.h"
36 #if APR_HAVE_SYS_UIO_H
37 #include <sys/uio.h> /* for struct iovec */
38 #endif
39 #if APR_HAVE_STDARG_H
40 #include <stdarg.h>
41 #endif
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
54 #define APR_BUCKET_BUFF_SIZE 8000
55 
57 typedef enum {
61 
114 /*
115  * Forward declaration of the main types.
116  */
117 
121 typedef struct apr_bucket apr_bucket;
124 
127 
135  const char *name;
140  int num_func;
151  enum {
156  } is_metadata;
164  void (*destroy)(void *data);
165 
176  apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len,
177  apr_read_type_e block);
178 
192  apr_status_t (*setaside)(apr_bucket *e, apr_pool_t *pool);
193 
203  apr_status_t (*split)(apr_bucket *e, apr_size_t point);
204 
211  apr_status_t (*copy)(apr_bucket *e, apr_bucket **c);
212 
213 };
214 
224 struct apr_bucket {
234  apr_size_t length;
242  apr_off_t start;
244  void *data;
252  void (*free)(void *e);
255 };
256 
264  apr_pool_t *p;
266  /*
267  * The apr_bucket_list structure doesn't actually need a name tag
268  * because it has no existence independent of struct apr_bucket_brigade;
269  * the ring macros are designed so that you can leave the name tag
270  * argument empty in this situation but apparently the Windows compiler
271  * doesn't like that.
272  */
273  APR_RING_HEAD(apr_bucket_list, apr_bucket) list;
276 };
277 
278 
282 typedef apr_status_t (*apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx);
283 
284 /*
285  * define APR_BUCKET_DEBUG if you want your brigades to be checked for
286  * validity at every possible instant. this will slow your code down
287  * substantially but is a very useful debugging tool.
288  */
289 #ifdef APR_BUCKET_DEBUG
290 
291 #define APR_BRIGADE_CHECK_CONSISTENCY(b) \
292  APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link)
293 
294 #define APR_BUCKET_CHECK_CONSISTENCY(e) \
295  APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
296 
297 #else
298 
304 #define APR_BRIGADE_CHECK_CONSISTENCY(b)
305 
311 #define APR_BUCKET_CHECK_CONSISTENCY(e)
312 #endif
313 
314 
331 #define APR_BRIGADE_SENTINEL(b) APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
332 
338 #define APR_BRIGADE_EMPTY(b) APR_RING_EMPTY(&(b)->list, apr_bucket, link)
339 
345 #define APR_BRIGADE_FIRST(b) APR_RING_FIRST(&(b)->list)
346 
351 #define APR_BRIGADE_LAST(b) APR_RING_LAST(&(b)->list)
352 
358 #define APR_BRIGADE_INSERT_HEAD(b, e) do { \
359  apr_bucket *ap__b = (e); \
360  APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link); \
361  APR_BRIGADE_CHECK_CONSISTENCY((b)); \
362  } while (0)
363 
369 #define APR_BRIGADE_INSERT_TAIL(b, e) do { \
370  apr_bucket *ap__b = (e); \
371  APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link); \
372  APR_BRIGADE_CHECK_CONSISTENCY((b)); \
373  } while (0)
374 
380 #define APR_BRIGADE_CONCAT(a, b) do { \
381  APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link); \
382  APR_BRIGADE_CHECK_CONSISTENCY((a)); \
383  } while (0)
384 
390 #define APR_BRIGADE_PREPEND(a, b) do { \
391  APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link); \
392  APR_BRIGADE_CHECK_CONSISTENCY((a)); \
393  } while (0)
394 
400 #define APR_BUCKET_INSERT_BEFORE(a, b) do { \
401  apr_bucket *ap__a = (a), *ap__b = (b); \
402  APR_RING_INSERT_BEFORE(ap__a, ap__b, link); \
403  APR_BUCKET_CHECK_CONSISTENCY(ap__a); \
404  } while (0)
405 
411 #define APR_BUCKET_INSERT_AFTER(a, b) do { \
412  apr_bucket *ap__a = (a), *ap__b = (b); \
413  APR_RING_INSERT_AFTER(ap__a, ap__b, link); \
414  APR_BUCKET_CHECK_CONSISTENCY(ap__a); \
415  } while (0)
416 
422 #define APR_BUCKET_NEXT(e) APR_RING_NEXT((e), link)
423 
428 #define APR_BUCKET_PREV(e) APR_RING_PREV((e), link)
429 
434 #define APR_BUCKET_REMOVE(e) APR_RING_REMOVE((e), link)
435 
440 #define APR_BUCKET_INIT(e) APR_RING_ELEM_INIT((e), link)
441 
448 #define APR_BUCKET_IS_METADATA(e) ((e)->type->is_metadata)
449 
455 #define APR_BUCKET_IS_FLUSH(e) ((e)->type == &apr_bucket_type_flush)
456 
461 #define APR_BUCKET_IS_EOS(e) ((e)->type == &apr_bucket_type_eos)
462 
467 #define APR_BUCKET_IS_FILE(e) ((e)->type == &apr_bucket_type_file)
468 
473 #define APR_BUCKET_IS_PIPE(e) ((e)->type == &apr_bucket_type_pipe)
474 
479 #define APR_BUCKET_IS_SOCKET(e) ((e)->type == &apr_bucket_type_socket)
480 
485 #define APR_BUCKET_IS_HEAP(e) ((e)->type == &apr_bucket_type_heap)
486 
491 #define APR_BUCKET_IS_TRANSIENT(e) ((e)->type == &apr_bucket_type_transient)
492 
497 #define APR_BUCKET_IS_IMMORTAL(e) ((e)->type == &apr_bucket_type_immortal)
498 #if APR_HAS_MMAP
499 
504 #define APR_BUCKET_IS_MMAP(e) ((e)->type == &apr_bucket_type_mmap)
505 #endif
506 
511 #define APR_BUCKET_IS_POOL(e) ((e)->type == &apr_bucket_type_pool)
512 
513 /*
514  * General-purpose reference counting for the various bucket types.
515  *
516  * Any bucket type that keeps track of the resources it uses (i.e.
517  * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
518  * attach a reference count to the resource so that it can be freed
519  * when the last bucket that uses it goes away. Resource-sharing may
520  * occur because of bucket splits or buckets that refer to globally
521  * cached data. */
522 
533  int refcount;
534 };
535 
536 /* ***** Reference-counted bucket types ***** */
537 
549  char *base;
551  apr_size_t alloc_len;
553  void (*free_func)(void *data);
554 };
555 
579  const char *base;
586  apr_pool_t *pool;
591 };
592 
593 #if APR_HAS_MMAP
594 
603  apr_mmap_t *mmap;
604 };
605 #endif
606 
616  apr_file_t *fd;
619  apr_pool_t *readpool;
620 #if APR_HAS_MMAP
621 
623  int can_mmap;
624 #endif /* APR_HAS_MMAP */
625 };
626 
637 #if APR_HAS_MMAP
639 #endif
641 };
642 
648 #define APR_BUCKET_ALLOC_SIZE APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
649 
650 /* ***** Bucket Brigade Functions ***** */
658 APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
659  apr_bucket_alloc_t *list);
660 
666 APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
667 
679 APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
680 
697  apr_bucket *e,
698  apr_bucket_brigade *a);
699 
712  apr_bucket *e);
713 
726 APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
727  apr_off_t point,
728  apr_bucket **after_point);
729 
738 APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
739  int read_all,
740  apr_off_t *length);
741 
749 APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
750  char *c,
751  apr_size_t *len);
752 
760 APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
761  char **c,
762  apr_size_t *len,
763  apr_pool_t *pool);
764 
773 APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
774  apr_bucket_brigade *bbIn,
775  apr_read_type_e block,
776  apr_off_t maxbytes);
777 
787 APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b,
788  struct iovec *vec, int *nvec);
789 
798 APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
799  apr_brigade_flush flush,
800  void *ctx,
801  va_list va);
802 
826 APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
827  apr_brigade_flush flush, void *ctx,
828  const char *str, apr_size_t nbyte);
829 
839 APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
840  apr_brigade_flush flush,
841  void *ctx,
842  const struct iovec *vec,
843  apr_size_t nvec);
844 
853 APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
854  apr_brigade_flush flush, void *ctx,
855  const char *str);
856 
865 APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
866  apr_brigade_flush flush, void *ctx,
867  const char c);
868 
877 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
878  apr_brigade_flush flush,
879  void *ctx, ...);
880 
891 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b,
892  apr_brigade_flush flush,
893  void *ctx,
894  const char *fmt, ...)
895  __attribute__((format(printf,4,5)));
896 
907 APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
908  apr_brigade_flush flush,
909  void *ctx,
910  const char *fmt, va_list va);
911 
925  apr_file_t *f,
926  apr_off_t start,
927  apr_off_t len,
928  apr_pool_t *p);
929 
930 
931 
932 /* ***** Bucket freelist functions ***** */
946 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
947 
956 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
957 
962 APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
963 
969 APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
970 
975 APU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
976 
977 
978 /* ***** Bucket Functions ***** */
985 #define apr_bucket_destroy(e) do { \
986  (e)->type->destroy((e)->data); \
987  (e)->free(e); \
988  } while (0)
989 
1001 #define apr_bucket_delete(e) do { \
1002  APR_BUCKET_REMOVE(e); \
1003  apr_bucket_destroy(e); \
1004  } while (0)
1005 
1073 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
1074 
1081 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
1082 
1093 #define apr_bucket_split(e,point) (e)->type->split(e, point)
1094 
1100 #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
1101 
1102 /* Bucket type handling */
1103 
1113 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
1114  apr_pool_t *pool);
1115 
1123 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
1124  apr_pool_t *pool);
1125 
1133 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
1134  apr_size_t point);
1135 
1143 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
1144  apr_bucket **c);
1145 
1155 APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
1156 
1163 /* There is no apr_bucket_read_notimpl, because it is a required function
1164  */
1165 
1166 
1167 /* All of the bucket types implemented by the core */
1172 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
1178 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
1182 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
1187 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
1188 #if APR_HAS_MMAP
1189 
1192 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
1193 #endif
1194 
1199 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
1203 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
1209 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
1215 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
1219 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
1220 
1221 
1222 /* ***** Simple buckets ***** */
1223 
1235 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
1236  apr_size_t point);
1237 
1248 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
1249  apr_bucket **b);
1250 
1251 
1252 /* ***** Shared, reference-counted buckets ***** */
1253 
1268 APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
1269  apr_off_t start,
1270  apr_size_t length);
1271 
1280 APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
1281 
1293 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
1294  apr_size_t point);
1295 
1305 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
1306  apr_bucket **b);
1307 
1308 
1309 /* ***** Functions to Create Buckets of varying types ***** */
1310 /*
1311  * Each bucket type foo has two initialization functions:
1312  * apr_bucket_foo_make which sets up some already-allocated memory as a
1313  * bucket of type foo; and apr_bucket_foo_create which allocates memory
1314  * for the bucket, calls apr_bucket_make_foo, and initializes the
1315  * bucket's list pointers. The apr_bucket_foo_make functions are used
1316  * inside the bucket code to change the type of buckets in place;
1317  * other code should call apr_bucket_foo_create. All the initialization
1318  * functions change nothing if they fail.
1319  */
1320 
1328 
1336 APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
1337 
1346 
1354 APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
1355 
1363 APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
1364  apr_size_t nbyte,
1365  apr_bucket_alloc_t *list);
1366 
1375  const char *buf,
1376  apr_size_t nbyte);
1377 
1385 APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
1386  apr_size_t nbyte,
1387  apr_bucket_alloc_t *list);
1388 
1397  const char *buf,
1398  apr_size_t nbyte);
1399 
1414 APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
1415  apr_size_t nbyte,
1416  void (*free_func)(void *data),
1417  apr_bucket_alloc_t *list);
1427 APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
1428  apr_size_t nbyte,
1429  void (*free_func)(void *data));
1430 
1440 APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
1441  apr_size_t length,
1442  apr_pool_t *pool,
1443  apr_bucket_alloc_t *list);
1444 
1453 APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
1454  apr_size_t length,
1455  apr_pool_t *pool);
1456 
1457 #if APR_HAS_MMAP
1458 
1467 APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
1468  apr_off_t start,
1469  apr_size_t length,
1470  apr_bucket_alloc_t *list);
1471 
1481 APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
1482  apr_off_t start,
1483  apr_size_t length);
1484 #endif
1485 
1492 APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
1493  apr_bucket_alloc_t *list);
1500 APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
1501  apr_socket_t *thissock);
1502 
1509 APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
1510  apr_bucket_alloc_t *list);
1511 
1518 APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
1519  apr_file_t *thispipe);
1520 
1537 APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
1538  apr_off_t offset,
1539  apr_size_t len,
1540  apr_pool_t *p,
1541  apr_bucket_alloc_t *list);
1542 
1553 APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
1554  apr_off_t offset,
1555  apr_size_t len, apr_pool_t *p);
1556 
1563 APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
1564  int enabled);
1565 
1567 #ifdef __cplusplus
1568 }
1569 #endif
1570 
1571 #endif /* !APR_BUCKETS_H */