gpt4 book ai didi

perl - 窥探不同 plack 中间件之间的 http header

转载 作者:行者123 更新时间:2023-12-04 19:06:44 26 4
gpt4 key购买 nike

如果我理解正确,PSGI 应用程序的工作方式如下:

  • 收到来自浏览器的请求
  • 该请求是通过一些中间件按顺序“冒泡”的,因为它们在 builder 中定义
  • 请求来到我的应用程序
  • 我的应用产生了一些响应
  • 此响应再次通过一些中间件冒泡
  • 最后响应发送到浏览器

  • 当请求到达我的 $app 时,我可以轻松地调试打印所有 header (例如 cookie)。 .

    问题是:
    如何调试打印headers的实际状态当请求通过许多中间件到达我的应用程序时,而响应又通过中间件发出。

    所以,有一个(简化) app.psgi ,如下:
    use strict;
    use warnings;
    use Plack::Builder;

    my $app = sub { ... };

    builder {
    # <- debug-print the first request headers
    # and the last respond headers here
    enable "Debug";
    # <- debug-print the actual state of request/respond headers here
    enable "mid2";
    # <- and here
    enable "mid3";
    # <- and here

    $app; # <- and finally here - this is of course EASY
    }

    这可能不像这样的事情那么容易,
    print STDERR Dumper $dont_know_what->request->headers(); #HTTP::Headers ???
    print STDERR Dumper $dont_know_what->respond->headers();

    所以添加赏金:) ;)

    最佳答案

    一种基本方法是创建一个中间件,在执行包装的应用程序之前转储 header ,然后立即转储。然后,您在伪代码中指出的每个要查看 header 的位置启用此中间件。

    以下代码通过在每次启用它时构建一个内嵌中间件来完成此操作。

    use Plack::Builder;
    use Plack::Request;
    use Plack::Response;

    sub headers_around {
    my $position = shift;

    # build and return the headers_around middleware as a closure
    return sub {
    my $app = shift;
    # gets called each request
    return sub {
    my $env = shift;

    my $req = Plack::Request->new($env);
    # display headers before next middleware
    print STDERR "req headers before $position:\n" . $req->headers->as_string . "\n=====\n";

    # execute the next app on the stack
    my $res = $app->($env);

    my $response = Plack::Response->new(@$res);

    # display headers after previous middleware
    print STDERR "res headers after $position:\n" . $response->headers->as_string . "\n=====\n";
    return $res;
    };
    };
    };

    builder {

    enable headers_around('Debug');
    enable 'Debug';

    enable headers_around('Lint');
    enable 'Lint';

    enable headers_around('StackTrace');
    enable 'StackTrace', force => 1;

    enable headers_around('App');
    mount '/' => builder { sub {
    return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
    }}
    };

    # now build the application enabling regular middleware with our inline middleware
    builder {

    enable headers_around('Debug');
    enable 'Debug';

    enable headers_around('Lint');
    enable 'Lint';

    enable headers_around('StackTrace');
    enable 'StackTrace', force => 1;

    enable headers_around('App');
    mount '/' => builder { sub {
    return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
    }}
    };

    当我使用 plackup 运行它时,我得到以下输出:
    $ plackup --app between_middleware.psgi
    HTTP::Server::PSGI: Accepting connections at http://0:5000/
    req headers before Debug:
    Connection: Keep-Alive
    Accept: */*
    Host: 0:5000
    User-Agent: Wget/1.12 (linux-gnu)

    =====
    req headers before Lint:
    Connection: Keep-Alive
    Accept: */*
    Host: 0:5000
    User-Agent: Wget/1.12 (linux-gnu)

    =====
    req headers before StackTrace:
    Connection: Keep-Alive
    Accept: */*
    Host: 0:5000
    User-Agent: Wget/1.12 (linux-gnu)

    =====
    req headers before App:
    Connection: Keep-Alive
    Accept: */*
    Host: 0:5000
    User-Agent: Wget/1.12 (linux-gnu)

    =====
    res headers after App:
    Content-Type: text/plain

    =====
    res headers after StackTrace:
    Content-Type: text/plain

    =====
    res headers after Lint:
    Content-Type: text/plain

    =====
    res headers after Debug:
    Content-Type: text/plain

    =====
    127.0.0.1 - - [02/Apr/2014:19:37:30 -0700] "GET / HTTP/1.0" 200 11 "-" "Wget/1.12 (linux-gnu)"

    显然你可以把它变成一个像 Ashley 那样的真正的中间件,你可能需要调整它以使用你现有的任何工具发送日志消息。

    关于perl - 窥探不同 plack 中间件之间的 http header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22710580/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com