mod_perl logo perl icon
previous page: Apache2::SubProcess -- Executing SubProcesses under mod_perlpage up: mod_perl 2.0 APInext page: Apache2::URI - Perl API for manipulating URIs

Apache2::SubRequest - Perl API for Apache subrequests






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
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


Table of Contents

Synopsis

  use Apache2::SubRequest ();
  
  # run internal redirects at once
  $r->internal_redirect($new_uri);
  $r->internal_redirect_handler($new_uri);
  
  # create internal redirect objects
  $subr = $r->lookup_uri("/foo");
  $subr = $r->lookup_method_uri("GET", "/tmp/bar")
  $subr = $r->lookup_file("/tmp/bar");
  # optionally manipulate the output through main request filters
  $subr = $r->lookup_uri("/foo", $r->output_filters);
  # now run them
  my $rc = $subr->run;


TOP

Description

Apache2::SubRequest contains API for creating and running of Apache sub-requests.

Apache2::SubRequest is a sub-class of Apache2::RequestRec object.



TOP

API

Apache2::SubRequest provides the following functions and/or methods:



TOP

DESTROY

Free the memory associated with a sub request:

  undef $subr; # but normally don't do that

DESTROY is called automatically when $subr goes out of scope.

If you want to free the memory earlier than that (for example if you run several subrequests), you can undef the object as:

  undef $subr;

but never call DESTROY explicitly, since it'll result in ap_destroy_sub_req being called more than once, resulting in multiple brain injuries and certain hair loss.



TOP

internal_redirect

Redirect the current request to some other uri internally

  $r->internal_redirect($new_uri);

In case that you want some other request to be served as the top-level request instead of what the client requested directly, call this method from a handler, and then immediately return Apache2::Const::OK. The client will be unaware the a different request was served to her behind the scenes.



TOP

internal_redirect_handler

Identical to internal_redirect, plus automatically sets $r->content_type is of the sub-request to be the same as of the main request, if $r->handler is true.

  $r->internal_redirect_handler($new_uri);

This function is designed for things like actions or CGI scripts, when using AddHandler, and you want to preserve the content type across an internal redirect.



TOP

lookup_file

Create a subrequest for the given file. This sub request can be inspected to find information about the requested file

  $ret = $r->lookup_file($new_file);
  $ret = $r->lookup_file($new_file, $next_filter);

See $r->lookup_uri for further discussion.



TOP

lookup_method_uri

Create a sub request for the given URI using a specific method. This sub request can be inspected to find information about the requested URI

  $ret = $r->lookup_method_uri($method, $new_uri);
  $ret = $r->lookup_method_uri($method, $new_uri, $next_filter);

See $r->lookup_uri for further discussion.



TOP

lookup_uri

Create a sub request from the given URI. This sub request can be inspected to find information about the requested URI.

  $ret = $r->lookup_uri($new_uri);
  $ret = $r->lookup_uri($new_uri, $next_filter);

Here is an example of a simple subrequest which serves uri /new_uri:

  sub handler {
      my $r = shift;
  
      my $subr = $r->lookup_uri("/new_uri");
      $subr->run;
  
      return Apache2::Const::OK;
  }

If let's say you have three request output filters registered to run for the main request:

  PerlOutputFilterHandler MyApache2::SubReqExample::filterA
  PerlOutputFilterHandler MyApache2::SubReqExample::filterB
  PerlOutputFilterHandler MyApache2::SubReqExample::filterC

and you wish to run them all, the code needs to become:

      my $subr = $r->lookup_uri("/new_uri", $r->output_filters);

and if you wish to run them all, but the first one (filterA), the code needs to be adjusted to be:

      my $subr = $r->lookup_uri("/new_uri", $r->output_filters->next);


TOP

run

Run a sub-request

  $rc = $subr->run();


TOP

Unsupported API

Apache2::SubRequest 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

internal_fast_redirect

META: Autogenerated - needs to be reviewed/completed

Redirect the current request to a sub_req, merging the pools

  $r->internal_fast_redirect($sub_req);

META: httpd-2.0/modules/http/http_request.c declares this function as:

  /* XXX: Is this function is so bogus and fragile that we deep-6 it? */

do we really want to expose it to mod_perl users?



TOP

lookup_dirent

META: Autogenerated - needs to be reviewed/completed

Create a sub request for the given apr_dir_read result. This sub request can be inspected to find information about the requested file

  $lr = $r->lookup_dirent($finfo);
  $lr = $r->lookup_dirent($finfo, $subtype);
  $lr = $r->lookup_dirent($finfo, $subtype, $next_filter);

META: where do we take the apr_dir_read result from?



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: Apache2::SubProcess -- Executing SubProcesses under mod_perlpage up: mod_perl 2.0 APInext page: Apache2::URI - Perl API for manipulating URIs