We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 08b4b73c authored by mattl's avatar mattl

Updating HTTP_Request2 to 2.3.0

Source: https://pear.php.net/package/HTTP_Request2
Release date: 2016-02-13 15:24 UTC
parent fb492d4b
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -21,7 +21,9 @@ ...@@ -21,7 +21,9 @@
/** /**
* A class representing an URL as per RFC 3986. * A class representing an URL as per RFC 3986.
*/ */
require_once 'Net/URL2.php'; if (!class_exists('Net_URL2', true)) {
require_once 'Net/URL2.php';
}
/** /**
* Exception class for HTTP_Request2 package * Exception class for HTTP_Request2 package
...@@ -35,7 +37,7 @@ require_once 'HTTP/Request2/Exception.php'; ...@@ -35,7 +37,7 @@ require_once 'HTTP/Request2/Exception.php';
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: 2.2.1 * @version Release: 2.3.0
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
* @link http://tools.ietf.org/html/rfc2616#section-5 * @link http://tools.ietf.org/html/rfc2616#section-5
*/ */
...@@ -213,7 +215,7 @@ class HTTP_Request2 implements SplSubject ...@@ -213,7 +215,7 @@ class HTTP_Request2 implements SplSubject
$this->setMethod($method); $this->setMethod($method);
} }
$this->setHeader( $this->setHeader(
'user-agent', 'HTTP_Request2/2.2.1 ' . 'user-agent', 'HTTP_Request2/2.3.0 ' .
'(http://pear.php.net/package/http_request2) PHP/' . phpversion() '(http://pear.php.net/package/http_request2) PHP/' . phpversion()
); );
} }
...@@ -794,6 +796,11 @@ class HTTP_Request2 implements SplSubject ...@@ -794,6 +796,11 @@ class HTTP_Request2 implements SplSubject
* encoded by Content-Encoding</li> * encoded by Content-Encoding</li>
* <li>'receivedBody' - after receiving the complete response * <li>'receivedBody' - after receiving the complete response
* body, data is HTTP_Request2_Response object</li> * body, data is HTTP_Request2_Response object</li>
* <li>'warning' - a problem arose during the request
* that is not severe enough to throw
* an Exception, data is the warning
* message (string). Currently dispatched if
* response body was received incompletely.</li>
* </ul> * </ul>
* Different adapters may not send all the event types. Mock adapter does * Different adapters may not send all the event types. Mock adapter does
* not send any events to the observers. * not send any events to the observers.
...@@ -1022,7 +1029,7 @@ class HTTP_Request2 implements SplSubject ...@@ -1022,7 +1029,7 @@ class HTTP_Request2 implements SplSubject
} }
// (deprecated) mime_content_type function available // (deprecated) mime_content_type function available
if (empty($info) && function_exists('mime_content_type')) { if (empty($info) && function_exists('mime_content_type')) {
return mime_content_type($filename); $info = mime_content_type($filename);
} }
return empty($info)? 'application/octet-stream': $info; return empty($info)? 'application/octet-stream': $info;
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -34,7 +34,7 @@ require_once 'HTTP/Request2/Response.php'; ...@@ -34,7 +34,7 @@ require_once 'HTTP/Request2/Response.php';
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: 2.2.1 * @version Release: 2.3.0
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
abstract class HTTP_Request2_Adapter abstract class HTTP_Request2_Adapter
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -30,7 +30,7 @@ require_once 'HTTP/Request2/Adapter.php'; ...@@ -30,7 +30,7 @@ require_once 'HTTP/Request2/Adapter.php';
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: 2.2.1 * @version Release: 2.3.0
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
...@@ -116,6 +116,12 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -116,6 +116,12 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
*/ */
protected $eventReceivedHeaders = false; protected $eventReceivedHeaders = false;
/**
* Whether 'sentBoody' event was sent to observers
* @var boolean
*/
protected $eventSentBody = false;
/** /**
* Position within request body * Position within request body
* @var integer * @var integer
...@@ -171,6 +177,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -171,6 +177,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
$this->position = 0; $this->position = 0;
$this->eventSentHeaders = false; $this->eventSentHeaders = false;
$this->eventReceivedHeaders = false; $this->eventReceivedHeaders = false;
$this->eventSentBody = false;
try { try {
if (false === curl_exec($ch = $this->createCurlHandle())) { if (false === curl_exec($ch = $this->createCurlHandle())) {
...@@ -180,6 +187,9 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -180,6 +187,9 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
} }
if (isset($ch)) { if (isset($ch)) {
$this->lastInfo = curl_getinfo($ch); $this->lastInfo = curl_getinfo($ch);
if (CURLE_OK !== curl_errno($ch)) {
$this->request->setLastEvent('warning', curl_error($ch));
}
curl_close($ch); curl_close($ch);
} }
...@@ -191,7 +201,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -191,7 +201,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
} }
if ($jar = $request->getCookieJar()) { if ($jar = $request->getCookieJar()) {
$jar->addCookiesFromResponse($response, $request->getUrl()); $jar->addCookiesFromResponse($response);
} }
if (0 < $this->lastInfo['size_download']) { if (0 < $this->lastInfo['size_download']) {
...@@ -400,9 +410,12 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -400,9 +410,12 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
protected function workaroundPhpBug47204($ch, &$headers) protected function workaroundPhpBug47204($ch, &$headers)
{ {
// no redirects, no digest auth -> probably no rewind needed // no redirects, no digest auth -> probably no rewind needed
// also apply workaround only for POSTs, othrerwise we get
// https://pear.php.net/bugs/bug.php?id=20440 for PUTs
if (!$this->request->getConfig('follow_redirects') if (!$this->request->getConfig('follow_redirects')
&& (!($auth = $this->request->getAuth()) && (!($auth = $this->request->getAuth())
|| HTTP_Request2::AUTH_DIGEST != $auth['scheme']) || HTTP_Request2::AUTH_DIGEST != $auth['scheme'])
|| HTTP_Request2::METHOD_POST !== $this->request->getMethod()
) { ) {
curl_setopt($ch, CURLOPT_READFUNCTION, array($this, 'callbackReadBody')); curl_setopt($ch, CURLOPT_READFUNCTION, array($this, 'callbackReadBody'));
...@@ -469,40 +482,36 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -469,40 +482,36 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
*/ */
protected function callbackWriteHeader($ch, $string) protected function callbackWriteHeader($ch, $string)
{ {
// we may receive a second set of headers if doing e.g. digest auth if (!$this->eventSentHeaders
if ($this->eventReceivedHeaders || !$this->eventSentHeaders) { // we may receive a second set of headers if doing e.g. digest auth
// don't bother with 100-Continue responses (bug #15785) // but don't bother with 100-Continue responses (bug #15785)
if (!$this->eventSentHeaders || $this->eventReceivedHeaders && $this->response->getStatus() >= 200
|| $this->response->getStatus() >= 200 ) {
) { $this->request->setLastEvent(
$this->request->setLastEvent( 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)
'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT) );
); }
} if (!$this->eventSentBody) {
$upload = curl_getinfo($ch, CURLINFO_SIZE_UPLOAD); $upload = curl_getinfo($ch, CURLINFO_SIZE_UPLOAD);
// if body wasn't read by a callback, send event with total body size // if body wasn't read by the callback, send event with total body size
if ($upload > $this->position) { if ($upload > $this->position) {
$this->request->setLastEvent( $this->request->setLastEvent(
'sentBodyPart', $upload - $this->position 'sentBodyPart', $upload - $this->position
); );
$this->position = $upload;
} }
if ($upload && (!$this->eventSentHeaders if ($upload > 0) {
|| $this->response->getStatus() >= 200)
) {
$this->request->setLastEvent('sentBody', $upload); $this->request->setLastEvent('sentBody', $upload);
} }
$this->eventSentHeaders = true;
// we'll need a new response object
if ($this->eventReceivedHeaders) {
$this->eventReceivedHeaders = false;
$this->response = null;
}
} }
if (empty($this->response)) { $this->eventSentHeaders = true;
$this->response = new HTTP_Request2_Response( $this->eventSentBody = true;
if ($this->eventReceivedHeaders || empty($this->response)) {
$this->eventReceivedHeaders = false;
$this->response = new HTTP_Request2_Response(
$string, false, curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) $string, false, curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)
); );
} else { } else {
$this->response->parseHeaderLine($string); $this->response->parseHeaderLine($string);
if ('' == trim($string)) { if ('' == trim($string)) {
...@@ -522,7 +531,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -522,7 +531,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
} }
if ($jar = $this->request->getCookieJar()) { if ($jar = $this->request->getCookieJar()) {
$jar->addCookiesFromResponse($this->response, $this->request->getUrl()); $jar->addCookiesFromResponse($this->response);
if (!$redirectUrl->isAbsolute()) { if (!$redirectUrl->isAbsolute()) {
$redirectUrl = $this->request->getUrl()->resolve($redirectUrl); $redirectUrl = $this->request->getUrl()->resolve($redirectUrl);
} }
...@@ -532,6 +541,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter ...@@ -532,6 +541,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
} }
} }
$this->eventReceivedHeaders = true; $this->eventReceivedHeaders = true;
$this->eventSentBody = false;
} }
} }
return strlen($string); return strlen($string);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -44,7 +44,7 @@ require_once 'HTTP/Request2/Adapter.php'; ...@@ -44,7 +44,7 @@ require_once 'HTTP/Request2/Adapter.php';
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: 2.2.1 * @version Release: 2.3.0
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -34,7 +34,7 @@ require_once 'HTTP/Request2/SocketWrapper.php'; ...@@ -34,7 +34,7 @@ require_once 'HTTP/Request2/SocketWrapper.php';
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @version Release: 2.2.1 * @version Release: 2.3.0
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
...@@ -147,7 +147,7 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -147,7 +147,7 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
if ($jar = $request->getCookieJar()) { if ($jar = $request->getCookieJar()) {
$jar->addCookiesFromResponse($response, $request->getUrl()); $jar->addCookiesFromResponse($response);
} }
if (!$this->canKeepAlive($keepAlive, $response)) { if (!$this->canKeepAlive($keepAlive, $response)) {
...@@ -261,9 +261,16 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -261,9 +261,16 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
foreach ($this->request->getConfig() as $name => $value) { foreach ($this->request->getConfig() as $name => $value) {
if ('ssl_' == substr($name, 0, 4) && null !== $value) { if ('ssl_' == substr($name, 0, 4) && null !== $value) {
if ('ssl_verify_host' == $name) { if ('ssl_verify_host' == $name) {
if ($value) { if (version_compare(phpversion(), '5.6', '<')) {
$options['ssl']['CN_match'] = $reqHost; if ($value) {
$options['ssl']['CN_match'] = $reqHost;
}
} else {
$options['ssl']['verify_peer_name'] = $value;
$options['ssl']['peer_name'] = $reqHost;
} }
} else { } else {
$options['ssl'][substr($name, 4)] = $value; $options['ssl'][substr($name, 4)] = $value;
} }
...@@ -281,7 +288,7 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -281,7 +288,7 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
// Changing SSL context options after connection is established does *not* // Changing SSL context options after connection is established does *not*
// work, we need a new connection if options change // work, we need a new connection if options change
$remote = ((!$secure || $httpProxy || $socksProxy)? 'tcp://': 'ssl://') $remote = ((!$secure || $httpProxy || $socksProxy)? 'tcp://': 'tls://')
. $host . ':' . $port; . $host . ':' . $port;
$socketKey = $remote . ( $socketKey = $remote . (
($secure && $httpProxy || $socksProxy) ($secure && $httpProxy || $socksProxy)
...@@ -312,12 +319,12 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -312,12 +319,12 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
$conninfo = "tcp://{$reqHost}:{$reqPort} via {$remote}"; $conninfo = "tcp://{$reqHost}:{$reqPort} via {$remote}";
} else { } else {
$this->socket->enableCrypto(); $this->socket->enableCrypto();
$conninfo = "ssl://{$reqHost}:{$reqPort} via {$remote}"; $conninfo = "tls://{$reqHost}:{$reqPort} via {$remote}";
} }
} elseif ($secure && $httpProxy && !$tunnel) { } elseif ($secure && $httpProxy && !$tunnel) {
$this->establishTunnel(); $this->establishTunnel();
$conninfo = "ssl://{$reqHost}:{$reqPort} via {$remote}"; $conninfo = "tls://{$reqHost}:{$reqPort} via {$remote}";
} else { } else {
$this->socket = new HTTP_Request2_SocketWrapper( $this->socket = new HTTP_Request2_SocketWrapper(
...@@ -1043,14 +1050,14 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -1043,14 +1050,14 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
$chunked = 'chunked' == $response->getHeader('transfer-encoding'); $chunked = 'chunked' == $response->getHeader('transfer-encoding');
$length = $response->getHeader('content-length'); $length = $response->getHeader('content-length');
$hasBody = false; $hasBody = false;
if ($chunked || null === $length || 0 < intval($length)) { // RFC 2616, section 4.4:
// RFC 2616, section 4.4: // 3. ... If a message is received with both a
// 3. ... If a message is received with both a // Transfer-Encoding header field and a Content-Length header field,
// Transfer-Encoding header field and a Content-Length header field, // the latter MUST be ignored.
// the latter MUST be ignored. $toRead = ($chunked || null === $length)? null: $length;
$toRead = ($chunked || null === $length)? null: $length; $this->chunkLength = 0;
$this->chunkLength = 0;
if ($chunked || null === $length || 0 < intval($length)) {
while (!$this->socket->eof() && (is_null($toRead) || 0 < $toRead)) { while (!$this->socket->eof() && (is_null($toRead) || 0 < $toRead)) {
if ($chunked) { if ($chunked) {
$data = $this->readChunked($bufferSize); $data = $this->readChunked($bufferSize);
...@@ -1075,6 +1082,11 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -1075,6 +1082,11 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
} }
} }
} }
if (0 !== $this->chunkLength || null !== $toRead && $toRead > 0) {
$this->request->setLastEvent(
'warning', 'transfer closed with outstanding read data remaining'
);
}
if ($hasBody) { if ($hasBody) {
$this->request->setLastEvent('receivedBody', $response); $this->request->setLastEvent('receivedBody', $response);
...@@ -1095,11 +1107,16 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter ...@@ -1095,11 +1107,16 @@ class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
// at start of the next chunk? // at start of the next chunk?
if (0 == $this->chunkLength) { if (0 == $this->chunkLength) {
$line = $this->socket->readLine($bufferSize); $line = $this->socket->readLine($bufferSize);
if (!preg_match('/^([0-9a-f]+)/i', $line, $matches)) { if ('' === $line && $this->socket->eof()) {
$this->chunkLength = -1; // indicate missing chunk
return '';
} elseif (!preg_match('/^([0-9a-f]+)/i', $line, $matches)) {
throw new HTTP_Request2_MessageException( throw new HTTP_Request2_MessageException(
"Cannot decode chunked response, invalid chunk length '{$line}'", "Cannot decode chunked response, invalid chunk length '{$line}'",
HTTP_Request2_Exception::DECODE_ERROR HTTP_Request2_Exception::DECODE_ERROR
); );
} else { } else {
$this->chunkLength = hexdec($matches[1]); $this->chunkLength = hexdec($matches[1]);
// Chunk with zero length indicates the end // Chunk with zero length indicates the end
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @category HTTP * @category HTTP
* @package HTTP_Request2 * @package HTTP_Request2
* @author Alexey Borzov <avb@php.net> * @author Alexey Borzov <avb@php.net>
* @copyright 2008-2014 Alexey Borzov <avb@php.net> * @copyright 2008-2016 Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
* @link http://pear.php.net/package/HTTP_Request2 * @link http://pear.php.net/package/HTTP_Request2
*/ */
...@@ -61,6 +61,12 @@ class HTTP_Request2_CookieJar implements Serializable ...@@ -61,6 +61,12 @@ class HTTP_Request2_CookieJar implements Serializable
*/ */
protected $useList = true; protected $useList = true;
/**
* Whether an attempt to store an invalid cookie should be ignored, rather than cause an Exception
* @var bool
*/
protected $ignoreInvalid = false;
/** /**
* Array with Public Suffix List data * Array with Public Suffix List data
* @var array * @var array
...@@ -75,12 +81,16 @@ class HTTP_Request2_CookieJar implements Serializable ...@@ -75,12 +81,16 @@ class HTTP_Request2_CookieJar implements Serializable
* see {@link serializeSessionCookies()} * see {@link serializeSessionCookies()}
* @param bool $usePublicSuffixList Controls using Public Suffix List, * @param bool $usePublicSuffixList Controls using Public Suffix List,
* see {@link usePublicSuffixList()} * see {@link usePublicSuffixList()}
* @param bool $ignoreInvalidCookies Whether invalid cookies should be ignored,
* see {@link ignoreInvalidCookies()}
*/ */
public function __construct( public function __construct(
$serializeSessionCookies = false, $usePublicSuffixList = true $serializeSessionCookies = false, $usePublicSuffixList = true,
$ignoreInvalidCookies = false
) { ) {
$this->serializeSessionCookies($serializeSessionCookies); $this->serializeSessionCookies($serializeSessionCookies);
$this->usePublicSuffixList($usePublicSuffixList); $this->usePublicSuffixList($usePublicSuffixList);
$this->ignoreInvalidCookies($ignoreInvalidCookies);
} }
/** /**
...@@ -194,11 +204,20 @@ class HTTP_Request2_CookieJar implements Serializable ...@@ -194,11 +204,20 @@ class HTTP_Request2_CookieJar implements Serializable
* {@link HTTP_Request2_Response::getCookies()} * {@link HTTP_Request2_Response::getCookies()}
* @param Net_URL2 $setter URL of the document that sent Set-Cookie header * @param Net_URL2 $setter URL of the document that sent Set-Cookie header
* *
* @throws HTTP_Request2_Exception * @return bool whether the cookie was successfully stored
* @throws HTTP_Request2_Exception
*/ */
public function store(array $cookie, Net_URL2 $setter = null) public function store(array $cookie, Net_URL2 $setter = null)
{ {
$cookie = $this->checkAndUpdateFields($cookie, $setter); try {
$cookie = $this->checkAndUpdateFields($cookie, $setter);
} catch (HTTP_Request2_Exception $e) {
if ($this->ignoreInvalid) {
return false;
} else {
throw $e;
}
}
if (strlen($cookie['value']) if (strlen($cookie['value'])
&& (is_null($cookie['expires']) || $cookie['expires'] > $this->now()) && (is_null($cookie['expires']) || $cookie['expires'] > $this->now())
...@@ -214,6 +233,8 @@ class HTTP_Request2_CookieJar implements Serializable ...@@ -214,6 +233,8 @@ class HTTP_Request2_CookieJar implements Serializable
} elseif (isset($this->cookies[$cookie['domain']][$cookie['path']][$cookie['name']])) { } elseif (isset($this->cookies[$cookie['domain']][$cookie['path']][$cookie['name']])) {
unset($this->cookies[$cookie['domain']][$cookie['path']][$cookie['name']]); unset($this->cookies[$cookie['domain']][$cookie['path']][$cookie['name']]);
} }
return true;
} }
/** /**
...@@ -221,13 +242,29 @@ class HTTP_Request2_CookieJar implements Serializable