【问题标题】:Getting the body of an http POST request, using mod-perl 2使用 mod-perl 2 获取 http POST 请求的正文
【发布时间】:2015-05-07 03:32:39
【问题描述】:

我正在编写一个快速脚本来处理提交的文件,并将该内容返回给用户。

我的测试代码如下所示:

#!/path/to/bin/perl
use strict;
use warnings;
use utf8;

use Apache2::RequestRec;
use Apache2::RequestIO; 

my ( $xmlin, $accepts ) = (q{}, q{});
my $format = 'json';

# read the posted content
while (
    Apache2::RequestIO::read($xmlin, 1024)
      ) {};

{
  no warnings;
  $accepts = $Apache2::RequestRec::headers_in{'Accepts'};
}
if ($accepts) {
  for ($accepts) {
    /application\/xml/i && do {
      $format = 'xml';
      last;
    };
    /text\/plain/i && do {
      $format = 'text';
      last;
    };
  } ## end for ($accepts)
} ## end if ($accepts)
print "format: $format; xml: $xmlin\n";

这段代码使用Undefined subroutine &Apache2::RequestIO::read编译失败

如果我注释掉while循环,代码运行正常。

不幸的是,Apache2::RequestIO 代码是通过 Apache2::XSLoader::load __PACKAGE__; 提取的,所以我无法检查实际代码....但我不明白为什么这不起作用

(是的,我也尝试过$r->read(...),但无济于事)

【问题讨论】:

  • 你能发布你的 apache 配置吗?

标签: perl apache2 mod-perl2


【解决方案1】:

我想我很清楚您的代码为什么不起作用。

Apache2::RequestIO 模块为 Apache2::RequestRec 添加了新功能。

换句话说,将新方法/函数添加到 Apache2::RequestRec 命名空间。

我首先将 Apache2::RequestIO::read 更改为 Apache2::RequestRec::read。

如果这不起作用,请使用处理程序。

我的代码可以做类似的事情

在你的 httpd.conf 中

PerlSwitches -I/path/to/module_dir
PerlLoadModule ModuleName
PerlResponseHandler ModuleName

模块名称.pm

package ModuleName;
use strict;
use warnings;
use Apache2::RequestIO();
use Apache2::RequestRec();
use Apache2::Const -compile => qw(OK);
sub handler {
    my ($r) = @_;
    {
        use bytes;
        my $content = '';
        my $offset = 0;
        my $cnt = 0;
        do {
            $cnt = $r->read($content,8192,$offset);
            $offset += $cnt;
        } while($cnt == 8192);
    }
   return Apache2::Const::HTTP_OK;
}

【讨论】:

    【解决方案2】:

    我也用Apache2::RequestIO读正文:

    sub body {
        my $self =  shift;
    
        return $self->{ body }   if defined $self->{ body };
        $self->apr->read( $self->{ body }, $self->headers_in->get( 'Content-Length' ) );
    
        $self->{ body };
    }
    

    在这种情况下,您应该继承原始Apache2::Request。特别注意our @ISA = qw(Apache2::Request);

    我不知道为什么,但是标准的body 方法返回我:

    $self->body        # {}
    $self->body_status # Missing parser
    

    Content-Typeapplication/json。所以我以这种方式解决这个问题。然后自己解析body:

    sub content {
        my $self =  shift;
    
        return   $self->{ content }   if defined $self->{ content };
    
        my $content_type =  $self->headers_in->get('Content-Type');
        $content_type =~ s/^(.*?);.*$/$1/;
    
        return   unless exists $self->{ $content_type };
    
        return $self->{ content } =  $self->{ $content_type }( $self->body, $self );
    }
    

    地点:

    use JSON;
    sub new {
        my ($proto, $r) = @_;
    
        my $self = $proto->SUPER::new($r);
        $self->{ 'application/json' } =  sub {
            decode_json shift;
        };
        return $self;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-10
      • 2016-05-03
      • 2011-05-27
      • 2015-02-18
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多