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 
696  apr_bucket *e,
697  apr_bucket_brigade *a);
698 
711  apr_bucket *e);
712 
725 APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
726  apr_off_t point,
727  apr_bucket **after_point);
728 
737 APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
738  int read_all,
739  apr_off_t *length);
740 
748 APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
749  char *c,
750  apr_size_t *len);
751 
759 APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb,
760  char **c,
761  apr_size_t *len,
762  apr_pool_t *pool);
763 
772 APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
773  apr_bucket_brigade *bbIn,
774  apr_read_type_e block,
775  apr_off_t maxbytes);
776 
786 APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b,
787  struct iovec *vec, int *nvec);
788 
797 APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
798  apr_brigade_flush flush,
799  void *ctx,
800  va_list va);
801 
825 APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
826  apr_brigade_flush flush, void *ctx,
827  const char *str, apr_size_t nbyte);
828 
838 APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
839  apr_brigade_flush flush,
840  void *ctx,
841  const struct iovec *vec,
842  apr_size_t nvec);
843 
852 APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
853  apr_brigade_flush flush, void *ctx,
854  const char *str);
855 
864 APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
865  apr_brigade_flush flush, void *ctx,
866  const char c);
867 
876 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
877  apr_brigade_flush flush,
878  void *ctx, ...);
879 
890 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b,
891  apr_brigade_flush flush,
892  void *ctx,
893  const char *fmt, ...)
894  __attribute__((format(printf,4,5)));
895 
906 APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b,
907  apr_brigade_flush flush,
908  void *ctx,
909  const char *fmt, va_list va);
910 
924  apr_file_t *f,
925  apr_off_t start,
926  apr_off_t len,
927  apr_pool_t *p);
928 
929 
930 
931 /* ***** Bucket freelist functions ***** */
945 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
946 
955 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
956 
961 APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
962 
968 APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
969 
974 APU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
975 
976 
977 /* ***** Bucket Functions ***** */
984 #define apr_bucket_destroy(e) do { \
985  (e)->type->destroy((e)->data); \
986  (e)->free(e); \
987  } while (0)
988 
1000 #define apr_bucket_delete(e) do { \
1001  APR_BUCKET_REMOVE(e); \
1002  apr_bucket_destroy(e); \
1003  } while (0)
1004 
1072 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
1073 
1080 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
1081 
1092 #define apr_bucket_split(e,point) (e)->type->split(e, point)
1093 
1099 #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
1100 
1101 /* Bucket type handling */
1102 
1112 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
1113  apr_pool_t *pool);
1114 
1122 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
1123  apr_pool_t *pool);
1124 
1132 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
1133  apr_size_t point);
1134 
1142 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
1143  apr_bucket **c);
1144 
1154 APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
1155 
1162 /* There is no apr_bucket_read_notimpl, because it is a required function
1163  */
1164 
1165 
1166 /* All of the bucket types implemented by the core */
1171 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
1177 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
1181 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
1186 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
1187 #if APR_HAS_MMAP
1188 
1191 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
1192 #endif
1193 
1198 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
1202 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
1208 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
1214 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
1218 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
1219 
1220 
1221 /* ***** Simple buckets ***** */
1222 
1234 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
1235  apr_size_t point);
1236 
1247 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
1248  apr_bucket **b);
1249 
1250 
1251 /* ***** Shared, reference-counted buckets ***** */
1252 
1267 APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
1268  apr_off_t start,
1269  apr_size_t length);
1270 
1279 APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
1280 
1292 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
1293  apr_size_t point);
1294 
1304 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
1305  apr_bucket **b);
1306 
1307 
1308 /* ***** Functions to Create Buckets of varying types ***** */
1309 /*
1310  * Each bucket type foo has two initialization functions:
1311  * apr_bucket_foo_make which sets up some already-allocated memory as a
1312  * bucket of type foo; and apr_bucket_foo_create which allocates memory
1313  * for the bucket, calls apr_bucket_make_foo, and initializes the
1314  * bucket's list pointers. The apr_bucket_foo_make functions are used
1315  * inside the bucket code to change the type of buckets in place;
1316  * other code should call apr_bucket_foo_create. All the initialization
1317  * functions change nothing if they fail.
1318  */
1319 
1327 
1335 APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
1336 
1345 
1353 APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
1354 
1362 APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf,
1363  apr_size_t nbyte,
1364  apr_bucket_alloc_t *list);
1365 
1374  const char *buf,
1375  apr_size_t nbyte);
1376 
1384 APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf,
1385  apr_size_t nbyte,
1386  apr_bucket_alloc_t *list);
1387 
1396  const char *buf,
1397  apr_size_t nbyte);
1398 
1413 APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
1414  apr_size_t nbyte,
1415  void (*free_func)(void *data),
1416  apr_bucket_alloc_t *list);
1426 APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
1427  apr_size_t nbyte,
1428  void (*free_func)(void *data));
1429 
1439 APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf,
1440  apr_size_t length,
1441  apr_pool_t *pool,
1442  apr_bucket_alloc_t *list);
1443 
1452 APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
1453  apr_size_t length,
1454  apr_pool_t *pool);
1455 
1456 #if APR_HAS_MMAP
1457 
1466 APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
1467  apr_off_t start,
1468  apr_size_t length,
1469  apr_bucket_alloc_t *list);
1470 
1480 APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
1481  apr_off_t start,
1482  apr_size_t length);
1483 #endif
1484 
1491 APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
1492  apr_bucket_alloc_t *list);
1499 APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b,
1500  apr_socket_t *thissock);
1501 
1508 APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
1509  apr_bucket_alloc_t *list);
1510 
1517 APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b,
1518  apr_file_t *thispipe);
1519 
1536 APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
1537  apr_off_t offset,
1538  apr_size_t len,
1539  apr_pool_t *p,
1540  apr_bucket_alloc_t *list);
1541 
1552 APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
1553  apr_off_t offset,
1554  apr_size_t len, apr_pool_t *p);
1555 
1562 APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
1563  int enabled);
1564 
1566 #ifdef __cplusplus
1567 }
1568 #endif
1569 
1570 #endif /* !APR_BUCKETS_H */