Clean up default settings and proxy URL parsing logic

This commit is contained in:
Kijin Sung 2023-04-23 14:26:19 +09:00
parent 8db0b06b6a
commit 6a9902c3bb
2 changed files with 35 additions and 28 deletions

View file

@ -287,10 +287,7 @@ class FileHandler
// Convert backward-compatible parameters to a format accepted by the HTTP class.
$converted_headers = [];
$converted_cookies = [];
$converted_settings = [
'verify' => \RX_BASEDIR . 'common/vendor/composer/ca-bundle/res/cacert.pem',
'timeout' => $timeout,
];
$converted_settings = ['timeout' => $timeout];
// Add headers.
foreach ($headers as $key => $val)

View file

@ -112,14 +112,17 @@ class HTTP
*/
public static function request(string $url, string $method = 'GET', $data = null, array $headers = [], array $cookies = [], array $settings = null): \Psr\Http\Message\ResponseInterface
{
// Apply default settings.
if (!isset($settings['timeout']))
// Set headers.
if ($headers)
{
$settings['timeout'] = self::DEFAULT_TIMEOUT;
$settings['headers'] = $headers;
}
if (!isset($settings['http_errors']))
// Set cookies.
if ($cookies && !isset($settings['cookies']))
{
$settings['http_errors'] = false;
$jar = \GuzzleHttp\Cookie\CookieJar::fromArray($cookies, parse_url($url, \PHP_URL_HOST));
$settings['cookies'] = $jar;
}
// Set the body or POST data.
@ -151,32 +154,21 @@ class HTTP
}
}
// Set headers.
if ($headers)
{
$settings['headers'] = $headers;
}
// Set cookies.
if ($cookies && !isset($settings['cookies']))
{
$jar = \GuzzleHttp\Cookie\CookieJar::fromArray($cookies, parse_url($url, \PHP_URL_HOST));
$settings['cookies'] = $jar;
}
// Set the proxy.
if (!isset($settings['proxy']) && defined('__PROXY_SERVER__'))
{
$settings['proxy'] = constant('__PROXY_SERVER__');
$proxy = parse_url(constant('__PROXY_SERVER__'));
$proxy_scheme = preg_match('/^socks/', $proxy['scheme'] ?? '') ? ($proxy['scheme'] . '://') : 'http://';
$proxy_auth = (!empty($proxy['user']) && !empty($proxy['pass'])) ? ($proxy['user'] . ':' . $proxy['pass'] . '@') : '';
$settings['proxy'] = sprintf('%s%s%s:%s', $proxy_scheme, $proxy_auth, $proxy['host'], $proxy['port']);
}
// Create a new Guzzle client, or reuse a cached one if available.
$guzzle = self::_createClient();
// Send the request.
if (!self::$_client)
{
self::$_client = new \GuzzleHttp\Client();
}
$start_time = microtime(true);
$response = self::$_client->request($method, $url, $settings);
$response = $guzzle->request($method, $url, $settings);
$status_code = $response->getStatusCode() ?: 0;
// Measure elapsed time and add a debug entry.
@ -186,6 +178,24 @@ class HTTP
return $response;
}
/**
* Create a Guzzle client with default options.
*
* @return \GuzzleHttp\Client
*/
protected static function _createClient(): \GuzzleHttp\Client
{
if (!self::$_client)
{
self::$_client = new \GuzzleHttp\Client([
'http_errors' => false,
'timeout' => self::DEFAULT_TIMEOUT,
'verify' => \RX_BASEDIR . 'common/vendor/composer/ca-bundle/res/cacert.pem',
]);
}
return self::$_client;
}
/**
* Record a request with the Debug class.
*