You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-dev@httpd.apache.org by fr...@taperfriendlymusic.org on 2004/08/02 05:31:31 UTC

Error handling in latest dev snapshot

Greetings,

Using libapreq2-2.04-dev, in a upload_hook subroutine my code dies on an
error condition and I get the following error message:

[Sun Aug 01 23:13:46 2004] [error] [client 127.0.0.1]
Apache::Request::upload: (20014) Error string not specified yet

Reading through the archives I came across discussion of implementation
details for error handling between Stas and Joe.  Is there an existing die
handler for Apache::Request::upload which I can utilize, or is that still
in the works?

Many thanks,

Fred

Re: Error handling in latest dev snapshot

Posted by Joe Schaefer <jo...@sunstarsys.com>.
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


Re: Error handling in latest dev snapshot

Posted by fr...@taperfriendlymusic.org.
> fred@taperfriendlymusic.org writes:
>
>> Using libapreq2-2.04-dev, in a upload_hook subroutine my code dies on
>> an error condition and I get the following error message:
>>
>> [Sun Aug 01 23:13:46 2004] [error] [client 127.0.0.1]
>> Apache::Request::upload: (20014) Error string not specified yet
>
> You should not call $req->upload within an upload hook.  Hooks are
> parser extensions and are *not suitable* for casual use.  Don't use
> UPLOAD_HOOK unless you absolutely must process the upload data
> *during parsing*.

My call to $req->upload is made outside of the upload_hook sub.  My
application is required to process the upload data during parsing so I
thought this was the best approach.  The other option I had in mind was to
process the data using an input filter and the bucket brigades api but
this looked easier and more like the right tool for the job.

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.  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]);
  # redirect at end of upload
}

>
> --
> Joe Schaefer
>
>


Re: Error handling in latest dev snapshot

Posted by Joe Schaefer <jo...@sunstarsys.com>.
fred@taperfriendlymusic.org writes:

> Using libapreq2-2.04-dev, in a upload_hook subroutine my code dies on
> an error condition and I get the following error message:
> 
> [Sun Aug 01 23:13:46 2004] [error] [client 127.0.0.1]
> Apache::Request::upload: (20014) Error string not specified yet

You should not call $req->upload within an upload hook.  Hooks are 
parser extensions and are *not suitable* for casual use.  Don't use 
UPLOAD_HOOK unless you absolutely must process the upload data 
*during parsing*.

-- 
Joe Schaefer