Skip to content

Commit 5e9a2dd

Browse files
committed
Prevent lookup of undefined key in header array while parsing. Response parsing logic priority tweak.
1 parent e1bf774 commit 5e9a2dd

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

restclient.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,26 @@ public function parse_response($response){
209209
$this->response_status_lines = [];
210210
$line = strtok($response, "\n");
211211
do {
212-
if(strpos($line, 'HTTP') === 0){
213-
// One or more HTTP status lines
214-
$this->response_status_lines[] = trim($line);
215-
}
216-
elseif(strlen(trim($line)) == 0){
212+
if(strlen(trim($line)) == 0){
217213
// Since we tokenize on \n, use the remaining \r to detect empty lines.
218214
if(count($headers) > 0) break; // Must be the newline after headers, move on to response body
219215
}
216+
elseif(strpos($line, 'HTTP') === 0){
217+
// One or more HTTP status lines
218+
$this->response_status_lines[] = trim($line);
219+
}
220220
else {
221221
// Has to be a header
222222
list($key, $value) = explode(':', $line, 2);
223223
$key = trim(strtolower(str_replace('-', '_', $key)));
224224
$value = trim($value);
225225

226-
if(is_array($headers[$key]))
226+
if(empty($headers[$key]))
227+
$headers[$key] = $value;
228+
elseif(is_array($headers[$key]))
227229
$headers[$key][] = $value;
228-
elseif(!empty($headers[$key]))
229-
$headers[$key] = [$headers[$key], $value];
230230
else
231-
$headers[$key] = $value;
231+
$headers[$key] = [$headers[$key], $value];
232232
}
233233
} while($line = strtok("\n"));
234234

0 commit comments

Comments
 (0)