8

我正在尝试使用Mojo::UserAgent来验证应用程序的 gzip 压缩(内容编码)。

不幸的是,这个 UA 似乎默默地解码了内容并删除了 Content-Encoding 标头的后缀。

以下是我的最小示例

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 3;

use Mojo::UserAgent;     # Version 8.26

my $ua = Mojo::UserAgent->new();

# As documented: https://docs.mojolicious.org/Mojolicious/Guides/Cookbook#Decorating-follow-up-requests
$ua->once(
    start => sub {
        my ( $ua, $tx ) = @_;
        $tx->req->headers->header( 'Accept-Encoding' => 'gzip' );
    }
);

my $tx = $ua->get('https://www.mojolicious.org');

is( $tx->req->headers->header('Accept-Encoding'), 'gzip', qq{Request Accept-Encoding is "gzip"} );

ok( $tx->res->is_success, "Response is success" );

# The following assertion fails.
# My theory is that Mojo::UserAgent is silently decoding the content, and changing
# the Content-Encoding and Content-Length to reflect the new values.  However, how
# do we inspect what the original response headers were?
is( $tx->res->headers->header('Content-Encoding'), 'gzip', qq{Response Content-Encoding is "gzip"} );

结果

$ perl mojo_useragent_content_encoding.pl
1..3
ok 1 - Request Accept-Encoding is "gzip"
ok 2 - Response is success
not ok 3 - Response Content-Encoding is "gzip"
#   Failed test 'Response Content-Encoding is "gzip"'
#   at mojo_useragent_content_encoding.pl line 30.
#          got: undef
#     expected: 'gzip'
# Looks like you failed 1 test of 3.

通过分析 Apache 日志,我能够确认有效负载正在被 gzip 压缩。此外,此 curl 还确认此示例网站正在使用 gzip 编码来处理请求

$ curl -i -H "Accept-Encoding: gzip" https://www.mojolicious.org
HTTP/1.1 200 OK
Date: Mon, 18 Jan 2021 21:28:14 GMT
Content-Type: text/html;charset=UTF-8
...
Content-Encoding: gzip
...

我可以用来LWP::UserAgent确认响应的正确内容编码。

但是,在执行任何理论上的后期处理之前,我无法确定如何检查 Mojo::UserAgent 响应以查看真实的标头。

4

1 回答 1

8

您可以$ua->transactor->compressed(0);在代码或MOJO_GZIP=0环境中设置以绕过自动解压缩。

如果您想保持自动解压并在达到解压阶段之前检查标头(这也会删除 Content-Encoding 标头),您可以在 contentbody事件上注册一个回调。此事件在解析标头之后但在处理正文之前发出。

use strict ;
use warnings;
use 5.30.0;
use Test::More tests => 3;
use Data::Dumper;
use Mojo::UserAgent;     # Version 8.26

my $ua = Mojo::UserAgent->new();

# As documented: https://docs.mojolicious.org/Mojolicious/Guides/Cookbook#Decorating-follow-up-requests
$ua->once(
          start => sub {
              my ( $ua, $tx ) = @_;
              $tx->req->headers->header( 'Accept-Encoding' => 'gzip' );

              my $res = $tx->res;
              say 'register event listener';
              $res->content->on(body=>sub{test_res_encoding($tx)});
          }
      );
$ua->transactor->compressed(0);

my $tx = $ua->get('https://www.mojolicious.org');

is( $tx->req->headers->header('Accept-Encoding'), 'gzip', qq{Request Accept-Encoding is "gzip"} );

ok( $tx->res->is_success, "Response is success" );

#say Dumper $tx->res->headers;
# The following assertion fails.
# My theory is that Mojo::UserAgent is silently decoding the content, and changing
# the Content-Encoding and Content-Length to reflect the new values.  However, how
# do we inspect what the original response headers were?
sub test_res_encoding{
    my $tx = shift;
    is( $tx->res->headers->header('Content-Encoding'),
        'gzip',
        qq{Response Content-Encoding is "gzip"} );
}

在您的环境中设置MOJO_EVENTEMITTER_DEBUG=1有助于了解发生了什么。

于 2021-01-19T01:50:02.873 回答