简体   繁体   English

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

[英]mod_perl error returning 202 Apache2::Const::HTTP_ACCEPTED

I am trying to build a simple asynchronous web service with Apache and mod_perl. 我正在尝试使用Apache和mod_perl构建一个简单的异步Web服务。 But every time I try to return HTTP status 202 (Accepted), I get an error. 但是每次我尝试返回HTTP状态202(已接受)时,都会收到错误消息。

Below a very simple example (non-asynchronous): 下面是一个非常简单的示例(非异步):

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;

I get the error 我得到错误

calling the handler in my browser on localhost, I get the output but also an error: 在本地主机上的浏览器中调用处理程序时,我得到了输出,但也出现了错误:

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.

I also get an error with Apache2::Const::HTTP_OK , the only one that works without error is Apache2::Const::OK . 我也遇到了Apache2::Const::HTTP_OK的错误,唯一没有错误的方法是Apache2::Const::OK

My apache error log makes no mention of this error. 我的Apache错误日志未提及此错误。

With mod_perl2 you do not return HTTP status codes (this is why it is necessary to use $r->status() so set the HTTP status code. 使用mod_perl2时,您不会返回HTTP状态代码(这就是为什么必须使用$r->status()从而设置HTTP状态代码的原因。

Instead, you return a value depending on what you want the server to do. 取而代之的是,根据服务器要执行的操作返回一个值。 The most common would be Apache2::Const::OK . 最常见的是Apache2::Const::OK This tells the server your handler has finished successfully. 这告诉服务器您的处理程序已成功完成。 This constant, if I recall correctly, has an integer value of 0 , not 200 . 如果我没记错的话,此常量的整数值为0 ,而不是200

Returning an HTTP status code from a mod_perl handler will be interpreted as an error. 从mod_perl处理程序返回HTTP状态代码将被解释为错误。

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

Different handler groups are supposed to return different values. 不同的处理程序组应该返回不同的值。

Make sure that you always explicitly return a wanted value and don't rely on the result of last expression to be used as the return value -- things will change in the future and you won't know why things aren't working anymore. 确保始终明确地返回想要的值,并且不要依赖最后一个表达式的结果作为返回值-将来情况会发生变化,并且您将不知道为什么事情不再起作用。

The only value that can be returned by all handlers is Apache2::Const::OK, which tells Apache that the handler has successfully finished its execution. 所有处理程序可以返回的唯一值是Apache2 :: Const :: OK,它告诉Apache处理程序已成功完成其执行。

Apache2::Const::DECLINED is another return value that indicates success, but it's only relevant for phases of type RUN_FIRST. Apache2 :: Const :: DECLINED是另一个指示成功的返回值,但仅与RUN_FIRST类型的阶段有关。

HTTP handlers may also return Apache2::Const::DONE which tells Apache to stop the normal HTTP request cycle and fast forward to the PerlLogHandler, followed by PerlCleanupHandler. HTTP处理程序还可以返回Apache2 :: Const :: DONE,该命令告诉Apache停止正常的HTTP请求周期并快速转发到PerlLogHandler,然后是PerlCleanupHandler。 HTTP handlers may return any HTTP status, which similarly to Apache2::Const::DONE will cause an abort of the request cycle, by also will be interpreted as an error. HTTP处理程序可以返回任何HTTP状态,类似于Apache2 :: Const :: DONE会导致请求周期中止,也将被解释为错误。 Therefore you don't want to return Apache2::Const::HTTP_OK from your HTTP response handler, but Apache2::Const::OK and Apache will send the 200 OK status by itself. 因此,您不想从HTTP响应处理程序返回Apache2 :: Const :: HTTP_OK,但是Apache2 :: Const :: OK和Apache会自行发送200 OK状态。

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

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

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