From 91ae56484d355a6255352844e75b541f0df6d2b3 Mon Sep 17 00:00:00 2001 From: joyqi Date: Fri, 22 Sep 2023 10:50:39 +0800 Subject: [PATCH] fix the source to get content type (#1622) --- var/Typecho/Request.php | 18 +++++++++++++++++- var/Typecho/Widget/Request.php | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/var/Typecho/Request.php b/var/Typecho/Request.php index c42c10a5..ac0c1496 100644 --- a/var/Typecho/Request.php +++ b/var/Typecho/Request.php @@ -351,6 +351,19 @@ class Request return ($this->pathInfo = '/' . ltrim(urldecode($pathInfo), '/')); } + /** + * 获取请求的内容类型 + * + * @return string|null + */ + public function getContentType(): ?string + { + return $this->getServer( + 'CONTENT_TYPE', + $this->getServer('HTTP_CONTENT_TYPE') + ); + } + /** * 获取环境变量 * @@ -492,7 +505,10 @@ class Request */ public function isJson(): bool { - return !!preg_match("/^\s*application\/json(;|$)/i", $this->getHeader('Content-Type', '')); + return !!preg_match( + "/^\s*application\/json(;|$)/i", + $this->getContentType() ?? '' + ); } /** diff --git a/var/Typecho/Widget/Request.php b/var/Typecho/Widget/Request.php index 4c1999c6..84da3830 100644 --- a/var/Typecho/Widget/Request.php +++ b/var/Typecho/Widget/Request.php @@ -213,6 +213,16 @@ class Request return $this->request->makeUriByRequest($parameter); } + /** + * 获取请求的内容类型 + * + * @return string|null + */ + public function getContentType(): ?string + { + return $this->request->getContentType(); + } + /** * 获取环境变量 * @@ -317,6 +327,16 @@ class Request return $this->request->isAjax(); } + /** + * 判断是否为json + * + * @return boolean + */ + public function isJson(): bool + { + return $this->request->isJson(); + } + /** * 判断输入是否满足要求 *