Fixed proxy of guzzle client does not work expected.

This commit is contained in:
李铭昕 2019-07-11 16:35:14 +08:00
parent 9fca0bfb00
commit 0e1f18e78f
2 changed files with 31 additions and 0 deletions

View File

@ -15,6 +15,7 @@ namespace Hyperf\Guzzle;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client;
@ -129,6 +130,18 @@ class CoroutineHandler
$settings['timeout'] = $options['timeout'];
}
// Proxy
if (isset($options['proxy'])) {
$uri = new Uri($options['proxy']);
$settings['http_proxy_host'] = $uri->getHost();
$settings['http_proxy_port'] = $uri->getPort();
if ($uri->getUserInfo()) {
[$user, $password] = explode(':', $uri->getUserInfo());
$settings['http_proxy_user'] = $user;
$settings['http_proxy_password'] = $password;
}
}
return $settings;
}

View File

@ -108,6 +108,24 @@ class CoroutineHandlerTest extends TestCase
$this->assertSame(md5('1234'), $res['headers']['X-TOKEN']);
}
public function testProxy()
{
$client = new Client([
'base_uri' => 'http://127.0.0.1:8080',
'handler' => HandlerStack::create(new CoroutineHandlerStub()),
'proxy' => 'http://user:pass@127.0.0.1:8081',
]);
$json = json_decode($client->get('/')->getBody()->getContents(), true);
$setting = $json['setting'];
$this->assertSame('127.0.0.1', $setting['http_proxy_host']);
$this->assertSame(8081, $setting['http_proxy_port']);
$this->assertSame('user', $setting['http_proxy_user']);
$this->assertSame('pass', $setting['http_proxy_password']);
}
public function testUserInfo()
{
$url = 'https://username:password@api.tb.swoft.lmx0536.cn';