[Nginx] perl module get an empty body

Maxim Dounin mdounin at mdounin.ru
Fri Aug 10 17:11:18 UTC 2012


Hello!

On Fri, Aug 10, 2012 at 10:12:56AM -0400, darkweaver871 wrote:

> OK, so is this better ?
> 
> #!/usr/bin/perl
> 
> package switcher_test;
> use nginx;
> 
> use Sys::Syslog;
> 
> my $facility = 'local2';
> 
> sub handler {
>         my $r = shift;
> 
>         return $r->has_request_body(\& test);
> 
> }
> 
> sub test {
>         my $r = shift;
>         if ($r->request_body =~ /.*test.*/) { return 3 };
>         openlog('switcher', 'ndelay', $facility);
>         syslog(LOG_DEBUG, 'request_body: '.$r->request_body);
>         syslog(LOG_DEBUG, 'request_body_file: '.$r->request_body_file);
>         return 4;
> }
> 
> 1;
> __END__
> 
> 
> It still doesn't work this way.

Again: you can't use this code in perl_set directive.  You have to 
use perl directive:

    perl switcher_test::handler;

And in perl package (mostly cut-n-paste from docs, with an 
additional test for body):

package switcher_test;

use nginx;

sub handler {
    my $r = shift;

    if ($r->request_method ne "POST") {
        return DECLINED;
    }

    if ($r->has_request_body(\&post)) {
        return OK;
    }

    return HTTP_BAD_REQUEST;
}

sub post {
    my $r = shift;

    $r->send_http_header;

    $r->print("request_body: \"", $r->request_body, "\"<br/>");
    $r->print("request_body_file: \"", $r->request_body_file, "\"<br/>\n");

    if ($r->request_body =~ /test/) {
        return HTTP_BAD_REQUEST;
    }

    return OK;
}

1;

Maxim Dounin



More information about the nginx mailing list