diff --git a/classes/check/rangerequestcheck.php b/classes/check/rangerequestcheck.php index 1e614f3..6aa685c 100644 --- a/classes/check/rangerequestcheck.php +++ b/classes/check/rangerequestcheck.php @@ -38,18 +38,24 @@ 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) { @@ -57,10 +63,8 @@ public function get_result() : result { } } - 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'], ])); diff --git a/lang/en/tool_heartbeat.php b/lang/en/tool_heartbeat.php index a783182..07e9a95 100644 --- a/lang/en/tool_heartbeat.php +++ b/lang/en/tool_heartbeat.php @@ -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.'; /*