Request.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Http\Client;
  15. use Cake\Core\Exception\Exception;
  16. use Psr\Http\Message\RequestInterface;
  17. use Zend\Diactoros\RequestTrait;
  18. use Zend\Diactoros\Stream;
  19. /**
  20. * Implements methods for HTTP requests.
  21. *
  22. * Used by Cake\Http\Client to contain request information
  23. * for making requests.
  24. */
  25. class Request extends Message implements RequestInterface
  26. {
  27. use RequestTrait;
  28. /**
  29. * Constructor
  30. *
  31. * Provides backwards compatible defaults for some properties.
  32. *
  33. * @param string $url The request URL
  34. * @param string $method The HTTP method to use.
  35. * @param array $headers The HTTP headers to set.
  36. * @param array|string|null $data The request body to use.
  37. */
  38. public function __construct($url = '', $method = self::METHOD_GET, array $headers = [], $data = null)
  39. {
  40. $this->validateMethod($method);
  41. $this->method = $method;
  42. $this->uri = $this->createUri($url);
  43. $headers += [
  44. 'Connection' => 'close',
  45. 'User-Agent' => 'CakePHP'
  46. ];
  47. $this->addHeaders($headers);
  48. $this->body($data);
  49. }
  50. /**
  51. * Get/Set the HTTP method.
  52. *
  53. * *Warning* This method mutates the request in-place for backwards
  54. * compatibility reasons, and is not part of the PSR7 interface.
  55. *
  56. * @param string|null $method The method for the request.
  57. * @return $this|string Either this or the current method.
  58. * @throws \Cake\Core\Exception\Exception On invalid methods.
  59. * @deprecated 3.3.0 Use getMethod() and withMethod() instead.
  60. */
  61. public function method($method = null)
  62. {
  63. if ($method === null) {
  64. return $this->method;
  65. }
  66. $name = get_called_class() . '::METHOD_' . strtoupper($method);
  67. if (!defined($name)) {
  68. throw new Exception('Invalid method type');
  69. }
  70. $this->method = $method;
  71. return $this;
  72. }
  73. /**
  74. * Get/Set the url for the request.
  75. *
  76. * *Warning* This method mutates the request in-place for backwards
  77. * compatibility reasons, and is not part of the PSR7 interface.
  78. *
  79. * @param string|null $url The url for the request. Leave null for get
  80. * @return $this|string Either $this or the url value.
  81. * @deprecated 3.3.0 Use getUri() and withUri() instead.
  82. */
  83. public function url($url = null)
  84. {
  85. if ($url === null) {
  86. return '' . $this->getUri();
  87. }
  88. $this->uri = $this->createUri($url);
  89. return $this;
  90. }
  91. /**
  92. * Get/Set headers into the request.
  93. *
  94. * You can get the value of a header, or set one/many headers.
  95. * Headers are set / fetched in a case insensitive way.
  96. *
  97. * ### Getting headers
  98. *
  99. * ```
  100. * $request->header('Content-Type');
  101. * ```
  102. *
  103. * ### Setting one header
  104. *
  105. * ```
  106. * $request->header('Content-Type', 'application/json');
  107. * ```
  108. *
  109. * ### Setting multiple headers
  110. *
  111. * ```
  112. * $request->header(['Connection' => 'close', 'User-Agent' => 'CakePHP']);
  113. * ```
  114. *
  115. * *Warning* This method mutates the request in-place for backwards
  116. * compatibility reasons, and is not part of the PSR7 interface.
  117. *
  118. * @param string|array|null $name The name to get, or array of multiple values to set.
  119. * @param string|null $value The value to set for the header.
  120. * @return mixed Either $this when setting or header value when getting.
  121. * @deprecated 3.3.0 Use withHeader() and getHeaderLine() instead.
  122. */
  123. public function header($name = null, $value = null)
  124. {
  125. if ($value === null && is_string($name)) {
  126. $val = $this->getHeaderLine($name);
  127. if ($val === '') {
  128. return null;
  129. }
  130. return $val;
  131. }
  132. if ($value !== null && !is_array($name)) {
  133. $name = [$name => $value];
  134. }
  135. $this->addHeaders($name);
  136. return $this;
  137. }
  138. /**
  139. * Add an array of headers to the request.
  140. *
  141. * @param array $headers The headers to add.
  142. * @return void
  143. */
  144. protected function addHeaders($headers)
  145. {
  146. foreach ($headers as $key => $val) {
  147. $normalized = strtolower($key);
  148. $this->headers[$key] = (array)$val;
  149. $this->headerNames[$normalized] = $key;
  150. }
  151. }
  152. /**
  153. * Get/Set cookie values.
  154. *
  155. * ### Getting a cookie
  156. *
  157. * ```
  158. * $request->cookie('session');
  159. * ```
  160. *
  161. * ### Setting one cookie
  162. *
  163. * ```
  164. * $request->cookie('session', '123456');
  165. * ```
  166. *
  167. * ### Setting multiple headers
  168. *
  169. * ```
  170. * $request->cookie(['test' => 'value', 'split' => 'banana']);
  171. * ```
  172. *
  173. * @param string $name The name of the cookie to get/set
  174. * @param string|null $value Either the value or null when getting values.
  175. * @return mixed Either $this or the cookie value.
  176. */
  177. public function cookie($name, $value = null)
  178. {
  179. if ($value === null && is_string($name)) {
  180. return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
  181. }
  182. if (is_string($name) && is_string($value)) {
  183. $name = [$name => $value];
  184. }
  185. foreach ($name as $key => $val) {
  186. $this->_cookies[$key] = $val;
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Get/Set HTTP version.
  192. *
  193. * *Warning* This method mutates the request in-place for backwards
  194. * compatibility reasons, and is not part of the PSR7 interface.
  195. *
  196. * @param string|null $version The HTTP version.
  197. * @return $this|string Either $this or the HTTP version.
  198. * @deprecated 3.3.0 Use getProtocolVersion() and withProtocolVersion() instead.
  199. */
  200. public function version($version = null)
  201. {
  202. if ($version === null) {
  203. return $this->protocol;
  204. }
  205. $this->protocol = $version;
  206. return $this;
  207. }
  208. /**
  209. * Get/set the body/payload for the message.
  210. *
  211. * Array data will be serialized with Cake\Http\FormData,
  212. * and the content-type will be set.
  213. *
  214. * @param string|array|null $body The body for the request. Leave null for get
  215. * @return mixed Either $this or the body value.
  216. */
  217. public function body($body = null)
  218. {
  219. if ($body === null) {
  220. $body = $this->getBody();
  221. return $body ? $body->__toString() : '';
  222. }
  223. if (is_array($body)) {
  224. $formData = new FormData();
  225. $formData->addMany($body);
  226. $this->header('Content-Type', $formData->contentType());
  227. $body = (string)$formData;
  228. }
  229. $stream = new Stream('php://memory', 'rw');
  230. $stream->write($body);
  231. $this->stream = $stream;
  232. return $this;
  233. }
  234. }
  235. // @deprecated Add backwards compat alias.
  236. class_alias('Cake\Http\Client\Request', 'Cake\Network\Http\Request');