]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Adapter.php
neo-quitter theme from hannesmannerheim
[quix0rs-gnu-social.git] / extlib / HTTP / Request2 / Adapter.php
1 <?php\r
2 /**\r
3  * Base class for HTTP_Request2 adapters\r
4  *\r
5  * PHP version 5\r
6  *\r
7  * LICENSE\r
8  *\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
12  *\r
13  * @category  HTTP\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
19  */\r
20 \r
21 /**\r
22  * Class representing a HTTP response\r
23  */\r
24 require_once 'HTTP/Request2/Response.php';\r
25 \r
26 /**\r
27  * Base class for HTTP_Request2 adapters\r
28  *\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
32  *\r
33  * @category HTTP\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
39  */\r
40 abstract class HTTP_Request2_Adapter\r
41 {\r
42     /**\r
43      * A list of methods that MUST NOT have a request body, per RFC 2616\r
44      * @var  array\r
45      */\r
46     protected static $bodyDisallowed = array('TRACE');\r
47 \r
48     /**\r
49      * Methods having defined semantics for request body\r
50      *\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
53      *\r
54      * @var  array\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
57      */\r
58     protected static $bodyRequired = array('POST', 'PUT');\r
59 \r
60     /**\r
61      * Request being sent\r
62      * @var  HTTP_Request2\r
63      */\r
64     protected $request;\r
65 \r
66     /**\r
67      * Request body\r
68      * @var  string|resource|HTTP_Request2_MultipartBody\r
69      * @see  HTTP_Request2::getBody()\r
70      */\r
71     protected $requestBody;\r
72 \r
73     /**\r
74      * Length of the request body\r
75      * @var  integer\r
76      */\r
77     protected $contentLength;\r
78 \r
79     /**\r
80      * Sends request to the remote server and returns its response\r
81      *\r
82      * @param HTTP_Request2 $request HTTP request message\r
83      *\r
84      * @return   HTTP_Request2_Response\r
85      * @throws   HTTP_Request2_Exception\r
86      */\r
87     abstract public function sendRequest(HTTP_Request2 $request);\r
88 \r
89     /**\r
90      * Calculates length of the request body, adds proper headers\r
91      *\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
95      */\r
96     protected function calculateRequestLength(&$headers)\r
97     {\r
98         $this->requestBody = $this->request->getBody();\r
99 \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
106         } else {\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
111         }\r
112 \r
113         if (in_array($this->request->getMethod(), self::$bodyDisallowed)\r
114             || 0 == $this->contentLength\r
115         ) {\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
120             } else {\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
125             }\r
126         } else {\r
127             if (empty($headers['content-type'])) {\r
128                 $headers['content-type'] = 'application/x-www-form-urlencoded';\r
129             }\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
133             }\r
134         }\r
135     }\r
136 }\r
137 ?>\r