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

Support CURLOPT_UNIX_SOCKET_PATH for SWOOLE_HOOK_CURL #121

Merged
merged 9 commits into from
Oct 9, 2021
1 change: 1 addition & 0 deletions src/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
!defined('CURLOPT_HEADEROPT') && define('CURLOPT_HEADEROPT', 229);
!defined('CURLOPT_PROXYHEADER') && define('CURLOPT_PROXYHEADER', 10228);
!defined('CURLOPT_RESOLVE') && define('CURLOPT_RESOLVE', 10203);
!defined('CURLOPT_UNIX_SOCKET_PATH') && define('CURLOPT_UNIX_SOCKET_PATH', 10231);
27 changes: 26 additions & 1 deletion src/core/Curl/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ final class Handler

private $resolve = [];

private $unix_socket_path = '';

public function __construct(string $url = '')
{
if ($url) {
Expand Down Expand Up @@ -228,6 +230,13 @@ private function create(?array $urlInfo = null): void
}
$this->urlInfo['host'] = $host = $this->resolve[$host][$port] ?? null ?: $host;
}
if ($this->unix_socket_path) {
$host = $this->unix_socket_path;
$port = 0;
if (stripos($host, 'unix:/') !== 0) {
deminy marked this conversation as resolved.
Show resolved Hide resolved
$host = "unix:/{$host}";
}
}
$this->client = new Client($host, $port, $urlInfo['scheme'] === 'https');
}

Expand All @@ -253,11 +262,19 @@ private function setUrl(string $url, bool $setInfo = true): bool
$this->setError(CURLE_URL_MALFORMAT, 'No URL set!');
return false;
}
if (strpos($url, '://') === false) {
if (strpos($url, '://') === false && $this->unix_socket_path === '') {
$url = 'http://' . $url;
}
if ($setInfo) {
$urlInfo = parse_url($url);
if ($this->unix_socket_path) {
if (empty($urlInfo['host']) && !empty($urlInfo['path'])) {
$urlInfo['host'] = explode('/', $urlInfo['path'])[1] ?? null;
}
if (!$this->hasHeader('Host') && !empty($urlInfo['host'])) {
$this->setHeader('Host', $urlInfo['host']);
}
}
if (!is_array($urlInfo)) {
$this->setError(CURLE_URL_MALFORMAT, "URL[{$url}] using bad/illegal format");
return false;
Expand Down Expand Up @@ -421,6 +438,9 @@ private function setOption(int $opt, $value): bool
case CURLOPT_PROXYAUTH:
/* ignored temporarily */
break;
case CURLOPT_UNIX_SOCKET_PATH:
$this->unix_socket_path = $value;
break;
case CURLOPT_NOBODY:
$this->nobody = boolval($value);
$this->method = 'HEAD';
Expand Down Expand Up @@ -841,6 +861,11 @@ private function execute()
$this->info['primary_ip'] = $this->urlInfo['host'];
}

if ($this->unix_socket_path) {
$this->info['primary_ip'] = realpath($this->unix_socket_path);
$this->info['primary_port'] = $this->urlInfo['port'];
}

$headerContent = '';
if ($client->headers) {
$cb = $this->headerFunction;
Expand Down