Skip to content

Commit

Permalink
MDL-77990 enrol_lti: fix http_client shim by using curl getResponse
Browse files Browse the repository at this point in the history
Fixes an error in the parsing of response headers containing multiple
HTTP responses in the raw response. Curl already handles this, so let
it do the work.
  • Loading branch information
snake committed May 5, 2023
1 parent 159a131 commit 75ba439
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions enrol/lti/classes/local/ltiadvantage/lib/http_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,11 @@ function(&$val, $key) {
$info = $this->curlclient->get_info();
if (!$this->curlclient->get_errno() && !$this->curlclient->error) {
// No errors, so format the response.
$headersize = $info['header_size'];
$resheaders = substr($res ?? '', 0, $headersize);
$resbody = substr($res ?? '', $headersize);
$headerlines = array_filter(explode("\r\n", $resheaders));
$parsedresponseheaders = [
'httpstatus' => array_shift($headerlines)
];
foreach ($headerlines as $headerline) {
$headerbits = explode(':', $headerline, 2);
if (count($headerbits) == 2) {
// Only parse headers having colon separation.
$parsedresponseheaders[$headerbits[0]] = $headerbits[1];
}
}
$response = new http_response(['headers' => $parsedresponseheaders, 'body' => $resbody], intval($info['http_code']));
$resbody = substr($res ?? '', $info['header_size']);
$headers = $this->curlclient->getResponse();
$response = new http_response(['headers' => $headers, 'body' => $resbody], intval($info['http_code']));
if ($response->getStatusCode() >= 400) {
throw new http_exception($response, "An HTTP error status was received: '{$response->getHeaders()['httpstatus']}'");
throw new http_exception($response, "An HTTP error status was received: '".reset($headers)."'");
}
return $response;
}
Expand Down

0 comments on commit 75ba439

Please sign in to comment.