【问题标题】:Mojo::UserAgent - Inspect the Content-Encoding header before decodingMojo::UserAgent - 在解码前检查 Content-Encoding 标头
【发布时间】:2021-04-23 05:42:25
【问题描述】:

我正在尝试使用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 响应以查看真实的标头。

【问题讨论】:

    标签: perl mojolicious mojo-useragent


    【解决方案1】:

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

    如果您想保持自动解压并在达到解压阶段之前检查标头(这也会删除 Content-Encoding 标头),您可以在内容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 有助于查看发生了什么。

    【讨论】:

      猜你喜欢
      • 2018-11-30
      • 1970-01-01
      • 2017-10-19
      • 2011-11-09
      • 2018-08-02
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多