媒体文件响应重复发送 Content-Length 与 Accept-Ranges 头 #3

Closed
opened 2026-11-23 17:33:55 +08:00 by DeepSeek · 0 comments
Collaborator

现象

/assets/uploads/<name> 的 200 与 206 响应中,Content-Length 和 Accept-Ranges 各出现两次。

复现步骤

curl -s -D - -o /dev/null http://127.0.0.1:8787/assets/uploads/<name>.svg

响应头(节选,未删改):

HTTP/1.1 200 OK
Content-Type: image/svg+xml
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'none'; sandbox
Cache-Control: public, max-age=31536000, immutable
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Accept-Ranges: bytes
Content-Length: 111
Content-Length: 111
Connection: keep-alive
Last-Modified: Mon, 23 Nov 2026 09:21:50 GMT

Range 请求同样重复:

curl -s -D - -o /dev/null -H 'Range: bytes=0-3' http://127.0.0.1:8787/assets/uploads/<name>.svg
# Accept-Ranges ×2、Content-Length ×2、Content-Range ×1

期望

每个响应只发送一个 Content-Length 和一个 Accept-Ranges。

影响

  • RFC 9110 §8.6 要求发送方不得生成多个同名 Content-Length;HTTP/2(RFC 9113 §8.1)对重复 Content-Length 更严格,可能被判为 malformed。
  • 严格校验的网关/CDN/客户端库会报错或丢弃响应,视频、音频的 Range 播放会受影响。
  • 目前 Webman 直接对外时不会崩,所以问题会被掩盖,等到挂上反代才暴露。

原因定位

app/Controller/MediaController.php:22-43 手动设置了这两个头:

$headers = [
    ... 'Access-Control-Allow-Origin' => '*', 'Accept-Ranges' => 'bytes',
];
...
return (new Response(200, $headers + ['Content-Length' => (string) $length]))->withFile($path);

而 Workerman 在编码文件响应时会再次写入(vendor/workerman/workerman/src/Protocols/Http.php:405-417):

$response->withHeaders([
    'Content-Length' => $bodyLen,
    'Accept-Ranges' => 'bytes',
]);

withHeaders() 是追加语义,于是同名头重复。Content-Range 之所以只有一个,是因为框架随后用 header() 覆盖了。

修复建议

从 MediaController::file() 里删掉 Content-Length、Accept-Ranges(以及 Content-Range),交给 withFile($path, $offset, $count) 自行生成;Content-Type、CSP、CORS、Cache-Control 保留。

环境

  • 版本:main @ 01ede89(v1.1.0)
  • workerman/workerman v5.2.4、webman-framework v2.2.5
## 现象 `/assets/uploads/<name>` 的 200 与 206 响应中,`Content-Length` 和 `Accept-Ranges` 各出现两次。 ## 复现步骤 ```bash curl -s -D - -o /dev/null http://127.0.0.1:8787/assets/uploads/<name>.svg ``` 响应头(节选,未删改): ``` HTTP/1.1 200 OK Content-Type: image/svg+xml X-Content-Type-Options: nosniff Content-Security-Policy: default-src 'none'; sandbox Cache-Control: public, max-age=31536000, immutable Access-Control-Allow-Origin: * Accept-Ranges: bytes Accept-Ranges: bytes Content-Length: 111 Content-Length: 111 Connection: keep-alive Last-Modified: Mon, 23 Nov 2026 09:21:50 GMT ``` Range 请求同样重复: ```bash curl -s -D - -o /dev/null -H 'Range: bytes=0-3' http://127.0.0.1:8787/assets/uploads/<name>.svg # Accept-Ranges ×2、Content-Length ×2、Content-Range ×1 ``` ## 期望 每个响应只发送一个 `Content-Length` 和一个 `Accept-Ranges`。 ## 影响 - RFC 9110 §8.6 要求发送方不得生成多个同名 `Content-Length`;HTTP/2(RFC 9113 §8.1)对重复 `Content-Length` 更严格,可能被判为 malformed。 - 严格校验的网关/CDN/客户端库会报错或丢弃响应,视频、音频的 Range 播放会受影响。 - 目前 Webman 直接对外时不会崩,所以问题会被掩盖,等到挂上反代才暴露。 ## 原因定位 `app/Controller/MediaController.php:22-43` 手动设置了这两个头: ```php $headers = [ ... 'Access-Control-Allow-Origin' => '*', 'Accept-Ranges' => 'bytes', ]; ... return (new Response(200, $headers + ['Content-Length' => (string) $length]))->withFile($path); ``` 而 Workerman 在编码文件响应时会再次写入(`vendor/workerman/workerman/src/Protocols/Http.php:405-417`): ```php $response->withHeaders([ 'Content-Length' => $bodyLen, 'Accept-Ranges' => 'bytes', ]); ``` `withHeaders()` 是追加语义,于是同名头重复。`Content-Range` 之所以只有一个,是因为框架随后用 `header()` 覆盖了。 ## 修复建议 从 `MediaController::file()` 里删掉 `Content-Length`、`Accept-Ranges`(以及 `Content-Range`),交给 `withFile($path, $offset, $count)` 自行生成;`Content-Type`、CSP、CORS、Cache-Control 保留。 ## 环境 - 版本:`main` @ 01ede89(v1.1.0) - workerman/workerman v5.2.4、webman-framework v2.2.5
enoch closed this issue 2026-11-23 19:09:17 +08:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: laysense/laysite#3