Skip to content

Commit 3f7444e

Browse files
author
JLTRY
committed
Fix #31 Content-Type header check is not case insensitive
use custom function to check if header has content-type json
1 parent 4e718f1 commit 3f7444e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/Client.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Joomla\Http\Exception\UnexpectedResponseException;
1414
use Joomla\Http\Http;
1515
use Joomla\Http\HttpFactory;
16+
use Joomla\Http\Response;
1617
use Joomla\Input\Input;
1718
use Joomla\Uri\Uri;
1819

@@ -65,8 +66,12 @@ class Client
6566
*
6667
* @since 1.0
6768
*/
68-
public function __construct($options = [], ?Http $http = null, ?Input $input = null, ?WebApplicationInterface $application = null)
69-
{
69+
public function __construct(
70+
$options = [],
71+
?Http $http = null,
72+
?Input $input = null,
73+
?WebApplicationInterface $application = null
74+
) {
7075
if (!\is_array($options) && !($options instanceof \ArrayAccess)) {
7176
throw new \InvalidArgumentException(
7277
'The options param must be an array or implement the ArrayAccess interface.'
@@ -79,6 +84,28 @@ public function __construct($options = [], ?Http $http = null, ?Input $input = n
7984
$this->application = $application;
8085
}
8186

87+
/**
88+
* Tests if given response contains JSON header
89+
*
90+
* @param Response $response The response object
91+
*
92+
* @return boolean
93+
*
94+
*/
95+
private static function isJsonResponse($response)
96+
{
97+
foreach (['Content-Type', 'content-type'] as $type) {
98+
if (array_key_exists($type, $response->headers)) {
99+
$content_type = is_array($response->headers[$type]) ?
100+
$response->headers[$type][0] : $response->headers[$type];
101+
if (strpos($content_type, 'application/json') !== false) {
102+
return true;
103+
}
104+
}
105+
}
106+
return false;
107+
}
108+
82109
/**
83110
* Get the access token or redirect to the authentication URL.
84111
*
@@ -112,7 +139,7 @@ public function authenticate()
112139
);
113140
}
114141

115-
if (strpos($response->headers['Content-Type'], 'application/json') !== false) {
142+
if (self::isJsonResponse($response)) {
116143
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
117144
} else {
118145
parse_str($response->body, $token);
@@ -127,7 +154,10 @@ public function authenticate()
127154
if ($this->getOption('sendheaders')) {
128155
if (!($this->application instanceof WebApplicationInterface)) {
129156
throw new \RuntimeException(
130-
\sprintf('A "%s" implementation is required to process authentication.', WebApplicationInterface::class)
157+
\sprintf(
158+
'A "%s" implementation is required to process authentication.',
159+
WebApplicationInterface::class
160+
)
131161
);
132162
}
133163

@@ -380,7 +410,7 @@ public function refreshToken($token = null)
380410
);
381411
}
382412

383-
if (strpos($response->headers['Content-Type'], 'application/json') !== false) {
413+
if (self::isJsonResponse($response)) {
384414
$token = array_merge(json_decode($response->body, true), ['created' => time()]);
385415
} else {
386416
parse_str($response->body, $token);

0 commit comments

Comments
 (0)