Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;
@@ -20,6 +18,13 @@ use Zend\Stdlib\ArrayUtils;
*/
class Curl implements HttpAdapter, StreamInterface
{
/**
* Operation timeout.
*
* @var int
*/
const ERROR_OPERATION_TIMEDOUT = 28;
/**
* Parameters array
*
@@ -39,7 +44,7 @@ class Curl implements HttpAdapter, StreamInterface
*
* @var resource|null
*/
protected $curl = null;
protected $curl;
/**
* List of cURL options that should never be overwritten
@@ -53,7 +58,7 @@ class Curl implements HttpAdapter, StreamInterface
*
* @var string
*/
protected $response = null;
protected $response;
/**
* Stream for storing output
@@ -71,7 +76,7 @@ class Curl implements HttpAdapter, StreamInterface
*/
public function __construct()
{
if (!extension_loaded('curl')) {
if (! extension_loaded('curl')) {
throw new AdapterException\InitializationException(
'cURL extension has to be loaded to use this Zend\Http\Client adapter'
);
@@ -104,10 +109,11 @@ class Curl implements HttpAdapter, StreamInterface
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if (!is_array($options)) {
throw new AdapterException\InvalidArgumentException(
'Array or Traversable object expected, got ' . gettype($options)
);
if (! is_array($options)) {
throw new AdapterException\InvalidArgumentException(sprintf(
'Array or Traversable object expected, got %s',
gettype($options)
));
}
/** Config Key Normalization */
@@ -117,7 +123,7 @@ class Curl implements HttpAdapter, StreamInterface
}
if (isset($options['proxyuser']) && isset($options['proxypass'])) {
$this->setCurlOption(CURLOPT_PROXYUSERPWD, $options['proxyuser'] . ":" . $options['proxypass']);
$this->setCurlOption(CURLOPT_PROXYUSERPWD, $options['proxyuser'] . ':' . $options['proxypass']);
unset($options['proxyuser'], $options['proxypass']);
}
@@ -166,7 +172,7 @@ class Curl implements HttpAdapter, StreamInterface
*/
public function setCurlOption($option, $value)
{
if (!isset($this->config['curloptions'])) {
if (! isset($this->config['curloptions'])) {
$this->config['curloptions'] = [];
}
$this->config['curloptions'][$option] = $value;
@@ -195,13 +201,22 @@ class Curl implements HttpAdapter, StreamInterface
curl_setopt($this->curl, CURLOPT_PORT, intval($port));
}
if (isset($this->config['timeout'])) {
if (isset($this->config['connecttimeout'])) {
$connectTimeout = $this->config['connecttimeout'];
} elseif (isset($this->config['timeout'])) {
$connectTimeout = $this->config['timeout'];
} else {
$connectTimeout = null;
}
if ($connectTimeout !== null) {
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $this->config['timeout'] * 1000);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000);
} else {
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->config['timeout']);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
}
}
if (isset($this->config['timeout'])) {
if (defined('CURLOPT_TIMEOUT_MS')) {
curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, $this->config['timeout'] * 1000);
} else {
@@ -209,12 +224,19 @@ class Curl implements HttpAdapter, StreamInterface
}
}
if (isset($this->config['sslcafile']) && $this->config['sslcafile']) {
curl_setopt($this->curl, CURLOPT_CAINFO, $this->config['sslcafile']);
}
if (isset($this->config['sslcapath']) && $this->config['sslcapath']) {
curl_setopt($this->curl, CURLOPT_CAPATH, $this->config['sslcapath']);
}
if (isset($this->config['maxredirects'])) {
// Set Max redirects
curl_setopt($this->curl, CURLOPT_MAXREDIRS, $this->config['maxredirects']);
}
if (!$this->curl) {
if (! $this->curl) {
$this->close();
throw new AdapterException\RuntimeException('Unable to Connect to ' . $host . ':' . $port);
@@ -247,16 +269,17 @@ class Curl implements HttpAdapter, StreamInterface
* to wrong host, no PUT file defined, unsupported method, or unsupported
* cURL option.
* @throws AdapterException\InvalidArgumentException if $method is currently not supported
* @throws AdapterException\TimeoutException if connection timed out
*/
public function write($method, $uri, $httpVersion = 1.1, $headers = [], $body = '')
{
// Make sure we're properly connected
if (!$this->curl) {
throw new AdapterException\RuntimeException("Trying to write but we are not connected");
if (! $this->curl) {
throw new AdapterException\RuntimeException('Trying to write but we are not connected');
}
if ($this->connectedTo[0] != $uri->getHost() || $this->connectedTo[1] != $uri->getPort()) {
throw new AdapterException\RuntimeException("Trying to write but we are connected to the wrong host");
throw new AdapterException\RuntimeException('Trying to write but we are connected to the wrong host');
}
// set URL
@@ -282,8 +305,8 @@ class Curl implements HttpAdapter, StreamInterface
if (isset($this->config['curloptions'][CURLOPT_INFILE])) {
// Now we will probably already have Content-Length set, so that we have to delete it
// from $headers at this point:
if (!isset($headers['Content-Length'])
&& !isset($this->config['curloptions'][CURLOPT_INFILESIZE])
if (! isset($headers['Content-Length'])
&& ! isset($this->config['curloptions'][CURLOPT_INFILESIZE])
) {
throw new AdapterException\RuntimeException(
'Cannot set a file-handle for cURL option CURLOPT_INFILE'
@@ -303,46 +326,49 @@ class Curl implements HttpAdapter, StreamInterface
$curlMethod = CURLOPT_UPLOAD;
} else {
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "PUT";
$curlValue = 'PUT';
}
break;
case 'PATCH':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "PATCH";
$curlValue = 'PATCH';
break;
case 'DELETE':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "DELETE";
$curlValue = 'DELETE';
break;
case 'OPTIONS':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "OPTIONS";
$curlValue = 'OPTIONS';
break;
case 'TRACE':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "TRACE";
$curlValue = 'TRACE';
break;
case 'HEAD':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "HEAD";
$curlValue = 'HEAD';
break;
default:
// For now, through an exception for unsupported request methods
throw new AdapterException\InvalidArgumentException("Method '$method' currently not supported");
throw new AdapterException\InvalidArgumentException(sprintf(
'Method \'%s\' currently not supported',
$method
));
}
if (is_resource($body) && $curlMethod != CURLOPT_UPLOAD) {
throw new AdapterException\RuntimeException("Streaming requests are allowed only with PUT");
throw new AdapterException\RuntimeException('Streaming requests are allowed only with PUT');
}
// get http version to use
$curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
$curlHttp = $httpVersion == 1.1 ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
// mark as HTTP request and set HTTP method
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, $curlHttp);
@@ -354,7 +380,7 @@ class Curl implements HttpAdapter, StreamInterface
if ($this->outputStream) {
// headers will be read into the response
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, [$this, "readHeader"]);
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, [$this, 'readHeader']);
// and data will be written into the file
curl_setopt($this->curl, CURLOPT_FILE, $this->outputStream);
} else {
@@ -373,7 +399,7 @@ class Curl implements HttpAdapter, StreamInterface
}
// set additional headers
if (!isset($headers['Accept'])) {
if (! isset($headers['Accept'])) {
$headers['Accept'] = '';
}
$curlHeaders = [];
@@ -402,7 +428,7 @@ class Curl implements HttpAdapter, StreamInterface
// set additional curl options
if (isset($this->config['curloptions'])) {
foreach ((array) $this->config['curloptions'] as $k => $v) {
if (!in_array($k, $this->invalidOverwritableCurlOptions)) {
if (! in_array($k, $this->invalidOverwritableCurlOptions)) {
if (curl_setopt($this->curl, $k, $v) == false) {
throw new AdapterException\RuntimeException(sprintf(
'Unknown or erroreous cURL option "%s" set',
@@ -413,19 +439,30 @@ class Curl implements HttpAdapter, StreamInterface
}
}
$this->response = '';
// send the request
$response = curl_exec($this->curl);
// if we used streaming, headers are already there
if (!is_resource($this->outputStream)) {
if (! is_resource($this->outputStream)) {
$this->response = $response;
}
$request = curl_getinfo($this->curl, CURLINFO_HEADER_OUT);
$request .= $body;
if (empty($this->response)) {
throw new AdapterException\RuntimeException("Error in cURL request: " . curl_error($this->curl));
if ($response === false || empty($this->response)) {
if (curl_errno($this->curl) === static::ERROR_OPERATION_TIMEDOUT) {
throw new AdapterException\TimeoutException(
'Read timed out',
AdapterException\TimeoutException::READ_TIMEOUT
);
}
throw new AdapterException\RuntimeException(sprintf(
'Error in cURL request: %s',
curl_error($this->curl)
));
}
// separating header from body because it is dangerous to accidentially replace strings in the body
@@ -434,7 +471,7 @@ class Curl implements HttpAdapter, StreamInterface
// cURL automatically decodes chunked-messages, this means we have to
// disallow the Zend\Http\Response to do it again.
$responseHeaders = preg_replace("/Transfer-Encoding:\s*chunked\\r\\n/i", "", $responseHeaders);
$responseHeaders = preg_replace("/Transfer-Encoding:\s*chunked\\r\\n/i", '', $responseHeaders);
// cURL can automatically handle content encoding; prevent double-decoding from occurring
if (isset($this->config['curloptions'][CURLOPT_ENCODING])

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;

View File

@@ -1,16 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;
/**
*/
class InitializationException extends RuntimeException
{
}

View File

@@ -1,18 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;
use Zend\Http\Client\Exception;
/**
*/
class InvalidArgumentException extends Exception\InvalidArgumentException implements
ExceptionInterface
{

View File

@@ -1,18 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;
use Zend\Http\Client\Exception;
/**
*/
class OutOfRangeException extends Exception\OutOfRangeException implements
ExceptionInterface
{

View File

@@ -1,18 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;
use Zend\Http\Client\Exception;
/**
*/
class RuntimeException extends Exception\RuntimeException implements
ExceptionInterface
{

View File

@@ -1,16 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter\Exception;
/**
*/
class TimeoutException extends RuntimeException implements ExceptionInterface
{
const READ_TIMEOUT = 1000;

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;
@@ -31,19 +29,21 @@ class Proxy extends Socket
* @var array
*/
protected $config = [
'persistent' => false,
'ssltransport' => 'ssl',
'sslcert' => null,
'sslpassphrase' => null,
'sslverifypeer' => true,
'sslcafile' => null,
'sslcapath' => null,
'sslallowselfsigned' => false,
'sslusecontext' => false,
'sslverifypeername' => true,
'proxy_host' => '',
'proxy_port' => 8080,
'proxy_user' => '',
'proxy_pass' => '',
'proxy_auth' => Client::AUTH_BASIC,
'persistent' => false
];
/**
@@ -62,7 +62,7 @@ class Proxy extends Socket
{
//enforcing that the proxy keys are set in the form proxy_*
foreach ($options as $k => $v) {
if (preg_match("/^proxy[a-z]+/", $k)) {
if (preg_match('/^proxy[a-z]+/', $k)) {
$options['proxy_' . substr($k, 5, strlen($k))] = $v;
unset($options[$k]);
}
@@ -123,25 +123,29 @@ class Proxy extends Socket
// Make sure we're properly connected
if (! $this->socket) {
throw new AdapterException\RuntimeException("Trying to write but we are not connected");
throw new AdapterException\RuntimeException('Trying to write but we are not connected');
}
$host = $this->config['proxy_host'];
$port = $this->config['proxy_port'];
if ($this->connectedTo[0] != "tcp://$host" || $this->connectedTo[1] != $port) {
throw new AdapterException\RuntimeException("Trying to write but we are connected to the wrong proxy server");
if ($this->connectedTo[0] != sprintf('tcp://%s', $host) || $this->connectedTo[1] != $port) {
throw new AdapterException\RuntimeException(
'Trying to write but we are connected to the wrong proxy server'
);
}
// Add Proxy-Authorization header
if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
$headers['proxy-authorization'] = Client::encodeAuthHeader(
$this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
$this->config['proxy_user'],
$this->config['proxy_pass'],
$this->config['proxy_auth']
);
}
// if we are proxying HTTPS, preform CONNECT handshake with the proxy
if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
if ($uri->getScheme() == 'https' && ! $this->negotiated) {
$this->connectHandshake($uri->getHost(), $uri->getPort(), $httpVer, $headers);
$this->negotiated = true;
}
@@ -155,17 +159,17 @@ class Proxy extends Socket
if ($uri->getQuery()) {
$path .= '?' . $uri->getQuery();
}
$request = "$method $path HTTP/$httpVer\r\n";
$request = sprintf('%s %s HTTP/%s%s', $method, $path, $httpVer, "\r\n");
} else {
$request = "$method $uri HTTP/$httpVer\r\n";
$request = sprintf('%s %s HTTP/%s%s', $method, $uri, $httpVer, "\r\n");
}
// Add all headers to the request string
foreach ($headers as $k => $v) {
if (is_string($k)) {
$v = "$k: $v";
$v = $k . ': ' . $v;
}
$request .= "$v\r\n";
$request .= $v . "\r\n";
}
if (is_resource($body)) {
@@ -179,8 +183,8 @@ class Proxy extends Socket
ErrorHandler::start();
$test = fwrite($this->socket, $request);
$error = ErrorHandler::stop();
if (!$test) {
throw new AdapterException\RuntimeException("Error writing request to proxy server", 0, $error);
if (! $test) {
throw new AdapterException\RuntimeException('Error writing request to proxy server', 0, $error);
}
if (is_resource($body)) {
@@ -203,18 +207,18 @@ class Proxy extends Socket
*/
protected function connectHandshake($host, $port = 443, $httpVer = '1.1', array &$headers = [])
{
$request = "CONNECT $host:$port HTTP/$httpVer\r\n" .
"Host: " . $host . "\r\n";
$request = 'CONNECT ' . $host . ':' . $port . ' HTTP/' . $httpVer . "\r\n"
. 'Host: ' . $host . "\r\n";
// Add the user-agent header
if (isset($this->config['useragent'])) {
$request .= "User-agent: " . $this->config['useragent'] . "\r\n";
$request .= 'User-agent: ' . $this->config['useragent'] . "\r\n";
}
// If the proxy-authorization header is set, send it to proxy but remove
// it from headers sent to target host
if (isset($headers['proxy-authorization'])) {
$request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
$request .= 'Proxy-authorization: ' . $headers['proxy-authorization'] . "\r\n";
unset($headers['proxy-authorization']);
}
@@ -224,8 +228,8 @@ class Proxy extends Socket
ErrorHandler::start();
$test = fwrite($this->socket, $request);
$error = ErrorHandler::stop();
if (!$test) {
throw new AdapterException\RuntimeException("Error writing request to proxy server", 0, $error);
if (! $test) {
throw new AdapterException\RuntimeException('Error writing request to proxy server', 0, $error);
}
// Read response headers only
@@ -236,7 +240,7 @@ class Proxy extends Socket
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
if ($gotStatus) {
$response .= $line;
if (!rtrim($line)) {
if (! rtrim($line)) {
break;
}
}
@@ -245,7 +249,10 @@ class Proxy extends Socket
// Check that the response from the proxy is 200
if (Response::fromString($response)->getStatusCode() != 200) {
throw new AdapterException\RuntimeException("Unable to connect to HTTPS proxy. Server response: " . $response);
throw new AdapterException\RuntimeException(sprintf(
'Unable to connect to HTTPS proxy. Server response: %s',
$response
));
}
// If all is good, switch socket to secure mode. We have to fall back
@@ -254,7 +261,7 @@ class Proxy extends Socket
STREAM_CRYPTO_METHOD_TLS_CLIENT,
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
];
$success = false;
@@ -266,14 +273,14 @@ class Proxy extends Socket
}
if (! $success) {
throw new AdapterException\RuntimeException("Unable to connect to" .
" HTTPS server through proxy: could not negotiate secure connection.");
throw new AdapterException\RuntimeException(
'Unable to connect to HTTPS server through proxy: could not negotiate secure connection.'
);
}
}
/**
* Close the connection to the server
*
*/
public function close()
{
@@ -283,7 +290,6 @@ class Proxy extends Socket
/**
* Destructor: make sure the socket is disconnected
*
*/
public function __destruct()
{

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;
@@ -12,6 +10,7 @@ namespace Zend\Http\Client\Adapter;
use Traversable;
use Zend\Http\Client\Adapter\AdapterInterface as HttpAdapter;
use Zend\Http\Client\Adapter\Exception as AdapterException;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\ErrorHandler;
@@ -39,7 +38,7 @@ class Socket implements HttpAdapter, StreamInterface
*
* @var resource|null
*/
protected $socket = null;
protected $socket;
/**
* What host/port are we connected to?
@@ -53,7 +52,7 @@ class Socket implements HttpAdapter, StreamInterface
*
* @var resource
*/
protected $outStream = null;
protected $outStream;
/**
* Parameters array
@@ -70,6 +69,7 @@ class Socket implements HttpAdapter, StreamInterface
'sslcapath' => null,
'sslallowselfsigned' => false,
'sslusecontext' => false,
'sslverifypeername' => true,
];
/**
@@ -77,14 +77,14 @@ class Socket implements HttpAdapter, StreamInterface
*
* @var string
*/
protected $method = null;
protected $method;
/**
* Stream context
*
* @var resource
*/
protected $context = null;
protected $context;
/**
* Adapter constructor, currently empty. Config is set using setOptions()
@@ -105,7 +105,7 @@ class Socket implements HttpAdapter, StreamInterface
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if (!is_array($options)) {
if (! is_array($options)) {
throw new AdapterException\InvalidArgumentException(
'Array or Zend\Config object expected, got ' . gettype($options)
);
@@ -148,9 +148,10 @@ class Socket implements HttpAdapter, StreamInterface
$this->context = stream_context_create($context);
} else {
// Invalid parameter
throw new AdapterException\InvalidArgumentException(
"Expecting either a stream context resource or array, got " . gettype($context)
);
throw new AdapterException\InvalidArgumentException(sprintf(
'Expecting either a stream context resource or array, got %s',
gettype($context)
));
}
return $this;
@@ -194,45 +195,61 @@ class Socket implements HttpAdapter, StreamInterface
}
// Now, if we are not connected, connect
if (!is_resource($this->socket) || ! $this->config['keepalive']) {
if (! is_resource($this->socket) || ! $this->config['keepalive']) {
$context = $this->getStreamContext();
if ($secure || $this->config['sslusecontext']) {
if ($this->config['sslverifypeer'] !== null) {
if (!stream_context_set_option($context, 'ssl', 'verify_peer', $this->config['sslverifypeer'])) {
if (! stream_context_set_option($context, 'ssl', 'verify_peer', $this->config['sslverifypeer'])) {
throw new AdapterException\RuntimeException('Unable to set sslverifypeer option');
}
}
if ($this->config['sslcafile']) {
if (!stream_context_set_option($context, 'ssl', 'cafile', $this->config['sslcafile'])) {
if (! stream_context_set_option($context, 'ssl', 'cafile', $this->config['sslcafile'])) {
throw new AdapterException\RuntimeException('Unable to set sslcafile option');
}
}
if ($this->config['sslcapath']) {
if (!stream_context_set_option($context, 'ssl', 'capath', $this->config['sslcapath'])) {
if (! stream_context_set_option($context, 'ssl', 'capath', $this->config['sslcapath'])) {
throw new AdapterException\RuntimeException('Unable to set sslcapath option');
}
}
if ($this->config['sslallowselfsigned'] !== null) {
if (!stream_context_set_option($context, 'ssl', 'allow_self_signed', $this->config['sslallowselfsigned'])) {
if (! stream_context_set_option(
$context,
'ssl',
'allow_self_signed',
$this->config['sslallowselfsigned']
)) {
throw new AdapterException\RuntimeException('Unable to set sslallowselfsigned option');
}
}
if ($this->config['sslcert'] !== null) {
if (!stream_context_set_option($context, 'ssl', 'local_cert', $this->config['sslcert'])) {
if (! stream_context_set_option($context, 'ssl', 'local_cert', $this->config['sslcert'])) {
throw new AdapterException\RuntimeException('Unable to set sslcert option');
}
}
if ($this->config['sslpassphrase'] !== null) {
if (!stream_context_set_option($context, 'ssl', 'passphrase', $this->config['sslpassphrase'])) {
if (! stream_context_set_option($context, 'ssl', 'passphrase', $this->config['sslpassphrase'])) {
throw new AdapterException\RuntimeException('Unable to set sslpassphrase option');
}
}
if ($this->config['sslverifypeername'] !== null) {
if (! stream_context_set_option(
$context,
'ssl',
'verify_peer_name',
$this->config['sslverifypeername']
)) {
throw new AdapterException\RuntimeException('Unable to set sslverifypeername option');
}
}
}
$flags = STREAM_CLIENT_CONNECT;
@@ -240,18 +257,23 @@ class Socket implements HttpAdapter, StreamInterface
$flags |= STREAM_CLIENT_PERSISTENT;
}
if (isset($this->config['connecttimeout'])) {
$connectTimeout = $this->config['connecttimeout'];
} else {
$connectTimeout = $this->config['timeout'];
}
ErrorHandler::start();
$this->socket = stream_socket_client(
$host . ':' . $port,
$errno,
$errstr,
(int) $this->config['timeout'],
(int) $connectTimeout,
$flags,
$context
);
$error = ErrorHandler::stop();
if (!$this->socket) {
if (! $this->socket) {
$this->close();
throw new AdapterException\RuntimeException(
sprintf(
@@ -266,7 +288,7 @@ class Socket implements HttpAdapter, StreamInterface
}
// Set the stream timeout
if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
if (! stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
throw new AdapterException\RuntimeException('Unable to set the connection timeout');
}
@@ -280,12 +302,12 @@ class Socket implements HttpAdapter, StreamInterface
ErrorHandler::start();
$test = stream_socket_enable_crypto($this->socket, true, $sslCryptoMethod);
$error = ErrorHandler::stop();
if (!$test || $error) {
if (! $test || $error) {
// Error handling is kind of difficult when it comes to SSL
$errorString = '';
if (extension_loaded('openssl')) {
while (($sslError = openssl_error_string()) != false) {
$errorString .= "; SSL error: $sslError";
$errorString .= sprintf('; SSL error: %s', $sslError);
}
}
$this->close();
@@ -293,16 +315,18 @@ class Socket implements HttpAdapter, StreamInterface
if ((! $errorString) && $this->config['sslverifypeer']) {
// There's good chance our error is due to sslcapath not being properly set
if (! ($this->config['sslcafile'] || $this->config['sslcapath'])) {
$errorString = 'make sure the "sslcafile" or "sslcapath" option are properly set for the environment.';
} elseif ($this->config['sslcafile'] && !is_file($this->config['sslcafile'])) {
$errorString = 'make sure the "sslcafile" or "sslcapath" option are properly set for the '
. 'environment.';
} elseif ($this->config['sslcafile'] && ! is_file($this->config['sslcafile'])) {
$errorString = 'make sure the "sslcafile" option points to a valid SSL certificate file';
} elseif ($this->config['sslcapath'] && !is_dir($this->config['sslcapath'])) {
$errorString = 'make sure the "sslcapath" option points to a valid SSL certificate directory';
} elseif ($this->config['sslcapath'] && ! is_dir($this->config['sslcapath'])) {
$errorString = 'make sure the "sslcapath" option points to a valid SSL certificate '
. 'directory';
}
}
if ($errorString) {
$errorString = ": $errorString";
$errorString = sprintf(': %s', $errorString);
}
throw new AdapterException\RuntimeException(sprintf(
@@ -312,7 +336,7 @@ class Socket implements HttpAdapter, StreamInterface
), 0, $error);
}
$host = $this->config['ssltransport'] . "://" . $host;
$host = $this->config['ssltransport'] . '://' . $host;
} else {
$host = 'tcp://' . $host;
}
@@ -355,12 +379,12 @@ class Socket implements HttpAdapter, StreamInterface
if ($uri->getQuery()) {
$path .= '?' . $uri->getQuery();
}
$request = "{$method} {$path} HTTP/{$httpVer}\r\n";
$request = $method . ' ' . $path . ' HTTP/' . $httpVer . "\r\n";
foreach ($headers as $k => $v) {
if (is_string($k)) {
$v = ucfirst($k) . ": $v";
$v = ucfirst($k) . ': ' . $v;
}
$request .= "$v\r\n";
$request .= $v . "\r\n";
}
if (is_resource($body)) {
@@ -411,7 +435,7 @@ class Socket implements HttpAdapter, StreamInterface
$this->_checkSocketReadTimeout();
$responseObj= Response::fromString($response);
$responseObj = Response::fromString($response);
$statusCode = $responseObj->getStatusCode();
@@ -427,8 +451,10 @@ class Socket implements HttpAdapter, StreamInterface
* Responses to HEAD requests and 204 or 304 responses are not expected
* to have a body - stop reading here
*/
if ($statusCode == 304 || $statusCode == 204 ||
$this->method == \Zend\Http\Request::METHOD_HEAD) {
if ($statusCode == 304
|| $statusCode == 204
|| $this->method == Request::METHOD_HEAD
) {
// Close the connection if requested to do so by the server
$connection = $headers->get('connection');
if ($connection && $connection->getFieldValue() == 'close') {
@@ -452,8 +478,10 @@ class Socket implements HttpAdapter, StreamInterface
$chunksize = trim($line);
if (! ctype_xdigit($chunksize)) {
$this->close();
throw new AdapterException\RuntimeException('Invalid chunk size "' .
$chunksize . '" unable to read chunked body');
throw new AdapterException\RuntimeException(sprintf(
'Invalid chunk size "%s" unable to read chunked body',
$chunksize
));
}
// Convert the hexadecimal value to plain integer
@@ -488,14 +516,16 @@ class Socket implements HttpAdapter, StreamInterface
ErrorHandler::stop();
$this->_checkSocketReadTimeout();
if (!$this->outStream) {
if (! $this->outStream) {
$response .= $chunk;
}
} while ($chunksize > 0);
} else {
$this->close();
throw new AdapterException\RuntimeException('Cannot handle "' .
$transferEncoding->getFieldValue() . '" transfer encoding');
throw new AdapterException\RuntimeException(sprintf(
'Cannot handle "%s" transfer encoding',
$transferEncoding->getFieldValue()
));
}
// We automatically decode chunked-messages when writing to a stream
@@ -590,15 +620,17 @@ class Socket implements HttpAdapter, StreamInterface
*
* @throws AdapterException\TimeoutException with READ_TIMEOUT code
*/
// @codingStandardsIgnoreStart
protected function _checkSocketReadTimeout()
{
// @codingStandardsIgnoreEnd
if ($this->socket) {
$info = stream_get_meta_data($this->socket);
$timedout = $info['timed_out'];
if ($timedout) {
$this->close();
throw new AdapterException\TimeoutException(
"Read timed out after {$this->config['timeout']} seconds",
sprintf('Read timed out after %d seconds', $this->config['timeout']),
AdapterException\TimeoutException::READ_TIMEOUT
);
}

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;

View File

@@ -1,10 +1,8 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http\Client\Adapter;
@@ -132,12 +130,12 @@ class Test implements AdapterInterface
if ($uri->getQuery()) {
$path .= '?' . $uri->getQuery();
}
$request = "{$method} {$path} HTTP/{$httpVer}\r\n";
$request = $method . ' ' . $path . ' HTTP/' . $httpVer . "\r\n";
foreach ($headers as $k => $v) {
if (is_string($k)) {
$v = ucfirst($k) . ": $v";
$v = ucfirst($k) . ': ' . $v;
}
$request .= "$v\r\n";
$request .= $v . "\r\n";
}
// Add the request body
@@ -209,7 +207,8 @@ class Test implements AdapterInterface
{
if ($index < 0 || $index >= count($this->responses)) {
throw new Exception\OutOfRangeException(
'Index out of range of response buffer size');
'Index out of range of response buffer size'
);
}
$this->responseIndex = $index;
}