PHP 响应 HEAD 请求

标签 php http litespeed

我有一个 PHP 脚本,可以按字节范围提供 PDF 文件的各个部分。

如果收到 HTTP HEAD 请求,它应该发回 header (包括 PDF 文件大小)而不是实际文件内容。我试过这个:

header('HTTP/1.1 200 OK');
header('Content-Type: application/pdf');
header('Accept-Ranges: bytes');
header('Content-Length: '.filesize($Pathname));
die;

问题是某些东西(我假设网络服务器 == LiteSpeed)将 Content-Length header 替换为 Content-Length: 0 - 这违背了整个目的。

谁能建议我应该做什么?谢谢

最佳答案

来自 w3c 超文本传输​​协议(protocol) -- HTTP/1.1:

When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected.

和:

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

所以,我想,如果您向服务器发送真正的 HEAD 请求,您的代码将正常工作。

关于PHP 响应 HEAD 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462493/

相关文章:

asp.net - .Net System.Net.HttpWebRequest 和身份验证 Cookie

c# - 系统参数异常 : The header has an empty value

apache - LiteSpeed 与 Apache httpd

PHP 使用外部磁盘驱动器下载文件

javascript - 使用 jquery 的 AJAX 的外部 scripts.js 在 HTMLInputElement.onkeyup 处返回 Uncaught ReferenceError

php - 如果不存在问题,则向表中的 INT 添加一个

javascript - 我可以在 javascript 中打开 php 标签,在 php 标签中打开 javascript 标签吗

c - 套接字事务未通过

php - 为什么我不写入我的数据库