IIS 形式的 Perl CGI POST returns 404

Perl CGI on IIS form POST returns 404

我正在为一个简单的 web 应用程序使用自制的小型 CGI 路由器,代码可以在 github

上找到

webapp 有一个这样的登录表单

my $form = start_form( -method => 'post', -action => '/login' );

    $form .= p( label('Mail'), textfield( -name => 'mail' ) );
    $form .= p( label('Mail'), password_field( -name => 'pass' ) );
    $form .= p( submit( -value => 'Log ind', -class => 'button button-primary' ) );

    $form .= end_form();

我有一个路由器处理这样的 post 请求

$router->add_route( 'POST', '/login', sub {

  if ( param ) {
    # Get mail and pass
    my $mail = $cgi->param( 'mail' );
    my $pass = $cgi->param( 'password' );

    # Check against user table
    # Set cookie and redirect to admin
  }

  # Otherwise redirect to /login
  print redirect( -url => '/login' );
});

在我的本地环境中,osx 10.10.3,perl 5,版本 18,subversion 2 (v5.18.2),这按预期工作,我提交表单并处理登录,没问题.

根据 ENV{'SERVER_SOFTWARE'}

,我的生产环境是 Microsoft-IIS/5.0

在生产环境中返回 404,不幸的是我无法获得任何可能揭示有用信息的日志文件。

.htaccess 文件在生产和本地环境中

# RewriteBase /
DirectoryIndex index.pl index.phtml index.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.pl [L,QSA]

问题很可能是环境不同,不仅要检查服务器软件,还要检查 perl 的版本和 CGI.pm 模块的版本。还值得注意的是,由于整个 NPH 问题,IIS 可能会导致 CGI.pm 出现问题 - 检查 CGI.pm 的 POD 以获取更多详细信息。

关于 CGIRouter:

Out of the box CGI qw/:standard/ offers many of the features you would expect from a web framework, it has methods for creating HTML markup like anchors, paragraphs and H tags. CGI may not be as sexy as some of the modern frameworks.

并不是说 CGI.pm 不是 "sexy",只是 not fit for purpose in any modern web app. Not to mention that the HTML markup functions are considered deprecated and shouldn't be used

adding a framework, any framework, would mean adding modules and dependencies which would then have to be kept up to date, as well as spending time on understanding the framework in question, it's cons and pros in order to pick the best one suited for the task at hand.

这也是一个none参数。 CGI.pm 已从 5.22 的 perl 核心中删除,因此您需要尽快安装它及其依赖项。如果您想使用简单易用且依赖性最小的框架编写简单的 RESTful Web 服务,请查看 Mojolicious::Lite。有关更多示例和其他替代方案,请参阅 CGI::Alternatives.

经过大量测试后,我意识到,问题不在路由器或 POST 没有被 IIS 处理。问题是重定向子例程。

而重定向( -url => '/admin' );适用于 OSX,不适用于 IIS。在 IIS 上,完整的 URL 必须像这样传递 redirect( -url => 'http://example.com/admin' ).