Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractMessage
  • EntityEnclosingRequest
  • Header
  • HeaderComparison
  • PostFile
  • Request
  • RequestFactory
  • Response

Interfaces

  • EntityEnclosingRequestInterface
  • MessageInterface
  • PostFileInterface
  • RequestFactoryInterface
  • RequestInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Http\Message;
  4: 
  5: use Guzzle\Common\Collection;
  6: use Guzzle\Http\EntityBody;
  7: use Guzzle\Http\Url;
  8: use Guzzle\Parser\ParserRegistry;
  9: 
 10: /**
 11:  * Default HTTP request factory used to create the default {@see Request} and {@see EntityEnclosingRequest} objects.
 12:  */
 13: class RequestFactory implements RequestFactoryInterface
 14: {
 15:     /**
 16:      * @var RequestFactory Singleton instance of the default request factory
 17:      */
 18:     protected static $instance;
 19: 
 20:     /**
 21:      * @var string Class to instantiate for requests with no body
 22:      */
 23:     protected $requestClass = 'Guzzle\\Http\\Message\\Request';
 24: 
 25:     /**
 26:      * @var string Class to instantiate for requests with a body
 27:      */
 28:     protected $entityEnclosingRequestClass = 'Guzzle\\Http\\Message\\EntityEnclosingRequest';
 29: 
 30:     /**
 31:      * Get a cached instance of the default request factory
 32:      *
 33:      * @return RequestFactory
 34:      */
 35:     public static function getInstance()
 36:     {
 37:         // @codeCoverageIgnoreStart
 38:         if (!static::$instance) {
 39:             static::$instance = new static();
 40:         }
 41:         // @codeCoverageIgnoreEnd
 42: 
 43:         return static::$instance;
 44:     }
 45: 
 46:     /**
 47:      * {@inheritdoc}
 48:      */
 49:     public function fromMessage($message)
 50:     {
 51:         $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($message);
 52: 
 53:         if (!$parsed) {
 54:             return false;
 55:         }
 56: 
 57:         $request = $this->fromParts($parsed['method'], $parsed['request_url'],
 58:             $parsed['headers'], $parsed['body'], $parsed['protocol'],
 59:             $parsed['version']);
 60: 
 61:         // EntityEnclosingRequest adds an "Expect: 100-Continue" header when using a raw request body for PUT or POST
 62:         // requests. This factory method should accurately reflect the message, so here we are removing the Expect
 63:         // header if one was not supplied in the message.
 64:         if (!isset($parsed['headers']['Expect']) && !isset($parsed['headers']['expect'])) {
 65:             $request->removeHeader('Expect');
 66:         }
 67: 
 68:         return $request;
 69:     }
 70: 
 71:     /**
 72:      * {@inheritdoc}
 73:      */
 74:     public function fromParts(
 75:         $method,
 76:         array $urlParts,
 77:         $headers = null,
 78:         $body = null,
 79:         $protocol = 'HTTP',
 80:         $protocolVersion = '1.1'
 81:     ) {
 82:         return $this->create($method, Url::buildUrl($urlParts, true), $headers, $body)
 83:                     ->setProtocolVersion($protocolVersion);
 84:     }
 85: 
 86:     /**
 87:      * {@inheritdoc}
 88:      */
 89:     public function create($method, $url, $headers = null, $body = null)
 90:     {
 91:         $method = strtoupper($method);
 92: 
 93:         if ($method == 'GET' || $method == 'HEAD' || $method == 'TRACE' || $method == 'OPTIONS') {
 94:             // Handle non-entity-enclosing request methods
 95:             $request = new $this->requestClass($method, $url, $headers);
 96:             if ($body) {
 97:                 // The body is where the response body will be stored
 98:                 $type = gettype($body);
 99:                 if ($type == 'string' || $type == 'resource' || $type == 'object') {
100:                     $request->setResponseBody($body);
101:                 }
102:             }
103:             return $request;
104:         }
105: 
106:         // Create an entity enclosing request by default
107:         $request = new $this->entityEnclosingRequestClass($method, $url, $headers);
108: 
109:         if ($body) {
110:             // Add POST fields and files to an entity enclosing request if an array is used
111:             if (is_array($body) || $body instanceof Collection) {
112:                 // Normalize PHP style cURL uploads with a leading '@' symbol
113:                 foreach ($body as $key => $value) {
114:                     if (is_string($value) && substr($value, 0, 1) == '@') {
115:                         $request->addPostFile($key, $value);
116:                         unset($body[$key]);
117:                     }
118:                 }
119:                 // Add the fields if they are still present and not all files
120:                 $request->addPostFields($body);
121:             } else {
122:                 // Add a raw entity body body to the request
123:                 $request->setBody(
124:                     $body,
125:                     (string) $request->getHeader('Content-Type'),
126:                     (string) $request->getHeader('Transfer-Encoding') == 'chunked'
127:                 );
128:             }
129:         }
130: 
131:         return $request;
132:     }
133: 
134:     /**
135:      * Clone a request while changing the method. Emulates the behavior of
136:      * {@see Guzzle\Http\Message\Request::clone}, but can change the HTTP method.
137:      *
138:      * @param RequestInterface $request Request to clone
139:      * @param string           $method  Method to set
140:      *
141:      * @return RequestInterface
142:      */
143:     public function cloneRequestWithMethod(RequestInterface $request, $method)
144:     {
145:         // Create the request with the same client if possible
146:         if ($client = $request->getClient()) {
147:             $cloned = $request->getClient()->createRequest($method, $request->getUrl(), $request->getHeaders());
148:         } else {
149:             $cloned = $this->create($method, $request->getUrl(), $request->getHeaders());
150:         }
151: 
152:         $cloned->getCurlOptions()->replace($request->getCurlOptions()->getAll());
153:         $cloned->setEventDispatcher(clone $request->getEventDispatcher());
154:         // Ensure that that the Content-Length header is not copied if changing to GET or HEAD
155:         if (!($cloned instanceof EntityEnclosingRequestInterface)) {
156:             $cloned->removeHeader('Content-Length');
157:         } elseif ($request instanceof EntityEnclosingRequestInterface) {
158:             $cloned->setBody($request->getBody());
159:         }
160:         $cloned->getParams()
161:             ->replace($request->getParams()->getAll())
162:             ->remove('curl_handle')
163:             ->remove('curl_multi');
164: 
165:         return $cloned;
166:     }
167: }
168: 
php-coveralls API documentation generated by ApiGen 2.8.0