Skip to content

Commit

Permalink
Support force_ip_resolve in StreamHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 committed Nov 15, 2016
1 parent f0339b9 commit f2337fe
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

/**
* HTTP handler that uses PHP's HTTP stream wrapper.
Expand Down Expand Up @@ -277,6 +278,7 @@ private function createStream(RequestInterface $request, array $options)

$params = [];
$context = $this->getDefaultContext($request, $options);
$uri = $request->getUri()->withFragment('');

if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
throw new \InvalidArgumentException('on_headers must be callable');
Expand All @@ -286,7 +288,7 @@ private function createStream(RequestInterface $request, array $options)
foreach ($options as $key => $value) {
$method = "add_{$key}";
if (isset($methods[$method])) {
$this->{$method}($request, $context, $value, $params);
$this->{$method}($request, $context, $value, $params, $uri);
}
}
}
Expand All @@ -308,8 +310,8 @@ function () use ($context, $params) {
);

return $this->createResource(
function () use ($request, &$http_response_header, $context) {
$resource = fopen((string) $request->getUri()->withFragment(''), 'r', null, $context);
function () use ($uri, &$http_response_header, $context) {
$resource = fopen((string) $uri, 'r', null, $context);
$this->lastHeaders = $http_response_header;
return $resource;
}
Expand Down Expand Up @@ -350,7 +352,7 @@ private function getDefaultContext(RequestInterface $request)
return $context;
}

private function add_proxy(RequestInterface $request, &$options, $value, &$params)
private function add_proxy(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if (!is_array($value)) {
$options['http']['proxy'] = $value;
Expand All @@ -369,14 +371,14 @@ private function add_proxy(RequestInterface $request, &$options, $value, &$param
}
}

private function add_timeout(RequestInterface $request, &$options, $value, &$params)
private function add_timeout(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if ($value > 0) {
$options['http']['timeout'] = $value;
}
}

private function add_verify(RequestInterface $request, &$options, $value, &$params)
private function add_verify(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if ($value === true) {
// PHP 5.6 or greater will find the system cert by default. When
Expand All @@ -402,7 +404,7 @@ private function add_verify(RequestInterface $request, &$options, $value, &$para
$options['ssl']['allow_self_signed'] = false;
}

private function add_cert(RequestInterface $request, &$options, $value, &$params)
private function add_cert(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if (is_array($value)) {
$options['ssl']['passphrase'] = $value[1];
Expand All @@ -416,7 +418,7 @@ private function add_cert(RequestInterface $request, &$options, $value, &$params
$options['ssl']['local_cert'] = $value;
}

private function add_progress(RequestInterface $request, &$options, $value, &$params)
private function add_progress(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
$this->addNotification(
$params,
Expand All @@ -428,7 +430,24 @@ function ($code, $a, $b, $c, $transferred, $total) use ($value) {
);
}

private function add_debug(RequestInterface $request, &$options, $value, &$params)
private function add_force_ip_resolve(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if ('v4' === $value) {
$records = dns_get_record($uri->getHost(), DNS_A);
if (!isset($records[0]['ip'])) {
throw new ConnectException("Could not resolve IPv4 address for host '{$uri->getHost()}'", $request);
}
$uri = $uri->withHost($records[0]['ip']);
} else if ('v6' === $value) {
$records = dns_get_record($uri->getHost(), DNS_AAAA);
if (!isset($records[0]['ipv6'])) {
throw new ConnectException("Could not resolve IPv6 address for host '{$uri->getHost()}'", $request);
}
$uri = $uri->withHost('[' . $records[0]['ipv6'] . ']');
}
}

private function add_debug(RequestInterface $request, &$options, $value, &$params, UriInterface &$uri)
{
if ($value === false) {
return;
Expand Down

0 comments on commit f2337fe

Please sign in to comment.