mod_perl错误返回202 Apache2 :: Const :: HTTP_ACCEPTED

时间:2014-11-19 19:52:47

标签: apache perl web-services http mod-perl

我正在尝试使用Apache和mod_perl构建一个简单的异步Web服务。但每当我尝试返回HTTP状态202(已接受)时,我都会收到错误。

下面是一个非常简单的例子(非异步):

package MyHandler;

use Apache2::Const '-compile' => qw 'OK HTTP_ACCEPTED HTTP_OK';
use Apache2::RequestRec;
use CGI;

sub handler {
        my $r = shift;
        print "Hallo";
        $r->content_type('text/plain');
        $r->status(Apache2::Const::HTTP_ACCEPTED);
        return Apache2::Const::HTTP_ACCEPTED;
}

1;

我收到错误

在我的浏览器中调用localhost上的处理程序,我得到输出但也是错误:

Hallo
Accepted

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

Apache2::Const::HTTP_OK我也收到错误,唯一可以正常运行的错误是Apache2::Const::OK

我的apache错误日志没有提及此错误。

2 个答案:

答案 0 :(得分:2)

使用mod_perl2,您不会返回HTTP状态代码(这就是为什么必须使用$r->status()所以设置HTTP状态代码。

而是根据您希望服务器执行的操作返回值。最常见的是Apache2::Const::OK。这告诉服务器您的处理程序已成功完成。如果我没记错的话,这个常数的整数值为0,而不是200

从mod_perl处理程序返回HTTP状态代码将被解释为错误。

https://perl.apache.org/docs/2.0/user/handlers/intro.html#Handler_Return_Values

  

不同的处理程序组应该返回不同的值。

     

确保始终显式返回所需值,并且不依赖于最后一个表达式的结果作为返回值 - 将来会发生变化,你不会知道为什么不是工作了。

     

所有处理程序可以返回的唯一值是Apache2 :: Const :: OK,它告诉Apache处理程序已成功完成执行。

     

Apache2 :: Const :: DECLINED是另一个表示成功的返回值,但它仅与RUN_FIRST类型的阶段相关。

     

HTTP处理程序也可能返回Apache2 :: Const :: DONE,它告诉Apache停止正常的HTTP请求周期并快速转发到PerlLogHandler,然后是PerlCleanupHandler。 HTTP处理程序可能返回任何HTTP状态,类似于Apache2 :: Const :: DONE将导致请求周期的中止,也将被解释为错误。因此,您不希望从HTTP响应处理程序返回Apache2 :: Const :: HTTP_OK,但Apache2 :: Const :: OK和Apache将自行发送200 OK状态。

答案 1 :(得分:-1)

在设置内容类型标题之前,请尝试不要打印任何内容。