我正在尝试根据 IF 条件发送 http 404 状态代码。但是,在客户端我看到 http 500 错误。在我的 apach2 错误日志中,我看到了格式错误的标头。多次查看我的代码后,我无法弄清楚出了什么问题!谁能建议我如何向客户发送 404 消息?
下面是我的perl代码:
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;
use Carp;
use File::Copy qw( copy );
use File::Spec::Functions qw( catfile );
use POSIX qw(strftime);
use Time::Local;
use HTTP::Status qw(:constants :is status_message);
use Digest::MD5 qw(md5 md5_hex md5_base64);
use File::Basename;
use URI;
my $extfile = '/home/suresh/clientrequest.txt';
open(FH, ">>$extfile") or die "Cannot open file";
my $query = CGI->new;
my $stcode = status_message(200);
my $uri =$ENV{'REQUEST_URI'};
my $rdate =strftime("%a, %d %b %Y %H:%M:%S %Z", localtime());
print FH "Got Following Headers:\n";
print FH $ENV{'REQUEST_URI'}, "\n";
my $dir = '/home/suresh/Assets/';
my $nffFile = fileparse ("$uri", qr/\.[^.]*/);
my $fullFname = $nffFile . ".nff";
my $path = catfile($dir, $fullFname);
print FH "fullname:", $fullFname, "\n";
#Search requested asset files
opendir(DIR, $dir);
my @files = readdir(DIR);
if (grep($_=~/$fullFname/,@files)){
print FH "Found the file: ", $fullFname, "\n";
open my $fh, '<:raw', $path;
print "$ENV{SERVER_PROTOCOL} 200 $stcode";
print $query->header(
-'Date'=> $rdate,
-'Content-Type'=>'application/octet-stream',
-'Connection'=>'Keep-Alive',
-'attachment'=>$path,
);
binmode STDOUT, ':raw';
copy $fh => \*STDOUT;
close $fh
or die "Cannot close '$path': $!";
}else {
$stcode = status_message(404);
print "$ENV{'SERVER_PROTOCOL'} 404 $stcode\n";
print $query->header(
-'Server'=>$ENV{'SERVER_SOFTWARE'},
-'Content-type'=>'text/plain',
);
}
closedir(DIR);