JF::ApacheRequest - a more complete Apache2::Request object


  use JF::ApacheRequest;
  my $r = JF::ApacheRequest->new;
  my $name   = $r->param("name");
  my $cookie = $r->cookie("auth");
  my $image  = $r->upload("image");


For starters, this object inherits from Apache2::Request (which inherits from Apache), so it does everything that either request object can do.

There are a few differences and extras. First, the differences:

JF::ApacheRequest->new() can be called without any arguments - it will get the Apache2::Request itself.

A call to $r->param() is NOT context sensitive. It never results in multiple values or an empty list. This is helpful in preventing unexpected (and possibly insecure) behavior. If there are multiple values, you get back only the first. If there were no values you get back undef. To get an arrayref of multiple values you must instead call $r->multi_param(). If there were none you get back an empty arrayref.

Then we have several extra enhancements:

  # get uploads
  my @upload_names = $r->upload();
  my ($upload_filename, $upload_data) = $r->upload("image");

These work just like their $r->param() counterparts, although you can't set values for an upload.

  # get cookies
  my @cookie_names = $r->cookie();
  my $cookie = $r->cookie("stuff");
  # set cookies
  $r->cookie("stuff", "12345");
  $r->cookie("mostuff", "67890",
      { -expires => "+1y", -path => "/cgi-bin/" }

Works like $r->param(). You can set a cookie with just a name and value. You can also pass in a hashref of the values you would pass to Apache2::Cookie->new(), minus the -name and -value (which are the first and second arguments instead). If you don't pass a hashref, the default will be different from Apache2::Cookie's default in two ways: the -path will be set to ``/'' so the cookie will be returned to all URL's at your site. You can override this if you pass -path in the hashref. The domain will be stripped of it's subdomain (i.e. ``'' and ``'' both become ``'')

  my $in = $r->param_hashref();
  my $up = $r->upload_hashref();
  my $ck = $r->cookie_hashref();

If you want the params, uploads, or cookies in a hashref, the above functions is how you would get them.

  my $newstring   = JF::Request->html_escape( $string );
  my $newarrayref = JF::Request->html_escape( $arrayref );
  my $newhashref  = JF::Request->html_escape( $hashref );
  my $encoded = JF::Request->url_encode( $string );
  my $decoded = JF::Request->url_decode( $string );

These functions convert strings as expected. They can also take arrayrefs as an argument and will convert each element in the array, or if a hashref is passed they will convert all the values (not the keys).

There's also $r->this_url which returns the server-relative url including any query string arguments ... basically just a combination of $r->uri and $r->query_string with enough smarts to leave off a trailing ``?'' if there's no query string.

That's about it.

Oh yeah - one last conveniece. A simple redirect method:

  return $r->redirect( $url );




  Apache2::Request, Apache2::Cookie, Apache2::Util


Jonathan Field -