gpt4 book ai didi

apache - 在 mod_perl 2 下关闭响应

转载 作者:行者123 更新时间:2023-12-03 22:43:05 24 4
gpt4 key购买 nike

我试图找出是否有办法在 mod_perl 2 下完成响应而不返回主处理程序。到目前为止,还没有在文档中找到一种方法。以下是我正在尝试实现的示例:

#!/usr/bin/perl
# This is some mod_perl handler
use strict;
use warnings;
use Apache2::Const ':common';

sub handler {
my $r = shift;
if ($r->method eq 'POST') {
# just to do something as example
do_post_response($r);
}
$r->content_type('text/plain');
print "Thank you, goodbye.";
return Apache2::Const::OK;
}

sub do_post_response {
my $r = shift;
unless (check_somthing()) {
# Suppose I find a situation that requires
# a different response than normal...
$r->content_type('text/plain');
print "We have a situation...";
$r->something_to_finish_the_request_immediatly(Apache2::Const::OK);
}
}

在常规 Perl 脚本中,作为独立运行或在 mod_cgi 下运行,我可以只使用 exit()使用新的响应,但在 mod_perl 下我需要在原始 handler 中返回一些东西子程序。这导致我跟踪整个调用链,其中所有调用都必须返回一些东西,直到我回到主 handler .

例如,而不是:
unless (check_something()) { ...

我需要做以下事情:
my $check = check_something();
return $check if $check;

而且我还必须在主处理程序中做类似的事情,这对于某些情况处理来说是非常难看的。

有没有办法在嵌套调用中关闭请求,就像我试图用我的例子说明的那样?

编辑:我发现我可以调用 goto LABEL并将该标签放在主 handler 中最后一次返回之前子程序。它有效,但仍然感觉像一个肮脏的黑客。我真的希望有更好的方法。

最佳答案

我认为你仍然可以调用 exit() 因为 mod_perl 覆盖了 exit 的作用:

exit

In the normal Perl code exit() is used to stop the program flow and exit the Perl interpreter. However under mod_perl we only want the stop the program flow without killing the Perl interpreter.

You should take no action if your code includes exit() calls and it's OK to continue using them. mod_perl worries to override the exit() function with its own version which stops the program flow, and performs all the necessary cleanups, but doesn't kill the server. This is done by overriding:

*CORE::GLOBAL::exit = \&ModPerl::Util::exit;



https://perl.apache.org/docs/2.0/user/coding/coding.html

关于apache - 在 mod_perl 2 下关闭响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215504/

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