"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/HTTP/Request2/Adapter.php" (20 Nov 2022, 4609 Bytes) of package /linux/www/serendipity-2.4.0.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Adapter.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * Base class for HTTP_Request2 adapters
    4  *
    5  * PHP version 5
    6  *
    7  * LICENSE
    8  *
    9  * This source file is subject to BSD 3-Clause License that is bundled
   10  * with this package in the file LICENSE and available at the URL
   11  * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
   12  *
   13  * @category  HTTP
   14  * @package   HTTP_Request2
   15  * @author    Alexey Borzov <avb@php.net>
   16  * @copyright 2008-2016 Alexey Borzov <avb@php.net>
   17  * @license   http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
   18  * @link      http://pear.php.net/package/HTTP_Request2
   19  */
   20 
   21 /**
   22  * Class representing a HTTP response
   23  */
   24 require_once 'HTTP/Request2/Response.php';
   25 
   26 /**
   27  * Base class for HTTP_Request2 adapters
   28  *
   29  * HTTP_Request2 class itself only defines methods for aggregating the request
   30  * data, all actual work of sending the request to the remote server and
   31  * receiving its response is performed by adapters.
   32  *
   33  * @category HTTP
   34  * @package  HTTP_Request2
   35  * @author   Alexey Borzov <avb@php.net>
   36  * @license  http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
   37  * @version  Release: 2.3.0
   38  * @link     http://pear.php.net/package/HTTP_Request2
   39  */
   40 abstract class HTTP_Request2_Adapter
   41 {
   42     /**
   43      * A list of methods that MUST NOT have a request body, per RFC 2616
   44      * @var  array
   45      */
   46     protected static $bodyDisallowed = array('TRACE');
   47 
   48     /**
   49      * Methods having defined semantics for request body
   50      *
   51      * Content-Length header (indicating that the body follows, section 4.3 of
   52      * RFC 2616) will be sent for these methods even if no body was added
   53      *
   54      * @var  array
   55      * @link http://pear.php.net/bugs/bug.php?id=12900
   56      * @link http://pear.php.net/bugs/bug.php?id=14740
   57      */
   58     protected static $bodyRequired = array('POST', 'PUT');
   59 
   60     /**
   61      * Request being sent
   62      * @var  HTTP_Request2
   63      */
   64     protected $request;
   65 
   66     /**
   67      * Request body
   68      * @var  string|resource|HTTP_Request2_MultipartBody
   69      * @see  HTTP_Request2::getBody()
   70      */
   71     protected $requestBody;
   72 
   73     /**
   74      * Length of the request body
   75      * @var  integer
   76      */
   77     protected $contentLength;
   78 
   79     /**
   80      * Sends request to the remote server and returns its response
   81      *
   82      * @param HTTP_Request2 $request HTTP request message
   83      *
   84      * @return   HTTP_Request2_Response
   85      * @throws   HTTP_Request2_Exception
   86      */
   87     abstract public function sendRequest(HTTP_Request2 $request);
   88 
   89     /**
   90      * Calculates length of the request body, adds proper headers
   91      *
   92      * @param array &$headers associative array of request headers, this method
   93      *                        will add proper 'Content-Length' and 'Content-Type'
   94      *                        headers to this array (or remove them if not needed)
   95      */
   96     protected function calculateRequestLength(&$headers)
   97     {
   98         $this->requestBody = $this->request->getBody();
   99 
  100         if (is_string($this->requestBody)) {
  101             $this->contentLength = strlen($this->requestBody);
  102         } elseif (is_resource($this->requestBody)) {
  103             $stat = fstat($this->requestBody);
  104             $this->contentLength = $stat['size'];
  105             rewind($this->requestBody);
  106         } else {
  107             $this->contentLength = $this->requestBody->getLength();
  108             $headers['content-type'] = 'multipart/form-data; boundary=' .
  109                                        $this->requestBody->getBoundary();
  110             $this->requestBody->rewind();
  111         }
  112 
  113         if (in_array($this->request->getMethod(), self::$bodyDisallowed)
  114             || 0 == $this->contentLength
  115         ) {
  116             // No body: send a Content-Length header nonetheless (request #12900),
  117             // but do that only for methods that require a body (bug #14740)
  118             if (in_array($this->request->getMethod(), self::$bodyRequired)) {
  119                 $headers['content-length'] = 0;
  120             } else {
  121                 unset($headers['content-length']);
  122                 // if the method doesn't require a body and doesn't have a
  123                 // body, don't send a Content-Type header. (request #16799)
  124                 unset($headers['content-type']);
  125             }
  126         } else {
  127             if (empty($headers['content-type'])) {
  128                 $headers['content-type'] = 'application/x-www-form-urlencoded';
  129             }
  130             // Content-Length should not be sent for chunked Transfer-Encoding (bug #20125)
  131             if (!isset($headers['transfer-encoding'])) {
  132                 $headers['content-length'] = $this->contentLength;
  133             }
  134         }
  135     }
  136 }
  137 ?>