]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - extlib/HTTP/Request2/Adapter.php
Rebuilt HTTPClient class as an extension of PEAR HTTP_Request2 package, adding redire...
[quix0rs-gnu-social.git] / extlib / HTTP / Request2 / Adapter.php
diff --git a/extlib/HTTP/Request2/Adapter.php b/extlib/HTTP/Request2/Adapter.php
new file mode 100644 (file)
index 0000000..39b092b
--- /dev/null
@@ -0,0 +1,152 @@
+<?php\r
+/**\r
+ * Base class for HTTP_Request2 adapters\r
+ *\r
+ * PHP version 5\r
+ *\r
+ * LICENSE:\r
+ *\r
+ * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ *    * Redistributions of source code must retain the above copyright\r
+ *      notice, this list of conditions and the following disclaimer.\r
+ *    * Redistributions in binary form must reproduce the above copyright\r
+ *      notice, this list of conditions and the following disclaimer in the\r
+ *      documentation and/or other materials provided with the distribution.\r
+ *    * The names of the authors may not be used to endorse or promote products\r
+ *      derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @category   HTTP\r
+ * @package    HTTP_Request2\r
+ * @author     Alexey Borzov <avb@php.net>\r
+ * @license    http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version    CVS: $Id: Adapter.php 274684 2009-01-26 23:07:27Z avb $\r
+ * @link       http://pear.php.net/package/HTTP_Request2\r
+ */\r
+\r
+/**\r
+ * Class representing a HTTP response\r
+ */\r
+require_once 'HTTP/Request2/Response.php';\r
+\r
+/**\r
+ * Base class for HTTP_Request2 adapters\r
+ *\r
+ * HTTP_Request2 class itself only defines methods for aggregating the request\r
+ * data, all actual work of sending the request to the remote server and \r
+ * receiving its response is performed by adapters.\r
+ *\r
+ * @category   HTTP\r
+ * @package    HTTP_Request2\r
+ * @author     Alexey Borzov <avb@php.net>\r
+ * @version    Release: 0.4.1\r
+ */\r
+abstract class HTTP_Request2_Adapter\r
+{\r
+   /**\r
+    * A list of methods that MUST NOT have a request body, per RFC 2616\r
+    * @var  array\r
+    */\r
+    protected static $bodyDisallowed = array('TRACE');\r
+\r
+   /**\r
+    * Methods having defined semantics for request body\r
+    *\r
+    * Content-Length header (indicating that the body follows, section 4.3 of\r
+    * RFC 2616) will be sent for these methods even if no body was added\r
+    *\r
+    * @var  array\r
+    * @link http://pear.php.net/bugs/bug.php?id=12900\r
+    * @link http://pear.php.net/bugs/bug.php?id=14740\r
+    */\r
+    protected static $bodyRequired = array('POST', 'PUT');\r
+\r
+   /**\r
+    * Request being sent\r
+    * @var  HTTP_Request2\r
+    */\r
+    protected $request;\r
+\r
+   /**\r
+    * Request body\r
+    * @var  string|resource|HTTP_Request2_MultipartBody\r
+    * @see  HTTP_Request2::getBody()\r
+    */\r
+    protected $requestBody;\r
+\r
+   /**\r
+    * Length of the request body\r
+    * @var  integer\r
+    */\r
+    protected $contentLength;\r
+\r
+   /**\r
+    * Sends request to the remote server and returns its response\r
+    *\r
+    * @param    HTTP_Request2\r
+    * @return   HTTP_Request2_Response\r
+    * @throws   HTTP_Request2_Exception\r
+    */\r
+    abstract public function sendRequest(HTTP_Request2 $request);\r
+\r
+   /**\r
+    * Calculates length of the request body, adds proper headers\r
+    *\r
+    * @param    array   associative array of request headers, this method will \r
+    *                   add proper 'Content-Length' and 'Content-Type' headers \r
+    *                   to this array (or remove them if not needed)\r
+    */\r
+    protected function calculateRequestLength(&$headers)\r
+    {\r
+        $this->requestBody = $this->request->getBody();\r
+\r
+        if (is_string($this->requestBody)) {\r
+            $this->contentLength = strlen($this->requestBody);\r
+        } elseif (is_resource($this->requestBody)) {\r
+            $stat = fstat($this->requestBody);\r
+            $this->contentLength = $stat['size'];\r
+            rewind($this->requestBody);\r
+        } else {\r
+            $this->contentLength = $this->requestBody->getLength();\r
+            $headers['content-type'] = 'multipart/form-data; boundary=' .\r
+                                       $this->requestBody->getBoundary();\r
+            $this->requestBody->rewind();\r
+        }\r
+\r
+        if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||\r
+            0 == $this->contentLength\r
+        ) {\r
+            unset($headers['content-type']);\r
+            // No body: send a Content-Length header nonetheless (request #12900),\r
+            // but do that only for methods that require a body (bug #14740)\r
+            if (in_array($this->request->getMethod(), self::$bodyRequired)) {\r
+                $headers['content-length'] = 0;\r
+            } else {\r
+                unset($headers['content-length']);\r
+            }\r
+        } else {\r
+            if (empty($headers['content-type'])) {\r
+                $headers['content-type'] = 'application/x-www-form-urlencoded';\r
+            }\r
+            $headers['content-length'] = $this->contentLength;\r
+        }\r
+    }\r
+}\r
+?>\r