Mojo::UserAgent - 在解码前检查 Content-Encoding header

Mojo::UserAgent - Inspect the Content-Encoding header before decoding

我正在尝试使用 Mojo::UserAgent 验证应用程序的 gzip 压缩 (Content-Encoding)。

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

以下是我的最小示例

#!/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 日志确认负载正在被压缩。此外,此 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 来确认正确的 Content-Encoding 响应。

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

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

如果您想保持自动解压并在解压阶段到达之前检查 headers(这也会删除 Content-Encoding header),您可以在内容 body 事件。此事件在解析 header 之后但在处理 body 之前发出。

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 有助于了解发生了什么。