3 * Base class for HTTP_Request2 adapters
\r
9 * This source file is subject to BSD 3-Clause License that is bundled
\r
10 * with this package in the file LICENSE and available at the URL
\r
11 * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
\r
14 * @package HTTP_Request2
\r
15 * @author Alexey Borzov <avb@php.net>
\r
16 * @copyright 2008-2014 Alexey Borzov <avb@php.net>
\r
17 * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
\r
18 * @link http://pear.php.net/package/HTTP_Request2
\r
22 * Class representing a HTTP response
\r
24 require_once 'HTTP/Request2/Response.php';
\r
27 * Base class for HTTP_Request2 adapters
\r
29 * HTTP_Request2 class itself only defines methods for aggregating the request
\r
30 * data, all actual work of sending the request to the remote server and
\r
31 * receiving its response is performed by adapters.
\r
34 * @package HTTP_Request2
\r
35 * @author Alexey Borzov <avb@php.net>
\r
36 * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
\r
37 * @version Release: 2.2.1
\r
38 * @link http://pear.php.net/package/HTTP_Request2
\r
40 abstract class HTTP_Request2_Adapter
\r
43 * A list of methods that MUST NOT have a request body, per RFC 2616
\r
46 protected static $bodyDisallowed = array('TRACE');
\r
49 * Methods having defined semantics for request body
\r
51 * Content-Length header (indicating that the body follows, section 4.3 of
\r
52 * RFC 2616) will be sent for these methods even if no body was added
\r
55 * @link http://pear.php.net/bugs/bug.php?id=12900
\r
56 * @link http://pear.php.net/bugs/bug.php?id=14740
\r
58 protected static $bodyRequired = array('POST', 'PUT');
\r
61 * Request being sent
\r
62 * @var HTTP_Request2
\r
68 * @var string|resource|HTTP_Request2_MultipartBody
\r
69 * @see HTTP_Request2::getBody()
\r
71 protected $requestBody;
\r
74 * Length of the request body
\r
77 protected $contentLength;
\r
80 * Sends request to the remote server and returns its response
\r
82 * @param HTTP_Request2 $request HTTP request message
\r
84 * @return HTTP_Request2_Response
\r
85 * @throws HTTP_Request2_Exception
\r
87 abstract public function sendRequest(HTTP_Request2 $request);
\r
90 * Calculates length of the request body, adds proper headers
\r
92 * @param array &$headers associative array of request headers, this method
\r
93 * will add proper 'Content-Length' and 'Content-Type'
\r
94 * headers to this array (or remove them if not needed)
\r
96 protected function calculateRequestLength(&$headers)
\r
98 $this->requestBody = $this->request->getBody();
\r
100 if (is_string($this->requestBody)) {
\r
101 $this->contentLength = strlen($this->requestBody);
\r
102 } elseif (is_resource($this->requestBody)) {
\r
103 $stat = fstat($this->requestBody);
\r
104 $this->contentLength = $stat['size'];
\r
105 rewind($this->requestBody);
\r
107 $this->contentLength = $this->requestBody->getLength();
\r
108 $headers['content-type'] = 'multipart/form-data; boundary=' .
\r
109 $this->requestBody->getBoundary();
\r
110 $this->requestBody->rewind();
\r
113 if (in_array($this->request->getMethod(), self::$bodyDisallowed)
\r
114 || 0 == $this->contentLength
\r
116 // No body: send a Content-Length header nonetheless (request #12900),
\r
117 // but do that only for methods that require a body (bug #14740)
\r
118 if (in_array($this->request->getMethod(), self::$bodyRequired)) {
\r
119 $headers['content-length'] = 0;
\r
121 unset($headers['content-length']);
\r
122 // if the method doesn't require a body and doesn't have a
\r
123 // body, don't send a Content-Type header. (request #16799)
\r
124 unset($headers['content-type']);
\r
127 if (empty($headers['content-type'])) {
\r
128 $headers['content-type'] = 'application/x-www-form-urlencoded';
\r
130 // Content-Length should not be sent for chunked Transfer-Encoding (bug #20125)
\r
131 if (!isset($headers['transfer-encoding'])) {
\r
132 $headers['content-length'] = $this->contentLength;
\r