简体   繁体   English

在mod_perl 2下关闭响应

[英]Close response under mod_perl 2

I'm trying to find out if there's a way to complete a response under mod_perl 2 without returning to the main handler. 我试图找出是否有一种方法可以在mod_perl 2下完成响应而不返回主处理程序。 Haven't been able to find a method for that in the docs so far. 到目前为止,尚无法在文档中找到该方法。 The following is an example of what I'm trying to achieve: 以下是我要实现的示例:

#!/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);
    }
}

In a regular Perl script, running as stand alone or under mod_cgi, I could just exit() with the new response, but under mod_perl I need to return something in the original handler subroutine. 在常规的Perl脚本中,既可以独立运行,也可以在mod_cgi下运行,我可以通过新的响应exit() ,但是在mod_perl下,我需要在原始handler子例程中返回某些内容。 This is leading me to keep track of a whole chain of calls where all of them have to return something until I get back to the main handler . 这导致我跟踪整个调用链,在这些调用中所有调用都必须返回某些内容,直到我回到主handler为止。

For example, instead of: 例如,代替:

unless (check_something()) { ...

I need to do things like: 我需要做类似的事情:

my $check = check_something();
return $check if $check;

and I also have to do something similar in the main handler, which is quite ungly for some situation handlings. 而且我还必须在主处理程序中执行类似的操作,这对于某些情况下的处理来说是非常不适当的。

Is there a way to close the request when inside a nested call, just like what I tried to illustrate with my example? 是否有办法在嵌套调用中关闭请求,就像我在示例中试图说明的那样?

EDIT: I've found that I can call a goto LABEL and place that label just before the last return in the main handler subroutine. 编辑:我发现我可以调用goto LABEL并将该标签放置在主handler子例程中的最后一次返回之前。 It works, but still feels like a dirty hack. 它可以工作,但仍然感觉像是一个肮脏的hack。 I really hope there's a nicer way. 我真的希望有更好的方法。

I think you are still fine to call exit() because mod_perl overrides what exit does: 我认为您仍然可以调用exit(),因为mod_perl会覆盖退出功能:

exit 出口

In the normal Perl code exit() is used to stop the program flow and exit the Perl interpreter. 在正常的Perl代码中,exit()用于停止程序流并退出Perl解释器。 However under mod_perl we only want the stop the program flow without killing the Perl interpreter. 但是在mod_perl下,我们只希望停止程序流而不杀死Perl解释器。

You should take no action if your code includes exit() calls and it's OK to continue using them. 如果您的代码中包含exit()调用,并且可以继续使用它们,则不要采取任何措施。 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. mod_perl担心用自己的版本覆盖exit()函数,该函数将停止程序流,并执行所有必要的清除操作,但不会终止服务器。 This is done by overriding: 这是通过重写:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM