From 4028d7d160158fcd342deb93c983b39bca984057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B5=E8=84=91=E6=98=9F=E4=BA=BA?= Date: Sat, 13 Jan 2024 22:11:38 +0800 Subject: [PATCH] fix: getHeader for Content-Type & Content-Length (#1703) * fix: Request::getHeader for Content-Type & Content-Length * fix: get content-type through native api --------- Co-authored-by: joyqi --- var/Typecho/Request.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/var/Typecho/Request.php b/var/Typecho/Request.php index 77303583..6c434f39 100644 --- a/var/Typecho/Request.php +++ b/var/Typecho/Request.php @@ -367,10 +367,7 @@ class Request */ public function getContentType(): ?string { - return $this->getServer( - 'CONTENT_TYPE', - $this->getServer('HTTP_CONTENT_TYPE') - ); + return $this->getHeader('Content-Type'); } /** @@ -420,8 +417,14 @@ class Request */ public function getHeader(string $key, ?string $default = null): ?string { - $key = 'HTTP_' . strtoupper(str_replace('-', '_', $key)); - return $this->getServer($key, $default); + $key = strtoupper(str_replace('-', '_', $key)); + + // Content-Type 和 Content-Length 这两个 header 还需要从不带 HTTP_ 的 key 尝试获取 + if (in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) { + $default = $this->getServer($key, $default); + } + + return $this->getServer('HTTP_' . $key, $default); } /**