Skip to content

Commit

Permalink
Prevent lookup of undefined key in header array while parsing. Respon…
Browse files Browse the repository at this point in the history
…se parsing logic priority tweak.
  • Loading branch information
tcdent committed May 19, 2016
1 parent e1bf774 commit 5e9a2dd
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions restclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,26 @@ public function parse_response($response){
$this->response_status_lines = [];
$line = strtok($response, "\n");
do {
if(strpos($line, 'HTTP') === 0){
// One or more HTTP status lines
$this->response_status_lines[] = trim($line);
}
elseif(strlen(trim($line)) == 0){
if(strlen(trim($line)) == 0){
// Since we tokenize on \n, use the remaining \r to detect empty lines.
if(count($headers) > 0) break; // Must be the newline after headers, move on to response body
}
elseif(strpos($line, 'HTTP') === 0){
// One or more HTTP status lines
$this->response_status_lines[] = trim($line);
}
else {
// Has to be a header
list($key, $value) = explode(':', $line, 2);
$key = trim(strtolower(str_replace('-', '_', $key)));
$value = trim($value);

if(is_array($headers[$key]))
if(empty($headers[$key]))
$headers[$key] = $value;
elseif(is_array($headers[$key]))
$headers[$key][] = $value;
elseif(!empty($headers[$key]))
$headers[$key] = [$headers[$key], $value];
else
$headers[$key] = $value;
$headers[$key] = [$headers[$key], $value];
}
} while($line = strtok("\n"));

Expand Down

0 comments on commit 5e9a2dd

Please sign in to comment.