NAME

Apache::Request - Methods for dealing with client request data

SYNOPSIS

use Apache::Request;
$req = Apache::Request->new($r);
@foo = $req->param("foo");
$bar = $req->args("bar");

DESCRIPTION

The Apache::Request module provides methods for parsing GET and POST parameters encoded with either application/x-www-form-urlencoded or multipart/form-data. Although Apache::Request provides a few new APIs for accessing the parsed data, it remains largely backwards-compatible with the original 1.X API. See the "PORTING from 1.X" section below for a list of known issues.

This manpage documents the Apache::Request package. Apache::Request::Table and Apache::Request::Error are also provided by this module, but are documented elsewhere. Please read the "SEE ALSO" section below for a list of related manpages.

Apache::Request

The interface is designed to mimic the CGI.pm routines for parsing query parameters. The main differences are

  • Apache::Request::new takes an environment-specific object $r as (second) argument. Newer versions of CGI.pm also accept this syntax within modperl.

  • The query parameters are stored in APR::Table derived objects, and are therefore retrieved from the table by using case-insensitive keys.

  • The query string is always parsed immediately, even for POST requests.

new

Apache::Request->new($r, %args)

Creates a new Apache::Request object.

my $req = Apache::Request->new($r, POST_MAX => "1M");

With mod_perl2, the environment object $r must be an Apache::RequestRec object. In that case, all methods from Apache::RequestRec are inherited. In the (default) CGI environment, $r must be an APR::Pool object.

The following args are optional:

  • POST_MAX, MAX_BODY

    Limit the size of POST data (in bytes).

  • DISABLE_UPLOADS

    Disable file uploads.

  • TEMP_DIR

    Sets the directory where upload files are spooled. On a *nix-like that supports link(2), the TEMP_DIR should be located on the same file system as the final destination file:

    use Apache::Upload;
    my $req = Apache::Request->new($r, TEMP_DIR => "/home/httpd/tmp");
    my $upload = $req->upload('file');
    $upload->link("/home/user/myfile");

    For more details on link, see Apache::Upload.

  • HOOK_DATA

    Extra configuration info passed as the fourth argument to an upload hook. See the description for the next item, UPLOAD_HOOK.

  • UPLOAD_HOOK

    Sets up a callback to run whenever file upload data is read. This can be used to provide an upload progress meter during file uploads. Apache will automatically continue writing the original data to $upload->fh after the hook exits.

    my $transparent_hook = sub {
      my ($upload, $data, $data_len, $hook_data) = @_;
      warn "$hook_data: got $data_len bytes for " . $upload->name;
    };
    
    my $req = Apache::Request->new($r, 
                                   HOOK_DATA => "Note",
                                   UPLOAD_HOOK => $transparent_hook,
                                  );

instance

Apache::Request->instance($r)

The default (and only) behavior of Apache::Request is to intelligently cache POST data for the duration of the request. Thus there is no longer the need for a separate instance() method as existed in Apache::Request for Apache 1.3 - all POST data is always available from each and every Apache::Request object created during the request's lifetime.

However an instance() method is aliased to new() in this release to ease the pain of porting from 1.X to 2.X.

param

$req->param()
$req->param($name)

Get the request parameters (using case-insensitive keys) by mimicing the OO interface of CGI::param.

# similar to CGI.pm

my $foo_value   = $req->param('foo');
my @foo_values  = $req->param('foo');
my @param_names = $req->param;

# the following differ slightly from CGI.pm

# returns ref to Apache::Request::Table object representing 
# all (args + body) params
my $table = $req->param;
@table_keys = keys %$table;

In list context, or when invoked with no arguments as $req->param(), param induces libapreq2 to read and parse all remaining data in the request body. However, scalar $req->param("foo") is lazy: libapreq2 will only read and parse more data if

1) no "foo" param appears in the query string arguments, AND
2) no "foo" param appears in the previously parsed POST data.

In this circumstance libapreq2 will read and parse additional blocks of the incoming request body until either

1) it has found the the "foo" param, or 
2) parsing is completed.

Observe that scalar $req->param("foo") will not raise an exception if it can locate "foo" in the existing body or args tables, even if the query-string parser or the body parser has failed. In all other circumstances param will throw an Apache::Request::Error object into $@ should either parser fail.

$req->args_status(1); # set error state for query-string parser 
ok $req->param_status == 1;

$foo = $req->param("foo");
ok $foo == 1;
eval { @foo = $req->param("foo") };
ok $@->isa("Apache::Request::Error");
undef $@;
eval { my $not_found = $req->param("non-existent-param") };
ok $@->isa("Apache::Request::Error");

$req->args_status(0); # reset query-string parser state to "success"

Note: modifications to the scalar $req->param() table only affect the returned table object (the underlying C apr_table_t is generated from the parse data by apreq_params()). Modifications do not affect the actual request data, and will not be seen by other libapreq2 applications.

parms, params

The functionality of these functions is assumed by param, so they are no longer necessary. Aliases to param are provided in this release for backwards compatibility, however they are deprecated and may be removed from a future release.

args

$req->args()
$req->args($name)

Returns an Apache::Request::Table object containing the query-string parameters of the Apache::Request object.

my $args = $req->args;

An optional name parameter can be passed to return the query string parameter associated with the given name:

my $foo_arg = $req->args("foo");

More generally, args() follows the same pattern as param() with respect to its return values and argument list. The main difference is that modifications to the scalar $req->args() table affect the underlying apr_table_t attribute in apreq_request_t, so their impact will be noticed by all libapreq2 applications during this request.

body

$req->body()
$req->body($name)

Returns an Apache::Request::Table object containing the POST data parameters of the Apache::Request object.

my $body = $req->body;

An optional name parameter can be passed to return the POST data parameter associated with the given name:

my $foo_body = $req->body("foo");

More generally, body() follows the same pattern as param() with respect to its return values and argument list. The main difference is that modifications to the scalar $req->body() table affect the underlying apr_table_t attribute in apreq_request_t, so their impact will be noticed by all libapreq2 applications during this request.

upload

$req->upload()
$req->upload($name)

Requires Apache::Upload. With no arguments, this method returns an Apache::Upload::Table object in scalar context, or the names of all Apache::Upload objects in list context.

An optional name parameter can be passed to return the Apache::Upload object associated with the given name:

my $upload = $req->upload($name);

More generally, upload() follows the same pattern as param() with respect to its return values and argument list. The main difference is that its returned values are Apache::Upload object refs, not simple scalars.

Note: modifications to the scalar $req->upload() table only affect the returned table object (the underlying C apr_table_t is generated by apreq_uploads()). They do not affect the actual request data, and will not be seen by other libapreq2 applications.

args_status

$req->args_status()
$req->args_status($set)

Get/set the APR status code of the query-string parser. APR_SUCCESS on success, error otherwise.

body_status

$req->body_status()
$req->body_status($set)

Get/set the current APR status code of the parsed POST data. APR_SUCCESS when parser has completed, APR_INCOMPLETE if parser has more data to parse, APR_EINIT if no post data has been parsed, error otherwise.

param_status

$req->param_status()

In scalar context, this returns args_status if there was an error during the query-string parse, otherwise this returns body_status, ie

$req->args_status || $req->body_status

In list context param_status returns the list (args_status, body_status).

parse

$req->parse()

Forces the request to be parsed immediately. In void context, this will throw an Apache::Request::Error should the either the query-string or body parser fail. In all other contexts it will return the two parsers' combined APR status code

$req->body_status || $req->args_status

However parse should be avoided in most normal situations. For example, in a mod_perl content handler it is more efficient to write

sub handler {
    my $r = shift;
    my $req = Apache::Request->new($r);
    $r->discard_request_body;   # efficiently parses the request body
    my $parser_status = $req->body_status;

    #...
}

Calling $r->discard_request_body outside the content handler is generally a mistake, so use $req->parse there, but only as a last resort. The Apache::Request API is designed around a lazy-parsing scheme, so calling parse should not affect the behavior of any other methods.

SUBCLASSING Apache::Request

If the instances of your subclass are hash references then you can actually inherit from Apache::Request as long as the Apache::Request object is stored in an attribute called "r" or "_r". (The Apache::Request class effectively does the delegation for you automagically, as long as it knows where to find the Apache::Request object to delegate to.) For example:

package MySubClass;
use Apache::Request;
our @ISA = qw(Apache::Request);
sub new {
	my($class, @args) = @_;
	return bless { r => Apache::Request->new(@args) }, $class;
}

PORTING from 1.X

This is the complete list of changes to existing methods from Apache::Request 1.X. These issues need to be addressed when porting 1.X apps to the new 2.X API.

  • Apache::Upload is now a separate module. Applications requiring the upload API must use Apache::Upload in 2.X. This is easily addressed by preloading the modules during server startup.

  • You must use the Apache::Request::Table API via scalar $req->args or scalar $req->body to assign new parameters to the request. You may no longer use the two-argument method calls; e.g.

    $req->param("foo" => "bar"); # NO: usage error in 2.X
    $req->args->{foo} = "bar";   # OK: assign to args table
    $req->body->add(foo => "bar");  # OK: add to body table
    
    $req->param->add(foo => "bar"); # NO: this is an expensive noop,
                                    # because the param table is
                                    # generated by overlaying $req->args
                                    # and $req->body.
    
    my $params = $req->param;
    $params->set(foo => "bar"); # OK: sets "foo" entry in $params, which
                                # is a local (args + body) table. Neither
                                # $req->args, $req->body, nor future calls
                                # to $req->param, are affected by mods to
                                # $params.
  • instance() is now identical to new(), and is now deprecated. It may be removed from a future 2.X release.

  • param includes the functionality of parms() and params(), so they are now deprecated and may be removed from a future 2.X release.

SEE ALSO

Apache::Request::Table, Apache::Request::Error, Apache::Upload, Apache::Cookie, APR::Table(3).

COPYRIGHT

Copyright 2003-2005  The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.