Php 内容长度标头未发送

问题描述 投票:0回答:1

几天来我的下载文件功能遇到了问题。

Content-Length 不再设置,我的 XMLHttpRequest 进度事件函数无法正确获取 lengthComputable。我的apache服务器是2.4.61

<?php
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $nomFichier);
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Length: " . filesize($cheminFichier));
function update_progress(e, elProgress) {
  if (e.lengthComputable) {
    var percentage = Math.round((e.loaded / e.total) * 100);
    
    if (elProgress) {
      var elProgress = jQuery(elProgress);
      elProgress.find('.progressBar').css('width', percentage + '%');
      elProgress.parent(".linkDetails").find('.progressPercent').html(percentage + '%');
    } else {
      jQuery(".progressBar").css('width', percentage + '%');
      jQuery(".progressPercent").html(percentage + '%');
    }
  } else {
    console.log("Unable to compute progress information since the total size is unknown");
  }
}

文件路径设置正确,文件大小返回正确的大小,但内容长度未设置。

但是我看到了一个新标题

Transfer-Encoding: chunked

在阅读了互联网上的一些帖子后,我发现这是 HTTP 规范的一部分,这是因为内容长度没有设置,但它是这样的。

实际上,我发现的唯一解决方法是添加自定义标题

Alternate-Content-Lenght
并使用它来更新进度条百分比值。

如何继续使用内容长度而不是要下载的文件块?

javascript php apache
1个回答
0
投票

与系统管理员交谈后,似乎是新版本的 Apache 导致了该问题。

这是更改日志的一部分:

SECURITY: CVE-2024-24795: Apache HTTP Server: HTTP Response
     Splitting in multiple modules (cve.mitre.org)
     HTTP Response splitting in multiple modules in Apache HTTP
     Server allows an attacker that can inject malicious response
     headers into backend applications to cause an HTTP
     desynchronization attack.

     After this change, CGI-like scripts cannot set Transfer-Encoding
     or Content-Length headers.  To restore the ability to set Content-Length
     header, set per-request environment variable 'ap_trust_cgilike_cl' to any
     non-empty value.

因此,您必须使用自定义标头来处理内容长度或使用 somme conf 之类的

SetEnvIf Request_URI "\.php$" ap_trust_cgilike_cl

© www.soinside.com 2019 - 2024. All rights reserved.