2009-10-28 19:29:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base class for HTTP_Request2 adapters
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
2014-07-10 15:23:21 +01:00
|
|
|
* LICENSE
|
2009-10-28 19:29:20 +00:00
|
|
|
*
|
2014-07-10 15:23:21 +01:00
|
|
|
* This source file is subject to BSD 3-Clause License that is bundled
|
|
|
|
* with this package in the file LICENSE and available at the URL
|
|
|
|
* https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
|
2009-10-28 19:29:20 +00:00
|
|
|
*
|
2014-07-10 15:23:21 +01:00
|
|
|
* @category HTTP
|
|
|
|
* @package HTTP_Request2
|
|
|
|
* @author Alexey Borzov <avb@php.net>
|
|
|
|
* @copyright 2008-2014 Alexey Borzov <avb@php.net>
|
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
|
|
|
|
* @link http://pear.php.net/package/HTTP_Request2
|
2009-10-28 19:29:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a HTTP response
|
|
|
|
*/
|
|
|
|
require_once 'HTTP/Request2/Response.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for HTTP_Request2 adapters
|
|
|
|
*
|
|
|
|
* HTTP_Request2 class itself only defines methods for aggregating the request
|
2011-06-22 20:56:27 +01:00
|
|
|
* data, all actual work of sending the request to the remote server and
|
2009-10-28 19:29:20 +00:00
|
|
|
* receiving its response is performed by adapters.
|
|
|
|
*
|
2013-10-05 13:29:02 +01:00
|
|
|
* @category HTTP
|
|
|
|
* @package HTTP_Request2
|
|
|
|
* @author Alexey Borzov <avb@php.net>
|
2014-07-10 15:23:21 +01:00
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
|
|
|
|
* @version Release: 2.2.1
|
2013-10-05 13:29:02 +01:00
|
|
|
* @link http://pear.php.net/package/HTTP_Request2
|
2009-10-28 19:29:20 +00:00
|
|
|
*/
|
|
|
|
abstract class HTTP_Request2_Adapter
|
|
|
|
{
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* A list of methods that MUST NOT have a request body, per RFC 2616
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected static $bodyDisallowed = array('TRACE');
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Methods having defined semantics for request body
|
|
|
|
*
|
|
|
|
* Content-Length header (indicating that the body follows, section 4.3 of
|
|
|
|
* RFC 2616) will be sent for these methods even if no body was added
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @link http://pear.php.net/bugs/bug.php?id=12900
|
|
|
|
* @link http://pear.php.net/bugs/bug.php?id=14740
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected static $bodyRequired = array('POST', 'PUT');
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Request being sent
|
|
|
|
* @var HTTP_Request2
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected $request;
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Request body
|
|
|
|
* @var string|resource|HTTP_Request2_MultipartBody
|
|
|
|
* @see HTTP_Request2::getBody()
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected $requestBody;
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Length of the request body
|
|
|
|
* @var integer
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected $contentLength;
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Sends request to the remote server and returns its response
|
|
|
|
*
|
|
|
|
* @param HTTP_Request2 $request HTTP request message
|
|
|
|
*
|
|
|
|
* @return HTTP_Request2_Response
|
|
|
|
* @throws HTTP_Request2_Exception
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
abstract public function sendRequest(HTTP_Request2 $request);
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
/**
|
|
|
|
* Calculates length of the request body, adds proper headers
|
|
|
|
*
|
|
|
|
* @param array &$headers associative array of request headers, this method
|
|
|
|
* will add proper 'Content-Length' and 'Content-Type'
|
|
|
|
* headers to this array (or remove them if not needed)
|
|
|
|
*/
|
2009-10-28 19:29:20 +00:00
|
|
|
protected function calculateRequestLength(&$headers)
|
|
|
|
{
|
|
|
|
$this->requestBody = $this->request->getBody();
|
|
|
|
|
|
|
|
if (is_string($this->requestBody)) {
|
|
|
|
$this->contentLength = strlen($this->requestBody);
|
|
|
|
} elseif (is_resource($this->requestBody)) {
|
|
|
|
$stat = fstat($this->requestBody);
|
|
|
|
$this->contentLength = $stat['size'];
|
|
|
|
rewind($this->requestBody);
|
|
|
|
} else {
|
|
|
|
$this->contentLength = $this->requestBody->getLength();
|
|
|
|
$headers['content-type'] = 'multipart/form-data; boundary=' .
|
|
|
|
$this->requestBody->getBoundary();
|
|
|
|
$this->requestBody->rewind();
|
|
|
|
}
|
|
|
|
|
2013-10-05 13:29:02 +01:00
|
|
|
if (in_array($this->request->getMethod(), self::$bodyDisallowed)
|
|
|
|
|| 0 == $this->contentLength
|
2009-10-28 19:29:20 +00:00
|
|
|
) {
|
|
|
|
// No body: send a Content-Length header nonetheless (request #12900),
|
|
|
|
// but do that only for methods that require a body (bug #14740)
|
|
|
|
if (in_array($this->request->getMethod(), self::$bodyRequired)) {
|
|
|
|
$headers['content-length'] = 0;
|
|
|
|
} else {
|
|
|
|
unset($headers['content-length']);
|
2011-06-22 20:56:27 +01:00
|
|
|
// if the method doesn't require a body and doesn't have a
|
|
|
|
// body, don't send a Content-Type header. (request #16799)
|
|
|
|
unset($headers['content-type']);
|
2009-10-28 19:29:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (empty($headers['content-type'])) {
|
|
|
|
$headers['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
|
}
|
2014-07-10 15:23:21 +01:00
|
|
|
// Content-Length should not be sent for chunked Transfer-Encoding (bug #20125)
|
|
|
|
if (!isset($headers['transfer-encoding'])) {
|
|
|
|
$headers['content-length'] = $this->contentLength;
|
|
|
|
}
|
2009-10-28 19:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|