13
13
use Joomla \Http \Exception \UnexpectedResponseException ;
14
14
use Joomla \Http \Http ;
15
15
use Joomla \Http \HttpFactory ;
16
+ use Joomla \Http \Response ;
16
17
use Joomla \Input \Input ;
17
18
use Joomla \Uri \Uri ;
18
19
@@ -65,8 +66,12 @@ class Client
65
66
*
66
67
* @since 1.0
67
68
*/
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
+ ) {
70
75
if (!\is_array ($ options ) && !($ options instanceof \ArrayAccess)) {
71
76
throw new \InvalidArgumentException (
72
77
'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
79
84
$ this ->application = $ application ;
80
85
}
81
86
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
+
82
109
/**
83
110
* Get the access token or redirect to the authentication URL.
84
111
*
@@ -112,7 +139,7 @@ public function authenticate()
112
139
);
113
140
}
114
141
115
- if (strpos ($ response-> headers [ ' Content-Type ' ], ' application/json ' ) !== false ) {
142
+ if (self :: isJsonResponse ($ response) ) {
116
143
$ token = array_merge (json_decode ($ response ->body , true ), ['created ' => time ()]);
117
144
} else {
118
145
parse_str ($ response ->body , $ token );
@@ -127,7 +154,10 @@ public function authenticate()
127
154
if ($ this ->getOption ('sendheaders ' )) {
128
155
if (!($ this ->application instanceof WebApplicationInterface)) {
129
156
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
+ )
131
161
);
132
162
}
133
163
@@ -380,7 +410,7 @@ public function refreshToken($token = null)
380
410
);
381
411
}
382
412
383
- if (strpos ($ response-> headers [ ' Content-Type ' ], ' application/json ' ) !== false ) {
413
+ if (self :: isJsonResponse ($ response) ) {
384
414
$ token = array_merge (json_decode ($ response ->body , true ), ['created ' => time ()]);
385
415
} else {
386
416
parse_str ($ response ->body , $ token );
0 commit comments