Drupal investigation

ServerRequest.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use InvalidArgumentException;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Psr\Http\Message\UriInterface;
  6. use Psr\Http\Message\StreamInterface;
  7. use Psr\Http\Message\UploadedFileInterface;
  8. /**
  9. * Server-side HTTP request
  10. *
  11. * Extends the Request definition to add methods for accessing incoming data,
  12. * specifically server parameters, cookies, matched path parameters, query
  13. * string arguments, body parameters, and upload file information.
  14. *
  15. * "Attributes" are discovered via decomposing the request (and usually
  16. * specifically the URI path), and typically will be injected by the application.
  17. *
  18. * Requests are considered immutable; all methods that might change state are
  19. * implemented such that they retain the internal state of the current
  20. * message and return a new instance that contains the changed state.
  21. */
  22. class ServerRequest extends Request implements ServerRequestInterface
  23. {
  24. /**
  25. * @var array
  26. */
  27. private $attributes = [];
  28. /**
  29. * @var array
  30. */
  31. private $cookieParams = [];
  32. /**
  33. * @var null|array|object
  34. */
  35. private $parsedBody;
  36. /**
  37. * @var array
  38. */
  39. private $queryParams = [];
  40. /**
  41. * @var array
  42. */
  43. private $serverParams;
  44. /**
  45. * @var array
  46. */
  47. private $uploadedFiles = [];
  48. /**
  49. * @param string $method HTTP method
  50. * @param string|UriInterface $uri URI
  51. * @param array $headers Request headers
  52. * @param string|null|resource|StreamInterface $body Request body
  53. * @param string $version Protocol version
  54. * @param array $serverParams Typically the $_SERVER superglobal
  55. */
  56. public function __construct(
  57. $method,
  58. $uri,
  59. array $headers = [],
  60. $body = null,
  61. $version = '1.1',
  62. array $serverParams = []
  63. ) {
  64. $this->serverParams = $serverParams;
  65. parent::__construct($method, $uri, $headers, $body, $version);
  66. }
  67. /**
  68. * Return an UploadedFile instance array.
  69. *
  70. * @param array $files A array which respect $_FILES structure
  71. * @throws InvalidArgumentException for unrecognized values
  72. * @return array
  73. */
  74. public static function normalizeFiles(array $files)
  75. {
  76. $normalized = [];
  77. foreach ($files as $key => $value) {
  78. if ($value instanceof UploadedFileInterface) {
  79. $normalized[$key] = $value;
  80. } elseif (is_array($value) && isset($value['tmp_name'])) {
  81. $normalized[$key] = self::createUploadedFileFromSpec($value);
  82. } elseif (is_array($value)) {
  83. $normalized[$key] = self::normalizeFiles($value);
  84. continue;
  85. } else {
  86. throw new InvalidArgumentException('Invalid value in files specification');
  87. }
  88. }
  89. return $normalized;
  90. }
  91. /**
  92. * Create and return an UploadedFile instance from a $_FILES specification.
  93. *
  94. * If the specification represents an array of values, this method will
  95. * delegate to normalizeNestedFileSpec() and return that return value.
  96. *
  97. * @param array $value $_FILES struct
  98. * @return array|UploadedFileInterface
  99. */
  100. private static function createUploadedFileFromSpec(array $value)
  101. {
  102. if (is_array($value['tmp_name'])) {
  103. return self::normalizeNestedFileSpec($value);
  104. }
  105. return new UploadedFile(
  106. $value['tmp_name'],
  107. (int) $value['size'],
  108. (int) $value['error'],
  109. $value['name'],
  110. $value['type']
  111. );
  112. }
  113. /**
  114. * Normalize an array of file specifications.
  115. *
  116. * Loops through all nested files and returns a normalized array of
  117. * UploadedFileInterface instances.
  118. *
  119. * @param array $files
  120. * @return UploadedFileInterface[]
  121. */
  122. private static function normalizeNestedFileSpec(array $files = [])
  123. {
  124. $normalizedFiles = [];
  125. foreach (array_keys($files['tmp_name']) as $key) {
  126. $spec = [
  127. 'tmp_name' => $files['tmp_name'][$key],
  128. 'size' => $files['size'][$key],
  129. 'error' => $files['error'][$key],
  130. 'name' => $files['name'][$key],
  131. 'type' => $files['type'][$key],
  132. ];
  133. $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
  134. }
  135. return $normalizedFiles;
  136. }
  137. /**
  138. * Return a ServerRequest populated with superglobals:
  139. * $_GET
  140. * $_POST
  141. * $_COOKIE
  142. * $_FILES
  143. * $_SERVER
  144. *
  145. * @return ServerRequestInterface
  146. */
  147. public static function fromGlobals()
  148. {
  149. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
  150. $headers = function_exists('getallheaders') ? getallheaders() : [];
  151. $uri = self::getUriFromGlobals();
  152. $body = new LazyOpenStream('php://input', 'r+');
  153. $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
  154. $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
  155. return $serverRequest
  156. ->withCookieParams($_COOKIE)
  157. ->withQueryParams($_GET)
  158. ->withParsedBody($_POST)
  159. ->withUploadedFiles(self::normalizeFiles($_FILES));
  160. }
  161. /**
  162. * Get a Uri populated with values from $_SERVER.
  163. *
  164. * @return UriInterface
  165. */
  166. public static function getUriFromGlobals() {
  167. $uri = new Uri('');
  168. $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
  169. $hasPort = false;
  170. if (isset($_SERVER['HTTP_HOST'])) {
  171. $hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']);
  172. $uri = $uri->withHost($hostHeaderParts[0]);
  173. if (isset($hostHeaderParts[1])) {
  174. $hasPort = true;
  175. $uri = $uri->withPort($hostHeaderParts[1]);
  176. }
  177. } elseif (isset($_SERVER['SERVER_NAME'])) {
  178. $uri = $uri->withHost($_SERVER['SERVER_NAME']);
  179. } elseif (isset($_SERVER['SERVER_ADDR'])) {
  180. $uri = $uri->withHost($_SERVER['SERVER_ADDR']);
  181. }
  182. if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
  183. $uri = $uri->withPort($_SERVER['SERVER_PORT']);
  184. }
  185. $hasQuery = false;
  186. if (isset($_SERVER['REQUEST_URI'])) {
  187. $requestUriParts = explode('?', $_SERVER['REQUEST_URI']);
  188. $uri = $uri->withPath($requestUriParts[0]);
  189. if (isset($requestUriParts[1])) {
  190. $hasQuery = true;
  191. $uri = $uri->withQuery($requestUriParts[1]);
  192. }
  193. }
  194. if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
  195. $uri = $uri->withQuery($_SERVER['QUERY_STRING']);
  196. }
  197. return $uri;
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getServerParams()
  203. {
  204. return $this->serverParams;
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function getUploadedFiles()
  210. {
  211. return $this->uploadedFiles;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function withUploadedFiles(array $uploadedFiles)
  217. {
  218. $new = clone $this;
  219. $new->uploadedFiles = $uploadedFiles;
  220. return $new;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function getCookieParams()
  226. {
  227. return $this->cookieParams;
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function withCookieParams(array $cookies)
  233. {
  234. $new = clone $this;
  235. $new->cookieParams = $cookies;
  236. return $new;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getQueryParams()
  242. {
  243. return $this->queryParams;
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function withQueryParams(array $query)
  249. {
  250. $new = clone $this;
  251. $new->queryParams = $query;
  252. return $new;
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function getParsedBody()
  258. {
  259. return $this->parsedBody;
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function withParsedBody($data)
  265. {
  266. $new = clone $this;
  267. $new->parsedBody = $data;
  268. return $new;
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function getAttributes()
  274. {
  275. return $this->attributes;
  276. }
  277. /**
  278. * {@inheritdoc}
  279. */
  280. public function getAttribute($attribute, $default = null)
  281. {
  282. if (false === array_key_exists($attribute, $this->attributes)) {
  283. return $default;
  284. }
  285. return $this->attributes[$attribute];
  286. }
  287. /**
  288. * {@inheritdoc}
  289. */
  290. public function withAttribute($attribute, $value)
  291. {
  292. $new = clone $this;
  293. $new->attributes[$attribute] = $value;
  294. return $new;
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function withoutAttribute($attribute)
  300. {
  301. if (false === array_key_exists($attribute, $this->attributes)) {
  302. return $this;
  303. }
  304. $new = clone $this;
  305. unset($new->attributes[$attribute]);
  306. return $new;
  307. }
  308. }