如何拦截Perl中的HTTP请求?

时间:2012-11-26 19:48:00

标签: perl http httprequest

我正在开发一个Web应用程序,我想知道是否有一种方法或某种模块可以在显示给客户端之前拦截或处理任何HTTP请求。我无法修改httpd.conf文件,所以它不是一个选项。 我想通过拒绝访问或重定向到其他页面或通过修改发送到客户端的响应和其他一些东西来为我的Web应用添加一些安全性。 我也听说过Request Dispatching,也许它可以帮到我。 有谁知道如何实现这个目标?

2 个答案:

答案 0 :(得分:3)

也许您可以使用Plack::Handler::Apache2来启用PSGI支持。从那里,您可以通过PSGI Middleware模块修改请求和响应。

如果不知道如何在mod_perl环境中设置Perl,很难更具体。

答案 1 :(得分:2)

您可能需要查看HTTP::Proxyperl模块来编写网络代理......例如:

 # alternate initialisation
  my $proxy = HTTP::Proxy->new;
  $proxy->port( 3128 ); # the classical accessors are here!

  # this is a MainLoop-like method
  $proxy->start;
  my $d = HTTP::Daemon->new || die;
  while (my $c = $d->accept) {
      while (my $r = $c->get_request) {
          if ($r->method eq 'GET' and $r->uri->path eq "/xyzzy") {
              # remember, this is *not* recommended practice :-)
              $c->send_file_response("/home/hd1/.zshrc");
          }
          else {
              $c->send_error(RC_FORBIDDEN)
          }
      }
      $c->close;
      undef($c);
  }