mod_perl logo perl icon
previous page: APR::Brigade - Perl API for manipulating APR Bucket Brigadespage up: mod_perl 2.0 APInext page: APR::BucketAlloc - Perl API for Bucket Allocation

APR::Bucket - Perl API for manipulating APR Buckets






mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford
Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes


Table of Contents

Synopsis

  use APR::Bucket ();
  my $ba = $c->bucket_alloc;
  
  $b1 = APR::Bucket->new($ba, "aaa");
  $b2 = APR::Bucket::eos_create($ba);
  $b3 = APR::Bucket::flush_create($ba);
  
  $b2->is_eos;
  $b3->is_flush;
  
  $len = $b1->length;
  $len = $b1->read($data);
  $type = $b1->type;
  
  $b1->insert_after($b2);
  $b1->insert_before($b3);
  $b1->remove;
  $b1->destroy;
  
  $b2->delete; # remove+destroy
  
  $b4 = APR::Bucket->new($ba, "to be setaside");
  $b4->setaside($pool);


TOP

Description

APR::Bucket allows you to create, manipulate and delete APR buckets.

You will probably find the various insert methods confusing, the tip is to read the function right to left. The following code sample helps to visualize the operations:

  my $bb = APR::Brigade->new($r->pool, $ba);
  my $d1 = APR::Bucket->new($ba, "d1");
  my $d2 = APR::Bucket->new($ba, "d2");
  my $f1 = APR::Bucket::flush_create($ba);
  my $f2 = APR::Bucket::flush_create($ba);
  my $e1 = APR::Bucket::eos_create($ba);
                           # head->tail
  $bb->insert_head(  $d1); # head->d1->tail
  $d1->insert_after( $d2); # head->d1->d2->tail
  $d2->insert_before($f1); # head->d1->f1->d2->tail
  $d2->insert_after( $f2); # head->d1->f1->d2->f2->tail
  $bb->insert_tail(  $e1); # head->d1->f1->d2->f2->e1->tail


TOP

API

APR::Bucket provides the following functions and/or methods:



TOP

delete

Tell the bucket to remove itself from the bucket brigade it belongs to, and destroy itself.

  $bucket->delete();

If the bucket is not attached to any bucket brigade then this operation just destroys the bucket.

delete is a convenience wrapper, internally doing:

  $b->remove;
  $b->destroy;

Examples:

Assuming that $bb already exists and filled with buckets, replace the existing data buckets with new buckets with upcased data;

  for (my $b = $bb->first; $b; $b = $bb->next($b)) {
     if ($b->read(my $data)) {
          my $nb = APR::Bucket->new($bb->bucket_alloc, uc $data);
          $b->insert_before($nb);
          $b->delete;
          $b = $nb;
      }
  }


TOP

destroy

Free the resources used by a bucket. If multiple buckets refer to the same resource it is freed when the last one goes away.

  $bucket->destroy();

A bucket needs to be destroyed if it was removed from a bucket brigade, to avoid memory leak.

If a bucket is linked to a bucket brigade, it needs to be removed from it, before it can be destroyed.

Usually instead of calling:

  $b->remove;
  $b->destroy;

it's better to call delete which does exactly that.



TOP

eos_create

Create an EndOfStream bucket.

  $b = APR::Bucket::eos_create($ba);

This bucket type indicates that there is no more data coming from down the filter stack. All filters should flush any buffered data at this point.

Example:

  use APR::Bucket ();
  use Apache2::Connection ();
  my $ba = $c->bucket_alloc;
  my $eos_b = APR::Bucket::eos_create($ba);


TOP

flush_create

Create a flush bucket.

  $b = APR::Bucket::flush_create($ba);

This bucket type indicates that filters should flush their data. There is no guarantee that they will flush it, but this is the best we can do.



TOP

insert_after

Insert a list of buckets after a specified bucket

  $after_bucket->insert_after($add_bucket);


TOP

insert_before

Insert a list of buckets before a specified bucket

  $before_bucket->insert_before($add_bucket);


TOP

is_eos

Determine if a bucket is an EOS bucket

  $ret = $bucket->is_eos();


TOP

is_flush

Determine if a bucket is a FLUSH bucket

  $ret = $bucket->is_flush();


TOP

length

Get the length of the data in the bucket.

  $len = $b->length;


TOP

new

Create a new bucket and initialize it with data:

  $nb = APR::Bucket->new($ba, $data);
  $nb =          $b->new($ba, $data);
  $nb = APR::Bucket->new($ba, $data, $offset);
  $nb = APR::Bucket->new($ba, $data, $offset, $len);

Examples:



TOP

read

Read the data from the bucket.

  $len = $b->read($buffer);
  $len = $b->read($buffer, $block);

It's important to know that certain bucket types (e.g. file bucket), may perform a split and insert extra buckets following the current one. Therefore never call $b->remove, before calling $b->read, or you may lose data.

Examples:

Blocking read:

  my $len = $b->read(my $buffer);

Non-blocking read:

  use APR::Const -compile 'NONBLOCK_READ';
  my $len = $b->read(my $buffer, APR::Const::NONBLOCK_READ);


TOP

remove

Tell the bucket to remove itself from the bucket brigade it belongs to.

  $bucket->remove();

If the bucket is not attached to any bucket brigade then this operation doesn't do anything.

When the bucket is removed, it's not not destroyed. Usually this is done in order to move the bucket to another bucket brigade. Or to copy the data way before destroying the bucket. If the bucket wasn't moved to another bucket brigade it must be destroyed.

Examples:

Assuming that $bb1 already exists and filled with buckets, move every odd bucket number to $bb2 and every even to $bb3:

  my $bb2 = APR::Brigade->new($c->pool, $c->bucket_alloc);
  my $bb3 = APR::Brigade->new($c->pool, $c->bucket_alloc);
  my $count = 0;
  while (my $bucket = $bb->first) {
      $count++;
      $bucket->remove;
      $count % 2
          ? $bb2->insert_tail($bucket)
          : $bb3->insert_tail($bucket);
  }


TOP

setaside

Ensure the bucket's data lasts at least as long as the given pool:

  my $status = $b->setaside($pool);

Usually setaside is called by certain output filters, in order to buffer socket writes of smaller buckets into a single write. This method works on all bucket types (not only the mod_perl bucket type), but as explained in the exceptions section, not all bucket types implement this method.

When a mod_perl bucket is setaside, its data is detached from the original perl scalar and copied into a pool bucket. That allows downstream filters to deal with the data originally owned by a Perl interpreter, making it possible for that interpreter to go away and do other things, or be destroyed.



TOP

type

Get the type of the data in the bucket.

  $type = $b->type;

You need to invoke APR::BucketType methods to access the data.

Example:

Create a flush bucket and read its type's name:

  use APR::Bucket ();
  use APR::BucketType ();
  my $b = APR::Bucket::flush_create($ba);
  my $type = $b->type;
  my $type_name =  $type->name; # FLUSH

The type name will be 'FLUSH' in this example.



TOP

Unsupported API

APR::Socket also provides auto-generated Perl interface for a few other methods which aren't tested at the moment and therefore their API is a subject to change. These methods will be finalized later as a need arises. If you want to rely on any of the following methods please contact the the mod_perl development mailing list so we can help each other take the steps necessary to shift the method to an officially supported API.



TOP

data

  $data = $b->data;

Gives a C pointer to the address of the data in the bucket. I can't see what use can be done of it in Perl.



TOP

start

  $start = $b->start;

It gives the offset to when a new bucket is created with a non-zero offset value:

  my $b = APR::Bucket->new($ba, $data, $offset, $len);

So if the offset was 3. $start will be 3 too.

I fail to see what it can be useful for to the end user (it's mainly used internally).



TOP

See Also

mod_perl 2.0 documentation.



TOP

Copyright

mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.



TOP

Authors

The mod_perl development team and numerous contributors.






TOP
previous page: APR::Brigade - Perl API for manipulating APR Bucket Brigadespage up: mod_perl 2.0 APInext page: APR::BucketAlloc - Perl API for Bucket Allocation