【问题标题】:mod_perl error returning 202 Apache2::Const::HTTP_ACCEPTEDmod_perl 错误返回 202 Apache2::Const::HTTP_ACCEPTED
【发布时间】:2015-01-17 11:56:29
【问题描述】:

我正在尝试使用 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 错误日志没有提到这个错误。

【问题讨论】:

  • 你的 apache 错误日志对此事有什么看法?
  • aah 忘了说:错误日志什么也没说!我会编辑帖子。
  • 我觉得难以置信。
  • 你试过 $r->print('foo'); ?
  • 我试过了,和print没有区别

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


【解决方案1】:

使用 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 状态。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 仍然是错误。我不确定使用 content_type 设置内容类型是否真的会打印出来。
    • mod_perl 文档 (perl.apache.org/docs/1.0/guide/porting.html) 告诉我,我真的不需要设置它,因为它已根据要求处理了 appache。但是,$r->content_type('text/plain');: 仍然是错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2019-12-09
    • 2013-01-21
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多