perl - 如何使用 mod_perl 发送自定义 http 状态代码

标签 perl mod-perl2

我写了一个 CGI 程序,我将带有 HTTP header 的状态错误发送到客户端。但是当我尝试使用 时mod_perl 它只响应 200 ok 状态。如何发送自定义状态代码?

这是我想响应自定义状态错误时的代码:

my $cgi      = new CGI;
print $cgi->header(-type=>"text/html", -charset=>'utf-8', -status=>599);

编辑 :
这是代码:
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI;
use SessionManagement;


my $cgi      = new CGI;
my $method = $cgi->param("method");

my $sessionManagement = new SessionManagement(cgi=>$cgi);
if($sessionManagement){

  if (defined($method)) {
    if($method eq "authentication") {
        loginMethod($cgi,$sessionManagement);
    } elsif ($method eq "someMethod"){
        someMethod($cgi);
    } else{
        print $cgi->header(-type=>"text/xml", -charset=>'utf-8');
        print "<html>method does not exist</html>";
    }

  } else {
      print $cgi->header(-type=>"text/html", -charset=>'utf-8' , -status=>599);
      print "<html>blah blah</html>";
  }

}else{
  print $cgi->header(-type=>"text/html", -charset=>'utf-8' , -status=>599);
  print "<html>blah blah</html>";
}

-------------------------------------

编辑 2

提供更多信息:
当我使用 curl -v 192.168.1.212/mymodperl/test.pl shell 中的命令。
这是回应:
* About to connect() to 192.168.1.212 port 80 (#0)
*   Trying 192.168.1.212... connected
* Connected to 192.168.1.212 (192.168.1.212) port 80 (#0)
> GET /mymodperl/test.pl HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.212
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 25 Nov 2017 11:04:18 GMT
< Server: Apache/2.2.15 (Red Hat)
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=ISO-8859-1
< 
<html>hi</html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
 root@localhost and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at 192.168.1.212 Port 80</address>
</body></html>
* Closing connection #0

最佳答案

来自 http://www.perlmonks.org/?node_id=826769 ,设置状态码的方法是

package My::Handler;

use strict;
use warnings 'all';
use Apache2::RequestRec;

sub handler : method {
  my ($class, $r) = @_;

  $r->status( 401 );
  return 401;
}

1;# return true:

编辑:澄清

来自 https://perl.apache.org/docs/2.0/user/handlers/intro.html

What are Handlers?

Apache distinguishes between numerous phases for which it provides hooks (because the C functions are called ap_hook_) where modules can plug various callbacks to extend and alter the default behavior of the webserver. mod_perl provides a Perl interface for most of the available hooks, so mod_perl modules writers can change the Apache behavior in Perl. These callbacks are usually referred to as handlers and therefore the configuration directives for the mod_perl handlers look like: PerlFooHandler, where Foo is one of the handler names. For example PerlResponseHandler configures the response callback.

A typical handler is simply a perl package with a handler subroutine. For example:


file:MyApache2/CurrentTime.pm
----------------------------
package MyApache2::CurrentTime;

use strict;
use warnings;

use Apache2::RequestRec ();
use Apache2::RequestIO ();

use Apache2::Const -compile => qw(OK);

sub handler {
  my $r = shift;

  $r->content_type('text/plain');
  $r->print("Now is: " . scalar(localtime) . "\n");

  return Apache2::Const::OK;
}
1;

This handler simply returns the current date and time as a response.

Since this is a response handler, we configure it as a such in httpd.conf:


PerlResponseHandler MyApache2::CurrentTime

Since the response handler should be configured for a specific location, let's write a complete configuration section:


PerlModule MyApache2::CurrentTime
<Location /time>
  SetHandler modperl
  PerlResponseHandler MyApache2::CurrentTime
</Location>

Now when a request is issued to http://localhost/time this response handler is executed and a response that includes the current time is returned to the client.

关于perl - 如何使用 mod_perl 发送自定义 http 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47450767/

相关文章:

perl - 是否有与 PHP 的 ignore_user_abort() 等效的 mod_perl2/Perl 5?

perl - 将 Perl 从 5.6 升级到 5.24

perl - 在 mod_perl 中基于 body 生成 Etag

浏览器缓存控制、动态内容

multithreading - 如何在 mod_perl 上使用线程/多线程

使用 LWP::UserAgent 通过代理的 Perl HTTPS

oracle - perl + DBD::Oracle + mod_perl + oracle LDAP 名称解析器 = 崩溃?

perl - 在 Net::SSLeay 中安装时出错

perl - 如何使用 Perl 在目录及其所有子目录中找到最新的 .pl 文件?

c++ - 在 64 位 CENTO 上重复安装 Perl