]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Adapter.php
Updating external libraries for net access
[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  * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>\r
10  * All rights reserved.\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions\r
14  * are met:\r
15  *\r
16  *    * Redistributions of source code must retain the above copyright\r
17  *      notice, this list of conditions and the following disclaimer.\r
18  *    * Redistributions in binary form must reproduce the above copyright\r
19  *      notice, this list of conditions and the following disclaimer in the\r
20  *      documentation and/or other materials provided with the distribution.\r
21  *    * The names of the authors may not be used to endorse or promote products\r
22  *      derived from this software without specific prior written permission.\r
23  *\r
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
32  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
35  *\r
36  * @category HTTP\r
37  * @package  HTTP_Request2\r
38  * @author   Alexey Borzov <avb@php.net>\r
39  * @license  http://opensource.org/licenses/bsd-license.php New BSD License\r
40  * @version  SVN: $Id: Adapter.php 324415 2012-03-21 10:50:50Z avb $\r
41  * @link     http://pear.php.net/package/HTTP_Request2\r
42  */\r
43 \r
44 /**\r
45  * Class representing a HTTP response\r
46  */\r
47 require_once 'HTTP/Request2/Response.php';\r
48 \r
49 /**\r
50  * Base class for HTTP_Request2 adapters\r
51  *\r
52  * HTTP_Request2 class itself only defines methods for aggregating the request\r
53  * data, all actual work of sending the request to the remote server and\r
54  * receiving its response is performed by adapters.\r
55  *\r
56  * @category HTTP\r
57  * @package  HTTP_Request2\r
58  * @author   Alexey Borzov <avb@php.net>\r
59  * @license  http://opensource.org/licenses/bsd-license.php New BSD License\r
60  * @version  Release: 2.1.1\r
61  * @link     http://pear.php.net/package/HTTP_Request2\r
62  */\r
63 abstract class HTTP_Request2_Adapter\r
64 {\r
65     /**\r
66      * A list of methods that MUST NOT have a request body, per RFC 2616\r
67      * @var  array\r
68      */\r
69     protected static $bodyDisallowed = array('TRACE');\r
70 \r
71     /**\r
72      * Methods having defined semantics for request body\r
73      *\r
74      * Content-Length header (indicating that the body follows, section 4.3 of\r
75      * RFC 2616) will be sent for these methods even if no body was added\r
76      *\r
77      * @var  array\r
78      * @link http://pear.php.net/bugs/bug.php?id=12900\r
79      * @link http://pear.php.net/bugs/bug.php?id=14740\r
80      */\r
81     protected static $bodyRequired = array('POST', 'PUT');\r
82 \r
83     /**\r
84      * Request being sent\r
85      * @var  HTTP_Request2\r
86      */\r
87     protected $request;\r
88 \r
89     /**\r
90      * Request body\r
91      * @var  string|resource|HTTP_Request2_MultipartBody\r
92      * @see  HTTP_Request2::getBody()\r
93      */\r
94     protected $requestBody;\r
95 \r
96     /**\r
97      * Length of the request body\r
98      * @var  integer\r
99      */\r
100     protected $contentLength;\r
101 \r
102     /**\r
103      * Sends request to the remote server and returns its response\r
104      *\r
105      * @param HTTP_Request2 $request HTTP request message\r
106      *\r
107      * @return   HTTP_Request2_Response\r
108      * @throws   HTTP_Request2_Exception\r
109      */\r
110     abstract public function sendRequest(HTTP_Request2 $request);\r
111 \r
112     /**\r
113      * Calculates length of the request body, adds proper headers\r
114      *\r
115      * @param array &$headers associative array of request headers, this method\r
116      *                        will add proper 'Content-Length' and 'Content-Type'\r
117      *                        headers to this array (or remove them if not needed)\r
118      */\r
119     protected function calculateRequestLength(&$headers)\r
120     {\r
121         $this->requestBody = $this->request->getBody();\r
122 \r
123         if (is_string($this->requestBody)) {\r
124             $this->contentLength = strlen($this->requestBody);\r
125         } elseif (is_resource($this->requestBody)) {\r
126             $stat = fstat($this->requestBody);\r
127             $this->contentLength = $stat['size'];\r
128             rewind($this->requestBody);\r
129         } else {\r
130             $this->contentLength = $this->requestBody->getLength();\r
131             $headers['content-type'] = 'multipart/form-data; boundary=' .\r
132                                        $this->requestBody->getBoundary();\r
133             $this->requestBody->rewind();\r
134         }\r
135 \r
136         if (in_array($this->request->getMethod(), self::$bodyDisallowed)\r
137             || 0 == $this->contentLength\r
138         ) {\r
139             // No body: send a Content-Length header nonetheless (request #12900),\r
140             // but do that only for methods that require a body (bug #14740)\r
141             if (in_array($this->request->getMethod(), self::$bodyRequired)) {\r
142                 $headers['content-length'] = 0;\r
143             } else {\r
144                 unset($headers['content-length']);\r
145                 // if the method doesn't require a body and doesn't have a\r
146                 // body, don't send a Content-Type header. (request #16799)\r
147                 unset($headers['content-type']);\r
148             }\r
149         } else {\r
150             if (empty($headers['content-type'])) {\r
151                 $headers['content-type'] = 'application/x-www-form-urlencoded';\r
152             }\r
153             $headers['content-length'] = $this->contentLength;\r
154         }\r
155     }\r
156 }\r
157 ?>\r