Skip to content

fix(http): 本文の無い 204 / 304 に Content-Type を付けない - #141

Merged
polidog merged 1 commit into
mainfrom
fix/135-no-content-type-on-204
Nov 3, 2026
Merged

polidog merged 1 commit into
mainfrom
fix/135-no-content-type-on-204

Conversation

@polidog

@polidog polidog commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Closes #135

Summary

本文の無い 204 / 304 に、PHP の既定の Content-type: text/html; charset=UTF-8 が付いていたのを止めます。

  • header_remove('Content-Type') だけでは default_mimetype は止まりません(php -S で確認済み)。空の Content-Type: を一度置いてから外すと止まるので、この処理を Http\BodilessHeaders(@internal)にまとめました。
  • Response::send(): ステータスが 204 / 304 で、ハンドラが Content-Type を明示していないときに外します。route.php の自動 OPTIONS、ページの自動 OPTIONS、Response::noContent() がここを通ります。Content-Type を明示した場合はそのまま送ります。
  • CachePolicy::sendNotModified(): HTTP キャッシュの 304 は Response を経由しないので、ここでも外します(php -S では元から付いていませんでしたが、SAPI によっては付くかもしれないので念のため)。
  • Cors のプリフライト 204 も Response を経由しないので、同じく外します。
  • README / README.ja の noContent() の説明に、Content-Type を付けないことを書き足しました。

テスト

tests/Http/BodilessResponseHttpServerTest.php を追加しました。既定の Content-Type は SAPI がヘッダを送る時点で付くため、プロセス内のテストや Xdebug のヘッダ一覧では見えません。そこで、既存の *HttpServerTest と同じく本物の php -S に対して確かめています。

  • route / ページの自動 OPTIONS、DELETE → noContent()、If-None-Match による 304、CORS プリフライトに Content-Type が無いこと
  • 204 で Content-Type を明示した場合と、本文のある応答では Content-Type が残ること

修正前は、自動 OPTIONS(route)、noContent()、プリフライトの 3 件が落ちることを確認しています。phpunit / phpstan analyse / php-cs-fixer fix --dry-run はすべて通ります。

🤖 Generated with Claude Code

本文の無い 204 / 304 に PHP の既定の
`Content-type: text/html; charset=UTF-8` が付いていたのを止める。
`header_remove('Content-Type')` だけでは default_mimetype は止まらないため、
空の `Content-Type:` を一度置いてから外す(BodilessHeaders)。

- Response::send(): 204 / 304 で、ハンドラが Content-Type を明示していなければ外す
  (route.php の自動 OPTIONS、ページの自動 OPTIONS、noContent() を含む)
- CachePolicy::sendNotModified(): HTTP キャッシュの 304
- Cors のプリフライト 204

Closes #135

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@polidog
polidog merged commit a517f1e into main Nov 3, 2026
1 check passed
@polidog
polidog deleted the fix/135-no-content-type-on-204 branch November 3, 2026 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

本文の無い 204 応答に Content-type: text/html が付く

1 participant