httpd-apreq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Schaefer <joe+gm...@sunstarsys.com>
Subject Re: Error handling in latest dev snapshot
Date Tue, 03 Aug 2004 02:39:03 GMT
fred@taperfriendlymusic.org writes:

[...]

> If part of the data is bad or contains some information I want to
> abort the upload and redirect.  I thought a good approach would be to
> die inside the $upload_hook sub, catch it and redirect but maybe there
> is a more elegant solution.

Having the upload hook die is the proper way to tell the 
mfd parser to immediately abort (the upload that triggered
the hook to croak will not appear in the body table). 
However this will leave the parser in an error state, 
so any Apache::Request method that requires a successful parse 
will wind up throwing an Apache::Request::Error.

To catch it, the caller can use an eval {} wrapper
  
  eval { $self->upload($r) }; # run my upload method
  if (ref $@ and $@->isa("Apache::Request::Error")) {
    # ... handle Apache::Request::Error object in $@
  }


or the upload method can force an immediate full parse, 
using say scalar $req->parse(). An error occurred if 
its return value is non-zero.

> Any feedback on this is appreciated.
> 
> sub upload : method {
>   my $self = shift;
>   my $r    = shift;
>   my $upload_hook = sub {
>     my ($upload, $data, $data_len, $hook_data) = @_;
>       # do some stuff with $data
>       # Die and/or redirect if $data fits a certain profile
>     }
>   my $req  = Apache::Request->new($r,
>                   HOOK_DATA   => 'Note',
>                   UPLOAD_HOOK => $upload_hook,
>                   TEMP_DIR    => '/tmp' );
>   my $upload = $req->upload(($req->upload)[0]);
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

<LongAside>
This is a very bad idiom, and should be removed from
our tests.  It would be a bit safer to write this

  my $upload = $req->upload(scalar +($req->upload)[0]);

because ($req->upload)[0] is the empty list when no uploads
are sent (in that case $upload is not an undef, but an 
empty Apache::Upload::Table instead).
</LongAside>

-- 
Joe Schaefer


Mime
View raw message