mirror of
https://github.com/vichan-devel/vichan.git
synced 2024-12-18 18:36:00 +01:00
http-driver.php: minor refactor
This commit is contained in:
parent
cae85a6a0c
commit
3eed312b6b
@ -4,55 +4,29 @@ namespace Vichan\Data\Driver;
|
|||||||
defined('TINYBOARD') or exit;
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
|
|
||||||
class HttpDrivers {
|
|
||||||
private const DEFAULT_USER_AGENT = 'Tinyboard';
|
|
||||||
|
|
||||||
|
|
||||||
public static function getHttpDriver(int $timeout, int $max_file_size): HttpDriver {
|
|
||||||
return new HttpDriver($timeout, self::DEFAULT_USER_AGENT, $max_file_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class HttpDriver {
|
class HttpDriver {
|
||||||
private mixed $inner;
|
private mixed $inner;
|
||||||
private int $timeout;
|
private int $timeout;
|
||||||
private string $user_agent;
|
|
||||||
private int $max_file_size;
|
private int $max_file_size;
|
||||||
|
|
||||||
|
|
||||||
private function resetTowards(string $url, int $timeout): void {
|
private function resetTowards(string $url, int $timeout): void {
|
||||||
curl_reset($this->inner);
|
\curl_reset($this->inner);
|
||||||
curl_setopt_array($this->inner, array(
|
\curl_setopt_array($this->inner, [
|
||||||
\CURLOPT_URL => $url,
|
\CURLOPT_URL => $url,
|
||||||
\CURLOPT_TIMEOUT => $timeout,
|
\CURLOPT_TIMEOUT => $timeout,
|
||||||
\CURLOPT_USERAGENT => $this->user_agent,
|
\CURLOPT_USERAGENT => 'Tinyboard',
|
||||||
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
|
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
|
||||||
));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setSizeLimit(): void {
|
public function __construct(int $timeout, int $max_file_size) {
|
||||||
// Adapted from: https://stackoverflow.com/a/17642638
|
|
||||||
\curl_setopt($this->inner, \CURLOPT_NOPROGRESS, false);
|
|
||||||
|
|
||||||
if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) {
|
|
||||||
\curl_setopt($this->inner, \CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) {
|
|
||||||
return (int)($dl <= $this->max_file_size);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
\curl_setopt($this->inner, \CURLOPT_PROGRESSFUNCTION, function($res, $next_dl, $dl, $next_up, $up) {
|
|
||||||
return (int)($dl <= $this->max_file_size);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function __construct(int $timeout, string $user_agent, int $max_file_size) {
|
|
||||||
$this->inner = \curl_init();
|
$this->inner = \curl_init();
|
||||||
$this->timeout = $timeout;
|
$this->timeout = $timeout;
|
||||||
$this->user_agent = $user_agent;
|
|
||||||
$this->max_file_size = $max_file_size;
|
$this->max_file_size = $max_file_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __destruct() {
|
public function __destruct() {
|
||||||
\curl_close($this->inner);
|
\curl_close($this->inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,11 +104,16 @@ class HttpDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->resetTowards($endpoint, $timeout);
|
$this->resetTowards($endpoint, $timeout);
|
||||||
\curl_setopt($this->inner, \CURLOPT_FAILONERROR, true);
|
// Adapted from: https://stackoverflow.com/a/17642638
|
||||||
\curl_setopt($this->inner, \CURLOPT_FOLLOWLOCATION, false);
|
$opt = (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) ? \CURLOPT_XFERINFOFUNCTION : \CURLOPT_PROGRESSFUNCTION;
|
||||||
\curl_setopt($this->inner, \CURLOPT_FILE, $fd);
|
\curl_setopt_array($this->inner, [
|
||||||
\curl_setopt($this->inner, \CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
\CURLOPT_NOPROGRESS => false,
|
||||||
$this->setSizeLimit();
|
$opt => fn($res, $next_dl, $dl, $next_up, $up) => (int)($dl <= $this->max_file_size),
|
||||||
|
\CURLOPT_FAILONERROR => true,
|
||||||
|
\CURLOPT_FOLLOWLOCATION => false,
|
||||||
|
\CURLOPT_FILE => $fd,
|
||||||
|
\CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||||
|
]);
|
||||||
$ret = \curl_exec($this->inner);
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
|
Loading…
Reference in New Issue
Block a user