Merge branch 'MDL-77990-401' of https://github.com/snake/moodle into MOODLE_401_STABLE

This commit is contained in:
Huong Nguyen 2023-07-13 09:22:19 +07:00
commit 73cce7054c
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
2 changed files with 18 additions and 17 deletions

View File

@ -77,23 +77,11 @@ class http_client implements IHttpClient {
$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;
}

View File

@ -24,7 +24,7 @@ namespace enrol_lti\local\ltiadvantage\lib;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \enrol_lti\local\ltiadvantage\lib\http_client
*/
class http_client_test extends \basic_testcase {
class http_client_test extends \advanced_testcase {
/**
* Verify the http_client delegates to curl during a "GET" request.
@ -116,4 +116,17 @@ class http_client_test extends \basic_testcase {
'delete' => ['DELETE'],
];
}
/**
* Verify that the response headers are properly read from curl, and exclude things like redirect headers, or 100-continues.
* @covers ::request
*/
public function test_header_parsing(): void {
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
$client = new http_client(new \curl());
$response = $client->request('POST', "$testurl?redir=1",
['headers' => ['Expect' => '100-continue'], 'body' => 'foo']);
$headers = $response->getHeaders();
$this->assertEquals('200 OK', reset($headers));
}
}