如果我理解正确,PSGI应用程序将作为下一个:
>从浏览器获得请求
>请求通过顺序中的某些中间件“起泡”,因为它们是在构建器中定义的
>请求来到我的应用程序
>我的应用程序产生一些回应
>这通过一些中间件再次响应泡沫
>最后将响应发送到浏览器
当请求登陆我的$app时,我可以轻松地调试打印所有标题(例如cookie).
问题是:
如何通过许多中间件向我的应用程序发出请求,同时响应通过中间件再次出现时,如何调试打印标题的实际状态.
所以,有一个(简单的)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();
所以加一个赏金:);)
解决方法
中间件
package ShowMeTheHeaders; use parent "Plack::Middleware"; use Plack::Request; use Plack::Response require Text::Wrap; my $_call_back = sub { my $response = Plack::Response->new(@{+shift}); print "* Response Headers:\n",Text::Wrap::wrap("\t","\t",$response->headers->as_string); return; # Explicit return suggested by docs. }; sub call { my $self = shift; my $request = Plack::Request->new(shift); print "* Request Headers:\n",$request->headers->as_string); my $response = $self->app->($request); Plack::Util::response_cb($response,$_call_back); } 1;
您可以在没有客观化的情况下执行此操作(Plack::Request和Plack::Response),但是您必须处理标题字段的原始属性和键,而不是完全更愉快的> as_string.另见Plack::Middleware的“响应回调”部分.
演示psgi
use warnings; use strict; use Plack::Builder; my $app = sub { [ 200,[ "Content-Type" => "text/plain" ],[ "O HAI,PLAK!" ] ]; }; builder { enable "+ShowMeTheHeaders"; mount "/" => $app; };