«HTTP::消息内容必须是字节»尝试发帖时出错

发布于 2024-09-03 22:20:43 字数 2406 浏览 4 评论 0原文

我有以下代码:

...
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 行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)

I have the following code:

...
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;
}
...

When I try to use function setImage it fails with error HTTP::Message content must be bytes at /usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm line 91. Worse that I can't reproduce this error without using all of my code and upgrading libwww-perl does nothing. What can cause it?

Versions of libww-perl: dev-perl/libwww-perl-5.836. HTTP::Request and HTTP::Request::Common came from libwww-perl package, versions: 5.827 and 5.824.

Trace:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-09-10 22:20:43

使用 Devel::SimpleTrace 并粘贴的痕迹。使用 cpan 安装模块。然后使用 -MDevel::SimpleTrace 运行程序,如 perl -MDevel::SimpleTrace ./myapp_run.pl

并粘贴 HTTP::Request:Common 的版本HTTP::MessageLWP

我的猜测是您会在堆栈跟踪中看到这一点:

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

*_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,返回
错误。

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

Use Devel::SimpleTrace and paste the trace. Install the module with cpan. Then run your program with -MDevel::SimpleTrace like perl -MDevel::SimpleTrace ./myapp_run.pl

And paste the version of HTTP::Request:Common, HTTP::Message, and LWP.

My guess is you'll see this in the stack trace:

This seems to be the code likely causing the error:

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

The docs in utf8 say this:

Fails if the original UTF-X sequence
cannot be represented in the native 8
bit encoding. On failure dies or, if
the value of FAIL_OK is true, returns
false.

You should be able to make a test case by running utf8::downgrade($http_message_content)

断爱 2024-09-10 22:20:43

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

One effective solution I found to this problem was to parse any text we put into HTTP::Message, during a HTTP request, through unidecode() from Text::Unidecode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文