From 6a9902c3bb71eda3f72a0e1f49dcd8da31f92a5d Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 23 Apr 2023 14:26:19 +0900 Subject: [PATCH] Clean up default settings and proxy URL parsing logic --- classes/file/FileHandler.class.php | 5 +-- common/framework/HTTP.php | 58 +++++++++++++++++------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/classes/file/FileHandler.class.php b/classes/file/FileHandler.class.php index 5dc39c338..205b92cf5 100644 --- a/classes/file/FileHandler.class.php +++ b/classes/file/FileHandler.class.php @@ -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) diff --git a/common/framework/HTTP.php b/common/framework/HTTP.php index ac5f7ae2b..0dd8f37a1 100644 --- a/common/framework/HTTP.php +++ b/common/framework/HTTP.php @@ -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. *