2

我有以下代码:

...
sub setImage {
    my $self=shift;
    my $filename=shift;
    unless(-r $filename) {
        warn "File $filename not found";
        return;
    }
    my $imgn=shift;
    my $operation=&URI::Escape::uri_escape_utf8(
        (shift) ? "Удалить! (Delete)" : "Сохранить! (Store)");
    my $FH=&::File::open($filename, 0, 0);
    my $image;
    # &utf8::downgrade($image);
    sysread($FH, $image, 102400, 0);
    close $FH;
    my $imginfo=eval{&Image::Info::image_info(\$image)};
    if($@ or $imginfo->{"error"}) {
        warn "Invalid image: ".($@ || $imginfo->{"error"});
        return undef;
    }
    my $fields=[
        DIR       => $self->url("fl"),
        OPERATION => $operation,
        FILE_NAME => ".photo$imgn",
        # FILE      => [$filename],
        FILE      => [undef, "image.".$imginfo->{"file_ext"},
            # Content_Type => $imginfo->{"file_media_type"},
            # Content_Type => 'application/octet-stream',
            Content      => $image,
        ],
    ];
    my $response=&ZLR::UA::post(
        &ZLR::UA::absURL("/cgi-bin/file_manager")."",
        $fields,
        Content_Type => "form-data",
    );
    print $response->decoded_content;
}
...

当我尝试使用函数 setImage 它失败并出现错误HTTP::Message content must be bytes at /usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm line 91。更糟糕的是,如果不使用我的所有代码并且升级 libwww-perl 就无法重现此错误。什么会导致它?

libww-perl 的版本:dev-perl/libwww-perl-5.836。HTTP::Request 和 HTTP::Request::Common 来自 libwww-perl 包,版本:5.827 和 5.824。

痕迹:

HTTP::Message content must be bytes at /usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm line 91
 at Carp::croak(unknown source)
 at HTTP::Message::__ANON__(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:16)
 at HTTP::Message::_set_content(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:136)
 at HTTP::Message::content(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:125)
 at HTTP::Request::Common::POST(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm:91)
 at LWP::UserAgent::post(/usr/lib64/perl5/vendor_perl/5.8.8/LWP/UserAgent.pm:397)
 at ZLR::UA::post(./zlrchecker.pl:71)
 at ZLR::Info::setImage(./zlrchecker.pl:1754)
 at main::main(./zlrchecker.pl:3893)
 at main::(./zlrchecker.pl:4148)
4

2 回答 2

4

使用Devel::SimpleTrace并粘贴跟踪。使用 cpan 安装模块。-MDevel::SimpleTrace然后用like运行你的程序perl -MDevel::SimpleTrace ./myapp_run.pl

并粘贴HTTP::Request:CommonHTTP::Message和的版本LWP

我的猜测是你会在堆栈跟踪中看到这个:

这似乎是可能导致错误的代码

*_utf8_downgrade = defined(&utf8::downgrade) ?
    sub {
        utf8::downgrade($_[0], 1) or
            Carp::croak("HTTP::Message content must be bytes")
    }
    :
    sub {
    };

utf8中的文档是这样说的:

如果原始 UTF-X 序列无法以本机 8 位编码表示,则失败。失败时死亡,或者,如果 FAIL_OK 的值为 true,则返回 false。

您应该能够通过运行来制作测试用例utf8::downgrade($http_message_content)

于 2010-06-01T16:23:24.310 回答
0

unidecode()我发现这个问题的一个有效解决方案是在 HTTP 请求期间通过from解析我们放入 HTTP::Message 的任何文本Text::Unidecode

于 2014-10-20T17:40:06.650 回答