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

标签 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;

我得到了错误

在本地主机上的浏览器中调用处理程序,我得到了输出,但也有一个错误:

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 错误日志没有提到这个错误。

最佳答案

使用 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

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::DECLINED is another return value that indicates success, but it's only relevant for phases of type 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 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. 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.

关于apache - mod_perl 错误返回 202 Apache2::Const::HTTP_ACCEPTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27025761/

相关文章:

regex - 如何在任何行的开头(包括第一行)与 Perl 正则表达式匹配?

java - 如何实现可以简单地用 Java 部署的 Web 服务(如 Jenkins)

java - Web 服务定义 (wsdl) 中的抽象类型

php - 新的 Laravel 项目抛出 500 错误 : require(): Failed opening required autoload. php

apache - TLS1.3 session 的 SSLSessionID 等效项是什么

javascript - 根据 IP 地址隐藏或显示 div 元素

angularjs - 单页应用程序的 vhosts conf

perl - 使用 Perl 从 Cisco Call Manager 获取 IP 地址列表

windows - 为什么 perl testing dir -d ""在 Windows 上返回 true?错误与否?

java - Spring WS 和 Validator 拦截器