Perl HTTP/1.1 206 部分内容

Perl HTTP/1.1 206 Partial Content

如何将服务器的 HTTP 响应设置为:

HTTP/1.1 206 Partial Content

在 PHP 中我会写 header('HTTP/1.1 206 Partial Content') 但在 Perl CGI 中呢?

我正在使用此代码,但客户端报告 HTTP 500 错误

if ( defined $ENV{HTTP_RANGE} ) {

    my $startrange = 500;
    my $endrange = 900;
    $length = $endrange-$startrange;

    print qq{HTTP/1.1 206 Partial Content\n};
    my $range = $startrange.'-'.$endrange.'/'.$filesize;
    print qq{Content-Range: bytes $range\n};
}

print qq{Accept-Ranges: bytes\n};
print qq{Content-Disposition: attachment; filename="$filename"\n};
print qq{Content-length: $length\n\n};

我查看了错误日志,上面写着:

malformed header from script 'download.cgi': Bad header: HTTP/1.1 206 Partial Content

你不应该发送 HTTP status line for Partial Content response in a CGI script. Your script should send a correct CGI response:

1*header-field NL [ response-body ]

这应该有效:

Status: 206 Partial Content\n
Content-Range: bytes 500-900/12000\n
Content-Type: ?\n

您的脚本还必须发送 Content-Type header:

If an entity body is returned, the script MUST supply a Content-Type field in the response.