简体   繁体   English

如何在mod_perl中隐藏默认的Apache错误文档?

[英]How do I suppress the default apache error document in mod_perl?

I'm developing a RESTful API and I wrote a mod_perl2 handler that takes care of the request. 我正在开发RESTful API,并编写了一个处理请求的mod_perl2处理程序。

My handler deals with error codes by setting $r->status($http_code) and return $http_code; 我的处理程序通过设置$r->status($http_code)return $http_code;处理错误代码return $http_code;

Everything is fine, except a little problem: when my http_code is different than 200 (for instance 404), apache appends a default HTML error document to my own generated response. 一切都很好,除了一个小问题:当我的http_code不同于200(例如404)时,apache将默认的HTML错误文档附加到我自己生成的响应中。

For instance: 例如:

GET /foo

Gives: 给出:

$VAR1 = bless( {
                 'status' => 404,
                 'data' => {},
                 'message' => 'Resource not found for foo'
               }, 'My::Response' );
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /foo was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache/2.0.54 (Fedora) Server at localhost Port 80</address>
</body></html>

How do I get rid of this apache generated HTML? 如何摆脱这种由Apache生成的HTML?

UPDATE: My fault. 更新:我的错。 My mod_perl2 handler was returning a HTTP_* code instead of Apache2::Const::OK. 我的mod_perl2处理程序正在返回HTTP_ *代码,而不是Apache2 :: Const :: OK。

See Apache2::Response . 参见Apache2 :: Response I do not have time to experiment right now, but that should work. 我现在没有时间进行实验,但是应该可以。

Are you asking how to send no message body in your response? 您是否在询问如何在响应中不发送邮件正文?

If you want something other than what apache is going to do for you, you need to handle the request yourself. 如果您想要的不是apache要做的事情,则需要自己处理请求。 What does the rest of your handler look like? 您的其他处理程序看起来如何? Posting code keeps us from guessing what you are doing. 发布代码可避免我们猜测您在做什么。

The return value from your handler lets apache know if you handled the request yourself or if it needs to do something more on your behalf. 处理程序的返回值使apache知道您是自己处理请求还是需要代表您做更多的事情。 I'm guessing that you're doing the latter. 我猜你正在做后者。

I was looking for this too. 我也在寻找这个。 And the trick was quite simple: 诀窍很简单:

$r->status(HTTP_NOT_FOUND);
$r->custom_response(404, "");
return OK;

where $r is Apache2::Response object. 其中$ r是Apache2 :: Response对象。

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

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