Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert range requests to use curl #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions classes/check/rangerequestcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,33 @@
class rangerequestcheck extends check {

public function get_result() : result {
global $DB;
global $DB, $CFG;

$url = new \moodle_url('/pluginfile.php/1/tool_heartbeat/test');
$url = $CFG->wwwroot . '/pluginfile.php/1/tool_heartbeat/test';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RANGE, '0-9');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl = new \curl();
$curl->setopt([
'CURLOPT_RANGE' => '0-9',
]);

$response = curl_exec($curl);
$response = $curl->get($url);
$datalength = strlen($response);
$info = curl_getinfo($curl);
$info = $curl->get_info();

if (empty($info)) {
// Happens when the url is blocked.
return new result(result::ERROR, get_string('checkrangerequestfailed', 'tool_heartbeat',
['error' => $response]));
}

if ($response !== false) {
if ($info['http_code'] === 206 && $info['size_download'] == 10) {
return new result(result::OK, get_string('checkrangerequestok', 'tool_heartbeat'));
}
}

curl_close($curl);

return new result(result::ERROR, get_string('checkrangerequestbad', 'tool_heartbeat', [
'url' => $url->out(),
'url' => $url,
'code' => $info['http_code'],
'bytes' => $info['size_download'],
]));
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
$string['checkrangerequestcheck'] = 'Range requests check';
$string['checkrangerequestok'] = 'Range requests are working, 206 response with only 10 bytes of data';
$string['checkrangerequestbad'] = 'Range requests are bad! HTTP {$a->code} response with only {$a->bytes} bytes of data for {$a->url}';
$string['checkrangerequestfailed'] = 'Range requests are not working: {$a->error}';
$string['ips_combine'] = 'The IPs listed above will be combined with the IPs listed below.';

/*
Expand Down